[ HDF5 Tutorial Top ]

# Creating an Attribute

## Contents:

- What is an Attribute?
- Programming Example
  - Description
  - Remarks
  - File Contents
  - Attribute Definition in DDL

## What is an Attribute?

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`/`h5acreate_f` and `H5Aclose`/`h5aclose_f`. For example:

C:

         attr_id = H5Acreate (dset_id, attr_name, type_id, space_id, creation_prp);
         status = H5Aclose (attr_id);

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`/`h5aread_f` and/or `H5Awrite`/`h5awrite_f` routines. For example:

C:

        status = H5Aread (attr_id, mem_type_id, buf);
        status = H5Awrite (attr_id, mem_type_id, buf);

FORTRAN:

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

## Programming Example

### Description

This 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.

[ C Example ] - `h5_crtatt.c`
[ FORTRAN Example ] - `attrexample.f90`
[ Java Example ] - `CreateAttribute.java`

NOTE: To download a tar file of the examples, including a Makefile, please go to the References page.

### Remarks

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

  C:

        hid_t H5Acreate (hid_t obj_id, const char *name, hid_t type_id,
                         hid_t space_id, hid_t creation_prp)

  FORTRAN:

        h5acreate_f (obj_id, name, type_id, space_id, attr_id, &
                     hdferr, creation_prp)

                  obj_id        INTEGER(HID_T)
                  name          CHARACTER(LEN=*)
                  type_id       INTEGER(HID_T)
                  space_id      INTEGER(HID_T)
                  attr_id       INTEGER(HID_T)
                  hdferr        INTEGER
                                (Possible values: 0 on success and -1 on failure)
                  creation_prp  INTEGER(HID_T), OPTIONAL

  - The obj_id parameter is the identifier of the object that the attribute is attached to.

  - The name parameter is the name of the attribute to create.

  - The type_id parameter is the identifier of the attribute's datatype.

  - The space_id parameter is the identifier of the attribute's dataspace.

  - The creation_prp parameter is the creation property list identifier. `H5P_DEFAULT` in C (`H5P_DEFAULT_F` in FORTRAN) specifies the default creation property list. This parameter is optional in FORTRAN; when it is omitted, the default creation property list is used.

  - In FORTRAN, the return code for this call is returned in hdferr: 0 if successful, -1 if not. The attribute identifier is returned in attr_id. In C, the function returns the attribute identifier if successful and a negative value if not.

- `H5Awrite`/`h5awrite_f` writes the entire attribute, and returns the status of the write.

  C:

        herr_t H5Awrite (hid_t attr_id, hid_t mem_type_id, void *buf)

  FORTRAN:

        h5awrite_f (attr_id, mem_type_id, buf, hdferr)

                  attr_id     INTEGER(HID_T)
                  memtype_id  INTEGER(HID_T)
                  buf         TYPE(VOID)
                  hdferr      INTEGER
                              (Possible values: 0 on success and -1 on failure)

  - The attr_id parameter is the identifier of the attribute to write.

  - The mem_type_id parameter is the identifier of the attribute's memory datatype.

  - The buf parameter is the data buffer to write out.

  - In C, this function returns a non-negative value if successful and a negative value, otherwise. In FORTRAN, the return value is in the hdferr parameter: 0 if successful, -1 otherwise.

- When an attribute is no longer accessed by a program, `H5Aclose`/`h5aclose_f` must be called to release the attribute from use. The C routine returns a non-negative value if successful; otherwise it returns a negative value. In FORTRAN, the return value is in the hdferr parameter: 0 if successful, -1 otherwise.

  C:

        herr_t H5Aclose (hid_t attr_id)

  FORTRAN:

        h5aclose_f (attr_id, hdferr)

                  attr_id  INTEGER(HID_T)
                  hdferr   INTEGER
                           (Possible values: 0 on success and -1 on failure)

  - An `H5Aclose`/`h5aclose_f` call is mandatory.

### File Contents

The contents of `dset.h5` (`dsetf.h5` for FORTRAN) and the attribute definition are shown below:

Fig. 7.1a `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
             }
          }
       }
    }
    }

Fig. 7.1b `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

Fig. 7.2 HDF5 Attribute Definition

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

The National Center for Supercomputing Applications
University of Illinois at Urbana-Champaign
hdfhelp@ncsa.uiuc.edu

###### Last Modified: June 22, 2001