Skip to content

Commit

Permalink
Add workaround for grdinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Dec 4, 2024
1 parent 8d18a74 commit 9af6b9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 11 additions & 2 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pygmt/src/grdinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9af6b9c

Please sign in to comment.