changeset 4171:1d8faeedda0c

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.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 18 Oct 2012 09:28:51 +0200
parents c0cc002d52ca
children 9fd17cb69047
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HistoricalWQTimerange.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQTimerange.java flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveExporter.java
diffstat 3 files changed, 95 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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<TimerangeItem> sort() {
+        ArrayList<TimerangeItem> items = new ArrayList<TimerangeItem>(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 :
--- 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<TimerangeItem> {
+        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<Timerange> ts;
 
 
@@ -38,5 +79,15 @@
     public Timerange[] getTimeranges() {
         return ts.toArray(new Timerange[ts.size()]);
     }
+
+    public List<TimerangeItem> sort() {
+        ArrayList<TimerangeItem> items = new ArrayList<TimerangeItem>(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 :
--- 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<WQTimerange.TimerangeItem> 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),

http://dive4elements.wald.intevation.org