Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wflow ZMQ server: allow JSON reading and writing of NaN and Inf #513

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading