[ HDF5 Tutorial Top ]

Reading from and Writing to a Dataset


Contents:


Reading from and Writing to a Dataset

During a dataset I/O operation, the library transfers raw data between memory and the file. The data in memory can have a datatype different from that of the file and can also be of a different size (i.e., the data in memory is a subset of the dataset elements, or vice versa). Therefore, to perform read or write operations, the application program must specify:

The steps to read from or write to a dataset are as follows:

  1. Obtain the dataset identifier.
  2. Specify the memory datatype.
  3. Specify the memory dataspace.
  4. Specify the file dataspace.
  5. Specify the transfer properties.
  6. Perform the desired operation on the dataset.
  7. Close the dataset.
  8. Close the dataspace, datatype, and property list if necessary.
To read from or write to a dataset, the H5Dread/h5dread_f and H5Dwrite/h5dwrite_f routines are used.

C:

   status = H5Dread (set_id, mem_type_id, mem_space_id, file_space_id,
                     xfer_prp, buf );
   status = H5Dwrite (set_id, mem_type_id, mem_space_id, file_space_id,
                     xfer_prp, buf);

FORTRAN:
   CALL h5dread_f(dset_id, mem_type_id, buf, error, &
                     mem_space_id=mspace_id, file_space_id=fspace_id, &
                     xfer_prp=xfer_plist_id)
        or
   CALL h5dread_f(dset_id, mem_type_id, buf, error)


   CALL h5dwrite_f(dset_id, mem_type_id, buf, error, &
                     mem_space_id=mspace_id, file_space_id=fspace_id, &
                     xfer_prp=xfer_plist_id)
        or
   CALL h5dwrite_f(dset_id, mem_type_id, buf, error)

Programming Example

Description

The following example shows how to read and write an existing dataset. It opens the file created in the previous example, obtains the dataset identifier for the dataset /dset, writes the dataset to the file, then reads the dataset back from memory. It then closes the dataset and file.
NOTE: To download a tar file of the examples, including a Makefile, please go to the References page.

Remarks

File Contents

Figure 6.1a shows the contents of dset.h5 (created by the C program).
Figure 6.1b shows the contents of dsetf.h5 (created by the FORTRAN program).

Fig. 6.1a   dset.h5 in DDL

      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
            }
         }
      }
      }

Fig. 6.1b   dsetf.h5 in DDL

HDF5 "dsetf.h5" {
GROUP "/" {
   DATASET "dset" {
      DATATYPE { H5T_STD_I32BE }
      DATASPACE { SIMPLE ( 6, 4 ) / ( 6, 4 ) }
      DATA {
         1, 7, 13, 19,
         2, 8, 14, 20,
         3, 9, 15, 21,
         4, 10, 16, 22,
         5, 11, 17, 23,
         6, 12, 18, 24
      }
   }
}
}


NCSA
The National Center for Supercomputing Applications

University of Illinois at Urbana-Champaign

hdfhelp@ncsa.uiuc.edu

Last Modified: June 22, 2001