view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculationResult.java @ 9630:8da911b5169c

3 Significant also for Q_1, Q_2...
author dnt_bjoernsen <d.tironi@bjoernsen.de>
date Mon, 14 Oct 2019 18:07:31 +0200
parents 677ff7ed9a60
children
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 java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections.Predicate;
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.SInfoResultType;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;

import gnu.trove.TDoubleArrayList;

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

    private final static class IsInfrastructureChoicePredicate implements Predicate {

        private final FloodDurationInfrastructureChoice m_infrastructure;

        public IsInfrastructureChoicePredicate(final FloodDurationInfrastructureChoice infrastructure) {
            this.m_infrastructure = infrastructure;
        }

        @Override
        public boolean evaluate(final Object object) {
            final ResultRow row = (ResultRow) object;
            final FloodDurationInfrastructureChoice test = new FloodDurationInfrastructureChoice(row);
            return this.m_infrastructure.equals(test);
        }
    }

    private final static class HasInfrastructurePredicate implements Predicate {

        @Override
        public boolean evaluate(final Object object) {
            final ResultRow row = (ResultRow) object;

            return row.getValue(SInfoResultType.riverside) != null;
        }
    }

    private static final long serialVersionUID = 1L;

    private final boolean isUseWspl;

    private final String[] waterlevelLabels;

    private final int maxWaterlevelPdf = 3;

    private final Set<FloodDurationInfrastructureChoice> m_infrastructures;

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

    private enum ExportMode {
        pdf, csv
    }

    public FloodDurationCalculationResult(final String label, final String[] mainvalueLabels, final Collection<ResultRow> rows, final boolean isUseWspl,
            final Set<FloodDurationInfrastructureChoice> infrastructures) {
        super(label, rows);
        this.waterlevelLabels = mainvalueLabels;
        this.isUseWspl = isUseWspl;
        this.m_infrastructures = infrastructures;
    }

    public Set<FloodDurationInfrastructureChoice> getInfrastructureMap() {
        return this.m_infrastructures;
    }

    /**
     * Collection of the result rows containing only the rows describing an infrastructure
     */
    // FIXME: bad to override, instead make new method 'getInfrastructureRows' or similar?
    @Override
    public Collection<ResultRow> getRows() {

        final Collection<ResultRow> rows = super.getRows();

        final List<ResultRow> infrasOnlyRows = new ArrayList<>();
        for (final ResultRow row : rows) {
            if (row.getValue(SInfoResultType.infrastructuregroup) != null && row.getValue(SInfoResultType.infrastructuretype) != null)
                infrasOnlyRows.add(row);
        }
        return Collections.unmodifiableCollection(infrasOnlyRows);
    }

    /**
     * Collection of all result rows
     */
    public Collection<ResultRow> getAllRows() {

        return super.getRows();
    }

    @Override
    protected void writeCSVResultMetadata(final ExportContextCSV exportContextCSV) {
        /* nothing to do, as we never have several results, everything is written into the general header */
    }

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

        return "/jasper/templates/sinfo.floodduration2.jrxml";
    }

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

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

        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.infrastructuregroup));
        lines.add(exportContextCSV.formatRowValue(row, SInfoResultType.infrastructuretype));

        final List<DurationWaterlevel> waterlevelList = (List<DurationWaterlevel>) row.getValue(SInfoResultType.customMultiRowColWaterlevel);

        final int expectedSizetoGaugelabel = lines.size() + (waterlevelList.size() < 2 ? 4 : 12); // removing columns only works for fixed
        // indices

        final NumberFormat wFormatter = exportContextCSV.getFlowDepthFormatter();
        final NumberFormat qFormatter = exportContextCSV.getQFormatter();
        final NumberFormat durFormatter = exportContextCSV.getFloodDurationFormatter();

        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(durFormatter));
            lines.add(item.getQFormatted3Significant(exportContextCSV.getContext()));
            lines.add(item.getBezeichnung());
        }

        while (mode == ExportMode.pdf && lines.size() < expectedSizetoGaugelabel)
            lines.add("");

        if (isUseWspl() || mode == ExportMode.pdf)
            lines.add(exportContextCSV.formatRowValue(row, GeneralResultType.gaugeLabel)); // PDF: necessary, because if removed, location would not be shown!

        lines.add(exportContextCSV.formatRowValue(row, GeneralResultType.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, river.getWstUnit()));
        header.add(exportContextCSV.formatCsvHeader(SInfoResultType.infrastructuregroup));
        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, river.getWstUnit()));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderFloodDurPerYearCsv(), appendIndex));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderQ(), appendIndex));
            header.add(exportContextCSV.msg(DurationWaterlevel.getHeaderBezeichnCsv(), appendIndex));
        }

        if (isUseWspl())
            header.add(exportContextCSV.formatCsvHeader(GeneralResultType.gaugeLabel));

        header.add(exportContextCSV.formatCsvHeader(GeneralResultType.location));

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

    public List<Double> getValidDurationChartKilometers() {

        // Wunsch BfG (Testung MS3): nur stationen mit infrastruktur
        final Predicate filter = new HasInfrastructurePredicate();
        return getValues(GeneralResultType.station, filter);
    }

    @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_group_header", SInfoResultType.infrastructuregroup);
        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", GeneralResultType.gaugeLabel);
        exportContextPDF.addJRMetadata(source, "location_header", GeneralResultType.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.waterlevelLabels != null) ? this.waterlevelLabels.length : 0;
    }

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

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

        return getPoints(GeneralResultType.station, type, new IsInfrastructureChoicePredicate(infrastructure));
    }

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

        final Collection<ResultRow> rows = getAllRows();

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

        for (final ResultRow row : 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() };
    }

    public boolean isUseWspl() {
        return this.isUseWspl;
    }

    public Collection<Entry<String, String>> getUniqueInfrastruktureTypes() {
        // TODO Auto-generated method stub
        return null;
    }
}

http://dive4elements.wald.intevation.org