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; |
|
14 |
|
15 import org.apache.commons.lang.math.DoubleRange; |
|
16 import org.dive4elements.artifacts.CallContext; |
|
17 import org.dive4elements.river.artifacts.model.Calculation; |
|
18 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
19 import org.dive4elements.river.artifacts.model.WKms; |
|
20 import org.dive4elements.river.artifacts.resources.Resources; |
|
21 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; |
|
22 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; |
|
23 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultRow; |
|
24 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
|
25 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthUtils; |
|
26 import org.dive4elements.river.artifacts.sinfo.flowdepth.WstSoundingIdPair; |
|
27 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.WaterlevelValuesFinder; |
|
28 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; |
|
29 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; |
|
30 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; |
|
31 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
|
32 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; |
|
33 import org.dive4elements.river.artifacts.states.WaterlevelData; |
|
34 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; |
|
35 import org.dive4elements.river.model.River; |
|
36 |
|
37 /** |
|
38 * @author Gernot Belger |
|
39 */ |
|
40 final class FlowDepthDevelopmentCalculation { |
|
41 |
|
42 private final CallContext context; |
|
43 |
|
44 public FlowDepthDevelopmentCalculation(final CallContext context) { |
|
45 this.context = context; |
|
46 } |
|
47 |
|
48 public CalculationResult calculate(final SINFOArtifact sinfo) { |
|
49 |
|
50 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); |
|
51 |
|
52 /* access input data */ |
|
53 final FlowDepthDevelopmentAccess access = new FlowDepthDevelopmentAccess(sinfo); |
|
54 final River river = access.getRiver(); |
|
55 final RiverInfo riverInfo = new RiverInfo(river); |
|
56 |
|
57 final WstSoundingIdPair currentPair = access.getCurrentPair(); |
|
58 final WstSoundingIdPair histPair = access.getHistoricalPair(); |
|
59 |
|
60 final DoubleRange calcRange = access.getRange(); |
|
61 |
|
62 /* calculate results for each diff pair */ |
|
63 final Calculation problems = new Calculation(); |
|
64 |
|
65 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); |
|
66 |
|
67 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); |
|
68 |
|
69 final FlowDepthDevelopmentCalculationResults results = new FlowDepthDevelopmentCalculationResults(calcModeLabel, user, riverInfo, calcRange); |
|
70 |
|
71 final FlowDepthDevelopmentCalculationResult result = calculateResult(calcRange, currentPair, histPair, problems, infoProvider); |
|
72 if (result != null) |
|
73 results.addResult(result); |
|
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 */ |
|
82 final WaterlevelData currentWaterlevel = loadWaterlevel(currentPair, problems); |
|
83 if (currentWaterlevel == null) |
|
84 return null; |
|
85 |
|
86 final WaterlevelData historicalWaterlevel = loadWaterlevel(histPair, problems); |
|
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... |
|
99 |
|
100 final BedHeightInfo currentSoundingInfo = currentSounding.getInfo(); |
|
101 final BedHeightInfo historicalSoundingInfo = historicalSounding.getInfo(); |
|
102 |
|
103 // final String label = createLabel(waterlevel, minBedHeight, maxBedHeight); |
|
104 |
|
105 // final WKms wstKms = waterlevel.getWkms(); |
|
106 final int currentWstYear = currentWaterlevel.getYear(); |
|
107 final int historicalWstYear = historicalWaterlevel.getYear(); |
|
108 final int currentSoundingYear = currentSoundingInfo.getYear(); |
|
109 final int historicalSoundingYear = historicalSoundingInfo.getYear(); |
|
110 |
|
111 // FIXME: distinguish error messages |
|
112 FlowDepthUtils.checkYearDifference("", currentWstYear, currentSoundingYear, problems); |
|
113 FlowDepthUtils.checkYearDifference("", historicalWstYear, historicalSoundingYear, problems); |
|
114 |
|
115 // checkWaterlevelDiscretisation(wstKms, calcRange, problems); |
|
116 // TODO: prüfen, ob sohlhöhen die calcRange abdecken/überschneiden |
|
117 |
|
118 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ |
|
119 final RiverInfoProvider currentRiverInfoProvider = infoProvider.forWaterlevel(currentWaterlevel); |
|
120 final RiverInfoProvider histRiverInfoProvider = infoProvider.forWaterlevel(historicalWaterlevel); |
|
121 |
|
122 final WstInfo currentWstInfo = new WstInfo(currentWaterlevel.getName(), currentWstYear, currentRiverInfoProvider.getReferenceGauge()); |
|
123 final WstInfo historicalWstInfo = new WstInfo(historicalWaterlevel.getName(), historicalWstYear, histRiverInfoProvider.getReferenceGauge()); |
|
124 |
|
125 final WKms currentWkms = currentWaterlevel.getWkms(); |
|
126 final WaterlevelValuesFinder currentWstProvider = WaterlevelValuesFinder.fromKms(currentWkms); |
|
127 // final DischargeValuesFinder currentDischargeProvider = DischargeValuesFinder.fromKms(currentWkms); |
|
128 |
|
129 final WKms historicalWkms = historicalWaterlevel.getWkms(); |
|
130 final WaterlevelValuesFinder historicalWstProvider = WaterlevelValuesFinder.fromKms(historicalWkms); |
|
131 // final DischargeValuesFinder historicalDischargeProvider = DischargeValuesFinder.fromKms(historicalWkms); |
|
132 |
|
133 final int currentMeanYear = (currentWstYear + currentSoundingYear) / 2; |
|
134 final int historcialMeanYear = (historicalWstYear + historicalSoundingYear) / 2; |
|
135 |
|
136 // final String waterlevelLabel = waterlevel.getName(); |
|
137 // final String soundingLabel = buildSoundingLabel(minBedHeight, maxBedHeight); |
|
138 |
|
139 final double diffYear = currentMeanYear - historcialMeanYear; |
|
140 |
|
141 /* real calculation loop */ |
|
142 final Collection<SInfoResultRow> rows = new ArrayList<>(); |
|
143 |
|
144 // FIXME: determine what is the spatial discretisation that we will use... |
|
145 final double[] allKms = currentWkms.allKms().toNativeArray(); |
|
146 for (final double station : allKms) { |
|
147 if (calcRange.containsDouble(station)) { |
|
148 |
|
149 final double currentWst = currentWstProvider.getWaterlevel(station); |
|
150 // final double currentDischarge = currentDischargeProvider.getDischarge(station); |
|
151 final double currentBedHeight = currentSounding.getMeanBedHeight(station); |
|
152 |
|
153 final double historicalWst = historicalWstProvider.getWaterlevel(station); |
|
154 // final double historicalDischarge = historicalDischargeProvider.getDischarge(station); |
|
155 final double historicalBedHeight = historicalSounding.getMeanBedHeight(station); |
|
156 |
|
157 final double diffWst = (currentWst - historicalWst) * 100; |
|
158 final double diffBedHeight = (currentBedHeight - historicalBedHeight) * 100; |
|
159 |
|
160 final double flowDepthDevelopment = diffWst - diffBedHeight; |
|
161 |
|
162 final double flowDepthDevelopmentPerYear = flowDepthDevelopment / diffYear; |
|
163 |
|
164 final double currentFlowDepth = currentWst - currentBedHeight; |
|
165 final double historicalFlowDepth = historicalWst - historicalBedHeight; |
|
166 |
|
167 // REMARK: access the location once only during calculation |
|
168 final String location = currentRiverInfoProvider.getLocation(station); |
|
169 |
|
170 final SInfoResultRow row = SInfoResultRow.create().// |
|
171 putValue(SInfoResultType.station, station). // |
|
172 putValue(SInfoResultType.flowdepthDevelopment, flowDepthDevelopment). // |
|
173 putValue(SInfoResultType.flowdepthDevelopmentPerYear, flowDepthDevelopmentPerYear). // |
|
174 putValue(SInfoResultType.waterlevelDifference, diffWst). // |
|
175 putValue(SInfoResultType.bedHeightDifference, diffBedHeight). // |
|
176 putValue(SInfoResultType.flowdepthCurrent, currentFlowDepth). // |
|
177 putValue(SInfoResultType.flowdepthHistorical, historicalFlowDepth). // |
|
178 putValue(SInfoResultType.location, location); |
|
179 rows.add(row); |
|
180 } |
|
181 } |
|
182 |
|
183 return new FlowDepthDevelopmentCalculationResult("", currentWstInfo, historicalWstInfo, currentSoundingInfo, historicalSoundingInfo, |
|
184 rows); |
|
185 } |
|
186 |
|
187 /* REMARK: fetch ALL wst kms, because we need to determine the original reference gauge */ |
|
188 private WaterlevelData loadWaterlevel(final WstSoundingIdPair pair, final Calculation problems) { |
|
189 final String wstId = pair.getWstId(); |
|
190 return new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, Double.NaN, problems); |
|
191 } |
|
192 |
|
193 private BedHeightsFinder loadBedHeight(final WstSoundingIdPair pair, final DoubleRange calcRange, final Calculation problems) { |
|
194 final String soundingId = pair.getSoundingId(); |
|
195 return BedHeightsFinder.forId(this.context, soundingId, calcRange, problems); |
|
196 } |
|
197 } |