Skip to content

Commit 0ea9886

Browse files
authored
Complete most of documentation for makecpt (#676)
Starting to properly document most major arguments for `makecpt` as per upstream GMT docs at https://docs.generic-mapping-tools.org/6.1/makecpt.html. Refreshed current aliases series (T), truncate (G), reverse (I) and continuous (Z). * Add sphinx extlink 'gmt-term' for linking to gmt.conf parameters * Complete long description of makecpt * Alias transparency (A), color_model (F) and log (Q) for makecpt * Alias categorical (W) for makecpt * Alias background (D), overrule_bg (M) and no_bg (N) * Alias cyclic (Ww) and ensure this cannot be used with categorical (W)
1 parent 2c14fbd commit 0ea9886

File tree

3 files changed

+143
-23
lines changed

3 files changed

+143
-23
lines changed

doc/conf.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737

3838
# configure links to GMT docs
3939
extlinks = {
40-
"gmt-docs": (
41-
"https://docs.generic-mapping-tools.org/latest/%s",
42-
"https://docs.generic-mapping-tools.org/latest/",
43-
)
40+
"gmt-docs": ("https://docs.generic-mapping-tools.org/latest/%s", None),
41+
"gmt-term": ("https://docs.generic-mapping-tools.org/latest/gmt.conf#term-%s", ""),
4442
}
4543

4644
# intersphinx configuration

pygmt/mathops.py

Lines changed: 103 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,105 @@
88

99
@fmt_docstring
1010
@use_alias(
11+
A="transparency",
1112
C="cmap",
12-
T="series",
13+
D="background",
14+
F="color_model",
1315
G="truncate",
1416
H="output",
1517
I="reverse",
18+
M="overrule_bg",
19+
N="no_bg",
20+
Q="log",
21+
T="series",
1622
V="verbose",
23+
W="categorical",
24+
Ww="cyclic",
1725
Z="continuous",
1826
)
1927
@kwargs_to_strings(T="sequence", G="sequence")
2028
def makecpt(**kwargs):
2129
"""
22-
Creates a static color palette table (CPT).
30+
Make GMT color palette tables.
31+
32+
This is a module that will help you make static color palette tables
33+
(CPTs). By default, the CPT will simply be saved to the current session,
34+
but you can use *output* to save it to a file. You define an equidistant
35+
set of contour intervals or pass your own z-table or list, and create a new
36+
CPT based on an existing master (dynamic) CPT. The resulting CPT can be
37+
reversed relative to the master cpt, and can be made continuous or
38+
discrete. For color tables beyond the standard GMT offerings, visit
39+
`cpt-city <http://soliton.vm.bytemark.co.uk/pub/cpt-city/>`_ and
40+
`Scientific Colour-Maps <http://www.fabiocrameri.ch/colourmaps.php>`_.
41+
42+
The CPT includes three additional colors beyond the range of z-values.
43+
These are the background color (B) assigned to values lower than the lowest
44+
*z*-value, the foreground color (F) assigned to values higher than the
45+
highest *z*-value, and the NaN color (N) painted wherever values are
46+
undefined.
47+
48+
If the master CPT includes B, F, and N entries, these will be copied into
49+
the new master file. If not, the parameters :gmt-term:`COLOR_BACKGROUND`,
50+
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` from the
51+
:gmt-docs:`gmt.conf <gmt.conf>` file or the command line will be used. This
52+
default behavior can be overruled using the options *background*,
53+
*overrule_bg* or *no_bg*.
54+
55+
The color model (RGB, HSV or CMYK) of the palette created by **makecpt**
56+
will be the same as specified in the header of the master CPT. When there
57+
is no :gmt-term:`COLOR_MODEL` entry in the master CPT, the
58+
:gmt-term:`COLOR_MODEL` specified in the :gmt-docs:`gmt.conf <gmt.conf>`
59+
file or on the command line will be used.
2360
2461
Full option list at :gmt-docs:`makecpt.html`
2562
2663
{aliases}
2764
2865
Parameters
2966
----------
67+
transparency : str
68+
Sets a constant level of transparency (0-100) for all color slices.
69+
Append **+a** to also affect the fore-, back-, and nan-colors
70+
[Default is no transparency, i.e., 0 (opaque)].
3071
cmap : str
3172
Selects the master color palette table (CPT) to use in the
3273
interpolation. Full list of built-in color palette tables can be found
3374
at :gmt-docs:`cookbook/cpts.html#built-in-color-palette-tables-cpt`.
75+
background : bool or str
76+
Select the back- and foreground colors to match the colors for lowest
77+
and highest *z*-values in the output CPT [Default (``background=True``
78+
or ``background='o'``) uses the colors specified in the master file, or
79+
those defined by the parameters :gmt-term:`COLOR_BACKGROUND`,
80+
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN`]. Use
81+
``background='i'`` to match the colors for the lowest and highest
82+
values in the input (instead of the output) CPT.
83+
color_model :
84+
``[R|r|h|c][+c[label]]``.
85+
Force output CPT to be written with r/g/b codes, gray-scale values or
86+
color name (**R**, default) or r/g/b codes only (**r**), or h-s-v codes
87+
(**h**), or c/m/y/k codes (**c**). Optionally or alternatively, append
88+
**+c** to write discrete palettes in categorical format. If *label* is
89+
appended then we create labels for each category to be used when the
90+
CPT is plotted. The *label* may be a comma-separated list of category
91+
names (you can skip a category by not giving a name), or give
92+
*start*[-], where we automatically build monotonically increasing
93+
labels from *start* (a single letter or an integer). Append - to build
94+
ranges *start*-*start+1* instead.
3495
series : list or str
35-
``[min/max/inc[+b|l|n]|file|list]``. Defines the range of the new CPT
36-
by giving the lowest and highest z-value (and optionally an interval).
37-
If this is not given, the existing range in the master CPT will be used
38-
intact.
96+
``[min/max/inc[+b|l|n]|file|list]``.
97+
Defines the range of the new CPT by giving the lowest and highest
98+
z-value (and optionally an interval). If this is not given, the
99+
existing range in the master CPT will be used intact. The values
100+
produced defines the color slice boundaries. If **+n** is used it
101+
refers to the number of such boundaries and not the number of slices.
102+
For details on array creation, see
103+
:gmt-docs:`makecpt.html#generate-1d-array`.
39104
truncate : list or str
40-
``zlo/zhi``. Truncate the incoming CPT so that the lowest and highest
41-
z-levels are to zlo and zhi. If one of these equal NaN then we leave
42-
that end of the CPT alone. The truncation takes place before any
43-
resampling. See also
44-
:gmt-docs:`cookbook/features.html#manipulating-cpts`.
105+
``zlo/zhi``.
106+
Truncate the incoming CPT so that the lowest and highest z-levels are
107+
to *zlo* and *zhi*. If one of these equal NaN then we leave that end of
108+
the CPT alone. The truncation takes place before any resampling. See
109+
also :gmt-docs:`cookbook/features.html#manipulating-cpts`.
45110
output : str
46111
Optional. The file name with extension .cpt to store the generated CPT
47112
file. If not given or False (default), saves the CPT as the session
@@ -51,19 +116,38 @@ def makecpt(**kwargs):
51116
progression in the master CPT. Set this to z to reverse the sign of
52117
z-values in the color table. Note that this change of z-direction
53118
happens before *truncate* and *series* values are used so the latter
54-
must be compatible with the changed z-range. See also
119+
must be compatible with the changed *z*-range. See also
55120
:gmt-docs:`cookbook/features.html#manipulating-cpts`.
121+
overrule_bg :
122+
Overrule background, foreground, and NaN colors specified in the master
123+
CPT with the values of the parameters :gmt-term:`COLOR_BACKGROUND`,
124+
:gmt-term:`COLOR_FOREGROUND`, and :gmt-term:`COLOR_NAN` specified in
125+
the :gmt-docs:`gmt.conf <gmt.conf>` file or on the command line. When
126+
combined with **background**, only :gmt-term:`COLOR_NAN` is considered.
127+
no_bg : bool
128+
Do not write out the background, foreground, and NaN-color fields
129+
[Default will write them, i.e. ``no_bg=False``].
130+
log : bool
131+
For logarithmic interpolation scheme with input given as logarithms.
132+
Expects input z-values provided via **series** to be log10(*z*),
133+
assigns colors, and writes out *z*.
56134
continuous : bool
57-
Creates a continuous CPT [Default is discontinuous, i.e., constant
58-
colors for each interval]. This option has no effect when no *series*
59-
is used, or when using *series=[z_min, z_max]*; in the first case the
60-
input CPT remains untouched, in the second case it is only scaled to
61-
match the range z_min/z_max.
62-
135+
Force a continuous CPT when building from a list of colors and a list
136+
of z-values [Default is None, i.e. discrete values].
63137
{V}
64-
138+
categorical : bool
139+
Do not interpolate the input color table but pick the output colors
140+
starting at the beginning of the color table, until colors for all
141+
intervals are assigned. This is particularly useful in combination with
142+
a categorical color table, like ``cmap='categorical'``.
143+
cyclic : bool
144+
Produce a wrapped (cyclic) color table that endlessly repeats its
145+
range. Note that ``cyclic=True`` cannot be set together with
146+
``categorical=True``.
65147
"""
66148
with Session() as lib:
149+
if "W" in kwargs and "Ww" in kwargs:
150+
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
67151
if "H" not in kwargs.keys(): # if no output is set
68152
arg_str = build_arg_string(kwargs)
69153
elif "H" in kwargs.keys(): # if output is set

pygmt/tests/test_makecpt.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,41 @@ def test_makecpt_continuous(grid):
182182
makecpt(cmap="blue,white", continuous=True, series="-4500,4500")
183183
fig.grdimage(grid, projection="W0/6i")
184184
return fig
185+
186+
187+
@check_figures_equal()
188+
def test_makecpt_categorical(region):
189+
"""
190+
Use static color palette table that is categorical.
191+
"""
192+
fig_ref = Figure()
193+
makecpt(C="categorical", W="")
194+
fig_ref.colorbar(cmap=True, region=region, frame=True, position="JBC")
195+
196+
fig_test = Figure()
197+
makecpt(cmap="categorical", categorical=True)
198+
fig_test.colorbar(cmap=True, region=region, frame=True, position="JBC")
199+
return fig_ref, fig_test
200+
201+
202+
@check_figures_equal()
203+
def test_makecpt_cyclic(region):
204+
"""
205+
Use static color palette table that is cyclic.
206+
"""
207+
fig_ref = Figure()
208+
makecpt(C="cork", W="w")
209+
fig_ref.colorbar(cmap=True, region=region, frame=True, position="JBC")
210+
211+
fig_test = Figure()
212+
makecpt(cmap="cork", cyclic=True)
213+
fig_test.colorbar(cmap=True, region=region, frame=True, position="JBC")
214+
return fig_ref, fig_test
215+
216+
217+
def test_makecpt_categorical_and_cyclic():
218+
"""
219+
Use incorrect setting by setting both categorical and cyclic to True.
220+
"""
221+
with pytest.raises(GMTInvalidInput):
222+
makecpt(cmap="batlow", categorical=True, cyclic=True)

0 commit comments

Comments
 (0)