view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixWQCurveFacet.java @ 3062:7660cfe5e8f6

FixWQCurveGenerator can generate charts from Fix*-Facets (in theory and not complete) flys-artifacts/trunk@4642 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Tue, 12 Jun 2012 12:40:44 +0000
parents 0b5a7a2c3724
children d87aadaa4f7e
line wrap: on
line source
package de.intevation.flys.artifacts.model.fixings;

import org.apache.log4j.Logger;

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

import de.intevation.flys.utils.KMIndex;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.FixationArtifactAccess;

import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.Parameters;

import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
import de.intevation.flys.artifacts.math.fitting.Function;

import de.intevation.flys.artifacts.states.DefaultState.ComputeType;

/**
 * Facet to show the W|Q values.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class FixWQCurveFacet
extends      DataFacet
implements   FacetTypes {

    /** House logger. */
    private static Logger logger = Logger.getLogger(FixWQCurveFacet.class);

    /** Trivial Constructor. */
    public FixWQCurveFacet() {
    }


    /**
     * @param name
     */
    public FixWQCurveFacet(String description) {
        super(0, FIX_WQ_CURVE, description, ComputeType.ADVANCE, null, null);
    }


    /**
     * 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) {
        logger.debug("FixWQCurveFacet.getData");
        if (artifact instanceof FLYSArtifact) {
            FLYSArtifact flys = (FLYSArtifact)artifact;
            FixationArtifactAccess access = new FixationArtifactAccess(flys);

            CalculationResult res =
                (CalculationResult) flys.compute(context,
                                                 ComputeType.ADVANCE,
                                                 false);

            FixResult result = (FixResult) res.getData();

            double km = access.getCurrentKm();
            logger.debug("FixWQCurveFacet.getData: km = " + km);

            String function = access.getFunction();
            Function ff = FunctionFactory.getInstance().getFunction(function);

            Parameters params = result.getParameters();
            String[] columnNames = params.getColumnNames();
            for(String columnName : columnNames) {
                logger.debug("FixWQCurveFacet.getData: columnName = '" + columnName + "'");
            }
            int row = params.binarySearch("km", km /*, Math.pow(10, -4)*/); //FIXME Use epsilon as soon access.getCurrentKm() is fixed
            if(row < 0) {
                row = -row - 1;
                logger.debug("getData: no direct hit in params.binarySearch");
            }
            String[] paramNames = ff.getParameterNames();
            int[] paramInd = params.columnIndices(paramNames);
            double[] coeffs = new double[paramNames.length];
            params.get(row, paramInd, coeffs);

            de.intevation.flys.artifacts.math.Function mf =
                ff.instantiate(coeffs);

            double maxQ = getMaxQ(result, km);
            logger.debug("getData: maxQ = " + maxQ);

            FixFunction fix = new FixFunction(
                ff.getName(),
                ff.getDescription(),
                mf,
                maxQ);

            return fix;
        }
        else {
            logger.debug("Not an instance of FixationArtifact.");
            return null;
        }
    }


    protected double getMaxQ(FixResult result, double km) {
        double maxQ = 0;

        KMIndex<QW []> kmQWRef = result.getReferenced();

        KMIndex.Entry<QW []> qwEntry = kmQWRef.binarySearch(km);
        if(qwEntry != null) {
            QW[] qwRef = qwEntry.getValue();
            if (qwRef != null) {
                for (int i = 0; i < qwRef.length; i++) {
                    if (qwRef[i].getQ() > maxQ) {
                        maxQ = qwRef[i].getQ();
                    }
                }
            }
        } else {
            logger.debug("getMaxQ: qwEntry is null, that's odd...");
        }

        KMIndex<AnalysisPeriod []> kmQWDAna = result.getAnalysisPeriods();
        KMIndex.Entry<AnalysisPeriod []> periodsEntry = kmQWDAna.binarySearch(km);

        if(periodsEntry != null) {
            AnalysisPeriod[] periods = kmQWDAna.binarySearch(km).getValue();
            if(periods != null) {
                for (int i = 0; i < periods.length; i++) {
                    QWD[] qwdAna = periods[i].getQWDs();
                    if (qwdAna != null) {
                        for (int j = 0; j < qwdAna.length; j++) {
                            if (qwdAna[j].getQ() > maxQ) {
                                maxQ = qwdAna[j].getQ();
                            }
                        }
                    }
                }
            }
        } else {
            logger.debug("getMaxQ: periodsEntry is null, that's odd...");
        }
        //return maxQ;
        return 1000; //FIXME replace by parameters.interpolate("q_max")...
    }

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

http://dive4elements.wald.intevation.org