Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthCalculation.java @ 8915:d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
author | gernotbelger |
---|---|
date | Wed, 28 Feb 2018 17:27:15 +0100 |
parents | e3519c3e7a0a |
children | 5d5d482da3e9 |
rev | line source |
---|---|
8854 | 1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
8877 | 2 * Software engineering by |
3 * Björnsen Beratende Ingenieure GmbH | |
8854 | 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.flowdepth; | |
11 | |
12 import java.util.Collection; | |
13 | |
8894 | 14 import org.apache.commons.lang.math.DoubleRange; |
8854 | 15 import org.dive4elements.artifacts.CallContext; |
16 import org.dive4elements.river.artifacts.BedHeightsArtifact; | |
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; | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
22 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; |
8854 | 23 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair; |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
24 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.DischargeValuesFinder; |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
25 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.TkhCalculator; |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
26 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
27 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; |
8894 | 28 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
29 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
8882 | 30 import org.dive4elements.river.artifacts.states.WaterlevelData; |
31 import org.dive4elements.river.artifacts.states.WaterlevelFetcher; | |
8854 | 32 import org.dive4elements.river.model.River; |
33 import org.dive4elements.river.utils.RiverUtils; | |
34 | |
35 class FlowDepthCalculation { | |
36 | |
8914
e3519c3e7a0a
Workflow for SINFO-Transport bodies heights inclduing winfo calculation
gernotbelger
parents:
8911
diff
changeset
|
37 // private static Logger log = Logger.getLogger(FlowDepthCalculation.class); |
8898 | 38 |
8877 | 39 private final CallContext context; |
8854 | 40 |
8882 | 41 public FlowDepthCalculation(final CallContext context) { |
8877 | 42 this.context = context; |
8854 | 43 } |
44 | |
8877 | 45 public CalculationResult calculate(final SINFOArtifact sinfo) { |
8854 | 46 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
47 final String user = CalculationUtils.findArtifactUser(this.context, sinfo); |
8854 | 48 |
8877 | 49 /* access input data */ |
50 final FlowDepthAccess access = new FlowDepthAccess(sinfo); | |
51 final River river = access.getRiver(); | |
8894 | 52 final RiverInfo riverInfo = new RiverInfo(river); |
8854 | 53 |
8877 | 54 final Collection<DifferencesPair> diffPairs = access.getDifferencePairs(); |
55 | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
56 final DoubleRange calcRange = access.getRange(); |
8877 | 57 |
58 final boolean useTkh = access.isUseTransportBodies(); | |
59 | |
60 /* calculate results for each diff pair */ | |
61 final Calculation problems = new Calculation(); | |
62 | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
63 final RiverInfoProvider infoProvider = RiverInfoProvider.forRange(this.context, river, calcRange); |
8877 | 64 |
8882 | 65 final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); |
8877 | 66 |
8894 | 67 final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, riverInfo, calcRange, useTkh); |
8877 | 68 |
69 for (final DifferencesPair diffPair : diffPairs) { | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
70 final FlowDepthCalculationResult result = calculateResult(calcRange, diffPair, problems, infoProvider, useTkh); |
8882 | 71 if (result != null) |
8877 | 72 results.addResult(result); |
73 } | |
74 | |
8882 | 75 return new CalculationResult(results, problems); |
8877 | 76 } |
77 | |
8898 | 78 /** |
79 * Calculates one W-MSH differences pair. | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
80 * |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
81 * @param infoProvider |
8898 | 82 */ |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
83 private FlowDepthCalculationResult calculateResult(final DoubleRange calcRange, final DifferencesPair diffPair, |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
84 final Calculation problems, final RiverInfoProvider infoProvider, final boolean useTkh) { |
8877 | 85 |
86 /* access real input data from database */ | |
87 final String soundingId = diffPair.getSoundingId(); | |
88 final String wstId = diffPair.getWstId(); | |
89 | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
90 final BedHeightsFinder bedHeight = loadBedHeight(soundingId, calcRange); |
8882 | 91 if (bedHeight == null) { |
8884 | 92 final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", soundingId); |
8877 | 93 problems.addProblem(message); |
94 return null; | |
95 } | |
96 | |
8882 | 97 /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */ |
8884 | 98 final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, Double.NaN); |
8882 | 99 if (waterlevel == null) { |
8884 | 100 final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", wstId); |
8877 | 101 problems.addProblem(message); |
102 return null; | |
103 } | |
8882 | 104 final WKms wstKms = waterlevel.getWkms(); |
105 | |
8883 | 106 final String wspLabel = wstKms.getName(); |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
107 final String soundingLabel = bedHeight.getInfo().getDescription(); |
8883 | 108 final String label = String.format("%s - %s", wspLabel, soundingLabel); |
8877 | 109 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
110 checkYearDifference(label, waterlevel, bedHeight.getInfo().getYear(), problems); |
8894 | 111 checkWaterlevelDiscretisation(wstKms, calcRange, problems); |
8901 | 112 // TODO: prüfen, ob sohlhöhen die calcRange abdecken/überschneiden |
8882 | 113 |
114 /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
115 final RiverInfoProvider riverInfoProvider = infoProvider.forWaterlevel(waterlevel); |
8894 | 116 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
117 final int wspYear = waterlevel.getYear(); |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
118 final WstInfo wstInfo = new WstInfo(wspLabel, wspYear, riverInfoProvider.getReferenceGauge()); |
8914
e3519c3e7a0a
Workflow for SINFO-Transport bodies heights inclduing winfo calculation
gernotbelger
parents:
8911
diff
changeset
|
119 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
120 final DischargeValuesFinder dischargeProvider = DischargeValuesFinder.fromKms(wstKms); |
8886 | 121 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
122 final River river = riverInfoProvider.getRiver(); |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
123 final TkhCalculator tkhCalculator = TkhCalculator.buildTkhCalculator(useTkh, this.context, problems, label, river, calcRange, dischargeProvider, |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
124 bedHeight); |
8883 | 125 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
126 final FlowDepthCalculator calculator = new FlowDepthCalculator(riverInfoProvider, wstKms, dischargeProvider, bedHeight, tkhCalculator); |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
127 return calculator.execute(label, wstInfo, calcRange); |
8877 | 128 } |
129 | |
8891 | 130 |
131 /** | |
8883 | 132 * Checks the year difference between waterlevels and sounding, and issues a warning if too big. |
133 * | |
134 * Zeitraum Zeitliche Differenz [a] | |
135 * X ≥ 1998 ± 3 | |
136 * 1958 ≤ X < 1998 ± 6 | |
137 * 1918 ≤ X < 1958 ± 12 | |
138 * X < 1918 ± 25 | |
139 */ | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
140 private void checkYearDifference(final String label, final WaterlevelData waterlevel, final Integer soundingYear, final Calculation problems) { |
8883 | 141 if (soundingYear == null) |
142 return; | |
143 | |
144 final int wstYear = waterlevel.getYear(); | |
145 if (wstYear < 0) | |
146 return; | |
147 | |
148 final int maxDifference = getMaxDifferenceYears(soundingYear); | |
149 | |
150 final int difference = Math.abs(soundingYear - wstYear); | |
151 if (difference > maxDifference) { | |
8894 | 152 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.year_difference", null, label, wstYear, |
153 soundingYear); | |
8883 | 154 problems.addProblem(message); |
155 } | |
156 } | |
157 | |
158 private int getMaxDifferenceYears(final int year) { | |
159 | |
160 if (year < 1918) | |
161 return 25; | |
162 | |
163 if (1918 <= year && year < 1958) | |
164 return 12; | |
165 | |
166 if (1958 <= year && year < 1998) | |
167 return 6; | |
168 | |
169 /* >= 1998 */ | |
170 return 3; | |
171 } | |
172 | |
8882 | 173 /* Checks if the discretisation of the waterlevel exceeds 1000m */ |
8894 | 174 |
175 private void checkWaterlevelDiscretisation(final WKms wstKms, final DoubleRange calcRange, final Calculation problems) { | |
176 | |
8882 | 177 final int size = wstKms.size(); |
178 for (int i = 0; i < size - 2; i++) { | |
179 final double kmPrev = wstKms.getKm(i); | |
180 final double kmNext = wstKms.getKm(i + 1); | |
181 | |
8894 | 182 /* only check if we are within the calculation range */ |
183 if (calcRange.overlapsRange(new DoubleRange(kmPrev, kmNext))) { | |
184 if (Math.abs(kmPrev - kmNext) > 1) { | |
185 final String label = wstKms.getName(); | |
8882 | 186 |
8894 | 187 final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label); |
188 problems.addProblem(kmPrev, message); | |
189 } | |
8882 | 190 } |
191 } | |
192 } | |
193 | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
194 private BedHeightsFinder loadBedHeight(final String soundingId, final DoubleRange calcRange) { |
8877 | 195 |
8883 | 196 // REMARK: absolutely unbelievable.... |
8898 | 197 // The way how bed-heights (and other data too) is accessed is different for nearly every calculation-type |
8882 | 198 // throughout flys. |
8877 | 199 // The knowledge on how to parse the datacage-ids is spread through the complete code-base... |
200 | |
8882 | 201 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but |
202 // this is plain random | |
8877 | 203 final String[] parts = soundingId.split(";"); |
204 | |
205 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); | |
206 | |
207 final Integer bedheightId = artifact.getDataAsInteger("height_id"); | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
208 if (bedheightId == null) { |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
209 // FIXME: error message! |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
210 return null; |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
211 } |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
212 |
8883 | 213 // REMARK: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the |
8882 | 214 // other type means) |
8877 | 215 // Luckily, the requirement is to only access 'single' data here. |
216 // final String bedheightType = artifact.getDataAsString("type"); | |
217 | |
8883 | 218 // REMARK: BedDifferences uses this, but we also need the metadata of the BedHeight |
219 // REMARK: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via | |
8882 | 220 // hibernate stuff |
8877 | 221 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
8882 | 222 // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
8877 | 223 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8914
diff
changeset
|
224 return BedHeightsFinder.forId(bedheightId, calcRange); |
8898 | 225 } |
8884 | 226 } |