Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthdev/FlowDepthDevelopmentCalculation.java @ 8964:45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
author | gernotbelger |
---|---|
date | Thu, 29 Mar 2018 15:48:17 +0200 |
parents | c40db8e8dcae |
children | b5600453bb8f |
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... | |
100 | |
101 final BedHeightInfo currentSoundingInfo = currentSounding.getInfo(); | |
102 final BedHeightInfo historicalSoundingInfo = historicalSounding.getInfo(); | |
103 | |
104 // final String label = createLabel(waterlevel, minBedHeight, maxBedHeight); | |
105 | |
106 // final WKms wstKms = waterlevel.getWkms(); | |
107 final int currentWstYear = currentWaterlevel.getYear(); | |
108 final int historicalWstYear = historicalWaterlevel.getYear(); | |
109 final int currentSoundingYear = currentSoundingInfo.getYear(); | |
110 final int historicalSoundingYear = historicalSoundingInfo.getYear(); | |
111 | |
112 // FIXME: distinguish error messages | |
113 FlowDepthUtils.checkYearDifference("", currentWstYear, currentSoundingYear, problems); | |
114 FlowDepthUtils.checkYearDifference("", historicalWstYear, historicalSoundingYear, problems); | |
115 | |
116 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
117 final RiverInfoProvider currentRiverInfoProvider = infoProvider.forWaterlevel(currentWaterlevel); | |
118 final RiverInfoProvider histRiverInfoProvider = infoProvider.forWaterlevel(historicalWaterlevel); | |
119 | |
120 final WstInfo currentWstInfo = new WstInfo(currentWaterlevel.getName(), currentWstYear, currentRiverInfoProvider.getReferenceGauge()); | |
121 final WstInfo historicalWstInfo = new WstInfo(historicalWaterlevel.getName(), historicalWstYear, histRiverInfoProvider.getReferenceGauge()); | |
122 | |
123 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
|
124 final WaterlevelValuesFinder currentWstProvider = WaterlevelValuesFinder.fromKms(problems, currentWkms); |
8951 | 125 |
126 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
|
127 final WaterlevelValuesFinder historicalWstProvider = WaterlevelValuesFinder.fromKms(problems, historicalWkms); |
8951 | 128 |
129 final int currentMeanYear = (currentWstYear + currentSoundingYear) / 2; | |
130 final int historcialMeanYear = (historicalWstYear + historicalSoundingYear) / 2; | |
131 | |
132 final double diffYear = currentMeanYear - historcialMeanYear; | |
133 | |
134 /* real calculation loop */ | |
135 final Collection<SInfoResultRow> rows = new ArrayList<>(); | |
136 | |
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 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
|
138 for (final double station : stations) { |
8951 | 139 if (calcRange.containsDouble(station)) { |
140 | |
141 final double currentWst = currentWstProvider.getWaterlevel(station); | |
142 final double currentBedHeight = currentSounding.getMeanBedHeight(station); | |
143 | |
144 final double historicalWst = historicalWstProvider.getWaterlevel(station); | |
145 final double historicalBedHeight = historicalSounding.getMeanBedHeight(station); | |
146 | |
147 final double diffWst = (currentWst - historicalWst) * 100; | |
148 final double diffBedHeight = (currentBedHeight - historicalBedHeight) * 100; | |
149 | |
150 final double flowDepthDevelopment = diffWst - diffBedHeight; | |
151 | |
152 final double flowDepthDevelopmentPerYear = flowDepthDevelopment / diffYear; | |
153 | |
154 final double currentFlowDepth = currentWst - currentBedHeight; | |
155 final double historicalFlowDepth = historicalWst - historicalBedHeight; | |
156 | |
157 // REMARK: access the location once only during calculation | |
158 final String location = currentRiverInfoProvider.getLocation(station); | |
159 | |
160 final SInfoResultRow row = SInfoResultRow.create().// | |
161 putValue(SInfoResultType.station, station). // | |
162 putValue(SInfoResultType.flowdepthDevelopment, flowDepthDevelopment). // | |
163 putValue(SInfoResultType.flowdepthDevelopmentPerYear, flowDepthDevelopmentPerYear). // | |
164 putValue(SInfoResultType.waterlevelDifference, diffWst). // | |
165 putValue(SInfoResultType.bedHeightDifference, diffBedHeight). // | |
166 putValue(SInfoResultType.flowdepthCurrent, currentFlowDepth). // | |
167 putValue(SInfoResultType.flowdepthHistorical, historicalFlowDepth). // | |
168 putValue(SInfoResultType.location, location); | |
169 rows.add(row); | |
170 } | |
171 } | |
172 | |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
173 final String label = buildLabel(currentWaterlevel, currentSoundingInfo, historicalWaterlevel, historicalSoundingInfo); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
174 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
175 return new FlowDepthDevelopmentCalculationResult(label, currentWstInfo, historicalWstInfo, currentSoundingInfo, historicalSoundingInfo, |
8951 | 176 rows); |
177 } | |
178 | |
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
|
179 /** |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
180 * 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
|
181 */ |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
182 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
|
183 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
184 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
|
185 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
186 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
|
187 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
|
188 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8953
diff
changeset
|
189 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
|
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 |
8953
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
192 private String buildLabel(final WaterlevelData currentWaterlevel, final BedHeightInfo currentSounding, final WaterlevelData historicalWaterlevel, |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
193 final BedHeightInfo historicalSounding) { |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
194 |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
195 return new StringBuilder(). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
196 append(currentWaterlevel.getName()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
197 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
198 append(historicalWaterlevel.getName()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
199 append(" - "). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
200 append(currentSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
201 append('/'). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
202 append(historicalSounding.getDescription()). // |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
203 toString(); |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
204 } |
c40db8e8dcae
SINFO - Flow depth development - implemented exporters
gernotbelger
parents:
8951
diff
changeset
|
205 |
8951 | 206 /* 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
|
207 private WaterlevelData loadWaterlevel(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { |
8951 | 208 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
|
209 return new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8951 | 210 } |
211 | |
212 private BedHeightsFinder loadBedHeight(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { | |
213 final String soundingId = pair.getSoundingId(); | |
214 return BedHeightsFinder.forId(this.context, soundingId, calcRange, problems); | |
215 } | |
216 } |