Skip to content

Commit e97795a

Browse files
authored
Merge pull request #155 from arbennett/simplify_config
Simplify config
2 parents 7f9022b + f313f89 commit e97795a

File tree

7 files changed

+36
-103
lines changed

7 files changed

+36
-103
lines changed

docs/configuration.rst

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,13 @@ __domain_vars__ for more details.
3232
``out_dir :: path``: The location to write output to. If this path doesn't
3333
exist, it will be created.
3434

35-
``out_state :: path/filename.nc``: The location to write state file to.
36-
3735
``forcing_fmt :: str``: A string representing the type of input files specified in
3836
the ``forcing`` entry. Can be one of the following: ``ascii``, ``binary``,
3937
``netcdf``, or ``data``.
4038

41-
``state_fmt :: str``: A string representing the type of state file specified in
42-
the ``state`` entry. Can be either ``netcdf`` or ``data``.
43-
44-
``domain_fmt :: str``: A string representing the type of state file specified in
45-
the ``domain`` entry. Can be either ``netcdf`` or ``data``.
46-
47-
``out_fmt:: str``: A string representing the type of output to write to
48-
``out_dir``. Can be either ``netcdf``, ``data``, or ``ascii``.
49-
50-
``method :: str``: A string representing the simulation methods to use. The
51-
current implementation only supports ``mtclim``.
52-
5339
**Optional Variables**
5440

55-
``out_prefix :: str``: The output file base name. Defaults to ``forcing``.
41+
``output_prefix :: str``: The output file base name. Defaults to ``forcing``.
5642

5743
``out_precision :: str``: Precision to use when writing output. Defaults to
5844
``f8``. Can be either ``f4`` or ``f8``.
@@ -63,19 +49,13 @@ memory. Each chunk of output is written as ``{out_prefix}_{date_range}`` when
6349
active. Any valid ``pandas.TimeGrouper`` string may be used (e.g. use '10AS'
6450
for 10 year chunks).
6551

66-
``iter_dims :: list``: The dimensions of input data to iterate over to
67-
accumulate sites. Defaults to ``['lat', 'lon']``.
68-
6952
``verbose :: bool``: Whether to print output to ``stdout``. Should be set using
7053
the ``-v`` flag for command line usage. This can be set for scripting purposes,
7154
if desired. Set to ``1`` to print output; defaults to ``0``.
7255

7356
``sw_prec_thresh :: float``: Minimum precipitation threshold to take into
7457
account when simulating incoming shortwave radiation. Defaults to ``0``.
7558

76-
``mtclim_swe_corr :: bool``: Whether to activate MtClim's SWE correction
77-
algorithm. Default to ``False``.
78-
7959
``utc_offset :: bool``: Whether to use UTC timecode offsets for shifting
8060
timeseries. Without this option all times should be considered local to
8161
the gridcell being processed. Large domain runs probably want to set this
@@ -96,12 +76,6 @@ dewpoint temperature in MtClim. Defaults to ``1e-6``.
9676
``tmax_daylength_fraction :: float`` : Weight for calculation of time of maximum
9777
daily temperature. Must be between ``0`` and ``1``. Defaults to ``0.67``.
9878

99-
``snow_crit_temp :: float``: Critical temperature for snow to melt. Defaults to
100-
``-6.0 C``.
101-
102-
``snow_melt_rate :: float``: Melt rate when temperature is less than
103-
``snow_crit_temp``. Defaults to ``0.042 cm/K``.
104-
10579
``rain_scalar :: float``: Scale factor for calculation of cloudy sky
10680
transmittance. Defaults to ``0.75``, range should be between ``0`` and
10781
``1``.

examples/example_ascii.conf

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This is an example of an input file for MetSim
22
[MetSim]
3+
out_vars = ['temp', 'prec', 'shortwave', 'longwave', 'vapor_pressure', 'rel_humid', 'air_pressure', 'wind']
34

45
# Time step in minutes
56
time_step = 60
@@ -15,15 +16,10 @@ forcing = ./metsim/data/ascii
1516
domain = ./metsim/data/stehekin.nc
1617
state = ./metsim/data/state_vic.nc
1718
forcing_fmt = ascii
18-
domain_fmt = netcdf
19-
state_fmt = netcdf
20-
out_fmt = netcdf
2119
out_dir = ./results
22-
out_state = ./results/state.nc
2320

2421
# How to disaggregate
2522
method = mtclim
26-
prec_type = uniform
2723

