changeset 732:39d191f011dc

ATWriter: Results are now in cm. Made it more robust against corner cases. flys-artifacts/trunk@2224 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 23 Jun 2011 21:44:24 +0000
parents 5ddb1b568f38
children 5f5e67aceb8c
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java
diffstat 3 files changed, 38 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 23 17:22:17 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jun 23 21:44:24 2011 +0000
@@ -1,3 +1,11 @@
+2011-06-23  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/ATWriter.java:
+	  Results are now in cm. Made it more robust against corner cases.
+
+	* src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java:
+	  Removed superfluous import.
+
 2011-06-23  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* doc/conf/artifacts/winfo.xml, doc/conf/conf.xml:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java	Thu Jun 23 17:22:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ATWriter.java	Thu Jun 23 21:44:24 2011 +0000
@@ -8,11 +8,14 @@
 
 import de.intevation.flys.artifacts.model.WQ;
 
-import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
 
-import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
+import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
+import org.apache.commons.math.analysis.interpolation.LinearInterpolator;
 
-import org.apache.commons.math.ArgumentOutsideDomainException;
+import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
+
+import org.apache.commons.math.FunctionEvaluationException;
 
 import org.apache.log4j.Logger;
 
@@ -22,14 +25,14 @@
 
     public static final int COLUMNS = 10;
 
-    public static final String EMPTY = "             ";
+    public static final String EMPTY = "         ";
 
     protected double minW;
     protected double maxW;
     protected double minQ;
     protected double maxQ;
 
-    protected PolynomialSplineFunction qFunc;
+    protected UnivariateRealFunction qFunc;
 
     public ATWriter() {
     }
@@ -41,6 +44,8 @@
         if (maxIndex < 1) { // Only first w can be written out.
             minW = maxW = wq.getW(0);
             minQ = maxQ = wq.getQ(0);
+            // constant function
+            qFunc = new PolynomialFunction(new double [] { minQ });
             return;
         }
 
@@ -52,9 +57,9 @@
             qs[i] = wq.getQ(i);
         }
 
-        SplineInterpolator interpolator = new SplineInterpolator();
-
-        qFunc = interpolator.interpolate(ws, qs);
+        qFunc = ws.length < 3
+            ? new LinearInterpolator().interpolate(ws, qs)
+            : new SplineInterpolator().interpolate(ws, qs);
 
         minW = wq.getW(0);
         maxW = wq.getW(maxIndex);
@@ -87,7 +92,7 @@
         try {
             return qFunc.value(w);
         }
-        catch (ArgumentOutsideDomainException aode) {
+        catch (FunctionEvaluationException aode) {
             // should not happen
             logger.warn("spline interpolation failed", aode);
             return w <= minW ? minQ : maxQ;
@@ -96,12 +101,12 @@
 
     protected static void printQ(PrintWriter out, double q) {
         String format;
-             if (q <   1) format = " % 8.3f";
-        else if (q <  10) format = " % 8.2f";
-        else if (q < 100) format = " % 8.1f";
+             if (q <   1d) format = " % 8.3f";
+        else if (q <  10d) format = " % 8.2f";
+        else if (q < 100d) format = " % 8.1f";
         else {
             format = " % 8.0f";
-            if (q > 1000) q = Math.rint(q/10)*10;
+            if (q > 1000d) q = Math.rint(q/10d)*10d;
         }
         out.printf(Locale.US, format, q);
     }
@@ -110,26 +115,28 @@
 
         PrintWriter out = new PrintWriter(writer);
 
-        double range = COLUMNS;
+        double rest = Math.abs(minW % COLUMNS);
 
-        double rest = Math.abs(minW % range);
-
-        int startW = (int)(minW - rest);
+        double startW = Math.round(minW*10.0)/10.0;
         if (rest >= 1d) {
-            out.printf(Locale.US, "%8d", startW);
+            startW -= 0.1;
+            out.printf(Locale.US, "%8d", (int)Math.round(startW*100.0));
             int columns = (int)Math.floor(rest);
             for (int i = columns; i < COLUMNS; ++i) {
                 out.print(EMPTY);
             }
-            for (int i = 0, N = COLUMNS-columns; i < N; ++i) {
-                printQ(out, getQ(startW + (COLUMNS-i-1)));
+            for (int i = COLUMNS-columns; i < COLUMNS; ++i) {
+                printQ(out, getQ(startW + i*0.01));
             }
-            startW += COLUMNS;
+            out.println();
+            startW += 0.1;
         }
 
         int col = 0;
-        for (int w = startW; w <= maxW; ++w) {
-            if (col == 0) out.printf(Locale.US, "%8d", w);
+        for (double w = startW; w <= maxW; w += 0.01) {
+            if (col == 0) {
+                out.printf(Locale.US, "%8d", (int)Math.round(w*100.0));
+            }
 
             printQ(out, getQ(w));
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java	Thu Jun 23 17:22:17 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java	Thu Jun 23 21:44:24 2011 +0000
@@ -5,7 +5,6 @@
 import org.w3c.dom.Document;
 
 import org.jfree.chart.JFreeChart;
-import org.jfree.chart.plot.XYPlot;
 import org.jfree.chart.title.TextTitle;
 
 import de.intevation.artifacts.Artifact;

http://dive4elements.wald.intevation.org