view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsFinder.java @ 8942:11bf13cf0463

Minor changes to tkh calculation. Loading default bed heights form config file.
author gernotbelger
date Fri, 09 Mar 2018 18:47:06 +0100
parents d9dbf0b74bc2
children 5d5d482da3e9
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.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.apache.commons.lang.math.DoubleRange;
import org.dive4elements.river.artifacts.math.Linear;
import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo;
import org.dive4elements.river.model.BedHeight;
import org.dive4elements.river.model.BedHeightValue;

/**
 * Provides bed heigts for vcarious calculations.
 *
 * @author Gernot Belger
 */
public final class BedHeightsFinder {

    private final BedHeightInfo info;

    private final NavigableMap<Double, BedHeightValue> values;

    /**
     * Create bed height finders from a collection of bed heights.
     */
    public static Collection<BedHeightsFinder> createTkhBedHeights(final DoubleRange range, final Collection<BedHeight> bedHeights) {
        final List<BedHeightsFinder> result = new ArrayList<>(bedHeights.size());

        for (final BedHeight bedHeight : bedHeights) {
            final BedHeightsFinder finder = createBedHeights(bedHeight, range);
            result.add(finder);
        }

        return result;
    }

    /**
     * Creates a {@link BedHeightsFinder} for a dataset from the database, specified by its id.
     *
     * @return <code>null</code> if no bed height with the given id exists.
     */
    public static BedHeightsFinder forId(final int id, final DoubleRange range) {

        final BedHeight bedHeight = BedHeight.getBedHeightById(id);
        if (bedHeight == null)
            return null;

        return BedHeightsFinder.createBedHeights(bedHeight, range);
    }

    /**
     * Create a finder for a given bed height.
     *
     * @param range
     */
    private static BedHeightsFinder createBedHeights(final BedHeight bedHeight, final DoubleRange range) {

        // FIXME: sort by station, but in what direction?
        // FIXME: using river.getKmUp()?
        final NavigableMap<Double, BedHeightValue> values = new TreeMap<>();

        for (final BedHeightValue bedHeightValue : bedHeight.getValues()) {
            final Double station = bedHeightValue.getStation();
            if (station != null && range.containsDouble(station)) {

                if (bedHeightValue.getHeight() != null)
                    values.put(station, bedHeightValue);
            }
        }

        final BedHeightInfo info = BedHeightInfo.from(bedHeight);

        return new BedHeightsFinder(info, values);
    }

    private BedHeightsFinder(final BedHeightInfo info, final NavigableMap<Double, BedHeightValue> values) {
        this.info = info;
        this.values = values;
    }

    public BedHeightInfo getInfo() {
        return this.info;
    }

    public Collection<Double> getStations() {
        return this.values.keySet();
    }

    public double getMeanBedHeight(final double km) {

        if (this.values.containsKey(km))
            return this.values.get(km).getHeight();

        final Entry<Double, BedHeightValue> floorEntry = this.values.floorEntry(km);
        final Entry<Double, BedHeightValue> ceilingEntry = this.values.ceilingEntry(km);

        if (floorEntry == null || ceilingEntry == null)
            return Double.NaN;

        final double floorKm = floorEntry.getKey();
        final double floorHeight = floorEntry.getValue().getHeight();
        final double ceilKm = ceilingEntry.getKey();
        final double ceilHeight = ceilingEntry.getValue().getHeight();

        // FIXME: check if we always want that...

        return Linear.linear(km, floorKm, ceilKm, floorHeight, ceilHeight);
    }
}

http://dive4elements.wald.intevation.org