[Dive4elements-commits] [PATCH] Simplified inner loop of water level exporter

Wald Commits scm-commit at wald.intevation.org
Sun Apr 7 11:38:47 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1365327495 -7200
# Node ID 7b1c5fe4ebf3a04d34b2b2a79cf2df96c541faa0
# Parent  966237892c9b356b872c7a10a43090307c9eb9a3
Simplified inner loop of water level exporter.

diff -r 966237892c9b -r 7b1c5fe4ebf3 flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java
--- 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);
             }
         }
diff -r 966237892c9b -r 7b1c5fe4ebf3 flys-backend/src/main/java/de/intevation/flys/model/Range.java
--- 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() {


More information about the Dive4elements-commits mailing list