summaryrefslogtreecommitdiffstats
path: root/doxygen/examples/H5S_examples.c
blob: c542ec0c944d261aa818eb279d4cabdbb368ba67 (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
/* -*- c-file-style: "stroustrup" -*- */

#include "hdf5.h"

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

int
main(void)
{
    int ret_val = EXIT_SUCCESS;

    //! <!-- [create] -->
    {
        __label__ fail_dspace;
        hid_t dspace;

        // create a 2D chess board shape (8x8)
        if ((dspace = H5Screate_simple(2, (hsize_t[]){8, 8}, NULL)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_dspace;
        }

        H5Sclose(dspace);
fail_dspace:;
    }
    //! <!-- [create] -->

    //! <!-- [read] -->
    {
        __label__ fail_dspace;
        hid_t dspace;

        if ((dspace = H5Screate(H5S_NULL)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_dspace;
        }

        // make changes to the selection on DSPACE
        // ...
        // parse the resulting selection

        switch (H5Sget_select_type(dspace)) {
            case H5S_SEL_HYPERSLABS:
                // we are dealing with a hyperslab selection
                // determine the regularity and block structure
                break;
            case H5S_SEL_POINTS:
                // we are dealing with a point selection
                // for example, retrieve the point list
                break;
            case H5S_SEL_ALL:
                // all dataset elements are selected
                break;
            case H5S_SEL_NONE:
                // the selection is empty
                break;
            default:
                ret_val = EXIT_FAILURE;
                break;
        }

        if (dspace != H5S_ALL)
            H5Sclose(dspace);

fail_dspace:;
    }
    //! <!-- [read] -->

    //! <!-- [update] -->
    {
        __label__ fail_select, fail_dspace;
        hid_t dspace;

        // create a 2D chess board shape (8x8)
        if ((dspace = H5Screate_simple(2, (hsize_t[]){8, 8}, NULL)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_dspace;
        }
        // select all black fields: do this w/ a hyperslab union in two steps

        // select the black fields on even rows
        if (H5Sselect_hyperslab(dspace, H5S_SELECT_SET, (hsize_t[]){0, 0}, (hsize_t[]){2, 2},
                                (hsize_t[]){4, 4}, NULL) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_select;
        }
        // select the black fields on odd rows
        // notice the H5S_SELECT_OR operator
        if (H5Sselect_hyperslab(dspace, H5S_SELECT_OR, (hsize_t[]){1, 1}, (hsize_t[]){2, 2},
                                (hsize_t[]){4, 4}, NULL) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_select;
        }

        // should be 32
        printf("%lld elements selected.\n", H5Sget_select_npoints(dspace));

fail_select:
        H5Sclose(dspace);
fail_dspace:;
    }
    //! <!-- [update] -->

    //! <!-- [delete] -->
    {
        __label__ fail_select, fail_dspace;
        hid_t dspace;

        if ((dspace = H5Screate(H5S_NULL)) == H5I_INVALID_HID) {
            ret_val = EXIT_FAILURE;
            goto fail_dspace;
        }

        // make changes to and work with the selection on DSPACE
        // ...
        // finally, clear the selection

        if (H5Sselect_none(dspace) < 0) {
            ret_val = EXIT_FAILURE;
            goto fail_select;
        }

fail_select:
        if (dspace != H5S_ALL)
            H5Sclose(dspace);
fail_dspace:;
    }
    //! <!-- [delete] -->

    return ret_val;
}