hdf5/doc/html/H5.sample_code.html

124 lines
3.7 KiB
HTML
Raw Normal View History

<html><head><title>
HDF5 Draft API Example Code
</title></head><body>
<center>
<h1>HDF5: API Example Code</h1>
</center>
<P>Example programs/sections of code below:
<dl COMPACT>
<dt><a href="#Example1">#1</a>
<dd>A simple example showing how to create a file.
<dt><a href="#Example2">#2</a>
<dd>A example showing how to create a homogenous multi-dimensional dataset.
<dt><a href="#Example3">#3</a>
<dd>A example showing how to read a generic dataset.
</dl>
<hr>
<h2><a name="Example1">Simple Example showing how to create a file.</a></h2>
<P>Notes:<br>
This example creates a new HDF5 file and allows write access.
If the file exists already, the H5F_ACC_TRUNC flag would also be necessary to
overwrite the previous file's information.
<P>Code:
<code> <pre>
hid_t file_id;
[svn-r673] Changes since 19980908 ---------------------- ./COPYING Reformatted as text instead of C. Removed zlib crew from the list of contributors since no zlib code is actually in the hdf5 library. ./INSTALL ./INSTALL.ascired ./MANIFEST Minor updates for Beta release including version number change. ./INSTALL_MAINT Added information about making a release. ./RELEASE Updated function list based on public header files. ./bin/checkposix Got rid of complaints about some obvious things. ./doc/html/H5.api.html ./doc/html/RM_H5F.html ./src/H5F.c ./src/H5Fpublic.h ./test/tfile.c Changed H5Fget_create_template() and H5Fget_access_template() to H5Fget_create_plist() and H5Fget_access_plist() since that conforms better to lots of other names. ./doc/html/Datatypes.html ./doc/html/ExternalFiles.html ./doc/html/Files.html ./doc/html/H5.api.html ./doc/html/H5.sample_code.html ./doc/html/RM_H5F.html ./doc/html/RM_H5Front.html Changed `template' to `property list', etc. ./doc/html/Ragged.html [NEW] Documentation for ragged arrays. ./src/H5Iprivate.h ./src/H5Ipublic.h ./src/H5I.c Changed the scope of some symbols to be more local. ./src/H5.c ./src/H5AC.c ./src/H5D.c ./src/H5E.c ./src/H5F.c ./src/H5Ffamily.c ./src/H5Fistore.c ./src/H5Flow.c ./src/H5Fsec2.c ./src/H5Fsplit.c ./src/H5Fstdio.c ./src/H5G.c ./src/H5Gnode.c ./src/H5HG.c ./src/H5I.c ./src/H5O.c ./src/H5Ocomp.c ./src/H5Odtype.c ./src/H5Oefl.c ./src/H5Omtime.c ./src/H5Oname.c ./src/H5P.c ./src/H5S.c ./src/H5Shyper.c ./src/H5Tbit.c ./src/H5Tconv.c ./src/H5V.c ./src/H5Z.c ./src/H5private.h Fixed some violations of our naming scheme by adding HD to the beginning of all Posix functions.
1998-09-09 03:15:44 +08:00
file_id=<A HREF="H5.apiv2.html#File-Create">H5Fcreate</a>("example1.h5",H5F_ACC_EXCL,H5P_DEFAULT,H5P_DEFAULT);
<A HREF="H5.apiv2.html#File-Close">H5Fclose</a>(file_id);
</pre> </code>
<hr>
<h2><a name="Example2">Example showing how create a homogenous multi-dimensional dataset.</a></h2>
<P>Notes:<br>
This example creates a 4-dimensional dataset of 32-bit floating-point
numbers, corresponding to the current Scientific Dataset functionality.
<P>Code:
<code> <pre>
1 hid_t file_id; /* new file's ID */
2 hid_t dim_id; /* new dimensionality's ID */
3 int rank=4; /* the number of dimensions */
4 hsize_t dims[4]={6,5,4,3}; /* the size of each dimension */
5 hid_t dataset_id; /* new dataset's ID */
6 float buf[6][5][4][3]; /* storage for the dataset's data */
7 herr_t status; /* function return status */
8
9 file_id = H5Fcreate ("example3.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
10 H5P_DEFAULT);
11 assert (file_id &gt;= 0);
12
13 /* Create & initialize a dimensionality object */
14 dim_id = H5Screate_simple (rank, dims);
15 assert (dim_id &gt;= 0);
16
17 /* Create & initialize the dataset object */
18 dataset_id = H5Dcreate (file_id, "Simple Object", H5T_NATIVE_FLOAT,
19 dim_id, H5P_DEFAULT);
20 assert (dataset_id &gt;= 0);
21
22 &lt;initialize data array&gt;
23
24 /* Write the entire dataset out */
25 status = H5Dwrite (dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
26 H5P_DEFAULT, buf);
27 assert (status &gt;= 0);
28
29 /* Release the IDs we've created */
30 H5Sclose (dim_id);
31 H5Dclose (dataset_id);
32 H5Fclose (file_id);
</pre> </code>
<hr>
<h2><a name="Example3">Example showing how read a generic dataset.</a></h2>
<P>Notes:<br>
This example shows how to get the information for and display a generic
dataset.
<P>Code:
<code> <pre>
1 hid_t file_id; /* file's ID */
2 hid_t dataset_id; /* dataset's ID in memory */
3 hid_t space_id; /* dataspace's ID in memory */
4 uintn nelems; /* number of elements in array */
5 double *buf; /* pointer to the dataset's data */
6 herr_t status; /* function return value */
7
8 file_id = H5Fopen ("example6.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
9 assert (file_id &gt;= 0);
10
11 /* Attach to a datatype object */
12 dataset_id = H5Dopen (file_id, "dataset1");
13 assert (dataset_id &gt;= 0);
14
15 /* Get the OID for the dataspace */
16 space_id = H5Dget_space (dataset_id);
17 assert (space_id &gt;= 0);
18
19 /* Allocate space for the data */
20 nelems = H5Sget_npoints (space_id);
21 buf = malloc (nelems * sizeof(double));
22
23 /* Read in the dataset */
24 status = H5Dread (dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL,, H5S_ALL,
25 H5P_DEFAULT, buf);
26 assert (status &gt;= 0);
27
28 /* Release the IDs we've accessed */
29 H5Sclose (space_id);
30 H5Dclose (dataset_id);
31 H5Fclose (file_id);
</pre> </code>