Skip to content

Commit a21f2c3

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 3292ba1 commit a21f2c3

File tree

5 files changed

+97
-17
lines changed

5 files changed

+97
-17
lines changed

gmt/base_plotting.py

Lines changed: 44 additions & 6 deletions
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

Lines changed: 7 additions & 3 deletions
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,18 @@ 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": "+",
277+
}
274278

275279
# Make the actual decorator function
276280
def converter(module_func):
@@ -284,7 +288,7 @@ def new_module(*args, **kwargs):
284288
for arg, fmt in conversions.items():
285289
if arg in kwargs:
286290
value = kwargs[arg]
287-
issequence = fmt == "sequence" or fmt == "sequence_comma"
291+
issequence = "sequence" in fmt
288292
if issequence and is_nonstr_iter(value):
289293
kwargs[arg] = separators[fmt].join(
290294
"{}".format(item) for item in value
Loading

gmt/tests/data/contours.txt

Lines changed: 10 additions & 0 deletions
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

Lines changed: 36 additions & 8 deletions
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,11 +23,10 @@ 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

24-
2530
@pytest.mark.mpl_image_compare
2631
def test_grdcontour_labels():
2732
"""Plot a contour image using a xarray grid
@@ -30,40 +35,63 @@ def test_grdcontour_labels():
3035
grid = load_earth_relief()
3136
fig = Figure()
3237
fig.grdcontour(grid,
33-
contour_interval="1000",
34-
annotation_interval="5000",
38+
interval="1000",
39+
annotation="5000",
3540
projection="W0/6i",
3641
pen=["a1p,red", "c0.5p,black"],
3742
label_placement="d3i",
3843
)
3944
return fig
4045

41-
4246
@pytest.mark.mpl_image_compare
4347
def test_grdcontour_slice():
4448
"Plot an contour image using an xarray grid that has been sliced"
4549
grid = load_earth_relief().sel(lat=slice(-30, 30))
4650
fig = Figure()
4751
fig.grdcontour(grid,
48-
contour_interval="1000",
52+
interval="1000",
4953
projection="M6i")
5054
return fig
5155

52-
5356
@pytest.mark.mpl_image_compare
5457
def test_grdcontour_file():
5558
"Plot a contour image using grid file input"
5659
fig = Figure()
5760
fig.grdcontour(
5861
"@earth_relief_60m",
59-
contour_interval="1000",
62+
interval="1000",
6063
limit="0",
6164
pen="0.5p,black",
6265
region=[-180, 180, -70, 70],
6366
projection="M10i",
6467
)
6568
return fig
6669

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

6896
def test_grdcontour_fails():
6997
"Should fail for unrecognized input"

0 commit comments

Comments
 (0)