Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pygmt/src/grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ def grdsample(
)
aliasdict.merge(kwargs)

# Normalize filename inputs to DataArray for consistent behavior with
# DataArray inputs (avoid subtle differences in -R/-I rounding and alignment).
if not isinstance(grid, xr.DataArray):
try:
grid = xr.load_dataarray(grid, engine="gmt", raster_kind="grid")
except Exception:
# If loading fails (e.g., remote special files), fall back to original input
pass

with Session() as lib:
with (
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
Expand Down
12 changes: 12 additions & 0 deletions pygmt/tests/test_grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ def test_grdsample_registration_changes(grid):
assert translated_grid.gmt.registration is GridRegistration.GRIDLINE
registration_grid = grdsample(grid=translated_grid, registration="p")
assert registration_grid.gmt.registration is GridRegistration.PIXEL


def test_grdsample_file_vs_dataarray_same(grid, region, spacing):
"""
Ensure grdsample results are identical for filename and DataArray inputs.
"""
# Use the source file path of the static grid to avoid remote download
src = grid.encoding.get("source")
assert src
res_file = grdsample(grid=src, spacing=spacing, region=region)
res_da = grdsample(grid=grid, spacing=spacing, region=region)
xr.testing.assert_allclose(a=res_file, b=res_da)
Loading