Skip to content

Commit 9a77ff2

Browse files
makecpt/grd2cpt: Refactor parameters truncate/overrule_bg/no_bg/log/continuous to the new alias system (#4061)
Co-authored-by: Michael Grund <[email protected]>
1 parent 12daf4a commit 9a77ff2

File tree

2 files changed

+95
-60
lines changed

2 files changed

+95
-60
lines changed

pygmt/src/grd2cpt.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import xarray as xr
66
from pygmt._typing import PathLike
7+
from pygmt.alias import Alias, AliasSystem
78
from pygmt.clib import Session
89
from pygmt.exceptions import GMTInvalidInput
910
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
@@ -18,22 +19,25 @@
1819
D="background",
1920
F="color_model",
2021
E="nlevels",
21-
G="truncate",
2222
H="output",
2323
I="reverse",
2424
L="limit",
25-
M="overrule_bg",
26-
N="no_bg",
27-
Q="log",
2825
R="region",
2926
T="series",
3027
V="verbose",
3128
W="categorical",
3229
Ww="cyclic",
33-
Z="continuous",
3430
)
35-
@kwargs_to_strings(G="sequence", L="sequence", R="sequence", T="sequence")
36-
def grd2cpt(grid: PathLike | xr.DataArray, **kwargs):
31+
@kwargs_to_strings(L="sequence", R="sequence", T="sequence")
32+
def grd2cpt(
33+
grid: PathLike | xr.DataArray,
34+
truncate: tuple[float, float] | None = None,
35+
overrule_bg: bool = False,
36+
no_bg: bool = False,
37+
log: bool = False,
38+
continuous: bool = False,
39+
**kwargs,
40+
):
3741
r"""
3842
Make linear or histogram-equalized color palette table from grid.
3943
@@ -78,6 +82,11 @@ def grd2cpt(grid: PathLike | xr.DataArray, **kwargs):
7882
Full GMT docs at :gmt-docs:`grd2cpt.html`.
7983
8084
{aliases}
85+
- G = truncate
86+
- M = overrule_bg
87+
- N = no_bg
88+
- Q = log
89+
- Z = continuous
8190
8291
Parameters
8392
----------
@@ -124,12 +133,12 @@ def grd2cpt(grid: PathLike | xr.DataArray, **kwargs):
124133
refers to the number of such boundaries and not the number of slices.
125134
For details on array creation, see
126135
:gmt-docs:`makecpt.html#generate-1d-array`.
127-
truncate : list or str
128-
*zlow/zhigh*.
129-
Truncate the incoming CPT so that the lowest and highest z-levels are
130-
to *zlow* and *zhigh*. If one of these equal NaN then we leave that
131-
end of the CPT alone. The truncation takes place before any resampling.
132-
See also :gmt-docs:`reference/features.html#manipulating-cpts`.
136+
truncate
137+
(*zlow*, *zhigh*).
138+
Truncate the incoming CPT so that the lowest and highest z-levels are to *zlow*
139+
and *zhigh*. If one of these equals NaN, then we leave that end of the CPT
140+
alone. The truncation takes place before any resampling. See also
141+
:gmt-docs:`reference/features.html#manipulating-cpts`.
133142
output : str
134143
Optional. The file name with extension .cpt to store the generated CPT
135144
file. If not given or ``False`` [Default], saves the CPT as the current
@@ -142,22 +151,22 @@ def grd2cpt(grid: PathLike | xr.DataArray, **kwargs):
142151
happens before ``truncate`` and ``series`` values are used so the
143152
latter must be compatible with the changed z-range. See also
144153
:gmt-docs:`reference/features.html#manipulating-cpts`.
145-
overrule_bg : str
146-
Overrule background, foreground, and NaN colors specified in the master
147-
CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`,
148-
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in
149-
the :gmt-docs:`gmt.conf <gmt.conf>` file. When combined with
150-
``background``, only :gmt-term:`COLOR_NAN` is considered.
151-
no_bg : bool
152-
Do not write out the background, foreground, and NaN-color fields
153-
[Default will write them, i.e. ``no_bg=False``].
154-
log : bool
155-
For logarithmic interpolation scheme with input given as logarithms.
156-
Expects input z-values provided via ``series`` to be log10(*z*),
157-
assigns colors, and writes out *z*.
158-
continuous : bool
159-
Force a continuous CPT when building from a list of colors and a list
160-
of z-values [Default is None, i.e. discrete values].
154+
overrule_bg
155+
Overrule background, foreground, and NaN colors specified in the master CPT with
156+
the values of the parameters :gmt-term:`COLOR_BACKGROUND`,
157+
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in the
158+
:gmt-docs:`gmt.conf <gmt.conf>` file or by :func:`pygmt.config`. When combined
159+
with ``background``, only :gmt-term:`COLOR_NAN` is considered.
160+
no_bg
161+
Do not write out the background, foreground, and NaN-color fields [Default will
162+
write them, i.e. ``no_bg=False``].
163+
log
164+
For logarithmic interpolation scheme with input given as logarithms. Expects
165+
input z-values provided via ``series`` to be log10(*z*), assigns colors, and
166+
writes out *z*.
167+
continuous
168+
Force a continuous CPT when building from a list of colors and a list of
169+
z-values [Default is False, i.e. discrete CPT].
161170
categorical : bool
162171
Do not interpolate the input color table but pick the output colors
163172
starting at the beginning of the color table, until colors for all
@@ -190,9 +199,17 @@ def grd2cpt(grid: PathLike | xr.DataArray, **kwargs):
190199
if (output := kwargs.pop("H", None)) is not None:
191200
kwargs["H"] = True
192201

202+
aliasdict = AliasSystem(
203+
G=Alias(truncate, name="truncate", sep="/", size=2),
204+
M=Alias(overrule_bg, name="overrule_bg"),
205+
N=Alias(no_bg, name="no_bg"),
206+
Q=Alias(log, name="log"),
207+
Z=Alias(continuous, name="continuous"),
208+
).merge(kwargs)
209+
193210
with Session() as lib:
194211
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
195212
lib.call_module(
196213
module="grd2cpt",
197-
args=build_arg_list(kwargs, infile=vingrd, outfile=output),
214+
args=build_arg_list(aliasdict, infile=vingrd, outfile=output),
198215
)

pygmt/src/makecpt.py

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
makecpt - Make GMT color palette tables.
33
"""
44

