[Dive4elements-commits] [PATCH] Artifacts: Official lines: Only apply in Q-Calculations and filter against the ranges of the gauges

Wald Commits scm-commit at wald.intevation.org
Fri Jun 21 16:28:55 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1371824927 -7200
# Node ID d2803cc7a3386ad13d57a7fbb7667544699d3211
# Parent  cf4889a257cb797711d06aa712ea0167491e801d
Artifacts: Official lines: Only apply in Q-Calculations and filter against the ranges of the gauges.

diff -r cf4889a257cb -r d2803cc7a338 artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java	Fri Jun 21 15:37:32 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java	Fri Jun 21 16:28:47 2013 +0200
@@ -136,22 +136,84 @@
         return rivers2officialLines;
     }
 
+    public static final Range MAX_RANGE = new Range(-Double.MAX_VALUE, +Double.MAX_VALUE);
+
+    private static final String nn(String s) {
+        return s != null ? s : "";
+    }
+
+    public static Range extractRange(D4EArtifact artifact) {
+        String mode      = nn(artifact.getDataAsString("ld_mode"));
+        String locations = nn(artifact.getDataAsString("ld_locations"));
+        String from      = nn(artifact.getDataAsString("ld_from"));
+        String to        = nn(artifact.getDataAsString("ld_to"));
+
+        if (mode.equals("location")) {
+            try {
+                String loc = locations.replace(" ", "");
+                String[] split = loc.split(",");
+                if (split.length < 1) {
+                    return MAX_RANGE;
+                }
+                double min = Double.parseDouble(split[0]);
+                double max = min;
+                for (int i = 1; i < split.length; ++i) {
+                    double v = Double.parseDouble(split[i]);
+                    if (v > max) max = v;
+                    if (v < min) min = v;
+                }
+                return new Range(min, max);
+            }
+            catch (NumberFormatException nfe) {
+                return MAX_RANGE;
+            }
+        }
+        try {
+            return new Range(Double.parseDouble(from), Double.parseDouble(to));
+        }
+        catch (NumberFormatException nfe) {
+            return MAX_RANGE;
+        }
+    }
+
+    private static List<ValueRange> filterByRange(Range range, List<ValueRange> ranges) {
+        List<ValueRange> list = new ArrayList<ValueRange>(ranges.size());
+        for (ValueRange r: ranges) {
+            if (r.intersects(range)) {
+                list.add(r);
+            }
+        }
+        return list;
+    }
+
+    private static boolean isQ(D4EArtifact artifact) {
+        Boolean b = artifact.getDataAsBoolean("wq_isq");
+        return b != null && b;
+    }
+
     public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) {
 
+        if (!isQ(artifact)) { // Only handle Q calculations
+            return Collections.<OfficialLine>emptyList();
+        }
+
         Map<String, List<ValueRange>> rivers2officialLines = getAll();
 
-        String riverName = artifact.getDataAsString("river");
+        String riverName = nn(artifact.getDataAsString("river"));
 
-        if (riverName == null) {
+        List<ValueRange> ranges = rivers2officialLines.get(riverName);
+
+        if (ranges == null) {
             return Collections.<OfficialLine>emptyList();
         }
 
-        List<ValueRange> ranges = rivers2officialLines.get(riverName);
+        ranges = filterByRange(extractRange(artifact), ranges);
 
         if (ranges.isEmpty()) {
             return Collections.<OfficialLine>emptyList();
         }
 
+
         // TODO: Figure out all the cases here.
 
         return Collections.<OfficialLine>emptyList();


More information about the Dive4elements-commits mailing list