forked from Unidata/netcdf4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_multiple_open_close.py
More file actions
52 lines (39 loc) · 1.87 KB
/
Copy pathtest_multiple_open_close.py
File metadata and controls
52 lines (39 loc) · 1.87 KB
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
import os
import tracemalloc
import unittest
import netCDF4
@unittest.skipUnless(
os.getenv("MEMORY_LEAK_TEST"), "computationally intensive test not enabled"
)
class MultipleVariablesByAttributesCallsTests(unittest.TestCase):
def test_multiple_calls(self):
netcdf_file = os.path.join(os.path.dirname(__file__), "netcdf_dummy_file.nc")
tracemalloc.start()
snapshot = tracemalloc.take_snapshot()
k_times = 10
for _k in range(k_times):
nc = netCDF4.Dataset(netcdf_file)
vs = nc.get_variables_by_attributes(axis='Z')
self.assertEqual(len(vs), 1)
vs = nc.get_variables_by_attributes(units='m/s')
self.assertEqual(len(vs), 4)
vs = nc.get_variables_by_attributes(axis='Z', units='m')
self.assertEqual(len(vs), 1)
vs = nc.get_variables_by_attributes(axis=lambda v: v in ['X', 'Y', 'Z', 'T'])
self.assertEqual(len(vs), 1)
vs = nc.get_variables_by_attributes(grid_mapping=lambda v: v is not None)
self.assertEqual(len(vs), 12)
vs = nc.get_variables_by_attributes(grid_mapping=lambda v: v is not None, long_name=lambda v: v is not None and 'Upward (w) velocity' in v)
self.assertEqual(len(vs), 1)
vs = nc.get_variables_by_attributes(units='m/s', grid_mapping=lambda v: v is not None)
self.assertEqual(len(vs), 4)
vs = nc.get_variables_by_attributes(grid_mapping=lambda v: v is not None, long_name='Upward (w) velocity')
self.assertEqual(len(vs), 1)
nc.close()
stats = tracemalloc.take_snapshot().compare_to(snapshot, 'filename')
tracemalloc.stop()
print("[ Top 10 differences ]")
for stat in stats[:10]:
print(stat)
if __name__ == '__main__':
unittest.main()