changeset 299:8940b0885865

Added a DischargeCurveGenerator that creates discharge curves. flys-artifacts/trunk@1648 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 06 Apr 2011 09:56:38 +0000
parents fc694a43a7e5
children 9a0e1289bab6
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/OutGenerator.java
diffstat 3 files changed, 157 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Apr 06 09:53:14 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Apr 06 09:56:38 2011 +0000
@@ -1,3 +1,11 @@
+2011-04-06  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/exports/OutGenerator.java: The
+	  generate() method throws an IOException now.
+
+	* src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java:
+	  New. This OutGenerator creates discharge curves.
+
 2011-04-06  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java	Wed Apr 06 09:56:38 2011 +0000
@@ -0,0 +1,147 @@
+package de.intevation.flys.exports;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+import org.jfree.data.xy.DefaultXYDataset;
+
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.PlotOrientation;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.model.Gauge;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.model.DischargeTables;
+import de.intevation.flys.artifacts.model.GaugesFactory;
+import de.intevation.flys.exports.ChartExportHelper;
+
+
+/**
+ * An OutGenerator that generates discharge curves.
+ *
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class DischargeCurveGenerator implements OutGenerator {
+
+    /** The logger used in this generator.*/
+    private static Logger logger =
+        Logger.getLogger(DischargeCurveGenerator.class);
+
+    /** The document of the incoming out() request.*/
+    protected Document request;
+
+    /** The output stream where the data should be written to.*/
+    protected OutputStream out;
+
+    /** The CallContext object.*/
+    protected CallContext context;
+
+    /** The Dataset that contains the data for the chart.*/
+    protected DefaultXYDataset dataset;
+
+
+    public DischargeCurveGenerator() {
+    }
+
+
+    public void init(Document request, OutputStream out, CallContext context) {
+        logger.debug("DischargeCurveGenerator.init");
+
+        this.request = request;
+        this.out     = out;
+        this.context = context;
+
+        this.dataset = new DefaultXYDataset();
+    }
+
+
+    public void doOut(Artifact artifact, Document attr) {
+        logger.debug("DischargeCurveGenerator.doOut");
+
+        if (!(artifact instanceof FLYSArtifact)) {
+            logger.error("Artifact is no instance of FLYSArtifact.");
+            return;
+        }
+
+        FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
+
+        String river = (String) flysArtifact.getData("river").getValue();
+        String from  = (String) flysArtifact.getData("ld_from").getValue();
+        String to    = (String) flysArtifact.getData("ld_to").getValue();
+
+        double f = Double.parseDouble(from);
+        double t = Double.parseDouble(to);
+
+        List<double[]> ranges = new ArrayList<double[]>();
+        ranges.add(new double [] { f, t });
+
+        List<Gauge> gauges   = GaugesFactory.getGauges(river);
+        List<Gauge> filtered = GaugesFactory.filterRanges(gauges, ranges);
+
+        if (logger.isDebugEnabled()) {
+            int numGauges   = gauges != null ? gauges.size() : 0;
+            int numFiltered = filtered != null ? filtered.size() : 0;
+
+            logger.debug("++++++++++++++++++++");
+            logger.debug("Search gauges for river: " + river);
+            logger.debug("-> ... from range: " + from);
+            logger.debug("-> ... to range: " + to);
+            logger.debug("-> Found " + numGauges + " gauges in total.");
+            logger.debug("-> Found " + numFiltered + " gauges after filtering.");
+            logger.debug("++++++++++++++++++++");
+        }
+
+        String[] gaugeNames = new String[filtered.size()];
+        int idx = 0;
+        for (Gauge gauge: filtered) {
+            gaugeNames[idx++] = gauge.getName();
+        }
+
+        DischargeTables dt = new DischargeTables(river, gaugeNames);
+        Map<String, double[][]> gaugeValues = dt.getValues(100d);
+
+        for (String gauge: gaugeNames) {
+            double[][] values = gaugeValues.get(gauge);
+            dataset.addSeries(gauge, values);
+        }
+
+    }
+
+
+    public void generate()
+    throws IOException
+    {
+        logger.debug("DischargeCurveGenerator.generate");
+
+        JFreeChart chart = ChartFactory.createXYLineChart(
+            "Abflusskurven",
+            "Q", "W",
+            dataset,
+            PlotOrientation.VERTICAL,
+            true,
+            false,
+            false);
+
+        chart.setBackgroundPaint(Color.WHITE);
+        chart.getPlot().setBackgroundPaint(Color.WHITE);
+
+        ChartExportHelper.exportImage(
+            out,
+            chart,
+            "png",
+            600, 400);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/OutGenerator.java	Wed Apr 06 09:53:14 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/OutGenerator.java	Wed Apr 06 09:56:38 2011 +0000
@@ -1,5 +1,6 @@
 package de.intevation.flys.exports;
 
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.w3c.dom.Document;
@@ -42,6 +43,6 @@
      * <i>request</i> (see init()) document to the OutputStream <i>out</i> (see
      * init()).
      */
-    void generate();
+    void generate() throws IOException;
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org