summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/rdwt.html
blob: 3b425572ebda191e21236714d9e8ea84a3051aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
<HTML><HEAD>
<TITLE>HDF5 Tutorial - Reading from and Writing to a Dataset
</TITLE> 
</HEAD>

<body bgcolor="#ffffff">

<!-- BEGIN MAIN BODY -->

<A HREF="http://www.ncsa.uiuc.edu/"><img border=0 
src="http://www.ncsa.uiuc.edu/Images/NCSAhome/footerlogo.gif"
width=78 height=27 alt="NCSA"><P></A>

 [ <A HREF="title.html"><I>HDF5 Tutorial Top</I></A> ]
<H1>
<BIG><BIG><BIG><FONT COLOR="#c101cd">Reading from and Writing to a Dataset</FONT>
</BIG></BIG></BIG></H1>

<hr noshade size=1>

<BODY>
<H2>Contents:</H2>
<UL>
 <LI><A HREF="#rdwr">Reading from and Writing to a Dataset</A>
 <LI> Programming Example 
      <UL>
       <LI> <A HREF="#desc">Description </A> 
       <LI> <A HREF="#rem">Remarks</A> 
       <LI> <A HREF="#fc">File Contents</A>
      </UL>
    </UL>
<HR>
<A NAME="rdwr">
<H2>Reading from and Writing to a Dataset</h2>
<P>
During a dataset I/O operation, the library transfers raw data between memory
and the file. The data in memory can have a datatype different from that of 
the file and can also be of a different size 
(i.e., the data in memory is a subset of the dataset elements, or vice versa).
Therefore, to perform read or write operations, the application
program must specify:
<UL>
  <LI> The dataset

  <LI> The dataset's datatype in memory

  <LI> The dataset's dataspace in memory

  <LI> The dataset's dataspace in the file

  <LI>The dataset transfer property list 
      (The dataset transfer property list controls various aspects of the 
      I/O operations, such as the number of processes participating in a
      collective I/O request or hints to the library to control caching of
      raw data. In this tutorial, we use the default dataset transfer 
      property list.)

  <LI> The data buffer
</UL>


<P>
The steps to read from or write to a dataset are
as follows:
<OL>
  <LI> Obtain the dataset identifier.
  <LI> Specify the memory datatype.
  <LI> Specify the memory dataspace.
  <LI> Specify the file dataspace.
  <LI> Specify the transfer properties.
  <LI> Perform the desired operation on the dataset.
  <LI> Close the dataset.
  <LI> Close the dataspace, datatype, and property list if necessary.
</OL>

To read from or write to a dataset, 
the <code>H5Dread</code>/<code>h5dread_f</code> and 
<code>H5Dwrite</code>/<code>h5dwrite_f</code>
routines are used. <P> 
<I>C</I>:
<PRE>
   status = H5Dread (set_id, mem_type_id, mem_space_id, file_space_id,
                     xfer_prp, buf );
   status = H5Dwrite (set_id, mem_type_id, mem_space_id, file_space_id,
                     xfer_prp, buf);

</PRE> 
<I>FORTRAN</I>:
<PRE>
   CALL h5dread_f(dset_id, mem_type_id, buf, error, &
                     mem_space_id=mspace_id, file_space_id=fspace_id, &
                     xfer_prp=xfer_plist_id)
        <font face=times><i>or</i></font>
   CALL h5dread_f(dset_id, mem_type_id, buf, error)


   CALL h5dwrite_f(dset_id, mem_type_id, buf, error, &
                     mem_space_id=mspace_id, file_space_id=fspace_id, &
                     xfer_prp=xfer_plist_id)
        <font face=times><i>or</i></font>
   CALL h5dwrite_f(dset_id, mem_type_id, buf, error)
</PRE>


