Please, help us to better know about our user community by answering the following short survey: https://www.hdfgroup.org/website-survey/
HDF5  1.8.23
C-API Reference
H5R

Detailed Description

Use the functions in this module to manage HDF5 references. Referents can be HDF5 objects, attributes, and selections on datasets a.k.a. dataset regions.

CreateRead
15 {
16 __label__ fail_file, fail_fspace, fail_dset, fail_sel, fail_aspace, fail_attr, fail_awrite;
17 hid_t file, fspace, dset, aspace, attr;
18 H5R_ref_t ref;
19
20 if ((file = H5Fcreate("reference.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID) {
21 ret_val = EXIT_FAILURE;
22 goto fail_file;
23 }
24 // create a region reference which selects all elements of the dataset at "/data"
25 if ((fspace = H5Screate_simple(2, (hsize_t[]){10, 20}, NULL)) == H5I_INVALID_HID) {
26 ret_val = EXIT_FAILURE;
27 goto fail_fspace;
28 }
29 if ((dset = H5Dcreate(file, "data", H5T_STD_I32LE, fspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) ==
31 ret_val = EXIT_FAILURE;
32 goto fail_dset;
33 }
34 if (H5Sselect_all(fspace) < 0 || H5Rcreate_region(file, "data", fspace, H5P_DEFAULT, &ref) < 0) {
35 ret_val = EXIT_FAILURE;
36 goto fail_sel;
37 }
38 // store the region reference in a scalar attribute of the root group called "region"
39 if ((aspace = H5Screate(H5S_SCALAR)) == H5I_INVALID_HID) {
40 ret_val = EXIT_FAILURE;
41 goto fail_aspace;
42 }
43 if ((attr = H5Acreate(file, "region", H5T_STD_REF, aspace, H5P_DEFAULT, H5P_DEFAULT)) ==
45 ret_val = EXIT_FAILURE;
46 goto fail_attr;
47 }
48 if (H5Awrite(attr, H5T_STD_REF, &ref) < 0) {
49 ret_val = EXIT_FAILURE;
50 goto fail_awrite;
51 }
52
53fail_awrite:
54 H5Aclose(attr);
55fail_attr:
56 H5Sclose(aspace);
57fail_aspace:
58 H5Rdestroy(&ref);
59fail_sel:
60 H5Dclose(dset);
61fail_dset:
62 H5Sclose(fspace);
63fail_fspace:
64 H5Fclose(file);
65fail_file:;
66 }
#define H5F_ACC_TRUNC
Definition: H5Fpublic.h:90
#define H5I_INVALID_HID
Definition: H5Ipublic.h:66
int hid_t
Definition: H5Ipublic.h:60
#define H5P_DEFAULT
Definition: H5Ppublic.h:255
@ H5S_SCALAR
Definition: H5Spublic.h:72
unsigned long long hsize_t
Definition: H5public.h:334
#define H5Acreate
Definition: H5version.h:156
herr_t H5Awrite(hid_t attr_id, hid_t type_id, const void *buf)
Writes data to an attribute.
herr_t H5Aclose(hid_t attr_id)
Closes the specified attribute.
#define H5Dcreate
Definition: H5version.h:180
herr_t H5Dclose(hid_t dset_id)
Closes the specified dataset.
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_simple(int rank, const hsize_t dims[], const hsize_t maxdims[])
Creates a new simple dataspace and opens it for access.
hid_t H5Screate(H5S_class_t type)
Creates a new dataspace of a specified type.
herr_t H5Sselect_all(hid_t spaceid)
Selects an entire dataspace.
#define H5T_STD_I32LE
Definition: H5Tpublic.h:462
70 {
71 __label__ fail_file, fail_attr, fail_aread;
72 hid_t file, attr;
73 H5R_ref_t ref;
74
75 if ((file = H5Fopen("reference.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) == H5I_INVALID_HID) {
76 ret_val = EXIT_FAILURE;
77 goto fail_file;
78 }
79
80 // read the dataset region reference from the attribute
81 if ((attr = H5Aopen(file, "region", H5P_DEFAULT)) == H5I_INVALID_HID) {
82 ret_val = EXIT_FAILURE;
83 goto fail_attr;
84 }
85 if (H5Aread(attr, H5T_STD_REF, &ref) < 0) {
86 ret_val = EXIT_FAILURE;
87 goto fail_aread;
88 }
89 assert(H5Rget_type(&ref) == H5R_DATASET_REGION2);
90
91 // get an HDF5 path name for the dataset of the region reference
92 {
93 char buf[255];
94 if (H5Rget_obj_name(&ref, H5P_DEFAULT, buf, 255) < 0) {
95 ret_val = EXIT_FAILURE;
96 }
97 printf("Object name: \"%s\"\n", buf);
98 }
99
100 H5Rdestroy(&ref);
101fail_aread:
102 H5Aclose(attr);
103fail_attr:
104 H5Fclose(file);
105fail_file:;
106 }
#define H5F_ACC_RDONLY
Definition: H5Fpublic.h:88
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 H5Aread(hid_t attr_id, hid_t type_id, void *buf)
Reads the value of an attribute.
hid_t H5Fopen(const char *filename, unsigned flags, hid_t fapl_id)
Opens an existing HDF5 file.
UpdateDelete
110 {
111 __label__ fail_file, fail_attr, fail_ref;
112 hid_t file, attr;
113 H5R_ref_t ref;
114
115 if ((file = H5Fopen("reference.h5", H5F_ACC_RDWR, H5P_DEFAULT)) == H5I_INVALID_HID) {
116 ret_val = EXIT_FAILURE;
117 goto fail_file;
118 }
119
120 // H5T_STD_REF is a generic reference type
121 // we can "update" the attribute value to refer to the attribute itself
122 if ((attr = H5Aopen(file, "region", H5P_DEFAULT)) == H5I_INVALID_HID) {
123 ret_val = EXIT_FAILURE;
124 goto fail_attr;
125 }
126 if (H5Rcreate_attr(file, "data", "region", H5P_DEFAULT, &ref) < 0) {
127 ret_val = EXIT_FAILURE;
128 goto fail_ref;
129 }
130
131 assert(H5Rget_type(&ref) == H5R_ATTR);
132
133 if (H5Awrite(attr, H5T_STD_REF, &ref) < 0) {
134 ret_val = EXIT_FAILURE;
135 }
136
137 H5Rdestroy(&ref);
138fail_ref:
139 H5Aclose(attr);
140fail_attr:
141 H5Fclose(file);
142fail_file:;
143 }
#define H5F_ACC_RDWR
Definition: H5Fpublic.h:89
147 {
148 __label__ fail_file, fail_ref;
149 hid_t file;
150 H5R_ref_t ref;
151
152 // create an HDF5 object reference to the root group
153 if ((file = H5Fopen("reference.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) == H5I_INVALID_HID) {
154 ret_val = EXIT_FAILURE;
155 goto fail_file;
156 }
157 if (H5Rcreate_object(file, ".", H5P_DEFAULT, &ref) < 0) {
158 ret_val = EXIT_FAILURE;
159 goto fail_ref;
160 }
161
162 // H5Rdestroy() releases all resources associated with an HDF5 reference
163 H5Rdestroy(&ref);
164fail_ref:
165 H5Fclose(file);
166fail_file:;
167 }

Macros

#define H5Rget_obj_type   H5Rget_obj_type2
 

Functions

herr_t H5Rcreate (void *ref, hid_t loc_id, const char *name, H5R_type_t ref_type, hid_t space_id)
 Creates a reference. More...
 
hid_t H5Rdereference (hid_t dataset, H5R_type_t ref_type, const void *ref)
 Opens the HDF5 object referenced. More...
 
hid_t H5Rget_region (hid_t dataset, H5R_type_t ref_type, const void *ref)
 Sets up a dataspace and selection as specified by a region reference. More...
 
herr_t H5Rget_obj_type2 (hid_t id, H5R_type_t ref_type, const void *_ref, H5O_type_t *obj_type)
 Retrieves the type of object that an object reference points to. More...
 
ssize_t H5Rget_name (hid_t loc_id, H5R_type_t ref_type, const void *ref, char *name, size_t size)
 Retrieves a name for a referenced object. More...
 
H5G_obj_t H5Rget_obj_type1 (hid_t id, H5R_type_t ref_type, const void *ref)
 Retrieves the type of object that an object reference points to. More...
 

Macro Definition Documentation

◆ H5Rget_obj_type

#define H5Rget_obj_type   H5Rget_obj_type2

H5Rget_obj_type() is a macro that is mapped to either H5Rget_obj_type1() or H5Rget_obj_type2() or H5R_get_obj_type3().

See also
API Compatibility Macros

Function Documentation

◆ H5Rcreate()

herr_t H5Rcreate ( void *  ref,
hid_t  loc_id,
const char *  name,
H5R_type_t  ref_type,
hid_t  space_id 
)

Creates a reference.


Parameters
[out]refReference created by the function call
[in]loc_idLocation identifier used to locate the object being pointed to
[in]nameName of object at location loc_id
[in]ref_typeType of reference
[in]space_idDataspace identifier with selection. Used only for dataset region references; pass as -1 if reference is an object reference, i.e., of type H5R_OBJECT
Returns
Returns a non-negative value if successful; otherwise returns a negative value.

H5Rcreate() creates the reference, ref, of the type specified in ref_type, pointing to the object name located at loc_id.

The HDF5 library maps the void type specified above for ref to the type specified in ref_type, which will be one of the following:

typedef enum {
H5R_BADTYPE = (-1),
H5R_type_t
Definition: H5Rpublic.h:81
@ H5R_OBJECT
Definition: H5Rpublic.h:83
@ H5R_BADTYPE
Definition: H5Rpublic.h:82
@ H5R_DATASET_REGION
Definition: H5Rpublic.h:84
@ H5R_MAXTYPE
Definition: H5Rpublic.h:85

The parameters loc_id and name are used to locate the object.

The parameter space_id identifies the dataset region that a dataset region reference points to. This parameter is used only with dataset region references and should be set to -1 if the reference is an object reference, H5R_OBJECT.

Since
1.8.0

◆ H5Rdereference()

hid_t H5Rdereference ( hid_t  dataset,
H5R_type_t  ref_type,
const void *  ref 
)

Opens the HDF5 object referenced.


Parameters
[in]datasetDataset identifier
[in]ref_typeThe reference type of ref
[in]refReference to open
Returns
Returns identifier of referenced object if successful; otherwise returns a negative value.
Deprecated:
This function has been renamed from H5Rdereference() and is deprecated in favor of the macro H5Rdereference() or the function H5Rdereference2().

Given a reference, ref, to an object or a region in an object, H5Rdereference1() opens that object and returns an identifier.

The parameter obj_id must be a valid identifier for an object in the HDF5 file containing the referenced object, including the file identifier.

The parameter ref_type specifies the reference type of the reference ref. ref_type may contain either of the following values:

The object opened with this function should be closed when it is no longer needed so that resource leaks will not develop. Use the appropriate close function such as H5Oclose() or H5Dclose() for datasets.

Since
1.8.0

◆ H5Rget_name()

ssize_t H5Rget_name ( hid_t  loc_id,
H5R_type_t  ref_type,
const void *  ref,
char *  name,
size_t  size 
)

Retrieves a name for a referenced object.


Parameters
[in]loc_idIdentifier for the file containing the reference or for any object in that file
[in]ref_typeType of reference
[in]refAn object or dataset region reference
[out]nameA buffer to place the name of the referenced object or dataset region. If NULL, then this call will return the size in bytes of the name.
[in]sizeThe size of the name buffer. When the size is passed in, the NULL terminator needs to be included.
Returns
Returns the length of the name if successful, returning 0 (zero) if no name is associated with the identifier. Otherwise returns a negative value.

H5Rget_name() retrieves a name for the object identified by ref.
loc_id is used to identify the file containing the reference. It can be the file identifier for the file containing the reference or an identifier for any object in that file.

H5R_type_t is the reference type of ref. Valid values include the following:

ref is the reference for which the target object’s name is sought.

If ref is an object reference, name will be returned with a name for the referenced object. If ref is a dataset region reference, name will contain a name for the object containing the referenced region.

Up to size characters of the name are returned in name; additional characters, if any, are not returned to the user application.

If the length of the name, which determines the required value of size, is unknown, a preliminary H5Rget_name() call can be made. The return value of this call will be the size of the object name. That value can then be assigned to size for a second H5Rget_name() call, which will retrieve the actual name.

If there is no name associated with the object identifier or if the name is NULL, H5Rget_name() returns the size of the name buffer (the size does not include the NULL terminator).

Note that an object in an HDF5 file may have multiple paths if there are multiple links pointing to it. This function may return any one of these paths.

Since
1.8.0

◆ H5Rget_obj_type1()

H5G_obj_t H5Rget_obj_type1 ( hid_t  id,
H5R_type_t  ref_type,
const void *  ref 
)

Retrieves the type of object that an object reference points to.


Parameters
[in]idThe dataset containing the reference object or the group containing that dataset
[in]ref_typeType of reference to query
[in]refReference to query
Returns
Returns a valid object type if successful; otherwise returns a negative value (H5G_UNKNOWN).
Deprecated:
This function has been renamed from H5Rget_obj_type() and is deprecated in favor of the macro H5Rget_obj_type() or the function H5Rget_obj_type2().

Given an object reference, ref, H5Rget_obj_type1() returns the type of the referenced object.

A reference type is the type of reference, either an object reference or a dataset region reference. An object reference points to an HDF5 object while a dataset region reference points to a defined region within a dataset.

The referenced object is the object the reference points to. The referenced object type, or the type of the referenced object, is the type of the object that the reference points to.

The location identifier, id, is the identifier for either the dataset containing the object reference or the group containing that dataset.

Valid reference types, to pass in as ref_type, include the following:

If the application does not already know the object reference type, that can be determined with three preliminary calls:

When the function completes successfully, it returns one of the following valid object type values (defined in H5Gpublic.h):

typedef enum H5G_obj_t {
H5G_UNKNOWN = -1,
H5G_obj_t
Definition: H5Gpublic.h:564
@ H5G_LINK
Definition: H5Gpublic.h:569
@ H5G_TYPE
Definition: H5Gpublic.h:568
@ H5G_UNKNOWN
Definition: H5Gpublic.h:565
@ H5G_GROUP
Definition: H5Gpublic.h:566
@ H5G_RESERVED_6
Definition: H5Gpublic.h:572
@ H5G_DATASET
Definition: H5Gpublic.h:567
@ H5G_RESERVED_5
Definition: H5Gpublic.h:571
@ H5G_UDLINK
Definition: H5Gpublic.h:570
@ H5G_RESERVED_7
Definition: H5Gpublic.h:573
Version
1.8.0 Function H5Rget_obj_type() renamed to H5Rget_obj_type1() and deprecated in this release.
Since
1.6.0

◆ H5Rget_obj_type2()

herr_t H5Rget_obj_type2 ( hid_t  id,
H5R_type_t  ref_type,
const void *  _ref,
H5O_type_t obj_type 
)

Retrieves the type of object that an object reference points to.


Parameters
[in]idThe dataset containing the reference object or the group containing that dataset
[in]ref_typeType of reference to query
[in]_refReference to query
[out]obj_typeType of referenced object
Returns
Returns a non-negative value if successful; otherwise returns a negative value.

Given an object reference, ref, H5Rget_obj_type2() returns the type of the referenced object in obj_type.

A reference type is the type of reference, either an object reference or a dataset region reference. An object reference points to an HDF5 object while a dataset region reference points to a defined region within a dataset.

The referenced object is the object the reference points to. The referenced object type, or the type of the referenced object, is the type of the object that the reference points to.

The location identifier, id, is the identifier for either the dataset containing the object reference or the group containing that dataset.

Valid reference types, to pass in as ref_type, include the following:

If the application does not already know the object reference type, that can be determined with three preliminary calls:

When the function completes successfully, it returns one of the following valid object type values (defined in H5Opublic.h):

typedef enum H5O_type_t {
H5O_type_t
Definition: H5Opublic.h:134
@ H5O_TYPE_DATASET
Definition: H5Opublic.h:137
@ H5O_TYPE_UNKNOWN
Definition: H5Opublic.h:135
@ H5O_TYPE_NAMED_DATATYPE
Definition: H5Opublic.h:138
@ H5O_TYPE_GROUP
Definition: H5Opublic.h:136
@ H5O_TYPE_NTYPES
Definition: H5Opublic.h:139
Since
1.8.0

◆ H5Rget_region()

hid_t H5Rget_region ( hid_t  dataset,
H5R_type_t  ref_type,
const void *  ref 
)

Sets up a dataspace and selection as specified by a region reference.


Parameters
[in]datasetFile identifier or identifier for any object in the file containing the referenced region
[in]ref_typeReference type of ref, which must be H5R_DATASET_REGION
[in]refRegion reference to open
Returns
Returns a valid dataspace identifier if successful; otherwise returns a negative value.

H5Rget_region() creates a copy of the dataspace of the dataset pointed to by a region reference, ref, and defines a selection matching the selection pointed to by ref within the dataspace copy.

dataset is used to identify the file containing the referenced region; it can be a file identifier or an identifier for any object in the file.

The parameter ref_type specifies the reference type of ref and must contain the value H5R_DATASET_REGION.

Use H5Sclose() to release the dataspace identifier returned by this function when the identifier is no longer needed.