U
    Cvf                     @  sp  d dl mZ d dlmZ d dlmZ d dlZd dlZd dlm	Z	 d dl
mZmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ d d	lmZmZmZ d d
lm Z  dd Z!G dd dZ"G dd dZ#ej$dddd Z%dd Z&dd Z'G dd dZ(G dd dZ)G dd dZ*G dd  d Z+G d!d" d"Z,G d#d$ d$Z-ed%d& Z.ed'd( Z/d)d* Z0dS )+    )annotations)datetime)productN)Version)	DataArrayDataset
MergeErrorcombine_by_coordscombine_nestedconcatmerge)dtypes)_check_shape_tile_ids_combine_all_along_first_dim_combine_nd_infer_concat_order_from_coords"_infer_concat_order_from_positions_new_tile_id)assert_equalassert_identicalrequires_cftimecreate_test_datac                 C  sL   t | t |kst|  D ]*\}}|| ks4tt| | ||  qd S N)lenAssertionErroritemskeysr   )Zdict1Zdict2kv r    =/tmp/pip-unpacked-wheel-h316xyqg/xarray/tests/test_combine.pyassert_combined_tile_ids_equal    s    r"   c                   @  sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )TestTileIDsFromNestedListc                 C  s<   t }|d|dg}|d|dd}t|}t|| d S Nr      r   r%   r   r   r"   selfdsinputexpectedactualr    r    r!   test_1d(   s
    z!TestTileIDsFromNestedList.test_1dc                 C  sr   t }|d|dg|d|dg|d|dgg}|d|d|d|d|d|dd}t|}t|| d S )Nr   r%               )r   r   r   r%   r%   r   r%   r%   )r1   r   )r1   r%   r)   r*   r    r    r!   test_2d0   s    .z!TestTileIDsFromNestedList.test_2dc                 C  s   t }|d|dg|d|dg|d|dgg|d|dg|d	|d
g|d|dggg}|d|d|d|d|d|d|d|d|d	|d
|d|dd}t|}t|| d S )Nr   r%   r1   r2   r3   r4            	   
      ))r   r   r   )r   r   r%   )r   r%   r   )r   r%   r%   )r   r1   r   )r   r1   r%   )r%   r   r   )r%   r   r%   )r%   r%   r   )r%   r%   r%   )r%   r1   r   )r%   r1   r%   r)   r*   r    r    r!   test_3d?   s&    ,,z!TestTileIDsFromNestedList.test_3dc                 C  s,   t d}|g}d|i}t|}t|| d S Nr   r'   r)   r*   r    r    r!   test_single_datasetW   s
    z-TestTileIDsFromNestedList.test_single_datasetc                 C  s@   t }|dg|dgg}|d|dd}t|}t|| d S )Nr   r%   )r5   r7   r)   r*   r    r    r!   test_redundant_nesting_   s
    z0TestTileIDsFromNestedList.test_redundant_nestingc                 C  s.   t d}|g g}d|i}t|}t|| d S rA   r)   r*   r    r    r!   test_ignore_empty_listg   s
    z0TestTileIDsFromNestedList.test_ignore_empty_listc                 C  sJ   t }|d|d|dgg}|d|d|dd}t|}t|| d S )Nr   r%   r1   )r'   r7   r8   r)   r*   r    r    r!   test_uneven_depth_inputn   s
    z1TestTileIDsFromNestedList.test_uneven_depth_inputc                 C  sL   t }|dg|d|dgg}|d|d|dd}t|}t|| d S )Nr   r%   r1   )r5   r7   r8   r)   r*   r    r    r!   test_uneven_length_inputx   s
    z2TestTileIDsFromNestedList.test_uneven_length_inputc                 C  s<   t }|d|dg}|d|dd}t|}t|| d S r$   r)   r*   r    r    r!   test_infer_from_datasets   s
    z2TestTileIDsFromNestedList.test_infer_from_datasetsN)__name__
__module____qualname__r0   r9   r@   rB   rC   rD   rE   rF   rG   r    r    r    r!   r#   '   s   

r#   c                   @  s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )TestTileIDsFromCoordsc                 C  sV   t dddgi}t dddgi}||d}t||g\}}t|| |dgksRtd S )Nxr   r%   r1   r2   r&   r   r   r"   r   r+   ds0ds1r.   r/   concat_dimsr    r    r!   r0      s    

zTestTileIDsFromCoords.test_1dc           
      C  s   t ddgdddgd}t ddgdddgd}t ddgd	d
