view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsFinder.java @ 8956:ee5ce13016ed

Work on SINFO-Fließtiefenentwicklung
author gernotbelger
date Tue, 20 Mar 2018 13:30:07 +0100
parents 798d9dcbccdd
children 45f1ad66560e
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.artifacts.CallContext;
import org.dive4elements.river.artifacts.BedHeightsArtifact;
import org.dive4elements.river.artifacts.math.Linear;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo;
import org.dive4elements.river.model.BedHeight;
import org.dive4elements.river.model.BedHeightValue;
import org.dive4elements.river.utils.RiverUtils;

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

    private final BedHeightInfo info;

    private final NavigableMap<Double, BedHeightValue> values;

    private double meanBedHeight;

    private double minBedHeight;

    private double maxBedHeight;

    /**
     * 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;
    }

    public static BedHeightsFinder forId(final CallContext context, final String soundingId, final DoubleRange calcRange, final Calculation problems) {

        // REMARK: absolutely unbelievable....
        // The way how bed-heights (and other data too) is accessed is different for nearly every calculation-type
        // throughout flys.
        // The knowledge on how to parse the datacage-ids is spread through the complete code-base...

        // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but
        // this is plain random
        final String[] parts = soundingId.split(";");

        final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], context);

        final Integer bedheightId = artifact.getDataAsInteger("height_id");

        // REMARK: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the
        // other type means)
        // Luckily, the requirement is to only access 'single' data here.
        // final String bedheightType = artifact.getDataAsString("type");

        // REMARK: BedDifferences uses this, but we also need the metadata of the BedHeight
        // REMARK: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via
        // hibernate stuff
        // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes.
        // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to);

        final BedHeightsFinder bedHeight = bedheightId == null ? null : BedHeightsFinder.forId(bedheightId, calcRange);
        if (bedHeight != null)
            return bedHeight;

        // FIXME: 10n
        problems.addProblem("Failed to access sounding with id '{0}'", soundingId);
        return null;
    }

    /**
     * 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.
     */
    private 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) {
        getBedHeights(km);
        return this.meanBedHeight;
    }

    public double getMinBedHeight(final double km) {
        getBedHeights(km);
        return this.minBedHeight;
    }

    public double getMaxBedHeight(final double km) {
        getBedHeights(km);
        return this.maxBedHeight;
    }

    private boolean getBedHeights(final double km) {
        if (this.values.containsKey(km)) {
            this.meanBedHeight = (this.values.get(km).getHeight() != null) ? this.values.get(km).getHeight().doubleValue() : Double.NaN;
            this.minBedHeight = (this.values.get(km).getMinHeight() != null) ? this.values.get(km).getMinHeight().doubleValue() : Double.NaN;
            this.maxBedHeight = (this.values.get(km).getMaxHeight() != null) ? this.values.get(km).getMaxHeight().doubleValue() : Double.NaN;
            return true;
        }

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

        if (floorEntry == null || ceilingEntry == null) {
            this.meanBedHeight = Double.NaN;
            this.minBedHeight = Double.NaN;
            this.maxBedHeight = Double.NaN;
            return false;
        }

        final double floorKm = floorEntry.getKey().doubleValue();
        final double ceilKm = ceilingEntry.getKey().doubleValue();

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

        this.meanBedHeight = interpolate(km, floorKm, ceilKm, floorEntry.getValue().getHeight(), ceilingEntry.getValue().getHeight());
        this.minBedHeight = interpolate(km, floorKm, ceilKm, floorEntry.getValue().getMinHeight(), ceilingEntry.getValue().getMinHeight());
        this.maxBedHeight = interpolate(km, floorKm, ceilKm, floorEntry.getValue().getMaxHeight(), ceilingEntry.getValue().getMaxHeight());
        return true;
    }

    private double interpolate(final double km, final double floorKm, final double ceilKm, final Double floorHeight, final Double ceilHeight) {
        if ((floorHeight != null) && (ceilHeight != null))
            return Linear.linear(km, floorKm, ceilKm, floorHeight, ceilHeight);
        else
            return Double.NaN;
    }
}

http://dive4elements.wald.intevation.org