Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthdev/FlowDepthDevelopmentCalculation.java @ 8980:b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
Some improvements to error handling and handling of empty results.
author | gernotbelger |
---|---|
date | Thu, 05 Apr 2018 18:30:34 +0200 |
parents | b5600453bb8f |
children | d5802f22e4f5 |
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); | |
8980
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
73 results.addResult(result, problems); |
8951 | 74 |
75 return new CalculationResult(results, problems); | |
76 } | |
77 | |
78 private FlowDepthDevelopmentCalculationResult calculateResult(final DoubleRange calcRange, final WstSoundingIdPair currentPair, | |
79 final WstSoundingIdPair histPair, final Calculation problems, final RiverInfoProvider infoProvider) { | |
80 | |
81 /* 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
|
82 final WaterlevelData currentWaterlevel = loadWaterlevel(currentPair, calcRange, problems); |
8951 | 83 if (currentWaterlevel == null) |
84 return null; | |
85 | |
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
|
86 final WaterlevelData historicalWaterlevel = loadWaterlevel(histPair, calcRange, problems); |
8951 | 87 if (historicalWaterlevel == null) |
88 return null; | |
89 | |
90 final BedHeightsFinder currentSounding = loadBedHeight(currentPair, calcRange, problems); | |
91 if (currentSounding == null) | |
92 return null; | |
93 | |
94 final BedHeightsFinder historicalSounding = loadBedHeight(histPair, calcRange, problems); | |
95 if (historicalSounding == null) | |
96 return null; | |
97 | |
98 // 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
|
99 // FIXME: what means 'same discharge' |
8951 | 100 |
101 final BedHeightInfo currentSoundingInfo = currentSounding.getInfo(); | |
102 final BedHeightInfo historicalSoundingInfo = historicalSounding.getInfo(); | |
103 | |
104 final int currentWstYear = currentWaterlevel.getYear(); | |
105 final int historicalWstYear = historicalWaterlevel.getYear(); | |
106 final int currentSoundingYear = currentSoundingInfo.getYear(); | |
107 final int historicalSoundingYear = historicalSoundingInfo.getYear(); | |
108 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
109 if (currentWstYear < 0) { |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
110 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
|
111 return null; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
112 } |
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 if (historicalWstYear < 0) { |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
115 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
|
116 return null; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
117 } |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
118 |
8951 | 119 // FIXME: distinguish error messages |
8980
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
120 FlowDepthUtils.checkYearDifference(Resources.getMsg(this.context.getMeta(), "flowdepthdevelopmentcalculation.yearDifferenceCurrent"), currentWstYear, |
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
121 currentSoundingYear, problems); |
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
122 FlowDepthUtils.checkYearDifference(Resources.getMsg(this.context.getMeta(), "flowdepthdevelopmentcalculation.yearDifferenceHistorical"), |
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
123 historicalWstYear, |
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
124 historicalSoundingYear, problems); |
8951 | 125 |
126 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
127 final RiverInfoProvider currentRiverInfoProvider = infoProvider.forWaterlevel(currentWaterlevel); | |
128 final RiverInfoProvider histRiverInfoProvider = infoProvider.forWaterlevel(historicalWaterlevel); | |
129 | |
130 final WstInfo currentWstInfo = new WstInfo(currentWaterlevel.getName(), currentWstYear, currentRiverInfoProvider.getReferenceGauge()); | |
131 final WstInfo historicalWstInfo = new WstInfo(historicalWaterlevel.getName(), historicalWstYear, histRiverInfoProvider.getReferenceGauge()); | |
132 | |
133 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
|
134 final WaterlevelValuesFinder currentWstProvider = WaterlevelValuesFinder.fromKms(problems, currentWkms); |
8951 | 135 |
136 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
|
137 final WaterlevelValuesFinder historicalWstProvider = WaterlevelValuesFinder.fromKms(problems, historicalWkms); |
8951 | 138 |
139 final int currentMeanYear = (currentWstYear + currentSoundingYear) / 2; | |
140 final int historcialMeanYear = (historicalWstYear + historicalSoundingYear) / 2; | |
141 | |
142 final double diffYear = currentMeanYear - historcialMeanYear; | |
143 | |
144 /* real calculation loop */ | |
145 final Collection<SInfoResultRow> rows = new ArrayList<>(); | |
146 | |
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
|
147 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
|
148 for (final double station : stations) { |
8951 | 149 if (calcRange.containsDouble(station)) { |
150 | |
151 final double currentWst = currentWstProvider.getWaterlevel(station); | |
152 final double currentBedHeight = currentSounding.getMeanBedHeight(station); | |
153 | |
154 final double historicalWst = historicalWstProvider.getWaterlevel(station); | |
155 final double historicalBedHeight = historicalSounding.getMeanBedHeight(station); | |
156 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
157 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
158 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
|
159 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
160 |
8951 | 161 final double diffWst = (currentWst - historicalWst) * 100; |
162 final double diffBedHeight = (currentBedHeight - historicalBedHeight) * 100; | |
163 | |
164 final double flowDepthDevelopment = diffWst - diffBedHeight; | |
165 | |
166 final double flowDepthDevelopmentPerYear = flowDepthDevelopment / diffYear; | |
167 | |
168 final double currentFlowDepth = currentWst - currentBedHeight; | |
169 final double historicalFlowDepth = historicalWst - historicalBedHeight; | |
170 | |
171 // REMARK: access the location once only during calculation | |
172 final String location = currentRiverInfoProvider.getLocation(station); | |
173 | |
174 final SInfoResultRow row = SInfoResultRow.create().// | |
175 putValue(SInfoResultType.station, station). // | |
176 putValue(SInfoResultType.flowdepthDevelopment, flowDepthDevelopment). // | |
177 putValue(SInfoResultType.flowdepthDevelopmentPerYear, flowDepthDevelopmentPerYear). // | |
178 putValue(SInfoResultType.waterlevelDifference, diffWst). // | |
179 putValue(SInfoResultType.bedHeightDifference, diffBedHeight). // | |
180 putValue(SInfoResultType.flowdepthCurrent, currentFlowDepth). // | |
181 putValue(SInfoResultType.flowdepthHistorical, historicalFlowDepth). // | |
182 putValue(SInfoResultType.location, location); | |
183 rows.add(row); | |
184 } | |
185 } | |
186 | |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
187 final String label = buildLabel(currentWaterlevel, currentSoundingInfo, historicalWaterlevel, historicalSoundingInfo); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
188 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
189 return new FlowDepthDevelopmentCalculationResult(label, currentWstInfo, historicalWstInfo, currentSoundingInfo, historicalSoundingInfo, |
8951 | 190 rows); |
191 } | |
192 | |
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
|
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 * 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
|
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 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
|
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 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
|
199 |
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 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
|
201 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
|
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 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
|
204 } |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
205 |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
206 private String buildLabel(final WaterlevelData currentWaterlevel, final BedHeightInfo currentSounding, final WaterlevelData historicalWaterlevel, |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
207 final BedHeightInfo historicalSounding) { |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
208 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
209 return new StringBuilder(). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
210 append(currentWaterlevel.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(historicalWaterlevel.getName()). // |
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(currentSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
215 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
216 append(historicalSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
217 toString(); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
218 } |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
219 |
8951 | 220 /* 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
|
221 private WaterlevelData loadWaterlevel(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { |
8951 | 222 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
|
223 return new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8951 | 224 } |
225 | |
226 private BedHeightsFinder loadBedHeight(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { | |
227 final String soundingId = pair.getSoundingId(); | |
228 return BedHeightsFinder.forId(this.context, soundingId, calcRange, problems); | |
229 } | |
230 } |