Skip to content

Commit 835b19b

Browse files
committed
Implemented Figure.grdcontour that uses grdcontour gmt module
Plots grids to contours, with its own set of distinct inputs from `pscontour` Test and Initial Documentation is complete
1 parent e5845ba commit 835b19b

7 files changed

+115
-1
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Plotting data and laying out the map:
2424
Figure.coast
2525
Figure.plot
2626
Figure.contour
27+
Figure.grdcontour
2728
Figure.grdimage
2829
Figure.logo
2930

gmt/base_plotting.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,51 @@ def coast(self, **kwargs):
125125
with Session() as lib:
126126
lib.call_module("coast", build_arg_string(kwargs))
127127

128+
129+
@fmt_docstring
130+
@use_alias(
131+
R="region",
132+
J="projection",
133+
A="annotation_interval",
134+
C="contour_interval",
135+
B="frame",
136+
G="label_placement",
137+
L="limit",
138+
W="pen",
139+
)
140+
@kwargs_to_strings(R="sequence")
141+
def grdcontour(self, grid, **kwargs):
142+
"""
143+
Convert grids or images to contours and plot them on maps
144+
145+
Takes a grid file name or an xarray.DataArray object as input.
146+
147+
{gmt_module_docs}
148+
149+
{aliases}
150+
151+
Parameters
152+
----------
153+
grid : str or xarray.DataArray
154+
The file name of the input grid or the grid loaded as a DataArray.
155+
156+
"""
157+
kwargs = self._preprocess(**kwargs)
158+
kind = data_kind(grid, None, None)
159+
with Session() as lib:
160+
if kind == "file":
161+
file_context = dummy_context(grid)
162+
elif kind == "grid":
163+
file_context = lib.virtualfile_from_grid(grid)
164+
else:
165+
raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid)))
166+
with file_context as fname:
167+
arg_str = " ".join([fname, build_arg_string(kwargs)])
168+
lib.call_module("grdcontour", arg_str)
169+
170+
128171
@fmt_docstring
129-
@use_alias(R="region", J="projection", B="frame", I="shading", C="cmap")
172+
@use_alias(R="region", J="projection", W="pen", B="frame", I="shading", C="cmap")
130173
@kwargs_to_strings(R="sequence")
131174
def grdimage(self, grid, **kwargs):
132175
"""
208 KB
Loading
153 KB
Loading
215 KB
Loading
91 KB
Loading

gmt/tests/test_grdcontour.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Test Figure.grdcontour
3+
"""
4+
import pytest
5+
import numpy as np
6+
7+
from .. import Figure
8+
from ..exceptions import GMTInvalidInput
9+
from ..datasets import load_earth_relief
10+
11+
@pytest.mark.mpl_image_compare
12+
def test_grdcontour():
13+
"""Plot a contour image using an xarray grid
14+
with fixed contour interval
15+
"""
16+
grid = load_earth_relief()
17+
fig = Figure()
18+
fig.grdcontour(grid,
19+
contour_interval="1000",
20+
projection="W0/6i")
21+
return fig
22+
23+
@pytest.mark.mpl_image_compare
24+
def test_grdcontour_labels():
25+
"""Plot a contour image using a xarray grid
26+
with contour labels and alternate colors
27+
"""
28+
grid = load_earth_relief()
29+
fig = Figure()
30+
fig.grdcontour(grid,
31+
contour_interval="1000",
32+
annotation_interval="5000",
33+
projection="W0/6i",
34+
pen=["a1p,red","c0.5p,black"],
35+
label_placement="d3i",
36+
)
37+
return fig
38+
39+
40+
@pytest.mark.mpl_image_compare
41+
def test_grdcontour_slice():
42+
"Plot an contour image using an xarray grid that has been sliced"
43+
grid = load_earth_relief().sel(lat=slice(-30, 30))
44+
fig = Figure()
45+
fig.grdcontour(grid,
46+
contour_interval="1000",
47+
projection="M6i")
48+
return fig
49+
50+
51+
@pytest.mark.mpl_image_compare
52+
def test_grdcontour_file():
53+
"Plot a contour image using grid file input"
54+
fig = Figure()
55+
fig.grdcontour(
56+
"@earth_relief_60m",
57+
contour_interval="1000",
58+
limit="0",
59+
pen="0.5p,black",
60+
region=[-180, 180, -70, 70],
61+
projection="M10i",
62+
)
63+
return fig
64+
65+
66+
def test_grdcontour_fails():
67+
"Should fail for unrecognized input"
68+
fig = Figure()
69+
with pytest.raises(GMTInvalidInput):
70+
fig.grdcontour(np.arange(20).reshape((4, 5)))

0 commit comments

Comments
 (0)