Skip to content

Commit d35cfec

Browse files
dopplershiftkgoebber
authored andcommitted
BUG: Fix xarray support with decode_coords='all'
This support was added in pydata/xarray#2844 and handles converting the grid_mapping variable to a coordinate in xarray itself, which was incompatible with some assumptions in parse_cf(). Add some handling for the case where the grid_mapping. Also add decode_coords='all' to our full test suite and adjust a few tests as necessary.
1 parent b55497a commit d35cfec

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/metpy/xarray.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,9 +835,10 @@ def parse_cf(self, varname=None, coordinates=None):
835835

836836
# Attempt to build the crs coordinate
837837
crs = None
838-
if 'grid_mapping' in var.attrs:
839-
# Use given CF grid_mapping
840-
proj_name = var.attrs['grid_mapping']
838+
839+
# Check both raw attribute and xarray-handled and moved to encoding
840+
proj_name = var.encoding.get('grid_mapping', var.attrs.get('grid_mapping'))
841+
if proj_name is not None:
841842
try:
842843
proj_var = self._dataset.variables[proj_name]
843844
except KeyError:

tests/test_xarray.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
grid_deltas_from_dataarray, preprocess_and_wrap)
1919

2020

21-
@pytest.fixture
22-
def test_ds():
21+
@pytest.fixture(params=[True, 'all'])
22+
def test_ds(request):
2323
"""Provide an xarray dataset for testing."""
24-
return xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))
24+
return xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False),
25+
decode_coords=request.param)
2526

2627

2728
@pytest.fixture
@@ -47,7 +48,8 @@ def test_var(test_ds):
4748
def test_var_multidim_full(test_ds):
4849
"""Provide a variable with x/y coords and multidimensional lat/lon auxiliary coords."""
4950
return (test_ds[{'isobaric': [6, 12], 'y': [95, 96], 'x': [122, 123]}]
50-
.squeeze().set_coords(['lat', 'lon'])['Temperature'])
51+
.squeeze().drop_vars('Lambert_Conformal')
52+
.set_coords(['lat', 'lon'])['Temperature'])
5153

5254

5355
@pytest.fixture
@@ -676,11 +678,6 @@ def test_find_axis_number_bad_identifier(test_var):
676678
assert 'axis is not valid' in str(exc.value)
677679

678680

679-
def test_cf_parse_with_grid_mapping(test_var):
680-
"""Test cf_parse dont delete grid_mapping attribute."""
681-
assert test_var.grid_mapping == 'Lambert_Conformal'
682-
683-
684681
def test_data_array_loc_get_with_units(test_var):
685682
"""Test the .loc indexer on the metpy accessor."""
686683
truth = test_var.loc[:, 850.]

0 commit comments

Comments
 (0)