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

Fix decode_cf_variable. #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion test/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pandas as pd
import warnings

from xray import conventions
from xray import conventions, Dataset
from . import TestCase, requires_netCDF4


Expand Down Expand Up @@ -139,6 +139,27 @@ def test_slice_decoded_cf_datetime_array(self):
self.assertEqual(actual.dtype, np.dtype('datetime64[ns]'))
self.assertArrayEqual(actual[[0, 2]], expected[[0, 2]])

@requires_netCDF4
def test_decode_cf_variable(self):
ds = Dataset({'time': ('time', [0, 1, 2],
{'units': 'days since 1900-01-01',
'calendar': 'standard'})})
actual = conventions.decode_cf_variable(ds['time'])
expected = pd.date_range('1900-01-01', periods=3).values
self.assertEqual(actual.dtype, np.dtype('datetime64[ns]'))
self.assertArrayEqual(actual.values, expected)

actual = conventions.decode_cf_variable(ds['time'].variable)
self.assertEqual(actual.dtype, np.dtype('datetime64[ns]'))
self.assertArrayEqual(actual.values, expected)

# default calendar
ds = Dataset({'time': ('time', [0, 1, 2],
{'units': 'days since 1900-01-01'})})
actual = conventions.decode_cf_variable(ds['time'])
self.assertEqual(actual.dtype, np.dtype('datetime64[ns]'))
self.assertArrayEqual(actual.values, expected)

@requires_netCDF4
def test_decode_non_standard_calendar(self):
import netCDF4 as nc4
Expand Down
6 changes: 4 additions & 2 deletions xray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ def get_to(source, dest, k):

def decode_cf_variable(var, concat_characters=True, mask_and_scale=True,
decode_times=True):
# use _data instead of data so as not to trigger loading data
if isinstance(var, xray.DataArray):
var = var.variable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider using var = xray.variable.as_variable(var) instead.

# use _data instead of values so as not to trigger loading data
data = var._data
dimensions = var.dimensions
attributes = var.attrs.copy()
Expand All @@ -433,7 +435,7 @@ def pop_to(source, dest, k):
return v

if 'dtype' in encoding:
if var.data.dtype != encoding['dtype']:
if data.dtype != encoding['dtype']:
raise ValueError("Refused to overwrite dtype")
encoding['dtype'] = data.dtype

Expand Down