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 |
8882
|
78 final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, |
|
79 to, useTkh); |
8877
|
80 |
|
81 for (final DifferencesPair diffPair : diffPairs) { |
8882
|
82 final FlowDepthCalculationResult result = calculateResult(river, from, to, diffPair, problems, gaugeIndex); |
|
83 if (result != null) |
8877
|
84 results.addResult(result); |
|
85 } |
|
86 |
8882
|
87 return new CalculationResult(results, problems); |
8877
|
88 } |
|
89 |
8882
|
90 private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, |
|
91 final DifferencesPair diffPair, final Calculation problems, final GaugeIndex gaugeIndex) { |
8877
|
92 |
|
93 /* access real input data from database */ |
|
94 final String soundingId = diffPair.getSoundingId(); |
|
95 final String wstId = diffPair.getWstId(); |
|
96 |
8882
|
97 final BedHeight bedHeight = loadBedHeight(soundingId, from, to); |
|
98 if (bedHeight == null) { |
|
99 final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", |
|
100 soundingId); |
8877
|
101 problems.addProblem(message); |
|
102 return null; |
|
103 } |
|
104 |
8882
|
105 /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */ |
|
106 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, |
|
107 Double.NaN); |
|
108 if (waterlevel == null) { |
|
109 final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", |
|
110 wstId); |
8877
|
111 problems.addProblem(message); |
|
112 return null; |
|
113 } |
8882
|
114 final WKms wstKms = waterlevel.getWkms(); |
|
115 |
8883
|
116 final String wspLabel = wstKms.getName(); |
|
117 final String soundingLabel = bedHeight.getDescription(); |
|
118 final String label = String.format("%s - %s", wspLabel, soundingLabel); |
8877
|
119 |
8883
|
120 checkYearDifference(label, waterlevel, bedHeight, problems); |
|
121 checkWaterlevelDiscretisation(wstKms, problems); |
8882
|
122 |
|
123 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ |
|
124 final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, |
|
125 CSV_NOT_IN_GAUGE_RANGE); |
|
126 |
|
127 final Gauge refGauge = waterlevel.findReferenceGauge(river); |
|
128 final String refGaugeName = refGauge == null ? notinrange : refGauge.getName(); |
8877
|
129 |
|
130 final BedHeightInfo sounding = BedHeightInfo.from(bedHeight); |
8883
|
131 final int wspYear = waterlevel.getYear(); |
8882
|
132 final WstInfo wstInfo = new WstInfo(wspLabel, wspYear, refGaugeName); |
8877
|
133 |
|
134 final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(label, wstInfo, sounding); |
|
135 |
8883
|
136 // FIXME: nur prüfen/beschaffen wenn TKH Berechnung aktiv |
|
137 /* Abflusswerte vorhanden? */ |
8882
|
138 if (!(wstKms instanceof QKms)) { |
|
139 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingQ", |
|
140 null, label); |
8877
|
141 problems.addProblem(message); |
|
142 // TODO: keine Berechnung TKH |
|
143 } |
|
144 |
|
145 // - Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb) |
8882
|
146 // - Abhängig von Peiljahr |
|
147 // - kein D50 vorhanden --> Fehler |
8883
|
148 |
8877
|
149 // - Art der Gewässersohle (starr/mobil) |
|
150 |
|
151 final String bedHeightLabel = bedHeight.getDescription(); |
|
152 final String wstLabel = wstKms.getName(); |
|
153 |
8883
|
154 final UnivariateRealFunction wstInterpolator = DoubleUtil.getLinearInterpolator(wstKms.allKms(), |
|
155 wstKms.allWs()); |
8877
|
156 |
8883
|
157 // FIXME: sort by station first, but in what direction? |
|
158 final List<BedHeightValue> values = bedHeight.getValues(); |
8877
|
159 |
8883
|
160 final List<BedHeightValue> sortedValues = new ArrayList<>(values); |
|
161 Collections.sort(sortedValues, new BedHeightStationComparator()); |
8882
|
162 |
8883
|
163 for (final BedHeightValue bedHeightValue : sortedValues) { |
8877
|
164 |
8883
|
165 final Double station = bedHeightValue.getStation(); |
|
166 if (station == null || station.isNaN()) |
|
167 continue; |
|
168 |
|
169 final Double meanBedHeightDbl = bedHeightValue.getHeight(); |
|
170 if (meanBedHeightDbl == null || meanBedHeightDbl.isNaN()) |
|
171 continue; |
|
172 |
|
173 final double km = station; |
|
174 final double meanBedHeight = meanBedHeightDbl; |
|
175 |
|
176 try { |
|
177 // FIXME: check out of range |
|
178 final double wst = wstInterpolator.value(km); |
|
179 |
|
180 final double flowDepth = wst - meanBedHeight; |
|
181 |
|
182 // FIXME: piecewise constant interpolation? |
|
183 // final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN; |
|
184 final double discharge = Double.NaN; |
|
185 |
|
186 // FIXME: calculate tkh |
|
187 final double tkh = 0; |
|
188 final double flowDepthTkh = flowDepth - tkh; |
|
189 |
|
190 // REMARK: access the location once only during calculation |
|
191 final String location = LocationProvider.getLocation(river.getName(), km); |
|
192 |
|
193 // REMARK: access the gauge once only during calculation |
|
194 final Gauge gauge = findGauge(waterlevel, refGauge, gaugeIndex, km); |
|
195 |
|
196 final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); |
|
197 |
|
198 resultData.addRow(km, flowDepth, flowDepthTkh, tkh, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, |
|
199 bedHeightLabel, location); |
|
200 } |
|
201 catch (final FunctionEvaluationException e) { |
|
202 /* should only happen if out of range */ |
|
203 e.printStackTrace(); |
|
204 /* simply ignore */ |
|
205 } |
|
206 |
8877
|
207 } |
|
208 |
|
209 return resultData; |
|
210 } |
|
211 |
8883
|
212 /** |
|
213 * Checks the year difference between waterlevels and sounding, and issues a warning if too big. |
|
214 * |
|
215 * Zeitraum Zeitliche Differenz [a] |
|
216 * X ≥ 1998 ± 3 |
|
217 * 1958 ≤ X < 1998 ± 6 |
|
218 * 1918 ≤ X < 1958 ± 12 |
|
219 * X < 1918 ± 25 |
|
220 */ |
|
221 private void checkYearDifference(final String label, final WaterlevelData waterlevel, final BedHeight sounding, |
|
222 final Calculation problems) { |
|
223 |
|
224 final Integer soundingYear = sounding.getYear(); |
|
225 if (soundingYear == null) |
|
226 return; |
|
227 |
|
228 final int wstYear = waterlevel.getYear(); |
|
229 if (wstYear < 0) |
|
230 return; |
|
231 |
|
232 final int maxDifference = getMaxDifferenceYears(soundingYear); |
|
233 |
|
234 final int difference = Math.abs(soundingYear - wstYear); |
|
235 if (difference > maxDifference) { |
|
236 final String message = Resources.getMsg(this.context.getMeta(), |
|
237 "sinfo_calc_flow_depth.warning.year_difference", null, label, difference); |
|
238 problems.addProblem(message); |
|
239 } |
|
240 } |
|
241 |
|
242 private int getMaxDifferenceYears(final int year) { |
|
243 |
|
244 if (year < 1918) |
|
245 return 25; |
|
246 |
|
247 if (1918 <= year && year < 1958) |
|
248 return 12; |
|
249 |
|
250 if (1958 <= year && year < 1998) |
|
251 return 6; |
|
252 |
|
253 /* >= 1998 */ |
|
254 return 3; |
|
255 } |
|
256 |
8882
|
257 private Gauge findGauge(final WaterlevelData waterlevel, final Gauge refGauge, final GaugeIndex gaugeIndex, |
|
258 final double km) { |
|
259 |
|
260 // REMARK: using same logic as in WaterlevelExporter here |
|
261 |
|
262 final boolean showAllGauges = waterlevel.isShowAllGauges(); |
|
263 |
|
264 if (showAllGauges) |
|
265 return gaugeIndex.findGauge(km); |
|
266 |
|
267 if (refGauge.getRange().contains(km)) |
|
268 return refGauge; |
|
269 |
|
270 return null; |
|
271 } |
|
272 |
|
273 /* Checks if the discretisation of the waterlevel exceeds 1000m */ |
|
274 private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) { |
|
275 final int size = wstKms.size(); |
|
276 for (int i = 0; i < size - 2; i++) { |
|
277 final double kmPrev = wstKms.getKm(i); |
|
278 final double kmNext = wstKms.getKm(i + 1); |
|
279 |
|
280 if (Math.abs(kmPrev - kmNext) > 1) { |
|
281 final String label = wstKms.getName(); |
|
282 |
|
283 final String message = Resources.getMsg(this.context.getMeta(), |
|
284 "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label); |
|
285 problems.addProblem(kmPrev, message); |
|
286 } |
|
287 } |
|
288 } |
|
289 |
8877
|
290 private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { |
|
291 |
8883
|
292 // REMARK: absolutely unbelievable.... |
8882
|
293 // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type |
|
294 // throughout flys. |
8877
|
295 // The knowledge on how to parse the datacage-ids is spread through the complete code-base... |
|
296 |
8882
|
297 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but |
|
298 // this is plain random |
8877
|
299 final String[] parts = soundingId.split(";"); |
|
300 |
|
301 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); |
|
302 |
|
303 final Integer bedheightId = artifact.getDataAsInteger("height_id"); |
8883
|
304 // REMARK: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the |
8882
|
305 // other type means) |
8877
|
306 // Luckily, the requirement is to only access 'single' data here. |
|
307 // final String bedheightType = artifact.getDataAsString("type"); |
|
308 |
8883
|
309 // REMARK: BedDifferences uses this, but we also need the metadata of the BedHeight |
|
310 // REMARK: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via |
8882
|
311 // hibernate stuff |
8877
|
312 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
8882
|
313 // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
8877
|
314 |
|
315 return BedHeight.getBedHeightById(bedheightId); |
|
316 } |
8883
|
317 } |