diff artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java @ 9269:83ebeb620b5a

Station specific main value annotations in S-Info flood duration curve, corrected infrastructure flood duration calculation
author mschaefer
date Thu, 19 Jul 2018 08:07:03 +0200
parents 465347d12990
children bcbae86ce7b3
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java	Wed Jul 18 18:53:15 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java	Thu Jul 19 08:07:03 2018 +0200
@@ -27,11 +27,13 @@
 import org.dive4elements.river.artifacts.model.CalculationResult;
 import org.dive4elements.river.artifacts.model.WQDay;
 import org.dive4elements.river.artifacts.model.WQKms;
+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.resources.Resources;
 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.exports.WaterlevelDescriptionBuilder;
 import org.dive4elements.river.jfree.StickyAxisAnnotation;
@@ -90,40 +92,52 @@
         final double[] stationsSorted = sortStations(allStations.keySet());
 
         // Calculate W and Q for all stations and the selected discharge states
-        // TODO Laut Herrn Reiß: Q und D jeweils konstant für jedes Pegel-Intervall, Q-Änderungen (Zuflüsse etc.) aus .wst
-        // ignorieren
+        // TODO Geht das schneller, wenn man WstValueTable statt WINFOArtifact.computeWaterlevelData nutzt?
         final WQKms[] wqkmsArray = calculateWaterlevels(winfo, stationsSorted, problems);
 
         // Determine discharge state labels of the main values
         updateMainValueLabels(wqkmsArray, winfo, problems);
 
-        // Create a finder for Q in the {river}.wst km-w-q table
-        final WQBaseTableFinder wqFinder = WQBaseTableFinder.loadValues(this.riverInfoProvider.getRiver(), calcRange.getMinimumDouble(),
-                calcRange.getMaximumDouble(), problems);
+        // Load base wst table (river).wst - first run takes long time, then it's cached
+        final WstValueTable wst = WstValueTableFactory.getTable(this.riverInfoProvider.getRiver());
 
         // Calculate the durations and create the result rows
         for (int i = 0; i <= stationsSorted.length - 1; i++) {
             final Gauge gauge = this.riverInfoProvider.getGauge(stationsSorted[i], true);
             final ResultRow row = createRow(stationsSorted[i], gauge, firstGauge, wqkmsArray, durFinders.get(gauge), i);
             if (allStations.containsKey(stationsSorted[i]) && (allStations.get(stationsSorted[i]) != null))
-                calculateInfrastructure(row, gauge, allStations.get(stationsSorted[i]), wqFinder, durFinders);
+                calculateInfrastructure(row, gauge, allStations.get(stationsSorted[i]), wst, durFinders);
             this.rows.add(row);
             if (secondBank.containsKey(stationsSorted[i])) {
                 final ResultRow row2 = ResultRow.create(row);
-                calculateInfrastructure(row2, gauge, secondBank.get(stationsSorted[i]), wqFinder, durFinders);
+                calculateInfrastructure(row2, gauge, secondBank.get(stationsSorted[i]), wst, durFinders);
                 this.rows.add(row2);
             }
         }
+
+        //// Create a finder for Q in the {river}.wst km-w-q table
+        // final WQBaseTableFinder wqFinder = WQBaseTableFinder.loadValues(this.riverInfoProvider.getRiver(),
+        //// calcRange.getMinimumDouble(),
+        // calcRange.getMaximumDouble(), problems);
+        //
+        //// Calculate the durations and create the result rows
+        // for (int i = 0; i <= stationsSorted.length - 1; i++) {
+        // final Gauge gauge = this.riverInfoProvider.getGauge(stationsSorted[i], true);
+        // final ResultRow row = createRow(stationsSorted[i], gauge, firstGauge, wqkmsArray, durFinders.get(gauge), i);
+        // if (allStations.containsKey(stationsSorted[i]) && (allStations.get(stationsSorted[i]) != null))
+        // calculateInfrastructure(row, gauge, allStations.get(stationsSorted[i]), wqFinder, durFinders);
+        // this.rows.add(row);
+        // if (secondBank.containsKey(stationsSorted[i])) {
+        // final ResultRow row2 = ResultRow.create(row);
+        // calculateInfrastructure(row2, gauge, secondBank.get(stationsSorted[i]), wqFinder, durFinders);
+        // this.rows.add(row2);
+        // }
+        // }
+
         final String[] mainValueLabels = new String[wqkmsArray.length];
         for (int i = 0; i <= wqkmsArray.length - 1; i++)
             mainValueLabels[i] = wqkmsArray[i].getName();
         results.addResult(new FloodDurationCalculationResult(label, mainValueLabels, this.rows), problems);
-
-        // calcWQDays(problems, stationsSorted[0], AttributeKey.LEFT, winfo, results);
-        //
-        // calcMainValueAnnotations(problems, 0, AttributeKey.LEFT, wqkmsArray, results);
-        //
-        // calcInfrastructureAnnotations(problems, this.rows.get(0), wqkmsArray, results);
     }
 
     /**
@@ -148,30 +162,63 @@
     }
 
     /**
-     * Calculate the data for the W and Q main value lines in the duration curve chart and add to result collection
+     * Calculates the data for the Q main value lines in the duration curve chart
      */
