Skip to content

Commit 1bc1686

Browse files
committed
Updated Docstring for Function, added and refined tests, more alias options. Added additional kwarg list type that combines elements with +
Added file contour definition for file based grdcontour test
1 parent 7d8f3e1 commit 1bc1686

File tree

5 files changed

+100
-14
lines changed

5 files changed

+100
-14
lines changed

gmt/base_plotting.py

+44-6
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,23 @@ def coast(self, **kwargs):
127127

128128
@fmt_docstring
129129
@use_alias(
130-
R="region",
131-
J="projection",
132-
A="annotation_interval",
133-
C="contour_interval",
130+
A="annotation",
134131
B="frame",
132+
C="interval",
135133
G="label_placement",
134+
J="projection",
136135
L="limit",
136+
Q="cut",
137+
R="region",
138+
S="resample",
139+
U="logo",
137140
W="pen",
138141
)
139-
@kwargs_to_strings(R="sequence")
142+
@kwargs_to_strings(
143+
R="sequence",
144+
L='sequence',
145+
A="sequence_plus",
146+
)
140147
def grdcontour(self, grid, **kwargs):
141148
"""
142149
Convert grids or images to contours and plot them on maps
@@ -151,7 +158,38 @@ def grdcontour(self, grid, **kwargs):
151158
----------
152159
grid : str or xarray.DataArray
153160
The file name of the input grid or the grid loaded as a DataArray.
154-
161+
C : str or int
162+
Specify the contour lines to generate.
163+
164+
- The filename of a `CPT` file where the color boundaries will
165+
be used as contour levels.
166+
- The filename of a 2 (or 3) column file containing the contour
167+
levels (col 1), (C)ontour or (A)nnotate (col 2), and optional
168+
angle (col 3)
169+
- A fixed contour interval ``cont_int`` or a single contour with
170+
``+[cont_int]``
171+
A : str, int, or list
172+
Specify or disable annotated contour levels, modifies annotated
173+
contours specified in ``-C``.
174+
175+
- Specify a fixed annotation interval ``annot_int`` or a
176+
single annotation level ``+[annot_int]``
177+
- Disable all annotation with ``'-'``
178+
- Optional label modifers can be specifed as a single string
179+
``'[annot_int]+e'`` or with a list of options
180+
``([annot_int], 'e', 'f10p', 'gred')``.
181+
L : str or list of 2 ints
182+
Do no draw contours below `low` or above `high`, specify as string
183+
``'[low]/[high]'`` or list ``[low,high]``.
184+
Q : string or int
185+
Do not draw contours with less than `cut` number of points.
186+
S : string or int
187+
Resample smoothing factor.
188+
{J}
189+
{R}
190+
{B}
191+
{G}
192+
{W}
155193
"""
156194
kwargs = self._preprocess(**kwargs)
157195
kind = data_kind(grid, None, None)

gmt/helpers/decorators.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def kwargs_to_strings(convert_bools=True, **conversions):
221221
* 'sequence': transforms a sequence (list, tuple) into a ``'/'`` separated
222222
string
223223
* 'sequence_comma': transforms a sequence into a ``','`` separated string
224+
* 'sequence_plus': transforms a sequence into a ``'+'`` separated string
224225
225226
Parameters
226227
----------
@@ -262,15 +263,17 @@ def kwargs_to_strings(convert_bools=True, **conversions):
262263
args: 123
263264
264265
"""
265-
valid_conversions = ["sequence", "sequence_comma"]
266+
valid_conversions = ["sequence", "sequence_comma", "sequence_plus"]
266267

267268
for arg, fmt in conversions.items():
268269
if fmt not in valid_conversions:
269270
raise GMTInvalidInput(
270271
"Invalid conversion type '{}' for argument '{}'.".format(fmt, arg)
271272
)
272273

273-
separators = {"sequence": "/", "sequence_comma": ","}
274+
separators = {"sequence": "/",
275+
"sequence_comma": ",",
276+
"sequence_plus": "+",}
274277

275278
# Make the actual decorator function
276279
def converter(module_func):
@@ -284,7 +287,9 @@ def new_module(*args, **kwargs):
284287
for arg, fmt in conversions.items():
285288
if arg in kwargs:
286289
value = kwargs[arg]
287-
issequence = fmt in ("sequence", "sequence_comma")
290+
issequence = fmt in ("sequence",
291+
"sequence_comma",
292+
"sequence_plus")
288293
if issequence and is_nonstr_iter(value):
289294
kwargs[arg] = separators[fmt].join(
290295
"{}".format(item) for item in value
Loading

gmt/tests/data/contours.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-6000 C
2+
-5000 C
3+
-4000 A
4+
-3000 C
5+
-2000 C
6+
-1000 C
7+
0 A
8+
1000 C
9+
2000 A
10+
3000 C

gmt/tests/test_grdcontour.py

+38-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
"""
44
import pytest
55
import numpy as np
6+
import os
7+
68

