view artifacts/src/main/java/org/dive4elements/river/exports/minfo/SedimentLoadLSGenerator.java @ 7081:3c4efd4b2c19 generator-refactoring

Simplyfy / unify "invert axis" logic - DataUtil now can check if two datasets are in the same direction - In the case of WKM data a no means that the Water flows Right to Left - If the water flows from right to left do inversion of diagrams
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 20 Sep 2013 17:36:11 +0200
parents e0311aa32efb
children 41567bf1e131
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.minfo;

import java.util.Arrays;
import java.util.Set;

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

import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.RangeAccess;
import org.dive4elements.river.artifacts.access.SedimentLoadAccess;
import org.dive4elements.river.artifacts.model.FacetTypes;
import org.dive4elements.river.artifacts.model.FlowVelocityData;
import org.dive4elements.river.artifacts.model.WKms;
import org.dive4elements.river.artifacts.model.minfo.BedDiffEpochResult;
import org.dive4elements.river.artifacts.model.minfo.BedDiffYearResult;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoad;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFraction;
import org.dive4elements.river.exports.StyledSeriesBuilder;
import org.dive4elements.river.exports.XYChartGenerator;
import org.dive4elements.river.jfree.Bounds;
import org.dive4elements.river.jfree.DoubleBounds;
import org.dive4elements.river.jfree.RiverAnnotation;
import org.dive4elements.river.jfree.StyledXYSeries;
import org.dive4elements.river.themes.ThemeDocument;
import org.dive4elements.river.utils.DataUtil;


