
    d	6                       U d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZmZ e	rd d
lmZ d dlmZ  ej        e          Z dZ!d Z"d Z#d Z$d Z%e&ddfdZ' G d deej(                  Z) G d d          Z* G d d          Z+ G d de*          Z, G d de,          Z- G d d          Z.i Z/d e0d!<   dS )"    )annotationsN)Iterable)TYPE_CHECKINGAnyClassVar)
cf_encoder)indexing)get_chunked_array_type)is_chunked_array)
FrozenDictNdimSizeLenMixinis_remote_uri)BufferedIOBase)Dataset
__values__c                "   t          | t          j                  rt          j        |           } t          | t                    rKt          |           s<t          j                            t          j                            |                     } | S N)	
isinstanceosPathLikefspathstrr   pathabspath
expanduser)r   s    6lib/python3.11/site-packages/xarray/backends/common.py_normalize_pathr      sl    $$$ y$ 9]4%8%8 9wrw11$7788K    c                    | t           } | S r   NONE_VAR_NAMEnames    r   _encode_variable_namer$   (   s    |Kr   c                     | t           k    rd } | S r   r    r"   s    r   _decode_variable_namer&   .   s    }Kr   c                    d}| j         2| j                            d          d         f|z   }| j         } | j         2dd                    |          z   }| |fS )z;Find the root and group name of a netCDF4/h5netcdf dataset. N/)parentr#   splitjoin)ds	hierarchygroups      r   find_root_and_groupr1   4   sb    I
)
W]]3''+-	9	Y )
 #((9%%%Eu9r      i  c           	     l   |dk    sJ t          |dz             D ]}	 | |         c S # |$ r ||k    r |d|z  z  }|t          j                            |          z   }d| d||z
   dt	          j                     }t                              |           t          j	        d|z             Y w xY wdS )	z
    Robustly index an array, using retry logic with exponential backoff if any
    of the errors ``catch`` are raised. The initial_delay is measured in ms.

    With the default settings, the maximum delay will be in the range of 32-64
    seconds.
    r         zgetitem failed, waiting z ms before trying again (z# tries remaining). Full traceback: gMbP?N)
rangenprandomrandint	traceback
format_excloggerdebugtimesleep)	arraykeycatchmax_retriesinitial_delayn
base_delay
next_delaymsgs	            r   robust_getitemrI   >   s    !;?## * *	*: 
	* 
	* 
	*K&A-J#bi&7&7
&C&CCJa: a a!Oa aHQH\H^H^a a  LLJtj()))))
	** *s   (BB10B1c                      e Zd ZdZdddZdS )BackendArrayr(   Ndtypenp.typing.DTypeLikec                f    t          j        t          d           f| j        z            }| |         S r   )r	   BasicIndexerslicendim)selfrL   rA   s      r   get_duck_arrayzBackendArray.get_duck_arrayZ   s+    #U4[[NTY$>??Cyr   r   )rL   rM   )__name__
__module____qualname__	__slots__rS   r(   r   r   rK   rK   W   s2        I      r   rK   c                  B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )AbstractDataStorer(   c                    t                      r   NotImplementedErrorrR   s    r   get_dimensionsz AbstractDataStore.get_dimensionsb       !###r   c                    t                      r   r[   r]   s    r   	get_attrszAbstractDataStore.get_attrse   r_   r   c                    t                      r   r[   r]   s    r   get_variableszAbstractDataStore.get_variablesh   r_   r   c                    i S r   r(   r]   s    r   get_encodingzAbstractDataStore.get_encodingk   s    	r   c                    t          d |                                                                 D                       }t          |                                           }||fS )a,  
        This loads the variables and attributes simultaneously.
        A centralized loading function makes it easier to create
        data stores that do automatic encoding/decoding.

        For example::

            class SuffixAppendingDataStore(AbstractDataStore):

                def load(self):
                    variables, attributes = AbstractDataStore.load(self)
                    variables = {'%s_suffix' % k: v
                                 for k, v in variables.items()}
                    attributes = {'%s_suffix' % k: v
                                  for k, v in attributes.items()}
                    return variables, attributes

        This function will be called anytime variables or attributes
        are requested, so care should be taken to make sure its fast.
        c              3  >   K   | ]\  }}t          |          |fV  d S r   )r&   ).0kvs      r   	<genexpr>z)AbstractDataStore.load.<locals>.<genexpr>   sE       
 
