summaryrefslogtreecommitdiffstats
path: root/doxygen/examples/H5R_examples.c
blob: b40b99270bfee7e06c53148f44df5738817a2f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- c-file-style: "stroustrup" -*- */

#include "hdf5.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    int ret_val = EXIT_SUCCESS;

    //! <!-- [create] -->
    {
        __label__ fail_file, fail_fspace, fail_dset, fail_sel, fail_aspace, fail_attr, fail_awrite;
        hid_t     file, fspace, dset, aspace, attr;
        H5R_ref_t ref;

        if ((file = H5Fcreate("reference.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_file;
        }
        // create a region reference which selects all elements of the dataset at "/data"
        if ((fspace = H5Screate_simple(2, (hsize_t[]){10, 20}, NULL)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_fspace;
        }
        if ((dset = H5Dcreate(file, "data", H5T_STD_I32LE, fspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) ==
            H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_dset;
        }
        if (H5Sselect_all(fspace) < 0 || H5Rcreate_region(file, "data", fspace, H5P_DEFAULT, &ref) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_sel;
        }
        // store the region reference in a scalar attribute of the root group called "region"
        if ((aspace = H5Screate(H5S_SCALAR)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_aspace;
        }
        if ((attr = H5Acreate(file, "region", H5T_STD_REF, aspace, H5P_DEFAULT, H5P_DEFAULT)) ==
            H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_attr;
        }
        if (H5Awrite(attr, H5T_STD_REF, &ref) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_awrite;
        }

fail_awrite:
        H5Aclose(attr);
fail_attr:
        H5Sclose(aspace);
fail_aspace:
        H5Rdestroy(&ref);
fail_sel:
        H5Dclose(dset);
fail_dset:
        H5Sclose(fspace);
fail_fspace:
        H5Fclose(file);
fail_file:;
    }
    //! <!-- [create] -->

    //! <!-- [read] -->
    {
        __label__ fail_file, fail_attr, fail_aread;
        hid_t     file, attr;
        H5R_ref_t ref;

        if ((file = H5Fopen("reference.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_file;
        }

        // read the dataset region reference from the attribute
        if ((attr = H5Aopen(file, "region", H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_attr;
        }
        if (H5Aread(attr, H5T_STD_REF, &ref) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_aread;
        }
        assert(H5Rget_type(&ref) == H5R_DATASET_REGION2);

        // get an HDF5 path name for the dataset of the region reference
        {
            char buf[255];
            if (H5Rget_obj_name(&ref, H5P_DEFAULT, buf, 255) < 0) {
                ret_val = EXIT_FAILURE;
            }
            printf("Object name: \"%s\"\n", buf);
        }

        H5Rdestroy(&ref);
fail_aread:
        H5Aclose(attr);
fail_attr:
        H5Fclose(file);
fail_file:;
    }
    //! <!-- [read] -->

    //! <!-- [update] -->
    {
        __label__ fail_file, fail_attr, fail_ref;
        hid_t     file, attr;
        H5R_ref_t ref;

        if ((file = H5Fopen("reference.h5", H5F_ACC_RDWR, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_file;
        }

        // H5T_STD_REF is a generic reference type
        // we can "update" the attribute value to refer to the attribute itself
        if ((attr = H5Aopen(file, "region", H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_attr;
        }
        if (H5Rcreate_attr(file, "data", "region", H5P_DEFAULT, &ref) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_ref;
        }

        assert(H5Rget_type(&ref) == H5R_ATTR);

        if (H5Awrite(attr, H5T_STD_REF, &ref) < 0) {
            ret_val = EXIT_FAILURE;
        }

        H5Rdestroy(&ref);
fail_ref:
        H5Aclose(attr);
fail_attr:
        H5Fclose(file);
fail_file:;
    }
    //! <!-- [update] -->

    //! <!-- [delete] -->
    {
        __label__ fail_file, fail_ref;
        hid_t     file;
        H5R_ref_t ref;

        // create an HDF5 object reference to the root group
        if ((file = H5Fopen("reference.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_file;
        }
        if (H5Rcreate_object(file, ".", H5P_DEFAULT, &ref) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_ref;
        }

        // H5Rdestroy() releases all resources associated with an HDF5 reference
        H5Rdestroy(&ref);
fail_ref:
        H5Fclose(file);
fail_file:;
    }
    //! <!-- [delete] -->

    return ret_val;
}