changeset 3441:bfbd478bd607

FixA: Moved Q -> W conversion code into segments to be reusable. flys-artifacts/trunk@5104 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 23 Jul 2012 14:19:07 +0000
parents fed6685692d2
children cd5eb8f5f6f1
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java
diffstat 3 files changed, 98 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Jul 23 11:52:21 2012 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jul 23 14:19:07 2012 +0000
@@ -1,3 +1,9 @@
+2012-07-23	Christian Lins	<christian.lins@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/Segment.java,
+	  src/main/java/de/intevation/flys/artifacts/model/Calculation4.java:
+	  Moved code to convert W to Q at gauge to segments to be reusable.
+
 2012-07-23	Christian Lins	<christian.lins@intevation.de>
 
 	* doc/conf/themes.xml,
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java	Mon Jul 23 11:52:21 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java	Mon Jul 23 14:19:07 2012 +0000
@@ -1,21 +1,18 @@
 package de.intevation.flys.artifacts.model;
 
-import java.util.List;
-import java.util.Arrays;
-import java.util.Collections;
+import de.intevation.flys.artifacts.math.BackJumpCorrector;
+import de.intevation.flys.artifacts.math.Function;
+import de.intevation.flys.artifacts.math.Identity;
+import de.intevation.flys.artifacts.math.Linear;
+
+import de.intevation.flys.artifacts.model.WstValueTable.QPosition;
+
+import de.intevation.flys.model.River;
 
 import de.intevation.flys.utils.DoubleUtil;
 
-import de.intevation.flys.model.River;
-import de.intevation.flys.model.Gauge;
-import de.intevation.flys.model.DischargeTable;
-
-import de.intevation.flys.artifacts.model.WstValueTable.QPosition;
-
-import de.intevation.flys.artifacts.math.Function;
-import de.intevation.flys.artifacts.math.Linear;
-import de.intevation.flys.artifacts.math.Identity;
-import de.intevation.flys.artifacts.math.BackJumpCorrector;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 
@@ -38,65 +35,7 @@
         this.segments = segments;
         this.isQ      = isQ;
 
-        int numResults = -1;
-
-        // assign reference points
-        for (Segment segment: segments) {
-            Gauge gauge = river.maxOverlap(segment.getFrom(), segment.getTo());
-
-            if (gauge == null) {
-                logger.warn("no gauge found. Defaults to mid point.");
-                segment.setReferencePoint(
-                    0.5*(segment.getFrom()+segment.getTo()));
-            }
-            else {
-                double ref = gauge.getStation().doubleValue();
-                logger.debug(
-                    "reference gauge: " + gauge.getName() +
-                    " (km " + ref + ")");
-                segment.setReferencePoint(ref);
-            }
-
-            double [] values = segment.values;
-
-            if (numResults == -1) {
-                numResults = values.length;
-            }
-            else if (numResults != values.length) {
-                throw new IllegalArgumentException("wrong length of values");
-            }
-
-            // convert to Q if needed
-            if (!isQ && gauge != null) {
-
-                DischargeTable dt = gauge.fetchMasterDischargeTable();
-
-                double [][] table =
-                    DischargeTables.loadDischargeTableValues(dt, 1);
-
-                // need the original values for naming
-                segment.backup();
-
-                for (int i = 0; i < values.length; ++i) {
-                    double w = values[i] / 100.0;
-                    double [] qs = DischargeTables.getQsForW(table, w);
-                    if (qs.length == 0) {
-                        logger.warn("No Qs found for W = " + values[i]);
-                        addProblem("cannot.find.w.for.q", values[i]);
-                        values[i] = Double.NaN;
-                    }
-                    else {
-                        values[i] = qs[0];
-                        if (qs.length > 1) {
-                            logger.warn(
-                                "More than one Q found for W = " + values[i]);
-                        }
-                    }
-                }
-            }
-        } // for all segments
-
-        Collections.sort(segments, Segment.REF_CMP);
+        Segment.setReferencePointConvertQ(segments, river, isQ, this);
     }
 
     public CalculationResult calculate(
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java	Mon Jul 23 11:52:21 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java	Mon Jul 23 14:19:07 2012 +0000
@@ -1,5 +1,9 @@
 package de.intevation.flys.artifacts.model;
 
+import de.intevation.flys.model.DischargeTable;
+import de.intevation.flys.model.Gauge;
+import de.intevation.flys.model.River;
+
 import de.intevation.flys.utils.DoubleUtil;
 
 import gnu.trove.TDoubleArrayList;
@@ -7,6 +11,7 @@
 import java.io.Serializable;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 
@@ -15,7 +20,7 @@
 public class Segment
 implements   Serializable
 {
-    private static Logger logger = Logger.getLogger(Segment.class);
+    private static Logger log = Logger.getLogger(Segment.class);
 
     public static final Comparator<Segment> REF_CMP =
         new Comparator<Segment>() {
@@ -112,7 +117,7 @@
         for (String segmentStr: input.split(":")) {
             String [] parts = segmentStr.split(";");
             if (parts.length < 3) {
-                logger.warn("invalid segment: '" + segmentStr + "'");
+                log.warn("invalid segment: '" + segmentStr + "'");
                 continue;
             }
             try {
@@ -130,11 +135,84 @@
                 segments.add(new Segment(from, to, values));
             }
             catch (NumberFormatException nfe) {
-                logger.warn("invalid segment: '" + segmentStr + "'");
+                log.warn("invalid segment: '" + segmentStr + "'");
             }
         }
 
         return segments;
     }
+
+    public static boolean setReferencePointConvertQ(
+        List<Segment> segments,
+        River         river,
+        boolean       isQ,
+        Calculation   report
+    ) {
+        int numResults = -1;
+
+        boolean success = true;
+
+        // assign reference points
+        for (Segment segment: segments) {
+            Gauge gauge = river.maxOverlap(segment.getFrom(), segment.getTo());
+
+            if (gauge == null) {
+                log.warn("no gauge found. Defaults to mid point.");
+                segment.setReferencePoint(
+                    0.5*(segment.getFrom()+segment.getTo()));
+            }
+            else {
+                double ref = gauge.getStation().doubleValue();
+                log.debug(
+                    "reference gauge: " + gauge.getName() +
+                    " (km " + ref + ")");
+                segment.setReferencePoint(ref);
+            }
+
+            double [] values = segment.values;
+
+            if (numResults == -1) {
+                numResults = values.length;
+            }
+            else if (numResults != values.length) {
+                log.warn("wrong length of values");
+                return false;
+            }
+
+            // convert to Q if needed
+            if (!isQ && gauge != null) {
+
+                DischargeTable dt = gauge.fetchMasterDischargeTable();
+
+                double [][] table =
+                    DischargeTables.loadDischargeTableValues(dt, 1);
+
+                // need the original values for naming
+                segment.backup();
+
+                for (int i = 0; i < values.length; ++i) {
+                    double w = values[i] / 100.0;
+                    double [] qs = DischargeTables.getQsForW(table, w);
+                    if (qs.length == 0) {
+                        log.warn("No Qs found for W = " + values[i]);
+                        report.addProblem("cannot.find.w.for.q", values[i]);
+                        values[i] = Double.NaN;
+                        success = false;
+                    }
+                    else {
+                        values[i] = qs[0];
+                        if (qs.length > 1) {
+                            log.warn(
+                                "More than one Q found for W = " + values[i]);
+                        }
+                    }
+                }
+            }
+        } // for all segments
+
+        Collections.sort(segments, Segment.REF_CMP);
+
+        return success;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org