Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess cf coords before subsetting #384

Merged
merged 3 commits into from
Apr 16, 2024
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Internal changes
* Refactored ``xs.spatial.subset`` into smaller functions. (:pull:`367`).
* An `encoding` argument was added to ``xs.config.load_config``. (:pull:`370`).
* Various small fixes to the code to address FutureWarnings. (:pull:`380`).
* ``xs.spatial.subset`` will try to guess CF coordinate if it can't find "latitude" or "longitude" in ``ds.cf``. (:pull:`384`).

Bug fixes
^^^^^^^^^
Expand Down
14 changes: 14 additions & 0 deletions tests/test_spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ def test_subset_wrong_method(self):
with pytest.raises(ValueError, match="Subsetting type not recognized"):
xs.spatial.subset(self.ds, "wrong", lon=-70, lat=45)

def test_subset_no_attributes(self):
ds = self.ds.copy()
ds.lat.attrs = {}
ds.lon.attrs = {}
assert "latitude" not in ds.cf

xs.spatial.subset(
ds,
"bbox",
name="test",
lon_bnds=[-63, -60],
lat_bnds=[47, 50],
)


def test_dask_coords():
ds = datablock_3d(
Expand Down
3 changes: 3 additions & 0 deletions xscen/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def subset(
UserWarning,
)

if "latitude" not in ds.cf or "longitude" not in ds.cf:
ds = ds.cf.guess_coord_axis()

if method == "gridpoint":
ds_subset = _subset_gridpoint(ds, name=name, **kwargs)
elif method == "bbox":
Expand Down
Loading