changeset 3290:36f3b53d5cfc

SQ relation: Added infrastructure to extract fractions from measurments. flys-artifacts/trunk@4958 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 12 Jul 2012 17:34:40 +0000
parents ec9aab93f292
children b52c4b34ec1b
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurements.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java
diffstat 6 files changed, 188 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jul 12 16:15:30 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jul 12 17:34:40 2012 +0000
@@ -1,3 +1,23 @@
+2012-07-12	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/Measurements.java:
+	  New. Container for measurements. Helps to extract the S/Q fractions.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java:
+	  Now uses the measurements container.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java:
+	  Added convenience methods to extract the different fractions.
+	  DONE: Q, S_SS, S_SF
+	  TODO: S_BL_S, S_BL_FG, S_BL_CG, S_BL
+
+	* src/main/java/de/intevation/flys/artifacts/model/SQOverview.java,
+	  src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java:
+	  Relax existence of SIEBSATZID.
+	  We have to use a left join during data extraction anyway.
+	  We force the existence of Q because we cannot do the fitting if
+	  we don't have them.
+
 2012-07-12	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java	Thu Jul 12 16:15:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java	Thu Jul 12 17:34:40 2012 +0000
@@ -95,8 +95,7 @@
         "    JOIN gewaesser g " +
         "       ON s.gewaesserid = g.gewaesserid " +
         "WHERE" +
-        "    m.gsiebsatzid IS NOT NULL AND" +
-        "    m.q           IS NOT NULL AND" +
+        "    m.q IS NOT NULL AND" +
         "    g.name = :name " +
         "ORDER by" +
         "    s.km, m.datum";
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java	Thu Jul 12 16:15:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java	Thu Jul 12 17:34:40 2012 +0000
@@ -13,7 +13,42 @@
         this.data = data;
     }
 