5+
from pygmt.alias import Alias, AliasSystem
56
from pygmt.clib import Session
67
from pygmt.exceptions import GMTInvalidInput
78
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
@@ -13,20 +14,22 @@
1314
C="cmap",
1415
D="background",
1516
F="color_model",
16-
G="truncate",
1717
H="output",
1818
I="reverse",
19-
M="overrule_bg",
20-
N="no_bg",
21-
Q="log",
2219
T="series",
2320
V="verbose",
2421
W="categorical",
2522
Ww="cyclic",
26-
Z="continuous",
2723
)
28-
@kwargs_to_strings(T="sequence", G="sequence")
29-
def makecpt(**kwargs):
24+
@kwargs_to_strings(T="sequence")
25+
def makecpt(
26+
truncate: tuple[float, float] | None = None,
27+
overrule_bg: bool = False,
28+
no_bg: bool = False,
29+
log: bool = False,
30+
continuous: bool = False,
31+
**kwargs,
32+
):
3033
r"""
3134
Make GMT color palette tables.
3235
@@ -67,6 +70,11 @@ def makecpt(**kwargs):
6770
Full GMT docs at :gmt-docs:`makecpt.html`.
6871
6972
{aliases}
73+
- G = truncate
74+
- M = overrule_bg
75+
- N = no_bg
76+
- Q = log
77+
- Z = continuous
7078
7179
Parameters
7280
----------
@@ -108,12 +116,12 @@ def makecpt(**kwargs):
108116
refers to the number of such boundaries and not the number of slices.
109117
For details on array creation, see
110118
:gmt-docs:`makecpt.html#generate-1d-array`.
111-
truncate : list or str
112-
*zlow/zhigh*.
113-
Truncate the incoming CPT so that the lowest and highest z-levels are
114-
to *zlow* and *zhigh*. If one of these equal NaN then we leave that
115-
end of the CPT alone. The truncation takes place before any resampling.
116-
See also :gmt-docs:`reference/features.html#manipulating-cpts`.
119+
truncate
120+
(*zlow*, *zhigh*).
121+
Truncate the incoming CPT so that the lowest and highest z-levels are to *zlow*
122+
and *zhigh*. If one of these equals NaN, then we leave that end of the CPT
123+
alone. The truncation takes place before any resampling. See also
124+
:gmt-docs:`reference/features.html#manipulating-cpts`.
117125
output : str
118126
Optional. The file name with extension .cpt to store the generated CPT
119127
file. If not given or ``False`` [Default], saves the CPT as the current
@@ -128,22 +136,22 @@ def makecpt(**kwargs):
128136
the ``truncate`` and ``series`` values are used, so the latter must be
129137
compatible with the changed z-range. See also
130138
:gmt-docs:`reference/features.html#manipulating-cpts`.
131-
overrule_bg : str
132-
Overrule background, foreground, and NaN colors specified in the master
133-
CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`,
134-
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in
135-
the :gmt-docs:`gmt.conf <gmt.conf>` file. When combined with
136-
``background``, only :gmt-term:`COLOR_NAN` is considered.
137-
no_bg : bool
138-
Do not write out the background, foreground, and NaN-color fields
139-
[Default will write them, i.e. ``no_bg=False``].
140-
log : bool
141-
For logarithmic interpolation scheme with input given as logarithms.
142-
Expects input z-values provided via ``series`` to be log10(*z*),
143-
assigns colors, and writes out *z*.
144-
continuous : bool
145-
Force a continuous CPT when building from a list of colors and a list
146-
of z-values [Default is None, i.e. discrete values].
139+
overrule_bg
140+
Overrule background, foreground, and NaN colors specified in the master CPT with
141+
the values of the parameters :gmt-term:`COLOR_BACKGROUND`,
142+
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in the
143+
:gmt-docs:`gmt.conf <gmt.conf>` file or by :func:`pygmt.config`. When combined
144+
with ``background``, only :gmt-term:`COLOR_NAN` is considered.
145+
no_bg
146+
Do not write out the background, foreground, and NaN-color fields [Default will
147+
write them, i.e. ``no_bg=False``].
148+
log
149+
For logarithmic interpolation scheme with input given as logarithms. Expects
150+
input z-values provided via ``series`` to be log10(*z*), assigns colors, and
151+
writes out *z*.
152+
continuous
153+
Force a continuous CPT when building from a list of colors and a list of
154+
z-values [Default is False, i.e. discrete CPT].
147155
{verbose}
148156
categorical : bool
149157
Do not interpolate the input color table but pick the output colors
@@ -162,5 +170,15 @@ def makecpt(**kwargs):
162170
if (output := kwargs.pop("H", None)) is not None:
163171
kwargs["H"] = True
164172

173+
aliasdict = AliasSystem(
174+
G=Alias(truncate, name="truncate", sep="/", size=2),
175+
M=Alias(overrule_bg, name="overrule_bg"),
176+
N=Alias(no_bg, name="no_bg"),
177+
Q=Alias(log, name="log"),
178+
Z=Alias(continuous, name="continuous"),
179+
).merge(kwargs)
180+
165181
with Session() as lib:
166-
lib.call_module(module="makecpt", args=build_arg_list(kwargs, outfile=output))
182+
lib.call_module(
183+
module="makecpt", args=build_arg_list(aliasdict, outfile=output)
184+
)

0 commit comments

Comments
 (0)