[Dive4elements-commits] [PATCH] D4E river artifacts: Removed historical discharge calculation from WINFO artifact and use access instead
Wald Commits
scm-commit at wald.intevation.org
Tue May 7 17:32:39 CEST 2013
# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1367940753 -7200
# Node ID 1058b3436dadd20e9683eb11bbe3ed5b00b2d76b
# Parent 37a0f4f7c54f63f55e8fa23ea31c8c3db6ef14ad
D4E river artifacts: Removed historical discharge calculation from WINFO artifact and use access instead.
diff -r 37a0f4f7c54f -r 1058b3436dad artifacts/src/main/java/org/dive4elements/river/artifacts/WINFOArtifact.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/WINFOArtifact.java Tue May 07 12:47:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/WINFOArtifact.java Tue May 07 17:32:33 2013 +0200
@@ -28,7 +28,6 @@
import org.dive4elements.river.artifacts.model.Calculation3;
import org.dive4elements.river.artifacts.model.Calculation4;
import org.dive4elements.river.artifacts.model.Calculation5;
-import org.dive4elements.river.artifacts.model.Calculation6;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.DischargeTables;
@@ -536,29 +535,6 @@
}
- public CalculationResult getHistoricalDischargeData() {
- Gauge gauge = RiverUtils.getReferenceGauge(this);
- String rawTimerange = getDataAsString("year_range");
- String rawValues = getDataAsString("historical_values");
- int mode = getDataAsInteger("historical_mode");
-
- long[] timerange = RiverUtils.longArrayFromString(rawTimerange);
- double[] values = RiverUtils.doubleArrayFromString(rawValues);
-
- Calendar start = new GregorianCalendar();
- start.setTimeInMillis(timerange[0]);
- Calendar end = new GregorianCalendar();
- end.setTimeInMillis(timerange[1]);
-
- Calculation6 calc = new Calculation6(
- mode,
- new long[] { start.getTimeInMillis(), end.getTimeInMillis() },
- values);
-
- return calc.calculate(gauge);
- }
-
-
/**
* Get corrected waterline against surface/profile.
*/
@@ -574,8 +550,6 @@
// Find W/C at km, linear naive approach.
WQCKms triple = (WQCKms) wqckms[idx-1];
- int old_idx = 0;
-
if (triple.size() == 0) {
logger.warn("Calculation of c/waterline is empty.");
return Lines.createWaterLines(points, 0.0f);
diff -r 37a0f4f7c54f -r 1058b3436dad artifacts/src/main/java/org/dive4elements/river/artifacts/access/HistoricalDischargeAccess.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/access/HistoricalDischargeAccess.java Tue May 07 12:47:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/HistoricalDischargeAccess.java Tue May 07 17:32:33 2013 +0200
@@ -17,12 +17,23 @@
public class HistoricalDischargeAccess extends RiverAccess {
public static enum EvaluationMode {
- W, Q
+ W(0), Q(1);
+
+ private final int mode;
+
+ EvaluationMode(int mode) {
+ this.mode = mode;
+ }
+
+ public int getMode() {
+ return mode;
+ }
}
public static final String DATA_EVALUATION_TIME = "year_range";
public static final String DATA_EVALUATION_MODE = "historical_mode";
- public static final String DATA_INPUT_VALUES = "historical_values";
+ public static final String DATA_INPUT_VALUES = "historical_values";
+ public static final String DATA_REFERENCE_GAUGE = "reference_gauge";
private Timerange evaluationTimerange;
private EvaluationMode evaluationMode;
@@ -30,6 +41,8 @@
private double[] qs;
private double[] ws;
+ private Long officialGaugeNumber;
+
public HistoricalDischargeAccess(D4EArtifact artifact) {
super(artifact);
}
@@ -51,6 +64,7 @@
return evaluationMode;
}
+
/**
* This method returns the time range specified by <i>year_range</i>
* parameter. This parameter has to be a string that consists of two long
@@ -67,6 +81,7 @@
Date end = new Date(startend[1]);
evaluationTimerange = new Timerange(start, end);
+ evaluationTimerange.sort();
}
}
@@ -114,4 +129,11 @@
return ws;
}
+
+ public Long getOfficialGaugeNumber() {
+ if (officialGaugeNumber == null) {
+ officialGaugeNumber = getLong(DATA_REFERENCE_GAUGE);
+ }
+ return officialGaugeNumber;
+ }
}
diff -r 37a0f4f7c54f -r 1058b3436dad artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation6.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation6.java Tue May 07 12:47:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation6.java Tue May 07 17:32:33 2013 +0200
@@ -19,6 +19,9 @@
import org.dive4elements.river.model.Gauge;
import org.dive4elements.river.model.TimeInterval;
+import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess;
+import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess.EvaluationMode;
+
/**
* Historical Discharge Calculation.
@@ -26,31 +29,84 @@
*/
public class Calculation6 extends Calculation {
- private static final Logger logger = Logger.getLogger(Calculation6.class);
+ private static final Logger log = Logger.getLogger(Calculation6.class);
- private int mode;
- private long[] timerange;
- private double[] values;
+ private int mode;
+ private long [] timerange;
+ private double [] values;
+ private Long officialGaugeNumber;
- public static final int MODE_W = 0;
- public static final int MODE_Q = 1;
public static final double SCALE = 1d;
+ public Calculation6(HistoricalDischargeAccess access) {
+ EvaluationMode mode = access.getEvaluationMode();
+ Timerange tr = access.getEvaluationTimerange();
+ double [] vs = mode != null && mode == EvaluationMode.W
+ ? access.getWs()
+ : access.getQs();
+
+ Long officialGaugeNumber = access.getOfficialGaugeNumber();
+
+ if (mode == null) {
+ // TODO: i18n
+ addProblem("hist.discharge.mode.not.set");
+ }
+ if (tr == null) {
+ // TODO: i18n
+ addProblem("hist.discharge.time.interval.not.set");
+ }
+ if (vs == null || vs.length == 0) {
+ // TODO: i18n
+ addProblem("hist.discharge.values.not.set");
+ }
+
+ if (officialGaugeNumber == null) {
+ // TODO: i18n
+ addProblem("hist.discharge.reference.gauge.not.set");
+ }
+
+ if (!hasProblems()) {
+ set(
+ mode.getMode(),
+ new long [] { tr.getStart(), tr.getEnd()},
+ values,
+ officialGaugeNumber);
+ }
+ }
+
+ protected void set(
+ int mode,
+ long [] timerange,
+ double [] values,
+ Long officialGaugeNumber
+ ) {
+ this.mode = mode;
+ this.timerange = timerange;
+ this.values = values;
+ this.officialGaugeNumber = officialGaugeNumber;
+ }
+
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.");
-
+ public CalculationResult calculate() {
+ if (hasProblems()) {
+ log.warn("Parameters not valid for calculation.");
return null;
}
- if (logger.isDebugEnabled()) {
+ Gauge gauge = Gauge.getGaugeByOfficialNumber(officialGaugeNumber);
+ if (gauge == null) {
+ // TODO: i18n
+ addProblem("hist.discharge.gauge.not.found");
+ return null;
+ }
+
+ if (log.isDebugEnabled()) {
debug();
}
@@ -59,7 +115,7 @@
int numTables = dts.size();
- logger.debug("Take " + numTables + " into account.");
+ log.debug("Take " + numTables + " into account.");
if (numTables == 0) {
addProblem("cannot.find.hist.q.tables");
@@ -68,31 +124,12 @@
WQTimerange[] wqt = prepareTimerangeData(refTable, dts);
WQKms[] wqs = prepareWQData(dts);
- logger.debug("Number of calculation results: " + wqt.length);
+ log.debug("Number of calculation results: " + wqt.length);
return new CalculationResult(new HistoricalDischargeData(wqt, wqs),
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 DischargeTable fetchReferenceTable(Gauge gauge) {
return gauge.fetchMasterDischargeTable();
}
@@ -132,7 +169,7 @@
return true;
}
- logger.debug("DischargeTable not in range: " + start + " -> " + stop);
+ log.debug("DischargeTable not in range: " + start + " -> " + stop);
return false;
}
@@ -188,9 +225,12 @@
List<WQTimerange> wqts = new ArrayList<WQTimerange>(values.length);
for (double value : values) {
- logger.debug("Prepare data for value: " + value);
+ log.debug("Prepare data for value: " + value);
- String name = mode == MODE_W ? "W=" + value : "Q=" + value;
+ String name = mode == EvaluationMode.W.getMode()
+ ? "W=" + value
+ : "Q=" + value;
+
WQTimerange wqt = null;
for (DischargeTable dt : dts) {
@@ -199,12 +239,12 @@
double w;
double q;
- if (mode == MODE_W) {
+ if (mode == EvaluationMode.W.getMode()) {
w = value;
q = findValueForW(dt, w, DischargeTables.HISTORICAL_SCALE);
if (Double.isNaN(q)) {
- logger.warn("Cannot find Q for W: " + w);
+ log.warn("Cannot find Q for W: " + w);
addProblem("cannot.find.hist.q.for.w", w, ti[0], ti[1]);
continue;
}
@@ -214,7 +254,7 @@
w = findValueForQ(dt, q, DischargeTables.HISTORICAL_SCALE);
}
- logger.debug("Q=" + q + " | W=" + w);
+ log.debug("Q=" + q + " | W=" + w);
if (wqt == null) {
wqt = new WQTimerange(name);
@@ -238,15 +278,17 @@
values.length);
for (double value : values) {
- logger.debug("Prepare data plus diff for value: " + value);
+ log.debug("Prepare data plus diff for value: " + value);
- String name = mode == MODE_W ? "W=" + value : "Q=" + value;
+ String name = mode == EvaluationMode.W.getMode()
+ ? "W=" + value
+ : "Q=" + value;
HistoricalWQTimerange wqt = null;
double ref;
double diff;
- if (refTable != null && mode == MODE_W) {
+ if (refTable != null && mode == EvaluationMode.W.getMode()) {
ref = findValueForW(refTable, value,
DischargeTables.MASTER_SCALE);
}
@@ -265,12 +307,12 @@
double w;
double q;
- if (mode == MODE_W) {
+ if (mode == EvaluationMode.W.getMode()) {
w = value;
q = findValueForW(dt, w, DischargeTables.HISTORICAL_SCALE);
if (Double.isNaN(q)) {
- logger.warn("Cannot find Q for W: " + w);
+ log.warn("Cannot find Q for W: " + w);
addProblem("cannot.find.hist.q.for.w", w, ti[0], ti[1]);
continue;
}
@@ -282,14 +324,14 @@
w = findValueForQ(dt, q, DischargeTables.HISTORICAL_SCALE);
if (Double.isNaN(w)) {
- logger.warn("Cannot find W for Q: " + q);
+ log.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);
+ log.debug("Q=" + q + " | W=" + w + " | Ref = " + ref);
if (wqt == null) {
wqt = new HistoricalWQTimerange(name);
@@ -315,7 +357,7 @@
Date end = ti.getStopTime();
if (end == null) {
- logger.warn("TimeInterval has no stop time set!");
+ log.warn("TimeInterval has no stop time set!");
end = new Date();
}
@@ -337,19 +379,14 @@
}
/**
- * Writes the parameters used for this calculation to logger.
+ * Writes the parameters used for this calculation to log.
*/
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("==================================");
+ log.debug("========== Calculation6 ==========");
+ log.debug(" Mode: " + mode);
+ log.debug(" Timerange: " + timerange[0] + " - " + timerange[1]);
+ log.debug(" Input values: " + Arrays.toString(values));
+ log.debug("==================================");
}
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 37a0f4f7c54f -r 1058b3436dad artifacts/src/main/java/org/dive4elements/river/artifacts/model/Timerange.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Timerange.java Tue May 07 12:47:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Timerange.java Tue May 07 17:32:33 2013 +0200
@@ -45,5 +45,11 @@
public long getEnd() {
return end;
}
+
+ public void sort() {
+ if (start > end) {
+ long t = start; start = end; end = t;
+ }
+ }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 37a0f4f7c54f -r 1058b3436dad artifacts/src/main/java/org/dive4elements/river/artifacts/states/HistoricalDischargeComputeState.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/HistoricalDischargeComputeState.java Tue May 07 12:47:05 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/HistoricalDischargeComputeState.java Tue May 07 17:32:33 2013 +0200
@@ -13,16 +13,21 @@
import java.util.List;
import org.apache.log4j.Logger;
-import org.w3c.dom.Element;
import org.dive4elements.artifactdatabase.state.Facet;
+
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
+
import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
+
import org.dive4elements.river.artifacts.D4EArtifact;
-import org.dive4elements.river.artifacts.WINFOArtifact;
+
+import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess.EvaluationMode;
+
import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess;
-import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess.EvaluationMode;
+
+import org.dive4elements.river.artifacts.model.Calculation6;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.DataFacet;
import org.dive4elements.river.artifacts.model.FacetTypes;
@@ -36,9 +41,12 @@
import org.dive4elements.river.artifacts.model.ReportFacet;
import org.dive4elements.river.artifacts.model.WQKms;
import org.dive4elements.river.artifacts.model.WQTimerange;
+
import org.dive4elements.river.artifacts.resources.Resources;
+
import org.dive4elements.river.model.TimeInterval;
+import org.w3c.dom.Element;
/**
* State to calculate historical discharge curves.
@@ -46,9 +54,9 @@
* @author <a href="mailto:ingo.weinzierl at intevation.de">Ingo Weinzierl</a>
*/
public class HistoricalDischargeComputeState
-extends DefaultState
-implements FacetTypes {
-
+extends DefaultState
+implements FacetTypes
+{
private static final Logger logger = Logger
.getLogger(HistoricalDischargeComputeState.class);
@@ -67,10 +75,12 @@
CallContext context, List<Facet> facets, Object old) {
logger.debug("HistoricalDischargeComputeState.computeAdvance");
- WINFOArtifact winfo = (WINFOArtifact) artifact;
+ HistoricalDischargeAccess access =
+ new HistoricalDischargeAccess(artifact);
- CalculationResult res = old instanceof CalculationResult ? (CalculationResult) old
- : winfo.getHistoricalDischargeData();
+ CalculationResult res = old instanceof CalculationResult
+ ? (CalculationResult)old
+ : new Calculation6(access).calculate();
if (facets == null) {
return res;
@@ -81,8 +91,6 @@
}
HistoricalDischargeData data = (HistoricalDischargeData) res.getData();
- HistoricalDischargeAccess access = new HistoricalDischargeAccess(
- artifact);
WQTimerange[] wqts = (WQTimerange[]) data.getWQTimeranges();
if (wqts != null && wqts.length > 0) {
@@ -103,8 +111,11 @@
return res;
}
- protected void prepareFacets(List<Facet> facets, WQTimerange[] wqts,
- HistoricalDischargeAccess access) {
+ protected void prepareFacets(
+ List<Facet> facets,
+ WQTimerange[] wqts,
+ HistoricalDischargeAccess access
+ ) {
int i = 0;
for (WQTimerange wqt : wqts) {
More information about the Dive4elements-commits
mailing list