changeset 3217:79dd823733e2

FixA: Added parameter exporter. flys-artifacts/trunk@4840 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 30 Jun 2012 22:02:37 +0000
parents a46910e61dfb
children e0e9a2629d7d
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/fixanalysis.xml flys-artifacts/doc/conf/conf.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/fixation/FixationCompute.java flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/DeltaWtExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/ParametersExporter.java
diffstat 9 files changed, 223 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/ChangeLog	Sat Jun 30 22:02:37 2012 +0000
@@ -1,3 +1,32 @@
+2012-06-30	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	Added parameter exporter for fixings analysis.
+
+	* src/main/java/de/intevation/flys/exports/fixings/ParametersExporter.java:
+	  New. Exports the contents of the parameters of the fix analysis.
+
+	* src/main/java/de/intevation/flys/artifacts/model/Parameters.java:
+	  Added visitor interface to visit all rows of the parameters.
+
+	* doc/conf/artifacts/fixanalysis.xml: Added new facet for
+	  parameter export.
+
+	* doc/conf/conf.xml: Register the new parameter exporter.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java:
+	  Added facet type.
+
+	* src/main/java/de/intevation/flys/artifacts/states/fixation/FixationCompute.java:
+	  Fixed indentation.
+	  Removed some programming oddities.
+	  Create facet for parameter export.
+
+	* src/main/java/de/intevation/flys/exports/fixings/DeltaWtExporter.java:
+	  Removed code which is already contained in the base class.
+
+	* src/main/java/de/intevation/flys/exports/AbstractExporter.java:
+	  Cleaned up code.
+
 2012-06-30	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/DeltaWtExporter.java:
@@ -7,6 +36,7 @@
 	  Re-added here
 
 	* doc/conf/conf.xml: Adjusted package name of DeltaWtExporter.
+
 2012-06-29	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/fixings/FixDerivedCurveInfoGenerator.java:
--- a/flys-artifacts/doc/conf/artifacts/fixanalysis.xml	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/doc/conf/artifacts/fixanalysis.xml	Sat Jun 30 22:02:37 2012 +0000
@@ -126,7 +126,12 @@
             <outputmodes>
                 <outputmode name="fix_deltawt_export" description="output.fix_deltawt_export" mine-type="text/plain" type="export">
                     <facets>
-                        <facet name="csv" description="face.fix_deltawt.csv" />
+                        <facet name="csv" description="facet.fix_deltawt.csv" />
+                    </facets>
+                </outputmode>
+                <outputmode name="fix_parameters_export" description="output.fix_parameters_export" mine-type="text/plain" type="export">
+                    <facets>
+                        <facet name="fix_parameters" description="facet.fix_parameters.csv" />
                     </facets>
                 </outputmode>
                 <outputmode name="fix_wq_curve" description="output.fix_wq_curve" mine-type="image/png" type="chart">
--- a/flys-artifacts/doc/conf/conf.xml	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/doc/conf/conf.xml	Sat Jun 30 22:02:37 2012 +0000
@@ -264,6 +264,7 @@
         <output-generator name="sq_relation_e_chartinfo">de.intevation.flys.exports.sq.SQRelationInfoGenerator</output-generator>
         <output-generator name="sq_relation_f_chartinfo">de.intevation.flys.exports.sq.SQRelationInfoGenerator</output-generator>
         <output-generator name="sq_relation_export">de.intevation.flys.exports.SQRelationExporter</output-generator>
+        <output-generator name="fix_parameters_export">de.intevation.flys.exports.fixings.ParametersExporter</output-generator>
         <output-generator name="fix_deltawt_export">de.intevation.flys.exports.fixings.DeltaWtExporter</output-generator>
         <output-generator name="fix_deltawt_curve">de.intevation.flys.exports.fixings.FixDeltaWtGenerator</output-generator>
         <output-generator name="fix_longitudinal_section_curve">de.intevation.flys.exports.fixings.FixLongitudinalSectionGenerator</output-generator>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java	Sat Jun 30 22:02:37 2012 +0000
