Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure.grdimage: Let the img_out parameter work as documented #2765

Closed
wants to merge 12 commits into from
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".
seisman marked this conversation as resolved.
Show resolved Hide resolved
# 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
Loading