[PATCH] Sum up fractions from stations and previous stations according to respective validity model

Wald Commits scm-commit at wald.intevation.org
Thu Sep 4 17:33:05 CEST 2014


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1409844781 -7200
# Node ID 8d447516b7ddb5b282a948fc453b610e69df0346
# Parent  adbf980004c00a4e53d24717f09c89db1e4e2dbf
Sum up fractions from stations and previous stations according to respective validity model.

diff -r adbf980004c0 -r 8d447516b7dd artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Thu Sep 04 16:25:06 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Thu Sep 04 17:33:01 2014 +0200
@@ -211,21 +211,13 @@
         }
 
         public void setPrev(Station prev) {
-            this.prev = next;
+            this.prev = prev;
         }
 
         public Station getPrev() {
             return prev;
         }
 
-        public Station getNext(boolean isKMUp) {
-            return isKMUp ? getNext() : getPrev();
-        }
-
-        public Station getPrev(boolean isKMUp) {
-            return isKMUp ? getPrev() : getNext();
-        }
-
         public void merge(Station other) {
             this.type |= other.type;
             for (int i = 0, N = grainFractions.size(); i < N; ++i) {
@@ -250,8 +242,8 @@
             return a;
         }
 
-        public Station nextByType(int type, boolean isKMUp) {
-            for (Station curr = this; curr != null; curr = curr.getNext(isKMUp)) {
+        public Station nextByType(int type) {
+            for (Station curr = this; curr != null; curr = curr.getNext()) {
                 if (curr.isType(type)) {
                     return curr;
                 }
@@ -259,8 +251,8 @@
             return null;
         }
 
-        public Station prevByType(int type, boolean isKMUp) {
-            for (Station curr = this; curr != null; curr = curr.getPrev(isKMUp)) {
+        public Station prevByType(int type) {
+            for (Station curr = this; curr != null; curr = curr.getPrev()) {
                 if (curr.isType(type)) {
                     return curr;
                 }
@@ -350,15 +342,15 @@
     public SedimentLoadData() {
     }
 
-    public SedimentLoadData(Collection<Station> stations) {
-        setStations(stations);
+    public SedimentLoadData(Collection<Station> stations, boolean kmUp) {
+        setStations(stations, kmUp);
     }
 
     public Station[] getStations() {
         return stations;
     }
 
-    public void setStations(Collection<Station> stations) {
+    public void setStations(Collection<Station> stations, boolean kmUp) {
         TreeMap<Double, Station> same =
             new TreeMap<Double, Station>(EpsilonComparator.CMP);
 
@@ -376,13 +368,21 @@
         for (Station station: same.values()) {
             this.stations[i++] = station;
         }
-        wireNeighbors();
+        wireNeighbors(kmUp);
     }
 
-    private void wireNeighbors() {
-        for (int i = 1; i < stations.length; ++i) {
-            stations[i-1].setNext(stations[i]);
-            stations[i].setPrev(stations[i-1]);
+    private void wireNeighbors(boolean kmUp) {
+        if (kmUp) {
+            for (int i = stations.length - 1; i > 0; --i) {
+                stations[i].setNext(stations[i-1]);
+                stations[i-1].setPrev(stations[i]);
+            }
+        }
+        else {
+            for (int i = 1; i < stations.length; ++i) {
+                stations[i-1].setNext(stations[i]);
+                stations[i].setPrev(stations[i-1]);
+            }
         }
     }
 
diff -r adbf980004c0 -r 8d447516b7dd 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	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);
                         }
diff -r adbf980004c0 -r 8d447516b7dd artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java	Thu Sep 04 16:25:06 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java	Thu Sep 04 17:33:01 2014 +0200
@@ -15,6 +15,7 @@
 import net.sf.ehcache.Element;
 
 import org.apache.log4j.Logger;
+import org.dive4elements.river.artifacts.model.RiverFactory;
 import org.dive4elements.river.artifacts.cache.CacheFactory;
 import org.dive4elements.river.backend.SessionHolder;
 import org.hibernate.SQLQuery;
@@ -165,7 +166,8 @@
                 new SedimentLoadData.Value(load, slv_value));
         }
 
-        SedimentLoadData sld = new SedimentLoadData(id2station.values());
+        SedimentLoadData sld = new SedimentLoadData(id2station.values(),
+            RiverFactory.getRiver(river).getKmUp());
 
         return sld;
     }


More information about the Dive4Elements-commits mailing list