Skip to content

Commit

Permalink
adjusted to upstream changes in nc-time-axis
Browse files Browse the repository at this point in the history
  • Loading branch information
jbusecke committed Jan 25, 2019
1 parent cc4e2b8 commit bbe4da2
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import_matplotlib_pyplot, label_from_attrs)

try:
from nc_time_axis import CalendarDateTime
import nc_time_axis
nc_axis_available = True
except ImportError:
nc_axis_available = False
Expand All @@ -49,7 +49,12 @@ def _ensure_plottable(*args):
# hacky
other_types = [datetime]
if nc_axis_available:
other_types.append(CalendarDateTime)
try:
from cftime import datetime as cftime_datetime
other_types.append(cftime_datetime)
except ImportError:
print('Could not import cftime')


for x in args:
if not (_valid_numpy_subdtype(np.array(x), numpy_types)
Expand Down Expand Up @@ -108,24 +113,24 @@ def _line_facetgrid(darray, row=None, col=None, hue=None,
return g.map_dataarray_line(hue=hue, **kwargs)


def _convert_cftime_data(values):
converted = [CalendarDateTime(v, v.calendar) for v in values]
return converted
# def _convert_cftime_data(values):
# converted = [CalendarDateTime(v, v.calendar) for v in values]
# return converted


def _convert_all_cftime(da):
try:
from cftime import datetime as cftime_datetime
except ImportError:
raise ImportError('cftime package missing')
da = da.copy()
# find the dim that has a cftime datatype
dims = set(da.dims)
cftime_dims = [d for d in dims if isinstance(da[d].data.ravel()[0],
cftime_datetime)]
for cd in cftime_dims:
da[cd].data = _convert_cftime_data(da[cd].data)
return da
# def _convert_all_cftime(da):
# try:
# from cftime import datetime as cftime_datetime
# except ImportError:
# raise ImportError('cftime package missing')
# da = da.copy()
# # find the dim that has a cftime datatype
# dims = set(da.dims)
# cftime_dims = [d for d in dims if isinstance(da[d].data.ravel()[0],
# cftime_datetime)]
# for cd in cftime_dims:
# da[cd].data = _convert_cftime_data(da[cd].data)
# return da


def plot(darray, row=None, col=None, col_wrap=None, ax=None, hue=None,
Expand Down Expand Up @@ -168,15 +173,12 @@ def plot(darray, row=None, col=None, col_wrap=None, ax=None, hue=None,
"""
darray = darray.squeeze()

# If I am not mistaken, this did check only if there are cftime.datetime
# objects in the actual data, not dims.
# Correction below

# Unsure about this. If I read correctly the commented line tests if
# cftime datetimes are in the data, not the dims. I assume we want to check
# the dims?
if any([contains_cftime_datetimes(darray[dim]) for dim in darray.dims]):
if nc_axis_available:
darray = _convert_all_cftime(darray)
else:
# if contains_cftime_datetimes(darray):
if not nc_axis_available:
raise ImportError(
'Built-in plotting of arrays of cftime.datetime objects or '
'arrays indexed by cftime.datetime objects requires the '
Expand Down

0 comments on commit bbe4da2

Please sign in to comment.