2
0
mirror of https://github.com/HDFGroup/hdf5.git synced 2025-04-24 17:51:25 +08:00

[svn-r23091] Changed C examples to reflect changes in documentation via Barbara;

mainly program additions from tutorial.

Tested: jam (gnu compilers)
This commit is contained in:
Scot Breitenfeld 2012-12-11 23:00:34 -05:00
parent 3079b8dde8
commit 7155aa6f1d
14 changed files with 852 additions and 5 deletions

@ -121,6 +121,15 @@
./examples/Makefile.in
./examples/h5_chunk_read.c
./examples/h5_compound.c
./examples/h5_crtgrpd.c
./examples/h5_subset.c
./examples/h5_cmprss.c
./examples/h5_rdwt.c
./examples/h5_crtgrpar.c
./examples/h5_extend.c
./examples/h5_crtatt.c
./examples/h5_crtgrp.c
./examples/h5_crtdat.c
./examples/h5_drivers.c
./examples/h5_dtransform.c
./examples/h5_elink_unix2win.c

@ -10,6 +10,15 @@ ADD_DEFINITIONS (${HDF5_EXTRA_C_FLAGS})
# Define Sources
#-----------------------------------------------------------------------------
SET (examples
h5_crtdat
h5_rdwt
h5_crtatt
h5_crtgrp
h5_crtgrpar
h5_crtgrpd
h5_cmprss
h5_extend
h5_subset
h5_write
h5_read
h5_extend_write
@ -51,11 +60,15 @@ IF (BUILD_TESTING)
-E remove
Attributes.h5
btrees_file.h5
cmprss.h5
default_file.h5
dset.h5
extend.h5
extlink_prefix_source.h5
extlink_source.h5
extlink_target.h5
group.h5
groups.h5
hard_link.h5
mount1.h5
mount2.h5
@ -71,6 +84,7 @@ IF (BUILD_TESTING)
separate_indexes_file.h5
small_lists_file.h5
soft_link.h5
subset.h5
unix2win.h5
)
IF (NOT "${last_test}" STREQUAL "")

@ -29,7 +29,9 @@ endif
# Don't tell automake about them, because if it knew they were programs,
# it would try to compile them instead of using the h5cc script.
# Use the boilerplate in config/examples.am instead.
EXAMPLE_PROG = h5_write h5_read h5_extend_write h5_chunk_read h5_compound \
EXAMPLE_PROG = h5_write h5_read h5_extend_write h5_chunk_read h5_compound \
h5_crtgrpd h5_subset h5_cmprss h5_rdwt h5_crtgrpar h5_extend \
h5_crtatt h5_crtgrp h5_crtdat \
h5_group h5_select h5_attribute h5_mount h5_reference h5_drivers \
h5_ref2reg h5_extlink h5_elink_unix2win h5_shared_mesg
TEST_SCRIPT=testh5cc.sh
@ -37,6 +39,8 @@ TEST_SCRIPT=testh5cc.sh
# Install files
# List all file that should be installed in examples directory
INSTALL_FILES = h5_write.c h5_read.c h5_extend_write.c h5_chunk_read.c \
h5_crtgrpd.c h5_subset.c h5_cmprss.c h5_rdwt.c h5_crtgrpar.c \
h5_extend.c h5_crtatt.c h5_crtgrp.c h5_crtdat.c \
h5_compound.c h5_group.c h5_select.c h5_attribute.c h5_mount.c \
h5_reference.c h5_drivers.c h5_extlink.c h5_elink_unix2win.c \
h5_ref2reg.c h5_shared_mesg.c ph5example.c
@ -55,9 +59,14 @@ $(EXTRA_PROG): $(H5CC)
$(H5CC) $(H5CCFLAGS) $(CFLAGS) -o $@ $(srcdir)/$@.c;
endif
# Two of the examples depend on files created by other examples.
# Some examples depend on files created by other examples.
h5_read.chkexe_: h5_write.chkexe_
h5_chunk_read.chkexe_: h5_extend_write.chkexe_
h5_crtgrpd.chkexe_: h5_crtgrpar.chkexe_
# h5_rdwt and h5_crtatt both modify the same file created by
# h5_crtdat. Serialize them.
h5_rdwt.chkexe_: h5_crtdat.chkexe_
h5_crtatt.chkexe_: h5_rdwt.chkexe_
# The external link examples demonstrate how to use paths; they need
# directories to be created to do this.
@ -82,6 +91,15 @@ EXAMPLETOPDIR=${DESTDIR}$(exec_prefix)/share/hdf5_examples
# library above.
h5_chunk_read: $(srcdir)/h5_chunk_read.c
h5_compound: $(srcdir)/h5_compound.c
h5_crtgrpd: $(srcdir)/h5_crtgrpd.c
h5_subset: $(srcdir)/h5_subset.c
h5_cmprss: $(srcdir)/h5_cmprss.c
h5_rdwt: $(srcdir)/h5_rdwt.c
h5_crtgrpar: $(srcdir)/h5_crtgrpar.c
h5_extend: $(srcdir)/h5_extend.c
h5_crtatt: $(srcdir)/h5_crtatt.c
h5_crtgrp: $(srcdir)/h5_crtgrp.c
h5_crtdat: $(srcdir)/h5_crtdat.c
h5_extend_write: $(srcdir)/h5_extend_write.c
h5_group: $(srcdir)/h5_group.c
h5_write: $(srcdir)/h5_write.c

