27
27
"""
28
28
Config(path::AbstractString)
29
29
Config(dict::AbstractDict)
30
- Config(dict::Dict{String,Any}, path::Union{String,Nothing})
31
30
32
31
Struct that contains the parsed TOML configuration, as well as a reference to the TOML path,
33
- if it exists. It behaves largely like a distionary , but it overloads `getproperty` and
32
+ if it exists. It behaves largely like a dictionary , but it overloads `getproperty` and
34
33
`setproperty` to support syntax like `config.model.reinit = false`.
35
34
"""
36
35
struct Config
37
36
dict:: Dict{String, Any} # nested key value mapping of all settings
38
37
path:: Union{String, Nothing} # path to the TOML file, or nothing
39
38
end
40
39
41
- Config (path:: AbstractString ) = Config (TOML. parsefile (path), path)
40
+ function Config (path:: AbstractString )
41
+ config = Config (TOML. parsefile (path), path)
42
+ config = optional_keys (config)
43
+ check_config_states (config)
44
+ return config
45
+ end
42
46
Config (dict:: AbstractDict ) = Config (dict, nothing )
43
47
48
+ " Add optional TOML keys `logging` and `time` to `config` (if not present in TOML file)"
49
+ function optional_keys (config:: Config )
50
+ if ! haskey (config, " logging" )
51
+ config. logging = Dict {String, Any} ()
52
+ elseif ! haskey (config, " time" )
53
+ config. time = Dict {String, Any} ()
54
+ end
55
+ return config
56
+ end
57
+
44
58
# allows using getproperty, e.g. config.input.time instead of config["input"]["time"]
45
59
function Base. getproperty (config:: Config , f:: Symbol )
46
60
dict = Dict (config)
@@ -950,8 +964,8 @@ function prepare_writer(
950
964
end
951
965
952
966
# create a separate state output netCDF that will hold the last timestep of all states
953
- # but only if config.state.path_output and config.state.variables have been set
954
- if check_config_states (config, " path_output" )
967
+ # but only if config.state.path_output has been set
968
+ if haskey (config, " state " ) && haskey (config . state , " path_output" )
955
969
state_ncnames = check_states (config)
956
970
state_map = out_map (state_ncnames, modelmap)
957
971
nc_state_path = output_path (config, config. state. path_output)
@@ -1645,7 +1659,7 @@ function get_index_dimension(var, config::Config, dim_value)::Int
1645
1659
return index
1646
1660
end
1647
1661
1648
- " Check state settings in `config` object (parsed TOML file)"
1662
+ " Check if state TOML keys are set in `config` object (parsed TOML file)"
1649
1663
function check_config_states (config:: Config , path:: AbstractString )
1650
1664
state_settings =
1651
1665
haskey (config, " state" ) &&
@@ -1654,6 +1668,24 @@ function check_config_states(config::Config, path::AbstractString)
1654
1668
return state_settings
1655
1669
end
1656
1670
1671
+ """
1672
+ Check if required state settings in `config` object (parsed TOML file) are set for reading
1673
+ or writing states.
1674
+ """
1675
+ function check_config_states (config:: Config )
1676
+ reinit = get (config. model, " reinit" , true ):: Bool
1677
+ if ! reinit
1678
+ state_settings = check_config_states (config, " path_input" )
1679
+ state_settings ||
1680
+ error (" The state section for reading states in the TOML file is incomplete" )
1681
+ elseif haskey (config, " state" ) && haskey (config. state, " path_output" )
1682
+ state_settings = check_config_states (config, " path_output" )
1683
+ state_settings ||
1684
+ error (" The state section for writing states in the TOML file is incomplete" )
1685
+ end
1686
+ return nothing
1687
+ end
1688
+
1657
1689
"""
1658
1690
Check output settings for file description (e.g. file format and data type), file path and
1659
1691
TOML `key`(e.g. containing variable name) in `config` object (parsed TOML file).
0 commit comments