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
Creating an Attribute

Navigate back: Main / Getting Started with HDF5 / Learning the Basics


Attributes are small datasets that can be used to describe the nature and/or the intended usage of the object they are attached to. In this section, we show how to create, read, and write an attribute.

Creating an attribute

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

The steps to create an attribute are as follows:

  1. Obtain the object identifier that the attribute is to be attached to.
  2. 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.
  3. Create the attribute.
  4. Close the attribute and datatype, dataspace, and attribute creation property list, if necessary.

To create and close an attribute, the calling program must use H5Acreate and H5Aclose. For example:

C

attr_id = H5Acreate (dataset_id, "Units", H5T_STD_I32BE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT)
status = H5Aclose (attr_id);
#define H5P_DEFAULT
Definition H5Ppublic.h:102
#define H5Acreate
Definition H5version.h:868
herr_t H5Aclose(hid_t attr_id)
Closes the specified attribute.
#define H5T_STD_I32BE
Definition H5Tpublic.h:305

Fortran

CALL h5acreate_f (dset_id, attr_nam, type_id, space_id, attr_id, &
hdferr, creation_prp=creat_plist_id)
or
CALL h5acreate_f (dset_id, attr_nam, type_id, space_id, attr_id, hdferr)
CALL h5aclose_f (attr_id, hdferr)

Reading/Writing an attribute

Attributes may only be read or written as an entire object; no partial I/O is supported. Therefore, to perform I/O operations on an attribute, the application needs only to specify the attribute and the attribute's memory datatype.

The steps to read or write an attribute are as follows.

  1. Obtain the attribute identifier.
  2. Specify the attribute's memory datatype.
  3. Perform the desired operation.
  4. Close the memory datatype if necessary.

To read and/or write an attribute, the calling program must contain the H5Aread and/or H5Awrite routines. For example:

C

status = H5Aread (attr_id, mem_type_id, buf);
status = H5Awrite (attr_id, mem_type_id, buf);
herr_t H5Aread(hid_t attr_id, hid_t type_id, void *buf)
Reads the value of an attribute.
herr_t H5Awrite(hid_t attr_id, hid_t type_id, const void *buf)
Writes data to an attribute.

Fortran

CALL h5awrite_f (attr_id, mem_type_id, buf, dims, hdferr)
CALL h5aread_f (attr_id, mem_type_id, buf, dims, hdferr)

High Level APIs

The High Level HDF5 Lite APIs (H5LT,H5LD) include functions that simplify and condense the steps for creating and reading datasets. Please be sure to review them, in addition to this tutorial.

Programming Example

Description

See Examples from Learning the Basics for the examples used in the Learning the Basics tutorial.

The example shows how to create and write a dataset attribute. It opens an existing file dset.h5 in C (dsetf.h5 in FORTRAN), obtains the identifier of the dataset /dset, defines the attribute's dataspace, creates the dataset attribute, writes the attribute, and then closes the attribute's dataspace, attribute, dataset, and file.

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

Remarks

H5Acreate creates an attribute which is attached to the object specified by the first parameter, and returns an identifier.

H5Awrite writes the entire attribute, and returns the status of the write.

When an attribute is no longer accessed by a program, H5Aclose must be called to release the attribute from use. An H5Aclose/h5aclose_f call is mandatory.

File Contents

Shown below is the contents and the attribute definition of dset.h5 (created by the C program).

dset.h5 in DDL

HDF5 "dset.h5" {
GROUP "/" {
DATASET "dset" {
DATATYPE { H5T_STD_I32BE }
DATASPACE { SIMPLE ( 4, 6 ) / ( 4, 6 ) }
DATA {
1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24
}
ATTRIBUTE "attr" {
DATATYPE { H5T_STD_I32BE }
DATASPACE { SIMPLE ( 2 ) / ( 2 ) }
DATA {
100, 200
}
}
}
}
}

Shown below is the contents and the attribute definition of dsetf.h5 (created by the FORTRAN program).

dsetf.h5 in DDL

HDF5 "dsetf.h5" {
GROUP "/" {
DATASET "dset" {
DATATYPE { H5T_STD_I32BE }
DATASPACE { SIMPLE ( 6, 4 ) / ( 6, 4 ) }
DATA {
1, 7, 13, 19,
2, 8, 14, 20,
3, 9, 15, 21,
4, 10, 16, 22,
5, 11, 17, 23,
6, 12, 18, 24
}
ATTRIBUTE "attr" {
DATATYPE { H5T_STD_I32BE }
DATASPACE { SIMPLE ( 2 ) / ( 2 ) }
DATA {
100, 200
}
}
}
}
}

Attribute Definition in DDL

HDF5 Attribute Definition

<attribute> ::= ATTRIBUTE "<attr_name>" { <datatype>
<dataspace>
<data> }

Navigate back: Main / Getting Started with HDF5 / Learning the Basics