@ -394,7 +394,9 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog $(EXTLINK_DIRS) *.h5
# Don't tell automake about them, because if it knew they were programs,
# it would try to compile them instead of using the h5cc script.
# Use the boilerplate in config/examples.am instead.
EXAMPLE_PROG = h5_write h5_read h5_extend_write h5_chunk_read h5_compound \
EXAMPLE_PROG = h5_write h5_read h5_extend_write h5_chunk_read h5_compound \
h5_crtgrpd h5_subset h5_cmprss h5_rdwt h5_crtgrpar h5_extend \
h5_crtatt h5_crtgrp h5_crtdat \
h5_group h5_select h5_attribute h5_mount h5_reference h5_drivers \
h5_ref2reg h5_extlink h5_elink_unix2win h5_shared_mesg
@ -403,6 +405,8 @@ TEST_SCRIPT = testh5cc.sh
# Install files
# List all file that should be installed in examples directory
INSTALL_FILES = h5_write.c h5_read.c h5_extend_write.c h5_chunk_read.c \
h5_crtgrpd.c h5_subset.c h5_cmprss.c h5_rdwt.c h5_crtgrpar.c \
h5_extend.c h5_crtatt.c h5_crtgrp.c h5_crtdat.c \
h5_compound.c h5_group.c h5_select.c h5_attribute.c h5_mount.c \
h5_reference.c h5_drivers.c h5_extlink.c h5_elink_unix2win.c \
h5_ref2reg.c h5_shared_mesg.c ph5example.c
@ -668,9 +672,14 @@ help:
@BUILD_PARALLEL_CONDITIONAL_FALSE@$(EXTRA_PROG): $(H5CC)
@BUILD_PARALLEL_CONDITIONAL_FALSE@ $(H5CC) $(H5CCFLAGS) $(CFLAGS) -o $@ $(srcdir)/$@.c;
# Two of the examples depend on files created by other examples.
# Some examples depend on files created by other examples.
h5_read.chkexe_: h5_write.chkexe_
h5_chunk_read.chkexe_: h5_extend_write.chkexe_
h5_crtgrpd.chkexe_: h5_crtgrpar.chkexe_
# h5_rdwt and h5_crtatt both modify the same file created by
# h5_crtdat. Serialize them.
h5_rdwt.chkexe_: h5_crtdat.chkexe_
h5_crtatt.chkexe_: h5_rdwt.chkexe_
$(EXTLINK_DIRS):
echo $(mkdir_p) $@
@ -684,6 +693,15 @@ $(EXTLINK_DIRS):
# library above.
h5_chunk_read: $(srcdir)/h5_chunk_read.c
h5_compound: $(srcdir)/h5_compound.c
h5_crtgrpd: $(srcdir)/h5_crtgrpd.c
h5_subset: $(srcdir)/h5_subset.c
h5_cmprss: $(srcdir)/h5_cmprss.c
h5_rdwt: $(srcdir)/h5_rdwt.c
h5_crtgrpar: $(srcdir)/h5_crtgrpar.c
h5_extend: $(srcdir)/h5_extend.c
h5_crtatt: $(srcdir)/h5_crtatt.c
h5_crtgrp: $(srcdir)/h5_crtgrp.c
h5_crtdat: $(srcdir)/h5_crtdat.c
h5_extend_write: $(srcdir)/h5_extend_write.c
h5_group: $(srcdir)/h5_group.c
h5_write: $(srcdir)/h5_write.c

