Skip to content

Commit 3448972

Browse files
committed
Alias drapegrid (G) for grdview
Included one test for a valid (xarray.DataArray) drapegrid input, and one test for an invalid input. Also using contextlib.ExitStack() as we have more than one file_context (one for reliefgrid, one for drapegrid) to enter and exit properly (see https://stackoverflow.com/questions/3024925/create-a-with-block-on-several-context-managers).
1 parent 5551eeb commit 3448972

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

pygmt/base_plotting.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Base class with plot generating commands.
33
Does not define any special non-GMT methods (savefig, show, etc).
44
"""
5+
import contextlib
6+
57
from .clib import Session
68
from .exceptions import GMTInvalidInput
79
from .helpers import (
@@ -240,6 +242,7 @@ def grdimage(self, grid, **kwargs):
240242
JZ="zsize",
241243
B="frame",
242244
C="cmap",
245+
G="drapegrid",
243246
N="plane",
244247
Q="surftype",
245248
Wc="contourpen",
@@ -271,6 +274,13 @@ def grdview(self, reliefgrid, **kwargs):
271274
cmap (C) : str
272275
The name of the color palette table to use.
273276
277+
drapegrid (G) : str or xarray.DataArray
278+
The file name or a DataArray of the image grid to be draped on top of the
279+
relief provided by reliefgrid. [Default determines colors from reliefgrid].
280+
Note that -Jz and -N always refers to the reliefgrid. The drapegrid only
281+
provides the information pertaining to colors, which (if drapegrid is a
282+
grid) will be looked-up via the CPT (see -C).
283+
274284
plane (N) : float or str
275285
``level[+gfill]``.
276286
Draws a plane at this z-level. If the optional color is provided via the +g
@@ -312,9 +322,22 @@ def grdview(self, reliefgrid, **kwargs):
312322
file_context = lib.virtualfile_from_grid(reliefgrid)
313323
else:
314324
raise GMTInvalidInput(
315-
"Unrecognized data type: {}".format(type(reliefgrid))
325+
f"Unrecognized data type for reliefgrid: {type(reliefgrid)}"
316326
)
317-
with file_context as fname:
327+
328+
with contextlib.ExitStack() as stack:
329+
fname = stack.enter_context(file_context)
330+
if "G" in kwargs:
331+
drapegrid = kwargs["G"]
332+
if data_kind(drapegrid) in ("file", "grid"):
333+
if data_kind(drapegrid) == "grid":
334+
drape_context = lib.virtualfile_from_grid(drapegrid)
335+
drapefile = stack.enter_context(drape_context)
336+
kwargs["G"] = drapefile
337+
else:
338+
raise GMTInvalidInput(
339+
f"Unrecognized data type for drapegrid: {type(drapegrid)}"
340+
)
318341
arg_str = " ".join([fname, build_arg_string(kwargs)])
319342
lib.call_module("grdview", arg_str)
320343

Loading

pygmt/tests/test_grdview.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,28 @@ def test_grdview_on_a_plane_styled_with_facadepen(grid):
200200
facadepen="0.5p,blue,dash",
201201
)
202202
return fig
203+
204+
205+
@pytest.mark.mpl_image_compare
206+
def test_grdview_drapegrid_dataarray(grid):
207+
"""
208+
Run grdview by passing in both a reliefgrid and drapegrid as an xarray.DataArray,
209+
setting a colormap for producing an image plot.
210+
"""
211+
drapegrid = 1.1 * grid
212+
213+
fig = Figure()
214+
fig.grdview(reliefgrid=grid, drapegrid=drapegrid, cmap="oleron", surftype="c")
215+
return fig
216+
217+
218+
def test_grdview_wrong_kind_of_drapegrid(grid):
219+
"""
220+
Run grdview using drapegrid input that is not an xarray.DataArray or file.
221+
"""
222+
dataset = grid.to_dataset() # convert xarray.DataArray to xarray.Dataset
223+
assert data_kind(dataset) == "matrix"
224+
225+
fig = Figure()
226+
with pytest.raises(GMTInvalidInput):
227+
fig.grdview(reliefgrid=grid, drapegrid=dataset)

0 commit comments

Comments
 (0)