summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/rdwt.html
blob: 1eb1a844a65be82f49747fd58024efb2256c9e94 (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
<HTML><HEAD>
<TITLE>HDF5 Tutorial - Reading to/Writing from 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 to/Writing from a Dataset</FONT>
</BIG></BIG></BIG></H1>

<hr noshade size=1>

<BODY>
<H2>Contents:</H2>
<UL>
 <LI><A HREF="#rdwr">Reading to/Writing from 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 to/Writing from a Dataset</h2>
<P>
During a dataset I/O operation, the library transfers raw data between memory
and the file. The memory can have a data type different than the file data type
and can also be a different size (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 data type in memory

  <LI> The dataset's dataspace in memory

  <LI> The dataset's dataspace in the file

  <LI> The transfer properties (The data transfer properties control various
    aspects of the I/O operations like 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 transfer properties.)

  <LI> The data buffer
</UL>


<P>
The steps to read to/write from a dataset are
as follows:
<OL>
  <LI> Obtain the dataset identifier.
  <LI> Specify the memory data type.
  <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/data type, and property list if necessary.
</OL>

To read to/write from a dataset, the calling program must contain the following call:
<PRE>
   H5Dread(dataset_id, mem_type_id, mem_space_id, file_space_id,
           xfer_plist_id, buf );
</PRE>
   or
<PRE>
   H5Dwrite(dataset_id, mem_type_id, mem_space_id, file_space_id,
            xfer_plist_id, buf);
</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,  
<I>/dset</I>, writes the dataset to the file, then reads the dataset back from 
memory. It then closes the dataset and file. <BR>
[ <A HREF="examples/h5_rdwt.c">Download h5_rdwt.c</A> ]

<PRE>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include &lt;hdf5.h&gt;
#define FILE "dset.h5"

main() {

   hid_t       file_id, dataset_id;  /* identifiers */
   herr_t      status;
   int         i, j, dset_data[4][6];

   /* Initialize the dataset. */
   for (i = 0; i &lt; 4; i++)
      for (j = 0; j &lt; 6; j++)
         dset_data[i][j] = i * 6 + j + 1;

   /* Open an existing file. */
   file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);

   /* Open an existing dataset. */

   dataset_id = H5Dopen(file_id, "/dset");

   /* Write the dataset. */
   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                     dset_data);

   status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                    dset_data);

   /* Close the dataset. */
   status = H5Dclose(dataset_id);

   /* Close the file. */
   status = H5Fclose(file_id);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
</PRE>
<A NAME="rem">
<H3><U>Remarks</U></H3>
<UL>
<LI> H5Fopen opens an existing file and returns a file identifier.
<PRE>
  hid_t H5Fopen (const char *name, unsigned flags, hid_t access_id) 
</PRE>
<UL>
   <LI> The first argument is the file name.

   <LI> The second argument is the file access mode. H5F_ACC_RDWR allows a file
      to be read from and written to.

   <LI> The third parameter is the identifier for the file access property list.
      H5P_DEFAULT specifies the default file access property list.
</UL>
<P>
<LI> H5Dopen opens an existing dataset with the name specified by the second
   argument at the location specified by the first parameter, and returns an
   identifier.
<PRE>
  hid_t H5Dopen (hid_t loc_id, const char *name) 
</PRE>
<P>
<LI> H5Dwrite writes raw data from an application buffer to the specified
   dataset, converting from the data type and data space of the dataset in
   memory to the data type and data space of the dataset in the file.
<PRE>
  herr_t H5Dwrite (hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, 
                   hid_t file_space_id, hid_t xfer_plist_id, const void * buf) 
</PRE>
<UL>
   <LI> The first parameter is the identifier of the dataset.

   <LI> The second parameter is the identifier of the dataset's datatype in
      memory. H5T_NATIVE_INT is an integer data type for the machine on which
      the library was compiled.

  <LI> The third parameter is the identifier of the dataset's dataspace in
      memory. H5S_ALL indicates that the dataset's dataspace in memory is the
      same as that in the file.

   <LI> The fourth parameter is the identifier of the dataset's dataspace in the
      file. H5S_ALL indicates that the entire dataspace of the dataset in the
      file is referenced.

   <LI> The fifth parameter is the identifier of the data transfer propery list.
      H5P_DEFAULT indicates that the default data transfer property list is used.

   <LI> The last parameter is the data buffer.
</UL>
<P>
<LI> H5Dread reads raw data from the specified dataset to an application buffer,
   converting from the file datatype and dataspace to the memory datatype and
   dataspace.
<PRE>
  herr_t H5Dread (hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, 
                  hid_t file_space_id, hid_t xfer_plist_id, void * buf) 
</PRE>
<UL>
   <LI> The first parameter is the identifier of the dataset read from.

   <LI> The second parameter is the identifier of the dataset's memory datatype.

   <LI> The third parameter is the identifier of the dataset's memory dataspace.

   <LI> The fourth parameter is the identifier of the dataset's file dataspace.

   <LI> The fifth parameter is the identifier of the data transfer propery list.

   <LI> The last parameter is the data buffer.
</UL>
</OL>

<A NAME="fc">
<H3><U>File Contents</U></H3>
Figure 6.1 shows the contents of 'dset.h5'.
<P>
      <B>Fig. 6.1</B> &nbsp;  <I>'dset.h5' 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>




<!-- 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> <H6>Last Modified: August 27, 1999</H6><BR>
<!-- modified by Barbara Jones - bljones@ncsa.uiuc.edu -->
</FONT>
<BR>
<!-- <A HREF="mailto:hdfhelp@ncsa.uiuc.edu"> -->

</BODY>
</HTML>