changeset 333:67b3f54188aa

Discharge tables: Sorting of q values was done wrong. flys-artifacts/trunk@1731 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 19 Apr 2011 14:48:28 +0000
parents bf4e12f1d025
children b7c8df643dc4
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java
diffstat 2 files changed, 50 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Apr 19 13:10:27 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue Apr 19 14:48:28 2011 +0000
@@ -1,3 +1,8 @@
+2011-04-19	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java:
+	  Sorting of q values was done wrong.
+
 2011-04-19  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/AnnotationsFactory.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java	Tue Apr 19 13:10:27 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java	Tue Apr 19 14:48:28 2011 +0000
@@ -27,18 +27,6 @@
 
     public static final int MASTER = 0;
 
-    public static final double EPSILON = 1e-5;
-
-    public static Comparator<double []> Q_COMPARATOR = 
-        new Comparator<double []>() {
-            public int compare(double [] a, double [] b) {
-                double d = a[0] - b[0];
-                if (d < -EPSILON) return -1;
-                if (d > +EPSILON) return +1;
-                return 0;
-            }
-        };
-
     protected List<String> gaugeNames;
 
     protected String riverName;
@@ -137,19 +125,56 @@
             List<DischargeTableValue> dtvs =
                 table.getDischargeTableValues();
 
-            double [][] vs = new double[2][dtvs.size()];
+            final double [][] vs = new double[2][dtvs.size()];
+
+            boolean qSorted = true;
+
+            double lastQ = -Double.MAX_VALUE;
 
             int idx = 0;
             for (DischargeTableValue dtv: dtvs) {
-                vs[0][idx] = dtv.getQ().doubleValue() * scale;
+                double q = dtv.getQ().doubleValue();
+                vs[0][idx] = q * scale;
                 vs[1][idx] = dtv.getW().doubleValue() * scale;
                 ++idx;
+
+                if (qSorted && lastQ > q) {
+                    qSorted = false;
+                }
+                lastQ = q;
             }
 
-            // TODO: Do this db level.
-            Arrays.sort(vs, Q_COMPARATOR);
+            if (!qSorted) {
+                log.debug("need to sort by q values");
+                // TODO: Do this db level.
+                // XXX: This is so ugly :-(
+                Integer [] indices = new Integer[vs[0].length];
+                for (int i = 0; i < indices.length; ++i) {
+                    indices[i] = i;
+                }
 
-            values.put(gaugeName, vs);
+                Arrays.sort(indices, new Comparator<Integer>() {
+                    public int compare(Integer a, Integer b) {
+                        double va = vs[1][a];
+                        double vb = vs[1][b];
+                        double d = va - vb;
+                        if (d < 0.0) return -1;
+                        if (d > 0.0) return +1;
+                        return 0;
+                    }
+                });
+
+                double [][] vs2 = new double[2][vs[0].length];
+                for (int i = 0; i < indices.length; ++i) {
+                    vs2[0][i] = vs[0][indices[i]];
+                    vs2[1][i] = vs[0][indices[i]];
+                }
+                values.put(gaugeName, vs2);
+            }
+            else {
+                values.put(gaugeName, vs);
+            }
+
         }
 
         return values;
@@ -157,10 +182,10 @@
 
     public static boolean getWForQ(
         double [][] values, 
-        double q,  
-        double [] result
+        double      q,  
+        double []   result
     ) {
-        int index = Arrays.binarySearch(values, new double [] { q }, Q_COMPARATOR);
+        int index = Arrays.binarySearch(values[1], q);
         if (index >= 0) {
             result[0] = values[1][index];
             return true;

http://dive4elements.wald.intevation.org