Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java @ 8993:0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
author | gernotbelger |
---|---|
date | Wed, 11 Apr 2018 14:09:13 +0200 |
parents | b194fa64506a |
children | d5802f22e4f5 |
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; | |
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; | |
8948 | 23 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultRow; |
24 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
8946 | 25 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
|
26 import org.dive4elements.river.artifacts.sinfo.flowdepth.WstSoundingIdPair; |
8946 | 27 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.DischargeValuesFinder; |
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 FlowDepthMinMaxCalculation { | |
42 | |
43 private final CallContext context; | |
44 | |
45 public FlowDepthMinMaxCalculation(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 FlowDepthMinMaxAccess access = new FlowDepthMinMaxAccess(sinfo); | |
55 final River river = access.getRiver(); | |
56 final RiverInfo riverInfo = new RiverInfo(river); | |
57 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
58 final Collection<WstSoundingIdPair> minMaxPairs = access.getMinMaxPairs(); |
8946 | 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 FlowDepthMinMaxCalculationResults results = new FlowDepthMinMaxCalculationResults(calcModeLabel, user, riverInfo, calcRange); | |
70 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
71 for (final WstSoundingIdPair minMaxPair : minMaxPairs) { |
8946 | 72 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
|
73 results.addResult(result, problems); |
8946 | 74 } |
75 | |
76 return new CalculationResult(results, problems); | |
77 } | |
78 | |
79 /** | |
80 * Calculates one W-MSH differences pair. | |
81 * | |
82 * @param infoProvider | |
83 */ | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
84 private FlowDepthMinMaxCalculationResult calculateResult(final DoubleRange calcRange, final WstSoundingIdPair minMaxPair, final Calculation problems, |
8946 | 85 final RiverInfoProvider infoProvider) { |
86 | |
87 /* access real input data from database */ | |
88 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
|
89 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
90 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
|
91 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
92 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
|
93 if (bedHeight == null) |
8946 | 94 return null; |
95 | |
96 /* 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
|
97 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, calcRange, problems); |
8946 | 98 if (waterlevel == null) |
99 return null; | |
100 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
101 final String label = createLabel(waterlevel, bedHeight); |
8946 | 102 |
103 final WKms wstKms = waterlevel.getWkms(); | |
104 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
105 final BedHeightInfo bedHeightInfo = bedHeight.getInfo(); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
106 |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
107 final int soundingYear = bedHeightInfo.getYear(); |
8951 | 108 FlowDepthUtils.checkYearDifference(label, waterlevel.getYear(), soundingYear, problems); |
8946 | 109 |
110 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
111 final RiverInfoProvider riverInfoProvider = infoProvider.forWaterlevel(waterlevel); | |
112 | |
113 final int wspYear = waterlevel.getYear(); | |
114 final WstInfo wstInfo = new WstInfo(waterlevel.getName(), wspYear, riverInfoProvider.getReferenceGauge()); | |
115 | |
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
|
116 final WaterlevelValuesFinder waterlevelProvider = WaterlevelValuesFinder.fromKms(problems, wstKms); |
8946 | 117 final DischargeValuesFinder dischargeProvider = DischargeValuesFinder.fromKms(wstKms); |
118 | |
119 final String waterlevelLabel = waterlevel.getName(); | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
120 final String soundingLabel = bedHeightInfo.getDescription(); |
8946 | 121 |
122 /* real calculation loop */ | |
8948 | 123 final Collection<SInfoResultRow> rows = new ArrayList<>(); |
8946 | 124 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
125 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
|
126 for (final double station : stations) { |
8946 | 127 if (calcRange.containsDouble(station)) { |
128 | |
129 final double wst = waterlevelProvider.getWaterlevel(station); | |
130 final double discharge = dischargeProvider.getDischarge(station); | |
131 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
132 final double minBedHeightValue = bedHeight.getMinBedHeight(station); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
133 final double maxBedHeightValue = bedHeight.getMaxBedHeight(station); |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
134 final double meanBedHeight = bedHeight.getMeanBedHeight(station); |
8946 | 135 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
136 final double minFlowDepth = wst - maxBedHeightValue; |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
137 final double maxFlowDepth = wst - minBedHeightValue; |
8946 | 138 |
139 // REMARK: access the location once only during calculation | |
140 final String location = riverInfoProvider.getLocation(station); | |
141 | |
142 // REMARK: access the gauge once only during calculation | |
143 final String gaugeLabel = riverInfoProvider.findGauge(station); | |
144 | |
8978
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
145 /* ignore invalid lines */ |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
146 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
|
147 continue; |
b5600453bb8f
SINFO Calculations: do not produce result rows if some input data has missing values.
gernotbelger
parents:
8964
diff
changeset
|
148 |
8948 | 149 final SInfoResultRow row = SInfoResultRow.create().// |
150 putValue(SInfoResultType.station, station). // | |
151 putValue(SInfoResultType.flowdepthmin, minFlowDepth). // | |
152 putValue(SInfoResultType.flowdepthmax, maxFlowDepth). // | |
153 putValue(SInfoResultType.waterlevel, wst). // | |
154 putValue(SInfoResultType.discharge, discharge). // | |
155 putValue(SInfoResultType.waterlevelLabel, waterlevelLabel). // | |
156 putValue(SInfoResultType.gaugeLabel, gaugeLabel). // | |
157 putValue(SInfoResultType.meanBedHeight, meanBedHeight). // | |
158 putValue(SInfoResultType.soundingLabel, soundingLabel). // | |
159 putValue(SInfoResultType.location, location); | |
160 rows.add(row); | |
8946 | 161 } |
162 } | |
163 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
164 return new FlowDepthMinMaxCalculationResult(label, wstInfo, bedHeightInfo, rows); |
8946 | 165 } |
166 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
167 private String createLabel(final WaterlevelData waterlevel, final BedHeightsFinder bedHeight) { |
8946 | 168 |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
169 return new StringBuilder(waterlevel.getName()). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
170 append(" - "). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
171 append(bedHeight.getInfo().getDescription()). // |
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8980
diff
changeset
|
172 toString(); |
8946 | 173 } |
174 } |