summaryrefslogtreecommitdiffstats
path: root/examples/h5_extend_write.c
blob: 2417cc00a3b8506951e6402c20ffa2f45f5e9192 (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
/*  
 *   This example shows how to work with extendible dataset.
 *   In the current version of the library dataset MUST be
 *   chunked.
 *   
 */
 
#include "hdf5.h"

#define FILE        "SDSextendible.h5"
#define DATASETNAME "ExtendibleArray" 
#define RANK         2
#define NX     10
#define NY     5 

main ()
{
   hid_t       file;                          /* handles */
   hid_t       datatype, dataspace, dataset;  
   hid_t       filespace;                   
   hid_t       cparms;                     
   size_t      dims[2]  = { 3, 3};            /* dataset dimensions
                                                 at the creation time  */ 
   size_t      dims1[2] = { 3, 3};            /* data1 dimensions */ 
   size_t      dims2[2] = { 7, 1};            /* data2 dimensions */  
   size_t      dims3[2] = { 2, 2};            /* data3 dimensions */ 

   size_t      maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
   size_t      chunk_dims[2] ={2, 5};
   size_t      size[2];
   int         offset[2];

   herr_t      status;                             

   int         data1[3][3] = { 1, 1, 1,       /* data to write */
                               1, 1, 1,
                               1, 1, 1 };      

   int         data2[7]    = { 2, 2, 2, 2, 2, 2, 2};

   int         data3[2][2] = { 3, 3,
                               3, 3};

/*
 * Create the data space with ulimited dimensions. 
 */
dataspace = H5Screate_simple(RANK, dims, maxdims); 

/*
 * Create a new file. If file exists its contents will be overwritten.
 */
file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

/* 
 * Modify dataset creation properties, i.e. enable chunking.
 */
cparms = H5Pcreate (H5P_DATASET_CREATE);
status = H5Pset_chunk( cparms, RANK, chunk_dims);

/*
 * Create a new dataset within the file using cparms
 * creation properties.
 */
dataset = H5Dcreate(file, DATASETNAME, H5T_NATIVE_INT, dataspace,
                 cparms);

/*
 * Extend the dataset. This call assures that dataset is at least 3 x 3.
 */
size[0]   = 3; 
size[1]   = 3; 
status = H5Dextend (dataset, size);

/*
 * Select a hyperslab.
 */
filespace = H5Dget_space (dataset);
offset[0] = 0;
offset[1] = 0;
status = H5Sset_hyperslab(filespace, offset, dims1, NULL);  

/*
 * Write the data to the hyperslab.
 */
status = H5Dwrite(dataset, H5T_NATIVE_INT, dataspace, filespace,
                  H5P_DEFAULT, data1);

/*
 * Extend the dataset. Dataset becomes 10 x 3.
 */
dims[0]   = dims1[0] + dims2[0];
size[0]   = dims[0];  
size[1]   = dims[1]; 
status = H5Dextend (dataset, size);

/*
 * Select a hyperslab.
 */
filespace = H5Dget_space (dataset);
offset[0] = 3;
offset[1] = 0;
status = H5Sset_hyperslab(filespace, offset, dims2, NULL);  

/*
 * Define memory space
 */
dataspace = H5Screate_simple(RANK, dims2, NULL); 

/*
 * Write the data to the hyperslab.
 */
status = H5Dwrite(dataset, H5T_NATIVE_INT, dataspace, filespace,
                  H5P_DEFAULT, data2);

/*
 * Extend the dataset. Dataset becomes 10 x 5.
 */
dims[1]   = dims1[1] + dims3[1];
size[0]   = dims[0];  
size[1]   = dims[1]; 
status = H5Dextend (dataset, size);

/*
 * Select a hyperslab
 */
filespace = H5Dget_space (dataset);
offset[0] = 0;
offset[1] = 3;
status = H5Sset_hyperslab(filespace, offset, dims3, NULL);  

/*
 * Define memory space.
 */
dataspace = H5Screate_simple(RANK, dims3, NULL); 

/*
 * Write the data to the hyperslab.
 */
status = H5Dwrite(dataset, H5T_NATIVE_INT, dataspace, filespace,
                  H5P_DEFAULT, data3);

/*
 * Resulting dataset
 *                 
         3 3 3 2 2
         3 3 3 2 2
         3 3 3 0 0
         2 0 0 0 0
         2 0 0 0 0
         2 0 0 0 0
         2 0 0 0 0
         2 0 0 0 0
         2 0 0 0 0
         2 0 0 0 0
 */ 
/*
 * Close/release resources.
 */
H5Dclose(dataset);
H5Sclose(dataspace);
H5Sclose(filespace);
H5Fclose(file);

}