Skip to content

Commit 684315d

Browse files
committed
Allow GMTDataArrayAccessor to work on sliced datacubes
If `grdinfo` can't read the source NetCDF file as it is an n-dimensional datacube (instead of a 2D grid), we fallback to setting the default gridline registration and Cartesian grid type.
1 parent 5cb1035 commit 684315d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pygmt/accessors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, xarray_obj):
3434
self._registration, self._gtype = map(
3535
int, grdinfo(self._source, per_column="n", o="10,11").split()
3636
)
37-
except KeyError:
37+
except (KeyError, ValueError):
3838
self._registration = 0 # Default to Gridline registration
3939
self._gtype = 0 # Default to Cartesian grid type
4040

pygmt/tests/test_accessor.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Test the behaviour of the GMTDataArrayAccessor class.
33
"""
4+
import os
5+
46
import pytest
57
import xarray as xr
68
from pygmt import which
@@ -66,3 +68,23 @@ def test_accessor_set_non_boolean():
6668

6769
with pytest.raises(GMTInvalidInput):
6870
grid.gmt.gtype = 2
71+
72+
73+
def test_accessor_sliced_datacube():
74+
"""
75+
Check that a 2D grid which is sliced from an n-dimensional datacube works
76+
with accessor methods. This is a regression test for
77+
https://github.com/GenericMappingTools/pygmt/issues/1578.
78+
"""
79+
try:
80+
fname = which(
81+
"https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc",
82+
download="u",
83+
)
84+
dataset = xr.open_dataset(fname)
85+
grid = dataset.sel(level=500, month=1, drop=True).z
86+
87+
assert grid.gmt.registration == 0 # gridline registration
88+
assert grid.gmt.gtype == 0 # cartesian coordinate type
89+
finally:
90+
os.remove(fname)

0 commit comments

Comments
 (0)