File: test_cdl.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 (79 lines) | stat: -rw-r--r-- 1,805 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
import unittest
import netCDF4
import os
import pathlib

test_ncdump="""netcdf ubyte {
dimensions:
	d = 2 ;
variables:
	byte ub(d) ;
		ub:_Unsigned = "true" ;
	byte sb(d) ;
	byte sb2(d) ;
		sb2:_Unsigned = "false" ;

// global attributes:
		:_Format = "classic" ;
}
"""
test_ncdump2="""netcdf ubyte {
dimensions:
	d = 2 ;
variables:
	byte ub(d) ;
		ub:_Unsigned = "true" ;
	byte sb(d) ;
	byte sb2(d) ;
		sb2:_Unsigned = "false" ;

// global attributes:
		:_Format = "classic" ;
data:

 ub = 0, -1 ;

 sb = -128, 127 ;

 sb2 = -127, -127 ;
}
"""


ubyte_filename = pathlib.Path(__file__).parent / "ubyte.nc"


@unittest.skipIf(os.getenv("NO_CDL"), "CDL test disabled")
class Test_CDL(unittest.TestCase):
    """
    Test import/export of CDL
    """

    def setUp(self):
        with netCDF4.Dataset(ubyte_filename) as f:
            f.tocdl(outfile="ubyte.cdl", data=True)

    def test_tocdl(self):
        # treated as unsigned integers.
        with netCDF4.Dataset(ubyte_filename) as f:
            assert f.tocdl() == test_ncdump
            assert f.tocdl(data=True) == test_ncdump2

    def test_fromcdl(self):
        with netCDF4.Dataset.fromcdl("ubyte.cdl", ncfilename="ubyte2.nc") as f1:
            with netCDF4.Dataset(ubyte_filename) as f2:
                assert f1.variables.keys() == f2.variables.keys()
                assert f1.filepath() == "ubyte2.nc"
                assert f1.dimensions.keys() == f2.dimensions.keys()
                assert len(f1.dimensions["d"]) == len(f2.dimensions["d"])
                assert (f1["ub"][:] == f2["ub"][:]).all()
                assert (f1["sb"][:] == f2["sb"][:]).all()

        os.remove("ubyte2.nc")

    def tearDown(self):
        # Remove the temporary files
        os.remove('ubyte.cdl')

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