<P>
<H2> Programming Example</H2>
<A NAME="desc">
<H3><U>Description</U></H3>      
The following example shows how to read and write an existing dataset.
It opens the file created in the previous example, obtains the dataset 
identifier for the dataset <code>/dset</code>, 
writes the dataset to the file, then reads the dataset back from 
memory. It then closes the dataset and file. <BR>
<UL>
[ <A HREF="examples/h5_rdwt.c">C Example</A> ] - <code>h5_rdwt.c</code> <BR>
[ <A HREF="examples/rwdsetexample.f90">FORTRAN Example</A> ] - <code>rwdsetexample.f90</code><BR>
[ <A HREF="examples/java/DatasetRdWt.java">Java Example</A> ] - <code>DatasetRdWt.java</code> <BR>
</UL>

<B>NOTE:</B> To download a tar file of the examples, including a Makefile,
please go to the <A HREF="references.html">References</A> page.

</PRE>
<A NAME="rem">
<H3><U>Remarks</U></H3>
<UL>
<LI><code>H5Fopen</code>/<code>h5fopen_f</code> opens an existing file and 
    returns a file identifier.
<PRE>
<I>C</I>:
  hid_t H5Fopen (const char *name, unsigned access_mode, hid_t access_prp) 

<I>FORTRAN</I>:
  h5fopen_f (name, access_mode, file_id, hdferr, access_prp)

        name         CHARACTER(LEN=*)
        access_mode  INTEGER
                     (Possible values: H5F_ACC_RDWR_F, H5F_ACC_RDONLY_F)
        file_id      INTEGER(HID_T)
        hdferr       INTEGER
                     (Possible values: 0 on success and -1 on failure)
        access_prp   INTEGER(HID_T), OPTIONAL

</PRE>
<UL>
   <LI> The argument <I>name</I> is the filename.
   <P>
   <LI> The <I>access_mode</I> parameter is the file access mode. 
        <code>H5F_ACC_RDWR</code> in C 
        (<code>H5F_ACC_RDWR_F</code> in FORTRAN) 
        allows read/write access 
        while <code>H5F_ACC_RDONLY</code> in C 
        (<code>H5F_ACC_RDONLY_F</code> in FORTRAN) 
        allows read-only access.

   <P>
   <LI> The <I>access_prp</I> parameter identifies the file access property list.
        <code>H5P_DEFAULT</code> in C and <code>H5P_DEFAULT_F</code> in FORTRAN
        specify the default file access property list.
        This parameter is optional in FORTRAN; if it is omitted, the default file
        access property list is used.

   <P>
   <LI>In FORTRAN, the return code is passed back in the <I>hdferr</I>
       parameter: 0 if successful, -1 if not.  In C, the function returns
       the file identifier if successful, and a negative value otherwise.
</UL>
<P>
<LI> <code>H5Dopen</code>/<code>h5dopen_f</code> opens an existing dataset 
     with the name specified by <i>name</i> at the location specified by 
     <i>loc_id</i>.
     For FORTRAN, the return value is passed in the <I>hdferr</I> parameter:
     0 if successful, -1 if not.  For C, the function returns the dataset
     identifier if successful, and a negative value if not.
 <P>
<I>C</I>:
<PRE>
  hid_t H5Dopen (hid_t loc_id, const char *name) 
</PRE>
<I>FORTRAN</I>:
<PRE>
  h5dopen_f(loc_id, name, hdferr) 

        loc_id   INTEGER(HID_T) 
        name     CHARACTER(LEN=*) 
        hdferr   INTEGER 
                 (Possible values: 0 on success and -1 on failure)
</PRE>

<P>
<LI><code>H5Dwrite</code>/<code>h5dwrite_f</code> writes raw data 
   from an application buffer to the specified
   dataset, converting from the datatype and dataspace of the dataset in
   memory to the datatype and dataspace of the dataset in the file.
<P>
<I>C</I>:
<PRE>
  herr_t H5Dwrite (hid_t dset_id, hid_t mem_type_id, hid_t mem_space_id, 
                   hid_t file_space_id, hid_t xfer_prp, const void * buf) 
