annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/GaugeMainValueNameFinder.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents
children a4121ec450d6
rev   line source
9176
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
2 * Software engineering by
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
5 *
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
9 */
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
10 package org.dive4elements.river.artifacts.sinfo.common;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
11
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
12 import java.util.Map.Entry;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
13 import java.util.NavigableMap;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
14 import java.util.TreeMap;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
15
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
16 import org.dive4elements.river.artifacts.model.Calculation;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
17 import org.dive4elements.river.model.Gauge;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
18 import org.dive4elements.river.model.MainValue;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
19 import org.dive4elements.river.model.MainValueType.MainValueTypeKey;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
20
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
21 /**
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
22 * Loading and search the main values of a gauge to find and build a name
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
23 *
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
24 * @author Matthias Schäfer
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
25 *
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
26 */
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
27 public final class GaugeMainValueNameFinder {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
28
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
29 /***** FIELDS *****/
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
30
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
31 // private static Logger log = Logger.getLogger(GaugeMainValueNameFinder.class);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
32
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
33 private final Gauge gauge;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
34
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
35 private Calculation problems;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
36
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
37 private final NavigableMap<Double, MainValue> mainValues;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
38
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
39 private final String approxPrefix = "ca."; // "\u2248" geht wohl nicht
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
40
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
41
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
42 /***** CONSTRUCTORS *****/
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
43
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
44 private GaugeMainValueNameFinder(final MainValueTypeKey type, final Gauge gauge, final Calculation problems) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
45 this.gauge = gauge;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
46 this.problems = problems;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
47 this.mainValues = new TreeMap<>();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
48 for (final MainValue mainValue : MainValue.getValuesOfGaugeAndType(gauge, type))
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
49 this.mainValues.put(Double.valueOf(mainValue.getValue().doubleValue()), mainValue);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
50 if (this.mainValues.isEmpty() && (this.problems != null)) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
51 this.problems.addProblem("gauge_main_values.missing", gauge.getName());
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
52 // Report only once
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
53 this.problems = null;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
54 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
55 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
56
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
57
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
58 /***** METHODS *****/
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
59
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
60 /**
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
61 * Loads the the main values table of a type and a gauge (GAUGE.glt)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
62 *
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
63 * @return The main values finder of a type and a gauge, or null
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
64 */
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
65 public static GaugeMainValueNameFinder loadValues(final MainValueTypeKey type, final Gauge gauge, final Calculation problems) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
66 return new GaugeMainValueNameFinder(type, gauge, problems);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
67 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
68
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
69 /**
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
70 * If this provider may return valid data at all.
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
71 */
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
72 public boolean isValid() {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
73 return (this.mainValues != null);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
74 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
75
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
76 /**
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
77 * Main value zone for a value.
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
78 */
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
79 public String getZoneName(final double value) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
80 if (!this.isValid())
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
81 return "";
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
82 if (Double.isNaN(value))
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
83 return "";
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
84
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
85 // Exact match
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
86 if (this.mainValues.containsKey(Double.valueOf(value)))
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
87 return this.mainValues.get(Double.valueOf(value)).getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
88
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
89 // Clearly below or just (max. 10%) below lowest named value
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
90 final Entry<Double, MainValue> lowerZone = this.mainValues.floorEntry(Double.valueOf(value));
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
91 if (lowerZone == null) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
92 if (value >= this.mainValues.firstKey().doubleValue() * 0.9)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
93 return this.approxPrefix + this.mainValues.firstEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
94 else
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
95 return "<" + this.mainValues.firstEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
96 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
97
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
98 // Clearly above or just (max. 10%) above highest named value
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
99 final Entry<Double, MainValue> higherZone = this.mainValues.ceilingEntry(Double.valueOf(value));
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
100 if (higherZone == null) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
101 if (value <= this.mainValues.lastKey().doubleValue() * 1.1)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
102 return this.approxPrefix + this.mainValues.lastEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
103 else
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
104 return ">" + this.mainValues.lastEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
105 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
106
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
107 // Near (10%) one of the borders of a zone interval, or clearly within a zone
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
108 if (value <= lowerZone.getKey().doubleValue() * 1.1)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
109 return this.approxPrefix + lowerZone.getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
110 else if (value >= higherZone.getKey().doubleValue() * 0.9)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
111 return this.approxPrefix + higherZone.getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
112 else
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
113 return lowerZone.getValue().getMainValue().getName() + "-" + higherZone.getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
114 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
115 }

http://dive4elements.wald.intevation.org