[PATCH 3 of 3] Work on U-Info salix line calculation

Wald Commits scm-commit at wald.intevation.org
Wed Jul 25 19:29:45 CEST 2018


# HG changeset patch
# User mschaefer
# Date 1532539765 -7200
# Node ID 9a9f076d5716c0cc69711f075cbf9df125f036f3
# Parent  9055afc19ec6aa9177a6e0735f60a927e9f1a098
Work on U-Info salix line calculation

diff -r 9055afc19ec6 -r 9a9f076d5716 artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineAccess.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineAccess.java	Wed Jul 25 19:28:54 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineAccess.java	Wed Jul 25 19:29:25 2018 +0200
@@ -32,6 +32,34 @@
     // ld_from_part; ld_to_part
     // scenario_selection (mögliche Werte:"scenarioType.option1" "scenarioType.option2" "scenarioType.option3"
 
+    /**
+     * Type of a salix line scenario with key property used by the client
+     */
+    public enum ScenarioType {
+        NONE(""), //
+        REGIONAL("scenarioType.option1"), //
+        SUPRAREGIONAL("scenarioType.option2"), //
+        HISTORICAL("scenarioType.option3");
+
+        private String key;
+
+        private ScenarioType(final String key) {
+            this.key = key;
+        }
+
+        public static ScenarioType forKey(final String key) {
+            for (final ScenarioType st : ScenarioType.values()) {
+                if (st.getKey().equals(key))
+                    return st;
+            }
+            return NONE;
+        }
+
+        public String getKey() {
+            return this.key;
+        }
+    }
+
     public SalixLineAccess(final UINFOArtifact artifact) {
         super(artifact);
 
@@ -44,11 +72,11 @@
         return super.getBoolean("use_scenario");
     }
 
