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@5863: teichmann@5831: package org.dive4elements.river.artifacts.model.minfo; ingo@3784: teichmann@5831: import org.dive4elements.river.artifacts.model.DateRange; andre@8581: import org.dive4elements.river.utils.DoubleUtil; andre@8581: ingo@3784: import gnu.trove.TDoubleArrayList; ingo@3784: andre@8581: import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; andre@8581: andre@8581: import org.apache.commons.math.ArgumentOutsideDomainException; ingo@3784: ingo@3784: public class BedloadDiameterResult ingo@3784: extends BedQualityDiameterResult ingo@3784: { ingo@3784: protected TDoubleArrayList diameter; andre@8581: protected PolynomialSplineFunction interpol; andre@8581: andre@8581: /** Set to true if this result can't be interpolated.*/ andre@8581: protected boolean nonInterpolResult; ingo@3784: ingo@3784: public BedloadDiameterResult( ingo@3784: String type, ingo@3784: TDoubleArrayList diameter, ingo@3784: TDoubleArrayList km, ingo@3784: DateRange range ingo@3784: ) { ingo@3784: super (type, km); ingo@3784: this.diameter = diameter; andre@8581: interpol = null; andre@8581: nonInterpolResult = false; ingo@3784: } ingo@3784: ingo@3784: public double getDiameter(int ndx) { ingo@3784: if (diameter != null) { ingo@3784: return this.diameter.get(ndx); ingo@3784: } ingo@3784: return Double.NaN; ingo@3784: } ingo@3784: ingo@3785: public double getDiameter(double km) { ingo@3785: if (kms.indexOf(km) >= 0) { ingo@3785: return diameter.get(kms.indexOf(km)); ingo@3785: } ingo@3785: return Double.NaN; ingo@3785: } ingo@3785: andre@8581: public double getDiameterInterpol(double km) { andre@8581: if (nonInterpolResult) { andre@8581: return Double.NaN; andre@8581: } andre@8581: if (interpol == null) { andre@8581: interpol = DoubleUtil.getLinearInterpolator(kms, diameter); andre@8581: if (interpol == null) { andre@8581: nonInterpolResult = true; andre@8581: return Double.NaN; andre@8581: } andre@8581: } andre@8581: try { andre@8581: return interpol.value(km); andre@8581: } catch (ArgumentOutsideDomainException e) { andre@8581: /* This is expected for many results. */ andre@8581: return Double.NaN; andre@8581: } andre@8581: } andre@8581: ingo@3784: public double[][] getDiameterData() { ingo@3784: return new double[][] { ingo@3784: kms.toNativeArray(), ingo@3784: diameter.toNativeArray() ingo@3784: }; ingo@3784: } ingo@3784: }