Skip to content

Commit 37578e1

Browse files
committed
Merge branch 'master' into mapping/plot3d
2 parents e47596f + 0ea9886 commit 37578e1

File tree

7 files changed

+236
-23
lines changed

7 files changed

+236
-23
lines changed

.github/workflows/ci_tests.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ jobs:
117117
shell: bash -l {0}
118118
run: make test PYTEST_EXTRA="-r P"
119119

120+
# Upload diff images on test failure
121+
- name: Upload diff images if any test fails
122+
uses: actions/upload-artifact@v2
123+
if: ${{ failure() }}
124+
with:
125+
name: artifact-${{ runner.os }}-${{ matrix.python-version }}
126+
path: tmp-test-dir-with-unique-name
127+
120128
# Build the documentation
121129
- name: Build the documentation
122130
shell: bash -l {0}

.github/workflows/ci_tests_dev.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,11 @@ jobs:
8686
# Run the tests
8787
- name: Test with pytest
8888
run: make test PYTEST_EXTRA="-r P"
89+
90+
# Upload diff images on test failure
91+
- name: Upload diff images if any test fails
92+
uses: actions/upload-artifact@v2
93+
if: ${{ failure() }}
94+
with:
95+
name: artifact-GMT-${{ matrix.gmt_git_ref }}-${{ runner.os }}
96+
path: tmp-test-dir-with-unique-name

doc/conf.py

+2-4
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

doc/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
tutorials/coastlines.rst
3434
tutorials/plot.rst
3535
tutorials/text.rst
36+
tutorials/configuration.rst
3637

3738
.. toctree::
3839
:maxdepth: 2

examples/tutorials/configuration.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Configuring PyGMT defaults
3+
==========================
4+
5+
Default GMT parameters can be set globally or locally using :class:`pygmt.config`.
6+
"""
7+
8+
import pygmt
9+
10+
########################################################################################
11+
# Configuring default GMT parameters
12+
# ----------------------------------
13+
#
14+
# Users can override default parameters either temporarily (locally) or permanently
15+
# (globally) using :meth:`pygmt.config`. The full list of default parameters that can be
16+
# changed can be found at :gmt-docs:`gmt.conf.html`.
17+
#
18+
# We demonstrate the usage of :meth:`pygmt.config` by configuring a map plot.
19+
20+
# Start with a basic figure with the default style
21+
fig = pygmt.Figure()
22+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
23+
fig.coast(land="black", water="skyblue")
24+
25+
fig.show()
26+
27+
########################################################################################
28+
# Globally overriding defaults
29+
# ----------------------------
30+
#
31+
# The ``MAP_FRAME_TYPE`` parameter specifies the style of map frame to use, of which there
32+
# are 5 options: ``fancy`` (default, seen above), ``fancy+``, ``plain``, ``graph``
33+
# (which does not apply to geographical maps) and ``inside``.
34+
#
35+
# The ``FORMAT_GEO_MAP`` parameter controls the format of geographical tick annotations.
36+
# The default uses degrees and minutes. Here we specify the ticks to be a decimal number
37+
# of degrees.
38+
39+
fig = pygmt.Figure()
40+
41+
# Configuration for the 'current figure'.
42+
pygmt.config(MAP_FRAME_TYPE="plain")
43+
pygmt.config(FORMAT_GEO_MAP="ddd.xx")
44+
45+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
46+
fig.coast(land="black", water="skyblue")
47+
48+
fig.show()
49+
50+
########################################################################################
51+
# Locally overriding defaults
52+
# ---------------------------
53+
#
54+
# It is also possible to temporarily override the default parameters, which is very
55+
# useful for limiting the scope of changes to a particular plot. :class:`pygmt.config` is
56+
# implemented as a context manager, which handles the setup and teardown of a GMT
57+
# session. Python users are likely familiar with the ``with open(...) as file:`` snippet,
58+
# which returns a ``file`` context manager. In this way, it can be used to override a parameter
59+
# for a single command, or a sequence of commands. An application of :class:`pygmt.config`
60+
# as a context manager is shown below:
61+
62+
fig = pygmt.Figure()
63+
64+
# This will have a fancy+ frame
65+
with pygmt.config(MAP_FRAME_TYPE="fancy+"):
66+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
67+
fig.coast(land="black", water="skyblue")
68+
69+
# Shift plot origin down by 10cm to plot another map
70+
fig.shift_origin(yshift="-10c")
71+
72+
# This figure retains the default "fancy" frame
73+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
74+
fig.coast(land="black", water="skyblue")
75+
76+
fig.show()

pygmt/mathops.py

+103-19
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

+38
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)