@@ -250,5 +250,6 @@
     String FIX_DEVIATION_DWT = "fix_deviation_dwt";
     String FIX_DEVIATION_LS = "fix_deviation_ls";
 
+    String FIX_PARAMETERS = "fix_parameters";
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Sat Jun 30 22:02:37 2012 +0000
@@ -13,6 +13,12 @@
 {
     private static Logger log = Logger.getLogger(Parameters.class);
 
+    public interface Visitor {
+
+        void visit(double [] row);
+
+    } // interface Visitor
+
     public static final double EPSILON = 1e-4;
 
     protected String []           columnNames;
@@ -290,5 +296,18 @@
         }
         return true;
     }
+
+    public void visit(Visitor visitor) {
+        visit(visitor, new double[columns.length]);
+    }
+
+    public void visit(Visitor visitor, double [] data) {
+        for (int i = 0, R = size(); i < R; ++i) {
+            for (int j = 0; j < data.length; ++j) {
+                data[j] = columns[j].getQuick(i);
+            }
+            visitor.visit(data);
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/fixation/FixationCompute.java	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/fixation/FixationCompute.java	Sat Jun 30 22:02:37 2012 +0000
@@ -45,7 +45,6 @@
 extends      DefaultState
 implements   FacetTypes
 {
-
     /** The log used in this class. */
     private static Logger log = Logger.getLogger(FixationCompute.class);
 
@@ -63,24 +62,27 @@
 
     private static final String I18N_REFERENCEDEVIATION = "fix.reference.deviation";
 
-    public static class IdGenerator {
-	private int id;
-
-	public IdGenerator() {
-	}
+    public static final String [] SECTOR_LABELS = {
+        "[0 - (MNQ+MQ)/2)",
+        "[(MNQ+MQ)/2 - (MQ+MHQ)/2)",
+        "[(MQ+MHQ)/2 - HQ5)",
+        "[HQ5 - \u221e)"
+    };
 
-	public IdGenerator(int id) {
-	    this.id = id;
-	}
+    public static class IdGenerator {
+        private int id;
 
-	public int next() {
-	    return id++;
-	}
+        public IdGenerator() {
+        }
 
-	public int next(int s) {
-	    return id += s;
-	}
-    }
+        public IdGenerator(int id) {
+            this.id = id;
+        }
+
+        public int next() {
+            return id++;
+        }
+    } // class IdGenerator
 
     /**
      * The default constructor that initializes an empty State object.
@@ -98,7 +100,6 @@
     ) {
         log.debug("FixationCompute.computeAdvance");
 
-
         CalculationResult res;
 
         FixationArtifactAccess access =
@@ -128,64 +129,69 @@
 
         facets.add(
             new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, hash, id));
+        facets.add(
+            new DataFacet(
+                FIX_PARAMETERS, "parameters", ComputeType.ADVANCE, hash, id));
 
         int maxId = -100;
 
-        int count = access.getAnalysisPeriods().length;
-
         int sectorMask = fr.getUsedSectorsInAnalysisPeriods();
 
-        for (int i = 0; i < count; i++) {
-            DateRange period = access.getAnalysisPeriods()[i];
-            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
+        int qsS = access.getQSectorStart();
+        int qsE = access.getQSectorEnd();
+
+        // TODO: i18n
+        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
+
+        DateRange [] periods = access.getAnalysisPeriods();
+
+        for (int i = 0; i < periods.length; i++) {
+            DateRange period = periods[i];
             String startDate = df.format(period.getFrom());
             String endDate = df.format(period.getTo());
 
-            for (int j = access.getQSectorStart().intValue();
-                 j <= access.getQSectorEnd().intValue();
-                 j++) {
+            for (int j = qsS; j <= qsE; j++) {
 
-                // Only emit facet for sectors that really have data.
+                // Only emit facets for sectors that really have data.
                 if ((sectorMask & (1 << j)) == 0) {
                     continue;
                 }
 
-                String sector = "";
-                switch (j) {
-                    case 0: sector = "[0 - (MNQ+MQ)/2)"; break;
-                    case 1: sector = "[(MNQ+MQ)/2 - (MQ+MHQ)/2)"; break;
-                    case 2: sector = "[(MQ+MHQ)/2 - HQ5)"; break;
-                    case 3: sector = "[HQ5 - \u221e)"; break;
-                }
+                String sector = SECTOR_LABELS[j];
                 String description = sector + ": " +
                                      startDate + " - " +
                                      endDate;
 
-                int sectorNdx = j - access.getQSectorStart().intValue();
+                int sectorNdx = j - qsS;
                 int facetNdx = i << 2;
                 facetNdx = facetNdx | j;
 
-		if (facetNdx > maxId)
-		    maxId = facetNdx;
+                if (facetNdx > maxId) {
+                    maxId = facetNdx;
+                }
 
                 facets.add(
-                    new FixAvSectorFacet(facetNdx,
-                                         FIX_SECTOR_AVERAGE_DWT + "_" + sectorNdx,
-                                         description));
+                    new FixAvSectorFacet(
+                        facetNdx,
+                        FIX_SECTOR_AVERAGE_DWT + "_" + sectorNdx,
+                        description));
                 facets.add(
-                    new FixLongitudinalAnalysisFacet(facetNdx,
-                                         FIX_SECTOR_AVERAGE_LS + "_" + sectorNdx,
-                                         description));
+                    new FixLongitudinalAnalysisFacet(
+                        facetNdx,
+                        FIX_SECTOR_AVERAGE_LS + "_" + sectorNdx,
+                        description));
                 // TODO: i18n
                 String dev = "Abweichung: " + description;
                 facets.add(
-                    new FixLongitudinalAnalysisFacet(facetNdx,
-                                         FIX_SECTOR_AVERAGE_LS_DEVIATION + "_" + sectorNdx,
-                                         dev));
+                    new FixLongitudinalAnalysisFacet(
+                        facetNdx,
+                        FIX_SECTOR_AVERAGE_LS_DEVIATION + "_" + sectorNdx,
+                        dev));
                 facets.add(
-                    new FixAvSectorFacet(facetNdx,
-                                         FIX_SECTOR_AVERAGE_WQ + "_" + sectorNdx,
-                                         description));
+                    new FixAvSectorFacet(
+                        facetNdx,
+                        FIX_SECTOR_AVERAGE_WQ + "_" + sectorNdx,
+                        description));
 
             }
 
@@ -205,7 +211,7 @@
                 eventDesc));
         }
 
-	IdGenerator idg = new IdGenerator(maxId + 1);
+        IdGenerator idg = new IdGenerator(maxId + 1);
 
         String i18n_ref = Resources.getMsg(context.getMeta(),
                 I18N_REFERENCEPERIOD,
@@ -248,20 +254,24 @@
         Boolean preprocessing = access.getPreprocessing();
 
         if (preprocessing != null && preprocessing) {
-            facets.add(new FixOutlierFacet(idg.next(), FIX_OUTLIER,
-                Resources.getMsg(context.getMeta(), I18N_OUTLIER, I18N_OUTLIER)));
+            facets.add(new FixOutlierFacet(
+                idg.next(),
+                FIX_OUTLIER,
+                Resources.getMsg(
+                    context.getMeta(), I18N_OUTLIER, I18N_OUTLIER)));
         }
 
         facets.add(new FixDerivateFacet(
-	    idg.next(),
-	    FIX_DERIVATE,
-            Resources.getMsg(context.getMeta(),
+            idg.next(),
+            FIX_DERIVATE,
+            Resources.getMsg(
+                context.getMeta(),
                 I18N_DERIVATIVE,
                 I18N_DERIVATIVE)));
 
         facets.add(new FixDeviationFacet(
-	    idg.next(),
-	    FIX_DEVIATION_DWT,
+            idg.next(),
+            FIX_DEVIATION_DWT,
             Resources.getMsg(context.getMeta(),
                 I18N_DEVIATION,
                 I18N_DEVIATION)));
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Sat Jun 30 22:02:37 2012 +0000
@@ -151,10 +151,14 @@
     {
         logger.debug("AbstractExporter.generate");
 
-        if (facet != null && facet.equals(FACET_CSV)) {
+        if (facet == null) {
+            throw new IOException("invalid (null) facet for exporter");
+        }
+
+        if (facet.equals(FACET_CSV)) {
             generateCSV();
         }
-        else if (facet != null && facet.equals(FACET_PDF)) {
+        else if (facet.equals(FACET_PDF)) {
             generatePDF();
         }
         else {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/DeltaWtExporter.java	Sat Jun 30 19:44:39 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/DeltaWtExporter.java	Sat Jun 30 22:02:37 2012 +0000
@@ -71,23 +71,6 @@
     }
 
     @Override
-    public void generate() throws IOException {
-        log.debug("DeltaWtExporter.generate");
-        if (facet == null) {
-            throw new IOException("invalid (null) facet for exporter");
-        }
-        else if (facet.equals(FACET_CSV)) {
-            generateCSV();
-        }
-        else if (facet.equals(FACET_PDF)) {
-            generatePDF();
-        }
-        else {
-            throw new IOException("invalid facet (" + facet + ") for exporter");
-        }
-    }
-
-    @Override
     protected void addData(Object d) {
         log.debug("DeltaWtExporter.addData");
         if (!(d instanceof CalculationResult)) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/ParametersExporter.java	Sat Jun 30 22:02:37 2012 +0000
@@ -0,0 +1,94 @@
+package de.intevation.flys.exports.fixings;
+
+import au.com.bytecode.opencsv.CSVWriter;
+
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.artifacts.model.CalculationResult;
+import de.intevation.flys.artifacts.model.Parameters;
+
+import de.intevation.flys.artifacts.model.fixings.FixResult;
+
+import de.intevation.flys.artifacts.resources.Resources;
+
+import de.intevation.flys.exports.AbstractExporter;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import java.text.NumberFormat;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+public class ParametersExporter
+extends      AbstractExporter
+{
+    private static Logger log = Logger.getLogger(DeltaWtExporter.class);
+
+    protected List<Parameters> parametersList;
+
+    @Override
+    public void init(Document request, OutputStream out, CallContext context) {
+        log.debug("ParametersExporter.init");
+        super.init(request, out, context);
+        parametersList = new ArrayList<Parameters>();
+    }
+
+    @Override
+    protected void addData(Object d) {
+        log.debug("ParametersExporter.addData");
+        if (!(d instanceof CalculationResult)) {
+            log.warn("Invalid data type");
+            return;
+        }
+
+        Object data = ((CalculationResult)d).getData();
+        if (!(data instanceof FixResult)) {
+            log.warn("Invalid data stored in result.");
+        }
+        FixResult result = (FixResult)data;
+        parametersList.add(result.getParameters());
+    }
+
+    @Override
+    protected void writeCSVData(final CSVWriter writer) throws IOException {
+
+        if (parametersList.isEmpty()) {
+            return;
+        }
+
+        Parameters parameters = parametersList.get(0);
+        writer.writeNext(parameters.getColumnNames());
+
+        final int numColumns = parameters.getNumberColumns();
+
+        parameters.visit(new Parameters.Visitor() {
+
+            String [] row = new String[numColumns];
+
+            NumberFormat format = NumberFormat.getInstance(
+                Resources.getLocale(context.getMeta()));
+
+            @Override
+            public void visit(double [] data) {
+                for (int i = 0; i < data.length; ++i) {
+                    row[i] = format.format(data[i]);
+                }
+                writer.writeNext(row);
+            }
+        }, new double[numColumns]);
+
+        writer.flush();
+    }
+
+    @Override
+    protected void writePDF(OutputStream out) {
+        // TODO: Implement me!
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org