A few examples of more advanced features are included here. See the Examples by API for many more examples.

Select a subset from a 2D dataset and write it to a plane of a 3D dataset

HDF5 allows you to read or write to a portion of a dataset by use of hyperslab selection. A hyperslab selection can be a logically contiguous collection of points in a dataspace, or it can be a regular pattern of points or blocks in a dataspace. You can select a hyperslab to write to or read from with the function H5S_SELECT_HYPERSLAB.

Programming Example

Description

This example creates a 5 x 6 integer array in a file called sds.h5 (sdsf.h5 in FORTRAN). It selects a 3 x 4 hyperslab from the dataset as follows (Dimension 0 is offset by 1 and Dimension 1 is offset by 2):

5 x 6 array:

      
  XXXX
  XXXX
      XXXX
      

Then it reads the hyperslab from this file into a 2-dimensional plane (size 7 x 7) of a 3-dimensional array (size 7 x 7 x 3), as follows (with Dimension 0 offset by 3):

 

       
       
       
XXXX         
XXXX   
XXXX   
       

To obtain the example, download:      C    F90

Remarks


Select and write to independent points in a dataset

The H5S_SELECT_ELEMENTS call allows you to select individual points in a dataspace. By use of it, you can read and write to individual points in a dataset.

The example below creates two HDF5 files, copy1.h5 and copy2.h5. In copy1.h5, it creates a 3 x 4 dataset called "Copy1" and writes zeroes to it. In copy2.h5, it creates a 3 x 4 dataset called "Copy2" and writes ones to it. Then it reopens both files and datasets, selects points in copy1.h5 and writes values to them. It copies the selection and writes values to copy2.h5. Lastly, it closes and reopens the files and prints the contents of the datasets.

See the example programs:    C     F90    Python

For details on compiling an HDF5 application click here.

Remarks

File Contents

Following is the DDL for copy1.h5 and copy2.h5, as viewed with the following commands:
             h5dump copy1.h5
             h5dump copy2.h5

 


C:

Fig. S.1a   copy1.h5 in DDL

   HDF5 "copy1.h5" {
   GROUP "/" {
      DATASET "Copy1" {
         DATATYPE { H5T_STD_I32BE }
         DATASPACE { SIMPLE ( 3, 4 ) / ( 3, 4 ) }
         DATA {
            0, 59, 0, 53,
            0, 0, 0, 0,
            0, 0, 0, 0
         }
      }
   }
   }

Fig. S.1b   copy2.h5 in DDL

   HDF5 "copy2.h5" {
   GROUP "/" {
      DATASET "Copy2" {
         DATATYPE { H5T_STD_I32BE }
         DATASPACE { SIMPLE ( 3, 4 ) / ( 3, 4 ) }
         DATA {
            1, 59, 1, 53,
            1, 1, 1, 1,
            1, 1, 1, 1
         }
      }
   }
   }

FORTRAN:

Fig. S.2a   copy1.h5 in DDL

   HDF5 "copy1.h5" {
   GROUP "/" {
      DATASET "Copy1" {
         DATATYPE { H5T_STD_I32BE }
         DATASPACE { SIMPLE ( 4, 3 ) / ( 4, 3 ) }
         DATA {
            0, 0, 0,
            53, 0, 0,
            0, 0, 0,
            59, 0, 0
         }
      }
   }
   }

Fig. S.2b   copy2.h5 in DDL

   HDF5 "copy2.h5" {
   GROUP "/" {
      DATASET "Copy2" {
         DATATYPE { H5T_STD_I32BE }
         DATASPACE { SIMPLE ( 4, 3 ) / ( 4, 3 ) }
         DATA {
            1, 1, 1,
            53, 1, 1,
            1, 1, 1,
            59, 1, 1
         }
      }
   }
   }

 

Creating a dataset with a compound datatype

A compound datatype is similar to a struct in C or a common block in FORTRAN. It is a collection of one or more datatypes and can include compound datatypes. To create and use a compound datatype you need to be familiar with various properties of the compound datatype:

Properties of members of a compound datatype are defined when the member is added to the compound datatype and cannot be subsequently modified.

Compound datatypes must be built out of other datatypes. First, one creates an empty compound datatype and specifies its total size. Then members are added to the compound datatype in any order.

High Level APIs

The High Level HDF5 Table APIs (H5TB) include functions to easily create tables in HDF5, using a compound datatype. Please be sure to review them, in addition to this tutorial.

Programming Example

Description

This example shows how to create a dataset with a compound datatype, write data to it, and then read data back:   C    F90

Remarks

 

 


References to Objects

In HDF5, objects (i.e. groups, datasets, and named datatypes) are usually accessed by name. This access method was discussed in previous sections. There is another way to access stored objects - by reference.

An object reference is based on the relative file address of the object header in the file and is constant for the life of the object. Once a reference to an object is created and stored in a dataset in the file, it can be used to dereference the object it points to. References are handy for creating a file index or for grouping related objects by storing references to them in one dataset.

Creating and Storing References to Objects

The following steps are involved in creating and storing file references to objects:

