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
37 changes: 37 additions & 0 deletions tests/test_20_open_dataset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import contextlib
from collections.abc import Hashable
from pathlib import Path
from typing import Any

import pytest
import xarray as xr
from xarray import AlignmentError

does_not_raise = contextlib.nullcontext


@pytest.mark.parametrize("download", [True, False])
Expand Down Expand Up @@ -142,3 +146,36 @@ def test_time_selection(
sel=sel,
)
assert ds.sizes["time"] == expected_size


@pytest.mark.parametrize(
"ignore_spatial_coords, raises",
[
("areacella", does_not_raise()),
(None, pytest.raises(AlignmentError)),
],
)
def test_ignore_spatial_coords(
tmp_path: Path,
index_node: str,
ignore_spatial_coords: str | None,
raises: contextlib.nullcontext,
) -> None:
esgpull_path = tmp_path / "esgpull"
selection = {
"query": [
'"CMIP6.CMIP.MPI-M.MPI-ESM1-2-HR.historical.r1i1p1f1.fx.areacella.gn.v20190710.areacella_fx_MPI-ESM1-2-HR_historical_r1i1p1f1_gn.nc"',
'"CMIP6.CMIP.MPI-M.MPI-ESM1-2-HR.historical.r1i1p1f1.Amon.tas.gn.v20190710.tas_Amon_MPI-ESM1-2-HR_historical_r1i1p1f1_gn_185001-185412.nc"',
]
}

with raises:
ds = xr.open_dataset(
selection, # type: ignore[arg-type]
esgpull_path=esgpull_path,
engine="esgf",
index_node=index_node,
chunks={},
ignore_spatial_coords=ignore_spatial_coords,
)
assert {"lat", "lon"} <= set(ds.variables)
12 changes: 12 additions & 0 deletions xarray_esgf/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def _open_datasets(
download: bool,
show_progress: bool,
sel: dict[Hashable, Any],
ignore_spatial_coords: str | Iterable[str],
) -> dict[str, Dataset]:
sel = {
k: slice(*v["slice"]) if isinstance(v, dict) else v for k, v in sel.items()
Expand All @@ -148,6 +149,10 @@ def _open_datasets(
concat_dims = [concat_dims]
concat_dims = concat_dims or []

if isinstance(ignore_spatial_coords, str):
ignore_spatial_coords = {ignore_spatial_coords}
ignore_spatial_coords = set(ignore_spatial_coords)

if download:
self.download()

Expand All @@ -162,7 +167,12 @@ 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})

if ignore_spatial_coords.intersection(ds.variables):
ds = ds.drop_vars(set(ds.variables) & {"lat", "lon"})

if all(ds.sizes.values()):
grouped_objects[file.dataset_id].append(ds.drop_encoding())

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

obj = combine_datasets([ds.reset_coords() for ds in combined_datasets.values()])
Expand Down
3 changes: 3 additions & 0 deletions xarray_esgf/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def open_dataset( # type: ignore[override]
download: bool = False,
show_progress: bool = True,
sel: dict[Hashable, Any] | None = None,
ignore_spatial_coords: str | Iterable[str] | None = None,
) -> Dataset:
client = Client(
selection=filename_or_obj,
Expand All @@ -38,6 +39,7 @@ def open_dataset( # type: ignore[override]
download=download,
show_progress=show_progress,
sel=sel,
ignore_spatial_coords=ignore_spatial_coords,
)

open_dataset_parameters = (
Expand All @@ -51,6 +53,7 @@ def open_dataset( # type: ignore[override]
"download",
"show_progress",
"sel",
"ignore_spatial_coords",
)

def guess_can_open(self, filename_or_obj: Any) -> bool:
Expand Down