hdf images hdf images

HDF5 Tutorial:   Learning The Basics
Creating an HDF5 File

Contents:


What is an HDF5 file?

An HDF5 file is a binary file containing scientific data and supporting metadata.

To create an HDF5 file, an application must specify not only a file name, but a file access mode, a file creation property list, and a file access property list. These terms are described below:

Please refer to the H5F section of the HDF5 Users' Guide and Reference Manual for detailed information regarding file access/creation property lists and access modes.

The steps to create and close an HDF5 file are as follows:

Programming Example

Description

The following example code demonstrates how to create and close an HDF5 file.

C:

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

   int main() {

      hid_t       file_id;   /* file identifier */
      herr_t      status;

      /* Create a new file using default properties. */
      file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

      /* Terminate access to the file. */
      status = H5Fclose(file_id); 
   }

Fortran 90:

  PROGRAM FILEEXAMPLE

     USE HDF5 ! This module contains all necessary modules 
        
     IMPLICIT NONE

     CHARACTER(LEN=8), PARAMETER :: filename = "filef.h5" ! File name
     INTEGER(HID_T) :: file_id                            ! File identifier
 
     INTEGER     ::   error  ! Error flag
     
!
!    Initialize FORTRAN interface.
!
     CALL h5open_f (error)
     !
     ! Create a new file using default properties.
     ! 
     CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)

     !
     ! Terminate access to the file.
     !
     CALL h5fclose_f(file_id, error)
!
!    Close FORTRAN interface.
!
     CALL h5close_f(error)
     END PROGRAM FILEEXAMPLE

See HDF5 Introductory Examples for the examples used in the Learning the Basics tutorial.

For details on compiling an HDF5 application: [ Compile Information ]

Remarks

File Contents

The HDF Group has developed tools for examining the contents of HDF5 files. The tool used throughout the HDF5 tutorial is the HDF5 dumper, h5dump, which displays the file contents in human-readable form. The output of h5dump is an ASCII display formatted according to the HDF5 DDL grammar. This grammar is defined, using Backus-Naur Form, in the DDL in BNF for HDF5.

To view the HDF5 file contents, simply type:

   h5dump <filename> 
Figure 4.1 describes the file contents of file.h5 (filef.h5) using a directed graph.

Fig. 4.1   Contents of file.h5 (filef.h5)


Figure 4.2 is the text description of file.h5, as generated by h5dump. The HDF5 file called file.h5 contains a group called /, or the root group. (The file called filef.h5, created by the FORTRAN version of the example, has the same output except that the filename shown is filef.h5.)

Fig. 4.2   file.h5 in DDL

         HDF5 "file.h5" {
         GROUP "/" {
         }
         }

File Definition in DDL

Figure 4.3 is the simplified DDL file definition for creating an HDF5 file. For simplicity, a simplified DDL is used in this tutorial. A complete and more rigorous DDL can be found in the DDL in BNF for HDF5, a section of the HDF5 User's Guide.

Fig. 4.3   HDF5 File Definition

The following symbol definitions are used in the DDL:

        ::=               defined as
        <tname>           a token with the name tname
        <a> | <b>         one of <a> or <b>
        <a>*              zero or more occurrences of <a>

The simplified DDL for file definition is as follows:

        <file> ::= HDF5 "<file_name>" { <root_group> }

        <root_group> ::= GROUP "/" { <group_attribute>* 
                                        <group_member>* }

        <group_attribute> ::= <attribute>

        <group_member> ::= <group> | <dataset>


- - Last modified: 21 December 2016