diff artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthCalculation.java @ 8894:a66f2a7c4f84

SINFO FlowDepth - slight code cleanup
author gernotbelger
date Thu, 15 Feb 2018 18:40:40 +0100
parents f431aec10d2c
children b6f7961e4cc5
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthCalculation.java	Thu Feb 15 13:47:19 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthCalculation.java	Thu Feb 15 18:40:40 2018 +0100
@@ -16,6 +16,7 @@
 import java.util.Date;
 import java.util.List;
 
+import org.apache.commons.lang.math.DoubleRange;
 import org.apache.commons.math.FunctionEvaluationException;
 import org.apache.commons.math.analysis.UnivariateRealFunction;
 import org.dive4elements.artifacts.ArtifactDatabase;
@@ -31,6 +32,9 @@
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact;
 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair;
+import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo;
+import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
+import org.dive4elements.river.artifacts.sinfo.util.WstInfo;
 import org.dive4elements.river.artifacts.states.WaterlevelData;
 import org.dive4elements.river.artifacts.states.WaterlevelFetcher;
 import org.dive4elements.river.model.BedHeight;
@@ -65,11 +69,13 @@
         /* access input data */
         final FlowDepthAccess access = new FlowDepthAccess(sinfo);
         final River river = access.getRiver();
+        final RiverInfo riverInfo = new RiverInfo(river);
 
         final Collection<DifferencesPair> diffPairs = access.getDifferencePairs();
 
         final double from = access.getFrom();
         final double to = access.getTo();
+        final DoubleRange calcRange = new DoubleRange(from, to);
 
         final boolean useTkh = access.isUseTransportBodies();
 
@@ -81,10 +87,10 @@
 
         final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name());
 
-        final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, to, useTkh);
+        final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, riverInfo, calcRange, useTkh);
 
         for (final DifferencesPair diffPair : diffPairs) {
-            final FlowDepthCalculationResult result = calculateResult(river, from, to, diffPair, problems, gaugeIndex);
+            final FlowDepthCalculationResult result = calculateResult(river, calcRange, diffPair, problems, gaugeIndex);
             if (result != null)
                 results.addResult(result);
         }
@@ -92,14 +98,14 @@
         return new CalculationResult(results, problems);
     }
 
-    private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, final DifferencesPair diffPair,
+    private FlowDepthCalculationResult calculateResult(final River river, final DoubleRange calcRange, final DifferencesPair diffPair,
             final Calculation problems, final GaugeIndex gaugeIndex) {
 
         /* access real input data from database */
         final String soundingId = diffPair.getSoundingId();
         final String wstId = diffPair.getWstId();
 
-        final BedHeight bedHeight = loadBedHeight(soundingId, from, to);
+        final BedHeight bedHeight = loadBedHeight(soundingId);
         if (bedHeight == null) {
             final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", soundingId);
             problems.addProblem(message);
@@ -120,7 +126,8 @@
         final String label = String.format("%s - %s", wspLabel, soundingLabel);
 
         checkYearDifference(label, waterlevel, bedHeight, problems);
-        checkWaterlevelDiscretisation(wstKms, problems);
+        checkWaterlevelDiscretisation(wstKms, calcRange, problems);
+        // TODO: prüfen, ob sohlhöen die calcRange abdecken/überschneiden
 
         /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */
         final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE);
@@ -142,7 +149,7 @@
             // TODO: keine Berechnung TKH
         }
 
-        final QualityMeasurements bedMeasurements = getBedMeasurements(river, from, to, sounding.getYear());
+        final QualityMeasurements bedMeasurements = getBedMeasurements(river, calcRange, sounding.getYear());
         // FIXME: prüfung ob (genug) werte vorhanden sind? was sind genau die kriterien? falls nein, problemhinzufügen und keine
         // berechnung tkh
         // FIXME: wie wird ggf. interpoliert? --> absprache?
@@ -165,7 +172,7 @@
         final List<BedHeightValue> sortedValues = new ArrayList<>(values);
         Collections.sort(sortedValues, new BedHeightStationComparator());
 
-        SoilKind lastKind = SoilKind.mobil;
+        SoilKind lastKind = SoilKind.starr;
 
         for (final BedHeightValue bedHeightValue : sortedValues) {
 
@@ -180,6 +187,9 @@
             final double km = station;
             final double meanBedHeight = meanBedHeightDbl;
 
+            if (!calcRange.containsDouble(km))
+                continue;
+
             try {
                 // FIXME: check out of range
                 final double wst = wstInterpolator.value(km);
@@ -193,7 +203,7 @@
                 // FIXME: calculate tkh
 
                 // REMARK: bissl spielerei zum testen damit die sohlart nicht zu schnell wechselt
-                final boolean changeKind = Math.random() > 0.95;
+                final boolean changeKind = false; // Math.random() > 0.95;
                 SoilKind kind;
                 if (changeKind) {
                     switch (lastKind) {
@@ -231,7 +241,6 @@
                     break;
                 }
 
-
                 // REMARK: access the location once only during calculation
                 final String location = LocationProvider.getLocation(river.getName(), km);
 
@@ -258,7 +267,7 @@
      * Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb)
      * Abhängig von Peiljahr
      */
-    private QualityMeasurements getBedMeasurements(final River river, final double from, final double to, final int soundingYear) {
+    private QualityMeasurements getBedMeasurements(final River river, final DoubleRange calcRange, final Integer soundingYear) {
 
         /* construct valid measurement time range */
         final Calendar cal = Calendar.getInstance();
@@ -270,7 +279,7 @@
         cal.set(soundingYear + VALID_BED_MEASUREMENT_YEARS, 11, 31);
         final Date endTime = cal.getTime();
 
-        return QualityMeasurementFactory.getBedMeasurements(river.getName(), from, to, startTime, endTime);
+        return QualityMeasurementFactory.getBedMeasurements(river.getName(), calcRange.getMinimumDouble(), calcRange.getMaximumDouble(), startTime, endTime);
     }
 
     /**
@@ -296,7 +305,8 @@
 
         final int difference = Math.abs(soundingYear - wstYear);
         if (difference > maxDifference) {
-            final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.year_difference", null, label, difference);
+            final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.year_difference", null, label, wstYear,
+                    soundingYear);
             problems.addProblem(message);
         }
     }
@@ -332,23 +342,27 @@
     }
 
     /* Checks if the discretisation of the waterlevel exceeds 1000m */
-    // FIXME: vermutlich sollten wir diesen check auf den gültigkeitsbereich einschränken
-    private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) {
+
+    private void checkWaterlevelDiscretisation(final WKms wstKms, final DoubleRange calcRange, final Calculation problems) {
+
         final int size = wstKms.size();
         for (int i = 0; i < size - 2; i++) {
             final double kmPrev = wstKms.getKm(i);
             final double kmNext = wstKms.getKm(i + 1);
 
-            if (Math.abs(kmPrev - kmNext) > 1) {
-                final String label = wstKms.getName();
+            /* only check if we are within the calculation range */
+            if (calcRange.overlapsRange(new DoubleRange(kmPrev, kmNext))) {
+                if (Math.abs(kmPrev - kmNext) > 1) {
+                    final String label = wstKms.getName();
 
-                final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label);
-                problems.addProblem(kmPrev, message);
+                    final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label);
+                    problems.addProblem(kmPrev, message);
+                }
             }
         }
     }
 
-    private BedHeight loadBedHeight(final String soundingId, final double from, final double to) {
+    private BedHeight loadBedHeight(final String soundingId) {
 
         // REMARK: absolutely unbelievable....
         // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type

http://dive4elements.wald.intevation.org