Skip to content

Commit ee2117d

Browse files
committed
TST: Add some tests for warnings in xarray handling
1 parent 3686ea7 commit ee2117d

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/metpy/xarray.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -688,15 +688,12 @@ def parse_cf(self, varname=None, coordinates=None):
688688
'Attempting to parse metpy_crs as a data variable. Unexpected merge conflicts '
689689
'may occur.'
690690
)
691-
elif 'metpy_crs' in var.coords:
692-
try:
693-
assert isinstance(var.coords['metpy_crs'].item(), CFProjection)
694-
except (ValueError, AssertionError):
695-
# Catch non-scalar and non-CFProjection coordinates
696-
warnings.warn(
697-
'metpy_crs already present as a non-CFProjection coordinate. Unexpected '
698-
'merge conflicts may occur.'
699-
)
691+
elif 'metpy_crs' in var.coords and (var.coords['metpy_crs'].size > 1 or not isinstance(
692+
var.coords['metpy_crs'].item(), CFProjection)):
693+
warnings.warn(
694+
'metpy_crs already present as a non-CFProjection coordinate. Unexpected '
695+
'merge conflicts may occur.'
696+
)
700697

701698
# Assign coordinates if the coordinates argument is given
702699
if coordinates is not None:

tests/test_xarray.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,30 @@ def test_missing_grid_mapping_var(caplog):
248248
assert 'Could not find' in caplog.text
249249

250250

251+
def test_parsecf_crs():
252+
"""Test calling `parse_cf` with the metpy_crs variable."""
253+
ds = xr.Dataset({'metpy_crs': xr.DataArray(1)})
254+
255+
with pytest.warns(UserWarning, match='Attempting to parse metpy_crs'):
256+
ds.metpy.parse_cf('metpy_crs')
257+
258+
259+
def test_parsecf_existing_scalar_crs():
260+
"""Test calling `parse_cf` on a variable with an existing scalar metpy_crs coordinate."""
261+
ds = xr.Dataset({'data': xr.DataArray(1, coords=dict(metpy_crs=1))})
262+
263+
with pytest.warns(UserWarning, match='metpy_crs already present'):
264+
ds.metpy.parse_cf('data')
265+
266+
267+
def test_parsecf_existing_vector_crs():
268+
"""Test calling `parse_cf` on a variable with an existing vector metpy_crs coordinate."""
269+
ds = xr.Dataset({'data': xr.DataArray(1, dims=('metpy_crs',), coords=(np.ones(3),))})
270+
271+
with pytest.warns(UserWarning, match='metpy_crs already present'):
272+
ds.metpy.parse_cf('data')
273+
274+
251275
def test_preprocess_and_wrap_only_preprocessing():
252276
"""Test xarray preprocessing and wrapping decorator for only preprocessing."""
253277
data = xr.DataArray(np.ones(3), attrs={'units': 'km'})

0 commit comments

Comments
 (0)