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

Implementing SINFO - FlowDepthMinMax calculation
author gernotbelger
date Tue, 13 Mar 2018 18:49:33 +0100
parents 82998242ba84
children a4f1ac81f26d
comparison
equal deleted inserted replaced
8945:4a6b6a3c279c 8946:5d5d482da3e9
11 11
12 import java.util.ArrayList; 12 import java.util.ArrayList;
13 import java.util.Collection; 13 import java.util.Collection;
14 14
15 import org.apache.commons.lang.math.DoubleRange; 15 import org.apache.commons.lang.math.DoubleRange;
16 import org.apache.commons.math.FunctionEvaluationException;
17 import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
18 import org.dive4elements.river.artifacts.model.WKms;
19 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; 16 import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider;
20 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.DischargeValuesFinder;
21 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.Tkh; 17 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.Tkh;
22 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.TkhCalculator; 18 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.TkhCalculator;
23 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; 19 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder;
24 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; 20 import org.dive4elements.river.artifacts.sinfo.util.WstInfo;
25 import org.dive4elements.river.utils.DoubleUtil;
26 21
27 /** 22 /**
28 * @author Gernot Belger 23 * @author Gernot Belger
29 */ 24 */
30 final class FlowDepthCalculator { 25 final class FlowDepthCalculator {
31 26
32 private final Collection<FlowDepthRow> rows = new ArrayList<>(); 27 private final Collection<FlowDepthRow> rows = new ArrayList<>();
33 28
34 private final DischargeValuesFinder dischargeProvider;
35
36 private final BedHeightsFinder bedHeight; 29 private final BedHeightsFinder bedHeight;
37 30
38 private final TkhCalculator tkhCalculator; 31 private final TkhCalculator tkhCalculator;
39
40 private final PolynomialSplineFunction wstInterpolator;
41 32
42 private final RiverInfoProvider riverInfoProvider; 33 private final RiverInfoProvider riverInfoProvider;
43 34
44 private final String bedHeightLabel; 35 private final String bedHeightLabel;
45 36
46 private final String wstLabel; 37 private final String wstLabel;
47 38
48 public FlowDepthCalculator(final RiverInfoProvider riverInfoProvider, final WKms wstKms, 39 public FlowDepthCalculator(final RiverInfoProvider riverInfoProvider, final String wstLabel, final BedHeightsFinder bedHeight,
49 final DischargeValuesFinder dischargeProvider, final BedHeightsFinder bedHeight, final TkhCalculator tkhCalculator) { 40 final TkhCalculator tkhCalculator) {
50 41
51 this.riverInfoProvider = riverInfoProvider; 42 this.riverInfoProvider = riverInfoProvider;
43 this.wstLabel = wstLabel;
52 44
53 this.dischargeProvider = dischargeProvider;
54 this.bedHeight = bedHeight; 45 this.bedHeight = bedHeight;
55 this.tkhCalculator = tkhCalculator; 46 this.tkhCalculator = tkhCalculator;
56 47
57 this.wstInterpolator = DoubleUtil.getLinearInterpolator(wstKms.allKms(), wstKms.allWs());
58
59 this.bedHeightLabel = bedHeight.getInfo().getDescription(); 48 this.bedHeightLabel = bedHeight.getInfo().getDescription();
60 this.wstLabel = wstKms.getName();
61 } 49 }
62 50
63 public FlowDepthCalculationResult execute(final String label, final WstInfo wstInfo, final DoubleRange calcRange) { 51 public FlowDepthCalculationResult execute(final String label, final WstInfo wstInfo, final DoubleRange calcRange) {
64 52
65 final Collection<Double> stations = this.bedHeight.getStations(); 53 final Collection<Double> stations = this.bedHeight.getStations();
66 for (final Double station : stations) { 54 for (final Double station : stations) {
67 if (calcRange.containsDouble(station)) 55 if (calcRange.containsDouble(station))
68 calculateResultRow(station); 56 calculateResultRow(station);
69 } 57 }
70 58
71 return new FlowDepthCalculationResult(label, wstInfo, this.bedHeight.getInfo(), this.tkhCalculator != null, this.rows); 59 final boolean hasTkh = this.tkhCalculator.hasTkh();
60
61 return new FlowDepthCalculationResult(label, wstInfo, this.bedHeight.getInfo(), hasTkh, this.rows);
72 } 62 }
73 63
74 private void calculateResultRow(final double station) { 64 private void calculateResultRow(final double station) {
75 65
76 try { 66 final Tkh tkh = this.tkhCalculator.getTkh(station);
77 // FIXME: check out of range of waterlevel?
78 final double wst = this.wstInterpolator.value(station);
79 67
80 final Tkh tkh = calculateTkh(station, wst); 68 // REMARK: access the location once only during calculation
69 final String location = this.riverInfoProvider.getLocation(station);
81 70
82 // REMARK: access the location once only during calculation 71 // REMARK: access the gauge once only during calculation
83 final String location = this.riverInfoProvider.getLocation(station); 72 final String gaugeLabel = this.riverInfoProvider.findGauge(station);
84 73
85 // REMARK: access the gauge once only during calculation 74 this.rows.add(new FlowDepthRow(tkh, this.wstLabel, gaugeLabel, this.bedHeightLabel, location));
86 final String gaugeLabel = this.riverInfoProvider.findGauge(station);
87
88 this.rows.add(new FlowDepthRow(tkh, this.wstLabel, gaugeLabel, this.bedHeightLabel, location));
89 }
90 catch (final FunctionEvaluationException e) {
91 /* should only happen if out of range */
92 e.printStackTrace();
93 /* simply ignore */
94 }
95 }
96
97 private Tkh calculateTkh(final double station, final double wst) throws FunctionEvaluationException {
98 if (this.tkhCalculator == null) {
99 final double discharge = this.dischargeProvider.getDischarge(station);
100 final double meanBedHeight = this.bedHeight.getMeanBedHeight(station);
101 final double flowDepth = wst - meanBedHeight;
102 return new Tkh(station, wst, meanBedHeight, flowDepth, discharge);
103 }
104
105 return this.tkhCalculator.getTkh(station, wst);
106 } 75 }
107 } 76 }

http://dive4elements.wald.intevation.org