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.states.sq; rrenkert@5501: aheinecke@7298: import java.util.List; aheinecke@7298: aheinecke@7298: import org.apache.log4j.Logger; aheinecke@7298: teichmann@5831: import org.dive4elements.artifactdatabase.state.DefaultFacet; teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; aheinecke@7298: import org.dive4elements.artifacts.DataProvider; teichmann@5831: import org.dive4elements.river.artifacts.math.fitting.Function; teichmann@5831: import org.dive4elements.river.artifacts.math.fitting.FunctionFactory; teichmann@5831: import org.dive4elements.river.artifacts.model.sq.SQFunction; teichmann@5831: import org.dive4elements.river.artifacts.model.sq.StaticSQRelation; rrenkert@5501: rrenkert@5501: rrenkert@5501: public class StaticSQRelationFacet rrenkert@5501: extends DefaultFacet rrenkert@5501: implements Facet rrenkert@5501: { rrenkert@5501: public static final String FUNCTION = "sq-pow"; rrenkert@5501: rrenkert@5501: private StaticSQRelation relation; rrenkert@5501: aheinecke@7298: private static final Logger logger = aheinecke@7298: Logger.getLogger(StaticSQRelationFacet.class); rrenkert@5501: rrenkert@5501: public StaticSQRelationFacet( rrenkert@5501: int ndx, rrenkert@5501: String name, rrenkert@5501: String description, rrenkert@5501: StaticSQRelation relation) { rrenkert@5501: super(ndx, name, description); rrenkert@5501: this.relation = relation; rrenkert@5501: } rrenkert@5501: rrenkert@5501: @Override rrenkert@5501: public Object getData(Artifact artifact, CallContext context) { rrenkert@5501: double qmax = relation.getQmax(); rrenkert@5501: double[] coeffs = new double[] {relation.getA(), relation.getB()}; rrenkert@5501: Function func = FunctionFactory.getInstance().getFunction(FUNCTION); teichmann@5831: org.dive4elements.river.artifacts.math.Function function = rrenkert@5501: func.instantiate(coeffs); aheinecke@7298: aheinecke@7298: /* Figure out a good starting point by checking for calculated aheinecke@7298: * SQ Curves and using their starting point */ aheinecke@7298: aheinecke@7298: // this is ok because we are a DefaultFacet and not a DataFacet aheinecke@7298: // and so we are not registred with Mr. Blackboard aheinecke@7298: List providers = context.getDataProvider(name); aheinecke@7298: aheinecke@7298: double startingPoint = Double.MAX_VALUE; aheinecke@7298: aheinecke@7298: for (DataProvider dp: providers) { aheinecke@7298: SQFunction other = (SQFunction) dp.provideData( aheinecke@7298: name, aheinecke@7298: null, aheinecke@7298: context); aheinecke@7298: if (other == null) { aheinecke@7298: // name is not really unique here but it's our only key aheinecke@7298: // should not happen anyhow. aheinecke@7298: logger.error("Did not get data from: " + name); aheinecke@7298: continue; aheinecke@7298: } aheinecke@7298: startingPoint = Math.min(other.getMinQ(), startingPoint); aheinecke@7298: } aheinecke@7298: if (startingPoint == Double.MAX_VALUE) { aheinecke@7298: startingPoint = 0; aheinecke@7298: } aheinecke@7298: aheinecke@7298: SQFunction sqf = new SQFunction(function, startingPoint, qmax); rrenkert@5501: return sqf; rrenkert@5501: } rrenkert@5501: rrenkert@5501: @Override rrenkert@5501: public Facet deepCopy() { rrenkert@5501: StaticSQRelationFacet copy = rrenkert@5501: new StaticSQRelationFacet(index, name, description, relation); rrenkert@5501: copy.set(this); rrenkert@5501: return copy; rrenkert@5501: } rrenkert@5501: }