summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/crtfile.html
blob: fc235c44f13ab8ac3220b8df00099e4ad01a3964 (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
<HTML><HEAD>
<TITLE>HDF5 Tutorial - Creating an HDF5 File
</TITLE> 
</HEAD>

<body bgcolor="#ffffff">

<!-- BEGIN MAIN BODY -->


 [ <A HREF="title.html"><I>HDF5 Tutorial Top</I></A> ]
<H1>
<BIG><BIG><BIG><FONT COLOR="#c101cd">Creating an HDF5 File</FONT>
</BIG></BIG></BIG></H1>

<hr noshade size=1>

<BODY>
<H2>Contents:</H2>
<UL>
    <LI> <A HREF="#def">What is an HDF5 file</A>?
    <LI>Programming Example 
    <UL>
      <LI> <A HREF="#desc">Description</A> 
      <LI> <A HREF="#rem">Remarks</A> 
      <LI> <A HREF="#fc">File Contents</A>
      <LI> <A HREF="#ddl">File Definition in DDL</A>
    </UL>
</UL>
<HR>
<A NAME="def">
<H2>What is an HDF5 file?</h2>
<P>
An HDF5 file is a binary file containing scientific data and supporting
metadata. The primary types of objects stored in an HDF5 file, groups and 
datasets, will be discussed in other sections of this tutorial.
<P>
To create a file, an application must specify a filename, file
access mode, file creation property list, and file access property list.
<P>
<UL>
  <LI><B> File access mode:</B><BR>
    When creating a file, the file access mode specifies the action to 
    take if the file already exists:
    <UL>
    <LI><code>H5F_ACC_TRUNC</code> specifies that if the file already exists,
      the current contents will be deleted so that the application can rewrite 
      the file with new data.
    <LI><code>H5F_ACC_EXCL</code> specifies that the open is to fail if 
      the file already exists.
    <LI>If the file does not already exist, the file access parameter is 
      ignored.
    <LI>In all cases, the application has both read and write access to
      a successfully created file.  
    </UL>
<P>
    Note that there are two different access modes for opening exisitng files:
    <UL>
    <LI><code>H5F_ACC_RDONLY</code> specifies that the application has 
      read access but will not be allowed to write any data.
    <LI><code>H5F_ACC_RDWR</code> specifies that the application has 
      read and write access.
    </UL> 
<P>
    For further information, see 
    <a href="../Files.html">The File Interface (H5F)</a> section of the 
    <cite>HDF5 User's Guide</cite> and
    the <a href="../RM_H5F.html#File-Create">H5F: File Interface</a> 
    section of the <cite>HDF5 Reference Manual</cite>.
<P>
  <LI><B> File creation property list:</B><BR>
    The file creation property list is used to control the file metadata.
    File metadata contains information about the size of the user-block, the
    size of various file data structures used by the HDF5 library, etc.
    In this tutorial, the default file creation property list,
    <code>H5P_DEFAULT</code>, is used.
<P>
    The user-block is a fixed-length block of data located at the beginning
    of the file which is ignored by the HDF5 library.  
    The user-block may be used to store
    any data or information found to be useful to applications.
<P>
    For further information, see 
    <a href="../Files.html">The File Interface (H5F)</a> section of the 
    <cite>HDF5 User's Guide</cite>.
<P>
  <LI><B> File access property list:</B><BR>
    The file access property list is used to control different methods of
    performing I/O on files. 
    The default file access property list, <code>H5P_DEFAULT</code>, 
    is used in this tutorial.
<P>
    For further information, see 
    <a href="../Files.html">The File Interface (H5F)</a> section of the 
    <cite>HDF5 User's Guide</cite>.
</UL>
<P>
The steps to create and close an HDF5 file are as follows:
<OL>
  <LI> Specify the file creation and access property lists, if necessary.
  <LI> Create the file.
  <LI> Close the file and close the property lists, if necessary.
</OL>
To create an HDF5 file, the calling program must contain calls to 
create and close the file.  For example:
<P>
<I>C</I>:<PRE>
   file_id = H5Fcreate (filename, access_mode, create_id, access_id);
   status = H5Fclose (file_id); 
</PRE>
<I>FORTRAN</I>:<PRE>
   CALL h5fcreate_f (filename, access_mode, file_id, hdferr, &
            creation_prp=create_id, access_prp=access_id)
        <i>or</i>
   CALL h5fcreate_f (filename, access_mode, file_id, hdferr)

   CALL h5fclose_f (file_id, hdferr)
</PRE>
In FORTRAN, the file creation property list, <code>creation_prp</code>, 
and file access property list, <code>access_prp</code>, 
are optional parameters; 
they can be omitted if the default values are to be used.
<P>
<H2>Programming Example</H2>
<A NAME="desc">
<H3><U>Description</U></H3>      
The following example demonstrates how to create and close an HDF5 file. 
It creates a file called <code>file.h5</code> in the C version,
<code>filef.h5</code> in FORTRAN, and then closes the file.<P>

<UL>
[ <A HREF="examples/h5_crtfile.c" target="ExternalWin">C Example</A> ] 
    -- <code>h5_crtfile.c</code> <BR> 
[ <A HREF="examples/fileexample.f90" target="ExternalWin">FORTRAN Example</A> ] 
    -- <code>fileexample.f90</code><BR>  
[ <A HREF="examples/java/CreateFile.java" target="ExternalWin">Java Example</A> ]  -- <code>CreateFile.java</code>
</UL>
<P>
<B>NOTE:</B> To download a tar file of all of the examples, including
a Makefile, please go to the <A HREF="references.html">References</A> page.

<A NAME="rem">
<H3><U>Remarks</U></H3>
<UL>
<LI><B>In C:</B>
   The include file <code>hdf5.h</code> contains definitions and declarations 
   and must be included in any program that uses the HDF5 library.
   <BR><B>In FORTRAN:</B>
   The module <code>HDF5</code> contains definitions and declarations 
   and must be used in any program that uses the HDF5 library.
<P>
<LI><code>H5Fcreate</code>/<code>h5fcreate_f</code> creates 
  an HDF5 file and returns the file identifier.
<PRE>
<I>C</I>:       
  hid_t H5Fcreate (const char *name, unsigned access_mode, hid_t creation_prp, 
                   hid_t access_prp) 
<I>FORTRAN</I>: 
  h5fcreate_f (name, access_mode, file_id, hdferr, creation_prp, access_prp)

           name          CHARACTER(LEN=*)
           access_flag   INTEGER 
                         (Valid values: H5F_ACC_RDWR_F, H5F_ACC_RDONLY_F, 
                         H5F_ACC_TRUNC_F, H5F_ACC_EXCL_F, H5F_ACC_DEBUG_F)
           file_id       INTEGER(HID_T)
           hdferr        INTEGER 
                         (Valid values: 0 on success and -1 on failure)
           creation_prp  INTEGER(HID_T), OPTIONAL
                         (Default value: H5P_DEFAULT_F)
           access_prp    INTEGER(HID_T), OPTIONAL
                         (Default value: H5P_DEFAULT_F) 
         
</PRE>
<UL>
   <LI> The <I>name</I> parameter specifies the name of the file to be created.
<P>
   <LI> The <I>access_mode</I> parameter specifies the file access mode. 
        <code>H5F_ACC_TRUNC</code> (<code>H5F_ACC_TRUNC_F</code> in FORTRAN) 
        will truncate a file if it already exists.
<P>
   <LI> The <I>creation_prp</I> parameter
        specifies the file creation property list.
        For C, using <code>H5P_DEFAULT</code> indicates that the 
        default file creation property list is to be used.  
        This option is optional in FORTRAN; if it is omitted, the default file 
        creation property list, <code>H5P_DEFAULT_F</code>, is used.
<P>
   <LI> The <I>access_prp</I> parameter
        specifies the file access property list.
        For C, using <code>H5P_DEFAULT</code> indicates that the 
        default file creation property list is to be used.  
        This option is optional in FORTRAN; if it is omitted, the default file 
        creation property list, <code>H5P_DEFAULT_F</code>, is used.
<P>
   <LI> In C, this function returns the file identifier if successful and 
        a negative value otherwise.  
        In FORTRAN, the file identifier is returned in the 
        <I>file_id</I> parameter.  If the call is successful, 0 (zero) is
        passed back in the <I>hdferr</I> parameter. Otherwise, <I>hdferr</I>
        will have a value of -1.  

</UL>
<P>
<LI> When a file is no longer accessed by a program, 
     <code>H5Fclose</code>/<code>h5fclose_f</code>
     must be called to release the resources used by the file. This call 
     is mandatory.
<PRE>
<I>C</I>:
    herr_t H5Fclose (hid_t file_id) 

<I>FORTRAN</I>:
    h5fclose_f(file_id, hdferr)
</PRE>
<P>
<LI>The root group is automatically created when a file is created.
   Every file has a root group and the path name of the root group is 
   always <code>/</code>.
</UL>
<A NAME="fc">
<H3><U>File Contents</U></H3>
The HDF team has developed tools for examining the contents of HDF5 files. 
The tool used in this tutorial is the HDF5 dumper, <code>h5dump</code>, 
which displays the file contents in human-readable form.  
The output of <code>h5dump</code> is an ASCII display formatted according 
to the HDF5 DDL grammar.
This grammar is defined, using Backus-Naur Form, in the 
<a href="../ddl.html">DDL in BNF for HDF5</a>.
<p>
To view the file contents, type: 
<PRE>
   <B>h5dump &lt;filename&gt</B> 
</PRE>

Figure 4.1 describes the file contents of <code>file.h5</code> (<code>filef.h5</code>)
using a directed graph.
The directed graphs in this tutorial use an oval to represent an HDF5 group
and a rectangle to represent an HDF5 dataset (none in this example). 
Arrows indicate the inclusion direction of the contents (none in this example).

<P>
<B>Fig. 4.1</B> &nbsp;  <I>Contents of <code>file.h5</code> (<code>filef.h5</code>)</I>
<PRE>
<!--
<IMG src="fileh5.jpg" width="205" height="208"></PRE> -->
<IMG src="img001.gif"></PRE>

Figure 4.2 is the text description of <code>file.h5</code>, as generated by 
<code>h5dump</code>. The HDF5 file called <code>file.h5</code> contains 
a group called <code>/</code>, or the <I>root group</I>.
(The file called <code>filef.h5</code>, 
created by the FORTRAN version of the example, has the same output except
that the filename shown is <code>filef.h5</code>.)
<P>
<B> Fig. 4.2</B> &nbsp; <I><code>file.h5</code> in DDL</I>
<PRE>

         HDF5 "file.h5" {
         GROUP "/" {
         }
         }

</PRE>
<A NAME="ddl">

<h3><U>File Definition in DDL</U></H3>

Figure 4.3 is the simplified DDL file definition for creating an HDF5 file. 
For simplicity, a simplified DDL is used in this tutorial. A complete and 
more rigorous DDL can be found in the
<a href="../ddl.html">DDL in BNF for HDF5</a>, a section of the 
<cite>HDF5 User's Guide</cite>.  
<P>
<B> Fig. 4.3</B> &nbsp; <I>HDF5 File Definition</I>
<P>
     The following symbol definitions are used in the DDL:
<PRE>

        ::=               defined as
        &lt;tname&gt           a token with the name <I>tname</I>
        &lt;a&gt | &lt;b&gt         one of &lt;a&gt or &lt;b&gt
        &lt;a&gt;*              zero or more occurrences of &lt;a&gt
</PRE>
     The simplified DDL for file definition is as follows:
<PRE>
        &lt;file&gt ::= HDF5 "&lt;file_name&gt;" { &lt;root_group&gt }

        &lt;root_group&gt ::= GROUP "/" { &lt;group_attribute&gt* &lt;group_member&gt;* }

        &lt;group_attribute&gt ::= &lt;attribute&gt

        &lt;group_member&gt ::= &lt;group&gt | &lt;dataset&gt
</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="footer-ncsalogo.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: June 22, 2001</H6><BR>
<!-- modified by Barbara Jones - bljones@ncsa.uiuc.edu -->
<!-- modified by Frank Baker - fbaker@ncsa.uiuc.edu -->
</FONT>

<!-- <A HREF="mailto:hdfhelp@ncsa.uiuc.edu"> -->

</BODY>
</HTML>