changeset 9382:8ae7137b67d7

Fixed: avoiding NaN exception in BigDecimal rounding
author mschaefer
date Tue, 07 Aug 2018 14:06:49 +0200
parents a9e6b0a75bde
children 0dd1d592714f
files artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthUtils.java artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java
diffstat 4 files changed, 24 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthUtils.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthUtils.java	Tue Aug 07 14:06:49 2018 +0200
@@ -10,6 +10,7 @@
 package org.dive4elements.river.artifacts.sinfo.flowdepth;
 
 import org.dive4elements.river.artifacts.model.Calculation;
+import org.dive4elements.river.utils.Formatter;
 
 /**
  * @author Gernot Belger
@@ -55,4 +56,13 @@
         /* >= 1998 */
         return 3;
     }
+
+    /**
+     * Calculates a flow depth, rounded to the active scale
+     */
+    public static double calcFlowDepth(final double wst, final double bedHeight) {
+        if (Double.isNaN(wst) || Double.isInfinite(wst) || Double.isNaN(bedHeight) || Double.isInfinite(bedHeight))
+            return Math.max(wst - bedHeight, 0.0);
+        return Math.max(Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(bedHeight)).doubleValue(), 0.0);
+    }
 }
\ No newline at end of file
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxCalculation.java	Tue Aug 07 14:06:49 2018 +0200
@@ -35,7 +35,6 @@
 import org.dive4elements.river.artifacts.states.WaterlevelData;
 import org.dive4elements.river.artifacts.states.WaterlevelFetcher;
 import org.dive4elements.river.model.River;
-import org.dive4elements.river.utils.Formatter;
 
 /**
  * @author Gernot Belger
@@ -135,8 +134,8 @@
                 final double maxBedHeightValue = bedHeight.getMaxBedHeight(station);
                 final double meanBedHeight = bedHeight.getMeanBedHeight(station);
 
-                final double minFlowDepth = Math.max(Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(maxBedHeightValue)).doubleValue(), 0.0);
-                final double maxFlowDepth = Math.max(Formatter.roundFlowDepth(wst).subtract(Formatter.roundFlowDepth(minBedHeightValue)).doubleValue(), 0.0);
+                final double minFlowDepth = FlowDepthUtils.calcFlowDepth(wst, maxBedHeightValue);
+                final double maxFlowDepth = FlowDepthUtils.calcFlowDepth(wst, minBedHeightValue);
 
                 // REMARK: access the location once only during calculation
                 final String location = riverInfoProvider.getLocation(station);
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Tue Aug 07 14:06:49 2018 +0200
@@ -183,6 +183,8 @@
      * Calculates the salix value
      */
     private double calcSalix(final double mhw, final double mw) {
+        if (Double.isNaN(mw) || Double.isInfinite(mw) || Double.isNaN(mhw) || Double.isInfinite(mhw))
+            return mw; // preserving NaN or Infinity
         return Formatter.roundW(mhw).subtract(SALIX_DISTANCE).subtract(Formatter.roundW(mw)).doubleValue();
     }
 
@@ -190,6 +192,8 @@
      * Calculates the inverse MW-MNW difference
      */
     private double calcMwmnw(final double mw, final double mnw) {
+        if (Double.isNaN(mw) || Double.isInfinite(mw) || Double.isNaN(mnw) || Double.isInfinite(mnw))
+            return mnw - mw; // preserving NaN or Inifinity
         return Formatter.roundW(mnw).subtract(Formatter.roundW(mw)).doubleValue();
     }
 
--- a/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Tue Aug 07 12:43:53 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java	Tue Aug 07 14:06:49 2018 +0200
@@ -385,6 +385,10 @@
         return Formatter.getFormatter(context, FLOWDEPTH_MAX_DIGITS, FLOWDEPTH_MAX_DIGITS);
     }
 
+    /**
+     * Decimal half even rounding of a flow depth value
+     * (throws an exception for NaN or Infinity)
+     */
     public static BigDecimal roundFlowDepth(final double value) {
         return BigDecimal.valueOf(value).setScale(FLOWDEPTH_MAX_DIGITS, RoundingMode.HALF_EVEN);
     }
@@ -393,6 +397,10 @@
         return Formatter.getFormatter(context, 2, 2);
     }
 
+    /**
+     * Decimal half even rounding of a W value
+     * (throws an exception for NaN or Infinity)
+     */
     public static BigDecimal roundW(final double value) {
         return BigDecimal.valueOf(value).setScale(WATERLEVEL_W_MAX_DIGITS, RoundingMode.HALF_EVEN);
     }

http://dive4elements.wald.intevation.org