# 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 rows = sqlQuery.list(); - final double[] kms = new double[rows.size()]; - final double[] values = new double[rows.size()]; - D50Measurement measurement; - int i = -1; - for (final Object[] row : rows) { - measurement = new D50Measurement(row, SQL_BED_D50_SELECT_ALIAS); - i++; - kms[i] = measurement.getKm(); - values[i] = measurement.getD50(); - log.debug(String.format("loadValues km %.3f d50(mm) %.1f count %d", kms[i], values[i], measurement.getCnt())); + final TDoubleArrayList kms = new TDoubleArrayList(); + final TDoubleArrayList values = new TDoubleArrayList(); + final TDoubleArrayList kmd50s = new TDoubleArrayList(); + for (int i = 0; i <= rows.size() - 1; i++) { + kmd50s.add((double) rows.get(i)[4]); + if (((i == rows.size() - 1) || !Utils.epsilonEquals((double) rows.get(i)[0], (double) rows.get(i+1)[0], 0.0001))) { + int k = kmd50s.size() / 2; + values.add(((k + k < kmd50s.size()) ? kmd50s.get(k) : (kmd50s.get(k-1) + kmd50s.get(k)) / 2) / 1000); + kms.add((double) rows.get(i)[0]); + log.debug(String.format("loadValues km %.3f d50(mm) %.1f count %d", kms.get(kms.size()-1), values.get(values.size()-1), kmd50s.size())); + kmd50s.clear(); + } } try { - return new BedQualityD50KmValueFinder(kms, values); + return new BedQualityD50KmValueFinder(kms.toNativeArray(), values.toNativeArray()); } catch (final Exception e) { e.printStackTrace(); @@ -237,7 +133,7 @@ /** * Returns the d50 value interpolated according to a km * - * @return d50 (mm) of the km, or NaN + * @return d50 (m) of the km, or NaN */ public double findD50(final double km) throws ArgumentOutsideDomainException { return this.interpolator.value(km); diff -r 5d5d0051723f -r 29442c03c6e3 artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java Wed Feb 28 18:55:39 2018 +0100 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java Thu Mar 01 09:01:58 2018 +0100 @@ -58,7 +58,7 @@ VALID_BED_MEASUREMENT_YEARS); if (bedMeasurementsFinder == null) { - final String message = Resources.getMsg(context.getMeta(), "sinfo_calc_flow_depth.warning.missingSoilKind", null, label); + final String message = Resources.getMsg(context.getMeta(), "sinfo_calc_flow_depth.warning.missingD50", null, label); problems.addProblem(message); return null; } @@ -159,29 +159,13 @@ if (!this.flowVelocitiesFinder.findKmQValues(km, discharge)) { // TODO: ggf. station in Fehlermeldung? - final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingQ", null, this.problemLabel); + final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingVelocity", null, this.problemLabel); this.problems.addProblem(km, message); // FIXME: cumulate problems to one message? + return new Tkh(km, wst, meanBedHeight, discharge, kind, Double.NaN, Double.NaN, Double.NaN); } final double tkh = calculateTkh(wst - meanBedHeight, this.flowVelocitiesFinder.getFindVmainFound(), d50, this.flowVelocitiesFinder.getFindTauFound()); - // FIXME: noch mal prüfen, im alten code wurde hier immer auf 0 gesetzt - if (Double.isNaN(tkh) || (tkh < 0)) { - // TODO: ggf. station in Fehlermeldung? - - // FIXME: Fehlermeldung nicht korrekt, passiert mit Wasserspiegel 'MHQ' und 'QP-1993 - final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingQ", null, this.problemLabel); - this.problems.addProblem(km, message); - - return new Tkh(km, wst, meanBedHeight, discharge, kind, Double.NaN, Double.NaN, Double.NaN); - } - - /* - * log.debug(String.format("calculateTkh km %.3f q %.0f w %.2f mbh %.2f vm %.1f tau %.1f d50(mm) %.1f tkh(cm) %.1f", - * km, discharge, wst, meanBedHeight, flowVelocitiesFinder.getFindVmainFound(), flowVelocitiesFinder.getFindTauFound(), - * d50*1000, tkh)); - */ - double tkhUp; double tkhDown; switch (kind) { @@ -211,7 +195,7 @@ * grain diameter D50 in m (!) * @param tau * shear stress in N/m^2 - * @return transport body height in cm (!) + * @return transport body height in cm (!), never negative */ private double calculateTkh(final double h, final double vm, final double d50, final double tau) { final double PHYS_G = 9.81; @@ -226,6 +210,11 @@ final double partReynolds = Math.sqrt((PHYS_SPECGRAV_S - 1) * PHYS_G * d50) / PHYS_VISCOSITY_NUE * d50; final double critShields = 0.22 * Math.pow(partReynolds, -0.6) + 0.06 * Math.pow(10, 7.7 * Math.pow(partReynolds, -0.6)); final double critTau = critShields * (PHYS_GRAIN_DENSITY_RHOS - PHYS_WATER_DENSITY_RHO) * PHYS_G * d50; - return 100 * h * (1 - Math.pow(froude, 2)) / (2 * PHYS_VELOCCOEFF_N * PHYS_FORMCOEFF_ALPHA) * (1 - critTau / tau); + final double tkh = 100 * h * (1 - Math.pow(froude, 2)) / (2 * PHYS_VELOCCOEFF_N * PHYS_FORMCOEFF_ALPHA) * (1 - critTau / tau); + // Some regular input values may give a negative calculation result; that is unwanted + if (tkh < 0.0) + return 0.0; + else + return tkh; } } \ No newline at end of file