dgd}t ddgd	d
dgd}t ddgdddgd}t ddgdddgd}||||||d}t||||||g\}}	t|| |	ddgkstd S )Nr   r%   r>         rL   yr1   r2   (   2   <   F   P   Z   )r5   r7   r6   r8   r   r1   )r%   r1   rL   rU   rM   )
r+   rO   rP   ds2ds3Zds4Zds5r.   r/   rQ   r    r    r!   r9      s$    
zTestTileIDsFromCoords.test_2dc              	   C  sR   t ddddgfi}t ddddgfi}tjtdd t||g W 5 Q R X d S )	NfoorL   r   r%   r1   r2   zCould not find any dimensionmatchr   pytestraises
ValueErrorr   r+   rO   rP   r    r    r!   test_no_dimension_coords   s    z.TestTileIDsFromCoords.test_no_dimension_coordsc              	   C  sJ   t dddgi}t dddgi}tjtdd t||g W 5 Q R X d S )NrL   r   r%   r2   r1   z=Coordinate variable x is neither monotonically increasing norr`   rb   rf   r    r    r!   test_coord_not_monotonic   s    z.TestTileIDsFromCoords.test_coord_not_monotonicc                 C  sV   t dddgi}t dddgi}||d}t||g\}}t|| |dgksRtd S )NrL   r2   r1   r%   r   r&   rM   rN   r    r    r!   #test_coord_monotonically_decreasing   s    

z9TestTileIDsFromCoords.test_coord_monotonically_decreasingc                 C  sD   t ddddgfi}d|i}t|g\}}t|| |g ks@td S )Nr_   rL   r   r%   r    rM   )r+   r,   r.   r/   rQ   r    r    r!   test_no_concatenation_needed   s
    
z2TestTileIDsFromCoords.test_no_concatenation_neededc                 C  s   t ddgdddgddgd}t d	d
gdddgddgd}t ddgdddgddgd}t d	d
gdddgddgd}||||d}t||||g\}}t|| |ddgkstd S )Nr   r%   r>   rR   rS   g?g?)rL   rU   tr1   r2   rV   rW   rX   )r5   r7   r6   r8   rL   rU   rM   )r+   rO   rP   r]   r^   r.   r/   rQ   r    r    r!   test_2d_plus_bystander_dim   s    
z0TestTileIDsFromCoords.test_2d_plus_bystander_dimc                 C  sV   t dddgi}t dddgi}||d}t||g\}}t|| |dgksRtd S )NZpersonZAliceZBobZCarolineZDanielr&   rM   rN   r    r    r!   test_string_coords   s    

z(TestTileIDsFromCoords.test_string_coordsc                 C  sV   t dddgi}t dddgi}||d}t||g\}}t|| |dgksRtd S )NZ
simulationZrun8Zrun9Zrun10Zrun11r&   rM   rN   r    r    r!   %test_lexicographic_sort_string_coords   s    

z;TestTileIDsFromCoords.test_lexicographic_sort_string_coordsc                 C  sv   t dtdddtdddgi}t dtdddtdd	d
gi}||d}t||g\}}t|| |dgksrtd S )Ntime  r2   r:     r;   i  r%   r1   r3   r&   )r   r   r   r"   r   rN   r    r    r!   test_datetime_coords   s      

z*TestTileIDsFromCoords.test_datetime_coordsN)rH   rI   rJ   r0   r9   rg   rh   ri   rj   rl   rm   rn   rr   r    r    r    r!   rK      s   			
	rK   module)Zscopec                   C  s   t S r   )_create_combined_idsr    r    r    r!   create_combined_ids   s    ru   c                 C  s(   t | }tt|}dd t||D S )Nc                 S  s   i | ]\}}|t |qS r    r   ).0Ztile_idnumr    r    r!   
<dictcomp>   s      z(_create_combined_ids.<locals>.<dictcomp>)_create_tile_idsranger   zip)shapetile_idsnumsr    r    r!   rt      s    rt   c                 C  s   t dd | D  }t|S )Nc                 s  s   | ]}t |V  qd S r   )rz   )rv   ir    r    r!   	<genexpr>   s     z#_create_tile_ids.<locals>.<genexpr>)r   list)r|   r}   r    r    r!   ry      s    ry   c                   @  s4   e Zd Zejddddddgdd Zd	d
 ZdS )TestNewTileIDszold_id, new_id))r2   r   r%   r6   )r5   r'   )r(   r    )r'   r    )r7   r'   c                 C  s   t }t||f|kstd S r   )r   r   r   )r+   Zold_idZnew_idr,   r    r    r!   test_new_tile_id  s    zTestNewTileIDs.test_new_tile_idc                 C  s0   d}||}t | }t|}||ks,td S )N)r%   r1   r2   )sortedr   ry   r   )r+   ru   r|   combined_idsZexpected_tile_idsZactual_tile_idsr    r    r!   test_get_new_tile_ids  s
    z$TestNewTileIDs.test_get_new_tile_idsN)rH   rI   rJ   rc   markparametrizer   r   r    r    r    r!   r     s   
r   c                   @  sH   e Zd Zejdddgdd Zdd Zejdddgdd	 Zd
S )TestCombineND
concat_dimdim1Znew_dimc                 C  sL   d}||}t }t||dddd}t|d|dg|d}t|d	|i d S )
N)r1   all	differentno_conflictsdim	data_varscoordscompatr   r%   r   r    )r   r   r   r"   )r+   ru   r   r|   r   r,   resultZexpected_dsr    r    r!   test_concat_once  s    zTestCombineND.test_concat_oncec                 C  s   d}||}t |ddddd}t}t|d|dgdd	}t|d
|dgdd	}t|d|dgdd	}|||g}	dd t|	D }
t||
 d S )Nr1   r2   r   r   r   r   r   r   r2   r   r%   r3   r1   r4   c                 S  s   i | ]\}}|f|qS r    r    )rv   r   r,   r    r    r!   rx   5  s      z<TestCombineND.test_concat_only_first_dim.<locals>.<dictcomp>)r   r   r   	enumerater"   )r+   ru   r|   r   r   r,   partway1partway2partway3Zexpected_datasetsr.   r    r    r!   test_concat_only_first_dim%  s     
z(TestCombineND.test_concat_only_first_dimc                 C  s   d}||}t |d|gd}t}t|d|dgdd}t|d|dgdd}t|d	|d
gdd}	t|||	g|d}
t||
 d S )Nr   r   )rQ   r   r2   r   r%   r3   r1   r4   )r   r   r   r   )r+   ru   r   r|   r   r   r,   r   r   r   r.   r    r    r!   test_concat_twice9  s    zTestCombineND.test_concat_twiceN)	rH   rI   rJ   rc   r   r   r   r   r   r    r    r    r!   r     s
   
r   c                   @  s   e Zd Zdd Zdd ZdS )TestCheckShapeTileIDsc              	   C  s8   t d}||d}tjtdd t| W 5 Q R X d S )Nr   )r'   r6   'sub-lists do not have consistent depthsr`   r   rc   rd   re   r   r+   r,   Zcombined_tile_idsr    r    r!   test_check_depthsI  s    
 z'TestCheckShapeTileIDs.test_check_depthsc              	   C  s>   t d}|||||d}tjtdd t| W 5 Q R X d S )Nr   )r5   r6   r\   r7   r8   (sub-lists do not have consistent lengthsr`   r   r   r    r    r!   test_check_lengthsQ  s     z(TestCheckShapeTileIDs.test_check_lengthsN)rH   rI   rJ   r   r   r    r    r    r!   r   H  s   r   c                   @  s0  e Zd Zdd Zejddeddgddgdfdeddgg dfd	eddgdgdfd
