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