view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadCalculation.java @ 4483:cc6323401643

Cosmetics: Removed some WSP, obsolete imports. Documentation added.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 13 Nov 2012 16:52:01 +0100
parents 1fb224bb2c6b
children a9753f717b3d
line wrap: on
line source
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;


/** Calculate sediment load. */
public class SedimentLoadCalculation
extends Calculation
{

    /** Private logger. */
    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;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org