Please, help us to better serve our user community by answering the following short survey: https://www.hdfgroup.org/website-survey/
HDF5  1.15.0
API Reference
 
Loading...
Searching...
No Matches
HDF5 Attributes

HDF5 Attributes

An HDF5 attribute is a small metadata object describing the nature and/or intended usage of a primary data object. A primary data object may be a dataset, group, or committed datatype.

Introduction

Attributes are assumed to be very small as data objects go, so storing them as standard HDF5 datasets would be quite inefficient. HDF5 attributes are therefore managed through a special attributes interface, Attributes (H5A), which is designed to easily attach attributes to primary data objects as small datasets containing metadata information and to minimize storage requirements.

Consider, as examples of the simplest case, a set of laboratory readings taken under known temperature and pressure conditions of 18.0 degrees Celsius and 0.5 atmospheres, respectively. The temperature and pressure stored as attributes of the dataset could be described as the following name/value pairs:

  • temp=18.0
  • pressure=0.5

While HDF5 attributes are not standard HDF5 datasets, they have much in common:

  • An attribute has a user-defined dataspace and the included metadata has a user-assigned datatype
  • Metadata can be of any valid HDF5 datatype
  • Attributes are addressed by name

But there are some very important differences:

  • There is no provision for special storage such as compression or chunking
  • There is no partial I/O or sub-setting capability for attribute data
  • Attributes cannot be shared
  • Attributes cannot have attributes
  • Being small, an attribute is stored in the object header of the object it describes and is thus attached directly to that object

Attribute Function Summaries

See also
Attributes (H5A) reference manual

Programming Model for Attributes

The figure below shows the UML model for an HDF5 attribute and its associated dataspace and datatype.

The UML model for an HDF5 attribute

Creating an attribute is similar to creating a dataset. To create an attribute, the application must specify the object to which the attribute is attached, the datatype and dataspace of the attribute data, and the attribute creation property list.

The following steps are required to create and write an HDF5 attribute:

  • Obtain the object identifier for the attribute's primary data object
  • Define the characteristics of the attribute and specify the attribute creation property list
    • Define the datatype
    • Define the dataspace
    • Specify the attribute creation property list
  • Create the attribute
  • Write the attribute data (optional)
  • Close the attribute (and datatype, dataspace, and attribute creation property list, if necessary)
  • Close the primary data object (if appropriate)

The following steps are required to open and read/write an existing attribute. Since HDF5 attributes allow no partial I/O, you need specify only the attribute and the attribute's memory datatype to read it:

  • Obtain the object identifier for the attribute's primary data object
  • Obtain the attribute's name or index
  • Open the attribute
  • Get attribute dataspace and datatype (optional)
  • Specify the attribute's memory type
  • Read and/or write the attribute data
  • Close the attribute
  • Close the primary data object (if appropriate)
