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

Change XArray projection warning to a logged message #976

Merged
merged 2 commits into from
Dec 10, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions metpy/tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_missing_grid_mapping():
assert 'crs' in data_var.coords


def test_missing_grid_mapping_var():
def test_missing_grid_mapping_var(caplog):
"""Test behavior when we can't find the variable pointed to by grid_mapping."""
x = xr.DataArray(np.arange(3),
attrs={'standard_name': 'projection_x_coordinate', 'units': 'radians'})
Expand All @@ -137,8 +137,11 @@ def test_missing_grid_mapping_var():
attrs={'grid_mapping': 'fixedgrid_projection'})
ds = xr.Dataset({'data': data})

with pytest.warns(UserWarning, match='Could not find'):
ds.metpy.parse_cf('data')
ds.metpy.parse_cf('data') # Should log a warning

for record in caplog.records:
assert record.levelname == 'WARNING'
assert 'Could not find' in caplog.text


def test_preprocess_xarray():
Expand Down
5 changes: 4 additions & 1 deletion metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import

import functools
import logging
import re
import warnings

Expand All @@ -17,6 +18,8 @@
readable_to_cf_axes = {'time': 'T', 'vertical': 'Z', 'y': 'Y', 'x': 'X'}
cf_to_readable_axes = {readable_to_cf_axes[key]: key for key in readable_to_cf_axes}

log = logging.getLogger(__name__)


@xr.register_dataarray_accessor('metpy')
class MetPyAccessor(object):
Expand Down Expand Up @@ -164,7 +167,7 @@ def parse_cf(self, varname=None, coordinates=None):
try:
proj_var = self._dataset.variables[proj_name]
except KeyError:
warnings.warn(
log.warning(
'Could not find variable corresponding to the value of '
'grid_mapping: {}'.format(proj_name))
else:
Expand Down