diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 686:3dc61e00385e facets-slt

Merged with trunk and introduced hashing of computed values. flys-artifacts/branches/facets-slt@2126 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 15 Jun 2011 15:28:54 +0000
parents fdc898a134a7
children 06689035024c
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Jun 08 13:03:21 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Jun 15 15:28:54 2011 +0000
@@ -1,9 +1,6 @@
 package de.intevation.flys.artifacts;
 
 import java.util.List;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.ArrayList;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -25,15 +22,19 @@
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.River;
 
+import de.intevation.flys.artifacts.states.CalculationSelect;
 import de.intevation.flys.artifacts.states.DefaultState;
 import de.intevation.flys.artifacts.context.FLYSContext;
 
+import de.intevation.flys.artifacts.model.ComputeCallback;
 import de.intevation.flys.artifacts.model.MainValuesFactory;
 import de.intevation.flys.artifacts.model.WQDay;
 import de.intevation.flys.artifacts.model.WQKms;
 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.Calculation1;
+import de.intevation.flys.artifacts.model.Calculation2;
+import de.intevation.flys.artifacts.model.Calculation3;
 import de.intevation.flys.artifacts.model.Calculation4;
 import de.intevation.flys.artifacts.model.Segment;
 
@@ -241,6 +242,76 @@
     // METHODS FOR RETRIEVING COMPUTED DATA FOR DIFFERENT CHART TYPES
     //
 
+
+    public Object compute() {
+        return compute(hash());
+    }
+
+
+    public Object compute(String hash) {
+        String calc = (String) getData(CalculationSelect.FIELD_MODE).getValue();
+
+        ComputeCallback callback = null;
+
+        if (calc.equals(CalculationSelect.CALCULATION_SURFACE_CURVE)) {
+            callback = createSurfaceCurveCallback();
+        }
+        else if (calc.equals(CalculationSelect.CALCULATION_DURATION_CURVE)) {
+            callback = createDurationCurveCallback();
+        }
+        else if (
+            calc.equals(
+                CalculationSelect.CALCULATION_DISCHARGE_LONGITUDINAL_CURVE))
+        {
+            callback = createDischargeLongitudinalCurveCallback();
+        }
+        else if (calc.equals(CalculationSelect.CALCULATION_DISCHARGE_CURVE)) {
+            callback = createDischargeCurveCallback();
+        }
+        else {
+            return null;
+        }
+
+        return compute(hash, callback);
+    }
+
+
+    protected ComputeCallback createSurfaceCurveCallback() {
+        return new ComputeCallback() {
+            public Object compute() {
+                return getWaterlevelData();
+            }
+        };
+    }
+
+
+    protected ComputeCallback createDurationCurveCallback() {
+        return new ComputeCallback() {
+            public Object compute() {
+                return getDurationCurveData();
+            }
+        };
+    }
+
+
+    protected ComputeCallback createDischargeLongitudinalCurveCallback() {
+        return new ComputeCallback() {
+            public Object compute() {
+                return getDischargeLongitudinalSectionData();
+            }
+        };
+    }
+
+
+    protected ComputeCallback createDischargeCurveCallback() {
+        return new ComputeCallback() {
+            public Object compute() {
+                return getComputedDischargeCurveData();
+            }
+        };
+    }
+
+
     /**
      * Returns the data that is computed by a waterlevel computation.
      *
@@ -277,43 +348,12 @@
             throw new NullPointerException("No Wst found for selected river.");
         }
 
-        HashSet<Integer> failed = new HashSet<Integer>();
-
         WQKms[] results = computeWaterlevelData(
-            kms, qs, wst, river.getKmUp(), failed);
-
-        // TODO Introduce a caching mechanism here!
-
-        setWaterlevelNames(
-            results, qSel ? qs : ws, qSel ? "Q" : "W", failed);
+            kms, qs, ws, wst, river.getKmUp());
 
         return results;
     }
 
-
-    /**
-     * Sets the name for waterlevels where each WQKms in <i>r</i> represents a
-     * column.
-     *
-     * @param r The waterlevel columns.
-     * @param v The input values of the computations.
-     * @param wq The WQ mode - can be one of "W" or "Q".
-     */
-    public static void setWaterlevelNames(
-        WQKms[]  r, 
-        double[] v, 
-        String   wq, 
-        Set      failed
-    ) {
-        int pos = 0;
-        for (int i = 0; i < v.length; i++) {
-            if (!failed.contains(i)) {
-                r[pos++].setName(wq + "=" + v[i]);
-            }
-        }
-    }
-
-
     /**
      * Computes the data of a waterlevel computation based on the interpolation
      * in WstValueTable.
@@ -325,35 +365,19 @@
      * @return an array of data triples that consist of W, Q and Kms.
      */
     public static WQKms[] computeWaterlevelData(
-        double[]      kms,
-        double[]      qs,
+        double []     kms,
+        double []     qs,
+        double []     ws,
         WstValueTable wst,
-        boolean       up,
-        Set<Integer>  failed
+        boolean       up
     ) {
         logger.info("WINFOArtifact.computeWaterlevelData");
 
-        WQKms[] wqkms = new WQKms[qs.length];
-
-        ArrayList<WQKms> results = new ArrayList<WQKms>();
-
-        int referenceIndex = up ? 0 : kms.length-1; 
+        Calculation1 calculation = new Calculation1(kms, qs, ws, up);
 
-        for (int i = 0; i < qs.length; i++) {
-            double [] oqs = new double[kms.length];
-            double [] ows = new double[kms.length];
-            WstValueTable.QPosition qPosition =
-                wst.interpolate(qs[i], kms[referenceIndex], kms, ows, oqs);
-            if (qPosition != null) {
-                results.add(new WQKms(kms, oqs, ows));
-            }
-            else {
-                logger.warn("interpolation failed for q = " + qs[i]);
-                failed.add(i);
-            }
-        }
+        WQKms[] wqkms = calculation.calculate(wst);
 
-        return results.toArray(new WQKms[results.size()]);
+        return wqkms;
     }
 
 
