forked from Unidata/netcdf4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_masked5.py
executable file
·67 lines (46 loc) · 1.74 KB
/
test_masked5.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
import unittest
import os
import tempfile
import numpy as np
from numpy import ma
from numpy.testing import assert_array_equal
from netCDF4 import Dataset, __netcdf4libversion__
# Test use of vector of missing values.
class VectorMissingValues(unittest.TestCase):
def setUp(self):
self.testfile = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
self.missing_values = [-999,999,0]
self.v = np.array([-999,0,1,2,3,999], dtype = "i2")
self.v_ma = ma.array([-1,0,1,2,3,4], dtype = "i2", \
mask = [True, True, False, False, False, True])
f = Dataset(self.testfile, 'w')
d = f.createDimension('x',6)
v = f.createVariable('v', "i2", 'x')
# issue 730: set fill_value for vlen str vars
v2 = f.createVariable('v2', str, 'x', fill_value='<missing>')
v.missing_value = self.missing_values
v[:] = self.v
v2[0]='first'
f.close()
def tearDown(self):
os.remove(self.testfile)
def test_scaled(self):
"""Testing auto-conversion of masked arrays"""
f = Dataset(self.testfile)
v = f.variables["v"]
v2 = f.variables["v2"]
self.assertTrue(isinstance(v[:], ma.masked_array))
assert_array_equal(v[:], self.v_ma)
assert_array_equal(v[2],self.v[2]) # issue #624.
v.set_auto_mask(False)
self.assertTrue(isinstance(v[:], np.ndarray))
assert_array_equal(v[:], self.v)
# issue 730
# this part fails with netcdf 4.1.3
# a bug in vlen strings?
if __netcdf4libversion__ >= '4.4.0':
assert v2[0] == 'first'
assert v2[1] == '<missing>'
f.close()
if __name__ == '__main__':
unittest.main()