eddgdgdfgdd Zdd Z	dd Z
ejjdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zejd)ejd*d+d*dd,gd-d. Zd/d0 Zd1d2 Zd3S )4TestNestedCombinec                 C  s  t ddgit ddgig}t dddgi}t|dd}t|| t|dgd}t|| t|gd d}t|| t|gdd}t|| t dddgit ddgig}t|dd}t ddddgi}t|| t ddgfddgfdt ddgfddgfdg}t|dd}t dddgfdddgfd}t|| t dgdgdt ddgig}t|dd}t ddgdgd}t|| d S )	NrL   r   r%   r   r1   arT   rU   rL   r   r
   r   r+   objsr.   r/   r    r    r!   test_nested_concat[  s0    





 z$TestNestedCombine.test_nested_concatjoin, expectedouterr   r%   rT   innerleftrightc                 C  s@   t dgdgdt dgdgdg}t|d|d}t|| d S Nr   rT   r%   rL   r   joinr   r+   r   r.   r   r/   r    r    r!   test_combine_nested_join|  s    
$z*TestNestedCombine.test_combine_nested_joinc              	   C  sP   t dgdgdt dgdgdg}tjtdd t|ddd W 5 Q R X d S )	Nr   rT   r%   zcannot align.*join.*exactr`   rL   exactr   r   rc   rd   re   r
   r+   r   r    r    r!   test_combine_nested_join_exact  s    $z0TestNestedCombine.test_combine_nested_join_exactc                 C  s   t t tg dd d S )NrL   r   )r   r   r
   r+   r    r    r!   test_empty_input  s    z"TestNestedCombine.test_empty_inputc              	   C  sP   t dgdgdt dgdgdg}tjtdd t|ddd	 W 5 Q R X d S )
Nr   r%   rT   r   znot equal across datasetsr`   rL   Zminimal)r   r   r   r   r    r    r!   (test_nested_concat_too_many_dims_at_once  s    $z:TestNestedCombine.test_nested_concat_too_many_dims_at_oncec                 C  s   t ddgfdgdt ddgfdgdg}t ddgdggfdgd}t|dd}t|| td	d
gddd}t ddgdggfdgd	d
gd}t||d}t|| d S )NrL   r>   r   r   rL   rR   rk   rL   rk   r   d      baznamedims)r   rL   )r   rL   r   )r   r
   r   r   )r+   r   r.   r/   r   r    r    r!    test_nested_concat_along_new_dim  s    
