changeset 1161:9bdf738abbad

Added CSV Exporter for WDifferences. flys-artifacts/trunk@2697 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 12 Sep 2011 09:06:05 +0000 (2011-09-12)
parents efe1b8545f5c
children 588798ef4791
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesExporter.java
diffstat 5 files changed, 184 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Sep 12 09:00:08 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Sep 12 09:06:05 2011 +0000
@@ -1,3 +1,18 @@
+2011-09-12  Felix Wolfsteller <felix.wolfsteller@intevation.de>
+
+ Added CSV Export for W-Differences.
+
+	* doc/conf/conf.xml: Added Exporter.
+	
+	* doc/conf/artifacts/winfo.xml: Removed transition over distances
+	  state, added export outputmode and facet.
+
+	* src/main/java/de/intevation/flys/exports/WDifferencesExporter.java:
+	  New, CSV-Exporter for WDifferences.
+
+	* src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java:
+	  Add export facet.
+
 2011-09-12  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/DataFacet.java:
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Mon Sep 12 09:00:08 2011 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Mon Sep 12 09:06:05 2011 +0000
@@ -41,7 +41,7 @@
 
         <transition transition="de.intevation.flys.artifacts.transitions.ValueCompareTransition">
             <from state="state.winfo.calculation_mode"/>
-            <to state="state.winfo.distance"/>
+            <to state="state.winfo.w_differences"/>
             <condition data="calculation_mode" value="calc.w.differences" operator="equal"/>
         </transition>
 
@@ -89,12 +89,6 @@
             </outputmodes>
         </state>
 
-        <transition transition="de.intevation.flys.artifacts.transitions.ValueCompareTransition">
-            <from state="state.winfo.distance"/>
-            <to state="state.winfo.w_differences"/>
-            <condition data="calculation_mode" value="calc.w.differences" operator="equal"/>
-        </transition>
-
         <transition transition="de.intevation.flys.artifacts.transitions.DefaultTransition">
             <from state="state.winfo.distance"/>
             <to state="state.winfo.wq_adapted"/>
@@ -135,7 +129,8 @@
         </state>
 
         <state id="state.winfo.w_differences" description="state.winfo.w_differences" state="de.intevation.flys.artifacts.states.WDifferencesState">
-            <data name="uuid" type="String" />
+            <data name="uuid1" type="String" />
+            <data name="uuid2" type="String" />
 
             <outputmodes>
                 <outputmode name="w_differences" description="output.w_differences" mime-type="image/png" type="chart">
@@ -143,6 +138,9 @@
                         <facet name="w_differences" description="facet.w_differences"/>
                     </facets>
                 </outputmode>
+                <outputmode name="w_differences_export" description="output.w_differences_export" mime-type="text/plain" type="export">
+                        <facet name="csv" description="facet.w_differences.csv" />
+                </outputmode>
             </outputmodes>
         </state>
 
--- a/flys-artifacts/doc/conf/conf.xml	Mon Sep 12 09:00:08 2011 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Mon Sep 12 09:06:05 2011 +0000
@@ -103,6 +103,7 @@
         <output-generator name="discharge_longitudinal_section_export">de.intevation.flys.exports.DischargeLongitudinalSectionExporter</output-generator>
         <output-generator name="w_differences">de.intevation.flys.exports.WDifferencesCurveGenerator</output-generator>
         <output-generator name="w_differences_chartinfo">de.intevation.flys.exports.WDifferencesCurveInfoGenerator</output-generator>
+        <output-generator name="w_differences_export">de.intevation.flys.exports.WDifferencesExporter</output-generator>
         <!-- Error report generators. -->
         <output-generator name="discharge_longitudinal_section_report">de.intevation.flys.exports.ReportGenerator</output-generator>
         <output-generator name="waterlevel_report">de.intevation.flys.exports.ReportGenerator</output-generator>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java	Mon Sep 12 09:00:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java	Mon Sep 12 09:06:05 2011 +0000
@@ -85,6 +85,7 @@
             // TODO: pass computetype and state id.
             //, ComputeType.ADVANCE, getID(), hash));
             facets.add(new DataFacet(W_DIFFERENCES, facetName));
