
    d                    z    d dl mZ d dlZd dlmZ d dlmZ  G d de          Z G d d          Z	d	 Z
d
 Zd ZdS )    )annotationsN)	DataArray)Datasetc                      e Zd ZdZdS )AccessorRegistrationWarningz/Warning for conflicts in accessor registration.N)__name__
__module____qualname____doc__     6lib/python3.11/site-packages/xarray/core/extensions.pyr   r   	   s        9999r   r   c                      e Zd ZdZd Zd ZdS )_CachedAccessorz?Custom property-like object (descriptor) for caching accessors.c                "    || _         || _        d S )N)_name	_accessor)selfnameaccessors      r   __init__z_CachedAccessor.__init__   s    
!r   c                &   || j         S 	 |j        }n# t          $ r i x}|_        Y nw xY w	 || j                 S # t          $ r Y nw xY w	 |                      |          }n&# t          $ r t          d| j        d          w xY w||| j        <   |S )Nzerror initializing z
 accessor.)r   _cacheAttributeErrorr   KeyErrorRuntimeError)r   objclscacheaccessor_objs        r   __get__z_CachedAccessor.__get__   s    ;>!	$JEE 	$ 	$ 	$!##ECJJJ	$	$$ 	 	 	D		O>>#..LL 	O 	O 	O MTZMMMNNN		O )djs'    )): 
AAA! !#BN)r   r	   r
   r   r   r!   r   r   r   r   r      s8        II" " "    r   r   c                      fd}|S )Nc           	         t                    r&t          j        d| dddt          d           t	          t          |                      | S )Nzregistration of accessor z under name z
 for type z: is overriding a preexisting attribute with the same name.   )
stacklevel)hasattrwarningswarnr   setattrr   )r   r   r   s    r   	decoratorz%_register_accessor.<locals>.decorator2   s    3 	MIH I ID I IVY I I I+	    	T?4::;;;r   r   )r   r   r*   s   `` r   _register_accessorr+   1   s*    	 	 	 	 	 	 r   c                ,    t          | t                    S )a0  Register a custom accessor on xarray.DataArray objects.

    Parameters
    ----------
    name : str
        Name under which the accessor should be registered. A warning is issued
        if this name conflicts with a preexisting attribute.

    See Also
    --------
    register_dataset_accessor
    )r+   r   r   s    r   register_dataarray_accessorr.   @   s     dI...r   c                ,    t          | t                    S )a  Register a custom property on xarray.Dataset objects.

    Parameters
    ----------
    name : str
        Name under which the accessor should be registered. A warning is issued
        if this name conflicts with a preexisting attribute.

    Examples
    --------
    In your library code:

    >>> @xr.register_dataset_accessor("geo")
    ... class GeoAccessor:
    ...     def __init__(self, xarray_obj):
    ...         self._obj = xarray_obj
    ...
    ...     @property
    ...     def center(self):
    ...         # return the geographic center point of this dataset
    ...         lon = self._obj.latitude
    ...         lat = self._obj.longitude
    ...         return (float(lon.mean()), float(lat.mean()))
    ...
    ...     def plot(self):
    ...         # plot this array's data on a map, e.g., using Cartopy
    ...         pass
    ...

    Back in an interactive IPython session:

    >>> ds = xr.Dataset(
    ...     {"longitude": np.linspace(0, 10), "latitude": np.linspace(0, 20)}
    ... )
    >>> ds.geo.center
    (10.0, 5.0)
    >>> ds.geo.plot()  # plots data on a map

    See Also
    --------
    register_dataarray_accessor
    )r+   r   r-   s    r   register_dataset_accessorr0   P   s    V dG,,,r   )
__future__r   r'   xarray.core.dataarrayr   xarray.core.datasetr   Warningr   r   r+   r.   r0   r   r   r   <module>r5      s    " " " " " "  + + + + + + ' ' ' ' ' ': : : : :' : : :! ! ! ! ! ! ! !H  / / / +- +- +- +- +-r   