Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/GaugeDischargeValuesFinder.java @ 9157:f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
author | mschaefer |
---|---|
date | Tue, 19 Jun 2018 14:19:32 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9156:568961ff709a | 9157:f9bb5d0a6ff3 |
---|---|
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.collision; | |
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 | |
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 final 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.isEmpty()) | |
56 this.wRange = new DoubleRange(ws.get(0), ws.get(ws.size() - 1)); | |
57 else | |
58 this.wRange = null; | |
59 this.wInterpolator = new LinearInterpolator().interpolate(ws.toNativeArray(), qs.toNativeArray()); | |
60 } | |
61 | |
62 | |
63 /***** METHODS *****/ | |
64 | |
65 /** | |
66 * Loads the the main discharge table of a gauge (GAUGE.at) | |
67 * | |
68 * @return The discharge table values finder of the gauge, or null | |
69 */ | |
70 public static GaugeDischargeValuesFinder loadValues(final Gauge gauge, final Calculation problems) { | |
71 final DischargeTable table = DischargeTable.getGaugeMainDischargeTable(gauge); | |
72 if ((table == null) || (table.getDischargeTableValues().size() == 0)) | |
73 return null; | |
74 else { | |
75 return new GaugeDischargeValuesFinder(gauge, problems, table); | |
76 } | |
77 } | |
78 | |
79 /** | |
80 * If this provider may return valid data at all. | |
81 */ | |
82 public boolean isValid() { | |
83 return (this.wInterpolator != null); | |
84 } | |
85 | |
86 /** | |
87 * Discharge for a W | |
88 * | |
89 * @return Q, or NegInf for w less than all, or PosInf for w greater then all, or NaN in case of exception | |
90 */ | |
91 public double getDischarge(final double w) { | |
92 try { | |
93 if (this.wInterpolator == null) | |
94 return Double.NaN; | |
95 else if (w < this.wRange.getMinimumDouble()) | |
96 return Double.NEGATIVE_INFINITY; | |
97 else if (w > this.wRange.getMaximumDouble()) | |
98 return Double.POSITIVE_INFINITY; | |
99 else | |
100 return this.wInterpolator.value(w); | |
101 } | |
102 catch (@SuppressWarnings("unused") final FunctionEvaluationException e) { | |
103 // ignore exception because this can/will happen regularly | |
104 return Double.NaN; | |
105 } | |
106 } | |
107 } |