@@ -390,8 +414,6 @@
             throw new NullPointerException("No Wst found for selected river.");
         }
 
-        // TODO Introduce a caching mechanism here!
-
         return computeDurationCurveData(g, wst, locations[0]);
     }
 
@@ -416,16 +438,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);
     }
 
 
@@ -458,26 +474,11 @@
 
         WQKms wqkms = computeDischargeCurveData(wst, locations[0]);
 
-        // TODO Introduce a caching mechanism here!
-
-        setComputedDischargeCurveNames(wqkms, locations[0]);
-
         return wqkms;
     }
 
 
     /**
-     * Sets the name of the computed discharge curve data.
-     *
-     * @param wqkms The computed WQKms object.
-     * @param l The location used for the computation.
-     */
-    public static void setComputedDischargeCurveNames(WQKms wqkms, double l) {
-        wqkms.setName(Double.toString(l));
-    }
-
-
-    /**
      * Computes the data used to create computed discharge curves.
      *
      * @param wst The WstValueTable that is used for the interpolation.
@@ -492,21 +493,11 @@
     {
         logger.info("WINFOArtifact.computeDischargeCurveData");
 
-        double[][] wqs = wst.interpolateWQ(location);
-
-        if (wqs == null) {
-            logger.error("Cannot compute discharge curve data.");
-            return null;
-        }
+        Calculation2 calculation = new Calculation2(location);
 
-        double[] ws = wqs[0];
-        double[] qs = wqs[1];
+        WQKms wqkms = calculation.calculate(wst);
 
-        WQKms wqkms = new WQKms(ws.length);
-
-        for (int i = 0; i < ws.length; i++) {
-            wqkms.add(ws[i], qs[i], location);
-        }
+        // TODO: Report errors to the user
 
         return wqkms;
     }

http://dive4elements.wald.intevation.org