comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java @ 8946:5d5d482da3e9

Implementing SINFO - FlowDepthMinMax calculation
author gernotbelger
date Tue, 13 Mar 2018 18:49:33 +0100
parents
children a4f1ac81f26d
comparison
equal deleted inserted replaced
8945:4a6b6a3c279c 8946:5d5d482da3e9
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.flowdepthminmax;
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.flowdepth.FlowDepthUtils;
24 import org.dive4elements.river.artifacts.sinfo.flowdepthminmax.FlowDepthMinMaxAccess.MinMaxIdPair;
25 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.DischargeValuesFinder;
26 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.WaterlevelValuesFinder;
27 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder;
28 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo;
29 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils;
30 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
31 import org.dive4elements.river.artifacts.sinfo.util.WstInfo;
32 import org.dive4elements.river.artifacts.states.WaterlevelData;
33 import org.dive4elements.river.artifacts.states.WaterlevelFetcher;
34 import org.dive4elements.river.model.River;
35
36 /**
37 * @author Gernot Belger
38 */
39 final class FlowDepthMinMaxCalculation {
40
41 private final CallContext context;
42
43 public FlowDepthMinMaxCalculation(final CallContext context) {
44 this.context = context;
45 }
46
47 public CalculationResult calculate(final SINFOArtifact sinfo) {
48
49 final String user = CalculationUtils.findArtifactUser(this.context, sinfo);
50
51 /* access input data */
52 final FlowDepthMinMaxAccess access = new FlowDepthMinMaxAccess(sinfo);
53 final River river = access.getRiver();
54 final RiverInfo riverInfo = new RiverInfo(river);
55
56 final Collection<MinMaxIdPair> minMaxPairs = access.getMinMaxPairs();
57
58 final DoubleRange calcRange = access.getRange();
59
60 /* calculate results for each diff pair */
61 final Calculation problems = new Calculation();
62
63 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange);
64
65 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name());
66
67 final FlowDepthMinMaxCalculationResults results = new FlowDepthMinMaxCalculationResults(calcModeLabel, user, riverInfo, calcRange);
68
69 for (final MinMaxIdPair minMaxPair : minMaxPairs) {
70 final FlowDepthMinMaxCalculationResult result = calculateResult(calcRange, minMaxPair, problems, infoProvider);
71 if (result != null)
72 results.addResult(result);
73 }
74
75 return new CalculationResult(results, problems);
76 }
77
78 /**
79 * Calculates one W-MSH differences pair.
80 *
81 * @param infoProvider
82 */
83 private FlowDepthMinMaxCalculationResult calculateResult(final DoubleRange calcRange, final MinMaxIdPair minMaxPair, final Calculation problems,
84 final RiverInfoProvider infoProvider) {
85
86 /* access real input data from database */
87 final String wstId = minMaxPair.getWstId();
88 final String minSoundingId = minMaxPair.getMinSoundingId();
89 final String maxSoundingId = minMaxPair.getMinSoundingId();
90
91 final BedHeightsFinder minBedHeight = minSoundingId == null ? null : BedHeightsFinder.forId(this.context, minSoundingId, calcRange, problems);
92 final BedHeightsFinder maxBedHeight = maxSoundingId == null ? null : BedHeightsFinder.forId(this.context, maxSoundingId, calcRange, problems);
93 if (minBedHeight == null && maxBedHeight == null)
94 return null;
95
96 /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */
97 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, Double.NaN, problems);
98 if (waterlevel == null)
99 return null;
100
101 final String label = createLabel(waterlevel, minBedHeight, maxBedHeight);
102
103 final WKms wstKms = waterlevel.getWkms();
104
105 final int soundingYear = checkSoundingYear(minBedHeight, maxBedHeight, problems);
106 FlowDepthUtils.checkYearDifference(label, waterlevel, soundingYear, problems);
107 // FIXME
108 // checkWaterlevelDiscretisation(wstKms, calcRange, problems);
109 // TODO: prüfen, ob sohlhöhen die calcRange abdecken/überschneiden
110
111 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */
112 final RiverInfoProvider riverInfoProvider = infoProvider.forWaterlevel(waterlevel);
113
114 final int wspYear = waterlevel.getYear();
115 final WstInfo wstInfo = new WstInfo(waterlevel.getName(), wspYear, riverInfoProvider.getReferenceGauge());
116
117 final WaterlevelValuesFinder waterlevelProvider = WaterlevelValuesFinder.fromKms(wstKms);
118 final DischargeValuesFinder dischargeProvider = DischargeValuesFinder.fromKms(wstKms);
119
120 final String waterlevelLabel = waterlevel.getName();
121 final String soundingLabel = buildSoundingLabel(minBedHeight, maxBedHeight);
122
123 /* real calculation loop */
124 final Collection<FlowDepthMinMaxRow> rows = new ArrayList<>();
125
126 // FIXME: determine what is the spatial discretisation that we will use...
127 final double[] allKms = wstKms.allKms().toNativeArray();
128 for (final double station : allKms) {
129 if (calcRange.containsDouble(station)) {
130
131 final double wst = waterlevelProvider.getWaterlevel(station);
132 final double discharge = dischargeProvider.getDischarge(station);
133
134 final double minBedHeightValue = minBedHeight == null ? Double.NaN : minBedHeight.getMeanBedHeight(station);
135 final double maxBedHeightValue = maxBedHeight == null ? Double.NaN : maxBedHeight.getMeanBedHeight(station);
136
137 final double minFlowDepth = wst - minBedHeightValue;
138 final double maxFlowDepth = wst - maxBedHeightValue;
139
140 // FIXME: unclear what is meant here...
141 final double meanBedHeight = Double.NaN;
142
143 // REMARK: access the location once only during calculation
144 final String location = riverInfoProvider.getLocation(station);
145
146 // REMARK: access the gauge once only during calculation
147 final String gaugeLabel = riverInfoProvider.findGauge(station);
148
149 rows.add(new FlowDepthMinMaxRow(station, minFlowDepth, maxFlowDepth, wst, discharge, waterlevelLabel, gaugeLabel, meanBedHeight, soundingLabel,
150 location));
151 }
152 }
153
154 final BedHeightInfo minBedHeightInfo = minBedHeight == null ? null : minBedHeight.getInfo();
155 final BedHeightInfo maxBedHeightInfo = maxBedHeight == null ? null : maxBedHeight.getInfo();
156 return new FlowDepthMinMaxCalculationResult(label, wstInfo, minBedHeightInfo, maxBedHeightInfo, rows);
157 }
158
159 private String buildSoundingLabel(final BedHeightsFinder minBedHeight, final BedHeightsFinder maxBedHeight) {
160
161 if (minBedHeight == null)
162 return maxBedHeight.getInfo().getDescription();
163
164 if (maxBedHeight == null)
165 return minBedHeight.getInfo().getDescription();
166
167 return String.format("%s / %s", minBedHeight.getInfo().getDescription(), maxBedHeight.getInfo().getDescription());
168 }
169
170 private String createLabel(final WaterlevelData waterlevel, final BedHeightsFinder minBedHeight, final BedHeightsFinder maxBedHeight) {
171
172 final StringBuilder buffer = new StringBuilder(waterlevel.getName());
173
174 if (minBedHeight != null)
175 buffer.append(" - "). //
176 append(minBedHeight.getInfo().getDescription());
177
178 if (maxBedHeight != null)
179 buffer.append(" - "). //
180 append(maxBedHeight.getInfo().getDescription());
181
182 return buffer.toString();
183 }
184
185 private int checkSoundingYear(final BedHeightsFinder minBedHeight, final BedHeightsFinder maxBedHeight, final Calculation problems) {
186
187 if (maxBedHeight == null)
188 return minBedHeight.getInfo().getYear();
189
190 if (minBedHeight == null)
191 return maxBedHeight.getInfo().getYear();
192
193 final int minYear = minBedHeight.getInfo().getYear();
194 final int maxYear = minBedHeight.getInfo().getYear();
195
196 if (minYear != maxYear)
197 problems.addProblem("sinfo.flowdepthminmaxcalculation.soundingyear.different");
198
199 return minYear;
200 }
201 }

http://dive4elements.wald.intevation.org