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

Allow GMTDataArrayAccessor to work on sliced datacubes #1581

Merged
merged 5 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pygmt/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, xarray_obj):
self._registration, self._gtype = map(
int, grdinfo(self._source, per_column="n", o="10,11").split()
)
except KeyError:
except (KeyError, ValueError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, previous imports of sliced datacubes would result in a ValueError, so this makes an exception for it and sets the grid registration and type to a default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Sliced datacubes will fallback to using the default grid registration and type instead of raising a ValueError.

self._registration = 0 # Default to Gridline registration
self._gtype = 0 # Default to Cartesian grid type

Expand Down
24 changes: 24 additions & 0 deletions pygmt/tests/test_accessor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Test the behaviour of the GMTDataArrayAccessor class.
"""
import os

import pytest
import xarray as xr
from pygmt import which
Expand Down Expand Up @@ -66,3 +68,25 @@ def test_accessor_set_non_boolean():

with pytest.raises(GMTInvalidInput):
grid.gmt.gtype = 2


def test_accessor_sliced_datacube():
"""
Check that a 2D grid which is sliced from an n-dimensional datacube works
with accessor methods.

This is a regression test for
https://github.com/GenericMappingTools/pygmt/issues/1578.
"""
try:
fname = which(
"https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc",
download="u",
)
Comment on lines +82 to +85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to cache this file in CI?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably don't need to, since this file is on GitHub, and our CI is on GitHub too, so if GitHub is down, nothing will work 😆

with xr.open_dataset(fname) as dataset:
grid = dataset.sel(level=500, month=1, drop=True).z

assert grid.gmt.registration == 0 # gridline registration
assert grid.gmt.gtype == 0 # cartesian coordinate type
finally:
os.remove(fname)