[Dive4elements-commits] [PATCH] Partial fix for flys/1303

Wald Commits scm-commit at wald.intevation.org
Wed Jun 26 13:33:20 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1372246395 -7200
# Node ID 898afcce1d0a8de26b1aacf1564ffb0713b64c6b
# Parent  2874e16c2cc6cd25e105f23e29050d8d394351ab
Partial fix for flys/1303

diff -r 2874e16c2cc6 -r 898afcce1d0a artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation.java	Wed Jun 26 13:26:43 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation.java	Wed Jun 26 13:33:15 2013 +0200
@@ -78,6 +78,21 @@
         }
 
         @Override
+        public String toString() {
+            StringBuilder sb = new StringBuilder("problem: ");
+            if (km != null) {
+                sb.append("km: ").append(km).append(' ');
+            }
+            sb.append(msg);
+            if (args != null) {
+                for (Object arg: args) {
+                    sb.append(' ').append(arg);
+                }
+            }
+            return sb.toString();
+        }
+
+        @Override
         public boolean equals(Object other) {
             if (!(other instanceof Problem)) {
                 return false;
@@ -159,6 +174,18 @@
         return problems;
     }
 
+    public String problemsToString() {
+        StringBuilder sb = new StringBuilder("[");
+        for (int i = 0, N = problems.size(); i < N; ++i) {
+            if (i > 0) {
+                sb.append(", ");
+            }
+            sb.append(problems.get(i));
+        }
+        sb.append(']');
+        return sb.toString();
+    }
+
     public void toXML(Document document, CallMeta meta) {
 
         Element root = document.createElement("problems");
diff -r 2874e16c2cc6 -r 898afcce1d0a artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation2.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation2.java	Wed Jun 26 13:26:43 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation2.java	Wed Jun 26 13:33:15 2013 +0200
@@ -8,6 +8,9 @@
 
 package org.dive4elements.river.artifacts.model;
 
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.Arrays;
 
 import org.apache.log4j.Logger;
@@ -28,12 +31,57 @@
         this.km = km;
     }
 
+    private void dump(double [][] wqs) {
+        double [] ws = wqs[0];
+        double [] qs = wqs[1];
+
+        String filename = "/tmp/computed-discharge-curve-" + km + "-" +
+            System.currentTimeMillis() + ".txt";
+
+        PrintWriter pw = null;
+        try {
+            pw =
+                new PrintWriter(
+                new FileWriter(filename));
+
+            for (int i = 0; i < ws.length; ++i) {
+                pw.println(ws[i] + " " + qs[i]);
+            }
+
+            pw.flush();
+        }
+        catch (IOException ioe) {
+            logger.error(ioe);
+        }
+        finally {
+            if (pw != null) {
+                pw.close();
+            }
+        }
+    }
+
     public CalculationResult calculate(WstValueTable wst) {
 
-        logger.debug("Calculation2.calculate");
+        boolean debug = logger.isDebugEnabled();
+
+        if (debug) {
+            logger.debug("Calculation2.calculate: km " + km);
+        }
 
         double [][] wqs = wst.interpolateWQ(km, this);
 
+        if (debug) {
+            if (hasProblems()) {
+                logger.debug("problems: " + problemsToString());
+            }
+            logger.debug("wqs: " + wqs);
+            if (wqs != null && wqs[0] != null) {
+                logger.debug("wqs length: " + wqs[0].length);
+                // TODO: Uncomment to see the data externally.
+                //dump(wqs);
+            }
+        }
+
         if (wqs == null || wqs[0].length == 0) {
             addProblem("cannot.compute.discharge.curve");
             return new CalculationResult(new WQKms[0], this);
diff -r 2874e16c2cc6 -r 898afcce1d0a artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstValueTable.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstValueTable.java	Wed Jun 26 13:26:43 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstValueTable.java	Wed Jun 26 13:33:15 2013 +0200
@@ -12,6 +12,7 @@
 
 import org.dive4elements.river.artifacts.math.Linear;
 import org.dive4elements.river.artifacts.math.Function;
+import org.dive4elements.river.utils.DoubleUtil;
 
 import java.util.Arrays;
 import java.util.ArrayList;
@@ -327,6 +328,8 @@
                 splineQs[i] = wqs;
             }
 
+            DoubleUtil.sortByFirst(splineQs, splineWs);
+
             SplineInterpolator interpolator = new SplineInterpolator();
 
             try {
diff -r 2874e16c2cc6 -r 898afcce1d0a artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java	Wed Jun 26 13:26:43 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java	Wed Jun 26 13:33:15 2013 +0200
@@ -13,6 +13,7 @@
 import gnu.trove.TDoubleArrayList;
 
 import java.util.Arrays;
+import java.util.Comparator;
 
 import org.apache.log4j.Logger;
 
@@ -24,6 +25,17 @@
 
     public static final double DEFAULT_STEP_PRECISION = 1e6;
 
+    public static final Comparator<double []> DOUBLE_PAIR_CMP =
+        new Comparator<double []>() {
+            @Override
+            public int compare(double [] a, double [] b) {
+                double diff = a[0] - b[0];
+                if (diff < 0d) return -1;
+                if (diff > 0d) return +1;
+                return 0;
+            }
+        };
+
     /** EPSILON for comparison of double precision values. */
     public static final double EPSILON = 1e-4;
 
@@ -225,6 +237,26 @@
         return max;
     }
 
+
+
+    /** Sort a and b with a as key. b is ordered accordingly */
+    public static final void sortByFirst(double [] a, double [] b) {
+        // XXX: Not efficient but bulletproof.
+        double [][] pairs = new double[a.length][2];
+        for (int i = 0; i < a.length; ++i) {
+            double [] p = pairs[i];
+            p[0] = a[i];
+            p[1] = b[i];
+
+        }
+        Arrays.sort(pairs, DOUBLE_PAIR_CMP);
+        for (int i = 0; i < a.length; ++i) {
+            double [] p = pairs[i];
+            a[i] = p[0];
+            b[i] = p[1];
+        }
+    }
+
     public static void removeNaNs(TDoubleArrayList [] arrays) {
 
         int dest = 0;


More information about the Dive4elements-commits mailing list