view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RelativePointFacet.java @ 2757:6e89147f5ca3

Expose reference curve parameters via blackboard. flys-artifacts/trunk@4492 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 24 May 2012 04:38:49 +0000
parents 94c6f4ad9b98
children b05faaa9099b
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import java.util.List;
import java.awt.geom.Point2D;

import org.apache.log4j.Logger;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.DataProvider;

import de.intevation.flys.artifacts.StaticWKmsArtifact;
import de.intevation.flys.artifacts.math.Linear;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.RelativePointFacet;
import de.intevation.flys.artifacts.model.WQDay;

/**
 * Facet to access a point.
 */
public class RelativePointFacet
extends      BlackboardDataFacet
implements   FacetTypes {

    private static Logger logger = Logger.getLogger(RelativePointFacet.class);

    /** Trivial Constructor. */
    public RelativePointFacet(String description) {
        this(RELATIVE_POINT, description);
    }


    public RelativePointFacet(String name, String description) {
        this.name        = name;
        this.description = description;
        this.index       = 0;
    }


    /**
     * Returns the data this facet requires.
     *
     * @param artifact the owner artifact.
     * @param context  the CallContext (ignored).
     *
     * @return the data.
     */
    @Override
    public Object getData(Artifact artifact, CallContext context) {
        // Find out whether we live in a duration curve context, there we would
        // provide only a single point.
        Object wqdays = null;
        double km = 0d;
        List<DataProvider> providers = context.
            getDataProvider("durationcurve");
        if (providers.size() < 1) {
            logger.warn("Could not find durationcurve data provider.");
        }
        else {
            wqdays = providers.get(0).provideData(
                DurationCurveFacet.BB_DURATIONCURVE,
                null,
                context);
        }
        List<DataProvider> kmproviders = context.
            getDataProvider(DurationCurveFacet.BB_DURATIONCURVE_KM);
        if (kmproviders.size() < 1) {
            logger.warn("Could not find durationcurve.km data provider.");
        }
        else {
            logger.debug("Found durationcurve.km data provider.");
            String dckm = providers.get(0).provideData(
                DurationCurveFacet.BB_DURATIONCURVE_KM,
                null,
                context).toString();
            km = Double.valueOf(dckm);
        }

        StaticWKmsArtifact staticData =
            (StaticWKmsArtifact) artifact;

        if (wqdays != null) {
            // Which W at this km?
            double w = staticData.getWAtKmLin(staticData.getWKms(0), km);
            if (w == -1) {
                logger.warn("w is -1, already bad sign!");
            }
            // Where is this W passed by in the wq-curve?
            WQDay wqday = (WQDay) wqdays;
            // Doing a linear Day Of KM.
            int idx = 0;
            boolean wIncreases = wqday.getW(0) < wqday.getW(wqday.size()-1);
            if (wIncreases) {
                while (idx < wqday.size() && wqday.getW(idx) < w) {
                    idx++;
                }
            }
            else {
                idx = wqday.size() -1;
                while (idx > 0 && wqday.getW(idx) > w) {
                    idx--;
                }
            }

            double day = 0d;
            int mod = (wIncreases) ? -1 : +1;
            if (idx != 0 && idx <= wqday.size()-1) {
                day = Linear.linear(w, wqday.getW(idx +mod), wqday.getW(idx),
                    wqday.getDay(idx+mod), wqday.getDay(idx));
            }

            return new Point2D.Double((double) day, w);
        }
        logger.warn("not wqkms / w / day found");
        
        // TODO better signal failure.
        return new Point2D.Double(0d, 0d);
    }


    /**
     * Create a deep copy of this Facet.
     * @return a deep copy.
     */
    @Override
    public RelativePointFacet deepCopy() {
        RelativePointFacet copy = new RelativePointFacet(description);
        copy.set(this);
        return copy;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org