a
    iDfW                  
   @  s   d Z ddlmZ ddlZddlZddlmZ ddlZddl	m
Z ddlmZ ddlmZ erhddl	mZ d	Zd
ZdZdd Zi Zdddddddddd	Zdd Zd%ddddddddddZ
ddddZdd d!dd"d#d$ZdS )&zR
Useful for:

* users learning xarray
* building tutorials in the documentation.

    )annotationsN)TYPE_CHECKING)open_dataset)	DataArray)Dataset)T_EngineZxarray_tutorial_dataz%https://github.com/pydata/xarray-dataZmasterc                 C  s6   dd l }t| tjr t| } n| d u r2|t} | S )Nr   )pooch
isinstanceosPathLikefspathZos_cache_default_cache_dir_name)pathr    r   \/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/xarray/tutorial.py_construct_cache_dir   s    
r         )	Zair_temperatureZair_temperature_gradientZASE_ice_velocityZ
basin_maskZersstv5ZrasmZROMS_exampleZtinyZ
eraint_uvzc                 C  s   t | }|dkrbzdd l}W nB ty`   zdd l}W n" tyZ   td|  dY n0 Y n0 |dkrzdd l}W nB ty   zdd l}W n" ty   td|  dY n0 Y n0 d S )Nr   r   zopening tutorial dataset z2 requires either scipy or netCDF4 to be installed.r   z5 requires either h5netcdf or netCDF4 to be installed.)file_formatsgetscipyImportErrornetCDF4h5netcdf)nameversionr   r   r   r   r   r   _check_netcdf_engine_installed7   s*    


r   T)enginestrboolzNone | str | os.PathLiker   r   )r   cache	cache_dirr   returnc             
   K  sD  zddl }W n. ty: } ztd|W Y d}~n
d}~0 0 | }|d t|}| tv rht|  }nt| }	|	jsd}
|du rt	|  |	
|
}	nR|	jdkr|du rd}zddl}W n. ty } ztd|W Y d}~n
d}~0 0 t d	t d
|	j }|j|d|d}t|fd|i|}|s@| }t|  |S )a  
    Open a dataset from the online repository (requires internet).

    If a local copy is found then always use that to avoid network traffic.

    Available datasets:

    * ``"air_temperature"``: NCEP reanalysis subset
    * ``"air_temperature_gradient"``: NCEP reanalysis subset with approximate x,y gradients
    * ``"basin_mask"``: Dataset with ocean basins marked using integers
    * ``"ASE_ice_velocity"``: MEaSUREs InSAR-Based Ice Velocity of the Amundsen Sea Embayment, Antarctica, Version 1
    * ``"rasm"``: Output of the Regional Arctic System Model (RASM)
    * ``"ROMS_example"``: Regional Ocean Model System (ROMS) output
    * ``"tiny"``: small synthetic dataset with a 1D data variable
    * ``"era5-2mt-2019-03-uk.grib"``: ERA5 temperature data over the UK
    * ``"eraint_uvz"``: data from ERA-Interim reanalysis, monthly averages of upper level data
    * ``"ersstv5"``: NOAA's Extended Reconstructed Sea Surface Temperature monthly averages

    Parameters
    ----------
    name : str
        Name of the file containing the dataset.
        e.g. 'air_temperature'
    cache_dir : path-like, optional
        The directory in which to search for and write cached data.
    cache : bool, optional
        If True, then cache data locally for use on subsequent calls
    **kws : dict, optional
        Passed to xarray.open_dataset

    See Also
    --------
    tutorial.load_dataset
    open_dataset
    load_dataset
    r   Nzhtutorial.open_dataset depends on pooch to download and manage datasets. To proceed please install pooch.WARNINGz.ncz.gribcfgribz:Reading this tutorial dataset requires the cfgrib package.z/raw//)urlZ
known_hashr   r   )r   r   Z
get_loggersetLevelr   external_urlspathlibPathsuffixr   with_suffixr$   base_urlr   r   retrieve_open_datasetloadunlink)r   r    r!   r   kwsr   eloggerr&   r   Zdefault_extensionr$   filepathdsr   r   r   r   R   sJ    ,



r   )r"   c                  O  s:   t | i |}| W  d   S 1 s,0    Y  dS )aS  
    Open, load into memory, and close a dataset from the online repository
    (requires internet).

    If a local copy is found then always use that to avoid network traffic.

    Available datasets:

    * ``"air_temperature"``: NCEP reanalysis subset
    * ``"air_temperature_gradient"``: NCEP reanalysis subset with approximate x,y gradients
    * ``"basin_mask"``: Dataset with ocean basins marked using integers
    * ``"rasm"``: Output of the Regional Arctic System Model (RASM)
    * ``"ROMS_example"``: Regional Ocean Model System (ROMS) output
    * ``"tiny"``: small synthetic dataset with a 1D data variable
    * ``"era5-2mt-2019-03-uk.grib"``: ERA5 temperature data over the UK
    * ``"eraint_uvz"``: data from ERA-Interim reanalysis, monthly averages of upper level data
    * ``"ersstv5"``: NOAA's Extended Reconstructed Sea Surface Temperature monthly averages

    Parameters
    ----------
    name : str
        Name of the file containing the dataset.
        e.g. 'air_temperature'
    cache_dir : path-like, optional
        The directory in which to search for and write cached data.
    cache : bool, optional
        If True, then cache data locally for use on subsequent calls
    **kws : dict, optional
        Passed to xarray.open_dataset

    See Also
    --------
    tutorial.open_dataset
    open_dataset
    load_dataset
    N)r   r0   )argskwargsr6   r   r   r   load_dataset   s    %r9   )seedz
None | int)r:   r"   c              
   C  s   t j| }tt g dg dt dt dddt dd|d dd	}d|jd
  |j	d  d|j
 |j  }d|j |j	d|j
   |j }t||d}g d|d< d|jjd< d|j	jd< d|j
jd< d|jjd< d|jjd< d|jjd< |S )z
    Create an example dataset.

    Parameters
    ----------
    seed : int, optional
        Seed for the random number generation.
    )r      r   r   )xyzwr   r      r;   r   g?)dimsZcoords   g      @g   )AB)ZoneZtwoZthreeZfiver?   ZxunitsZunitsZyunitsZzunitsZwunitsZAunitsZBunits)nprandomZdefault_rngr   zerosZarangeZlinspaceZstandard_normalr<   r=   r>   r?   r   attrsrD   rE   )r:   rngrD   rE   r6   r   r   r   scatter_example_dataset   s*    	
( rK   )TN)__doc__
__future__r   r
   r)   typingr   numpyrF   Zxarray.backends.apir   r/   Zxarray.core.dataarrayr   Zxarray.core.datasetr   r   r   r-   r   r   r(   r   r   r9   rK   r   r   r   r   <module>   sB     X)