# HG changeset patch # User Ingo Weinzierl # Date 1350545331 -7200 # Node ID 1d8faeedda0cbb3f8bb991901c794f744b12d962 # Parent c0cc002d52ca120152914e11c75d253a7c0f5cc8 Sort calculation results of a historical discharge calculation based on their start date. Therefore, there are two new inner classes TimerangeItem and HistoricalTimerangeItem that wrap a W, Q, Timerange (and delta Q). WQTimerange and HistoricalWQTimerange now implement a sort() that return the results (TimerangeItem or HistoricalWQTimerangeItem) sorted by their start date. diff -r c0cc002d52ca -r 1d8faeedda0c flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HistoricalWQTimerange.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HistoricalWQTimerange.java Wed Oct 17 23:00:31 2012 +0200 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HistoricalWQTimerange.java Thu Oct 18 09:28:51 2012 +0200 @@ -2,6 +2,10 @@ import gnu.trove.TDoubleArrayList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * A subclass of WQTimerange that stores besides W, Q and Timerange values @@ -11,6 +15,27 @@ */ public class HistoricalWQTimerange extends WQTimerange { + public static class HistoricalTimerangeItem extends TimerangeItem { + public double diff; + + public HistoricalTimerangeItem (Timerange timerange, double q, double w, double diff) { + super(timerange, q, w); + this.diff = diff; + } + + public double[] get(double[] wq) { + if (wq.length >= 3) { + wq[0] = w; + wq[1] = q; + } + else if (wq.length >= 2) { + return super.get(wq); + } + + return wq; + } + } + protected TDoubleArrayList diffs; @@ -45,5 +70,16 @@ public double[] getDiffs() { return diffs.toNativeArray(); } + + @Override + public List sort() { + ArrayList items = new ArrayList(ts.size()); + for (int i = 0, n = size(); i < n; i++) { + items.add(new HistoricalTimerangeItem(getTimerange(i), getQ(i), getW(i), diffs.get(i))); + } + + Collections.sort(items); + return items; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c0cc002d52ca -r 1d8faeedda0c flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQTimerange.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQTimerange.java Wed Oct 17 23:00:31 2012 +0200 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQTimerange.java Thu Oct 18 09:28:51 2012 +0200 @@ -1,6 +1,7 @@ package de.intevation.flys.artifacts.model; import java.util.ArrayList; +import java.util.Collections; import java.util.List; @@ -9,6 +10,46 @@ */ public class WQTimerange extends WQ { + public static class TimerangeItem implements Comparable { + public double q; + public double w; + public Timerange timerange; + + public TimerangeItem (Timerange timerange, double q, double w) { + this.timerange = timerange; + this.q = q; + this.w = w; + } + + public double[] get(double[] wq) { + if (wq.length >= 2) { + wq[0] = w; + wq[1] = q; + } + + return wq; + } + + @Override + public int compareTo(TimerangeItem other) { + if (other.timerange.getStart() < timerange.getStart()) { + return 1; + } + else if (other.timerange.getStart() > timerange.getStart()) { + return -1; + } + else if (other.timerange.getEnd() < timerange.getEnd()) { + return 1; + } + else if (other.timerange.getEnd() > timerange.getEnd()){ + return -1; + } + else { + return 0; + } + } + } + protected List ts; @@ -38,5 +79,15 @@ public Timerange[] getTimeranges() { return ts.toArray(new Timerange[ts.size()]); } + + public List sort() { + ArrayList items = new ArrayList(ts.size()); + for (int i = 0, n = size(); i < n; i++) { + items.add(new TimerangeItem(getTimerange(i), getQ(i), getW(i))); + } + + Collections.sort(items); + return items; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c0cc002d52ca -r 1d8faeedda0c flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java Wed Oct 17 23:00:31 2012 +0200 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java Thu Oct 18 09:28:51 2012 +0200 @@ -9,6 +9,7 @@ import java.util.Map; import java.util.HashMap; import java.util.Locale; +import java.util.TreeMap; import org.w3c.dom.Document; @@ -157,12 +158,16 @@ String gaugeName = getReferenceGaugename(); - for (int i = 0, n = wqt.size(); i < n; i++) { - Timerange tr = wqt.getTimerange(i); + List sorted = wqt.sort(); + + for (int i = 0, n = sorted.size(); i < n; i++) { + WQTimerange.TimerangeItem item = sorted.get(i); + + Timerange tr = item.timerange; Date start = new Date(tr.getStart()); Date end = new Date(tr.getEnd()); - wqt.get(i, wq); + item.get(wq); writer.writeNext(new String[] { df.format(start) + " - " + df.format(end),