view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculationResult.java @ 9229:0dcd1cd41915

Different themes/facets for left bank and right bank infrastructures in S-Info flood durations, some fixmes done
author mschaefer
date Thu, 05 Jul 2018 16:49:42 +0200
parents 4e6111f87dda
children b515ed950d39
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * 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.flood_duration;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.dive4elements.river.artifacts.common.AbstractCalculationExportableResult;
import org.dive4elements.river.artifacts.common.AbstractExportContext;
import org.dive4elements.river.artifacts.common.ExportContextCSV;
import org.dive4elements.river.artifacts.common.ExportContextPDF;
import org.dive4elements.river.artifacts.common.GeneralResultType;
import org.dive4elements.river.artifacts.common.IResultType;
import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource;
import org.dive4elements.river.artifacts.common.ResultRow;
import org.dive4elements.river.artifacts.sinfo.common.SInfoI18NStrings;
import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
import org.dive4elements.river.exports.DiagramGenerator;
import org.dive4elements.river.model.Attribute.AttributeKey;

import gnu.trove.TDoubleArrayList;

/**
 * Contains the result of a {@link FloodDurationCalculation}.
 *
 * @author Gernot Belger
 */
public final class FloodDurationCalculationResult extends AbstractCalculationExportableResult {

    private static final long serialVersionUID = 1L;

    private final String[] mainvalueLabels;

    // private final WstInfo wstInfo;

    private final int maxWaterlevelPdf = 3;

    public interface ValueGetter {
        double getValue(DurationWaterlevel waterlevel);
    }

    private enum ExportMode {
        pdf, csv
    }

    public FloodDurationCalculationResult(final String label, final String[] mainvalueLabels, final Collection<ResultRow> rows) {
        super(label, rows);
        this.mainvalueLabels = mainvalueLabels;
    }

    /**
     * Collection of the result rows containing only the rows describing an infrastructure
     */
    @Override
    public Collection<ResultRow> getRows() {
        final List<ResultRow> infrasOnlyRows = new ArrayList<>();
        for (final ResultRow row : this.rows)
            if (row.getValue(SInfoResultType.infrastructuretype) != null)
                infrasOnlyRows.add(row);
        return Collections.unmodifiableCollection(infrasOnlyRows);
    }