z2TestNestedCombine.test_nested_concat_along_new_dimc           
      C  s  t ddi}t|||gd d}t|| t dddgfddgd}t dddgfddgd}t ddddgfdddgd}t||gd d}t|| t||gd gd}t|| t ddi}t dtji}t||gd d}t|| t||gd gd}t|| t dddg}td	gd
d
d}	t||	gd}t d
dgfd
dgfdd
d	gi}t|| t dddgfdddgfdg}td	gd
d
d}	t||	gd}t dddggfdddggfdd
d	gi}t|| d S )NrL   r   r   r%   r1   r   r2   rT   r   r   r   z)r   r   )r   r
   r   npnanr   )
r+   datar/   rP   r]   r.   Ztmp1Ztmp2r   r   r    r    r!   test_nested_merge  s:    




"
 z#TestNestedCombine.test_nested_mergec                 C  s   t dddggfit dddggfigt dddggfit dddggfigg}t|ddgd	}t ddddgddggfi}t|| d S )
Nr   rT   r   r%   r1   r2   rL   rU   r   r   r+   r   r/   r.   r    r    r!   test_concat_multiple_dims  s    &&z+TestNestedCombine.test_concat_multiple_dimsc                 C  s   t ddggddgd}t ddggddgd}t ddggddgd}t dd	ggddgd}t||g||ggddgd
}t||g||ggddgd
}t|| dS )z,Inspired by the discussion on GH issue #2777r   r   rL   rU   )r   r   r   br%   r1   r2   r   Nr   r
   r   )r+   da1da2da3da4Zx_firstZy_firstr    r    r!   test_concat_name_symmetry  s    z+TestNestedCombine.test_concat_name_symmetryc                 C  s   t dd}|jdd}|jdd}|jjtdd|jjtdddg|jjtdd|jjtdddgg}|dd	g }t|d d
gd}t|| d S )NFZ	add_attrsTdeepr3   dim2r=   var1var2r   r   )r   copyr   iselslicer   r
   r   )r+   r   data1data2r   r.   r/   r    r    r!   !test_concat_one_dim_merge_another  s    
$$z3TestNestedCombine.test_concat_one_dim_merge_anotherc                 C  s   t }t|d|dgdd}t|d|dgdd}t|d|dgdd}t|||gd	d}|d|d|dg|d|d|dgg}t|dd	gd
}t|| d S )Nr   r2   r   r   r%   r3   r1   r4   r   r   )r   r   r
   r   )r+   r,   r   r   r   r.   datasetsr   r    r    r!   test_auto_combine_2d  s    ,z&TestNestedCombine.test_auto_combine_2dc              	   C  s  dd }t |d|dgdd}t |d|dgdd}t |d	|d
