home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 375627070

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
375627070 MDU6SXNzdWUzNzU2MjcwNzA= 2527 Undesired automatic conversion of 'dtype' based on variable units 'days'? 22454970 closed 0     2 2018-10-30T18:14:38Z 2018-10-31T16:04:08Z 2018-10-31T16:04:08Z NONE      

```python 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

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/2527/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 0 rows from issues_id in issues_labels
  • 2 rows from issue in issue_comments
Powered by Datasette · Queries took 244.167ms · About: xarray-datasette