    @Override
    protected void writeCSVResultMetadata(final ExportContextCSV exportContextCSV) {
        if (this.mainvalueLabels.length >= 1) {
            // "##METADATEN WASSERSPIEGELLAGE"
            exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL);
            for (int i = 1; i <= getWaterlevelCount(); // this.mainvalueLabels.length;
                    i++) {
                // "# Bezeichnung der Wasserspiegellage: "
                final String label = this.getMainValueLabel(i - 1);
                exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_NAME, String.format("%d: %s", i, label));
            }
            // "# Bezugspegel: "
            for (final ResultRow row : this.rows) {
                exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, row.getValue(SInfoResultType.gaugeLabel));
                break;
            }
        }
        exportContextCSV.writeBlankLine();
    }

    @Override
    protected String getJasperFile() {
        if (this.getWaterlevelCount() <= 1)
            return "/jasper/templates/sinfo.floodduration.jrxml";
        else
            return "/jasper/templates/sinfo.floodduration2.jrxml";
    }

    protected String[] formatRow(final AbstractExportContext exportContextCSV, final ResultRow row, final ExportMode mode) {

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

        lines.add(exportContextCSV.formatRowValue(row, GeneralResultType.station));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.riverside));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.floodDuration));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.floodDischarge));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.infrastructureHeight));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.infrastructuretype));

        final List<DurationWaterlevel> waterlevelList = (List<DurationWaterlevel>) row.getValue(SInfoResultType.customMultiRowColWaterlevel);
        if (waterlevelList != null) {
            final NumberFormat wFormatter = exportContextCSV.getFlowDepthFormatter();
            final NumberFormat qFormatter = exportContextCSV.getQFormatter();

            for (int i = 0; i < waterlevelList.size(); i++) {

                if (i == this.maxWaterlevelPdf && mode == ExportMode.pdf)
                    break;

                final DurationWaterlevel item = waterlevelList.get(i);
                lines.add(item.getWFormatted(wFormatter));
                lines.add(item.getFloodDurDaysPerYearFormatted());
                lines.add(item.getQFormatted(qFormatter));
                lines.add(item.getBezeichnung());
            }
        }

        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.gaugeLabel));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.location));

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

    @Override
    public void writeCSVHeader(final ExportContextCSV exportContextCSV, final RiverInfo river) {

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

        header.add(exportContextCSV.formatCsvHeader(GeneralResultType.station));
        header.add(exportContextCSV.formatCsvHeader(SInfoResultType.riverside));
        header.add(exportContextCSV.formatCsvHeader(SInfoResultType.floodDuration));
        header.add(exportContextCSV.msgUnitCSV(SInfoResultType.floodDischarge, SInfoResultType.floodDischarge.getUnit()));
        header.add(exportContextCSV.msgUnitCSV(SInfoResultType.infrastructureHeight, SInfoResultType.infrastructureHeight.getUnit()));
        header.add(exportContextCSV.formatCsvHeader(SInfoResultType.infrastructuretype));

        // add dynamic headers
        final int waterlevelCount = // results.
                getWaterlevelCount();
        for (int i = 0; i < waterlevelCount; i++) {
            final String appendIndex = "_" + Integer.toString(i + 1);
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderWCsv(), appendIndex, "results.getRiver().getWstUnit()"));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderFloodDurPerYearCsv(), appendIndex));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderQ(), appendIndex));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderBezeichnCsv(), appendIndex));
        }

        header.add(exportContextCSV.formatCsvHeader(SInfoResultType.gaugeLabel));
        header.add(exportContextCSV.formatCsvHeader(SInfoResultType.location));

        exportContextCSV.writeCSVLine(header.toArray(new String[header.size()]));

    }

    @Override
    protected String[] formatCSVRow(final ExportContextCSV exportContextCSV, final ResultRow row) {

        return this.formatRow(exportContextCSV, row, ExportMode.csv);
    }

    @Override
    protected String[] formatPDFRow(final ExportContextPDF exportContextPDF, final ResultRow row) {

        return this.formatRow(exportContextPDF, row, ExportMode.pdf);
    }

    @Override
    protected void addJRTableHeader(final ExportContextPDF exportContextPDF, final MetaAndTableJRDataSource source) {

        /* column headings */
        exportContextPDF.addJRMetadata(source, "station_header", GeneralResultType.station);
        exportContextPDF.addJRMetadata(source, "riverside_header", SInfoResultType.riverside);
        exportContextPDF.addJRMetadata(source, "inundationduration_header", SInfoResultType.floodDuration);
        exportContextPDF.addJRMetadata(source, "inundationduration_q_header", SInfoResultType.floodDischarge);
        exportContextPDF.addJRMetadata(source, "infrastructure_height_header", SInfoResultType.infrastructureHeight);
        exportContextPDF.addJRMetadata(source, "infrastructure_type_header", SInfoResultType.infrastructuretype);

        for (int i = 1; i <= this.getWaterlevelCount(); i++) {

            final String appendIndex = "_" + Integer.toString(i);
            exportContextPDF.addJRMetadata(source, getPdfHeader("w", i), exportContextPDF.msg(DurationWaterlevel.getHeaderWPdf(), appendIndex));
            exportContextPDF.addJRMetadata(source, getPdfHeader("duration", i),
                    exportContextPDF.msg(DurationWaterlevel.getHeaderFloodDurPerYearPdf(), appendIndex));
            exportContextPDF.addJRMetadata(source, getPdfHeader("q", i), exportContextPDF.msg(DurationWaterlevel.getHeaderQ(), appendIndex));
            exportContextPDF.addJRMetadata(source, getPdfHeader("bezeichnung", i),
                    exportContextPDF.msg(DurationWaterlevel.getHeaderBezeichnPdf(), appendIndex));
        }

        exportContextPDF.addJRMetadata(source, "gauge_header", SInfoResultType.gaugeLabel);
        exportContextPDF.addJRMetadata(source, "location_header", SInfoResultType.location);
    }

    private final String getPdfHeader(final String rootStr, final int index) {
        final String hd = "_header";
        final StringBuilder builder = new StringBuilder();
        return builder.append(rootStr).append("_").append(index).append(hd).toString();
    }

    public final int getWaterlevelCount() {
        return (this.mainvalueLabels != null) ? this.mainvalueLabels.length : 0;
    }

    public String getMainValueLabel(final int j) {
        if (this.mainvalueLabels != null && j < this.mainvalueLabels.length)
            return this.mainvalueLabels[j];
        return "";
    }

    /**
     * Gets the longitudinal section of a result value type for one river side
     */
    public final double[][] getInfrastructurePoints(final IResultType type, final AttributeKey riverside) {

        final TDoubleArrayList xPoints = new TDoubleArrayList(this.rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(this.rows.size());

        for (final ResultRow row : this.rows) {

            final double station = row.getDoubleValue(GeneralResultType.station);
            final double value = row.getDoubleValue(type);
            if (row.getValue(SInfoResultType.riverside) == riverside) {
                xPoints.add(station);
                yPoints.add(value);
            }
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    /**
     * Gets a longitudinal section of W, Q, or flood duration of one of the waterlevels
     */
    public final double[][] getMainValueDurationPoints(final DiagramGenerator generator, final ValueGetter valuegetter, final int dataIndex) {

        final TDoubleArrayList xPoints = new TDoubleArrayList(this.rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(this.rows.size());

        for (final ResultRow row : this.rows) {

            final double station = row.getDoubleValue(GeneralResultType.station);

            final List<DurationWaterlevel> waterlevels = (List<DurationWaterlevel>) row.getValue(SInfoResultType.customMultiRowColWaterlevel);
            final DurationWaterlevel waterlevel = waterlevels.get(dataIndex);

            final Double value = valuegetter.getValue(waterlevel);

            xPoints.add(station);
            yPoints.add(value);
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }
}

http://dive4elements.wald.intevation.org