.2a"1%%q)
 
 
 
 
 
r   )r   rc   itemsra   rR   	variables
attributess      r   loadzAbstractDataStore.loadn   sk    *  
 
6:6H6H6J6J6P6P6R6R
 
 
 
 
	   0 011
*$$r   c                    d S r   r(   r]   s    r   closezAbstractDataStore.close   s    r   c                    | S r   r(   r]   s    r   	__enter__zAbstractDataStore.__enter__   s    r   c                .    |                                   d S r   )rr   )rR   exception_typeexception_valuer:   s       r   __exit__zAbstractDataStore.__exit__   s    

r   N)rT   rU   rV   rW   r^   ra   rc   re   rp   rr   rt   rx   r(   r   r   rY   rY   _   s        I$ $ $$ $ $$ $ $  % % %6        r   rY   c                  *    e Zd ZdZddZddZddZdS )	ArrayWritersourcestargetsregionslockNc                >    g | _         g | _        g | _        || _        d S r   r{   )rR   r   s     r   __init__zArrayWriter.__init__   s"    			r   c                    t          |          rP| j                            |           | j                            |           | j                            |           d S |r|||<   d S ||d<   d S )N.)r   r|   appendr}   r~   )rR   sourcetargetregions       r   addzArrayWriter.add   s|    F## 	%L'''L'''L''''' %!'v$sr   Tc                    | j         rQt          | j          }|i } |j        | j         | j        f| j        |d| j        d|}g | _         g | _        g | _        |S d S )NT)r   computeflushr~   )r|   r
   storer}   r   r~   )rR   r   chunkmanager_store_kwargschunkmanagerdelayed_stores        r   synczArrayWriter.sync   s    < 	!14<@L )0,.).L. Y  , M DLDLDL  -	! 	!r   r   )TN)rT   rU   rV   rW   r   r   r   r(   r   r   rz   rz      sU        9I   	% 	% 	% 	%! ! ! ! ! !r   rz   c                  p    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
 e            d	d	fd
Zd ZddZddZd	S )AbstractWritableDataStorer(   c                      fd|                                 D             } fd|                                 D             }||fS )a  
        Encode the variables and attributes in this store

        Parameters
        ----------
        variables : dict-like
            Dictionary of key/value (variable name / xr.Variable) pairs
        attributes : dict-like
            Dictionary of key/value (attribute name / attribute) pairs

        Returns
        -------
        variables : dict-like
        attributes : dict-like

        c                B    i | ]\  }}|                     |          S r(   encode_variablerh   ri   rj   rR   s      r   
<dictcomp>z4AbstractWritableDataStore.encode.<locals>.<dictcomp>   -    NNNDAqQ,,Q//NNNr   c                B    i | ]\  }}|                     |          S r(   encode_attributer   s      r   r   z4AbstractWritableDataStore.encode.<locals>.<dictcomp>   -    QQQdaa..q11QQQr   )rl   rm   s   `  r   encodez AbstractWritableDataStore.encode   sY    " ONNNIOO<M<MNNN	QQQQj>N>N>P>PQQQ
*$$r   c                    |S )zencode one variabler(   )rR   rj   s     r   r   z)AbstractWritableDataStore.encode_variable       r   c                    |S )zencode one attributer(   )rR   as     r   r   z*AbstractWritableDataStore.encode_attribute   r   r   c                    t                      r   r[   )rR   dimlengths      r   set_dimensionz'AbstractWritableDataStore.set_dimension   r_   r   c                    t                      r   r[   rR   ri   rj   s      r   set_attributez'AbstractWritableDataStore.set_attribute   r_   r   c                    t                      r   r[   r   s      r   set_variablez&AbstractWritableDataStore.set_variable   r_   r   c                <    |                      ||j                   dS )z
        in stores, variables are all variables AND coordinates
        in xarray.Dataset variables are variables NOT coordinates,
        so here we pass the whole dataset in instead of doing
        dataset.variables
        N)r   attrs)rR   datasets     r   store_datasetz'AbstractWritableDataStore.store_dataset   s      	

