Skip to content

Commit

Permalink
Improve the readability of the grdview function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seisman committed Dec 19, 2020
1 parent f1a10df commit df571fd
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions pygmt/base_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,18 +598,17 @@ def grdview(self, grid, **kwargs):
raise GMTInvalidInput(f"Unrecognized data type for grid: {type(grid)}")

with contextlib.ExitStack() as stack:
fname = stack.enter_context(file_context)
if "G" in kwargs:
if "G" in kwargs: # deal with kwargs["G"] if drapegrid is xr.DataArray
drapegrid = kwargs["G"]
if data_kind(drapegrid) in ("file", "grid"):
if data_kind(drapegrid) == "grid":
drape_context = lib.virtualfile_from_grid(drapegrid)
drapefile = stack.enter_context(drape_context)
kwargs["G"] = drapefile
kwargs["G"] = stack.enter_context(drape_context)
else:
raise GMTInvalidInput(
f"Unrecognized data type for drapegrid: {type(drapegrid)}"
)
fname = stack.enter_context(file_context)
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("grdview", arg_str)

Expand Down

0 comments on commit df571fd

Please sign in to comment.