a
    İa                     @   s   d Z ddlZddl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Zd	Zd
Zdd Zi ZdddZddddddZdd ZdddddZd ddZ	dd Zdd ZdS )!zR
Useful for:

* users learning xarray
* building tutorials in the documentation.

    N   )open_dataset)open_rasterio)	DataArray)DatasetZ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   .lib/python3.9/site-packages/xarray/tutorial.py_construct_cache_dir   s    
r   zDhttps://github.com/mapbox/rasterio/raw/1.2.1/tests/data/RGB.byte.tifzAhttps://github.com/mapbox/rasterio/raw/1.2.1/tests/data/shade.tif)zRGB.byteZshade      )Zair_temperatureZ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_installed0   s*    


r   T)enginec             
   K   s  zddl }W n. ty: } ztd|W Y d}~n
d}~0 0 | }|d t|}| tv rht|  }n\t| }	|	jsd}
|du rt	|  |	
|
}	n|	jdkr|du 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
    * ``"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

    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
    --------
    xarray.open_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   cache	cache_dirr   kwsr   eloggerr    r   Zdefault_extensionfilepathdsr   r   r   r   K   s:    &



r   c              
   K   s   zddl }W n. ty: } ztd|W Y d}~n
d}~0 0 | }|d t|}t| }|du rvtd|  |j|d|d}	t	|	fi |}
|s|

 }
t|	  |
S )a  
    Open a rasterio dataset from the online repository (requires internet).

    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/mapbox/rasterio
    r   Nzitutorial.open_rasterio depends on pooch to download and manage datasets. To proceed please install pooch.r   zunknown rasterio dataset: r   )r   r   r!   r"   r   external_rasterio_urlsr   
ValueErrorr)   _open_rasterior+   r$   r%   r,   )r   r   r-   r.   r/   r   r0   r1   r    r2   Zarrr   r   r   r      s(    '

r   c                  O   s:   t | i |}| W  d   S 1 s,0    Y  dS )z
    Open, load into memory, and close a dataset from the online repository
    (requires internet).

    See Also
    --------
    open_dataset
    N)r   r+   )argskwargsr3   r   r   r   load_dataset   s    	r9   c               
   C   s   t tg dg dtdtdddtddtjd g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 )N)r      r   r   )xyzwr   r   r   r:   r   g?)ZdimsZcoords   g      @g   )AB)ZoneZtwoZthreeZfiver>   ZxunitsZunitsZyunitsZzunitsZwunitsZAunitsZBunits)r   npZzerosZarangeZlinspaceZrandomZrandnr;   r<   r=   r>   r   attrsrA   rB   )rA   rB   r3   r   r   r   scatter_example_dataset   s(    
( rE   )TN)NTN)__doc__r	   r$   ZnumpyrC   Zbackends.apir   r*   Zbackends.rasterio_r   r6   Zcore.dataarrayr   Zcore.datasetr   r   r(   r   r   r#   r4   r   r   r9   rE   r   r   r   r   <module>   sB   	  N   
A