comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/GaugeDischargeValuesFinder.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents
children b4402594213b
comparison
equal deleted inserted replaced
9175:34dc0163ad2d 9176:1614cb14308f
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.common;
11
12 import org.apache.commons.lang.math.DoubleRange;
13 import org.apache.commons.math.FunctionEvaluationException;
14 import org.apache.commons.math.analysis.UnivariateRealFunction;
15 import org.apache.commons.math.analysis.interpolation.LinearInterpolator;
16 import org.dive4elements.river.artifacts.model.Calculation;
17 import org.dive4elements.river.model.DischargeTable;
18 import org.dive4elements.river.model.DischargeTableValue;
19 import org.dive4elements.river.model.Gauge;
20
21 import gnu.trove.TDoubleArrayList;
22
23 /**
24 * Loading and search/interpolation of a gauge's discharge table (.at)
25 *
26 * @author Matthias Schäfer
27 *
28 */
29 public final class GaugeDischargeValuesFinder {
30
31 /***** FIELDS *****/
32
33 // private static Logger log = Logger.getLogger(GaugeDischargeValuesFinder.class);
34
35 private final Gauge gauge;
36
37 private Calculation problems;
38
39 private final UnivariateRealFunction wInterpolator;
40
41 private final DoubleRange wRange;
42
43
44 /***** CONSTRUCTORS *****/
45
46 private GaugeDischargeValuesFinder(final Gauge gauge, final Calculation problems, final DischargeTable dischargeTable) {
47 this.gauge = gauge;
48 this.problems = problems;
49 final TDoubleArrayList ws = new TDoubleArrayList();
50 final TDoubleArrayList qs = new TDoubleArrayList();
51 for (final DischargeTableValue v : DischargeTable.getValuesSortedByW(dischargeTable)) {
52 ws.add(v.getW().doubleValue());
53 qs.add(v.getQ().doubleValue());
54 }
55 if (ws.size() >= 2) {
56 this.wInterpolator = new LinearInterpolator().interpolate(ws.toNativeArray(), qs.toNativeArray());
57 this.wRange = new DoubleRange(ws.get(0), ws.get(ws.size() - 1));
58 }
59 else {
60 this.wInterpolator = null;
61 this.wRange = null;
62 }
63 if ((this.wInterpolator == null) && (this.problems != null)) {
64 this.problems.addProblem("gauge_discharge_table.missing", gauge.getName());
65 // Report only once
66 this.problems = null;
67 }
68 }
69
70
71 /***** METHODS *****/
72
73 /**
74 * Loads the the main discharge table of a gauge (GAUGE.at)
75 *
76 * @return The discharge table values finder of the gauge, or null
77 */
78 public static GaugeDischargeValuesFinder loadValues(final Gauge gauge, final Calculation problems) {
79 final DischargeTable table = DischargeTable.getGaugeMainDischargeTable(gauge);
80 if ((table == null) || (table.getDischargeTableValues().size() == 0)) {
81 problems.addProblem("gauge_discharge_table.missing", gauge.getName());
82 return null;
83 }
84 else
85 return new GaugeDischargeValuesFinder(gauge, problems, table);
86 }
87
88 /**
89 * If this provider may return valid data at all.
90 */
91 public boolean isValid() {
92 return (this.wInterpolator != null);
93 }
94
95 /**
96 * Discharge for a W
97 *
98 * @param w
99 * W in cm above gauge datum
100 * @return Q, or NegInf for w less than all, or PosInf for w greater then all, or NaN in case of exception
101 */
102 public double getDischarge(final double w) {
103 try {
104 if (this.wInterpolator == null)
105 return Double.NaN;
106 else if (w < this.wRange.getMinimumDouble())
107 return Double.NEGATIVE_INFINITY;
108 else if (w > this.wRange.getMaximumDouble())
109 return Double.POSITIVE_INFINITY;
110 else
111 return this.wInterpolator.value(w);
112 }
113 catch (@SuppressWarnings("unused") final FunctionEvaluationException e) {
114 // ignore exception because this can/will happen regularly
115 return Double.NaN;
116 }
117 }
118 }

http://dive4elements.wald.intevation.org