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

Make coordinate detection more robust #3437

Merged
merged 1 commit into from
Mar 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/metpy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,16 @@ def assign_coordinates(self, coordinates):
def _generate_coordinate_map(self):
"""Generate a coordinate map via CF conventions and other methods."""
coords = self._data_array.coords.values()
# Parse all the coordinates, attempting to identify x, longitude, y, latitude,
# vertical, time
coord_lists = {'time': [], 'vertical': [], 'y': [], 'latitude': [], 'x': [],
'longitude': []}
# Parse all the coordinates, attempting to identify longitude, latitude, x, y,
# time, vertical, in that order.
coord_lists = {'longitude': [], 'latitude': [], 'x': [], 'y': [], 'time': [],
'vertical': []}
for coord_var in coords:
# Identify the coordinate type using check_axis helper
for axis in coord_lists:
if check_axis(coord_var, axis):
coord_lists[axis].append(coord_var)
break # Ensure a coordinate variable only goes to one axis

# Fill in x/y with longitude/latitude if x/y not otherwise present
for geometric, graticule in (('y', 'latitude'), ('x', 'longitude')):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ def test_missing_grid_mapping_invalid(test_var_multidim_no_xy):
assert 'metpy_crs' not in data_var.coords


def test_xy_not_vertical(test_ds):
"""Test not detecting x/y as a vertical coordinate based on metadata."""
test_ds.x.attrs['positive'] = 'up'
test_ds.y.attrs['positive'] = 'up'
data_var = test_ds.metpy.parse_cf('Temperature')
assert data_var.metpy.vertical.identical(data_var.coords['isobaric'])


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),
Expand Down