Skip to content

Commit

Permalink
TST: Add some tests for warnings in xarray handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dopplershift committed Oct 7, 2020
1 parent 3686ea7 commit ee2117d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,12 @@ def parse_cf(self, varname=None, coordinates=None):
'Attempting to parse metpy_crs as a data variable. Unexpected merge conflicts '
'may occur.'
)
elif 'metpy_crs' in var.coords:
try:
assert isinstance(var.coords['metpy_crs'].item(), CFProjection)
except (ValueError, AssertionError):
# Catch non-scalar and non-CFProjection coordinates
warnings.warn(
'metpy_crs already present as a non-CFProjection coordinate. Unexpected '
'merge conflicts may occur.'
)
elif 'metpy_crs' in var.coords and (var.coords['metpy_crs'].size > 1 or not isinstance(
var.coords['metpy_crs'].item(), CFProjection)):
warnings.warn(
'metpy_crs already present as a non-CFProjection coordinate. Unexpected '
'merge conflicts may occur.'
)

# Assign coordinates if the coordinates argument is given
if coordinates is not None:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,30 @@ def test_missing_grid_mapping_var(caplog):
assert 'Could not find' in caplog.text


def test_parsecf_crs():
"""Test calling `parse_cf` with the metpy_crs variable."""
ds = xr.Dataset({'metpy_crs': xr.DataArray(1)})

with pytest.warns(UserWarning, match='Attempting to parse metpy_crs'):
ds.metpy.parse_cf('metpy_crs')


def test_parsecf_existing_scalar_crs():
"""Test calling `parse_cf` on a variable with an existing scalar metpy_crs coordinate."""
ds = xr.Dataset({'data': xr.DataArray(1, coords=dict(metpy_crs=1))})

with pytest.warns(UserWarning, match='metpy_crs already present'):
ds.metpy.parse_cf('data')


def test_parsecf_existing_vector_crs():
"""Test calling `parse_cf` on a variable with an existing vector metpy_crs coordinate."""
ds = xr.Dataset({'data': xr.DataArray(1, dims=('metpy_crs',), coords=(np.ones(3),))})

with pytest.warns(UserWarning, match='metpy_crs already present'):
ds.metpy.parse_cf('data')


def test_preprocess_and_wrap_only_preprocessing():
"""Test xarray preprocessing and wrapping decorator for only preprocessing."""
data = xr.DataArray(np.ones(3), attrs={'units': 'km'})
Expand Down

0 comments on commit ee2117d

Please sign in to comment.