Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions tests/test_accessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
import xarray as xr

import xarray_subset_grid.accessor # noqa: F401


def test_data_vars_returns_empty_set_when_grid_not_recognized():
ds = xr.Dataset(
data_vars={"foo": ("x", [1, 2, 3])},
coords={"x": [0, 1, 2]},
)

with pytest.warns(UserWarning, match="no grid type found in this dataset"):
accessor = ds.xsg

assert accessor.grid is None
assert accessor.data_vars == set()
11 changes: 5 additions & 6 deletions xarray_subset_grid/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# from typing import Optional, Union
import numpy as np
import xarray as xr
from xarray.core.coordinates import DatasetCoordinates

from xarray_subset_grid.grid import Grid
from xarray_subset_grid.grids import (
Expand Down Expand Up @@ -78,18 +79,16 @@ def data_vars(self) -> set[str]:
data analysis. These can be discarded when subsetting the
dataset when they are not needed.
"""
if self._ds:
if self._grid:
return self._grid.data_vars(self._ds)
return set()

@property
def coords(self) -> set[str]:
if self._ds:
return self._ds.coords
return set()
def coords(self) -> DatasetCoordinates:
return self._ds.coords

@property
def grid_vars(self) -> set[str]:
def grid_vars(self) -> set[str] | list[str]:
"""List of grid variables.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the type annotation suggestion is good -- let's make it always a set

but is this line reqwuired?

set(self._grid.grid_vars(self._ds))

probably better to make sure that _grid._grid_vars is already a set.


These variables are used to define the grid and thus should be
Expand Down
Loading