125
examples/h5_cmprss.c Normal file

@ -0,0 +1,125 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to create a compressed dataset.
* It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "cmprss.h5"
#define RANK 2
#define DIM0 100
#define DIM1 20
int main () {
hid_t file_id, dataset_id, dataspace_id; /* identifiers */
hid_t plist_id;
size_t nelmts;
unsigned flags, filter_info;
H5Z_filter_t filter_type;
herr_t status;
hsize_t dims[2];
hsize_t cdims[2];
int idx;
int i,j, numfilt;
int buf[DIM0][DIM1];
int rbuf [DIM0][DIM1];
/* Uncomment these variables to use SZIP compression
unsigned szip_options_mask;
unsigned szip_pixels_per_block;
*/
/* Create a file. */
file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create dataset "Compressed Data" in the group using absolute name. */
dims[0] = DIM0;
dims[1] = DIM1;
dataspace_id = H5Screate_simple (RANK, dims, NULL);
plist_id = H5Pcreate (H5P_DATASET_CREATE);
/* Dataset must be chunked for compression */
cdims[0] = 20;
cdims[1] = 20;
status = H5Pset_chunk (plist_id, 2, cdims);
/* Set ZLIB / DEFLATE Compression using compression level 6.
* To use SZIP Compression comment out these lines.
*/
status = H5Pset_deflate (plist_id, 6);
/* Uncomment these lines to set SZIP Compression
szip_options_mask = H5_SZIP_NN_OPTION_MASK;
szip_pixels_per_block = 16;
status = H5Pset_szip (plist_id, szip_options_mask, szip_pixels_per_block);
*/
dataset_id = H5Dcreate (file_id, "Compressed_Data", H5T_STD_I32BE,
dataspace_id, H5P_DEFAULT, plist_id, H5P_DEFAULT);
for (i = 0; i< DIM0; i++)
for (j=0; j<DIM1; j++)
buf[i][j] = i+j;
status = H5Dwrite (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
status = H5Sclose (dataspace_id);
status = H5Dclose (dataset_id);
status = H5Pclose (plist_id);
status = H5Fclose (file_id);
/* Now reopen the file and dataset in the file. */
file_id = H5Fopen (FILE, H5F_ACC_RDWR, H5P_DEFAULT);
dataset_id = H5Dopen (file_id, "Compressed_Data", H5P_DEFAULT);
/* Retrieve filter information. */
plist_id = H5Dget_create_plist (dataset_id);
numfilt = H5Pget_nfilters (plist_id);
printf ("Number of filters associated with dataset: %i\n", numfilt);
for (i=0; i<numfilt; i++) {
nelmts = 0;
filter_type = H5Pget_filter (plist_id, 0, &flags, &nelmts, NULL, 0, NULL,
&filter_info);
printf ("Filter Type: ");
switch (filter_type) {
case H5Z_FILTER_DEFLATE:
printf ("H5Z_FILTER_DEFLATE\n");
break;
case H5Z_FILTER_SZIP:
printf ("H5Z_FILTER_SZIP\n");
break;
default:
printf ("Other filter type included.\n");
}
}
status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, rbuf);
status = H5Dclose (dataset_id);
status = H5Pclose (plist_id);
status = H5Fclose (file_id);
}

63
examples/h5_crtatt.c Normal file

@ -0,0 +1,63 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to create an attribute attached to a
* dataset. It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "dset.h5"
int main() {
hid_t file_id, dataset_id, attribute_id, dataspace_id; /* identifiers */
hsize_t dims;
int attr_data[2];
herr_t status;
/* Initialize the attribute data. */
attr_data[0] = 100;
attr_data[1] = 200;
/* Open an existing file. */
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/* Open an existing dataset. */
dataset_id = H5Dopen(file_id, "/dset", H5P_DEFAULT);
/* Create the data space for the attribute. */
dims = 2;
dataspace_id = H5Screate_simple(1, &dims, NULL);
/* Create a dataset attribute. */
attribute_id = H5Acreate (dataset_id, "Units", H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT);
/* Write the attribute data. */
status = H5Awrite(attribute_id, H5T_NATIVE_INT, attr_data);
/* Close the attribute. */
status = H5Aclose(attribute_id);
/* Close the dataspace. */
status = H5Sclose(dataspace_id);
/* Close to the dataset. */
status = H5Dclose(dataset_id);
/* Close the file. */
status = H5Fclose(file_id);
}

