changeset 4087:1562a5fb36dc

Handle Nash-Sutcliff more tolerant for NaN/Inf values. Due to limitation of JSON we encode these values as 0 and +/- MaxFloat64.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Jul 2019 17:15:00 +0200
parents 419f28898db0
children d9c3eb32190f 8b98ac6a1de8 15ab4e474f11
files pkg/controllers/gauges.go
diffstat 1 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/gauges.go	Thu Jul 25 16:45:07 2019 +0200
+++ b/pkg/controllers/gauges.go	Thu Jul 25 17:15:00 2019 +0200
@@ -636,7 +636,7 @@
 			}
 		}
 
-		cs[i].Value = common.NashSutcliffe(predicted, observed)
+		cs[i].Value = sanitizeFloat64(common.NashSutcliffe(predicted, observed))
 		cs[i].Samples = len(predicted)
 
 		predicted = predicted[:0]
@@ -652,6 +652,18 @@
 	return
 }
 
+func sanitizeFloat64(x float64) float64 {
+	switch {
+	case math.IsNaN(x):
+		return 0
+	case math.IsInf(x, +1):
+		return math.MaxFloat64
+	case math.IsInf(x, -1):
+		return -math.MaxFloat64
+	}
+	return x
+}
+
 func waterlevels(rw http.ResponseWriter, req *http.Request) {
 	gauge := mux.Vars(req)["gauge"]