gdd}t |||gdd}i }|jdd|d< i |d _|jdd|d< dd	ddd
dd|d _|jdd|d< ddi|d _dd }|jdd||< |ddigd || _|d|d|d	g|d|d|d
gg}ddi|d d _dd	d|d d _ddd|d d	 _ddd|d d _dd
d|d d _ddd|d d	 _tjtdd t|ddgdd}	W 5 Q R X |D ]&}
t|ddg|
d}	t|	||
  qd S )Nc                 S  s   t | ddS )NFr   r   )rL   r    r    r!   <lambda>      zLTestNestedCombine.test_auto_combine_2d_combine_attrs_kwarg.<locals>.<lambda>r   r2   r   r   r%   r3   r1   r4   r   Tr   dropr   r:   )r   r   cdefoverrider   c                 S  s   | d S )Nr   r    attrscontextr    r    r!   r     r   r   r   r   r   )r   r   )r   r   )r   r   combine_attrs='identical'r`   	identical)r   combine_attrs)r   r   r   rc   rd   re   r
   r   )r+   r,   r   r   r   r.   expected_dictr   r   r   r   r    r    r!   (test_auto_combine_2d_combine_attrs_kwarg  sR    
,    z:TestNestedCombine.test_auto_combine_2d_combine_attrs_kwargc                 C  s~   t dddgfddgdt dddgfddgdg}t ddtjddgddtjggfiddddgi}t|d	d
}t|| d S )NrL   r1   r2   r%   r   r   r   r   rk   r   )r   r   r   r
   r   r+   r   r.   r/   r    r    r!   (test_combine_nested_missing_data_new_dim0  s     z:TestNestedCombine.test_combine_nested_missing_data_new_dimc              	   C  s   t }|d|d|dg|d|dgg}tjtdd t|dd	gd
 W 5 Q R X |d|dg|d|dggg}tjtdd t|dd	gd
 W 5 Q R X |d|dg|d|dgg}tjtdd t|dgd
 W 5 Q R X d S )Nr   r%   r1   r2   r3   r   r`   r   r   r   r   zconcat_dims has length)r   rc   rd   re   r
   )r+   r,   r   r    r    r!   test_invalid_hypercube_input=  s     & "  z.TestNestedCombine.test_invalid_hypercube_inputc                 C  s  t ddddgfit ddddgfigt dddd	gfit ddd
dgfigg}t ddddd	gfdddd
dgfd}t|dd gdd}t|| t ddddgfit dddd	gfigt ddddgfit ddd
dgfigg}t|d dgdd}t|| d S )Nr_   rL   r   r%   barr>   rR   r1   r2   rS   rV   )r_   r   equals)r   r   r   r   r    r    r!   !test_merge_one_dim_concat_anotherP  s    &&&
&&z3TestNestedCombine.test_merge_one_dim_concat_anotherc                 C  s   t ddgit ddgigg}t|d dgd}t dddgi}t|| t ddgigt ddgigg}t|dd gd}t dddgi}t|| t ddgigg}t|d d gd}t ddgi}t|| d S )NrL   r   r%   r   r   r   r    r    r!   *test_combine_concat_over_redundant_nestingb  s    
 
z<TestNestedCombine.test_combine_concat_over_redundant_nesting
fill_valuer1          @r   c                 C  s   t dddgfdddgfddgdt dddgfdddgfddgdg}|tjkr^tj }}n$t|trz|d	 }|d
 }n| }}t d|ddgdd|ggfd|ddgdd|ggfdddddgi}t|d|d}t|| d S )NrL   r1   r2   r%   r   r   rL   r   r   r   r   r   rk   )r   r   )	r   r   NAr   r   
isinstancedictr
   r   )r+   r   r   Zfill_value_aZfill_value_br.   r/   r    r    r!   test_combine_nested_fill_valuer  s"    ""


z0TestNestedCombine.test_combine_nested_fill_valuec                 C  s  t ddgdddgidd}t|gdd}|}t|| t ddgdddgidd}t dd	gdd
dgidd}t||gdd}t dddd	gdddd
dgidd}t|| t dggdgdgdddgd}t dggdgdgdddgd}t dggdgdgdddgd}t dggdgdgdddgd}	||g||	gg}
t ddgddggddgddgdddgd}t|
ddgd}t|| d S )N      ?r   rL   r   r%   r   r   r   r         @      @r1   r2   g        rT   rU   r   )r+   unnamed_arrayr/   r.   unnamed_array1unnamed_array2r   r   r   r   r   r    r    r!   'test_combine_nested_unnamed_data_arrays  s2    

  
    z9TestNestedCombine.test_combine_nested_unnamed_data_arraysc              	   C  sR   t ddgddddgidtdddgig}tjtdd t|d W 5 Q R X d S )	Nr   r%   rL   r   r   r1   r2   z+Can't combine datasets with unnamed arrays.r`   )r   r   rc   rd   re   r
   r   r    r    r!   )test_nested_combine_mixed_datasets_arrays  s     z;TestNestedCombine.test_nested_combine_mixed_datasets_arraysN)rH   rI   rJ   r   rc   r   r   r   r   r   r   Zxfailr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r    r    r    r!   r   Z  s:   !	

