view artifacts/src/main/java/org/dive4elements/river/exports/ComputedDischargeCurveGenerator.java @ 7691:fa4fbd66e752

(issue1579) Fix axes syncronisation at Gauges The SyncNumberAxis was completely broken. It only synced in one direction and even that did not work correctly when data was added to the axis (and the syncAxis rescaled but forgot the old axis) then there were lots of ways to bypass that scaling. And i also think the trans calculation was wrong. It has been replaced by a "mostly" simple method to just keep the W in M and W in CM+Datum axes in sync. I say "Mostly" because it had to deal with the Bounds interface.
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 13 Dec 2013 19:03:00 +0100
parents b64c263df4d1
children 75ef6963f1c9
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.exports.process.DischargeProcessor;
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.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.data.xy.XYSeries;

import org.apache.log4j.Logger;


/**
 * An OutGenerator that generates discharge curves, also at locations
 * not at a gauge.
 *
 * @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]";

    /** 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 });
    }

    /**
     * Process data, build up plot.
     */
    @Override
    public void doOut(
        ArtifactAndFacet artifactFacet,
        ThemeDocument    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;
        }

        DischargeProcessor dProcessor = new DischargeProcessor(getRange()[0]);
        if (dProcessor.canHandle(name)) {
            dProcessor.doOut(this, artifactFacet, attr, visible, YAXIS.W.idx);
        }
        else 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 (STATIC_WKMS_INTERPOL.equals(name) ||
            HEIGHTMARKS_POINTS.equals(name) ||
            STATIC_WQKMS_W.equals(name)) {
            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 wq data as double[][]
     */
    protected void doWQOut(
        Object           wq,
        ArtifactAndFacet aaf,
        ThemeDocument    theme,
        boolean          visible
    ) {
        logger.debug("ComputedDischargeCurveGenerator: doWQOut");
        XYSeries series = new StyledXYSeries(aaf.getFacetDescription(), theme);
        if (wq instanceof double[][]) {
            double [][] data = (double [][]) wq;
            StyledSeriesBuilder.addPoints(series, data, true);
        } else if (wq instanceof WQKms) {
            WQKms wqkms = (WQKms) wq;
            StyledSeriesBuilder.addPointsQW(series, (WQKms) wq);
        }

        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,
        ThemeDocument    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,
        ThemeDocument    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,
        ThemeDocument 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,
        ThemeDocument 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