teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5831: package org.dive4elements.river.artifacts.model.minfo; raimund@3887: raimund@3887: import gnu.trove.TDoubleArrayList; raimund@3887: raimund@3887: import org.apache.log4j.Logger; raimund@3887: teichmann@6148: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.river.artifacts.access.BedDifferencesAccess; teichmann@5831: import org.dive4elements.river.artifacts.model.Calculation; teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; raimund@3887: felix@6573: /** felix@6573: * Perform calculation of differences of bed height (german Sohlhoehe). felix@6573: * The input are either single year data or epochs. felix@6573: */ raimund@3887: public class BedDiffCalculation raimund@3887: extends Calculation raimund@3887: { teichmann@6148: private static final Logger logger = teichmann@6148: Logger.getLogger(BedDiffCalculation.class); raimund@3887: teichmann@6148: protected String river; teichmann@6148: protected int [][] heightIds; raimund@3887: raimund@3887: public BedDiffCalculation() { raimund@3887: } raimund@3887: teichmann@6148: public CalculationResult calculate(BedDifferencesAccess access, CallContext context) { raimund@3898: logger.info("BedDiffCalculation.calculate"); raimund@3887: felix@7261: String river = access.getRiverName(); teichmann@6148: int [][] heightIds = access.extractHeightIds(context); raimund@3887: raimund@3887: if (river == null) { raimund@3887: // TODO: i18n raimund@3887: addProblem("minfo.missing.river"); raimund@3887: } raimund@3887: raimund@3887: if (!hasProblems()) { teichmann@6148: this.river = river; teichmann@6148: this.heightIds = heightIds; raimund@3887: return internalCalculate(); raimund@3887: } raimund@3887: raimund@3887: return new CalculationResult(); raimund@3887: } raimund@3887: raimund@3887: private CalculationResult internalCalculate() { rrenkert@6215: BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length]; raimund@3898: rrenkert@6215: for (int i = 0; i < heightIds.length; i++) { felix@7391: BedHeightData [] pair = getHeightPair(heightIds[i], "single"); rrenkert@6215: results[i] = calculateYearDifference(pair); raimund@3887: } rrenkert@6215: return new CalculationResult(results, this); raimund@3887: } raimund@3887: felix@6573: /** Get two BedHeights from factory. */ felix@7391: private static BedHeightData [] getHeightPair(int [] ids, String type) { felix@7391: return new BedHeightData [] { teichmann@6148: BedHeightFactory.getHeight(type, ids[0], 0), teichmann@6148: BedHeightFactory.getHeight(type, ids[1], 0) teichmann@6148: }; raimund@3887: } raimund@3887: felix@7391: private BedDiffYearResult calculateYearDifference(BedHeightData[] pair) { felix@7370: logger.debug("BedDiffCalculation.calculateYearDifference"); felix@7391: BedHeightSingleData s1 = (BedHeightSingleData) pair[0]; felix@7391: BedHeightSingleData s2 = (BedHeightSingleData) pair[1]; teichmann@6147: teichmann@6147: TDoubleArrayList stations = s1.getStations(); teichmann@6147: int size = stations.size(); teichmann@6147: rrenkert@7828: TDoubleArrayList diffRes = new TDoubleArrayList(size); rrenkert@7828: TDoubleArrayList kms = new TDoubleArrayList(size); rrenkert@7828: TDoubleArrayList soundings = new TDoubleArrayList(size); rrenkert@7828: TDoubleArrayList absolute = new TDoubleArrayList(size); rrenkert@7828: TDoubleArrayList gap = new TDoubleArrayList(size); rrenkert@7828: TDoubleArrayList heights1 = new TDoubleArrayList(size); rrenkert@7828: TDoubleArrayList heights2 = new TDoubleArrayList(size); rrenkert@7829: TDoubleArrayList morphs1 = new TDoubleArrayList(size); rrenkert@7829: TDoubleArrayList morphs2 = new TDoubleArrayList(size); teichmann@6147: teichmann@6147: int range = Math.abs(s1.getYear() - s2.getYear()); teichmann@6147: teichmann@6147: for (int i = 0; i < size; i++) { teichmann@6147: double station = stations.getQuick(i); teichmann@6147: double h1 = s1.getHeight(station); teichmann@6147: double h2 = s2.getHeight(station); rrenkert@7829: double m1 = s1.getWidth(station); rrenkert@7829: double m2 = s2.getWidth(station); teichmann@6147: double hDiff = h1 - h2; teichmann@6147: teichmann@6147: if (!Double.isNaN(hDiff)) { raimund@3887: diffRes.add(hDiff); teichmann@6147: kms.add(station); teichmann@6147: rrenkert@7828: soundings.add(Math.max( rrenkert@7828: s1.getSoundingWidth(station), rrenkert@7828: s2.getSoundingWidth(station))); teichmann@6147: teichmann@6147: gap.add(Math.max( teichmann@6147: s1.getDataGap(station), teichmann@6147: s2.getDataGap(station))); teichmann@6147: teichmann@6147: absolute.add((hDiff / range) * 100d); teichmann@6147: heights1.add(h1); teichmann@6147: heights2.add(h2); rrenkert@7829: morphs1.add(m1); rrenkert@7829: morphs2.add(m2); raimund@3887: } raimund@3887: } raimund@3898: return new BedDiffYearResult( raimund@3898: kms, raimund@3898: diffRes, raimund@3898: heights1, raimund@3898: heights2, rrenkert@7829: morphs1, rrenkert@7829: morphs2, rrenkert@7828: soundings, raimund@3898: absolute, raimund@3898: gap, raimund@3898: s1.getYear(), felix@6089: s2.getYear(), felix@6089: s1.getName(), felix@6089: s2.getName()); raimund@3887: } raimund@3887: } felix@6948: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :