Skip to content

Commit

Permalink
Wflow ZMQ server: allow JSON reading and writing of NaN and Inf (#…
Browse files Browse the repository at this point in the history
…513)

* Wflow server: allow JSON reading and writing of `NaN` and `Inf`
By setting `allow_inf` arg to `true`.

* Add small server test
To test if `NaN` values are allowed.

* Update changelog
  • Loading branch information
vers-w authored Dec 18, 2024
1 parent fbc2383 commit ecca90a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
`SimpleReservoir` and `Lake` struct, and not to the corresponding river index. This
resulted in incorrect surface water abstractions from reservoir and lake volumes, and
surface water abstractions were set at zero at the wrong river locations.
- Wflow ZMQ server: allow JSON reading and writing of `NaN` and `Inf` values to avoid a JSON
spec error. For example, during initialization of a wflow model, some (diagnostic) model
variables are initialized with `NaN` values.

### Changed
- Removed vertical concepts `HBV` and `FLEXTopo`.
Expand Down
4 changes: 2 additions & 2 deletions server/src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function wflow_bmi(s::ZMQ.Socket, handler::ModelHandler, f)
response(s)
else
@info "Send response including output from Wflow function `$(f.fn)`"
ZMQ.send(s, JSON3.write(ret))
ZMQ.send(s, JSON3.write(ret; allow_inf = true))
end
catch e
@error "Wflow function `$(f.fn)` failed" exception = (e, catch_backtrace())
Expand Down Expand Up @@ -151,7 +151,7 @@ function start(port::Int)
while true
# Wait for next request from client
req = ZMQ.recv(socket)
json = JSON3.read(req)
json = JSON3.read(req; allow_inf = true)
@info "Received request to run function `$(json.fn)`..."

if haskey(map_structs, json.fn)
Expand Down
8 changes: 7 additions & 1 deletion server/test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ZMQ.connect(socket, "tcp://localhost:5555")

function request(message)
ZMQ.send(socket, JSON3.write(message))
ret_value = JSON3.read(ZMQ.recv(socket), Dict)
ret_value = JSON3.read(ZMQ.recv(socket), Dict; allow_inf = true)
return ret_value
end

Expand All @@ -24,6 +24,12 @@ end
@test request((fn = "get_time_units",)) == Dict("time_units" => "s")
end

@testset "Reading and writing NaN values allowed" begin
msg =
(fn = "get_value", name = "vertical.soil.variables.vwc[1]", dest = fill(0.0, 50063))
@test isnan(mean(request(msg)["value"]))
end

@testset "update functions" begin
@test request((fn = "update_until", time = 86400.0)) == Dict("status" => "OK")
@test request((fn = "get_current_time",)) == Dict("current_time" => 86400)
Expand Down

0 comments on commit ecca90a

Please sign in to comment.