-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli_options.jl
263 lines (246 loc) · 9.36 KB
/
cli_options.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import ArgParse
function argparse_settings()
s = ArgParse.ArgParseSettings()
ArgParse.@add_arg_table! s begin
# ClimaCoupler flags
"--run_name"
help = "Name of this run."
arg_type = String
"--dt_cpl"
help = " Coupling time step in seconds"
arg_type = Int
default = 400
"--anim"
help = "Boolean flag indicating whether to make animations"
arg_type = Bool
default = false
"--energy_check"
help = "Boolean flag indicating whether to check energy conservation"
arg_type = Bool
default = false
"--ci_plots"
help = "Boolean flag indicating whether to make CI plots"
arg_type = Bool
default = false
"--conservation_softfail"
help = "Boolean flag indicating whether to soft fail on conservation errors"
arg_type = Bool
default = false
"--mode_name"
help = "Mode of coupled simulation. [`amip`, `slabplanet`, `slabplanet_aqua`, `slabplanet_terra`, `slabplanet_eisenman`]"
arg_type = String
default = "amip"
"--mono_surface"
help = "Boolean flag indicating whether (1st order) monotone and conservative remapping is applied."
arg_type = Bool
default = false
"--turb_flux_partition"
help = "Method to partition turbulent fluxes. [`PartitionedStateFluxes`, `CombinedStateFluxes`]"
arg_type = String
default = "CombinedStateFluxes"
"--hourly_checkpoint"
help = "Boolean flag indicating whether to checkpoint at intervals of 1 hour or multiple hours"
arg_type = Bool
default = false
"--hourly_checkpoint_dt"
help = "Time interval for hourly checkpointing in hours (20 days by default)"
arg_type = Int
default = 480
"--coupler_output_dir"
help = "Directory to save output files. Note that TempestRemap fails if interactive and paths are too long."
arg_type = String
default = "experiments/AMIP/output"
"--restart_dir"
help = "Directory containing restart files"
arg_type = String
default = "unspecified"
"--restart_t"
help = "Restart time"
arg_type = Int
default = 0
"--config_file"
help = "A yaml file used to set the configuration of the coupled model"
"--print_config_dict"
help = "Boolean flag indicating whether to print the final configuration dictionary"
arg_type = Bool
default = true
"--FLOAT_TYPE"
help = "Floating point precision [`Float64` (default), `Float32`]"
arg_type = String
default = "Float64"
"--coupler_toml_file"
help = "A toml file used to overwrite the model parameters. If nothing is specified, the default parameters are used."
"--evolving_ocean"
help = "Boolean flag indicating whether to use a dynamic slab ocean model or constant surface temperatures"
arg_type = Bool
default = true
"--device"
help = "Device type to use [`auto` (default) `CPUSingleThreaded`, `CPUMultiThreaded`, `CUDADevice`]"
arg_type = String
default = "auto"
# ClimaAtmos specific
"--surface_setup"
help = "Triggers ClimaAtmos into the coupled mode [`PrescribedSurface` (default)]" # retained here for standalone Atmos benchmarks
arg_type = String
default = "PrescribedSurface"
"--atmos_config_file"
help = "A yaml file used to set the atmospheric model configuration. If nothing is specified, the default configuration is used."
"--albedo_model"
help = "Type of albedo model. [`ConstantAlbedo` (default), `RegressionFunctionAlbedo`, `CouplerAlbedo`]"
arg_type = String
default = "CouplerAlbedo"
# ClimaLand specific
"--land_albedo_type"
help = "Access land surface albedo information from data file. [`function`, `map_static`, `map_temporal`]"
arg_type = String
default = "map_static" # to be replaced by land config file, when available
"--land_domain_type"
help = "Type of land domain. [`sphere` (default), `single_column`]"
arg_type = String
default = "sphere"
"--land_temperature_anomaly"
help = "Type of temperature anomaly for bucket model. [`amip`, `aquaplanet` (default)]"
arg_type = String
default = "aquaplanet"
end
return s
end
parse_commandline(s) = ArgParse.parse_args(ARGS, s)
function cli_defaults(s::ArgParse.ArgParseSettings)
defaults = Dict()
# TODO: Don't use ArgParse internals
for arg in s.args_table.fields
defaults[arg.dest_name] = arg.default
end
return defaults
end
"""
job_id_from_parsed_args(
s::ArgParseSettings,
parsed_args = ArgParse.parse_args(ARGS, s)
)
Returns a unique name (`String`) given
- `s::ArgParse.ArgParseSettings` The arg parse settings
- `parsed_args` The parse arguments
The `ArgParseSettings` are used for truncating
this string based on the default values.
"""
job_id_from_parsed_args(s, parsed_args = ArgParse.parse_args(ARGS, s)) =
job_id_from_parsed_args(cli_defaults(s), parsed_args)
function job_id_from_parsed_args(defaults::Dict, parsed_args)
_parsed_args = deepcopy(parsed_args)
s = ""
warn = false
for k in keys(_parsed_args)
# Skip defaults to alleviate verbose names
!haskey(defaults, k) && continue
defaults[k] == _parsed_args[k] && continue
if _parsed_args[k] isa String
# We don't need keys if the value is a string
# (alleviate verbose names)
s *= _parsed_args[k]
elseif _parsed_args[k] isa Int
s *= k * "_" * string(_parsed_args[k])
elseif _parsed_args[k] isa AbstractFloat
warn = true
else
s *= k * "_" * string(_parsed_args[k])
end
s *= "_"
end
s = replace(s, "/" => "_")
s = strip(s, '_')
warn && @warn "Truncated job ID:$s may not be unique due to use of Real"
return s
end
"""
print_repl_script(str::String)
Generate a block of code to run a particular
buildkite job given the `command:` string.
Example:
"""
function print_repl_script(str)
ib = """"""
ib *= """\n"""
ib *= """using Revise; include("src/utils/cli_options.jl");\n"""
ib *= """\n"""
ib *= """parsed_args = parse_commandline(argparse_settings());\n"""
parsed_args = parsed_args_from_command_line_flags(str)
for (flag, val) in parsed_args
if val isa AbstractString
ib *= "parsed_args[\"$flag\"] = \"$val\";\n"
else
ib *= "parsed_args[\"$flag\"] = $val;\n"
end
end
ib *= """\n"""
ib *= """include("examples/hybrid/driver.jl")\n"""
println(ib)
end
parsed_args_from_ARGS(ARGS, parsed_args = Dict()) = parsed_args_from_ARGS_string(strip(join(ARGS, " ")), parsed_args)
parsed_args_from_command_line_flags(str, parsed_args = Dict()) =
parsed_args_from_ARGS_string(strip(last(split(str, ".jl"))), parsed_args)
function parsed_args_from_ARGS_string(str, parsed_args = Dict())
str = replace(str, " " => " ", " " => " ", " " => " ")
parsed_args_list = split(str, " ")
parsed_args_list == [""] && return parsed_args
@assert iseven(length(parsed_args_list))
parsed_arg_pairs = map(1:2:(length(parsed_args_list) - 1)) do i
Pair(parsed_args_list[i], strip(parsed_args_list[i + 1], '\"'))
end
function parse_arg(val)
for T in (Bool, Int, Float32, Float64)
try
return parse(T, val)
catch
end
end
return String(val) # string
end
for (flag, val) in parsed_arg_pairs
parsed_args[replace(flag, "--" => "")] = parse_arg(val)
end
return parsed_args
end
"""
parsed_args_per_job_id()
parsed_args_per_job_id(buildkite_yaml)
A dict of `parsed_args` to run the ClimaAtmos driver
whose keys are the `job_id`s from buildkite yaml.
# Example
To run the `sphere_aquaplanet_rhoe_equilmoist_allsky`
buildkite job from the standard buildkite pipeline, use:
```
using Revise; include("examples/hybrid/cli_options.jl");
dict = parsed_args_per_job_id();
parsed_args = dict["sphere_aquaplanet_rhoe_equilmoist_allsky"];
include("examples/hybrid/driver.jl")
```
"""
function parsed_args_per_job_id(; trigger = "driver.jl")
cc_dir = joinpath(@__DIR__, "..", "..", "..")
buildkite_yaml = joinpath(cc_dir, ".buildkite", "pipeline.yml")
parsed_args_per_job_id(buildkite_yaml; trigger)
end
function parsed_args_per_job_id(buildkite_yaml; trigger = "driver.jl")
buildkite_commands = readlines(buildkite_yaml)
filter!(x -> occursin(trigger, x), buildkite_commands)
@assert length(buildkite_commands) > 0 # sanity check
result = Dict()
for bkcs in buildkite_commands
default_parsed_args = parse_commandline(argparse_settings())
job_id = first(split(last(split(bkcs, "--run_name ")), " "))
job_id = strip(job_id, '\"')
result[job_id] = parsed_args_from_command_line_flags(bkcs, default_parsed_args)
end
return result
end
function non_default_command_line_flags_parsed_args(parsed_args)
default_parsed_args = parse_commandline(argparse_settings())
s = ""
for k in keys(parsed_args)
default_parsed_args[k] == parsed_args[k] && continue
s *= "--$k $(parsed_args[k]) "
end
return rstrip(s)
end