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 |
|
12 import java.util.Collection; |
|
13 import java.util.List; |
|
14 |
8879
|
15 import org.dive4elements.artifacts.ArtifactDatabase; |
8854
|
16 import org.dive4elements.artifacts.CallContext; |
|
17 import org.dive4elements.river.artifacts.BedHeightsArtifact; |
|
18 import org.dive4elements.river.artifacts.model.Calculation; |
|
19 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
20 import org.dive4elements.river.artifacts.model.LocationProvider; |
8877
|
21 import org.dive4elements.river.artifacts.model.QKms; |
8854
|
22 import org.dive4elements.river.artifacts.model.WKms; |
|
23 import org.dive4elements.river.artifacts.resources.Resources; |
|
24 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; |
|
25 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair; |
8882
|
26 import org.dive4elements.river.artifacts.states.WaterlevelData; |
|
27 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; |
8854
|
28 import org.dive4elements.river.model.BedHeight; |
|
29 import org.dive4elements.river.model.Gauge; |
|
30 import org.dive4elements.river.model.River; |
|
31 import org.dive4elements.river.utils.GaugeIndex; |
|
32 import org.dive4elements.river.utils.RiverUtils; |
|
33 |
|
34 class FlowDepthCalculation { |
|
35 |
|
36 private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; |
|
37 |
8877
|
38 private final CallContext context; |
8854
|
39 |
8882
|
40 public FlowDepthCalculation(final CallContext context) { |
8877
|
41 this.context = context; |
8854
|
42 } |
|
43 |
8877
|
44 public CalculationResult calculate(final SINFOArtifact sinfo) { |
8854
|
45 |
8879
|
46 /* |
|
47 * find the user of this artifact, sadly this is not part of the calling context, so instead we determine the |
|
48 * owner oft the artifact |
|
49 */ |
|
50 final ArtifactDatabase database = this.context.getDatabase(); |
|
51 final String user = database.findArtifactUser(sinfo.identifier()); |
8854
|
52 |
8877
|
53 /* access input data */ |
|
54 final FlowDepthAccess access = new FlowDepthAccess(sinfo); |
|
55 final River river = access.getRiver(); |
8854
|
56 |
8877
|
57 final Collection<DifferencesPair> diffPairs = access.getDifferencePairs(); |
|
58 |
|
59 final double from = access.getFrom(); |
|
60 final double to = access.getTo(); |
|
61 |
|
62 final boolean useTkh = access.isUseTransportBodies(); |
|
63 |
|
64 /* calculate results for each diff pair */ |
|
65 final Calculation problems = new Calculation(); |
|
66 |
|
67 final List<Gauge> gauges = river.determineGauges(from, to); |
|
68 final GaugeIndex gaugeIndex = new GaugeIndex(gauges); |
|
69 |
8882
|
70 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); |
8877
|
71 |
8882
|
72 final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, |
|
73 to, useTkh); |
8877
|
74 |
|
75 for (final DifferencesPair diffPair : diffPairs) { |
8882
|
76 final FlowDepthCalculationResult result = calculateResult(river, from, to, diffPair, problems, gaugeIndex); |
|
77 if (result != null) |
8877
|
78 results.addResult(result); |
|
79 } |
|
80 |
8882
|
81 return new CalculationResult(results, problems); |
8877
|
82 } |
|
83 |
8882
|
84 private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, |
|
85 final DifferencesPair diffPair, final Calculation problems, final GaugeIndex gaugeIndex) { |
8877
|
86 |
|
87 /* access real input data from database */ |
|
88 final String soundingId = diffPair.getSoundingId(); |
|
89 final String wstId = diffPair.getWstId(); |
|
90 |
8882
|
91 final BedHeight bedHeight = loadBedHeight(soundingId, from, to); |
|
92 if (bedHeight == null) { |
|
93 final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", |
|
94 soundingId); |
8877
|
95 problems.addProblem(message); |
|
96 return null; |
|
97 } |
|
98 |
8882
|
99 /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */ |
|
100 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, |
|
101 Double.NaN); |
|
102 if (waterlevel == null) { |
|
103 final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", |
|
104 wstId); |
8877
|
105 problems.addProblem(message); |
|
106 return null; |
|
107 } |
8882
|
108 final WKms wstKms = waterlevel.getWkms(); |
|
109 |
|
110 checkWaterlevelDiscretisation(wstKms, problems); |
8877
|
111 |
|
112 // FIXME: woher bekommen? |
|
113 final int wspYear = 0; |
8882
|
114 |
|
115 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ |
|
116 final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, |
|
117 CSV_NOT_IN_GAUGE_RANGE); |
|
118 |
|
119 final Gauge refGauge = waterlevel.findReferenceGauge(river); |
|
120 final String refGaugeName = refGauge == null ? notinrange : refGauge.getName(); |
8877
|
121 |
|
122 final String wspLabel = wstKms.getName(); |
|
123 final String soundingLabel = bedHeight.getDescription(); |
|
124 final String label = String.format("%s - %s", wspLabel, soundingLabel); |
|
125 |
|
126 final BedHeightInfo sounding = BedHeightInfo.from(bedHeight); |
8882
|
127 final WstInfo wstInfo = new WstInfo(wspLabel, wspYear, refGaugeName); |
8877
|
128 |
|
129 final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(label, wstInfo, sounding); |
|
130 |
|
131 // TODO: Berechnung der Transportkörperhöhen |
8882
|
132 // - woher kommen die zusätzlichen eingangsdaten? sind das fixe daten pro gewässer? --> falls ja, warum nicht |
|
133 // einmal berechnen und in db ablegen? |
8877
|
134 |
|
135 // Benötigte Daten |
|
136 // - Abfluss / Station |
8882
|
137 // - kein Abfluss --> Fehler |
|
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 |
8877
|
148 // - Art der Gewässersohle (starr/mobil) |
|
149 |
|
150 final String bedHeightLabel = bedHeight.getDescription(); |
|
151 final String wstLabel = wstKms.getName(); |
|
152 |
|
153 // FIXME: basis der diskretisierung ist bedHeight, die wspl werden interpoliert |
|
154 for (int i = 0; i < wstKms.size(); i++) { |
|
155 |
|
156 final double km = wstKms.getKm(i); |
|
157 final double wst = wstKms.getW(i); |
|
158 // FIXME: interpolate from bedheights? |
|
159 final double meanBedHeight = 79.32; |
|
160 |
|
161 final double flowDepth = wst - meanBedHeight; |
|
162 |
|
163 final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN; |
|
164 |
|
165 // FIXME: calculate tkh |
|
166 final double tkh = 0; |
|
167 final double flowDepthTkh = flowDepth - tkh; |
|
168 |
|
169 // REMARK: access the location once only during calculation |
|
170 final String location = LocationProvider.getLocation(river.getName(), km); |
|
171 |
|
172 // REMARK: access the gauge once only during calculation |
8882
|
173 final Gauge gauge = findGauge(waterlevel, refGauge, gaugeIndex, km); |
|
174 |
8877
|
175 final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); |
|
176 |
8882
|
177 resultData.addRow(km, flowDepth, flowDepthTkh, tkh, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, |
|
178 bedHeightLabel, location); |
8877
|
179 } |
|
180 |
|
181 return resultData; |
|
182 } |
|
183 |
8882
|
184 private Gauge findGauge(final WaterlevelData waterlevel, final Gauge refGauge, final GaugeIndex gaugeIndex, |
|
185 final double km) { |
|
186 |
|
187 // REMARK: using same logic as in WaterlevelExporter here |
|
188 |
|
189 final boolean showAllGauges = waterlevel.isShowAllGauges(); |
|
190 |
|
191 if (showAllGauges) |
|
192 return gaugeIndex.findGauge(km); |
|
193 |
|
194 if (refGauge.getRange().contains(km)) |
|
195 return refGauge; |
|
196 |
|
197 return null; |
|
198 } |
|
199 |
|
200 /* Checks if the discretisation of the waterlevel exceeds 1000m */ |
|
201 private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) { |
|
202 final int size = wstKms.size(); |
|
203 for (int i = 0; i < size - 2; i++) { |
|
204 final double kmPrev = wstKms.getKm(i); |
|
205 final double kmNext = wstKms.getKm(i + 1); |
|
206 |
|
207 if (Math.abs(kmPrev - kmNext) > 1) { |
|
208 final String label = wstKms.getName(); |
|
209 |
|
210 final String message = Resources.getMsg(this.context.getMeta(), |
|
211 "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label); |
|
212 problems.addProblem(kmPrev, message); |
|
213 } |
|
214 } |
|
215 } |
|
216 |
8877
|
217 private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { |
|
218 |
|
219 // FIXME: absolutely unbelievable.... |
8882
|
220 // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type |
|
221 // throughout flys. |
8877
|
222 // The knowledge on how to parse the datacage-ids is spread through the complete code-base... |
|
223 |
8882
|
224 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but |
|
225 // this is plain random |
8877
|
226 final String[] parts = soundingId.split(";"); |
|
227 |
|
228 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); |
|
229 |
|
230 final Integer bedheightId = artifact.getDataAsInteger("height_id"); |
8882
|
231 // FIXME: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the |
|
232 // other type means) |
8877
|
233 // Luckily, the requirement is to only access 'single' data here. |
|
234 // final String bedheightType = artifact.getDataAsString("type"); |
|
235 |
|
236 // FIXME: BedDifferences uses this, but we also need the metadata of the BedHeight |
8882
|
237 // FIXME: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via |
|
238 // hibernate stuff |
8877
|
239 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
8882
|
240 // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
8877
|
241 |
|
242 return BedHeight.getBedHeightById(bedheightId); |
|
243 } |
8854
|
244 } |