changeset 4821:bcf25d8c183e

Moved NaN removal code from W to DoubleUtil. Create QKms when calculating the 'Umhuellende'.
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 21 Jan 2013 14:03:22 +0100
parents 26c849cf54e7
children dd146d5ee99f
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QKmsImpl.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/W.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQCKms.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKms.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WW.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WWQQ.java flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java
diffstat 11 files changed, 106 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java	Mon Jan 21 14:03:22 2013 +0100
@@ -1,18 +1,21 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.artifacts.access.Calculation4Access;
+
 import de.intevation.flys.artifacts.math.BackJumpCorrector;
 import de.intevation.flys.artifacts.math.Function;
 import de.intevation.flys.artifacts.math.Identity;
 import de.intevation.flys.artifacts.math.Linear;
 
 import de.intevation.flys.artifacts.model.RiverFactory;
+
 import de.intevation.flys.artifacts.model.WstValueTable.QPosition;
 
 import de.intevation.flys.model.River;
 
 import de.intevation.flys.utils.DoubleUtil;
 
-import de.intevation.flys.artifacts.access.Calculation4Access;
+import gnu.trove.TDoubleArrayList;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -273,12 +276,14 @@
         }
 
         // Generate the "Umhuellende".
-        results = generateInfolding(table, results, from, to, step);
+        QKms [] qkms = generateInfolding(table, results, from, to, step);
+
+        // TODO: Use qkms in a new result type.
 
         return new CalculationResult(results, this);
     }
 
-    protected WQKms [] generateInfolding(
+    protected QKms [] generateInfolding(
         WstValueTable wst,
         WQKms []      results,
         double        from,
@@ -290,43 +295,43 @@
         InfoldingColumns ic = new InfoldingColumns(columns);
         ic.markInfoldingColumns(results);
 
-        List<WQKms> infoldings = new ArrayList<WQKms>();
+        List<QKms> infoldings = new ArrayList<QKms>();
 
         boolean [] infoldingColumns = ic.getInfoldingColumns();
+
+        double [] kms = null;
+
         for (int i = 0; i < infoldingColumns.length; ++i) {
             if (!infoldingColumns[i]) {
                 continue;
             }
-            double q = columns[i].getQRangeTree().findQ(from);
-            if (Double.isNaN(q)) {
-                addProblem(from, "cannot.find.q");
-                continue;
+
+            kms = kms == null
+                ? DoubleUtil.explode(from, to, step)
+                : (double [])kms.clone();
+
+            QRangeTree.QuickQFinder qf =
+                columns[i].getQRangeTree().new QuickQFinder();
+
+            int numProblemsBefore = numProblems();
+            double [] qs = qf.findQs(kms, this);
+
+            // TODO: i18n
+            String name = "Umh\u00fcllende " + columns[i].getName();
+
+            QKmsImpl qkms = new QKmsImpl(
+                new TDoubleArrayList(kms),
+                new TDoubleArrayList(qs),
+                name);
+
+            if (numProblems() > numProblemsBefore) {
+                qkms.removeNaNs();
             }
-            double [] kms = DoubleUtil.explode(from, to, step);
-            double [] oqs = new double[kms.length];
-            double [] ows = new double[kms.length];
-            boolean success =
-                wst.interpolate(q, from, kms, ows, oqs, this) != null;
 
-            if (success) {
-                // TODO: generate a better name. I18N.
-                String name = "Umh\u00fcllende " + columns[i].getName();
-                WQKms wqkms = new WQKms(kms, oqs, ows, name);
-                infoldings.add(wqkms);
-            }
+            infoldings.add(qkms);
         }
 
-        int N = infoldings.size();
-        if (N > 0) {
-            WQKms [] newResults = new WQKms[results.length + N];
-            System.arraycopy(results, 0, newResults, 0, results.length);
-            for (int i = 0; i < N; ++i) {
-                newResults[i+results.length] = infoldings.get(i);
-            }
-            results = newResults;
-        }
-
-        return results;
+        return infoldings.toArray(new QKms[infoldings.size()]);
     }
 
     protected String createName(int index) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jan 21 14:03:22 2013 +0100
@@ -2,6 +2,8 @@
 
 import de.intevation.flys.artifacts.math.Linear;
 
+import de.intevation.flys.utils.DoubleUtil;
+
 import gnu.trove.TDoubleArrayList;
 
 import java.io.Serializable;
@@ -124,7 +126,7 @@
     }
 
     public void removeNaNs() {
-        W.removeNaNs(columns);
+        DoubleUtil.removeNaNs(columns);
     }
 
     public int [] columnIndices(String [] columns) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QKmsImpl.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QKmsImpl.java	Mon Jan 21 14:03:22 2013 +0100
@@ -2,6 +2,8 @@
 
 import gnu.trove.TDoubleArrayList;
 
+import de.intevation.flys.utils.DoubleUtil;
+
 public class QKmsImpl
 extends      NamedObjectImpl
 implements   QKms
@@ -73,4 +75,8 @@
     public TDoubleArrayList allQs() {
         return qs;
     }
+
+    public void removeNaNs() {
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { kms, qs });
+    }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java	Mon Jan 21 14:03:22 2013 +0100
@@ -145,6 +145,23 @@
             last = QRangeTree.this.findNode(pos);
             return last != null ? last.q : Double.NaN;
         }
