Skip to content
forked from pydata/xarray

Commit ff2fd49

Browse files
committed
Don't set attributes on bounds variables.
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.
1 parent 4e778f0 commit ff2fd49

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

xarray/backends/api.py

+2
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ def open_mfdataset(paths, chunks=None, concat_dim=_CONCAT_DIM_DEFAULT,
724724

725725
combined._file_obj = _MultiFileCloser(file_objs)
726726
combined.attrs = datasets[0].attrs
727+
for v in combined:
728+
combined[v].encoding = datasets[0][v].attrs
727729
return combined
728730

729731

xarray/conventions.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def encode_cf_variable(var, needs_copy=True, name=None):
235235
var = maybe_default_fill_value(var)
236236
var = maybe_encode_bools(var)
237237
var = ensure_dtype_not_object(var, name=name)
238+
238239
return var
239240

240241

@@ -599,7 +600,7 @@ def cf_encoder(variables, attributes):
599600
as possible. This includes masking, scaling, character
600601
array handling, and CF-time encoding.
601602
602-
Decode a set of CF encoded variables and attributes.
603+
Encode a set of CF encoded variables and attributes.
603604
604605
See Also, decode_cf_variable
605606
@@ -619,6 +620,19 @@ def cf_encoder(variables, attributes):
619620
620621
See also: encode_cf_variable
621622
"""
623+
622624
new_vars = OrderedDict((k, encode_cf_variable(v, name=k))
623625
for k, v in variables.items())
626+
627+
# Remove attrs from bounds variables before encoding
628+
for var in new_vars.values():
629+
bounds = var.attrs['bounds'] if 'bounds' in var.attrs else None
630+
if bounds and bounds in new_vars:
631+
# see http://cfconventions.org/cf-conventions/cf-conventions.html#cell-boundaries
632+
for attr in ['units', 'standard_name', 'axis', 'positive',
633+
'calendar', 'long_name', 'leap_month', 'leap_year',
634+
'month_lengths']:
635+
if attr in new_vars[bounds].attrs:
636+
new_vars[bounds].attrs.pop(attr)
637+
624638
return new_vars, attributes

0 commit comments

Comments
 (0)