
    d                       d Z ddlmZ ddlZddlmZmZ ddlZddl	Z	ddl
mZ erddlZddlmZ ddlZn0 e	j        d          Z e	j        d          Z e	j        d          Zdd	lmZmZ dd
lmZ ddlmZmZmZmZmZ ddlZddlm Z m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ eZe	j0        d             Z1g Z2e're23                    d           e&re23                    d           e%re23                    d           g dddgdgdZ4g dZ5e	j6        7                    de5          d             Z8e)d             Z9e(e)d                         Z:e	j6        7                    de5          d             Z;e*e	j6        7                    dd d!g          e	j6        7                    d"d d!g          d.d&                                    Z<e	j6        =                     eej>                   ed'          k     d()           ed *          d/d+                        Z?d/d,Z@e	j6        =                     eej>                   ed'          k     d()           ed *          d/d-                        ZAdS )0z isort:skip_file     )annotationsN)TYPE_CHECKINGAny)Versiondaskz
dask.arraydistributed)ClientLock)
futures_of)cleanupclustergen_clusterlooploop_in_thread)	HDF5_LOCKCombinedLock)assert_allcloseassert_identicalhas_h5netcdfhas_netCDF4	has_scipyrequires_cftimerequires_netCDF4requires_zarr)
ON_WINDOWScreate_tmp_file)create_test_datac                F    t          |                     d                    S )Nztestfile.nc)strjoin)tmpdirs    =lib/python3.11/site-packages/xarray/tests/test_distributed.pytmp_netcdf_filenamer#   3   s    v{{=))***    scipynetcdf4h5netcdf)NETCDF3_CLASSICNETCDF3_64BIT_OFFSETNETCDF3_64BIT_DATANETCDF4_CLASSICNETCDF4r(   NETCDF3_64BITr,   )r&   r%   r'   ))r&   r(   )r&   r+   )r&   r,   )r'   r,   )r%   r-   zengine,nc_formatc                8   |t           vrt          j        d           dddd}t                      5 \  }\  }}t	          |d         |           5  t                                          |          }|dk    rbt          j        t                    5  |	                    |||	           d d d            n# 1 swxY w Y   	 d d d            d d d            d S |	                    |||	           t          j        |||
          5 }	t          |	j        j        t          j                  sJ |	                                }
t%          ||
           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nengine not available         dim1dim2dim3addressr   r%   engineformatchunksr:   )ENGINESpytestskipr   r	   r   chunkraisesNotImplementedError	to_netcdfxropen_dataset
isinstancevar1datadaArraycomputer   r   r#   r:   	nc_formatr=   saboriginalrestoredcomputeds              r"   &test_dask_distributed_netcdf_roundtriprU   U   s    W*+++A..F	 4kq&1aAiLt,,, 	4 	4'))//77H  ]#677  &&+F9 '                  	4 	4 	4 	4 	4 	44 4 4 4 4 4 4 4 26)TTT#F6   4!(-"4bh?????#++--(3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4s   FAE7B6*E76B::E7=B:>E7F/E7	AE E7 E$$E7'E$(E7+F7E;	;F>E;	?FFFc                   t                      5 \  }\  }}t          |d         |           5  t          j        dt	          j        d          i          }|                    |           t          j        |          5 }|j        j	        dk    sJ 	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr7   r8   x )
r   r	   rE   DatasetrJ   zerosrD   rF   rW   shape)r   r#   rO   rP   rQ   rR   actuals          r"   ?test_dask_distributed_write_netcdf_with_dimensionless_variablesr]   s   s    
 ,kq&1aAiLt,,, 	, 	,z3"566H2333!455 ,x~+++++, , , , , , , , , , , , , , ,		, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,, , , , , , , , , , , , , , , , , ,sY   CAB6?BB6B##B6&B#'B6*C6B:	:C=B:	>CCCc           	     X   t          j        ddd          }t          j        d          }t          j                            |j        |j        f          }t          j        |||dd          }| d	z  } |j        |           t                      5 \  }\  }}t          |d
                   5  dD ]F}	t          j
        ||	          5 }
t          |
d         |           d d d            n# 1 swxY w Y   G	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )N2001010120010501360_day)calendard   )timeLontest)coordsnameztest.ncr7   )FT)parallel)rE   cftime_rangenparangerandomsize	DataArrayrD   r   r	   open_mfdatasetr   )tmp_pathTre   rI   rJ   	file_pathrO   rP   rQ   ri   tfs              r"   4test_open_mfdataset_can_open_files_with_cftime_indexru      s    	
JCCCA
)C..C9QVSX.//D	dAc#:#:	H	H	HB9$IBL	 5kq&1aAiL!! 	5 	5) 5 5&y8DDD 5$RZ4445 5 5 5 5 5 5 5 5 5 5 5 5 5 55	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5sZ   D0DC.	"D.C22D5C26D;DD	DD	DD#&D#c                B   |t           vrt          j        d           dddd}t                      5 \  }\  }}t	          |d         |           5  t                      }|                    |||           t          j        |||	          5 }	t          |	j
        j        t          j                  sJ |	                                }