+
+        public double [] findQs(double [] kms, Calculation report) {
+            return findQs(kms, new double[kms.length], report);
+        }
+
+        public double [] findQs(
+            double []    kms, 
+            double []   qs, 
+            Calculation report
+        ) {
+            for (int i = 0; i < kms.length; ++i) {
+                if (Double.isNaN(qs[i] = findQ(kms[i]))) {
+                    report.addProblem(kms[i], "cannot.find.q");
+                }
+            }
+            return qs;
+        }
     } // class QuickQFinder
 
     protected Node root;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/W.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/W.java	Mon Jan 21 14:03:22 2013 +0100
@@ -1,6 +1,7 @@
 package de.intevation.flys.artifacts.model;
 
 import de.intevation.flys.utils.DataUtil;
+import de.intevation.flys.utils.DoubleUtil;
 
 import gnu.trove.TDoubleArrayList;
 
@@ -60,34 +61,8 @@
         return ws.min();
     }
 
-    public static void removeNaNs(TDoubleArrayList [] arrays) {
-
-        int dest = 0;
-
-        int A = arrays.length;
-        int N = arrays[0].size();
-
-        OUTER: for (int i = 0; i < N; ++i) {
-            for (int j = 0; j < A; ++j) {
-                TDoubleArrayList a = arrays[j];
-                double v = a.getQuick(i);
-                if (Double.isNaN(v)) {
-                    continue OUTER;
-                }
-                a.setQuick(dest, v);
-            }
-            ++dest;
-        }
-
-        if (dest < N) {
-            for (int i = 0; i < A; ++i) {
-                arrays[i].remove(dest, N-dest);
-            }
-        }
-    }
-
     public void removeNaNs() {
-        removeNaNs(new TDoubleArrayList [] { ws });
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws });
     }
 
     public boolean guessWaterIncreasing() {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java	Mon Jan 21 14:03:22 2013 +0100
@@ -1,10 +1,12 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.utils.DoubleUtil;
+
+import gnu.trove.TDoubleArrayList;
+
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import gnu.trove.TDoubleArrayList;
-
 import org.apache.log4j.Logger;
 
 public class WQ
@@ -96,7 +98,7 @@
 
     @Override
     public void removeNaNs() {
-        removeNaNs(new TDoubleArrayList [] { ws, qs });
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws, qs });
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQCKms.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQCKms.java	Mon Jan 21 14:03:22 2013 +0100
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.utils.DoubleUtil;
+
 import gnu.trove.TDoubleArrayList;
 
 /**
@@ -32,7 +34,7 @@
 
     @Override
     public void removeNaNs() {
-        removeNaNs(new TDoubleArrayList [] { ws, qs, cws, kms });
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws, qs, cws, kms });
     }
 
     /**
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKms.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKms.java	Mon Jan 21 14:03:22 2013 +0100
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.utils.DoubleUtil;
+
 import gnu.trove.TDoubleArrayList;
 
 import org.apache.log4j.Logger;
@@ -54,7 +56,7 @@
 
     @Override
     public void removeNaNs() {
-        removeNaNs(new TDoubleArrayList [] { ws, qs, kms });
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws, qs, kms });
     }
 
     /**
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WW.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WW.java	Mon Jan 21 14:03:22 2013 +0100
@@ -3,6 +3,8 @@
 import de.intevation.flys.artifacts.math.Function;
 import de.intevation.flys.artifacts.math.Identity;
 
+import de.intevation.flys.utils.DoubleUtil;
+
 import gnu.trove.TDoubleArrayList;
 
 public class WW
@@ -175,7 +177,7 @@
 
     @Override
     public void removeNaNs() {
-        removeNaNs(new TDoubleArrayList [] { ws, ws2 });
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws, ws2 });
     }
 
     public double minWs2() {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WWQQ.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WWQQ.java	Mon Jan 21 14:03:22 2013 +0100
@@ -2,6 +2,8 @@
 
 import de.intevation.flys.artifacts.math.Identity;
 
+import de.intevation.flys.utils.DoubleUtil;
+
 import gnu.trove.TDoubleArrayList;
 
 public class WWQQ
@@ -91,7 +93,7 @@
 
     @Override
     public void removeNaNs() {
-        removeNaNs(new TDoubleArrayList [] { ws, ws2, qs1, qs2 });
+        DoubleUtil.removeNaNs(new TDoubleArrayList [] { ws, ws2, qs1, qs2 });
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java	Mon Jan 21 12:49:44 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java	Mon Jan 21 14:03:22 2013 +0100
@@ -209,5 +209,31 @@
         }
         return max;
     }
+
+    public static void removeNaNs(TDoubleArrayList [] arrays) {
+
+        int dest = 0;
+
+        int A = arrays.length;
+        int N = arrays[0].size();
+
+        OUTER: for (int i = 0; i < N; ++i) {
+            for (int j = 0; j < A; ++j) {
+                TDoubleArrayList a = arrays[j];
+                double v = a.getQuick(i);
+                if (Double.isNaN(v)) {
+                    continue OUTER;
+                }
+                a.setQuick(dest, v);
+            }
+            ++dest;
+        }
+
+        if (dest < N) {
+            for (int i = 0; i < A; ++i) {
+                arrays[i].remove(dest, N-dest);
+            }
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org