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

build_arg_string: Accept a dict containing configurable GMT parameters #2324

Merged
merged 12 commits into from
Feb 4, 2023
21 changes: 15 additions & 6 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ def dummy_context(arg):
yield arg


def build_arg_string(kwdict, infile=None, outfile=None):
def build_arg_string(kwdict, confdict=None, infile=None, outfile=None):
r"""
Convert a dict and optional input/output files into a GMT argument string.
Convert keyword dicts and input/output files into a GMT argument string.
seisman marked this conversation as resolved.
Show resolved Hide resolved

Make sure all values in ``kwdict`` have been previously converted to a
string representation using the ``kwargs_to_strings`` decorator. The only
exceptions are True, False and None.

Any lists or tuples left will be interpreted as multiple entries for the
same command line argument. For example, the kwargs entry ``'B': ['xa',
same command line option. For example, the kwargs entry ``'B': ['xa',
'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string.

Note that spaces `` `` in arguments are converted to the equivalent octal
Expand All @@ -140,6 +140,8 @@ def build_arg_string(kwdict, infile=None, outfile=None):
----------
kwdict : dict
A dict containing parsed keyword arguments.
seisman marked this conversation as resolved.
Show resolved Hide resolved
confdict : dict
A dict containing GMT's configurations.
seisman marked this conversation as resolved.
Show resolved Hide resolved
infile : str or pathlib.Path
The input file.
outfile : str or pathlib.Path
Expand All @@ -149,8 +151,10 @@ def build_arg_string(kwdict, infile=None, outfile=None):
-------
args : str
The space-delimited argument string with '-' inserted before each
keyword. The arguments are sorted alphabetically, with optional input
file at the beginning and optional output file at the end.
keyword, or '--' inserted before GMT's configuration key-value pairs.
seisman marked this conversation as resolved.
Show resolved Hide resolved
The keyword arguments are sorted alphabetically, followed by GMT's
seisman marked this conversation as resolved.
Show resolved Hide resolved
configuration key-value pairs, with optional input file at the
beginning and optional output file at the end.

Examples
--------
Expand Down Expand Up @@ -199,11 +203,12 @@ def build_arg_string(kwdict, infile=None, outfile=None):
>>> print(
... build_arg_string(
... dict(A="0", B=True, C="rainbow"),
... confdict=dict(FORMAT_DATE_MAP="o dd"),
... infile="input.txt",
... outfile="output.txt",
... )
... )
input.txt -A0 -B -Crainbow ->output.txt
input.txt -A0 -B -Crainbow --FORMAT_DATE_MAP="o dd" ->output.txt
"""
gmt_args = []

Expand All @@ -227,6 +232,10 @@ def build_arg_string(kwdict, infile=None, outfile=None):
_value = str(kwdict[key]).replace(" ", "")
gmt_args.append(rf"-{key}{_value}")
gmt_args = sorted(gmt_args)

if confdict:
gmt_args.extend(f'--{key}="{value}"' for key, value in confdict.items())

if infile:
gmt_args = [str(infile)] + gmt_args
if outfile:
Expand Down