Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java @ 8053:72760ca2fc2b
Sediment load: dispatch calculation modes.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 18 Jul 2014 11:58:59 +0200 |
parents | 1dae69eff79d |
children | cd35b76f1ef8 |
line wrap: on
line source
/* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde * Software engineering by Intevation GmbH * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.artifacts.model.minfo; import java.util.List; import java.util.Set; 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; import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Value; import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Station; import org.dive4elements.river.model.River; import org.dive4elements.river.utils.DoubleUtil; public class SedimentLoadDataCalculation extends Calculation { private static final Logger log = Logger .getLogger(SedimentLoadDataCalculation.class); public static final int [] TOTAL_LOAD_FLYS = { SedimentLoadData.GF_COARSE, SedimentLoadData.GF_FINE_MIDDLE, SedimentLoadData.GF_SAND, SedimentLoadData.GF_SUSP_SEDIMENT }; public static final int [] BED_LOAD_FLYS = { SedimentLoadData.GF_COARSE, SedimentLoadData.GF_FINE_MIDDLE, SedimentLoadData.GF_SAND }; public static final int [] BED_LOAD_SUSP_SAND_FLYS = { SedimentLoadData.GF_COARSE, SedimentLoadData.GF_FINE_MIDDLE, SedimentLoadData.GF_SAND, SedimentLoadData.GF_SUSP_SAND }; public static final int [] TOTAL_LOAD_BFG = { SedimentLoadData.GF_TOTAL }; public static final int [] BED_LOAD_BFG = { SedimentLoadData.GF_BED_LOAD }; public static final int [] SUSPENDED_LOAD_BFG = { SedimentLoadData.GF_SUSPENDED_LOAD }; public static final int [] COARSE_FLYS = { SedimentLoadData.GF_COARSE }; public static final int [] FINE_MIDDLE_FLYS = { SedimentLoadData.GF_FINE_MIDDLE }; public static final int [] SAND_FLYS = { SedimentLoadData.GF_SAND }; public static final int [] SUSP_SAND_FLYS = { SedimentLoadData.GF_SUSP_SAND }; public static final int [] SUSP_SAND_BED_FLYS = { SedimentLoadData.GF_SUSP_SAND_BED }; public static final int [] SUSP_SEDIMENT_FLYS = { SedimentLoadData.GF_SUSP_SEDIMENT }; public static final class Sum implements Value.Visitor { private int n; private double sum; private double scale; public Sum() { } public Sum(double scale) { this.scale = scale; } public double getSum() { return sum * scale; } public int getN() { return n; } public double getScale() { return scale; } public void reset() { n = 0; sum = 0.0; } @Override public void visit(Value value) { sum += value.getValue(); ++n; } } // class Aggregate private String river; private String yearEpoch; private String unit; private int [][] epoch; private int [] period; private double from; private double to; public SedimentLoadDataCalculation() { } public CalculationResult calculate(SedimentLoadAccess access) { log.info("SedimentLoadDataCalculation.calculate"); String river = access.getRiverName(); String yearEpoch = access.getYearEpoch(); String unit = access.getUnit(); int [] period = null; int [][] epoch = null; double from = access.getUpperKM(); double to = access.getLowerKM(); if (yearEpoch.equals("year")) { period = access.getPeriod(); } else if (yearEpoch.equals("epoch") || yearEpoch.equals("off_epoch")) { epoch = access.getEpochs(); } 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.from = from; this.to = to; return internalCalculate(); } return new CalculationResult(this); } private CalculationResult internalCalculate() { if ("year".equals(yearEpoch)) return calculateYears(); if ("epoch".equals(yearEpoch)) return calculateEpochs(); if ("off_epoch".equals(yearEpoch)) return calculateOffEpochs(); // TODO: i18n addProblem("minfo.sediment.load.unknown.calc.mode"); return new CalculationResult(this); } private CalculationResult calculateYears() { // TODO: Implement me! return null; } private CalculationResult calculateEpochs() { // TODO: Implement me! return null; } private CalculationResult calculateOffEpochs() { // TODO: Implement me! return null; } /** Figure out flow direction of river. */ private boolean isKmUp() { River r = RiverFactory.getRiver(river); if (r == null) { addProblem("minfo.missing.river"); return true; } return r.getKmUp(); } public double[][] sum( SedimentLoadData sld, int [] grainFractions, Value.Filter filter, Sum sum, boolean isKMUp, Set<Integer> missingFractions ) { List<Station> stations = sld.findStations(from, to); double [] values = new double[grainFractions.length]; double [][] result = new double[2][stations.size()]; for (int j = 0, S = stations.size(); j < S; ++j) { Station station = stations.get(j); for (int i = 0; i < grainFractions.length; ++i) { int gf = grainFractions[i]; sum.reset(); 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 (prev != null) { prev.filterGrainFraction(gf, filter, sum); } } } if (sum.getN() == 0) { missingFractions.add(gf); values[i] = Double.NaN; } else { values[i] = sum.getSum(); } } result[0][j] = station.getStation(); result[1][j] = DoubleUtil.sum(values); } // TODO: Handle 'virtual' measument stations 'from' and 'to'. return result; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :