Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undesired automatic conversion of 'dtype' based on variable units 'days'? #2527

Closed
tlogan2000 opened this issue Oct 30, 2018 · 2 comments
Closed

Comments

@tlogan2000
Copy link

tlogan2000 commented Oct 30, 2018

import xarray as xr
from glob import os
import numpy as np
import urllib.request
xr.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.0
for f in freqs:

    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 template
    ds1 = xr.open_dataset(infile, drop_variables=['tasmax', 'time','time_vectors','ts'])
    ds1.coords['time'] = output.time.values
    ds1['su'] = output

    comp = dict(zlib=True, complevel=5)
    encoding = {var: comp for var in ds1.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

@shoyer
Copy link
Member

shoyer commented Oct 30, 2018

see related issues #1621 (and a work-in-progress fix #2105)

@tlogan2000
Copy link
Author

Great thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants