-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsdi.py
More file actions
54 lines (42 loc) · 1.48 KB
/
wsdi.py
File metadata and controls
54 lines (42 loc) · 1.48 KB
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
"""Command line program for calculating the Warm Spell Duration Index (WSDI)"""
import argparse
import numpy as np
import xarray as xr
import xclim as xc
import dask.diagnostics
import cmdline_provenance as cmdprov
dask.diagnostics.ProgressBar().register()
def main(args):
"""Run the program."""
infile1 = args.infiles[0]
if 'zarr' in infile1:
ds = xr.open_dataset(args.infiles[0], engine='zarr')
else:
ds = xr.open_mfdataset(args.infiles, attrs_file=args.infiles[-1])
ds['tasmax'] = xc.core.units.convert_units_to(ds['tasmax'], 'degC')
time_end = '2014-12-30' if ds.time.dt.calendar == '360_day' else '2014-12-31'
tx90 = xc.core.calendar.percentile_doy(
ds['tasmax'].sel(time=slice('1950-01-01', time_end)),
window=5,
per=90
)
tx90 = tx90.compute()
tx90 = tx90.sel(percentiles=90)
wsdi_da = xc.indicators.icclim.WSDI(
tasmax=ds['tasmax'],
tasmax_per=tx90,
freq='YS',
)
wsdi_ds = wsdi_da.to_dataset()
wsdi_ds.attrs = ds.attrs
wsdi_ds.attrs['history'] = cmdprov.new_log()
wsdi_ds.to_netcdf(args.outfile)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("infiles", type=str, nargs='*', help="input tasmax files")
parser.add_argument("outfile", type=str, help="output file name")
args = parser.parse_args()
main(args)