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