changeset 9490:e44c1a8b0c54

Fixed: rounding problems in iota/salix scenario calculation
author mschaefer
date Wed, 19 Sep 2018 16:21:08 +0200
parents 8562c60371b8
children d7d22ea8573d
files artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixScenario.java
diffstat 3 files changed, 16 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java	Tue Sep 18 17:28:22 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java	Wed Sep 19 16:21:08 2018 +0200
@@ -12,6 +12,7 @@
 import java.text.NumberFormat;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.common.AbstractResultType;
 import org.dive4elements.river.artifacts.common.I18NStrings;
@@ -30,6 +31,8 @@
 
     private static final long serialVersionUID = 1L;
 
+    protected static Logger log = Logger.getLogger(SInfoResultType.class);
+
     protected SInfoResultType(final String unit, final String csvHeader) {
         super(unit, csvHeader, csvHeader);
     }
@@ -209,6 +212,8 @@
         @Override
         public String exportValue(final CallContext context, final Object value) {
             final double doubleValue = asDouble(value);
+            log.trace(String.format("meanBedHeight.exportValue value: %f doubleValue: %f formatted to: %s", value, doubleValue,
+                    exportDoubleValue(context, doubleValue)));
             return exportDoubleValue(context, doubleValue);
         }
 
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Tue Sep 18 17:28:22 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Wed Sep 19 16:21:08 2018 +0200
@@ -167,16 +167,18 @@
         row.putValue(UInfoResultType.waterlevelMH5, hw5);
 
         // Calc salix-line and mw-mnw
-        row.putValue(UInfoResultType.salixline, calcSalix(mhw, mw));
+        row.putValue(UInfoResultType.salixline, calcSalix(mhw, mw, 0.0));
         row.putValue(UInfoResultType.salix_mw_mnw, calcMwmnw(mw, mnw));
-        row.putValue(UInfoResultType.salixw, mhw - SALIX_DISTANCE.doubleValue());
+        final double salixw = Formatter.roundW(mhw).subtract(SALIX_DISTANCE).doubleValue();
+        row.putValue(UInfoResultType.salixw, salixw);
         // Calc scenario values (always all scenario types set, Result variant extracts the fields needed)
         final List<SalixScenario> scenarios = new ArrayList<>();
         final List<Double> deltaws = getDeltaWs(station, rangeScenarios);
         for (final Double deltaw : deltaws) {
             if (deltaw != null) {
-                final double salix = calcSalix(mhw, mw + deltaw);
-                scenarios.add(new SalixScenario((int) (deltaw * 100), salix, mhw + deltaw - SALIX_DISTANCE.doubleValue()));
+                final double salix = calcSalix(mhw, mw, deltaw.doubleValue());
+                final double scen = calcSalix(mhw, 0.0, deltaw.doubleValue());
+                scenarios.add(new SalixScenario((int) (deltaw * 100), salix, scen));
             }
             else {
                 scenarios.add(null);
@@ -199,10 +201,10 @@
     /**
      * Calculates the salix value
      */
-    private double calcSalix(final double mhw, final double mw) {
+    private double calcSalix(final double mhw, final double mw, final double deltamw) {
         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();
+            return mhw - mw; // preserving NaN or Infinity
+        return Formatter.roundW(mhw).subtract(SALIX_DISTANCE).subtract(Formatter.roundW(mw).add(Formatter.roundW(deltamw))).doubleValue();
     }
 
     /**
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixScenario.java	Tue Sep 18 17:28:22 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixScenario.java	Wed Sep 19 16:21:08 2018 +0200
@@ -58,11 +58,11 @@
         return this.salixW;
     }
 
-    public String getSalixWFormatted() {
+    public String getSalixWFormatted(final NumberFormat formatter) {
         if (Double.isNaN(this.salixW))
             return StringUtils.EMPTY;
 
-        return String.valueOf(this.salixW);
+        return formatter.format(this.salixW);
     }
 
     public static final String getScenarioValueHeader() {

http://dive4elements.wald.intevation.org