changeset 3059:4f7171ac6153

Parameters: Linear interpolation of parameters. flys-artifacts/trunk@4638 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 Jun 2012 14:51:21 +0000
parents df4d6b286af8
children 94d78e0dc5e9
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java
diffstat 2 files changed, 59 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Jun 11 12:19:46 2012 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jun 11 14:51:21 2012 +0000
@@ -1,3 +1,12 @@
+2012-06-11	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/Parameters.java:
+	  Added interpolate methods that return an array of linear interpolated
+	  values for a given key. Say you have a column named "km" you can call
+	  'values = parameters.interpolate("km", 12)' to fetch a set 
+	  of proportional interpolated parameters even if there are only
+	  "km": 10 and "km": 15 in the data structure. Returns null if out of bounds.
+
 2012-06-11  Raimund Renkert <raimund.renkert@intevation.de>
 
 	* doc/conf/artifacts/minfo.xml:
@@ -18,7 +27,7 @@
 	  src/main/resources/messages_de.properties:
 	  Added i18n strings for states.
 
-2012-06-11	Sascha L. Teichmann	<sascha.teihmann@intevation.de>
+2012-06-11	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/fixings/FixWQCurveGenerator.java:
 	  Fixed broken logger class.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jun 11 12:19:46 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jun 11 14:51:21 2012 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.artifacts.math.Linear;
+
 import gnu.trove.TDoubleArrayList;
 
 import java.io.Serializable;
@@ -11,6 +13,8 @@
 {
     private static Logger log = Logger.getLogger(Parameters.class);
 
+    public static final double EPSILON = 1e-4;
+
     protected String []           columnNames;
     protected TDoubleArrayList [] columns;
 
@@ -153,7 +157,51 @@
             else             return mid;
         }
 
-        return -1;
+        return -(lo + 1);
+    }
+
+    public double [] interpolate(int columnIndex, double key) {
+        return interpolate(columnIndex, key, new double[columns.length]);
+    }
+
+    public double [] interpolate(String columnName, double key) {
+        return interpolate(
+            columnIndex(columnName), key, new double[columns.length]);
+    }
+
+    public double [] interpolate(
+        String    columnName,
+        double    key, 
+        double [] values
+    ) {
+        return interpolate(columnIndex(columnName), key, values);
+    }
+
+    public double [] interpolate(int columnIndex, double key, double [] values) {
+
+        int row = binarySearch(columnIndex, key, EPSILON);
+
+        if (row >= 0) {
+            for (int i = 0; i < values.length; ++i) {
+                values[i] = columns[i].getQuick(row);
+            }
+        }
+        else {
+            row = -row - 1;
+            if (row < 1 || row >= size()) {
+                return null;
+            }
+            double v1 = columns[columnIndex].getQuick(row-1);
+            double v2 = columns[columnIndex].getQuick(row);
+            double factor = Linear.factor(key, v1, v2);
+            for (int i = 0; i < values.length; ++i) {
+                values[i] = Linear.weight(
+                    factor,
+                    columns[i].getQuick(row-1),
+                    columns[i].getQuick(row));
+            }
+        }
+        return values;
     }
 
     public boolean isSorted(String columnName) {

http://dive4elements.wald.intevation.org