diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/Parameters.java @ 7299:cde863b2dae3

(issue1529) Only interpolate within a step limit in WQ diagram Created together with Sascha Teichmann
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 11 Oct 2013 17:11:15 +0200
parents af13ceeba52a
children 5e38e2924c07
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Parameters.java	Fri Oct 11 15:35:25 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Parameters.java	Fri Oct 11 17:11:15 2013 +0200
@@ -314,6 +314,67 @@
         return values;
     }
 
+    public double [] interpolateWithLimit(
+        String    keyName,
+        double    key,
+        String [] columnNames,
+        double    limit
+    ) {
+        int keyIndex = columnIndex(keyName);
+        return keyIndex < 0
+            ? null
+            : interpolateWithLimit(keyIndex, key, columnNames, limit);
+    }
+
+    /* Only interpolate if the difference between the two key's
+     * is less then limit */
+    public double [] interpolateWithLimit(
+        int       keyIndex,
+        double    key,
+        String [] columnNames,
+        double    limit
+    ) {
+        int row = binarySearch(keyIndex, key, EPSILON);
+
+        if (row >= 0) { // direct match
+            double [] values = new double[columnNames.length];
+            for (int i = 0; i < values.length; ++i) {
+                int ci = columnIndex(columnNames[i]);
+                values[i] = ci < 0
+                    ? Double.NaN
+                    : columns[ci].getQuick(row);
+            }
+            return values;
+        }
+
+        row = -row - 1;
+        if (row < 1 || row >= size()) {
+            log.debug("interpolate: row is out of bounds");
+            return null;
+        }
+
+        double v1 = columns[keyIndex].getQuick(row-1);
+        double v2 = columns[keyIndex].getQuick(row);
+        if (Math.abs(v1-v2) > limit) {
+            return null;
+        }
+        double factor = Linear.factor(key, v1, v2);
+
+        double [] values = new double[columnNames.length];
+
+        for (int i = 0; i < values.length; ++i) {
+            int ci = columnIndex(columnNames[i]);
+            values[i] = ci < 0
+                ? Double.NaN
+                : Linear.weight(
+                    factor,
+                    columns[ci].getQuick(row-1),
+                    columns[ci].getQuick(row));
+        }
+
+        return values;
+    }
+
     public boolean isSorted(String columnName) {
         return isSorted(columnIndex(columnName));
     }

http://dive4elements.wald.intevation.org