ingo@2215: package de.intevation.flys.artifacts.model;
ingo@2215:
ingo@2228: import java.util.ArrayList;
ingo@2228: import java.util.Date;
ingo@2228: import java.util.List;
ingo@2228:
ingo@2215: import org.apache.log4j.Logger;
ingo@2215:
ingo@2228: import de.intevation.flys.model.DischargeTable;
ingo@2219: import de.intevation.flys.model.Gauge;
ingo@2228: import de.intevation.flys.model.TimeInterval;
ingo@2219:
ingo@2215:
ingo@2215: /**
ingo@2215: * @author Ingo Weinzierl
ingo@2215: */
ingo@2215: public class Calculation6 extends Calculation {
ingo@2215:
ingo@2215: private static final Logger logger = Logger.getLogger(Calculation6.class);
ingo@2215:
ingo@2219: private int mode;
ingo@2228: private long[] timerange;
ingo@2219: private double[] values;
ingo@2215:
ingo@2219:
ingo@2219: public static final int MODE_W = 0;
ingo@2219: public static final int MODE_Q = 1;
ingo@2219:
ingo@2230: public static final double SCALE = 100d;
ingo@2230:
ingo@2219:
ingo@2228: public Calculation6(int mode, long[] timerange, double[] values) {
ingo@2219: this.mode = mode;
ingo@2219: this.timerange = timerange;
ingo@2219: this.values = values;
ingo@2215: }
ingo@2215:
ingo@2215:
ingo@2219: public CalculationResult calculate(Gauge gauge) {
ingo@2219: if (!checkParameters() || gauge == null) {
ingo@2219: logger.warn("Parameters not valid for calculation.");
ingo@2219:
ingo@2219: return null;
ingo@2219: }
ingo@2219:
ingo@2219: if (logger.isDebugEnabled()) {
ingo@2219: debug();
ingo@2219: }
ingo@2219:
ingo@2228: List dts = fetchDischargeTables(gauge);
ingo@2219:
ingo@2228: logger.debug("Take " + dts.size() + " into account.");
ingo@2228:
ingo@2228: WQTimerange[] wqt = prepareCalculationData(dts);
ingo@2228:
ingo@2228: return new CalculationResult(wqt, this);
ingo@2215: }
ingo@2219:
ingo@2219:
ingo@2219: protected boolean checkParameters() {
ingo@2219: if (!(mode == MODE_W || mode == MODE_Q)) {
ingo@2219: logger.warn("Invalid mode '" + mode + "' for calculation.");
ingo@2219: return false;
ingo@2219: }
ingo@2219:
ingo@2219: if (timerange == null || timerange.length < 2) {
ingo@2219: logger.warn("Invalid timerange for calculation.");
ingo@2219: return false;
ingo@2219: }
ingo@2219:
ingo@2219: if (values == null || values.length == 0) {
ingo@2219: logger.warn("No values for W or Q specified.");
ingo@2219: return false;
ingo@2219: }
ingo@2219:
ingo@2219: return true;
ingo@2219: }
ingo@2219:
ingo@2219:
ingo@2228: protected List fetchDischargeTables(Gauge gauge) {
ingo@2228: List relevant = new ArrayList();
ingo@2228: List all = gauge.getDischargeTables();
ingo@2228:
ingo@2228: for (DischargeTable dt: all) {
ingo@2228: if (isDischargeTableRelevant(dt)) {
ingo@2228: relevant.add(dt);
ingo@2228: }
ingo@2228: }
ingo@2228:
ingo@2228: return relevant;
ingo@2228: }
ingo@2228:
ingo@2228:
ingo@2228: protected boolean isDischargeTableRelevant(DischargeTable dt) {
ingo@2228: TimeInterval ti = dt.getTimeInterval();
ingo@2228:
ingo@2228: Date start = ti.getStartTime();
ingo@2228: long startTime = start.getTime();
ingo@2228:
ingo@2228: if (startTime >= timerange[0] && startTime <= timerange[1]) {
ingo@2228: return true;
ingo@2228: }
ingo@2228:
ingo@2228: Date stop = ti.getStopTime();
ingo@2228: long stopTime = stop != null ? stop.getTime() : -1l;
ingo@2228:
ingo@2228: if (stopTime >= timerange[0] && stopTime <= timerange[1]) {
ingo@2228: return true;
ingo@2228: }
ingo@2228:
ingo@2228: logger.debug("DischargeTable not in range: " + start + " -> " + stop);
ingo@2228:
ingo@2228: return false;
ingo@2228: }
ingo@2228:
ingo@2228:
ingo@2228: protected WQTimerange[] prepareCalculationData(List dts) {
ingo@2228: List wqts = new ArrayList(values.length);
ingo@2228:
ingo@2230: boolean debug = logger.isDebugEnabled();
ingo@2230:
ingo@2228: for (double value: values) {
ingo@2228: logger.debug("Prepare data for value: " + value);
ingo@2228:
ingo@2228: String name = mode == MODE_W ? "W=" + value : "Q=" + value;
ingo@2228: WQTimerange wqt = new WQTimerange(name);
ingo@2228:
ingo@2228: for (DischargeTable dt: dts) {
ingo@2228: TimeInterval ti = dt.getTimeInterval();
ingo@2228:
ingo@2228: Timerange t = new Timerange(ti.getStartTime(),ti.getStopTime());
ingo@2228: double w;
ingo@2228: double q;
ingo@2228:
ingo@2228: if (mode == MODE_W) {
ingo@2228: w = value;
ingo@2228: q = findValueForW(dt, w);
ingo@2228: }
ingo@2228: else {
ingo@2228: q = value;
ingo@2228: w = findValueForQ(dt, q);
ingo@2228: }
ingo@2228:
ingo@2230: if (debug) {
ingo@2230: logger.debug("Q=" + q + " | W=" + w);
ingo@2230: }
ingo@2230:
ingo@2228: wqt.add(w, q, t);
ingo@2228: }
ingo@2228:
ingo@2228: wqts.add(wqt);
ingo@2228: }
ingo@2228:
ingo@2228: return (WQTimerange[]) wqts.toArray(new WQTimerange[wqts.size()]);
ingo@2228: }
ingo@2228:
ingo@2228:
ingo@2228: protected double findValueForW(DischargeTable dt, double w) {
ingo@2230: double[][] vs = DischargeTables.loadDischargeTableValues(dt,SCALE,true);
ingo@2230: return DischargeTables.getQForW(vs, w);
ingo@2228: }
ingo@2228:
ingo@2228:
ingo@2228: protected double findValueForQ(DischargeTable dt, double q) {
ingo@2230: double[][] vs = DischargeTables.loadDischargeTableValues(dt,SCALE,true);
ingo@2228: logger.warn("TODO: IMPLEMENT ME!");
ingo@2230:
ingo@2228: return 10;
ingo@2228: }
ingo@2228:
ingo@2228:
ingo@2219: /**
ingo@2219: * Writes the parameters used for this calculation to logger.
ingo@2219: */
ingo@2219: public void debug() {
ingo@2219: StringBuilder sb = new StringBuilder();
ingo@2219: for (double value: values) {
ingo@2219: sb.append(String.valueOf(value) + " ");
ingo@2219: }
ingo@2219:
ingo@2219: logger.debug("========== Calculation6 ==========");
ingo@2219: logger.debug(" Mode: " + mode);
ingo@2219: logger.debug(" Timerange: " + timerange[0] + " - " + timerange[1]);
ingo@2219: logger.debug(" Input values: " + sb.toString());
ingo@2219: logger.debug("==================================");
ingo@2219: }
ingo@2215: }
ingo@2215: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :