diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWKmsArtifact.java @ 2745:b63017afbca8

Add helper to allow for points at duration curve facet. flys-artifacts/trunk@4480 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 23 May 2012 19:31:26 +0000
parents 10e6400d4166
children 2f7fed1eb4bf
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWKmsArtifact.java	Wed May 23 18:27:45 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWKmsArtifact.java	Wed May 23 19:31:26 2012 +0000
@@ -17,6 +17,7 @@
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.ArtifactNamespaceContext;
 import de.intevation.artifacts.CallMeta;
+import de.intevation.flys.artifacts.math.Linear;
 
 import de.intevation.flys.artifacts.model.CrossSectionWaterLineFacet;
 import de.intevation.flys.artifacts.model.FacetTypes;
@@ -234,10 +235,42 @@
 
 
     /**
+     * Returns W at Km of WKms, linearly interpolated.
+     * Returns -1 if not found.
+     */
+    public static double getWAtKmLin(WKms wkms, double km) {
+        // Uninformed search.
+        int size = wkms.size();
+        int idx = 0;
+        boolean kmIncreasing = (wkms.getKm(0) < wkms.getKm(wkms.size()-1))
+            ? true : false;
+        if (kmIncreasing) {
+            while (idx < size && wkms.getKm(idx) < km) {
+                idx++;
+            }
+        }
+        else {
+            idx = wkms.size() -1;
+            while (idx > 0 && wkms.getKm(idx) > km) {
+                idx--;
+            }
+        }
+
+        if (idx == size -1 || idx == 0) {
+            return -1;
+        }
+
+        // Do linear interpolation
+        int mod = kmIncreasing ? -1 : +1;
+        return Linear.linear(km, wkms.getKm(idx+mod), wkms.getKm(idx), wkms.getW(idx+mod), wkms.getW(idx));
+    }
+
+
+    /**
      * Returns W at Km of WKms, searching linearly.
      * Returns -1 if not found.
      */
-    public double getWAtKm(WKms wkms, double km) {
+    public static double getWAtKm(WKms wkms, double km) {
         // Uninformed search.
         int size = wkms.size();
         for (int i = 0; i < size; i++) {

http://dive4elements.wald.intevation.org