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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
<!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.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>HDF5: The HDF5 Library and Programming Model</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="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="hdf5doxy.css" rel="stylesheet" type="text/css">
<!-- <link href="hdf5doxy.css" rel="stylesheet" type="text/css"/>
-->
<script type="text/javascript" src="hdf5_navtree_hacks.js"></script>
</head>
<body>
<div style="background:#FFDDDD;font-size:120%;text-align:center;margin:0;padding:5px">Please, help us to better serve our user community by answering the following short survey: <a href="https://www.hdfgroup.org/website-survey/">https://www.hdfgroup.org/website-survey/</a></div>
<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="HDFG-logo.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname"><a href="https://www.hdfgroup.org">HDF5</a>
 <span id="projectnumber">1.15.0.edb5cff</span>
</div>
<div id="projectbrief">API Reference</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.svg"
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.svg" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_h5__u_g.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</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="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">The HDF5 Library and Programming Model </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="sec_program"></a>
The HDF5 Library and Programming Model</h1>
<h2><a class="anchor" id="subsec_program_intro"></a>
Introduction</h2>
<p>The HDF5 library implements the HDF5 abstract data model and storage model. These models were described in the preceding chapter.</p>
<p>Two major objectives of the HDF5 products are to provide tools that can be used on as many computational platforms as possible (portability), and to provide a reasonably object-oriented data model and programming interface.</p>
<p>To be as portable as possible, the HDF5 library is implemented in portable C. C is not an object-oriented language, but the library uses several mechanisms and conventions to implement an object model.</p>
<p>One mechanism the HDF5 library uses is to implement the objects as data structures. To refer to an object, the HDF5 library implements its own pointers. These pointers are called identifiers. An identifier is then used to invoke operations on a specific instance of an object. For example, when a group is opened, the API returns a group identifier. This identifier is a reference to that specific group and will be used to invoke future operations on that group. The identifier is valid only within the context it is created and remains valid until it is closed or the file is closed. This mechanism is essentially the same as the mechanism that C++ or other object-oriented languages use to refer to objects except that the syntax is C.</p>
<p>Similarly, object-oriented languages collect all the methods for an object in a single name space. An example is the methods of a C++ class. The C language does not have any such mechanism, but the HDF5 library simulates this through its API naming convention. API function names begin with a common prefix that is related to the class of objects that the function operates on. The table below lists the HDF5 objects and the standard prefixes used by the corresponding HDF5 APIs. For example, functions that operate on datatype objects all have names beginning with H5T.</p>
<table class="doxtable">
<caption>Access flags and modes</caption>
<tr>
<th>Prefix </th><th>Operates on </th></tr>
<tr>
<td><a class="el" href="group___h5_a.html">Attributes (H5A)</a> </td><td>Attributes </td></tr>
<tr>
<td><a class="el" href="group___h5_d.html">Datasets (H5D)</a> </td><td>Datasets </td></tr>
<tr>
<td><a class="el" href="group___h5_e.html">Error Handling (H5E)</a> </td><td>Error reports </td></tr>
<tr>
<td><a class="el" href="group___h5_f.html">Files (H5F)</a> </td><td>Files </td></tr>
<tr>
<td><a class="el" href="group___h5_g.html">Groups (H5G)</a> </td><td>Groups </td></tr>
<tr>
<td><a class="el" href="group___h5_i.html">Identifiers (H5I)</a> </td><td>Identifiers </td></tr>
<tr>
<td><a class="el" href="group___h5_l.html">Links (H5L)</a> </td><td>Links </td></tr>
<tr>
<td><a class="el" href="group___h5_o.html">Objects (H5O)</a> </td><td>Objects </td></tr>
<tr>
<td><a class="el" href="group___h5_p.html">Property Lists (H5P)</a> </td><td>Property lists </td></tr>
<tr>
<td><a class="el" href="group___h5_r.html">References (H5R)</a> </td><td>References </td></tr>
<tr>
<td><a class="el" href="group___h5_s.html">Dataspaces (H5S)</a> </td><td>Dataspaces </td></tr>
<tr>
<td><a class="el" href="group___h5_t.html">Datatypes (H5T)</a> </td><td>Datatypes </td></tr>
<tr>
<td><a class="el" href="group___h5_z.html">Filters (H5Z)</a> </td><td>Filters </td></tr>
</table>
<h2><a class="anchor" id="subsec_program_model"></a>
The HDF5 Programming Model</h2>
<p>In this section we introduce the HDF5 programming model by means of a series of short code samples. These samples illustrate a broad selection of common HDF5 tasks. More details are provided in the following chapters and in the HDF5 Reference Manual.</p>
<h3><a class="anchor" id="subsubsec_program_model_create"></a>
Creating an HDF5 File</h3>
<p>Before an HDF5 file can be used or referred to in any manner, it must be explicitly created or opened. When the need for access to a file ends, the file must be closed. The example below provides a C code fragment illustrating these steps. In this example, the values for the file creation property list and the file access property list are set to the defaults <a class="el" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>.</p>
<p><em>Creating and closing an HDF5 file</em> </p><div class="fragment"><div class="line"><a class="code" href="_h5_ipublic_8h.html#a0045db7ff9c22ad35db6ae91662e1943">hid_t</a> file; <span class="comment">// declare file identifier</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create a new file using H5F_ACC_TRUNC to truncate and overwrite</span></div>
<div class="line"><span class="comment">// any file of the same name, default file creation properties, and</span></div>
<div class="line"><span class="comment">// default file access properties. Then close the file.</span></div>
<div class="line">file = <a class="code" href="group___h5_f.html#gae64b51ee9ac0781bc4ccc599d98387f4">H5Fcreate</a>(FILE, <a class="code" href="_h5_fpublic_8h.html#a5a2d6726f9ad8d2bca8df2b817e5ad6a">H5F_ACC_TRUNC</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line">status = <a class="code" href="group___h5_f.html#gac55cd91d80822e4f8c2a7f04ea71b124">H5Fclose</a>(file);</div>
<div class="ttc" id="a_h5_fpublic_8h_html_a5a2d6726f9ad8d2bca8df2b817e5ad6a"><div class="ttname"><a href="_h5_fpublic_8h.html#a5a2d6726f9ad8d2bca8df2b817e5ad6a">H5F_ACC_TRUNC</a></div><div class="ttdeci">#define H5F_ACC_TRUNC</div><div class="ttdef"><b>Definition:</b> H5Fpublic.h:50</div></div>
<div class="ttc" id="a_h5_ipublic_8h_html_a0045db7ff9c22ad35db6ae91662e1943"><div class="ttname"><a href="_h5_ipublic_8h.html#a0045db7ff9c22ad35db6ae91662e1943">hid_t</a></div><div class="ttdeci">int64_t hid_t</div><div class="ttdef"><b>Definition:</b> H5Ipublic.h:60</div></div>
<div class="ttc" id="a_h5_ppublic_8h_html_afa85e97bfbf9bf1c58e39263846c568f"><div class="ttname"><a href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a></div><div class="ttdeci">#define H5P_DEFAULT</div><div class="ttdef"><b>Definition:</b> H5Ppublic.h:102</div></div>
<div class="ttc" id="agroup___h5_f_html_gac55cd91d80822e4f8c2a7f04ea71b124"><div class="ttname"><a href="group___h5_f.html#gac55cd91d80822e4f8c2a7f04ea71b124">H5Fclose</a></div><div class="ttdeci">herr_t H5Fclose(hid_t file_id)</div><div class="ttdoc">Terminates access to an HDF5 file.</div></div>
<div class="ttc" id="agroup___h5_f_html_gae64b51ee9ac0781bc4ccc599d98387f4"><div class="ttname"><a href="group___h5_f.html#gae64b51ee9ac0781bc4ccc599d98387f4">H5Fcreate</a></div><div class="ttdeci">hid_t H5Fcreate(const char *filename, unsigned flags, hid_t fcpl_id, hid_t fapl_id)</div><div class="ttdoc">Creates an HDF5 file.</div></div>
</div><!-- fragment --><p>Note: If there is a possibility that a file of the declared name already exists and you wish to open a new file regardless of that possibility, the flag <a class="el" href="_h5_fpublic_8h.html#a5a2d6726f9ad8d2bca8df2b817e5ad6a">H5F_ACC_TRUNC</a> will cause the operation to overwrite the previous file. If the operation should fail in such a circumstance, use the flag <a class="el" href="_h5_fpublic_8h.html#a7a47250dc1435705233dca7297ba3d90">H5F_ACC_EXCL</a> instead.</p>
<h3><a class="anchor" id="subsubsec_program_model_dset"></a>
Creating and Initializing a Dataset</h3>
<p>The essential objects within a dataset are datatype and dataspace. These are independent objects and are created separately from any dataset to which they may be attached. Hence, creating a dataset requires, at a minimum, the following steps: </p><ol>
<li>
Create and initialize a dataspace for the dataset </li>
<li>
Define a datatype for the dataset </li>
<li>
Create and initialize the dataset</li>
</ol>
<p>The code in the example below illustrates the execution of these steps.</p>
<p><em>Create a dataset</em> </p><div class="fragment"><div class="line"><a class="code" href="_h5_ipublic_8h.html#a0045db7ff9c22ad35db6ae91662e1943">hid_t</a> dataset, datatype, dataspace; <span class="comment">// declare identifiers</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create a dataspace: Describe the size of the array and</span></div>
<div class="line"><span class="comment">// create the dataspace for a fixed-size dataset.</span></div>
<div class="line">dimsf[0] = NX;</div>
<div class="line">dimsf[1] = NY;</div>
<div class="line">dataspace = <a class="code" href="group___h5_s.html#ga8e35eea5738b4805856eac7d595254ae">H5Screate_simple</a>(RANK, dimsf, NULL);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Define a datatype for the data in the dataset.</span></div>
<div class="line"><span class="comment">// We will store little endian integers.</span></div>
<div class="line">datatype = <a class="code" href="group___h5_t.html#gaec07efbab84f4e5b4ed22f010786be8e">H5Tcopy</a>(<a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>);</div>
<div class="line">status = <a class="code" href="group___a_t_o_m.html#gab1aab76b1214a819281f2156c6d45d71">H5Tset_order</a>(datatype, <a class="code" href="_h5_tpublic_8h.html#a2a6a8eb856a0829fecaac60f803c9fd0ae5668f73f6c28feddb7af175ac53012d">H5T_ORDER_LE</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create a new dataset within the file using the defined</span></div>
<div class="line"><span class="comment">// dataspace and datatype and default dataset creation</span></div>
<div class="line"><span class="comment">// properties.</span></div>
<div class="line"><span class="comment">// NOTE: H5T_NATIVE_INT can be used as the datatype if</span></div>
<div class="line"><span class="comment">// conversion to little endian is not needed.</span></div>
<div class="line">dataset = <a class="code" href="group___h5_d.html#ga0647ba4bbd26d5230cc07f3a5685b2cf">H5Dcreate</a>(file, DATASETNAME, datatype, dataspace, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="ttc" id="a_h5_tpublic_8h_html_a2a6a8eb856a0829fecaac60f803c9fd0ae5668f73f6c28feddb7af175ac53012d"><div class="ttname"><a href="_h5_tpublic_8h.html#a2a6a8eb856a0829fecaac60f803c9fd0ae5668f73f6c28feddb7af175ac53012d">H5T_ORDER_LE</a></div><div class="ttdeci">@ H5T_ORDER_LE</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:54</div></div>
<div class="ttc" id="agroup___a_t_o_m_html_gab1aab76b1214a819281f2156c6d45d71"><div class="ttname"><a href="group___a_t_o_m.html#gab1aab76b1214a819281f2156c6d45d71">H5Tset_order</a></div><div class="ttdeci">herr_t H5Tset_order(hid_t type_id, H5T_order_t order)</div><div class="ttdoc">Sets the byte order of a datatype.</div></div>
<div class="ttc" id="agroup___h5_d_html_ga0647ba4bbd26d5230cc07f3a5685b2cf"><div class="ttname"><a href="group___h5_d.html#ga0647ba4bbd26d5230cc07f3a5685b2cf">H5Dcreate</a></div><div class="ttdeci">#define H5Dcreate</div><div class="ttdef"><b>Definition:</b> H5version.h:892</div></div>
<div class="ttc" id="agroup___h5_s_html_ga8e35eea5738b4805856eac7d595254ae"><div class="ttname"><a href="group___h5_s.html#ga8e35eea5738b4805856eac7d595254ae">H5Screate_simple</a></div><div class="ttdeci">hid_t H5Screate_simple(int rank, const hsize_t dims[], const hsize_t maxdims[])</div><div class="ttdoc">Creates a new simple dataspace and opens it for access.</div></div>
<div class="ttc" id="agroup___h5_t_html_gaec07efbab84f4e5b4ed22f010786be8e"><div class="ttname"><a href="group___h5_t.html#gaec07efbab84f4e5b4ed22f010786be8e">H5Tcopy</a></div><div class="ttdeci">hid_t H5Tcopy(hid_t type_id)</div><div class="ttdoc">Copies an existing datatype.</div></div>
<div class="ttc" id="agroup___p_d_t_n_a_t_html_ga3cf93ffc6782be68070ef8e00f219ec2"><div class="ttname"><a href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a></div><div class="ttdeci">#define H5T_NATIVE_INT</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:767</div></div>
</div><!-- fragment --><h3><a class="anchor" id="subsubsec_program_model_close"></a>
Closing an Object</h3>
<p>An application should close an object such as a datatype, dataspace, or dataset once the object is no longer needed. Since each is an independent object, each must be released (or closed) separately. This action is frequently referred to as releasing the object's identifier. The code in the example below closes the datatype, dataspace, and dataset that were created in the preceding section.</p>
<p><em>Close an object</em> </p><div class="fragment"><div class="line"><a class="code" href="group___h5_t.html#gafcba4db244f6a4d71e99c6e72b8678f0">H5Tclose</a>(datatype);</div>
<div class="line"><a class="code" href="group___h5_d.html#gae47c3f38db49db127faf221624c30609">H5Dclose</a>(dataset);</div>
<div class="line"><a class="code" href="group___h5_s.html#ga2b53128a39c8f104c1c9c2a91590fcc1">H5Sclose</a>(dataspace);</div>
<div class="ttc" id="agroup___h5_d_html_gae47c3f38db49db127faf221624c30609"><div class="ttname"><a href="group___h5_d.html#gae47c3f38db49db127faf221624c30609">H5Dclose</a></div><div class="ttdeci">herr_t H5Dclose(hid_t dset_id)</div><div class="ttdoc">Closes the specified dataset.</div></div>
<div class="ttc" id="agroup___h5_s_html_ga2b53128a39c8f104c1c9c2a91590fcc1"><div class="ttname"><a href="group___h5_s.html#ga2b53128a39c8f104c1c9c2a91590fcc1">H5Sclose</a></div><div class="ttdeci">herr_t H5Sclose(hid_t space_id)</div><div class="ttdoc">Releases and terminates access to a dataspace.</div></div>
<div class="ttc" id="agroup___h5_t_html_gafcba4db244f6a4d71e99c6e72b8678f0"><div class="ttname"><a href="group___h5_t.html#gafcba4db244f6a4d71e99c6e72b8678f0">H5Tclose</a></div><div class="ttdeci">herr_t H5Tclose(hid_t type_id)</div><div class="ttdoc">Releases a datatype.</div></div>
</div><!-- fragment --><p>There is a long list of HDF5 library items that return a unique identifier when the item is created or opened. Each time that one of these items is opened, a unique identifier is returned. Closing a file does not mean that the groups, datasets, or other open items are also closed. Each opened item must be closed separately.</p>
<p>For more information, </p><dl class="section see"><dt>See also</dt><dd><a href="http://www.hdfgroup.org/HDF5/doc/Advanced/UsingIdentifiers/index.html">Using Identifiers</a> in the HDF5 Application Developer's Guide under General Topics in HDF5.</dd></dl>
<h4>How Closing a File Effects Other Open Structural Elements</h4>
<p>Every structural element in an HDF5 file can be opened, and these elements can be opened more than once. Elements range in size from the entire file down to attributes. When an element is opened, the HDF5 library returns a unique identifier to the application. Every element that is opened must be closed. If an element was opened more than once, each identifier that was returned to the application must be closed. For example, if a dataset was opened twice, both dataset identifiers must be released (closed) before the dataset can be considered closed. Suppose an application has opened a file, a group in the file, and two datasets in the group. In order for the file to be totally closed, the file, group, and datasets must each be closed. Closing the file before the group or the datasets will not affect the state of the group or datasets: the group and datasets will still be open.</p>
<p>There are several exceptions to the above general rule. One is when the <a class="el" href="group___h5.html#ga8a9fe81dcf66972ed75ea481e7750574" title="Flushes all data to disk, closes all open objects, and releases memory.">H5close</a> function is used. <a class="el" href="group___h5.html#ga8a9fe81dcf66972ed75ea481e7750574" title="Flushes all data to disk, closes all open objects, and releases memory.">H5close</a> causes a general shutdown of the library: all data is written to disk, all identifiers are closed, and all memory used by the library is cleaned up. Another exception occurs on parallel processing systems. Suppose on a parallel system an application has opened a file, a group in the file, and two datasets in the group. If the application uses the <a class="el" href="group___h5_f.html#gac55cd91d80822e4f8c2a7f04ea71b124" title="Terminates access to an HDF5 file.">H5Fclose</a> function to close the file, the call will fail with an error. The open group and datasets must be closed before the file can be closed. A third exception is when the file access property list includes the property <a class="el" href="_h5_fpublic_8h.html#aa85fa00d037d2b0401cf72edf9a6475fae6af53249bfe320745828497f28b6390">H5F_CLOSE_STRONG</a>. This property closes any open elements when the file is closed with <a class="el" href="group___h5_f.html#gac55cd91d80822e4f8c2a7f04ea71b124" title="Terminates access to an HDF5 file.">H5Fclose</a>. For more information, see the <a class="el" href="group___f_a_p_l.html#ga60e3567f677fd3ade75b909b636d7b9c" title="Sets the file close degree.">H5Pset_fclose_degree</a> function in the HDF5 Reference Manual.</p>
<h3><a class="anchor" id="subsubsec_program_model_data"></a>
Writing or Reading a Dataset to or from a File</h3>
<p>Having created the dataset, the actual data can be written with a call to <a class="el" href="group___h5_d.html#ga98f44998b67587662af8b0d8a0a75906" title="Writes raw data from a buffer to a dataset.">H5Dwrite</a>. See the example below.</p>
<p><em>Writing a dataset</em> </p><div class="fragment"><div class="line"><span class="comment">// Write the data to the dataset using default transfer properties.</span></div>
<div class="line">status = <a class="code" href="group___h5_d.html#ga98f44998b67587662af8b0d8a0a75906">H5Dwrite</a>(dataset, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, <a class="code" href="_h5_spublic_8h.html#a5f96eeee84b987f18470737f85af0484">H5S_ALL</a>, <a class="code" href="_h5_spublic_8h.html#a5f96eeee84b987f18470737f85af0484">H5S_ALL</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, data);</div>
<div class="ttc" id="a_h5_spublic_8h_html_a5f96eeee84b987f18470737f85af0484"><div class="ttname"><a href="_h5_spublic_8h.html#a5f96eeee84b987f18470737f85af0484">H5S_ALL</a></div><div class="ttdeci">#define H5S_ALL</div><div class="ttdef"><b>Definition:</b> H5Spublic.h:32</div></div>
<div class="ttc" id="agroup___h5_d_html_ga98f44998b67587662af8b0d8a0a75906"><div class="ttname"><a href="group___h5_d.html#ga98f44998b67587662af8b0d8a0a75906">H5Dwrite</a></div><div class="ttdeci">herr_t H5Dwrite(hid_t dset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t dxpl_id, const void *buf)</div><div class="ttdoc">Writes raw data from a buffer to a dataset.</div></div>
</div><!-- fragment --><p>Note that the third and fourth <a class="el" href="group___h5_d.html#ga98f44998b67587662af8b0d8a0a75906" title="Writes raw data from a buffer to a dataset.">H5Dwrite</a> parameters in the above example describe the dataspaces in memory and in the file, respectively. For now, these are both set to <a class="el" href="_h5_spublic_8h.html#a5f96eeee84b987f18470737f85af0484">H5S_ALL</a> which indicates that the entire dataset is to be written. The selection of partial datasets and the use of differing dataspaces in memory and in storage will be discussed later in this chapter and in more detail elsewhere in this guide.</p>
<p>Reading the dataset from storage is similar to writing the dataset to storage. To read an entire dataset, substitute <a class="el" href="group___h5_d.html#ga8287d5a7be7b8e55ffeff68f7d26811c" title="Reads raw data from a dataset into a provided buffer.">H5Dread</a> for <a class="el" href="group___h5_d.html#ga98f44998b67587662af8b0d8a0a75906" title="Writes raw data from a buffer to a dataset.">H5Dwrite</a> in the above example.</p>
<h3><a class="anchor" id="subsubsec_program_model_partial"></a>
Reading and Writing a Portion of a Dataset</h3>
<p>The previous section described writing or reading an entire dataset. HDF5 also supports access to portions of a dataset. These parts of datasets are known as selections.</p>
<p>The simplest type of selection is a simple hyperslab. This is an n-dimensional rectangular sub-set of a dataset where n is equal to the dataset's rank. Other available selections include a more complex hyperslab with user-defined stride and block size, a list of independent points, or the union of any of these.</p>
<p>The figure below shows several sample selections.</p>
<table class="doxtable">
<caption align="top">Dataset selections</caption>
<tr>
<td><div class="image">
<img src="Pmodel_fig5_a.gif" alt=""/>
</div>
</td></tr>
<tr>
<td><div class="image">
<img src="Pmodel_fig5_b.gif" alt=""/>
</div>
</td></tr>
<tr>
<td><div class="image">
<img src="Pmodel_fig5_c.gif" alt=""/>
</div>
</td></tr>
<tr>
<td><div class="image">
<img src="Pmodel_fig5_d.gif" alt=""/>
</div>
<div class="image">
<img src="Pmodel_fig5_e.gif" alt=""/>
</div>
</td></tr>
</table>
<p>Note: In the figure above, selections can take the form of a simple hyperslab, a hyperslab with user-defined stride and block, a selection of points, or a union of any of these forms.</p>
<p>Selections and hyperslabs are portions of a dataset. As described above, a simple hyperslab is a rectangular array of data elements with the same rank as the dataset's dataspace. Thus, a simple hyperslab is a logically contiguous collection of points within the dataset.</p>
<p>The more general case of a hyperslab can also be a regular pattern of points or blocks within the dataspace. Four parameters are required to describe a general hyperslab: the starting coordinates, the block size, the stride or space between blocks, and the number of blocks. These parameters are each expressed as a one-dimensional array with length equal to the rank of the dataspace and are described in the table below.</p>
<table class="doxtable">
<caption></caption>
<tr>
<th>Parameter </th><th>Definition </th></tr>
<tr>
<td>start </td><td>The coordinates of the starting location of the hyperslab in the dataset's dataspace. </td></tr>
<tr>
<td>block </td><td>The size of each block to be selected from the dataspace. If the block parameter is set to NULL, the block size defaults to a single element in each dimension, as if the block array was set to all 1s (all ones). This will result in the selection of a uniformly spaced set of count points starting at start and on the interval defined by stride. </td></tr>
<tr>
<td>stride </td><td>The number of elements separating the starting point of each element or block to be selected. If the stride parameter is set to NULL, the stride size defaults to 1 (one) in each dimension and no elements are skipped. </td></tr>
<tr>
<td>count </td><td>The number of elements or blocks to select along each dimension. </td></tr>
</table>
<h4>Reading Data into a Differently Shaped Memory Block</h4>
<p>For maximum flexibility in user applications, a selection in storage can be mapped into a differently-shaped selection in memory. All that is required is that the two selections contain the same number of data elements. In this example, we will first define the selection to be read from the dataset in storage, and then we will define the selection as it will appear in application memory.</p>
<p>Suppose we want to read a 3 x 4 hyperslab from a two-dimensional dataset in a file beginning at the dataset element <1,2>. The first task is to create the dataspace that describes the overall rank and dimensions of the dataset in the file and to specify the position and size of the in-file hyperslab that we are extracting from that dataset. See the code below.</p>
<p><em>Define the selection to be read from storage</em> </p><div class="fragment"><div class="line"><span class="comment">// Define dataset dataspace in file.</span></div>
<div class="line">dataspace = <a class="code" href="group___h5_d.html#gad42a46be153d895d8c28a11ebf5a0d0a">H5Dget_space</a>(dataset); <span class="comment">// dataspace identifier</span></div>
<div class="line">rank = <a class="code" href="group___h5_s.html#gae5282a81692b80b5b19dd12d05b9b28e">H5Sget_simple_extent_ndims</a>(dataspace);</div>
<div class="line"> </div>
<div class="line">status_n = <a class="code" href="group___h5_s.html#gac494409b615d8e67c5edd9eb2848b2f3">H5Sget_simple_extent_dims</a>(dataspace, dims_out, NULL);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Define hyperslab in the dataset.</span></div>
<div class="line">offset[0] = 1;</div>
<div class="line">offset[1] = 2;</div>
<div class="line">count[0] = 3;</div>
<div class="line">count[1] = 4;</div>
<div class="line">status = <a class="code" href="group___h5_s.html#ga6adfdf1b95dc108a65bf66e97d38536d">H5Sselect_hyperslab</a>(dataspace, <a class="code" href="_h5_spublic_8h.html#a10093bab27cc5720efdab3186993da0fab90faf3dc59ecf6f28197ef471141550">H5S_SELECT_SET</a>, offset, NULL, count, NULL);</div>
<div class="ttc" id="a_h5_spublic_8h_html_a10093bab27cc5720efdab3186993da0fab90faf3dc59ecf6f28197ef471141550"><div class="ttname"><a href="_h5_spublic_8h.html#a10093bab27cc5720efdab3186993da0fab90faf3dc59ecf6f28197ef471141550">H5S_SELECT_SET</a></div><div class="ttdeci">@ H5S_SELECT_SET</div><div class="ttdef"><b>Definition:</b> H5Spublic.h:87</div></div>
<div class="ttc" id="agroup___h5_d_html_gad42a46be153d895d8c28a11ebf5a0d0a"><div class="ttname"><a href="group___h5_d.html#gad42a46be153d895d8c28a11ebf5a0d0a">H5Dget_space</a></div><div class="ttdeci">hid_t H5Dget_space(hid_t dset_id)</div><div class="ttdoc">Returns an identifier for a copy of the dataspace for a dataset.</div></div>
<div class="ttc" id="agroup___h5_s_html_ga6adfdf1b95dc108a65bf66e97d38536d"><div class="ttname"><a href="group___h5_s.html#ga6adfdf1b95dc108a65bf66e97d38536d">H5Sselect_hyperslab</a></div><div class="ttdeci">herr_t H5Sselect_hyperslab(hid_t space_id, H5S_seloper_t op, const hsize_t start[], const hsize_t stride[], const hsize_t count[], const hsize_t block[])</div><div class="ttdoc">Selects a hyperslab region to add to the current selected region.</div></div>
<div class="ttc" id="agroup___h5_s_html_gac494409b615d8e67c5edd9eb2848b2f3"><div class="ttname"><a href="group___h5_s.html#gac494409b615d8e67c5edd9eb2848b2f3">H5Sget_simple_extent_dims</a></div><div class="ttdeci">int H5Sget_simple_extent_dims(hid_t space_id, hsize_t dims[], hsize_t maxdims[])</div><div class="ttdoc">Retrieves dataspace dimension size and maximum size.</div></div>
<div class="ttc" id="agroup___h5_s_html_gae5282a81692b80b5b19dd12d05b9b28e"><div class="ttname"><a href="group___h5_s.html#gae5282a81692b80b5b19dd12d05b9b28e">H5Sget_simple_extent_ndims</a></div><div class="ttdeci">int H5Sget_simple_extent_ndims(hid_t space_id)</div><div class="ttdoc">Determines the dimensionality of a dataspace.</div></div>
</div><!-- fragment --><p>The next task is to define a dataspace in memory. Suppose that we have in memory a three-dimensional 7 x 7 x 3 array into which we wish to read the two-dimensional 3 x 4 hyperslab described above and that we want the memory selection to begin at the element <3,0,0> and reside in the plane of the first two dimensions of the array. Since the in-memory dataspace is three-dimensional, we have to describe the in-memory selection as three-dimensional. Since we are keeping the selection in the plane of the first two dimensions of the in-memory dataset, the in-memory selection will be a 3 x 4 x 1 array defined as <3,4,1>.</p>
<p>Notice that we must describe two things: the dimensions of the in-memory array, and the size and position of the hyperslab that we wish to read in. The code below illustrates how this would be done.</p>
<p><em>Define the memory dataspace and selection</em> </p><div class="fragment"><div class="line"><span class="comment">// Define memory dataspace.</span></div>
<div class="line">dimsm[0] = 7;</div>
<div class="line">dimsm[1] = 7;</div>
<div class="line">dimsm[2] = 3;</div>
<div class="line">memspace = <a class="code" href="group___h5_s.html#ga8e35eea5738b4805856eac7d595254ae">H5Screate_simple</a>(RANK_OUT,dimsm,NULL);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Define memory hyperslab.</span></div>
<div class="line">offset_out[0] = 3;</div>
<div class="line">offset_out[1] = 0;</div>
<div class="line">offset_out[2] = 0;</div>
<div class="line">count_out[0] = 3;</div>
<div class="line">count_out[1] = 4;</div>
<div class="line">count_out[2] = 1;</div>
<div class="line">status = <a class="code" href="group___h5_s.html#ga6adfdf1b95dc108a65bf66e97d38536d">H5Sselect_hyperslab</a>(memspace, <a class="code" href="_h5_spublic_8h.html#a10093bab27cc5720efdab3186993da0fab90faf3dc59ecf6f28197ef471141550">H5S_SELECT_SET</a>, offset_out, NULL, count_out, NULL);</div>
</div><!-- fragment --><p>The hyperslab defined in the code above has the following parameters: start=(3,0,0), count=(3,4,1), stride and block size are NULL.</p>
<h4>Writing Data into a Differently Shaped Disk Storage Block</h4>
<p>Now let's consider the opposite process of writing a selection from memory to a selection in a dataset in a file. Suppose that the source dataspace in memory is a 50-element, one-dimensional array called vector and that the source selection is a 48-element simple hyperslab that starts at the second element of vector. See the figure below.</p>
<table class="doxtable">
<tr>
<td><div class="image">
<img src="Pmodel_fig2.gif" alt=""/>
<div class="caption">
A one-dimensional array</div></div>
</td></tr>
</table>
<p>Further suppose that we wish to write this data to the file as a series of 3 x 2-element blocks in a two-dimensional dataset, skipping one row and one column between blocks. Since the source selection contains 48 data elements and each block in the destination selection contains 6 data elements, we must define the destination selection with 8 blocks. We will write 2 blocks in the first dimension and 4 in the second. The code below shows how to achieve this objective.</p>
<p><em>The destination selection</em> </p><div class="fragment"><div class="line"><span class="comment">// Select the hyperslab for the dataset in the file, using</span></div>
<div class="line"><span class="comment">// 3 x 2 blocks, a (4,3) stride, a (2,4) count, and starting</span></div>
<div class="line"><span class="comment">// at the position (0,1).</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">ret = <a class="code" href="group___h5_s.html#ga6adfdf1b95dc108a65bf66e97d38536d">H5Sselect_hyperslab</a>(fid, <a class="code" href="_h5_spublic_8h.html#a10093bab27cc5720efdab3186993da0fab90faf3dc59ecf6f28197ef471141550">H5S_SELECT_SET</a>, start, stride, count, block);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create dataspace for the first dataset.</span></div>
<div class="line">mid1 = <a class="code" href="group___h5_s.html#ga8e35eea5738b4805856eac7d595254ae">H5Screate_simple</a>(MSPACE1_RANK, dim1, NULL);</div>
<div class="line"> </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">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">ret = <a class="code" href="group___h5_s.html#ga6adfdf1b95dc108a65bf66e97d38536d">H5Sselect_hyperslab</a>(mid1, <a class="code" href="_h5_spublic_8h.html#a10093bab27cc5720efdab3186993da0fab90faf3dc59ecf6f28197ef471141550">H5S_SELECT_SET</a>, start, stride, count, block);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Write selection from the vector buffer to the dataset in the file.</span></div>
<div class="line">ret = <a class="code" href="group___h5_d.html#ga98f44998b67587662af8b0d8a0a75906">H5Dwrite</a>(dataset, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, mid1, fid, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, vector);</div>
</div><!-- fragment --><h3><a class="anchor" id="subsubsec_program_model_info"></a>
Getting Information about a Dataset</h3>
<p>Although reading is analogous to writing, it is often first necessary to query a file to obtain information about the dataset to be read. For instance, we often need to determine the datatype associated with a dataset, or its dataspace (in other words, rank and dimensions). As illustrated in the code example below, there are several get routines for obtaining this information.</p>
<p><em>Routines to get dataset parameters</em> </p><div class="fragment"><div class="line"><span class="comment">// Get datatype and dataspace identifiers,</span></div>
<div class="line"><span class="comment">// then query datatype class, order, and size, and</span></div>
<div class="line"><span class="comment">// then query dataspace rank and dimensions.</span></div>
<div class="line">datatype = <a class="code" href="group___h5_d.html#ga7cd04b8332e8a0939b9973fbc500cadb">H5Dget_type</a> (dataset); <span class="comment">// datatype identifier</span></div>
<div class="line"><span class="keyword">class </span>= H5Tget_class (datatype);</div>
<div class="line"><span class="keywordflow">if</span> (<span class="keyword">class</span> == <a class="code" href="_h5_tpublic_8h.html#a071841985f647f69516dbe77d93167f2aba1fc36abc23f073912e337d2291b037">H5T_INTEGER</a>)</div>
<div class="line"> printf(<span class="stringliteral">"Dataset has INTEGER type \n"</span>);</div>
<div class="line"> </div>
<div class="line">order = <a class="code" href="group___a_t_o_m.html#gaeb5bd7ec46787a4b6d33947dc73c2a5f">H5Tget_order</a> (datatype);</div>
<div class="line"><span class="keywordflow">if</span> (order == <a class="code" href="_h5_tpublic_8h.html#a2a6a8eb856a0829fecaac60f803c9fd0ae5668f73f6c28feddb7af175ac53012d">H5T_ORDER_LE</a>)</div>
<div class="line"> printf(<span class="stringliteral">"Little endian order \n"</span>);</div>
<div class="line"> </div>
<div class="line">size = <a class="code" href="group___h5_t.html#ga1b971589cd7a86f3e84affdee455564e">H5Tget_size</a> (datatype);</div>
<div class="line">printf (<span class="stringliteral">"Size is %d \n"</span>, size);</div>
<div class="line"> </div>
<div class="line">dataspace = <a class="code" href="group___h5_d.html#gad42a46be153d895d8c28a11ebf5a0d0a">H5Dget_space</a> (dataset); <span class="comment">// dataspace identifier</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Find rank and retrieve current and maximum dimension sizes.</span></div>
<div class="line">rank = <a class="code" href="group___h5_s.html#gac494409b615d8e67c5edd9eb2848b2f3">H5Sget_simple_extent_dims</a> (dataspace, dims, max_dims);</div>
<div class="ttc" id="a_h5_tpublic_8h_html_a071841985f647f69516dbe77d93167f2aba1fc36abc23f073912e337d2291b037"><div class="ttname"><a href="_h5_tpublic_8h.html#a071841985f647f69516dbe77d93167f2aba1fc36abc23f073912e337d2291b037">H5T_INTEGER</a></div><div class="ttdeci">@ H5T_INTEGER</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:32</div></div>
<div class="ttc" id="agroup___a_t_o_m_html_gaeb5bd7ec46787a4b6d33947dc73c2a5f"><div class="ttname"><a href="group___a_t_o_m.html#gaeb5bd7ec46787a4b6d33947dc73c2a5f">H5Tget_order</a></div><div class="ttdeci">H5T_order_t H5Tget_order(hid_t type_id)</div><div class="ttdoc">Returns the byte order of an atomic datatype.</div></div>
<div class="ttc" id="agroup___h5_d_html_ga7cd04b8332e8a0939b9973fbc500cadb"><div class="ttname"><a href="group___h5_d.html#ga7cd04b8332e8a0939b9973fbc500cadb">H5Dget_type</a></div><div class="ttdeci">hid_t H5Dget_type(hid_t dset_id)</div><div class="ttdoc">Returns an identifier for a copy of the datatype for a dataset.</div></div>
<div class="ttc" id="agroup___h5_t_html_ga1b971589cd7a86f3e84affdee455564e"><div class="ttname"><a href="group___h5_t.html#ga1b971589cd7a86f3e84affdee455564e">H5Tget_size</a></div><div class="ttdeci">size_t H5Tget_size(hid_t type_id)</div><div class="ttdoc">Returns the size of a datatype.</div></div>
</div><!-- fragment --><h3><a class="anchor" id="subsubsec_program_model_compound"></a>
Creating and Defining Compound Datatypes</h3>
<p>A compound datatype is a collection of one or more data elements. Each element might be an atomic type, a small array, or another compound datatype.</p>
<p>The provision for nested compound datatypes allows these structures to become quite complex. An HDF5 compound datatype has some similarities to a C struct or a Fortran common block. Though not originally designed with databases in mind, HDF5 compound datatypes are sometimes used in a way that is similar to a database record. Compound datatypes can become either a powerful tool or a complex and difficult-to-debug construct. Reasonable caution is advised.</p>
<p>To create and use a compound datatype, you need to create a datatype with class compound (<a class="el" href="_h5_tpublic_8h.html#a071841985f647f69516dbe77d93167f2a7a401c61604dc846dbd3f9eb6fcb0fe6">H5T_COMPOUND</a>) and specify the total size of the data element in bytes. A compound datatype consists of zero or more uniquely named members. Members can be defined in any order but must occupy non-overlapping regions within the datum. The table below lists the properties of compound datatype members.</p>
<table class="doxtable">
<caption></caption>
<tr>
<th>Parameter </th><th>Definition </th></tr>
<tr>
<td>Index </td><td>An index number between zero and N-1, where N is the number of members in the compound. The elements are indexed in the order of their location in the array of bytes. </td></tr>
<tr>
<td>Name </td><td>A string that must be unique within the members of the same datatype. </td></tr>
<tr>
<td>Datatype </td><td>An HDF5 datatype. </td></tr>
<tr>
<td>Offset </td><td>A fixed byte offset which defines the location of the first byte of that member in the compound datatype. </td></tr>
</table>
<p>Properties of the members of a compound datatype are defined when the member is added to the compound type. These properties cannot be modified later.</p>
<h4>Defining Compound Datatypes</h4>
<p>Compound datatypes must be built out of other datatypes. To do this, you first create an empty compound datatype and specify its total size. Members are then added to the compound datatype in any order.</p>
<p>Each member must have a descriptive name. This is the key used to uniquely identify the member within the compound datatype. A member name in an HDF5 datatype does not necessarily have to be the same as the name of the corresponding member in the C struct in memory although this is often the case. You also do not need to define all the members of the C struct in the HDF5 compound datatype (or vice versa).</p>
<p>Usually a C struct will be defined to hold a data point in memory, and the offsets of the members in memory will be the offsets of the struct members from the beginning of an instance of the struct. The library defines the macro that computes the offset of member m within a struct variable s: </p><div class="fragment"><div class="line"><a class="code" href="_h5_tpublic_8h.html#af5242159129a7f37ab85d33d85a1ccac">HOFFSET</a>(s,m)</div>
<div class="ttc" id="a_h5_tpublic_8h_html_af5242159129a7f37ab85d33d85a1ccac"><div class="ttname"><a href="_h5_tpublic_8h.html#af5242159129a7f37ab85d33d85a1ccac">HOFFSET</a></div><div class="ttdeci">#define HOFFSET(S, M)</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:22</div></div>
</div><!-- fragment --><p>The code below shows an example in which a compound datatype is created to describe complex numbers whose type is defined by the complex_t struct.</p>
<p><em>A compound datatype for complex numbers</em> </p><div class="fragment"><div class="line">Typedef <span class="keyword">struct </span>{</div>
<div class="line"> <span class="keywordtype">double</span> re; <span class="comment">//real part</span></div>
<div class="line"> <span class="keywordtype">double</span> im; <span class="comment">//imaginary part</span></div>
<div class="line">} complex_t;</div>
<div class="line"> </div>
<div class="line">complex_t tmp; <span class="comment">//used only to compute offsets</span></div>
<div class="line"><a class="code" href="_h5_ipublic_8h.html#a0045db7ff9c22ad35db6ae91662e1943">hid_t</a> complex_id = <a class="code" href="group___h5_t.html#gaa9afc38e1a7d35e4d0bec24c569b3c65">H5Tcreate</a> (<a class="code" href="_h5_tpublic_8h.html#a071841985f647f69516dbe77d93167f2a7a401c61604dc846dbd3f9eb6fcb0fe6">H5T_COMPOUND</a>, <span class="keyword">sizeof</span> tmp);</div>
<div class="line"><a class="code" href="group___c_o_m_p_o_u_n_d.html#ga487d8f64a76f48b6eeb7f402d3b8b081">H5Tinsert</a> (complex_id, <span class="stringliteral">"real"</span>, <a class="code" href="_h5_tpublic_8h.html#af5242159129a7f37ab85d33d85a1ccac">HOFFSET</a>(tmp,re), <a class="code" href="group___p_d_t_n_a_t.html#ga087f3b793a299e416bd68678c2ef7c09">H5T_NATIVE_DOUBLE</a>);</div>
<div class="line"><a class="code" href="group___c_o_m_p_o_u_n_d.html#ga487d8f64a76f48b6eeb7f402d3b8b081">H5Tinsert</a> (complex_id, <span class="stringliteral">"imaginary"</span>, <a class="code" href="_h5_tpublic_8h.html#af5242159129a7f37ab85d33d85a1ccac">HOFFSET</a>(tmp,im), <a class="code" href="group___p_d_t_n_a_t.html#ga087f3b793a299e416bd68678c2ef7c09">H5T_NATIVE_DOUBLE</a>);</div>
<div class="ttc" id="a_h5_tpublic_8h_html_a071841985f647f69516dbe77d93167f2a7a401c61604dc846dbd3f9eb6fcb0fe6"><div class="ttname"><a href="_h5_tpublic_8h.html#a071841985f647f69516dbe77d93167f2a7a401c61604dc846dbd3f9eb6fcb0fe6">H5T_COMPOUND</a></div><div class="ttdeci">@ H5T_COMPOUND</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:38</div></div>
<div class="ttc" id="agroup___c_o_m_p_o_u_n_d_html_ga487d8f64a76f48b6eeb7f402d3b8b081"><div class="ttname"><a href="group___c_o_m_p_o_u_n_d.html#ga487d8f64a76f48b6eeb7f402d3b8b081">H5Tinsert</a></div><div class="ttdeci">herr_t H5Tinsert(hid_t parent_id, const char *name, size_t offset, hid_t member_id)</div><div class="ttdoc">Adds a new member to a compound datatype.</div></div>
<div class="ttc" id="agroup___h5_t_html_gaa9afc38e1a7d35e4d0bec24c569b3c65"><div class="ttname"><a href="group___h5_t.html#gaa9afc38e1a7d35e4d0bec24c569b3c65">H5Tcreate</a></div><div class="ttdeci">hid_t H5Tcreate(H5T_class_t type, size_t size)</div><div class="ttdoc">Creates a new datatype.</div></div>
<div class="ttc" id="agroup___p_d_t_n_a_t_html_ga087f3b793a299e416bd68678c2ef7c09"><div class="ttname"><a href="group___p_d_t_n_a_t.html#ga087f3b793a299e416bd68678c2ef7c09">H5T_NATIVE_DOUBLE</a></div><div class="ttdeci">#define H5T_NATIVE_DOUBLE</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:802</div></div>
</div><!-- fragment --><h3><a class="anchor" id="subsubsec_program_model_extend"></a>
Creating and Writing Extendable Datasets</h3>
<p>An extendable dataset is one whose dimensions can grow. One can define an HDF5 dataset to have certain initial dimensions with the capacity to later increase the size of any of the initial dimensions. For example, the figure below shows a 3 x 3 dataset (a) which is later extended to be a 10 x 3 dataset by adding 7 rows (b), and further extended to be a 10 x 5 dataset by adding two columns (c).</p>
<table class="doxtable">
<tr>
<td><div class="image">
<img src="Pmodel_fig3.gif" alt=""/>
<div class="caption">
Extending a dataset</div></div>
</td></tr>
</table>
<p>HDF5 requires the use of chunking when defining extendable datasets. Chunking makes it possible to extend datasets efficiently without having to reorganize contiguous storage excessively.</p>
<p>To summarize, an extendable dataset requires two conditions: </p><ol>
<li>
Define the dataspace of the dataset as unlimited in all dimensions that might eventually be extended </li>
<li>
Enable chunking in the dataset creation properties</li>
</ol>
<p>For example, suppose we wish to create a dataset similar to the one shown in the figure above. We want to start with a 3 x 3 dataset, and then later we will extend it. To do this, go through the steps below.</p>
<p>First, declare the dataspace to have unlimited dimensions. See the code shown below. Note the use of the predefined constant <a class="el" href="_h5_spublic_8h.html#a5af9ab788797b2ea9a4843857674ac18">H5S_UNLIMITED</a> to specify that a dimension is unlimited.</p>
<p><em>Declaring a dataspace with unlimited dimensions</em> </p><div class="fragment"><div class="line"><span class="comment">// dataset dimensions at creation time</span></div>
<div class="line"><a class="code" href="_h5public_8h.html#a7f81cce70fb546af88da24d9285d3c1c">hsize_t</a> dims[2] = {3, 3};</div>
<div class="line"><a class="code" href="_h5public_8h.html#a7f81cce70fb546af88da24d9285d3c1c">hsize_t</a> maxdims[2] = {<a class="code" href="_h5_spublic_8h.html#a5af9ab788797b2ea9a4843857674ac18">H5S_UNLIMITED</a>, <a class="code" href="_h5_spublic_8h.html#a5af9ab788797b2ea9a4843857674ac18">H5S_UNLIMITED</a>};</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create the data space with unlimited dimensions.</span></div>
<div class="line">dataspace = <a class="code" href="group___h5_s.html#ga8e35eea5738b4805856eac7d595254ae">H5Screate_simple</a>(RANK, dims, maxdims);</div>
<div class="ttc" id="a_h5_spublic_8h_html_a5af9ab788797b2ea9a4843857674ac18"><div class="ttname"><a href="_h5_spublic_8h.html#a5af9ab788797b2ea9a4843857674ac18">H5S_UNLIMITED</a></div><div class="ttdeci">#define H5S_UNLIMITED</div><div class="ttdef"><b>Definition:</b> H5Spublic.h:48</div></div>
<div class="ttc" id="a_h5public_8h_html_a7f81cce70fb546af88da24d9285d3c1c"><div class="ttname"><a href="_h5public_8h.html#a7f81cce70fb546af88da24d9285d3c1c">hsize_t</a></div><div class="ttdeci">uint64_t hsize_t</div><div class="ttdef"><b>Definition:</b> H5public.h:297</div></div>
</div><!-- fragment --><p>Next, set the dataset creation property list to enable chunking. See the code below.</p>
<p><em>Enable chunking</em> </p><div class="fragment"><div class="line"><a class="code" href="_h5_ipublic_8h.html#a0045db7ff9c22ad35db6ae91662e1943">hid_t</a> cparms;</div>
<div class="line"><a class="code" href="_h5public_8h.html#a7f81cce70fb546af88da24d9285d3c1c">hsize_t</a> chunk_dims[2] ={2, 5};</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Modify dataset creation properties to enable chunking.</span></div>
<div class="line">cparms = <a class="code" href="group___p_l_c_r.html#gaf1b11da01d4d45d788c45f8bc5f0cbfa">H5Pcreate</a> (<a class="code" href="_h5_ppublic_8h.html#afcd7f8186c404f3a1d768632eacba102">H5P_DATASET_CREATE</a>);</div>
<div class="line">status = <a class="code" href="group___d_c_p_l.html#ga3584d592e377da3604b7604e266dcf5b">H5Pset_chunk</a>(cparms, RANK, chunk_dims);</div>
<div class="ttc" id="a_h5_ppublic_8h_html_afcd7f8186c404f3a1d768632eacba102"><div class="ttname"><a href="_h5_ppublic_8h.html#afcd7f8186c404f3a1d768632eacba102">H5P_DATASET_CREATE</a></div><div class="ttdeci">#define H5P_DATASET_CREATE</div><div class="ttdef"><b>Definition:</b> H5Ppublic.h:53</div></div>
<div class="ttc" id="agroup___d_c_p_l_html_ga3584d592e377da3604b7604e266dcf5b"><div class="ttname"><a href="group___d_c_p_l.html#ga3584d592e377da3604b7604e266dcf5b">H5Pset_chunk</a></div><div class="ttdeci">herr_t H5Pset_chunk(hid_t plist_id, int ndims, const hsize_t dim[])</div><div class="ttdoc">Sets the size of the chunks used to store a chunked layout dataset.</div></div>
<div class="ttc" id="agroup___p_l_c_r_html_gaf1b11da01d4d45d788c45f8bc5f0cbfa"><div class="ttname"><a href="group___p_l_c_r.html#gaf1b11da01d4d45d788c45f8bc5f0cbfa">H5Pcreate</a></div><div class="ttdeci">hid_t H5Pcreate(hid_t cls_id)</div><div class="ttdoc">Creates a new property list as an instance of a property list class.</div></div>
</div><!-- fragment --><p>The next step is to create the dataset. See the code below.</p>
<p><em>Create a dataset</em> </p><div class="fragment"><div class="line"><span class="comment">// Create a new dataset within the file using cparms creation properties.</span></div>
<div class="line">dataset = <a class="code" href="group___h5_d.html#ga0647ba4bbd26d5230cc07f3a5685b2cf">H5Dcreate</a>(file, DATASETNAME, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, dataspace, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, cparms, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
</div><!-- fragment --><p>Finally, when the time comes to extend the size of the dataset, invoke <a class="el" href="group___h5_d.html#gac4c0ff57977b1f39c1055296e39cbe91" title="Extends a dataset.">H5Dextend</a>. Extending the dataset along the first dimension by seven rows leaves the dataset with new dimensions of <10,3>. See the code below.</p>
<p><em>Extend the dataset by seven rows</em> </p><div class="fragment"><div class="line"><span class="comment">// Extend the dataset. Dataset becomes 10 x 3.</span></div>
<div class="line">dims[0] = dims[0] + 7;</div>
<div class="line">size[0] = dims[0];</div>
<div class="line">size[1] = dims[1];</div>
<div class="line"> </div>
<div class="line">status = <a class="code" href="group___h5_d.html#gac4c0ff57977b1f39c1055296e39cbe91">H5Dextend</a> (dataset, size);</div>
<div class="ttc" id="agroup___h5_d_html_gac4c0ff57977b1f39c1055296e39cbe91"><div class="ttname"><a href="group___h5_d.html#gac4c0ff57977b1f39c1055296e39cbe91">H5Dextend</a></div><div class="ttdeci">herr_t H5Dextend(hid_t dset_id, const hsize_t size[])</div><div class="ttdoc">Extends a dataset.</div></div>
</div><!-- fragment --><h3><a class="anchor" id="subsubsec_program_model_group"></a>
Creating and Working with Groups</h3>
<p>Groups provide a mechanism for organizing meaningful and extendable sets of datasets within an HDF5 file. The <a class="el" href="group___h5_g.html">Groups (H5G)</a> API provides several routines for working with groups.</p>
<h4>Creating a Group</h4>
<p>With no datatype, dataspace, or storage layout to define, creating a group is considerably simpler than creating a dataset. For example, the following code creates a group called Data in the root group of file.</p>
<p><em> Create a group</em> </p><div class="fragment"><div class="line"><span class="comment">// Create a group in the file.</span></div>
<div class="line">grp = <a class="code" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a>(file, <span class="stringliteral">"/Data"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="ttc" id="agroup___h5_g_html_ga187cee27a9fc4f1a311eb19b0522c7b8"><div class="ttname"><a href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a></div><div class="ttdeci">#define H5Gcreate</div><div class="ttdef"><b>Definition:</b> H5version.h:997</div></div>
</div><!-- fragment --><p>A group may be created within another group by providing the absolute name of the group to the <a class="el" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a> function or by specifying its location. For example, to create the group Data_new in the group Data, you might use the sequence of calls shown below.</p>
<p><em>Create a group within a group</em> </p><div class="fragment"><div class="line"><span class="comment">// Create group "Data_new" in the group "Data" by specifying</span></div>
<div class="line"><span class="comment">// absolute name of the group.</span></div>
<div class="line">grp_new = <a class="code" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a>(file, <span class="stringliteral">"/Data/Data_new"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// or</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create group "Data_new" in the "Data" group.</span></div>
<div class="line">grp_new = <a class="code" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a>(grp, <span class="stringliteral">"Data_new"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
</div><!-- fragment --><p>This first parameter of <a class="el" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a> is a location identifier. file in the first example specifies only the file. <em>grp</em> in the second example specifies a particular group in a particular file. Note that in this instance, the group identifier <em>grp</em> is used as the first parameter in the <a class="el" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a> call so that the relative name of Data_new can be used.</p>
<p>The third parameter of <a class="el" href="group___h5_g.html#ga187cee27a9fc4f1a311eb19b0522c7b8">H5Gcreate</a> optionally specifies how much file space to reserve to store the names of objects that will be created in this group. If a non-positive value is supplied, the library provides a default size.</p>
<p>Use <a class="el" href="group___h5_g.html#ga8dbe20b390d2504f0bd3589ed8f4e221" title="Closes the specified group.">H5Gclose</a> to close the group and release the group identifier.</p>
<h4>Creating a Dataset within a Group</h4>
<p>As with groups, a dataset can be created in a particular group by specifying either its absolute name in the file or its relative name with respect to that group. The next code excerpt uses the absolute name.</p>
<p><em>Create a dataset within a group using a relative name</em> </p><div class="fragment"><div class="line"><span class="comment">// Create the dataset "Compressed_Data" in the group Data using</span></div>
<div class="line"><span class="comment">// the absolute name. The dataset creation property list is</span></div>
<div class="line"><span class="comment">// modified to use GZIP compression with the compression</span></div>
<div class="line"><span class="comment">// effort set to 6. Note that compression can be used only when</span></div>
<div class="line"><span class="comment">// the dataset is chunked.</span></div>
<div class="line">dims[0] = 1000;</div>
<div class="line">dims[1] = 20;</div>
<div class="line">cdims[0] = 20;</div>
<div class="line">cdims[1] = 20;</div>
<div class="line">dataspace = <a class="code" href="group___h5_s.html#ga8e35eea5738b4805856eac7d595254ae">H5Screate_simple</a>(RANK, dims, NULL);</div>
<div class="line"> </div>
<div class="line">plist = <a class="code" href="group___p_l_c_r.html#gaf1b11da01d4d45d788c45f8bc5f0cbfa">H5Pcreate</a>(<a class="code" href="_h5_ppublic_8h.html#afcd7f8186c404f3a1d768632eacba102">H5P_DATASET_CREATE</a>);</div>
<div class="line"><a class="code" href="group___d_c_p_l.html#ga3584d592e377da3604b7604e266dcf5b">H5Pset_chunk</a>(plist, 2, cdims);</div>
<div class="line"><a class="code" href="group___d_c_p_l.html#gaf1f569bfc54552bdb9317d2b63318a0d">H5Pset_deflate</a>(plist, 6);</div>
<div class="line"> </div>
<div class="line">dataset = <a class="code" href="group___h5_d.html#ga0647ba4bbd26d5230cc07f3a5685b2cf">H5Dcreate</a>(file, <span class="stringliteral">"/Data/Compressed_Data"</span>, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, dataspace, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>,</div>
<div class="line"> plist, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="ttc" id="agroup___d_c_p_l_html_gaf1f569bfc54552bdb9317d2b63318a0d"><div class="ttname"><a href="group___d_c_p_l.html#gaf1f569bfc54552bdb9317d2b63318a0d">H5Pset_deflate</a></div><div class="ttdeci">herr_t H5Pset_deflate(hid_t plist_id, unsigned level)</div><div class="ttdoc">Sets deflate (GNU gzip) compression method and compression level.</div></div>
</div><!-- fragment --><p>Alternatively, you can first obtain an identifier for the group in which the dataset is to be created, and then create the dataset with a relative name.</p>
<p><em>Create a dataset within a group using a relative name</em> </p><div class="fragment"><div class="line"> <span class="comment">// Open the group.</span></div>
<div class="line">grp = <a class="code" href="group___h5_g.html#ga3eca6807deff4f9e51fc5fe0befc2245">H5Gopen</a>(file, <span class="stringliteral">"Data"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create the dataset "Compressed_Data" in the "Data" group</span></div>
<div class="line"><span class="comment">// by providing a group identifier and a relative dataset</span></div>
<div class="line"><span class="comment">// name as parameters to the H5Dcreate function.</span></div>
<div class="line">dataset = <a class="code" href="group___h5_d.html#ga0647ba4bbd26d5230cc07f3a5685b2cf">H5Dcreate</a>(grp, <span class="stringliteral">"Compressed_Data"</span>, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, dataspace, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, plist, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="ttc" id="agroup___h5_g_html_ga3eca6807deff4f9e51fc5fe0befc2245"><div class="ttname"><a href="group___h5_g.html#ga3eca6807deff4f9e51fc5fe0befc2245">H5Gopen</a></div><div class="ttdeci">#define H5Gopen</div><div class="ttdef"><b>Definition:</b> H5version.h:1008</div></div>
</div><!-- fragment --><h4>Accessing an Object in a Group</h4>
<p>Any object in a group can be accessed by its absolute or relative name. The first code snippet below illustrates the use of the absolute name to access the dataset <em>Compressed_Data</em> in the group <em>Data</em> created in the examples above. The second code snippet illustrates the use of the relative name.</p>
<p><em>Accessing a group using its relative name</em> </p><div class="fragment"><div class="line"><span class="comment">// Open the dataset "Compressed_Data" in the "Data" group.</span></div>
<div class="line">dataset = <a class="code" href="_h5version_8h.html#a7dba2e5b2045f31c0932123ffb54f7a3">H5Dopen</a>(file, <span class="stringliteral">"/Data/Compressed_Data"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Open the group "data" in the file.</span></div>
<div class="line">grp = <a class="code" href="group___h5_g.html#ga3eca6807deff4f9e51fc5fe0befc2245">H5Gopen</a>(file, <span class="stringliteral">"Data"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Access the "Compressed_Data" dataset in the group.</span></div>
<div class="line">dataset = <a class="code" href="_h5version_8h.html#a7dba2e5b2045f31c0932123ffb54f7a3">H5Dopen</a>(grp, <span class="stringliteral">"Compressed_Data"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="ttc" id="a_h5version_8h_html_a7dba2e5b2045f31c0932123ffb54f7a3"><div class="ttname"><a href="_h5version_8h.html#a7dba2e5b2045f31c0932123ffb54f7a3">H5Dopen</a></div><div class="ttdeci">#define H5Dopen</div><div class="ttdef"><b>Definition:</b> H5version.h:903</div></div>
</div><!-- fragment --><h3><a class="anchor" id="subsubsec_program_model_attr"></a>
Working with Attributes</h3>
<p>An attribute is a small dataset that is attached to a normal dataset or group. Attributes share many of the characteristics of datasets, so the programming model for working with attributes is similar in many ways to the model for working with datasets. The primary differences are that an attribute must be attached to a dataset or a group and sub-setting operations cannot be performed on attributes.</p>
<p>To create an attribute belonging to a particular dataset or group, first create a dataspace for the attribute with the call to <a class="el" href="group___h5_s.html#gabee514327cba34ca9951b24fa14fb083" title="Creates a new dataspace of a specified type.">H5Screate</a>, and then create the attribute using <a class="el" href="group___h5_a.html#ga4a76e4e5ab6eb0fd2aa7990d38d55f24">H5Acreate</a>. For example, the code shown below creates an attribute called “Integer attribute” that is a member of a dataset whose identifier is dataset. The attribute identifier is attr2. <a class="el" href="group___h5_a.html#gab70871e205d57450c83efd9912be2b5c" title="Writes data to an attribute.">H5Awrite</a> then sets the value of the attribute of that of the integer variable point. <a class="el" href="group___h5_a.html#gaef4394b661e2c930879e9868e122bdda" title="Closes the specified attribute.">H5Aclose</a> then releases the attribute identifier.</p>
<p><em>Create an attribute</em> </p><div class="fragment"><div class="line"><span class="keywordtype">int</span> point = 1; <span class="comment">// Value of the scalar attribute</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create scalar attribute.</span></div>
<div class="line">aid2 = <a class="code" href="group___h5_s.html#gabee514327cba34ca9951b24fa14fb083">H5Screate</a>(<a class="code" href="_h5_spublic_8h.html#ae53f3c6a52563646fbac9ead8ecdbf0aaf6a34a2439db8aa7bb63ed0c4aaa5eb8">H5S_SCALAR</a>);</div>
<div class="line">attr2 = <a class="code" href="group___h5_a.html#ga4a76e4e5ab6eb0fd2aa7990d38d55f24">H5Acreate</a>(dataset, <span class="stringliteral">"Integer attribute"</span>, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, aid2, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Write scalar attribute.</span></div>
<div class="line">ret = <a class="code" href="group___h5_a.html#gab70871e205d57450c83efd9912be2b5c">H5Awrite</a>(attr2, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, &point);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Close attribute dataspace.</span></div>
<div class="line">ret = <a class="code" href="group___h5_s.html#ga2b53128a39c8f104c1c9c2a91590fcc1">H5Sclose</a>(aid2);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Close attribute.</span></div>
<div class="line">ret = <a class="code" href="group___h5_a.html#gaef4394b661e2c930879e9868e122bdda">H5Aclose</a>(attr2);</div>
<div class="ttc" id="a_h5_spublic_8h_html_ae53f3c6a52563646fbac9ead8ecdbf0aaf6a34a2439db8aa7bb63ed0c4aaa5eb8"><div class="ttname"><a href="_h5_spublic_8h.html#ae53f3c6a52563646fbac9ead8ecdbf0aaf6a34a2439db8aa7bb63ed0c4aaa5eb8">H5S_SCALAR</a></div><div class="ttdeci">@ H5S_SCALAR</div><div class="ttdef"><b>Definition:</b> H5Spublic.h:77</div></div>
<div class="ttc" id="agroup___h5_a_html_ga4a76e4e5ab6eb0fd2aa7990d38d55f24"><div class="ttname"><a href="group___h5_a.html#ga4a76e4e5ab6eb0fd2aa7990d38d55f24">H5Acreate</a></div><div class="ttdeci">#define H5Acreate</div><div class="ttdef"><b>Definition:</b> H5version.h:868</div></div>
<div class="ttc" id="agroup___h5_a_html_gab70871e205d57450c83efd9912be2b5c"><div class="ttname"><a href="group___h5_a.html#gab70871e205d57450c83efd9912be2b5c">H5Awrite</a></div><div class="ttdeci">herr_t H5Awrite(hid_t attr_id, hid_t type_id, const void *buf)</div><div class="ttdoc">Writes data to an attribute.</div></div>
<div class="ttc" id="agroup___h5_a_html_gaef4394b661e2c930879e9868e122bdda"><div class="ttname"><a href="group___h5_a.html#gaef4394b661e2c930879e9868e122bdda">H5Aclose</a></div><div class="ttdeci">herr_t H5Aclose(hid_t attr_id)</div><div class="ttdoc">Closes the specified attribute.</div></div>
<div class="ttc" id="agroup___h5_s_html_gabee514327cba34ca9951b24fa14fb083"><div class="ttname"><a href="group___h5_s.html#gabee514327cba34ca9951b24fa14fb083">H5Screate</a></div><div class="ttdeci">hid_t H5Screate(H5S_class_t type)</div><div class="ttdoc">Creates a new dataspace of a specified type.</div></div>
</div><!-- fragment --><p><em>Read a known attribute</em> </p><div class="fragment"><div class="line"><span class="comment">// Attach to the scalar attribute using attribute name, then</span></div>
<div class="line"><span class="comment">// read and display its value.</span></div>
<div class="line">attr = <a class="code" href="group___h5_a.html#gadb49a0b5b9798d2e944d877adba8ae10">H5Aopen_by_name</a>(file_id, dataset_name, <span class="stringliteral">"Integer attribute"</span>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line">ret = <a class="code" href="group___h5_a.html#gaacb27a997f7c98e8a833d0fd63b58f1c">H5Aread</a>(attr, <a class="code" href="group___p_d_t_n_a_t.html#ga3cf93ffc6782be68070ef8e00f219ec2">H5T_NATIVE_INT</a>, &point_out);</div>
<div class="line">printf(<span class="stringliteral">"The value of the attribute \"Integer attribute\" is %d \n"</span>, point_out);</div>
<div class="line">ret = <a class="code" href="group___h5_a.html#gaef4394b661e2c930879e9868e122bdda">H5Aclose</a>(attr);</div>
<div class="ttc" id="agroup___h5_a_html_gaacb27a997f7c98e8a833d0fd63b58f1c"><div class="ttname"><a href="group___h5_a.html#gaacb27a997f7c98e8a833d0fd63b58f1c">H5Aread</a></div><div class="ttdeci">herr_t H5Aread(hid_t attr_id, hid_t type_id, void *buf)</div><div class="ttdoc">Reads the value of an attribute.</div></div>
<div class="ttc" id="agroup___h5_a_html_gadb49a0b5b9798d2e944d877adba8ae10"><div class="ttname"><a href="group___h5_a.html#gadb49a0b5b9798d2e944d877adba8ae10">H5Aopen_by_name</a></div><div class="ttdeci">hid_t H5Aopen_by_name(hid_t loc_id, const char *obj_name, const char *attr_name, hid_t aapl_id, hid_t lapl_id)</div><div class="ttdoc">Opens an attribute for an object by object name and attribute name.</div></div>
</div><!-- fragment --><p>To read a scalar attribute whose name and datatype are known, first open the attribute using <a class="el" href="group___h5_a.html#gadb49a0b5b9798d2e944d877adba8ae10" title="Opens an attribute for an object by object name and attribute name.">H5Aopen_by_name</a>, and then use <a class="el" href="group___h5_a.html#gaacb27a997f7c98e8a833d0fd63b58f1c" title="Reads the value of an attribute.">H5Aread</a> to get its value. For example, the code shown below reads a scalar attribute called “Integer attribute” whose datatype is a native integer and whose parent dataset has the identifier dataset.</p>
<p>To read an attribute whose characteristics are not known, go through these steps. First, query the file to obtain information about the attribute such as its name, datatype, rank, and dimensions, and then read the attribute. The following code opens an attribute by its index value using <a class="el" href="group___h5_a.html#gab1451cdff4f77dcf9feaee83c8179b2d" title="Opens the nth attribute attached to an object.">H5Aopen_by_idx</a>, and then it reads in information about the datatype with <a class="el" href="group___h5_a.html#gaacb27a997f7c98e8a833d0fd63b58f1c" title="Reads the value of an attribute.">H5Aread</a>.</p>
<p><em>Read an unknown attribute</em> </p><div class="fragment"><div class="line"><span class="comment">// Attach to the string attribute using its index, then read and</span></div>
<div class="line"><span class="comment">// display the value.</span></div>
<div class="line">attr = <a class="code" href="group___h5_a.html#gab1451cdff4f77dcf9feaee83c8179b2d">H5Aopen_by_idx</a>(file_id, dataset_name, index_type, iter_order, 2, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>, <a class="code" href="_h5_ppublic_8h.html#afa85e97bfbf9bf1c58e39263846c568f">H5P_DEFAULT</a>);</div>
<div class="line"> </div>
<div class="line">atype = <a class="code" href="group___h5_t.html#gaec07efbab84f4e5b4ed22f010786be8e">H5Tcopy</a>(<a class="code" href="group___p_d_t_s.html#ga7f6b6959fefe56d2e871659110936d2d">H5T_C_S1</a>);</div>
<div class="line"><a class="code" href="group___h5_t.html#gae5f38bfd4a4c557496b3194b5180212c">H5Tset_size</a>(atype, 4);</div>
<div class="line"> </div>
<div class="line">ret = <a class="code" href="group___h5_a.html#gaacb27a997f7c98e8a833d0fd63b58f1c">H5Aread</a>(attr, atype, string_out);</div>
<div class="line">printf(<span class="stringliteral">"The value of the attribute with the index 2 is %s \n"</span>, string_out);</div>
<div class="ttc" id="agroup___h5_a_html_gab1451cdff4f77dcf9feaee83c8179b2d"><div class="ttname"><a href="group___h5_a.html#gab1451cdff4f77dcf9feaee83c8179b2d">H5Aopen_by_idx</a></div><div class="ttdeci">hid_t H5Aopen_by_idx(hid_t loc_id, const char *obj_name, H5_index_t idx_type, H5_iter_order_t order, hsize_t n, hid_t aapl_id, hid_t lapl_id)</div><div class="ttdoc">Opens the nth attribute attached to an object.</div></div>
<div class="ttc" id="agroup___h5_t_html_gae5f38bfd4a4c557496b3194b5180212c"><div class="ttname"><a href="group___h5_t.html#gae5f38bfd4a4c557496b3194b5180212c">H5Tset_size</a></div><div class="ttdeci">herr_t H5Tset_size(hid_t type_id, size_t size)</div><div class="ttdoc">Sets size for a datatype.</div></div>
<div class="ttc" id="agroup___p_d_t_s_html_ga7f6b6959fefe56d2e871659110936d2d"><div class="ttname"><a href="group___p_d_t_s.html#ga7f6b6959fefe56d2e871659110936d2d">H5T_C_S1</a></div><div class="ttdeci">#define H5T_C_S1</div><div class="ttdef"><b>Definition:</b> H5Tpublic.h:476</div></div>
</div><!-- fragment --><p>In practice, if the characteristics of attributes are not known, the code involved in accessing and processing the attribute can be quite complex. For this reason, HDF5 includes a function called <a class="el" href="group___h5_a.html#gab9dcfc543cd4282f32b8ea19e08ffa6c">H5Aiterate</a>. This function applies a user-supplied function to each of a set of attributes. The user-supplied function can contain the code that interprets, accesses, and processes each attribute.</p>
<h2><a class="anchor" id="subsec_program_transfer_pipeline"></a>
The Data Transfer Pipeline</h2>
<p>The HDF5 library implements data transfers between different storage locations. At the lowest levels, the HDF5 Library reads and writes blocks of bytes to and from storage using calls to the virtual file layer (VFL) drivers. In addition to this, the HDF5 library manages caches of metadata and a data I/O pipeline. The data I/O pipeline applies compression to data blocks, transforms data elements, and implements selections.</p>
<p>A substantial portion of the HDF5 library's work is in transferring data from one environment or media to another. This most often involves a transfer between system memory and a storage medium. Data transfers are affected by compression, encryption, machine-dependent differences in numerical representation, and other features. So, the bit-by-bit arrangement of a given dataset is often substantially different in the two environments.</p>
<p>Consider the representation on disk of a compressed and encrypted little-endian array as compared to the same array after it has been read from disk, decrypted, decompressed, and loaded into memory on a big-endian system. HDF5 performs all of the operations necessary to make that transition during the I/O process with many of the operations being handled by the VFL and the data transfer pipeline.</p>
<p>The figure below provides a simplified view of a sample data transfer with four stages. Note that the modules are used only when needed. For example, if the data is not compressed, the compression stage is omitted.</p>
<table class="doxtable">
<tr>
<td><div class="image">
<img src="Pmodel_fig6.gif" alt=""/>
<div class="caption">
A data transfer from storage to memory</div></div>
</td></tr>
</table>
<p>For a given I/O request, different combinations of actions may be performed by the pipeline. The library automatically sets up the pipeline and passes data through the processing steps. For example, for a read request (from disk to memory), the library must determine which logical blocks contain the requested data elements and fetch each block into the library's cache. If the data needs to be decompressed, then the compression algorithm is applied to the block after it is read from disk. If the data is a selection, the selected elements are extracted from the data block after it is decompressed. If the data needs to be transformed (for example, byte swapped), then the data elements are transformed after decompression and selection.</p>
<p>While an application must sometimes set up some elements of the pipeline, use of the pipeline is normally transparent to the user program. The library determines what must be done based on the metadata for the file, the object, and the specific request. An example of when an application might be required to set up some elements in the pipeline is if the application used a custom error-checking algorithm.</p>
<p>In some cases, it is necessary to pass parameters to and from modules in the pipeline or among other parts of the library that are not directly called through the programming API. This is accomplished through the use of dataset transfer and data access property lists.</p>
<p>The VFL provides an interface whereby user applications can add custom modules to the data transfer pipeline. For example, a custom compression algorithm can be used with the HDF5 Library by linking an appropriate module into the pipeline through the VFL. This requires creating an appropriate wrapper for the compression module and registering it with the library with <a class="el" href="group___h5_z.html#ga93145acc38c2c60d832b7a9b0123706b" title="Registers a new filter with the HDF5 library.">H5Zregister</a>. The algorithm can then be applied to a dataset with an <a class="el" href="group___o_c_p_l.html#ga191c567ee50b2063979cdef156a768c5" title="Adds a filter to the filter pipeline.">H5Pset_filter</a> call which will add the algorithm to the selected dataset's transfer property list.</p>
<p>Previous Chapter <a class="el" href="_h5_d_m__u_g.html#sec_data_model">The HDF5 Data Model and File Structure</a> - Next Chapter <a class="el" href="_h5_f__u_g.html#sec_file">The HDF5 File</a> </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
</ul>
</div>
</body>
</html>
|