Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java @ 9665:8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
author | mschaefer |
---|---|
date | Wed, 06 May 2020 16:17:38 +0200 |
parents | 8e6b9cb9486a |
children |
rev | line source |
---|---|
8898 | 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 | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
11 package org.dive4elements.river.artifacts.sinfo.tkhcalculation; |
8898 | 12 |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
13 import java.io.IOException; |
8898 | 14 import java.util.Date; |
15 import java.util.List; | |
16 | |
17 import org.apache.commons.lang.math.DoubleRange; | |
18 import org.apache.commons.math.ArgumentOutsideDomainException; | |
19 import org.apache.commons.math.analysis.interpolation.LinearInterpolator; | |
20 import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; | |
21 import org.apache.log4j.Logger; | |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
22 import org.dive4elements.river.artifacts.math.Utils; |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
23 import org.dive4elements.river.artifacts.model.Calculation; |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
24 import org.dive4elements.river.artifacts.model.DateRange; |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
25 import org.dive4elements.river.artifacts.sinfo.tkhstate.BedQualityD50TimeRangeConfig; |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
26 import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException; |
8898 | 27 import org.dive4elements.river.backend.SedDBSessionHolder; |
28 import org.dive4elements.river.model.River; | |
29 import org.hibernate.SQLQuery; | |
30 import org.hibernate.Session; | |
31 import org.hibernate.type.StandardBasicTypes; | |
32 | |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
33 import gnu.trove.TDoubleArrayList; |
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
34 |
8898 | 35 /** |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
36 * Searchable sorted km array with parallel bed measurements value array and linear interpolation for km and d50 between |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
37 * the array elements.<br /> |
8901 | 38 * <br /> |
39 * See comment of SQL command on how the values are filtered and aggregated. | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
40 * |
8898 | 41 * @author Matthias Schäfer |
42 * | |
43 */ | |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
44 public class BedQualityD50KmValueFinder { |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
45 |
8898 | 46 /***** FIELDS *****/ |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
47 |
8898 | 48 /** |
49 * Private log to use here. | |
50 */ | |
51 private static Logger log = Logger.getLogger(BedQualityD50KmValueFinder.class); | |
52 | |
53 /** | |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
54 * Query selecting all sub layer bed measurements with their d50 for a km range and a time period<br /> |
8898 | 55 * <br /> |
56 * A km may have bed measurements for multiple dates, multiple distances from the river bank, and multiple depth layers. | |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
57 * The query filters by km range, time period and layer (sub layer: below bed to max. 50 cm depth).<br /> |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
58 * |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
59 * If PostgreSQL would support a median aggregate function like Oracle does, the aggregation could be placed into this |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
60 * query. |
8898 | 61 */ |
9017 | 62 private static final String SQL_BED_D50_SUBLAYER_MEASUREMENT = // |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
63 "SELECT s.km, t.datum, p.tiefevon, p.tiefebis, a.d50" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
64 + " FROM sohltest t INNER JOIN station s ON t.stationid = s.stationid" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
65 + " INNER JOIN gewaesser g ON s.gewaesserid = g.gewaesserid" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
66 + " INNER JOIN sohlprobe p ON t.sohltestid = p.sohltestid" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
67 + " INNER JOIN siebanalyse a ON p.sohlprobeid = a.sohlprobeid" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
68 + " WHERE (g.name = :name)" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
69 + " AND (p.tiefevon > 0.0) AND (p.tiefebis <= 0.5)" // |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
70 + " AND (t.datum BETWEEN :fromdate AND :todate)" // |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
71 + " ORDER BY s.km ASC, a.d50 ASC"; |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
72 |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
73 public static final Date MIN_DATE = new Date(-10000000000000l); // Database does not cope with Long.MIN/Long.Max, so we go just a few hundred years |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
74 // back/forward |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
75 public static final Date MAX_DATE = new Date(10000000000000l); |
9339
f812880963a9
Fixed: d50 interpolation always with complete river range
mschaefer
parents:
9091
diff
changeset
|
76 |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
77 private Calculation problems; |
8898 | 78 |
79 /** | |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
80 * Real linear interpolator for kms and d50 values (m) |
8898 | 81 */ |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
82 private final PolynomialSplineFunction interpolator; |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
83 |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
84 /***** CONSTRUCTORS *****/ |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
85 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
86 private BedQualityD50KmValueFinder(final Calculation problems, final double[] kms, final double[] values) { |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
87 this.problems = problems; |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
88 |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
89 this.interpolator = new LinearInterpolator().interpolate(kms, values); |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
90 } |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
91 |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
92 /***** METHODS *****/ |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
93 |
8898 | 94 /** |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
95 * Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb) |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
96 * Abhängig von Peiljahr |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
97 * |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
98 * @param problems |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
99 * @throws TsvReaderException |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
100 * @throws IOException |
8898 | 101 */ |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
102 public static BedQualityD50KmValueFinder loadBedMeasurements(final Calculation problems, final River river, final DoubleRange kmRange, |
9532 | 103 final int soundingYear) { |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
104 |
9532 | 105 final DateRange dateRange = BedQualityD50TimeRangeConfig.getDefaults(river, soundingYear, problems); |
106 if (dateRange == null) | |
107 return null; | |
108 | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
109 return loadBedMeasurements(problems, river, kmRange, dateRange, soundingYear); |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
110 } |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
111 |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
112 private static BedQualityD50KmValueFinder loadBedMeasurements(final Calculation problems, final River river, final DoubleRange kmRange, |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
113 final DateRange dateRange, final int soundingYear) { |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
114 |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
115 final Date startTime = dateRange.getFrom(); |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
116 final Date endTime = dateRange.getTo(); |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
117 |
9091 | 118 final String seddbRiver = river.nameForSeddb(); |
119 log.debug(String.format("loadValues '%s' km %.3f - %.3f %tF - %tF", seddbRiver, kmRange.getMinimumDouble(), kmRange.getMaximumDouble(), startTime, | |
120 endTime)); | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
121 final Session session = SedDBSessionHolder.HOLDER.get(); |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
122 final SQLQuery sqlQuery = session.createSQLQuery(SQL_BED_D50_SUBLAYER_MEASUREMENT).addScalar("km", StandardBasicTypes.DOUBLE) |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
123 .addScalar("datum", StandardBasicTypes.DATE).addScalar("tiefevon", StandardBasicTypes.DOUBLE).addScalar("tiefebis", StandardBasicTypes.DOUBLE) |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
124 .addScalar("d50", StandardBasicTypes.DOUBLE); |
8898 | 125 sqlQuery.setString("name", seddbRiver); |
9339
f812880963a9
Fixed: d50 interpolation always with complete river range
mschaefer
parents:
9091
diff
changeset
|
126 // sqlQuery.setDouble("fromkm", kmRange.getMinimumDouble()); |
f812880963a9
Fixed: d50 interpolation always with complete river range
mschaefer
parents:
9091
diff
changeset
|
127 // sqlQuery.setDouble("tokm", kmRange.getMaximumDouble()); |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
128 sqlQuery.setDate("fromdate", startTime); |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
129 sqlQuery.setDate("todate", endTime); |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
130 final List<Object[]> rows = sqlQuery.list(); |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
131 |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
132 // Aggregate d50 values for each km |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
133 // final TDoubleArrayList kmsm = new TDoubleArrayList(); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
134 // final TDoubleArrayList valuesm = new TDoubleArrayList(); |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
135 final TDoubleArrayList kms = new TDoubleArrayList(); |
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
136 final TDoubleArrayList values = new TDoubleArrayList(); |
9081 | 137 if (rows != null) { |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
138 // // median |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
139 // final TDoubleArrayList kmd50s = new TDoubleArrayList(); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
140 // for (int i = 0; i <= rows.size() - 1; i++) { |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
141 // log.trace("loadValues rows(" + i + ") " + rows.get(i)[0] + " " + rows.get(i)[1] + " " + rows.get(i)[2] + " " + |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
142 // rows.get(i)[3] + " " |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
143 // + rows.get(i)[4]); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
144 // kmd50s.add((double) rows.get(i)[4]); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
145 // if (((i == rows.size() - 1) || !Utils.epsilonEquals((double) rows.get(i)[0], (double) rows.get(i + 1)[0], 0.0001))) { |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
146 // final int k = kmd50s.size() / 2; |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
147 // valuesm.add(((k + k < kmd50s.size()) ? kmd50s.get(k) : (kmd50s.get(k - 1) + kmd50s.get(k)) / 2) / 1000); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
148 // kmsm.add((double) rows.get(i)[0]); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
149 // log.debug(String.format("loadValues km %.3f d50(mm) %.1f count %d", kmsm.get(kmsm.size() - 1), |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
150 // valuesm.get(valuesm.size() - 1) * 1000, |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
151 // kmd50s.size())); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
152 // kmd50s.clear(); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
153 // } |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
154 // } |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
155 // arithmetic mean |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
156 double sum = 0; |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
157 int n = 0; |
9081 | 158 for (int i = 0; i <= rows.size() - 1; i++) { |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
159 sum += (double) rows.get(i)[4]; |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
160 n++; |
9081 | 161 if (((i == rows.size() - 1) || !Utils.epsilonEquals((double) rows.get(i)[0], (double) rows.get(i + 1)[0], 0.0001))) { |
162 kms.add((double) rows.get(i)[0]); | |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
163 values.add((sum / n) / 1000); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
164 log.debug(String.format("loadValues km %.3f d50(mm) %.1f count %d", kms.get(kms.size() - 1), values.get(values.size() - 1) * 1000, n)); |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
165 sum = 0; |
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
166 n = 0; |
9081 | 167 } |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
168 } |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
169 } |
9017 | 170 if (kms.size() >= 1) |
171 log.debug(String.format("loadValues: %d kms found from %.3f to %.3f", kms.size(), kms.get(0), kms.get(kms.size() - 1))); | |
172 else | |
173 log.debug("loadValues: no kms found"); | |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
174 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
175 if (kms.size() < 2 || values.size() < 2) { |
9468 | 176 problems.addProblem("bedqualityd50kmvaluefinder.empty", Integer.toString(soundingYear)); |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
177 return null; |
8898 | 178 } |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
179 |
8911
37ff7f435912
SINFO Flowdepth: more error checks, d50 interpolation, avoid negative tkh
mschaefer
parents:
8901
diff
changeset
|
180 try { |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
181 return new BedQualityD50KmValueFinder(problems, kms.toNativeArray(), values.toNativeArray()); |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
182 } |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
183 catch (final Exception e) { |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
184 e.printStackTrace(); |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
185 problems.addProblem("bedqualityd50kmvaluefinder.error", e.getLocalizedMessage()); |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
186 return null; |
8911
37ff7f435912
SINFO Flowdepth: more error checks, d50 interpolation, avoid negative tkh
mschaefer
parents:
8901
diff
changeset
|
187 } |
8898 | 188 } |
189 | |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
190 /** |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
191 * Returns the d50 value interpolated according to a km |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
192 * |
8920
29442c03c6e3
d50 aggregation by median instead of arithmetic mean,
mschaefer
parents:
8915
diff
changeset
|
193 * @return d50 (m) of the km, or NaN |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
194 */ |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
195 public double findD50(final double km) { |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
196 try { |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
197 return this.interpolator.value(km); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
198 } |
9665
8a84c9fef8ec
S-info/tkh: d50 average changed from median to arithmetic mean
mschaefer
parents:
9532
diff
changeset
|
199 catch (final ArgumentOutsideDomainException e) { |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9468
diff
changeset
|
200 // No stack trace because this might happen a lot (intended) and we produce an error message anyways. |
9086 | 201 // e.printStackTrace(); |
8964
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
202 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
203 if (this.problems != null) { |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
204 this.problems.addProblem(km, "bedqualityd50kmvaluefinder.missing"); |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
205 // Report only once |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
206 this.problems = null; |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
207 } |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
208 |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
209 return Double.NaN; |
45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents:
8921
diff
changeset
|
210 } |
8915
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
211 } |
d9dbf0b74bc2
Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents:
8911
diff
changeset
|
212 } |