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

http://dive4elements.wald.intevation.org