2824
[chunks]
2925
lat = 10
@@ -40,7 +36,6 @@ wind = wind
4036
prec = prec
4137
t_max = t_max
4238
t_min = t_min
43-
swe = swe
4439

4540
[domain_vars]
4641
lat = lat

examples/example_bin.conf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This is an example of an input file for MetSim
22
[MetSim]
3+
out_vars = ['temp', 'prec', 'shortwave', 'longwave', 'vapor_pressure', 'rel_humid', 'air_pressure', 'wind']
34

45
# Time step in minutes
56
time_step = 30
@@ -14,15 +15,7 @@ forcing = ./metsim/data/binary
1415
domain = ./metsim/data/stehekin.nc
1516
state = ./metsim/data/state_vic.nc
1617
forcing_fmt = binary
17-
domain_fmt = netcdf
18-
state_fmt = netcdf
19-
2018
out_dir = ./results
21-
out_fmt = netcdf
22-
23-
# How to disaggregate
24-
method = mtclim
25-
prec_type = uniform
2619

2720
[chunks]
2821
lat = 10
@@ -39,7 +32,6 @@ wind = 100.0 signed
3932
prec = prec
4033
t_max = t_max
4134
t_min = t_min
42-
swe = swe
4335

4436
[domain_vars]
4537
lat = lat

examples/example_nc.conf

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This is an example of an input file for MetSim
22
[MetSim]
3+
out_vars = ['temp', 'prec', 'shortwave', 'longwave', 'vapor_pressure', 'rel_humid', 'air_pressure', 'wind']
34

45
# Time step in minutes
56
time_step = 30
@@ -14,17 +15,13 @@ stop = 1950/1/31
1415
forcing = ./metsim/data/test.nc
1516
domain = ./metsim/data/domain.nc
1617
state = ./metsim/data/state_nc.nc
18+
1719
forcing_fmt = netcdf
18-
domain_fmt = netcdf
19-
state_fmt = netcdf
20+
in_format = netcdf
21+
2022
out_dir = ./results
2123
out_prefix = forcing
22-
in_format = netcdf
23-
out_fmt = netcdf
24-
out_state = ./results/state.nc
2524

26-
# How to disaggregate
27-
method = mtclim
2825
prec_type = triangle
2926
utc_offset = True
3027

@@ -33,21 +30,20 @@ lat = 3
3330
lon = 3
3431

3532
[forcing_vars]
36-
Prec = prec
37-
Tmax = t_max
38-
Tmin = t_min
39-
wind = wind
33+
prec = Prec
34+
t_max = Tmax
35+
t_min = Tmin
36+
wind = wind
4037

4138
[state_vars]
42-
prec = prec
39+
prec = prec
4340
t_max = t_max
4441
t_min = t_min
45-
swe = swe
4642

4743
[domain_vars]
48-
lat = lat
49-
lon = lon
44+
lat = lat
45+
lon = lon
5046
mask = mask
5147
elev = elev
5248
t_pk = t_pk
53-
dur = dur
49+
dur = dur

metsim/cli/ms.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,19 @@ def init(opts):
5656
config.optionxform = str
5757
config.read(opts.config)
5858
conf = OrderedDict(config['MetSim'])
59-
conf['forcing_vars'] = OrderedDict(config['forcing_vars'])
60-
conf['domain_vars'] = OrderedDict(config['domain_vars'])
61-
conf['state_vars'] = OrderedDict(config['state_vars'])
62-
conf['chunks'] = OrderedDict(config['chunks'])
63-
out_dir = os.path.abspath(conf['out_dir'])
64-
out_state = conf.get('out_state', None)
65-
if out_state is None:
66-
out_state = os.path.join(out_dir, 'state.nc')
6759

68-
method = conf['method']
60+
def invert_dict(d):
61+
return OrderedDict({v: k for k, v in d.items()})
6962

70-
prec_type = conf.get('prec_type', None)
71-
if prec_type is None:
72-
prec_type = 'uniform'
63+
def to_list(s):
64+
return json.loads(s.replace("'", '"'))
65+
66+
conf['forcing_vars'] = OrderedDict(config['forcing_vars'])
67+
if conf['forcing_fmt'] != 'binary':
68+
conf['forcing_vars'] = invert_dict(conf['forcing_vars'])
69+
conf['domain_vars'] = invert_dict(OrderedDict(config['domain_vars']))
70+
conf['state_vars'] = invert_dict(OrderedDict(config['state_vars']))
71+
conf['chunks'] = OrderedDict(config['chunks'])
7372

