changeset 3981:6bcc50e2cc7d

More code for S(Q) relation.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 28 Sep 2012 16:41:08 +0200
parents 6cc5186b9b48
children f9729662f1be
files flys-artifacts/ChangeLog 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
diffstat 3 files changed, 258 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Sep 28 13:11:25 2012 +0200
+++ b/flys-artifacts/ChangeLog	Fri Sep 28 16:41:08 2012 +0200
@@ -1,3 +1,11 @@
+2012-09-29	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java:
+	  More code for calculating S fractions. Totally broken and incomplete!
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java:
+	  Fetch more data from data base (sieve diameters).
+
 2012-09-29	Björn Ricks	<bjoern.ricks@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/StaticFLYSArtifact.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java	Fri Sep 28 13:11:25 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java	Fri Sep 28 16:41:08 2012 +0200
@@ -2,8 +2,26 @@
 
 import java.util.Map;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 public class Measurement
 {
+    private static final Log log =
+        LogFactory.getLog(Measurement.class);
+
+    public static final double ADD_8 = Math.log(10) - Math.log(8)/Math.log(6.3);
+    public static final double SCALE_8 = Math.log(6.3);
+
+    public static final double ADD_4 = Math.log(8) - Math.log(6.3)/Math.log(10);
+    public static final double SCALE_4 = Math.log(6.3);
+
+    public static final double [] SIEVE_DIAMETERS = {
+        100d,   63d,  31.5d,    16d,
+          8d,    4d,     2d,     1d,
+        0.5d, 0.25d, 0.125d, 0.063d
+    };
+
     protected Map<String, Object> data;
 
     protected Measurement prev;
@@ -16,11 +34,31 @@
         this.data = data;
     }
 
+    public Measurement head() {
+        Measurement current = this;
+        while (current.prev != null) {
+            current = current.prev;
+        }
+        return current;
+    }
+
     protected double get(String name) {
         Number value = (Number)data.get(name);
         return value != null ? value.doubleValue() : Double.NaN;
     }
 
+    protected void set(String name, double value) {
+        data.put(name, Double.valueOf(value));
+    }
+
+    protected Object getData(String name) {
+        return data.get(name);
+    }
+
+    protected void putData(String name, Object value) {
+        data.put(name, value);
+    }
+
     public double S_SS() {
         return get("TSAND");
     }
