view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RelativePointFacet.java @ 4711:35dd03e04e38

Added new parameter SKIP_DA66S to importer.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 19 Dec 2012 13:59:57 +0100
parents cd5eb8f5f6f1
children cce12c06466f
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;

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

    /** Own logger. */
    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;
    }


    protected Point2D calculateDurationCurvePoint(CallContext context,
        StaticWKmsArtifact artifact)
    {
        // TODO here and in reference curve calc: Do warn if more than 1
        // provider found or (way better) handle it.
        Object wqdays = null;
        double km     = 0d;
        List<DataProvider> providers = context.
            getDataProvider(DurationCurveFacet.BB_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);
        }

        if (wqdays != null) {
            // Which W at this km?
            double w = artifact.getWAtKmLin(artifact.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);
    }


    /**
     * Calculate a reference curve point, that is, a point made of
     * the Ws from start and end km param of the reference curve.
     */
    public Point2D calculateReferenceCurvePoint(CallContext context,
        StaticWKmsArtifact artifact) {
        List<DataProvider> providers = context.
            getDataProvider(ReferenceCurveFacet.BB_REFERENCECURVE_STARTKM);
        if (providers.size() < 1) {
            logger.warn("Could not find reference curve startkm data provider.");
        }

        Double start = (Double) providers.get(0).
            provideData(ReferenceCurveFacet.BB_REFERENCECURVE_STARTKM, null, context);

        providers = context.
            getDataProvider(ReferenceCurveFacet.BB_REFERENCECURVE_ENDKMS);
        if (providers.size() < 1) {
            logger.warn("Could not find reference curve endkms data provider.");
        }
        double[] ends = (double[]) providers.get(0).
            provideData(ReferenceCurveFacet.BB_REFERENCECURVE_ENDKMS, null, context);

        logger.debug("Got s " + start + " e " + ends);
        double startW = artifact.getWAtKmLin(artifact.getWKms(0), start.doubleValue());
        // TODO handle multiple ends.
        double endW = artifact.getWAtKmLin(artifact.getWKms(0), ends[0]);
        logger.debug("Gotw s " + startW + " e " + endW);
        return new Point2D.Double(startW, endW);
    }


    /**
     * 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) {
        StaticWKmsArtifact staticData = (StaticWKmsArtifact) artifact;
        // Find out whether we live in a duration curve context, there we would
        // provide only a single point.

        if (context.getDataProvider(
            DurationCurveFacet.BB_DURATIONCURVE_KM).size() > 0) {
            return calculateDurationCurvePoint(context, staticData);
        }
        else if (context.getDataProvider(
            ReferenceCurveFacet.BB_REFERENCECURVE_STARTKM).size() > 0) {
            return calculateReferenceCurvePoint(context, staticData);
        }

        // 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