changeset 3105:9592b7d76633

Generate fake data for SQ curves; Implemented the Facet and Generator stuff. flys-artifacts/trunk@4704 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 19 Jun 2012 13:55:14 +0000
parents 6391ca82141c
children 23de6d678fba
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/SQCurveFacet.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/SQFunction.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/SQRelationGenerator.java flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java
diffstat 9 files changed, 206 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/ChangeLog	Tue Jun 19 13:55:14 2012 +0000
@@ -1,3 +1,30 @@
+2012-06-19  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/jfree/JFreeUtil.java: New function to
+	  sample a Function2D. This method is used, because JFreeChart's
+	  DataUtilities.sampleFunction2D() returns an instance of XYSeries but we
+	  need StyledXYSeries.
+
+	* src/main/java/de/intevation/flys/artifacts/model/Parameters.java: Added a
+	  method to retrieve parameter values for a list of parameter names.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java:
+	  Create a fake Parameters object for the SQ curve.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java:
+	  Added getMinQ() and getMaxQ() to determine the Q range.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQFunction.java: New
+	  model class that stores a Function and a min and max Q value.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java:
+	  Return a SQFunction instance based on the parameters and Q range of the
+	  SQFractionResult.
+
+	* src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java: Use
+	  JFreeUtil's new function sampleFunction2D to create a StyledXYSeries for
+	  the SQ curve.
+
 2012-06-19	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/math/fitting/SQPow.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Tue Jun 19 13:55:14 2012 +0000
@@ -115,6 +115,21 @@
         return indices;
     }
 
