Thursday, February 24, 2011

Compound Data in Packet Table

As I promised, here I am back with some new stuff i.e. Packet Table in HDF5. Packet tables are one of the most flexible data structures in HDF5 which allows you to store data either fixed or variable length. If you have a situation where you need to write your data coming from multiple sources into a single object, packet tables are worth to accomplish your job. There exists one more data structure which is 'Table'. Unlike packet table, this allows you to write only fixed length data defined in a structure. In performance wise also it is recommended to use packet tables over 'tables'. Actually, I was also finding it very hard to implement compound data with Packet table but doing some research and digging into HDF5 libraries I managed to do it. So here we go.

Consider, We have a Structure defined as:
/*
 *
 * Struct MyTable.
 *
 */
typedef struct 
{
    /// Field 1
    int field1;
    /// Field 2
    float field2;
    /// Field 3
    int field3;
}myTable;

Now, we need to create a data type:

/* Create data type to be used to write data in the Table */
hid_t myDataType = H5Tcreate(H5T_COMPOUND, sizeof(myTable));

herr_t status;
status = H5Tinsert (myDataType , "Field 1",
            HOFFSET (myTable, field1), H5T_NATIVE_INT);

status = H5Tinsert (myDataType , "Field 2",
            HOFFSET (myTable, field2), H5T_NATIVE_FLOAT);

status = H5Tinsert (myDataType , "Field 3",
            HOFFSET (myTable, field3), H5T_NATIVE_INT);

Here our compound datatype is ready and we can create a packet table where we can associate this type.

/* Create packet table */
ptMyTable = H5PTcreate_fl(file, "My Table", myDataType , chunk_size, compression);

As, variable length data having memory leak issues(Check here) I have created a packet table using an API for fixed length.

So, if you view it with HDF viewer it will look like:





No comments: