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

work on collison, flood_duration
author gernotbelger
date Tue, 03 Jul 2018 17:00:48 +0200
parents 53cc5b496692
children 4e6111f87dda
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 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 int waterlevelCount;
    // 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;
        this.waterlevelCount = mainvalueLabels.length;
    }

    /**
     * The label of one of the optional main values, or null
     */
    // public String getMainValueLabel(final int index) {
    // if (index <= this.mainvalueLabels.length - 1)
    // return this.mainvalueLabels[index];
    // else
    // return null;
    // }

    /**
     * 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);
    }

    /**
     * Fetches the km-longitudinal section of the infrastructures and one of their result fields
     */
    public final double[][] fetchInfrastructurePoints(final IResultType type) {
        final TDoubleArrayList xPoints = new TDoubleArrayList(this.rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(this.rows.size());
        for (final ResultRow row : this.rows) {
            if (row.getValue(SInfoResultType.infrastructuretype) != null) {
                xPoints.add(row.getDoubleValue(GeneralResultType.station));
                yPoints.add(row.getDoubleValue(type));
            }
        }
        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    /**
     * Fetches the km-longitudinal section of a main value
     */
    public final double[][] fetchMainValuePoints(final IResultType type) {
        final TDoubleArrayList xPoints = new TDoubleArrayList(this.rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(this.rows.size());
        // final IResultType check = new IResultType[] { SInfoResultType.mainValue1Duration, SInfoResultType.mainValue2Duration,
        // SInfoResultType.mainValue3Duration }[index];
        for (final ResultRow row : this.rows) {
            // if (!Double.isNaN(row.getDoubleValue(check))) {
            xPoints.add(row.getDoubleValue(GeneralResultType.station));
            yPoints.add(row.getDoubleValue(type));
            // }
        }
        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    @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: "
            exportContextCSV.writeCSVMetaEntry(SInfoI18NStrings.CSV_META_HEADER_WATERLEVEL_GAUGE, "TODO: gauge");
        }
        exportContextCSV.writeBlankLine();
    }

    @Override
    protected String getJasperFile() {
        if (this.waterlevelCount <= 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());
            }
        }

        if ((this.waterlevelCount == 0 || this.waterlevelCount == 2) && mode == ExportMode.pdf) {
            // lines.add("dummy1");
            // lines.add("dummy2");
            // lines.add("dummy3");
            // lines.add("dummy4");
        }

        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 int naturalIndex = i + 1;
            final String appendIndex = new StringBuilder().append("_").append(naturalIndex).toString();
            final Object[] args = new Object[] { appendIndex };
            // new StringBuilder().append('\u2081').toString(); // schlechter UTF-8-Support für subscript ints
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderW(), new Object[] { appendIndex, "results.getRiver().getWstUnit()" }));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderFloodDurPerYear(), args));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderQ(), args));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderBezeichn(), args));
        }

        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);

        // add dynamic headers

        if (this.waterlevelCount == 0 || this.waterlevelCount == 2) {
            // source.addMetaData("dummy1", "dummy1");
            // source.addMetaData("dummy2", "dummy2");
            // source.addMetaData("dummy3", "dummy3");
            // source.addMetaData("dummy4", "dummy4");
        }

        for (int i = 0; i < this.waterlevelCount; i++) {
            final int naturalIndex = i + 1;

            final Object[] args = new String[] { new StringBuilder().append("_").append(naturalIndex).toString() };
            exportContextPDF.addJRMetadata(source, getPdfHeader("w", naturalIndex), exportContextPDF.msg(DurationWaterlevel.getHeaderW(), args));
            exportContextPDF.addJRMetadata(source, getPdfHeader("duration", naturalIndex),
                    exportContextPDF.msg(DurationWaterlevel.getHeaderFloodDurPerYear(), args));
            exportContextPDF.addJRMetadata(source, getPdfHeader("q", naturalIndex), exportContextPDF.msg(DurationWaterlevel.getHeaderQ(), args));
            exportContextPDF.addJRMetadata(source, getPdfHeader("bezeichnung", naturalIndex),
                    exportContextPDF.msg(DurationWaterlevel.getHeaderBezeichn(), args));
        }

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

    public final int getWaterlevelCount() {
        return this.waterlevelCount;
    }

    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 String getMainValueLabel(final int j) {

        if (this.mainvalueLabels != null && j < this.mainvalueLabels.length)
            return this.mainvalueLabels[j];
        return "";
    }

    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