U
    Cvf3%                  
   @  s  d Z 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Z	ddl
mZ ddlmZ ddlmZ ddlmZ er|dd	l
mZ d
ZdZdZdd Zi Zddd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"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)open_rasterio)	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 kr2|t} | S )Nr   )pooch
isinstanceosPathLikefspathZos_cache_default_cache_dir_name)pathr	    r   3/tmp/pip-unpacked-wheel-h316xyqg/xarray/tutorial.py_construct_cache_dir   s    
r   zFhttps://github.com/rasterio/rasterio/raw/1.2.1/tests/data/RGB.byte.tifzChttps://github.com/rasterio/rasterio/raw/1.2.1/tests/data/shade.tif)zRGB.byteZshade      )	Zair_temperatureZair_temperature_gradientZASE_ice_velocityZ
basin_maskZersstv5ZrasmZROMS_exampleZtinyZ
eraint_uvzc                 C  s   t | }|dkrfzdd l}W nF tk
rd   zdd l}W n$ tk
r^   td|  dY nX Y nX |dkrzdd l}W nF tk
r   zdd l}W n$ tk
r   td|  dY nX Y nX 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_installed<   s*    


r   T)enginestrboolzNone | str | os.PathLiker   r   )r   cache	cache_dirr   returnc             
   K  s  zddl }W n, tk
r8 } ztd|W 5 d}~X Y nX | }|d t|}| tkrft|  }n\t| }	|	jsd}
|dkrt	|  |	
|
}	n|	jdkr|dkrd}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.gribZcfgribz/raw//urlZ
known_hashr   r   )r	   r   
get_loggersetLevelr   external_urlspathlibPathsuffixr   with_suffixbase_urlr   r   retrieve_open_datasetloadunlink)r   r!   r"   r   kwsr	   eloggerr'   r   Zdefault_extensionfilepathdsr   r   r   r   W   s:    ,



r   c              
   K  s   t jdtdd zddl}W n, tk
rH } ztd|W 5 d}~X Y nX | }|d t|}t	| }|dkrt
d|  |j|d|d	}	t|	f|}
|s|
 }
t|	  |
S )
am  
    Open a rasterio dataset from the online repository (requires internet).

    .. deprecated:: 0.20.0

        Deprecated in favor of rioxarray.
        For information about transitioning, see:
        https://corteva.github.io/rioxarray/stable/getting_started/getting_started.html

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

    Available datasets:

    * ``"RGB.byte"``: TIFF file derived from USGS Landsat 7 ETM imagery.
    * ``"shade"``: TIFF file derived from from USGS SRTM 90 data

    ``RGB.byte`` and ``shade`` are downloaded from the ``rasterio`` repository [1]_.

    Parameters
    ----------
    name : str
        Name of the file containing the dataset.
        e.g. 'RGB.byte'
    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_rasterio

    See Also
    --------
    xarray.open_rasterio

    References
    ----------
    .. [1] https://github.com/rasterio/rasterio
    zopen_rasterio is Deprecated in favor of rioxarray. For information about transitioning, see: https://corteva.github.io/rioxarray/stable/getting_started/getting_started.html   )
stacklevelr   Nzitutorial.open_rasterio depends on pooch to download and manage datasets. To proceed please install pooch.r$   zunknown rasterio dataset: r&   )warningswarnDeprecationWarningr	   r   r(   r)   r   external_rasterio_urlsr   
ValueErrorr0   _open_rasterior2   r+   r,   r3   )r   r   r!   r"   r4   r	   r5   r6   r'   r7   Zarrr   r   r   r      s2    -

r   )r#   c               
   O  s*   t | |}| W  5 Q R  S Q R X 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   r2   )argskwargsr8   r   r   r   load_dataset   s    %rC   )seedz
None | int)rD   r#   c              
   C  s  t j| }tt ddddgddddg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}ddddg|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   xyzwr      g?)rF   rG   rH   rI   )ZdimsZcoordsr9   g      @g   )ABZoneZtwoZthreeZfiveZxunitsZunitsZyunitsZzunitsZwunitsZAunitsZBunits)nprandomZdefault_rngr   zerosZarangeZlinspaceZstandard_normalrF   rG   rH   rI   r   attrsrL   rM   )rD   rngrL   rM   r8   r   r   r   scatter_example_dataset   s*    	

( rS   )TN)NTN)__doc__
__future__r   r   r+   r;   typingr   ZnumpyrN   Zxarray.backends.apir   r1   Zxarray.backends.rasterio_r   r@   Zxarray.core.dataarrayr   Zxarray.core.datasetr   r   r   r/   r   r   r*   r>   r   r   rC   rS   r   r   r   r   <module>   sT     T   
N)