An HDF5 dataset is an array of data elements, arranged according to the specifications of the dataspace. In general, a data element is the smallest addressable unit of storage in the HDF5 file. (Compound datatypes are the exception to this rule.) The HDF5 datatype defines the storage format for a single data element (Figure 1).
The model for HDF5 attributes is extremely similar to datasets: an attribute has a dataspace and a datatype, as shown in Figure 1. The information in this chapter applies to both datasets and attributes.
|
Figure 1 |
Abstractly, each data element within the dataset is a sequence of bits, interpreted as a single value from a set of values (e.g., a number or a character). For a given data type, there is a standard or convention for representing the values as bits, and when the bits are represented in a particular storage the bits are laid out in a specific storage scheme, e.g., as 8-bit bytes, with a specific ordering and alignment of bytes within the storage array.
HDF5 datatypes implement a flexible, extensible, and portable mechanism for specifying and discovering the storage layout of the data elements, determining how to interpret the elements (e.g., as floating point numbers), and for transferring data from different compatible layouts.
An HDF5 datatype describes one specific layout of bits, a dataset has a single datatype which applies to every data element. When a dataset is created, the storage datatype is defined, the datatype cannot be changed.
When data is transferred (e.g., a read or write), each end point of the transfer has a datatype, which describes the correct storage for the elements. The source and destination may have different (but compatible) layouts, in which case the data elements are automatically transformed during the transfer.
HDF5 datatypes describe commonly used binary formats for numbers (integers and floating point) and characters (ASCII). A given computing architecture and programming language supports certain number and character representations. For example, a computer may support 8-, 16-, 32-, and 64-bit signed integers, stored in memory in little-endian byte order. These would presumably correspond to the C programming language types 'char', 'short', 'int', and 'long'.
When reading and writing from memory, the HDF5 library must know the appropriate datatype that describes the architecture specific layout. The HDF5 library provides the platform independent 'NATIVE' types, which are mapped to an appropriate datatype for each platform. So the type 'H5T_NATIVE_INT' is an alias for the appropriate descriptor for each platform.
Data in memory has a datatype
In addition to numbers and characters, an HDF5 datatype can describe more abstract classes of types, including date-times, enumerations, strings, bit strings, and references (pointers to objects in the HDF5 file). HDF5 supports several classes of composite datatypes, which are compose one or more other datatypes. In addition to the standard predefined datatypes, users can define new datatypes within the datatype classes.
The HDF5 datatype model is very general and flexible
The HDF5 Library implements an object-oriented model of datatypes. HDF5 datatypes are organized as a logical set of base types, or datatype classes. Each datatype class defines a format for representing logical values as a sequence of bits. For example the H5T_CLASS_INT is a format for representing twos complement integers of various sizes.
A datatype class is defined as a set of one or more datatype properties. A datatype property is a property of the bit string. The datatype properties are defined by the logical model of the datatype class. For example, the integer class (twos complement integers) has properties such as "signed or unsigned", "length", and "byte-order". The float class (IEEE floating point numbers) has these properties, plus "exponent bits", "exponent sign", etc.
A datatype is derived from one datatype class: a given datatype has a specific value for the datatype properties defined by the class. For example, for 32-bit signed integers, stored big-endian, the HDF5 datatype is a sub-type of integer, with the properties set to: signed=1, size=4 (bytes), byte-order=BE.
The HDF5 datatype API provides methods to create datatypes of different datatype classes, to set the datatype properties of a new datatype, and to discover the datatype properties of an existing datatype.
The datatype for a dataset is stored in the HDF5 file as part of the metadata for the dataset. A datatype can be shared by more than one dataset in the file. A datatype can optionally be stored as a named object in the file.
When transferring data (e.g., a read or write), the data elements of the source and destination storage must have compatible types. As a general rule, data elements with the same datatype class are compatible, while elements from different datatype classes are not compatible. When transferring data of one datatype to another compatible datatype, the HDF5 Library uses the datatype properties of the source and destination to automatically transform each data element. For example, when reading from data stored as 32-bit, signed integers, big-endian, into 32-bit signed integers, little-endian, the HDF5 Library will automatically swap the bytes.
Thus, data transfer operations (H5Dread, H5Dwrite, H5Aread, H5Awrite) require a datatype for both the source and the destination.
|
Figure 2 |
The HDF5 Library defines a set of predefined datatypes, corresponding to commonly used storage formats, such as twos complement integers, IEEE Floating point numbers, etc., 4- and 8-byte sizes, big endian and little endian byte orders. In addition, a user can derive types with custom values for the properties. For example, a user program may create a datatype to describe a 6-bit integer, or a 600-bit floating point number.
In addition to atomic datatypes, the HDF5 Library supports composite datatypes. A composite datatype is an aggregation of one or more datatypes. Each class of composite datatypes has properties that describe the organization of the composite datatype (Figure 3). Composite datatypes include:
|
Figure 3 |
Figure 4 shows the HDF5 datatype classes. Each class is defined to have a set of properties which describe layout of the data element and the interpretation of the bits. Table 1 lists the properties for the datatype classes.
|
Figure 4 |
Table 1. Datatype Classes and their properties. |
|||
Class |
Description |
Properties |
Notes |
Integer |
Twos complement integers |
Size (bytes), precision (bits), offset (bits), pad, byte order, signed/unsigned |
|
Float |
Floating Point numbers |
Size (bytes), precision (bits), offset (bits), pad, byte order, sign position, exponent position, exponent size (bits), exponent sign, exponent bias, mantissa position, mantissa (size) bits, mantissa sign, mantissa normalization, internal padding |
See IEEE 754 for a definition of these properties. These properties describe non-IEEE 754 floating point formats as well. |
Character |
Array of 1-byte character encoding |
Size (characters), Character set, byte order, pad/no pad, pad character |
Currently, only ASCII is supported. |
Date and Time |
Date/time string |
Size (bytes), precision (bits), offset (bits), pad, byte order, |
ISO-8601 Date/time string |
Bitfield |
String of bits |
Size (bytes), precision (bits), offset (bits), pad, byte order |
When stored, are packed into bytes |
Opaque |
Uninterpreted data |
Size (bytes), precision (bits), offset (bits), pad, byte order, tag |
A sequence of bytes, stored and retrieved as a block. The ‘tag’ is a string that can be used to label the value. |
Enumeration |
A list of discrete values, with symbolic names in the form of strings. |
Number of elements, element names, element values |
Enumeration is a list of pairs, (name, value). The name is a string, the value is an unsigned integer. |
Reference |
Reference to object or region within the HDF5 file |
See the Reference API, H5R |
|
Array |
Array (1-4 dimensions) of data elements |
Number of dimensions, dimension sizes, base datatype |
The array is accessed atomically: no selection or subsetting. |
Variable length |
A variable length 1-dimensional array of data data elements |
Current size, base type |
|
Compound |
A Datatype composed of a sequence of Datatypes |
Number of members, member names, member types, member offset, member class, member size, byte order |
|
The HDF5 library predefines a modest number of commonly used datatypes.
These types have standard symbolic names of the form
H5T_arch_base
where arch is an architecture
name and base is a programming type name (Table 2). New types can
be derived from the predefined types by copying the predefined type (see
H5Tcopy()
) and then modifying the result.
The base name of most types consists of a letter to indicate the class (Table 3), a precision in bits, and an indication of the byte order (Table 4).
Table 5 shows examples of predefined datatypes.
The full list can be found in the "HDF5 Predefined Datatypes" section
of the HDF5 Reference Manual.
Table 2 |
|
Architecture Name |
Description |
|
IEEE-754 standard floating point types in various byte orders. |
|
This is an architecture that contains semi-standard datatypes like signed two's complement integers, unsigned integers, and bitfields in various byte orders. |
|
Types which are specific to Unix operating systems
are defined in this architecture. The only type currently
defined is the
Unix date and time types ( |
|
Types which are specific to the C
or Fortran
programming languages are defined in these architectures. For instance,
|
|
This architecture contains C-like
datatypes for
the machine on which the library was compiled. The types were actually
defined by running the |
|
Cray architectures. These are word-addressable, big-endian systems with non-IEEE floating point. |
|
All Intel and compatible CPU's including 80286, 80386, 80486, Pentium, Pentium-Pro, and Pentium-II. These are little-endian systems with IEEE floating-point. |
|
All MIPS CPU's commonly used in SGI systems. These are big-endian systems with IEEE floating-point. |
|
All DEC Alpha CPU's, little-endian systems with IEEE floating-point. |
Table 3 |
|
|
Bitfield |
D |
Date and time |
F |
Floating point |
I |
Signed integer |
R |
References |
S |
Character string |
U |
Unsigned integer |
Table 4 |
|
BE |
Big endian |
LE |
Little endian |
VX |
Vax order |
Table 5 |
|
Example |
Description |
|
Eight-byte, little-endian, IEEE floating-point |
|
Four-byte, big-endian, IEEE floating point |
|
Four-byte, little-endian, signed two's complement integer |
|
Two-byte, big-endian, unsigned integer |
|
Four-byte, little-endian, time_t |
|
One-byte, null-terminated string of eight-bit characters |
|
Eight-byte bit field on an Intel CPU |
|
Eight-byte Cray floating point |
|
Reference to an entire object in a file |
The HDF5 Library predefines a set of NATIVE
datatypes which
are similar to C type names. The native types are set to be an alias for the
appropriate HDF5 datatype for each platform. For example,
H5T_NATIVE_INT
corresponds to a C int type. On an Intel based PC, this type is the same as
H5T_STD_32LE, while on a MIPS system this would be equivalent to
H5T_STD_32BE
. Table 6 shows examples of NATIVE types and corresponding
C types for a common 32-bit workstation.
Table 6 |
|
Example |
Corresponding C Type |
|
char |
|
signed char |
|
unsigned char |
|
short |
|
unsigned short |
|
int |
|
unsigned |
|
long |
|
unsigned long |
|
long long |
|
unsigned long long |
|
float |
|
double |
|
long double |
|
hsize_t |
|
hssize_t |
|
herr_t |
|
hbool_t |
The HDF5 Library manages datatypes as objects. The HDF5 datatype API
manipulates the datatype objects through C function calls. New datatypes
can be created from scratch or copied from existing datatypes. When a
datatype is no longer needed its resources should be released by calling
H5Tclose()
.
The datatype object is used in several roles in the HDF5 model and library. Essentially, a datatype is used whenever the format of data elements is needed. There are four major uses of datatypes in the HDF5 library: at dataset creation, during data transfers, when discovering the contents of a file, and for specifying user defined data types (Table 7).
Table 7 |
|
Use |
Description |
Dataset creation |
The datatype of the data elements must be declared when the dataset is created. |
Data transfer |
The datatype (format) of the data elements must be defined for both the source and destination. |
Discovery |
The datatype of a dataset can be interrogated to retrieve a complete description of the storage layout. |
Creating User defined Datatypes |
Users can define their own datatypes by creating datatype objects and setting its properties. |
All the data elements of a dataset have the same datatype. When a dataset
is created (H5Tcreate
), the datatype for the data elements must
be specified. The datatype of a dataset can never be changed. Figure 5 shows
the use of a datatype to create a dataset called "/dset". In this example,
the dataset will be stored as 32-bit signed integers, in big endian
order.
|
Figure 5 |
Probably the most common use of datatypes is to write or read data from a dataset or attribute. In these operations, each data element is transferred from the source to the destination (possibly rearranging the order of the elements). Since the source and destination do not need to be identical (i.e., one is disk and the other is memory) the transfer requires both the format of the source element and the destination element. Therefore, data transfers use two datatype objects, for the source and destination.
When data is written, the source is memory and the destination is disk (file). The memory datatype describes the format of the data element in the machine memory, and the file datatype describes the desired format of the data element on disk. Similarly, when reading, the source datatype describes the format of the data element on disk, and the destination datatype describes the format in memory.
In the most common cases, the file datatype is the datatype specified when the dataset was created, and the memory datatype should be the appropriate NATIVE type.
Figures 5 and 6, respectively, show examples of writing data to and reading data from a dataset. The data in memory is declared C type 'int', the datatype H5T_NATIVE_INT corresponds to this type. The datatype of the dataset should be of datatype class INTEGER.
|
Figure 6 |
|
Figure 7 |
The HDF5 Library enables a program to determine the datatype class and properties for any data type. In order to discover the storage format of data in a dataset, the datatype is obtained, and the properties determined by queries to the datatype object. Figure 8 shows an example of code that analyzes the datatype for an integer, and prints out a description of its storage properties (byte Order, signed, size.)
|
Figure 8 |
Most programs will primarily use the predefined datatypes described above, possibly in composite datatypes such as compound or array datatypes. However, the HDF5 datatype model is extremely general; a user program can define a great variety of atomic datatypes (storage layouts). In particular, the datatype properties can define signed and unsigned integers of any size and byte order, and floating point numbers with different formats, size, and byte order. The HDF5 datatype API provides methods to set these properties.
User defined types can be used to define the layout of data in
memory, e.g.,
to match some platform specific number format or application
defined bit-field.
The user defined type can also describe data in the file, e.g., some
application-defined format. The user defined types can be translated to and
from standard types of the same class, as described above.
3. Datatype (H5T) Function Summaries
C Function F90 Function |
Purpose |
H5Tcreate
|
Creates a new datatype. |
H5Topen
|
Opens a named datatype. |
H5Tcommit
|
Commits a transient datatype to a file, creating a new named datatype. |
H5Tcommitted
|
Determines whether a datatype is a named type or a transient type. |
H5Tcopy
|
Copies an existing datatype. |
H5Tequal
|
Determines whether two datatype identifiers refer to the same datatype. |
H5Tlock
|
Locks a datatype. |
H5Tget_class
|
Returns the datatype class identifier. |
H5Tget_size
|
Returns the size of a datatype. |
H5Tget_super
|
Returns the base datatype from which a datatype is derived. |
H5Tget_native_type
|
Returns the native datatype of a specified datatype. |
H5Tdetect_class
|
Determines whether a datatype is of the given datatype class. |
H5Tclose
|
Releases a datatype. |
C Function F90 Function |
Purpose |
H5Tconvert
|
Converts data from between specified datatypes. |
H5Tfind
|
Finds a conversion function. |
H5Tset_overflow
|
Sets the overflow handler to a specified function. |
H5Tget_overflow
|
Returns a pointer to the current global overflow function. |
H5Tregister
|
Registers a conversion function. |
H5Tunregister
|
Removes a conversion function from all conversion paths. |
C Function F90 Function |
Purpose |
H5Tset_size
|
Sets the total size for an atomic datatype. |
H5Tget_order
|
Returns the byte order of an atomic datatype. |
H5Tset_order
|
Sets the byte ordering of an atomic datatype. |
H5Tget_precision
|
Returns the precision of an atomic datatype. |
H5Tset_precision
|
Sets the precision of an atomic datatype. |
H5Tget_offset
|
Retrieves the bit offset of the first significant bit. |
H5Tset_offset
|
Sets the bit offset of the first significant bit. |
H5Tget_pad
|
Retrieves the padding type of the least and most-significant bit padding. |
H5Tset_pad
|
Sets the least and most-significant bits padding types. |
H5Tget_sign
|
Retrieves the sign type for an integer type. |
H5Tset_sign
|
Sets the sign property for an integer type. |
H5Tget_fields
|
Retrieves floating point datatype bit field information. |
H5Tset_fields
|
Sets locations and sizes of floating point bit fields. |
H5Tget_ebias
|
Retrieves the exponent bias of a floating-point type. |
H5Tset_ebias
|
Sets the exponent bias of a floating-point type. |
H5Tget_norm
|
Retrieves mantissa normalization of a floating-point datatype. |
H5Tset_norm
|
Sets the mantissa normalization of a floating-point datatype. |
H5Tget_inpad
|
Retrieves the internal padding type for unused bits in floating-point datatypes. |
H5Tset_inpad
|
Fills unused internal floating point bits. |
H5Tget_cset
|
Retrieves the character set type of a string datatype. |
H5Tset_cset
|
Sets character set to be used. |
H5Tget_strpad
|
Retrieves the storage mechanism for a string datatype. |
H5Tset_strpad
|
Defines the storage mechanism for character strings. |
C Function F90 Function |
Purpose |
H5Tenum_create
|
Creates a new enumeration datatype. |
H5Tenum_insert
|
Inserts a new enumeration datatype member. |
H5Tenum_nameof
|
Returns the symbol name corresponding to a specified member of an enumeration datatype. |
H5Tenum_valueof
|
Returns the value corresponding to a specified member of an enumeration datatype. |
H5Tget_member_value
|
Returns the value of an enumeration datatype member. |
H5Tget_nmembers
|
Retrieves the number of elements in a compound or enumeration datatype. |
H5Tget_member_name
|
Retrieves the name of a compound or enumeration datatype member. |
H5Tget_member_index
|
Retrieves the index of a compound or enumeration datatype member. |
C Function F90 Function |
Purpose |
H5Tget_nmembers
|
Retrieves the number of elements in a compound or enumeration datatype. |
H5Tget_member_class
|
Returns datatype class of compound datatype member. |
H5Tget_member_name
|
Retrieves the name of a compound or enumeration datatype member. |
H5Tget_member_index
|
Retrieves the index of a compound or enumeration datatype member. |
H5Tget_member_offset
|
Retrieves the offset of a field of a compound datatype. |
H5Tget_member_type
|
Returns the datatype of the specified member. |
H5Tinsert
|
Adds a new member to a compound datatype. |
H5Tpack
|
Recursively removes padding from within a compound datatype. |
C Function F90 Function |
Purpose |
H5Tarray_create
|
Creates an array datatype object. |
H5Tget_array_ndims
|
Returns the rank of an array datatype. |
H5Tget_array_dims
|
Returns sizes of array dimensions and dimension permutations. |
C Function F90 Function |
Purpose |
H5Tvlen_create
|
Creates a new variable-length datatype. |
H5Tis_variable_str
|
Determines whether datatype is a variable-length string. |
C Function F90 Function |
Purpose |
H5Tset_tag
|
Tags an opaque datatype. |
H5Tget_tag
|
Gets the tag associated with an opaque datatype. |
The HDF5 Library implements an object-oriented model of datatypes. HDF5 datatypes are organized as a logical set of base types, or datatype classes. The HDF5 Library manages datatypes as objects. The HDF5 datatype API manipulates the datatype objects through C function calls. Figure 9 shows the abstract view of the datatype object. Table 8 shows the methods (C functions) that operate on datatype object as a whole. New datatypes can be created from scratch or copied from existing datatypes.
| |||
Figure 9. The datatype object |
Table 8. General operations on datatype objects |
|
API function |
Description |
|
Create a new datatype object of datatype class class. The following datatype classes are supported with this function:
H5Tcopy() . |
|
Obtain a modifiable transient datatype which is a copy of type. If type is a dataset identifier then the type returned is a modifiable transient copy of the datatype of the specified dataset. |
|
Open a named datatype. The named datatype returned by this function is read-only. |
|
Determines if two types are equal. |
|
Releases resources associated with a datatype obtained from H5Tcopy, H5Topen, or H5Tcreate. It is illegal to close an immutable transient datatype (e.g., predefined types). |
|
Commit a transient datatype (not immutable) a file to become a named datatype. Named datatypes can be shared. |
|
Test whether the datatype is transient or commited (named). |
|
Make a transient datatype immutable (read-only and not closable). Predefined types are locked. |
In order to use a datatype, the object must be created
(H5Tcreate
),
or a reference obtained by cloning from an existing type
(H5Tcopy
),
or opened (H5Topen
). In addition, a reference to the datatype of
a dataset or attribute can be obtained with
H5Dget_type
or H5Aget_type
.
For composite datatypes a reference to the datatype for members or base types
can be obtained (H5Tget_membertype
, H5Tget_super
).
When the datatype object is no longer needed, the reference is discarded with
H5Tclose
.
Two datatype objects can be tested to see if they are the same
with H5Tequal
.
This function returns true if the two datatype references refer to the same
datatype object. However, if two datatype objects define equivalent datatypes
(the same datatype class and datatype properties), they will not be
considered
'equal'.
A datatype can be written to the file as a first class object
(H5Tcommit
).
Named datatypes can be used in the same way as any other dataype.
Named datatypes
are explained below.
Any HDF5 datatype object can be queried to discover all of its datatype properties. For each datatype class, there are a set of API functions to retrieve the datatype properties for this class.
Table 9 lists the functions to discover the properties of atomic datatypes. Table 10 lists the queries relevant to specific numeric types. Table 11 gives the properties for atomic string datatype, and Table 12 gives the property of the opaque datatype.
Table 9 |
|
Functions to Discover Properties of Atomic DataTypes |
Description |
|
The datatype class:
|
|
The total size of the element in bytes, including padding which may appear on either side of the actual value. |
|
The byte order describes how the bytes of
the datatype are laid out in memory. If the lowest memory
address contains
the least significant byte of the datum then it is said to be
little-endian
or |
|
The |
|
The |
|
Padding is the bits of a data
element which
are not significant as defined by the |
Table 10 |
|
Properties of Atomic Numeric Types |
Description |
|
(INTEGER) Integer data can
be signed two's complement ( |
|
(FLOAT) A floating-point data element has bit fields which are the exponent and mantissa as well as a mantissa sign bit. These properties define the location (bit position of least significant bit of the field) and size (in bits) of each field. The sign bit is always of length one and none of the fields are allowed to overlap. |
|
(FLOAT) The
exponent is stored
as a non-negative value which is |
|
(FLOAT) This property describes the normalization method of the mantissa.
|
|
(FLOAT) If any internal
bits (that is, bits between the sign bit, the mantissa field, and the
exponent field but within the precision field) are unused,
then they will
be filled according to the value of this property. The padding can be:
H5T_PAD_NONE, |
Table 11 |
|
Properties of Atomic String Datatypes |
Description |
|
The only character set currently supported
is |
|
The string datatype has a fixed length,
but the String may be shorter than the length. This property
defines the
storage mechanism for the left over bytes. The options are:
|
Table 12 |
|
Properties of Opaque Atomic Datatypes |
Description |
char *H5Tget_tag(hid_t type_id) |
A user defined string. |
The composite datatype classes can also be analyzed to discover their datatype properties and the datatypes that are members or base types of the composite datatype. The member or base type can, in turn, be analyzed. Table 13 lists the functions that can access the datatype properties of the different composite datatypes.
Table 13 |
|
Properties of Composite Datatype |
Description |
|
(COMPOUND) The number of fields in the compound datatype. |
|
(COMPOUND) The
datatype class
of compound datatype member |
|
(COMPOUND) The
name of field
|
|
(COMPOUND) The byte offset of the beginning of a field within a compound datatype. |
|
(COMPOUND) The datatype of the specified member. |
|
(ARRAY) The number of dimensions (rank) of the array datatype object. |
|
(ARRAY) The sizes of the dimensions and the dimension permutations of the array datatype object. |
|
(ARRAY, VL, ENUM) The base datatype from which the datatype type is derived. |
|
(ENUM) The symbol name that corresponds to the specified value of the enumeration datatype |
|
(ENUM) The value that corresponds to the specified name of the enumeration datatype |
|
(ENUM) The value of the
enumeration datatype member |
The HDF5 Library enables user programs to create and modify datatypes. The essential steps are:
To create a user defined atomic datatype, the procedure is to
clone a predefined
datatype of the appropriate datatype class (H5Tcopy
). Then set
the datatype properties appropriate to the datatype class. For example, Table
14 shows how to create a datatype to describe a 1024-bit unsigned
integer.
Table 14 |
|
hid_t new_type = H5Tcopy (H5T_NATIVE_INT); H5Tset_precision(new_type, 1024); H5Tset_sign(new_type, H5T_SGN_NONE); |
Composite datatypes are created with a specific API call for each datatype class. Table 15 shows the creation method for each datatype class. A newly created datatype cannot be used until the datatype properties are set. For example, a newly created compound datatype has no members and cannot be used.
Table 15 |
|
Datatype Class |
Function to Create |
COMPOUND |
|
OPAQUE |
|
COMPOUND |
|
ENUM |
|
ARRAY |
|
VL |
|
Once the datatype is created and the datatype properties set, the datatype object can be used.
Predefined datatypes are defined by the library during
initialization using
the same mechanisms as described here. Each predefined datatype is
locked (H5Tlock
),
so that it cannot be changed or destroyed. User defined datatypes may also be
locked using H5Tlock
.
Table 16 summarizes the API methods that set properties of atomic types. Table 17 shows properties specific to numeric types, Table 18 shows properties specific to the string datatype class. Note that offset, pad, etc. don't apply to strings. Table 19 shows the specific property of the OPAQUE datatype class.
Table 16 |
|
Functions to set Properties of Atomic DataTypes |
Description |
|
Set the total size of the element in bytes, including padding which may appear on either side of the actual value. If this property is reset to a smaller value which would cause the significant part of the data to extend beyond the edge of the datatype then the offset property is decremented a bit at a time. If the offset reaches zero and the significant part of the data still extends beyond the edge of the datatype then the precision property is decremented a bit at a time. Decreasing the size of a datatype may fail if the H5T_FLOAT bit fields would extend beyond the significant part of the type. |
|
Set the byte order to
little-endian ( |
|
Set the number of significant
bits of a datatype.
The |
|
Set the bit location of the least
significant
bit of a bit field whose length is |
|
Set the padding to zeros
( |
Table 17 |
|
Properties of Numeric Types |
Description |
|
(INTEGER) Integer data can
be signed two's complement ( |
|
(FLOAT) Set the properties define the location (bit position of least significant bit of the field) and size (in bits) of each field. The sign bit is always of length one and none of the fields are allowed to overlap. |
|
(FLOAT) The
exponent is stored
as a non-negative value which is |
|
(FLOAT) This property describes the normalization method of the mantissa.
|
|
(FLOAT) If any internal
bits (that is, bits between the sign bit, the mantissa field, and the
exponent field but within the precision field) are unused,
then they will
be filled according to the value of this property. The padding can be:
H5T_PAD_NONE, |
Table 18 |
|
Properties of Atomic String Datatypes |
Description |
|
Set the length of the string, in bytes. The
precision is automatically set to 8* |
|
The precision must be a multiple of 8. |
|
The only character set currently supported
is |
|
The string datatype has a fixed length, but the string may be shorter than the length. This property defines the storage mechanism for the left over bytes. The method used to store character strings differs with the programming language:
Valid string padding values, as passed in the parameter strpad, are as follows:
|
Table 19 |
|
Properties of Opaque Atomic Datatypes |
Description |
|
Tags the opaque datatype type_id with an ASCII identifier tag. |
Figure 10 shows an example of how to create a 128-bit, little-endian signed integer type one could use the following (increasing the precision of a type automatically increases the total size). Note that the proper procedure is to begin from a type of the intended datatype class, in this case, a NATIVE INT.
|
Figure 10 |
Figure 11 shows the storage layout as the type is defined. The H5Tcopy creates a datatype that is the same as H5T_NATIVE_INT. In this example, suppose this is a 32-bit big endian number (Figure 11a). The precision is set to 128 bits, which automatically extends the size to 8 bytes (Figure 11b). Finally, the byte order is set to little-endian (Figure 11c).
| |||||||||||||||||||||||||||||||||||||||||||||||||
Figure 11 |
The significant bits of a data element can be offset from the beginning of
the memory for that element by an amount of padding. The offset
property specifies the number of bits of padding that appear to the
"right of"
the value. Table 20 and Figure 12 shows how a 32-bit unsigned
integer with 16-bits
of precision having the value 0x1122
will be laid out
in memory.
Table 20 |
||||
Byte Position |
Big-Endian |
Big-Endian |
Little-Endian |
Little-Endian |
0: |
[pad] |
[0x11] |
[0x22] |
[pad] |
1: |
[pad] |
[0x22] |
[0x11] |
[pad] |
2: |
[0x11] |
[pad] |
[pad] |
[0x22] |
3: |
[0x22] |
[pad] |
[pad] |
[0x11] |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Figure 12 |
If the offset is incremented then the total size is incremented also if necessary to prevent significant bits of the value from hanging over the edge of the datatype.
The bits of the entire data are numbered beginning at zero at
the least significant
bit of the least significant byte (the byte at the lowest memory address for
a little-endian type or the byte at the highest address for a
big-endian type).
The offset
property defines the bit location of the
least signficant
bit of a bit field whose length is precision
. If the offset is
increased so the significant bits "hang over" the edge of the datum, then the
size
property is automatically incremented.
To illustrate the properties of the integer datatype class, Figure 13 shows how to create a user defined datatype that describes a 24-bit signed integer that starts on the third bit of a 32-bit word. The datatype is specialized from a 32-bit integer, the precision is set to 24 bits, and the offset is set to 3.
|
Figure 13 |
Figure 14 shows the storage layout for a data element. Note that
the unused
bits in the offset will be set to zero and the unused bits at the end will be
set to one, as specified in the H5Tset_pad
call.
|
||||||||||||||||
Figure 14. A User defined integer Datatype: range -1,048,583 to 1,048,584 |
To illustrate a user defined floating point number, Figure 13 shows how to create a 24-bit floating point number, that starts 5 bits into a 4 byte word. The floating point number is defined to have a mantissa of 19 bits (bits 5-23), and exponent of 3 bits (25-27) and the sign bit is bit 28. (Note that this is an illustration of what can be done, not necessarily a floating point format that a user would require.)
|
Figure 15 |
|
||||||||||||||||
Figure 16. A User defined Floating Point Datatype. |
Figure 16 shows the storage layout of a data element for this datatype. Note that there is an unused bit (24) between the mantissa and the exponent. This bit is filled with the inpad value, in this case 0.
The sign bit is always of length one and none of the fields are allowed to overlap. When expanding a floating-point type one should set the precision first; when decreasing the size one should set the field positions and sizes first.
All composite datatypes must be user defined, there are no predefined composite datatypes.
The subsections below describe how compound datatypes are created and how to write and read data of compound datatype.
Compound datatypes are conceptually similar to a C struct or Fortran 95 derived types. The compound datatype defines a contiguous sequence of bytes, which are formatted using one up to 2^16 datatypes (members). A compound datatype may have any number of members, in any order, and the members may have any datatype, including compound. Thus, complex nested compound datatypes can be created. The total size of the compound datatype is greater than or equal to the sum of the size of its members, up to a maximum of 2^32 bytes. HDF5 does not support datatypes with distinguished records or the equivalent of C unions or Fortran 95 EQUIVALENCE statement.
Usually a C struct or Fortran derived type will be defined to
hold a data
point in memory, and the offsets of the members in memory will be the
offsets of the struct members from the beginning of an instance
of the struct. The HDF5 C libary provides a macro
HOFFSET(s,m)
to calculate the member's ofset. The HDF5 Fortran applications
have to calculate offsets
by using sizes of members datatypes and by taking in
consideration the order of
members in the Fortran derived type.
HOFFSET(s,m)
offsetof(s,m)
stddef.h
does
exactly the same thing as the HOFFSET()
macro.
Note for Fortran users: Offsets of Fortran structure members correspond to the offsets within a packed datatype (see explanation below) stored in an HDF5 file.
Each member of a compound datatype must have a descriptive name which is the key used to uniquely identify the member within the compound datatype. A member name in an HDF5 datatype does not necessarily have to be the same as the name of the member in the C struct of Fortran derived type, although this is often the case. Nor does one need to define all members of the C struct of Fortran derived type in the HDF5 compound datatype (or vice versa).
Unlike atomic datatypes which are derived from other atomic datatypes, compound datatypes are created from scratch. First, one creates an empty compound datatype and specifies its total size. Then members are added to the compound datatype in any order. Each member type is inserted at a designated offset. Each member has a name which is the key used to uniquely identify the member within the compound datatype.
Figure 17a shows an example of creating an HDF5 C compound
datatype to describe
a complex number, which is a structure with two components, "real"
and "imagenery",
each double. An equivalent C struct
is whose type is defined by
the complex_t
struct
, is shown.
|
Figure 17a |
Figure 17b shows an example of creating an HDF5 Fortran compound
datatype to describe
a complex number, which is a Fortran derived type with two
components, "real" and "imagenary",
each DOUBLE PRECISION. An equivalent Fortran TYPE
is
whose type is defined by
the TYPE
complex_t
, is shown.
|
Figure 17b |
Important Note: The compound datatype is created with
a size sufficient to hold
all its members. In the C example above, the size of the C
struct
and
the HOFFSET macro are used as a convenient
mechanism to determine the appropriate size and offset.
Alternatively, the size and offset could
be manually determined, e.g., the size can be set to 16 with "real"
at offset 0 and "imaginary" at offset
8. However, different platforms and compilers have different sizes
for "double",
and may have alignment restrictions which require additional padding within
the structure. It is much more portable to use the HOFFSET macro,
which assures
that the values will be correct for any platform.
Figure 18 shows how the compound datatype would be laid out, assuming that NATIVE_DOUBLE are 64-bit numbers, and there are no alignment requirements. The total size of the compound datatype will be 16 bytes, the "real" component will start at byte 0, and "imaginary" will start at byte 8.
|
|||||||||||||||||||||||||||||||||||||||
Figure 18 |
The members of a compound datatype may be any HDF5 datatype, including compound, array, and VL. Figures 19 and 20 show an example which creates a compound datatype composed of two complex values, each of which is a compound datatype as in Figure 18 above.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Figure 19 |
|
Figure 20 |
Note that a similar result could be accomplished by creating a compound datatype and inserting four fields (Figure 21). This results in the same layout as above (Figure 19). The difference would be how the fields are addressed. In the first case, the real part of 'y' is called 'y.re'; in the second case it is 'y-re'.
|
Figure 21 |
The members of a compound datatype do not always fill all the bytes. The HOFFSET macro assures that the members will be laid out according to the requirements of the platform and language. Figure 22 shows an example of a C struct which requires extra bytes of padding on many platforms. The second element, 'b', is a 1-byte character, followed by an 8 byte double, 'c'. On many systems, the 8-byte value must be stored on a 4- or 8-byte boundary, requiring the struct to be larger than the sum of the size of its elements.
In Figure 22 , the sizeof
and HOFFSET
macro is used
to assure that the members are inserted at the correct offset to
match the memory
conventions of the platform. Figure 23 shows how this data element would be
stored in memory, assuming the double must start on a 4-byte boundary. Notice
the extra bytes between 'b' and 'c'.
|
Figure 22 |
|
Figure 23 |
However, data stored on disk does not require alignment, so
unaligned versions
of compound data structures can be created to improve space
efficiency on disk.
These unaligned compound datatypes can be created by computing
offsets by hand
to eliminate inter-member padding, or the members can be packed by
calling H5Tpack
(which modifies a datatype directly, so it is usually preceded by a call to
H5Tcopy
):
Figure 24a shows how to create a disk version of the compound datatype from Figure 22 above in order to store data on disk in as compact a form as possible. Figure 25 shows the layout of the bytes in the packed data structure. Packed compound datatypes should generally not be used to describe memory as they may violate alignment constraints for the architecture being used. Note also that using a packed datatype for disk storage may involve a higher data conversion cost.
|
Figure 24a |
Figure 24b shows the sequence of Fortran calls to create a packed compound datatype. An HDF5 Fortran compound datatype never describes a compound datatype in memory and compound data is ALWAYS written by fields as described in the next section. Therefore packing is not needed unless the the offset of each consecutive member is not equal to the sum of the sizes of the previous members.
|
Figure 24b |
Creating datasets with compound datatypes is similar to creating datasets with any other HDF5 datatypes. But writing and reading may be different since datsets that have compound datatypes can be written or read by a field (member) or subsets of fields (members). The compound datatype is the only compoiste datatype that supports "sub-setting" by the elements the datatype is built from.
Figure 25a shows C example of creating and writing a dataset with a compound datatype.
|
Figure 25a |
Figure 25b shows the content of the file written on the liitle-endian machine.
|
Figure 25b |
It is not necessary to write the whole data at once. Datasets with
compound datatypes
can be written by field. In order to do this one has to remember to
set transfer property of the
dataset using H5Pset_preserve
call and to define memory
datatype that corresponds
to a field. Figure 25c shows how field b is written to the dataset.
|
Figure 25c |
Figure 25d shows the content of the file written on the little-endian machine. Only float and double fileds are written. Default fill value is used to initialize unwritten integer filed.
|
Figure 25d |
Figure 25e contains a Fortran example that creates and writes a dataset with a compound datatype. As this example illustrates, writing and reading compound datatypes in Fortran is always done by fields. The content of the written file is the same as shown in the Figure 25b.
|
Figure 25e |
Many scientific datasets have multiple measurements for each point in a space. There are several natural ways to represent this data, depending on the variables and how they are used in computation (Table 21).
Table 21 |
||
Storage Strategy |
Stored as |
Remarks |
Mulitple planes |
Several datasets with identical dataspaces |
This is optimal when variables are accessed individually, or when often uses only selected variables. |
Additional dimension |
One dataset, the last "dimension" is a vector of variables |
This can give good performance, although selecting only a few variables may be slow. This may not reflect the science. |
Record with multiple values |
One dataset with compound datatype |
This enables the variables to be read all together or selected. Also handles "vectors" of heterogenous data. |
Vector or Tensor value |
One dataset, each data element is a small array of values. |
This uses the same amount of space as the previous two, and may represent the science model better. |
|
|
Figure 26 |
The HDF5 H5T_ARRAY datatype defines the data element to be a homogeneous, multi-dimensional array, as in Figure 26d, above. The elements of the array can be any HDF5 datatype (including compound and array), the size of the datatype is the total size of the array. A dataset of array datatype cannot be subdivided for I/O within the data element, the entire array of the data element must be transferred. If the data elements need to be accessed separated, e.g., by plane, then the array datatype should not be used. Table 22 gives advantages and disadvantages of the storage methods.
Table 22 |
||
Method |
Advantages |
Disadvantages |
a) Multiple Datasets |
|
|
b) N+1 Dimension |
|
|
c) Compound Datatype |
|
|
d) Array |
|
|
An array datatype may be multi-dimensional, with 1 to H5S_MAX_RANK (the maximum rank of a dataset is currently 32). The dimensions can be any size greater than 0, but unlimited dimensions are not supported (although the datatype can be a variable length datatype).
|
An array datatype is create with the
H5Tarray_create
call, which
specifies the number of dimensions, the size of each dimension, and the base
type of the array. The array datatype can then be used in any way
that any datatype
object is used. Figure 27 shows the creation of a datatype that is
a two-dimensional
array of native integers, which is then used to create a dataset. Note that
the dataset can a dataspace that is any number and size of dimensions. Figure
28 shows the layout in memory, assuming that the native integers are 4 bytes.
Each data element has 6 elements, for a total of 24 bytes.
|
Figure 27 |
|
Figure 28 |
A variable-length (VL) datatype is a one-dimensional sequence of a datatype which are not fixed in length from one dataset location to another, i.e., each data element may have a different number of members. Variable-length datatypes cannot be divided, the entire data element must be transferred.
VL datatypes are useful to the scientific community in many different ways, possibly including:
A VL datatype is created by calling H5Tvlen_create
,
which specifies
the base datatype. Figure 29 shows an example of code that creates
a VL datatype
of unsigned integers. Each data element is a one-dimensional array of zero or
more members, which must be stored in a structure, hvl_t
(Figure
30).
|
Figure 29 |
|
Figure 30 |
Figure 31 shows how the VL data is written. For each of the 10 data elements, a length and data buffer must be allocated. Figure 33 shows how the data is laid out in memory.
An analogous procedure must be used to read the data (Figure 32).
An appropriate array of vl_t
must be allocated,
and the data read. It is then traversed one data element at a time.
The H5Dvlen_free
call frees the data buffer for the buffer.
With each element possibly being of different sequence lengths for a
dataset with a VL datatype, the memory for the VL datatype
must be dynamically allocated. Currently there are two methods of
managing the
memory for VL datatypes: the standard C malloc/free memory
allocation routines
or a method of calling user-defined memory management routines to allocate or
free memory (set with H5Pset_vlen_mem_manager
). Since the memory
allocated when reading (or writing) may be complicated to release,
the H5Dvlen_reclaim
)
is provided to traverse a memory buffer and free the VL datatype information
without leaking memory.
|
Figure 31 |
|
Figure 32 |
|
Figure 33 |
The user program must carefully manage these relatively complex
data structures,
such as suggested by Figure 33. The H5Dvlen_reclaim
function performs
a standard traversal, freeing all the data. This function analyzes
the datatype
and dataspace objects, and visits each VL data element, recursing
through nested
types. By default, the system free
is called for the pointer in
each vl_t
. Obviously, this call assumes that all of this memory
was allocated with the system malloc
.
The user program may specify custom memory manager routines, one
for allocating
and one for freeing. These may be set with the
H5Pvlen_mem_manager
,
and must have the following prototypes:
typedef void *(*H5MM_allocate_t)(size_t size, void
*info)
;
typedef void (*H5MM_free_t)(void *mem, void *free_info)
;
The utility function H5Dget_vlen_buf_size
checks
the number of
bytes required to store the VL data from the dataset. This function analyzes
the datatype and dataspace object to visit all the VL data
elements, to determine
the number of bytes required to store the data for the in the
destination storage
(memory). The size
value is adjusted for data
conversion and alignment
in the destination.
Several datatype classes define special types of objects.
Text data is represented by arrays of characters, called strings. Many programming languages support different conventions for storing strings, which may be fixed or variable length, and may have different rules for padding unused storage. HDF5 can represent strings in several ways.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Figure 34 |
First, a dataset may have a dataset with datatype H5T_NATIVE_CHAR, with each character of the string as an element of the dataset. This will store an unstructured block of text data, but gives little indication of any structure in the text (Figure 34a).
A second alternative is to store the data using the datatype class H5T_STRING, with each element a fixed length (Figure 34b). In this approach, each element might be a word or a sentence, addressed by the dataspace. The dataset reserves space for the specified number of characters, although some strings may be shorter. This approach is simple and usually is fast to access, but can waste storage space if the length of the Strings varies.
A third alternative is to use a variable-length datatype (Figure 34c).
This can be done using the standard mechanisms described above (e.g., using
H5T_NATIVE_CHAR
instead of H5T_NATIVE_INT
in Figure
29 above). The program would use vl_t
structures to write and
read the data.
A fourth alternative is to use a special feature of the string
datatype class,
to set the size of the datatype to H5T_VARIABLE
(Figure 34c).
Figure 35 shows a declaration of a datatype of type H5T_C_S1
,
which is set to H5T_VARIABLE
. The HDF5 Library automatically
translates between this and the vl_t
structure. (Note: the
H5T_VARIABLE
size can only be used with string datatypes.)
|
Figure 35 |
Variable-length strings can be read into C strings (i.e., pointers to zero
terminated arrays of char
) (Figure 36).
|
Figure 36 |
In HDF5, objects (i.e. groups, datasets, and named datatypes) are usually
accessed by name. There is another way to access stored objects --
by reference.
There are two reference datatypes, object reference and region reference.
Object reference objects are created with the H5Rcreate
and
other calls (cross reference). These objects can be stored and retrieved
in a dataset as elements with reference datatype. Figure 37 shows an example
of code that creates references to four objects, and then writes the array
of object references to a dataset. Figure 38 shows a dataset of datatype
reference being read, and one of the object reference objects being
dereferenced to obtain an object pointer.
In order to store references to regions of a dataset, the datatype should be H5T_REGION_OBJ. Note that a data element must be either an object reference or a region reference: these are different types and cannot be mixed within a single array.
A reference datatype cannot be divided for I/O, an element is read or written completely.
|
Figure 37 |
|
Figure 38 |
The enum datatype implements a set of (name, value) pairs, similar to C/C++ enum. The values are currently limited to integer dataype class. Each name can be the name of only one value, and each value can have only one name. There can be up to 2^16 different names for a given enumeration.
The data elements of the ENUMERATION are stored according to the datatype, e.g., as an array of integers. Figure 39 shows an example of how to create an enumeration with five elements. The elements map symbolic names to 2-byte integers (Table 23).
|
Figure 39 |
Table 23 |
|
Name |
Value |
RED |
0 |
GREEN |
1 |
BLUE |
2 |
WHITE |
3 |
BLACK |
4 |
Figure 40 shows how an array of eight values might be stored. Conceptually, the array is an array of symbolic names [BLACK, RED, WHITE, BLUE, …] (Figure 40a). These are stored as the values, i.e., as short integers. So, the first 2 bytes are the value associated with "BLACK", which is the number 4, and so on (Figure 40b).
|
||||||||||||||||||||||||
Figure 40 |
The order that members are inserted into an enumeration type is unimportant; the important part is the associations between the symbol names and the values. Thus, two enumeration datatypes will be considered equal if and only if both types have the same symbol/value associations and both have equal underlying integer datatypes. Type equality is tested with the H5Tequal() function.
In some cases, a user may have data objects that should be stored and retrieved as blobs, with no attempt to interpret them. For example, an application might wish to store an array of encrypted certificates, which are 100 bytes long
While an arbitrary block of data may always be stored as bytes, characters, integers, or whatever, this might mislead programs about the meaning of the data. The opaque datatype defines data elements which are uninterpreted by HDF5. The opaque data may be labeled with H5Tset_tag, with a string that might be used by an application. For example, the encrypted certificates might have a tag to indicate the encryption and the certificate standard.
Some data is represented as bits, where the number of bits is not an integral byte and the bits are not necessarily interpreted as a standard type. Some examples might include readings from machine registers (e.g., switch positions), a cloud mask, or data structures with several small integers that should be store in a single byte.
This data could be stored as integers, strings, or enumerations. However, these storage methods would likely have considerable wasted space. For example, storing a cloud mask with one byte per value would use 8 times the space of a packed array of bits. Similarly, the status of an inst
The HDF5 bitfield dataype class defines a data element that is a contiguous sequence of bits, which are stored on disk in a packed array. The programming model is the same as for unsigned integers: the dataype object is created by copying a predefined datatype, and then the precision, offset, and padding are set.
The HDF5 time datatype defines storage layout for various date and time standards. Currently, only Unix "time" and "timeval" structs are supported. The H5T_UNIX_D32BE (LE) defines storage for 4 bytes (sufficient for the time struct), H5T_UNIX_D64BE (LE) is sufficient for timeval. The data is treated as a single opaque value.
The "fill value" for a dataset is the specification of the default value assigned to data elements that have not yet been written. In the case of a dataset with an atomic datatype, the fill value is a single value of the appropriate datatype, such as '0' or '-1.0'. In the case of a dataset with a composite datatype, the "fill value" is a single data element of the appropriate type. For example, for an array or compound datatype, the "fill value" is a single data element with values for all the component elements of the array or compound datatype.
The fill value is set (permanently) when the dataset is created.
The fill value is set in the dataset creation properties
in the H5Dcreate
call. Note that the H5Dcreate
call must also include the datatype of the dataset, and the value provided
for the fill value will be interpreted as a single element of this datatype.
Figure 41 shows example code which creates a dataset of integers with fill
value -1. Any unwritten data elements will be set to -1.
|
Figure 41 |
|
Figure 42 |
Figure 42 shows how to create a "fill value" for a compound datatype. The procedure is the same as the previous example, except the filler must be a structure with the correct fields. Each field is initialized to the desired fill value.
The fill value for a dataset can be retrieved by reading the dataset
creation properties of the dataset, and then reading the fill value with
H5Pget_fill_value
. The data will be read into memory using
the storage layout specified by the datatype. This transfer will convert
data in the same way as H5Dread
.
Figure 43 shows how to get the fill value from the dataset created in
Figure 41 above.
|
Figure 43 |
A similar procedure is followed for any datatype. Figure 45 shows how to
read the fill value created in Figure 42. Note that the program must pass an
element large enough to hold a fill value of the datatype indicated by the
argument to H5Pget_fill_value
. Also, the program must
understand
the datatype in order to interpret its components. This may be difficult to
determine without knowledge of the application that created the dataset.
|
Figure 44 |
Several composite datatype classes define collections of other datatypes, including other composite datatypes. In general, a datatype can be nested to any depth, with any combination of datatypes.
For example, a compound datatype can have members that are other compound datatypes, arrays, VL datatypes. An array can be an array of array, an array of compound, or an array of VL. And a VL datatype can be a variable length array of compound, array, or VL datatypes.
These complicated combinations of datatypes form a logical tree, with a single root datatype, and leaves which must be atomic datatypes (predefined or user-defined). Figure 45 shows an example of a logical tree describing a compound datatype constructed from different datatypes.
Recall that the datatype is a description of the layout of storage. The complicated compound datatype is constructed from component datatypes, each of which describe the layout of part of the storage. Any datatype can be used as a component of a compound datatype, with the following restrictions:
These restrictions are essentially the rules for C structures and similar record types familiar from programming languages. Multiple typing, such as a C union, is not allowed in HDF5 datatypes.
|
Figure 45 |
To construct a complicated compound datatype, each component is constructed, and then added to the enclosing datatype description. Figure 46 shows some example code to create a compound datatype with four members:
This datatype is shown as a logical tree in Figure 47, the output of the h5dump utility is shown in Figure 48.
Each datatype is created as a separate datatype object. Figure 49 shows the storage layout for the four individual datatypes. Then the dataypes are inserted into the outer datatype at an appropriate offset. Figure 50 shows the resulting storage layout. The combined record is 89 bytes long.
The Dataset is created using the combined compound datatype. The dataset is declared to be a 4 by 3 array of compound data. Each data element is an instance of the 89-byte compound datatype. Figure 51 shows the layout of the dataset, and expands one of the elements to show the relative position of the component data elements.
Each data element is a compound datatype, which can be written or read as a record, or each field may be read or written individually. The first field ("T1") is itself a compound datatype with three fields ("T1.a", "T1.b", and "T1.c"). "T1" can be read or written as a record, or individual fields can be accessed. Similarly, the second filed is a compound datatype with two fields ("T2.f1", "T2.f2").
The third field ("T3") is an array datatype. Thus, "T3" should be accessed as an array of 40 integers. Array data can only be read or written as a single element, so all 40 integers must be read or written to the third field. The fourth field ("T4") is a single string of length 25.
|
Figure 46 |
|
Figure 47 |
|
Figure 48 |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Figure 49 |
|
Figure 50 |
|
||
Figure 51 |
A complicated compound datatype can be analyzed piece by piece, to discover the exact storage layout. In the example above, the outer datatype is analyzed to discover that it is a compound datatype with 4 members. Each member is analyzed in turn to construct a complete map of the storage layout.
Figure 52 shows an example of code that partially analyses a nested compound datatype. The name and overall offset and size of the component datatype is discovered, and then it's type is analyzed, depending on the datatype class. Through this method, the complete storage layout can be discovered.
|
Figure 52 |
Applications programs access HDF5 datatypes through handles, which are obtained by creating a new datatype, or copying or opening an existing datatype. The handle can be used until it is closed, or the program exits (Figure 53a,b). By default, a datatype object is transient, and disappears when it is closed.
When a dataset or attribute is created (H5Dcreate
or H5Acreate
),
its datatype object is stored in the HDF5 file as part of the HDF5
object (the
dataset or attribute) (Figure 53c). Once an object created, its
datatype cannot
be changed or deleted. The datatype can be accessed by calling
H5Dget_type
,
H5Aget_type
, H5Tget_super
, or
H5Tget_member_type
(Figure 53d). These calls return a handle to a transient copy of the
datatype of the dataset or attribute unless the datatype is a named datatype
as explained below.
Note that when an object is created, the stored datatype is a copy of the transient datatype. If two objects are created with the same datatype, the information is stored in each object, with the same effect as if two different datatypes were created and used.
A transient datatype can be stored (H5Tcommit
) in
the HDF5 file
as an independent, named object, called a named datatype (Figure
53e). Subsequently,
when a named datatype is opened with H5Topen
(Figure 53f), or is
obtained with H5Tget_type
or similar call (Figure
53k), the return
is a handle to a transient copy of the stored datatype. The handle
can be used
in the same way as other datatype handles, except the named datatype cannot
be modified. When a named datatype is copied with H5Tcopy
, the
return is a new, modifiable, transient datatype object (Figure 53f).
When an object is created using a named datatype
(H5Dcreate
, H5Acreate
),
the stored datatype is used without copying it to the object (Figure 53j). In
this case, if multiple objects are created using the same named
datatype, they
all share the exact same datatype object. This saves space and
makes clear that
the datatype is shared. Note that a named datatype can be shared by objects
within the same HDF5 file, but not by objects in other files.
A named datatype can be deleted from the file by calling
H5Gunlink
(Figure 53i). If one or more objects are still using the datatype, the named
datatype cannot be accessed with H5Topen, but will not be removed
from the file
until it is no longer used. The H5Tget_type
and
similar calls will
return a transient copy of the datatype.
|
Figure 53 |
Transient datatypes are initially modifiable, its properties can be changed. Note that when a datatype is copied or when it is written to the file (when an object is created) or the datatype is used to create a composite datatype, a copy of the current state of the datatype is used. If the datatype is then modified, the changes have no effect on datasets, attributes, or datatypes that have already been created.
A transient datatype can be made read-only
(H5Tlock
),
after which it can no longer be changed. Note that the datatype is
still transient,
and otherwise does not change. A datatype that is
immutable is read-only
but cannot be closed except when the entire library is closed.
The predefined types such as H5T_NATIVE_INT
are
immutable transient
types.
|
Figure 54 |
To create two or more datasets that share a common datatype, one first commits the datatype, giving it a name, then uses that datatype to create the datasets.
|
Figure 55 |
Table 24 |
|
Function |
Description |
|
A named datatype can be opened by calling this function, which returns a datatype identifier. The identifier should eventually be released by calling H5Tclose() to release resources. The named datatype returned by this function is read-only or a negative value is returned for failure. The location is either a file or group identifier. |
|
A transient datatype (not immutable) can be committed to a file and turned into a named datatype by calling this function. The location is either a file or group identifier and when combined with name refers to a new named datatype. |
|
A type can be queried to determine if it is a named type or a transient type. If this function returns a positive value then the type is named (that is, it has been committed perhaps by some other application). Datasets which return committed datatypes with H5Dget_type() are able to share the datatype with other datasets in the same file. |
When data is transferred (write or read) the storage layout of the data elements may be different. For example, an integer might be stored on disk in big endian byte order, and read into memory with little endian byte order. In this case, each data element will be transformed by the HDF5 library during the data transfer.
The conversion of data elements is controlled by specifying datatype of the source and specifying the intended datatype of the destination. The storage format on disk is the datatype specified when the dataset is create. The datatype of memory must be specified in the library call.
In order to be convertible, the datatype of the source and destination must have the same datatype class. Thus, integers can be converted to other integers, and floats to other floats, but integers cannot (yet) be converted to floats. For each atomic datatype class, the possible conversions are defined.
Basically, any datatype can be converted to another datatype of the same
datatype class. The HDF5 library automatically converts all properties.
If the destination is too small to hold the source value then an overflow
or underflow exception occurs. If a handler is defined, with
H5Tset_overflow()
,
it will be called. Otherwise,
a default action will be performed. Table 25 summarizes the default
action.
Table 25 |
||
Datatype Class |
Possible Exceptions |
Default Action |
Integer |
size, offset, pad |
|
Float |
size, offset, pad, ebits, etc. |
|
String |
size |
Truncates, zero terminate if required. |
Enumeration |
No field |
All Bits set |
When data is transferred (write or read) the format of the data elements may be transformed between the source and the destination, according to the datatypes of the source and destination. |
In order to be convertible, the datatype of the source and destination must have the same datatype class. |
For example, when reading data from a dataset, the source datatype is the datatype set when the dataset was created, and the destination datatype is the description of the storage layout in memory, which must be specified in the H5Dread call. Figure 56 shows an example of reading a dataset of 32-bit integers. Figure 57 shows the data transformation that is performed.
|
Figure 56 |
| |||||||||||||||||||||||||||||||||||||||||||||
Figure 57 |
One thing to note in Figure 56 is the use of the predefined native datatype, H5T_NATIVE_INT. Recall that in this example, the data was stored as a 4-bytes in big endian order. The application wants to read this data into an array of integers in memory. Depending on the system, the storage layout of memory might be either big or little endian, so the data may need to be transformed on some platforms and not on others. The H5T_NATIVE_INT type is set by the HDF5 library to be the correct type to describe the storage layout of the memory on the system. Thus, the code in Figure 56 will work correctly on any platform, performing a transformation when needed.
There are predefined native types for most atomic datatypes, which can be combined in composite datatypes. In general, the predefined native datatypes should always be used for data stored in memory.
Predefined native datatypes describe the storage properties of memory. |
For composite datatypes, the component atomic datatypes will be converted. For a variable length datatype, the source and destination must have compatible base datatypes. For a fixed-size string datatype, the length and padding of the strings will be converted. Variable length strings are converted as variable length datatypes.
For an array datatype, the source and destination must have the same rank and dimensions, and the base datatype must be compatible. For example an array datatype of 4 x 3 32-bit big endian integers can be transferred to an array datatype of 4 x 3 little endian integers, but not to a 3 x 4 array.
For an enumeration datatype, data elements are converted by matching the symbol names of the source and destination Datatype. Figure 58 shows an example of how two enumerations with the same names and different values would be converted. The value '2' in the source dataset would be converted to '0x0004' in the destination.
If the source data stream contains values which are not in the domain of the conversion map then an overflow exception is raised within the library.
| |||||||||||||||||||||||||||||||||||
Figure 58 |
For compound datatypes, each field of the source and destination datatype is converted according to its type. The name and order of the fields must be the same in the source and the destination but the source and destination may have different alignments of the fields, and only some of the fields might be transferred.
Figure 59 shows the compound datatypes shows sample code to create a
compound datatype with the fields aligned on word boundaries (s1_tid)
and with the fields packed (s2_tid). The former is suitable as a description
of the storage layout in memory, the latter would give a more compact store
on disk. These types can be used for transferring data, with
s2_tid
used to create the dataset, and s1_tid
used as
the memory datatype.
|
Figure 59 |
When the data is transferred, the fields within each data element will be aligned according to the datatype specification. Figure 60 shows how one data element would be aligned in memory and on disk. Note that the size and byte order of the elements might also be converted during the transfer.
It is also possible to transfer some of the fields of a compound
datatypes.
Continuing the example, from Figure 59, Figure 61 shows a compound datatype
that selects the first and third fields of the s1_tid
.
The second datatype can be used as the memory datatype, in which case data
is read from or written to these two fields, while skipping the middle field.
Figure 62 shows the data for two data elements.
|
Figure 60 |
|
Figure 61 |
|
Figure 62 |