changeset 1033:821aaceb2776

Fix for flys/issue191 flys-artifacts/trunk@2494 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 15 Aug 2011 10:56:58 +0000
parents abd2ab6dff4f
children bf3b3a8d089b
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java
diffstat 4 files changed, 84 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Aug 12 12:54:19 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Aug 15 10:56:58 2011 +0000
@@ -1,3 +1,17 @@
+2011-08-15  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	Fix for flys/issue191
+
+	* src/main/java/de/intevation/flys/artifacts/model/WQ.java(longestIncreasingWRangeIndices):
+	  Added a method to find the longest index range with increasing w values.
+
+	* src/main/java/de/intevation/flys/exports/ATWriter.java: Export the longest
+	  range of monotone increasing w values instead of the first one.
+	  TODO: The first line of the export is still broken.
+
+	* src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java:
+	  Removed superfluous import.
+
 2011-08-12  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Fri Aug 12 12:54:19 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Mon Aug 15 10:56:58 2011 +0000
@@ -1,7 +1,6 @@
 package de.intevation.flys.artifacts;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import org.w3c.dom.Document;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java	Fri Aug 12 12:54:19 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java	Mon Aug 15 10:56:58 2011 +0000
@@ -4,9 +4,13 @@
 
 import java.util.Random;
 
+import org.apache.log4j.Logger;
+
 public class WQ
 extends      NamedObjectImpl
 {
+    private static Logger logger = Logger.getLogger(WQ.class);
+
     // TODO: s/w/ws/g
     protected TDoubleArrayList w;
 
@@ -135,5 +139,53 @@
         return up > samples/2;
     }
 
+    public int [] longestIncreasingWRangeIndices() {
+        return longestIncreasingWRangeIndices(new int[2]);
+    }
+
+    public int [] longestIncreasingWRangeIndices(int [] bounds) {
+
+        int N = size();
+        int start = 0;
+        int stop  = 0;
+
+        double lastW = Double.MAX_VALUE;
+
+        for (int i = 0; i < N; ++i) {
+            double v = w.getQuick(i);
+            if (v <= lastW) {
+                if (stop-start > bounds[1]-bounds[0]) {
+                    bounds[0] = start;
+                    bounds[1] = stop;
+                    if (logger.isDebugEnabled()) {
+                        logger.debug("new range: " +
+                            bounds[0] + " - " + bounds[1] + " (" +
+                            w.getQuick(bounds[0]) + ", " +
+                            w.getQuick(bounds[1]) + ")");
+
+                    }
+                }
+                start = stop = i;
+            }
+            else {
+                stop = i;
+            }
+            lastW = v;
+        }
+
+        if (stop-start > bounds[1]-bounds[0]) {
+            bounds[0] = start;
+            bounds[1] = stop;
+            if (logger.isDebugEnabled()) {
+                logger.debug("new range @end: " +
+                    bounds[0] + " - " + bounds[1] + " (" +
+                    w.getQuick(bounds[0]) + ", " +
+                    w.getQuick(bounds[1]) + ")");
+
+            }
+        }
+
+        return bounds;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java	Fri Aug 12 12:54:19 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java	Mon Aug 15 10:56:58 2011 +0000
@@ -39,52 +39,39 @@
 
     public ATWriter(WQ wq) throws IllegalArgumentException {
 
-        int maxIndex = maxIncreasingWIndex(wq);
+        int [] bounds = wq.longestIncreasingWRangeIndices();
 
-        if (maxIndex < 1) { // Only first w can be written out.
-            minW = maxW = wq.getW(0);
-            minQ = maxQ = wq.getQ(0);
+        if (logger.isDebugEnabled()) {
+            logger.debug("exporting w between indices " +
+                bounds[0] + " and " + bounds[1] + " (" +
+                wq.getW(bounds[0]) + ", " + wq.getW(bounds[1]));
+        }
+
+        if (bounds[1]-bounds[0] < 1) { // Only first w can be written out.
+            minW = maxW = wq.getW(bounds[0]);
+            minQ = maxQ = wq.getQ(bounds[0]);
             // constant function
             qFunc = new PolynomialFunction(new double [] { minQ });
             return;
         }
 
-        double [] ws = new double[maxIndex];
+        double [] ws = new double[bounds[1]-bounds[0]];
         double [] qs = new double[ws.length];
 
         for (int i = 0; i < ws.length; ++i) {
-            ws[i] = wq.getW(i);
-            qs[i] = wq.getQ(i);
+            int idx = bounds[0]+i;
+            ws[i] = wq.getW(idx);
+            qs[i] = wq.getQ(idx);
         }
 
         qFunc = ws.length < 3
             ? new LinearInterpolator().interpolate(ws, qs)
             : new SplineInterpolator().interpolate(ws, qs);
 
-        minW = wq.getW(0);
-        maxW = wq.getW(maxIndex);
-        minQ = wq.getQ(0);
-        maxQ = wq.getQ(maxIndex);
-    }
-
-    public static int maxIncreasingWIndex(WQ wq) {
-
-        int N = wq.size();
-
-        if (N < 2) {
-            return N-1;
-        }
-
-        double last = wq.getW(0);
-
-        for (int i = 1; i < N; ++i) {
-            double current = wq.getW(i);
-            if (current <= last) {
-                return i-1;
-            }
-        }
-
-        return N-1;
+        minW = wq.getW(bounds[0]);
+        maxW = wq.getW(bounds[1]);
+        minQ = wq.getQ(bounds[0]);
+        maxQ = wq.getQ(bounds[1]);
     }
 
     public double getQ(double w) {

http://dive4elements.wald.intevation.org