""" This example shows how to read and write data to a dataset using the Fletcher32 checksum filter. The program first checks if the Fletcher32 filter is available, then if it is it writes integers to a dataset using Fletcher32, then closes the file. Next, it reopens the file, reads back the data, checks if the filter detected an error and outputs the type of filter and the maximum value in the dataset to the screen. """ import sys import numpy as np import h5py FILE = "h5ex_d_checksum.h5" DATASET = "DS1" # Strings are handled very differently between python2 and python3. if sys.hexversion >= 0x03000000: FILE = FILE.encode() DATASET = DATASET.encode() DIM0 = 32 DIM1 = 64 CHUNK0 = 4 CHUNK1 = 8 chunk = (CHUNK0, CHUNK1) def run(): # Check if the fletcher32 filter is available and can be used for both # encoding and decoding. if not h5py.h5z.filter_avail(h5py.h5z.FILTER_FLETCHER32): raise RuntimeError("Fletcher32 filter not available.") filter_info = h5py.h5z.get_filter_info(h5py.h5z.FILTER_FLETCHER32) if ((filter_info & h5py.h5z.FILTER_CONFIG_ENCODE_ENABLED) & (filter_info & h5py.h5z.FILTER_CONFIG_DECODE_ENABLED)): msg = "Fletcher32 filter not available for encoding and decoding." raise RuntimeError(msg) # Initialize the data. wdata = np.zeros((DIM0, DIM1), dtype=np.int32) for i in range(DIM0): for j in range(DIM1): wdata[i][j] = i * j - j # Create a new file using the default properties. fid = h5py.h5f.create(FILE) # Create the dataspace. No maximum size parameter needed. dims = (DIM0, DIM1) space_id = h5py.h5s.create_simple(dims) # Create the dataset creation property list, add the fletcher32 filter # and set a chunk size. dcpl = h5py.h5p.create(h5py.h5p.DATASET_CREATE) dcpl.set_fletcher32() dcpl.set_chunk(chunk) # Create the datasets using the dataset creation property list. dset = h5py.h5d.create(fid, DATASET, h5py.h5t.STD_I32LE, space_id, dcpl, h5py.h5p.DEFAULT) dset.write(h5py.h5s.ALL, h5py.h5s.ALL, wdata) # Explicitly close and release resources. del space_id del dcpl del dset del fid # Open file and dataset using the default properties. fid = h5py.h5f.open(FILE) dset = h5py.h5d.open(fid, DATASET) dcpl = dset.get_create_plist() # Retrieve and print the filter type. We know there is only one filter, # so the index is zero. filter_type, flags, vals, name = dcpl.get_filter(0) if filter_type != h5py.h5z.FILTER_FLETCHER32: raise RuntimeError("Fletcher32 filter not retrieved.") else: print("Filter type is H5Z_FILTER_FLETCHER32.") rdata = np.zeros((DIM0, DIM1)) dset.read(h5py.h5s.ALL, h5py.h5s.ALL, rdata) # Verify that the dataset was read correctly. np.testing.assert_array_equal(rdata, wdata) if __name__ == "__main__": run()