next up previous index
Next: Exercise Up: Creating and Structuring HDF5 Previous: HDF5 Datasets

Writing On and Reading From HDF5 Datasets

In this example we are going to write data on the dataset created in the previous section. This time we are not going to create the file and the dataset again, because they are already there. Instead we are going to open them. Then we'll write on the dataset and then we'll read the data back and then we'll close the dataset and the file.

Here's the program:

/* 
 *   Writing and reading an existing dataset.
 */

#include "hdf5.h"
#define FILE "dset.h5"

main() {

   hid_t       file_id, dataset_id;  /* identifiers */
   herr_t      status;
   int         i, j, dset_data[4][6];

   /* Initialize the dataset. */
   for (i = 0; i < 4; i++)
      for (j = 0; j < 6; j++)
         dset_data[i][j] = i * 6 + j + 1;

   /* Open an existing file. */
   file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);

   /* Open an existing dataset. */
   dataset_id = H5Dopen(file_id, "/dset");

   /* Write the dataset. */
   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
                     dset_data);

   status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
                    dset_data);

   /* Close the dataset. */
   status = H5Dclose(dataset_id);

   /* Close the file. */
   status = H5Fclose(file_id);
}
Let me compile the program first:
gustav@bh1 $ h5cc -o h5_rdwt h5_rdwt.c
gustav@bh1 $
Now I am going to run it, but before I do I'm going to show you what's on the file dset.h5 and then I'm going to show you again what's in it after the program has completed its execution:
gustav@bh1 $ h5dump dset.h5
HDF5 "dset.h5" {
GROUP "/" {
   DATASET "dset" {
      DATATYPE  H5T_STD_I32BE
      DATASPACE  SIMPLE { ( 4, 6 ) / ( 4, 6 ) }
      DATA {
         0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0
      }
   }
}
}
gustav@bh1 $ ./h5_rdwt
gustav@bh1 $ h5dump dset.h5
HDF5 "dset.h5" {
GROUP "/" {
   DATASET "dset" {
      DATATYPE  H5T_STD_I32BE
      DATASPACE  SIMPLE { ( 4, 6 ) / ( 4, 6 ) }
      DATA {
         1, 2, 3, 4, 5, 6,
         7, 8, 9, 10, 11, 12,
         13, 14, 15, 16, 17, 18,
         19, 20, 21, 22, 23, 24
      }
   }
}
}
gustav@bh1 $
Well, we have indeed written the data on the dataset - without corrupting the rest of the file in the process.

So, how it's done.

The program begins with filling the two dimensional array with data:

   for (i = 0; i < 4; i++)
      for (j = 0; j < 6; j++)
         dset_data[i][j] = i * 6 + j + 1;
Then we open the file by calling function  H5Fopen:
   file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
By now you can already tell that this is a file manipulation function, because its name begins with HDF5. The function takes three arguments: So we have opened the file. Now we have to open the dataset:
   dataset_id = H5Dopen(file_id, "/dset");
Function  H5Dopen opens an existing dataset. The first argument is the file identifier and the second argument is the full pathname of the dataset. But you can also use a group identifier in the place of the first argument, if you have it, in which case the second argument would be the name of the fileset in relation to the group. In this case, it would not have the preceding slash.

Once we have opened the dataset, we can write on it. We do this by calling function  H5Dwrite:

   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
                     dset_data);
which takes the following arguments: Function H5Dwrite returns an integer, which is negative if the operation has failed for some reason.

The next step is to read the data back:

   status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
                    dset_data);
The arguments of function  H5Dread here are exactly the same as for H5Dwrite: the operation is entirely symmetric with respect to the latter, which is rather nice. The reason why this can be done this way is because the datasets here are pre-sized. The reading is the same as writing in reverse.

Having finished with the dataset we have to close it first:

   status = H5Dclose(dataset_id);
and then we close the file:
   status = H5Fclose(file_id);



 
next up previous index
Next: Exercise Up: Creating and Structuring HDF5 Previous: HDF5 Datasets
Zdzislaw Meglicki
2004-04-29