-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
315 lines (292 loc) · 9.05 KB
/
train.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""Command line program for calculating QQ-scaling adjustment factors."""
import argparse
import logging
import xclim as xc
from xclim import sdba
import dask.diagnostics
import utils
def train(
ds_hist,
ds_ref,
hist_var,
ref_var,
scaling,
time_grouping=None,
nquantiles=100,
spatial_grid='hist',
ssr=False
):
"""Calculate qq-scaling adjustment factors.
Parameters
----------
ds_hist : xarray Dataset
Historical data
ds_ref : xarray Dataset
Reference data
hist_var : str
Historical variable (i.e. in ds_hist)
ref_var : str
Reference variable (i.e. in ds_ref)
scaling : {'additive', 'multiplicative'}
Scaling method
time_grouping : {'monthly', '3monthly'} default None
Time period grouping (default is no grouping)
nquantiles : int, default 100
Number of quantiles to process
spatial_grid : {'hist', 'ref'}, default 'hist'
Spatial grid for output data (hist or ref grid)
ssr : bool, default False
Perform singularity stochastic removal
Returns
-------
xarray Dataset
"""
hist_units = ds_hist[hist_var].attrs['units']
ref_units = ds_ref[ref_var].attrs['units']
dims = ds_hist[hist_var].dims
on_spatial_grid = ('lat' in dims) and ('lon' in dims)
if on_spatial_grid:
if len(ds_hist['lat']) != len(ds_ref['lat']):
if spatial_grid == 'ref':
ds_hist = utils.regrid(ds_hist, ds_ref, variable=hist_var)
logging.info('Regridding hist data to ref grid')
spatial_coords = {'lat': ds_ref['lat'], 'lon': ds_ref['lon']}
elif spatial_grid == 'hist':
ds_ref = utils.regrid(ds_ref, ds_hist, variable=ref_var)
logging.info('Regridding ref data to hist grid')
spatial_coords = {'lat': ds_hist['lat'], 'lon': ds_hist['lon']}
else:
spatial_coords = {'lat': ds_ref['lat'], 'lon': ds_ref['lon']}
assert len(ds_hist['lat']) == len(ds_ref['lat'])
assert len(ds_hist['lon']) == len(ds_ref['lon'])
scaling_methods = {'additive': '+', 'multiplicative': '*'}
if time_grouping == 'monthly':
group = 'time.month'
elif time_grouping == '3monthly':
group = sdba.Grouper('time.month', window=3)
else:
group = 'time'
if ssr:
da_ref = utils.apply_ssr(ds_ref[ref_var])
da_hist = utils.apply_ssr(ds_hist[hist_var])
else:
da_ref = ds_ref[ref_var]
da_hist = ds_hist[hist_var]
qm = sdba.QuantileDeltaMapping.train(
da_ref,
da_hist,
nquantiles=nquantiles,
group=group,
kind=scaling_methods[scaling]
)
qm.ds = qm.ds.squeeze()
try:
qm.ds = qm.ds.drop_vars('group')
except ValueError:
pass
qm.ds['hist_q'].attrs['units'] = hist_units
if on_spatial_grid:
qm.ds = qm.ds.assign_coords(spatial_coords) #xclim strips lat/lon attributes
qm.ds = qm.ds.transpose('lat', 'lon', ...)
if 'month' in qm.ds.dims:
qm.ds = qm.ds.transpose('month', ...)
qm.ds = qm.ds.transpose('quantiles', ...)
hist_times = ds_hist['time'].dt.strftime('%Y-%m-%d').values
qm.ds.attrs['historical_period_start'] = hist_times[0]
qm.ds.attrs['historical_period_end'] = hist_times[-1]
ref_times = ds_ref['time'].dt.strftime('%Y-%m-%d').values
qm.ds.attrs['reference_period_start'] = ref_times[0]
qm.ds.attrs['reference_period_end'] = ref_times[-1]
qm.ds.attrs['xclim_version'] = xc.__version__
return qm.ds
def main(args):
"""Run the program."""
dask.diagnostics.ProgressBar().register()
ds_hist = utils.read_data(
args.hist_files,
args.hist_var,
time_bounds=args.hist_time_bounds,
input_units=args.input_hist_units,
output_units=args.output_units,
valid_min=args.valid_min,
valid_max=args.valid_max,
)
calendar_hist = type(ds_hist['time'].values[0])
ds_ref = utils.read_data(
args.ref_files,
args.ref_var,
time_bounds=args.ref_time_bounds,
lat_bounds=args.lat_bounds,
lon_bounds=args.lon_bounds,
input_units=args.input_ref_units,
output_units=args.output_units,
output_calendar=calendar_hist,
valid_min=args.valid_min,
valid_max=args.valid_max,
)
ds_out = train(
ds_hist,
ds_ref,
args.hist_var,
args.ref_var,
args.scaling,
time_grouping=args.time_grouping,
nquantiles=args.nquantiles,
spatial_grid=args.spatial_grid,
ssr=args.ssr,
)
if args.short_history:
unique_dirnames = utils.get_unique_dirnames(args.hist_files + args.ref_files)
else:
unique_dirnames = []
ds_out.attrs['history'] = utils.get_new_log(wildcard_prefixes=unique_dirnames)
encoding = {}
outfile_vars = list(ds_out.coords) + list(ds_out.keys())
for outfile_var in outfile_vars:
encoding[outfile_var] = {'_FillValue': None}
if args.compress:
encoding['af']['least_significant_digit'] = 2
encoding['af']['zlib'] = True
encoding['hist_q']['least_significant_digit'] = 2
encoding['hist_q']['zlib'] = True
ds_out.to_netcdf(args.output_file, encoding=encoding)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
argument_default=argparse.SUPPRESS,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("hist_var", type=str, help="historical variable to process")
parser.add_argument("ref_var", type=str, help="reference variable to process")
parser.add_argument("output_file", type=str, help="output file")
parser.add_argument(
"--hist_files",
type=str,
nargs='*',
required=True,
help="historical data files"
)
parser.add_argument(
"--ref_files",
type=str,
nargs='*',
required=True,
help="reference data files"
)
parser.add_argument(
"--hist_time_bounds",
type=str,
nargs=2,
metavar=('START_DATE', 'END_DATE'),
required=True,
help="historical time bounds in YYYY-MM-DD format"
)
parser.add_argument(
"--ref_time_bounds",
type=str,
nargs=2,
metavar=('START_DATE', 'END_DATE'),
required=True,
help="reference time bounds in YYYY-MM-DD format"
)
parser.add_argument(
"--lat_bounds",
type=float,
nargs=2,
default=None,
help='Latitude bounds for reference data: (south_bound, north_bound)',
)
parser.add_argument(
"--lon_bounds",
type=float,
nargs=2,
default=None,
help='Longitude bounds for reference data: (west_bound, east_bound)',
)
parser.add_argument(
"--nquantiles",
type=int,
default=100,
help="Number of quantiles to process",
)
parser.add_argument(
"--scaling",
type=str,
choices=('additive', 'multiplicative'),
default='additive',
help="scaling method",
)
parser.add_argument(
"--time_grouping",
type=str,
choices=('monthly', '3monthly'),
default=None,
help="Time period grouping",
)
parser.add_argument(
"--spatial_grid",
type=str,
choices=('hist', 'ref'),
default='hist',
help="Spatial grid for output data (hist or ref grid)",
)
parser.add_argument(
"--input_hist_units",
type=str,
default=None,
help="input historical data units"
)
parser.add_argument(
"--input_ref_units",
type=str,
default=None,
help="input reference data units"
)
parser.add_argument(
"--output_units",
type=str,
default=None,
help="output data units"
)
parser.add_argument(
"--valid_min",
type=float,
default=None,
help="Minimum valid value",
)
parser.add_argument(
"--valid_max",
type=float,
default=None,
help="Maximum valid value",
)
parser.add_argument(
"--ssr",
action="store_true",
default=False,
help='Apply Singularity Stochastic Removal to input data',
)
parser.add_argument(
"--verbose",
action="store_true",
default=False,
help='Set logging level to INFO',
)
parser.add_argument(
"--compress",
action="store_true",
default=False,
help="compress the output data file"
)
parser.add_argument(
"--short_history",
action='store_true',
default=False,
help="Use wildcards to shorten the file lists in output_file history attribute",
)
args = parser.parse_args()
log_level = logging.INFO if args.verbose else logging.WARNING
logging.basicConfig(level=log_level)
with dask.diagnostics.ResourceProfiler() as rprof:
main(args)
utils.profiling_stats(rprof)