# HG changeset patch # User Ingo Weinzierl # Date 1328107680 0 # Node ID 4db19a88bddbbf456ffc3041882c475b7d33f030 # Parent 2e6f0ef36352b35fbb6feef8e9cea208c10863f8 Implemented the first step of Calculation6; create Facets for each calculation results. flys-artifacts/trunk@3868 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 2e6f0ef36352 -r 4db19a88bddb flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Wed Feb 01 14:43:50 2012 +0000 +++ b/flys-artifacts/ChangeLog Wed Feb 01 14:48:00 2012 +0000 @@ -1,3 +1,17 @@ +2012-02-01 Ingo Weinzierl + + * src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Create time + millis for timerange used in Calculation6. + + * src/main/java/de/intevation/flys/artifacts/model/Calculation6.java: Added + the first step of the calculation: fetch relevant discharge tables based + on the given input parameters. The calculated values are not the right + ones! Finding Ws and Qs in discharge tables is still not implemented! WORK + IN PROGRESS! + + * src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java: + Create facets for each WQTimerange object calculated by Calculation6. + 2012-02-01 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/model/Timerange.java: Added a diff -r 2e6f0ef36352 -r 4db19a88bddb flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java Wed Feb 01 14:43:50 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java Wed Feb 01 14:48:00 2012 +0000 @@ -58,7 +58,9 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Collections; +import java.util.GregorianCalendar; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -746,7 +748,15 @@ int[] timerange = FLYSUtils.intArrayFromString(rawTimerange); double[] values = FLYSUtils.doubleArrayFromString(rawValues); - return new Calculation6(mode, timerange, values).calculate(gauge); + Calendar start = new GregorianCalendar(timerange[0], 0, 1); + Calendar end = new GregorianCalendar(timerange[1], 0, 1); + + Calculation6 calc = new Calculation6( + mode, + new long[] { start.getTimeInMillis(), end.getTimeInMillis() }, + values); + + return calc.calculate(gauge); } diff -r 2e6f0ef36352 -r 4db19a88bddb flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java Wed Feb 01 14:43:50 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation6.java Wed Feb 01 14:48:00 2012 +0000 @@ -1,8 +1,14 @@ 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; /** @@ -13,7 +19,7 @@ private static final Logger logger = Logger.getLogger(Calculation6.class); private int mode; - private int[] timerange; + private long[] timerange; private double[] values; @@ -21,7 +27,7 @@ public static final int MODE_Q = 1; - public Calculation6(int mode, int[] timerange, double[] values) { + public Calculation6(int mode, long[] timerange, double[] values) { this.mode = mode; this.timerange = timerange; this.values = values; @@ -39,9 +45,13 @@ debug(); } - logger.warn("TODO: IMPLEMENT ME"); + List dts = fetchDischargeTables(gauge); - return null; + logger.debug("Take " + dts.size() + " into account."); + + WQTimerange[] wqt = prepareCalculationData(dts); + + return new CalculationResult(wqt, this); } @@ -65,6 +75,90 @@ } + protected List fetchDischargeTables(Gauge gauge) { + List relevant = new ArrayList(); + List 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 dts) { + List wqts = new ArrayList(values.length); + + for (double value: values) { + logger.debug("Prepare data for value: " + value); + + String name = mode == MODE_W ? "W=" + value : "Q=" + value; + WQTimerange wqt = new WQTimerange(name); + + for (DischargeTable dt: dts) { + TimeInterval ti = dt.getTimeInterval(); + + Timerange t = new Timerange(ti.getStartTime(),ti.getStopTime()); + double w; + double q; + + if (mode == MODE_W) { + w = value; + q = findValueForW(dt, w); + } + else { + q = value; + w = findValueForQ(dt, q); + } + + wqt.add(w, q, t); + } + + wqts.add(wqt); + } + + return (WQTimerange[]) wqts.toArray(new WQTimerange[wqts.size()]); + } + + + protected double findValueForW(DischargeTable dt, double w) { + logger.warn("TODO: IMPLEMENT ME!"); + return 5; + } + + + protected double findValueForQ(DischargeTable dt, double q) { + logger.warn("TODO: IMPLEMENT ME!"); + return 10; + } + + /** * Writes the parameters used for this calculation to logger. */ diff -r 2e6f0ef36352 -r 4db19a88bddb flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java Wed Feb 01 14:43:50 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java Wed Feb 01 14:48:00 2012 +0000 @@ -18,6 +18,7 @@ import de.intevation.flys.artifacts.model.CalculationResult; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.HistoricalDischargeFacet; +import de.intevation.flys.artifacts.model.WQTimerange; /** @@ -64,14 +65,31 @@ return res; } - logger.debug("TODO: IMPLEMENT ME!"); + WQTimerange[] data = (WQTimerange[]) res.getData(); + if (data == null || data.length == 0) { + logger.warn("Historical Discharge calculation has no results!"); + return res; + } - HistoricalDischargeFacet facet = new HistoricalDischargeFacet( - 0, HISTORICAL_DISCHARGE_Q, "THE NEW HIST Q FACET DESC"); - - facets.add(facet); + prepareFacets(facets, data); return res; } + + + protected void prepareFacets(List facets, WQTimerange[] wqts) { + int i = 0; + + for (WQTimerange wqt: wqts) { + logger.debug("Prepare facet for: " + wqt.getName()); + + // TODO CREATE BETTER TITLE FOR FACETS + + facets.add(new HistoricalDischargeFacet( + i++, + HISTORICAL_DISCHARGE_Q, + wqt.getName())); + } + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :