File: test_diskless.py

package info (click to toggle)
netcdf4-python 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,588 kB
  • sloc: python: 6,002; ansic: 854; makefile: 15; sh: 2
file content (87 lines) | stat: -rw-r--r-- 3,181 bytes parent folder | download
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
import unittest, os, tempfile
import numpy as np
from numpy.random.mtrand import uniform
from numpy.testing import assert_array_equal, assert_array_almost_equal
import netCDF4

# rudimentary test of diskless file capability.

# create an n1dim by n2dim by n3dim random array
n1dim = 10
n2dim = 73
n3dim = 144
ranarr = 100.*uniform(size=(n1dim,n2dim,n3dim))
ranarr2 = 100.*uniform(size=(n1dim,n2dim,n3dim))
FILE_NAME = tempfile.NamedTemporaryFile(suffix='.nc', delete=True).name
FILE_NAME2 = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name


@unittest.skipIf(
    netCDF4.__netcdf4libversion__ < "4.2.1"
    or netCDF4.__has_parallel4_support__
    or netCDF4.__has_pnetcdf_support__,
    "no diskless support",
)
class DisklessTestCase(unittest.TestCase):
    def setUp(self):
        # in memory file, does not exist on disk (closing it
        # makes data disappear from memory)
        self.file = FILE_NAME
        f = netCDF4.Dataset(self.file,'w',diskless=True, persist=False)
        self.f = f
        # foo has a single unlimited dimension
        f.createDimension('n1', n1dim)
        f.createDimension('n2', n2dim)
        f.createDimension('n3', n3dim)
        foo = f.createVariable('data1', ranarr.dtype.str[1:], ('n1','n2','n3'))
        # write some data to it.
        foo[0:n1dim-1] = ranarr[:-1,:,:]
        foo[n1dim-1] = ranarr[-1,:,:]
        # bar has 2 unlimited dimensions
        f.createDimension('n4', None)
        # write some data to it.
        bar = f.createVariable('data2', ranarr.dtype.str[1:], ('n1','n2','n4'))
        bar[0:n1dim,:, 0:n3dim] = ranarr2

        # in memory file, that is persisted to disk when close method called.
        self.file2 = FILE_NAME2
        f2 = netCDF4.Dataset(self.file2,'w',diskless=True, persist=True)
        f2.createDimension('n1', n1dim)
        f2.createDimension('n2', n2dim)
        f2.createDimension('n3', n3dim)
        foo = f2.createVariable('data1', ranarr.dtype.str[1:], ('n1','n2','n3'))
        # write some data to it.
        foo[0:n1dim-1] = ranarr[:-1,:,:]
        foo[n1dim-1] = ranarr[-1,:,:]
        f2.close()

    def tearDown(self):
        # Remove the temporary files
        os.remove(self.file2)
        self.f.close()

    def runTest(self):
        """testing diskless file capability"""
        foo = self.f.variables['data1']
        bar = self.f.variables['data2']
        # check shape.
        self.assertTrue(foo.shape == (n1dim,n2dim,n3dim))
        self.assertTrue(bar.shape == (n1dim,n2dim,n3dim))
        # check data.
        assert_array_almost_equal(foo[:], ranarr)
        assert_array_almost_equal(bar[:], ranarr2)
        # file does not actually exist on disk
        assert os.path.isfile(self.file)==False 
        # open persisted file.
        # first, check that file does actually exist on disk
        assert os.path.isfile(self.file2)==True 
        f = netCDF4.Dataset(self.file2)
        foo = f.variables['data1']
        # check shape.
        self.assertTrue(foo.shape == (n1dim,n2dim,n3dim))
        # check data.
        assert_array_almost_equal(foo[:], ranarr)
        f.close()

if __name__ == '__main__':
    unittest.main()