1
|
|
|
# Copyright (c) 2008-2015 MetPy Developers. |
2
|
|
|
# Distributed under the terms of the BSD 3-Clause License. |
3
|
|
|
# SPDX-License-Identifier: BSD-3-Clause |
4
|
|
|
"""Tests for the `ctables` module.""" |
5
|
|
|
|
6
|
|
|
import os.path |
7
|
|
|
import tempfile |
8
|
|
|
try: |
9
|
|
|
buffer_args = dict(bufsize=0) |
10
|
|
|
from StringIO import StringIO |
11
|
|
|
except ImportError: |
12
|
|
|
buffer_args = dict(buffering=1) |
13
|
|
|
from io import StringIO |
14
|
|
|
|
15
|
|
|
import numpy as np |
16
|
|
|
import pytest |
17
|
|
|
|
18
|
|
|
from metpy.plots.ctables import ColortableRegistry, convert_gempak_table |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@pytest.fixture() |
22
|
|
|
def registry(): |
23
|
|
|
"""Set up a registry for use by the tests.""" |
24
|
|
|
return ColortableRegistry() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def test_package_resource(registry): |
28
|
|
|
"""Test registry scanning package resource.""" |
29
|
|
|
registry.scan_resource('metpy.plots', 'nexrad_tables') |
30
|
|
|
assert 'cc_table' in registry |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def test_scan_dir(registry): |
34
|
|
|
"""Test registry scanning a directory and ignoring files it can't handle .""" |
35
|
|
|
try: |
36
|
|
|
kwargs = dict(mode='w', dir='.', suffix='.tbl', delete=False, **buffer_args) |
37
|
|
|
with tempfile.NamedTemporaryFile(**kwargs) as fobj: |
38
|
|
|
fobj.write('"red"\n"lime"\n"blue"\n') |
39
|
|
|
fname = fobj.name |
40
|
|
|
|
41
|
|
|
# Unrelated table file that *should not* impact the scan |
42
|
|
|
with tempfile.NamedTemporaryFile(**kwargs) as fobj: |
43
|
|
|
fobj.write('PADK 704540 ADAK NAS\n') |
44
|
|
|
bad_file = fobj.name |
45
|
|
|
|
46
|
|
|
# Needs to be outside with so it's closed on windows |
47
|
|
|
registry.scan_dir(os.path.dirname(fname)) |
48
|
|
|
name = os.path.splitext(os.path.basename(fname))[0] |
49
|
|
|
|
50
|
|
|
assert name in registry |
51
|
|
|
assert registry[name] == [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)] |
52
|
|
|
finally: |
53
|
|
|
os.remove(fname) |
54
|
|
|
os.remove(bad_file) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_read_file(registry): |
58
|
|
|
"""Test reading a colortable from a file.""" |
59
|
|
|
fobj = StringIO('(0., 0., 1.0)\n"red"\n"#0000FF" #Blue') |
60
|
|
|
|
61
|
|
|
registry.add_colortable(fobj, 'test_table') |
62
|
|
|
|
63
|
|
|
assert 'test_table' in registry |
64
|
|
|
assert registry['test_table'] == [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)] |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_read_bad_file(registry): |
68
|
|
|
"""Test what error results when reading a malformed file.""" |
69
|
|
|
with pytest.raises(RuntimeError): |
70
|
|
|
fobj = StringIO('PADK 704540 ADAK NAS ' |
71
|
|
|
'AK US 5188 -17665 4 0') |
72
|
|
|
registry.add_colortable(fobj, 'sfstns') |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_get_colortable(registry): |
76
|
|
|
"""Test getting a colortable from the registry.""" |
77
|
|
|
true_colors = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0)] |
78
|
|
|
registry['table'] = true_colors |
79
|
|
|
|
80
|
|
|
table = registry.get_colortable('table') |
81
|
|
|
assert table.N == 2 |
82
|
|
|
assert table.colors == true_colors |
83
|
|
|
|
84
|
|
|
|
85
|
|
View Code Duplication |
def test_get_steps(registry): |
|
|
|
|
86
|
|
|
"""Test getting a colortable and norm with appropriate steps.""" |
87
|
|
|
registry['table'] = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] |
88
|
|
|
norm, cmap = registry.get_with_steps('table', 5., 10.) |
89
|
|
|
assert cmap(norm(np.array([6.]))).tolist() == [[0.0, 0.0, 1.0, 1.0]] |
90
|
|
|
assert cmap(norm(np.array([14.9]))).tolist() == [[0.0, 0.0, 1.0, 1.0]] |
91
|
|
|
assert cmap(norm(np.array([15.1]))).tolist() == [[1.0, 0.0, 0.0, 1.0]] |
92
|
|
|
assert cmap(norm(np.array([26.]))).tolist() == [[0.0, 1.0, 0.0, 1.0]] |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_get_steps_negative_start(registry): |
96
|
|
|
"""Test bad start for get with steps (issue #81).""" |
97
|
|
|
registry['table'] = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] |
98
|
|
|
norm, _ = registry.get_with_steps('table', -10, 5) |
99
|
|
|
assert norm.vmin == -10 |
100
|
|
|
assert norm.vmax == 5 |
101
|
|
|
|
102
|
|
|
|
103
|
|
View Code Duplication |
def test_get_range(registry): |
|
|
|
|
104
|
|
|
"""Test getting a colortable and norm with appropriate range.""" |
105
|
|
|
registry['table'] = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] |
106
|
|
|
norm, cmap = registry.get_with_range('table', 5., 35.) |
107
|
|
|
assert cmap(norm(np.array([6.]))).tolist() == [[0.0, 0.0, 1.0, 1.0]] |
108
|
|
|
assert cmap(norm(np.array([14.9]))).tolist() == [[0.0, 0.0, 1.0, 1.0]] |
109
|
|
|
assert cmap(norm(np.array([15.1]))).tolist() == [[1.0, 0.0, 0.0, 1.0]] |
110
|
|
|
assert cmap(norm(np.array([26.]))).tolist() == [[0.0, 1.0, 0.0, 1.0]] |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def test_get_boundaries(registry): |
114
|
|
|
"""Test getting a colortable with explicit boundaries.""" |
115
|
|
|
registry['table'] = [(0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] |
116
|
|
|
norm, cmap = registry.get_with_boundaries('table', [0., 8., 10., 20.]) |
117
|
|
|
assert cmap(norm(np.array([7.]))).tolist() == [[0.0, 0.0, 1.0, 1.0]] |
118
|
|
|
assert cmap(norm(np.array([9.]))).tolist() == [[1.0, 0.0, 0.0, 1.0]] |
119
|
|
|
assert cmap(norm(np.array([10.1]))).tolist() == [[0.0, 1.0, 0.0, 1.0]] |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def test_gempak(): |
123
|
|
|
"""Test GEMPAK colortable conversion.""" |
124
|
|
|
infile = StringIO('''! wvcolor.tbl |
125
|
|
|
0 0 0 |
126
|
|
|
255 255 255 |
127
|
|
|
''') |
128
|
|
|
outfile = StringIO() |
129
|
|
|
|
130
|
|
|
# Do the conversion |
131
|
|
|
convert_gempak_table(infile, outfile) |
132
|
|
|
|
133
|
|
|
# Reset and grab contents |
134
|
|
|
outfile.seek(0) |
135
|
|
|
result = outfile.read() |
136
|
|
|
|
137
|
|
|
assert result == '(0.000000, 0.000000, 0.000000)\n(1.000000, 1.000000, 1.000000)\n' |
138
|
|
|
|