Skip to content

Commit 87e7d42

Browse files
authored
Ensure surface and grdcut loads GMTDataArray accessor info into xarray (#539)
* Ensure surface and grdcut loads GMTDataArray accessor info into xarray For modules like `surface` and `grdcut` that output an xarray.DataArray grid, we need to ensure that the registration and gtype information is loaded properly into the xarray.DataArray before the temporary netcdf file gets deleted. * Fix pylint pointless-statement warning
1 parent eb45675 commit 87e7d42

File tree

4 files changed

+6
-0
lines changed

4 files changed

+6
-0
lines changed

Diff for: pygmt/gridding.py

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def surface(x=None, y=None, z=None, data=None, **kwargs):
9090
if outfile == tmpfile.name: # if user did not set outfile, return DataArray
9191
with xr.open_dataarray(outfile) as dataarray:
9292
result = dataarray.load()
93+
_ = result.gmt # load GMTDataArray accessor information
9394
elif outfile != tmpfile.name: # if user sets an outfile, return None
9495
result = None
9596

Diff for: pygmt/gridops.py

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def grdcut(grid, **kwargs):
113113
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
114114
with xr.open_dataarray(outgrid) as dataarray:
115115
result = dataarray.load()
116+
_ = result.gmt # load GMTDataArray accessor information
116117
else:
117118
result = None # if user sets an outgrid, return None
118119

Diff for: pygmt/tests/test_grdcut.py

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def test_grdcut_file_in_dataarray_out():
2626
"grdcut an input grid file, and output as DataArray"
2727
outgrid = grdcut("@earth_relief_01d", region="0/180/0/90")
2828
assert isinstance(outgrid, xr.DataArray)
29+
assert outgrid.gmt.registration == 1 # Pixel registration
30+
assert outgrid.gmt.gtype == 1 # Geographic type
2931
# check information of the output grid
3032
# the '@earth_relief_01d' is in pixel registration, so the grid range is
3133
# not exactly 0/180/0/90

Diff for: pygmt/tests/test_surface.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def test_surface_input_file():
2323
fname = which("@tut_ship.xyz", download="c")
2424
output = surface(data=fname, spacing="5m", region=[245, 255, 20, 30])
2525
assert isinstance(output, xr.DataArray)
26+
assert output.gmt.registration == 0 # Gridline registration
27+
assert output.gmt.gtype == 0 # Cartesian type
2628
return output
2729

2830

0 commit comments

Comments
 (0)