view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java @ 5766:a6dc4c232da2

Datacage config and out generator for morphologic width.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 18 Apr 2013 14:17:10 +0200
parents 3c7e7d47939f
children
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.math.Linear;
import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
import de.intevation.flys.artifacts.model.extreme.ExtremeResult;

import org.apache.log4j.Logger;

/**
 * Facet of a Waterlevel (WQKms).
 */
public class WaterlevelFacet extends DataFacet {

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

    public WaterlevelFacet(int index, String name, String description) {
        super(index, name, description, ComputeType.ADVANCE, null, null);
    }

    public WaterlevelFacet(
        int         index,
        String      name,
        String      description,
        ComputeType type,
        String      stateID,
        String      hash
    ) {
        super(index, name, description, type, hash, stateID);
    }

    public WaterlevelFacet() {
    }

    protected WQKms [] getWQKms(CalculationResult res) {
        if (res.getData() instanceof ExtremeResult)
            return ((ExtremeResult) res.getData()).getWQKms();
        else if (res.getData() instanceof WQKms[]) {
            return (WQKms []) res.getData();
        }
        else {
            logger.error("WaterlevelFacet got wrong data type " + res.getData());
            return null;
        }
    }

    /**
     * Get waterlevel data.
     * @return a WQKms at given index.
     */
    @Override
    public Object getData(Artifact artifact, CallContext context) {

        if (logger.isDebugEnabled()) {
            logger.debug("Get data for waterlevels at index: " + index +
                " /stateId: " + stateId);
        }

        if (artifact == null) {
            logger.error("WaterlevelFacet.getData: artifact is null");
            return null;
        }

        FLYSArtifact winfo = (FLYSArtifact) artifact;

        CalculationResult res = (CalculationResult)
            winfo.compute(context, hash, stateId, type, false);

        if (res == null) {
            logger.error("WaterlevelFacet.getData: null result");
            return null;
        }

        WQKms [] wqkms = getWQKms(res);
        Object KM = context.getContextValue("currentKm");

        // Interpolation.
        if (KM != null) {
            linearInterpolate(wqkms[index], (Double) KM);
        }
        else {
            logger.debug("Do not interpolate.");
        }

        return wqkms != null ? wqkms[index] : null;
    }


    /**
     * Linear interpolation of WQKms.
     * TODO rewrite.
     * @return [w, q, km]
     */
    public WQKms linearInterpolate(WQKms wqkms, double km) {
        logger.debug("interpolate at given km (" + km + ")");

        WQKms resultWQKms = new WQKms();
        int size = wqkms.size();
        boolean kmIncreasing = wqkms.getKm(0) < wqkms.getKm(size-1);
        int mod = kmIncreasing ? +1 : -1;
        int idx = 0;
        // Move idx to closest from one direction, check for match.
        if (!kmIncreasing) {
            while (idx < size && wqkms.getKm(idx) < km) {
                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
                    return resultWQKms;
                }
                idx++;
            }
        }
        else {
            idx = size-1;
            while (idx > 0 && wqkms.getKm(idx) > km) {
                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
                    return resultWQKms;
                }
                idx--;
            }
        }
        if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
            resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
            return resultWQKms;
        }

        if ((idx != -1) && (idx < size) && (idx - mod != -1) && (idx - mod < size)) {
            double inW = Linear.linear(
                km,
                wqkms.getKm(idx), wqkms.getKm(idx - mod),
                wqkms.getW(idx), wqkms.getW(idx - mod));
            double inQ = Linear.linear(
                km,
                wqkms.getKm(idx), wqkms.getKm(idx - mod),
                wqkms.getQ(idx), wqkms.getQ(idx - mod));
            resultWQKms.add(inW, inQ, km);
        }
        else {
            logger.debug("waterlevelfacet stuff " + idx + " size " + size + " mod: " + mod);
        }

        return resultWQKms;
    }


    /** Copy deeply. */
    @Override
    public Facet deepCopy() {
        WaterlevelFacet copy = new WaterlevelFacet();
        copy.set(this);
        copy.type    = type;
        copy.hash    = hash;
        copy.stateId = stateId;
        return copy;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org