7473
# If the forcing variable is a directory, scan it for files
7574
if os.path.isdir(conf['forcing']):
@@ -78,26 +77,14 @@ def init(opts):
7877
else:
7978
forcing_files = conf['forcing']
8079

81-
# We assume there is only one domain file and one state file
82-
domain_file = conf['domain']
83-
state_file = conf['state']
84-
chunks = conf['chunks']
85-
86-
def to_list(s):
87-
return json.loads(s.replace("'", '"'))
88-
80+
# Update the full configuration
8981
conf.update({"calendar": conf.get('calendar', 'standard'),
9082
"scheduler": opts.scheduler,
9183
"num_workers": opts.num_workers,
92-
"method": method,
93-
"out_dir": out_dir,
94-
"out_state": out_state,
95-
"state": state_file,
96-
"domain": domain_file,
97-
"forcing": forcing_files,
98-
"chunks": chunks,
9984
"verbose": logging.DEBUG if opts.verbose else logging.INFO,
100-
"prec_type": prec_type})
85+
"forcing": forcing_files,
86+
"out_dir": os.path.abspath(conf['out_dir']),
87+
"prec_type": conf.get('prec_type', 'uniform')})
10188
conf['out_vars'] = to_list(conf.get('out_vars', '[]'))
10289
conf['iter_dims'] = to_list(conf.get('iter_dims', '["lat", "lon"]'))
10390
conf = {k: v for k, v in conf.items() if v != []}

metsim/io.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,14 @@ def read_met_data(params: dict, domain: xr.Dataset) -> xr.Dataset:
4848

4949
def read_domain(params: dict) -> xr.Dataset:
5050
"""Load in a domain file"""
51-
read_funcs = {
52-
"netcdf": read_netcdf,
53-
"data": read_data
54-
}
55-
return read_funcs[params['domain_fmt']](
51+
return read_netcdf(
5652
params['domain'], calendar=params['calendar'],
5753
var_dict=params.get('domain_vars', None))
5854

5955

6056
def read_state(params: dict, domain: xr.Dataset) -> xr.Dataset:
6157
"""Load in a state file"""
62-
read_funcs = {
63-
"netcdf": read_netcdf,
64-
"data": read_data
65-
}
66-
67-
return read_funcs[params['state_fmt']](
58+
return read_netcdf(
6859
params['state'], domain=domain,
6960
start=params['state_start'], stop=params['state_stop'],
7061
calendar=params['calendar'],

metsim/metsim.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,16 @@ class MetSim(object):
112112
# Class variables
113113
methods = {'mtclim': mtclim}
114114
params = {
115-
"method": '',
115+
"method": 'mtclim',
116116
"domain": '',
117117
"state": '',
118118
"out_dir": '',
119-
"out_prefix": 'forcing',
119+
"output_prefix": 'forcing',
120120
"start": 'forcing',
121121
"stop": 'forcing',
122122
"time_step": -1,
123123
"calendar": 'standard',
124124
"prec_type": 'uniform',
125-
"out_fmt": '',
126125
"out_precision": 'f4',
127126
"verbose": 0,
128127
"sw_prec_thresh": 0.0,
@@ -445,7 +444,7 @@ def _get_output_times(self, freq=None):
445444

446445
def _get_output_filename(self, times):
447446
suffix = self.get_nc_output_suffix(times)
448-
fname = '{}_{}.nc'.format(self.params['out_prefix'], suffix)
447+
fname = '{}_{}.nc'.format(self.params['output_prefix'], suffix)
449448
output_filename = os.path.join(
450449
os.path.abspath(self.params['out_dir']), fname)
451450
return output_filename
@@ -531,8 +530,7 @@ def _validate_setup(self):
531530
errs.append("Requires input forcings to be specified")
532531

533532
# Parameters that can't be empty strings or None
534-
non_empty = ['method', 'out_dir', 'time_step',
535-
'forcing_fmt', 'domain_fmt', 'state_fmt']
533+
non_empty = ['out_dir', 'time_step', 'forcing_fmt']
536534
for each in non_empty:
537535
if self.params.get(each, None) is None or self.params[each] == '':
538536
errs.append("Cannot have empty value for {}".format(each))

0 commit comments

Comments
 (0)