|
1
|
|
|
# Copyright (c) 2016 MetPy Developers. |
|
2
|
|
|
# Distributed under the terms of the BSD 3-Clause License. |
|
3
|
|
|
# SPDX-License-Identifier: BSD-3-Clause |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
import pytest |
|
7
|
|
|
|
|
8
|
|
|
from metpy.io.cdm import Dataset |
|
9
|
|
|
from metpy.io.tools import hexdump, UnitLinker |
|
10
|
|
|
from metpy.testing import assert_array_equal |
|
11
|
|
|
from metpy.units import units |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
@pytest.fixture() |
|
15
|
|
|
def test_var(): |
|
16
|
|
|
r'Fixture to create a dataset and variable for tests' |
|
17
|
|
|
ds = Dataset() |
|
18
|
|
|
ds.createDimension('x', 5) |
|
19
|
|
|
var = ds.createVariable('data', 'f4', ('x',), 5) |
|
20
|
|
|
var[:] = np.arange(5) |
|
21
|
|
|
return var |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def test_unit_linker(test_var): |
|
25
|
|
|
r'Test that UnitLinker successfully adds units' |
|
26
|
|
|
test_var.units = 'meters' |
|
27
|
|
|
new_var = UnitLinker(test_var) |
|
28
|
|
|
assert_array_equal(new_var[:], np.arange(5) * units.m) |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def test_unit_linker_get_units(test_var): |
|
32
|
|
|
r'Test that we can get the units from UnitLinker' |
|
33
|
|
|
test_var.units = 'knots' |
|
34
|
|
|
new_var = UnitLinker(test_var) |
|
35
|
|
|
assert new_var.units == units('knots') |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def test_unit_linker_missing(test_var): |
|
39
|
|
|
r'Test that UnitLinker works with missing units' |
|
40
|
|
|
new_var = UnitLinker(test_var) |
|
41
|
|
|
assert_array_equal(new_var[:], np.arange(5)) |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def test_unit_linker_bad(test_var): |
|
45
|
|
|
r'Test that UnitLinker ignores bad unit strings' |
|
46
|
|
|
test_var.units = 'badunit' |
|
47
|
|
|
new_var = UnitLinker(test_var) |
|
48
|
|
|
assert_array_equal(new_var[:], np.arange(5)) |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def test_unit_override(test_var): |
|
52
|
|
|
r"Test that we can override a variable's bad unit string" |
|
53
|
|
|
test_var.units = 'C' |
|
54
|
|
|
new_var = UnitLinker(test_var) |
|
55
|
|
|
new_var.units = 'degC' |
|
56
|
|
|
assert_array_equal(new_var[:], np.arange(5) * units.degC) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def test_unit_override_obj(test_var): |
|
60
|
|
|
r'Test that we can override with an object' |
|
61
|
|
|
test_var.units = 'C' |
|
62
|
|
|
new_var = UnitLinker(test_var) |
|
63
|
|
|
new_var.units = units.degC |
|
64
|
|
|
assert_array_equal(new_var[:], np.arange(5) * units.degC) |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
def test_attribute_forwarding(test_var): |
|
68
|
|
|
r'Test that we are properly able to access attributes from the variable' |
|
69
|
|
|
test_var.att = 'abc' |
|
70
|
|
|
new_var = UnitLinker(test_var) |
|
71
|
|
|
assert new_var.att == test_var.att |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_hexdump(): |
|
75
|
|
|
r'Test hexdump tool' |
|
76
|
|
|
data = bytearray([77, 101, 116, 80, 121]) |
|
77
|
|
|
assert hexdump(data, 4, width=8) == '4D657450 79------ 0 0 MetPy' |
|
78
|
|
|
|