Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedHeightsFinder.java @ 9469:6acd22e86951
Improvements on Config-Files
author | gernotbelger |
---|---|
date | Tue, 04 Sep 2018 16:42:03 +0200 |
parents | ecadc9ed0ba0 |
children | 7228bd10a8cc |
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 heights 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; /** * Create bed height finders from a collection of bed heights. */ 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); 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 height 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); } /** * Create a finder for a given bed height. * */ private static BedHeightsFinder createBedHeights(final Calculation problems, 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(problems, info, values); } private BedHeightsFinder(final Calculation problems, final BedHeightInfo info, final NavigableMap<Double, BedHeightValue> values) { this.info = info; this.values = values; this.problems = problems; } 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; 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 && 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); } }