+    public double [] get(int row, String [] columns) {
+        return get(row, columns, new double[columns.length]);
+    }
+
+    public double [] get(int row, String [] columns, double [] values) {
+        for (int i = 0; i < columns.length; ++i) {
+            int idx = columnIndex(columns[i]);
+            values[i] = idx < 0
+                ? Double.NaN
+                : this.columns[idx].getQuick(row);
+        }
+
+        return values;
+    }
+
     public void get(int row, int [] columnIndices, double [] values) {
         for (int i = 0; i < columnIndices.length; ++i) {
             int index = columnIndices[i];
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java	Tue Jun 19 13:55:14 2012 +0000
@@ -5,9 +5,13 @@
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
-
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.math.fitting.Function;
+import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
+import de.intevation.flys.artifacts.model.CalculationResult;
+import de.intevation.flys.artifacts.model.DataFacet;
 import de.intevation.flys.artifacts.model.FacetTypes;
-import de.intevation.flys.artifacts.model.DataFacet;
+import de.intevation.flys.artifacts.model.Parameters;
 
 import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
 
@@ -22,6 +26,9 @@
     private static final Logger log = Logger.getLogger(SQCurveFacet.class);
 
 
+    public static final String FUNCTION = "sq-pow";
+
+
     private int fractionIdx;
 
 
@@ -45,7 +52,34 @@
     @Override
     public Object getData(Artifact artifact, CallContext context) {
         log.debug("SQCurveFacet.getData");
-        log.error("NOT IMPLEMENTED!");
+
+        if (artifact instanceof FLYSArtifact) {
+            FLYSArtifact flys = (FLYSArtifact) artifact;
+
+            CalculationResult res = (CalculationResult) flys.compute(
+                context, ComputeType.ADVANCE, false);
+
+            SQResult[]       results = (SQResult[]) res.getData();
+            SQFractionResult result  = results[index].getFraction(fractionIdx);
+
+            Function func = FunctionFactory.getInstance().getFunction(FUNCTION);
+            String[] paramNames = func.getParameterNames();
+
+            Parameters params = result.getParameters();
+            double[]   coeffs = params.get(0, paramNames);
+
+            if (log.isDebugEnabled()) {
+                for (int i = 0, N = paramNames.length; i < N; i++) {
+                    log.debug("retrieved parameter " + paramNames[i] +
+                              " = " + coeffs[i]);
+                }
+            }
+
+            de.intevation.flys.artifacts.math.Function mf =
+                func.instantiate(coeffs);
+
+            return new SQFunction(mf, result.getMinQ(), result.getMaxQ());
+        }
 
         return null;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Tue Jun 19 13:55:14 2012 +0000
@@ -74,5 +74,35 @@
     public void addOutliers(SQ[] outliers) {
         this.outliers.add(outliers);
     }
+
+
+    public double getMinQ() {
+        double min = Double.MAX_VALUE;
+
+        for (SQ sq: measurements) {
+            double q = sq.getQ();
+
+            if (q < min) {
+                min = q;
+            }
+        }
+
+        return min;
+    }
+
+
+    public double getMaxQ() {
+        double max = -Double.MAX_VALUE;
+
+        for (SQ sq: measurements) {
+            double q = sq.getQ();
+
+            if (q > max) {
+                max = q;
+            }
+        }
+
+        return max;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFunction.java	Tue Jun 19 13:55:14 2012 +0000
@@ -0,0 +1,36 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import de.intevation.flys.artifacts.math.Function;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class SQFunction {
+
+    private Function function;
+
+    private double minQ;
+    private double maxQ;
+
+
+    public SQFunction(Function function, double minQ, double maxQ) {
+        this.function = function;
+        this.minQ     = minQ;
+        this.maxQ     = maxQ;
+    }
+
+
+    public Function getFunction() {
+        return function;
+    }
+
+    public double getMinQ() {
+        return minQ;
+    }
+
+    public double getMaxQ() {
+        return maxQ;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Tue Jun 19 13:55:14 2012 +0000
@@ -7,6 +7,7 @@
 
 import de.intevation.flys.artifacts.model.Calculation;
 import de.intevation.flys.artifacts.model.CalculationResult;
+import de.intevation.flys.artifacts.model.Parameters;
 
 import de.intevation.flys.artifacts.MINFOArtifact;
 
@@ -40,6 +41,7 @@
 
     protected SQResult[] getFakeResult() {
 
+        Parameters params = getFakeParameters();
         SQ[] measurements = getFakeMeasurements();
         SQ[] fOutliers    = getFakeOutliers(10);
         SQ[] tOutliers    = getFakeOutliers(20);
@@ -52,19 +54,33 @@
 
         res.setFraction(
             SQResult.FRACTION_A,
-            new SQFractionResult(null, measurements, outliers));
+            new SQFractionResult(params, measurements, outliers));
 
         res.setFraction(
             SQResult.FRACTION_B,
-            new SQFractionResult(null, measurements, outliers));
+            new SQFractionResult(params, measurements, outliers));
 
         res.setFraction(
             SQResult.FRACTION_C,
-            new SQFractionResult(null, measurements, outliers));
+            new SQFractionResult(params, measurements, outliers));
 
         return new SQResult[] { res };
     }
 
+
+    protected Parameters getFakeParameters() {
+        Parameters params = new Parameters(new String[] {
+            "a",
+            "b"
+        });
+
+        params.set(0, 0, 0.0082);
+        params.set(0, 1, 1.09);
+
+        return params;
+    }
+
+
     protected SQ[] getFakeMeasurements() {
         List<SQ> a = new ArrayList<SQ>();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQResult.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQResult.java	Tue Jun 19 13:55:14 2012 +0000
@@ -1,10 +1,7 @@
 package de.intevation.flys.artifacts.model.sq;
 
 import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
 
-import de.intevation.flys.artifacts.model.Parameters;
 
 
 public class SQResult implements Serializable {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java	Tue Jun 19 13:55:14 2012 +0000
@@ -13,7 +13,10 @@
 
 import de.intevation.flys.artifacts.model.FacetTypes;
 import de.intevation.flys.artifacts.model.sq.SQ;
+import de.intevation.flys.artifacts.model.sq.SQFunction;
 import de.intevation.flys.exports.XYChartGenerator;
+import de.intevation.flys.jfree.Function2DAdapter;
+import de.intevation.flys.jfree.JFreeUtil;
 import de.intevation.flys.jfree.StyledXYSeries;
 
 /**
@@ -118,7 +121,19 @@
         boolean          visible
     ) {
         logger.debug("doSQCurveOut");
-        logger.error("NOT IMPLEMENTED: doSQCurveOut");
+
+        Facet      f    = artifactAndFacet.getFacet();
+        SQFunction func = (SQFunction) artifactAndFacet.getData(context);
+
+        XYSeries series = JFreeUtil.sampleFunction2D(
+            new Function2DAdapter(func.getFunction()),
+            attr,
+            "SQ Curve",
+            10,
+            func.getMinQ(),
+            func.getMaxQ());
+
+        addAxisSeries(series, YAXIS.S.idx, visible);
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java	Tue Jun 19 13:20:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java	Tue Jun 19 13:55:14 2012 +0000
@@ -5,9 +5,14 @@
 
 import java.util.Iterator;
 
+import org.w3c.dom.Document;
+
 import org.jfree.chart.entity.ChartEntity;
 import org.jfree.chart.entity.EntityCollection;
 
+import org.jfree.data.function.Function2D;
+
+
 public class JFreeUtil {
 
     /** Do not instantiate. */
@@ -45,5 +50,26 @@
 
         return false;
     }
+
+
+    public static StyledXYSeries sampleFunction2D(
+        Function2D func,
+        Document   theme,
+        String     seriesKey,
+        int        samples,
+        double     start,
+        double     end
+    ) {
+        StyledXYSeries series = new StyledXYSeries(seriesKey, theme);
+
+        double step = (end - start) / (samples - 1);
+
+        for (int i = 0; i < samples; i++) {
+            double x = start + (step * i);
+            series.add(x, func.getValue(x));
+        }
+
+        return series;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org