8854
|
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
8877
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
8854
|
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.flowdepth; |
|
11 |
8883
|
12 import java.util.ArrayList; |
8854
|
13 import java.util.Collection; |
8883
|
14 import java.util.Collections; |
8854
|
15 import java.util.List; |
|
16 |
8883
|
17 import org.apache.commons.math.FunctionEvaluationException; |
|
18 import org.apache.commons.math.analysis.UnivariateRealFunction; |
8879
|
19 import org.dive4elements.artifacts.ArtifactDatabase; |
8854
|
20 import org.dive4elements.artifacts.CallContext; |
|
21 import org.dive4elements.river.artifacts.BedHeightsArtifact; |
|
22 import org.dive4elements.river.artifacts.model.Calculation; |
|
23 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
24 import org.dive4elements.river.artifacts.model.LocationProvider; |
8877
|
25 import org.dive4elements.river.artifacts.model.QKms; |
8854
|
26 import org.dive4elements.river.artifacts.model.WKms; |
|
27 import org.dive4elements.river.artifacts.resources.Resources; |
|
28 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; |
|
29 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair; |
8882
|
30 import org.dive4elements.river.artifacts.states.WaterlevelData; |
|
31 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; |
8854
|
32 import org.dive4elements.river.model.BedHeight; |
8883
|
33 import org.dive4elements.river.model.BedHeightValue; |
8854
|
34 import org.dive4elements.river.model.Gauge; |
|
35 import org.dive4elements.river.model.River; |
8883
|
36 import org.dive4elements.river.utils.DoubleUtil; |
8854
|
37 import org.dive4elements.river.utils.GaugeIndex; |
|
38 import org.dive4elements.river.utils.RiverUtils; |
|
39 |
|
40 class FlowDepthCalculation { |
|
41 |
|
42 private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; |
|
43 |
8877
|
44 private final CallContext context; |
8854
|
45 |
8882
|
46 public FlowDepthCalculation(final CallContext context) { |
8877
|
47 this.context = context; |
8854
|
48 } |
|
49 |
8877
|
50 public CalculationResult calculate(final SINFOArtifact sinfo) { |
8854
|
51 |
8879
|
52 /* |
|
53 * find the user of this artifact, sadly this is not part of the calling context, so instead we determine the |
|
54 * owner oft the artifact |
|
55 */ |
|
56 final ArtifactDatabase database = this.context.getDatabase(); |
|
57 final String user = database.findArtifactUser(sinfo.identifier()); |
8854
|
58 |
8877
|
59 /* access input data */ |
|
60 final FlowDepthAccess access = new FlowDepthAccess(sinfo); |
|
61 final River river = access.getRiver(); |
8854
|
62 |
8877
|
63 final Collection<DifferencesPair> diffPairs = access.getDifferencePairs(); |
|
64 |
|
65 final double from = access.getFrom(); |
|
66 final double to = access.getTo(); |
|
67 |
|
68 final boolean useTkh = access.isUseTransportBodies(); |
|
69 |
|
70 /* calculate results for each diff pair */ |
|
71 final Calculation problems = new Calculation(); |
|
72 |
|
73 final List<Gauge> gauges = river.determineGauges(from, to); |
|
74 final GaugeIndex gaugeIndex = new GaugeIndex(gauges); |
|
75 |
8882
|
76 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); |
8877
|
77 |
8884
|
78 final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, to, useTkh); |
8877
|
79 |
|
80 for (final DifferencesPair diffPair : diffPairs) { |
8882
|
81 final FlowDepthCalculationResult result = calculateResult(river, from, to, diffPair, problems, gaugeIndex); |
|
82 if (result != null) |
8877
|
83 results.addResult(result); |
|
84 } |
|
85 |
8882
|
86 return new CalculationResult(results, problems); |
8877
|
87 } |
|
88 |
8884
|
89 private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, final DifferencesPair diffPair, |
|
90 final Calculation problems, final GaugeIndex gaugeIndex) { |
8877
|
91 |
|
92 /* access real input data from database */ |
|
93 final String soundingId = diffPair.getSoundingId(); |
|
94 final String wstId = diffPair.getWstId(); |
|
95 |
8882
|
96 final BedHeight bedHeight = loadBedHeight(soundingId, from, to); |
|
97 if (bedHeight == null) { |
8884
|
98 final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", soundingId); |
8877
|
99 problems.addProblem(message); |
|
100 return null; |
|
101 } |
|
102 |
8882
|
103 /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */ |
8884
|
104 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, Double.NaN); |
8882
|
105 if (waterlevel == null) { |
8884
|
106 final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", wstId); |
8877
|
107 problems.addProblem(message); |
|
108 return null; |
|
109 } |
8882
|
110 final WKms wstKms = waterlevel.getWkms(); |
|
111 |
8883
|
112 final String wspLabel = wstKms.getName(); |
|
113 final String soundingLabel = bedHeight.getDescription(); |
|
114 final String label = String.format("%s - %s", wspLabel, soundingLabel); |
8877
|
115 |
8883
|
116 checkYearDifference(label, waterlevel, bedHeight, problems); |
|
117 checkWaterlevelDiscretisation(wstKms, problems); |
8882
|
118 |
|
119 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ |
8884
|
120 final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE); |
8882
|
121 |
|
122 final Gauge refGauge = waterlevel.findReferenceGauge(river); |
|
123 final String refGaugeName = refGauge == null ? notinrange : refGauge.getName(); |
8877
|
124 |
|
125 final BedHeightInfo sounding = BedHeightInfo.from(bedHeight); |
8883
|
126 final int wspYear = waterlevel.getYear(); |
8882
|
127 final WstInfo wstInfo = new WstInfo(wspLabel, wspYear, refGaugeName); |
8877
|
128 |
|
129 final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(label, wstInfo, sounding); |
|
130 |
8883
|
131 // FIXME: nur prüfen/beschaffen wenn TKH Berechnung aktiv |
|
132 /* Abflusswerte vorhanden? */ |
8882
|
133 if (!(wstKms instanceof QKms)) { |
8884
|
134 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingQ", null, label); |
8877
|
135 problems.addProblem(message); |
|
136 // TODO: keine Berechnung TKH |
|
137 } |
|
138 |
8884
|
139 // FIXME |
8877
|
140 // - Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb) |
8882
|
141 // - Abhängig von Peiljahr |
|
142 // - kein D50 vorhanden --> Fehler |
8883
|
143 |
8884
|
144 // FIXME |
8877
|
145 // - Art der Gewässersohle (starr/mobil) |
|
146 |
|
147 final String bedHeightLabel = bedHeight.getDescription(); |
|
148 final String wstLabel = wstKms.getName(); |
|
149 |
8884
|
150 final UnivariateRealFunction wstInterpolator = DoubleUtil.getLinearInterpolator(wstKms.allKms(), wstKms.allWs()); |
8877
|
151 |
8883
|
152 // FIXME: sort by station first, but in what direction? |
|
153 final List<BedHeightValue> values = bedHeight.getValues(); |
8877
|
154 |
8883
|
155 final List<BedHeightValue> sortedValues = new ArrayList<>(values); |
|
156 Collections.sort(sortedValues, new BedHeightStationComparator()); |
8882
|
157 |
8886
|
158 SoilKind lastKind = SoilKind.mobil; |
|
159 |
8883
|
160 for (final BedHeightValue bedHeightValue : sortedValues) { |
8877
|
161 |
8883
|
162 final Double station = bedHeightValue.getStation(); |
|
163 if (station == null || station.isNaN()) |
|
164 continue; |
|
165 |
|
166 final Double meanBedHeightDbl = bedHeightValue.getHeight(); |
|
167 if (meanBedHeightDbl == null || meanBedHeightDbl.isNaN()) |
|
168 continue; |
|
169 |
|
170 final double km = station; |
|
171 final double meanBedHeight = meanBedHeightDbl; |
|
172 |
|
173 try { |
|
174 // FIXME: check out of range |
|
175 final double wst = wstInterpolator.value(km); |
|
176 |
|
177 final double flowDepth = wst - meanBedHeight; |
|
178 |
|
179 // FIXME: piecewise constant interpolation? |
|
180 // final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN; |
|
181 final double discharge = Double.NaN; |
|
182 |
|
183 // FIXME: calculate tkh |
8886
|
184 |
|
185 // REMARK: bissl spielerei zum testen damit die sohlart nicht zu schnell wechselt |
|
186 final boolean changeKind = Math.random() > 0.95; |
|
187 SoilKind kind; |
|
188 if (changeKind) { |
|
189 switch (lastKind) { |
|
190 case starr: |
|
191 kind = SoilKind.mobil; |
|
192 break; |
|
193 |
|
194 case mobil: |
|
195 default: |
|
196 kind = SoilKind.starr; |
|
197 break; |
|
198 |
|
199 } |
|
200 } else |
|
201 kind = lastKind; |
|
202 lastKind = kind; |
|
203 |
|
204 final double tkh = 100 + 10 * (Math.random() - 0.5); |
|
205 |
|
206 final double flowDepthTkh; |
|
207 final double tkhUp; |
|
208 final double tkhDown; |
|
209 switch (kind) { |
|
210 case starr: |
|
211 flowDepthTkh = wst - (meanBedHeight + tkh / 100); |
|
212 tkhUp = tkh; |
|
213 tkhDown = 0; |
|
214 break; |
|
215 |
|
216 case mobil: |
|
217 default: |
|
218 flowDepthTkh = wst - (meanBedHeight + tkh / 200); |
|
219 tkhUp = tkh / 2; |
|
220 tkhDown = -tkh / 2; |
|
221 break; |
|
222 } |
|
223 |
8883
|
224 |
|
225 // REMARK: access the location once only during calculation |
|
226 final String location = LocationProvider.getLocation(river.getName(), km); |
|
227 |
|
228 // REMARK: access the gauge once only during calculation |
|
229 final Gauge gauge = findGauge(waterlevel, refGauge, gaugeIndex, km); |
|
230 |
|
231 final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); |
|
232 |
8886
|
233 resultData.addRow(km, flowDepth, flowDepthTkh, kind, tkh, tkhUp, tkhDown, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, bedHeightLabel, |
|
234 location); |
8883
|
235 } |
|
236 catch (final FunctionEvaluationException e) { |
|
237 /* should only happen if out of range */ |
|
238 e.printStackTrace(); |
|
239 /* simply ignore */ |
|
240 } |
|
241 |
8877
|
242 } |
|
243 |
|
244 return resultData; |
|
245 } |
|
246 |
8883
|
247 /** |
|
248 * Checks the year difference between waterlevels and sounding, and issues a warning if too big. |
|
249 * |
|
250 * Zeitraum Zeitliche Differenz [a] |
|
251 * X ≥ 1998 ± 3 |
|
252 * 1958 ≤ X < 1998 ± 6 |
|
253 * 1918 ≤ X < 1958 ± 12 |
|
254 * X < 1918 ± 25 |
|
255 */ |
8884
|
256 private void checkYearDifference(final String label, final WaterlevelData waterlevel, final BedHeight sounding, final Calculation problems) { |
8883
|
257 |
|
258 final Integer soundingYear = sounding.getYear(); |
|
259 if (soundingYear == null) |
|
260 return; |
|
261 |
|
262 final int wstYear = waterlevel.getYear(); |
|
263 if (wstYear < 0) |
|
264 return; |
|
265 |
|
266 final int maxDifference = getMaxDifferenceYears(soundingYear); |
|
267 |
|
268 final int difference = Math.abs(soundingYear - wstYear); |
|
269 if (difference > maxDifference) { |
8884
|
270 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.year_difference", null, label, difference); |
8883
|
271 problems.addProblem(message); |
|
272 } |
|
273 } |
|
274 |
|
275 private int getMaxDifferenceYears(final int year) { |
|
276 |
|
277 if (year < 1918) |
|
278 return 25; |
|
279 |
|
280 if (1918 <= year && year < 1958) |
|
281 return 12; |
|
282 |
|
283 if (1958 <= year && year < 1998) |
|
284 return 6; |
|
285 |
|
286 /* >= 1998 */ |
|
287 return 3; |
|
288 } |
|
289 |
8884
|
290 private Gauge findGauge(final WaterlevelData waterlevel, final Gauge refGauge, final GaugeIndex gaugeIndex, final double km) { |
8882
|
291 |
|
292 // REMARK: using same logic as in WaterlevelExporter here |
|
293 |
|
294 final boolean showAllGauges = waterlevel.isShowAllGauges(); |
|
295 |
|
296 if (showAllGauges) |
|
297 return gaugeIndex.findGauge(km); |
|
298 |
|
299 if (refGauge.getRange().contains(km)) |
|
300 return refGauge; |
|
301 |
|
302 return null; |
|
303 } |
|
304 |
|
305 /* Checks if the discretisation of the waterlevel exceeds 1000m */ |
8884
|
306 // FIXME: vermutlich sollten wir diesen check auf den gültigkeitsbereich einschränken |
8882
|
307 private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) { |
|
308 final int size = wstKms.size(); |
|
309 for (int i = 0; i < size - 2; i++) { |
|
310 final double kmPrev = wstKms.getKm(i); |
|
311 final double kmNext = wstKms.getKm(i + 1); |
|
312 |
|
313 if (Math.abs(kmPrev - kmNext) > 1) { |
|
314 final String label = wstKms.getName(); |
|
315 |
8884
|
316 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label); |
8882
|
317 problems.addProblem(kmPrev, message); |
|
318 } |
|
319 } |
|
320 } |
|
321 |
8877
|
322 private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { |
|
323 |
8883
|
324 // REMARK: absolutely unbelievable.... |
8882
|
325 // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type |
|
326 // throughout flys. |
8877
|
327 // The knowledge on how to parse the datacage-ids is spread through the complete code-base... |
|
328 |
8882
|
329 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but |
|
330 // this is plain random |
8877
|
331 final String[] parts = soundingId.split(";"); |
|
332 |
|
333 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); |
|
334 |
|
335 final Integer bedheightId = artifact.getDataAsInteger("height_id"); |
8883
|
336 // REMARK: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the |
8882
|
337 // other type means) |
8877
|
338 // Luckily, the requirement is to only access 'single' data here. |
|
339 // final String bedheightType = artifact.getDataAsString("type"); |
|
340 |
8883
|
341 // REMARK: BedDifferences uses this, but we also need the metadata of the BedHeight |
|
342 // REMARK: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via |
8882
|
343 // hibernate stuff |
8877
|
344 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
8882
|
345 // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
8877
|
346 |
|
347 return BedHeight.getBedHeightById(bedheightId); |
|
348 } |
8884
|
349 } |