@@ -73,11 +111,11 @@
     }
 
     public double SIEB(int i) {
-        return get(String.format("SIEB%02d", i));
+        return get(siebString(i));
     }
 
     public double RSIEB(int i) {
-        return get(String.format("RSIEB%02d", i));
+        return get(rsiebString(i));
     }
 
     public double REST() {
@@ -124,5 +162,139 @@
     public void setNext(Measurement next) {
         this.next = next;
     }
+
+    protected int findSieveIndex(double diameter) {
+        for (int i = 1; i <= 22; ++i) {
+            double size = SIEB(i);
+            if (Math.abs(size - diameter) < 0.00001) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    public static int sieve(double value) {
+        for (int i = 0; i < SIEVE_DIAMETERS.length; ++i) {
+            if (value >= SIEVE_DIAMETERS[i]) {
+                return i+1;
+            }
+        }
+        return SIEVE_DIAMETERS.length;
+    }
+
+    private static final String rsiebString(int idx) {
+        return String.format("RSIEB%02d", idx);
+    }
+
+    private static final String siebString(int idx) {
+        return String.format("SIEB%02d", idx);
+    }
+
+    private static final String quantString(int idx) {
+        return String.format("QUANT%02d", idx);
+    }
+
+    private static final String normQuantString(int idx) {
+        return String.format("NORMQUANT%02d", idx);
+    }
+
+    protected void deleteSieve(int idx) {
+        data.remove(rsiebString(idx));
+        data.remove(siebString(idx));
+    }
+
+    protected void putSieve(int idx, double diameter, double value) {
+        data.put(rsiebString(idx), Double.valueOf(value));
+        data.put(siebString(idx), Double.valueOf(diameter));
+    }
+
+    protected double totalRSIEB() {
+        double sum = 0d;
+        for (int i = 1; i <= 21; ++i) {
+            Double x = (Double)data.get(rsiebString(i));
+            if (x != null) {
+                sum += x;
+            }
+        }
+        return sum;
+    }
+
+    protected double totalQUANT() {
+        double sum = 0d;
+        for (int i = 1; i <= 22; ++i) {
+            Double x = (Double)data.get(quantString(i));
+            if (x != null) {
+                sum += x;
+            }
+        }
+        return sum;
+    }
+
+    public void adjustOriginalSieves() {
+
+        // If we already have an 8mm diameter sieve
+        // we dont need to 'invent' it.
+        if (findSieveIndex(8d) > -1) {
+            return;
+        }
+
+        // create a new 8mm sieve.
+        // delete 6.3mm sieve.
+        // modify 4mm sieve.
+        //
+        int sixIdx  = findSieveIndex(6.3d);
+        int tenIdx  = findSieveIndex(10d);
+        int fourIdx = findSieveIndex(4d);
+
+        if (sixIdx < 0 || tenIdx < 0 || fourIdx < 0) {
+            log.warn("missind diameter");
+            return;
+        }
+
+        double sixValue  = RSIEB(sixIdx);
+        double tenValue  = RSIEB(tenIdx);
+        double fourValue = RSIEB(fourIdx);
+
+        deleteSieve(sixIdx);
+
+        double eightValue   = ADD_8 - SCALE_8*sixValue + tenValue;
+        double newFourValue = ADD_4 - SCALE_4*sixValue + fourValue;
+
+        putSieve(22, 8d, eightValue);
+        putSieve(fourIdx, 4d, newFourValue);
+   }
+
+
+    public void fillSieveCategories() {
+        adjustOriginalSieves();
+
+        for (int i = 1; i <= 22; ++i) {
+            Double rsieb = (Double)getData(rsiebString(i));
+            Double sieb  = (Double)getData(siebString(i));
+            if (rsieb == null || sieb == null) {
+                continue;
+            }
+
+            int idx = sieve(sieb);
+            String quantString = quantString(idx);
+            Double old = (Double)getData(quantString);
+            old = old == null ? 0d : rsieb + old;
+            putData(quantString, old);
+        }
+
+        double totalQUANT = totalQUANT();
+
+        for (int i = 1; i <= 22; ++i) {
+            String qs = quantString(i);
+            String ns = normQuantString(i);
+            Double quant = (Double)getData(qs);
+            if (quant == null) {
+                putData(ns, Double.valueOf(0d));
+            }
+            else {
+                putData(ns, quant / totalQUANT);
+            }
+        }
+    }
 }
 // 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/MeasurementFactory.java	Fri Sep 28 13:11:25 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java	Fri Sep 28 16:41:08 2012 +0200
@@ -1,5 +1,6 @@
 package de.intevation.flys.artifacts.model.sq;
 
+import java.util.Date;
 import java.util.List;
 
 import de.intevation.flys.artifacts.model.DateRange;
@@ -25,6 +26,8 @@
 
     public static final String SQL_MEASSURE =
         "SELECT m.datum        AS DATUM," +
+               "g.GLOTRECHTEID AS GLOTRECHTEID," +
+               "gp.LFDNR       AS LFDNR," +
                "g.UFERABST     AS UFERABST," +
                "g.UFERABLINKS  AS UFERABLINKS," +
                "m.TSCHWEB      AS TSCHWEB," +
@@ -32,7 +35,6 @@
                "gp.MESSDAUER   AS MESSDAUER," +
                "gp.MENGE       AS MENGE," +
                "gp.GTRIEB      AS GTRIEB," +
-               "gp.LFDNR       AS LFDNR," +
                "m.TGESCHIEBE   AS TGESCHIEBE," +
                "gs.RSIEB01     AS RSIEB01," +
                "gs.RSIEB02     AS RSIEB02," +
@@ -55,8 +57,28 @@
                "gs.RSIEB19     AS RSIEB19," +
                "gs.RSIEB20     AS RSIEB20," +
                "gs.RSIEB21     AS RSIEB21," +
-               "gs.REST        AS REST," +
-               "g.GLOTRECHTEID AS GLOTRECHTEID " +
+               "gs.REST        AS REST, " +
+               "COALESCE(sie.SIEB01, 0) AS SIEB01, " +
+               "COALESCE(sie.SIEB02, 0) AS SIEB02, " +
+               "COALESCE(sie.SIEB03, 0) AS SIEB03, " +
+               "COALESCE(sie.SIEB04, 0) AS SIEB04, " +
+               "COALESCE(sie.SIEB05, 0) AS SIEB05, " +
+               "COALESCE(sie.SIEB06, 0) AS SIEB06, " +
+               "COALESCE(sie.SIEB07, 0) AS SIEB07, " +
+               "COALESCE(sie.SIEB08, 0) AS SIEB08, " +
+               "COALESCE(sie.SIEB09, 0) AS SIEB09, " +
+               "COALESCE(sie.SIEB10, 0) AS SIEB10, " +
+               "COALESCE(sie.SIEB11, 0) AS SIEB11, " +
+               "COALESCE(sie.SIEB12, 0) AS SIEB12, " +
+               "COALESCE(sie.SIEB13, 0) AS SIEB13, " +
+               "COALESCE(sie.SIEB14, 0) AS SIEB14, " +
+               "COALESCE(sie.SIEB15, 0) AS SIEB15, " +
+               "COALESCE(sie.SIEB16, 0) AS SIEB16, " +
+               "COALESCE(sie.SIEB17, 0) AS SIEB17, " +
+               "COALESCE(sie.SIEB18, 0) AS SIEB18, " +
+               "COALESCE(sie.SIEB19, 0) AS SIEB19, " +
+               "COALESCE(sie.SIEB20, 0) AS SIEB20, " +
+               "COALESCE(sie.SIEB21, 0) AS SIEB21 " +
         "FROM MESSUNG m " +
             "JOIN STATION    s ON m.STATIONID    = s.STATIONID " +
             "JOIN glotrechte g ON m.MESSUNGID    = g.MESSUNGID " +
@@ -68,7 +90,19 @@
             "AND s.KM BETWEEN :location - 0.001 AND :location + 0.001 " +
             "AND m.DATUM BETWEEN :from AND :to " +
             "AND m.TGESCHIEBE IS NOT NULL " +
-        "ORDER BY m.DATUM";
+            "AND (" +
+                "COALESCE(gs.RSIEB01, 0) + COALESCE(gs.RSIEB02, 0) +" +
+                "COALESCE(gs.RSIEB03, 0) + COALESCE(gs.RSIEB04, 0) +" +
+                "COALESCE(gs.RSIEB05, 0) + COALESCE(gs.RSIEB06, 0) +" +
+                "COALESCE(gs.RSIEB07, 0) + COALESCE(gs.RSIEB08, 0) +" +
+                "COALESCE(gs.RSIEB09, 0) + COALESCE(gs.RSIEB10, 0) +" +
+                "COALESCE(gs.RSIEB11, 0) + COALESCE(gs.RSIEB12, 0) +" +
+                "COALESCE(gs.RSIEB13, 0) + COALESCE(gs.RSIEB14, 0) +" +
+                "COALESCE(gs.RSIEB15, 0) + COALESCE(gs.RSIEB16, 0) +" +
+                "COALESCE(gs.RSIEB17, 0) + COALESCE(gs.RSIEB18, 0) +" +
+                "COALESCE(gs.RSIEB19, 0) + COALESCE(gs.RSIEB20, 0) +" +
+                "COALESCE(gs.RSIEB21, 0) + COALESCE(gs.REST, 0)) > 0 " +
+        "ORDER BY m.DATUM, gp.LFDNR, g.UFERABST";
 
     public static final class MeasurementResultTransformer
     extends                   BasicTransformerAdapter
@@ -109,6 +143,8 @@
         SQLQuery query = session.createSQLQuery(SQL_MEASSURE)
             .addScalar("Q_BPEGEL",     StandardBasicTypes.DOUBLE)
             .addScalar("DATUM",        StandardBasicTypes.DATE)
+            .addScalar("GLOTRECHTEID", StandardBasicTypes.INTEGER)
+            .addScalar("LFDNR",        StandardBasicTypes.INTEGER)
             .addScalar("UFERABST",     StandardBasicTypes.DOUBLE)
             .addScalar("UFERABLINKS",  StandardBasicTypes.DOUBLE)
             .addScalar("TSCHWEB",      StandardBasicTypes.DOUBLE)
@@ -116,7 +152,6 @@
             .addScalar("MESSDAUER",    StandardBasicTypes.DOUBLE)
             .addScalar("MENGE",        StandardBasicTypes.DOUBLE)
             .addScalar("GTRIEB",       StandardBasicTypes.DOUBLE)
-            .addScalar("LFDNR",        StandardBasicTypes.DOUBLE)
             .addScalar("TGESCHIEBE",   StandardBasicTypes.DOUBLE)
             .addScalar("RSIEB01",      StandardBasicTypes.DOUBLE)
             .addScalar("RSIEB02",      StandardBasicTypes.DOUBLE)
@@ -140,7 +175,27 @@
             .addScalar("RSIEB20",      StandardBasicTypes.DOUBLE)
             .addScalar("RSIEB21",      StandardBasicTypes.DOUBLE)
             .addScalar("REST",         StandardBasicTypes.DOUBLE)
-            .addScalar("GLOTRECHTEID", StandardBasicTypes.DOUBLE);
+            .addScalar("SIEB01",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB02",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB03",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB04",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB05",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB06",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB07",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB08",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB09",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB10",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB11",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB12",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB13",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB14",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB15",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB16",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB17",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB18",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB19",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB20",       StandardBasicTypes.DOUBLE)
+            .addScalar("SIEB21",       StandardBasicTypes.DOUBLE);
 
         query.setString("river_name", river);
         query.setDouble("location", location);
@@ -152,12 +207,26 @@
         @SuppressWarnings("unchecked")
 		List<Measurement> measuments = (List<Measurement>)query.list();
 
+        Integer lastLR = null;
+
         for (int i = 0, N = measuments.size(); i < N; ++i) {
             Measurement m = measuments.get(i);
+
+            Integer currentLR = (Integer)m.getData("GLOTRECHTEID");
+
+            boolean newDS = lastLR == null
+                || (currentLR != null && !lastLR.equals(currentLR));
+
             Measurement p = i >   0 ? measuments.get(i-1) : null;
             Measurement n = i < N-1 ? measuments.get(i+1) : null;
-            m.setPrev(p);
+            m.setPrev(newDS ? null : p);
             m.setNext(n);
+
+            if (p != null && newDS) {
+                p.setNext(null);
+            }
+
+            lastLR = currentLR;
         }
 
         return new Measurements(measuments);

http://dive4elements.wald.intevation.org