changeset 2228:4db19a88bddb

Implemented the first step of Calculation6; create Facets for each calculation results. flys-artifacts/trunk@3868 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Feb 2012 14:48:00 +0000 (2012-02-01)
parents 2e6f0ef36352
children 9dc81827b187
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/Calculation6.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java
diffstat 4 files changed, 146 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Feb 01 14:43:50 2012 +0000
+++ b/flys-artifacts/ChangeLog	Wed Feb 01 14:48:00 2012 +0000
@@ -1,3 +1,17 @@
+2012-02-01  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Create time
+	  millis for timerange used in Calculation6.
+
+	* src/main/java/de/intevation/flys/artifacts/model/Calculation6.java: Added
+	  the first step of the calculation: fetch relevant discharge tables based
+	  on the given input parameters. The calculated values are not the right
+	  ones! Finding Ws and Qs in discharge tables is still not implemented! WORK
+	  IN PROGRESS!
+
+	* src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java:
+	  Create facets for each WQTimerange object calculated by Calculation6.
+
 2012-02-01  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/Timerange.java: Added a
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Feb 01 14:43:50 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Feb 01 14:48:00 2012 +0000
@@ -58,7 +58,9 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Collections;
+import java.util.GregorianCalendar;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -746,7 +748,15 @@
         int[]    timerange = FLYSUtils.intArrayFromString(rawTimerange);
         double[] values    = FLYSUtils.doubleArrayFromString(rawValues);
 
-        return new Calculation6(mode, timerange, values).calculate(gauge);
+        Calendar start = new GregorianCalendar(timerange[0], 0, 1);
+        Calendar end   = new GregorianCalendar(timerange[1], 0, 1);
+
+        Calculation6 calc = new Calculation6(
+            mode,
+            new long[] { start.getTimeInMillis(), end.getTimeInMillis() },
+            values);
+
+        return calc.calculate(gauge);
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java	Wed Feb 01 14:43:50 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java	Wed Feb 01 14:48:00 2012 +0000
@@ -1,8 +1,14 @@
 package de.intevation.flys.artifacts.model;
 
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
+import de.intevation.flys.model.DischargeTable;
 import de.intevation.flys.model.Gauge;
+import de.intevation.flys.model.TimeInterval;
 
 
 /**
@@ -13,7 +19,7 @@
     private static final Logger logger = Logger.getLogger(Calculation6.class);
 
     private int      mode;
-    private int[]    timerange;
+    private long[]   timerange;
     private double[] values;
 
 
@@ -21,7 +27,7 @@
     public static final int MODE_Q = 1;
 
 
-    public Calculation6(int mode, int[] timerange, double[] values) {
+    public Calculation6(int mode, long[] timerange, double[] values) {
         this.mode      = mode;
         this.timerange = timerange;
         this.values    = values;
@@ -39,9 +45,13 @@
             debug();
         }
 
-        logger.warn("TODO: IMPLEMENT ME");
+        List<DischargeTable> dts = fetchDischargeTables(gauge);
 
-        return null;
+        logger.debug("Take " + dts.size() + " into account.");
+
+        WQTimerange[] wqt = prepareCalculationData(dts);
+
+        return new CalculationResult(wqt, this);
     }
 
 
@@ -65,6 +75,90 @@
     }
 
 
+    protected List<DischargeTable> fetchDischargeTables(Gauge gauge) {
+        List<DischargeTable> relevant = new ArrayList<DischargeTable>();
+        List<DischargeTable> all      = gauge.getDischargeTables();
+
+        for (DischargeTable dt: all) {
+            if (isDischargeTableRelevant(dt)) {
+                relevant.add(dt);
+            }
+        }
+
+        return relevant;
+    }
+
+
+    protected boolean isDischargeTableRelevant(DischargeTable dt) {
+        TimeInterval ti = dt.getTimeInterval();
+
+        Date start     = ti.getStartTime();
+        long startTime = start.getTime();
+
+        if (startTime >= timerange[0] && startTime <= timerange[1]) {
+            return true;
+        }
+
+        Date stop     = ti.getStopTime();
+        long stopTime = stop != null ? stop.getTime() : -1l;
+
+        if (stopTime >= timerange[0] && stopTime <= timerange[1]) {
+            return true;
+        }
+
+        logger.debug("DischargeTable not in range: " + start + " -> " + stop);
+
+        return false;
+    }
+
+
+    protected WQTimerange[] prepareCalculationData(List<DischargeTable> dts) {
+        List<WQTimerange> wqts = new ArrayList<WQTimerange>(values.length);
+
+        for (double value: values) {
+            logger.debug("Prepare data for value: " + value);
+
+            String name = mode == MODE_W ? "W=" + value : "Q=" + value;
+            WQTimerange wqt = new WQTimerange(name);
+
+            for (DischargeTable dt: dts) {
+                TimeInterval ti = dt.getTimeInterval();
+
+                Timerange t = new Timerange(ti.getStartTime(),ti.getStopTime());
+                double    w;
+                double    q;
+
+                if (mode == MODE_W) {
+                    w = value;
+                    q = findValueForW(dt, w);
+                }
+                else {
+                    q = value;
+                    w = findValueForQ(dt, q);
+                }
+
+                wqt.add(w, q, t);
+            }
+
+            wqts.add(wqt);
+        }
+
+        return (WQTimerange[]) wqts.toArray(new WQTimerange[wqts.size()]);
+    }
+
+
+    protected double findValueForW(DischargeTable dt, double w) {
+        logger.warn("TODO: IMPLEMENT ME!");
+        return 5;
+    }
+
+
+    protected double findValueForQ(DischargeTable dt, double q) {
+        logger.warn("TODO: IMPLEMENT ME!");
+        return 10;
+    }
+
+
     /**
      * Writes the parameters used for this calculation to logger.
      */
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java	Wed Feb 01 14:43:50 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java	Wed Feb 01 14:48:00 2012 +0000
@@ -18,6 +18,7 @@
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.HistoricalDischargeFacet;
+import de.intevation.flys.artifacts.model.WQTimerange;
 
 
 /**
@@ -64,14 +65,31 @@
             return res;
         }
 
-        logger.debug("TODO: IMPLEMENT ME!");
+        WQTimerange[] data = (WQTimerange[]) res.getData();
+        if (data == null || data.length == 0) {
+            logger.warn("Historical Discharge calculation has no results!");
+            return res;
+        }
 
-        HistoricalDischargeFacet facet = new HistoricalDischargeFacet(
-            0, HISTORICAL_DISCHARGE_Q, "THE NEW HIST Q FACET DESC");
-
-        facets.add(facet);
+        prepareFacets(facets, data);
 
         return res;
     }
+
+
+    protected void prepareFacets(List<Facet> facets, WQTimerange[] wqts) {
+        int i = 0;
+
+        for (WQTimerange wqt: wqts) {
+            logger.debug("Prepare facet for: " + wqt.getName());
+
+            // TODO CREATE BETTER TITLE FOR FACETS
+
+            facets.add(new HistoricalDischargeFacet(
+                i++,
+                HISTORICAL_DISCHARGE_Q,
+                wqt.getName()));
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org