You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importxarrayasxrfromglobimportosimportnumpyasnpimporturllib.requestxr.set_options(enable_cftimeindex=True)
url='https://github.com/Ouranosinc/xclim/raw/master/tests/testdata/NRCANdaily/nrcan_canada_daily_tasmax_1990.nc'infile=r'~/nrcan_canada_daily_tasmax_1990.nc'urllib.request.urlretrieve(url,infile)
freqs= ['MS'] # , 'QS-DEC', 'YS']ds=xr.open_dataset(infile)
su= (ds.tasmax>25.0+273.15) *1.0forfinfreqs:
output=su.resample(time=f).sum(dim='time')
output.attrs['units'] ='days'output.attrs['standard_name'] ='summer_days'output.attrs['long_name'] ='summer days'output.attrs['description'] ='Number of days where daily maximum temperature exceeds 25℃'# use original file as templateds1=xr.open_dataset(infile, drop_variables=['tasmax', 'time','time_vectors','ts'])
ds1.coords['time'] =output.time.valuesds1['su'] =outputcomp=dict(zlib=True, complevel=5)
encoding= {var: compforvarinds1.data_vars}
print(os.path.basename(infile).replace('.nc', '_SummerDays-'+f) +' : writing '+f+' to netcdf')
# with dask.config.set(pool=ThreadPool(4)):ds1.to_netcdf('~/testNRCANsummerdays.nc', format='NETCDF4', encoding=encoding)
ds2=xr.open_dataset('~/testNRCANsummerdays.nc')
print(ds1)
print(ds2)
print(ds1.su.max())
print(ds2.su.max())
Problem description
I am calculating a climate index 'summer days' ('su') from daily maximum temperatures. Everything goes fine but when I reread a newly created output .nc file my 'su' dtype has changed from float to timedelta64. This seems to be due to the units ('days') that I assign to my newly created variable which when read by xarray must trigger an automatic conversion? If I alter the units to 'days>25C' everything is ok.
Is there a way to avoid this behavior and still keep my units as 'days' which is the CF_standard for this climate index calculation? (note there a large number of cases such as this - wet days, dry-days etc etc all of which have 'days' as the expected unit
The text was updated successfully, but these errors were encountered:
Problem description
I am calculating a climate index 'summer days' ('su') from daily maximum temperatures. Everything goes fine but when I reread a newly created output .nc file my 'su' dtype has changed from float to timedelta64. This seems to be due to the units ('days') that I assign to my newly created variable which when read by xarray must trigger an automatic conversion? If I alter the units to 'days>25C' everything is ok.
Is there a way to avoid this behavior and still keep my units as 'days' which is the CF_standard for this climate index calculation? (note there a large number of cases such as this - wet days, dry-days etc etc all of which have 'days' as the expected unit
The text was updated successfully, but these errors were encountered: