Skip to content

Commit

Permalink
Attributes from netCDF4 intialization retained
Browse files Browse the repository at this point in the history
Ensures that attrs for open_mfdataset are now retained
  • Loading branch information
pwolfram committed Oct 4, 2016
1 parent 9cf107b commit ed0025c
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,42 @@ def _get_priority_vars(objects, priority_arg, compat='equals'):
priority_vars = merge_variables(expanded, compat=compat)
return priority_vars

def _merge_attrs(objects, mergesep='; '):
""" Merge attrs from a list of objects
This is needed to merge multiple object all containing attributes.
Parameters
----------
objects : list of objects containing attributes
Each item in list has an associated attrs
mergesep : str, optional
Merge separator for output of merged attribute strings
Returns
-------
Merged attributes from attr property of each item in objects
"""

mergedattrs = OrderedDict()

# merge the attributes (ignoring order)
for attrs in [aobj.attrs for aobj in objects]:
for k, v in attrs.iteritems():
vals = mergedattrs.get(k, set())
vals.add(v)
mergedattrs[k] = vals

# format for output
for k, v in mergedattrs.iteritems():
if len(v) == 1:
mergedattrs[k] = v.pop()
else:
mergedattrs[k] = mergesep.join(list(v))

return mergedattrs


def merge_coords(objs, compat='minimal', join='outer', priority_arg=None,
indexes=None):
Expand Down Expand Up @@ -532,7 +568,8 @@ def merge(objects, compat='no_conflicts', join='outer'):
for obj in objects]

variables, coord_names, dims = merge_core(dict_like_objects, compat, join)
merged = Dataset._construct_direct(variables, coord_names, dims)
attrs = _merge_attrs(dict_like_objects)
merged = Dataset._construct_direct(variables, coord_names, dims, attrs)

return merged

Expand Down

0 comments on commit ed0025c

Please sign in to comment.