changeset 4373:1fb224bb2c6b

Added sediment load calculation.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 02 Nov 2012 14:54:29 +0100
parents 19772b414d46
children e596b2cc9f38
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadCalculation.java
diffstat 1 files changed, 261 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadCalculation.java	Fri Nov 02 14:54:29 2012 +0100
@@ -0,0 +1,261 @@
+package de.intevation.flys.artifacts.model.minfo;
+
+import gnu.trove.TDoubleArrayList;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+import org.jfree.util.Log;
+
+import de.intevation.flys.artifacts.access.SedimentLoadAccess;
+import de.intevation.flys.artifacts.model.Calculation;
+import de.intevation.flys.artifacts.model.CalculationResult;
+
+
+public class SedimentLoadCalculation
+extends Calculation
+{
+
+    private static final Logger logger = Logger
+        .getLogger(SedimentLoadCalculation.class);
+
+    protected String river;
+    protected String yearEpoch;
+    protected double kmUp;
+    protected double kmLow;
+    protected int[] period;
+    protected int[][] epoch;
+    protected String unit;
+
+    public SedimentLoadCalculation() {
+    }
+
+    public CalculationResult calculate(SedimentLoadAccess access) {
+        logger.info("SedimentLoadCalculation.calculate");
+
+        String river = access.getRiver();
+        String yearEpoch = access.getYearEpoch();
+        String unit = access.getUnit();
+        int[] period = null;
+        int[][] epoch = null;
+        double kmUp = access.getUpperKM();
+        double kmLow = access.getLowerKM();
+        if (yearEpoch.equals("year")) {
+            period = access.getPeriod();
+            epoch = null;
+        }
+        else if (yearEpoch.equals("epoch")) {
+            epoch = access.getEpochs();
+            period = null;
+        }
+        else {
+            addProblem("minfo.missing.year_epoch");
+        }
+
+        if (river == null) {
+            // TODO: i18n
+            addProblem("minfo.missing.river");
+        }
+
+        if (period == null && epoch == null) {
+            addProblem("minfo.missing.time");
+        }
+
+        if (!hasProblems()) {
+            this.river = river;
+            this.yearEpoch = yearEpoch;
+            this.unit = unit;
+            this.period = period;
+            this.epoch = epoch;
+            this.kmUp = kmUp;
+            this.kmLow = kmLow;
+            return internalCalculate();
+        }
+
+        return new CalculationResult();
+    }
+
+    private CalculationResult internalCalculate() {
+        logger.debug("internalCalulate; mode:" + yearEpoch);
+        if (yearEpoch.equals("year")) {
+            List<SedimentLoadResult> results =
+                new ArrayList<SedimentLoadResult>();
+            for (int i = period[0]; i <= period[1]; i++) {
+                logger.debug("calculating for year: " + i);
+                SedimentLoadResult res = calculateYear(i);
+                results.add(res);
+            }
+            return new CalculationResult(
+                results.toArray(new SedimentLoadResult[results.size()]), this);
+        }
+        else if (yearEpoch.equals("epoch")) {
+            List<SedimentLoadResult> results =
+                new ArrayList<SedimentLoadResult>();
+            for (int i = 0; i < epoch.length; i++) {
+                SedimentLoadResult res = calculateEpoch(i);
+                results.add(res);
+            }
+            return new CalculationResult(
+                results.toArray(new SedimentLoadResult[results.size()]), this);
+        }
+        else if (yearEpoch.equals("off_epoch")) {
+            List<SedimentLoadResult> results =
+                new ArrayList<SedimentLoadResult>();
+            for (int i = 0; i < epoch.length; i++) {
+                SedimentLoadResult res = calculateOffEpoch(i);
+                results.add(res);
+            }
+            return new CalculationResult(
+                results.toArray(new SedimentLoadResult[results.size()]), this);
+        }
+        return null;
+    }
+
+    private SedimentLoadResult calculateEpoch(int i) {
+        List<SedimentLoad> epochLoads = new ArrayList<SedimentLoad>();
+        for (int j = epoch[i][0]; j < epoch[i][1]; j++) {
+            epochLoads.add(SedimentLoadFactory.getLoadwithData(
+                this.river,
+                this.yearEpoch,
+                this.kmLow,
+                this.kmUp,
+                j,
+                j));
+        }
+
+        SedimentLoad resLoad = new SedimentLoad();
+        TDoubleArrayList kms = new TDoubleArrayList();
+
+        for (SedimentLoad load : epochLoads) {
+            for (double km : load.getKms()) {
+                if (!kms.contains(km)) {
+                    kms.add(km);
+                }
+            }
+        }
+
+        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());
+                    cSum++;
+                }
+                if (f.getFine_middle() > 0d) {
+                    double fm = resLoad.getFraction(km).getFine_middle();
+                    resLoad.setFineMiddle(km, fm + f.getFine_middle());
+                    fmSum++;
+                }
+                if (f.getSand() > 0d) {
+                    double s = resLoad.getFraction(km).getSand();
+                    resLoad.setSand(km, s + f.getSand());
+                    sSum++;
+                }
+                if (f.getSusp_sand() > 0d) {
+                    double s = resLoad.getFraction(km).getSand();
+                    resLoad.setSuspSand(km, s + f.getSusp_sand());
+                    ssSum++;
+                }
+                if (f.getSusp_sand_bed() > 0d) {
+                    double s = resLoad.getFraction(km).getSusp_sand_bed();
+                    resLoad.setSuspSandBed(km, s + f.getSusp_sand_bed());
+                    ssbSum++;
+                }
+                if (f.getSusp_sediment() > 0d) {
+                    double s = resLoad.getFraction(km).getSusp_sediment();
+                    resLoad.setSuspSediment(km, s + f.getSusp_sediment());
+                    sseSum++;
+                }
+            }
+            SedimentLoadFraction fr = resLoad.getFraction(km);
+            resLoad.setCoarse(km, fr.getCoarse()/cSum);
+            resLoad.setFineMiddle(km, fr.getFine_middle()/fmSum);
+            resLoad.setSand(km, fr.getSand()/sSum);
+            resLoad.setSuspSand(km, fr.getSusp_sand()/ssSum);
+            resLoad.setSuspSandBed(km, fr.getSusp_sand_bed()/ssbSum);
+            resLoad.setSuspSediment(km, fr.getSusp_sediment()/sseSum);
+        }
+        resLoad.setDescription("");
+        resLoad.setEpoch(true);
+
+        SedimentLoad sl = calculateTotalLoad(resLoad);
+        return new SedimentLoadResult(epoch[i][0], epoch[i][1], sl);
+    }
+
+    private SedimentLoadResult calculateOffEpoch(int i) {
+        return null;
+    }
+
+    private SedimentLoadResult calculateYear(int y) {
+        SedimentLoad load = SedimentLoadFactory.getLoadwithData(
+            this.river,
+            this.yearEpoch,
+            this.kmLow,
+            this.kmUp,
+            y,
+            y);
+
+        SedimentLoad sl = calculateTotalLoad(load);
+        if (unit.equals("m3_per_a")) {
+            SedimentLoad slu = calculateUnit(sl);
+        }
+
+        SedimentLoadResult result = new SedimentLoadResult(
+            y,
+            0,
+            sl);
+        return result;
+    }
+
+    private SedimentLoad validateData(SedimentLoad load) {
+        SedimentLoad clean = new SedimentLoad();
+        Set<Double> kms = load.getKms();
+        for (double km : kms) {
+            SedimentLoadFraction fraction = load.getFraction(km);
+            if (fraction.getCoarse() > 0 &&
+                fraction.getFine_middle() > 0 &&
+                fraction.getSand() > 0 &&
+                fraction.getSusp_sand() > 0&&
+                fraction.getSusp_sand_bed() > 0 &&
+                fraction.getSusp_sediment() > 0) {
+                clean.addKm(km, fraction);
+            }
+        }
+        return clean;
+    }
+
+    private SedimentLoad calculateTotalLoad(SedimentLoad load) {
+        Log.debug("calculateTotalLoad");
+        SedimentLoad clean = validateData(load);
+        if (clean.getKms().size() > 0) {
+            Iterator<Double> iter = clean.getKms().iterator();
+            while (iter.hasNext()) {
+                double km = iter.next();
+                SedimentLoadFraction f =clean.getFraction(km);
+                double total = f.getCoarse() +
+                    f.getFine_middle() +
+                    f.getSand() +
+                    f.getSusp_sand() +
+                    f.getSusp_sediment();
+                load.setTotal(km, total);
+            }
+        }
+        return load;
+    }
+
+    private SedimentLoad calculateUnit(SedimentLoad load) {
+        //TODO implement me!
+        return load;
+    }
+}

http://dive4elements.wald.intevation.org