'	.
r   c                   @  s  e Zd Zdd Zdd Zejddeddgddgd	fd
eddgg d	fdeddgdgd	fdeddgdgd	fgdd Z	dd Z
ejddeddgddgd	i dfdeddgddgd	ddddfdeddgddgd	ddidfdd eddgddgd	ddddfgdd Zdd Zdd  Zejd!dddddd"d#ddd"d$d%fddddi dddd%fdi dd"d#dd"d#d%fddddd&d"d#ddd"d$d'fdddddd"d#i d%fd(dddddddddd%fd(ddddd"d#dddd'fddddd&d)d"d$dddd%fd*ddd"d$dd"d&d+dd"d&d,d%fg	d-d. Zejd!dddddd"d#ddd"d$d%fddddi dddd%fdi dd"d#dd"d#d%fddddd&d"d#ddd"d$d'fdddddd"d#i d%fd(dddddddddd%fd(ddddd"d#dddd'fddddd&d)d"d$dddd%fd*ddd"d$dd"d&d+dd"d&d,d%fg	d/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?S )@TestCombineDatasetsbyCoordsc              	   C  s  t ddgit ddgig}t|}t dddgi}t|| t|g}t|| t dddgit ddgig}t|}t ddddgi}t|| t ddgfddgfdgdt ddgfddgfdgdg}t|}t dddgfdddgfddgd}t|| t dgdgdt dgdgdg}t|}t ddgddgd}t|| t ddit ddig}tjtd	d
 t| W 5 Q R X t dgdgdt ddgig}tjtdd
 t| W 5 Q R X d S )NrL   r   r%   r1   r   rL   rU   r   rT   r   z(Could not find any dimension coordinatesr`   z"Every dimension needs a coordinate)r   r	   r   r   rc   rd   re   r   r    r    r!   test_combine_by_coords  s:    



$
$
  z2TestCombineDatasetsbyCoords.test_combine_by_coordsc                 C  s   t t tg  d S r   )r   r   r	   r   r    r    r!   r     s    z,TestCombineDatasetsbyCoords.test_empty_inputr   r   r   r%   rT   r   r   r   c                 C  s@   t dgdgdt dgdgdg}t|d|d}t|| d S r   r   r   r    r    r!   test_combine_coords_join  s    
$z4TestCombineDatasetsbyCoords.test_combine_coords_joinc              	   C  sP   t dgdgdt dgdgdg}tjtdd t|ddd W 5 Q R X d S )	Nr   rT   r%   zcannot align.*join.*exact.*r`   rL   r   r   r   r   r    r    r!   test_combine_coords_join_exact  s    $z:TestCombineDatasetsbyCoords.test_combine_coords_join_exactzcombine_attrs, expectedr   r   r   r1   r   r   r   c                 C  s   | d S )Nr%   r    r   r    r    r!   r     r   z$TestCombineDatasetsbyCoords.<lambda>c              	   C  s   t dgdgdddidt dgdgdddddg}t|dd	|d
}t|| |dkrd|d jd< tjtdd t|dd	|d
}W 5 Q R X d S )Nr   rT   r   r%   r  r1   r   rL   r   r   r   r   r   zcombine_attrs='no_conflicts'r`   r   r
   r   r   rc   rd   re   )r+   r   r.   r   r/   r    r    r!   !test_combine_coords_combine_attrs  s&       
   z=TestCombineDatasetsbyCoords.test_combine_coords_combine_attrsc              	   C  s   t dgdgdddidt dgdgdddidg}t ddgddgdddid}t|dddd	}t|| d
|d jd< tjtdd t|dddd	}W 5 Q R X d S )Nr   rT   r   r%   r  rL   r   r   r  r1   r   r   r`   r  r   r    r    r!   +test_combine_coords_combine_attrs_identical  s&       
   zGTestCombineDatasetsbyCoords.test_combine_coords_combine_attrs_identicalc                 C  s|   t dgdgddddddt dgdgddddddg}t ddgddgddddd	d}t|d
