view artifacts/src/main/java/org/dive4elements/river/exports/process/DischargeProcessor.java @ 8098:09725b65955a

Add new and simplyfied SedimentLoadFacet The SedimentLoadFacet is intended to work with the Measurement stations. It uses the same mechanismn to access the Mesurement station values as the calculation does. SedimentLoadLS values need a different facet that will come soon.
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 15 Aug 2014 18:27:19 +0200
parents 67e87c2d3748
children 0b4cd7a5f079
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.exports.process;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jfree.data.xy.XYSeries;

import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.model.FacetTypes;
import org.dive4elements.river.artifacts.model.WQKms;
import org.dive4elements.river.exports.DischargeCurveGenerator;
import org.dive4elements.river.exports.XYChartGenerator;
import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation;
import org.dive4elements.river.jfree.RiverAnnotation;
import org.dive4elements.river.jfree.StickyAxisAnnotation;
import org.dive4elements.river.jfree.StyledXYSeries;
import org.dive4elements.river.themes.ThemeDocument;

import org.jfree.chart.annotations.XYTextAnnotation;


/** Helper for data handling in discharge diagrams. */
public class DischargeProcessor
extends DefaultProcessor implements FacetTypes {

    private final static Logger logger =
            Logger.getLogger(DischargeProcessor.class);

    /** Station for which the diagram is shown. */
    private double km;

    /** Tolerance for comparison of kilometers. */
    public static final double KM_EPSILON = 0.001d;


    /** This processor needs to be constructed with a given km. */
    private DischargeProcessor() {
        km = Double.NaN;
    }


    public DischargeProcessor(double km) {
        this.km = km;
    }


    /** Process data, add it to plot. */
    @Override
    public void doOut(
            XYChartGenerator generator,
            ArtifactAndFacet bundle,
            ThemeDocument theme,
            boolean visible,
            int axisIndex
    ) {
        CallContext context = generator.getCallContext();
        Object data = bundle.getData(context);
        if (data instanceof WQKms) {
            doWQKmsPointOut(
                generator, (WQKms) data, bundle, theme, visible, axisIndex);
            return;
        }
        else if (data instanceof RiverAnnotation) {
            doRiverAnnotationOut(
                generator, (RiverAnnotation) data, bundle, theme, visible);
            return;
        }
        else if (data instanceof double[][]) {
            doMarksOut(
                generator, (double[][]) data, bundle, theme, visible);
            return;
        }
        else {
            logger.error("Can't process "
                + data.getClass().getName() + " objects of facet "
                + bundle.getFacetName());
        }
    }


    /** True if this processor knows how to deal with facetType. */
    @Override
    public boolean canHandle(String facetType) {
        return STATIC_WQKMS_W.equals(facetType)
            || COMPUTED_DISCHARGE_MAINVALUES_Q.equals(facetType)
            || MAINVALUES_Q.equals(facetType)
            || COMPUTED_DISCHARGE_MAINVALUES_W.equals(facetType)
            || MAINVALUES_W.equals(facetType)
            || STATIC_W_INTERPOL.equals(facetType);
    }


    /** The station of the current calculation/view. */
    protected double getKm() {
        return km;
    }


    /** Handle WQKms data by finding w/q values at given km. */
    protected void doWQKmsPointOut(XYChartGenerator generator,
        WQKms wqkms,
        ArtifactAndFacet bundle,
        ThemeDocument theme,
        boolean visible,
        int axidx
    ) {
        logger.debug("doWQKmsPointOut");
        String title = bundle.getFacetDescription();
        XYSeries series = new StyledXYSeries(
            title,
            theme);

        double[] kms = wqkms.getKms();

        for (int i = 0 ; i< kms.length; i++) {
            if (Math.abs(kms[i] - getKm()) <= KM_EPSILON) {
                series.add(wqkms.getQ(i), wqkms.getW(i));
                generator.addAxisSeries(series, axidx, visible);
                if(visible && theme.parseShowPointLabel()) {
                    List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>();
                    XYTextAnnotation anno = new CollisionFreeXYTextAnnotation(
                            title,
                            wqkms.getQ(i),
                            // TODO add a percentage to the extend of W axis
                            wqkms.getW(i));
                    textAnnos.add(anno);
                    RiverAnnotation flysAnno = new RiverAnnotation(null, null, null, theme);
                    flysAnno.setTextAnnotations(textAnnos);
                    generator.addAnnotations(flysAnno);
                }
                return;
            }
        }

        logger.warn("No WQ found for km " + getKm());
    }

    protected void doRiverAnnotationOut(XYChartGenerator generator,
        RiverAnnotation annotations,
        ArtifactAndFacet bundle,
        ThemeDocument theme,
        boolean visible
    ) {
        if (!(generator instanceof DischargeCurveGenerator)) {
            logger.error("DischargeProcessor can only be used in " +
                " in DischargeCurveGenerator-classes.");
            return;
        }
        logger.debug("doRiverAnnotationOut");
        DischargeCurveGenerator dGenerator =
            (DischargeCurveGenerator) generator;

        dGenerator.translateRiverAnnotation(annotations);
        dGenerator.doAnnotations(
            annotations,
            bundle, theme, visible);
    }


    /**
     * Put Sticky Axis Markers to Y-axis for each value.
     * @param data [[-,y1],[-,y2],...] ('x'-coordinates ignored)
     */
    protected void doMarksOut(XYChartGenerator generator,
        double[][] data,
        ArtifactAndFacet bundle,
        ThemeDocument theme,
        boolean visible
    ) {
        logger.debug("doMarksOut");

        if (!visible) {
            return;
        }

        // TODO subtract gauge null point if at gauge.
        String title = bundle.getFacetDescription();
        List<StickyAxisAnnotation> yMarks = new ArrayList<StickyAxisAnnotation>();

        for (double yPos: data[1]) {
            yMarks.add(new StickyAxisAnnotation(
                title,
                (float) yPos,
                StickyAxisAnnotation.SimpleAxis.Y_AXIS));
        }

        generator.doAnnotations(new RiverAnnotation(title, yMarks),
            bundle, theme, visible);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org