diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java @ 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 febc39e77672
children 49baebb39305
line wrap: on
line diff
--- 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