Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java @ 9369:1a4d2ce77423
radio choice fix
author | gernotbelger |
---|---|
date | Thu, 02 Aug 2018 17:39:13 +0200 |
parents | 2aec052d4088 |
children | 6f7e92c16050 |
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.uinfo.salix; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NavigableMap; import org.dive4elements.river.artifacts.WINFOArtifact; import org.dive4elements.river.artifacts.access.ComputationRangeAccess; 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.model.WstValueTable; import org.dive4elements.river.artifacts.model.WstValueTable.QPosition; import org.dive4elements.river.artifacts.model.WstValueTableFactory; import org.dive4elements.river.artifacts.sinfo.common.RiverInfoProvider; import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; import org.dive4elements.river.artifacts.sinfo.tkhstate.WinfoArtifactWrapper; import org.dive4elements.river.artifacts.uinfo.UINFOArtifact; import org.dive4elements.river.artifacts.uinfo.common.UInfoResultType; import org.dive4elements.river.artifacts.uinfo.salix.SalixLineAccess.ScenarioType; import org.dive4elements.river.model.Gauge; import org.dive4elements.river.model.MainValue; import org.dive4elements.river.model.MainValueType.MainValueTypeKey; import org.dive4elements.river.utils.Formatter; /** * Calculation of the result rows of the u-info salix line calc mode * * @author Matthias Schäfer */ final class SalixLineCalculator { private final List<ResultRow> rows = new ArrayList<>(); private final RiverInfoProvider riverInfoProvider; private final Map<Gauge, QPosition> gaugeMwPos; private final Map<Gauge, QPosition> gaugeMnwPos; private final Map<Gauge, QPosition> gaugeMhwPos; private Calculation problems; private WstValueTable wst; public SalixLineCalculator(final RiverInfoProvider riverInfoProvider) { this.riverInfoProvider = riverInfoProvider; this.gaugeMwPos = new HashMap<>(); this.gaugeMnwPos = new HashMap<>(); this.gaugeMhwPos = new HashMap<>(); } /** * Calculate the salix line result rows */ public void execute(final Calculation problems, final UINFOArtifact uinfo, final NavigableMap<Double, List<Double>> rangeScenarios, final ScenarioType scenarioType, final String[] scenarioLabels, final String rangeString, final String additionalString, final SalixLineCalculationResults results) { this.problems = problems; this.wst = WstValueTableFactory.getTable(this.riverInfoProvider.getRiver()); fetchGaugeMainValuePositions(); final WINFOArtifact winfo = new WinfoArtifactWrapper(uinfo); winfo.addStringData("ld_mode", "distance"); winfo.addStringData("ld_step", "100"); for (final double station : new ComputationRangeAccess(winfo).getKms()) { this.rows.add(createRow(station, rangeScenarios)); } if (scenarioType == ScenarioType.REGIONAL) results.addResult(new SalixLineCalculationRegionalResult("Salix-regional", scenarioLabels, rangeString, additionalString, this.rows), problems); else if (scenarioType == ScenarioType.SUPRAREGIONAL) results.addResult(new SalixLineCalculationSupraRegionalResult("Salix-supra", scenarioLabels, rangeString, additionalString, this.rows), problems); else if (scenarioType == ScenarioType.HISTORICAL) results.addResult(new SalixLineCalculationHistoricalResult("Salix-hist", scenarioLabels, rangeString, additionalString, this.rows), problems); else results.addResult(new SalixLineCalculationResult("Salix-simple", this.rows), problems); } /** * Fetch MQ, MNQ and MHQ of all gauges and determine the wst QPosition for each one */ private void fetchGaugeMainValuePositions() { this.gaugeMwPos.clear(); this.gaugeMnwPos.clear(); this.gaugeMhwPos.clear(); for (final Gauge gauge : this.riverInfoProvider.getGauges()) { this.gaugeMwPos.put(gauge, null); this.gaugeMnwPos.put(gauge, null); this.gaugeMhwPos.put(gauge, null); final double gaugeKm = gauge.getStation().doubleValue(); for (final MainValue mv : MainValue.getValuesOfGaugeAndType(gauge, MainValueTypeKey.Q)) { if (mv.getMainValue().getName().equalsIgnoreCase("mq")) this.gaugeMwPos.put(gauge, this.wst.getQPosition(gaugeKm, mv.getValue().doubleValue())); else if (mv.getMainValue().getName().equalsIgnoreCase("mnq")) this.gaugeMnwPos.put(gauge, this.wst.getQPosition(gaugeKm, mv.getValue().doubleValue())); else if (mv.getMainValue().getName().equalsIgnoreCase("mhq")) this.gaugeMhwPos.put(gauge, this.wst.getQPosition(gaugeKm, mv.getValue().doubleValue())); } } } /** * Create a result row for a station and its gauge, and add w-q-values as selected * * @param rangeScenarios2 */ private ResultRow createRow(final double station, final NavigableMap<Double, List<Double>> rangeScenarios) { final ResultRow row = ResultRow.create(); // Find station's gauge final Gauge gauge = this.riverInfoProvider.getGauge(station, true); row.putValue(GeneralResultType.station, station); // Interpolate mnw, mw, and mhw final double mnw = interpolateW(station, this.gaugeMnwPos.get(gauge)); final double mw = interpolateW(station, this.gaugeMwPos.get(gauge)); final double mhw = interpolateW(station, this.gaugeMhwPos.get(gauge)); row.putValue(SInfoResultType.waterlevel, mnw); row.putValue(SInfoResultType.waterlevel1, mw); row.putValue(SInfoResultType.waterlevel2, mhw); // Calc salix-line and mw-mnw row.putValue(UInfoResultType.salixline, calcSalix(mhw, mw)); row.putValue(UInfoResultType.salix_mw_mnw, calcMwmnw(mw, mnw)); // Calc scenario values (always all scenario types set, Result variant extracts the fields needed) final List<SalixScenario> scenarios = new ArrayList<>(); final double[] deltaws = getDeltaWs(station, rangeScenarios); for (int i = 0; i <= deltaws.length - 1; i++) { if (Math.abs(deltaws[i]) > 0.0001) { final double salix = calcSalix(mhw, mw + deltaws[i]); scenarios.add(new SalixScenario((int) (deltaws[i] * 100), salix)); } else { scenarios.add(null); } } row.putValue(UInfoResultType.customMultiRowColSalixScenarios, scenarios); return row; } /** * Interpolates the W for a station with a fixed (virtual) wst column position */ private double interpolateW(final double station, final QPosition qPosition) { if (qPosition != null) return this.wst.interpolateW(station, qPosition, this.problems); return Double.NaN; } /** * Calculates the salix value */ private double calcSalix(final double mhw, final double mw) { return Formatter.roundW(mhw) - 2.31 - Formatter.roundW(mw); } /** * Calculates the inverse MW-MNW difference */ private double calcMwmnw(final double mw, final double mnw) { return Formatter.roundW(mnw) - Formatter.roundW(mw); } /** * Gets the station-specific list of delta-ws of the active scenario, at least with one 0 item in any case * * @param rangeScenarios */ private double[] getDeltaWs(final double station, final NavigableMap<Double, List<Double>> rangeScenarios) { final Entry<Double, List<Double>> stationScenarios = rangeScenarios.floorEntry(station); if (stationScenarios == null) return new double[] { 0.0 }; final double[] deltaws = new double[stationScenarios.getValue().size()]; for (int i = 0; i <= stationScenarios.getValue().size() - 1; i++) deltaws[i] = stationScenarios.getValue().get(i); return deltaws; } }