Skip to content

Commit

Permalink
Add an internal parameter to make grdsample work
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Jul 18, 2024
1 parent 5fc4a20 commit d434bf9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, "
Expand All @@ -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:
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion pygmt/src/grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d434bf9

Please sign in to comment.