changeset 8724:47199406994a

(issue1801) Determine gauge at a station always with same tolerance.
author Tom Gottfried <tom@intevation.de>
date Wed, 29 Apr 2015 12:30:57 +0200 (2015-04-29)
parents 686d8876edf9
children 68827654ea7a
files artifacts/src/main/java/org/dive4elements/river/artifacts/MainValuesArtifact.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveExporter.java artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixATWriter.java artifacts/src/main/java/org/dive4elements/river/exports/injector/InjectorConstants.java artifacts/src/main/java/org/dive4elements/river/exports/injector/PNPInjector.java artifacts/src/main/java/org/dive4elements/river/exports/process/DefaultProcessor.java backend/src/main/java/org/dive4elements/river/model/River.java
diffstat 9 files changed, 27 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/MainValuesArtifact.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/MainValuesArtifact.java	Wed Apr 29 12:30:57 2015 +0200
@@ -44,7 +44,6 @@
 
 import org.dive4elements.river.artifacts.states.StaticState;
 import org.dive4elements.river.artifacts.resources.Resources;
-import org.dive4elements.river.exports.injector.InjectorConstants;
 
 
 /**
@@ -374,9 +373,7 @@
             }
 
             for (MainValue mv : orig) {
-                Gauge g = river.determineGaugeByStation(
-                    kms[0] - InjectorConstants.GAUGE_EPSILON,
-                    kms[0] + InjectorConstants.GAUGE_EPSILON);
+                Gauge g = river.determineGaugeAtStation(kms[0]);
                 if (pnpObject instanceof Number) {
                     if (mv.getMainValue().getType().getName().equals("W")) {
                         filteredList.add(new NamedDouble(
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java	Wed Apr 29 12:30:57 2015 +0200
@@ -25,7 +25,6 @@
 import org.apache.log4j.Logger;
 
 import static org.dive4elements.river.exports.injector.InjectorConstants.PNP;
-import static org.dive4elements.river.exports.injector.InjectorConstants.GAUGE_EPSILON;
 
 /**
  * A Facet that returns discharge curve data at a gauge
@@ -89,9 +88,7 @@
                 access.getLocations().length > 0) {
                 km = access.getLocations()[0];
             }
-            Gauge g = access.getRiver().determineGaugeByStation(
-                    km - GAUGE_EPSILON,
-                    km + GAUGE_EPSILON);
+            Gauge g = access.getRiver().determineGaugeAtStation(km);
             if (g != null) {
                 return new WQKms(
                     kms,
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ATExporter.java	Wed Apr 29 12:30:57 2015 +0200
@@ -96,8 +96,8 @@
         River river = new RiverAccess(master).getRiver();
         double[] kms = new RangeAccess(master).getLocations();
 
-        Gauge gauge = river.determineGaugeByPosition(kms[0]);
-        if (Math.abs(kms[0] - gauge.getStation().doubleValue()) < 1e-4) {
+        Gauge gauge = river.determineGaugeAtStation(kms[0]);
+        if (gauge != null) {
             // at gauge.
             TimeInterval interval =
                 gauge.fetchMasterDischargeTable().getTimeInterval();
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveExporter.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveExporter.java	Wed Apr 29 12:30:57 2015 +0200
@@ -47,8 +47,6 @@
 import org.dive4elements.river.utils.RiverUtils;
 import org.dive4elements.river.utils.Formatter;
 
-import static org.dive4elements.river.exports.injector.InjectorConstants.GAUGE_EPSILON;
-
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
@@ -111,9 +109,8 @@
 
             double[] kms = rangeAccess.getKmRange();
 
-            Gauge gauge = river.determineGaugeByPosition(kms[0]);
-            if (Math.abs(kms[0] - gauge.getStation().doubleValue())
-                < GAUGE_EPSILON) {
+            Gauge gauge = river.determineGaugeAtStation(kms[0]);
+            if (gauge != null) {
                 wUnit = "cm";
                 gaugeName = gauge.getName();
                 gaugeDatum = gauge.getDatum().doubleValue();
--- a/artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixATWriter.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixATWriter.java	Wed Apr 29 12:30:57 2015 +0200
@@ -76,8 +76,8 @@
 
         int subtractPNP = 0;
         // Special case handling for at's at gauges
-        Gauge gauge = river.determineGaugeByPosition(km);
-        if (Math.abs(km - gauge.getStation().doubleValue()) < 1e-4) {
+        Gauge gauge = river.determineGaugeAtStation(km);
+        if (gauge != null) {
             printGaugeHeader(out, meta, river, gauge);
             subtractPNP = (int)Math.round(gauge.getDatum().doubleValue() * 100);
         } else {
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/InjectorConstants.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/InjectorConstants.java	Wed Apr 29 12:30:57 2015 +0200
@@ -5,5 +5,4 @@
 {
     public static final String CURRENT_KM = "currentkm";
     public static final String PNP = "PNP";
-    public static final double GAUGE_EPSILON = 0.1;
 }
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/PNPInjector.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/PNPInjector.java	Wed Apr 29 12:30:57 2015 +0200
@@ -13,7 +13,6 @@
 
 import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM;
 import static org.dive4elements.river.exports.injector.InjectorConstants.PNP;
-import static org.dive4elements.river.exports.injector.InjectorConstants.GAUGE_EPSILON;
 
 public class PNPInjector
 implements ContextInjector
@@ -51,10 +50,7 @@
         }
 
         Gauge gauge =
-            river.determineGaugeByStation(
-                km - GAUGE_EPSILON,
-                km + GAUGE_EPSILON);
-
+            river.determineGaugeAtStation(km);
         if (gauge == null) {
             log.debug("no gauge found at current km. not injecting pnp");
             return;
--- a/artifacts/src/main/java/org/dive4elements/river/exports/process/DefaultProcessor.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/process/DefaultProcessor.java	Wed Apr 29 12:30:57 2015 +0200
@@ -43,7 +43,6 @@
             ThemeDocument    theme,
             boolean          visible,
             int              index) {
-        return;
     }
 
     @Override
@@ -52,7 +51,6 @@
             ArtifactAndFacet bundle,
             ThemeDocument    theme,
             boolean          visible) {
-        return;
     }
 
     @Override
--- a/backend/src/main/java/org/dive4elements/river/model/River.java	Wed Apr 29 11:56:04 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/River.java	Wed Apr 29 12:30:57 2015 +0200
@@ -32,16 +32,22 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 
+import org.apache.log4j.Logger;
 
 @Entity
 @Table(name = "rivers")
 public class River
 implements   Serializable
 {
+    private static Logger log = Logger.getLogger(River.class);
+
     public static final MathContext PRECISION = new MathContext(6);
 
     public static final double EPSILON = 1e-5;
 
+    // Tolerance for determining whether we are at the station of a gauge
+    public static final double GAUGE_EPSILON = 0.1;
+
     public static final Comparator<Double> KM_CMP = new Comparator<Double>() {
         @Override
         public int compare(Double a, Double b) {
@@ -267,20 +273,27 @@
         return gauges.isEmpty() ? null : gauges.get(0);
     }
 
-    public Gauge determineGaugeByStation(double a, double b) {
 
-        if (a > b) { double t = a; a = b; b = t; }
-
+    /**
+     * @param s station at which the gauge is requested.
+     * @return Gauge within tolerance at given station. null if there is none.
+     */
+    public Gauge determineGaugeAtStation(double s) {
         Session session = SessionHolder.HOLDER.get();
 
         Query query = session.createQuery(
             "from Gauge where river.id=:river " +
             "and station between :a and :b");
         query.setParameter("river", getId());
-        query.setParameter("a", new BigDecimal(a));
-        query.setParameter("b", new BigDecimal(b));
+        query.setParameter("a", new BigDecimal(s - GAUGE_EPSILON));
+        query.setParameter("b", new BigDecimal(s + GAUGE_EPSILON));
 
         List<Gauge> gauges = query.list();
+        if (gauges.size() > 1) {
+            log.warn("More than one gauge found at km " + s +
+                " within +-" + GAUGE_EPSILON +
+                ". Returning arbitrary result.");
+        }
         return gauges.isEmpty() ? null : gauges.get(0);
     }
 

http://dive4elements.wald.intevation.org