51
examples/h5_crtdat.c Normal file

@ -0,0 +1,51 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to create a dataset that is a 4 x 6
* array. It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "dset.h5"
int main() {
hid_t file_id, dataset_id, dataspace_id; /* identifiers */
hsize_t dims[2];
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create the data space for the dataset. */
dims[0] = 4;
dims[1] = 6;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the dataset. */
dataset_id = H5Dcreate(file_id, "/dset", H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* End access to the dataset and release resources used by it. */
status = H5Dclose(dataset_id);
/* Terminate access to the data space. */
status = H5Sclose(dataspace_id);
/* Close the file. */
status = H5Fclose(file_id);
}

40
examples/h5_crtgrp.c Normal file

@ -0,0 +1,40 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to create and close a group.
* It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "group.h5"
int main() {
hid_t file_id, group_id; /* identifiers */
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create a group named "/MyGroup" in the file. */
group_id = H5Gcreate(file_id, "/MyGroup", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Close the group. */
status = H5Gclose(group_id);
/* Terminate access to the file. */
status = H5Fclose(file_id);
}

48
examples/h5_crtgrpar.c Normal file

@ -0,0 +1,48 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates the creation of groups using absolute and
* relative names. It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "groups.h5"
int main() {
hid_t file_id, group1_id, group2_id, group3_id; /* identifiers */
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create group "MyGroup" in the root group using absolute name. */
group1_id = H5Gcreate(file_id, "/MyGroup", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Create group "Group_A" in group "MyGroup" using absolute name. */
group2_id = H5Gcreate(file_id, "/MyGroup/Group_A", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Create group "Group_B" in group "MyGroup" using relative name. */
group3_id = H5Gcreate(group1_id, "Group_B", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Close groups. */
status = H5Gclose(group1_id);
status = H5Gclose(group2_id);
status = H5Gclose(group3_id);
/* Close the file. */
status = H5Fclose(file_id);
}

91
examples/h5_crtgrpd.c Normal file

@ -0,0 +1,91 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to create a dataset in a group.
* It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "groups.h5"
int main() {
hid_t file_id, group_id, dataset_id, dataspace_id; /* identifiers */
hsize_t dims[2];
herr_t status;
int i, j, dset1_data[3][3], dset2_data[2][10];
/* Initialize the first dataset. */
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
dset1_data[i][j] = j + 1;
/* Initialize the second dataset. */
for (i = 0; i < 2; i++)
for (j = 0; j < 10; j++)
dset2_data[i][j] = j + 1;
/* Open an existing file. */
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/* Create the data space for the first dataset. */
dims[0] = 3;
dims[1] = 3;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create a dataset in group "MyGroup". */
dataset_id = H5Dcreate(file_id, "/MyGroup/dset1", H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Write the first dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset1_data);
/* Close the data space for the first dataset. */
status = H5Sclose(dataspace_id);
/* Close the first dataset. */
status = H5Dclose(dataset_id);
/* Open an existing group of the specified file. */
group_id = H5Gopen(file_id, "/MyGroup/Group_A", H5P_DEFAULT);
/* Create the data space for the second dataset. */
dims[0] = 2;
dims[1] = 10;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the second dataset in group "Group_A". */
dataset_id = H5Dcreate(group_id, "dset2", H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* Write the second dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset2_data);
/* Close the data space for the second dataset. */
status = H5Sclose(dataspace_id);
/* Close the second dataset */
status = H5Dclose(dataset_id);
/* Close the group. */
status = H5Gclose(group_id);
/* Close the file. */
status = H5Fclose(file_id);
}

146
examples/h5_extend.c Normal file

@ -0,0 +1,146 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example how to work with extendible datasets. The dataset
* must be chunked in order to be extendible.
*
* It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILENAME "extend.h5"
#define DATASETNAME "ExtendibleArray"
#define RANK 2
int
main (void)
{
hid_t file; /* handles */
hid_t dataspace, dataset;
hid_t filespace, memspace;
hid_t prop;
hsize_t dims[2] = {3, 3}; /* dataset dimensions at creation time */
hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
herr_t status;
hsize_t chunk_dims[2] = {2, 5};
int data[3][3] = { {1, 1, 1}, /* data to write */
{1, 1, 1},
{1, 1, 1} };
/* Variables used in extending and writing to the extended portion of dataset */
hsize_t size[2];
hsize_t offset[2];
hsize_t dimsext[2] = {7, 3}; /* extend dimensions */
int dataext[7][3] = { {2, 3, 4},
{2, 3, 4},
{2, 3, 4},
{2, 3, 4},
{2, 3, 4},
{2, 3, 4},
{2, 3, 4} };
/* Variables used in reading data back */
hsize_t chunk_dimsr[2];
hsize_t dimsr[2];
hsize_t i, j;
int rdata[10][3];
herr_t status_n;
int rank, rank_chunk;
/* Create the data space with unlimited dimensions. */
dataspace = H5Screate_simple (RANK, dims, maxdims);
/* Create a new file. If file exists its contents will be overwritten. */
file = H5Fcreate (FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Modify dataset creation properties, i.e. enable chunking */
prop = H5Pcreate (H5P_DATASET_CREATE);
status = H5Pset_chunk (prop, RANK, chunk_dims);
/* Create a new dataset within the file using chunk
creation properties. */
dataset = H5Dcreate (file, DATASETNAME, H5T_NATIVE_INT, dataspace,
H5P_DEFAULT, prop, H5P_DEFAULT);
/* Write data to dataset */
status = H5Dwrite (dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, data);
/* Extend the dataset. Dataset becomes 10 x 3 */
size[0] = dims[0]+ dimsext[0];
size[1] = dims[1];
status = H5Dextend (dataset, size);
/* Select a hyperslab in extended portion of dataset */
filespace = H5Dget_space (dataset);
offset[0] = 3;
offset[1] = 0;
status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL,
dimsext, NULL);
/* Define memory space */
memspace = H5Screate_simple (RANK, dimsext, NULL);
/* Write the data to the extended portion of dataset */
status = H5Dwrite (dataset, H5T_NATIVE_INT, memspace, filespace,
H5P_DEFAULT, dataext);
/* Close resources */
status = H5Dclose (dataset);
status = H5Pclose (prop);
status = H5Sclose (dataspace);
status = H5Sclose (memspace);
status = H5Sclose (filespace);
status = H5Fclose (file);
/********************************************
* Re-open the file and read the data back. *
********************************************/
file = H5Fopen (FILENAME, H5F_ACC_RDONLY, H5P_DEFAULT);
dataset = H5Dopen (file, DATASETNAME, H5P_DEFAULT);
filespace = H5Dget_space (dataset);
rank = H5Sget_simple_extent_ndims (filespace);
status_n = H5Sget_simple_extent_dims (filespace, dimsr, NULL);
prop = H5Dget_create_plist (dataset);
if (H5D_CHUNKED == H5Pget_layout (prop))
rank_chunk = H5Pget_chunk (prop, rank, chunk_dimsr);
memspace = H5Screate_simple (rank, dimsr, NULL);
status = H5Dread (dataset, H5T_NATIVE_INT, memspace, filespace,
H5P_DEFAULT, rdata);
printf("\n");
printf("Dataset: \n");
for (j = 0; j < dimsr[0]; j++)
{
for (i = 0; i < dimsr[1]; i++)
printf("%d ", rdata[j][i]);
printf("\n");
}
status = H5Pclose (prop);
status = H5Dclose (dataset);
status = H5Sclose (filespace);
status = H5Sclose (memspace);
status = H5Fclose (file);
}

53
examples/h5_rdwt.c Normal file

@ -0,0 +1,53 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to write and read data in an existing
* dataset. It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "dset.h5"
int 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 < 4; i++)
for (j = 0; j < 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", H5P_DEFAULT);
/* 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);
}

153
examples/h5_subset.c Normal file

@ -0,0 +1,153 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This example illustrates how to read/write a subset of data (a slab)
* from/to a dataset in an HDF5 file. It is used in the HDF5 Tutorial.
*/
#include "hdf5.h"
#define FILE "subset.h5"
#define DATASETNAME "IntArray"
#define RANK 2
#define DIM0_SUB 3 /* subset dimensions */
#define DIM1_SUB 4
#define DIM0 8 /* size of dataset */
#define DIM1 10
int
main (void)
{
hsize_t dims[2], dimsm[2];
int data[DIM0][DIM1]; /* data to write */
int sdata[DIM0_SUB][DIM1_SUB]; /* subset to write */
int rdata[DIM0][DIM1]; /* buffer for read */
hid_t file_id, dataset_id; /* handles */
hid_t dataspace_id, memspace_id;
herr_t status;
hsize_t count[2]; /* size of subset in the file */
hsize_t offset[2]; /* subset offset in the file */
hsize_t stride[2];
hsize_t block[2];
int i, j;
/*****************************************************************
* Create a new file with default creation and access properties.*
* Then create a dataset and write data to it and close the file *
* and dataset. *
*****************************************************************/
file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
dims[0] = DIM0;
dims[1] = DIM1;
dataspace_id = H5Screate_simple (RANK, dims, NULL);
dataset_id = H5Dcreate (file_id, DATASETNAME, H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
for (j = 0; j < DIM0; j++) {
for (i = 0; i < DIM1; i++)
if (i< (DIM1/2))
data[j][i] = 1;
else
data[j][i] = 2;
}
status = H5Dwrite (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, data);
printf ("\nData Written to File:\n");
for (i = 0; i<DIM0; i++){
for (j = 0; j<DIM1; j++)
printf (" %i", data[i][j]);
printf ("\n");
}
status = H5Sclose (dataspace_id);
status = H5Dclose (dataset_id);
status = H5Fclose (file_id);
/*****************************************************
* Reopen the file and dataset and write a subset of *
* values to the dataset.
*****************************************************/
file_id = H5Fopen (FILE, H5F_ACC_RDWR, H5P_DEFAULT);
dataset_id = H5Dopen (file_id, DATASETNAME, H5P_DEFAULT);
/* Specify size and shape of subset to write. */
offset[0] = 1;
offset[1] = 2;
count[0] = DIM0_SUB;
count[1] = DIM1_SUB;
stride[0] = 1;
stride[1] = 1;
block[0] = 1;
block[1] = 1;
/* Create memory space with size of subset. Get file dataspace
and select subset from file dataspace. */
dimsm[0] = DIM0_SUB;
dimsm[1] = DIM1_SUB;
memspace_id = H5Screate_simple (RANK, dimsm, NULL);
dataspace_id = H5Dget_space (dataset_id);
status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
stride, count, block);
/* Write a subset of data to the dataset, then read the
entire dataset back from the file. */
printf ("\nWrite subset to file specifying:\n");
printf (" offset=1x2 stride=1x1 count=3x4 block=1x1\n");
for (j = 0; j < DIM0_SUB; j++) {
for (i = 0; i < DIM1_SUB; i++)
sdata[j][i] = 5;
}
status = H5Dwrite (dataset_id, H5T_NATIVE_INT, memspace_id,
dataspace_id, H5P_DEFAULT, sdata);
status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, rdata);
printf ("\nData in File after Subset is Written:\n");
for (i = 0; i<DIM0; i++){
for (j = 0; j<DIM1; j++)
printf (" %i", rdata[i][j]);
printf ("\n");
}
status = H5Sclose (memspace_id);
status = H5Sclose (dataspace_id);
status = H5Dclose (dataset_id);
status = H5Fclose (file_id);
}

@ -78,7 +78,25 @@ fi
# Run tests
if [ $? -eq 0 ]
then
if (RunTest h5_write &&\
if (RunTest h5_crtdat &&\
rm h5_crtdat &&\
RunTest h5_extend &&\
rm h5_extend &&\
RunTest h5_rdwt &&\
rm h5_rdwt &&\
RunTest h5_crtatt &&\
rm h5_crtatt &&\
RunTest h5_crtgrp &&\
rm h5_crtgrp &&\
RunTest h5_crtgrpar &&\
rm h5_crtgrpar &&\
RunTest h5_crtgrpd &&\
rm h5_crtgrpd &&\
RunTest h5_subset &&\
rm h5_subset &&\
RunTest h5_cmprss &&\
rm h5_cmprss &&\
RunTest h5_write &&\
rm h5_write &&\
RunTest h5_read &&\
rm h5_read &&\