Skip to content

example of how to use cf convention to use 2d coordinates for plotting #606

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
14 changes: 10 additions & 4 deletions xray/conventions.py
Original file line number Diff line number Diff line change
@@ -825,7 +825,8 @@ def stackable(dim):
var_coord_names = var_attrs['coordinates'].split()
if all(k in variables for k in var_coord_names):
coord_names.update(var_coord_names)
del var_attrs['coordinates']
# del var_attrs['coordinates']
var_attrs['coordinates'] = tuple(var_coord_names)

if decode_coords and 'coordinates' in attributes:
attributes = OrderedDict(attributes)
@@ -943,9 +944,14 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names):
for var_name, coord_names in variable_coordinates.items():
attrs = variables[var_name].attrs
if 'coordinates' in attrs:
raise ValueError('cannot serialize coordinates because variable '
"%s already has an attribute 'coordinates'"
% var_name)
if (isinstance(attrs['coordinates'], tuple) and
len(attrs['coordinates']) == 2):
# Using CF coordinates
coord_names = attrs['coordinates']
else:
raise ValueError('cannot serialize coordinates because variable '
"%s already has an attribute 'coordinates'"
% var_name)
attrs['coordinates'] = ' '.join(map(str, coord_names))

# These coordinates are not associated with any particular variables, so we
21 changes: 16 additions & 5 deletions xray/plot/plot.py
Original file line number Diff line number Diff line change
@@ -62,20 +62,31 @@ def _infer_xy_labels(plotfunc, darray, x, y):
'{1} or {2} for y'
.format(y, *dims))

cf_coords = getattr(darray, 'coordinates', None)

# Get label names
if x and y:
xlab = x
ylab = y
elif x and not y:
xlab = x
del dims[dims.index(x)]
ylab = dims.pop()
if cf_coords:
ylab = cf_coords[1]
else:
del dims[dims.index(x)]
ylab = dims.pop()
elif y and not x:
ylab = y
del dims[dims.index(y)]
xlab = dims.pop()
if cf_coords:
xlab = cf_coords[0]
else:
del dims[dims.index(y)]
xlab = dims.pop()
else:
ylab, xlab = dims
if cf_coords:
xlab, ylab = cf_coords
else:
ylab, xlab = dims

return xlab, ylab