view artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixLongitudinalSectionGenerator.java @ 7076:7f600001c807 generator-refactoring

Add LTR inversion code to diagram generator. This code is used in serveral diagrams and as it modifies a whole diagram it should be central. (This should also make maintenance easier). This function can be called by processors to make sure that their data is plotted with an LTR waterflow.
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 20 Sep 2013 16:33:22 +0200
parents e0311aa32efb
children 3c4efd4b2c19
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.fixings;

import java.awt.BasicStroke;
import java.awt.Color;

import org.apache.log4j.Logger;
import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.river.artifacts.D4EArtifact;
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.artifacts.model.fixings.AnalysisPeriod;
import org.dive4elements.river.artifacts.model.fixings.QWD;
import org.dive4elements.river.exports.ChartGenerator;
import org.dive4elements.river.exports.StyledSeriesBuilder;
import org.dive4elements.river.exports.process.KMIndexProcessor;
import org.dive4elements.river.exports.process.Processor;
import org.dive4elements.river.exports.process.WOutProcessor;
import org.dive4elements.river.jfree.RiverAnnotation;
import org.dive4elements.river.jfree.StyledAreaSeriesCollection;
import org.dive4elements.river.jfree.StyledXYSeries;
import org.dive4elements.river.themes.ThemeDocument;
import org.dive4elements.river.utils.DataUtil;
import org.dive4elements.river.utils.KMIndex;
import org.dive4elements.river.utils.RiverUtils;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.data.xy.XYSeries;

