Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygmt.grd2cpt & pygmt.makecpt: Simplify the logic for dealing with CPT output #3334

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions pygmt/src/grd2cpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
seisman marked this conversation as resolved.
Show resolved Hide resolved
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),
)
19 changes: 9 additions & 10 deletions pygmt/src/makecpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
seisman marked this conversation as resolved.
Show resolved Hide resolved
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))