view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthExporter.java @ 8946:5d5d482da3e9

Implementing SINFO - FlowDepthMinMax calculation
author gernotbelger
date Tue, 13 Mar 2018 18:49:33 +0100
parents 9c02733a1b3c
children a4f1ac81f26d
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.artifacts.sinfo.flowdepth;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings;
import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter;
import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo;
import org.dive4elements.river.artifacts.sinfo.util.MetaAndTableJRDataSource;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
import org.dive4elements.river.artifacts.sinfo.util.WstInfo;
import org.dive4elements.river.utils.RiverUtils;

import au.com.bytecode.opencsv.CSVWriter;

/**
 * Generates different output formats (csv, pdf) of data that resulted from a flow depths min/max computation.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 * @author Gernot Belger
 */
// REMARK: must be public because its registered in generators.xml
public class FlowDepthExporter extends AbstractSInfoExporter<FlowDepthRow, FlowDepthCalculationResult, FlowDepthCalculationResults> {

    /** The log used in this exporter. */
    private static Logger log = Logger.getLogger(FlowDepthExporter.class);

    private static final String CSV_FLOWDEPTHMINMAX_HEADER = "sinfo.export.flow_depth_minmax.csv.header.flowdepthminmax";

    private static final String CSV_FLOWDEPTHTKHMINMAX_HEADER = "sinfo.export.flow_depth_minmax.csv.header.flowdepthTkh";

    private static final String CSV_TKH_HEADER = "sinfo.export.flow_depth.csv.header.tkh";

    private static final String JASPER_FILE = "/jasper/sinfo.flowdepthminmax.jasper";

    @Override
    protected Logger getLog() {
        return log;
    }

    @Override
    protected void writeCSVGlobalMetadata(final CSVWriter writer, final FlowDepthCalculationResults results) {
        log.info("FlowDepthExporter.writeCSVMeta");

        super.writeCSVGlobalMetadataDefaults(writer, results);

        writer.writeNext(new String[] { "" });
    }

    @Override
    protected void writeCSVResultMetadata(final CSVWriter writer, final FlowDepthCalculationResults results, final FlowDepthCalculationResult result) {

        final BedHeightInfo sounding = result.getSounding();
        super.writeCSVSoundingMetadata(writer, sounding);

        final WstInfo wst = result.getWst();
        super.writeCSVWaterlevelMetadata(writer, wst);
    }

    /**
     * Write the header, with different headings depending on whether at a
     * gauge or at a location.
     *
     * @param river
     * @param useTkh
     */

    @Override
    protected void writeCSVHeader(final CSVWriter writer, final FlowDepthCalculationResults results, final RiverInfo river) {
        log.info("FlowDepthExporter.writeCSVHeader");

        final Collection<String> header = new ArrayList<>(11);

        header.add(msg(SInfoI18NStrings.CSV_KM_HEADER));
        header.add(msgUnit(CSV_FLOWDEPTHMINMAX_HEADER, SInfoI18NStrings.UNIT_M));
        if (getData().isUseTkh()) {
            header.add(msgUnit(CSV_FLOWDEPTHTKHMINMAX_HEADER, SInfoI18NStrings.UNIT_M));
            header.add(msgUnit(CSV_TKH_HEADER, SInfoI18NStrings.UNIT_CM));
        }

        header.add(msgUnit(SInfoI18NStrings.CSV_WATERLEVEL_HEADER, river.getWstUnit()));
        header.add(msgUnit(SInfoI18NStrings.CSV_DISCHARGE_HEADER, SInfoI18NStrings.UNIT_CUBIC_M));
        header.add(msg(SInfoI18NStrings.CSV_LABEL_HEADER));
        header.add(msg(SInfoI18NStrings.CSV_GAUGE_HEADER));
        header.add(msgUnit(SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, river.getWstUnit()));
        header.add(msg(SInfoI18NStrings.CSV_SOUNDING_HEADER));
        header.add(msg(SInfoI18NStrings.CSV_LOCATION_HEADER));

        writer.writeNext(header.toArray(new String[header.size()]));
    }

    @Override
    protected String[] formatCSVRow(final FlowDepthCalculationResults results, final FlowDepthRow row) {
        return formatFlowDepthRow(row);
    }

    /**
     * Format a row of a flow depth result into an array of string, both used by csv and pdf
     *
     * @param useTkh
     */
    private String[] formatFlowDepthRow(final FlowDepthRow row) {

        final Collection<String> lines = new ArrayList<>(11);

        // Fluss-km
        lines.add(getKmFormatter().format(row.getStation()));

        // Fließtiefe [m]
        lines.add(getFlowDepthFormatter().format(row.getFlowDepth()));

        if (getData().isUseTkh()) {
            // Fließtiefe mit TKH [m]
            lines.add(getFlowDepthFormatter().format(row.getFlowDepthWithTkh()));

            // TKH [cm]
            lines.add(getTkhFormatter().format(row.getTkh()));
        }

        // Wasserstand [NN + m]
        lines.add(getW2Formatter().format(row.getWaterlevel()));

        // Q [m³/s]
        final double discharge = row.getDischarge();
        if (Double.isNaN(discharge))
            lines.add(StringUtils.EMPTY);
        else {
            final double roundedDischarge = RiverUtils.roundQ(discharge);
            lines.add(getQFormatter().format(roundedDischarge));
        }

        // Bezeichnung
        lines.add(row.getWaterlevelLabel());

        // Bezugspegel
        lines.add(row.getGauge());

        // Mittlere Sohlhöhe [NN + m]
        lines.add(getMeanBedHeighFormatter().format(row.getMeanBedHeight()));

        // Peilung/Epoche
        lines.add(row.getSoundageLabel());

        // Lage
        lines.add(row.getLocation());

        return lines.toArray(new String[lines.size()]);
    }

    @Override
    protected final String getJasperFile() {
        return JASPER_FILE;
    }

    @Override
    protected final void addJRMetaData(final MetaAndTableJRDataSource source, final FlowDepthCalculationResults results) {

        /* general metadata */
        super.addJRMetaDataDefaults(source, results);

        /* column headings */
        source.addMetaData("station_header", msg(SInfoI18NStrings.CSV_KM_HEADER));
        source.addMetaData("flowdepth_header", msg(CSV_FLOWDEPTHMINMAX_HEADER));
        source.addMetaData("flowdepth_tkh_header", msg(CSV_FLOWDEPTHTKHMINMAX_HEADER));
        source.addMetaData("tkh_header", msg(CSV_TKH_HEADER));
        source.addMetaData("waterlevel_header", msg(SInfoI18NStrings.CSV_WATERLEVEL_HEADER));
        source.addMetaData("discharge_header", msg(SInfoI18NStrings.CSV_DISCHARGE_HEADER));
        source.addMetaData("waterlevel_name_header", msg(SInfoI18NStrings.CSV_LABEL_HEADER));
        source.addMetaData("gauge_header", msg(SInfoI18NStrings.CSV_GAUGE_HEADER));
        source.addMetaData("bedheight_header", msg(SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER_SHORT));
        source.addMetaData("sounding_name_header", msg(SInfoI18NStrings.CSV_SOUNDING_HEADER));
        source.addMetaData("location_header", msg(SInfoI18NStrings.CSV_LOCATION_HEADER));
    }

    @Override
    protected String[] formatPDFRow(final FlowDepthCalculationResults results, final FlowDepthRow row) {
        return formatFlowDepthRow(row);
    }
}

http://dive4elements.wald.intevation.org