changeset 6928:2119f13f95ab

SedimentLoadCalculation: Refactoring, extracted method.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 27 Aug 2013 14:58:03 +0200
parents 0288db5e90d5
children 35ecfd1a861a
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java
diffstat 1 files changed, 71 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java	Tue Aug 27 12:46:11 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java	Tue Aug 27 14:58:03 2013 +0200
@@ -125,6 +125,76 @@
         return null;
     }
 
+    private void calculateEpochKm(
+        List<SedimentLoad> epochLoads,
+        SedimentLoad resLoad,
+        double km
+    ) {
+        int cSum = 0;
+        int fmSum = 0;
+        int sSum = 0;
+        int ssSum = 0;
+        int ssbSum = 0;
+        int sseSum = 0;
+        for (SedimentLoad load : epochLoads) {
+            SedimentLoadFraction f = load.getFraction(km);
+            if (f.getCoarse() > 0d) {
+                double c = resLoad.getFraction(km).getCoarse();
+                resLoad.setCoarse(km, c + f.getCoarse(), f.getCoarseRange());
+                cSum++;
+            }
+            if (f.getFineMiddle() > 0d) {
+                double fm = resLoad.getFraction(km).getFineMiddle();
+                resLoad.setFineMiddle(km, fm + f.getFineMiddle(), f.getFineMiddleRange());
+                fmSum++;
+            }
+            if (f.getSand() > 0d) {
+                double s = resLoad.getFraction(km).getSand();
+                resLoad.setSand(km, s + f.getSand(), f.getSandRange());
+                sSum++;
+            }
+            if (f.getSuspSand() > 0d) {
+                double s = resLoad.getFraction(km).getSuspSand();
+                resLoad.setSuspSand(km, s + f.getSuspSand(), f.getSuspSandRange());
+                ssSum++;
+            }
+            if (f.getSuspSandBed() > 0d) {
+                double s = resLoad.getFraction(km).getSuspSandBed();
+                resLoad.setSuspSandBed(km, s + f.getSuspSandBed(), f.getSuspSandBedRange());
+                ssbSum++;
+            }
+            if (f.getSuspSediment() > 0d) {
+                double s = resLoad.getFraction(km).getSuspSediment();
+                resLoad.setSuspSediment(km, s + f.getSuspSediment(), f.getSuspSedimentRange());
+                sseSum++;
+            }
+        }
+
+        SedimentLoadFraction fr = resLoad.getFraction(km);
+        // Prevent divisions by zero, the fraction defaults to 0d.
+        if (cSum != 0) {
+            resLoad.setCoarse(km, fr.getCoarse()/cSum, fr.getCoarseRange());
+        }
+        if (fmSum != 0) {
+            resLoad.setFineMiddle(km, fr.getFineMiddle()/fmSum,
+                fr.getFineMiddleRange());
+        }
+        if (sSum != 0) {
+            resLoad.setSand(km, fr.getSand()/sSum, fr.getSandRange());
+        }
+        if (ssSum != 0) {
+            resLoad.setSuspSand(km, fr.getSuspSand()/ssSum,
+                fr.getSuspSandRange());
+        }
+        if (ssbSum != 0) {
+            resLoad.setSuspSandBed(km, fr.getSuspSandBed()/ssbSum,
+                fr.getSuspSandBedRange());
+        }
+        if (sseSum != 0) {
+            resLoad.setSuspSediment(km, fr.getSuspSediment()/sseSum, fr.getSuspSedimentRange());
+        }
+    }
+
     private SedimentLoadResult calculateEpoch(int i) {
         List<SedimentLoad> epochLoads = new ArrayList<SedimentLoad>();
         for (int j = epoch[i][0]; j < epoch[i][1]; j++) {
@@ -149,69 +219,7 @@
         }
 
         for (int j = 0; j < kms.size(); j++) {
-            int cSum = 0;
-            int fmSum = 0;
-            int sSum = 0;
-            int ssSum = 0;
-            int ssbSum = 0;
-            int sseSum = 0;
-            double km = kms.get(j);
-            for (SedimentLoad load : epochLoads) {
-                SedimentLoadFraction f = load.getFraction(km);
-                if (f.getCoarse() > 0d) {
-                    double c = resLoad.getFraction(km).getCoarse();
-                    resLoad.setCoarse(km, c + f.getCoarse(), f.getCoarseRange());
-                    cSum++;
-                }
-                if (f.getFineMiddle() > 0d) {
-                    double fm = resLoad.getFraction(km).getFineMiddle();
-                    resLoad.setFineMiddle(km, fm + f.getFineMiddle(), f.getFineMiddleRange());
-                    fmSum++;
-                }
-                if (f.getSand() > 0d) {
-                    double s = resLoad.getFraction(km).getSand();
-                    resLoad.setSand(km, s + f.getSand(), f.getSandRange());
-                    sSum++;
-                }
-                if (f.getSuspSand() > 0d) {
-                    double s = resLoad.getFraction(km).getSuspSand();
-                    resLoad.setSuspSand(km, s + f.getSuspSand(), f.getSuspSandRange());
-                    ssSum++;
-                }
-                if (f.getSuspSandBed() > 0d) {
-                    double s = resLoad.getFraction(km).getSuspSandBed();
-                    resLoad.setSuspSandBed(km, s + f.getSuspSandBed(), f.getSuspSandBedRange());
-                    ssbSum++;
-                }
-                if (f.getSuspSediment() > 0d) {
-                    double s = resLoad.getFraction(km).getSuspSediment();
-                    resLoad.setSuspSediment(km, s + f.getSuspSediment(), f.getSuspSedimentRange());
-                    sseSum++;
-                }
-            }
-            SedimentLoadFraction fr = resLoad.getFraction(km);
-            // Prevent divisions by zero, the fraction defaults to 0d.
-            if (cSum != 0) {
-                resLoad.setCoarse(km, fr.getCoarse()/cSum, fr.getCoarseRange());
-            }
-            if (fmSum != 0) {
-                resLoad.setFineMiddle(km, fr.getFineMiddle()/fmSum,
-                    fr.getFineMiddleRange());
-            }
-            if (sSum != 0) {
-                resLoad.setSand(km, fr.getSand()/sSum, fr.getSandRange());
-            }
-            if (ssSum != 0) {
-                resLoad.setSuspSand(km, fr.getSuspSand()/ssSum,
-                    fr.getSuspSandRange());
-            }
-            if (ssbSum != 0) {
-                resLoad.setSuspSandBed(km, fr.getSuspSandBed()/ssbSum,
-                    fr.getSuspSandBedRange());
-            }
-            if (sseSum != 0) {
-                resLoad.setSuspSediment(km, fr.getSuspSediment()/sseSum, fr.getSuspSedimentRange());
-            }
+            calculateEpochKm(epochLoads, resLoad, kms.get(j));
         }
         resLoad.setDescription("");
         resLoad.setEpoch(true);

http://dive4elements.wald.intevation.org