HDF5: API Example Code

Example programs/sections of code below:

#1
A simple example showing how to create a file.
#2
A example showing how to create a homogenous multi-dimensional dataset.
#3
A example showing how to read a generic dataset.

Simple Example showing how to create a file.

Notes:
This example creates a new HDF5 file and allows write access. If the file exists already, the H5F_ACC_TRUNC flag would also be necessary to overwrite the previous file's information.

Code:

    hid_t file_id;

    file_id=H5Fcreate("example1.h5",H5F_ACC_EXCL,H5P_DEFAULT_TEMPLATE,H5P_DEFAULT_TEMPLATE);

    H5Fclose(file_id);


Example showing how create a homogenous multi-dimensional dataset.

Notes:
This example creates a 4-dimensional dataset of 32-bit floating-point numbers, corresponding to the current Scientific Dataset functionality.

Code:

 1 hid_t file_id;              /* new file's ID */
 2 hid_t dim_id;               /* new dimensionality's ID */
 3 int rank=4;                 /* the number of dimensions */
 4 hsize_t dims[4]={6,5,4,3};  /* the size of each dimension */
 5 hid_t dataset_id;           /* new dataset's ID */
 6 float buf[6][5][4][3];      /* storage for the dataset's data */
 7 herr_t status;              /* function return status */
 8 
 9 file_id = H5Fcreate ("example3.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
10                      H5P_DEFAULT);
11 assert (file_id >= 0);
12 
13 /* Create & initialize a dimensionality object */
14 dim_id = H5Screate_simple (rank, dims);
15 assert (dim_id >= 0);
16 
17 /* Create & initialize the dataset object */
18 dataset_id = H5Dcreate (file_id, "Simple Object", H5T_NATIVE_FLOAT,
19                         dim_id, H5P_DEFAULT);
20 assert (dataset_id >= 0);
21 
22 <initialize data array>
23 
24 /* Write the entire dataset out */
25 status = H5Dwrite (dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
26                    H5P_DEFAULT, buf);
27 assert (status >= 0);
28 
29 /* Release the IDs we've created */
30 H5Sclose (dim_id);
31 H5Dclose (dataset_id);
32 H5Fclose (file_id);

Example showing how read a generic dataset.

Notes:
This example shows how to get the information for and display a generic dataset.

Code:

 1 hid_t file_id;          /* file's ID */
 2 hid_t dataset_id;       /* dataset's ID in memory */
 3 hid_t space_id;         /* dataspace's ID in memory */
 4 uintn nelems;           /* number of elements in array */
 5 double *buf;            /* pointer to the dataset's data */
 6 herr_t status;          /* function return value */
 7 
 8 file_id = H5Fopen ("example6.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
 9 assert (file_id >= 0);
10 
11 /* Attach to a datatype object */
12 dataset_id = H5Dopen (file_id, "dataset1");
13 assert (dataset_id >= 0);
14 
15 /* Get the OID for the dataspace */
16 space_id = H5Dget_space (dataset_id);
17 assert (space_id >= 0);
18 
19 /* Allocate space for the data */
20 nelems = H5Sget_npoints (space_id);
21 buf = malloc (nelems * sizeof(double));
22 
23 /* Read in the dataset */
24 status = H5Dread (dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL,, H5S_ALL,
25                   H5P_DEFAULT, buf);
26 assert (status >= 0);
27 
28 /* Release the IDs we've accessed */
29 H5Sclose (space_id);
30 H5Dclose (dataset_id);
31 H5Fclose (file_id);