/** Generator for Longitudinal Sections of SedimentLoad-Calculations. */
public class SedimentLoadLSGenerator
extends XYChartGenerator
implements FacetTypes
{
    /** Y-Axis enum defining the four possible axes. */
    public enum YAXIS {
        L(0),
        D(1),
        DW(2),
        V(3);

        protected int idx;

        private YAXIS(int c) {
            idx = c;
        }
    }
    /** The logger that is used in this generator. */
    private static Logger logger = Logger.getLogger(SedimentLoadLSGenerator.class);

    public static final String I18N_CHART_TITLE = "chart.sedimentload.ls.title";
    public static final String I18N_XAXIS_LABEL = "chart.sedimentload.ls.xaxis.label";
    public static final String I18N_YAXIS_LABEL_1 = "chart.sedimentload.ls.yaxis.label.tpera";
    public static final String I18N_YAXIS_LABEL_2 = "chart.sedimentload.ls.yaxis.label.m3pera";
    public static final String I18N_YAXIS_D_LABEL = "chart.beddifference.yaxis.label.diff";
    public static final String I18N_YAXIS_V_LABEL =
        "chart.flow_velocity.section.yaxis.label";
    public final static String I18N_WDIFF_YAXIS_LABEL =
        "chart.w_differences.yaxis.label";

    public final static String I18N_WDIFF_YAXIS_LABEL_DEFAULT = "m";
    public static final String I18N_CHART_TITLE_DEFAULT = "Sedimentfracht";
    public static final String I18N_XAXIS_LABEL_DEFAULT = "Fluss-Km";
    public static final String I18N_YAXIS_LABEL_DEFAULT_1 = "[t/a]";
    public static final String I18N_YAXIS_LABEL_DEFAULT_2 = "[m\u00b3/a]";
    public static final String I18N_YAXIS_D_LABEL_DEFAULT = "delta S [m]";
    public static final String I18N_YAXIS_V_LABEL_DEFAULT = "Geschwindigkeit v [m/s]";

    /** Enumerator over y-axes. */
    @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
    public void doOut(ArtifactAndFacet bundle, ThemeDocument attr, boolean visible) {
        String name = bundle.getFacetName();

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

        if (name == null) {
            logger.error("No facet name for doOut(). No output generated!");
            return;
        }

        Facet facet = bundle.getFacet();
        D4EArtifact artifact = (D4EArtifact)bundle.getArtifact();

        if (facet == null) {
            return;
        }
        if (getXBounds(0) != null && getDomainAxisRange() != null) {
            logger.debug(Arrays.toString(getDomainAxisRangeFromRequest()));
            Bounds bounds =
                calculateZoom(getXBounds(0), getDomainAxisRange());
            context.putContextValue("startkm", bounds.getLower());
            context.putContextValue("endkm", bounds.getUpper());
        }
        else if (getXBounds(0) != null && getDomainAxisRange() == null) {
            context.putContextValue("startkm", getXBounds(0).getLower());
            context.putContextValue("endkm", getXBounds(0).getUpper());
        }
        else if (getXBounds(0) == null && getDomainAxisRange() == null) {
            RangeAccess access = new RangeAccess(artifact);
            context.putContextValue("startkm", access.getFrom());
            context.putContextValue("endkm", access.getTo());
        }
        else if (getXBounds(0) == null && getDomainAxisRange() != null){
            RangeAccess access = new RangeAccess(artifact);
            Bounds b = new DoubleBounds(access.getFrom(), access.getTo());
            Bounds bounds =
                calculateZoom(b, getDomainAxisRange());
            context.putContextValue("startkm", bounds.getLower());
            context.putContextValue("endkm", bounds.getUpper());
        }
        if (FacetTypes.IS.SEDIMENT_LOAD(name)) {
            doSedimentLoadOut(
                (double[][]) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (FacetTypes.IS.SEDIMENT_LOAD_UNKNOWN(name)) {
            doSedimentLoadUnknownOut(
                (SedimentLoad)bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(FLOW_VELOCITY_TOTALCHANNEL)) {
            doFlowVelocityTotalOut(
                (FlowVelocityData) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(FLOW_VELOCITY_TOTALCHANNEL_FILTERED)) {
            doFlowVelocityTotalOut(
                (FlowVelocityData) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(FLOW_VELOCITY_MAINCHANNEL)) {
            doFlowVelocityMainOut(
                (FlowVelocityData) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(FLOW_VELOCITY_MAINCHANNEL_FILTERED)) {
            doFlowVelocityMainOut(
                (FlowVelocityData) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(BED_DIFFERENCE_YEAR)) {
            doBedDifferenceYearOut(
                (BedDiffYearResult) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(BED_DIFFERENCE_YEAR_FILTERED)) {
            doBedDifferenceYearOut(
                (BedDiffYearResult) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(BED_DIFFERENCE_EPOCH)) {
            doBedDifferenceEpochOut(
                (BedDiffEpochResult) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(W_DIFFERENCES)) {
            doWDifferencesOut(
                (WKms) bundle.getData(context),
                bundle,
                attr,
                visible);
        }
        else if (name.equals(LONGITUDINAL_ANNOTATION)) {
            doAnnotations(
                (RiverAnnotation) bundle.getData(context),
                 bundle,
                 attr,
                 visible);
        }
        else if (FacetTypes.IS.MANUALPOINTS(name)) {
            doPoints(
                bundle.getData(context),
                bundle,
                attr,
                visible,
                YAXIS.L.idx);
        }
    }

    @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) {
        String label = "default";
        if (pos == YAXIS.L.idx) {
            SedimentLoadAccess slaccess =
                new SedimentLoadAccess((D4EArtifact) getMaster());
            String unit = slaccess.getUnit();
            if (unit != null && unit.equals("m3_per_a")) {
                label = msg(I18N_YAXIS_LABEL_2, I18N_YAXIS_LABEL_DEFAULT_2);
            }
            else {
                label = msg(I18N_YAXIS_LABEL_1, I18N_YAXIS_LABEL_DEFAULT_1);
            }
        }
        else if (pos == YAXIS.V.idx) {
            label = msg(I18N_YAXIS_V_LABEL, I18N_YAXIS_V_LABEL_DEFAULT);
        }
        else if (pos == YAXIS.D.idx) {
            label = msg(I18N_YAXIS_D_LABEL, I18N_YAXIS_D_LABEL_DEFAULT);
        }
        else if (pos == YAXIS.DW.idx) {
            label = msg(I18N_WDIFF_YAXIS_LABEL, I18N_WDIFF_YAXIS_LABEL_DEFAULT);
        }

        return label;
    }

    protected void doSedimentLoadOut(double[][] data,
        ArtifactAndFacet aandf, ThemeDocument theme, boolean visible) {

        // Allow for gaps (NaNs).
        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), false, theme);
        StyledSeriesBuilder.addPoints(series, data, false);

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

    protected void doSedimentLoadUnknownOut(SedimentLoad load,
        ArtifactAndFacet aandf, ThemeDocument theme, boolean visible) {

        Set<Double> kms = load.getKms();
        double[][] data = new double[2][kms.size()];
        int counter = 0;
        for (Double km: kms) {
            SedimentLoadFraction fraction = load.getFraction(km);
            data[0][counter] = km;
            data[1][counter] = fraction.getUnknown();
            counter++;
        }
        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);
        StyledSeriesBuilder.addPoints(series, data, false);

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

    protected void doFlowVelocityMainOut(
        FlowVelocityData data,
        ArtifactAndFacet aandf,
        ThemeDocument    theme,
        boolean          visible
    ) {
        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);
        StyledSeriesBuilder.addPoints(series, data.getMainChannelPoints(), true);

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

    protected void doFlowVelocityTotalOut(
        FlowVelocityData data,
        ArtifactAndFacet aandf,
        ThemeDocument    theme,
        boolean          visible
    ) {
        if (data == null) {
            logger.warn("No data to add to FlowVelocity chart.");
            return;
         }

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

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

    protected void doBedDifferenceYearOut(
        BedDiffYearResult data,
        ArtifactAndFacet aandf,
        ThemeDocument theme,
        boolean visible
    ) {
        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);
        StyledSeriesBuilder.addPoints(series, data.getDifferencesData(), true);

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

    protected void doBedDifferenceEpochOut(
        BedDiffEpochResult data,
        ArtifactAndFacet aandf,
        ThemeDocument theme,
        boolean visible
    ) {
        XYSeries series = new StyledXYSeries(aandf.getFacetDescription(), theme);
        StyledSeriesBuilder.addPoints(series, data.getDifferencesData(), true);

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

    protected void doWDifferencesOut(
        WKms       wkms,
        ArtifactAndFacet aandf,
        ThemeDocument   theme,
        boolean    visible
    ) {
        if (wkms == null) {
            logger.warn("No data to add to WDifferencesChart.");
            return;
        }

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

        StyledSeriesBuilder.addPoints(series, wkms);

        addAxisSeries(series, YAXIS.D.idx, visible);
        if (wkms.guessWaterIncreasing()) {
            setInverted(true);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org