Skip to content
forked from pydata/xarray

Commit

Permalink
Don't set attributes on bounds variables.
Browse files Browse the repository at this point in the history
Fixes pydata#2921

1. Removes certain attributes from bounds variables on encode.
2. open_mfdataset: Sets encoding on variables based on encoding in first file.
  • Loading branch information
dcherian committed May 15, 2019
1 parent 4e778f0 commit ff2fd49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ def open_mfdataset(paths, chunks=None, concat_dim=_CONCAT_DIM_DEFAULT,

combined._file_obj = _MultiFileCloser(file_objs)
combined.attrs = datasets[0].attrs
for v in combined:
combined[v].encoding = datasets[0][v].attrs
return combined


Expand Down
16 changes: 15 additions & 1 deletion xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def encode_cf_variable(var, needs_copy=True, name=None):
var = maybe_default_fill_value(var)
var = maybe_encode_bools(var)
var = ensure_dtype_not_object(var, name=name)

return var


Expand Down Expand Up @@ -599,7 +600,7 @@ def cf_encoder(variables, attributes):
as possible. This includes masking, scaling, character
array handling, and CF-time encoding.
Decode a set of CF encoded variables and attributes.
Encode a set of CF encoded variables and attributes.
See Also, decode_cf_variable
Expand All @@ -619,6 +620,19 @@ def cf_encoder(variables, attributes):
See also: encode_cf_variable
"""

new_vars = OrderedDict((k, encode_cf_variable(v, name=k))
for k, v in variables.items())

# Remove attrs from bounds variables before encoding
for var in new_vars.values():
bounds = var.attrs['bounds'] if 'bounds' in var.attrs else None
if bounds and bounds in new_vars:
# see http://cfconventions.org/cf-conventions/cf-conventions.html#cell-boundaries
for attr in ['units', 'standard_name', 'axis', 'positive',
'calendar', 'long_name', 'leap_month', 'leap_year',
'month_lengths']:
if attr in new_vars[bounds].attrs:
new_vars[bounds].attrs.pop(attr)

return new_vars, attributes

0 comments on commit ff2fd49

Please sign in to comment.