Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthdev/FlowDepthDevelopmentCalculation.java @ 8978:b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
Also check for years of waterlevels in flow depth development calculation.
author | gernotbelger |
---|---|
date | Thu, 05 Apr 2018 11:57:54 +0200 |
parents | 45f1ad66560e |
children | b194fa64506a |
rev | line source |
---|---|
8951 | 1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
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.flowdepthdev; | |
11 | |
12 import java.util.ArrayList; | |
13 import java.util.Collection; | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
14 import java.util.TreeSet; |
8951 | 15 |
16 import org.apache.commons.lang.math.DoubleRange; | |
17 import org.dive4elements.artifacts.CallContext; | |
18 import org.dive4elements.river.artifacts.model.Calculation; | |
19 import org.dive4elements.river.artifacts.model.CalculationResult; | |
20 import org.dive4elements.river.artifacts.model.WKms; | |
21 import org.dive4elements.river.artifacts.resources.Resources; | |
22 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; | |
23 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; | |
24 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultRow; | |
25 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
26 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthUtils; | |
27 import org.dive4elements.river.artifacts.sinfo.flowdepth.WstSoundingIdPair; | |
28 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.WaterlevelValuesFinder; | |
29 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; | |
30 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; | |
31 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; | |
32 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
33 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
34 import org.dive4elements.river.artifacts.states.WaterlevelData; | |
35 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; | |
36 import org.dive4elements.river.model.River; | |
37 | |
38 /** | |
39 * @author Gernot Belger | |
40 */ | |
41 final class FlowDepthDevelopmentCalculation { | |
42 | |
43 private final CallContext context; | |
44 | |
45 public FlowDepthDevelopmentCalculation(final CallContext context) { | |
46 this.context = context; | |
47 } | |
48 | |
49 public CalculationResult calculate(final SINFOArtifact sinfo) { | |
50 | |
51 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); | |
52 | |
53 /* access input data */ | |
54 final FlowDepthDevelopmentAccess access = new FlowDepthDevelopmentAccess(sinfo); | |
55 final River river = access.getRiver(); | |
56 final RiverInfo riverInfo = new RiverInfo(river); | |
57 | |
58 final WstSoundingIdPair currentPair = access.getCurrentPair(); | |
59 final WstSoundingIdPair histPair = access.getHistoricalPair(); | |
60 | |
61 final DoubleRange calcRange = access.getRange(); | |
62 | |
63 /* calculate results for each diff pair */ | |
64 final Calculation problems = new Calculation(); | |
65 | |
66 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); | |
67 | |
68 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); | |
69 | |
70 final FlowDepthDevelopmentCalculationResults results = new FlowDepthDevelopmentCalculationResults(calcModeLabel, user, riverInfo, calcRange); | |
71 | |
72 final FlowDepthDevelopmentCalculationResult result = calculateResult(calcRange, currentPair, histPair, problems, infoProvider); | |
73 if (result != null) | |
74 results.addResult(result); | |
75 | |
76 return new CalculationResult(results, problems); | |
77 } | |
78 | |
79 private FlowDepthDevelopmentCalculationResult calculateResult(final DoubleRange calcRange, final WstSoundingIdPair currentPair, | |
80 final WstSoundingIdPair histPair, final Calculation problems, final RiverInfoProvider infoProvider) { | |
81 | |
82 /* access real input data from database */ | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
83 final WaterlevelData currentWaterlevel = loadWaterlevel(currentPair, calcRange, problems); |
8951 | 84 if (currentWaterlevel == null) |
85 return null; | |
86 | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
87 final WaterlevelData historicalWaterlevel = loadWaterlevel(histPair, calcRange, problems); |
8951 | 88 if (historicalWaterlevel == null) |
89 return null; | |
90 | |
91 final BedHeightsFinder currentSounding = loadBedHeight(currentPair, calcRange, problems); | |
92 if (currentSounding == null) | |
93 return null; | |
94 | |
95 final BedHeightsFinder historicalSounding = loadBedHeight(histPair, calcRange, problems); | |
96 if (historicalSounding == null) | |
97 return null; | |
98 | |
99 // FIXME: check current/hist wst have same discharge... | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
100 // FIXME: what means 'same discharge' |
8951 | 101 |
102 final BedHeightInfo currentSoundingInfo = currentSounding.getInfo(); | |
103 final BedHeightInfo historicalSoundingInfo = historicalSounding.getInfo(); | |
104 | |
105 final int currentWstYear = currentWaterlevel.getYear(); | |
106 final int historicalWstYear = historicalWaterlevel.getYear(); | |
107 final int currentSoundingYear = currentSoundingInfo.getYear(); | |
108 final int historicalSoundingYear = historicalSoundingInfo.getYear(); | |
109 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
110 if (currentWstYear < 0) { |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
111 problems.addProblem("flowdepthdevelopmentcalculation.missingCurrentYear", currentWaterlevel.getName()); |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
112 return null; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
113 } |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
114 |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
115 if (historicalWstYear < 0) { |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
116 problems.addProblem("flowdepthdevelopmentcalculation.missingHistoricalYear", historicalWaterlevel.getName()); |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
117 return null; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
118 } |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
119 |
8951 | 120 // FIXME: distinguish error messages |
121 FlowDepthUtils.checkYearDifference("", currentWstYear, currentSoundingYear, problems); | |
122 FlowDepthUtils.checkYearDifference("", historicalWstYear, historicalSoundingYear, problems); | |
123 | |
124 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
125 final RiverInfoProvider currentRiverInfoProvider = infoProvider.forWaterlevel(currentWaterlevel); | |
126 final RiverInfoProvider histRiverInfoProvider = infoProvider.forWaterlevel(historicalWaterlevel); | |
127 | |
128 final WstInfo currentWstInfo = new WstInfo(currentWaterlevel.getName(), currentWstYear, currentRiverInfoProvider.getReferenceGauge()); | |
129 final WstInfo historicalWstInfo = new WstInfo(historicalWaterlevel.getName(), historicalWstYear, histRiverInfoProvider.getReferenceGauge()); | |
130 | |
131 final WKms currentWkms = currentWaterlevel.getWkms(); | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
132 final WaterlevelValuesFinder currentWstProvider = WaterlevelValuesFinder.fromKms(problems, currentWkms); |
8951 | 133 |
134 final WKms historicalWkms = historicalWaterlevel.getWkms(); | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
135 final WaterlevelValuesFinder historicalWstProvider = WaterlevelValuesFinder.fromKms(problems, historicalWkms); |
8951 | 136 |
137 final int currentMeanYear = (currentWstYear + currentSoundingYear) / 2; | |
138 final int historcialMeanYear = (historicalWstYear + historicalSoundingYear) / 2; | |
139 | |
140 final double diffYear = currentMeanYear - historcialMeanYear; | |
141 | |
142 /* real calculation loop */ | |
143 final Collection<SInfoResultRow> rows = new ArrayList<>(); | |
144 | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
145 final Collection<Double> stations = determineCalculationSteps(currentSounding, historicalSounding); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
146 for (final double station : stations) { |
8951 | 147 if (calcRange.containsDouble(station)) { |
148 | |
149 final double currentWst = currentWstProvider.getWaterlevel(station); | |
150 final double currentBedHeight = currentSounding.getMeanBedHeight(station); | |
151 | |
152 final double historicalWst = historicalWstProvider.getWaterlevel(station); | |
153 final double historicalBedHeight = historicalSounding.getMeanBedHeight(station); | |
154 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
155 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
156 if (Double.isNaN(currentWst) || Double.isNaN(currentBedHeight) || Double.isNaN(historicalWst) || Double.isNaN(historicalBedHeight)) |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
157 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
158 |
8951 | 159 final double diffWst = (currentWst - historicalWst) * 100; |
160 final double diffBedHeight = (currentBedHeight - historicalBedHeight) * 100; | |
161 | |
162 final double flowDepthDevelopment = diffWst - diffBedHeight; | |
163 | |
164 final double flowDepthDevelopmentPerYear = flowDepthDevelopment / diffYear; | |
165 | |
166 final double currentFlowDepth = currentWst - currentBedHeight; | |
167 final double historicalFlowDepth = historicalWst - historicalBedHeight; | |
168 | |
169 // REMARK: access the location once only during calculation | |
170 final String location = currentRiverInfoProvider.getLocation(station); | |
171 | |
172 final SInfoResultRow row = SInfoResultRow.create().// | |
173 putValue(SInfoResultType.station, station). // | |
174 putValue(SInfoResultType.flowdepthDevelopment, flowDepthDevelopment). // | |
175 putValue(SInfoResultType.flowdepthDevelopmentPerYear, flowDepthDevelopmentPerYear). // | |
176 putValue(SInfoResultType.waterlevelDifference, diffWst). // | |
177 putValue(SInfoResultType.bedHeightDifference, diffBedHeight). // | |
178 putValue(SInfoResultType.flowdepthCurrent, currentFlowDepth). // | |
179 putValue(SInfoResultType.flowdepthHistorical, historicalFlowDepth). // | |
180 putValue(SInfoResultType.location, location); | |
181 rows.add(row); | |
182 } | |
183 } | |
184 | |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
185 final String label = buildLabel(currentWaterlevel, currentSoundingInfo, historicalWaterlevel, historicalSoundingInfo); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
186 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
187 return new FlowDepthDevelopmentCalculationResult(label, currentWstInfo, historicalWstInfo, currentSoundingInfo, historicalSoundingInfo, |
8951 | 188 rows); |
189 } | |
190 | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
191 /** |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
192 * Calculation steps are simply the union of all stations of all involved bed-height datasets |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
193 */ |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
194 private Collection<Double> determineCalculationSteps(final BedHeightsFinder currentSounding, final BedHeightsFinder historicalSounding) { |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
195 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
196 final Collection<Double> allStations = new TreeSet<>(); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
197 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
198 allStations.addAll(currentSounding.getStations()); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
199 allStations.addAll(historicalSounding.getStations()); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
200 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
201 return allStations; |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
202 } |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
203 |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
204 private String buildLabel(final WaterlevelData currentWaterlevel, final BedHeightInfo currentSounding, final WaterlevelData historicalWaterlevel, |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
205 final BedHeightInfo historicalSounding) { |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
206 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
207 return new StringBuilder(). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
208 append(currentWaterlevel.getName()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
209 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
210 append(historicalWaterlevel.getName()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
211 append(" - "). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
212 append(currentSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
213 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
214 append(historicalSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
215 toString(); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
216 } |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
217 |
8951 | 218 /* REMARK: fetch ALL wst kms, because we need to determine the original reference gauge */ |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
219 private WaterlevelData loadWaterlevel(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { |
8951 | 220 final String wstId = pair.getWstId(); |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
221 return new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8951 | 222 } |
223 | |
224 private BedHeightsFinder loadBedHeight(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { | |
225 final String soundingId = pair.getSoundingId(); | |
226 return BedHeightsFinder.forId(this.context, soundingId, calcRange, problems); | |
227 } | |
228 } |