Skip to content

Commit c8f31c9

Browse files
committed
Add an internal parameter to make grdsample work
1 parent 5fc4a20 commit c8f31c9

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

pygmt/clib/session.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,7 @@ def virtualfile_in( # noqa: PLR0912
15541554
extra_arrays=None,
15551555
required_z=False,
15561556
required_data=True,
1557+
_grid_mode="GMT_IN", # An internal parameter to workaround GMT API bugs.
15571558
):
15581559
"""
15591560
Store any data inside a virtual file.
@@ -1638,7 +1639,7 @@ def virtualfile_in( # noqa: PLR0912
16381639
}[kind]
16391640

16401641
# Ensure the data is an iterable (Python list or tuple)
1641-
if kind in {"geojson", "grid", "image", "file", "arg"}:
1642+
if kind in {"geojson", "image", "file", "arg"}:
16421643
if kind == "image" and data.dtype != "uint8":
16431644
msg = (
16441645
f"Input image has dtype: {data.dtype} which is unsupported, "
@@ -1649,6 +1650,8 @@ def virtualfile_in( # noqa: PLR0912
16491650
)
16501651
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
16511652
_data = (data,) if not isinstance(data, pathlib.PurePath) else (str(data),)
1653+
elif kind == "grid":
1654+
_data = (data, _grid_mode)
16521655
elif kind == "vectors":
16531656
_data = [np.atleast_1d(x), np.atleast_1d(y)]
16541657
if z is not None:
@@ -1873,7 +1876,7 @@ def read_virtualfile(
18731876
return ctp.cast(pointer, ctp.POINTER(dtype))
18741877

18751878
@contextlib.contextmanager
1876-
def virtualfile_from_xrgrid(self, xrgrid):
1879+
def virtualfile_from_xrgrid(self, xrgrid, _grid_mode="GMT_IN"):
18771880
"""
18781881
Create a virtual file from an xarray.DataArray object.
18791882
"""
@@ -1906,7 +1909,7 @@ def virtualfile_from_xrgrid(self, xrgrid):
19061909
matrix = np.pad(matrix, self["GMT_PAD_DEFAULT"]).astype(np.float32)
19071910
gmtgrid.contents.data = matrix.ctypes.data_as(ctp.POINTER(gmt_grdfloat))
19081911

1909-
with self.open_virtualfile(family, geometry, "GMT_IN", gmtgrid) as vfile:
1912+
with self.open_virtualfile(family, geometry, _grid_mode, gmtgrid) as vfile:
19101913
yield vfile
19111914

19121915
def virtualfile_to_dataset(

pygmt/src/grdsample.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def grdsample(grid, outgrid: str | None = None, **kwargs):
8181
"""
8282
with Session() as lib:
8383
with (
84-
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
84+
lib.virtualfile_in(
85+
check_kind="raster", data=grid, _grid_mode="GMT_IN|GMT_IS_REFERENCE"
86+
) as vingrd,
8587
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
8688
):
8789
kwargs["G"] = voutgrd

0 commit comments

Comments
 (0)