Skip to content

Commit

Permalink
Merge pull request #161 from matt-long/encoding
Browse files Browse the repository at this point in the history
Preserve encoding on merge
  • Loading branch information
andersy005 authored Oct 17, 2019
2 parents 1cac0b1 + efc7452 commit b08f446
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion intake_esm/merge_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,35 @@ def apply_aggregation(v, agg_column=None, key=None, level=0):

attrs = dict_union(*[ds.attrs for ds in dsets])

# copy encoding for each variable from first encounter
variables = set([v for ds in dsets for v in ds.variables])

encoding = {}
for ds in dsets:
for v in variables:
if v in ds.variables and v not in encoding:
if ds[v].encoding:
encoding[v] = ds[v].encoding
# get rid of the misleading file-specific attributes
# github.com/pydata/xarray/issues/2550
for enc_attrs in ['source', 'original_shape']:
if enc_attrs in encoding[v]:
del encoding[v][enc_attrs]

if agg_type == 'join_new':
ds = join_new(dsets, dim_name=agg_column, coord_value=keys, options=agg_options)

elif agg_type == 'join_existing':

ds = join_existing(dsets, options=agg_options)

elif agg_type == 'union':
ds = union(dsets, options=agg_options)

ds.attrs = attrs
for v in ds.variables:
if v in encoding and not ds[v].encoding:
ds[v].encoding = encoding[v]

return ds

return apply_aggregation(v)
Expand Down
1 change: 1 addition & 0 deletions tests/cmip6/test_cmip6.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_to_dataset_dict(esmcol_path, query, kwargs):
_, ds = cat.to_dataset_dict().popitem()
assert 'member_id' in ds.dims
assert len(ds.__dask_keys__()) > 0
assert ds.time.encoding


@pytest.mark.parametrize(
Expand Down

0 comments on commit b08f446

Please sign in to comment.