summaryrefslogtreecommitdiffstats
path: root/examples/h5_group.c
blob: 0ff7945ed5a471426e1fc0ffe79addb6302d71c4 (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
/*
 * This example shows how to create groups within the file and    
 * datasets within the file and groups.
 */ 


#include "hdf5.h"


#define FILE    "DIR.h5"
#define RANK    2

main()
{

   hid_t    file, dir;
   hid_t    dataset, dataspace;

   herr_t   status;
   size_t   dims[2];
   size_t   size[1];

/*
 * Create a file.
 */
file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5C_DEFAULT, H5C_DEFAULT);

/*
 * Create two groups in a file.
 */
dir = H5Gcreate(file, "/IntData", 0);
status = H5Gclose(dir);

dir = H5Gcreate(file,"/FloatData", 0);
status = H5Gclose(dir);

/* 
 * Create dataspace for the character string
 */
size[0] = 80;
dataspace = H5Pcreate_simple(1, size, NULL);

/*
 * Create dataset "String" in the root group.  
 */
dataset = H5Dcreate(file, "String", H5T_NATIVE_CHAR, dataspace, H5C_DEFAULT);
H5Dclose(dataset);

/*
 * Create dataset "String" in the /IntData group.  
 */
dataset = H5Dcreate(file, "/IntData/String", H5T_NATIVE_CHAR, dataspace,
                    H5C_DEFAULT);
H5Dclose(dataset);

/*
 * Create dataset "String" in the /FloatData group.  
 */
dataset = H5Dcreate(file, "/FloatData/String", H5T_NATIVE_CHAR, dataspace,
                    H5C_DEFAULT);
H5Pclose(dataspace);
H5Dclose(dataset);

/*
 * Create IntArray dataset in the /IntData group by specifying full path.
 */
dims[0] = 2;
dims[1] = 3;
dataspace = H5Pcreate_simple(RANK, dims, NULL);
dataset = H5Dcreate(file, "/IntData/IntArray", H5T_NATIVE_INT, dataspace,
                    H5C_DEFAULT); 
H5Pclose(dataspace);
H5Dclose(dataset);

/*
 * Set current group to /IntData and attach to the dataset String.
 */

status = H5Gset (file, "/IntData");
dataset = H5Dopen(file, "String");
if (dataset > 0) printf("String dataset in /IntData group is found\n"); 
H5Dclose(dataset);

/*
 * Set current group to /FloatData.
 */
status = H5Gset (file, "/FloatData");

/* 
 * Create two datasets FlatArray and DoubleArray.
 */

dims[0] = 5;
dims[1] = 10;
dataspace = H5Pcreate_simple(RANK, dims, NULL);
dataset = H5Dcreate(file, "FloatArray", H5T_NATIVE_FLOAT, dataspace, H5C_DEFAULT); 
H5Pclose(dataspace);
H5Dclose(dataset);

dims[0] = 4;
dims[1] = 6;
dataspace = H5Pcreate_simple(RANK, dims, NULL);
dataset = H5Dcreate(file, "DoubleArray", H5T_NATIVE_DOUBLE, dataspace,
                    H5C_DEFAULT); 
H5Pclose(dataspace);
H5Dclose(dataset);

/* 
 * Attach to /FloatData/String dataset.
 */

dataset = H5Dopen(file, "/FloatData/String");
if (dataset > 0) printf("/FloatData/String dataset is found\n"); 
H5Dclose(dataset);
H5Fclose(file);

}