diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java @ 8193:8d447516b7dd

Sum up fractions from stations and previous stations according to respective validity model.
author Tom Gottfried <tom@intevation.de>
date Thu, 04 Sep 2014 17:33:01 +0200
parents 76e1e9d81ce2
children 59e50aa0c6c2
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java	Thu Sep 04 16:25:06 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java	Thu Sep 04 17:33:01 2014 +0200
@@ -17,7 +17,6 @@
 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.Value;
 import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Station;
@@ -98,6 +97,11 @@
         public int [] getGrainFractions() {
             return grainFractions;
         }
+
+        public int getStationType() {
+            return SedimentLoadData.measurementStationType(
+                SedimentLoadData.grainFractionIndex(this.description));
+        }
     } // class LoadSum
 
     public static final LoadSum [] LOAD_SUMS = {
@@ -223,7 +227,6 @@
 
         SedimentLoadDataResult sldr = new SedimentLoadDataResult();
 
-        boolean isKmUp = isKmUp();
         Set<Integer> missingFractions = new TreeSet<Integer>();
 
         Not notEpochs = new Not(IsEpoch.INSTANCE);
@@ -243,8 +246,8 @@
             for (LoadSum ls: LOAD_SUMS) {
 
                 double [][] result = sum(
-                    sld, ls.getGrainFractions(), filter, sum, isKmUp,
-                    missingFractions);
+                    sld, ls.getGrainFractions(), ls.getStationType(),
+                    filter, sum, missingFractions);
 
                 if (result[0].length == 0 || DoubleUtil.isNaN(result[1])) {
                     // TODO: resolve i18n
@@ -266,7 +269,6 @@
         return new CalculationResult(sldr, this);
     }
 
-
     private CalculationResult calculateEpochs() {
         SedimentLoadData sld =
             SedimentLoadDataFactory.INSTANCE.getSedimentLoadData(river);
@@ -277,7 +279,6 @@
 
         SedimentLoadDataResult sldr = new SedimentLoadDataResult();
 
-        boolean isKmUp = isKmUp();
         Set<Integer> missingFractions = new TreeSet<Integer>();
 
         Sum sum = new Sum();
@@ -303,8 +304,8 @@
                         .add(new TimeRangeIntersects(year));
 
                     double [][] result = sum(
-                        sld, ls.getGrainFractions(), filter, sum, isKmUp,
-                        missingFractions);
+                        sld, ls.getGrainFractions(), ls.getStationType(),
+                        filter, sum, missingFractions);
 
                     if (result[0].length == 0 || DoubleUtil.isNaN(result[1])) {
                         // TODO: resolve i18n
@@ -342,7 +343,6 @@
 
         SedimentDensity sd = getSedimentDensity();
 
-        boolean isKmUp = isKmUp();
         Set<Integer> missingFractions = new TreeSet<Integer>();
 
         for (int [] epoch: epochs) {
@@ -358,8 +358,8 @@
 
             for (LoadSum ls: LOAD_SUMS) {
                 double [][] result = sum(
-                    sld, ls.getGrainFractions(), filter, sum, isKmUp,
-                    missingFractions);
+                    sld, ls.getGrainFractions(), ls.getStationType(),
+                    filter, sum, missingFractions);
 
                 if (result[0].length == 0 || DoubleUtil.isNaN(result[1])) {
                     // TODO: resolve i18n
@@ -377,16 +377,6 @@
         return new CalculationResult(sldr, this);
     }
 
-    /** Figure out flow direction of river. */
-    private final boolean isKmUp() {
-        River r = RiverFactory.getRiver(river);
-        if (r == null) {
-            addProblem("minfo.missing.river");
-            return true;
-        }
-        return r.getKmUp();
-    }
-
     private final boolean inM3() {
         return unit.equals("m3_per_a");
     }
@@ -415,9 +405,9 @@
     public double[][] sum(
         SedimentLoadData sld,
         int []           grainFractions,
+        int              lsSType,
         Value.Filter     filter,
         Sum              sum,
-        boolean          isKMUp,
         Set<Integer>     missingFractions
     ) {
         List<Station> stations = sld.findStations(from, to);
@@ -428,15 +418,34 @@
 
         for (int j = 0, S = stations.size(); j < S; ++j) {
             Station station = stations.get(j);
+            int sType = station.getType();
+
             for (int i = 0; i < grainFractions.length; ++i) {
                 int gf = grainFractions[i];
+                int gfSType = SedimentLoadData.measurementStationType(gf);
+
                 sum.reset();
+
+                // Add current single fraction at current station
                 station.filterGrainFraction(gf, filter, sum);
-                if (sum.getN() == 0) { // No values found
-                    int msType = SedimentLoadData.measurementStationType(gf);
-                    // Station of right fraction type already? No: take previous.
-                    if (!station.isType(msType)) {
-                        Station prev = station.prevByType(msType, isKMUp);
+
+                if (gfSType == Station.UNKNOWN) {
+                    log.error("No measurement station type defined for " +
+                              "fraction-index" + gf);
+                }
+                else {
+                    if (lsSType != Station.BED_LOAD &&
+                        lsSType != Station.SUSPENDED &&
+                        (sType == Station.BED_LOAD ||
+                         sType == Station.SUSPENDED) &&
+                        sum.getN() == 0) {
+                        /* In case the station-type of the load sum is
+                           a combined type and we are at non-combined station:
+                           we need to add values from previous station of
+                           the other type for fractions not given here. */
+                        int otherType = sType == Station.BED_LOAD ?
+                            Station.SUSPENDED : Station.BED_LOAD;
+                        Station prev = station.prevByType(otherType);
                         if (prev != null) {
                             prev.filterGrainFraction(gf, filter, sum);
                         }

http://dive4elements.wald.intevation.org