|
3 | 3 | import os
|
4 | 4 | import pathlib
|
5 | 5 | import sys
|
6 |
| -from typing import TYPE_CHECKING, Dict, List, Tuple |
| 6 | +from typing import Dict, List, Tuple |
7 | 7 |
|
8 | 8 | 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 |
16 | 10 |
|
17 | 11 | import xcdat.bounds # noqa: F401
|
18 | 12 | from xcdat.axis import CFAxisKey
|
19 | 13 |
|
20 |
| -if TYPE_CHECKING: |
21 |
| - import os |
22 |
| - |
23 |
| - from xarray.backends.api import T_Engine |
24 |
| - |
25 | 14 | try:
|
26 | 15 | import pooch
|
27 | 16 | except ImportError as e:
|
|
31 | 20 | ) from e
|
32 | 21 |
|
33 | 22 |
|
34 |
| -_default_cache_dir_name = "xcdat_tutorial_data" |
| 23 | +DEFAULT_CACHE_DIR_NAME = "xcdat_tutorial_data" |
35 | 24 | base_url = "https://github.com/xCDAT/xcdat-data"
|
36 | 25 | version = "main"
|
37 | 26 |
|
|
57 | 46 | def open_dataset(
|
58 | 47 | name: str,
|
59 | 48 | cache: bool = True,
|
60 |
| - cache_dir: None | str | os.PathLike = None, |
| 49 | + cache_dir: None | str | os.PathLike = DEFAULT_CACHE_DIR_NAME, |
61 | 50 | add_bounds: List[CFAxisKey] | Tuple[CFAxisKey, ...] | None = ("X", "Y"),
|
62 |
| - *, |
63 |
| - engine: T_Engine = None, |
64 |
| - **kws, |
| 51 | + **kargs, |
65 | 52 | ) -> xr.Dataset:
|
66 | 53 | """
|
67 |
| - Open a dataset from the online repository (requires internet). |
| 54 | + Open a dataset from the online repository (requires internet). |
68 | 55 |
|
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. |
73 | 58 |
|
74 | 59 | If a local copy is found then always use that to avoid network traffic.
|
75 | 60 |
|
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``. |
113 | 85 | """
|
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 |
118 | 88 |
|
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") |
124 | 91 |
|
125 |
| - logger = pooch.get_logger() |
126 |
| - logger.setLevel("WARNING") |
| 92 | + cache_dir = _construct_cache_dir(cache_dir) |
127 | 93 |
|
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 | + ) |
135 | 99 |
|
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}" |
138 | 102 |
|
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) |
141 | 105 |
|
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) |
146 | 110 |
|
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() |
150 | 114 |
|
151 | 115 | return ds
|
0 commit comments