
    d*                    v   d dl m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	 d dl
Zd dlmZmZmZmZmZ d dlmZmZ d dlmZmZ d dlmZmZmZ d d	lmZ d d
lmZ d dl m!Z!m"Z"m#Z#m$Z$ d dl%m&Z& erd dlm'Z' d dlm(Z( d dl)m*Z* d Z+d Z, G d de          Z-d Z. G d de          Z/ G d de          Z0de0fed<   dS )    )annotationsN)Iterable)TYPE_CHECKINGAny)BACKEND_ENTRYPOINTSBackendArrayBackendEntrypointWritableCFDataStore_normalize_path)CachingFileManagerDummyFileManager)ensure_lockget_write_lock)encode_nc3_attr_valueencode_nc3_variableis_valid_nc3_name)StoreBackendEntrypoint)NumpyIndexingAdapter)Frozen
FrozenDictclose_on_error'try_read_magic_number_from_file_or_path)Variable)BufferedIOBase)AbstractDataStore)Datasetc                \    t          | t                    r|                     dd          S | S )Nzutf-8replace)
isinstancebytesdecode)ss    6lib/python3.11/site-packages/xarray/backends/scipy_.py_decode_stringr$   *   s-    !U ,xx+++H    c                >    d |                                  D             S )Nc                D    i | ]\  }}||d k    r|nt          |          S )
_FillValue)r$   .0kvs      r#   
<dictcomp>z!_decode_attrs.<locals>.<dictcomp>3   s4    UUU!QAA%%qq>!+<+<UUUr%   )items)ds    r#   _decode_attrsr0   0   s!     VU17799UUUUr%   c                  (    e Zd Zd ZddZd Zd ZdS )ScipyArrayWrapperc                    || _         || _        |                                 j        }|j        | _        t          j        |j        j        t          |j        j	                  z             | _        d S N)
	datastorevariable_nameget_variabledatashapenpdtypekindstritemsize)selfr6   r5   arrays       r#   __init__zScipyArrayWrapper.__init__7   sZ    "*!!##([
Xek.U[5I1J1JJKK


r%   Tc                d    | j         j                            |          }|j        | j                 S r4   )r5   _manageracquire	variablesr6   )r?   
needs_lockdss      r#   r7   zScipyArrayWrapper.get_variable>   s*    ^$,,Z88|D.//r%   c                    t          |                                 j                  |         }| j        j        j        }t          j        || j        |          S )N)r;   copy)	r   r7   r8   r5   rG   use_mmapr:   r@   r;   )r?   keyr8   rI   s       r#   __getitem__zScipyArrayWrapper.__getitem__B   sK    #D$5$5$7$7$<==cB ~ )xDJT::::r%   c                    | j         j        5  |                     d          }	 |||<   n"# t          $ r |t          u r||d d <   n Y nw xY wd d d            d S # 1 swxY w Y   d S )NF)rF   )r5   lockr7   	TypeErrorEllipsis)r?   rK   valuer8   s       r#   __setitem__zScipyArrayWrapper.__setitem__J   s    ^  		 		$$$66D!S		   (??#DGG G			 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s1   A+AA
A	A

AA!AN)T)__name__
__module____qualname__rA   r7   rL   rR    r%   r#   r2   r2   6   sX        L L L0 0 0 0; ; ;
 
 
 
 
r%   r2   c                T   dd l }t          | t                    ry|                     d          rd	 |j                            t          j        |           |||          S # t          $ r&}|j	        d         }d|v rt          d           d }~ww xY wt          | t                    r)|                     d          rt	          j        |           } 	 |j                            | |||          S # t          $ r-}|j	        d         }d|v rd}||z  }t          |           d }~ww xY w)Nr   .gzmodemmapversionzis not a valid NetCDF 3 filez2gzipped file loading only supports NetCDF 3 files.   CDFz
            If this is a NetCDF4 file, you may need to install the
            netcdf4 library, e.g.,

            $ pip install netcdf4
            )scipy.ior   r=   endswithionetcdf_filegzipopenrO   args
