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

Check optional sections in TOML file #546

Merged
merged 5 commits into from
Feb 13, 2025
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
44 changes: 38 additions & 6 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,34 @@ end
"""
Config(path::AbstractString)
Config(dict::AbstractDict)
Config(dict::Dict{String,Any}, path::Union{String,Nothing})

Struct that contains the parsed TOML configuration, as well as a reference to the TOML path,
if it exists. It behaves largely like a distionary, but it overloads `getproperty` and
if it exists. It behaves largely like a dictionary, but it overloads `getproperty` and
`setproperty` to support syntax like `config.model.reinit = false`.
"""
struct Config
dict::Dict{String, Any} # nested key value mapping of all settings
path::Union{String, Nothing} # path to the TOML file, or nothing
end

Config(path::AbstractString) = Config(TOML.parsefile(path), path)
function Config(path::AbstractString)
config = Config(TOML.parsefile(path), path)
config = optional_keys(config)
check_config_states(config)
return config
end
Config(dict::AbstractDict) = Config(dict, nothing)

"Add optional TOML keys `logging` and `time` to `config` (if not present in TOML file)"
function optional_keys(config::Config)
if !haskey(config, "logging")
config.logging = Dict{String, Any}()
elseif !haskey(config, "time")
config.time = Dict{String, Any}()
end
return config
end

# allows using getproperty, e.g. config.input.time instead of config["input"]["time"]
function Base.getproperty(config::Config, f::Symbol)
dict = Dict(config)
Expand Down Expand Up @@ -950,8 +964,8 @@ function prepare_writer(
end

# create a separate state output netCDF that will hold the last timestep of all states
# but only if config.state.path_output and config.state.variables have been set
if check_config_states(config, "path_output")
# but only if config.state.path_output has been set
if haskey(config, "state") && haskey(config.state, "path_output")
state_ncnames = check_states(config)
state_map = out_map(state_ncnames, modelmap)
nc_state_path = output_path(config, config.state.path_output)
Expand Down Expand Up @@ -1645,7 +1659,7 @@ function get_index_dimension(var, config::Config, dim_value)::Int
return index
end

"Check state settings in `config` object (parsed TOML file)"
"Check if state TOML keys are set in `config` object (parsed TOML file)"
function check_config_states(config::Config, path::AbstractString)
state_settings =
haskey(config, "state") &&
Expand All @@ -1654,6 +1668,24 @@ function check_config_states(config::Config, path::AbstractString)
return state_settings
end

"""
Check if required state settings in `config` object (parsed TOML file) are set for reading
or writing states.
"""
function check_config_states(config::Config)
reinit = get(config.model, "reinit", true)::Bool
if !reinit
state_settings = check_config_states(config, "path_input")
state_settings ||
error("The state section for reading states in the TOML file is incomplete")
elseif haskey(config, "state") && haskey(config.state, "path_output")
state_settings = check_config_states(config, "path_output")
state_settings ||
error("The state section for writing states in the TOML file is incomplete")
end
return nothing
end

"""
Check output settings for file description (e.g. file format and data type), file path and
TOML `key`(e.g. containing variable name) in `config` object (parsed TOML file).
Expand Down
2 changes: 0 additions & 2 deletions src/sbm_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,6 @@ function set_states!(model::AbstractModel{<:Union{SbmModel, SbmGwfModel}})

# read and set states in model object if reinit=false
if reinit == false
state_settings = check_config_states(config, "path_input")
state_settings || error("The state section in the TOML file is incomplete")
nriv = length(network.river.indices)
instate_path = input_path(config, config.state.path_input)
@info "Set initial conditions from state file `$instate_path`."
Expand Down
2 changes: 0 additions & 2 deletions src/sediment_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ function set_states!(model::AbstractModel{<:SedimentModel})
(; config) = model
reinit = get(config.model, "reinit", true)::Bool
if reinit == false
state_settings = check_config_states(config, "path_input")
state_settings || error("The state section in the TOML file is incomplete")
instate_path = input_path(config, config.state.path_input)
@info "Set initial conditions from state file `$instate_path`."
set_states!(instate_path, model; type = Float)
Expand Down
3 changes: 3 additions & 0 deletions test/run_sbm_gwf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ end

Wflow.close_files(model; delete_output = false)

# test complete run including logging entry TOML file (not set)
Wflow.run(tomlpath; silent = true)

# test local-inertial option for river flow routing
tomlpath = joinpath(@__DIR__, "sbm_gwf_config.toml")
config = Wflow.Config(tomlpath)
Expand Down