Reading References and Accessing Objects Using References

The following steps are involved in reading references to objects and accessing objects using references:

Programming Example

Description

The examples below create objects (group, dataset, ...), create references to those objects (in an object reference dataset), and then close the objects and object reference dataset. Then they re-open the object references dataset, and re-open the objects by dereferencing the references in the object reference dataset:

C    F90

Remarks


 

References to Dataset Regions

Previously you learned about creating, reading, and writing dataset selections. Here you will learn how to store dataset selections in a file, and how to read them back using references to dataset regions.

A dataset region reference points to the dataset selection by storing the relative file address of the dataset header and the global heap offset of the referenced selection. The selection referenced is located by retrieving the coordinates of the areas in the selection from the global heap. This internal mechanism of storing and retrieving dataset selections is transparent to the user. A reference to a dataset selection (a region) is constant for the life of the dataset.

Creating and Storing References to Dataset Regions

The following steps are involved in creating and storing references to dataset regions:

  1. Create a dataset in which to store the dataset regions (the selections).

     

  2. Create selections in the dataset(s). The dataset(s) should already exist in the file.

     

  3. Create references to the selections and store them in a buffer.

     

  4. Write the dataset region references to the file.

     

  5. Close all objects.

Reading References to Dataset Regions

The following steps are involved in reading references to dataset regions and referenced dataset regions (selections).

  1. Open and read the dataset containing references to the dataset regions. The datatype H5T_STD_REF_DSETREG must be used during the read operation.

     

  2. Use H5Rdereference / h5rdeference_f to obtain the dataset identifier from the read dataset region reference. OR Use H5Rget_region / h5rget_region_f to obtain the dataspace identifier for the dataset containing the selection from the read dataset region reference.

     

  3. Obtain information about the selection or read selected data from the dataset.

     

  4. Close all objects when they are no longer needed.

Programming Example

Description

The examples below create a dataset in a file, and write references to regions in the dataset to a dataset of region references. (The regions in the dataset are selected using H5S_SELECT_HYPERSLAB and H5S_SELECT_ELEMENTS.)

Then it reopens the file, dereferences the references and outputs the referenced regions to the screen.

Examples:    C    F90

Remarks


 Mounting a file

HDF5 allows you to combine two or more HDF5 files in memory in a manner similar to mounting files in UNIX. The group structure and metadata from one file appear as though they exist in another file. The following steps are involved:

  1. Open the files.
  2. Choose the mount point in the first file (the parent file). The mount point in HDF5 is a group, which CANNOT be the root group.
  3. Use the HDF5 routine H5Fmount / h5fmount_f to mount the second file (the child file) in the first file.
  4. Work with the objects in the second file as if they were members of the mount point group in the first file. The previous contents of the mount point group are temporarily hidden.
  5. Unmount the second file using H5Funmount / h5funmount_f when the work is done.

Programming Example

Description

In the following example, we create one file containing a group and another file containing a dataset. Mounting is used to access the dataset from the second file as a member of a group in the first file. The following figures illustrate this concept.

             FILE1                                   FILE2
  
      --------------------                   --------------------
      !                  !                   !                  !
      !      /           !                   !       /          !
      !       |          !                   !        |         !
      !       |          !                   !        |         !
      !       V          !                   !        V         !
      !     --------     !                   !     ----------   !
      !     ! Group !    !                   !     ! Dataset!   !
      !     ---------    !                   !     ----------   !
      !------------------!                   !------------------! 

After mounting FILE2 under the group in FILE1, the parent file has the following structure:

 
                                FILE1                                 
  
                         --------------------                   
                         !                  !                  
                         !      /           !               
                         !       |          !            
                         !       |          !         
                         !       V          !    
                         !     --------     !              
                         !     ! Group !    !            
                         !     ---------    !           
                         !         |        !
                         !         |        !
                         !         V        !
                         !    -----------   !
                         !    ! Dataset !   !
                         !    !----------   !
                         !                  !
                         !------------------!                    

[ C program ] - h5_mount.c
[ FORTRAN program ] - mountexample.f90

For details on compiling an HDF5 application: [ Compiling HDF5 Applications ]

Remarks


What is a File Driver ?

In HDF5, a file driver is a mapping between the HDF5 format address space and storage. By default, HDF5 simply maps the format address space directly onto a single file.

However, users may want the ability to map the format address space onto different types of storage with various types of maps. With HDF5 we provide a small set of pre-defined file drivers, and we also provide the Virtual File Layer API to enable users to implement their own mappings.

Detailed information on file drivers can be found under VFL Technical Notes in the Documentation.

File Drivers Defined in HDF5

Following are the file drivers that HDF5 provides.

Programming Model for Using a Pre-Defined File Driver

User Designed File Drivers

These are out of the scope of this tutorial. Refer to the Technical Notes documentation on the Virtual File Layer.

How Does a General Application Open an HDF5 File ?

A general application does not know what drivers were used to create a file. It would have to try different file drivers until it succeeds. An example of a general application is the h5dump tool that we provide with HDF5.