[PATCH 1 of 3] Inject current km and pnp into the context used to calculate W in cm at gauge

Wald Commits scm-commit at wald.intevation.org
Thu Sep 25 17:25:14 CEST 2014


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1411658592 -7200
# Node ID 10917ec29625092b846a588901c708282c31d39e
# Parent  a5e7e2d833ea2e0259c5dc912ad85574baa65bcb
Inject current km and pnp into the context used to calculate W in cm at gauge.

diff -r a5e7e2d833ea -r 10917ec29625 artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java	Wed Sep 24 18:33:11 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java	Thu Sep 25 17:23:12 2014 +0200
@@ -24,6 +24,8 @@
 
 import org.apache.log4j.Logger;
 
+import static org.dive4elements.river.exports.injector.InjectorConstants.PNP;
+
 /**
  * A Facet that returns discharge curve data at a gauge
  *
@@ -77,7 +79,23 @@
         }
         double [] kms = new double[values[0].length];
         Arrays.fill(kms, gauge.getStation().doubleValue());
-        return new WQKms(kms, values[0], values[1], name, WQKms.CENTIMETER_AT_GAUGE);
+
+        Object pnpObject = context.getContextValue(PNP);
+        if (!(pnpObject instanceof Number)) {
+            return new WQKms(kms, values[0], values[1], name, W.CENTIMETER_AT_GAUGE);
+        }
+        double pnp = Double.valueOf(pnpObject.toString());
+        double[] ws = transformToM(values[1], pnp);
+
+        return new WQKms(kms, values[0], ws, name);
+    }
+
+    private double[] transformToM(double[] ws, double pnp) {
+        double[] retVals = new double[ws.length];
+        for (int i = 0; i < ws.length; i++) {
+            retVals[i] = ws[i]/100 + pnp;
+        }
+        return retVals;
     }
 
     @Override
diff -r a5e7e2d833ea -r 10917ec29625 artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java	Wed Sep 24 18:33:11 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/ComputedDischargeInjector.java	Thu Sep 25 17:23:12 2014 +0200
@@ -29,8 +29,11 @@
     ) {
         RangeAccess access = new RangeAccess((D4EArtifact)artifact);
         if (access.hasFrom()) {
-            double km = access.getFrom();
-            ctx.putContextValue(CURRENT_KM, km);
+            ctx.putContextValue(CURRENT_KM, access.getFrom());
+        }
+        else if (access.getLocations() != null &&
+            access.getLocations().length > 0) {
+            ctx.putContextValue(CURRENT_KM, access.getLocations()[0]);
         }
     }
 }
diff -r a5e7e2d833ea -r 10917ec29625 artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java	Wed Sep 24 18:33:11 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/DischargeInjector.java	Thu Sep 25 17:23:12 2014 +0200
@@ -27,17 +27,22 @@
     @Override
     public void injectContext(CallContext ctx, Artifact artifact, Document doc) {
         RangeAccess access = new RangeAccess((D4EArtifact)artifact);
-        if (!access.hasFrom()) {
+        if (access.hasFrom()) {
+            ctx.putContextValue(CURRENT_KM, access.getFrom());
             return;
         }
-        double km = access.getFrom();
-        Gauge gauge = access.getRiver().determineGaugeByStation(
-            km - GAUGE_EPSILON, km + GAUGE_EPSILON);
-        if (gauge == null) {
-            log.error("No Gauge could be found at station " + km + "!");
+        D4EArtifact d4e = (D4EArtifact)artifact;
+        String name = d4e.getDataAsString("gauge_name");
+        if (name == null || name.equals("")) {
             return;
         }
-        ctx.putContextValue(CURRENT_KM, km);
+        Gauge gauge = access.getRiver().determineGaugeByName(name);
+        if (gauge == null) {
+            log.error("No Gauge could be found for name " + name + "!");
+            return;
+        }
+        ctx.putContextValue(CURRENT_KM, gauge.getStation());
+        return;
     }
 
 }
diff -r a5e7e2d833ea -r 10917ec29625 artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java	Wed Sep 24 18:33:11 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java	Thu Sep 25 17:23:12 2014 +0200
@@ -33,7 +33,7 @@
         Artifact artifact,
         Document request) {
         Double km = getCurrentKmFromRequest(request);
-        if (km == Double.NaN) {
+        if (Double.isNaN(km)) {
             RangeAccess access = new RangeAccess((D4EArtifact)artifact);
             km = access.getFrom();
             log.debug("from access");
diff -r a5e7e2d833ea -r 10917ec29625 artifacts/src/main/java/org/dive4elements/river/exports/injector/PNPInjector.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/injector/PNPInjector.java	Wed Sep 24 18:33:11 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/injector/PNPInjector.java	Thu Sep 25 17:23:12 2014 +0200
@@ -1,5 +1,6 @@
 package org.dive4elements.river.exports.injector;
 
+import org.apache.log4j.Logger;
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.artifacts.ContextInjector;
@@ -17,6 +18,7 @@
 implements ContextInjector
 {
 
+    private Logger log = Logger.getLogger(PNPInjector.class);
     @Override
     public void setup(Element cfg) {
         // TODO Auto-generated method stub
@@ -31,19 +33,26 @@
     ) {
         Object currentKm = ctx.getContextValue(CURRENT_KM);
         if (currentKm == null) {
+            log.debug("no current km. not injecting pnp");
             return;
         }
 
         RiverAccess access = new RiverAccess((D4EArtifact)artifact);
-        Double km = (Double)currentKm;
+        Double km = Double.valueOf(currentKm.toString());
+        if (Double.isNaN(km) || Double.isInfinite(km)) {
+            log.debug("current km is NAN or infinte. not injecting pnp.");
+            return;
+        }
         Gauge gauge =
             access.getRiver().determineGaugeByStation(
                 km - GAUGE_EPSILON,
                 km + GAUGE_EPSILON);
 
         if (gauge == null) {
+            log.debug("no gauge found at current km. not injecting pnp");
             return;
         }
+        log.debug("injecting pnp: " + gauge.getDatum());
         ctx.putContextValue(PNP, gauge.getDatum());
     }
 


More information about the Dive4Elements-commits mailing list