ValueErrorr    
startswithBytesIO)filenamerZ   r[   r\   scipyeerrmsgmsgs           r#   _open_scipy_netcdfrm   W   sh   OOO (C   X%6%6u%=%= 
	8''	(##$T7 (     	 	 	VAYF-77 !UVVV	 (E"" (x':':6'B'B (:h''x##H4dG#TTT   )V33C cMFF###s/   /A   
B*!BBC0 0
D':(D""D'c                      e Zd ZdZ	 ddZed             Zd Zd Zd Z	d	 Z
d
 ZddZd Zd Zd Z	 ddZd Zd ZdS )ScipyDataStorezStore for reading and writing data via scipy.io.netcdf.

    This store has the advantage of being able to be initialized with a
    StringIO object, allow for serialization without writing to disk.

    It only supports the NetCDF3 file-format.
    rNc           
        |t          d          ||dk    rd}n|dk    rd}nt          d|          |*|dk    r$t          |t                    rt          |          }t	          |          | _        t          |t                    r)t          t          |||t          ||          	          }n"t          ||||
          }	t          |	          }|| _
        d S )Nz7cannot save to a group with the scipy.io.netcdf backendNETCDF3_64BIT   NETCDF3_CLASSIC   z,invalid format for scipy.io.netcdf backend: rp   )r[   r\   )rZ   rN   kwargsrY   )re   r   r=   r   r   rN   r   rm   dictr   rC   )
r?   filename_or_objrZ   formatgroupr[   rN   r\   managerscipy_datasets
             r#   rA   zScipyDataStore.__init__   s    VWWW>V66GG(((GGVFVVWWW<DCKKJ,L,LK!/22D%%	os++ 	6("w777  GG /dw  M '}55Gr%   c                4    | j                                         S r4   )rC   rD   r?   s    r#   rG   zScipyDataStore.ds   s    }$$&&&r%   c                n    t          |j        t          ||           t          |j                            S r4   )r   
dimensionsr2   r0   _attributes)r?   namevars      r#   open_store_variablez"ScipyDataStore.open_store_variable   s3    NdD))#/**
 
 	
r%   c                r     t           fd j        j                                        D                       S )Nc              3  N   K   | ]\  }}|                     ||          fV   d S r4   )r   )r*   r+   r,   r?   s      r#   	<genexpr>z/ScipyDataStore.get_variables.<locals>.<genexpr>   sM       
 
