diff --git a/pygmt/src/grdimage.py b/pygmt/src/grdimage.py index 6adf6f70ffa..c8226b332d8 100644 --- a/pygmt/src/grdimage.py +++ b/pygmt/src/grdimage.py @@ -175,6 +175,14 @@ def grdimage(self, grid, **kwargs): """ kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access + # Special handling of -A option. + # For -A option, the syntax is different for GMT CLI and external wrappers. + # For GMT CLI, "gmt grdimage ingrid.nc -Aimg_out;xxx". + # For external wrappers, "gmt grdimage ingrid.nc -A > img_out.xxx". + outfile = kwargs.pop("A", None) + if outfile is not None: + kwargs["A"] = True + with Session() as lib: with lib.virtualfile_from_data( check_kind="raster", data=grid @@ -183,5 +191,6 @@ def grdimage(self, grid, **kwargs): ) as shadegrid: kwargs["I"] = shadegrid lib.call_module( - module="grdimage", args=build_arg_string(kwargs, infile=fname) + module="grdimage", + args=build_arg_string(kwargs, infile=fname, outfile=outfile), ) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index b51daedde9d..97a43812d97 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -1,12 +1,15 @@ """ Test Figure.grdimage. """ +from pathlib import Path + import numpy as np import pytest import xarray as xr from pygmt import Figure from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import check_figures_equal @@ -241,3 +244,16 @@ def test_grdimage_central_meridians_and_standard_parallels(grid, proj_type, lon0 ) fig_test.grdimage(grid, projection=f"{proj_type}{lon0}/{lat0}/15c", cmap="geo") return fig_ref, fig_test + + +def test_grdimage_img_out(grid): + """ + Test that the img_out (-A) parameter works as expected. + """ + fig = Figure() + for suffix in [".png", ".jpg", ".tiff", ".pdf=PDF"]: + with GMTTempFile(suffix=suffix) as tmpfile: + fig.grdimage(grid, cmap="earth", projection="W0/6i", img_out=tmpfile.name) + # Remove the driver string + filename = tmpfile.name.split("=")[0] + assert Path(filename).stat().st_size > 0