[PATCH] (issue1787) Globally sorting WST-columns over avg(Q)+max(Q) might give unexpected results at some kms. We thus sort locally since Rev 93a31cfb18c0

Wald Commits scm-commit at wald.intevation.org
Thu Apr 23 19:25:16 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1429809640 -7200
# Node ID 682a26e4158b4b664a6ae918a50dae3f8011fcbd
# Parent  93a31cfb18c04c712adbc514f5f4921664fbd432
(issue1787) Globally sorting WST-columns over avg(Q)+max(Q) might give unexpected results at some kms. We thus sort locally since Rev 93a31cfb18c0.

diff -r 93a31cfb18c0 -r 682a26e4158b artifacts/src/main/java/org/dive4elements/river/artifacts/model/QRangeTree.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/QRangeTree.java	Thu Apr 23 19:13:33 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/QRangeTree.java	Thu Apr 23 19:20:40 2015 +0200
@@ -278,16 +278,6 @@
         return parent;
     }
 
-    public double averageQ() {
-        double sum = 0d;
-        int n = 0;
-        for (Node node = head(); node != null; node = node.next) {
-            sum += node.q;
-            ++n;
-        }
-        return sum/n;
-    }
-
     public double maxQ() {
         double max = -Double.MAX_VALUE;
         for (Node node = head(); node != null; node = node.next) {
diff -r 93a31cfb18c0 -r 682a26e4158b artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstValueTableFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstValueTableFactory.java	Thu Apr 23 19:13:33 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstValueTableFactory.java	Thu Apr 23 19:20:40 2015 +0200
@@ -106,8 +106,8 @@
         // Fetch data for one column only.
 
         WstValueTable.Column [] columns = loadColumns(session, wst_id);
-        int [] map = loadQRangesMap(session, columns, wst_id);
-        List<WstValueTable.Row> rows = loadRows(session, wst_id, columns.length, map);
+        loadQRanges(session, columns, wst_id);
+        List<WstValueTable.Row> rows = loadRows(session, wst_id, columns.length);
 
         WstValueTable valueTable = new WstValueTable(columns, rows);
 
@@ -212,10 +212,9 @@
 
         WstValueTable.Column [] columns = loadColumns(session, wst);
 
-        int map [] = loadQRangesMap(session, columns, wst);
+        loadQRanges(session, columns, wst);
 
-        List<WstValueTable.Row> rows =
-            loadRows(session, wst.getId(), columns.length, map);
+        List<WstValueTable.Row> rows = loadRows(session, wst, columns.length);
 
         return new WstValueTable(columns, rows);
     }
@@ -283,15 +282,6 @@
         int     wst_id,
         int     numColumns
     ) {
-        return loadRows(session, wst_id, numColumns, null);
-    }
-
-    protected static List<WstValueTable.Row> loadRows(
-        Session session,
-        int     wst_id,
-        int     numColumns,
-        int []  map
-    ) {
         SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS)
             .addScalar("km",         StandardBasicTypes.DOUBLE)
             .addScalar("w",          StandardBasicTypes.DOUBLE)
@@ -316,8 +306,7 @@
                 rows.add(row);
             }
             Double w = (Double)result[1];
-            int index = map != null ? map[column] : column;
-            ws[index] = w != null ? w : Double.NaN;
+            ws[column] = w != null ? w : Double.NaN;
             lastColumn = column;
         }
 
@@ -406,7 +395,7 @@
         columns[0].setQRangeTree(qRangeTree);
     }
 
-    protected static int [] loadQRangesMap(
+    protected static void loadQRanges(
         Session                 session,
         WstValueTable.Column [] columns,
         int                     wst_id
@@ -445,8 +434,6 @@
             columns[lastColumn].setQRangeTree(qRangeTree);
         }
 
-        return sortColumnsByAverageQ(columns);
-
         /* This is debug code to visualize the q ranges trees
 
         java.io.PrintWriter out = null;
@@ -477,51 +464,12 @@
         */
     }
 
-    private static final class QIndex implements Comparable<QIndex> {
-        double q;
-        int    index;
-
-        QIndex(double q, int index) {
-            this.q     = q;
-            this.index = index;
-        }
-
-        @Override
-        public int compareTo(QIndex other) {
-            double diff = q - other.q;
-            if (diff < 0d) return -1;
-            if (diff > 0d) return +1;
-            return 0;
-        }
-    } // class QIndex
-
-    /** Ensure that the q colums are sorted in ascending order. */
-    protected static int [] sortColumnsByAverageQ(WstValueTable.Column [] columns) {
-        QIndex [] order = new QIndex[columns.length];
-        for (int i = 0; i < order.length; ++i) {
-            QRangeTree tree = columns[i].getQRangeTree();
-            double avg = tree.averageQ();
-            double max = tree.maxQ();
-            double q   = (avg+max)*0.5d;
-            order[i] = new QIndex(q, i);
-        }
-        Arrays.sort(order);
-        WstValueTable.Column [] copy = new WstValueTable.Column[order.length];
-        int [] map = new int[order.length];
-        for (int i = 0; i < copy.length; ++i) {
-            copy[i] = columns[order[i].index];
-            map[order[i].index] = i;
-        }
-        System.arraycopy(copy, 0, columns, 0, order.length);
-        return map;
-    }
-
-    protected static int [] loadQRangesMap(
+    protected static void loadQRanges(
         Session                 session,
         WstValueTable.Column [] columns,
         Wst                     wst
     ) {
-        return loadQRangesMap(session, columns, wst.getId());
+        loadQRanges(session, columns, wst.getId());
     }
 
 }


More information about the Dive4Elements-commits mailing list