Skip to content

Commit 600d9ae

Browse files
authored
Improve the readability of the grdview function (#751)
When I worked on PR #750 based on the implemention of the `grdview` function, I found that the `grdview` codes are a little difficult to understand. The main reason is that `contextlib.ExitStack()` is new to me. Another reason is that these two codes `fname = stack.enter_context(file_context)` and `arg_str = " ".join([fname, build_arg_string(kwargs)])` are separated by the long `if` block.
1 parent f1a10df commit 600d9ae

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

pygmt/base_plotting.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,18 +598,17 @@ def grdview(self, grid, **kwargs):
598598
raise GMTInvalidInput(f"Unrecognized data type for grid: {type(grid)}")
599599

600600
with contextlib.ExitStack() as stack:
601-
fname = stack.enter_context(file_context)
602-
if "G" in kwargs:
601+
if "G" in kwargs: # deal with kwargs["G"] if drapegrid is xr.DataArray
603602
drapegrid = kwargs["G"]
604603
if data_kind(drapegrid) in ("file", "grid"):
605604
if data_kind(drapegrid) == "grid":
606605
drape_context = lib.virtualfile_from_grid(drapegrid)
607-
drapefile = stack.enter_context(drape_context)
608-
kwargs["G"] = drapefile
606+
kwargs["G"] = stack.enter_context(drape_context)
609607
else:
610608
raise GMTInvalidInput(
611609
f"Unrecognized data type for drapegrid: {type(drapegrid)}"
612610
)
611+
fname = stack.enter_context(file_context)
613612
arg_str = " ".join([fname, build_arg_string(kwargs)])
614613
lib.call_module("grdview", arg_str)
615614

0 commit comments

Comments
 (0)