+            facets.add(new DataFacet(CSV, "CSV data"));
         }
         else {
             logger.debug("Not adding facets in WDifferencesState.");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesExporter.java	Mon Sep 12 09:06:05 2011 +0000
@@ -0,0 +1,161 @@
+package de.intevation.flys.exports;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.w3c.dom.Document;
+
+import org.apache.log4j.Logger;
+
+import au.com.bytecode.opencsv.CSVWriter;
+
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.artifacts.model.CalculationResult;
+import de.intevation.flys.artifacts.model.WKms;
+
+import de.intevation.flys.utils.Formatter;
+
+/**
+ * (CSV)Exporter for WDifferences.
+ */
+public class WDifferencesExporter extends AbstractExporter {
+
+    /** The logger used in this exporter. */
+    private static Logger logger = Logger.getLogger(WDifferencesExporter.class);
+
+
+    public static final String CSV_KM_HEADER =
+        "export.w_differences.csv.header.km";
+
+    public static final String CSV_W_HEADER =
+        "export.w_differences.csv.header.w";
+
+    public static final String DEFAULT_CSV_KM_HEADER = "Fluss-Km";
+    public static final String DEFAULT_CSV_W_HEADER  = "W [NN + m]";
+
+
+    /** The storage that contains all WKms objects for the different facets. */
+    protected List<WKms[]> data;
+
+
+    public void init(Document request, OutputStream out, CallContext context) {
+        logger.debug("WDifferencesExporter.init");
+
+        super.init(request, out, context);
+
+        this.data = new ArrayList<WKms[]>();
+    }
+
+
+    /**
+     * Genereate data in csv format.
+     */
+    @Override
+    public void generate()
+    throws IOException
+    {
+        logger.debug("WDifferencesExporter.generate");
+
+        if (facet == null) {
+            throw new IOException("invalid (null) facet for exporter");
+        }
+        else if (facet.equals(AbstractExporter.FACET_CSV)) {
+            generateCSV();
+        }
+        else {
+            throw new IOException("invalid facet (" + facet + ") for exporter");
+        }
+    }
+
+
+    /**
+     * Adds given data.
+     * @param d either a WKms or a CalculationResult to add to data.
+     */
+    @Override
+    protected void addData(Object d) {
+        if (d instanceof CalculationResult) {
+            d = ((CalculationResult)d).getData();
+            if (d instanceof WKms []) {
+                data.add((WKms [])d);
+            }
+        }
+        else if (d instanceof WKms) {
+            data.add(new WKms[] { (WKms) d });
+        }
+    }
+
+
+    /**
+     * Lets writer write all data (including header).
+     * @param writer Writer to write data with.
+     */
+    @Override
+    protected void writeCSVData(CSVWriter writer) {
+        logger.info("WDifferencesExporter.writeData");
+
+        writeCSVHeader(writer);
+
+        for (WKms[] tmp: data) {
+            for (WKms wkms: tmp) {
+                wKms2CSV(writer, wkms);
+            }
+        }
+    }
+
+
+    /**
+     * Lets csvwriter write the header (first line in file).
+     * @param write Writer to write header with.
+     */
+    protected void writeCSVHeader(CSVWriter writer) {
+        logger.info("WDifferencesExporter.writeCSVHeader");
+
+        writer.writeNext(new String[] {
+            msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER),
+            msg(CSV_W_HEADER, DEFAULT_CSV_W_HEADER)
+        });
+    }
+
+
+    protected void wKms2CSV(CSVWriter writer, WKms wkms) {
+        logger.debug("WDifferencesExporter.wQKms2CSV");
+
+        NumberFormat kmf  = getKmFormatter();
+        NumberFormat wf   = getWFormatter();
+        int          size = wkms.size();
+
+        for (int i = 0; i < size; i ++) {
+
+            writer.writeNext(new String[] {
+                kmf.format(wkms.getKm(i)),
+                wf.format(wkms.getW(i))
+            });
+        }
+    }
+
+
+    /**
+     * Returns the number formatter for kilometer values.
+     *
+     * @return the number formatter for kilometer values.
+     */
+    protected NumberFormat getKmFormatter() {
+        return Formatter.getWaterlevelKM(context);
+    }
+
+
+    /**
+     * Returns the number formatter for W values.
+     *
+     * @return the number formatter for W values.
+     */
+    protected NumberFormat getWFormatter() {
+        return Formatter.getWaterlevelW(context);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org