ddd}t|| d S )Nr   rT   r%   r1   r2   r   r   r   r  )r   r   r   r   r   r   rL   r   drop_conflictsr  r   r   r    r    r!   0test_combine_nested_combine_attrs_drop_conflicts$  s    "   zLTestCombineDatasetsbyCoords.test_combine_nested_combine_attrs_drop_conflictsz?combine_attrs, attrs1, attrs2, expected_attrs, expect_exceptionr2   r   r  Fr3   Tr   r4   r  )r   r   r   r  c           
   	   C  s   t dddg|fdddg|fdddg|fd}t dddg|fdddg|fdddg|fd}|rtjtd	d
 t||g|d W 5 Q R X nXt||g|d}t dddddg|fdddddg|fdddddddg|fi}	t||	 dS )=check that combine_attrs is used on data variables and coordsrL   r%   r1   r2   r  r   r   r   r   r`   r   r   Nr   rc   rd   r   r	   r   
r+   r   Zattrs1Zattrs2Zexpected_attrsZexpect_exceptionr   r   r/   r.   r    r    r!   +test_combine_nested_combine_attrs_variables/  s.    *zGTestCombineDatasetsbyCoords.test_combine_nested_combine_attrs_variablesc           
   	   C  s   t ddg|fddg|fddg|fd}t ddg|fddg|fddg|fd}|rtjtdd t||g|d W 5 Q R X nHt||g|d}t dddg|fdddg|fdddg|fd}	t||	 dS )	r  r   r   r  r%   r   r`   r  Nr   r!  r    r    r!   .test_combine_by_coords_combine_attrs_variablesw  s"    *""zJTestCombineDatasetsbyCoords.test_combine_by_coords_combine_attrs_variablesc                 C  sF   t  }|jtddd|jtddg}t|}|}||sBtd S )Nr3   r=   r   )r   r   r   r	   Zbroadcast_equalsr   )r+   r   r   r/   r.   r    r    r!   test_infer_order_from_coords  s
    "z8TestCombineDatasetsbyCoords.test_infer_order_from_coordsc              
   C  s   dddg}t jdd}ttddg|d dd d f fdtd	d
dg|dd}ttddg|dd d d f fdtddddg|dd}ttddg|fdtd	d
dddddg|dd}t||f}t|| d S )Nr   r   r   r;   r2   rL   rU   )r   r%   r1   rT   )r   r   r3   r4   r:   )r   randomZrandr   r  r	   r   )r+   Zycoordr   rP   r]   r.   r/   r    r    r!   )test_combine_leaving_bystander_dimensions  s     
  zETestCombineDatasetsbyCoords.test_combine_leaving_bystander_dimensionsc                 C  s   t ddgfdgdt ddgfdgdt ddgfdgdg}t dddgfddtjgfddddgi}t|}t|| d S )NrL   r   r   )r   rL   r%   r   )r   r   r   r	   r   r   r    r    r!   (test_combine_by_coords_previously_failed  s    *zDTestCombineDatasetsbyCoords.test_combine_by_coords_previously_failedc              	   C  sJ   t ddiddit ddidddg}tt t|d W 5 Q R X d S )NrL   r   rU   r%   rU   r   r   rc   rd   re   r	   )r+   r   r    r    r!   "test_combine_by_coords_still_fails  s    &z>TestCombineDatasetsbyCoords.test_combine_by_coords_still_failsc                 C  s|   t ddit ddig}t|}t ddd}t|| t dddt tjddg}t|}t dddd}t|| d S )	NrL   r   rU   r%   rT   r1   r(  )rL   rU   r   )r   r	   r   r   r   r   r    r    r!    test_combine_by_coords_no_concat  s    
z<TestCombineDatasetsbyCoords.test_combine_by_coords_no_concatc              	   C  sL   t ddddgi}t dddgi}tjtdd t||g W 5 Q R X d S )	NrL   r   r%   r4   r1   r2   z8does not have monotonic global indexes along dimension xr`   r)  rf   r    r    r!   "test_check_for_impossible_ordering  s    z>TestCombineDatasetsbyCoords.test_check_for_impossible_orderingc              	   C  s   t dddggfidgdgdd}t dddggfidgdgdd}t dddggfidgdgdd}t|||g}t ddddgdtjggfiddgddgdd}t|| tt t|||gd d W 5 Q R X d S )Nr   r   r%   r   r   )r   )r   r	   r   r   r   rc   rd   re   )r+   x1Zx2Zx3r/   r.   r    r    r!   +test_combine_by_coords_incomplete_hypercube  s    """
zGTestCombineDatasetsbyCoords.test_combine_by_coords_incomplete_hypercubeN)rH   rI   rJ   r  r   rc   r   r   r   r  r  r  r  r  r"  r#  r$  r&  r'  r*  r+  r,  r/  r    r    r    r!   r    s   &	
 






&
"





&
	r  c                   @  s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestCombineMixedObjectsbyCoordsc              	   C  s   t dddgdddgidd}t dd	gdd
dgidd}tjtdd t||g W 5 Q R X t ddgddddgid}tdd
dgi}tjtdd t||g W 5 Q R X d S )Nr   r  r   rL   r   r%   r   r   r   r   r  r	  r1   r2   r  z3Can't automatically combine unnamed DataArrays withr`   r  )r   rc   rd   re   r	   r   )r+   named_daZ
unnamed_dadar,   r    r    r!   /test_combine_by_coords_mixed_unnamed_dataarrays  s     zOTestCombineMixedObjectsbyCoords.test_combine_by_coords_mixed_unnamed_dataarraysc                 C  sx   t dddgddddgid}tddd	d
gfi}t||g}tdddgfdd	d
gfdddddgfid}t|| d S )Nr   r3   r4   rL   r   r%   )r   r   r   r   r   r1   r2   r   r-  )r   r   r	   r   )r+   r3  r,   r/   r.   r    r    r!   3test_combine_coords_mixed_datasets_named_dataarrays!  s     zSTestCombineMixedObjectsbyCoords.test_combine_coords_mixed_datasets_named_dataarraysc                 C  s   t ddgdddgidd}t|g}|}t|| t ddgdddgidd}t ddgdd	d
gidd}t||g}t ddddgdddd	d
gidd}t|| d S )Nr  r   rL   r   r%   r  r  r	  r1   r2   )r   r	   r   )r+   r
  r/   r.   r  r  r    r    r!   -test_combine_by_coords_all_unnamed_dataarrays*  s    


  zMTestCombineMixedObjectsbyCoords.test_combine_by_coords_all_unnamed_dataarraysc                 C  s   t dddgdddgidd}t|g}| }t|| t dddgdddgidd}t dd	d
