diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java @ 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 d3e2080d3ada
children a9c93b7c9da1
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org