view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsFinder.java @ 9597:5395c6d4ca50

Softwaretests...20181219 7.3: no interpolation of missing bed heights for Uinfo/Salix historical scenario and B&U/Bzws
author mschaefer
date Tue, 05 Feb 2019 15:47:58 +0100
parents b9c87bbff6a4
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.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.model.BedHeightValueType;
import org.dive4elements.river.utils.RiverUtils;

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

    private static double MAX_DISTANCE_KM = 1;

    private final BedHeightInfo info;

    private final NavigableMap<Double, BedHeightValue> values;

    private Calculation problems;

    private final boolean isNull;

    private final boolean doInterpolate;

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

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

        return result;
    }

    /**
     * Create not-interpolated bed level finders from a collection of bed levels.
     */
    public static Collection<BedHeightsFinder> createScenarioBedHeights(final Calculation problems, final DoubleRange range,
            final Collection<BedHeight> bedHeights) {
        final List<BedHeightsFinder> result = new ArrayList<>(bedHeights.size());

        for (final BedHeight bedHeight : bedHeights) {
            final BedHeightsFinder finder = createBedHeights(problems, bedHeight, range, false);
            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(problems, bedheightId, calcRange);
        if (bedHeight == null) {
            problems.addProblem("sinfo.bedheightsfinder.notfound", soundingId);
            return null;
        }

        if (bedHeight.isEmpty()) {
            problems.addProblem("sinfo.bedheightsfinder.empty");
            return null;
        }

        return bedHeight;
    }

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

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

        return BedHeightsFinder.createBedHeights(problems, bedHeight, range, true);
    }

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

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

        return BedHeightsFinder.createBedHeights(problems, bedHeight, range, doInterpolate);
    }

    /**
     * Creates a {@link BedHeightsFinder} that returns always NaN heights
     */
    public static BedHeightsFinder NullFinder() {
        final NavigableMap<Double, BedHeightValue> values = new TreeMap<>();
        return new BedHeightsFinder(null, null, values, true, false);
    }

    /**
     * Create a finder for a given bed level.
     *
     */
    private static BedHeightsFinder createBedHeights(final Calculation problems, final BedHeight bedHeight, final DoubleRange range,
            final boolean doInterpolate) {

        // 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(problems, info, values, false, doInterpolate);
    }

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

    /**
     * Whether this is a null (always NaN) finder.
     *
     * @return
     */
    public boolean isNull() {
        return this.isNull;
    }

    public boolean isEmpty() {
        return this.values.isEmpty();
    }

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

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

    public DoubleRange getKmRange() {
        if (this.values.isEmpty())
            return null;
        return new DoubleRange(this.values.firstKey().doubleValue(), this.values.lastKey().doubleValue());
    }

    public double getMeanBedHeight(final double km) {
        return interpolateBedHeights(km, BedHeightValueType.value);
    }

    public double getMinBedHeight(final double km) {
        return interpolateBedHeights(km, BedHeightValueType.min);
    }

    public double getMaxBedHeight(final double km) {
        return interpolateBedHeights(km, BedHeightValueType.max);
    }

    public double getFieldHeight(final double km, final int index) {
        return interpolateBedHeights(km, BedHeightValueType.field(index));
    }

    private double interpolateBedHeights(final double km, final BedHeightValueType type) {
        if (this.values.containsKey(km)) {
            final Double value = type.getValue(this.values.get(km));
            return value == null ? Double.NaN : value.doubleValue();
        }

        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;

        // return NaN if value not found and no-interpolation mode, and report once
        if (!this.doInterpolate && (floorEntry != ceilingEntry)) {
            if (this.problems != null) {
                this.problems.addProblem(km, "sinfo.bedheightsfinder.missing_bedheights");
                this.problems = null;
            }
            return Double.NaN;
        }

        final double floorKm = floorEntry.getKey().doubleValue();
        final double ceilKm = ceilingEntry.getKey().doubleValue();
        /* report once if the interpolation distance exceeds 1000m */
        if (Math.abs(floorKm - ceilKm) > MAX_DISTANCE_KM) {
            if (this.problems != null) {
                this.problems.addProblem(km, "linearInterpolator.maxdistance", MAX_DISTANCE_KM * 1000);
                this.problems = null;
            }
            return Double.NaN;
        }

        final Double floorHeight = type.getValue(floorEntry.getValue());
        final Double ceilingHeight = type.getValue(ceilingEntry.getValue());

        if (floorHeight == null || ceilingHeight == null)
            return Double.NaN;

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

http://dive4elements.wald.intevation.org