From 9af6b9c54fae6425eb6f1695d018aa86098f8c24 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 4 Dec 2024 22:19:58 +0800 Subject: [PATCH] Add workaround for grdinfo --- pygmt/clib/session.py | 13 +++++++++++-- pygmt/src/grdinfo.py | 5 ++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index abfde28c70c..7314c532cd9 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -209,7 +209,7 @@ def info(self) -> dict[str, str]: } return self._info - def __init__(self, in_mode: str = "GMT_IN"): + def __init__(self, in_mode: str = "GMT_IN", grid_as_matrix: bool = False): """ Initialize to store session-level variables. @@ -220,8 +220,12 @@ def __init__(self, in_mode: str = "GMT_IN"): :meth:`pygmt.clib.Session.virtualfile_from_xrgrid` only. For some modules (e.g., ``grdgradient`` and ``grdsample``), we may need ``"GMT_IN|GMT_IS_REFERENCE"`` due to potential upstream bugs in GMT. + grid_as_matrix + Whether to treat the grid as a matrix. Defaults to ``False``. If ``True``, + will use the :meth:`pygmt.clib.Session.virtualfile_from_grid` method instead """ self._in_mode = in_mode + self._grid_as_matrix = grid_as_matrix def __enter__(self): """ @@ -1812,7 +1816,7 @@ def virtualfile_from_stringio( seg.header = None seg.text = None - def virtualfile_in( + def virtualfile_in( # noqa: PLR0912 self, check_kind=None, data=None, @@ -1910,6 +1914,11 @@ def virtualfile_in( "vectors": self.virtualfile_from_vectors, }[kind] + # Patch for upstream bugs where grdinfo -L doesn't work with + # virtualfile_from_xrgrid. + if kind == "grid" and self._grid_as_matrix is True: + _virtualfile_from = self.virtualfile_from_grid + # "_data" is the data that will be passed to the _virtualfile_from function. # "_data" defaults to "data" but should be adjusted for some cases. _data = data diff --git a/pygmt/src/grdinfo.py b/pygmt/src/grdinfo.py index 3194165e528..59d2547251f 100644 --- a/pygmt/src/grdinfo.py +++ b/pygmt/src/grdinfo.py @@ -110,8 +110,11 @@ def grdinfo(grid, **kwargs): info : str A string with information about the grid. """ + # Workaround for upstream bug https://github.com/GenericMappingTools/gmt/issues/8525 + grid_as_matrix = bool(kwargs.get("L")) + with GMTTempFile() as outfile: - with Session() as lib: + with Session(grid_as_matrix=grid_as_matrix) as lib: with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd: lib.call_module( module="grdinfo",