</PRE>
<I>FORTRAN</I>:
<PRE>
  h5dwrite_f (dset_id, mem_type_id, buf, hdferr, mem_space_id, &
                                       file_space_id, xfer_prp)

        dset_id        INTEGER(HID_T)
        mem_type_id    INTEGER(HID_T)
        buf(*,...*)    TYPE
        hdferr         INTEGER 
                       (Possible values: 0 on success and -1 on failure)
        mem_space_id   INTEGER(HID_T), OPTIONAL
                       (Default value: H5S_ALL_F)
        file_space_id  INTEGER(HID_T), OPTIONAL
                       (Default value: H5S_ALL_F)
        xfer_prp       INTEGER(HID_T), OPTIONAL
                       (Default value: H5P_DEFAULT_F)
</PRE>
<UL>
   <LI> The <I>dset_id</I> is the dataset identifier.
   <P>

   <LI> The <I>mem_type_id</I> parameter is the identifier of the dataset's 
        memory datatype.  <code>H5T_NATIVE_INT</code> in C 
        (<code>H5T_NATIVE_INTEGER</code> in FORTRAN) is an integer datatype 
        for the machine on which the library was compiled.
   <P>

  <LI> The <I>mem_space_id</I> parameter is the identifier of the dataset's 
       memory dataspace. <code>H5S_ALL</code> in C (<code>H5S_ALL_F</code> 
       in FORTRAN) is the default value and indicates that the whole dataspace 
       in memory is selected for the I/O operation.
       This parameter is optional in FORTRAN; if it is omitted, the default 
       will be used.
   <P>

   <LI> The <I>file_space_id</I> parameter is the identifier of the 
        dataset's file dataspace.  
        <code>H5S_ALL</code> in C (<code>H5S_ALL_F</code> in FORTRAN)
        is the default value and indicates that the entire dataspace of 
        the dataset in the file is selected for the I/O operation.
        This parameter is optional in FORTRAN; if it is omitted, the default 
        will be used.
   <P>

   <LI> The <I>xfer_prp</I> parameter is the data transfer propery list 
        identifier.
        <code>H5P_DEFAULT</code> in C 
        (<code>H5P_DEFAULT_F</code> in FORTRAN) is the default value and
        indicates that the default data transfer property list is used. 
        This parameter is optional in FORTRAN; if it is omitted, the default 
        will be used.
   <P>

   <LI> The <I>buf</I> parameter is the data buffer to write.
   <P>

   <LI> In FORTRAN, the <I>hdferr</I> parameter is for the error code
        passed back:  0 if successful, -1 if not.  In C, this function
        returns a non-negative value if successful; otherwise it returns 
        a negative value. 
</UL>
<P>
<LI><code>H5Dread</code>/<code>h5dread_f</code> reads raw data from the 
    specified dataset to an application buffer,
   converting from the file datatype and dataspace to the memory datatype and
   dataspace.
<P>
<I>C</I>:
<PRE>
  herr_t H5Dread (hid_t dset_id, hid_t mem_type_id, hid_t mem_space_id, 
                  hid_t file_space_id, hid_t xfer_prp, void * buf) 
</PRE>
<I>FORTRAN</I>:
<PRE>
  h5dread_f (dset_id, mem_type_id, buf, hdferr, mem_space_id, &
                  file_space_id, xfer_prp)

        dset_id        INTEGER(HID_T)
        mem_type_id    INTEGER(HID_T)
        buf(*,...*)    TYPE
        hdferr         INTEGER 
                       (Possible values: 0 on success and -1 on failure)
        mem_space_id   INTEGER(HID_T), OPTIONAL
                       (Default value: H5S_ALL_F)
        file_space_id  INTEGER(HID_T), OPTIONAL
                       (Default value: H5S_ALL_F)
        xfer_prp       INTEGER(HID_T), OPTIONAL
                       (Default value: H5P_DEFAULT_F)

</PRE>

