changeset 362:d79a51fc4f1d

Added necessary methods to start the computation of waterlevel data. flys-artifacts/trunk@1770 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 29 Apr 2011 07:46:27 +0000
parents 3e3ec9613883
children 8422ffc1f2f9
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java
diffstat 3 files changed, 187 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Apr 29 07:27:05 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Apr 29 07:46:27 2011 +0000
@@ -1,3 +1,12 @@
+2011-04-29  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Added some
+	  methods to retrieve necessary information for computing the data of a
+	  waterlevel.
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Added
+	  methods to compute and retrieve the data of a waterlevel computation.
+
 2011-04-29  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WQKms.java: New. This
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Fri Apr 29 07:27:05 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Fri Apr 29 07:46:27 2011 +0000
@@ -61,6 +61,10 @@
     /** The constant string that shows that an operation failed.*/
     public static final String OPERATION_FAILED = "FAILURE";
 
+    /** The default number of steps between the start end end of a selected Q
+     * range.*/
+    public static final int DEFAULT_Q_STEPS = 30;
+
 
     /** The identifier of the current state. */
     protected String currentStateId;
@@ -476,6 +480,30 @@
 
 
     /**
+     * Returns the selected Kms.
+     *
+     * @return the selected Kms.
+     */
+    public double[] getKms() {
+        StateData dStep = getData("ld_step");
+
+        if (dStep == null) {
+            logger.warn("No step width given. Cannot compute Kms.");
+            return null;
+        }
+
+        double   step     = Double.parseDouble((String) dStep.getValue());
+        double[] distance = getDistance();
+        double   lower    = distance[0];
+
+        // transform step from 'm' into 'km'
+        step = step / 1000;
+
+        return getExplodedValues(distance[0], distance[1], step);
+    }
+
+
+    /**
      * Returns the gauge based on the current distance and river.
      *
      * @return the gauge.
@@ -497,5 +525,76 @@
 
         return gauge;
     }
+
+
+    /**
+     * This method returns the Q values.
+     *
+     * @return the selected Q values or null, if no Q values are selected.
+     */
+    public double[] getQs() {
+        StateData dFrom = getData("wq_from");
+        StateData dTo   = getData("wq_to");
+
+        if (dFrom == null || dTo == null) {
+            logger.warn("Missing start or end value for Q range.");
+            return null;
+        }
+
+        double from = Double.parseDouble((String) dFrom.getValue());
+        double to   = Double.parseDouble((String) dTo.getValue());
+
+        StateData dStep = getData("wq_step");
+
+        if (dStep == null) {
+            logger.warn("No step width given. Cannot compute Qs.");
+            return null;
+        }
+
+        double step  = Double.parseDouble((String) dStep.getValue());
+
+        // if no width is given, the DEFAULT_Q_STEPS is used to compute the step
+        // width. Maybe, we should round the value to a number of digits.
+        if (step == 0d) {
+            double diff = to - from;
+            step = diff / DEFAULT_Q_STEPS;
+        }
+
+        return getExplodedValues(from, to, step);
+    }
+
+
+    /**
+     * Returns an array of double values. The values contained in this array
+     * begin with the value <i>from</i> and end with the value <i>to</i>. The
+     * number of values in the result array depends on the <i>step</i> width.
+     *
+     * @param from The lower value.
+     * @param to The upper value.
+     * @param step The step width between two values in the result array.
+     *
+     * @return an array of double values.
+     */
+    public double[] getExplodedValues(double from, double to, double step) {
+        double lower = from;
+
+        double diff = to - from;
+        double tmp  = (diff * 100 / step);
+        int    tmpN = (int) (tmp / 100);
+        int    num  = tmpN + 2;
+
+        double[] values = new double[num];
+        int      idx    = 0;
+
+        do {
+            values[idx++] = lower;
+            lower        += step;
+        }
+        while (lower < to);
+
+        values[idx] = to;
+
+        return values;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Apr 29 07:27:05 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Apr 29 07:46:27 2011 +0000
@@ -20,8 +20,12 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.flys.model.River;
+
 import de.intevation.flys.artifacts.states.DefaultState;
 import de.intevation.flys.artifacts.context.FLYSContext;
+import de.intevation.flys.artifacts.model.WQKms;
+import de.intevation.flys.artifacts.model.WstValueTable;
 
 
 /**
@@ -215,7 +219,82 @@
 
             ui.appendChild(state.describeStatic(doc, ui, context, uuid));
         }
+    }
 
+
+    //
+    // METHODS FOR RETRIEVING COMPUTED DATA FOR DIFFERENT CHART TYPES
+    //
+
+    /**
+     * Returns the data that is computed by a waterlevel computation.
+     *
+     * @return an array of data triples that consist of W, Q and Kms.
+     */
+    public WQKms[] getWaterlevelData()
+    throws NullPointerException
+    {
+        logger.debug("WINFOArtifact.getWaterlevelData");
+
+        River river = getRiver();
+        if (river == null) {
+            throw new NullPointerException("No river selected.");
+        }
+
+        double[] kms = getKms();
+        if (kms == null) {
+            throw new NullPointerException("No Kms selected.");
+        }
+
+        double[] qs = getQs();
+        if (qs == null) {
+            // TODO Use DischargeTableValue.getQForW() to compute the Qs.
+        }
+
+        WstValueTable wst = WstValueTable.getTable(river);
+        if (wst == null) {
+            throw new NullPointerException("No Wst found for selected river.");
+        }
+
+        // TODO Introduce a caching mechanism here!
+
+        return computeWaterlevelData(kms, qs, wst);
+    }
+
+
+    /**
+     * Computes the data of a waterlevel computation based on the interpolation
+     * in WstValueTable.
+     *
+     * @param kms The kilometer values.
+     * @param qa The discharge values.
+     * @param wst The WstValueTable used for the interpolation.
+     *
+     * @return an array of data triples that consist of W, Q and Kms.
+     */
+    public static WQKms[] computeWaterlevelData(
+        double[]      kms,
+        double[]      qs,
+        WstValueTable wst)
+    {
+        logger.info("WINFOArtifact.computeWaterlevelData");
+
+        WQKms[] wqkms = new WQKms[qs.length];
+        for (int i = 0; i < wqkms.length; i++) {
+            wqkms[i] = new WQKms();
+        }
+
+        for (double km: kms) {
+            double[] interpolatedW = wst.interpolateW(km, qs);
+
+            // TODO Modify the interpolation to return a better formed data
+            // structure.
+            for (int i = 0; i < interpolatedW.length; i++) {
+                wqkms[i].add(interpolatedW[i], qs[i], km);
+            }
+        }
+
+        return wqkms;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org