diff --git a/CHANGES.rst b/CHANGES.rst index d9e51d1c..d3a05a67 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ^^^^^^^^^ diff --git a/tests/test_spatial.py b/tests/test_spatial.py index db89704e..b7559efd 100644 --- a/tests/test_spatial.py +++ b/tests/test_spatial.py @@ -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( diff --git a/xscen/spatial.py b/xscen/spatial.py index 0fa66666..8ebe2239 100644 --- a/xscen/spatial.py +++ b/xscen/spatial.py @@ -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":