changeset 2045:d13be39cfd1d

#242 Added header with meta information into CSV exports. flys-artifacts/trunk@3531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 22 Dec 2011 13:22:55 +0000
parents 5d6e82abe859
children 2ae0627f956e
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java flys-artifacts/src/main/resources/messages.properties flys-artifacts/src/main/resources/messages_de.properties flys-artifacts/src/main/resources/messages_de_DE.properties flys-artifacts/src/main/resources/messages_en.properties
diffstat 8 files changed, 290 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Dec 22 13:22:55 2011 +0000
@@ -1,3 +1,24 @@
+2011-12-22  Ingo Weinzierl <ingo@intevation.de>
+
+	flys/issue242 (W-INFO: Fehlende Header in Datenexporten)
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: Added functions
+	  getQs(), getWs(), getGauge(), getGaugename() and getRivername() that all
+	  take a parameter FLYSArtifact.
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Moved the
+	  implementation of getGauge() to FLYSUtils. The getGauge() in this class
+	  just calls and returns FLYSUtils.getGauge().
+
+	* src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: Write a
+	  header into a CSV export containing meta information about this export.
+
+	* src/main/resources/messages.properties,
+	  src/main/resources/messages_de_DE.properties,
+	  src/main/resources/messages_en.properties,
+	  src/main/resources/messages_de.properties: Added strings used in the CSV
+	  export as header.
+
 2011-12-22	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/CrossSectionGenerator.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Thu Dec 22 13:22:55 2011 +0000
