changeset 9096:9bd4505a20dc

Fixed removing duplicates
author mschaefer
date Sat, 26 May 2018 15:41:47 +0200
parents 1b24fdbf7fe2
children 82917e414d76
files artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowVelocityKmModelValues.java artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/FlowVelocityModelKmValueFinder.java
diffstat 2 files changed, 32 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowVelocityKmModelValues.java	Fri May 25 16:03:15 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowVelocityKmModelValues.java	Sat May 26 15:41:47 2018 +0200
@@ -14,6 +14,7 @@
 import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
 
 import gnu.trove.TDoubleArrayList;
+import gnu.trove.TIntArrayList;
 
 /**
  * Sorted arrays of a station's q, v, and tau model values, running in parallel
@@ -36,6 +37,11 @@
     private final TDoubleArrayList qs;
 
     /**
+     * Same q values count used to calculate mean
+     */
+    private final TIntArrayList counts;
+
+    /**
      * The station's main section velocity for the q values
      */
     private final TDoubleArrayList vmains;
@@ -83,6 +89,7 @@
     public FlowVelocityKmModelValues(final double km) {
         this.km = km;
         this.qs = new TDoubleArrayList();
+        this.counts = new TIntArrayList();
         this.vmains = new TDoubleArrayList();
         this.taus = new TDoubleArrayList();
         this.vInterpolator = null;
@@ -94,7 +101,7 @@
      */
     public FlowVelocityKmModelValues(final double km, final FlowVelocityKmModelValues src) {
         this(km);
-        src.copyTo(this.qs, this.vmains, this.taus);
+        src.copyTo(this.qs, this.counts, this.vmains, this.taus);
     }
 
     /***** METHODS *****/
@@ -116,9 +123,10 @@
     /**
      * Adds all q-v-tau to another set of arrays
      */
-    void copyTo(final TDoubleArrayList dstqs, final TDoubleArrayList dstvmains, final TDoubleArrayList dsttaus) {
+    void copyTo(final TDoubleArrayList dstqs, final TIntArrayList dstcounts, final TDoubleArrayList dstvmains, final TDoubleArrayList dsttaus) {
         for (int i = 0; i <= this.qs.size(); i++) {
             dstqs.add(this.qs.getQuick(i));
+            dstcounts.add(this.counts.getQuick(i));
             dstvmains.add(this.vmains.getQuick(i));
             dsttaus.add(this.taus.getQuick(i));
         }
@@ -155,12 +163,22 @@
     }
 
     /**
-     * Adds a q-v-tau value triple.
+     * Adds a q-v-tau value triple, averaging the last values if q duplicates.
      */
     public void addValues(final double q, final double vmain, final double tau) {
-        this.qs.add(q);
-        this.vmains.add(vmain);
-        this.taus.add(tau);
+        final int j = this.qs.size() - 1;
+        if ((j >= 0) && (q < this.qs.getQuick(j) + 0.001)) {
+            this.qs.setQuick(j, (this.qs.getQuick(j) * this.counts.getQuick(j) + q) / (this.counts.getQuick(j) + 1));
+            this.vmains.setQuick(j, (this.vmains.getQuick(j) * this.counts.getQuick(j) + vmain) / (this.counts.getQuick(j) + 1));
+            this.taus.setQuick(j, (this.taus.getQuick(j) * this.counts.getQuick(j) + tau) / (this.counts.getQuick(j) + 1));
+            this.counts.setQuick(j, this.counts.getQuick(j) + 1);
+        }
+        else {
+            this.qs.add(q);
+            this.counts.add(1);
+            this.vmains.add(vmain);
+            this.taus.add(tau);
+        }
     }
 
     /**
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/FlowVelocityModelKmValueFinder.java	Fri May 25 16:03:15 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/FlowVelocityModelKmValueFinder.java	Sat May 26 15:41:47 2018 +0200
@@ -52,7 +52,7 @@
      * (Might be several 10000 rows if many stations and large q range)
      */
     private static final String SQL_SELECT_ALL = //
-            "SELECT DISTINCT fvmv.station AS station, fvmv.q AS q, fvmv.main_channel AS vmain, fvmv.shear_stress AS tau"
+            "SELECT fvmv.station AS station, fvmv.q AS q, fvmv.main_channel AS vmain, fvmv.shear_stress AS tau"
             + "  FROM (discharge_zone dz INNER JOIN flow_velocity_model fvm ON dz.id = fvm.discharge_zone_id)"
             + "    INNER JOIN flow_velocity_model_values fvmv ON fvm.id = fvmv.flow_velocity_model_id"
             + "  WHERE (dz.river_id = :river_id) AND (fvmv.station BETWEEN :kmfrom - 0.0001 AND :kmto + 0.0001)"
@@ -63,10 +63,10 @@
      * Query for a river's max km below a limit with all its q, main-v and tau values.
      */
     private static final String SQL_SELECT_KMLOWER = //
-            "SELECT DISTINCT fvmv.station AS station, fvmv.q AS q, fvmv.main_channel AS vmain, fvmv.shear_stress AS tau"
+            "SELECT fvmv.station AS station, fvmv.q AS q, fvmv.main_channel AS vmain, fvmv.shear_stress AS tau"
             + "  FROM flow_velocity_model_values fvmv"
             + "    INNER JOIN (SELECT MAX(fvmvi.station) AS kmmax"
-                    + "      FROM discharge_zone dz INNER JOIN flow_velocity_model fvm ON dz.id = fvm.discharge_zone_id"
+            + "      FROM discharge_zone dz INNER JOIN flow_velocity_model fvm ON dz.id = fvm.discharge_zone_id"
             + "        INNER JOIN flow_velocity_model_values fvmvi ON fvm.id = fvmvi.flow_velocity_model_id"
             + "        WHERE (dz.river_id = :river_id) AND (fvmvi.station < :kmfrom - 0.0001)) finf ON fvmv.station = finf.kmmax"
             + "  ORDER BY fvmv.q ASC";
@@ -75,10 +75,10 @@
      * Query for a river's min km above a limit with all its q, main-v and tau values.
      */
     private static final String SQL_SELECT_KMUPPER = //
-            "SELECT DISTINCT fvmv.station AS station, fvmv.q AS q, fvmv.main_channel AS vmain, fvmv.shear_stress AS tau"
+            "SELECT fvmv.station AS station, fvmv.q AS q, fvmv.main_channel AS vmain, fvmv.shear_stress AS tau"
             + "  FROM flow_velocity_model_values fvmv"
             + "    INNER JOIN (SELECT MIN(fvmvi.station) AS kmmin"
-                    + "      FROM discharge_zone dz INNER JOIN flow_velocity_model fvm ON dz.id = fvm.discharge_zone_id"
+            + "      FROM discharge_zone dz INNER JOIN flow_velocity_model fvm ON dz.id = fvm.discharge_zone_id"
             + "        INNER JOIN flow_velocity_model_values fvmvi ON fvm.id = fvmvi.flow_velocity_model_id"
             + "        WHERE (dz.river_id = :river_id) AND (fvmvi.station > :kmto + 0.0001)) fsup ON fvmv.station = fsup.kmmin"
             + "  ORDER BY fvmv.q ASC";
@@ -277,6 +277,7 @@
      * @return Number of rows
      */
     private int addKms(final List<Object[]> rows, final boolean isDemoValuesCorrection) {
+        log.trace("addKms i km j q v tau");
         for (final Object[] row : rows) {
             if ((this.kms.size() == 0) || !Utils.epsilonEquals(this.kms.get(this.kms.size() - 1), (double) row[0], 0.0001)) {
                 this.kms.add((double) row[0]);
@@ -288,8 +289,8 @@
             else
                 this.values.get(this.values.size() - 1).addValues((double) row[1], (double) row[2], (double) row[3]);
             final int j = this.values.size() - 1;
-            log.trace(String.format("addKms %d km %.3f q %.0f v %.2f tau %.2f", this.values.size() - 1, this.values.get(j).getKm(), (double) row[1],
-                    (double) row[2], (double) row[3]));
+            log.trace(String.format("addKms %d %.3f %d %.0f %.2f %.2f", this.values.size() - 1, this.values.get(j).getKm(),
+                    this.values.get(j).size() - 1, (double) row[1], (double) row[2], (double) row[3]));
         }
         return rows.size();
     }

http://dive4elements.wald.intevation.org