From 75cbc9ef7735d075e487b57dd1e4d108d24f86ba Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 17 Jul 2024 15:19:41 +0800 Subject: [PATCH 1/2] pygmt.grd2cpt & pygmt.makecpt: Simplify the logic for dealing with CPT output --- pygmt/src/grd2cpt.py | 18 ++++++++++-------- pygmt/src/makecpt.py | 19 +++++++++---------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index de4a9ab5248..f7a8e52f276 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -183,13 +183,15 @@ 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: + if not isinstance(output, str) or output == "": + raise GMTInvalidInput("'output' should be a proper file name.") + 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..259933a8894 100644 --- a/pygmt/src/makecpt.py +++ b/pygmt/src/makecpt.py @@ -153,14 +153,13 @@ 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: + if not isinstance(output, str) or output == "": + raise GMTInvalidInput("'output' should be a proper file name.") + 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)) From 29c213929f95ac4d401e5e71a1744863f54341f2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 19 Jul 2024 09:51:07 +0800 Subject: [PATCH 2/2] Remove the checking of invalid output file name --- pygmt/src/grd2cpt.py | 2 -- pygmt/src/makecpt.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/pygmt/src/grd2cpt.py b/pygmt/src/grd2cpt.py index f7a8e52f276..f6d954c3cad 100644 --- a/pygmt/src/grd2cpt.py +++ b/pygmt/src/grd2cpt.py @@ -185,8 +185,6 @@ def grd2cpt(grid, **kwargs): raise GMTInvalidInput("Set only categorical or cyclic to True, not both.") if (output := kwargs.pop("H", None)) is not None: - if not isinstance(output, str) or output == "": - raise GMTInvalidInput("'output' should be a proper file name.") kwargs["H"] = True with Session() as lib: diff --git a/pygmt/src/makecpt.py b/pygmt/src/makecpt.py index 259933a8894..e5ef6f5c556 100644 --- a/pygmt/src/makecpt.py +++ b/pygmt/src/makecpt.py @@ -157,8 +157,6 @@ def makecpt(**kwargs): raise GMTInvalidInput("Set only categorical or cyclic to True, not both.") if (output := kwargs.pop("H", None)) is not None: - if not isinstance(output, str) or output == "": - raise GMTInvalidInput("'output' should be a proper file name.") kwargs["H"] = True with Session() as lib: