annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/SoilKindKmValueFinder.java @ 8980:b194fa64506a

SINFO - show results themes according to spec, either raw data or floating mean values. Some improvements to error handling and handling of empty results.
author gernotbelger
date Thu, 05 Apr 2018 18:30:34 +0200
parents 45f1ad66560e
children 6e5ff436febe
rev   line source
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
2 * Software engineering by
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
3 * Björnsen Beratende Ingenieure GmbH
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
5 *
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
6 * This file is Free Software under the GNU AGPL (>=v3)
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
8 * documentation coming with Dive4Elements River for details.
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
9 */
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
10
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
11 package org.dive4elements.river.artifacts.sinfo.tkhcalculation;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
12
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
13 import java.util.List;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
14 import java.util.Map.Entry;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
15 import java.util.NavigableMap;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
16 import java.util.TreeMap;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
17
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
18 import org.apache.commons.lang.math.DoubleRange;
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
19 import org.dive4elements.river.artifacts.model.Calculation;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
20 import org.dive4elements.river.backend.SessionHolder;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
21 import org.dive4elements.river.model.River;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
22 import org.hibernate.SQLQuery;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
23 import org.hibernate.Session;
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
24 import org.hibernate.type.StandardBasicTypes;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
25
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
26 /**
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
27 * @author Matthias Schäfer
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
28 */
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
29 final class SoilKindKmValueFinder {
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
30
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
31 /***** FIELDS *****/
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
32
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
33 // private static Logger log = Logger.getLogger(SoilKindKmValueFinder.class);
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
34
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
35 /**
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
36 * Query selecting the bed mobility attribute for a range of stations of a river
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
37 */
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
38 private static final String SQL_BED_MOBILITY = "SELECT bmv.station, bmv.moving"
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
39 + " FROM bed_mobility bm INNER JOIN bed_mobility_values bmv ON bm.id = bmv.bed_mobility_id"
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
40 + " WHERE (bm.river_id=:river_id) AND (bmv.station BETWEEN (:fromkm-0.0001) AND (:tokm+0.0001))" + " ORDER BY bmv.station ASC";
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
41
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
42 private final NavigableMap<Double, SoilKind> kmMobility = new TreeMap<>();
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
43
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
44 private Calculation problems;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
45
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
46 /***** CONSTRUCTORS *****/
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
47
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
48 private SoilKindKmValueFinder(final Calculation problems, final List<Object[]> queryRows) {
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
49 this.problems = problems;
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
50
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
51 for (int i = 0; i <= queryRows.size() - 1; i++) {
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
52 this.kmMobility.put(Double.valueOf((double) queryRows.get(i)[0]), (((int) queryRows.get(i)[1]) == 1) ? SoilKind.mobil : SoilKind.starr);
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
53 }
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
54 }
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
55
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
56 /***** METHODS *****/
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
57
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
58 /**
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
59 * Loads the range of the river's kms with their soil kind.
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
60 *
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
61 * @return Whether the load has been successful
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
62 */
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
63 public static SoilKindKmValueFinder loadValues(final Calculation problems, final River river, final DoubleRange kmRange) {
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
64 final Session session = SessionHolder.HOLDER.get();
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
65 final SQLQuery sqlQuery = session.createSQLQuery(SQL_BED_MOBILITY).addScalar("station", StandardBasicTypes.DOUBLE).addScalar("moving",
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
66 StandardBasicTypes.INTEGER);
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
67 sqlQuery.setInteger("river_id", river.getId().intValue());
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
68 sqlQuery.setDouble("fromkm", kmRange.getMinimumDouble());
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
69 sqlQuery.setDouble("tokm", kmRange.getMaximumDouble());
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
70 final List<Object[]> rows = sqlQuery.list();
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
71 if (rows.size() >= 1)
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
72 return new SoilKindKmValueFinder(problems, rows);
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
73
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
74 problems.addProblem("soilkindkmvaluefinder.empty");
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
75 return null;
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
76 }
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
77
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
78 /***** METHODS *****/
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
79
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
80 /**
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
81 * Searches a km with its soil kind
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
82 */
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
83 public SoilKind findSoilKind(final double km) {
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
84
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
85 if (this.kmMobility.containsKey(Double.valueOf(km)))
8952
1a8f19f3b776 Connection to new database table bed_mobility implemented
mschaefer
parents: 8915
diff changeset
86 return this.kmMobility.get(Double.valueOf(km));
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
87
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
88 final Entry<Double, SoilKind> streamUp = this.kmMobility.floorEntry(Double.valueOf(km));
8980
b194fa64506a SINFO - show results themes according to spec, either raw data or floating mean values.
gernotbelger
parents: 8964
diff changeset
89 if (streamUp == null) {
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
90 reportProblem(km);
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
91 return null;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
92 }
8964
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
93
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
94 // Return the soil kind of the neighbouring station with the shorter distance to the candidate.
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
95 final Entry<Double, SoilKind> streamDown = this.kmMobility.ceilingEntry(Double.valueOf(km));
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
96 if (streamDown == null)
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
97 return streamUp.getValue();
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
98
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
99 final double streamUpValue = streamUp.getKey().doubleValue();
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
100 final double streamDownValue = streamDown.getKey().doubleValue();
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
101
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
102 if ((streamUpValue + streamDownValue) / 2 <= km)
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
103 return streamUp.getValue();
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
104
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
105 return streamDown.getValue();
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
106 }
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
107
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
108 private void reportProblem(final double km) {
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
109 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: 8952
diff changeset
110 return;
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
111
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
112 this.problems.addProblem(km, "soilkindkmvaluefinder.missing");
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
113
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
114 // report problem only once
45f1ad66560e Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
gernotbelger
parents: 8952
diff changeset
115 this.problems = null;
8898
89f3c5462a16 Implemented S-INFO Flowdepth TKH calculation
mschaefer
parents:
diff changeset
116 }
8915
d9dbf0b74bc2 Refaktoring of flow depth calculation, extracting tkh part. First implementation of tkh calculation.
gernotbelger
parents: 8898
diff changeset
117 }

http://dive4elements.wald.intevation.org