From dd1566636190565e04c5b3092272823a72388526 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Mon, 15 Apr 2024 17:24:28 -0400 Subject: [PATCH 1/2] Guess cf coords before subsetting --- tests/test_spatial.py | 14 ++++++++++++++ xscen/spatial.py | 3 +++ 2 files changed, 17 insertions(+) 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..c018e04d 100644 --- a/xscen/spatial.py +++ b/xscen/spatial.py @@ -183,6 +183,9 @@ def subset( UserWarning, ) + if "latitude" not in ds.cf: + ds = ds.cf.guess_coord_axis() + if method == "gridpoint": ds_subset = _subset_gridpoint(ds, name=name, **kwargs) elif method == "bbox": From 1276901b8d771e56988eeab6e0b1d9453e76a8d1 Mon Sep 17 00:00:00 2001 From: Pascal Bourgault Date: Mon, 15 Apr 2024 17:29:29 -0400 Subject: [PATCH 2/2] update changes - test both dims --- CHANGES.rst | 1 + xscen/spatial.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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/xscen/spatial.py b/xscen/spatial.py index c018e04d..8ebe2239 100644 --- a/xscen/spatial.py +++ b/xscen/spatial.py @@ -183,7 +183,7 @@ def subset( UserWarning, ) - if "latitude" not in ds.cf: + if "latitude" not in ds.cf or "longitude" not in ds.cf: ds = ds.cf.guess_coord_axis() if method == "gridpoint":