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 |
|
15 import org.dive4elements.artifacts.CallContext; |
|
16 import org.dive4elements.river.artifacts.BedHeightsArtifact; |
|
17 import org.dive4elements.river.artifacts.model.Calculation; |
|
18 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
19 import org.dive4elements.river.artifacts.model.LocationProvider; |
8877
|
20 import org.dive4elements.river.artifacts.model.QKms; |
8854
|
21 import org.dive4elements.river.artifacts.model.WKms; |
|
22 import org.dive4elements.river.artifacts.resources.Resources; |
|
23 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; |
|
24 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair; |
|
25 import org.dive4elements.river.artifacts.states.WDifferencesState; |
|
26 import org.dive4elements.river.model.BedHeight; |
|
27 import org.dive4elements.river.model.Gauge; |
|
28 import org.dive4elements.river.model.River; |
|
29 import org.dive4elements.river.utils.GaugeIndex; |
|
30 import org.dive4elements.river.utils.RiverUtils; |
|
31 |
|
32 class FlowDepthCalculation { |
|
33 |
|
34 private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; |
|
35 |
8877
|
36 private final CallContext context; |
8854
|
37 |
8877
|
38 public FlowDepthCalculation( final CallContext context ) { |
|
39 this.context = context; |
8854
|
40 } |
|
41 |
8877
|
42 public CalculationResult calculate(final SINFOArtifact sinfo) { |
8854
|
43 |
8877
|
44 // FIXME: find user of this artifact; probably only possible by selecting the collection that contains this artifact... |
|
45 final String user = "unbekannt"; |
8854
|
46 |
8877
|
47 /* access input data */ |
|
48 final FlowDepthAccess access = new FlowDepthAccess(sinfo); |
|
49 final River river = access.getRiver(); |
8854
|
50 |
8877
|
51 final Collection<DifferencesPair> diffPairs = access.getDifferencePairs(); |
|
52 |
|
53 final double from = access.getFrom(); |
|
54 final double to = access.getTo(); |
|
55 |
|
56 final boolean useTkh = access.isUseTransportBodies(); |
|
57 |
|
58 /* calculate results for each diff pair */ |
|
59 final Calculation problems = new Calculation(); |
|
60 |
|
61 final List<Gauge> gauges = river.determineGauges(from, to); |
|
62 final GaugeIndex gaugeIndex = new GaugeIndex(gauges); |
|
63 |
|
64 final String calcModeLabel = Resources.getMsg(this.context.getMeta(),sinfo.getCalculationMode().name() ); |
|
65 |
|
66 final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, to, useTkh); |
|
67 |
|
68 for (final DifferencesPair diffPair : diffPairs) { |
|
69 final FlowDepthCalculationResult result = calculateResult( river, from, to, diffPair, problems, gaugeIndex ); |
|
70 if( result != null ) |
|
71 results.addResult(result); |
|
72 } |
|
73 |
|
74 return new CalculationResult(results,problems); |
|
75 } |
|
76 |
|
77 private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, final DifferencesPair diffPair, final Calculation problems, final GaugeIndex gaugeIndex) { |
|
78 |
|
79 /* access real input data from database */ |
|
80 final String soundingId = diffPair.getSoundingId(); |
|
81 final String wstId = diffPair.getWstId(); |
|
82 |
|
83 final BedHeight bedHeight = loadBedHeight( soundingId, from, to ); |
|
84 if( bedHeight == null ) |
|
85 { |
|
86 final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", soundingId); |
|
87 problems.addProblem(message); |
|
88 return null; |
|
89 } |
|
90 |
|
91 final WKms wstKms = new WDifferencesState().getWKms(wstId, this.context, from, to); |
|
92 if( wstKms == null ) |
|
93 { |
|
94 final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", wstId); |
|
95 problems.addProblem(message); |
|
96 return null; |
|
97 } |
|
98 |
|
99 // FIXME: woher bekommen? |
|
100 final int wspYear = 0; |
|
101 |
|
102 final String wspLabel = wstKms.getName(); |
|
103 final String soundingLabel = bedHeight.getDescription(); |
|
104 final String label = String.format("%s - %s", wspLabel, soundingLabel); |
|
105 |
|
106 final BedHeightInfo sounding = BedHeightInfo.from(bedHeight); |
|
107 final WstInfo wstInfo = new WstInfo(wspLabel, wspYear); |
|
108 |
|
109 final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(label, wstInfo, sounding); |
|
110 |
|
111 final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE); |
|
112 |
|
113 // TODO: prüfe diskretisierung wsp --> > 1000m --> Fehlermeldung |
|
114 |
|
115 // TODO: Berechnung der Transportkörperhöhen |
|
116 // - woher kommen die zusätzlichen eingangsdaten? sind das fixe daten pro gewässer? --> falls ja, warum nicht einmal berechnen und in db ablegen? |
|
117 |
|
118 // Benötigte Daten |
|
119 // - Abfluss / Station |
|
120 // - kein Abfluss --> Fehler |
|
121 if( !(wstKms instanceof QKms)) |
|
122 { |
|
123 final String message = Resources.format(this.context.getMeta(), "{0}: keine Abflussdaten vorhanden, Transportkörperhöhenberechnung nicht möglich", label); |
|
124 problems.addProblem(message); |
|
125 // TODO: keine Berechnung TKH |
|
126 } |
|
127 |
|
128 // - Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb) |
|
129 // - Abhängig von Peiljahr |
|
130 // - kein D50 vorhanden --> Fehler |
|
131 // - Art der Gewässersohle (starr/mobil) |
|
132 |
|
133 |
|
134 final String bedHeightLabel = bedHeight.getDescription(); |
|
135 final String wstLabel = wstKms.getName(); |
|
136 |
|
137 // FIXME: basis der diskretisierung ist bedHeight, die wspl werden interpoliert |
|
138 for (int i = 0; i < wstKms.size(); i++) { |
|
139 |
|
140 final double km = wstKms.getKm(i); |
|
141 final double wst = wstKms.getW(i); |
|
142 // FIXME: interpolate from bedheights? |
|
143 final double meanBedHeight = 79.32; |
|
144 |
|
145 final double flowDepth = wst - meanBedHeight; |
|
146 |
|
147 final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN; |
|
148 |
|
149 // FIXME: calculate tkh |
|
150 final double tkh = 0; |
|
151 final double flowDepthTkh = flowDepth - tkh; |
|
152 |
|
153 |
|
154 // REMARK: access the location once only during calculation |
|
155 final String location = LocationProvider.getLocation(river.getName(), km); |
|
156 |
|
157 // REMARK: access the gauge once only during calculation |
|
158 // FIXME: copy specific handling from original wst |
|
159 final Gauge gauge = gaugeIndex.findGauge(km); |
|
160 final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); |
|
161 |
|
162 resultData.addRow( km, flowDepth, flowDepthTkh, tkh, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, bedHeightLabel, location ); |
|
163 } |
|
164 |
|
165 return resultData; |
|
166 } |
|
167 |
|
168 private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { |
|
169 |
|
170 // FIXME: absolutely unbelievable.... |
|
171 // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type throughout flys. |
|
172 // The knowledge on how to parse the datacage-ids is spread through the complete code-base... |
|
173 |
|
174 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but this is plain random |
|
175 final String[] parts = soundingId.split(";"); |
|
176 |
|
177 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); |
|
178 |
|
179 final Integer bedheightId = artifact.getDataAsInteger("height_id"); |
|
180 // FIXME: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the other type means) |
|
181 // Luckily, the requirement is to only access 'single' data here. |
|
182 // final String bedheightType = artifact.getDataAsString("type"); |
|
183 |
|
184 // FIXME: BedDifferences uses this, but we also need the metadata of the BedHeight |
|
185 // FIXME: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via hibernate stuff |
|
186 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
|
187 //return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
|
188 |
|
189 return BedHeight.getBedHeightById(bedheightId); |
|
190 } |
8854
|
191 } |