def deep_align(objects, join='inner', copy=True, indexes=None,
               exclude=frozenset(), raise_on_invalid=True):
    """Align objects for merging, recursing into dictionary values.

    This function is not public API.
    """
    from .dataarray import DataArray
    from .dataset import Dataset

    if indexes is None:
        indexes = {}

    def is_alignable(obj):
        return isinstance(obj, (DataArray, Dataset))

    positions = []
    keys = []
    out = []
    targets = []
    no_key = object()
    not_replaced = object()
    for n, variables in enumerate(objects):
        if is_alignable(variables):
            positions.append(n)
            keys.append(no_key)
            targets.append(variables)
            out.append(not_replaced)
        elif is_dict_like(variables):
            for k, v in variables.items():
                if is_alignable(v) and k not in indexes:
                    # Skip variables in indexes for alignment, because these
                    # should to be overwritten instead:
                    # https://github.com/pydata/xarray/issues/725
                    positions.append(n)
                    keys.append(k)
                    targets.append(v)
            out.append(OrderedDict(variables))
        elif raise_on_invalid:
            raise ValueError('object to align is neither an xarray.Dataset, '
                             'an xarray.DataArray nor a dictionary: %r'
                             % variables)
        else:
            out.append(variables)

    aligned = align(*targets, join=join, copy=copy, indexes=indexes,
                    exclude=exclude)

    for position, key, aligned_obj in zip(positions, keys, aligned):
        if key is no_key:
            out[position] = aligned_obj
        else:
            out[position][key] = aligned_obj

    # something went wrong: we should have replaced all sentinel values
    assert all(arg is not not_replaced for arg in out)

    return out
def deep_align ( objects , join = 'inner' , copy = True , indexes = None , exclude = frozenset ( ) , raise_on_invalid = True ) : from . dataarray import DataArray from . dataset import Dataset if indexes is None : indexes = { } def is_alignable ( obj ) : return isinstance ( obj , ( DataArray , Dataset ) ) positions = [ ] keys = [ ] out = [ ] targets = [ ] no_key = object ( ) not_replaced = object ( ) for n , variables in enumerate ( objects ) : if is_alignable ( variables ) : positions . append ( n ) keys . append ( no_key ) targets . append ( variables ) out . append ( not_replaced ) elif is_dict_like ( variables ) : for k , v in variables . items ( ) : if is_alignable ( v ) and k not in indexes : positions . append ( n ) keys . append ( k ) targets . append ( v ) out . append ( OrderedDict ( variables ) ) elif raise_on_invalid : raise ValueError ( 'object to align is neither an xarray.Dataset, ' 'an xarray.DataArray nor a dictionary: %r' % variables ) else : out . append ( variables ) aligned = align ( * targets , join = join , copy = copy , indexes = indexes , exclude = exclude ) for position , key , aligned_obj in zip ( positions , keys , aligned ) : if key is no_key : out [ position ] = aligned_obj else : out [ position ] [ key ] = aligned_obj assert all ( arg is not not_replaced for arg in out ) return out
Align objects for merging, recursing into dictionary values.

    This function is not public API.