Skip to content

Commit 95f28fc

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 95f28fc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pygmt/accessors.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 24 additions & 0 deletions
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,25 @@ 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.
77+
78+
This is a regression test for
79+
https://github.com/GenericMappingTools/pygmt/issues/1578.
80+
"""
81+
try:
82+
fname = which(
83+
"https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc",
84+
download="u",
85+
)
86+
dataset = xr.open_dataset(fname)
87+
grid = dataset.sel(level=500, month=1, drop=True).z
88+
89+
assert grid.gmt.registration == 0 # gridline registration
90+
assert grid.gmt.gtype == 0 # cartesian coordinate type
91+
finally:
92+
os.remove(fname)

0 commit comments

Comments
 (0)