HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/merge.cpython-36.pyc
3

���h5�@s�ddlmZmZmZddlmZddlmZddlm	Z	m
Z
mZdd�Zdd	d
�Z
dd�Zd
d�Zdd�Ze�ddfdd�Zddd�Zdd�ZdS)�)�align�
partial_align�align_variables)�ChainMap)�as_variable)�
basestring�	iteritems�OrderedDictcCs`yt||d�}Wntk
r,td��YnX||jkr\|jdkrTtd||jf��|j�}|S)z2Prepare a variable for adding it to a Dataset
    )�keyzXvariables must be given by arrays or a tuple of the form (dims, data[, attrs, encoding])rz{the variable %r has the same name as one of its dimensions %r, but it is not 1-dimensional and thus it is not a valid index)r�	TypeError�dims�ndim�
ValueErrorZto_coord)�name�var�r�1/tmp/pip-build-5_djhm0z/xray/xarray/core/merge.py�_as_dataset_variables

rN�	identicalc	s�|dkri}t�}t��t||����fdd������fdd�}xft|�D]Z\}}t|d�r��j|j�x*t|j�D]\}}||krv|||j�qvW|j}|||�qLW|�fS)a�Expand a dictionary of variables.

    Returns a dictionary of Variable objects suitable for inserting into a
    Dataset._variables dictionary.

    This includes converting tuples (dims, data) into Variable objects,
    converting coordinate variables into Coordinate objects and expanding
    DataArray objects into Variables plus coordinates.

    Raises ValueError if any conflicting values are found, between any of the
    new or old variables.
    Ncsj�|}||jkrf||jkr&|�|<n@tt|j|j��}|jt|j|j��|j|��|<�j|j�dS)N)rr	�zip�shape�updateZexpand_dims)rrZexisting_varZcommon_dims)�new_coord_names�	variablesrr�maybe_promote_or_replace,s



z2expand_variables.<locals>.maybe_promote_or_replacecsjt||�}|�kr,|�|<�j�|j�n:t�|��|�sTtd|�||f���dkrf�||�dS)NzCconflicting value for variable %s:
first value: %r
second value: %r�broadcast_equals)rrr�getattrr)rr)�compatrrrrr�add_variable8s
z&expand_variables.<locals>.add_variable�coords)r	�setrr�hasattrrr�variable)	Z
raw_variablesZ
old_variablesr�
new_variablesrrrZdimZcoordr)rrrrr�expand_variabless 



r$csFt�fdd�|j�D��}t|||�\}}|j�}|j|�|||fS)Nc3s"|]\}}|�kr||fVqdS)Nr)�.0�k�v)�overwrite_varsrr�	<genexpr>Ssz _merge_expand.<locals>.<genexpr>)�dict�itemsr$�copyr)r�otherr(rZpossible_conflicts�new_varsr�replace_varsr)r(r�
_merge_expandRs

r0c	CsBt|||dd�\}}t|j|j||�\}}}|j|j�|||fS)NF)�joinr,)rr0�
_variablesrZ_coord_names)	�selfr-r(rr1�aligned_selfr/r.rrrr�_merge_dataset_with_dataset[s
r5cs~t�ddd��dd��j�D�}t|f�fdd�|D��|d|d��}|d}t����jt||d	d���t|j�||�S)
N�outerF)r1r,cSsg|]\}}t|d�r|�qS)�indexes)r!)r%r&r'rrr�
<listcomp>hsz,_merge_dataset_with_dict.<locals>.<listcomp>csg|]}�|�qSrr)r%�a)r-rrr8is)r1r,�exclude�r)rr+rr	rrr0r2)r3r-r(rr1�	alignable�alignedr4r)r-r�_merge_dataset_with_dictesr>rr6c
Cs�ddlm}|d	kr td|��t|t�r0|g}t|�}t||�rHt}nt}||||||d�\}}}	|	t|j�@}
t|j	�t|�|	@}|
|B|}|r�tdt
|���||	fS)
z
    Guts of Dataset.merge
    r)�Datasetr�equalsrzFcompat=%r invalid: must be 'broadcast_equals', 'equals' or 'identical')rr1zZcannot merge: the following variables are coordinates on one dataset but not the other: %s)rr@r)�datasetr?r�
isinstancerr r5r>Z	data_varsr�list)
rAr-r(rr1r?Z
merge_funcr/r.rZnewly_coordsZno_longer_coordsZambiguous_coordsrrr�merge_datasetsts&

rDFcszdd��j�D�}g}xF|D]>�t��fdd�|j�D��}|j��jfd|i|���qWt��}|jt||��|S)zMReindex all DataArrays in the provided dict, leaving other values alone.
    cSsg|]\}}t|d�r|�qS)r7)r!)r%r&r'rrrr8�sz._reindex_variables_against.<locals>.<listcomp>c3s(|] \}}|��jkr||fVqdS)N)r)r%r&r')r9rrrr)�sz-_reindex_variables_against.<locals>.<genexpr>r,)r+r*�appendZreindexr	rr)rr7r,r<r=Z
valid_indexesr#r)r9rr�_reindex_variables_against�s
"rFcCs6t|ddd�}t||dd�}t|||dd�\}}}|S)zt
    Return the new dictionary of coordinate variables given by merging in
    ``other`` to to these variables.
    r6F)r1r,)r,r)r)rrFr0)r7rr-r/�_�__rrr�merge_dataarray_coords�s
rI)Nr)F)�	alignmentrrr�utilsrr"rZpycompatrrr	rr$r0r5r>r rDrFrIrrrr�<module>s
9	
!