Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java @ 9312:740d65e4aa14
Q [m³/s] one message
author | gernotbelger |
---|---|
date | Thu, 26 Jul 2018 15:54:20 +0200 |
parents | d5802f22e4f5 |
children | 7b2b086e45f0 |
rev | line source |
---|---|
8946 | 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; | |
8997 | 17 import org.dive4elements.river.artifacts.common.GeneralResultType; |
18 import org.dive4elements.river.artifacts.common.ResultRow; | |
8946 | 19 import org.dive4elements.river.artifacts.model.Calculation; |
20 import org.dive4elements.river.artifacts.model.CalculationResult; | |
21 import org.dive4elements.river.artifacts.model.WKms; | |
22 import org.dive4elements.river.artifacts.resources.Resources; | |
23 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; | |
24 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; | |
8948 | 25 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
8946 | 26 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthUtils; |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
27 import org.dive4elements.river.artifacts.sinfo.flowdepth.WstSoundingIdPair; |
8946 | 28 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.DischargeValuesFinder; |
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 FlowDepthMinMaxCalculation { | |
43 | |
44 private final CallContext context; | |
45 | |
46 public FlowDepthMinMaxCalculation(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 FlowDepthMinMaxAccess access = new FlowDepthMinMaxAccess(sinfo); | |
56 final River river = access.getRiver(); | |
57 final RiverInfo riverInfo = new RiverInfo(river); | |
58 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
59 final Collection<WstSoundingIdPair> minMaxPairs = access.getMinMaxPairs(); |
8946 | 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 FlowDepthMinMaxCalculationResults results = new FlowDepthMinMaxCalculationResults(calcModeLabel, user, riverInfo, calcRange); | |
71 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
72 for (final WstSoundingIdPair minMaxPair : minMaxPairs) { |
8946 | 73 final FlowDepthMinMaxCalculationResult result = calculateResult(calcRange, minMaxPair, 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); |
8946 | 75 } |
76 | |
77 return new CalculationResult(results, problems); | |
78 } | |
79 | |
80 /** | |
81 * Calculates one W-MSH differences pair. | |
82 * | |
83 * @param infoProvider | |
84 */ | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
85 private FlowDepthMinMaxCalculationResult calculateResult(final DoubleRange calcRange, final WstSoundingIdPair minMaxPair, final Calculation problems, |
8946 | 86 final RiverInfoProvider infoProvider) { |
87 | |
88 /* access real input data from database */ | |
89 final String wstId = minMaxPair.getWstId(); | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
90 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
91 final String soundingId = minMaxPair.getSoundingId(); |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
92 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
93 final BedHeightsFinder bedHeight = BedHeightsFinder.forId(this.context, soundingId, calcRange, problems); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
94 if (bedHeight == null) |
8946 | 95 return null; |
96 | |
97 /* REMARK: fetch ALL wst kms, because we want 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:
8951
diff
changeset
|
98 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8946 | 99 if (waterlevel == null) |
100 return null; | |
101 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
102 final String label = createLabel(waterlevel, bedHeight); |
8946 | 103 |
104 final WKms wstKms = waterlevel.getWkms(); | |
105 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
106 final BedHeightInfo bedHeightInfo = bedHeight.getInfo(); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
107 |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
108 final int soundingYear = bedHeightInfo.getYear(); |
8951 | 109 FlowDepthUtils.checkYearDifference(label, waterlevel.getYear(), soundingYear, problems); |
8946 | 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 | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
117 final WaterlevelValuesFinder waterlevelProvider = WaterlevelValuesFinder.fromKms(problems, wstKms); |
8946 | 118 final DischargeValuesFinder dischargeProvider = DischargeValuesFinder.fromKms(wstKms); |
119 | |
120 final String waterlevelLabel = waterlevel.getName(); | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
121 final String soundingLabel = bedHeightInfo.getDescription(); |
8946 | 122 |
123 /* real calculation loop */ | |
9312 | 124 final Collection<ResultRow> rows = new ArrayList<>(); |
8946 | 125 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
126 final Collection<Double> stations = bedHeight.getStations(); |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8951
diff
changeset
|
127 for (final double station : stations) { |
8946 | 128 if (calcRange.containsDouble(station)) { |
129 | |
130 final double wst = waterlevelProvider.getWaterlevel(station); | |
131 final double discharge = dischargeProvider.getDischarge(station); | |
132 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
133 final double minBedHeightValue = bedHeight.getMinBedHeight(station); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
134 final double maxBedHeightValue = bedHeight.getMaxBedHeight(station); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
135 final double meanBedHeight = bedHeight.getMeanBedHeight(station); |
8946 | 136 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
137 final double minFlowDepth = wst - maxBedHeightValue; |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
138 final double maxFlowDepth = wst - minBedHeightValue; |
8946 | 139 |
140 // REMARK: access the location once only during calculation | |
141 final String location = riverInfoProvider.getLocation(station); | |
142 | |
143 // REMARK: access the gauge once only during calculation | |
144 final String gaugeLabel = riverInfoProvider.findGauge(station); | |
145 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
146 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
147 if (Double.isNaN(wst) || Double.isNaN(minBedHeightValue) || Double.isNaN(maxBedHeightValue)) |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
148 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
149 |
9312 | 150 final ResultRow row = ResultRow.create().// |
8997 | 151 putValue(GeneralResultType.station, station). // |
8948 | 152 putValue(SInfoResultType.flowdepthmin, minFlowDepth). // |
153 putValue(SInfoResultType.flowdepthmax, maxFlowDepth). // | |
154 putValue(SInfoResultType.waterlevel, wst). // | |
155 putValue(SInfoResultType.discharge, discharge). // | |
156 putValue(SInfoResultType.waterlevelLabel, waterlevelLabel). // | |
157 putValue(SInfoResultType.gaugeLabel, gaugeLabel). // | |
158 putValue(SInfoResultType.meanBedHeight, meanBedHeight). // | |
159 putValue(SInfoResultType.soundingLabel, soundingLabel). // | |
9312 | 160 putValue(GeneralResultType.location, location); |
8948 | 161 rows.add(row); |
8946 | 162 } |
163 } | |
164 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
165 return new FlowDepthMinMaxCalculationResult(label, wstInfo, bedHeightInfo, rows); |
8946 | 166 } |
167 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
168 private String createLabel(final WaterlevelData waterlevel, final BedHeightsFinder bedHeight) { |
8946 | 169 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
170 return new StringBuilder(waterlevel.getName()). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
171 append(" - "). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
172 append(bedHeight.getInfo().getDescription()). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
173 toString(); |
8946 | 174 } |
175 } |