changeset 3304:02d5731b43a2

SQ relation: Build a valid SQResult. flys-artifacts/trunk@4989 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 13 Jul 2012 11:12:35 +0000
parents 96daa8a4c48f
children b24920235944
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java flys-artifacts/src/main/java/de/intevation/flys/exports/OutputHelper.java flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java
diffstat 6 files changed, 84 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 13 10:54:56 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 13 11:12:35 2012 +0000
@@ -1,3 +1,9 @@
+2012-07-13	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java,
+	  src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java:
+	  Finally! SQResult is build and should be ready for display. :-)
+
 2012-07-13	Christian Lins <christian.lins@intevation.de>
 
 	* src/main/java/de/intevation/flys/utils/MapfileGenerator.java,
@@ -63,6 +69,13 @@
 2012-07-13	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java,
+	  src/main/java/de/intevation/flys/exports/OutputHelper.java,
+	  src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java:
+	  Added missing vim lines.
+
+2012-07-13	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java,
 	  src/main/java/de/intevation/flys/exports/OutputHelper.java:
 	  Removed trailing whitespace.
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Fri Jul 13 10:54:56 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Fri Jul 13 11:12:35 2012 +0000
@@ -1,25 +1,20 @@
 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;
 
+import java.io.Serializable;
+
+import java.util.List;
 
 public class SQFractionResult implements Serializable {
 
     protected Parameters parameters;
-
-    protected SQ[] measurements;
+    protected SQ[]       measurements;
     protected List<SQ[]> outliers;
 
-
     public SQFractionResult() {
-        this.outliers = new ArrayList<SQ[]>();
     }
 
-
     public SQFractionResult(
         Parameters parameters,
         SQ[]       measurements,
@@ -30,6 +25,11 @@
         this.outliers     = outliers;
     }
 
+    public boolean isValid() {
+        return parameters   != null
+            && measurements != null 
+            && outliers     != null;
+    }
 
     public Parameters getParameters() {
         return parameters;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java	Fri Jul 13 10:54:56 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java	Fri Jul 13 11:12:35 2012 +0000
@@ -58,3 +58,4 @@
         return copy;
     }
 }
+// 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	Fri Jul 13 10:54:56 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Fri Jul 13 11:12:35 2012 +0000
@@ -2,12 +2,18 @@
 
 import de.intevation.flys.artifacts.access.SQRelationAccess;
 
+import de.intevation.flys.artifacts.math.fitting.Function;
+import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
+
 import de.intevation.flys.artifacts.model.Calculation;
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.DateRange;
+import de.intevation.flys.artifacts.model.Parameters;
 
 import de.intevation.flys.backend.SedDBSessionHolder;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
 public class SQRelationCalculation extends Calculation {
@@ -15,11 +21,13 @@
     private static final Logger log =
         Logger.getLogger(SQRelationCalculation.class);
 
+    public static final String SQ_FUNCTION_NAME = "sq-pow";
+
     protected String       river;
     protected double       location;
     protected DateRange [] periods;
     protected double       outliers;
-
+    
     public SQRelationCalculation() {
     }
 
@@ -79,6 +87,16 @@
 
         boolean debug = log.isDebugEnabled();
 
+        Function function = FunctionFactory
+            .getInstance()
+            .getFunction(SQ_FUNCTION_NAME);
+
+        if (function == null) {
+            log.error("No '" + SQ_FUNCTION_NAME + "' function found.");
+            // TODO: i18n
+            addProblem("sq.missing.sq.function");
+        }
+
         // XXX: We can only produce one SQResult.
         DateRange period = periods[0];
 
@@ -92,11 +110,50 @@
         SQFractionResult [] fractionResults =
             new SQFractionResult[SQResult.NUMBER_FRACTIONS];
 
+        String [] parameterNames = function.getParameterNames();
+
+        Fitting fitting = new Fitting(function, outliers);
         for (int i = 0; i < fractionResults.length; ++i) {
-            // TODO: Implement me!
+            List<SQ> sqs = measurements.getSQs(i);
+
+            SQFractionResult fractionResult;
+
+            if (!fitting.fit(sqs)) {
+                // TODO: i18n
+                addProblem("sq.fitting.failed." + i);
+                fractionResult = new SQFractionResult();
+            }
+            else {
+                Parameters parameters = createParameters(parameterNames);
+                int row = parameters.newRow();
+                double [] coeffs = fitting.getParameters();
+                for (int j = 0; j < parameterNames.length; ++j) {
+                    parameters.set(row, parameterNames[j], coeffs[j]);
+                }
+                parameters.set(row, "chi_sqr", fitting.getChiSqr());
+                parameters.set(row, "std_dev", fitting.getStandardDeviation());
+
+                fractionResult = new SQFractionResult(
+                    parameters,
+                    fitting.getRemaining(),
+                    fitting.getOutliers());
+            }
+
+            fractionResults[i] = fractionResult;
+
+            fitting.reset();
         }
 
         return new CalculationResult(new SQResult(fractionResults), this);
     }
+
+    public static final Parameters createParameters(String [] names) {
+
+        String [] columns = new String[names.length + 2];
+        columns[0] = "chi_sqr";
+        columns[1] = "std_dev";
+        System.arraycopy(names, 0, columns, 2, names.length);
+        return new Parameters(columns);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/OutputHelper.java	Fri Jul 13 10:54:56 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/OutputHelper.java	Fri Jul 13 11:12:35 2012 +0000
@@ -511,5 +511,5 @@
             return themes.size();
         }
     }
-
 }
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java	Fri Jul 13 10:54:56 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java	Fri Jul 13 11:12:35 2012 +0000
@@ -223,3 +223,4 @@
         return size[0] > 0 && size[1] > 0 ? size : null;
     }
 }
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org