t          ||
           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )
Nr/   r0   r1   r2   r3   r7   r8   r9   r<   )r>   r?   r@   r   r	   r   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r   rM   s              r"   2test_dask_distributed_read_netcdf_integration_testrw      s    W*+++A..F	 
4kq&1aAiLt,,, 		4 		4'))H26)TTT#F6   4!(-"4bh?????#++--(3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4			4 		4 		4 		4 		4 		4 		4 		4 		4 		4 		4 		4 		4 		4 		4
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4 
4sZ   D>C<AC%C<%C))C<,C)-C<0D<D 	 DD 	DDDconsolidatedTFrL   boolreturnNonec           	        |rddi}dddii}ni x}}dddd}t                      5 \  }\  }}t          |d         | 	          5  t                                          |          }	t	          t
          d
          5 }
 |	j        |
fd|i|}|s|                                 t          j	        |
fddd|5 }t          |j        j        t          j                  sJ |                                }t          |	|           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nrx   Tbackend_kwargsr0   r1      r3   r7   r8   z.zarrc)allow_cleanup_failuresuffixrL   autozarrr<   )r   r	   r   rA   r   r   to_zarrrL   rE   rF   rG   rH   rI   rJ   rK   r   )r   rx   rL   write_kwargsread_kwargsr=   rO   rP   rQ   rR   filenamemaybe_futuresrS   rT   s                 r"   +test_dask_distributed_zarr_integration_testr      s     ((6'='7.$9O&P%''{A..F	 8kq&1aAiLt,,, 	8 	8'))//77H &0   8 0 0! !&-!1=! !  ,!))+++_%+F >I  8%hm&8"(CCCCC'//11H#Hh7778 8 8 8 8 8 8 8 8 8 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s~   E8D=<<D&8AD	D&DD&DD&D=&D**D=-D*.D=1E=E	EE	EEEz	2022.02.0z-https://github.com/dask/distributed/pull/5739)	conditionreason)clientc                n  K   t                      }t          j        |          rJ |                    ddi          dz   }t          j        |          sJ t          j        |j                  sJ t          j        |j                  sJ |                                }t          |          sJ t          j        |          sJ t          j        |j                  sJ t          j        |j                  sJ t          |	                                          t          |	                                          k    sJ t          |          rJ t          |          sJ |                     |          }| d {V }t          j        |          rJ t          |dz   |           |j        sJ d S )Nr5   r0   
   )r   r   is_dask_collectionrA   rH   var2persistr   len__dask_graph__r   rL   r   tasks)	crO   rP   rQ   rW   yzfuturews	            r"   
test_asyncr      s      	A&q)))))	r!A"1%%%%%"16*****"16*****			Aq66MMM"1%%%%%"16*****"16*****q!!""S)9)9););%<%<<<<<!}}a==YYq\\FA&q)))))AFA7NNNNNr$   c                 R    t          t          t          j        j                  sJ d S N)rG   r   r   utilsSerializableLockrX   r$   r"   test_hdf5_lockr      s#    i!<=======r$   c           
       K   dd}t           t                      t          d          t          t           g          t          t           t          d          g          fD ]}|                     |t	          t          d                    |          }|                     |           d {V  t          j        t          j	        |                    }t          |          t          |          k    sJ d S )Nc                D    |5  | dz   cd d d            S # 1 swxY w Y   d S )N   rX   )rW   locks     r"   fz"test_serializable_locks.<locals>.f   ss     	 	q5	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   zfilename.ncr   )r   r   )r   r
   r   maplistrangegatherpickleloadsdumpstype)r   rO   rP   rQ   r   r   futureslock2s           r"   test_serializable_locksr      s          	]i[!!im!4!4566 ) ) %%4b		??%66hhwV\$//00DzzT%[[((((() )r$   )rx   ry   rL   ry   rz   r{   )rz   r{   )B__doc__
__future__r   r   typingr   r   numpyrk   r?   packaging.versionr   r   
dask.arrayarrayrJ   r   importorskipdask.distributedr	   r
   distributed.clientr   distributed.utils_testr   r   r   r   r   xarrayrE   xarray.backends.locksr   r   xarray.testsr   r   r   r   r   r   r   r   xarray.tests.test_backendsr   r   xarray.tests.test_datasetr   fixturer#   r>   append
NC_FORMATSENGINES_AND_FORMATSmarkparametrizerU   r]   ru   rw   r   xfail__version__r   r   r   rX   r$   r"   <module>r      s     " " " " " "  % % % % % % % %      % % % % % % 5KKK6v&&D		\	*	*B%&%m44K ) ) ) ) ) ) ) ) ) ) ) ) ) )                  9 9 9 9 9 9 9 9	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	        7 6 6 6 6 6 + + +  NN7 NN9 NN:    1
 

    +-@AA4 4 BA4: 	, 	, 	, 5 5  5 +-@AA4 4 BA4* $77T5M228 8 8 32 87 88 gk-..1E1EE:    D   	 
6> > > > gk-..1E1EE:    D) ) ) 	 
) ) )r$   