79
from .. import Figure
810
from ..exceptions import GMTInvalidInput
911
from ..datasets import load_earth_relief
1012

1113

14+
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
15+
TEST_CONTOUR_FILE = os.path.join(TEST_DATA_DIR, "contours.txt")
16+
17+
1218
@pytest.mark.mpl_image_compare
1319
def test_grdcontour():
1420
"""Plot a contour image using an xarray grid
@@ -17,7 +23,7 @@ def test_grdcontour():
1723
grid = load_earth_relief()
1824
fig = Figure()
1925
fig.grdcontour(grid,
20-
contour_interval="1000",
26+
interval="1000",
2127
projection="W0/6i")
2228
return fig
2329

@@ -30,8 +36,8 @@ def test_grdcontour_labels():
3036
grid = load_earth_relief()
3137
fig = Figure()
3238
fig.grdcontour(grid,
33-
contour_interval="1000",
34-
annotation_interval="5000",
39+
interval="1000",
40+
annotation="5000",
3541
projection="W0/6i",
3642
pen=["a1p,red", "c0.5p,black"],
3743
label_placement="d3i",
@@ -45,7 +51,7 @@ def test_grdcontour_slice():
4551
grid = load_earth_relief().sel(lat=slice(-30, 30))
4652
fig = Figure()
4753
fig.grdcontour(grid,
48-
contour_interval="1000",
54+
interval="1000",
4955
projection="M6i")
5056
return fig
5157

@@ -56,7 +62,7 @@ def test_grdcontour_file():
5662
fig = Figure()
5763
fig.grdcontour(
5864
"@earth_relief_60m",
59-
contour_interval="1000",
65+
interval="1000",
6066
limit="0",
6167
pen="0.5p,black",
6268
region=[-180, 180, -70, 70],
@@ -65,6 +71,33 @@ def test_grdcontour_file():
6571
return fig
6672

6773

74+
@pytest.mark.mpl_image_compare
75+
def test_grdcontour_interval_file_full_opts():
76+
""" Plot based on external contour level file """
77+
fig = Figure()
78+
comargs = {
79+
'region': [-161.5,-154,18.5,23],
80+
'interval': TEST_CONTOUR_FILE,
81+
'grid': "@earth_relief_10m",
82+
'resample': "100",
83+
'projection': "M6i",
84+
'cut': 10,
85+
}
86+
87+
fig.grdcontour(
88+
**comargs,
89+
limit=(-25000,-1),
90+
pen=["a1p,blue", "c0.5p,blue"],
91+
)
92+
93+
fig.grdcontour(
94+
**comargs,
95+
limit="0",
96+
pen=["a1p,black", "c0.5p,black"],
97+
)
98+
return fig
99+
100+
68101
def test_grdcontour_fails():
69102
"Should fail for unrecognized input"
70103
fig = Figure()

0 commit comments

Comments
 (0)