"""Useful for:* users learning xwrf* building tutorials in the documentation."""from__future__importannotations# noqa: F401importosimportpathlibimportxarrayasxr_default_cache_dir_name='xwrf_tutorial_data'base_url='https://github.com/xarray-contrib/xwrf-data'version='main'def_construct_cache_dir(path):importpoochifisinstance(path,os.PathLike):path=os.fspath(path)elifpathisNone:path=pooch.os_cache(_default_cache_dir_name)returnpathsample_datasets={'dummy':'data/dummy.nc','dummy_attrs_only':'data/dummy_attrs_only.nc','dummy_salem_parsed':'data/dummy_salem_parsed.nc','polar_stereographic_1':'data/geo_em_d01_polarstereo.nc','polar_stereographic_2':'data/geo_em_d02_polarstereo.nc','lambert_conformal':'data/lambert_conformal_sample.nc','mercator':'data/mercator_sample.nc','tiny':'data/tiny.nc','met_em_sample':'data/met_em.d01.2005-08-28_12:00:00.nc','wrfout':'data/wrfout_d01_2099-10-01_00:00:00.nc','ideal':'data/ideal.nc',}# idea borrowed from Seaborn and Xarray
[docs]defopen_dataset(name:str,cache:bool=True,cache_dir:str|pathlib.Path=None,*,engine:str='netcdf4',**kws,)->xr.Dataset:""" 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: * ``"dummy"`` * ``"dummy_attrs_only"`` * ``"dummy_salem_parsed"`` * ``"polar_stereographic_1"`` * ``"polar_stereographic_2"`` * ``"lambert_conformal"`` * ``"mercator"`` * ``"met_em_sample"`` * ``"wrfout"`` * ``"ideal"`` Parameters ---------- name : str Name of the dataset. e.g. 'mercator' cache : bool, optional If True, then cache data locally for use on subsequent calls cache_dir : path-like, optional The directory in which to search for and write cached data. engine : str, optional Name of the backend engine to use. **kws : dict, optional Additional keyword arguments passed through to the :py:func:`~xarray.open_dataset` function. Returns ------- xarray.Dataset The dataset. """try:importpoochexceptImportErrorase:raiseImportError('tutorial.open_dataset depends on pooch to download and manage datasets.'' To proceed please install pooch using:'' `python -m pip install pooch` or `conda install -c conda-forge pooch`.')fromelogger=pooch.get_logger()logger.setLevel('WARNING')cache_dir=_construct_cache_dir(cache_dir)try:path=sample_datasets[name]exceptKeyErrorasexc:raiseKeyError(f'{name} is not a valid dataset name. Valid names include: {list(sample_datasets.keys())}.')fromexcurl=f'{base_url}/raw/{version}/{path}'# retrieve the filefilepath=pooch.retrieve(url=url,known_hash=None,path=cache_dir)ds=xr.open_dataset(filepath,engine=engine,**kws)ifnotcache:ds=ds.load()pathlib.Path(filepath).unlink()returnds
defload_dataset(*args,**kwargs)->xr.Dataset:""" Open, load into memory, and close a dataset from the online repository (requires internet) """withopen_dataset(*args,**kwargs)asds:returnds.load()