-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
268 lines (226 loc) · 7.48 KB
/
preprocess.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
"""Command line program for ACS bias correction data pre-processing."""
import argparse
import numpy as np
import xarray as xr
import xcdat
import xclim
import cmdline_provenance as cmdprov
output_units = {
'tasmax': 'degC',
'tasmin': 'degC',
'pr': 'mm d-1',
'sfcWindmax': 'm s-1',
'rsds': 'W m-2',
'hursmin': '%',
'hursmax': '%',
}
def convert_units(da, target_units):
"""Convert units.
Parameters
----------
da : xarray DataArray
Input array containing a units attribute
target_units : str
Units to convert to
Returns
-------
da : xarray DataArray
Array with converted units
"""
xclim_unit_check = {
'degrees_Celsius': 'degC',
'deg_k': 'degK',
'kg/m2/s': 'kg m-2 s-1',
'mm': 'mm d-1',
}
if da.attrs["units"] in xclim_unit_check:
da.attrs["units"] = xclim_unit_check[da.units]
try:
with xr.set_options(keep_attrs=True):
da = xclim.units.convert_units_to(da, target_units)
except Exception as e:
if (da.attrs['units'] == 'kg m-2 s-1') and (target_units in ['mm d-1', 'mm day-1']):
da = da * 86400
da.attrs["units"] = target_units
elif (da.attrs['units'] == 'MJ m^-2') and target_units == 'W m-2':
da = da * (1e6 / 86400)
da.attrs["units"] = target_units
else:
raise e
if target_units == 'degC':
da.attrs['units'] = 'degC'
return da
def fix_metadata(ds, var):
"Apply metadata fixes."""
# Remove xcdat attributes
del ds['lat_bnds'].attrs['xcdat_bounds']
del ds['lon_bnds'].attrs['xcdat_bounds']
try:
del ds['time_bnds'].attrs['xcdat_bounds']
except KeyError:
pass
# Rename input attributes
for attr in ['tracking_id', 'doi']:
if attr in ds.attrs:
ds.attrs['input_' + attr] = ds.attrs[attr]
# Delete global attributes
global_keys_to_delete = [
'tracking_id',
'doi',
'axiom_version',
'axiom_schemas_version',
'axiom_schema',
'productive_version',
'processing_level',
'geospatial_lat_min',
'geospatial_lat_max',
'geospatial_lat_units',
'geospatial_lon_min',
'geospatial_lon_max',
'geospatial_lon_units',
'date_modified',
'date_metadata_modified',
'creation_date',
'time_coverage_start',
'time_coverage_end',
'time_coverage_duration',
'time_coverage_resolution',
'git_url_postprocessing',
'git_hash_postprocessing',
'grid',
'project_id',
'comment',
'DPIE_WRF_HASH',
'wrf_options',
'contact',
'product',
'nest_level',
'nest_parent',
'wrf_schemes_ra_sw_physics',
'wrf_schemes_ra_lw_physics',
'wrf_schemes_sf_sfclay_physics',
'wrf_schemes_sf_surface_physics',
'wrf_schemes_mp_physics',
'wrf_schemes_bl_pbl_physics',
'wrf_schemes_cu_physics',
'references',
'NCO',
]
for key in global_keys_to_delete:
try:
del ds.attrs[key]
except KeyError:
pass
# Delete variable attributes
var_keys_to_delete = [
'cell_methods',
'valid_range',
'grid_mapping',
'MD5',
]
for key in var_keys_to_delete:
try:
del ds[var].attrs[key]
except KeyError:
pass
# Add/update variable attributes
ds['lat'].attrs['long_name'] = 'latitude'
ds['lat'].attrs['standard_name'] = 'latitude'
ds['lon'].attrs['long_name'] = 'longitude'
ds['lon'].attrs['standard_name'] = 'longitude'
ds['time'].attrs['long_name'] = 'time'
ds['time'].attrs['standard_name'] = 'time'
ds['time'].attrs['axis'] = 'T'
# Add/update global attributes
ds.attrs['domain'] = 'Australia/AGCD'
ds.attrs['domain_id'] = 'AGCD-05i'
ds.attrs['title'] = 'Pre-processed model output in preparation for bias adjustment'
ds.attrs['frequency'] = 'day'
ds.attrs['variable_id'] = var
# Variables to delete
drop_vars = ['sigma', 'level_height', 'model_level_number', 'height']
for drop_var in drop_vars:
try:
ds = ds.drop(drop_var)
except ValueError:
pass
try:
del ds['time_bnds'].encoding['coordinates']
except KeyError:
pass
return ds
def get_output_encoding(ds, var, nlats, nlons):
"""Define the output file encoding."""
var_shape = ds[var].shape
assert len(var_shape) == 3
assert var_shape[1] == nlats
assert var_shape[2] == nlons
encoding = {}
ds_vars = list(ds.coords) + list(ds.keys())
for ds_var in ds_vars:
encoding[ds_var] = {'_FillValue': None}
encoding[var]['zlib'] = True
encoding[var]['dtype'] = 'float32'
encoding[var]['chunksizes'] = (1, nlats, nlons)
encoding['time']['units'] = 'days since 1950-01-10'
return encoding
def replace_rlon(ds, rlon_file):
"""Replace rlon values (needed for NARCliM2.0)"""
rlon_values = []
with open(rlon_file, 'r') as file:
for line in file.readlines():
rlon_values.append(float(line))
rlon_attrs = ds['rlon'].attrs
ds['rlon'] = rlon_values
ds['rlon'].attrs = rlon_attrs
ds = ds.drop_vars('rlon_bnds')
ds = ds.bounds.add_missing_bounds()
return ds
def main(args):
"""Run the program."""
input_ds = xcdat.open_mfdataset(args.infiles, mask_and_scale=True)
if args.rlon:
input_ds = replace_rlon(input_ds, args.rlon)
# Temporal aggregation
if args.var == 'hursmax':
input_ds = input_ds.resample(time='D').max('time', keep_attrs=True)
input_ds = input_ds.rename({'hurs': 'hursmax'})
input_ds['hursmax'].attrs['long_name'] = 'Daily Maximum Near-Surface Relative Humidity'
elif args.var == 'hursmin':
input_ds = input_ds.resample(time='D').min('time', keep_attrs=True)
input_ds = input_ds.rename({'hurs': 'hursmin'})
input_ds['hursmin'].attrs['long_name'] = 'Daily Minimum Near-Surface Relative Humidity'
# AGCD grid
lats = np.round(np.arange(-44.5, -9.99, 0.05), decimals=2)
lons = np.round(np.arange(112, 156.26, 0.05), decimals=2)
npcp_grid = xcdat.create_grid(lats, lons)
output_ds = input_ds.regridder.horizontal(
args.var,
npcp_grid,
tool='xesmf',
method=args.method,
)
output_ds[args.var] = convert_units(output_ds[args.var], output_units[args.var])
output_ds = fix_metadata(output_ds, args.var)
output_ds.attrs['history'] = cmdprov.new_log(
code_url='https://github.com/AusClimateService/bias-correction-data-release'
)
output_encoding = get_output_encoding(output_ds, args.var, len(lats), len(lons))
output_ds.to_netcdf(args.outfile, encoding=output_encoding)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("infiles", type=str, nargs='*', help="input files")
parser.add_argument("var", type=str, help="input variable")
parser.add_argument("method", type=str, choices=('bilinear', 'conservative'), help="regridding method")
parser.add_argument("outfile", type=str, help="output file")
parser.add_argument(
"--rlon",
default=None,
type=str,
help="file containing correct rlon values"
)
args = parser.parse_args()
main(args)