view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents 9b2e46090099
children b4402594213b
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.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.math.DoubleRange;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.common.GeneralResultType;
import org.dive4elements.river.artifacts.common.ResultRow;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.sinfo.common.GaugeDurationValuesFinder;
import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider;
import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType;
import org.dive4elements.river.artifacts.sinfo.common.WQBaseTableFinder;
import org.dive4elements.river.artifacts.sinfo.flood_duration.RiversideRadioChoice.RiversideChoiceKey;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
import org.dive4elements.river.model.Attribute.AttributeKey;
import org.dive4elements.river.model.Gauge;
import org.dive4elements.river.model.sinfo.InfrastructureValue;

/**
 * Calculation of the result rows of the flood duration of the infrastructures in a river km range
 * and selected main value durations
 *
 * @author Matthias Schäfer
 */
final class FloodDurationCalculator {

    private final Collection<ResultRow> rows = new ArrayList<>();

    private final RiverInfoProvider riverInfoProvider;

    private final CallContext context;


    public FloodDurationCalculator(final CallContext context, final RiverInfoProvider riverInfoProvider) {
        this.context = context;
        this.riverInfoProvider = riverInfoProvider;
    }

    /**
     * Calculate the result rows
     *
     * @return a result collection with one result
     */
    public FloodDurationCalculationResults execute(final Calculation problems, final String label, final String calcModeLabel,
            final DoubleRange calcRange, final RiversideChoiceKey riverside, final String user) {

        final WQBaseTableFinder wqFinder = WQBaseTableFinder.loadValues(this.riverInfoProvider.getRiver(), calcRange.getMinimumDouble(),
                calcRange.getMaximumDouble(), problems);
        final Map<Gauge, GaugeDurationValuesFinder> durFinders = new HashMap<>();
        for (final Gauge gauge : this.riverInfoProvider.getRiver().determineGauges(calcRange.getMinimumDouble(), calcRange.getMaximumDouble())) {
            durFinders.put(gauge, GaugeDurationValuesFinder.loadValues(gauge, problems));
        }
        final AttributeKey bankKey = riverside.getAttributeKey();
        for (final InfrastructureValue infrastructure : InfrastructureValue.getValues(this.riverInfoProvider.getRiver(), calcRange.getMinimumDouble(),
                calcRange.getMaximumDouble(), bankKey)) {
            calculateResultRow(infrastructure, wqFinder, durFinders);
        }

        final FloodDurationCalculationResult result = new FloodDurationCalculationResult(label, this.rows);

        final RiverInfo riverInfo = new RiverInfo(this.riverInfoProvider.getRiver());

        final FloodDurationCalculationResults results = new FloodDurationCalculationResults(calcModeLabel, user, riverInfo, calcRange);
        results.addResult(result, problems);
        return results;
    }

    /**
     * Calculate the result row for one infrastructure
     */
    private void calculateResultRow(final InfrastructureValue infrastructure, final WQBaseTableFinder wqFinder,
            final Map<Gauge, GaugeDurationValuesFinder> durFinders) {

        final ResultRow row = ResultRow.create();

        final Gauge gauge = this.riverInfoProvider.getGauge(infrastructure.getStation(), true);
        final double q = wqFinder.getDischarge(infrastructure.getStation(), infrastructure.getHeight());
        final double qOut = Double.isInfinite(q) ? Double.NaN : q;
        // REMARK: access the location once only during calculation
        final String location = this.riverInfoProvider.getLocation(infrastructure.getStation());
        row.putValue(GeneralResultType.station, infrastructure.getStation());
        row.putValue(SInfoResultType.riverside, infrastructure.getAttributeKey().getName()); // TODO i18n
        row.putValue(SInfoResultType.floodDuration, 365 - durFinders.get(gauge).getDuration(q));
        row.putValue(SInfoResultType.floodDischarge, qOut);
        row.putValue(SInfoResultType.infrastructureHeight, infrastructure.getHeight());
        row.putValue(SInfoResultType.infrastructuretype, infrastructure.getInfrastructure().getType().getName());

        // TODO Berechne W, Überflutungsdauer, Q und Bezeichnung von bis zu drei WSPL
        // row.putValue(SInfoResultType.customMultiRowColWaterlevel, rowWsps);

        row.putValue(SInfoResultType.gaugeLabel, gauge.getName());
        row.putValue(SInfoResultType.location, location);

        this.rows.add(row);
    }
}

http://dive4elements.wald.intevation.org