Skip to content

Commit

Permalink
Merge pull request #3664 from dopplershift/xarray-decode
Browse files Browse the repository at this point in the history
Fix xarray support with decode_coords='all'
  • Loading branch information
dcamron authored Oct 28, 2024
2 parents e446cfc + 9951000 commit 2bb4d56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,10 @@ def parse_cf(self, varname=None, coordinates=None):

# Attempt to build the crs coordinate
crs = None
if 'grid_mapping' in var.attrs:
# Use given CF grid_mapping
proj_name = var.attrs['grid_mapping']

# Check both raw attribute and xarray-handled and moved to encoding
proj_name = var.encoding.get('grid_mapping', var.attrs.get('grid_mapping'))
if proj_name is not None:
try:
proj_var = self._dataset.variables[proj_name]
except KeyError:
Expand Down
15 changes: 6 additions & 9 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
grid_deltas_from_dataarray, preprocess_and_wrap)


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


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


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


def test_cf_parse_with_grid_mapping(test_var):
"""Test cf_parse dont delete grid_mapping attribute."""
assert test_var.grid_mapping == 'Lambert_Conformal'


def test_data_array_loc_get_with_units(test_var):
"""Test the .loc indexer on the metpy accessor."""
truth = test_var.loc[:, 850.]
Expand Down

0 comments on commit 2bb4d56

Please sign in to comment.