Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java @ 9362:392745cccede
Fixed: waterlevels from database should not get the "Bezugspegel" column in the result output.
author | gernotbelger |
---|---|
date | Wed, 01 Aug 2018 18:40:57 +0200 |
parents | 7dc238bd062c |
children | a0a2e68a1e11 |
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; | |
9335
7dc238bd062c
Fixed: calculation of flow depth and tkh with rounding to cm, calculator return differentiated
mschaefer
parents:
9318
diff
changeset
|
38 import org.dive4elements.river.utils.Formatter; |
8946 | 39 |
40 /** | |
41 * @author Gernot Belger | |
42 */ | |
43 final class FlowDepthMinMaxCalculation { | |
44 | |
45 private final CallContext context; | |
46 | |
47 public FlowDepthMinMaxCalculation(final CallContext context) { | |
48 this.context = context; | |
49 } | |
50 | |
51 public CalculationResult calculate(final SINFOArtifact sinfo) { | |
52 | |
53 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); | |
54 | |
55 /* access input data */ | |
56 final FlowDepthMinMaxAccess access = new FlowDepthMinMaxAccess(sinfo); | |
57 final River river = access.getRiver(); | |
58 final RiverInfo riverInfo = new RiverInfo(river); | |
59 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
60 final Collection<WstSoundingIdPair> minMaxPairs = access.getMinMaxPairs(); |
8946 | 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 FlowDepthMinMaxCalculationResults results = new FlowDepthMinMaxCalculationResults(calcModeLabel, user, riverInfo, calcRange); | |
72 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
73 for (final WstSoundingIdPair minMaxPair : minMaxPairs) { |
8946 | 74 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
|
75 results.addResult(result, problems); |
8946 | 76 } |
77 | |
78 return new CalculationResult(results, problems); | |
79 } | |
80 | |
81 /** | |
82 * Calculates one W-MSH differences pair. | |
83 * | |
84 * @param infoProvider | |
85 */ | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
86 private FlowDepthMinMaxCalculationResult calculateResult(final DoubleRange calcRange, final WstSoundingIdPair minMaxPair, final Calculation problems, |
8946 | 87 final RiverInfoProvider infoProvider) { |
88 | |
89 /* access real input data from database */ | |
90 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
|
91 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
92 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
|
93 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
94 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
|
95 if (bedHeight == null) |
8946 | 96 return null; |
97 | |
98 /* 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
|
99 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8946 | 100 if (waterlevel == null) |
101 return null; | |
102 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
103 final String label = createLabel(waterlevel, bedHeight); |
8946 | 104 |
105 final WKms wstKms = waterlevel.getWkms(); | |
106 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
107 final BedHeightInfo bedHeightInfo = bedHeight.getInfo(); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
108 |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
109 final int soundingYear = bedHeightInfo.getYear(); |
8951 | 110 FlowDepthUtils.checkYearDifference(label, waterlevel.getYear(), soundingYear, problems); |
8946 | 111 |
112 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
113 final RiverInfoProvider riverInfoProvider = infoProvider.forWaterlevel(waterlevel); | |
114 | |
115 final int wspYear = waterlevel.getYear(); | |
9362
392745cccede
Fixed: waterlevels from database should not get the "Bezugspegel" column in the result output.
gernotbelger
parents:
9335
diff
changeset
|
116 final WstInfo wstInfo = new WstInfo(waterlevel.getName(), wspYear, riverInfoProvider.getReferenceGauge(), true); |
8946 | 117 |
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
|
118 final WaterlevelValuesFinder waterlevelProvider = WaterlevelValuesFinder.fromKms(problems, wstKms); |
8946 | 119 final DischargeValuesFinder dischargeProvider = DischargeValuesFinder.fromKms(wstKms); |
120 | |
121 final String waterlevelLabel = waterlevel.getName(); | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
122 final String soundingLabel = bedHeightInfo.getDescription(); |
8946 | 123 |
124 /* real calculation loop */ | |
9312 | 125 final Collection<ResultRow> rows = new ArrayList<>(); |
8946 | 126 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
127 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
|
128 for (final double station : stations) { |
8946 | 129 if (calcRange.containsDouble(station)) { |
130 | |
131 final double wst = waterlevelProvider.getWaterlevel(station); | |
132 final double discharge = dischargeProvider.getDischarge(station); | |
133 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
134 final double minBedHeightValue = bedHeight.getMinBedHeight(station); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
135 final double maxBedHeightValue = bedHeight.getMaxBedHeight(station); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
136 final double meanBedHeight = bedHeight.getMeanBedHeight(station); |
8946 | 137 |
9335
7dc238bd062c
Fixed: calculation of flow depth and tkh with rounding to cm, calculator return differentiated
mschaefer
parents:
9318
diff
changeset
|
138 final double minFlowDepth = Math.max(Formatter.roundFlowDepth(wst) - Formatter.roundFlowDepth(maxBedHeightValue), 0.0); |
7dc238bd062c
Fixed: calculation of flow depth and tkh with rounding to cm, calculator return differentiated
mschaefer
parents:
9318
diff
changeset
|
139 final double maxFlowDepth = Math.max(Formatter.roundFlowDepth(wst) - Formatter.roundFlowDepth(minBedHeightValue), 0.0); |
8946 | 140 |
141 // REMARK: access the location once only during calculation | |
142 final String location = riverInfoProvider.getLocation(station); | |
143 | |
144 // REMARK: access the gauge once only during calculation | |
145 final String gaugeLabel = riverInfoProvider.findGauge(station); | |
146 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
147 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
148 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
|
149 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
150 |
9312 | 151 final ResultRow row = ResultRow.create().// |
8997 | 152 putValue(GeneralResultType.station, station). // |
8948 | 153 putValue(SInfoResultType.flowdepthmin, minFlowDepth). // |
154 putValue(SInfoResultType.flowdepthmax, maxFlowDepth). // | |
155 putValue(SInfoResultType.waterlevel, wst). // | |
156 putValue(SInfoResultType.discharge, discharge). // | |
9318
7b2b086e45f0
collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents:
9312
diff
changeset
|
157 putValue(GeneralResultType.waterlevelLabel, waterlevelLabel). // |
7b2b086e45f0
collision pdf details, ResultType refactoring, bezugswst result
gernotbelger
parents:
9312
diff
changeset
|
158 putValue(GeneralResultType.gaugeLabel, gaugeLabel). // |
8948 | 159 putValue(SInfoResultType.meanBedHeight, meanBedHeight). // |
160 putValue(SInfoResultType.soundingLabel, soundingLabel). // | |
9312 | 161 putValue(GeneralResultType.location, location); |
8948 | 162 rows.add(row); |
8946 | 163 } |
164 } | |
165 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
166 return new FlowDepthMinMaxCalculationResult(label, wstInfo, bedHeightInfo, rows); |
8946 | 167 } |
168 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
169 private String createLabel(final WaterlevelData waterlevel, final BedHeightsFinder bedHeight) { |
8946 | 170 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
171 return new StringBuilder(waterlevel.getName()). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
172 append(" - "). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
173 append(bedHeight.getInfo().getDescription()). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
174 toString(); |
8946 | 175 } |
176 } |