Skip to content

Commit aaa981a

Browse files
committed
Remove usage of xarray.tutorial_open_dataset()`
1 parent 903eff7 commit aaa981a

File tree

1 file changed

+53
-89
lines changed

1 file changed

+53
-89
lines changed

xcdat/tutorial.py

+53-89
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,14 @@
33
import os
44
import pathlib
55
import sys
6-
from typing import TYPE_CHECKING, Dict, List, Tuple
6+
from typing import Dict, List, Tuple
77

88
import xarray as xr
9-
from xarray.tutorial import (
10-
_construct_cache_dir,
11-
file_formats,
12-
)
13-
from xarray.tutorial import (
14-
open_dataset as xr_tut_open_dataset,
15-
)
9+
from xarray.tutorial import _construct_cache_dir, file_formats
1610

1711
import xcdat.bounds # noqa: F401
1812
from xcdat.axis import CFAxisKey
1913

20-
if TYPE_CHECKING:
21-
import os
22-
23-
from xarray.backends.api import T_Engine
24-
2514
try:
2615
import pooch
2716
except ImportError as e:
@@ -31,7 +20,7 @@
3120
) from e
3221

3322

34-
_default_cache_dir_name = "xcdat_tutorial_data"
23+
DEFAULT_CACHE_DIR_NAME = "xcdat_tutorial_data"
3524
base_url = "https://github.com/xCDAT/xcdat-data"
3625
version = "main"
3726

@@ -57,95 +46,70 @@
5746
def open_dataset(
5847
name: str,
5948
cache: bool = True,
60-
cache_dir: None | str | os.PathLike = None,
49+
cache_dir: None | str | os.PathLike = DEFAULT_CACHE_DIR_NAME,
6150
add_bounds: List[CFAxisKey] | Tuple[CFAxisKey, ...] | None = ("X", "Y"),
62-
*,
63-
engine: T_Engine = None,
64-
**kws,
51+
**kargs,
6552
) -> xr.Dataset:
6653
"""
67-
Open a dataset from the online repository (requires internet).
54+
Open a dataset from the online repository (requires internet).
6855
69-
If an Xarray tutorial dataset is specified, this function will use the
70-
``xarray.tutorial.open_dataset()`` function. This function is mostly based
71-
on ``xarray.tutorial.open_dataset()`` with some modifications such as adding
72-
bounds to the dataset.
56+
This function is mostly based on ``xarray.tutorial.open_dataset()`` with
57+
some modifications, including adding missing bounds to the dataset.
7358
7459
If a local copy is found then always use that to avoid network traffic.
7560
76-
Available Xarray datasets:
77-
78-
* ``"air_temperature"``: NCEP reanalysis subset
79-
* ``"air_temperature_gradient"``: NCEP reanalysis subset with approximate x,y gradients
80-
* ``"basin_mask"``: Dataset with ocean basins marked using integers
81-
* ``"ASE_ice_velocity"``: MEaSUREs InSAR-Based Ice Velocity of the Amundsen Sea Embayment, Antarctica, Version 1
82-
* ``"rasm"``: Output of the Regional Arctic System Model (RASM)
83-
* ``"ROMS_example"``: Regional Ocean Model System (ROMS) output
84-
* ``"tiny"``: small synthetic dataset with a 1D data variable
85-
* ``"era5-2mt-2019-03-uk.grib"``: ERA5 temperature data over the UK
86-
* ``"eraint_uvz"``: data from ERA-Interim reanalysis, monthly averages of upper level data
87-
* ``"ersstv5"``: NOAA's Extended Reconstructed Sea Surface Temperature monthly averages
88-
89-
Available xCDAT datasets:
90-
91-
* ``"pr_amon_access"``: Monthly precipitation data from the ACCESS-ESM1-5 model.
92-
* ``"so_omon_cesm2"``: Monthly ocean salinity data from the CESM2 model.
93-
* ``"tas_amon_access"``: Monthly near-surface air temperature from the ACCESS-ESM1-5 model.
94-
* ``"tas_amon_canesm5"``: Monthly near-surface air temperature from the CanESM5 model.
95-
* ``"thetao_omon_cesm2"``: Monthly ocean potential temperature from the CESM2 model.
96-
* ``"cl_amon_e3sm2"``: Monthly cloud fraction data from the E3SM-2-0 model.
97-
* ``"ta_amon_e3sm2"``: Monthly air temperature data from the E3SM-2-0 model.
98-
99-
Parameters
100-
----------
101-
name : str
102-
Name of the file containing the dataset.
103-
e.g. 'air_temperature'
104-
cache_dir : path-like, optional
105-
The directory in which to search for and write cached data.
106-
cache : bool, optional
107-
If True, then cache data locally for use on subsequent calls
108-
add_bounds : List[CFAxisKey] | Tuple[CFAxisKey] | None, optional
109-
List or tuple of axis keys for which to add bounds, by default
110-
("X", "Y").
111-
**kws : dict, optional
112-
Passed to ``xarray.open_dataset`` or ``xcdat.open_dataset``.
61+
Available xCDAT datasets:
62+
63+
* ``"pr_amon_access"``: Monthly precipitation data from the ACCESS-ESM1-5 model.
64+
* ``"so_omon_cesm2"``: Monthly ocean salinity data from the CESM2 model.
65+
* ``"tas_amon_access"``: Monthly near-surface air temperature from the ACCESS-ESM1-5 model.
66+
* ``"tas_amon_canesm5"``: Monthly near-surface air temperature from the CanESM5 model.
67+
* ``"thetao_omon_cesm2"``: Monthly ocean potential temperature from the CESM2 model.
68+
* ``"cl_amon_e3sm2"``: Monthly cloud fraction data from the E3SM-2-0 model.
69+
* ``"ta_amon_e3sm2"``: Monthly air temperature data from the E3SM-2-0 model.
70+
71+
Parameters
72+
----------
73+
name : str
74+
Name of the file containing the dataset.
75+
e.g. 'air_temperature'
76+
cache_dir : path-like, optional
77+
The directory in which to search for and write cached data.
78+
cache : bool, optional
79+
If True, then cache data locally for use on subsequent calls
80+
add_bounds : List[CFAxisKey] | Tuple[CFAxisKey] | None, optional
81+
List or tuple of axis keys for which to add bounds, by default
82+
("X", "Y").
83+
**kargs : dict, optional
84+
Passed to ``xcdat.open_dataset``.
11385
"""
114-
if name in XARRAY_DATASETS:
115-
ds = xr_tut_open_dataset(
116-
name=name, cache=cache, cache_dir=cache_dir, engine=engine, **kws
117-
)
86+
# Avoid circular import in __init__.py
87+
from xcdat.dataset import open_dataset as xc_open_dataset
11888

119-
if add_bounds is not None:
120-
ds = ds.bounds.add_missing_bounds(axes=add_bounds)
121-
else:
122-
# Avoid circular import.
123-
from xcdat.dataset import open_dataset as xc_open_dataset
89+
logger = pooch.get_logger()
90+
logger.setLevel("WARNING")
12491

125-
logger = pooch.get_logger()
126-
logger.setLevel("WARNING")
92+
cache_dir = _construct_cache_dir(cache_dir)
12793

128-
cache_dir = _construct_cache_dir(cache_dir)
129-
130-
filename = XCDAT_DATASETS.get(name)
131-
if filename is None:
132-
raise ValueError(
133-
f"Dataset {name} not found. Available xcdat datasets are: {XCDAT_DATASETS.keys()}"
134-
)
94+
filename = XCDAT_DATASETS.get(name)
95+
if filename is None:
96+
raise ValueError(
97+
f"Dataset {name} not found. Available xcdat datasets are: {XCDAT_DATASETS.keys()}"
98+
)
13599

136-
path = pathlib.Path(filename)
137-
url = f"{base_url}/raw/{version}/{path.name}"
100+
path = pathlib.Path(filename)
101+
url = f"{base_url}/raw/{version}/{path.name}"
138102

139-
headers = {"User-Agent": f"xcdat {sys.modules['xcdat'].__version__}"}
140-
downloader = pooch.HTTPDownloader(headers=headers)
103+
headers = {"User-Agent": f"xcdat {sys.modules['xcdat'].__version__}"}
104+
downloader = pooch.HTTPDownloader(headers=headers)
141105

142-
filepath = pooch.retrieve(
143-
url=url, known_hash=None, path=cache_dir, downloader=downloader
144-
)
145-
ds = xc_open_dataset(filepath, **kws, add_bounds=add_bounds)
106+
filepath = pooch.retrieve(
107+
url=url, known_hash=None, path=cache_dir, downloader=downloader
108+
)
109+
ds = xc_open_dataset(filepath, **kargs, add_bounds=add_bounds)
146110

147-
if not cache:
148-
ds = ds.load()
149-
pathlib.Path(filepath).unlink()
111+
if not cache:
112+
ds = ds.load()
113+
pathlib.Path(filepath).unlink()
150114

151115
return ds

0 commit comments

Comments
 (0)