Adding Attributes
Contents:
There are two categories of attributes, User-Defined and
Pre-Defined. This tutorial only covers User-Defined
attributes. There are specialized attribute routines for pre-defined
attributes, which you can read about in the HDF User's Guide.
There are three types of User-Defined attributes:
- File Attributes - describe an entire file (global attributes)
- Dataset Attributes - describe individual SDS arrays (local
attributes)
- Dimension Attributes - provide information applicable to an SDS
dimension
The function you use to add attributes to an HDF file is:
Depending on the type of attribute you are creating, you pass
this function either the file identifier, SDS identifier, or dimension
identifier.
The following sample program shows how to add attributes to an HDF file
(You can either download the C or FORTRAN program at the end, or just take a
look at the HTML version with the HDF calls highlighted):
If you have downloaded this program, then you can
compile and run it. It adds a file attribute,
an SDS attribute, and a dimension attribute to the sd.hdf file
created previously.
Following is a detailed review of what this program does:
- Opens sd.hdf, created previously, with Write access.
- Creates a file attribute called "file_contents" by passing the sd_id
to SDsetattr (sfscatt). For FORTRAN, the sfscatt function is
called to create a character attribute.
- Selects the SDS with index 0, and creates a dataset attribute by passing
the SDS identifier (sds_id) to SDsetattr (sfsnatt). For FORTRAN, the
sfsnatt function is called to create a numeric attribute.
- Gets the dimension identifier (dim_id) for Dimension 0, by calling
SDgetdimid (sfdimid).
- Creates a dimension attribute called "dim_metric" by passing the
dimension identifier (dim_id) to
SDsetattr
(sfscatt). For FORTRAN, the sfscatt function is
called to create a character attribute.
- Terminates access to the data array and the SD interface, and closes the file.
NOTE:
When creating dimension attributes (as well as dimension scales, which
have not yet been covered), a new dataset actually gets created for the dimensions.
The data for this new dataset is undefined (FloatInf values show up), because no
dimension scale was set.
If you run the sd_gi example from the previous tutorial after you have
added the attributes from this tutorial, you will notice that there is another
dataset listed.
To view the attributes, you can examine the contents of sd.hdf, using the
hdp command, as follows:
- To see the file attribute:
- hdp list -a sd.hdf
- To see the dataset attribute and dimension attribute:
- hdp dumpsds sd.hdf
Take a look at the new dataset, as described in the NOTE: above.
Programs used in this tutorial:
[Compiling a program]