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@8202: private static final Logger log = 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) { teichmann@8202: log.info("BedDiffCalculation.calculate"); raimund@3887: tom@8169: this.river = access.getRiverName(); tom@8169: this.heightIds = access.extractHeightIds(context); andre@8610: double from = access.getFrom(); andre@8610: double to = access.getTo(); andre@8610: double start; andre@8610: double end; andre@8610: andre@8610: if ((!Double.isNaN(from) && !Double.isNaN(to)) && from > to) { andre@8610: log.debug("Reordering range."); andre@8610: start = to; andre@8610: end = from; andre@8610: } else { andre@8610: start = from; andre@8610: end = to; andre@8610: } raimund@3887: tom@8169: BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length]; tom@8169: for (int i = 0; i < heightIds.length; i++) { andre@8610: BedHeightData [] pair = getHeightPair(heightIds[i], start, end); tom@8169: if (pair[0].getYear() == null || pair[1].getYear() == null) { tom@8169: addProblem("beddiff.missing.year"); tom@8169: } tom@8273: results[i] = calculateYearDifference(pair, heightIds[i]); raimund@3887: } raimund@3887: rrenkert@6215: return new CalculationResult(results, this); raimund@3887: } raimund@3887: felix@6573: /** Get two BedHeights from factory. */ tom@8568: private static BedHeightData [] getHeightPair(int [] ids, double from, double to) { tom@8568: return new BedHeightData [] { tom@8568: (BedHeightData)BedHeightFactory.getHeight("single", ids[0], from, to), tom@8568: (BedHeightData)BedHeightFactory.getHeight("single", ids[1], from, to) teichmann@6148: }; raimund@3887: } raimund@3887: tom@8169: private BedDiffYearResult calculateYearDifference( tom@8568: BedHeightData[] pair, tom@8273: int[] ids tom@8169: ) { teichmann@8202: log.debug("BedDiffCalculation.calculateYearDifference"); tom@8568: BedHeightData s1 = pair[0]; tom@8568: BedHeightData s2 = pair[1]; teichmann@6147: teichmann@6147: TDoubleArrayList stations = s1.getStations(); teichmann@6147: int size = stations.size(); teichmann@6147: rrenkert@8422: TDoubleArrayList diffRes = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList kms = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList soundings1 = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList soundings2 = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList absolute = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList gap1 = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList gap2 = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList heights1 = new TDoubleArrayList(size); rrenkert@8422: TDoubleArrayList heights2 = 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); 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@8422: soundings1.add(s1.getSoundingWidth(station)); rrenkert@8422: soundings2.add(s2.getSoundingWidth(station)); teichmann@6147: rrenkert@8422: gap1.add(s1.getDataGap(station)); rrenkert@8422: gap2.add(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); raimund@3887: } raimund@3887: } raimund@3898: return new BedDiffYearResult( raimund@3898: kms, raimund@3898: diffRes, raimund@3898: heights1, raimund@3898: heights2, rrenkert@8422: soundings1, rrenkert@8422: soundings2, raimund@3898: absolute, rrenkert@8422: gap1, rrenkert@8422: gap2, raimund@3898: s1.getYear(), felix@6089: s2.getYear(), felix@6089: s1.getName(), tom@8273: s2.getName(), tom@8273: ids[0], tom@8273: ids[1]); raimund@3887: } raimund@3887: } felix@6948: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :