Search code examples
c++validationhdf5hdf

How can I check that a file is a valid HDF5 file?


Based on the example given here, I have a file image loaded into memory as a string with a valid handler. This was done using H5LTopen_file_image().

How can I check that a file is a valid HDF5 file?

I found only a program called H5check, which has a complicated source code. So, I'm wondering, is there a simple function with a simple return value to verify that whatever in the hid_t handler is a valid HDF5 file?


Solution

  • In C++:

        std::string filename = "foo.h5";
        if(!H5::H5File::isHdf5(filename.c_str()))
        {
            std::string err_msg = filename + " is not an HDF5 file.\n";
            throw std::logic_error(err_msg);
        }
    

    In Python, use

    import h5py
    if not h5py.is_hdf5('foo.h5'):
        raise ValueError('Not an hdf5 file')