-    /** TODO: Extract data. */
+    protected double get(String name) {
+        Number value = (Number)data.get(name);
+        return value != null ? value.doubleValue() : Double.NaN;
+    }
+
+    public double S_SS() {
+        return get("TSAND");
+    }
+
+    public double S_SF() {
+        return get("TSCHWEB") - get("TSAND");
+    }
+
+    public double Q() {
+        return get("Q");
+    }
+
+    public double S_BL_S() {
+        // TODO: Implement me!
+        return Double.NaN;
+    }
+
+    public double S_BL_FG() {
+        // TODO: Implement me!
+        return Double.NaN;
+    }
+
+    public double S_BL_CG() {
+        // TODO: Implement me!
+        return Double.NaN;
+    }
+
+    public double S_BL() {
+        // TODO: Implement me!
+        return Double.NaN;
+    }
 
     @Override
     public String toString() {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java	Thu Jul 12 16:15:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java	Thu Jul 12 17:34:40 2012 +0000
@@ -5,7 +5,6 @@
 import de.intevation.flys.backend.SedDBSessionHolder;
 
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 import org.apache.log4j.Logger;
@@ -27,6 +26,7 @@
             "m.TSAND   AS TSAND,"   +
             "m.TSCHWEB AS TSCHWEB," +
             "m.CSCHWEB AS CSCHWEB " +
+            "m.Q       AS Q " +
             /* TODO: Select more */
         "FROM messung m " +
             "JOIN station   s ON m.stationid   = s.stationid " +
@@ -34,7 +34,8 @@
             "WHERE " +
                 "g.name = :river_name AND " +
                 "s.km BETWEEN :location - 0.001 AND :location + 0.001 AND " +
-                "m.datum BETWEEN :from AND :to";
+                "m.datum BETWEEN :from AND :to AND " +
+                "m.Q IS NOT NULL";
 
     public static final class MeasurementResultTransformer
     extends                   BasicTransformerAdapter
@@ -58,7 +59,7 @@
     private MeasurementFactory() {
     }
 
-    protected static List<Measurement> load(
+    protected static Measurements load(
         Session   session,
         String    river,
         double    location,
@@ -67,7 +68,8 @@
         SQLQuery query = session.createSQLQuery(SQL_MEASSURE)
             .addScalar("TSAND",   StandardBasicTypes.DOUBLE)
             .addScalar("TSCHWEB", StandardBasicTypes.DOUBLE)
-            .addScalar("CSCHWEB", StandardBasicTypes.DOUBLE);
+            .addScalar("CSCHWEB", StandardBasicTypes.DOUBLE)
+            .addScalar("Q",       StandardBasicTypes.DOUBLE);
 
         query.setString("river_name", river);
         query.setDouble("location", location);
@@ -76,10 +78,10 @@
 
         query.setResultTransformer(MeasurementResultTransformer.INSTANCE);
 
-        return query.list();
+        return new Measurements(query.list());
     }
 
-    public static List<Measurement> getMeasurements(
+    public static Measurements getMeasurements(
         String    river,
         double    location,
         DateRange dateRange
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurements.java	Thu Jul 12 17:34:40 2012 +0000
@@ -0,0 +1,121 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class Measurements
+{
+    public interface SExtractor {
+        double getS(Measurement measument);
+    } // interface SExtractor
+
+    public static final SExtractor S_SF_EXTRACTOR = new SExtractor() {
+        @Override
+        public double getS(Measurement measument) {
+            return measument.S_SF();
+        }
+    };
+
+    public static final SExtractor S_SS_EXTRACTOR = new SExtractor() {
+        @Override
+        public double getS(Measurement measument) {
+            return measument.S_SS();
+        }
+    };
+
+    public static final SExtractor S_BL_S_EXTRACTOR = new SExtractor() {
+        @Override
+        public double getS(Measurement measument) {
+            return measument.S_BL_S();
+        }
+    };
+
+    public static final SExtractor S_BL_FG_EXTRACTOR = new SExtractor() {
+        @Override
+        public double getS(Measurement measument) {
+            return measument.S_BL_FG();
+        }
+    };
+
+    public static final SExtractor S_BL_CG_EXTRACTOR = new SExtractor() {
+        @Override
+        public double getS(Measurement measument) {
+            return measument.S_BL_CG();
+        }
+    };
+
+    public static final SExtractor S_BL_EXTRACTOR = new SExtractor() {
+        @Override
+        public double getS(Measurement measument) {
+            return measument.S_BL();
+        }
+    };
+
+    protected List<Measurement> measuments;
+
+    public Measurements() {
+    }
+
+    public Measurements(List<Measurement> measuments) {
+        this.measuments = measuments;
+    }
+
+    public List<SQ> extractSQ(SExtractor extractor) {
+        List<SQ> result = new ArrayList(measuments.size());
+        for (Measurement measument: measuments) {
+            SQ sq = new SQ(extractor.getS(measument), measument.Q());
+            if (sq.isValid()) {
+                result.add(sq);
+            }
+        }
+        return result;
+    }
+
+    public List<SQ> S_SF() {
+        return extractSQ(S_SF_EXTRACTOR);
+    }
+
+    public List<SQ> S_SS() {
+        return extractSQ(S_SS_EXTRACTOR);
+    }
+
+    public List<SQ> S_BL_S() {
+        return extractSQ(S_BL_S_EXTRACTOR);
+    }
+
+    public List<SQ> S_BL_FG() {
+        return extractSQ(S_BL_FG_EXTRACTOR);
+    }
+
+    public List<SQ> S_BL_CG() {
+        return extractSQ(S_BL_CG_EXTRACTOR);
+    }
+
+    public List<SQ> S_BL() {
+        return extractSQ(S_BL_EXTRACTOR);
+    }
+
+    public List<SQ> getSQs(int index) {
+        switch (index) {
+            case 0: return S_SF();
+            case 1: return S_SS();
+            case 2: return S_BL_S();
+            case 3: return S_BL_FG();
+            case 4: return S_BL_CG();
+            case 5: return S_BL();
+        }
+        // XXX: Should we throw an exception?
+        return null;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("Measurements [");
+        for (int i = 0, M = measuments.size(); i < M; ++i) {
+            if (i > 0) sb.append(", ");
+            sb.append(measuments.get(i));
+        }
+        return sb.append(']').toString();
+    }
+}
+// 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/sq/SQRelationCalculation.java	Thu Jul 12 16:15:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java	Thu Jul 12 17:34:40 2012 +0000
@@ -8,8 +8,6 @@
 
 import de.intevation.flys.backend.SedDBSessionHolder;
 
-import java.util.List;
-
 import org.apache.log4j.Logger;
 
 public class SQRelationCalculation extends Calculation {
@@ -82,13 +80,11 @@
         boolean debug = log.isDebugEnabled();
 
         for (DateRange period: periods) {
-            List<Measurement> measurements =
+            Measurements measurements =
                 MeasurementFactory.getMeasurements(river, location, period);
 
             if (debug) {
-                for (Measurement measument: measurements) {
-                    log.debug(measument.toString());
-                }
+                log.debug(measurements.toString());
             }
         }
 

http://dive4elements.wald.intevation.org