changeset 9395:0255c51283a4

Bundu dynamix main value calculation: added hsq-ii, removed hq5, correction of duration q calculation
author mschaefer
date Mon, 13 Aug 2018 09:16:44 +0200
parents 439699ff9b2d
children 6ebc9357550c
files artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesService.java
diffstat 1 files changed, 42 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesService.java	Fri Aug 10 17:31:46 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesService.java	Mon Aug 13 09:16:44 2018 +0200
@@ -9,6 +9,7 @@
 package org.dive4elements.river.artifacts.services;
 
 import java.math.BigDecimal;
+import java.math.MathContext;
 import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -180,10 +181,15 @@
         final double mhq = DoubleUtil.sum(mhqs.toNativeArray()) / mhqs.size();
         mainValues.add(createMainValue(gauge, fetchNamedQMainValue("MHQ", session), mhq, timeperiod));
 
-        // Compute hq5
-        mhqs.sort();
-        final double hq5 = mhqs.get((int) Math.ceil(4 * mhqs.size() / 5));
-        mainValues.add(createMainValue(gauge, fetchNamedQMainValue("HQ5", session), hq5, timeperiod));
+        // Compute hq5 - obsolete
+        // mhqs.sort();
+        // final double hq5 = mhqs.get((int) Math.ceil(4 * mhqs.size() / 5));
+        // mainValues.add(createMainValue(gauge, fetchNamedQMainValue("HQ5", session), hq5, timeperiod));
+
+        // Add HSQ-II from the gauge's main values
+        final MainValue hsq2 = fetchHsqII(gauge, session);
+        if (hsq2 != null)
+            mainValues.add(hsq2);
 
         // Query the gauge's daily Q values and build a list sorted by ascending Q
         final TDoubleArrayList qs = new TDoubleArrayList();
@@ -196,10 +202,12 @@
         double glq20 = Double.NaN;
         for (int i = 0, k = 0; (i <= 364) && (k <= qs.size() - 1); i++, k += yearCnt) {
             final NamedMainValue nmv = fetchNamedQMainValue(i, session, mainValues.get(0).getMainValue().getType());
-            if (nmv != null)
-                mainValues.add(createMainValue(gauge, nmv, getDurationQ(qs, k), timeperiod));
-            if (i == 20)
-                glq20 = qs.get(k);
+            if (nmv != null) {
+                final double q = getDurationQ(qs, k);
+                mainValues.add(createMainValue(gauge, nmv, q, timeperiod));
+                if (i == 20)
+                    glq20 = q;
+            }
         }
         mainValues.add(createMainValue(gauge, fetchNamedQMainValue("GlQ", session), glq20, timeperiod));
     }
@@ -216,6 +224,21 @@
     }
 
     /**
+     * Fetches the gauge's HSQ-II from the database, or returns null
+     */
+    private MainValue fetchHsqII(final Gauge gauge, final Session session) {
+        final NamedMainValue nmv = NamedMainValue.fetchByNameAndType("HSQ-II", MainValueTypeKey.UNKNOWN.getName(), session);
+        if (nmv == null)
+            return null;
+        final List<MainValue> mvs = gauge.getMainValues();
+        for (final MainValue mv : mvs) {
+            if (mv.getMainValue().getId() == nmv.getId())
+                return mv;
+        }
+        return null;
+    }
+
+    /**
      * Fetches a named main Q value from the database, if existing
      */
     private NamedMainValue fetchNamedQMainValue(final String name, final Session session) {
@@ -247,10 +270,16 @@
      * Gets the q from a list at a list position, or the next larger q if the immediate successors are equal
      */
     private double getDurationQ(final TDoubleArrayList qs, final int i) {
-        for (int j = i; j + 1 <= qs.size() - 1; j++)
-            if (qs.get(j + 1) > qs.get(j) + 0.001)
-                return qs.get(j);
-        // TODO Increment q on third significant digit
-        return qs.get(qs.size() - 1);
+        if (i == 0)
+            return qs.getQuick(0);
+        for (int j = i; j <= qs.size() - 1; j++) {
+            if (qs.getQuick(j) > qs.getQuick(j - 1) + 0.001)
+                return qs.getQuick(j);
+        }
+        // Identical values at end of list: increment q on third significant digit
+        final MathContext mc = new MathContext(3, RoundingMode.FLOOR);
+        BigDecimal qplus = BigDecimal.valueOf(qs.getQuick(qs.size() - 1));
+        qplus = qplus.round(mc).add(BigDecimal.ONE.scaleByPowerOfTen(-qplus.scale()));
+        return qplus.doubleValue();
     }
 }
\ No newline at end of file

http://dive4elements.wald.intevation.org