HDF5 documents and links Introduction to HDF5 HDF5 Reference Manual HDF5 User's Guide for Release 1.6 | And in this document, the HDF5 User's Guide from Release 1.4.5: Files Datasets Datatypes Dataspaces Groups References Attributes Property Lists Error Handling Filters Caching Chunking Mounting Files Performance Debugging Environment DDL

# The Attribute Interface (H5A)

## 1. Introduction

The attribute API (H5A) is primarily designed to easily allow small datasets to be attached to primary datasets as metadata information. Additional goals for the H5A interface include keeping storage requirements for each attribute to a minimum and easily sharing attributes among datasets.

Because attributes are intended to be small objects, large datasets intended as additional information for a primary dataset should be stored as supplemental datasets in a group with the primary dataset. Attributes can then be attached to the group containing everything to indicate a particular type of dataset with supplemental datasets is located in the group. How small is "small" is not defined by the library and is up to the user's interpretation.

Attributes are not separate objects in the file, they are always contained in the object header of the object they are attached to. The I/O functions defined below are required to read or write attribute information, not the H5D I/O routines.

## 2. Creating, Opening, Closing and Deleting Attributes

Attributes are created with the `H5Acreate()` function, and existing attributes can be accessed with either the `H5Aopen_name()` or `H5Aopen_idx()` functions. All three functions return an object ID which should be eventually released by calling `H5Aclose()`.

`hid_t H5Acreate (hid_t loc_id, const char *name, hid_t type_id, hid_t space_id, hid_t create_plist_id)`
  This function creates an attribute which is attached to the object specified with loc_id. The name specified with name for each attribute for an object must be unique for that object. The type_id and space_id are created with the H5T and H5S interfaces respectively. Currently only simple dataspaces are allowed for attribute dataspaces. The create_plist_id property list is currently unused, but will be used in the future for optional properties of attributes. The attribute ID returned from this function must be released with H5Aclose or resource leaks will develop. Attempting to create an attribute with the same name as an already existing attribute will fail, leaving the pre-existing attribute in place. This function returns an attribute ID for success or negative for failure.
`hid_t H5Aopen_name (hid_t loc_id, const char *name)`
  This function opens an attribute which is attached to the object specified with loc_id. The name specified with name indicates the attribute to access. The attribute ID returned from this function must be released with H5Aclose or resource leaks will develop. This function returns an attribute ID for success or negative for failure.
`hid_t H5Aopen_idx (hid_t loc_id, unsigned idx)`
  This function opens an attribute which is attached to the object specified with loc_id. The attribute specified with idx indicates the idxth attribute to access, starting with '0'. The attribute ID returned from this function must be released with H5Aclose or resource leaks will develop. This function returns an attribute ID for success or negative for failure.
`herr_t H5Aclose (hid_t attr_id)`
  This function releases an attribute from use. Further use of the attribute ID will result in undefined behavior. This function returns non-negative on success, negative on failure.
`herr_t H5Adelete (hid_t loc_id, const char *name)`
  This function removes the named attribute from a dataset or group. This function should not be used when attribute IDs are open on loc_id as it may cause the internal indexes of the attributes to change and future writes to the open attributes to produce incorrect results. Returns non-negative on success, negative on failure.

## 3. Attribute I/O Functions

Attributes may only be written as an entire object, no partial I/O is currently supported.

`herr_t H5Awrite (hid_t attr_id, hid_t mem_type_id, void *buf)`
  This function writes an attribute, specified with attr_id, with mem_type_id specifying the datatype in memory. The entire attribute is written from buf to the file. This function returns non-negative on success, negative on failure.
`herr_t H5Aread (hid_t attr_id, hid_t mem_type_id, void *buf)`
  This function read an attribute, specified with attr_id, with mem_type_id specifying the datatype in memory. The entire attribute is read into buf from the file. This function returns non-negative on success, negative on failure.

## 4. Attribute Inquiry Functions

`herr_t H5Aiterate (hid_t loc_id, unsigned *attr_number, H5A_operator operator, void *operator_data)`
  This function iterates over the attributes of the dataset or group specified with loc_id. For each attribute of the object, the operator_data and some additional information (specified below) are passed to the operator function. The iteration begins with the *attr_number object in the group and the next attribute to be processed by the operator is returned in *attr_number.

  The iterator returns a negative value if something is wrong, the return value of the last operator if it was non-zero, or zero if all attributes were processed.

  The prototype for H5A_operator_t is:
  `typedef herr_t (*H5A_operator_t)(hid_t loc_id, const char *attr_name, void *operator_data);`

  The operation receives the ID for the group or dataset being iterated over (loc_id), the name of the current attribute about the object (attr_name) and the pointer to the operator data passed in to H5Aiterate (operator_data). The return values from an operator are:

  - Zero causes the iterator to continue, returning zero when all attributes have been processed.
  - Positive causes the iterator to immediately return that positive value, indicating short-circuit success. The iterator can be restarted at the next attribute.
  - Negative causes the iterator to immediately return that value, indicating failure. The iterator can be restarted at the next attribute.
`hid_t H5Aget_space (hid_t attr_id)`
  This function retrieves a copy of the dataspace for an attribute. The dataspace ID returned from this function must be released with H5Sclose or resource leaks will develop. This function returns a dataspace ID for success or negative for failure.
`hid_t H5Aget_type (hid_t attr_id)`
  This function retrieves a copy of the datatype for an attribute. The datatype ID returned from this function must be released with H5Tclose or resource leaks will develop. This function returns a datatype ID for success or negative for failure.
`ssize_t H5Aget_name (hid_t attr_id, size_t buf_size, char *buf)`
  This function retrieves the name of an attribute for an attribute ID. Up to buf_size characters are stored in buf followed by a '\0' string terminator. If the name of the attribute is longer than buf_size-1, the string terminator is stored in the last position of the buffer to properly terminate the string. This function returns the length of the attribute's name (which may be longer than buf_size) on success or negative for failure.
`int H5Aget_num_attrs (hid_t loc_id)`
  This function returns the number of attributes attached to a dataset or group, loc_id. This function returns non-negative for success or negative for failure.

HDF5 documents and links Introduction to HDF5 HDF5 Reference Manual HDF5 User's Guide for Release 1.6 | And in this document, the HDF5 User's Guide from Release 1.4.5: Files Datasets Datatypes Dataspaces Groups References Attributes Property Lists Error Handling Filters Caching Chunking Mounting Files Performance Debugging Environment DDL

Last modified: 6 July 2000