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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.5"/>
<title>HDF5 C++ API: writedata.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="hdf_logo.jpg"/></td>
<td style="padding-left: 0.5em;">
<div id="projectname">HDF5 C++ API
 <span id="projectnumber">1.8.13</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Pages</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">writedata.cpp</div> </div>
</div><!--header-->
<div class="contents">
<dl class="section user"><dt></dt><dd>This example shows how to write datasets.</dd></dl>
<div class="fragment"><div class="line"><span class="comment">/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *</span></div>
<div class="line"><span class="comment"> * Copyright by The HDF Group. *</span></div>
<div class="line"><span class="comment"> * Copyright by the Board of Trustees of the University of Illinois. *</span></div>
<div class="line"><span class="comment"> * All rights reserved. *</span></div>
<div class="line"><span class="comment"> * *</span></div>
<div class="line"><span class="comment"> * This file is part of HDF5. The full HDF5 copyright notice, including *</span></div>
<div class="line"><span class="comment"> * terms governing use, modification, and redistribution, is contained in *</span></div>
<div class="line"><span class="comment"> * the files COPYING and Copyright.html. COPYING can be found at the root *</span></div>
<div class="line"><span class="comment"> * of the source code distribution tree; Copyright.html can be found at the *</span></div>
<div class="line"><span class="comment"> * root level of an installed copy of the electronic HDF5 document set and *</span></div>
<div class="line"><span class="comment"> * is linked from the top-level documents page. It can also be found at *</span></div>
<div class="line"><span class="comment"> * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *</span></div>
<div class="line"><span class="comment"> * access to either file, you may request a copy from help@hdfgroup.org. *</span></div>
<div class="line"><span class="comment"> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * This program shows how the select_hyperslab and select_elements</span></div>
<div class="line"><span class="comment"> * functions are used to write selected data from memory to the file.</span></div>
<div class="line"><span class="comment"> * Program takes 48 elements from the linear buffer and writes them into</span></div>
<div class="line"><span class="comment"> * the matrix using 3x2 blocks, (4,3) stride and (2,4) count.</span></div>
<div class="line"><span class="comment"> * Then four elements of the matrix are overwritten with the new values and</span></div>
<div class="line"><span class="comment"> * file is closed. Program reopens the file and reads and displays the result.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifdef OLD_HEADER_FILENAME</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#include <iostream.h></span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#include <iostream></span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#include <string></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifndef H5_NO_NAMESPACE</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#ifndef H5_NO_STD</span></div>
<div class="line"><span class="preprocessor"></span> <span class="keyword">using</span> std::cout;</div>
<div class="line"> <span class="keyword">using</span> std::endl;</div>
<div class="line"><span class="preprocessor">#endif // H5_NO_STD</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="preprocessor">#include "H5Cpp.h"</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifndef H5_NO_NAMESPACE</span></div>
<div class="line"><span class="preprocessor"></span> <span class="keyword">using namespace </span>H5;</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="keyword">const</span> H5std_string FILE_NAME( <span class="stringliteral">"Select.h5"</span> );</div>
<div class="line"><span class="keyword">const</span> H5std_string DATASET_NAME( <span class="stringliteral">"Matrix in file"</span> );</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE1_RANK = 1; <span class="comment">// Rank of the first dataset in memory</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE1_DIM = 50; <span class="comment">// Dataset size in memory</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE2_RANK = 1; <span class="comment">// Rank of the second dataset in memory</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE2_DIM = 4; <span class="comment">// Dataset size in memory</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> FSPACE_RANK = 2; <span class="comment">// Dataset rank as it is stored in the file</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> FSPACE_DIM1 = 8; <span class="comment">// Dimension sizes of the dataset as it is</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> FSPACE_DIM2 = 12; <span class="comment">// stored in the file</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE_RANK = 2; <span class="comment">// Rank of the first dataset in memory</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE_DIM1 = 8; <span class="comment">// We will read dataset back from the file</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> MSPACE_DIM2 = 9; <span class="comment">// to the dataset in memory with these</span></div>
<div class="line"> <span class="comment">// dataspace parameters</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> NPOINTS = 4; <span class="comment">// Number of points that will be selected</span></div>
<div class="line"> <span class="comment">// and overwritten</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordtype">int</span> i,j; <span class="comment">// loop indices */</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Try block to detect exceptions raised by any of the calls inside it</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordflow">try</span></div>
<div class="line"> {</div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Turn off the auto-printing when failure occurs so that we can</span></div>
<div class="line"><span class="comment"> * handle the errors appropriately</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <a name="a0"></a><a class="code" href="class_h5_1_1_exception.html#ad49f16300979996c094c2e4f57868414">Exception::dontPrint</a>();</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create a file.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <a name="_a1"></a><a class="code" href="class_h5_1_1_h5_file.html">H5File</a>* file = <span class="keyword">new</span> <a class="code" href="class_h5_1_1_h5_file.html">H5File</a>( FILE_NAME, H5F_ACC_TRUNC );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create property list for a dataset and set up fill values.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordtype">int</span> fillvalue = 0; <span class="comment">/* Fill value for the dataset */</span></div>
<div class="line"> <a name="_a2"></a><a class="code" href="class_h5_1_1_d_set_creat_prop_list.html">DSetCreatPropList</a> plist;</div>
<div class="line"> plist.<a name="a3"></a><a class="code" href="class_h5_1_1_d_set_creat_prop_list.html#a06475f58202e5e96ca50e977174bb4ca">setFillValue</a>(<a name="a4"></a><a class="code" href="class_h5_1_1_pred_type.html#a3308bcd537696a8d728cd3b443f2fc7b">PredType::NATIVE_INT</a>, &fillvalue);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create dataspace for the dataset in the file.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; <span class="comment">// dim sizes of ds (on disk)</span></div>
<div class="line"> <a name="_a5"></a><a class="code" href="class_h5_1_1_data_space.html">DataSpace</a> fspace( FSPACE_RANK, fdim );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create dataset and write it into the file.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <a name="_a6"></a><a class="code" href="class_h5_1_1_data_set.html">DataSet</a>* dataset = <span class="keyword">new</span> <a class="code" href="class_h5_1_1_data_set.html">DataSet</a>(file-><a name="a7"></a><a class="code" href="class_h5_1_1_common_f_g.html#a12a4af21ca0231d4f2c4008b12177bb1">createDataSet</a>(</div>
<div class="line"> DATASET_NAME, <a class="code" href="class_h5_1_1_pred_type.html#a3308bcd537696a8d728cd3b443f2fc7b">PredType::NATIVE_INT</a>, fspace, plist));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Select hyperslab for the dataset in the file, using 3x2 blocks,</span></div>
<div class="line"><span class="comment"> * (4,3) stride and (2,4) count starting at the position (0,1).</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> hsize_t start[2]; <span class="comment">// Start of hyperslab</span></div>
<div class="line"> hsize_t stride[2]; <span class="comment">// Stride of hyperslab</span></div>
<div class="line"> hsize_t count[2]; <span class="comment">// Block count</span></div>
<div class="line"> hsize_t block[2]; <span class="comment">// Block sizes</span></div>
<div class="line"> start[0] = 0; start[1] = 1;</div>
<div class="line"> stride[0] = 4; stride[1] = 3;</div>
<div class="line"> count[0] = 2; count[1] = 4;</div>
<div class="line"> block[0] = 3; block[1] = 2;</div>
<div class="line"> fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create dataspace for the first dataset.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> hsize_t dim1[] = {MSPACE1_DIM}; <span class="comment">/* Dimension size of the first dataset</span></div>
<div class="line"><span class="comment"> (in memory) */</span></div>
<div class="line"> <a class="code" href="class_h5_1_1_data_space.html">DataSpace</a> mspace1( MSPACE1_RANK, dim1 );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Select hyperslab.</span></div>
<div class="line"><span class="comment"> * We will use 48 elements of the vector buffer starting at the</span></div>
<div class="line"><span class="comment"> * second element. Selected elements are 1 2 3 . . . 48</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> start[0] = 1;</div>
<div class="line"> stride[0] = 1;</div>
<div class="line"> count[0] = 48;</div>
<div class="line"> block[0] = 1;</div>
<div class="line"> mspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Write selection from the vector buffer to the dataset in the file.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * File dataset should look like this:</span></div>
<div class="line"><span class="comment"> * 0 1 2 0 3 4 0 5 6 0 7 8</span></div>
<div class="line"><span class="comment"> * 0 9 10 0 11 12 0 13 14 0 15 16</span></div>
<div class="line"><span class="comment"> * 0 17 18 0 19 20 0 21 22 0 23 24</span></div>
<div class="line"><span class="comment"> * 0 0 0 0 0 0 0 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> * 0 25 26 0 27 28 0 29 30 0 31 32</span></div>
<div class="line"><span class="comment"> * 0 33 34 0 35 36 0 37 38 0 39 40</span></div>
<div class="line"><span class="comment"> * 0 41 42 0 43 44 0 45 46 0 47 48</span></div>
<div class="line"><span class="comment"> * 0 0 0 0 0 0 0 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordtype">int</span> vector[MSPACE1_DIM]; <span class="comment">// vector buffer for dset</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Buffer initialization.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> vector[0] = vector[MSPACE1_DIM - 1] = -1;</div>
<div class="line"> <span class="keywordflow">for</span> (i = 1; i < MSPACE1_DIM - 1; i++)</div>
<div class="line"> vector[i] = i;</div>
<div class="line"></div>
<div class="line"> dataset-><a name="a8"></a><a class="code" href="class_h5_1_1_data_set.html#a3ce98e7a626fd32a23d1bcc41537326f">write</a>( vector, <a class="code" href="class_h5_1_1_pred_type.html#a3308bcd537696a8d728cd3b443f2fc7b">PredType::NATIVE_INT</a>, mspace1, fspace );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Reset the selection for the file dataspace fid.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> fspace.selectNone();</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create dataspace for the second dataset.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> hsize_t dim2[] = {MSPACE2_DIM}; <span class="comment">/* Dimension size of the second dataset</span></div>
<div class="line"><span class="comment"> (in memory */</span></div>
<div class="line"> <a class="code" href="class_h5_1_1_data_space.html">DataSpace</a> mspace2( MSPACE2_RANK, dim2 );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Select sequence of NPOINTS points in the file dataspace.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> hsize_t coord[NPOINTS][FSPACE_RANK]; <span class="comment">/* Array to store selected points</span></div>
<div class="line"><span class="comment"> from the file dataspace */</span></div>
<div class="line"> coord[0][0] = 0; coord[0][1] = 0;</div>
<div class="line"> coord[1][0] = 3; coord[1][1] = 3;</div>
<div class="line"> coord[2][0] = 3; coord[2][1] = 5;</div>
<div class="line"> coord[3][0] = 5; coord[3][1] = 6;</div>
<div class="line"></div>
<div class="line"> fspace.selectElements( H5S_SELECT_SET, NPOINTS, (<span class="keyword">const</span> hsize_t *)coord);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Write new selection of points to the dataset.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordtype">int</span> values[] = {53, 59, 61, 67}; <span class="comment">/* New values to be written */</span></div>
<div class="line"> dataset-><a class="code" href="class_h5_1_1_data_set.html#a3ce98e7a626fd32a23d1bcc41537326f">write</a>( values, <a class="code" href="class_h5_1_1_pred_type.html#a3308bcd537696a8d728cd3b443f2fc7b">PredType::NATIVE_INT</a>, mspace2, fspace );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * File dataset should look like this:</span></div>
<div class="line"><span class="comment"> * 53 1 2 0 3 4 0 5 6 0 7 8</span></div>
<div class="line"><span class="comment"> * 0 9 10 0 11 12 0 13 14 0 15 16</span></div>
<div class="line"><span class="comment"> * 0 17 18 0 19 20 0 21 22 0 23 24</span></div>
<div class="line"><span class="comment"> * 0 0 0 59 0 61 0 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> * 0 25 26 0 27 28 0 29 30 0 31 32</span></div>
<div class="line"><span class="comment"> * 0 33 34 0 35 36 67 37 38 0 39 40</span></div>
<div class="line"><span class="comment"> * 0 41 42 0 43 44 0 45 46 0 47 48</span></div>
<div class="line"><span class="comment"> * 0 0 0 0 0 0 0 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Close the dataset and the file.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keyword">delete</span> dataset;</div>
<div class="line"> <span class="keyword">delete</span> file;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Open the file.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> file = <span class="keyword">new</span> <a class="code" href="class_h5_1_1_h5_file.html">H5File</a>( FILE_NAME, H5F_ACC_RDONLY );</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Open the dataset.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> dataset = <span class="keyword">new</span> <a class="code" href="class_h5_1_1_data_set.html">DataSet</a>( file-><a name="a9"></a><a class="code" href="class_h5_1_1_common_f_g.html#a6a2557f173c32577db9feeb1b77d4e40">openDataSet</a>( DATASET_NAME ));</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Get dataspace of the dataset.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> fspace = dataset-><a name="a10"></a><a class="code" href="class_h5_1_1_data_set.html#a1f727ed91722af4630ba20b2d8b5b12d">getSpace</a>();</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Select first hyperslab for the dataset in the file. The following</span></div>
<div class="line"><span class="comment"> * elements are selected:</span></div>
<div class="line"><span class="comment"> * 10 0 11 12</span></div>
<div class="line"><span class="comment"> * 18 0 19 20</span></div>
<div class="line"><span class="comment"> * 0 59 0 61</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> start[0] = 1; start[1] = 2;</div>
<div class="line"> block[0] = 1; block[1] = 1;</div>
<div class="line"> stride[0] = 1; stride[1] = 1;</div>
<div class="line"> count[0] = 3; count[1] = 4;</div>
<div class="line"> fspace.selectHyperslab(H5S_SELECT_SET, count, start, stride, block);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Add second selected hyperslab to the selection.</span></div>
<div class="line"><span class="comment"> * The following elements are selected:</span></div>
<div class="line"><span class="comment"> * 19 20 0 21 22</span></div>
<div class="line"><span class="comment"> * 0 61 0 0 0</span></div>
<div class="line"><span class="comment"> * 27 28 0 29 30</span></div>
<div class="line"><span class="comment"> * 35 36 67 37 38</span></div>
<div class="line"><span class="comment"> * 43 44 0 45 46</span></div>
<div class="line"><span class="comment"> * 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> * Note that two hyperslabs overlap. Common elements are:</span></div>
<div class="line"><span class="comment"> * 19 20</span></div>
<div class="line"><span class="comment"> * 0 61</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> start[0] = 2; start[1] = 4;</div>
<div class="line"> block[0] = 1; block[1] = 1;</div>
<div class="line"> stride[0] = 1; stride[1] = 1;</div>
<div class="line"> count[0] = 6; count[1] = 5;</div>
<div class="line"> fspace.selectHyperslab(H5S_SELECT_OR, count, start, stride, block);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Create memory dataspace.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> hsize_t mdim[] = {MSPACE_DIM1, MSPACE_DIM2}; <span class="comment">/* Dimension sizes of the</span></div>
<div class="line"><span class="comment"> dataset in memory when we</span></div>
<div class="line"><span class="comment"> read selection from the</span></div>
<div class="line"><span class="comment"> dataset on the disk */</span></div>
<div class="line"> <a class="code" href="class_h5_1_1_data_space.html">DataSpace</a> mspace(MSPACE_RANK, mdim);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Select two hyperslabs in memory. Hyperslabs has the same</span></div>
<div class="line"><span class="comment"> * size and shape as the selected hyperslabs for the file dataspace.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> start[0] = 0; start[1] = 0;</div>
<div class="line"> block[0] = 1; block[1] = 1;</div>
<div class="line"> stride[0] = 1; stride[1] = 1;</div>
<div class="line"> count[0] = 3; count[1] = 4;</div>
<div class="line"> mspace.selectHyperslab(H5S_SELECT_SET, count, start, stride, block);</div>
<div class="line"> start[0] = 1; start[1] = 2;</div>
<div class="line"> block[0] = 1; block[1] = 1;</div>
<div class="line"> stride[0] = 1; stride[1] = 1;</div>
<div class="line"> count[0] = 6; count[1] = 5;</div>
<div class="line"> mspace.selectHyperslab(H5S_SELECT_OR, count, start, stride, block);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Initialize data buffer.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordtype">int</span> matrix_out[MSPACE_DIM1][MSPACE_DIM2];</div>
<div class="line"> <span class="keywordflow">for</span> (i = 0; i < MSPACE_DIM1; i++)</div>
<div class="line"> <span class="keywordflow">for</span> (j = 0; j < MSPACE_DIM2; j++)</div>
<div class="line"> matrix_out[i][j] = 0;</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Read data back to the buffer matrix.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> dataset-><a name="a11"></a><a class="code" href="class_h5_1_1_data_set.html#a2f0278a519122419ae9981666277c9bb">read</a>(matrix_out, <a class="code" href="class_h5_1_1_pred_type.html#a3308bcd537696a8d728cd3b443f2fc7b">PredType::NATIVE_INT</a>, mspace, fspace);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Display the result. Memory dataset is:</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * 10 0 11 12 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> * 18 0 19 20 0 21 22 0 0</span></div>
<div class="line"><span class="comment"> * 0 59 0 61 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> * 0 0 27 28 0 29 30 0 0</span></div>
<div class="line"><span class="comment"> * 0 0 35 36 67 37 38 0 0</span></div>
<div class="line"><span class="comment"> * 0 0 43 44 0 45 46 0 0</span></div>
<div class="line"><span class="comment"> * 0 0 0 0 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> * 0 0 0 0 0 0 0 0 0</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keywordflow">for</span> (i=0; i < MSPACE_DIM1; i++)</div>
<div class="line"> {</div>
<div class="line"> <span class="keywordflow">for</span>(j=0; j < MSPACE_DIM2; j++)</div>
<div class="line"> cout << matrix_out[i][j] << <span class="stringliteral">" "</span>;</div>
<div class="line"> cout << endl;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Close the dataset and the file.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keyword">delete</span> dataset;</div>
<div class="line"> <span class="keyword">delete</span> file;</div>
<div class="line"> } <span class="comment">// end of try block</span></div>
<div class="line"></div>
<div class="line"> <span class="comment">// catch failure caused by the H5File operations</span></div>
<div class="line"> <span class="keywordflow">catch</span>( <a name="_a12"></a><a class="code" href="class_h5_1_1_file_i_exception.html">FileIException</a> error )</div>
<div class="line"> {</div>
<div class="line"> error.<a name="a13"></a><a class="code" href="class_h5_1_1_exception.html#ad5bf0f93b4156d6366110dd5e08342a2">printError</a>();</div>
<div class="line"> <span class="keywordflow">return</span> -1;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// catch failure caused by the DataSet operations</span></div>
<div class="line"> <span class="keywordflow">catch</span>( <a name="_a14"></a><a class="code" href="class_h5_1_1_data_set_i_exception.html">DataSetIException</a> error )</div>
<div class="line"> {</div>
<div class="line"> error.<a class="code" href="class_h5_1_1_exception.html#ad5bf0f93b4156d6366110dd5e08342a2">printError</a>();</div>
<div class="line"> <span class="keywordflow">return</span> -1;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">// catch failure caused by the DataSpace operations</span></div>
<div class="line"> <span class="keywordflow">catch</span>( <a name="_a15"></a><a class="code" href="class_h5_1_1_data_space_i_exception.html">DataSpaceIException</a> error )</div>
<div class="line"> {</div>
<div class="line"> error.<a class="code" href="class_h5_1_1_exception.html#ad5bf0f93b4156d6366110dd5e08342a2">printError</a>();</div>
<div class="line"> <span class="keywordflow">return</span> -1;</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Mon Jul 21 2014 10:58:44 for HDF5 C++ API by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.5
</small></address>
</body>
</html>
|