changeset 3992:a9c93b7c9da1

Simpify the S(Q) fraction sieving stuff.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 30 Sep 2012 21:15:23 +0200
parents 3a1cac4bfe70
children 7d056b7a50d8
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 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Sieve.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SieveArray.java
diffstat 5 files changed, 267 insertions(+), 177 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Sun Sep 30 19:03:26 2012 +0200
+++ b/flys-artifacts/ChangeLog	Sun Sep 30 21:15:23 2012 +0200
@@ -1,3 +1,17 @@
+2012-09-30	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Sieve.java:
+	  Model a sieve with diameter and load which simplifies the logic a lot.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SieveArray.java:
+	  New. A standard sieve array with 12 sieves.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java:
+	  Removed the string lookup crap and move the code to classes.
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java:
+	  Directy generate sieves now not the symbolic string lookup stuff.
+
 2012-09-30	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/GaugeDischargeCurveArtifact.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java	Sun Sep 30 19:03:26 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java	Sun Sep 30 21:15:23 2012 +0200
@@ -1,5 +1,6 @@
 package de.intevation.flys.artifacts.model.sq;
 
+import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.logging.Log;
@@ -16,13 +17,11 @@
     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 Map<String, Object> data;
+    protected List<Sieve> sieves;
+
+    protected SieveArray  sieveArray;
 
     protected Measurement prev;
     protected Measurement next;
@@ -30,8 +29,11 @@
     public Measurement() {
     }
 