48AqQ((A../
 
 
 
 
 
r%   )r   rG   rE   r.   r~   s   `r#   get_variableszScipyDataStore.get_variables   sL     
 
 
 
<@G<M<S<S<U<U
 
 
 
 
 	
r%   c                N    t          t          | j        j                            S r4   )r   r0   rG   r   r~   s    r#   	get_attrszScipyDataStore.get_attrs   s    mDG$788999r%   c                4    t          | j        j                  S r4   )r   rG   r   r~   s    r#   get_dimensionszScipyDataStore.get_dimensions   s    dg()))r%   c                V    dd | j         j                                        D             iS )Nunlimited_dimsc                    h | ]	\  }}||
S r4   rV   r)   s      r#   	<setcomp>z.ScipyDataStore.get_encoding.<locals>.<setcomp>   s    UUUTQ19q999r%   )rG   r   r.   r~   s    r#   get_encodingzScipyDataStore.get_encoding   s1    UUTW-?-E-E-G-GUUU
 	
r%   Fc                    || j         j        v r$t          t          |           j         d          |s|nd }| j                             ||           d S )Nz& does not support modifying dimensions)rG   r   re   typerS   createDimension)r?   r   lengthis_unlimited
dim_lengths        r#   set_dimensionzScipyDataStore.set_dimension   sg    47%%%::&NNN   $09VVT
j11111r%   c                B    t          |          st          d          d S )NzNot a valid attribute name)r   re   )r?   rK   s     r#   _validate_attr_keyz!ScipyDataStore._validate_attr_key   s+     %% 	;9:::	; 	;r%   c                z    |                      |           t          |          }t          | j        ||           d S r4   )r   r   setattrrG   )r?   rK   rQ   s      r#   set_attributezScipyDataStore.set_attribute   s=    $$$%e,,e$$$$$r%   c                $    t          |          }|S r4   )r   )r?   variables     r#   encode_variablezScipyDataStore.encode_variable   s    &x00r%   c                   |r8|j         r1|j         dd ik    r$t          dt          |j                              |j        }|| j        j        vr&| j                            ||j        |j                   | j        j        |         }|j	        
                                D ]+\  }}|                     |           t          |||           ,t          ||           }	|	|fS )Nr(   z'unexpected encoding for scipy backend: )encodingre   listr8   rG   rE   createVariabler;   dimsattrsr.   r   r   r2   )
r?   r   r   check_encodingr   r8   	scipy_varr+   r,   targets
             r#   prepare_variablezScipyDataStore.prepare_variable   s     	!	 !lD%999S$x?P:Q:QSS   } tw(((G""4X]CCCG%d+	N((** 	% 	%DAq##A&&&Iq!$$$$"4..t|r%   c                8    | j                                          d S r4   )rG   syncr~   s    r#   r   zScipyDataStore.sync   s    r%   c                8    | j                                          d S r4   )rC   closer~   s    r#   r   zScipyDataStore.close   s    r%   )rp   NNNN)F)FN)rS   rT   rU   __doc__rA   propertyrG   r   r   r   r   r   r   r   r   r   r   r   r   rV   r%   r#   ro   ro   }   s         SW           D ' ' X'
 
 

 
 

: : :* * *
 
 

2 2 2 2; ; ;% % %
  
 DH   6      r%   ro   c                  F    e Zd ZdZdZdZddZd	d	d	d	d
d
d
dd
d
d
d
dddZd
S )ScipyBackendEntrypointa  
    Backend for netCDF files based on the scipy package.

    It can open ".nc", ".nc4", ".cdf" and ".gz" files but will only be
    selected as the default if the "netcdf4" and "h5netcdf" engines are
    not available. It has the advantage that is is a lightweight engine
    that has no system requirements (unlike netcdf4 and h5netcdf).

    Additionally it can open gizp compressed (".gz") files.

    For more information about the underlying library, visit:
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.netcdf_file.html

    See Also
    --------
    backends.ScipyDataStore
    backends.NetCDF4BackendEntrypoint
    backends.H5netcdfBackendEntrypoint
    zAOpen netCDF files (.nc, .nc4, .cdf and .gz) using scipy in XarrayzWhttps://docs.xarray.dev/en/stable/generated/xarray.backends.ScipyBackendEntrypoint.htmlrx   ;str | os.PathLike[Any] | BufferedIOBase | AbstractDataStorereturnboolc                   t          |          }|P|                    d          r;t          j        |          5 }t          |          }d d d            n# 1 swxY w Y   ||                    d          S t	          |t
          t          j        f          r&t          j        	                    |          \  }}|dv S dS )Ns   r]   >   .nc.cdf.nc4rX   F)
r   rf   rb   rc   r   r=   osPathLikepathsplitext)r?   rx   magic_numberf_exts         r#   guess_can_openz%ScipyBackendEntrypoint.guess_can_open  s    ?OO#(?(?(L(L#?++ JqFqIIJ J J J J J J J J J J J J J J#**6222oR['9:: 	9W%%o66FAs888us   AAATNrp   )mask_and_scaledecode_timesconcat_charactersdecode_coordsdrop_variables
use_cftimedecode_timedeltarZ   ry   rz   r[   rN   r   str | Iterable[str] | Noner   c                   t          |          }t          ||	|
|||          }t                      }t          |          5  |                    ||||||||          }d d d            n# 1 swxY w Y   |S )N)rZ   ry   rz   r[   rN   )r   r   r   r   r   r   r   )r   ro   r   r   open_dataset)r?   rx   r   r   r   r   r   r   r   rZ   ry   rz   r[   rN   storestore_entrypointrG   s                    r#   r   z#ScipyBackendEntrypoint.open_dataset#  s    " */::$vUTX
 
 
 233E"" 
	 
	!..-)"3+-%!1 / 	 	B
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 	s   A,,A03A0)rx   r   r   r   )rx   r   r   r   r   r   )rS   rT   rU   r   descriptionurlr   r   rV   r%   r#   r   r      s         ( VK
cC   * 59" " " " " " " "r%   r   ri   )1
__future__r   rb   r`   r   collections.abcr   typingr   r   numpyr:   xarray.backends.commonr   r   r	   r
   r   xarray.backends.file_managerr   r   xarray.backends.locksr   r   xarray.backends.netcdf3r   r   r   xarray.backends.storer   xarray.core.indexingr   xarray.core.utilsr   r   r   r   xarray.core.variabler   r   r   xarray.core.datasetr   r$   r0   r2   rm   ro   r   rV   r%   r#   <module>r      s   " " " " " "  				 				 $ $ $ $ $ $ % % % % % % % %                  N M M M M M M M = = = = = = = =         
 9 8 8 8 8 8 5 5 5 5 5 5            * ) ) ) ) ) ,!!!!!!888888++++++  V V V       B# # #Lz z z z z( z z zzK K K K K. K K K\ !()?@ G   r%   