diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index de4a9ab5248..f6d954c3cad 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -183,13 +183,13 @@ def grd2cpt(grid, **kwargs): """ if kwargs.get("W") is not None and kwargs.get("Ww") is not None: raise GMTInvalidInput("Set only categorical or cyclic to True, not both.") + + if (output := kwargs.pop("H", None)) is not None: + kwargs["H"] = True + with Session() as lib: with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd: - if kwargs.get("H") is None: # if no output is set - arg_str = build_arg_list(kwargs, infile=vingrd) - else: # if output is set - outfile, kwargs["H"] = kwargs["H"], True - if not outfile or not isinstance(outfile, str): - raise GMTInvalidInput("'output' should be a proper file name.") - arg_str = build_arg_list(kwargs, infile=vingrd, outfile=outfile) - lib.call_module(module="grd2cpt", args=arg_str) + lib.call_module( + module="grd2cpt", + args=build_arg_list(kwargs, infile=vingrd, outfile=output), + ) diff --git a/pygmt/src/makecpt.py b/pygmt/src/makecpt.py index 695ea4c5afa..e5ef6f5c556 100644 --- a/pygmt/src/makecpt.py +++ b/pygmt/src/makecpt.py @@ -153,14 +153,11 @@ def makecpt(**kwargs): range. Note that ``cyclic=True`` cannot be set together with ``categorical=True``. """ + if kwargs.get("W") is not None and kwargs.get("Ww") is not None: + raise GMTInvalidInput("Set only categorical or cyclic to True, not both.") + + if (output := kwargs.pop("H", None)) is not None: + kwargs["H"] = True + with Session() as lib: - if kwargs.get("W") is not None and kwargs.get("Ww") is not None: - raise GMTInvalidInput("Set only categorical or cyclic to True, not both.") - if kwargs.get("H") is None: # if no output is set - arg_str = build_arg_list(kwargs) - else: # if output is set - outfile, kwargs["H"] = kwargs.pop("H"), True - if not outfile or not isinstance(outfile, str): - raise GMTInvalidInput("'output' should be a proper file name.") - arg_str = build_arg_list(kwargs, outfile=outfile) - lib.call_module(module="makecpt", args=arg_str) + lib.call_module(module="makecpt", args=build_arg_list(kwargs, outfile=output))