-    public Measurement(Map<String, Object> data) {
+    public Measurement(Map<String, Object> data, List<Sieve> sieves) {
         this.data = data;
+        this.sieves = sieves;
+        adjustOriginalSieves();
+        this.sieveArray = calculateSieveArray();
     }
 
     public Measurement head() {
@@ -110,24 +112,30 @@
         return S_SS() + S_BL_S() + S_BL_FG() + S_BL_CG();
     }
 
-    public double SIEB(int i) {
-        return get(siebString(i));
-    }
-
-    public double RSIEB(int i) {
-        return get(rsiebString(i));
-    }
-
-    public double REST() {
-        return get("REST");
-    }
-
     @Override
     public String toString() {
         return "Measurement: " + data;
     }
 
     /**
+     * Gets the sieves for this instance.
+     *
+     * @return The sieves.
+     */
+    public List<Sieve> getSieves() {
+        return this.sieves;
+    }
+
+    /**
+     * Gets the sieveArray for this instance.
+     *
+     * @return The sieveArray.
+     */
+    public SieveArray getSieveArray() {
+        return this.sieveArray;
+    }
+
+    /**
      * Gets the prev for this instance.
      *
      * @return The prev.
@@ -163,138 +171,70 @@
         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;
+    protected Sieve findSieve(double diameter) {
+        for (Sieve s: sieves) {
+            if (s.matchesDiameter(diameter)) {
+                return s;
             }
         }
-        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);
+        return null;
     }
 
-    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 void deleteSieve(double diameter) {
+        for (int i = sieves.size()-1; i >= 0; --i) {
+            if (sieves.get(i).matchesDiameter(diameter)) {
+                sieves.remove(i);
+                break;
+            }
+        }
     }
 
-    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() {
+    protected void adjustOriginalSieves() {
 
         // If we already have an 8mm diameter sieve
         // we dont need to 'invent' it.
-        if (findSieveIndex(8d) > -1) {
+        if (findSieve(8d) != null) {
             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) {
+        Sieve six  = findSieve(6.3d);
+        Sieve ten  = findSieve(10d);
+        Sieve four = findSieve(4d);
+
+        if (six == null || ten == null || four == null) {
             log.warn("missind diameter");
             return;
         }
 
-        double sixValue  = RSIEB(sixIdx);
-        double tenValue  = RSIEB(tenIdx);
-        double fourValue = RSIEB(fourIdx);
+        double sixValue  = six.getLoad();
+        double tenValue  = ten.getLoad();
+        double fourValue = four.getLoad();
 
-        deleteSieve(sixIdx);
+        deleteSieve(6.3);
 
         double eightValue   = ADD_8 - SCALE_8*sixValue + tenValue;
         double newFourValue = ADD_4 - SCALE_4*sixValue + fourValue;
 
-        putSieve(22, 8d, eightValue);
-        putSieve(fourIdx, 4d, newFourValue);
+        sieves.add(new Sieve(8d, eightValue));
+        sieves.add(new Sieve(4d, newFourValue));
    }
 
 
-    public void fillSieveCategories() {
-        adjustOriginalSieves();
+    public SieveArray calculateSieveArray() {
 
-        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;
-            }
+        SieveArray sa = new SieveArray();
 
-            int idx = sieve(sieb);
-            String quantString = quantString(idx);
-            Double old = (Double)getData(quantString);
-            old = old == null ? 0d : rsieb + old;
-            putData(quantString, old);
+        for (Sieve s: sieves) {
+            sa.doSieving(s);
         }
 
-        double totalQUANT = totalQUANT();
+        sa.calculateNormLoads();
 
-        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);
-            }
-        }
+        return sa;
     }
 }
 // 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	Sun Sep 30 19:03:26 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java	Sun Sep 30 21:15:23 2012 +0200
@@ -1,5 +1,6 @@
 package de.intevation.flys.artifacts.model.sq;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import de.intevation.flys.artifacts.model.DateRange;
@@ -35,6 +36,27 @@
                "gp.MENGE       AS MENGE," +
                "gp.GTRIEB      AS GTRIEB," +
                "m.TGESCHIEBE   AS TGESCHIEBE," +
+               "sie.SIEB01     AS SIEB01," +
+               "sie.SIEB02     AS SIEB02," +
+               "sie.SIEB03     AS SIEB03," +
+               "sie.SIEB04     AS SIEB04," +
+               "sie.SIEB05     AS SIEB05," +
+               "sie.SIEB06     AS SIEB06," +
+               "sie.SIEB07     AS SIEB07," +
+               "sie.SIEB08     AS SIEB08," +
+               "sie.SIEB09     AS SIEB09," +
+               "sie.SIEB10     AS SIEB10," +
+               "sie.SIEB11     AS SIEB11," +
+               "sie.SIEB12     AS SIEB12," +
+               "sie.SIEB13     AS SIEB13," +
+               "sie.SIEB14     AS SIEB14," +
+               "sie.SIEB15     AS SIEB15," +
+               "sie.SIEB16     AS SIEB16," +
+               "sie.SIEB17     AS SIEB17," +
+               "sie.SIEB18     AS SIEB18," +
+               "sie.SIEB19     AS SIEB19," +
+               "sie.SIEB20     AS SIEB20," +
+               "sie.SIEB21     AS SIEB21," +
                "gs.RSIEB01     AS RSIEB01," +
                "gs.RSIEB02     AS RSIEB02," +
                "gs.RSIEB03     AS RSIEB03," +
@@ -56,28 +78,7 @@
                "gs.RSIEB19     AS RSIEB19," +
                "gs.RSIEB20     AS RSIEB20," +
                "gs.RSIEB21     AS RSIEB21," +
-               "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 " +
+               "gs.REST        AS REST " +
         "FROM MESSUNG m " +
             "JOIN STATION    s ON m.STATIONID    = s.STATIONID " +
             "JOIN glotrechte g ON m.MESSUNGID    = g.MESSUNGID " +
@@ -112,15 +113,45 @@
         public MeasurementResultTransformer() {
         }
 
+        private static final int index(String s) {
+            return Integer.parseInt(s.substring(s.length()-2))-1;
+        }
+
         @Override
         public Object transformTuple(Object [] tuple, String [] aliases) {
             Map<String, Object> map = new HashMap<String, Object>();
+
+            Sieve [] sieves = new Sieve[20];
+
+            List<Sieve> validSieves = new ArrayList<Sieve>(20);
+
             for (int i = 0; i < tuple.length; ++i) {
-                if (tuple[i] != null) {
-                    map.put(aliases[i], tuple[i]);
+                Object value = tuple[i];
+                if (value == null) {
+                    continue;
+                }
+                String alias = aliases[i];
+                if (alias.startsWith("SIEB")) {
+                    Sieve s = new Sieve((Double)value, 0d);
+                    sieves[index(alias)] = s;
+                }
+                else if (alias.startsWith("RSIEB")) {
+                    Sieve s = sieves[index(alias)];
+                    if (s != null) {
+                        s.setLoad((Double)value);
+                        validSieves.add(s);
+                    }
+                }
+                else if (alias.equals("REST")) {
+                    Sieve s = new Sieve(0d, (Double)value);
+                    validSieves.add(s);
+                }
+                else {
+                    map.put(alias, value);
                 }
             }
-            return new Measurement(map);
+
+            return new Measurement(map, validSieves);
         }
     } // class BasicTransformerAdapter
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Sieve.java	Sun Sep 30 19:03:26 2012 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Sieve.java	Sun Sep 30 21:15:23 2012 +0200
@@ -1,52 +1,66 @@
 package de.intevation.flys.artifacts.model.sq;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
 public class Sieve
 {
-    public double [] fractionSizes;
+    public static final double EPSILON = 1e-6;
 
-    protected List<List<SQ>> fractions;
+    protected double diameter;
+    protected double load;
 
+    /**
+     * Constructs a new instance.
+     */
     public Sieve() {
-    }
-
-    public Sieve(double [] fractionSizes) {
-        this.fractionSizes = fractionSizes;
-        fractions = new ArrayList<List<SQ>>(fractionSizes.length+1);
-        for (int i = fractionSizes.length; i >= 0; --i) {
-            fractions.add(new ArrayList<SQ>());
-        }
+        this(Double.NaN, Double.NaN);
     }
 
-    public void sieve(Iterator<SQ> sqs) {
-        OUTER: while (sqs.hasNext()) {
-            SQ sq = sqs.next();
-            double q = sq.getQ();
-            for (int i = 0; i < fractionSizes.length; ++i) {
-                if (q < fractionSizes[i]) {
-                    fractions.get(i).add(sq);
-                    continue OUTER;
-                }
-            }
-            fractions.get(fractions.size()-1).add(sq);
-        }
+    public Sieve(double diameter, double load) {
+        this.diameter = diameter;
+        this.load = load;
     }
 
-    public int numFractions() {
-        return fractions.size();
+    /**
+     * Gets the diameter for this instance.
+     *
+     * @return The diameter.
+     */
+    public double getDiameter() {
+        return this.diameter;
     }
 
-    public List<SQ> getFraction(int idx) {
-        return fractions.get(idx);
+    /**
+     * Sets the diameter for this instance.
+     *
+     * @param diameter The diameter.
+     */
+    public void setDiameter(double diameter) {
+        this.diameter = diameter;
     }
 
-    public void reset() {
-        for (List<SQ> fraction: fractions) {
-            fraction.clear();
-        }
+    /**
+     * Gets the load for this instance.
+     *
+     * @return The load.
+     */
+    public double getLoad() {
+        return this.load;
+    }
+
+    /**
+     * Sets the load for this instance.
+     *
+     * @param load The load.
+     */
+    public void setLoad(double load) {
+        this.load = load;
+    }
+
+    public boolean matchesDiameter(double diameter) {
+        return Math.abs(diameter - this.diameter) < EPSILON;
+    }
+
+    public boolean hasDiameter() {
+        return !Double.isNaN(diameter);
     }
 }
 // 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/SieveArray.java	Sun Sep 30 21:15:23 2012 +0200
@@ -0,0 +1,91 @@
+package de.intevation.flys.artifacts.model.sq;
+
+public class SieveArray
+{
+    public static final double EPSILON = 1e-8;
+
+    public static final double [] SIEVE_DIAMETERS = {
+        100d,   63d,  31.5d,    16d,
+          8d,    4d,     2d,     1d,
+        0.5d, 0.25d, 0.125d, 0.063d
+    };
+
+    protected double [] loads;
+    protected double [] normLoads;
+
+    public SieveArray() {
+        loads = new double[SIEVE_DIAMETERS.length+1];
+        normLoads = new double[SIEVE_DIAMETERS.length+1];
+    }
+
+    public void doSieving(Sieve s) {
+
+        double diameter = s.getDiameter();
+
+        for (int i = 0; i < SIEVE_DIAMETERS.length; ++i) {
+            if (diameter >= SIEVE_DIAMETERS[i]) {
+                loads[i] += s.getLoad();
+                return;
+            }
+        }
+        loads[loads.length-1] += s.getLoad();
+    }
+
+    public double totalLoad() {
+        double sum = 0d;
+        for (double load: loads) {
+            sum += load;
+        }
+        return sum;
+    }
+
+    public void calculateNormLoads() {
+        double total = totalLoad();
+        if (Math.abs(total) < EPSILON) {
+            return;
+        }
+        total = 1d/total;
+        for (int i = 0; i < normLoads.length; ++i) {
+            normLoads[i] = total*loads[i];
+        }
+    }
+
+    /**
+     * Gets the loads for this instance.
+     *
+     * @return The loads.
+     */
+    public double[] getLoads() {
+        return this.loads;
+    }
+
+    /**
+     * Gets the loads for this instance.
+     *
+     * @param index The index to get.
+     * @return The loads.
+     */
+    public double getLoads(int index) {
+        return this.loads[index];
+    }
+
+    /**
+     * Gets the normLoads for this instance.
+     *
+     * @return The normLoads.
+     */
+    public double[] getNormLoads() {
+        return this.normLoads;
+    }
+
+    /**
+     * Gets the normLoads for this instance.
+     *
+     * @param index The index to get.
+     * @return The normLoads.
+     */
+    public double getNormLoads(int index) {
+        return this.normLoads[index];
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org