changeset 3310:8af1111af180

SQ relation: More fixes. flys-artifacts/trunk@4999 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 13 Jul 2012 15:41:56 +0000
parents fd3243d892a9
children 53ad37c8abe3
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.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/SQRelationCalculation.java flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java
diffstat 6 files changed, 88 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 13 15:04:36 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 13 15:41:56 2012 +0000
@@ -1,3 +1,16 @@
+2012-07-13	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java:
+	  Fixed more SQL syntax errors.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java:
+	  Generate more than one SQResult (To my big suprise it's possible).
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java,
+	  src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java.
+	  src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java:
+	  Made more NPE bullet proof.
+
 2012-07-13	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java	Fri Jul 13 15:04:36 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java	Fri Jul 13 15:41:56 2012 +0000
@@ -45,18 +45,18 @@
             "m.TSCHWEB AS TSCHWEB," +
             "m.CSCHWEB AS CSCHWEB," +
             "m.Q       AS Q," +
-            "%GSIEBSATZ%" +
-            "%SSIEBUNG%" +
+            "%GSIEBSATZ%," +
+            "%SSIEBUNG% " +
         "FROM messung m " +
             "JOIN station   s ON m.stationid   = s.stationid " +
             "JOIN gewaesser g ON s.gewaesserid = g.gewaesserid " +
-            "LEFT JOIN GSIEBSATZ gs ON m.gsiebsatzid = gs.gewaesserid " +
-            "LEFT JOIN SSIEBUNG  ss ON gs.gsiebsatzid = ss.gewaesserid " +
-            "WHERE " +
-                "g.name = :river_name AND " +
-                "s.km BETWEEN :location - 0.001 AND :location + 0.001 AND " +
-                "m.datum BETWEEN :from AND :to AND " +
-                "m.Q IS NOT NULL")
+            "LEFT JOIN GSIEBSATZ gs ON m.gsiebsatzid = gs.gsiebsatzid " +
+            "LEFT JOIN SSIEBUNG  ss ON gs.gsiebsatzid = ss.gsiebsatzid " +
+        "WHERE " +
+            "g.name = :river_name AND " +
+            "s.km BETWEEN :location - 0.001 AND :location + 0.001 AND " +
+            "m.datum BETWEEN :from AND :to AND " +
+            "m.Q IS NOT NULL")
         .replace("%GSIEBSATZ%", projection("gs", GSIEBSATZ))
         .replace("%SSIEBUNG%",  projection("ss", SSIEBUNG));
 
@@ -65,13 +65,15 @@
         String [] columnNames
     ) {
         StringBuilder sb = new StringBuilder();
-        for (String columnName: columnNames) {
+        for (int i = 0; i < columnNames.length; ++i) {
+            if (i > 0) {
+                sb.append(',');
+            }
             sb.append(prefix)
               .append('.')
-              .append(columnName)
+              .append(columnNames[i])
               .append(" AS ")
-              .append(columnName)
-              .append(',');
+              .append(columnNames[i]);
         }
         return sb.toString();
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java	Fri Jul 13 15:04:36 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java	Fri Jul 13 15:41:56 2012 +0000
@@ -66,6 +66,12 @@
             String[] paramNames = func.getParameterNames();
 
             Parameters params = result.getParameters();
+
+            if (params == null) {
+                log.debug("no parameters found");
+                return null;
+            }
+
             double[]   coeffs = params.get(0, paramNames);
 
             if (log.isDebugEnabled()) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Fri Jul 13 15:04:36 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQFractionResult.java	Fri Jul 13 15:41:56 2012 +0000
@@ -59,12 +59,12 @@
 
 
     public int getOutliersCount() {
-        return outliers.size();
+        return outliers == null ? 0 : outliers.size();
     }
 
 
     public SQ[] getOutliers(int idx) {
-        if (idx < outliers.size()) {
+        if (outliers != null && idx >= 0 && idx < outliers.size()) {
             return outliers.get(idx);
         }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Fri Jul 13 15:04:36 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Fri Jul 13 15:41:56 2012 +0000
@@ -12,6 +12,7 @@
 
 import de.intevation.flys.backend.SedDBSessionHolder;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.log4j.Logger;
@@ -97,54 +98,58 @@
             addProblem("sq.missing.sq.function");
         }
 
-        // XXX: We can only produce one SQResult.
-        DateRange period = periods[0];
-
-        Measurements measurements =
-            MeasurementFactory.getMeasurements(river, location, period);
-
-        if (debug) {
-            log.debug(measurements.toString());
-        }
-
-        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) {
-            List<SQ> sqs = measurements.getSQs(i);
-
-            SQFractionResult fractionResult;
+        List<SQResult> results = new ArrayList<SQResult>(periods.length);
 
-            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());
+        Fitting fitting = new Fitting(function, outliers);
 
-                fractionResult = new SQFractionResult(
-                    parameters,
-                    fitting.getRemaining(),
-                    fitting.getOutliers());
+        for (DateRange period: periods) {
+            Measurements measurements =
+                MeasurementFactory.getMeasurements(river, location, period);
+
+            if (debug) {
+                log.debug(measurements.toString());
             }
 
-            fractionResults[i] = fractionResult;
+            SQFractionResult [] fractionResults =
+                new SQFractionResult[SQResult.NUMBER_FRACTIONS];
 
-            fitting.reset();
+            for (int i = 0; i < fractionResults.length; ++i) {
+                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());
+                }
+                fitting.reset();
+                fractionResults[i] = fractionResult;
+            }
+            results.add(new SQResult(fractionResults));
         }
 
-        return new CalculationResult(new SQResult(fractionResults), this);
+        return new CalculationResult(
+            results.toArray(new SQResult[results.size()]), 
+            this);
     }
 
     public static final Parameters createParameters(String [] names) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java	Fri Jul 13 15:04:36 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java	Fri Jul 13 15:41:56 2012 +0000
@@ -137,11 +137,15 @@
         Facet      f    = artifactAndFacet.getFacet();
         SQFunction func = (SQFunction) artifactAndFacet.getData(context);
 
+        if (func == null) {
+            return;
+        }
+
         XYSeries series = JFreeUtil.sampleFunction2D(
             func.getFunction(),
             attr,
             desc,
-            10,
+            500,
             func.getMinQ(),
             func.getMaxQ());
 
@@ -169,6 +173,10 @@
 
         Facet    f      = artifactAndFacet.getFacet();
         SQ[]     sqs    = (SQ[]) artifactAndFacet.getData(context);
+        if (sqs == null) {
+            logger.debug("No SQs found for facet");
+            return;
+        }
         XYSeries series = new StyledXYSeries(desc, attr);
 
         for (SQ sq: sqs) {

http://dive4elements.wald.intevation.org