[PATCH] Sediment load: dispatch calculation modes

Wald Commits scm-commit at wald.intevation.org
Fri Jul 18 11:59:04 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1405677539 -7200
# Node ID 72760ca2fc2b037393cfd5f73a851f5723771c81
# Parent  1dae69eff79dd23ab0af8bfd9ec76573855edcea
Sediment load: dispatch calculation modes.

diff -r 1dae69eff79d -r 72760ca2fc2b artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java	Fri Jul 18 11:16:50 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java	Fri Jul 18 11:58:59 2014 +0200
@@ -13,10 +13,13 @@
 import org.dive4elements.river.artifacts.access.SedimentLoadAccess;
 import org.dive4elements.river.artifacts.model.Calculation;
 import org.dive4elements.river.artifacts.model.CalculationResult;
+import org.dive4elements.river.artifacts.model.RiverFactory;
 import org.apache.log4j.Logger;
 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData;
 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Value;
 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Station;
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.utils.DoubleUtil;
 
 public class SedimentLoadDataCalculation
 extends      Calculation
@@ -173,20 +176,43 @@
             return internalCalculate();
         }
 
-        return new CalculationResult();
+        return new CalculationResult(this);
     }
 
     private CalculationResult internalCalculate() {
+        if ("year".equals(yearEpoch))      return calculateYears();
+        if ("epoch".equals(yearEpoch))     return calculateEpochs();
+        if ("off_epoch".equals(yearEpoch)) return calculateOffEpochs();
+
+        // TODO: i18n
+        addProblem("minfo.sediment.load.unknown.calc.mode");
+
+        return new CalculationResult(this);
+    }
+
+    private CalculationResult calculateYears() {
         // TODO: Implement me!
         return null;
     }
 
-    private static final double sum(double [] values) {
-        double sum = 0.0;
-        for (double value: values) {
-            sum += value;
+    private CalculationResult calculateEpochs() {
+        // TODO: Implement me!
+        return null;
+    }
+
+    private CalculationResult calculateOffEpochs() {
+        // TODO: Implement me!
+        return null;
+    }
+
+    /** Figure out flow direction of river. */
+    private boolean isKmUp() {
+        River r = RiverFactory.getRiver(river);
+        if (r == null) {
+            addProblem("minfo.missing.river");
+            return true;
         }
-        return sum;
+        return r.getKmUp();
     }
 
     public double[][] sum(
@@ -228,7 +254,7 @@
                 }
             }
             result[0][j] = station.getStation();
-            result[1][j] = sum(values);
+            result[1][j] = DoubleUtil.sum(values);
         }
 
         // TODO: Handle 'virtual' measument stations 'from' and 'to'.
diff -r 1dae69eff79d -r 72760ca2fc2b artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataValueFilter.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataValueFilter.java	Fri Jul 18 11:16:50 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataValueFilter.java	Fri Jul 18 11:58:59 2014 +0200
@@ -98,6 +98,23 @@
         private Date a;
         private Date b;
 
+        public TimeRangeIntersects(int startYear, int endYear) {
+            this(firstJan(Math.min(startYear, endYear)),
+                lastDec(Math.max(startYear, endYear)));
+        }
+
+        private static Date firstJan(int year) {
+            Calendar cal = Calendar.getInstance();
+            cal.set(year, 1, 1, 0, 0, 0);
+            return cal.getTime();
+        }
+
+        private static Date lastDec(int year) {
+            Calendar cal = Calendar.getInstance();
+            cal.set(year, 12, 31, 23, 59, 59);
+            return cal.getTime();
+        }
+
         public TimeRangeIntersects(Date a, Date b) {
             if (a.after(b)) {
                 this.b = a;
diff -r 1dae69eff79d -r 72760ca2fc2b artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java	Fri Jul 18 11:16:50 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java	Fri Jul 18 11:58:59 2014 +0200
@@ -171,6 +171,14 @@
         return out;
     }
 
+    public static final double sum(double [] values) {
+        double sum = 0.0;
+        for (double value: values) {
+            sum += value;
+        }
+        return sum;
+    }
+
     public static final double [] fill(int N, double value) {
         double [] result = new double[N];
         Arrays.fill(result, value);
@@ -234,6 +242,15 @@
         return true;
     }
 
+    public static final boolean isNaN(double [] values) {
+        for (double value: values) {
+            if (!Double.isNaN(value)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /** In an array of doubles, search and return the maximum value. */
     public static final double maxInArray(double[] values) {
         double max = - Double.MAX_VALUE;


More information about the Dive4Elements-commits mailing list