changeset 676:c501f27c1f71

Added error reporting to 'Dauerzahlen' calculation. flys-artifacts/trunk@2100 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 10 Jun 2011 12:38:08 +0000
parents 8b0152363bdb
children a95f34f1f39a
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation3.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQDay.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java
diffstat 6 files changed, 118 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jun 10 10:10:04 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jun 10 12:38:08 2011 +0000
@@ -1,3 +1,20 @@
+2011-06-10  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/Calculation3.java:
+	  New. Factored out version of "Dauerzahlen".
+
+	* src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java:
+	  Loop errors through for q->w interpolations.
+	  
+	* src/main/java/de/intevation/flys/artifacts/model/WQDay.java:
+	  Added constructor to directly create with calculated results.
+
+	* src/main/java/de/intevation/flys/artifacts/model/Calculation.java:
+	  Added method to return the number of problems.
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java:
+	  Use factored out version of calculation 3.
+
 2011-06-10  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WQKms.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Jun 10 10:10:04 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Jun 10 12:38:08 2011 +0000
@@ -34,6 +34,7 @@
 import de.intevation.flys.artifacts.model.WstValueTable;
 import de.intevation.flys.artifacts.model.WstValueTable.QPosition;
 import de.intevation.flys.artifacts.model.WstValueTableFactory;
+import de.intevation.flys.artifacts.model.Calculation3;
 import de.intevation.flys.artifacts.model.Calculation4;
 import de.intevation.flys.artifacts.model.Segment;
 
@@ -416,16 +417,10 @@
         int[]    days = (int[]) obj[0];
         double[] qs   = (double[]) obj[1];
 
-        double[] interpolatedW = new double[qs.length];
-        interpolatedW          = wst.interpolateW(location, qs, interpolatedW);
-
-        WQDay wqday = new WQDay(qs.length);
+        Calculation3 calculation = new Calculation3(location, days, qs);
 
-        for (int i = 0; i < days.length; i++) {
-            wqday.add(days[i], interpolatedW[i], qs[i]);
-        }
-
-        return wqday;
+        // TODO: report the errors to the user.
+        return calculation.calculate(wst);
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation.java	Fri Jun 10 10:10:04 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation.java	Fri Jun 10 12:38:08 2011 +0000
@@ -6,10 +6,14 @@
 import java.util.List;
 import java.util.ArrayList;
 
+import java.io.Serializable;
+
 public class Calculation
+implements   Serializable
 {
-    public static class Problem {
-
+    public static class Problem 
+    implements          Serializable
+    {
         protected Double km;
         protected String msg;
 
@@ -59,6 +63,10 @@
         return problems != null && !problems.isEmpty();
     }
 
+    public int numProblems() {
+        return problems != null ? problems.size() : 0;
+    }
+
     public List<Problem> getProblems() {
         return problems;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation3.java	Fri Jun 10 12:38:08 2011 +0000
@@ -0,0 +1,37 @@
+package de.intevation.flys.artifacts.model;
+
+import org.apache.log4j.Logger;
+
+public class Calculation3
+extends      Calculation
+{
+    private static Logger logger = Logger.getLogger(Calculation3.class);
+
+    protected double    km;
+    protected int    [] days;
+    protected double [] qs;
+
+    public Calculation3() {
+    }
+
+    public Calculation3(double km, int [] days, double [] qs) {
+        this.km   = km;
+        this.days = days;
+        this.qs   = qs;
+    }
+
+    public WQDay calculate(WstValueTable wst) {
+
+        double [] ws = wst.interpolateW(km, qs, new double[qs.length], this);
+
+        WQDay wqday = new WQDay(days, ws, qs);
+
+        if (hasProblems()) {
+            logger.debug("calculation caused "+numProblems()+" problem(s).");
+            wqday.removeNaNs();
+        }
+
+        return wqday;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQDay.java	Fri Jun 10 10:10:04 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQDay.java	Fri Jun 10 12:38:08 2011 +0000
@@ -34,6 +34,12 @@
         qs   = new TDoubleArrayList(capacity);
     }
 
+    public WQDay(int [] days, double [] ws, double [] qs) {
+        this.days = new TIntArrayList(days);
+        this.ws   = new TDoubleArrayList(ws);
+        this.qs   = new TDoubleArrayList(qs);
+    }
+
 
     public void add(int day, double w, double q) {
         days.add(day);
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Fri Jun 10 10:10:04 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Fri Jun 10 12:38:08 2011 +0000
@@ -107,7 +107,8 @@
             double        km,
             double []     iqs,
             double []     ows,
-            WstValueTable table
+            WstValueTable table,
+            Calculation   errors
         ) {
             double kmWeight = Linear.factor(km, this.km, other.km);
 
@@ -117,9 +118,23 @@
                 if (table.getQPosition(km, iqs[i], qPosition) != null) {
                     double wt =       getW(qPosition);
                     double wo = other.getW(qPosition);
-                    ows[i] = Linear.weight(kmWeight, wt, wo);
+                    if (Double.isNaN(wt) || Double.isNaN(wo)) {
+                        if (errors != null) {
+                            // TODO: I18N
+                            errors.addProblem(
+                                km, "cannot find w for q = " + iqs[i]);
+                        }
+                        ows[i] = Double.NaN;
+                    }
+                    else {
+                        ows[i] = Linear.weight(kmWeight, wt, wo);
+                    }
                 }
                 else {
+                    if (errors != null) {
+                        // TODO: I18N
+                        errors.addProblem(km, "cannot find q = " + iqs[i]);
+                    }
                     ows[i] = Double.NaN;
                 }
             }
@@ -282,7 +297,15 @@
     }
 
     public double [] interpolateW(double km, double [] qs, double [] ws) {
+        return interpolateW(km, qs, ws, null);
+    }
 
+    public double [] interpolateW(
+        double      km,
+        double []   qs, 
+        double []   ws,
+        Calculation errors
+    ) {
         int rowIndex = Collections.binarySearch(rows, new Row(km));
 
         QPosition qPosition = new QPosition();
@@ -290,9 +313,21 @@
         if (rowIndex >= 0) { // direct row match
             Row row = rows.get(rowIndex);
             for (int i = 0; i < qs.length; ++i) {
-                ws[i] = getQPosition(km, qs[i], qPosition) != null
-                    ? row.getW(qPosition)
-                    : Double.NaN;
+                if (getQPosition(km, qs[i], qPosition) == null) {
+                    if (errors != null) {
+                        // TODO: I18N
+                        errors.addProblem(km, "cannot find q = " + qs[i]);
+                    }
+                    ws[i] = Double.NaN;
+                }
+                else {
+                    if (Double.isNaN(ws[i] = row.getW(qPosition))
+                    && errors != null) {
+                        // TODO: I18N
+                        errors.addProblem(
+                            km, "cannot find w for q = " + qs[i]);
+                    }
+                }
             }
         }
         else { // needs bilinear interpolation
@@ -301,11 +336,14 @@
             if (rowIndex < 1 || rowIndex >= rows.size()) {
                 // do not extrapolate
                 Arrays.fill(ws, Double.NaN);
+                if (errors != null) {
+                    errors.addProblem(km, "km not found");
+                }
             }
             else {
                 Row r1 = rows.get(rowIndex-1);
                 Row r2 = rows.get(rowIndex);
-                r1.interpolateW(r2, km, qs, ws, this);
+                r1.interpolateW(r2, km, qs, ws, this, errors);
             }
         }
 

http://dive4elements.wald.intevation.org