Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java @ 4237:640adb7bced5
Merged.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 24 Oct 2012 14:27:21 +0200 |
parents | b3aa91e45010 |
children | 49cb65d5932d |
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java Wed Oct 24 10:47:24 2012 +0200 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java Wed Oct 24 14:27:21 2012 +0200 @@ -18,24 +18,21 @@ private static final Logger logger = Logger.getLogger(Calculation6.class); - private int mode; - private long[] timerange; + 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 = 1d; - public Calculation6(int mode, long[] timerange, double[] values) { - this.mode = mode; + this.mode = mode; this.timerange = timerange; - this.values = values; + this.values = values; } - public CalculationResult calculate(Gauge gauge) { if (!checkParameters() || gauge == null) { logger.warn("Parameters not valid for calculation."); @@ -47,7 +44,7 @@ debug(); } - DischargeTable refTable = fetchReferenceTable(gauge); + DischargeTable refTable = fetchReferenceTable(gauge); List<DischargeTable> dts = fetchDischargeTables(gauge); int numTables = dts.size(); @@ -65,7 +62,6 @@ return new CalculationResult(wqt, this); } - protected boolean checkParameters() { if (!(mode == MODE_W || mode == MODE_Q)) { logger.warn("Invalid mode '" + mode + "' for calculation."); @@ -85,17 +81,15 @@ return true; } - protected DischargeTable fetchReferenceTable(Gauge gauge) { return gauge.fetchMasterDischargeTable(); } - protected List<DischargeTable> fetchDischargeTables(Gauge gauge) { List<DischargeTable> relevant = new ArrayList<DischargeTable>(); - List<DischargeTable> all = gauge.getDischargeTables(); + List<DischargeTable> all = gauge.getDischargeTables(); - for (DischargeTable dt: all) { + for (DischargeTable dt : all) { if (isDischargeTableRelevant(dt)) { relevant.add(dt); } @@ -104,7 +98,6 @@ return relevant; } - protected boolean isDischargeTableRelevant(DischargeTable dt) { TimeInterval ti = dt.getTimeInterval(); @@ -112,14 +105,14 @@ return false; } - Date start = ti.getStartTime(); + Date start = ti.getStartTime(); long startTime = start.getTime(); if (startTime >= timerange[0] && startTime <= timerange[1]) { return true; } - Date stop = ti.getStopTime(); + Date stop = ti.getStopTime(); long stopTime = stop != null ? stop.getTime() : -1l; if (stopTime >= timerange[0] && stopTime <= timerange[1]) { @@ -131,11 +124,8 @@ return false; } - - protected WQTimerange[] prepareCalculationData( - DischargeTable refTable, - List<DischargeTable> dts - ) { + protected WQTimerange[] prepareCalculationData(DischargeTable refTable, + List<DischargeTable> dts) { if (refTable == null) { addProblem("cannot.find.hist.q.reftable"); return prepareSimpleData(dts); @@ -145,22 +135,20 @@ } } - protected WQTimerange[] prepareSimpleData(List<DischargeTable> dts) { - List<WQTimerange> wqts = - new ArrayList<WQTimerange>(values.length); + List<WQTimerange> wqts = new ArrayList<WQTimerange>(values.length); - for (double value: values) { + for (double value : values) { logger.debug("Prepare data for value: " + value); - String name = mode == MODE_W ? "W=" + value : "Q=" + value; - WQTimerange wqt = null; + String name = mode == MODE_W ? "W=" + value : "Q=" + value; + WQTimerange wqt = null; - for (DischargeTable dt: dts) { - Date[] ti = prepareTimeInterval(dt); + for (DischargeTable dt : dts) { + Date[] ti = prepareTimeInterval(dt); Timerange t = new Timerange(ti[0], ti[1]); - double w; - double q; + double w; + double q; if (mode == MODE_W) { w = value; @@ -174,7 +162,7 @@ } else { q = value; - w = findValueForQ(dt, q); + w = findValueForQ(dt, q, DischargeTables.HISTORICAL_SCALE); } logger.debug("Q=" + q + " | W=" + w); @@ -194,43 +182,42 @@ return wqts.toArray(new WQTimerange[wqts.size()]); } + protected HistoricalWQTimerange[] prepareData(DischargeTable refTable, + List<DischargeTable> dts) { + List<HistoricalWQTimerange> wqts = new ArrayList<HistoricalWQTimerange>( + values.length); - protected HistoricalWQTimerange[] prepareData( - DischargeTable refTable, - List<DischargeTable> dts - ) { - List<HistoricalWQTimerange> wqts = - new ArrayList<HistoricalWQTimerange>(values.length); - - for (double value: values) { + for (double value : values) { logger.debug("Prepare data plus diff for value: " + value); String name = mode == MODE_W ? "W=" + value : "Q=" + value; - HistoricalWQTimerange wqt = null; + HistoricalWQTimerange wqt = null; double ref; double diff; if (refTable != null && mode == MODE_W) { - ref = findValueForW(refTable, value, DischargeTables.MASTER_SCALE); + ref = findValueForW(refTable, value, + DischargeTables.MASTER_SCALE); } else if (refTable != null) { - ref = findValueForQ(refTable, value); + ref = findValueForQ(refTable, value, + DischargeTables.MASTER_SCALE); } else { ref = Double.NaN; } - for (DischargeTable dt: dts) { + for (DischargeTable dt : dts) { Date[] ti = prepareTimeInterval(dt); - Timerange t = new Timerange(ti[0] ,ti[1]); - double w; - double q; + Timerange t = new Timerange(ti[0], ti[1]); + double w; + double q; if (mode == MODE_W) { - w = value; - q = findValueForW(dt, w, DischargeTables.HISTORICAL_SCALE); + w = value; + q = findValueForW(dt, w, DischargeTables.HISTORICAL_SCALE); if (Double.isNaN(q)) { logger.warn("Cannot find Q for W: " + w); @@ -238,12 +225,18 @@ continue; } - diff = ref-q; + diff = ref - q; } else { - q = value; - w = findValueForQ(dt, q); - diff = ref-w; + q = value; + w = findValueForQ(dt, q, DischargeTables.HISTORICAL_SCALE); + + if (Double.isNaN(w)) { + logger.warn("Cannot find W for Q: " + q); + addProblem("cannot.find.hist.w.for.q", q, ti[0], ti[1]); + continue; + } + diff = ref - w; } logger.debug("Q=" + q + " | W=" + w + " | Ref = " + ref); @@ -260,16 +253,15 @@ } } - return (HistoricalWQTimerange[]) - wqts.toArray(new HistoricalWQTimerange[wqts.size()]); + return (HistoricalWQTimerange[]) wqts + .toArray(new HistoricalWQTimerange[wqts.size()]); } - protected Date[] prepareTimeInterval(DischargeTable dt) { TimeInterval ti = dt.getTimeInterval(); Date start = ti.getStartTime(); - Date end = ti.getStopTime(); + Date end = ti.getStopTime(); if (end == null) { logger.warn("TimeInterval has no stop time set!"); @@ -280,28 +272,25 @@ return new Date[] { start, end }; } - protected double findValueForW(DischargeTable dt, double w, double scale) { double[][] vs = DischargeTables.loadDischargeTableValues(dt, scale); - double [] qs = DischargeTables.getQsForW(vs, w); + double[] qs = DischargeTables.getQsForW(vs, w); return qs.length == 0 ? Double.NaN : qs[0]; } + protected double findValueForQ(DischargeTable dt, double q, double scale) { + double[][] vs = DischargeTables.loadDischargeTableValues(dt, scale); + double[] ws = DischargeTables.getWsForQ(vs, q); - protected double findValueForQ(DischargeTable dt, double q) { - double[][] vs = DischargeTables.loadDischargeTableValues(dt, SCALE); - logger.warn("TODO: IMPLEMENT ME!"); - - return 10; + return ws.length == 0 ? Double.NaN : ws[0]; } - /** * Writes the parameters used for this calculation to logger. */ public void debug() { StringBuilder sb = new StringBuilder(); - for (double value: values) { + for (double value : values) { sb.append(String.valueOf(value) + " "); }