public class FixLongitudinalSectionGenerator
extends FixChartGenerator
implements FacetTypes
{
    private static Logger logger =
            Logger.getLogger(FixLongitudinalSectionGenerator.class);

    public static final String I18N_CHART_TITLE =
            "chart.fixings.longitudinalsection.title";

    public static final String I18N_CHART_SUBTITLE =
            "chart.fixings.longitudinalsection.subtitle";

    public static final String I18N_XAXIS_LABEL =
            "chart.fixings.longitudinalsection.xaxis.label";

    public static final String I18N_CHART_TITLE_DEFAULT  =
            "Fixierungsanalyse";

    public static final String I18N_XAXIS_LABEL_DEFAULT  =
            "[km]";

    public static final String I18N_DW_YAXIS_LABEL_DEFAULT  =
            "delta W [cm]";

    public static final String I18N_DW_YAXIS_LABEL =
            "chart.fixings.longitudinalsection.yaxis.label";

    public static final String I18N_W_YAXIS_LABEL =
        "chart.longitudinal.section.yaxis.label";

    public static final String I18N_Q_YAXIS_LABEL =
        "chart.longitudinal.section.yaxis.second.label";

    public static final String I18N_W_YAXIS_LABEL_DEFAULT  = "W [NN + m]";
    public static final String I18N_Q_YAXIS_LABEL_DEFAULT = "Q [m\u00b3/s]";

    public static enum YAXIS {
        dW(0), W(1), Q(2);
        public int idx;
        private YAXIS(int c) {
            idx = c;
        }
    }

    @Override
    public void doOut(ArtifactAndFacet aaf, ThemeDocument doc, boolean visible) {
        String name = aaf.getFacetName();
        logger.debug("FixLongitudinalSectionGenerator: doOut: " + name);

        Processor processor = new KMIndexProcessor();
        Processor wProcessor = new WOutProcessor();
        if (name.contains(FIX_SECTOR_AVERAGE_LS_DEVIATION)) {
            doSectorAverageDeviationOut(aaf, doc, visible);
        }
        else if (processor.canHandle(name)) {
            processor.doOut(this, aaf, doc, visible, YAXIS.dW.idx);
        }
        else if (wProcessor.canHandle(name)) {
            wProcessor.doOut(this, aaf, doc, visible, YAXIS.W.idx);
        }
        else if (name.equals(STATIC_WQKMS_Q)) {
            doQOut(
                (WQKms) aaf.getData(context),
                aaf,
                doc,
                visible);
        }
        else if (name.equals(FIX_DEVIATION_LS)) {
            doReferenceDeviationOut(aaf, doc, visible);
        }
        else if (name.equals(LONGITUDINAL_ANNOTATION)) {
            doAnnotations(
                    (RiverAnnotation) aaf.getData(context),
                    aaf,
                    doc,
                    visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints (aaf.getData(context),
                    aaf,
                    doc, visible, YAXIS.dW.idx);
        }
        else {
            logger.warn("Unknown facet name " + name);
        }
    }

    /**
     * Process the output for Q facets in a longitudinal section curve.
     *
     * @param wqkms An array of WQKms values.
     * @param aandf The facet and artifact. This facet does NOT support any data objects. Use
     * D4EArtifact.getNativeFacet() instead to retrieve a Facet which supports
     * data.
     * @param theme The theme that contains styling information.
     * @param visible The visibility of the curve.
     */
    protected void doQOut(
        WQKms    wqkms,
        ArtifactAndFacet aandf,
        ThemeDocument theme,
        boolean  visible
    ) {
        logger.debug("LongitudinalSectionGenerator.doQOut");

        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);

        StyledSeriesBuilder.addStepPointsKmQ(series, wqkms);

        addAxisSeries(series, YAXIS.Q.idx, visible);

        if (needInvertAxis(wqkms)) {
            setInverted(true);
        }
    }

    /**
     * This method determines - taking JFreeCharts auto x value ordering into
     * account - if the x axis need to be inverted. Waterlines in these charts
     * should decrease.
     *
     * @param wkms The data object that stores the x and y values used for this
     * chart.
     */
    public boolean needInvertAxis(WKms wkms) {
        boolean wsUp = wkms.guessWaterIncreasing();
        boolean kmUp = DataUtil.guessWaterIncreasing(wkms.allKms());
        boolean inv = (wsUp && kmUp) || (!wsUp && !kmUp);

        int size = wkms.size();

        if (logger.isDebugEnabled()) {
            logger.debug("(Wkms)Values  : " + size);
            if (size > 0) {
                logger.debug("Start km: " + wkms.getKm(0));
                logger.debug("End   km: " + wkms.getKm(size-1));
            }
            logger.debug("wsUp: " + wsUp);
            logger.debug("kmUp: " + kmUp);
            logger.debug("inv:  " + inv);
        }

        return inv;
    }

    @SuppressWarnings("unchecked")
    protected void doSectorAverageDeviationOut(
            ArtifactAndFacet aaf,
            ThemeDocument doc,
            boolean visible)
    {
        logger.debug("doSectorAverageOut" + aaf.getFacet().getIndex());

        int index = aaf.getFacet().getIndex();
        int sectorNdx = index & 3;

        KMIndex<AnalysisPeriod> kms =
                (KMIndex<AnalysisPeriod>)aaf.getData(context);

        if(kms == null) {
            return;
        }

        StyledAreaSeriesCollection area = new StyledAreaSeriesCollection(doc);
        XYSeries upper =
                new StyledXYSeries(aaf.getFacetDescription(), false, doc);
        XYSeries lower =
                new StyledXYSeries(aaf.getFacetDescription() + " ", false, doc);

        for (KMIndex.Entry<AnalysisPeriod> entry: kms) {
            double km = entry.getKm();
            AnalysisPeriod ap = entry.getValue();
            QWD qwd = ap.getQSectorAverages()[sectorNdx];
            double dev = ap.getQSectorStdDev(sectorNdx);
            logger.debug("std-dev: " + dev);
            if (qwd == null) {
                continue;
            }
            double deltaW = qwd.getDeltaW();
            double up = deltaW + dev;
            double lo = deltaW - dev;
            upper.add(km, up);
            lower.add(km, lo);
        }
        area.addSeries(upper);
        area.addSeries(lower);

        addAreaSeries(area, 0, visible);
    }


    @SuppressWarnings("unchecked")
    protected void doReferenceDeviationOut(
            ArtifactAndFacet aaf,
            ThemeDocument doc,
            boolean visible)
    {
        logger.debug("doReferenceOut");

        KMIndex<double[]> kms =
                (KMIndex<double[]>)aaf.getData(context);

        if(kms == null) {
            return;
        }

        StyledAreaSeriesCollection area = new StyledAreaSeriesCollection(doc);
        XYSeries upper =
                new StyledXYSeries(aaf.getFacetDescription(), false, doc);
        XYSeries lower =
                new StyledXYSeries(aaf.getFacetDescription() + " ", false, doc);


        for (KMIndex.Entry<double[]> entry: kms) {
            double km = entry.getKm();
            double[] devArray = entry.getValue();
            if (devArray == null) {
                continue;
            }
            double dev = devArray[0];
            double up = dev;
            double lo = -dev;
            upper.add(km, up, false);
            lower.add(km, lo, false);
        }
        area.addSeries(upper);
        area.addSeries(lower);

        Marker marker = new ValueMarker(0);
        marker.setStroke(new BasicStroke(2));
        marker.setPaint(Color.BLACK);
        addValueMarker(marker);
        addAreaSeries(area, 0, visible);
    }

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

    @Override
    protected String getDefaultXAxisLabel() {
        return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT);
    }

    @Override
    protected String getDefaultYAxisLabel(int pos) {
        if (pos == YAXIS.dW.idx) {
            return msg(I18N_DW_YAXIS_LABEL, I18N_DW_YAXIS_LABEL_DEFAULT);
        }
        else if (pos == YAXIS.W.idx) {
            D4EArtifact flys = (D4EArtifact) master;
            String unit = RiverUtils.getRiver(flys).getWstUnit().getName();
            return msg(
                I18N_W_YAXIS_LABEL,
                I18N_W_YAXIS_LABEL_DEFAULT,
                new Object[] { unit });
        }
        else if (pos == YAXIS.Q.idx) {
            return msg(I18N_Q_YAXIS_LABEL, I18N_Q_YAXIS_LABEL_DEFAULT);
        }
        return "";
    }

    @Override
    protected ChartGenerator.YAxisWalker getYAxisWalker() {
        return new YAxisWalker() {
            @Override
            public int length() {
                return YAXIS.values().length;
            }

            @Override
            public String getId(int idx) {
                YAXIS[] yaxes = YAXIS.values();
                return yaxes[idx].toString();
            }
        };
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org