-
Notifications
You must be signed in to change notification settings - Fork 214
/
test_grdcontour.py
144 lines (120 loc) · 3.48 KB
/
test_grdcontour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Test Figure.grdcontour.
"""
from pathlib import Path
import numpy as np
import pytest
from pygmt import Figure
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers.testing import load_static_earth_relief
TEST_CONTOUR_FILE = Path(__file__).parent / "data" / "contours.txt"
@pytest.fixture(scope="module", name="grid")
def fixture_grid():
"""
Load the grid data from the static_earth_relief file.
"""
return load_static_earth_relief()
@pytest.mark.mpl_image_compare
def test_grdcontour(grid):
"""
Plot a contour image using an xarray grid with fixed (different) contour and
annotation intervals.
"""
fig = Figure()
fig.grdcontour(grid=grid, levels=50, annotation=200, projection="M10c", frame=True)
return fig
@pytest.mark.mpl_image_compare
def test_grdcontour_one_level(grid):
"""
Plot a contour image using an xarray grid with one contour level and one
(different) annotation level.
"""
fig = Figure()
fig.grdcontour(
grid=grid, levels=[400], annotation=[570], projection="M10c", frame=True
)
return fig
@pytest.mark.mpl_image_compare(filename="test_grdcontour_one_level.png")
def test_grdcontour_old_annotations(grid):
"""
Test the old syntax for the annotation parameter using "sequence_plus".
Modified from the "test_grdcontour_one_level()" test. Can be removed in v0.14.0.
"""
fig = Figure()
fig.grdcontour(
grid=grid,
levels=[400],
annotation=["570,", "gwhite"],
projection="M10c",
frame=True,
)
return fig
@pytest.mark.mpl_image_compare
def test_grdcontour_multiple_levels(grid):
"""
Plot a contour image using an xarray grid with multiple (different) contour
and annotation levels.
"""
fig = Figure()
fig.grdcontour(
grid=grid,
levels=[400, 450, 500],
annotation=[400, 570],
projection="M10c",
frame=True,
)
return fig
@pytest.mark.benchmark
@pytest.mark.mpl_image_compare
def test_grdcontour_labels(grid):
"""
Plot a contour image using a xarray grid with contour labels and alternate colors.
"""
fig = Figure()
fig.grdcontour(
grid=grid,
levels=50,
annotation=200,
projection="M10c",
pen=["a1p,red", "c0.5p,black"],
label_placement="d6c",
frame=True,
)
return fig
@pytest.mark.mpl_image_compare
def test_grdcontour_slice(grid):
"""
Plot an contour image using an xarray grid that has been sliced.
"""
grid_ = grid.sel(lat=slice(-20, -10))
fig = Figure()
fig.grdcontour(grid=grid_, levels=100, projection="M10c", frame=True)
return fig
@pytest.mark.mpl_image_compare
def test_grdcontour_interval_file_full_opts(grid):
"""
Plot based on external contour level file.
"""
fig = Figure()
comargs = {
"region": [-53, -49, -20, -17],
"levels": TEST_CONTOUR_FILE,
"grid": grid,
"resample": 100,
"projection": "M10c",
"cut": 10,
}
# Plot contours below 650 in blue
fig.grdcontour(
**comargs, limit=(0, 649), pen=["a1p,blue", "c0.5p,blue"], frame=True
)
# Plot contours above 650 in black
fig.grdcontour(**comargs, limit=(650, 1000), pen=["a1p,black", "c0.5p,black"])
return fig
def test_grdcontour_fails():
"""
Should fail for unrecognized input.
"""
fig = Figure()
with pytest.raises(GMTInvalidInput):
fig.grdcontour(np.arange(20).reshape((4, 5)))