view flys-artifacts/src/main/java/de/intevation/flys/exports/HistoricalDischargeCurveGenerator.java @ 3785:a5f65e8983be

Merged revisions 5501-5502,5504-5508,5511-5513,5516-5519 via svnmerge from file:///home/clients/bsh/bsh-generischer-viewer/Material/SVN/flys-artifacts/trunk ........ r5501 | felix | 2012-09-18 11:49:45 +0200 (Di, 18 Sep 2012) | 1 line fix issue865 - missing showarea theme prop. ........ r5502 | clins | 2012-09-18 12:18:30 +0200 (Di, 18 Sep 2012) | 1 line Add robustness checks to prevent NPEs ........ r5504 | felix | 2012-09-18 14:03:15 +0200 (Di, 18 Sep 2012) | 1 line i18n for area label (fix issue487). ........ r5505 | clins | 2012-09-18 16:19:59 +0200 (Di, 18 Sep 2012) | 1 line Update themes to show point descriptions ........ r5506 | rrenkert | 2012-09-18 17:00:30 +0200 (Di, 18 Sep 2012) | 3 lines Removed incorrect characteristic diameter. ........ r5507 | rrenkert | 2012-09-18 17:03:20 +0200 (Di, 18 Sep 2012) | 3 lines Fixed some stupid bugs in bed quality data factory and calculation. ........ r5508 | teichmann | 2012-09-18 17:45:49 +0200 (Di, 18 Sep 2012) | 1 line The usual whitespace and import cleanups. ........ r5511 | teichmann | 2012-09-18 18:24:51 +0200 (Di, 18 Sep 2012) | 1 line Use generics aware Collections.emptyList(). ........ r5512 | teichmann | 2012-09-18 20:36:52 +0200 (Di, 18 Sep 2012) | 1 line Some more little steps towards "Auslagerung extremer Wasserspiegellagen". ........ r5513 | clins | 2012-09-18 23:38:19 +0200 (Di, 18 Sep 2012) | 1 line A and B facets of fix analyis are now deactivated by default ........ r5516 | bricks | 2012-09-19 10:45:51 +0200 (Mi, 19 Sep 2012) | 2 lines Add the gauge station to the GaugeOverviewInfoService xml response ........ r5517 | rrenkert | 2012-09-19 10:50:23 +0200 (Mi, 19 Sep 2012) | 3 lines Added CSV export to bed quality calculation. ........ r5518 | bricks | 2012-09-19 11:04:04 +0200 (Mi, 19 Sep 2012) | 2 lines Fix date in changelog entry ........ r5519 | teichmann | 2012-09-19 11:17:14 +0200 (Mi, 19 Sep 2012) | 1 line Removed trailing whitespace. ........ flys-artifacts/tags/2.9.1@5531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 19 Sep 2012 14:58:31 +0000
parents 97ad960f5579
children 5ff3b2f5fb1c
line wrap: on
line source
package de.intevation.flys.exports;

import de.intevation.artifactdatabase.state.ArtifactAndFacet;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.HistoricalWQTimerange;
import de.intevation.flys.artifacts.model.Timerange;
import de.intevation.flys.artifacts.model.WQTimerange;
import de.intevation.flys.jfree.StyledTimeSeries;
import de.intevation.flys.utils.FLYSUtils;

import java.util.Date;

