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 |
8883
|
158 for (final BedHeightValue bedHeightValue : sortedValues) { |
8877
|
159 |
8883
|
160 final Double station = bedHeightValue.getStation(); |
|
161 if (station == null || station.isNaN()) |
|
162 continue; |
|
163 |
|
164 final Double meanBedHeightDbl = bedHeightValue.getHeight(); |
|
165 if (meanBedHeightDbl == null || meanBedHeightDbl.isNaN()) |
|
166 continue; |
|
167 |
|
168 final double km = station; |
|
169 final double meanBedHeight = meanBedHeightDbl; |
|
170 |
|
171 try { |
|
172 // FIXME: check out of range |
|
173 final double wst = wstInterpolator.value(km); |
|
174 |
|
175 final double flowDepth = wst - meanBedHeight; |
|
176 |
|
177 // FIXME: piecewise constant interpolation? |
|
178 // final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN; |
|
179 final double discharge = Double.NaN; |
|
180 |
|
181 // FIXME: calculate tkh |
|
182 final double tkh = 0; |
|
183 final double flowDepthTkh = flowDepth - tkh; |
|
184 |
|
185 // REMARK: access the location once only during calculation |
|
186 final String location = LocationProvider.getLocation(river.getName(), km); |
|
187 |
|
188 // REMARK: access the gauge once only during calculation |
|
189 final Gauge gauge = findGauge(waterlevel, refGauge, gaugeIndex, km); |
|
190 |
|
191 final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); |
|
192 |
8884
|
193 resultData.addRow(km, flowDepth, flowDepthTkh, tkh, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, bedHeightLabel, location); |
8883
|
194 } |
|
195 catch (final FunctionEvaluationException e) { |
|
196 /* should only happen if out of range */ |
|
197 e.printStackTrace(); |
|
198 /* simply ignore */ |
|
199 } |
|
200 |
8877
|
201 } |
|
202 |
|
203 return resultData; |
|
204 } |
|
205 |
8883
|
206 /** |
|
207 * Checks the year difference between waterlevels and sounding, and issues a warning if too big. |
|
208 * |
|
209 * Zeitraum Zeitliche Differenz [a] |
|
210 * X ≥ 1998 ± 3 |
|
211 * 1958 ≤ X < 1998 ± 6 |
|
212 * 1918 ≤ X < 1958 ± 12 |
|
213 * X < 1918 ± 25 |
|
214 */ |
8884
|
215 private void checkYearDifference(final String label, final WaterlevelData waterlevel, final BedHeight sounding, final Calculation problems) { |
8883
|
216 |
|
217 final Integer soundingYear = sounding.getYear(); |
|
218 if (soundingYear == null) |
|
219 return; |
|
220 |
|
221 final int wstYear = waterlevel.getYear(); |
|
222 if (wstYear < 0) |
|
223 return; |
|
224 |
|
225 final int maxDifference = getMaxDifferenceYears(soundingYear); |
|
226 |
|
227 final int difference = Math.abs(soundingYear - wstYear); |
|
228 if (difference > maxDifference) { |
8884
|
229 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.year_difference", null, label, difference); |
8883
|
230 problems.addProblem(message); |
|
231 } |
|
232 } |
|
233 |
|
234 private int getMaxDifferenceYears(final int year) { |
|
235 |
|
236 if (year < 1918) |
|
237 return 25; |
|
238 |
|
239 if (1918 <= year && year < 1958) |
|
240 return 12; |
|
241 |
|
242 if (1958 <= year && year < 1998) |
|
243 return 6; |
|
244 |
|
245 /* >= 1998 */ |
|
246 return 3; |
|
247 } |
|
248 |
8884
|
249 private Gauge findGauge(final WaterlevelData waterlevel, final Gauge refGauge, final GaugeIndex gaugeIndex, final double km) { |
8882
|
250 |
|
251 // REMARK: using same logic as in WaterlevelExporter here |
|
252 |
|
253 final boolean showAllGauges = waterlevel.isShowAllGauges(); |
|
254 |
|
255 if (showAllGauges) |
|
256 return gaugeIndex.findGauge(km); |
|
257 |
|
258 if (refGauge.getRange().contains(km)) |
|
259 return refGauge; |
|
260 |
|
261 return null; |
|
262 } |
|
263 |
|
264 /* Checks if the discretisation of the waterlevel exceeds 1000m */ |
8884
|
265 // FIXME: vermutlich sollten wir diesen check auf den gültigkeitsbereich einschränken |
8882
|
266 private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) { |
|
267 final int size = wstKms.size(); |
|
268 for (int i = 0; i < size - 2; i++) { |
|
269 final double kmPrev = wstKms.getKm(i); |
|
270 final double kmNext = wstKms.getKm(i + 1); |
|
271 |
|
272 if (Math.abs(kmPrev - kmNext) > 1) { |
|
273 final String label = wstKms.getName(); |
|
274 |
8884
|
275 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label); |
8882
|
276 problems.addProblem(kmPrev, message); |
|
277 } |
|
278 } |
|
279 } |
|
280 |
8877
|
281 private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { |
|
282 |
8883
|
283 // REMARK: absolutely unbelievable.... |
8882
|
284 // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type |
|
285 // throughout flys. |
8877
|
286 // The knowledge on how to parse the datacage-ids is spread through the complete code-base... |
|
287 |
8882
|
288 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but |
|
289 // this is plain random |
8877
|
290 final String[] parts = soundingId.split(";"); |
|
291 |
|
292 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); |
|
293 |
|
294 final Integer bedheightId = artifact.getDataAsInteger("height_id"); |
8883
|
295 // REMARK: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the |
8882
|
296 // other type means) |
8877
|
297 // Luckily, the requirement is to only access 'single' data here. |
|
298 // final String bedheightType = artifact.getDataAsString("type"); |
|
299 |
8883
|
300 // REMARK: BedDifferences uses this, but we also need the metadata of the BedHeight |
|
301 // REMARK: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via |
8882
|
302 // hibernate stuff |
8877
|
303 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
8882
|
304 // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
8877
|
305 |
|
306 return BedHeight.getBedHeightById(bedheightId); |
|
307 } |
8884
|
308 } |