Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/GaugeDischargeZoneFinder.java @ 9157:f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
author | mschaefer |
---|---|
date | Tue, 19 Jun 2018 14:19:32 +0200 |
parents | |
children | a4121ec450d6 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/GaugeDischargeZoneFinder.java Tue Jun 19 14:19:32 2018 +0200 @@ -0,0 +1,108 @@ +/** 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.collision; + +import java.util.Map.Entry; +import java.util.NavigableMap; +import java.util.TreeMap; + +import org.dive4elements.river.artifacts.model.Calculation; +import org.dive4elements.river.model.Gauge; +import org.dive4elements.river.model.MainValue; +import org.dive4elements.river.model.MainValueType.MainValueTypeKey; + +/** + * Loading and search the discharge zones of a gauge + * + * @author Matthias Schäfer + * + */ +public final class GaugeDischargeZoneFinder { + + /***** FIELDS *****/ + + // private static Logger log = Logger.getLogger(GaugeDischargeZoneFinder.class); + + private final Gauge gauge; + + private final Calculation problems; + + private final NavigableMap<Double, MainValue> qZones; + + private final String approxPrefix = "ca."; // "\u2248" geht wohl nicht + + + /***** CONSTRUCTORS *****/ + + private GaugeDischargeZoneFinder(final Gauge gauge, final Calculation problems) { + this.gauge = gauge; + this.problems = problems; + this.qZones = new TreeMap<>(); + for (final MainValue mainValue : MainValue.getValuesOfGaugeAndType(gauge, MainValueTypeKey.Q)) + this.qZones.put(Double.valueOf(mainValue.getValue().doubleValue()), mainValue); + } + + + /***** METHODS *****/ + + /** + * Loads the the main discharge table of a gauge (GAUGE.at) + * + * @return The discharge table values finder of the gauge, or null + */ + public static GaugeDischargeZoneFinder loadValues(final Gauge gauge, final Calculation problems) { + return new GaugeDischargeZoneFinder(gauge, problems); + } + + /** + * If this provider may return valid data at all. + */ + public boolean isValid() { + return (this.qZones != null); + } + + /** + * Discharge zone for a Q. + */ + public String getDischargeZone(final double q) { + if (Double.isNaN(q)) + return ""; + + // Exact match + if (this.qZones.containsKey(Double.valueOf(q))) + return this.qZones.get(Double.valueOf(q)).getMainValue().getName(); + + // Clearly below or just (max. 10%) below lowest named discharge + final Entry<Double, MainValue> lowerZone = this.qZones.floorEntry(Double.valueOf(q)); + if (lowerZone == null) { + if (q >= this.qZones.firstKey().doubleValue() * 0.9) + return this.approxPrefix + this.qZones.firstEntry().getValue().getMainValue().getName(); + else + return "<" + this.qZones.firstEntry().getValue().getMainValue().getName(); + } + + // Clearly above or just (max. 10%) above highest named discharge + final Entry<Double, MainValue> higherZone = this.qZones.ceilingEntry(Double.valueOf(q)); + if (higherZone == null) { + if (q <= this.qZones.lastKey().doubleValue() * 1.1) + return this.approxPrefix + this.qZones.lastEntry().getValue().getMainValue().getName(); + else + return ">" + this.qZones.lastEntry().getValue().getMainValue().getName(); + } + + // Near (10%) one of the borders of a zone interval, or clearly within a zone + if (q <= lowerZone.getKey().doubleValue() * 1.1) + return this.approxPrefix + lowerZone.getValue().getMainValue().getName(); + else if (q >= higherZone.getKey().doubleValue() * 0.9) + return this.approxPrefix + higherZone.getValue().getMainValue().getName(); + else + return lowerZone.getValue().getMainValue().getName() + "-" + higherZone.getValue().getMainValue().getName(); + } +} \ No newline at end of file