-    public void calcMainValueAnnotations(final Calculation problems, final int stationIndex, final AttributeKey riverside,
-            final WQKms[] wqkmsArray, final FloodDurationCalculationResults results) {
+    public List<StickyAxisAnnotation> calcMainValueQAnnotations(final Calculation problems, final double station, final FloodDurationCalculationResult result) {
 
-        // Same way as in MainValueWFacet and ..QFacet, but special label handling
-        final List<StickyAxisAnnotation> ws = new ArrayList<>();
-        final List<StickyAxisAnnotation> qs = new ArrayList<>();
-        for (int i = 0; i <= wqkmsArray.length - 1; i++) {
-            final String qLabel = wqkmsArray[i].getName().startsWith("W=") ? "Q(" + wqkmsArray[i].getName() + ")" : wqkmsArray[i].getName();
-            final StickyAxisAnnotation qAnnotation = new StickyAxisAnnotation(qLabel, (float) wqkmsArray[i].getQ(stationIndex), SimpleAxis.Y_AXIS,
+        // Search the station in the previously calculated result rows and terminate if no infrastructure row found
+        double station1 = station;
+        if (Double.isNaN(station)) {
+            for (final ResultRow row : result.getRows()) {
+                station1 = row.getDoubleValue(GeneralResultType.station);
+                break;
+            }
+        }
+        final List<ResultRow> stationRows = searchStation(station1, result.getRows());
+        if (stationRows.isEmpty() || (stationRows.get(0).getValue(SInfoResultType.infrastructuretype) == null)) {
+            return new ArrayList<>();
+        }
+        final ResultRow row = stationRows.get(0);
+        final List<StickyAxisAnnotation> annotations = new ArrayList<>();
+        final List<DurationWaterlevel> wqds = (List<DurationWaterlevel>) row.getValue(SInfoResultType.customMultiRowColWaterlevel);
+        for (final DurationWaterlevel wqd : wqds) {
+            final String label = wqd.getBezeichnung().startsWith("W=") ? "Q(" + wqd.getBezeichnung() + ")" : wqd.getBezeichnung();
+            final StickyAxisAnnotation annotation = new StickyAxisAnnotation(label, (float) wqd.getDischarge(), SimpleAxis.Y_AXIS,
                     FloodDurationCurveGenerator.YAXIS.Q.idx);
-            qs.add(qAnnotation);
-            FloodDurationMainValuesQFacet.setHitPoint(results.getDurationCurve(), qAnnotation);
-            final String wLabel = !wqkmsArray[i].getName().startsWith("W=") ? "W(" + wqkmsArray[i].getName() + ")" : wqkmsArray[i].getName();
-            final StickyAxisAnnotation wAnnotation = new StickyAxisAnnotation(wLabel, (float) wqkmsArray[i].getW(stationIndex), SimpleAxis.Y_AXIS,
+            annotation.setHitPoint(wqd.getFloodDurDaysPerYear());
+            annotations.add(annotation);
+        }
+        return annotations;
+    }
+
+    /**
+     * Calculates the data for the W main value lines in the duration curve chart
+     */
+    public List<StickyAxisAnnotation> calcMainValueWAnnotations(final Calculation problems, final double station, final FloodDurationCalculationResult result) {
+
+        // Search the station in the previously calculated result rows and terminate if no infrastructure row found
+        double station1 = station;
+        if (Double.isNaN(station)) {
+            for (final ResultRow row : result.getRows()) {
+                station1 = row.getDoubleValue(GeneralResultType.station);
+                break;
+            }
+        }
+        final List<ResultRow> stationRows = searchStation(station1, result.getRows());
+        if (stationRows.isEmpty() || (stationRows.get(0).getValue(SInfoResultType.infrastructuretype) == null)) {
+            return new ArrayList<>();
+        }
+        final List<StickyAxisAnnotation> annotations = new ArrayList<>();
+        final ResultRow row = stationRows.get(0);
+        final List<DurationWaterlevel> wqds = (List<DurationWaterlevel>) row.getValue(SInfoResultType.customMultiRowColWaterlevel);
+        for (final DurationWaterlevel wqd : wqds) {
+            final String label = !wqd.getBezeichnung().startsWith("W=") ? "W(" + wqd.getBezeichnung() + ")" : wqd.getBezeichnung();
+            final StickyAxisAnnotation annotation = new StickyAxisAnnotation(label, (float) wqd.getWaterlevel(), SimpleAxis.Y_AXIS,
                     FloodDurationCurveGenerator.YAXIS.W.idx);
-            ws.add(wAnnotation);
-            FloodDurationMainValuesWFacet.setHitPoint(results.getDurationCurve(), wAnnotation);
+            annotation.setHitPoint(wqd.getFloodDurDaysPerYear());
+            annotations.add(annotation);
         }
-        results.getMainValueQAnnotations().clear();
-        results.getMainValueQAnnotations().addAll(qs);
-        results.getMainValueWAnnotations().clear();
-        results.getMainValueWAnnotations().addAll(ws);
+        return annotations;
     }
 
     /**
@@ -191,13 +238,13 @@
         }
         final List<ResultRow> stationRows = searchStation(station1, result.getRows());
         if (stationRows.isEmpty() || (stationRows.get(0).getValue(SInfoResultType.infrastructuretype) == null)) {
-            return null;
+            return new ArrayList<>();
         }
         // Same way as in MainValueWFacet and ..QFacet
         final List<StickyAxisAnnotation> annotations = new ArrayList<>();
         for (final ResultRow row : stationRows) {
-            annotations.add(calcWAnnotation(row));
-            annotations.add(calcQAnnotation(row));
+            annotations.add(calcInfrastructureWAnnotation(row));
+            annotations.add(calcInfrastructureQAnnotation(row));
         }
         return annotations;
     }
@@ -219,7 +266,7 @@
     /**
      * Calculates the Q annotation lines of an infrastructure
      */
-    private StickyAxisAnnotation calcQAnnotation(final ResultRow row) {
+    private StickyAxisAnnotation calcInfrastructureQAnnotation(final ResultRow row) {
         final String label = Resources.getMsg(this.context.getMeta(), "sinfo.chart.flood_duration.curve.infrastructure",
                 "sinfo.chart.flood_duration.curve.infrastructure",
                 SInfoResultType.infrastructuretype.exportValue(this.context, row.getValue(SInfoResultType.infrastructuretype))
@@ -233,7 +280,7 @@
     /**
      * Calculates the W annotation lines of an infrastructure
      */
-    private StickyAxisAnnotation calcWAnnotation(final ResultRow row) {
+    private StickyAxisAnnotation calcInfrastructureWAnnotation(final ResultRow row) {
         final String label = Resources.getMsg(this.context.getMeta(), "sinfo.chart.flood_duration.curve.infrastructure",
                 "sinfo.chart.flood_duration.curve.infrastructure",
                 SInfoResultType.infrastructuretype.exportValue(this.context, row.getValue(SInfoResultType.infrastructuretype))
@@ -378,15 +425,43 @@
         return row;
     }
 
+    /// **
+    // * Calculate the result row fields for one infrastructure - gives wrong duration numbers where Q changes within the
+    /// gauge range
+    // */
+    // private void calculateInfrastructure(final ResultRow row, final Gauge gauge, final InfrastructureValue
+    /// infrastructure, final WQBaseTableFinder wqFinder,
+    // final Map<Gauge, GaugeDurationValuesFinder> durFinders) {
+    //
+    // final double q = wqFinder.getDischarge(infrastructure.getStation(), infrastructure.getHeight());
+    // final double qOut = Double.isInfinite(q) ? Double.NaN : q;
+    // final double dur = underflowDaysToOverflowDays(durFinders.get(gauge).getDuration(q));
+    // row.putValue(SInfoResultType.riverside, infrastructure.getAttributeKey());
+    // row.putValue(SInfoResultType.floodDuration, dur);
+    // row.putValue(SInfoResultType.floodDischarge, qOut);
+    // row.putValue(SInfoResultType.infrastructureHeight, infrastructure.getHeight());
+    // row.putValue(SInfoResultType.infrastructuretype, infrastructure.getInfrastructure().getType().getName());
+    // }
+
     /**
      * Calculate the result row fields for one infrastructure
      */
-    private void calculateInfrastructure(final ResultRow row, final Gauge gauge, final InfrastructureValue infrastructure, final WQBaseTableFinder wqFinder,
+    private void calculateInfrastructure(final ResultRow row, final Gauge gauge, final InfrastructureValue infrastructure, final WstValueTable wst,
             final Map<Gauge, GaugeDurationValuesFinder> durFinders) {
 
-        final double q = wqFinder.getDischarge(infrastructure.getStation(), infrastructure.getHeight());
+        // Interpolate the infrastructure height in the wst table to get the corresponding Q
+        final Calculation problems = new Calculation();
+        final double[] qs = wst.findQsForW(infrastructure.getStation().doubleValue(), infrastructure.getHeight().doubleValue(), problems);
+        // TODO Fehlerbehandlung (kein Q gefunden)
+        final double q = (qs.length >= 1) ? qs[0] : Double.NaN;
         final double qOut = Double.isInfinite(q) ? Double.NaN : q;
-        final double dur = underflowDaysToOverflowDays(durFinders.get(gauge).getDuration(q));
+        // Determine the relative column position of the Q
+        final QPosition qPos = wst.getQPosition(infrastructure.getStation().doubleValue(), q);
+        // Get the Q for the found column position for the station of the gauge
+        final double qGauge = wst.getQ(qPos, gauge.getStation().doubleValue());
+        // Interpolate the Q-D-table of the gauge
+        final double dur = underflowDaysToOverflowDays(durFinders.get(gauge).getDuration(qGauge));
+        // Set result row
         row.putValue(SInfoResultType.riverside, infrastructure.getAttributeKey());
         row.putValue(SInfoResultType.floodDuration, dur);
         row.putValue(SInfoResultType.floodDischarge, qOut);

http://dive4elements.wald.intevation.org