CreateUpdate
14 {
15 __label__ fail_acpl, fail_attr, fail_file;
16 hid_t file, acpl, fspace, attr;
17
18 unsigned mode = H5F_ACC_TRUNC;
19 char file_name[] = "f1.h5";
20 // attribute names can be arbitrary Unicode strings
21 char attr_name[] = "Χαρακτηριστικό";
22
23 if ((file = H5Fcreate(file_name, mode, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID) {
24 ret_val = EXIT_FAILURE;
25 goto fail_file;
26 }
28 ret_val = EXIT_FAILURE;
29 goto fail_acpl;
30 }
31 // use UTF-8 encoding for the attribute name
32 if (H5Pset_char_encoding(acpl, H5T_CSET_UTF8) < 0) {
33 ret_val = EXIT_FAILURE;
34 goto fail_fspace;
35 }
36 // create a scalar (singleton) attribute
37 if ((fspace = H5Screate(H5S_SCALAR)) == H5I_INVALID_HID) {
38 ret_val = EXIT_FAILURE;
39 goto fail_fspace;
40 }
41 // create an attribute on the root group
42 if ((attr = H5Acreate2(file, attr_name, H5T_STD_I32LE, fspace, acpl, H5P_DEFAULT)) ==
44 ret_val = EXIT_FAILURE;
45 goto fail_attr;
46 }
47
48 H5Aclose(attr);
49fail_attr:
50 H5Sclose(fspace);
51fail_fspace:
52 H5Pclose(acpl);
53fail_acpl:
54 H5Fclose(file);
55fail_file:;
56 }
#define H5F_ACC_TRUNC
Definition H5Fpublic.h:50
int64_t hid_t
Definition H5Ipublic.h:60
#define H5I_INVALID_HID
Definition H5Ipublic.h:75
#define H5P_ATTRIBUTE_CREATE
Definition H5Ppublic.h:64
#define H5P_DEFAULT
Definition H5Ppublic.h:102
@ H5S_SCALAR
Definition H5Spublic.h:77
@ H5T_CSET_UTF8
Definition H5Tpublic.h:96
herr_t H5Pset_char_encoding(hid_t plist_id, H5T_cset_t encoding)
Sets the character encoding used to encode link and attribute names.
hid_t H5Acreate2(hid_t loc_id, const char *attr_name, hid_t type_id, hid_t space_id, hid_t acpl_id, hid_t aapl_id)
Creates an attribute attached to a specified object.
herr_t H5Aclose(hid_t attr_id)
Closes the specified attribute.
herr_t H5Fclose(hid_t file_id)
Terminates access to an HDF5 file.
hid_t H5Fcreate(const char *filename, unsigned flags, hid_t fcpl_id, hid_t fapl_id)
Creates an HDF5 file.
herr_t H5Sclose(hid_t space_id)
Releases and terminates access to a dataspace.
hid_t H5Screate(H5S_class_t type)
Creates a new dataspace of a specified type.
#define H5T_STD_I32LE
Definition H5Tpublic.h:310
herr_t H5Pclose(hid_t plist_id)
Terminates access to a property list.
hid_t H5Pcreate(hid_t cls_id)
Creates a new property list as an instance of a property list class.
91 {
92 __label__ fail_attr, fail_file;
93 hid_t file, attr;
94
95 unsigned mode = H5F_ACC_RDWR;
96 char file_name[] = "f1.h5";
97 char attr_name[] = "Χαρακτηριστικό";
98 int value = 1234;
99
100 if ((file = H5Fopen(file_name, mode, H5P_DEFAULT)) == H5I_INVALID_HID) {
101 ret_val = EXIT_FAILURE;
102 goto fail_file;
103 }
104 if ((attr = H5Aopen(file, attr_name, H5P_DEFAULT)) == H5I_INVALID_HID) {
105 ret_val = EXIT_FAILURE;
106 goto fail_attr;
107 }
108 // update the attribute value
109 if (H5Awrite(attr, H5T_NATIVE_INT, &value) < 0)
110 ret_val = EXIT_FAILURE;
111
112 H5Aclose(attr);
113fail_attr:
114 H5Fclose(file);
115fail_file:;
116 }
#define H5F_ACC_RDWR
Definition H5Fpublic.h:49
hid_t H5Aopen(hid_t obj_id, const char *attr_name, hid_t aapl_id)
Opens an attribute for an object specified by object identifier and attribute name.
herr_t H5Awrite(hid_t attr_id, hid_t type_id, const void *buf)
Writes data to an attribute.
hid_t H5Fopen(const char *filename, unsigned flags, hid_t fapl_id)
Opens an existing HDF5 file.
#define H5T_NATIVE_INT
Definition H5Tpublic.h:767
ReadDelete
60 {
61 __label__ fail_attr, fail_file;
62 hid_t file, attr;
63
64 unsigned mode = H5F_ACC_RDONLY;
65 char file_name[] = "f1.h5";
66 char attr_name[] = "Χαρακτηριστικό";
67 int value;
68
69 if ((file = H5Fopen(file_name, mode, H5P_DEFAULT)) == H5I_INVALID_HID) {
70 ret_val = EXIT_FAILURE;
71 goto fail_file;
72 }
73 if ((attr = H5Aopen(file, attr_name, H5P_DEFAULT)) == H5I_INVALID_HID) {
74 ret_val = EXIT_FAILURE;
75 goto fail_attr;
76 }
77 // read the attribute value
78 if (H5Aread(attr, H5T_NATIVE_INT, &value) < 0)
79 ret_val = EXIT_FAILURE;
80
81 // do something w/ the attribute value
82
83 H5Aclose(attr);
84fail_attr:
85 H5Fclose(file);
86fail_file:;
87 }
#define H5F_ACC_RDONLY
Definition H5Fpublic.h:48
herr_t H5Aread(hid_t attr_id, hid_t type_id, void *buf)
Reads the value of an attribute.
120 {
121 __label__ fail_attr, fail_file;
122 hid_t file;
123
124 unsigned mode = H5F_ACC_RDWR;
125 char file_name[] = "f1.h5";
126 char attr_name[] = "Χαρακτηριστικό";
127
128 if ((file = H5Fopen(file_name, mode, H5P_DEFAULT)) == H5I_INVALID_HID) {
129 ret_val = EXIT_FAILURE;
130 goto fail_file;
131 }
132 // delete the attribute
133 if (H5Adelete(file, attr_name) < 0) {
134 ret_val = EXIT_FAILURE;
135 goto fail_attr;
136 }
137
138fail_attr:
139 H5Fclose(file);
140fail_file:;
141 }
herr_t H5Adelete(hid_t loc_id, const char *attr_name)
Deletes an attribute from a specified location.

Working with Attributes

The Structure of an Attribute

An attribute has two parts: name and value(s).

HDF5 attributes are sometimes discussed as name/value pairs in the form name=value.

An attribute's name is a null-terminated ASCII or UTF-8 character string. Each attribute attached to an object has a unique name.

The value portion of the attribute contains one or more data elements of the same datatype.

HDF5 attributes have all the characteristics of HDF5 datasets except that there is no partial I/O capability. In other words, attributes can be written and read only in full with no sub-setting.

Creating, Writing, and Reading Attributes

If attributes are used in an HDF5 file, these functions will be employed: H5Acreate, H5Awrite, and H5Aread. H5Acreate and H5Awrite are used together to place the attribute in the file. If an attribute is to be used and is not currently in memory, H5Aread generally comes into play usually in concert with one each of the H5Aget_* and H5Aopen_* functions.

To create an attribute, call H5Acreate:

hid_t H5Acreate (hid_t loc_id, const char *name,
hid_t type_id, hid_t space_id, hid_t create_plist,
hid_t access_plist)
#define H5Acreate
Definition H5version.h:868

loc_id identifies the object (dataset, group, or committed datatype) to which the attribute is to be attached. name, type_id, space_id, and create_plist convey, respectively, the attribute's name, datatype, dataspace, and attribute creation property list. The attribute's name must be locally unique: it must be unique within the context of the object to which it is attached.

H5Acreate creates the attribute in memory. The attribute does not exist in the file until H5Awrite writes it there.

To write or read an attribute, call H5Awrite or H5Aread, respectively:

herr_t H5Awrite (hid_t attr_id, hid_t mem_type_id, const void *buf)
herr_t H5Aread (hid_t attr_id, hid_t mem_type_id, void *buf)
int herr_t
Definition H5public.h:235

attr_id identifies the attribute while mem_type_id identifies the in-memory datatype of the attribute data.

H5Awrite writes the attribute data from the buffer buf to the file. H5Aread reads attribute data from the file into buf.

The HDF5 Library converts the metadata between the in-memory datatype, mem_type_id, and the in-file datatype, defined when the attribute was created, without user intervention.

Accessing Attributes by Name or Index

Attributes can be accessed by name or index value. The use of an index value makes it possible to iterate through all of the attributes associated with a given object.

To access an attribute by its name, use the H5Aopen_by_name function. H5Aopen_by_name returns an attribute identifier that can then be used by any function that must access an attribute such as H5Aread. Use the function H5Aget_name to determine an attribute's name.

To access an attribute by its index value, use the H5Aopen_by_idx function. To determine an attribute index value when it is not already known, use the H5Oget_info function. H5Aopen_by_idx is generally used in the course of opening several attributes for later access. Use H5Aiterate if the intent is to perform the same operation on every attribute attached to an object.

Obtaining Information Regarding an Object's Attributes

In the course of working with HDF5 attributes, one may need to obtain any of several pieces of information:

  • An attribute name
  • The dataspace of an attribute
  • The datatype of an attribute
  • The number of attributes attached to an object

To obtain an attribute's name, call H5Aget_name with an attribute identifier, attr_id:

ssize_t H5Aget_name (hid_t attr_id, size_t buf_size, char *buf)
ssize_t H5Aget_name(hid_t attr_id, size_t buf_size, char *buf)
Gets an attribute name.

As with other attribute functions, attr_id identifies the attribute; buf_size defines the size of the buffer; and buf is the buffer to which the attribute's name will be read.

If the length of the attribute name, and hence the value required for buf_size, is unknown, a first call to H5Aget_name will return that size. If the value of buf_size used in that first call is too small, the name will simply be truncated in buf. A second H5Aget_name call can then be used to retrieve the name in an appropriately-sized buffer.

To determine the dataspace or datatype of an attribute, call H5Aget_space or H5Aget_type, respectively:

hid_t H5Aget_type(hid_t attr_id)
Gets an attribute's datatype.
hid_t H5Aget_space(hid_t attr_id)
Gets a copy of the dataspace for an attribute.

H5Aget_space returns the dataspace identifier for the attribute attr_id. H5Aget_type returns the datatype identifier for the attribute attr_id.

To determine the number of attributes attached to an object, use the H5Oget_info function. The function signature is below.

herr_t H5Oget_info( hid_t object_id, H5O_info_t *object_info )
#define H5O_info_t
Definition H5version.h:1310
#define H5Oget_info
Definition H5version.h:1097

The number of attributes will be returned in the object_info buffer. This is generally the preferred first step in determining attribute index values. If the call returns N, the attributes attached to the object object_id have index values of 0 through N-1.

Iterating across an Object's Attributes

It is sometimes useful to be able to perform the identical operation across all of the attributes attached to an object. At the simplest level, you might just want to open each attribute. At a higher level, you might wish to perform a rather complex operation on each attribute as you iterate across the set.

To iterate an operation across the attributes attached to an object, one must make a series of calls to H5Aiterate

herr_t H5Aiterate (hid_t obj_id, H5_index_t index_type,
void *op_data)
herr_t(* H5A_operator2_t)(hid_t location_id, const char *attr_name, const H5A_info_t *ainfo, void *op_data)
Definition H5Apublic.h:55
H5_iter_order_t
Definition H5public.h:344
uint64_t hsize_t
Definition H5public.h:297
H5_index_t
Definition H5public.h:367
#define H5Aiterate
Definition H5version.h:879

H5Aiterate successively marches across all of the attributes attached to the object specified in loc_id, performing the operation(s) specified in op_func with the data specified in op_data on each attribute.

When H5Aiterate is called, index contains the index of the attribute to be accessed in this call. When H5Aiterate returns, index will contain the index of the next attribute. If the returned index is the null pointer, then all attributes have been processed, and the iterative process is complete.

op_func is a user-defined operation that adheres to the H5A_operator_t prototype. This prototype and certain requirements imposed on the operator's behavior are described in the H5Aiterate entry in the HDF5 Reference Manual.

op_data is also user-defined to meet the requirements of op_func. Beyond providing a parameter with which to pass this data, HDF5 provides no tools for its management and imposes no restrictions.

Deleting an Attribute

Once an attribute has outlived its usefulness or is no longer appropriate, it may become necessary to delete it.

To delete an attribute, call H5Adelete

herr_t H5Adelete (hid_t loc_id, const char *name)

H5Adelete removes the attribute name from the group, dataset, or committed datatype specified in loc_id.

H5Adelete must not be called if there are any open attribute identifiers on the object loc_id. Such a call can cause the internal attribute indexes to change; future writes to an open attribute would then produce unintended results.

Closing an Attribute

As is the case with all HDF5 objects, once access to an attribute it is no longer needed, that attribute must be closed. It is best practice to close it as soon as practicable; it is mandatory that it be closed prior to the H5close call closing the HDF5 Library.

To close an attribute, call H5Aclose

H5Aclose closes the specified attribute by terminating access to its identifier, attr_id.

Special Issues

Some special issues for attributes are discussed below.

Large Numbers of Attributes Stored in Dense Attribute Storage

The dense attribute storage scheme was added in version 1.8 so that datasets, groups, and committed datatypes that have large numbers of attributes could be processed more quickly.

Attributes start out being stored in an object's header. This is known as compact storage. For more information, see "Storage Strategies."

As the number of attributes grows, attribute-related performance slows. To improve performance, dense attribute storage can be initiated with the H5Pset_attr_phase_change function. See the HDF5 Reference Manual for more information.

When dense attribute storage is enabled, a threshold is defined for the number of attributes kept in compact storage. When the number is exceeded, the library moves all of the attributes into dense storage at another location. The library handles the movement of attributes and the pointers between the locations automatically. If some of the attributes are deleted so that the number falls below the threshold, then the attributes are moved back to compact storage by the library.

The improvements in performance from using dense attribute storage are the result of holding attributes in a heap and indexing the heap with a B-tree.

Note that there are some disadvantages to using dense attribute storage. One is that this is a new feature. Datasets, groups, and committed datatypes that use dense storage cannot be read by applications built with earlier versions of the library. Another disadvantage is that attributes in dense storage cannot be compressed.

Large Attributes Stored in Dense Attribute Storage

We generally consider the maximum size of an attribute to be 64K bytes. The library has two ways of storing attributes larger than 64K bytes: in dense attribute storage or in a separate dataset. Using dense attribute storage is described in this section, and storing in a separate dataset is described in the next section.

To use dense attribute storage to store large attributes, set the number of attributes that will be stored in compact storage to 0 with the H5Pset_attr_phase_change function. This will force all attributes to be put into dense attribute storage and will avoid the 64KB size limitation for a single attribute in compact attribute storage.

The example code below illustrates how to create a large attribute that will be kept in dense storage.

Create
14 {
15 __label__ fail_acpl, fail_attr, fail_file;
16 hid_t file, acpl, fspace, attr;
17
18 unsigned mode = H5F_ACC_TRUNC;
19 char file_name[] = "f1.h5";
20 // attribute names can be arbitrary Unicode strings
21 char attr_name[] = "Χαρακτηριστικό";
22
23 if ((file = H5Fcreate(file_name, mode, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID) {
24 ret_val = EXIT_FAILURE;
25 goto fail_file;
26 }
28 ret_val = EXIT_FAILURE;
29 goto fail_acpl;
30 }
31 // use UTF-8 encoding for the attribute name
32 if (H5Pset_char_encoding(acpl, H5T_CSET_UTF8) < 0) {
33 ret_val = EXIT_FAILURE;
34 goto fail_fspace;
35 }
36 // create a scalar (singleton) attribute
37 if ((fspace = H5Screate(H5S_SCALAR)) == H5I_INVALID_HID) {
38 ret_val = EXIT_FAILURE;
39 goto fail_fspace;
40 }
41 // create an attribute on the root group
42 if ((attr = H5Acreate2(file, attr_name, H5T_STD_I32LE, fspace, acpl, H5P_DEFAULT)) ==
44 ret_val = EXIT_FAILURE;
45 goto fail_attr;
46 }
47
48 H5Aclose(attr);
49fail_attr:
50 H5Sclose(fspace);
51fail_fspace:
52 H5Pclose(acpl);
53fail_acpl:
54 H5Fclose(file);
55fail_file:;
56 }

Large Attributes Stored in a Separate Dataset

In addition to dense attribute storage (see above), a large attribute can be stored in a separate dataset. In the figure below, DatasetA holds an attribute that is too large for the object header in Dataset1. By putting a pointer to DatasetA as an attribute in Dataset1, the attribute becomes available to those working with Dataset1. This way of handling large attributes can be used in situations where backward compatibility is important and where compression is important. Applications built with versions before 1.8.x can read large attributes stored in separate datasets. Datasets can be compressed while attributes cannot.

A large or shared HDF5 attribute and its associated dataset(s)

Note: In the figure above, DatasetA is an attribute of Dataset1 that is too large to store in Dataset1's header. DatasetA is associated with Dataset1 by means of an object reference pointer attached as an attribute to Dataset1. The attribute in DatasetA can be shared among multiple datasets by means of additional object reference pointers attached to additional datasets.

Shared Attributes

Attributes written and managed through the Attributes (H5A) interface cannot be shared. If shared attributes are required, they must be handled in the manner described above for large attributes and illustrated in the figure above.

Attribute Names

While any ASCII or UTF-8 character may be used in the name given to an attribute, it is usually wise to avoid the following kinds of characters:

  • Commonly used separators or delimiters such as slash, backslash, colon, and semi-colon (\, /, :, ;)
  • Escape characters
  • Wild cards such as asterisk and question mark (*, ?) NULL can be used within a name, but HDF5 names are terminated with a NULL: whatever comes after the NULL will be ignored by HDF5.

The use of ASCII or UTF-8 characters is determined by the character encoding property. See H5Pset_char_encoding in the HDF5 Reference Manual.

No Special I/O or Storage

HDF5 attributes have all the characteristics of HDF5 datasets except the following:

  • Attributes are written and read only in full: there is no provision for partial I/O or sub-setting
  • No special storage capability is provided for attributes: there is no compression or chunking, and attributes are not extendable

Previous Chapter HDF5 Dataspaces and Partial I/O - Next Chapter HDF5 Error Handling