7GM*****r   Nc                    |t                      }|                     ||          \  }}|                     |           |                     ||           |                     ||||           dS )a  
        Top level method for putting data on this store, this method:
          - encodes variables/attributes
          - sets dimensions
          - sets variables

        Parameters
        ----------
        variables : dict-like
            Dictionary of key/value (variable name / xr.Variable) pairs
        attributes : dict-like
            Dictionary of key/value (attribute name / attribute) pairs
        check_encoding_set : list-like
            List of variables that should be checked for invalid encoding
            values
        writer : ArrayWriter
        unlimited_dims : list-like
            List of dimension names that should be treated as unlimited
            dimensions.
        Nunlimited_dims)rz   r   set_attributesset_dimensionsset_variables)rR   rn   ro   check_encoding_setwriterr   s         r   r   zAbstractWritableDataStore.store   s    8 > ]]F $Iz B B	:J'''InEEE)6. 	 	
 	
 	
 	
 	
r   c                f    |                                 D ]\  }}|                     ||           dS )z
        This provides a centralized method to set the dataset attributes on the
        data store.

        Parameters
        ----------
        attributes : dict-like
            Dictionary of key/value (attribute name / attribute) pairs
        N)rl   r   )rR   ro   ri   rj   s       r   r   z(AbstractWritableDataStore.set_attributes  sF     $$&& 	% 	%DAqq!$$$$	% 	%r   c                    |                                 D ]J\  }}t          |          }||v }|                     ||||          \  }	}
|                    |
|	           KdS )a  
        This provides a centralized method to set the variables on the data
        store.

        Parameters
        ----------
        variables : dict-like
            Dictionary of key/value (variable name / xr.Variable) pairs
        check_encoding_set : list-like
            List of variables that should be checked for invalid encoding
            values
        writer : ArrayWriter
        unlimited_dims : list-like
            List of dimension names that should be treated as unlimited
            dimensions.
        r   N)rl   r$   prepare_variabler   )rR   rn   r   r   r   vnrj   r#   checkr   r   s              r   r   z'AbstractWritableDataStore.set_variables'  s    $ __&& 	' 	'EB(,,D,,E!22a~ 3  NFF JJvv&&&&	' 	'r   c           
        |t                      }|                                 }i }|D ]}d||<   |                                D ]<}|                    t	          t          |j        |j                                       =|                                D ]S\  }}||v r+|||         k    rt          d|d| d||          d          ||vr||v }| 
                    |||           TdS )au  
        This provides a centralized method to set the dimensions on the data
        store.

        Parameters
        ----------
        variables : dict-like
            Dictionary of key/value (variable name / xr.Variable) pairs
        unlimited_dims : list-like
            List of dimension names that should be treated as unlimited
            dimensions.
        Nz,Unable to update size for existing dimensionz (z != ))setr^   valuesupdatedictzipdimsshaperl   
ValueErrorr   )	rR   rn   r   existing_dimsr   rj   r   r   is_unlimiteds	            r   r   z(AbstractWritableDataStore.set_dimensionsB  sH    ! UUN++-- 	 	ADGG!!## 	4 	4AKKS11223333::<< 	> 	>KCm##-2D(D(D BB B &B B,9#,>B B B   M))"n4""3===	> 	>r   r   )rT   rU   rV   rW   r   r   r   r   r   r   r   	frozensetr   r   r   r   r(   r   r   r   r      s        I% % %*    $ $ $$ $ $$ $ $+ + + %9;;%
 %
 %
 %
N% % %' ' ' '6 >  >  >  >  >  >r   r   c                      e Zd ZdZd ZdS )WritableCFDataStorer(   c                     t          ||          \  }} fd|                                D             } fd|                                D             }||fS )Nc                B    i | ]\  }}|                     |          S r(   r   r   s      r   r   z.WritableCFDataStore.encode.<locals>.<dictcomp>l  r   r   c                B    i | ]\  }}|                     |          S r(   r   r   s      r   r   z.WritableCFDataStore.encode.<locals>.<dictcomp>m  r   r   )r   rl   rm   s   `  r   r   zWritableCFDataStore.encodeh  sl     !+9j A A	:NNNNIOO<M<MNNN	QQQQj>N>N>P>PQQQ
*$$r   N)rT   rU   rV   rW   r   r(   r   r   r   r   e  s(        I% % % % %r   r   c                  \    e Zd ZU dZdZded<   dZded<   dZded<   ddZddddZ	ddZ
dS )BackendEntrypointa  
    ``BackendEntrypoint`` is a class container and it is the main interface
    for the backend plugins, see :ref:`RST backend_entrypoint`.
    It shall implement:

    - ``open_dataset`` method: it shall implement reading from file, variables
      decoding and it returns an instance of :py:class:`~xarray.Dataset`.
      It shall take in input at least ``filename_or_obj`` argument and
      ``drop_variables`` keyword argument.
      For more details see :ref:`RST open_dataset`.
    - ``guess_can_open`` method: it shall return ``True`` if the backend is able to open
      ``filename_or_obj``, ``False`` otherwise. The implementation of this
      method is not mandatory.

    Attributes
    ----------

    open_dataset_parameters : tuple, default: None
        A list of ``open_dataset`` method parameters.
        The setting of this attribute is not mandatory.
    description : str, default: ""
        A short string describing the engine.
        The setting of this attribute is not mandatory.
    url : str, default: ""
        A string with the URL to the backend's documentation.
        The setting of this attribute is not mandatory.
    NzClassVar[tuple | None]open_dataset_parameters zClassVar[str]descriptionurlreturnr   c                    dt          |           j         d}| j        r|d| j         z  }| j        r|d| j         z  }|S )N<>z
  z
  Learn more at )typerT   r   r   )rR   txts     r   __repr__zBackendEntrypoint.__repr__  s`    ($t**%((( 	-,$*,,,C8 	32222C
r   )drop_variablesfilename_or_obj;str | os.PathLike[Any] | BufferedIOBase | AbstractDataStorer   str | Iterable[str] | Nonekwargsr   r   c                   t           )`
        Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`.
        r[   )rR   r   r   r   s       r   open_datasetzBackendEntrypoint.open_dataset  s
     "!r   boolc                    dS )r   Fr(   )rR   r   s     r   guess_can_openz BackendEntrypoint.guess_can_open  s	     ur   )r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   )rT   rU   rV   __doc__r   __annotations__r   r   r   r   r   r(   r   r   r   r   q  s          8 7;::::!#K####C    6:	" " " " " "     r   r   z5dict[str, tuple[str | None, type[BackendEntrypoint]]]BACKEND_ENTRYPOINTS)1
__future__r   loggingr   r>   r:   collections.abcr   typingr   r   r   numpyr7   xarray.conventionsr   xarray.corer	   xarray.core.parallelcompatr
   xarray.core.pycompatr   xarray.core.utilsr   r   r   ior   xarray.core.datasetr   	getLoggerrT   r<   r!   r   r$   r&   r1   	ExceptionrI   ExplicitlyIndexedrK   rY   rz   r   r   r   r   r   r(   r   r   <module>r      s   " " " " " " "  				      $ $ $ $ $ $ / / / / / / / / / /     ) ) ) ) ) )             = = = = = = 1 1 1 1 1 1 I I I I I I I I I I ,!!!!!!++++++ 
	8	$	$          &/AS * * * *2    #X%?   1 1 1 1 1 1 1 1h+! +! +! +! +! +! +! +!\a> a> a> a> a> 1 a> a> a>H	% 	% 	% 	% 	%3 	% 	% 	%> > > > > > > >D NP  O O O O O Or   