view artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveGenerator.java @ 6601:5ecc6d4d73f2

Add official fixings to Waterlevel CSV Export (issue1384) This searches the collection for staticwqkms artifacts that contain official data and adds that data to the export. The data is filtered by the calculation range and sorted by the calculation direction.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 18 Jul 2013 13:16:33 +0200
parents 6791411ed1fc
children aee8cb5c801a
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;

import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.StaticWKmsArtifact;
import org.dive4elements.river.artifacts.WINFOArtifact;
import org.dive4elements.river.artifacts.model.FacetTypes;
import org.dive4elements.river.artifacts.model.WKms;
import org.dive4elements.river.artifacts.model.WQKms;
import org.dive4elements.river.jfree.RiverAnnotation;
import org.dive4elements.river.jfree.StickyAxisAnnotation;
import org.dive4elements.river.jfree.StyledXYSeries;

import org.dive4elements.river.utils.RiverUtils;

import java.awt.Font;

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

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;


/**
 * An OutGenerator that generates discharge curves.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ComputedDischargeCurveGenerator
extends      DischargeCurveGenerator
implements   FacetTypes
{
    /** The logger used in this generator. */
    private static Logger logger =
        Logger.getLogger(ComputedDischargeCurveGenerator.class);

    public static final String I18N_CHART_TITLE =
        "chart.computed.discharge.curve.title";

    public static final String I18N_CHART_SUBTITLE =
        "chart.computed.discharge.curve.subtitle";

    public static final String I18N_YAXIS_LABEL =
        "chart.computed.discharge.curve.yaxis.label";

    public static final String I18N_CHART_TITLE_DEFAULT = "Abflusskurve";
    public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]";

    protected NumberAxis firstYAxis;

    /** Trivial Constructor. */
    public ComputedDischargeCurveGenerator () {
        super();
    }


    @Override
    protected String getDefaultChartTitle() {
        return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT);
    }


    @Override
    protected String getDefaultChartSubtitle() {
        double[] dist = getRange();

        Object[] args = new Object[] {
            getRiverName(),
            dist[0]
        };

        return msg(I18N_CHART_SUBTITLE, "", args);
    }



    @Override
    protected String getDefaultYAxisLabel(int pos) {
        D4EArtifact flys = (D4EArtifact) master;

        String unit = RiverUtils.getRiver(flys).getWstUnit().getName();
        if (pos == 0 && getCurrentGaugeDatum() != 0)
            unit = "cm";

        return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT, new Object[] { unit });
    }


    /**
     * Create Y (range) axis for given index, here with a special axis
     * that depends on other axis (does translation and scaling for
     * special case at gauge in cm).
     */
    @Override
    protected NumberAxis createYAxis(int index) {
        if (index == 0) {
            firstYAxis = super.createYAxis(0);
            return firstYAxis;
        }
        YAxisWalker walker = getYAxisWalker();

        Font labelFont = new Font(
            DEFAULT_FONT_NAME,
            Font.BOLD,
            getYAxisFontSize(index));

        SyncNumberAxis axis = new SyncNumberAxis(
            walker.getId(index),
            getYAxisLabel(index),
            firstYAxis);

        axis.setAutoRangeIncludesZero(false);
        axis.setLabelFont(labelFont);
        axis.setTickLabelFont(labelFont);
        axis.setShift((double)-getCurrentGaugeDatum());

        return axis;
    }


    /**
     * Process data, build up plot.
     */
    @Override
    public void doOut(
        ArtifactAndFacet artifactFacet,
        Document         attr,
        boolean          visible
    ) {
        String name = artifactFacet.getFacetName();

        logger.debug("ComputedDischargeCurveGenerator.doOut: " + name);

        if (name == null) {
            logger.warn("Broken facet in computed discharge out generation.");
            return;
        }

        //XXX DEAD CODE // Facet facet = artifactFacet.getFacet();

        if (name.equals(COMPUTED_DISCHARGE_Q)) {
            doDischargeQOut((WQKms) artifactFacet.getData(context), artifactFacet, attr, visible);
        }
        else if (name.equals(STATIC_WQ)) {
            doWQOut(artifactFacet.getData(context), artifactFacet, attr, visible);
        }
        else if (name.equals(STATIC_WQ_ANNOTATIONS)) {
            doWQAnnotations(
                artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (name.equals(COMPUTED_DISCHARGE_MAINVALUES_Q)
                || name.equals(MAINVALUES_Q)
                || name.equals(COMPUTED_DISCHARGE_MAINVALUES_W)
                || name.equals(MAINVALUES_W)
        ) {
            RiverAnnotation mainValues = (RiverAnnotation) artifactFacet.getData(context);
            translateRiverAnnotation(mainValues);
            doAnnotations(
                mainValues,
                artifactFacet, attr, visible);
        }
        else if (name.equals(STATIC_WKMS_INTERPOL) || name.equals(HEIGHTMARKS_POINTS)) {
            doWAnnotations(
                artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (name.equals(STATIC_WKMS)) {
            doWAnnotations(
                artifactFacet.getData(context),
                artifactFacet,
                attr,
                visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints(artifactFacet.getData(context),
                artifactFacet,
                attr, visible, YAXIS.W.idx);
        }
        else if (name.equals(DISCHARGE_CURVE)) {
            doDischargeOut(
                (WINFOArtifact) artifactFacet.getArtifact(),
                artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                attr,
                visible);
        }
        else {
            logger.warn("Unknown facet type for computed discharge: " + name);
            return;
        }
    }


    /**
     * Add WQ Data to plot.
     * @param wqkms data as double[][]
     */
    protected void doWQOut(
        Object           wqkms,
        ArtifactAndFacet aaf,
        Document         theme,
        boolean          visible
    ) {
        logger.debug("ComputedDischargeCurveGenerator: doWQOut");
        double [][] data = (double [][]) wqkms;

        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);
        StyledSeriesBuilder.addPoints(series, data, true);

        addAxisSeries(series, YAXIS.W.idx, visible);
    }


    /**
     * Add discharge Q-Series to plot, scale if at gauge.
     * @param wqkms actual data
     * @param theme theme to use.
     */
    protected void doDischargeQOut(
        WQKms            wqkms,
        ArtifactAndFacet aaf,
        Document         theme,
        boolean          visible
    ) {
        logger.debug("ComputedDischargeCurveGenerator: doDischargeQOut");
        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);

        double subtractPNP = getCurrentGaugeDatum();

        if (subtractPNP == 0d) {
            StyledSeriesBuilder.addPointsQW(series, wqkms);
            addAxisSeries(series, YAXIS.W.idx, visible);
        }
        else {
            XYSeries series2 = new StyledXYSeries(aaf.getFacetDescription(), theme);
            StyledSeriesBuilder.addPointsQW(series2, wqkms);
            addAxisSeries(series2, YAXIS.W.idx, false);

            // Use second axis...
            StyledSeriesBuilder.addPointsQW(series, wqkms, -subtractPNP, 100d);
            addAxisSeries(series, YAXIS.WCm.idx, visible);
        }
    }


    /**
     * Add W/Q-Series to plot.
     * @param wqkms actual data
     * @param theme theme to use.
     */
    protected void doQOut(
        WQKms            wqkms,
        ArtifactAndFacet aaf,
        Document         theme,
        boolean          visible
    ) {
        logger.debug("ComputedDischargeCurveGenerator: doQOut (add W/Q data).");
        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);
        StyledSeriesBuilder.addPointsQW(series, wqkms);

        addAxisSeries(series, YAXIS.W.idx, visible);
    }


    /**
     * Add WQ-Annotations to plot.
     * @param wqkms actual data
     * @param theme theme to use.
     */
    protected void doWQAnnotations(
        Object   wqkms,
        ArtifactAndFacet aandf,
        Document theme,
        boolean  visible
    ) {
        List<StickyAxisAnnotation> xy = new ArrayList<StickyAxisAnnotation>();
        double [][] data = (double [][]) wqkms;
        for (int i = 0; i< data[0].length; i++) {
            // TODO we need linear interpolation?
            xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(),
                (float) data[0][i], StickyAxisAnnotation.SimpleAxis.X_AXIS));
            xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(),
                (float) data[1][i], StickyAxisAnnotation.SimpleAxis.Y_AXIS));
        }

        doAnnotations(new RiverAnnotation(aandf.getFacetDescription(), xy),
            aandf, theme, visible);
    }


    /**
     * Add W-Annotations to plot.
     * @param wqkms actual data (double[][]).
     * @param theme theme to use.
     */
    protected void doWAnnotations(
        Object   wqkms,
        ArtifactAndFacet aandf,
        Document theme,
        boolean  visible
    ) {
        Facet facet = aandf.getFacet();

        List<StickyAxisAnnotation> xy = new ArrayList<StickyAxisAnnotation>();
        // Try to find them as WKms as well...
        if (wqkms instanceof double[][]) {
            logger.debug("its double[][] time, baby");
            double [][] data = (double [][]) wqkms;
            // TODO Do we need interpolation?
            for (int i = 0; i< data[0].length; i++) {
                xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(),
                    (float) data[1][i], StickyAxisAnnotation.SimpleAxis.Y_AXIS));
            }

            doAnnotations(new RiverAnnotation(facet.getDescription(), xy),
                aandf, theme, visible);
        }
        else {
            logger.debug("its wkms time, baby");
            WKms data = (WKms) wqkms;
            // Assume its WKms.
            // XXX DEAD CODE // double location = getRange()[0];
            double w = StaticWKmsArtifact.getWAtKmLin(data, getRange()[0]);
            xy.add(new StickyAxisAnnotation(aandf.getFacetDescription(),
              (float) w, StickyAxisAnnotation.SimpleAxis.Y_AXIS));

            doAnnotations(new RiverAnnotation(facet.getDescription(), xy),
                aandf, theme, visible);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org