view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java @ 2279:6eb6162f2cd2

Fix i18n flys-artifacts/trunk@3941 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 06 Feb 2012 16:12:48 +0000
parents 40608c82e9cb
children abb390101595
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import de.intevation.flys.model.DischargeTable;
import de.intevation.flys.model.Gauge;
import de.intevation.flys.model.TimeInterval;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class Calculation6 extends Calculation {

    private static final Logger logger = Logger.getLogger(Calculation6.class);

    private int      mode;
    private long[]   timerange;
    private double[] values;


    public static final int MODE_W = 0;
    public static final int MODE_Q = 1;

    public static final double SCALE = 100d;


    public Calculation6(int mode, long[] timerange, double[] values) {
        this.mode      = mode;
        this.timerange = timerange;
        this.values    = values;
    }


    public CalculationResult calculate(Gauge gauge) {
        if (!checkParameters() || gauge == null) {
            logger.warn("Parameters not valid for calculation.");

            return null;
        }

        if (logger.isDebugEnabled()) {
            debug();
        }

        List<DischargeTable> dts = fetchDischargeTables(gauge);

        logger.debug("Take " + dts.size() + " into account.");

        WQTimerange[] wqt = prepareCalculationData(dts);

        logger.debug("Number of calculation results: " + wqt.length);

        return new CalculationResult(wqt, this);
    }


    protected boolean checkParameters() {
        if (!(mode == MODE_W || mode == MODE_Q)) {
            logger.warn("Invalid mode '" + mode + "' for calculation.");
            return false;
        }

        if (timerange == null || timerange.length < 2) {
            logger.warn("Invalid timerange for calculation.");
            return false;
        }

        if (values == null || values.length == 0) {
            logger.warn("No values for W or Q specified.");
            return false;
        }

        return true;
    }


    protected List<DischargeTable> fetchDischargeTables(Gauge gauge) {
        List<DischargeTable> relevant = new ArrayList<DischargeTable>();
        List<DischargeTable> all      = gauge.getDischargeTables();

        for (DischargeTable dt: all) {
            if (isDischargeTableRelevant(dt)) {
                relevant.add(dt);
            }
        }

        return relevant;
    }


    protected boolean isDischargeTableRelevant(DischargeTable dt) {
        TimeInterval ti = dt.getTimeInterval();

        Date start     = ti.getStartTime();
        long startTime = start.getTime();

        if (startTime >= timerange[0] && startTime <= timerange[1]) {
            return true;
        }

        Date stop     = ti.getStopTime();
        long stopTime = stop != null ? stop.getTime() : -1l;

        if (stopTime >= timerange[0] && stopTime <= timerange[1]) {
            return true;
        }

        logger.debug("DischargeTable not in range: " + start + " -> " + stop);

        return false;
    }


    protected WQTimerange[] prepareCalculationData(List<DischargeTable> dts) {
        List<WQTimerange> wqts = new ArrayList<WQTimerange>(values.length);

        boolean debug = logger.isDebugEnabled();

        for (double value: values) {
            logger.debug("Prepare data for value: " + value);

            String      name = mode == MODE_W ? "W=" + value : "Q=" + value;
            WQTimerange wqt  = null;

            for (DischargeTable dt: dts) {
                TimeInterval ti = dt.getTimeInterval();

                Date start = ti.getStartTime();
                Date end   = ti.getStopTime();

                if (end == null) {
                    logger.warn("TimeInterval has no stop time set!");

                    end = new Date();
                }

                Timerange t = new Timerange(ti.getStartTime(),ti.getStopTime());
                double    w;
                double    q;

                if (mode == MODE_W) {
                    w = value;
                    q = findValueForW(dt, w);

                    if (Double.isNaN(q)) {
                        logger.warn("Cannot find Q for W: " + w);
                        addProblem("cannot.find.hist.q.for.w", w, start, end);
                        continue;
                    }
                }
                else {
                    q = value;
                    w = findValueForQ(dt, q);
                }

                if (debug) {
                    logger.debug("Q=" + q + " | W=" + w);
                }

                if (wqt == null) {
                    wqt = new WQTimerange(name);
                }

                wqt.add(w, q, t);
            }

            if (wqt != null) {
                wqts.add(wqt);
            }
        }

        return (WQTimerange[]) wqts.toArray(new WQTimerange[wqts.size()]);
    }


    protected double findValueForW(DischargeTable dt, double w) {
        double[][] vs = DischargeTables.loadDischargeTableValues(dt,SCALE,true);
        return DischargeTables.getQForW(vs, w);
    }


    protected double findValueForQ(DischargeTable dt, double q) {
        double[][] vs = DischargeTables.loadDischargeTableValues(dt,SCALE,true);
        logger.warn("TODO: IMPLEMENT ME!");

        return 10;
    }


    /**
     * Writes the parameters used for this calculation to logger.
     */
    public void debug() {
        StringBuilder sb = new StringBuilder();
        for (double value: values) {
            sb.append(String.valueOf(value) + " ");
        }

        logger.debug("========== Calculation6 ==========");
        logger.debug("   Mode:         " + mode);
        logger.debug("   Timerange:    " + timerange[0] + " - " + timerange[1]);
        logger.debug("   Input values: " + sb.toString());
        logger.debug("==================================");
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org