annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/GaugeMainValueNameFinder.java @ 9195:a4121ec450d6

'ca.'-issue ExportContextCSV+PDF separated uinfo.inundationduration url export
author gernotbelger
date Fri, 29 Jun 2018 14:52:54 +0200
parents 1614cb14308f
children
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
9195
a4121ec450d6 'ca.'-issue
gernotbelger
parents: 9176
diff changeset
39 private final String approxPrefix = "\u2248";// "ca.";
9176
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 /***** CONSTRUCTORS *****/
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
42
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
43 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
44 this.gauge = gauge;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
45 this.problems = problems;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
46 this.mainValues = new TreeMap<>();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
47 for (final MainValue mainValue : MainValue.getValuesOfGaugeAndType(gauge, type))
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
48 this.mainValues.put(Double.valueOf(mainValue.getValue().doubleValue()), mainValue);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
49 if (this.mainValues.isEmpty() && (this.problems != null)) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
50 this.problems.addProblem("gauge_main_values.missing", gauge.getName());
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
51 // Report only once
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
52 this.problems = null;
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
53 }
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 /***** METHODS *****/
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 /**
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
59 * 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
60 *
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
61 * @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
62 */
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
63 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
64 return new GaugeMainValueNameFinder(type, gauge, problems);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
65 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
66
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 * If this provider may return valid data at all.
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 public boolean isValid() {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
71 return (this.mainValues != null);
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
72 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
73
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 * Main value zone for a value.
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 public String getZoneName(final double value) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
78 if (!this.isValid())
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
79 return "";
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
80 if (Double.isNaN(value))
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
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
83 // Exact match
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
84 if (this.mainValues.containsKey(Double.valueOf(value)))
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
85 return this.mainValues.get(Double.valueOf(value)).getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
86
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
87 // Clearly below or just (max. 10%) below lowest named value
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
88 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
89 if (lowerZone == null) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
90 if (value >= this.mainValues.firstKey().doubleValue() * 0.9)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
91 return this.approxPrefix + this.mainValues.firstEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
92 else
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
93 return "<" + this.mainValues.firstEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
94 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
95
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
96 // Clearly above or just (max. 10%) above highest named value
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
97 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
98 if (higherZone == null) {
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
99 if (value <= this.mainValues.lastKey().doubleValue() * 1.1)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
100 return this.approxPrefix + this.mainValues.lastEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
101 else
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
102 return ">" + this.mainValues.lastEntry().getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
103 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
104
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
105 // 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
106 if (value <= lowerZone.getKey().doubleValue() * 1.1)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
107 return this.approxPrefix + lowerZone.getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
108 else if (value >= higherZone.getKey().doubleValue() * 0.9)
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
109 return this.approxPrefix + higherZone.getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
110 else
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
111 return lowerZone.getValue().getMainValue().getName() + "-" + higherZone.getValue().getMainValue().getName();
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
112 }
1614cb14308f Work on calculations for S-Info flood duration workflow
mschaefer
parents:
diff changeset
113 }

http://dive4elements.wald.intevation.org