Skip to content

Commit 6c436a3

Browse files
authored
pygmt.grd2cpt & pygmt.makecpt: Simplify the logic for dealing with CPT output (#3334)
1 parent 419e931 commit 6c436a3

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

pygmt/src/grd2cpt.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ def grd2cpt(grid, **kwargs):
183183
"""
184184
if kwargs.get("W") is not None and kwargs.get("Ww") is not None:
185185
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
186+
187+
if (output := kwargs.pop("H", None)) is not None:
188+
kwargs["H"] = True
189+
186190
with Session() as lib:
187191
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
188-
if kwargs.get("H") is None: # if no output is set
189-
arg_str = build_arg_list(kwargs, infile=vingrd)
190-
else: # if output is set
191-
outfile, kwargs["H"] = kwargs["H"], True
192-
if not outfile or not isinstance(outfile, str):
193-
raise GMTInvalidInput("'output' should be a proper file name.")
194-
arg_str = build_arg_list(kwargs, infile=vingrd, outfile=outfile)
195-
lib.call_module(module="grd2cpt", args=arg_str)
192+
lib.call_module(
193+
module="grd2cpt",
194+
args=build_arg_list(kwargs, infile=vingrd, outfile=output),
195+
)

pygmt/src/makecpt.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,11 @@ def makecpt(**kwargs):
153153
range. Note that ``cyclic=True`` cannot be set together with
154154
``categorical=True``.
155155
"""
156+
if kwargs.get("W") is not None and kwargs.get("Ww") is not None:
157+
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
158+
159+
if (output := kwargs.pop("H", None)) is not None:
160+
kwargs["H"] = True
161+
156162
with Session() as lib:
157-
if kwargs.get("W") is not None and kwargs.get("Ww") is not None:
158-
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
159-
if kwargs.get("H") is None: # if no output is set
160-
arg_str = build_arg_list(kwargs)
161-
else: # if output is set
162-
outfile, kwargs["H"] = kwargs.pop("H"), True
163-
if not outfile or not isinstance(outfile, str):
164-
raise GMTInvalidInput("'output' should be a proper file name.")
165-
arg_str = build_arg_list(kwargs, outfile=outfile)
166-
lib.call_module(module="makecpt", args=arg_str)
163+
lib.call_module(module="makecpt", args=build_arg_list(kwargs, outfile=output))

0 commit comments

Comments
 (0)