diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index fccdfdb5920..ef55a78de44 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1554,6 +1554,7 @@ def virtualfile_in( # noqa: PLR0912 extra_arrays=None, required_z=False, required_data=True, + _grid_mode="GMT_IN", # An internal parameter to workaround GMT API bugs. ): """ Store any data inside a virtual file. @@ -1638,7 +1639,7 @@ def virtualfile_in( # noqa: PLR0912 }[kind] # Ensure the data is an iterable (Python list or tuple) - if kind in {"geojson", "grid", "image", "file", "arg"}: + if kind in {"geojson", "image", "file", "arg"}: if kind == "image" and data.dtype != "uint8": msg = ( f"Input image has dtype: {data.dtype} which is unsupported, " @@ -1649,6 +1650,8 @@ def virtualfile_in( # noqa: PLR0912 ) warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) _data = (data,) if not isinstance(data, pathlib.PurePath) else (str(data),) + elif kind == "grid": + _data = (data, _grid_mode) elif kind == "vectors": _data = [np.atleast_1d(x), np.atleast_1d(y)] if z is not None: @@ -1873,7 +1876,7 @@ def read_virtualfile( return ctp.cast(pointer, ctp.POINTER(dtype)) @contextlib.contextmanager - def virtualfile_from_xrgrid(self, xrgrid): + def virtualfile_from_xrgrid(self, xrgrid, _grid_mode="GMT_IN"): """ Create a virtual file from an xarray.DataArray object. """ @@ -1906,7 +1909,7 @@ def virtualfile_from_xrgrid(self, xrgrid): matrix = np.pad(matrix, self["GMT_PAD_DEFAULT"]).astype(np.float32) gmtgrid.contents.data = matrix.ctypes.data_as(ctp.POINTER(gmt_grdfloat)) - with self.open_virtualfile(family, geometry, "GMT_IN", gmtgrid) as vfile: + with self.open_virtualfile(family, geometry, _grid_mode, gmtgrid) as vfile: yield vfile def virtualfile_to_dataset( diff --git a/pygmt/src/grdsample.py b/pygmt/src/grdsample.py index 27675ff3744..fea98c50819 100644 --- a/pygmt/src/grdsample.py +++ b/pygmt/src/grdsample.py @@ -81,7 +81,9 @@ def grdsample(grid, outgrid: str | None = None, **kwargs): """ with Session() as lib: with ( - lib.virtualfile_in(check_kind="raster", data=grid) as vingrd, + lib.virtualfile_in( + check_kind="raster", data=grid, _mode="GMT_IN|GMT_IS_REFERENCE" + ) as vingrd, lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd, ): kwargs["G"] = voutgrd