Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/test_20_open_dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from collections.abc import Hashable
from pathlib import Path
from typing import Any

import pytest
import xarray as xr
Expand Down Expand Up @@ -103,3 +105,34 @@ def test_combine_coords(tmp_path: Path, index_node: str) -> None:
)
assert set(ds.coords) == {"areacella", "lat", "lon", "experiment_id", "orog"}
assert not ds.data_vars


@pytest.mark.parametrize(
"sel,expected_size",
[
({}, 12),
({"time": "2019-01"}, 1),
({"time": {"slice": ["2019-01", "2019-02"]}}, 2),
],
)
def test_time_selection(
tmp_path: Path,
index_node: str,
sel: dict[Hashable, Any],
expected_size: int,
) -> None:
esgpull_path = tmp_path / "esgpull"
selection = {
"query": [
'"tas_Amon_EC-Earth3-CC_ssp245_r1i1p1f1_gr_201901-201912.nc"',
]
}
ds = xr.open_dataset(
selection, # type: ignore[arg-type]
esgpull_path=esgpull_path,
engine="esgf",
index_node=index_node,
chunks={},
sel=sel,
)
assert ds.sizes["time"] == expected_size
21 changes: 16 additions & 5 deletions xarray_esgf/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Callable, Hashable, Iterable
from functools import cached_property
from pathlib import Path
from typing import Literal, get_args
from typing import Any, Literal, get_args

import tqdm
import xarray as xr
Expand Down Expand Up @@ -117,10 +117,15 @@ def download(self) -> list[File]:
def _open_datasets(
self,
concat_dims: DATASET_ID_KEYS | Iterable[DATASET_ID_KEYS] | None,
drop_variables: str | Iterable[str] | None = None,
download: bool = False,
show_progress: bool = True,
drop_variables: str | Iterable[str] | None,
download: bool,
show_progress: bool,
sel: dict[Hashable, Any],
) -> dict[str, Dataset]:
sel = {
k: slice(*v["slice"]) if isinstance(v, dict) else v for k, v in sel.items()
}

if isinstance(concat_dims, str):
concat_dims = [concat_dims]
concat_dims = concat_dims or []
Expand All @@ -139,6 +144,7 @@ def _open_datasets(
drop_variables=drop_variables,
storage_options={"ssl": self.verify_ssl},
)
ds = ds.sel({k: v for k, v in sel.items() if k in ds.dims})
grouped_objects[file.dataset_id].append(ds.drop_encoding())

combined_datasets = {}
Expand Down Expand Up @@ -173,9 +179,14 @@ def open_dataset(
drop_variables: str | Iterable[str] | None = None,
download: bool = False,
show_progress: bool = True,
sel: dict[Hashable, Any] | None = None,
) -> Dataset:
combined_datasets = self._open_datasets(
concat_dims, drop_variables, download, show_progress
concat_dims=concat_dims,
drop_variables=drop_variables,
download=download,
show_progress=show_progress,
sel=sel or {},
)

obj = xr.combine_by_coords(
Expand Down
4 changes: 3 additions & 1 deletion xarray_esgf/engine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Iterable
from collections.abc import Hashable, Iterable
from pathlib import Path
from typing import Any

Expand All @@ -22,6 +22,7 @@ def open_dataset( # type: ignore[override]
concat_dims: DATASET_ID_KEYS | Iterable[DATASET_ID_KEYS] | None = None,
download: bool = False,
show_progress: bool = True,
sel: dict[Hashable, Any] | None = None,
) -> Dataset:
client = Client(
selection=filename_or_obj,
Expand All @@ -36,6 +37,7 @@ def open_dataset( # type: ignore[override]
drop_variables=drop_variables,
download=download,
show_progress=show_progress,
sel=sel,
)

open_dataset_parameters = (
Expand Down