hdf5/doc/html/H5.sample_code.html

124 lines
3.8 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;
file_id=<A HREF="H5.apiv2.html#File-Create">H5Fcreate</a>("example1.h5",H5F_ACC_EXCL,H5P_DEFAULT_TEMPLATE,H5P_DEFAULT_TEMPLATE);
<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>