Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java @ 8980:b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
Some improvements to error handling and handling of empty results.
author | gernotbelger |
---|---|
date | Thu, 05 Apr 2018 18:30:34 +0200 |
parents | b5600453bb8f |
children | 0adc6d04de95 |
rev | line source |
---|---|
8946 | 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.flowdepthminmax; | |
11 | |
12 import java.util.ArrayList; | |
13 import java.util.Collection; | |
14 | |
15 import org.apache.commons.lang.math.DoubleRange; | |
16 import org.dive4elements.artifacts.CallContext; | |
17 import org.dive4elements.river.artifacts.model.Calculation; | |
18 import org.dive4elements.river.artifacts.model.CalculationResult; | |
19 import org.dive4elements.river.artifacts.model.WKms; | |
20 import org.dive4elements.river.artifacts.resources.Resources; | |
21 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; | |
22 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; | |
8948 | 23 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultRow; |
24 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
8946 | 25 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthUtils; |
26 import org.dive4elements.river.artifacts.sinfo.flowdepthminmax.FlowDepthMinMaxAccess.MinMaxIdPair; | |
27 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.DischargeValuesFinder; | |
28 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.WaterlevelValuesFinder; | |
29 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; | |
30 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; | |
31 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; | |
32 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
33 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
34 import org.dive4elements.river.artifacts.states.WaterlevelData; | |
35 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; | |
36 import org.dive4elements.river.model.River; | |
37 | |
38 /** | |
39 * @author Gernot Belger | |
40 */ | |
41 final class FlowDepthMinMaxCalculation { | |
42 | |
43 private final CallContext context; | |
44 | |
45 public FlowDepthMinMaxCalculation(final CallContext context) { | |
46 this.context = context; | |
47 } | |
48 | |
49 public CalculationResult calculate(final SINFOArtifact sinfo) { | |
50 | |
51 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); | |
52 | |
53 /* access input data */ | |
54 final FlowDepthMinMaxAccess access = new FlowDepthMinMaxAccess(sinfo); | |
55 final River river = access.getRiver(); | |
56 final RiverInfo riverInfo = new RiverInfo(river); | |
57 | |
58 final Collection<MinMaxIdPair> minMaxPairs = access.getMinMaxPairs(); | |
59 | |
60 final DoubleRange calcRange = access.getRange(); | |
61 | |
62 /* calculate results for each diff pair */ | |
63 final Calculation problems = new Calculation(); | |
64 | |
65 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); | |
66 | |
67 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); | |
68 | |
69 final FlowDepthMinMaxCalculationResults results = new FlowDepthMinMaxCalculationResults(calcModeLabel, user, riverInfo, calcRange); | |
70 | |
71 for (final MinMaxIdPair minMaxPair : minMaxPairs) { | |
72 final FlowDepthMinMaxCalculationResult result = calculateResult(calcRange, minMaxPair, problems, infoProvider); | |
8980
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
73 results.addResult(result, problems); |
8946 | 74 } |
75 | |
76 return new CalculationResult(results, problems); | |
77 } | |
78 | |
79 /** | |
80 * Calculates one W-MSH differences pair. | |
81 * | |
82 * @param infoProvider | |
83 */ | |
84 private FlowDepthMinMaxCalculationResult calculateResult(final DoubleRange calcRange, final MinMaxIdPair minMaxPair, final Calculation problems, | |
85 final RiverInfoProvider infoProvider) { | |
86 | |
87 /* access real input data from database */ | |
88 final String wstId = minMaxPair.getWstId(); | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
89 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
90 // FIXME: bfg überzeugen dass man immer nur pärchen auswählen kann --> min/max id ist gleich! |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
91 |
8946 | 92 final String minSoundingId = minMaxPair.getMinSoundingId(); |
8948 | 93 final String maxSoundingId = minMaxPair.getMaxSoundingId(); |
8946 | 94 |
95 final BedHeightsFinder minBedHeight = minSoundingId == null ? null : BedHeightsFinder.forId(this.context, minSoundingId, calcRange, problems); | |
96 final BedHeightsFinder maxBedHeight = maxSoundingId == null ? null : BedHeightsFinder.forId(this.context, maxSoundingId, calcRange, problems); | |
97 if (minBedHeight == null && maxBedHeight == null) | |
98 return null; | |
99 | |
100 /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */ | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
101 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8946 | 102 if (waterlevel == null) |
103 return null; | |
104 | |
105 final String label = createLabel(waterlevel, minBedHeight, maxBedHeight); | |
106 | |
107 final WKms wstKms = waterlevel.getWkms(); | |
108 | |
109 final int soundingYear = checkSoundingYear(minBedHeight, maxBedHeight, problems); | |
8951 | 110 FlowDepthUtils.checkYearDifference(label, waterlevel.getYear(), soundingYear, problems); |
8946 | 111 |
112 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
113 final RiverInfoProvider riverInfoProvider = infoProvider.forWaterlevel(waterlevel); | |
114 | |
115 final int wspYear = waterlevel.getYear(); | |
116 final WstInfo wstInfo = new WstInfo(waterlevel.getName(), wspYear, riverInfoProvider.getReferenceGauge()); | |
117 | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
118 final WaterlevelValuesFinder waterlevelProvider = WaterlevelValuesFinder.fromKms(problems, wstKms); |
8946 | 119 final DischargeValuesFinder dischargeProvider = DischargeValuesFinder.fromKms(wstKms); |
120 | |
121 final String waterlevelLabel = waterlevel.getName(); | |
122 final String soundingLabel = buildSoundingLabel(minBedHeight, maxBedHeight); | |
123 | |
124 /* real calculation loop */ | |
8948 | 125 final Collection<SInfoResultRow> rows = new ArrayList<>(); |
8946 | 126 |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
127 // FIXME: we use the stations of one of the bed heights atm, because we probably will later use only data from one bed heights datasets! |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
128 final Collection<Double> stations = minBedHeight == null ? maxBedHeight.getStations() : minBedHeight.getStations(); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
129 for (final double station : stations) { |
8946 | 130 if (calcRange.containsDouble(station)) { |
131 | |
132 final double wst = waterlevelProvider.getWaterlevel(station); | |
133 final double discharge = dischargeProvider.getDischarge(station); | |
134 | |
135 final double minBedHeightValue = minBedHeight == null ? Double.NaN : minBedHeight.getMeanBedHeight(station); | |
136 final double maxBedHeightValue = maxBedHeight == null ? Double.NaN : maxBedHeight.getMeanBedHeight(station); | |
137 | |
138 final double minFlowDepth = wst - minBedHeightValue; | |
139 final double maxFlowDepth = wst - maxBedHeightValue; | |
140 | |
141 // FIXME: unclear what is meant here... | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
142 // FIXME: this will simply the bed height of 'the' bed height if we reduce this to simply using one sounding dataset |
8946 | 143 final double meanBedHeight = Double.NaN; |
144 | |
145 // REMARK: access the location once only during calculation | |
146 final String location = riverInfoProvider.getLocation(station); | |
147 | |
148 // REMARK: access the gauge once only during calculation | |
149 final String gaugeLabel = riverInfoProvider.findGauge(station); | |
150 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
151 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
152 if (Double.isNaN(wst) || Double.isNaN(minBedHeightValue) || Double.isNaN(maxBedHeightValue)) |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
153 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
154 |
8948 | 155 final SInfoResultRow row = SInfoResultRow.create().// |
156 putValue(SInfoResultType.station, station). // | |
157 putValue(SInfoResultType.flowdepthmin, minFlowDepth). // | |
158 putValue(SInfoResultType.flowdepthmax, maxFlowDepth). // | |
159 putValue(SInfoResultType.waterlevel, wst). // | |
160 putValue(SInfoResultType.discharge, discharge). // | |
161 putValue(SInfoResultType.waterlevelLabel, waterlevelLabel). // | |
162 putValue(SInfoResultType.gaugeLabel, gaugeLabel). // | |
163 putValue(SInfoResultType.meanBedHeight, meanBedHeight). // | |
164 putValue(SInfoResultType.soundingLabel, soundingLabel). // | |
165 putValue(SInfoResultType.location, location); | |
166 rows.add(row); | |
8946 | 167 } |
168 } | |
169 | |
170 final BedHeightInfo minBedHeightInfo = minBedHeight == null ? null : minBedHeight.getInfo(); | |
171 final BedHeightInfo maxBedHeightInfo = maxBedHeight == null ? null : maxBedHeight.getInfo(); | |
8948 | 172 |
8946 | 173 return new FlowDepthMinMaxCalculationResult(label, wstInfo, minBedHeightInfo, maxBedHeightInfo, rows); |
174 } | |
175 | |
176 private String buildSoundingLabel(final BedHeightsFinder minBedHeight, final BedHeightsFinder maxBedHeight) { | |
177 | |
178 if (minBedHeight == null) | |
179 return maxBedHeight.getInfo().getDescription(); | |
180 | |
181 if (maxBedHeight == null) | |
182 return minBedHeight.getInfo().getDescription(); | |
183 | |
184 return String.format("%s / %s", minBedHeight.getInfo().getDescription(), maxBedHeight.getInfo().getDescription()); | |
185 } | |
186 | |
187 private String createLabel(final WaterlevelData waterlevel, final BedHeightsFinder minBedHeight, final BedHeightsFinder maxBedHeight) { | |
188 | |
189 final StringBuilder buffer = new StringBuilder(waterlevel.getName()); | |
190 | |
191 if (minBedHeight != null) | |
192 buffer.append(" - "). // | |
193 append(minBedHeight.getInfo().getDescription()); | |
194 | |
195 if (maxBedHeight != null) | |
196 buffer.append(" - "). // | |
197 append(maxBedHeight.getInfo().getDescription()); | |
198 | |
199 return buffer.toString(); | |
200 } | |
201 | |
202 private int checkSoundingYear(final BedHeightsFinder minBedHeight, final BedHeightsFinder maxBedHeight, final Calculation problems) { | |
203 | |
204 if (maxBedHeight == null) | |
205 return minBedHeight.getInfo().getYear(); | |
206 | |
207 if (minBedHeight == null) | |
208 return maxBedHeight.getInfo().getYear(); | |
209 | |
210 final int minYear = minBedHeight.getInfo().getYear(); | |
211 final int maxYear = minBedHeight.getInfo().getYear(); | |
212 | |
213 if (minYear != maxYear) | |
214 problems.addProblem("sinfo.flowdepthminmaxcalculation.soundingyear.different"); | |
215 | |
216 return minYear; | |
217 } | |
218 } |