view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsUtils.java @ 9573:b9c87bbff6a4

mean bed height -> mean bed LEVEL
author gernotbelger
date Tue, 06 Nov 2018 10:56:22 +0100
parents 8e100593aec3
children
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.sinfo.tkhstate;

import java.util.Collection;
import java.util.TreeSet;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.math.DoubleRange;

/**
 * Helper functions for bed level calculations
 */
public final class BedHeightsUtils {

    /**
     * Get a union of all stations in a collection of bed levels
     */
    public static double[] extractStations(final Collection<BedHeightsFinder> bedHeightFinders) {

        return extractStations(bedHeightFinders, false);
    }

    /**
     * Get a union of all stations in a collection of bed levels
     */
    public static double[] extractStations(final Collection<BedHeightsFinder> bedHeightFinders, final boolean minMaxAware) {

        final Collection<Double> allStations = extractStationCollection(bedHeightFinders, minMaxAware);
        return ArrayUtils.toPrimitive(allStations.toArray(new Double[allStations.size()]));
    }

    /**
     * Get a union of all stations in a collection of bed levels
     */
    public static Collection<Double> extractStationCollection(final Collection<BedHeightsFinder> bedHeightFinders, final boolean minMaxAware) {

        if (!minMaxAware)
            return extractStationCollection(bedHeightFinders);

        final DoubleRange kmRange = extractMinimumStationRange(bedHeightFinders);

        final Collection<Double> allStations = new TreeSet<>();
        for (final BedHeightsFinder bhf : bedHeightFinders) {
            for (final Double station : bhf.getStations())
                if ((station.doubleValue() >= kmRange.getMinimumDouble() - 0.0001) && (station.doubleValue() <= kmRange.getMaximumDouble() + 0.0001))
                    allStations.add(station.doubleValue());
        }
        return allStations;
    }

    /**
     * Get a union of all stations in a collection of bed levels
     */
    public static Collection<Double> extractStationCollection(final Collection<BedHeightsFinder> bedHeights) {

        final Collection<Double> allStations = new TreeSet<>();

        for (final BedHeightsFinder bedHeight : bedHeights)
            allStations.addAll(bedHeight.getStations());

        return allStations;
    }

    /**
     * Intersects all km ranges of a collection of bed levels
     */
    public static DoubleRange extractMinimumStationRange(final Collection<BedHeightsFinder> bedHeightFinders) {
        DoubleRange kmRange = null;
        for (final BedHeightsFinder bhf : bedHeightFinders)
            kmRange = intersectRanges(kmRange, bhf.getKmRange());
        return new DoubleRange(kmRange.getMinimumDouble() - 0.0001, kmRange.getMaximumDouble() + 0.0001);
    }

    /**
     * Creates a range with the minimum overlapping of two ranges
     */
    public static DoubleRange intersectRanges(final DoubleRange a, final DoubleRange b) {
        if (a == null)
            return new DoubleRange(b.getMinimumDouble(), b.getMaximumDouble());
        if (b == null)
            return new DoubleRange(a.getMinimumDouble(), a.getMaximumDouble());
        return new DoubleRange(Math.max(a.getMinimumDouble(), b.getMinimumDouble()), Math.min(a.getMaximumDouble(), b.getMaximumDouble()));
    }
}

http://dive4elements.wald.intevation.org