changeset 5587:7b1c5fe4ebf3

Simplified inner loop of water level exporter.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 07 Apr 2013 11:38:15 +0200
parents 966237892c9b
children 3e8f7b4bdf20 7277bdacc4a9
files flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java flys-backend/src/main/java/de/intevation/flys/model/Range.java
diffstat 2 files changed, 58 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Fri Apr 05 23:28:56 2013 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Sun Apr 07 11:38:15 2013 +0200
@@ -453,16 +453,24 @@
 
 
     /** Linearly search for gauge which is valid at km. */
-    private Gauge findGauge(double km, List<Gauge> gauges) {
+    private static Gauge findGauge(double km, List<Gauge> gauges) {
         for (Gauge gauge: gauges) {
-            if (km >= gauge.getRange().getA().doubleValue()
-                && km <= gauge.getRange().getB().doubleValue()) {
+            if (gauge.getRange().contains(km)) {
                 return gauge;
             }
         }
         return null;
     }
 
+    private static Segment findSegment(double km, List<Segment> segments) {
+        for (Segment segment: segments) {
+            if (segment.inside(km)) {
+                return segment;
+            }
+        }
+        return null;
+    }
+
 
     private void writeRow4(CSVWriter writer, double wqkm[], FLYSArtifact flys) {
         NumberFormat kmf = getKmFormatter();
@@ -555,39 +563,59 @@
             // Get W/Q input per gauge for this case.
             FixRealizingAccess fixAccess = new FixRealizingAccess(flys, getCallContext());
             segments = fixAccess.getSegments();
-            if (segments != null && segments.size() > 0) {
+            if (segments != null && !segments.isEmpty()) {
                 isFixRealize = true;
             }
         }
 
-        for (int i = 0; i < size; i ++) {
-            result = wqkms.get(i, result);
+        if (atGauge) { // "At gauge" needs more output.
 
-            // Check if there has been W input per Gauge and use it.
-            if (segments != null) {
-                for (Segment segment: segments) {
-                    if (segment.inside(result[2])) {
-                        NumberFormat nf =
-                            Formatter.getFormatter(context.getMeta() , 0, 0);
-                        colDesc = nf.format(segment.getValues()[0]);
+            // Kms tend to be close together so caching the last sector
+            // is a good time saving heuristic.
+            Segment lastSegment = null;
+            Gauge   lastGauge   = null;
+
+            NumberFormat nf =
+                Formatter.getFormatter(context.getMeta(), 0, 0);
+
+            for (int i = 0; i < size; ++i) {
+                result = wqkms.get(i, result);
+                double km = result[2];
+
+                if (segments != null) {
+                    Segment found = lastSegment != null
+                                    && lastSegment.inside(km)
+                        ? lastSegment
+                        : findSegment(km, segments);
+
+                    if (found != null) {
+                        colDesc = nf.format(found.getValues()[0]);
                     }
+                    lastSegment = found;
                 }
-            }
 
-            if (atGauge) {
                 String gaugeN;
                 if (isFixRealize) {
-                    gaugeN = findGauge(result[2], gauges).getName();
+                    Gauge found = lastGauge != null
+                                  && lastGauge.getRange().contains(km)
+                        ? lastGauge
+                        : findGauge(km, gauges);
+
+                    gaugeN = found != null ? found.getName() : notinrange;
+                    lastGauge = found;
                 }
                 else {
                     // TODO issue1114: Take correct gauge
-                    gaugeN = result[2] >= a && result[2] <= b
+                    gaugeN = km >= a && km <= b
                         ? gaugeName
                         : notinrange;
                 }
                 writeRow6(writer, result, colDesc, flys, gaugeN);
             }
-            else {
+        }
+        else { // Not at gauge.
+            for (int i = 0; i < size; ++i) {
+                result = wqkms.get(i, result);
                 writeRow4(writer, result, flys);
             }
         }
--- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java	Fri Apr 05 23:28:56 2013 +0200
+++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java	Sun Apr 07 11:38:15 2013 +0200
@@ -73,6 +73,18 @@
         this.b = b;
     }
 
+    public boolean contains(double x) {
+        BigDecimal b = this.b != null ? this.b : a;
+        double av = a.doubleValue();
+        double bv = b.doubleValue();
+        if (av > bv) {
+            double t = av;
+            av = bv;
+            bv = t;
+        }
+        return x >= av && x <= bv;
+    }
+
     @OneToOne
     @JoinColumn(name = "river_id")
     public River getRiver() {

http://dive4elements.wald.intevation.org