@@ -914,32 +914,7 @@
      * @return the gauge.
      */
     public Gauge getGauge() {
-        River river = FLYSUtils.getRiver(this);
-
-        if (river == null) {
-            logger.debug("no river found");
-            return null;
-        }
-
-        double[] dist  = FLYSUtils.getKmRange(this);
-
-        if (dist == null) {
-            logger.debug("no range found");
-            return null;
-        }
-
-        if (logger.isDebugEnabled()) {
-            logger.debug("Determine gauge for:");
-            logger.debug("... river: " + river.getName());
-            logger.debug("... distance: " + dist[0] + " - " + dist[1]);
-        }
-
-        Gauge gauge = river.determineGauge(dist[0], dist[1]);
-
-        String name = gauge != null ? gauge.getName() : "'n/a";
-        logger.debug("Found gauge: " + name);
-
-        return gauge;
+        return FLYSUtils.getGauge(this);
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Thu Dec 22 13:22:55 2011 +0000
@@ -2,9 +2,12 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -15,11 +18,14 @@
 import au.com.bytecode.opencsv.CSVWriter;
 
 import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
 
+import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.WINFOArtifact;
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.WQCKms;
 import de.intevation.flys.artifacts.model.WQKms;
+import de.intevation.flys.artifacts.resources.Resources;
 
 import de.intevation.flys.utils.FLYSUtils;
 import de.intevation.flys.utils.Formatter;
@@ -46,6 +52,31 @@
     public static final String CSV_Q_HEADER =
         "export.waterlevel.csv.header.q";
 
+    public static final String CSV_META_RESULT =
+        "export.waterlevel.csv.meta.result";
+
+    public static final String CSV_META_CREATION =
+        "export.waterlevel.csv.meta.creation";
+
+    public static final String CSV_META_CALCULATIONBASE =
+        "export.waterlevel.csv.meta.calculationbase";
+
+    public static final String CSV_META_RIVER =
+        "export.waterlevel.csv.meta.river";
+
+    public static final String CSV_META_RANGE =
+        "export.waterlevel.csv.meta.range";
+
+    public static final String CSV_META_GAUGE =
+        "export.waterlevel.csv.meta.gauge";
+
+    public static final String CSV_META_Q =
+        "export.waterlevel.csv.meta.q";
+
+    public static final String CSV_META_W =
+        "export.waterlevel.csv.meta.w";
+
+
     public static final Pattern NUMBERS_PATTERN =
         Pattern.compile("\\D*(\\d++.\\d*)\\D*");
 
@@ -147,6 +178,7 @@
     protected void writeCSVData(CSVWriter writer) {
         logger.info("WaterlevelExporter.writeData");
 
+        writeCSVMeta(writer);
         writeCSVHeader(writer);
 
         for (WQKms[] tmp: data) {
@@ -157,6 +189,115 @@
     }
 
 
+    protected void writeCSVMeta(CSVWriter writer) {
+        logger.info("WaterlevelExporter.writeCSVMeta");
+
+        CallMeta meta = context.getMeta();
+
+        FLYSArtifact flys = (FLYSArtifact) master;
+
+        writer.writeNext(new String[] {
+            Resources.getMsg(
+                meta,
+                CSV_META_RESULT,
+                CSV_META_RESULT,
+                new Object[] { FLYSUtils.getRivername(flys) })
+        });
+
+        Locale locale = Resources.getLocale(meta);
+        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+
+        writer.writeNext(new String[] {
+            Resources.getMsg(
+                meta,
+                CSV_META_CREATION,
+                CSV_META_CREATION,
+                new Object[] { df.format(new Date()) })
+        });
+
+        writer.writeNext(new String[] {
+            Resources.getMsg(
+                meta,
+                CSV_META_CALCULATIONBASE,
+                CSV_META_CALCULATIONBASE,
+                new Object[] { "" }) // TODO what is required at this place?
+        });
+
+        writer.writeNext(new String[] {
+            Resources.getMsg(
+                meta,
+                CSV_META_RIVER,
+                CSV_META_RIVER,
+                new Object[] { FLYSUtils.getRivername(flys) })
+        });
+
+        double[] kms = FLYSUtils.getKmRange(flys);
+        writer.writeNext(new String[] {
+            Resources.getMsg(
+                meta,
+                CSV_META_RANGE,
+                CSV_META_RANGE,
+                new Object[] { kms[0], kms[kms.length-1] })
+        });
+
+        writer.writeNext(new String[] {
+            Resources.getMsg(
+                meta,
+                CSV_META_GAUGE,
+                CSV_META_GAUGE,
+                new Object[] { FLYSUtils.getGaugename(flys) })
+        });
+
+        FLYSUtils.WQ_MODE wq = FLYSUtils.getWQMode(flys);
+        if (wq == FLYSUtils.WQ_MODE.QFREE || wq == FLYSUtils.WQ_MODE.QGAUGE) {
+            double[] qs  = FLYSUtils.getQs(flys);
+
+            String lower = "";
+            String upper = "";
+
+            if (qs != null && qs.length > 0) {
+                lower = String.valueOf(qs[0]);
+                upper = String.valueOf(qs[qs.length-1]);
+            }
+            else {
+                logger.warn("Could not determine Q range!");
+            }
+
+            writer.writeNext(new String[] {
+                Resources.getMsg(
+                    meta,
+                    CSV_META_Q,
+                    CSV_META_Q,
+                    new Object[] { lower, upper })
+            });
+        }
+        else {
+            double[] ws = FLYSUtils.getWs(flys);
+
+            String lower = "";
+            String upper = "";
+
+            if (ws != null && ws.length > 0) {
+                lower = String.valueOf(ws[0]);
+                upper = String.valueOf(ws[ws.length-1]);
+            }
+            else {
+                logger.warn("Could not determine W range!");
+            }
+
+            writer.writeNext(new String[] {
+                Resources.getMsg(
+                    meta,
+                    CSV_META_W,
+                    CSV_META_W,
+                    new Object[] { lower, upper })
+            });
+        }
+
+        writer.writeNext(new String[] { "" });
+    }
+
+
     protected void writeCSVHeader(CSVWriter writer) {
         logger.info("WaterlevelExporter.writeCSVHeader");
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Thu Dec 22 13:22:55 2011 +0000
@@ -259,13 +259,57 @@
 
 
     /**
+     * Returns the Qs for a given FLYSArtifact. This method currently accepts
+     * only instances of WINFOArtifact.
+     *
+     * @param flys A FLYSArtifact.
+     *
+     * @return the Qs.
+     */
+    public static double[] getQs(FLYSArtifact flys) {
+        double[] kmRange = getKmRange(flys);
+
+        // XXX this is not nice!
+        if (flys instanceof WINFOArtifact) {
+            return ((WINFOArtifact) flys).getQs(kmRange);
+        }
+
+        logger.warn("This method currently supports WINFOArtifact only!");
+
+        return null;
+    }
+
+
+    /**
+     * Returns the Ws for a given FLYSArtifact. This method currently accepts
+     * only instances of WINFOArtifact.
+     *
+     * @param flys A FLYSArtifact.
+     *
+     * @return the Ws.
+     */
+    public static double[] getWs(FLYSArtifact flys) {
+        double[] kmRange = getKmRange(flys);
+
+        // XXX this is not nice!
+        if (flys instanceof WINFOArtifact) {
+            return ((WINFOArtifact) flys).getWs(kmRange);
+        }
+
+        logger.warn("This method currently supports WINFOArtifact only!");
+
+        return null;
+    }
+
+
+    /**
      * Returns the selected River object based on the 'river' data that might
      * have been inserted by the user.
      *
      * @return the selected River or null if no river has been chosen yet.
      */
     public static River getRiver(FLYSArtifact flys) {
-        String sRiver = flys.getDataAsString("river");
+        String sRiver = getRivername(flys);
 
         return (sRiver != null)
             ? RiverFactory.getRiver(sRiver)
@@ -274,6 +318,19 @@
 
 
     /**
+     * Returns the name of the river specified in the given <i>flys</i>
+     * Artifact.
+     *
+     * @param flys The FLYSArtifact that stores a river relevant information.
+     *
+     * @return the name of the specified river or null.
+     */
+    public static String getRivername(FLYSArtifact flys) {
+        return flys != null ? flys.getDataAsString("river") : null;
+    }
+
+
+    /**
      * Extracts the SRID defined in the global configuration for the river
      * specified in <i>artifact</i>.
      *
@@ -307,6 +364,43 @@
     }
 
 
+    public static Gauge getGauge(FLYSArtifact flys) {
+        River river = getRiver(flys);
+
+        if (river == null) {
+            logger.debug("no river found");
+            return null;
+        }
+
+        double[] dist  = getKmRange(flys);
+
+        if (dist == null) {
+            logger.debug("no range found");
+            return null;
+        }
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("Determine gauge for:");
+            logger.debug("... river: " + river.getName());
+            logger.debug("... distance: " + dist[0] + " - " + dist[1]);
+        }
+
+        Gauge gauge = river.determineGauge(dist[0], dist[1]);
+
+        String name = gauge != null ? gauge.getName() : "'n/a";
+        logger.debug("Found gauge: " + name);
+
+        return gauge;
+    }
+
+
+    public static String getGaugename(FLYSArtifact flys) {
+        Gauge gauge = getGauge(flys);
+
+        return gauge != null ? gauge.getName() : null;
+    }
+
+
     public static String createWspWTitle(
         WINFOArtifact winfo,
         CallContext   cc,
--- a/flys-artifacts/src/main/resources/messages.properties	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/resources/messages.properties	Thu Dec 22 13:22:55 2011 +0000
@@ -74,6 +74,14 @@
 export.waterlevel.csv.header.km = River-Km
 export.waterlevel.csv.header.w = W [NN + m]
 export.waterlevel.csv.header.q = Q [m\u00b3/s]
+export.waterlevel.csv.meta.result = # Calculation Output - {0} - Waterlevel - FLYS 3
+export.waterlevel.csv.meta.creation = # Time of creation: {0}
+export.waterlevel.csv.meta.calculationbase = # Calculation base: {0}
+export.waterlevel.csv.meta.river = # River: {0}
+export.waterlevel.csv.meta.range = # Location/Range (km): {0} - {1}
+export.waterlevel.csv.meta.gauge = # Gauge: {0}
+export.waterlevel.csv.meta.q = # Q (m\u00b3/s): {0} - {1}
+export.waterlevel.csv.meta.w = # W (NN + m): {0} - {1}
 export.computed.discharge.curve.csv.header.w = W [NN + m]
 export.computed.discharge.curve.csv.header.q = Q [m\u00b3/s]
 export.duration.curve.csv.header.duration = D [Days]
--- a/flys-artifacts/src/main/resources/messages_de.properties	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/resources/messages_de.properties	Thu Dec 22 13:22:55 2011 +0000
@@ -74,6 +74,14 @@
 export.waterlevel.csv.header.km = Fluss-Km
 export.waterlevel.csv.header.w = W [NN + m]
 export.waterlevel.csv.header.q = Q [m\u00b3/s]
+export.waterlevel.csv.meta.result = # Ergebnisausgabe - {0} - Wasserstand - FLYS 3
+export.waterlevel.csv.meta.creation = # Datum der Erstellung: {0}
+export.waterlevel.csv.meta.calculationbase = # Berechnungsgrundlage: {0}
+export.waterlevel.csv.meta.river = # Gew\u00e4sser: {0}
+export.waterlevel.csv.meta.range = # Ort/Bereich (km): {0} - {1}
+export.waterlevel.csv.meta.gauge = # Bezugspegel: {0}
+export.waterlevel.csv.meta.q = # Q (m\u00b3/s): {0} - {1}
+export.waterlevel.csv.meta.w = # W (NN + m): {0} - {1}
 export.computed.discharge.curve.csv.header.w = W [NN + m]
 export.computed.discharge.curve.csv.header.q = Q [m\u00b3/s]
 export.duration.curve.csv.header.duration = D [Tagen]
--- a/flys-artifacts/src/main/resources/messages_de_DE.properties	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/resources/messages_de_DE.properties	Thu Dec 22 13:22:55 2011 +0000
@@ -74,6 +74,14 @@
 export.waterlevel.csv.header.km = Fluss-Km
 export.waterlevel.csv.header.w = W [NN + m]
 export.waterlevel.csv.header.q = Q [m\u00b3/s]
+export.waterlevel.csv.meta.result = # Ergebnisausgabe - {0} - Wasserstand - FLYS 3
+export.waterlevel.csv.meta.creation = # Datum der Erstellung: {0}
+export.waterlevel.csv.meta.calculationbase = # Berechnungsgrundlage: {0}
+export.waterlevel.csv.meta.river = # Gew\u00e4sser: {0}
+export.waterlevel.csv.meta.range = # Ort/Bereich (km): {0} - {1}
+export.waterlevel.csv.meta.gauge = # Bezugspegel: {0}
+export.waterlevel.csv.meta.q = # Q (m\u00b3/s): {0} - {1}
+export.waterlevel.csv.meta.w = # W (NN + m): {0} - {1}
 export.computed.discharge.curve.csv.header.w = W [NN + m]
 export.computed.discharge.curve.csv.header.q = Q [m\u00b3/s]
 export.duration.curve.csv.header.duration = D [Tagen]
--- a/flys-artifacts/src/main/resources/messages_en.properties	Thu Dec 22 12:56:46 2011 +0000
+++ b/flys-artifacts/src/main/resources/messages_en.properties	Thu Dec 22 13:22:55 2011 +0000
@@ -70,6 +70,14 @@
 export.waterlevel.csv.header.km = River-Km
 export.waterlevel.csv.header.w = W [NN + m]
 export.waterlevel.csv.header.q = Q [m\u00b3/s]
+export.waterlevel.csv.meta.result = # Calculation Output - {0} - Waterlevel - FLYS 3
+export.waterlevel.csv.meta.creation = # Time of creation: {0}
+export.waterlevel.csv.meta.calculationbase = # Calculation base: {0}
+export.waterlevel.csv.meta.river = # River: {0}
+export.waterlevel.csv.meta.range = # Location/Range (km): {0} - {1}
+export.waterlevel.csv.meta.gauge = # Gauge: {0}
+export.waterlevel.csv.meta.q = # Q (m\u00b3/s): {0} - {1}
+export.waterlevel.csv.meta.w = # W (NN + m): {0} - {1}
 export.computed.discharge.curve.csv.header.w = W [NN + m]
 export.computed.discharge.curve.csv.header.q = Q [m\u00b3/s]
 export.duration.curve.csv.header.duration = D [Days]

http://dive4elements.wald.intevation.org