view 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 source
package de.intevation.flys.artifacts.model.sq;

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;
    protected Measurement next;

    public Measurement() {
    }

    public Measurement(Map<String, Object> data) {
        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");
    }

    public double S_SF() {
        return get("TSCHWEB") - get("TSAND");
    }

    public double Q() {
        return get("Q_BPEGEL");
    }

    public double TOTAL_BL() {
        return get("TGESCHIEBE");
    }

    public double BL_G() {
        // TODO: Implement me!
        return Double.NaN;
    }

    public double BL_C() {
        // TODO: Implement me!
        return Double.NaN;
    }

    public double BL_S() {
        // TODO: Implement me!
        return Double.NaN;
    }

    public double S_BL_S() {
        return TOTAL_BL() * BL_S();
    }

    public double S_BL_FG() {
        return TOTAL_BL() * BL_G();
    }

    public double S_BL_CG() {
        return TOTAL_BL() * BL_C();
    }

    public double S_BL_1() {
        return S_BL_S() + S_BL_FG() + S_BL_CG();
    }

    public double S_BL_2() {
        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 prev for this instance.
     *
     * @return The prev.
     */
    public Measurement getPrev() {
        return this.prev;
    }

    /**
     * Sets the prev for this instance.
     *
     * @param prev The prev.
     */
    public void setPrev(Measurement prev) {
        this.prev = prev;
    }

    /**
     * Gets the next for this instance.
     *
     * @return The next.
     */
    public Measurement getNext() {
        return this.next;
    }

    /**
     * Sets the next for this instance.
     *
     * @param next The next.
     */
    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