<p>
<UL>
   <LI>The <I>dset_id</I> parameter is the dataset identifier. 
   <P>

   <LI>The <I>mem_type_id</I> parameter is the identifier of the dataset's 
       memory datatype.  <code>H5T_NATIVE_INT</code> in C 
       (<code>H5T_NATIVE_INTEGER</code> in FORTRAN) is an integer datatype 
       for the machine on which the library was compiled.
   <P>

   <LI>The <I>mem_space_id</I> parameter is the identifier of the dataset's 
       memory dataspace.  <code>H5S_ALL</code> in C (<code>H5S_ALL_F</code> 
       in FORTRAN) is the default value and indicates that the whole dataspace 
       in memory is selected for the I/O operation.
       This parameter is optional in FORTRAN; if it is omitted, the default 
       will be used.
   <P>

   <LI>The <I>file_space_id</I> parameter is the identifier of the 
       dataset's file dataspace.
       <code>H5S_ALL</code> in C (<code>H5S_ALL_F</code> in FORTRAN)
       is the default value and indicates that the entire dataspace of 
       the dataset in the file is selected for the I/O operation.
       This parameter is optional in FORTRAN; if it is omitted, the default 
       will be used.

   <P>
   <LI>The <I>xfer_prp</I> parameter is the data transfer propery list 
       identifier.
       <code>H5P_DEFAULT</code> in C
       (<code>H5P_DEFAULT_F</code> in FORTRAN) is the default value and
       indicates that the default data transfer property list is used. 
       This parameter is optional in FORTRAN; if it is omitted, the default 
       will be used.
   <P>

   <LI> The <I>buf</I> parameter is the data buffer to read into.
   <P>

   <LI> In FORTRAN, the <I>hdferr</I> parameter is for the error code
        passed back:  0 if successful, -1 if not.  In C, this function
        returns a non-negative value if successful; otherwise it returns 
        a negative value.
</UL>
</UL>
<A NAME="fc">
<H3><U>File Contents</U></H3>
Figure 6.1a  shows the contents of <code>dset.h5</code> (created by the C program).
<BR>
Figure 6.1b  shows the contents of <code>dsetf.h5</code> (created by the FORTRAN 
program).  
<P>
      <B>Fig. 6.1a</B> &nbsp;  <I><code>dset.h5</code> in DDL</I>
<PRE>
      HDF5 "dset.h5" {
      GROUP "/" {
         DATASET "dset" {
            DATATYPE { H5T_STD_I32BE }
            DATASPACE { SIMPLE ( 4, 6 ) / ( 4, 6 ) }
            DATA {
               1, 2, 3, 4, 5, 6,
               7, 8, 9, 10, 11, 12,
               13, 14, 15, 16, 17, 18,
               19, 20, 21, 22, 23, 24
            }
         }
      }
      }
</PRE>
<P>
      <B>Fig. 6.1b</B> &nbsp;  <I><code>dsetf.h5</code> in DDL</I>
<PRE>
HDF5 "dsetf.h5" {
GROUP "/" {
   DATASET "dset" {
      DATATYPE { H5T_STD_I32BE }
      DATASPACE { SIMPLE ( 6, 4 ) / ( 6, 4 ) }
      DATA {
         1, 7, 13, 19,
         2, 8, 14, 20,
         3, 9, 15, 21,
         4, 10, 16, 22,
         5, 11, 17, 23,
         6, 12, 18, 24
      }
   }
}
}
</PRE>




<!-- BEGIN FOOTER INFO -->

<P><hr noshade size=1>
<font face="arial,helvetica" size="-1">
  <a href="http://www.ncsa.uiuc.edu/"><img border=0
     src="http://www.ncsa.uiuc.edu/Images/NCSAhome/footerlogo.gif"
     width=78 height=27 alt="NCSA"><br>
  The National Center for Supercomputing Applications</A><br>
  <a href="http://www.uiuc.edu/">University of Illinois
    at Urbana-Champaign</a><br>
  <br>
<!-- <A HREF="helpdesk.mail.html"> -->
<A HREF="mailto:hdfhelp@ncsa.uiuc.edu">
hdfhelp@ncsa.uiuc.edu</A>
<br>
<BR> <H6>Last Modified: March 16, 2001</H6><BR>
<!-- modified by Barbara Jones - bljones@ncsa.uiuc.edu -->
<!-- modified by Frank Baker - fbaker@ncsa.uiuc.edu -->
</FONT>
<BR>
<!-- <A HREF="mailto:hdfhelp@ncsa.uiuc.edu"> -->

</BODY>
</HTML>