Skip to content

Commit dcd3415

Browse files
committed
Let grdcut() accept xarray.DataArray as input
1 parent 87e7d42 commit dcd3415

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

pygmt/gridops.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def grdcut(grid, **kwargs):
4747
4848
Parameters
4949
----------
50-
grid : str
51-
The name of the input grid file.
50+
grid : str or xarray.DataArray
51+
The file name of the input grid or the grid loaded as a DataArray.
5252
outgrid : str or None
5353
The name of the output netCDF file with extension .nc to store the grid
5454
in.
@@ -94,12 +94,7 @@ def grdcut(grid, **kwargs):
9494
if kind == "file":
9595
file_context = dummy_context(grid)
9696
elif kind == "grid":
97-
raise NotImplementedError(
98-
"xarray.DataArray is not supported as the input grid yet!"
99-
)
100-
# file_context = lib.virtualfile_from_grid(grid)
101-
# See https://github.com/GenericMappingTools/gmt/pull/3532
102-
# for a feature request.
97+
file_context = lib.virtualfile_from_grid(grid)
10398
else:
10499
raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid)))
105100

pygmt/tests/test_grdcut.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,30 @@ def test_grdcut_file_in_dataarray_out():
4343

4444
def test_grdcut_dataarray_in_file_out():
4545
"grdcut an input DataArray, and output to a grid file"
46-
# Not supported yet.
47-
# See https://github.com/GenericMappingTools/gmt/pull/3532
46+
ingrid = load_earth_relief()
47+
with GMTTempFile(suffix=".nc") as tmpfile:
48+
result = grdcut(ingrid, outgrid=tmpfile.name, region="0/180/0/90")
49+
assert result is None # grdcut returns None if output to a file
50+
result = grdinfo(tmpfile.name, C=True)
51+
assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1 1\n"
4852

4953

5054
def test_grdcut_dataarray_in_dataarray_out():
51-
"grdcut an input DataArray, and output to a grid file"
52-
# Not supported yet.
53-
# See https://github.com/GenericMappingTools/gmt/pull/3532
54-
55-
56-
def test_grdcut_dataarray_in_fail():
57-
"Make sure that grdcut fails correctly if DataArray is the input grid"
58-
with pytest.raises(NotImplementedError):
59-
grid = load_earth_relief()
60-
grdcut(grid, region="0/180/0/90")
55+
"grdcut an input DataArray, and output as DataArray"
56+
ingrid = load_earth_relief()
57+
outgrid = grdcut(ingrid, region="0/180/0/90")
58+
assert isinstance(outgrid, xr.DataArray)
59+
# check information of the output grid
60+
# the '@earth_relief_01d' is in pixel registration, so the grid range is
61+
# not exactly 0/180/0/90
62+
assert outgrid.coords["lat"].data.min() == 0.5
63+
assert outgrid.coords["lat"].data.max() == 89.5
64+
assert outgrid.coords["lon"].data.min() == 0.5
65+
assert outgrid.coords["lon"].data.max() == 179.5
66+
assert outgrid.data.min() == -8182.0
67+
assert outgrid.data.max() == 5651.5
68+
assert outgrid.sizes["lat"] == 90
69+
assert outgrid.sizes["lon"] == 180
6170

6271

6372
def test_grdcut_fails():

0 commit comments

Comments
 (0)