Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test group existence in hdf5/c++

Tags:

c++

hdf5

I am opening an existing HDF5 file for appending data; I want to assure that group called /A exists for subsequent access. I am looking for an easy way to either create /A conditionally (create and return new group if not existing, or return the existing group). One way is to test for /A existence. How can I do it efficiently?

According to the API docs, I can do something like this:

H5::H5File h5file(filename,H5F_ACC_RDWR);
H5::H5Group grp;
try{
   grp=h5file.openGroup("A");
} catch(H5::Exception& e){
   /* group does not exists, create it */
   grp=h5file.createGroup("A");
}

but the obvious ugliness comes from the fact that exception is used to communicate information which is not exceptional at all.

There is H5::CommonFG::getObjinfo, which seems to wrap H5Gget_objinfo in such way that false (nonexistent) return value of the C routine throws an exception; so again the same problem.

Is it clean to recourse to the C API in this case, or is there some function directly designed to test existence in the C++ API which I am overlooking?

like image 600
eudoxos Avatar asked Feb 27 '16 09:02

eudoxos


People also ask

How do I check my HDF5 data?

View Metadata of an HDF5 Object To view the metadata of a data object, Right click on the object and then select 'Show Properties'. A window will open and display metadata information such as name, type, attributes, data type, and data space.

What is an HDF5 group?

Groups are the container mechanism by which HDF5 files are organized. From a Python perspective, they operate somewhat like dictionaries. In this case the “keys” are the names of group members, and the “values” are the members themselves ( Group and Dataset ) objects.

Why is HDF5 file so large?

This is probably due to your chunk layout - the more chunk sizes are small the more your HDF5 file will be bloated. Try to find an optimal balance between chunk sizes (to solve your use-case properly) and the overhead (size-wise) that they introduce in the HDF5 file.

What is .H5 format?

An H5 is one of the Hierarchical Data Formats (HDF) used to store large amount of data. It is used to store large amount of data in the form of multidimensional arrays. The format is primarily used to store scientific data that is well-organized for quick retrieval and analysis.


1 Answers

As suggested by Yossarian in his comment and in this answer you can use HDF5 1.8.0+ C-API

bool pathExists(hid_t id, const std::string& path)
{
  return H5Lexists( id, path.c_str(), H5P_DEFAULT ) > 0;
}

H5::H5File h5file(filename,H5F_ACC_RDWR);
H5::H5Group grp;
if (pathExists(h5file.getId(), "A")) 
   grp=h5file.openGroup("A");
else
   grp=h5file.createGroup("A");
like image 141
Peter Avatar answered Oct 14 '22 13:10

Peter