-    public String getScenario() {
-        if (getUseScenario()) {
-            return getString("scenario_selection");
-        }
-        return null;
+    public ScenarioType getScenario() {
+        if (getUseScenario())
+            return ScenarioType.forKey(getString("scenario_selection"));
+        else
+            return ScenarioType.NONE;
     }
 
     public Double getFromPart() {
diff -r 9055afc19ec6 -r 9a9f076d5716 artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculation.java	Wed Jul 25 19:28:54 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculation.java	Wed Jul 25 19:29:25 2018 +0200
@@ -9,11 +9,16 @@
  */
 package org.dive4elements.river.artifacts.uinfo.salix;
 
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
+import java.util.NavigableMap;
+import java.util.TreeMap;
 
 import org.apache.commons.lang.math.DoubleRange;
 import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.river.artifacts.access.RangeAccess;
 import org.dive4elements.river.artifacts.model.Calculation;
 import org.dive4elements.river.artifacts.model.CalculationResult;
 import org.dive4elements.river.artifacts.resources.Resources;
@@ -21,6 +26,7 @@
 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils;
 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
 import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
+import org.dive4elements.river.artifacts.uinfo.salix.SalixLineAccess.ScenarioType;
 import org.dive4elements.river.model.River;
 
 /**
@@ -46,27 +52,29 @@
         final River river = accessSalix.getRiver();
         final RiverInfo riverInfo = new RiverInfo(river);
 
-        final DoubleRange range = accessSalix.getRange();
-        final boolean useScenario = accessSalix.getUseScenario();
-        final String selectedScenario = accessSalix.getScenario();
-        final Double fromPart = accessSalix.getFromPart();
-        final Double toPart = accessSalix.getToPart();
-
-        final boolean useSCenario = accessSalix.getUseScenario();
-        final String scenario = accessSalix.getScenario();
+        // final DoubleRange range = accessSalix.getRange();
+        // final boolean useScenario = accessSalix.getUseScenario();
+        // final String selectedScenario = accessSalix.getScenario();
+        // final Double fromPart = accessSalix.getFromPart();
+        // final Double toPart = accessSalix.getToPart();
+        //
+        // final boolean useSCenario = accessSalix.getUseScenario();
+        // final String scenario = accessSalix.getScenario();
         // calculation_mode
         // ld_from , ld_to
         // use_scenario (boolean)
         // ld_from_part; ld_to_part
         // scenario_selection (mögliche Werte:"scenarioType.option1" "scenarioType.option2" "scenarioType.option3"
-        final RiverInfoProvider riverInfoProvider = RiverInfoProvider.forRange(this.context, river, range);
+        final RiverInfoProvider riverInfoProvider = RiverInfoProvider.forRange(this.context, river, accessSalix.getRange());
 
         // FIXME: real calculation
 
-        final SalixLineCalculationResults results = new SalixLineCalculationResults(calcModeLabel, user, riverInfo, range);
+        final SalixLineCalculationResults results = new SalixLineCalculationResults(calcModeLabel, user, riverInfo, accessSalix.getRange());
 
         final SalixLineCalculator calculator = new SalixLineCalculator(this.context, riverInfoProvider);
-        calculator.execute(problems, uinfo, results);
+        final NavigableMap<Double, List<Double>> rangeScenarios = buildRangeScenarios(accessSalix);
+
+        calculator.execute(problems, uinfo, rangeScenarios, accessSalix.getScenario(), results);
 
         // final Collection<ResultRow> rows = new ArrayList<>();
         // SalixLineCalculationNoScenarioResult result = null; // verzweigung etwas ungünstig. möglicherweise auch die
@@ -145,6 +153,61 @@
         return new CalculationResult(results, problems);
     }
 
+    /**
+     * Build a map of delta-Ws by km-range for the selected scenario
+     */
+    private NavigableMap<Double, List<Double>> buildRangeScenarios(final SalixLineAccess access) {
+        final NavigableMap<Double, List<Double>> rangeScenarios = new TreeMap<>();
+        if (access.getScenario() == ScenarioType.REGIONAL)
+            fillRangeScenarios(rangeScenarios, access, access.getFromPart().doubleValue(), access.getToPart().doubleValue(),
+                    access.getRegionalScenarioIntegers());
+        else if (access.getScenario() == ScenarioType.SUPRAREGIONAL)
+            fillRangeScenarios(rangeScenarios, access.getSupraRegionalString());
+        // TODO else if (access.getScenario().equals(ScenarioType.HISTORICAL.getKey()))
+        // historisches Szenario aus MSH etc.
+        else
+            fillRangeScenarios(rangeScenarios, access);
+        return rangeScenarios;
+    }
+
+    /**
+     * Fill a map of delta-Ws with only one 0-delta for the whole calc range (no scenario)
+     */
+    private void fillRangeScenarios(final NavigableMap<Double, List<Double>> rangeScenarios, final RangeAccess calcRange) {
+        final List<Double> nulls = new ArrayList<>();
+        nulls.add(0.0);
+        rangeScenarios.put(Double.valueOf(calcRange.getLowerKm() - 0.0001), nulls);
+    }
+
+    /**
+     * Fill a map of delta-Ws by km-range from the regional scenario input data
+     */
+    private void fillRangeScenarios(final NavigableMap<Double, List<Double>> rangeScenarios, final RangeAccess calcRange, final double partFrom,
+            final double partTo, final int[] deltaWs) {
+        final List<Double> nulls = new ArrayList<>();
+        final List<Double> dwsm = new ArrayList<>();
+        for (int i = 0; i <= deltaWs.length - 1; i++) {
+            nulls.add(0.0);
+            dwsm.add(deltaWs[i] / 100.0);
+        }
+        rangeScenarios.put(Double.valueOf(calcRange.getLowerKm() - 0.0001), nulls);
+        rangeScenarios.put(Double.valueOf(partFrom - 0.0001), dwsm);
+        rangeScenarios.put(Double.valueOf(partTo + 0.0001), nulls);
+    }
+
+    /**
+     * Fill a map of delta-Ws by km-range from the supraregional scenario input data
+     * (the zones input by the user cover the calc range completely)
+     */
+    private void fillRangeScenarios(final NavigableMap<Double, List<Double>> rangeScenarios, final String zones) {
+        final List<SalixZone> parts = SalixZone.parse(zones);
+        for (final SalixZone part : parts) {
+            final List<Double> dwsm = new ArrayList<>();
+            dwsm.add(part.getDwsplValue() / 100.0);
+            rangeScenarios.put(Double.valueOf(part.getFromKm().doubleValue() - 0.0001), dwsm);
+        }
+    }
+
     private SalixScenario findScenarioByKm(final double km, final Map<DoubleRange, SalixScenario> rangeScenarioMap) {
         final Iterator<DoubleRange> rangeIterator = rangeScenarioMap.keySet().iterator();
         while (rangeIterator.hasNext()) {
diff -r 9055afc19ec6 -r 9a9f076d5716 artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculationRegionalResult.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculationRegionalResult.java	Wed Jul 25 19:28:54 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculationRegionalResult.java	Wed Jul 25 19:29:25 2018 +0200
@@ -26,11 +26,9 @@
 final class SalixLineCalculationRegionalResult extends SalixLineCalculationNoScenarioResult {
 
     private static final long serialVersionUID = 1L;
-    private final int[] scenarioValues;
 
-    public SalixLineCalculationRegionalResult(final String label, final WstInfo wst, final Collection<ResultRow> rows, final int[] scenarioValues) {
+    public SalixLineCalculationRegionalResult(final String label, final WstInfo wst, final Collection<ResultRow> rows) {
         super(label, wst, rows);
-        this.scenarioValues = scenarioValues; // TODO: diese values müssen eigentlich nur ne anzahl sein, oder so. Ergibt sihc in der Berech
     }
 
     @Override
@@ -38,12 +36,9 @@
 
         final Collection<String> header = super.writeNoScenarioHeader(exportContextCSV);
 
-        for (int i = 0; i < this.scenarioValues.length; i++) {
-            // index wurde verworfen, stattdessen 2 spalten
+        header.add(exportContextCSV.msg(SalixScenario.getScenarioValueHeader()));
+        header.add(exportContextCSV.msg(SalixScenario.getDwsplHeader()));
 
-            header.add(exportContextCSV.msg(SalixScenario.getScenarioValueHeader()) + " (" + (i + 1) + ")"); // index kann auch weg, war nur ne idee
-            header.add(exportContextCSV.msg(SalixScenario.getDwsplHeader()) + " (" + (i + 1) + ")");
-        }
         exportContextCSV.writeCSVLine(header.toArray(new String[header.size()]));
     }
 
diff -r 9055afc19ec6 -r 9a9f076d5716 artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Wed Jul 25 19:28:54 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixLineCalculator.java	Wed Jul 25 19:29:25 2018 +0200
@@ -13,6 +13,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.NavigableMap;
 
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.WINFOArtifact;
@@ -29,6 +30,7 @@
 import org.dive4elements.river.artifacts.sinfo.tkhstate.WinfoArtifactWrapper;
 import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
 import org.dive4elements.river.artifacts.uinfo.commons.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;
@@ -54,6 +56,9 @@
 
     private WstValueTable wst;
 
+    private NavigableMap<Double, List<Double>> rangeScenarios;
+
+
     public SalixLineCalculator(final CallContext context, final RiverInfoProvider riverInfoProvider) {
         this.context = context;
         this.riverInfoProvider = riverInfoProvider;
@@ -66,10 +71,12 @@
     /**
      * Calculate the salix line result rows
      */
-    public void execute(final Calculation problems, final UINFOArtifact uinfo, final SalixLineCalculationResults results) {
+    public void execute(final Calculation problems, final UINFOArtifact uinfo, final NavigableMap<Double, List<Double>> rangeScenarios,
+            final ScenarioType scenarioType, final SalixLineCalculationResults results) {
 
         this.problems = problems;
         this.wst = WstValueTableFactory.getTable(this.riverInfoProvider.getRiver());
+        this.rangeScenarios = rangeScenarios;
 
         fetchGaugeMainValuePositions();
 
@@ -79,7 +86,14 @@
         for (final double station : new ComputationRangeAccess(winfo).getKms()) {
             this.rows.add(createRow(station));
         }
-        results.addResult(new SalixLineCalculationNoScenarioResult("Salix", null, this.rows), problems);
+        if (scenarioType == ScenarioType.REGIONAL)
+            results.addResult(new SalixLineCalculationRegionalResult("Salix", null, this.rows), problems);
+        else if (scenarioType == ScenarioType.SUPRAREGIONAL)
+            results.addResult(new SalixLineCalculationSupraRegionalResult("Salix", null, this.rows), problems);
+        else if (scenarioType == ScenarioType.HISTORICAL)
+            results.addResult(new SalixLineCalculationHistoricalResult("Salix", null, this.rows), problems);
+        else
+            results.addResult(new SalixLineCalculationNoScenarioResult("Salix", null, this.rows), problems);
     }
 
     /**


More information about the Dive4Elements-commits mailing list