There are two groups in the HDF5 file depicted above: Vis and Viz and SimOut. Under the Viz group are a variety of images and a table that is shared with the SimOut group. The SimOut group contains a 3-dimensional array, a 2-dimensional array and a link to a 2-dimensional array in another HDF5 file.
Attributes can optionally be associated with HDF5 objects. They have two parts: a name and a value. Attributes are accessed by opening the object that they are attached to so are not independent objects. Typically an attribute is small in size and contains user metadata about the object that it is attached to.
To introduce the programming model, examples in Python and C are included below. The Python examples use the HDF5 Python APIs (h5py). See the Examples from "Learning the Basics" page for complete examples that can be downloaded and run for C, FORTRAN, C++, Java and Python.
- Open the object.
- Access the object.
- Close the object.
The library imposes an order on the operations by argument dependencies. For example, a file must be opened before a dataset because the dataset open call requires a file handle as an argument. Objects can be closed in any order. However, once an object is closed it no longer can be accessed.
The following Python and C examples create a file,
file.h5, and then close it. The resulting HDF5 file will only contain a root group:
Calling h5py.File with ‘w’ for the file access flag will create a new HDF5 file and overwrite an existing file with the same name. “file” is the file handle returned from opening the file. When finished with the file, it must be closed. When not specifying property lists, the default property lists are used:
The code excerpts below show the calls that need to be made to create a 4 x 6 integer dataset
dset in a file
dset.h5. The dataset will be located in the root group:
With Python, the creation of the dataspace is included as a parameter in the dataset creation method. Just one call will create a 4 x 6 integer dataset
dset. A pre-defined Big Endian 32-bit integer datatype is specified. The create_dataset method creates the dataset in the root group (the file object). The dataset is close by the Python interface.
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the dataset. */
- Open the object that you wish to add an attribute to.
- Create the attribute
- Write to the attribute
- Close the attribute and the object it is attached to.
The example below creates attributes that are attached to the dataset
The dataspace, datatype, and data are specified in the call to create an attribute in Python:
To create an integer attribute in C, you must create the dataspace, create the attribute, write to it and then close it in separate steps:
hid_t attribute_id, dataspace_id; /* identifiers */
/* Initialize the attribute data. */
attr_data = 100;
attr_data = 200;
/* Create the data space for the attribute. */
dataspace_id = H5Screate_simple(1, &dims, NULL);
/* Create a dataset attribute. */
attribute_id = H5Acreate2 (dataset_id, "Units", H5T_STD_I32BE,
dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
/* Write the attribute data. */
status = H5Awrite(attribute_id, H5T_NATIVE_INT, attr_data);
/* Close the attribute. */
status = H5Aclose(attribute_id);
/* Close the dataspace. */
status = H5Sclose(dataspace_id);