Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthdev/FlowDepthDevelopmentCalculation.java @ 9040:5294114b1df4
Fixed/changed some FIXMEs/TODOs
author | gernotbelger |
---|---|
date | Wed, 02 May 2018 12:19:31 +0200 |
parents | d5802f22e4f5 |
children | 740d65e4aa14 |
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; | |
8997 | 18 import org.dive4elements.river.artifacts.common.GeneralResultType; |
19 import org.dive4elements.river.artifacts.common.ResultRow; | |
8951 | 20 import org.dive4elements.river.artifacts.model.Calculation; |
21 import org.dive4elements.river.artifacts.model.CalculationResult; | |
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.common.RiverInfoProvider; | |
26 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
27 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthUtils; | |
28 import org.dive4elements.river.artifacts.sinfo.flowdepth.WstSoundingIdPair; | |
29 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.WaterlevelValuesFinder; | |
30 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; | |
31 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; | |
32 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; | |
33 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
34 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
35 import org.dive4elements.river.artifacts.states.WaterlevelData; | |
36 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; | |
37 import org.dive4elements.river.model.River; | |
38 | |
39 /** | |
40 * @author Gernot Belger | |
41 */ | |
42 final class FlowDepthDevelopmentCalculation { | |
43 | |
44 private final CallContext context; | |
45 | |
46 public FlowDepthDevelopmentCalculation(final CallContext context) { | |
47 this.context = context; | |
48 } | |
49 | |
50 public CalculationResult calculate(final SINFOArtifact sinfo) { | |
51 | |
52 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); | |
53 | |
54 /* access input data */ | |
55 final FlowDepthDevelopmentAccess access = new FlowDepthDevelopmentAccess(sinfo); | |
56 final River river = access.getRiver(); | |
57 final RiverInfo riverInfo = new RiverInfo(river); | |
58 | |
59 final WstSoundingIdPair currentPair = access.getCurrentPair(); | |
60 final WstSoundingIdPair histPair = access.getHistoricalPair(); | |
61 | |
62 final DoubleRange calcRange = access.getRange(); | |
63 | |
64 /* calculate results for each diff pair */ | |
65 final Calculation problems = new Calculation(); | |
66 | |
67 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); | |
68 | |
69 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); | |
70 | |
71 final FlowDepthDevelopmentCalculationResults results = new FlowDepthDevelopmentCalculationResults(calcModeLabel, user, riverInfo, calcRange); | |
72 | |
73 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
|
74 results.addResult(result, problems); |
8951 | 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 final BedHeightInfo currentSoundingInfo = currentSounding.getInfo(); | |
100 final BedHeightInfo historicalSoundingInfo = historicalSounding.getInfo(); | |
101 | |
102 final int currentWstYear = currentWaterlevel.getYear(); | |
103 final int historicalWstYear = historicalWaterlevel.getYear(); | |
104 final int currentSoundingYear = currentSoundingInfo.getYear(); | |
105 final int historicalSoundingYear = historicalSoundingInfo.getYear(); | |
106 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
107 if (currentWstYear < 0) { |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
108 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
|
109 return null; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
110 } |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
111 |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
112 if (historicalWstYear < 0) { |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
113 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
|
114 return null; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
115 } |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
116 |
8980
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
117 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
|
118 currentSoundingYear, problems); |
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
119 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
|
120 historicalWstYear, |
b194fa64506a
SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents:
8978
diff
changeset
|
121 historicalSoundingYear, problems); |
8951 | 122 |
123 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
124 final RiverInfoProvider currentRiverInfoProvider = infoProvider.forWaterlevel(currentWaterlevel); | |
125 final RiverInfoProvider histRiverInfoProvider = infoProvider.forWaterlevel(historicalWaterlevel); | |
126 | |
127 final WstInfo currentWstInfo = new WstInfo(currentWaterlevel.getName(), currentWstYear, currentRiverInfoProvider.getReferenceGauge()); | |
128 final WstInfo historicalWstInfo = new WstInfo(historicalWaterlevel.getName(), historicalWstYear, histRiverInfoProvider.getReferenceGauge()); | |
129 | |
130 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
|
131 final WaterlevelValuesFinder currentWstProvider = WaterlevelValuesFinder.fromKms(problems, currentWkms); |
8951 | 132 |
133 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
|
134 final WaterlevelValuesFinder historicalWstProvider = WaterlevelValuesFinder.fromKms(problems, historicalWkms); |
8951 | 135 |
136 final int currentMeanYear = (currentWstYear + currentSoundingYear) / 2; | |
137 final int historcialMeanYear = (historicalWstYear + historicalSoundingYear) / 2; | |
138 | |
139 final double diffYear = currentMeanYear - historcialMeanYear; | |
140 | |
141 /* real calculation loop */ | |
8997 | 142 final Collection< ResultRow> rows = new ArrayList<>(); |
8951 | 143 |
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
|
144 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
|
145 for (final double station : stations) { |
8951 | 146 if (calcRange.containsDouble(station)) { |
147 | |
148 final double currentWst = currentWstProvider.getWaterlevel(station); | |
149 final double currentBedHeight = currentSounding.getMeanBedHeight(station); | |
150 | |
151 final double historicalWst = historicalWstProvider.getWaterlevel(station); | |
152 final double historicalBedHeight = historicalSounding.getMeanBedHeight(station); | |
153 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
154 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
155 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
|
156 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
157 |
8951 | 158 final double diffWst = (currentWst - historicalWst) * 100; |
159 final double diffBedHeight = (currentBedHeight - historicalBedHeight) * 100; | |
160 | |
161 final double flowDepthDevelopment = diffWst - diffBedHeight; | |
162 | |
163 final double flowDepthDevelopmentPerYear = flowDepthDevelopment / diffYear; | |
164 | |
165 final double currentFlowDepth = currentWst - currentBedHeight; | |
166 final double historicalFlowDepth = historicalWst - historicalBedHeight; | |
167 | |
168 // REMARK: access the location once only during calculation | |
169 final String location = currentRiverInfoProvider.getLocation(station); | |
170 | |
8997 | 171 final ResultRow row = ResultRow.create().// |
172 putValue(GeneralResultType.station, station). // | |
8951 | 173 putValue(SInfoResultType.flowdepthDevelopment, flowDepthDevelopment). // |
174 putValue(SInfoResultType.flowdepthDevelopmentPerYear, flowDepthDevelopmentPerYear). // | |
175 putValue(SInfoResultType.waterlevelDifference, diffWst). // | |
176 putValue(SInfoResultType.bedHeightDifference, diffBedHeight). // | |
177 putValue(SInfoResultType.flowdepthCurrent, currentFlowDepth). // | |
178 putValue(SInfoResultType.flowdepthHistorical, historicalFlowDepth). // | |
179 putValue(SInfoResultType.location, location); | |
180 rows.add(row); | |
181 } | |
182 } | |
183 | |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
184 final String label = buildLabel(currentWaterlevel, currentSoundingInfo, historicalWaterlevel, historicalSoundingInfo); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
185 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
186 return new FlowDepthDevelopmentCalculationResult(label, currentWstInfo, historicalWstInfo, currentSoundingInfo, historicalSoundingInfo, |
8951 | 187 rows); |
188 } | |
189 | |
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
|
190 /** |
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 * 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
|
192 */ |
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 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
|
194 |
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 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
|
196 |
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 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
|
198 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
|
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 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
|
201 } |
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 |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
203 private String buildLabel(final WaterlevelData currentWaterlevel, final BedHeightInfo currentSounding, final WaterlevelData historicalWaterlevel, |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
204 final BedHeightInfo historicalSounding) { |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
205 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
206 return new StringBuilder(). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
207 append(currentWaterlevel.getName()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
208 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
209 append(historicalWaterlevel.getName()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
210 append(" - "). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
211 append(currentSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
212 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
213 append(historicalSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
214 toString(); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
215 } |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
216 |
8951 | 217 /* 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
|
218 private WaterlevelData loadWaterlevel(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { |
8951 | 219 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
|
220 return new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8951 | 221 } |
222 | |
223 private BedHeightsFinder loadBedHeight(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { | |
224 final String soundingId = pair.getSoundingId(); | |
225 return BedHeightsFinder.forId(this.context, soundingId, calcRange, problems); | |
226 } | |
227 } |