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: tom@8169: this.river = access.getRiverName(); tom@8169: this.heightIds = access.extractHeightIds(context); raimund@3887: tom@8169: BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length]; tom@8169: for (int i = 0; i < heightIds.length; i++) { tom@8169: BedHeightSingleData [] pair = getHeightPair(heightIds[i]); tom@8169: if (pair[0].getYear() == null || pair[1].getYear() == null) { tom@8169: addProblem("beddiff.missing.year"); tom@8169: } tom@8169: results[i] = calculateYearDifference(pair); raimund@3887: } raimund@3887: rrenkert@6215: return new CalculationResult(results, this); raimund@3887: } raimund@3887: felix@6573: /** Get two BedHeights from factory. */ tom@8169: private static BedHeightSingleData [] getHeightPair(int [] ids) { tom@8169: return new BedHeightSingleData [] { tom@8169: (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[0]), tom@8169: (BedHeightSingleData)BedHeightFactory.getHeight("single", ids[1]) teichmann@6148: }; raimund@3887: } raimund@3887: tom@8169: private BedDiffYearResult calculateYearDifference( tom@8169: BedHeightSingleData[] pair tom@8169: ) { felix@7370: logger.debug("BedDiffCalculation.calculateYearDifference"); tom@8169: BedHeightSingleData s1 = pair[0]; tom@8169: BedHeightSingleData s2 = 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: tom@8169: Integer range = null; tom@8169: if (s1.getYear() != null && s2.getYear() != null) { tom@8169: range = Math.abs(s1.getYear() - s2.getYear()); tom@8169: } 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: tom@8169: if (range != null) { tom@8169: absolute.add((hDiff / range) * 100d); tom@8169: } 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 :