changeset 5445:3c7e7d47939f

WaterlevelFacet: Slight refactoring (more needed), fix flys/issue1149.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 27 Mar 2013 09:51:01 +0100
parents 70e0732bd4dd
children 3bd786772798
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java
diffstat 1 files changed, 59 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java	Wed Mar 27 09:49:04 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java	Wed Mar 27 09:51:01 2013 +0100
@@ -76,47 +76,73 @@
 
         WQKms [] wqkms = getWQKms(res);
         Object KM = context.getContextValue("currentKm");
-        if (KM != null) {
-            logger.debug("interpolate at given km");
-            // TODO handle exact match.
-
-            WQKms wqkmsI = wqkms[index];
-            double km = (Double)KM;
 
-            // TODO employ DataUtils interface to TDoubleArraList
-            int size = wqkmsI.size();
-            boolean kmIncreasing = wqkmsI.getKm(0) < wqkmsI.getKm(size-1);
-            int mod = kmIncreasing ? +1 : -1;
-            int idx = 0;
-            if (!kmIncreasing) {
-                while (idx < size && wqkmsI.getKm(idx) < km) {
-                    idx++;
+        // Interpolation.
+        if (KM != null) {
+            linearInterpolate(wqkms[index], (Double) KM);
+        }
+        else {
+            logger.debug("Do not interpolate.");
+        }
+
+        return wqkms != null ? wqkms[index] : null;
+    }
+
+
+    /**
+     * Linear interpolation of WQKms.
+     * TODO rewrite.
+     * @return [w, q, km]
+     */
+    public WQKms linearInterpolate(WQKms wqkms, double km) {
+        logger.debug("interpolate at given km (" + km + ")");
+
+        WQKms resultWQKms = new WQKms();
+        int size = wqkms.size();
+        boolean kmIncreasing = wqkms.getKm(0) < wqkms.getKm(size-1);
+        int mod = kmIncreasing ? +1 : -1;
+        int idx = 0;
+        // Move idx to closest from one direction, check for match.
+        if (!kmIncreasing) {
+            while (idx < size && wqkms.getKm(idx) < km) {
+                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
+                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
+                    return resultWQKms;
                 }
+                idx++;
             }
-            else {
-                idx = size-1;
-                while (idx > 0 && wqkmsI.getKm(idx) > km) {
-                    idx--;
+        }
+        else {
+            idx = size-1;
+            while (idx > 0 && wqkms.getKm(idx) > km) {
+                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
+                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
+                    return resultWQKms;
                 }
+                idx--;
             }
-
-            WQKms resultWQKms = new WQKms();
-            if ((idx != -1) && (idx < size) && (idx - mod != -1) && (idx - mod < size)) {
-                double inW = Linear.linear(
-                    km,
-                    wqkmsI.getKm(idx), wqkmsI.getKm(idx - mod),
-                    wqkmsI.getW(idx), wqkmsI.getW(idx - mod));
-                double inQ = Linear.linear(
-                    km,
-                    wqkmsI.getKm(idx), wqkmsI.getKm(idx - mod),
-                    wqkmsI.getQ(idx), wqkmsI.getQ(idx - mod));
-                resultWQKms.add(inW, inQ, km);
-            }
-
+        }
+        if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
+            resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
             return resultWQKms;
         }
 
-        return wqkms != null ? wqkms[index] : null;
+        if ((idx != -1) && (idx < size) && (idx - mod != -1) && (idx - mod < size)) {
+            double inW = Linear.linear(
+                km,
+                wqkms.getKm(idx), wqkms.getKm(idx - mod),
+                wqkms.getW(idx), wqkms.getW(idx - mod));
+            double inQ = Linear.linear(
+                km,
+                wqkms.getKm(idx), wqkms.getKm(idx - mod),
+                wqkms.getQ(idx), wqkms.getQ(idx - mod));
+            resultWQKms.add(inW, inQ, km);
+        }
+        else {
+            logger.debug("waterlevelfacet stuff " + idx + " size " + size + " mod: " + mod);
+        }
+
+        return resultWQKms;
     }
 
 

http://dive4elements.wald.intevation.org