Skip to content

Commit

Permalink
Ensure surface and grdcut loads GMTDataArray accessor info into xarray (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
weiji14 authored Jul 21, 2020
1 parent eb45675 commit 87e7d42
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions pygmt/gridding.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def surface(x=None, y=None, z=None, data=None, **kwargs):
if outfile == tmpfile.name: # if user did not set outfile, return DataArray
with xr.open_dataarray(outfile) as dataarray:
result = dataarray.load()
_ = result.gmt # load GMTDataArray accessor information
elif outfile != tmpfile.name: # if user sets an outfile, return None
result = None

Expand Down
1 change: 1 addition & 0 deletions pygmt/gridops.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def grdcut(grid, **kwargs):
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
with xr.open_dataarray(outgrid) as dataarray:
result = dataarray.load()
_ = result.gmt # load GMTDataArray accessor information
else:
result = None # if user sets an outgrid, return None

Expand Down
2 changes: 2 additions & 0 deletions pygmt/tests/test_grdcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def test_grdcut_file_in_dataarray_out():
"grdcut an input grid file, and output as DataArray"
outgrid = grdcut("@earth_relief_01d", region="0/180/0/90")
assert isinstance(outgrid, xr.DataArray)
assert outgrid.gmt.registration == 1 # Pixel registration
assert outgrid.gmt.gtype == 1 # Geographic type
# check information of the output grid
# the '@earth_relief_01d' is in pixel registration, so the grid range is
# not exactly 0/180/0/90
Expand Down
2 changes: 2 additions & 0 deletions pygmt/tests/test_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def test_surface_input_file():
fname = which("@tut_ship.xyz", download="c")
output = surface(data=fname, spacing="5m", region=[245, 255, 20, 30])
assert isinstance(output, xr.DataArray)
assert output.gmt.registration == 0 # Gridline registration
assert output.gmt.gtype == 0 # Cartesian type
return output


Expand Down

0 comments on commit 87e7d42

Please sign in to comment.