changeset 3392:56f62b5209f5

S/Q relation: Write parameters as export result. flys-artifacts/trunk@5020 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 16 Jul 2012 18:26:00 +0000
parents 2b3c4abe034f
children 525e976102ac
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQResult.java flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationExporter.java
diffstat 6 files changed, 106 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Jul 16 10:12:14 2012 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jul 16 18:26:00 2012 +0000
@@ -1,3 +1,20 @@
+2012-07-16	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/Parameters.java:
+	  Added getValue() method.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java:
+	  Store location of calculation in result, too.
+	  
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java:
+	  Added method getTotalCount() to return the number of used S/Q pairs.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQResult.java:
+	  Store location of result, too.
+
+	* src/main/java/de/intevation/flys/exports/sq/SQRelationExporter.java:
+	  Write parameters as export result.
+
 2012-07-16	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jul 16 10:12:14 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jul 16 18:26:00 2012 +0000
@@ -121,6 +121,13 @@
         return indices;
     }
 
+    public double getValue(int row, String column) {
+        int idx = columnIndex(column);
+        return idx >= 0
+            ? columns[idx].getQuick(row)
+            : Double.NaN;
+    }
+
     public double [] get(int row, String [] columns) {
         return get(row, columns, new double[columns.length]);
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Mon Jul 16 10:12:14 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Mon Jul 16 18:26:00 2012 +0000
@@ -59,7 +59,9 @@
 
 
     public int getOutliersCount() {
-        return outliers == null ? 0 : outliers.size();
+        return outliers != null
+            ? outliers.size()
+            : 0;
     }
 
 
@@ -75,6 +77,11 @@
         this.outliers.add(outliers);
     }
 
+    public int getTotalCount() {
+        return measurements != null
+            ? measurements.length
+            : 0;
+    }
 
     public double getMinQ() {
         double min = Double.MAX_VALUE;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Mon Jul 16 10:12:14 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Mon Jul 16 18:26:00 2012 +0000
@@ -148,7 +148,7 @@
                 fitting.reset();
                 fractionResults[i] = fractionResult;
             }
-            results.add(new SQResult(fractionResults));
+            results.add(new SQResult(location, fractionResults));
         }
 
         return new CalculationResult(
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQResult.java	Mon Jul 16 10:12:14 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQResult.java	Mon Jul 16 18:26:00 2012 +0000
@@ -15,13 +15,15 @@
     public static final int FRACTION_E = 4;
     public static final int FRACTION_F = 5;
 
+    protected double km;
     protected SQFractionResult[] fractions;
 
     public SQResult() {
-        this(new SQFractionResult[NUMBER_FRACTIONS]);
+        this(0d, new SQFractionResult[NUMBER_FRACTIONS]);
     }
 
-    public SQResult(SQFractionResult [] fractions) {
+    public SQResult(double km, SQFractionResult [] fractions) {
+        this.km        = km;
         this.fractions = fractions;
     }
 
@@ -36,5 +38,23 @@
             this.fractions[idx] = fraction;
         }
     }
