Mercurial > gemma
diff pkg/controllers/gauges.go @ 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 | 18777f6df3ef |
children | 472aedc8927d |
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"]