Skip to content

Commit

Permalink
Figure.grdimage: Let the img_out parameter work as documented
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Oct 24, 2023
1 parent 904553d commit f911796
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pygmt/src/grdimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
)
16 changes: 16 additions & 0 deletions pygmt/tests/test_grdimage.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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

0 comments on commit f911796

Please sign in to comment.