# HG changeset patch
# User mschaefer
# Date 1519891318 -3600
# Node ID 29442c03c6e3fa205ac663714868dea604502b2e
# Parent 5d5d0051723fec488b7df6bfefba86c0a5bc7931
d50 aggregation by median instead of arithmetic mean,
negative tkh replaced by 0
diff -r 5d5d0051723f -r 29442c03c6e3 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java Wed Feb 28 18:55:39 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java Thu Mar 01 09:01:58 2018 +0100
@@ -19,12 +19,15 @@
import org.apache.commons.math.analysis.interpolation.LinearInterpolator;
import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
import org.apache.log4j.Logger;
+import org.dive4elements.river.artifacts.math.Utils;
import org.dive4elements.river.backend.SedDBSessionHolder;
import org.dive4elements.river.model.River;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;
+import gnu.trove.TDoubleArrayList;
+
/**
* Searchable sorted km array with parallel bed measurements value array and linear interpolation for km and d50 between
* the array elements.
@@ -34,116 +37,7 @@
* @author Matthias Schäfer
*
*/
-final class BedQualityD50KmValueFinder {
-
- /***** INNER CLASSES *****/
-
- /**
- * A bed measurements aggregate with its d50 characteristic grain diameter
- */
- private static class D50Measurement {
- private double km;
-
- public double getKm() {
- return this.km;
- }
-
- // private Date mindate;
-
- // public Date getMinDate() {
- // return this.mindate;
- // }
-
- // private Date maxdate;
-
- // public Date getMaxDate() {
- // return this.maxdate;
- // }
-
- private int cnt;
-
- public int getCnt() {
- return this.cnt;
- }
-
- // private double mindepth;
-
- // public double getMinDepth() {
- // return this.mindepth;
- // }
-
- // private double maxdepth;
-
- // public double getMaxDepth() {
- // return this.maxdepth;
- // }
-
- private double d50;
-
- /**
- * D50 in m
- */
- public double getD50() {
- return this.d50;
- }
-
- // /**
- // * Parameter constructor
- // */
- // public D50Measurement(final double km, final Date mindate, final Date maxdate, final int cnt, final double mindepth,
- // final double maxdepth,
- // final double d50mm) {
- // this.km = km;
- // this.mindate = mindate;
- // this.maxdate = maxdate;
- // this.cnt = cnt;
- // this.mindepth = mindepth;
- // this.maxdepth = maxdepth;
- // this.d50 = d50mm / 1000;
- // }
-
- /**
- * Query result row constructor
- */
- public D50Measurement(final Object[] tuple, final String[] aliases) {
- this.km = 0;
- // this.mindate = null;
- // this.maxdate = null;
- this.cnt = 0;
- // this.mindepth = Double.NaN;
- // this.maxdepth = Double.NaN;
- this.d50 = Double.NaN;
- for (int i = 0; i < tuple.length; ++i) {
- if (tuple[i] == null)
- continue;
- switch (aliases[i]) {
- case "km":
- this.km = ((Number) tuple[i]).doubleValue();
- break;
- // case "mindate":
- // this.mindate = (Date) tuple[i];
- // break;
- // case "maxdate":
- // this.maxdate = (Date) tuple[i];
- // break;
- case "cnt":
- this.cnt = ((Number) tuple[i]).intValue();
- break;
- // case "mindepth":
- // this.mindepth = ((Number) tuple[i]).doubleValue();
- // break;
- // case "maxdepth":
- // this.maxdepth = ((Number) tuple[i]).doubleValue();
- // break;
- case "d50":
- this.d50 = ((Number) tuple[i]).doubleValue() / 1000; // mm to m
- break;
- default:
- break;
- }
- }
- }
- }
+public class BedQualityD50KmValueFinder {
/***** FIELDS *****/
@@ -153,38 +47,38 @@
private static Logger log = Logger.getLogger(BedQualityD50KmValueFinder.class);
/**
- * Query that aggregates by km for a km range and a time period all sub layer bed measurements with their d50
+ * Query selecting all sub layer bed measurements with their d50 for a km range and a time period
*
* A km may have bed measurements for multiple dates, multiple distances from the river bank, and multiple depth layers.
- * The query filters by km range, time period and layer (sub layer: below bed to max. 50 cm depth).
- * Those measurements are then grouped by km, and the D50 aggregated as average value.
+ * The query filters by km range, time period and layer (sub layer: below bed to max. 50 cm depth).
+ *
+ * If PostgreSQL would support a median aggregate function like Oracle does, the aggregation could be placed into this query.
*/
private static final String SQL_BED_D50_SUBLAYER_MEASUREMENT =
- "SELECT t.km, MIN(t.datum) AS mindate, MAX(t.datum) AS maxdate, COUNT(*) AS cnt," //
- + " MIN(p.tiefevon) AS mindepth, MAX(p.tiefebis) AS maxdepth, AVG(a.d50) AS d50" //
- + " FROM sohltest t INNER JOIN station s ON t.stationid = s.stationid" //
- + " INNER JOIN gewaesser g ON s.gewaesserid = g.gewaesserid" //
- + " INNER JOIN sohlprobe p ON t.sohltestid = p.sohltestid" //
- + " INNER JOIN siebanalyse a ON p.sohlprobeid = a.sohlprobeid" //
- + " WHERE (g.name = :name) AND (s.km BETWEEN :fromkm - 0.0001 AND :tokm + 0.0001)" //
- + " AND (p.tiefevon > 0.0) AND (p.tiefebis <= 0.5)" //
- + " AND (t.datum BETWEEN :fromdate AND :todate)" //
- + " GROUP BY t.km" //
- + " ORDER BY t.km"; //
-
- private static final String[] SQL_BED_D50_SELECT_ALIAS = { "km", "mindate", "maxdate", "cnt", "mindepth", "maxdepth", "d50" };
+ "SELECT t.km, t.datum, p.tiefevon, p.tiefebis, a.d50"
+ + " FROM sohltest t INNER JOIN station s ON t.stationid = s.stationid"
+ + " INNER JOIN gewaesser g ON s.gewaesserid = g.gewaesserid"
+ + " INNER JOIN sohlprobe p ON t.sohltestid = p.sohltestid"
+ + " INNER JOIN siebanalyse a ON p.sohlprobeid = a.sohlprobeid"
+ + " WHERE (g.name = :name) AND (s.km BETWEEN :fromkm - 0.0001 AND :tokm + 0.0001)"
+ + " AND (p.tiefevon > 0.0) AND (p.tiefebis <= 0.5)"
+ + " AND (t.datum BETWEEN :fromdate AND :todate)"
+ + " ORDER BY t.km ASC, a.d50 ASC";
/**
- * Real linear interpolator for kms and d50 values
+ * Real linear interpolator for kms and d50 values (m)
*/
private final PolynomialSplineFunction interpolator;
- /***** METHODS *****/
+ /***** CONSTRUCTORS *****/
+
private BedQualityD50KmValueFinder(final double[] kms, final double[] values) {
this.interpolator = new LinearInterpolator().interpolate(kms, values);
}
+ /***** METHODS *****/
+
/**
* Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb)
* Abhängig von Peiljahr
@@ -204,8 +98,8 @@
log.debug(String.format("loadValues km %.3f - %.3f %tF - %tF", kmRange.getMinimumDouble(), kmRange.getMaximumDouble(), startTime, endTime));
final Session session = SedDBSessionHolder.HOLDER.get();
final SQLQuery sqlQuery = session.createSQLQuery(SQL_BED_D50_SUBLAYER_MEASUREMENT).addScalar("km", StandardBasicTypes.DOUBLE)
- .addScalar("mindate", StandardBasicTypes.DATE).addScalar("maxdate", StandardBasicTypes.DATE).addScalar("cnt", StandardBasicTypes.INTEGER)
- .addScalar("mindepth", StandardBasicTypes.DOUBLE).addScalar("maxdepth", StandardBasicTypes.DOUBLE).addScalar("d50", StandardBasicTypes.DOUBLE);
+ .addScalar("datum", StandardBasicTypes.DATE).addScalar("tiefevon", StandardBasicTypes.DOUBLE)
+ .addScalar("tiefebis", StandardBasicTypes.DOUBLE).addScalar("d50", StandardBasicTypes.DOUBLE);
final String seddbRiver = river.nameForSeddb();
sqlQuery.setString("name", seddbRiver);
sqlQuery.setDouble("fromkm", kmRange.getMinimumDouble());
@@ -214,19 +108,21 @@
sqlQuery.setDate("todate", endTime);
final List