|
| 1 | +import numpy as np |
| 2 | +from netCDF4 import set_alignment, get_alignment, Dataset |
| 3 | +import netCDF4 |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | + |
| 9 | +# During testing, sometimes development versions are used. |
| 10 | +# They may be written as 4.9.1-development |
| 11 | +libversion_no_development = netCDF4.__netcdf4libversion__.split('-')[0] |
| 12 | +libversion = tuple(int(v) for v in libversion_no_development.split('.')) |
| 13 | +has_alignment = (libversion[0] > 4) or ( |
| 14 | + libversion[0] == 4 and (libversion[1] >= 9) |
| 15 | +) |
| 16 | +try: |
| 17 | + has_h5ls = subprocess.check_call(['h5ls', '--version'], stdout=subprocess.PIPE) == 0 |
| 18 | +except Exception: |
| 19 | + has_h5ls = False |
| 20 | + |
| 21 | +file_name = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name |
| 22 | + |
| 23 | + |
| 24 | +class AlignmentTestCase(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + self.file = file_name |
| 27 | + |
| 28 | + # This is a global variable in netcdf4, it must be set before File |
| 29 | + # creation |
| 30 | + if has_alignment: |
| 31 | + set_alignment(1024, 4096) |
| 32 | + assert get_alignment() == (1024, 4096) |
| 33 | + |
| 34 | + f = Dataset(self.file, 'w') |
| 35 | + f.createDimension('x', 4096) |
| 36 | + # Create many datasets so that we decrease the chance of |
| 37 | + # the dataset being randomly aligned |
| 38 | + for i in range(10): |
| 39 | + f.createVariable(f'data{i:02d}', np.float64, ('x',)) |
| 40 | + v = f.variables[f'data{i:02d}'] |
| 41 | + v[...] = 0 |
| 42 | + f.close() |
| 43 | + if has_alignment: |
| 44 | + # ensure to reset the alignment to 1 (default values) so as not to |
| 45 | + # disrupt other tests |
| 46 | + set_alignment(1, 1) |
| 47 | + assert get_alignment() == (1, 1) |
| 48 | + |
| 49 | + def test_version_settings(self): |
| 50 | + if has_alignment: |
| 51 | + # One should always be able to set the alignment to 1, 1 |
| 52 | + set_alignment(1, 1) |
| 53 | + assert get_alignment() == (1, 1) |
| 54 | + else: |
| 55 | + with self.assertRaises(RuntimeError): |
| 56 | + set_alignment(1, 1) |
| 57 | + with self.assertRaises(RuntimeError): |
| 58 | + get_alignment() |
| 59 | + |
| 60 | + # if we have no support for alignment, we have no guarantees on |
| 61 | + # how the data can be aligned |
| 62 | + @unittest.skipIf( |
| 63 | + not has_h5ls, |
| 64 | + "h5ls not found." |
| 65 | + ) |
| 66 | + @unittest.skipIf( |
| 67 | + not has_alignment, |
| 68 | + "No support for set_alignment in libnetcdf." |
| 69 | + ) |
| 70 | + def test_setting_alignment(self): |
| 71 | + # We choose to use h5ls instead of h5py since h5ls is very likely |
| 72 | + # to be installed alongside the rest of the tooling required to build |
| 73 | + # netcdf4-python |
| 74 | + # Output from h5ls is expected to look like: |
| 75 | + """ |
| 76 | +Opened "/tmp/tmpqexgozg1.nc" with sec2 driver. |
| 77 | +data00 Dataset {4096/4096} |
| 78 | + Attribute: DIMENSION_LIST {1} |
| 79 | + Type: variable length of |
| 80 | + object reference |
| 81 | + Attribute: _Netcdf4Coordinates {1} |
| 82 | + Type: 32-bit little-endian integer |
| 83 | + Location: 1:563 |
| 84 | + Links: 1 |
| 85 | + Storage: 32768 logical bytes, 32768 allocated bytes, 100.00% utilization |
| 86 | + Type: IEEE 64-bit little-endian float |
| 87 | + Address: 8192 |
| 88 | +data01 Dataset {4096/4096} |
| 89 | + Attribute: DIMENSION_LIST {1} |
| 90 | + Type: variable length of |
| 91 | + object reference |
| 92 | + Attribute: _Netcdf4Coordinates {1} |
| 93 | + Type: 32-bit little-endian integer |
| 94 | + Location: 1:1087 |
| 95 | + Links: 1 |
| 96 | + Storage: 32768 logical bytes, 32768 allocated bytes, 100.00% utilization |
| 97 | + Type: IEEE 64-bit little-endian float |
| 98 | + Address: 40960 |
| 99 | +[...] |
| 100 | +x Dataset {4096/4096} |
| 101 | + Attribute: CLASS scalar |
| 102 | + Type: 16-byte null-terminated ASCII string |
| 103 | + Attribute: NAME scalar |
| 104 | + Type: 64-byte null-terminated ASCII string |
| 105 | + Attribute: REFERENCE_LIST {10} |
| 106 | + Type: struct { |
| 107 | + "dataset" +0 object reference |
| 108 | + "dimension" +8 32-bit little-endian unsigned integer |
| 109 | + } 16 bytes |
| 110 | + Attribute: _Netcdf4Dimid scalar |
| 111 | + Type: 32-bit little-endian integer |
| 112 | + Location: 1:239 |
| 113 | + Links: 1 |
| 114 | + Storage: 16384 logical bytes, 0 allocated bytes |
| 115 | + Type: IEEE 32-bit big-endian float |
| 116 | + Address: 18446744073709551615 |
| 117 | +""" |
| 118 | + h5ls_results = subprocess.check_output( |
| 119 | + ["h5ls", "--verbose", "--address", "--simple", self.file] |
| 120 | + ).decode() |
| 121 | + |
| 122 | + addresses = { |
| 123 | + f'data{i:02d}': -1 |
| 124 | + for i in range(10) |
| 125 | + } |
| 126 | + |
| 127 | + data_variable = None |
| 128 | + for line in h5ls_results.split('\n'): |
| 129 | + if not line.startswith(' '): |
| 130 | + data_variable = line.split(' ')[0] |
| 131 | + # only process the data variables we care to inpsect |
| 132 | + if data_variable not in addresses: |
| 133 | + continue |
| 134 | + line = line.strip() |
| 135 | + if line.startswith('Address:'): |
| 136 | + address = int(line.split(':')[1].strip()) |
| 137 | + addresses[data_variable] = address |
| 138 | + |
| 139 | + for key, address in addresses.items(): |
| 140 | + is_aligned = (address % 4096) == 0 |
| 141 | + assert is_aligned, f"{key} is not aligned. Address = 0x{address:x}" |
| 142 | + |
| 143 | + # Alternative implementation in h5py |
| 144 | + # import h5py |
| 145 | + # with h5py.File(self.file, 'r') as h5file: |
| 146 | + # for i in range(10): |
| 147 | + # v = h5file[f'data{i:02d}'] |
| 148 | + # assert (dataset.id.get_offset() % 4096) == 0 |
| 149 | + |
| 150 | + def tearDown(self): |
| 151 | + # Remove the temporary files |
| 152 | + os.remove(self.file) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + unittest.main() |
0 commit comments