import org.apache.log4j.Logger;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Day;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.w3c.dom.Document;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class HistoricalDischargeCurveGenerator
extends      TimeseriesChartGenerator
implements   FacetTypes
{
    private static Logger logger =
        Logger.getLogger(HistoricalDischargeCurveGenerator.class);


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

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

    public static final String I18N_XAXIS_LABEL =
        "chart.historical.discharge.xaxis.label";

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

    public static final String I18N_YAXIS_SECOND_LABEL =
        "chart.historical.discharge.yaxis.second.label";


    public static enum YAXIS {
        Q(0);
        protected int idx;
        private YAXIS(int c) {
            idx = c;
        }
    }


    @Override
    protected 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();
            }
        };
    }


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


    @Override
    protected String getDefaultChartSubtitle() {
        String[] args = new String[] {
            FLYSUtils.getReferenceGaugeName((FLYSArtifact) master)
        };

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


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

    @Override
    protected String getDefaultYAxisLabel(int pos) {
        if (pos == 0) {
            return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL);
        }
        else if (pos == 1) {
            return msg(I18N_YAXIS_SECOND_LABEL, I18N_YAXIS_SECOND_LABEL);
        }
        else {
            return "NO TITLE FOR Y AXIS: " + pos;
        }
    }


    @Override
    public void doOut(
        ArtifactAndFacet artifactFacet,
        Document         theme,
        boolean          visible
    ) {
        String name = artifactFacet.getFacetName();
        logger.debug("HistoricalDischargeCurveGenerator.doOut: " + name);
        logger.debug("Theme description is: " + artifactFacet.getFacetDescription());


        if (name.equals(HISTORICAL_DISCHARGE_Q)) {
            doHistoricalDischargeOut(
                (FLYSArtifact) artifactFacet.getArtifact(),
                artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                theme,
                visible);
        }
        else if (name.equals(HISTORICAL_DISCHARGE_Q_DIFF)) {
            doHistoricalDischargeDifferenceOut(
                (FLYSArtifact) artifactFacet.getArtifact(),
                artifactFacet.getData(context),
                artifactFacet.getFacetDescription(),
                theme,
                visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints (artifactFacet.getData(context),
                      artifactFacet,
                      theme, visible, YAXIS.Q.idx);
        }
        // TODO ADD THE CASE FOR DISPLAYING W VALUES
        else {
           logger.warn("doOut(): unknown facet name: " + name);
           return;
        }
    }


    protected void doHistoricalDischargeOut(
        FLYSArtifact artifact,
        Object       data,
        String       desc,
        Document     theme,
        boolean      visible)
    {
        logger.debug("doHistoricalDischargeOut(): description = " + desc);

        WQTimerange wqt = (WQTimerange) data;

        TimeSeriesCollection tsc = newTimeSeriesCollection(
            wqt.getTimeranges(),
            wqt.getQs(),
            theme,
            desc);

        addAxisDataset(tsc, 0, visible);
    }


    protected void doHistoricalDischargeDifferenceOut(
        FLYSArtifact artifact,
        Object       data,
        String       desc,
        Document     theme,
        boolean      visible
    ) {
        logger.debug("doHistoricalDischargeDifferenceOut: desc = " + desc);

        HistoricalWQTimerange wqt = (HistoricalWQTimerange) data;

        TimeSeriesCollection tsc = newTimeSeriesCollection(
            wqt.getTimeranges(),
            wqt.getDiffs(),
            theme,
            desc);

        addAxisDataset(tsc, 0, visible);
    }


    /**
     * Creates a new TimeSeriesCollection with a single TimeSeries. The
     * TimeSeries will consist of two RegularTimePeriods for each W/Q value
     * provided by <i>wqt</i>. This has the effect, that the line in the chart
     * looks like a "step chart".
     */
    protected TimeSeriesCollection newTimeSeriesCollection(
        Timerange[] timeranges,
        double[]    values,
        Document    theme,
        String      desc
    ) {
        logger.debug("Create new TimeSeriesCollection for: " + desc);

        TimeSeriesCollection tsc = new TimeSeriesCollection();
        TimeSeries        series = new StyledTimeSeries(desc, theme);

        for (int i = 0, n = timeranges.length; i < n; i++) {
            RegularTimePeriod[] rtp = newRegularTimePeriod(timeranges[i]);

            try {
                if (Double.isNaN(values[i])) {
                    logger.warn("Skip TimePeriod because value is NaN.");
                    continue;
                }

                series.add(rtp[0], values[i]);
                series.add(rtp[1], values[i]);

                if (logger.isDebugEnabled()) {
                    logger.debug("added Item to TimeSeries:");
                    logger.debug("   TimePeriod: " + rtp[0] + " - " + rtp[1]);
                    logger.debug("   Value:      " + values[i]);
                }
            }
            catch (SeriesException se) {
                logger.warn("Error while adding TimePeriod: " + se);
            }
        }

        tsc.addSeries(series);

        return tsc;
    }


    /**
     * Creates an array that consists of two <i>Minute</i> periods [start, end].
     *
     * @param timerange Supports start and end time.
     *
     * @return an array with two <i>Minute</i> periods [start, end].
     */
    protected RegularTimePeriod[] newRegularTimePeriod(Timerange timerange) {
        Date start = new Date(timerange.getStart());
        Date end   = new Date(timerange.getEnd() - 1000 * 60 * 60 * 24);

        return new RegularTimePeriod[] {
            new Day(start),
            new Day(end)
        };
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org