gdddgidd}t||g}tt ddgdddgiddt d	d
gdddgiddd}t|| d S )Nr   r  r   rL   r   r%   r1  r   r  r	  r1   r2   r  r   )r   r	   
to_datasetr   r   )r+   r2  r/   r.   	named_da1	named_da2r    r    r!   +test_combine_by_coords_all_named_dataarrays:  s    

zKTestCombineMixedObjectsbyCoords.test_combine_by_coords_all_named_dataarraysc                 C  s^   t dddgdddgidd}t ddd	gdd
dgidd}t||g}t||g}t|| d S )Nr   r  r   rL   r   r%   r1  r  r	  r1   r2   )r   r	   r   r   )r+   r8  r9  r/   r.   r    r    r!   8test_combine_by_coords_all_dataarrays_with_the_same_nameM  s
    zXTestCombineMixedObjectsbyCoords.test_combine_by_coords_all_dataarrays_with_the_same_nameN)rH   rI   rJ   r4  r5  r6  r:  r;  r    r    r    r!   r0    s
   	r0  c            
      C  s   dd l } | dddg}| dddg}| dddg}tdgdg|gdd	 }td
gdg|gdd	 }tdgdg|gdd	 }t|||g}t|||g}tdd
dgdg|gdd	 }	t||	 d S )Nr   i        i  i  ro   r   r   r   r   r%   r1   )cftimeDatetimeGregorianr   r7  r	   r   Zconcatenater   )
r?  time_1time_2Ztime_3da_1da_2Zda_3r   Zexpected_timer.   r    r    r!   +test_combine_by_coords_distant_cftime_datesV  s        
rE  c               	   C  s   dd l } | dddg}| dddg}tdgdg|gdd }tdgdg|gdd }t| jtdkrtd	}nd
}tjt	|d t
||g W 5 Q R X d S )Nr   rp   r%   rq   ro   r   r>  z1.5zCannot combine along dimension 'time' with mixed types. Found:.* If importing data directly from a file then setting `use_cftime=True` may fix this issue.z)cannot compare .* \(different calendars\)r`   )r?  r@  ZDatetimeProlepticGregorianr   r7  r   __version__rc   rd   	TypeErrorr	   )r?  rA  rB  rC  rD  	error_msgr    r    r!   5test_combine_by_coords_raises_for_differing_calendarsl  s    rI  c               	   C  sb   t dgdgdggdd } t dgdgdggdd }tjtdd t| |g W 5 Q R X d S )	Nr   ro   r   r>  r%      bz7Cannot combine along dimension 'time' with mixed types.r`   )r   r7  rc   rd   rG  r	   )rC  rD  r    r    r!   1test_combine_by_coords_raises_for_differing_types  s     rK  )1
__future__r   r   	itertoolsr   Znumpyr   rc   Zpackaging.versionr   Zxarrayr   r   r   r	   r
   r   r   Zxarray.corer   Zxarray.core.combiner   r   r   r   r   r   Zxarray.testsr   r   r   Zxarray.tests.test_datasetr   r"   r#   rK   Zfixtureru   rt   ry   r   r   r   r   r  r0  rE  rI  rK  r    r    r    r!   <module>   s@   $	 dg

4  ]  \G