+
+    public static final String [] FRACTION_NAMES = {
+        "A", "B", "C", "D", "E", "F"
+    };
+
+    public String getFractionName(int idx) {
+        return idx >= 0 && idx < FRACTION_NAMES.length
+            ? FRACTION_NAMES[idx]
+            : "";
+    }
+
+    public double getKm() {
+        return km;
+    }
+
+    public void setKm(double km) {
+        this.km = km;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationExporter.java	Mon Jul 16 10:12:14 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationExporter.java	Mon Jul 16 18:26:00 2012 +0000
@@ -6,15 +6,19 @@
 
 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.sq.SQFractionResult;
+import de.intevation.flys.artifacts.model.sq.SQResult;
+import de.intevation.flys.artifacts.model.Parameters;
+
 import de.intevation.flys.exports.AbstractExporter;
 
+import org.apache.log4j.Logger;
 
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
@@ -69,67 +73,77 @@
         "export.sqrelation.csv.header.variance";
 
 
-    // TODO use correct model class here!
-    protected List<Object[]> data;
+    protected List<SQResult []> data;
 
 
     public void init(Document request, OutputStream out, CallContext cc) {
         super.init(request, out, cc);
-        data = new ArrayList<Object[]>();
+        data = new ArrayList<SQResult []>();
     }
 
 
     @Override
     protected void addData(Object d) {
         if (d instanceof CalculationResult) {
-            d = ((CalculationResult) d).getData();
-
-            if (d instanceof Object[]) {
-                logger.debug("Add new data of type XXX");
-                data.add((Object[]) d);
+            d = ((CalculationResult)d).getData();
+            if (d instanceof SQResult []) {
+                data.add((SQResult [])d);
             }
         }
     }
 
+    protected void writeCSVHeader(CSVWriter writer) {
+        writer.writeNext(new String[] {
+            msg(CSV_KM        , CSV_KM),
+            msg(CSV_PARAMETER,  CSV_PARAMETER),
+            msg(CSV_COEFF_A   , CSV_COEFF_A),
+            msg(CSV_COEFF_B   , CSV_COEFF_B),
+            msg(CSV_N_TOTAL   , CSV_N_TOTAL),
+            msg(CSV_N_OUTLIERS, CSV_N_OUTLIERS),
+            msg(CSV_VARIANCE  , CSV_VARIANCE)
+        });
+    }
 
     @Override
     protected void writeCSVData(CSVWriter writer) {
         logger.debug("writeCSVData");
-        logger.error("NOT IMPLEMENTED: writeCSVData");
-
-        logger.debug("CSV gets " + data.size() + " rows.");
 
         writeCSVHeader(writer);
 
-        for (Object[] d: data) {
-            data2CSV(writer, d);
+        for (SQResult [] results: data) {
+            for (SQResult result: results) {
+                data2CSV(writer, result);
+            }
         }
     }
 
+    protected void data2CSV(CSVWriter writer, SQResult result) {
+        logger.debug("data2CSV");
 
-    protected void writeCSVHeader(CSVWriter writer) {
-        writer.writeNext(new String[] {
-            msg(CSV_PARAMETER, CSV_PARAMETER),
-            msg(CSV_STATION, CSV_STATION),
-            msg(CSV_KM        , CSV_KM),
-            msg(CSV_FUNCTION  , CSV_FUNCTION),
-            msg(CSV_GAUGE     , CSV_GAUGE),
-            msg(CSV_COEFF_A   , CSV_COEFF_A),
-            msg(CSV_COEFF_B   , CSV_COEFF_B),
-            msg(CSV_COEFF_Q   , CSV_COEFF_Q),
-            msg(CSV_COEFF_R   , CSV_COEFF_R),
-            msg(CSV_N_TOTAL   , CSV_N_TOTAL),
-            msg(CSV_N_OUTLIERS, CSV_N_OUTLIERS),
-            msg(CSV_C_DUAN    , CSV_C_DUAN),
-            msg(CSV_C_FERGUSON, CSV_C_FERGUSON),
-            msg(CSV_VARIANCE  , CSV_VARIANCE)
-        });
-    }
+        String km = String.valueOf(result.getKm());
 
-
-    protected void data2CSV(CSVWriter writer, Object[] mData) {
-        logger.debug("data2CSV");
-        logger.error("NOT IMPLEMENTED: data2CSV");
+        for (int i = 0; i < SQResult.NUMBER_FRACTIONS; ++i) {
+            SQFractionResult fraction = result.getFraction(i);
+            if (!fraction.isValid()) {
+                continue;
+            }
+            String name = result.getFractionName(i);
+            Parameters parameters = fraction.getParameters();
+            double a  = parameters.getValue(0, "a");
+            double b  = parameters.getValue(0, "b");
+            double sd = Math.sqrt(parameters.getValue(0, "std_dev"));
+            int    t  = fraction.getTotalCount();
+            int    o  = fraction.getOutliersCount();
+            writer.writeNext(new String[] {
+                km,
+                name,
+                String.valueOf(a),
+                String.valueOf(b),
+                String.valueOf(t),
+                String.valueOf(o),
+                String.valueOf(sd)
+            });
+        }
     }
 
 

http://dive4elements.wald.intevation.org