diff options
author | Vailin Choi <vchoi@hdfgroup.org> | 2013-10-24 21:42:38 (GMT) |
---|---|---|
committer | Vailin Choi <vchoi@hdfgroup.org> | 2013-10-24 21:42:38 (GMT) |
commit | 07a49f439fc44ddec27fd6a25ee0fd52de87ec3c (patch) | |
tree | 09a088c34a092f42fdb4502d3ea25a6f16cc892b /c++/examples | |
parent | bfd1c84d5cdc403c1414395cd49197cd4fc74108 (diff) | |
download | hdf5-07a49f439fc44ddec27fd6a25ee0fd52de87ec3c.zip hdf5-07a49f439fc44ddec27fd6a25ee0fd52de87ec3c.tar.gz hdf5-07a49f439fc44ddec27fd6a25ee0fd52de87ec3c.tar.bz2 |
[svn-r24354] Bring revision #24336 from revise_chunks.
h5committested.
Diffstat (limited to 'c++/examples')
-rw-r--r-- | c++/examples/Makefile.am | 20 | ||||
-rw-r--r-- | c++/examples/Makefile.in | 22 | ||||
-rw-r--r-- | c++/examples/h5tutr_cmprss.cpp | 162 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtatt.cpp | 95 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtdat.cpp | 82 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtgrp.cpp | 69 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtgrpar.cpp | 92 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtgrpd.cpp | 141 | ||||
-rw-r--r-- | c++/examples/h5tutr_extend.cpp | 176 | ||||
-rw-r--r-- | c++/examples/h5tutr_rdwt.cpp | 79 | ||||
-rw-r--r-- | c++/examples/h5tutr_subset.cpp | 183 | ||||
-rw-r--r-- | c++/examples/run-c++-ex.sh.in | 31 |
12 files changed, 1148 insertions, 4 deletions
diff --git a/c++/examples/Makefile.am b/c++/examples/Makefile.am index cd9a9f6..f479776 100644 --- a/c++/examples/Makefile.am +++ b/c++/examples/Makefile.am @@ -23,12 +23,18 @@ include $(top_srcdir)/config/commence.am # These are the programs that 'make all' or 'make prog' will build and # which 'make check' will run. List them in the order they should be run. -EXAMPLE_PROG=create readdata writedata compound extend_ds chunks h5group +EXAMPLE_PROG=create readdata writedata compound extend_ds chunks h5group \ + h5tutr_cmprss h5tutr_crtatt h5tutr_crtdat h5tutr_crtgrpar \ + h5tutr_crtgrp h5tutr_crtgrpd h5tutr_extend h5tutr_rdwt \ + h5tutr_subset TEST_SCRIPT=testh5c++.sh # These are the example files to be installed INSTALL_FILES=create.cpp readdata.cpp writedata.cpp compound.cpp \ - extend_ds.cpp chunks.cpp h5group.cpp + extend_ds.cpp chunks.cpp h5group.cpp \ + h5tutr_cmprss.cpp h5tutr_crtatt.cpp h5tutr_crtdat.cpp \ + h5tutr_crtgrpar.cpp h5tutr_crtgrp.cpp h5tutr_crtgrpd.cpp \ + h5tutr_extend.cpp h5tutr_rdwt.cpp h5tutr_subset.cpp INSTALL_SCRIPT_FILES = run-c++-ex.sh # Some of the examples depend on files created by running other examples @@ -58,5 +64,15 @@ extend_ds: $(srcdir)/extend_ds.cpp chunks: $(srcdir)/chunks.cpp h5group: $(srcdir)/h5group.cpp +h5tutr_cmprss: $(srcdir)/h5tutr_cmprss.cpp +h5tutr_crtatt: $(srcdir)/h5tutr_crtatt.cpp +h5tutr_crtdat: $(srcdir)/h5tutr_crtdat.cpp +h5tutr_crtgrpar: $(srcdir)/h5tutr_crtgrpar.cpp +h5tutr_crtgrp: $(srcdir)/h5tutr_crtgrp.cpp +h5tutr_crtgrpd: $(srcdir)/h5tutr_crtgrpd.cpp +h5tutr_extend: $(srcdir)/h5tutr_extend.cpp +h5tutr_rdwt: $(srcdir)/h5tutr_rdwt.cpp +h5tutr_subset: $(srcdir)/h5tutr_subset.cpp + include $(top_srcdir)/config/examples.am include $(top_srcdir)/config/conclude.am diff --git a/c++/examples/Makefile.in b/c++/examples/Makefile.in index e8aa093..f392787 100644 --- a/c++/examples/Makefile.in +++ b/c++/examples/Makefile.in @@ -222,6 +222,7 @@ LIPO = @LIPO@ LL_PATH = @LL_PATH@ LN_S = @LN_S@ LTLIBOBJS = @LTLIBOBJS@ +LT_ADD_LIBHDF5_DEPENDENCY = @LT_ADD_LIBHDF5_DEPENDENCY@ LT_STATIC_EXEC = @LT_STATIC_EXEC@ MAINT = @MAINT@ MAKEINFO = @MAKEINFO@ @@ -391,12 +392,19 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog *.h5 # These are the programs that 'make all' or 'make prog' will build and # which 'make check' will run. List them in the order they should be run. -EXAMPLE_PROG = create readdata writedata compound extend_ds chunks h5group +EXAMPLE_PROG = create readdata writedata compound extend_ds chunks h5group \ + h5tutr_cmprss h5tutr_crtatt h5tutr_crtdat h5tutr_crtgrpar \ + h5tutr_crtgrp h5tutr_crtgrpd h5tutr_extend h5tutr_rdwt \ + h5tutr_subset + TEST_SCRIPT = testh5c++.sh # These are the example files to be installed INSTALL_FILES = create.cpp readdata.cpp writedata.cpp compound.cpp \ - extend_ds.cpp chunks.cpp h5group.cpp + extend_ds.cpp chunks.cpp h5group.cpp \ + h5tutr_cmprss.cpp h5tutr_crtatt.cpp h5tutr_crtdat.cpp \ + h5tutr_crtgrpar.cpp h5tutr_crtgrp.cpp h5tutr_crtgrpd.cpp \ + h5tutr_extend.cpp h5tutr_rdwt.cpp h5tutr_subset.cpp INSTALL_SCRIPT_FILES = run-c++-ex.sh @@ -668,6 +676,16 @@ extend_ds: $(srcdir)/extend_ds.cpp chunks: $(srcdir)/chunks.cpp h5group: $(srcdir)/h5group.cpp +h5tutr_cmprss: $(srcdir)/h5tutr_cmprss.cpp +h5tutr_crtatt: $(srcdir)/h5tutr_crtatt.cpp +h5tutr_crtdat: $(srcdir)/h5tutr_crtdat.cpp +h5tutr_crtgrpar: $(srcdir)/h5tutr_crtgrpar.cpp +h5tutr_crtgrp: $(srcdir)/h5tutr_crtgrp.cpp +h5tutr_crtgrpd: $(srcdir)/h5tutr_crtgrpd.cpp +h5tutr_extend: $(srcdir)/h5tutr_extend.cpp +h5tutr_rdwt: $(srcdir)/h5tutr_rdwt.cpp +h5tutr_subset: $(srcdir)/h5tutr_subset.cpp + # How to create EXAMPLEDIR if it doesn't already exist $(EXAMPLEDIR): -$(top_srcdir)/bin/mkdirs $@ diff --git a/c++/examples/h5tutr_cmprss.cpp b/c++/examples/h5tutr_cmprss.cpp new file mode 100644 index 0000000..7db73c4 --- /dev/null +++ b/c++/examples/h5tutr_cmprss.cpp @@ -0,0 +1,162 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#ifndef H5_NO_STD + using std::cout; + using std::endl; +#endif // H5_NO_STD +#endif + +const H5std_string FILE_NAME("cmprss.h5"); +const H5std_string DATASET_NAME("Compressed_Data"); +const int DIM0 = 100; +const int DIM1 = 20; + +int main (void) +{ + hsize_t dims[2] = { DIM0, DIM1 }; // dataset dimensions + hsize_t chunk_dims[2] = { 20, 20 }; // chunk dimensions + int i,j, buf[DIM0][DIM1]; + + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Create a new file using the default property lists. + H5File file(FILE_NAME, H5F_ACC_TRUNC); + + // Create the data space for the dataset. + DataSpace *dataspace = new DataSpace(2, dims); + + // Modify dataset creation property to enable chunking + DSetCreatPropList *plist = new DSetCreatPropList; + plist->setChunk(2, chunk_dims); + + // Set ZLIB (DEFLATE) Compression using level 6. + // To use SZIP compression comment out this line. + plist->setDeflate(6); + + // Uncomment these lines to set SZIP Compression + // unsigned szip_options_mask = H5_SZIP_NN_OPTION_MASK; + // unsigned szip_pixels_per_block = 16; + // plist->setSzip(szip_options_mask, szip_pixels_per_block); + + // Create the dataset. + DataSet *dataset = new DataSet(file.createDataSet( DATASET_NAME, + PredType::STD_I32BE, *dataspace, *plist) ); + + for (i = 0; i< DIM0; i++) + for (j=0; j<DIM1; j++) + buf[i][j] = i+j; + + // Write data to dataset. + dataset->write(buf, PredType::NATIVE_INT); + + // Close objects and file. Either approach will close the HDF5 item. + delete dataspace; + delete dataset; + delete plist; + file.close(); + + // ----------------------------------------------- + // Re-open the file and dataset, retrieve filter + // information for dataset and read the data back. + // ----------------------------------------------- + + int rbuf[DIM0][DIM1]; + int numfilt; + size_t nelmts={1}, namelen={1}; + unsigned flags, filter_info, cd_values[1], idx; + char name[1]; + H5Z_filter_t filter_type; + + // Open the file and the dataset in the file. + file.openFile(FILE_NAME, H5F_ACC_RDONLY); + dataset = new DataSet(file.openDataSet( DATASET_NAME)); + + // Get the create property list of the dataset. + plist = new DSetCreatPropList(dataset->getCreatePlist ()); + + // Get the number of filters associated with the dataset. + numfilt = plist->getNfilters(); + cout << "Number of filters associated with dataset: " << numfilt << endl; + + for (idx=0; idx < numfilt; idx++) { + nelmts = 0; + + filter_type = plist->getFilter(idx, flags, nelmts, cd_values, namelen, name , filter_info); + + cout << "Filter Type: "; + + switch (filter_type) { + case H5Z_FILTER_DEFLATE: + cout << "H5Z_FILTER_DEFLATE" << endl; + break; + case H5Z_FILTER_SZIP: + cout << "H5Z_FILTER_SZIP" << endl; + break; + default: + cout << "Other filter type included." << endl; + } + } + + // Read data. + dataset->read(rbuf, PredType::NATIVE_INT); + + delete plist; + delete dataset; + file.close(); // can be skipped + + } // end of try block + + // catch failure caused by the H5File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSet operations + catch(DataSetIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSpace operations + catch(DataSpaceIException error) + { + error.printError(); + return -1; + } + + return 0; // successfully terminated +} + diff --git a/c++/examples/h5tutr_crtatt.cpp b/c++/examples/h5tutr_crtatt.cpp new file mode 100644 index 0000000..651dc69 --- /dev/null +++ b/c++/examples/h5tutr_crtatt.cpp @@ -0,0 +1,95 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#endif + +const H5std_string FILE_NAME( "dset.h5" ); +const H5std_string DATASET_NAME( "dset" ); +const H5std_string ATTR_NAME( "Units" ); + +const int DIM1 = 2; + +int main (void) +{ + int attr_data[2] = { 100, 200}; + hsize_t dims[1] = { DIM1 }; + + + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Open an existing file and dataset. + H5File file( FILE_NAME, H5F_ACC_RDWR ); + DataSet dataset = file.openDataSet( DATASET_NAME ); + + // Create the data space for the attribute. + DataSpace attr_dataspace = DataSpace (1, dims ); + + // Create a dataset attribute. + Attribute attribute = dataset.createAttribute( ATTR_NAME, PredType::STD_I32BE, + attr_dataspace); + + // Write the attribute data. + attribute.write( PredType::NATIVE_INT, attr_data); + + } // end of try block + + // catch failure caused by the H5File operations + catch( DataSpaceIException error ) + { + error.printError(); + return -1; + } + + // catch failure caused by the H5File operations + catch( AttributeIException error ) + { + error.printError(); + return -1; + } + + // catch failure caused by the H5File operations + catch( FileIException error ) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSet operations + catch( DataSetIException error ) + { + error.printError(); + return -1; + } + + return 0; // successfully terminated +} + diff --git a/c++/examples/h5tutr_crtdat.cpp b/c++/examples/h5tutr_crtdat.cpp new file mode 100644 index 0000000..d7f887e --- /dev/null +++ b/c++/examples/h5tutr_crtdat.cpp @@ -0,0 +1,82 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#endif + +const H5std_string FILE_NAME("dset.h5"); +const H5std_string DATASET_NAME("dset"); +const int NX = 4; // dataset dimensions +const int NY = 6; +const int RANK = 2; + +int main (void) +{ + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Create a new file using the default property lists. + H5File file(FILE_NAME, H5F_ACC_TRUNC); + + // Create the data space for the dataset. + hsize_t dims[2]; // dataset dimensions + dims[0] = NX; + dims[1] = NY; + DataSpace dataspace(RANK, dims); + + // Create the dataset. + DataSet dataset = file.createDataSet(DATASET_NAME, PredType::STD_I32BE, dataspace); + + } // end of try block + + // catch failure caused by the H5File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSet operations + catch(DataSetIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSpace operations + catch(DataSpaceIException error) + { + error.printError(); + return -1; + } + + return 0; // successfully terminated +} + diff --git a/c++/examples/h5tutr_crtgrp.cpp b/c++/examples/h5tutr_crtgrp.cpp new file mode 100644 index 0000000..bbafd10 --- /dev/null +++ b/c++/examples/h5tutr_crtgrp.cpp @@ -0,0 +1,69 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#ifndef H5_NO_STD + using std::cout; + using std::endl; +#endif // H5_NO_STD +#endif + +const H5std_string FILE_NAME("group.h5"); + +int main(void) +{ + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Create a new file using default property lists. + H5File file(FILE_NAME, H5F_ACC_TRUNC); + + // Create a group named "/MygGroup" in the file + Group group(file.createGroup("/MyGroup")); + + // File and group will be closed as their instances go out of scope. + + } // end of try block + + // catch failure caused by the H5File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + // catch failure caused by the Group operations + catch(GroupIException error) + { + error.printError(); + return -1; + } + + return 0; +} diff --git a/c++/examples/h5tutr_crtgrpar.cpp b/c++/examples/h5tutr_crtgrpar.cpp new file mode 100644 index 0000000..d58dff6 --- /dev/null +++ b/c++/examples/h5tutr_crtgrpar.cpp @@ -0,0 +1,92 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#ifndef H5_NO_STD + using std::cout; + using std::endl; +#endif // H5_NO_STD +#endif + +const H5std_string FILE_NAME("groups.h5"); + +int main(void) +{ + + // Try block to detect exceptions raised by any of the calls inside it + try + { + + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately. + + Exception::dontPrint(); + + // Create a new file using default properties. + + H5File file(FILE_NAME, H5F_ACC_TRUNC); + + // Create group "MyGroup" in the root group using an absolute name. + + Group group1(file.createGroup( "/MyGroup")); + + // Create group "Group_A" in group "MyGroup" using an + // absolute name. + + Group group2(file.createGroup("/MyGroup/Group_A")); + + // Create group "Group_B" in group "MyGroup" using a + // relative name. + + Group group3(group1.createGroup ("Group_B")); + + // Close the groups and file. + + group1.close(); + group2.close(); + group3.close(); + file.close(); + + } // end of try block + + // catch failure caused by the File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the Group operations + catch(GroupIException error) + { + error.printError(); + return -1; + } + + return 0; +} + + diff --git a/c++/examples/h5tutr_crtgrpd.cpp b/c++/examples/h5tutr_crtgrpd.cpp new file mode 100644 index 0000000..fdb9108 --- /dev/null +++ b/c++/examples/h5tutr_crtgrpd.cpp @@ -0,0 +1,141 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#ifndef H5_NO_STD + using std::cout; + using std::endl; +#endif // H5_NO_STD +#endif + +const H5std_string FILE_NAME("groups.h5"); +const H5std_string DATASET_NAME1("/MyGroup/dset1"); +const H5std_string DATASET_NAME2("dset2"); +const int RANK = 2; +const int D1DIM1 = 3; +const int D1DIM2 = 3; +const int D2DIM1 = 2; +const int D2DIM2 = 10; + +int main(void) +{ + int dset1_data[D1DIM1][D1DIM2], dset2_data[D2DIM1][D2DIM2]; // data buffers + int i, j; + + // Try block to catch exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Initialize the first dataset. + for (i = 0; i < D1DIM1; i++) + for (j = 0; j < D1DIM2; j++) + dset1_data[i][j] = j + 1; + + // Initialize the second dataset. + for (i = 0; i < D2DIM1; i++) + for (j = 0; j < D2DIM2; j++) + dset2_data[i][j] = j + 1; + + // Open an existing file and dataset. + H5File file(FILE_NAME, H5F_ACC_RDWR); + + // Create the data space for the first dataset. Note the use of + // pointer for the instance 'dataspace'. It can be deleted and + // used again later for another data space. An HDF5 identifier is + // closed by the destructor or the method 'close()'. + hsize_t dims[RANK]; // dataset dimensions + dims[0] = D1DIM1; + dims[1] = D1DIM2; + DataSpace *dataspace = new DataSpace (RANK, dims); + + // Create the dataset in group "MyGroup". Same note as for the + // dataspace above. + DataSet *dataset = new DataSet (file.createDataSet(DATASET_NAME1, + PredType::STD_I32BE, *dataspace)); + + // Write the data to the dataset using default memory space, file + // space, and transfer properties. + dataset->write(dset1_data, PredType::NATIVE_INT); + + // Close the current dataset and data space. + delete dataset; + delete dataspace; + + // Create the data space for the second dataset. + dims[0] = D2DIM1; + dims[1] = D2DIM2; + dataspace = new DataSpace (RANK, dims); + + // Create group "Group_A" in group "MyGroup". + Group group(file.openGroup("/MyGroup/Group_A")); + + // Create the second dataset in group "Group_A". + dataset = new DataSet (group.createDataSet(DATASET_NAME2, + PredType::STD_I32BE, *dataspace)); + + // Write the data to the dataset using default memory space, file + // space, and transfer properties. + dataset->write(dset2_data, PredType::NATIVE_INT); + + // Close all objects. + delete dataspace; + delete dataset; + group.close(); + + } // end of try block + + // catch failure caused by the H5File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + // catch failure caused by the DataSet operations + catch(DataSetIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSpace operations + catch(DataSpaceIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the Group operations + catch(GroupIException error) + { + error.printError(); + return -1; + } + + return 0; +} diff --git a/c++/examples/h5tutr_extend.cpp b/c++/examples/h5tutr_extend.cpp new file mode 100644 index 0000000..c3a18d7 --- /dev/null +++ b/c++/examples/h5tutr_extend.cpp @@ -0,0 +1,176 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#ifndef H5_NO_STD + using std::cout; + using std::endl; +#endif // H5_NO_STD +#endif + +const H5std_string FILE_NAME("extend.h5"); +const H5std_string DATASETNAME("ExtendibleArray"); + +int main (void) +{ + hsize_t dims[2] = {3,3}; // dataset dimensions at creation + hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED}; + 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} }; + + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Create a new file using the default property lists. + H5File file(FILE_NAME, H5F_ACC_TRUNC); + + // Create the data space for the dataset. Note the use of pointer + // for the instance 'dataspace'. It can be deleted and used again + // later for another dataspace. An HDF5 identifier can be closed + // by the destructor or the method 'close()'. + DataSpace *dataspace = new DataSpace (2, dims, maxdims); + + // Modify dataset creation property to enable chunking + DSetCreatPropList prop; + prop.setChunk(2, chunk_dims); + + // Create the chunked dataset. Note the use of pointer. + DataSet *dataset = new DataSet(file.createDataSet( DATASETNAME, + PredType::STD_I32BE, *dataspace, prop) ); + + // Write data to dataset. + dataset->write(data, PredType::NATIVE_INT); + + // Extend the dataset. Dataset becomes 10 x 3. + size[0] = dims[0] + dimsext[0]; + size[1] = dims[1]; + dataset->extend(size); + + // Select a hyperslab in extended portion of the dataset. + DataSpace *filespace = new DataSpace(dataset->getSpace ()); + offset[0] = 3; + offset[1] = 0; + filespace->selectHyperslab(H5S_SELECT_SET, dimsext, offset); + + // Define memory space. + DataSpace *memspace = new DataSpace(2, dimsext, NULL); + + // Write data to the extended portion of the dataset. + dataset->write(dataext, PredType::NATIVE_INT, *memspace, *filespace); + + // Close all objects and file. + prop.close(); + delete filespace; + delete memspace; + delete dataspace; + delete dataset; + file.close(); + + // --------------------------------------- + // Re-open the file and read the data back + // --------------------------------------- + + int rdata[10][3]; + int i,j, rank, rank_chunk; + hsize_t chunk_dimsr[2], dimsr[2]; + + // Open the file and dataset. + file.openFile(FILE_NAME, H5F_ACC_RDONLY); + dataset = new DataSet(file.openDataSet( DATASETNAME)); + + // Get the dataset's dataspace and creation property list. + filespace = new DataSpace(dataset->getSpace()); + prop = dataset->getCreatePlist(); + + // Get information to obtain memory dataspace. + rank = filespace->getSimpleExtentNdims(); + herr_t status_n = filespace->getSimpleExtentDims(dimsr); + + if (H5D_CHUNKED == prop.getLayout()) + rank_chunk = prop.getChunk(rank, chunk_dimsr); + + memspace = new DataSpace(rank, dimsr, NULL); + dataset->read(rdata, PredType::NATIVE_INT, *memspace, *filespace); + + cout << endl; + for (j = 0; j < dimsr[0]; j++) { + for (i = 0; i < dimsr[1]; i++) + cout << " " << rdata[j][i]; + cout << endl; + } + + // Close all objects and file. + prop.close(); + delete filespace; + delete memspace; + delete dataset; + file.close(); + + } // end of try block + + // catch failure caused by the H5File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSet operations + catch(DataSetIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSpace operations + catch(DataSpaceIException error) + { + error.printError(); + return -1; + } + + return 0; // successfully terminated +} + diff --git a/c++/examples/h5tutr_rdwt.cpp b/c++/examples/h5tutr_rdwt.cpp new file mode 100644 index 0000000..e6e45b7 --- /dev/null +++ b/c++/examples/h5tutr_rdwt.cpp @@ -0,0 +1,79 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 to and read from an existing + * dataset. It is used in the HDF5 Tutorial. + */ + +#include <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#endif + +const H5std_string FILE_NAME( "dset.h5" ); +const H5std_string DATASET_NAME( "dset" ); +const int DIM0 = 4; // dataset dimensions +const int DIM1 = 6; + +int main (void) +{ + + // Data initialization. + + int i, j; + int data[DIM0][DIM1]; // buffer for data to write + + for (j = 0; j < DIM0; j++) + for (i = 0; i < DIM1; i++) + data[j][i] = i * 6 + j + 1; + + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // Open an existing file and dataset. + H5File file( FILE_NAME, H5F_ACC_RDWR ); + DataSet dataset = file.openDataSet( DATASET_NAME ); + + // Write the data to the dataset using default memory space, file + // space, and transfer properties. + dataset.write( data, PredType::NATIVE_INT ); + + } // end of try block + + // catch failure caused by the H5File operations + catch( FileIException error ) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSet operations + catch( DataSetIException error ) + { + error.printError(); + return -1; + } + + return 0; // successfully terminated +} diff --git a/c++/examples/h5tutr_subset.cpp b/c++/examples/h5tutr_subset.cpp new file mode 100644 index 0000000..77174ab --- /dev/null +++ b/c++/examples/h5tutr_subset.cpp @@ -0,0 +1,183 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 <iostream> +#include <string> + +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE + using namespace H5; +#ifndef H5_NO_STD + using std::cout; + using std::endl; +#endif // H5_NO_STD +#endif + +const H5std_string FILE_NAME("subset.h5"); +const H5std_string DATASET_NAME("IntArray"); + +const int RANK = 2; +const int DIM0_SUB = 3; // subset dimensions +const int DIM1_SUB = 4; +const int DIM0 = 8; // size of dataset +const int DIM1 = 10; + +int main (void) +{ + int i,j; + int data[DIM0][DIM1], sdata[DIM0_SUB][DIM1_SUB], rdata[DIM0][DIM1]; + + // Try block to detect exceptions raised by any of the calls inside it + try + { + // Turn off the auto-printing when failure occurs so that we can + // handle the errors appropriately + Exception::dontPrint(); + + // --------------------------------------------------- + // Create a new file using the default property lists. + // Then create a dataset and write data to it. + // Close the file and dataset. + // --------------------------------------------------- + + H5File file(FILE_NAME, H5F_ACC_TRUNC); + + hsize_t dims[2]; + dims[0] = DIM0; + dims[1] = DIM1; + DataSpace dataspace = DataSpace (RANK, dims); + + DataSet dataset(file.createDataSet( DATASET_NAME, + PredType::STD_I32BE, dataspace) ); + + + 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; + } + + dataset.write(data, PredType::NATIVE_INT); + + cout << endl << "Data Written to File:" << endl; + for (j = 0; j < DIM0; j++) { + for (i = 0; i < DIM1; i++) + cout << " " << data[j][i]; + cout << endl; + } + + dataspace.close(); + dataset.close(); + file.close(); + + // --------------------------------------------------- + // Reopen the file and dataset and write a subset of + // values to the dataset. + // --------------------------------------------------- + + hsize_t offset[2], count[2], stride[2], block[2]; + hsize_t dimsm[2]; + + file.openFile(FILE_NAME, H5F_ACC_RDWR); + dataset = file.openDataSet(DATASET_NAME); + + // 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; + + // Define Memory Dataspace. Get file dataspace and select + // a subset from the file dataspace. + + dimsm[0] = DIM0_SUB; + dimsm[1] = DIM1_SUB; + + DataSpace memspace(RANK, dimsm, NULL); + + dataspace = dataset.getSpace(); + dataspace.selectHyperslab(H5S_SELECT_SET, count, offset, stride, block); + + // Write a subset of data to the dataset, then read the + // entire dataset back from the file. + + cout << endl << "Write subset to file specifying: " << endl; + cout << " offset=1x2 stride=1x1 count=3x4 block=1x1" << endl; + for (j = 0; j < DIM0_SUB; j++) { + for (i = 0; i < DIM1_SUB; i++) + sdata[j][i] = 5; + } + + dataset.write(sdata, PredType::NATIVE_INT, memspace, dataspace); + dataset.read(rdata, PredType::NATIVE_INT); + + + cout << endl << "Data in File after Subset is Written:" << endl; + for (i = 0; i < DIM0; i++) { + for (j = 0; j < DIM1; j++) + cout << " " << rdata[i][j]; + cout << endl; + } + cout << endl; + + // It is not necessary to close these objects because close() will + // be called when the object instances are going out of scope. + dataspace.close(); + memspace.close(); + dataset.close(); + file.close(); + + } // end of try block + + // catch failure caused by the H5File operations + catch(FileIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSet operations + catch(DataSetIException error) + { + error.printError(); + return -1; + } + + // catch failure caused by the DataSpace operations + catch(DataSpaceIException error) + { + error.printError(); + return -1; + } + + return 0; // successfully terminated +} + diff --git a/c++/examples/run-c++-ex.sh.in b/c++/examples/run-c++-ex.sh.in index 6b33577..77ac36a 100644 --- a/c++/examples/run-c++-ex.sh.in +++ b/c++/examples/run-c++-ex.sh.in @@ -64,6 +64,10 @@ RunTest() # Run tests if [ $? -eq 0 ] then + echo "*************************************************" + echo "** Run C++ API Examples **" + echo "** **" + echo "*************************************************" if (RunTest create &&\ rm create &&\ RunTest readdata &&\ @@ -82,6 +86,33 @@ then else EXIT_VALUE=${EXIT_FAILURE} fi + echo + echo "***************************************************" + echo "** Run Tutorial Examples **" + echo "** **" + echo "***************************************************" + if (RunTest h5tutr_crtdat &&\ + rm h5tutr_crtdat &&\ + RunTest h5tutr_rdwt &&\ + rm h5tutr_rdwt &&\ + RunTest h5tutr_crtatt &&\ + rm h5tutr_crtatt &&\ + RunTest h5tutr_crtgrp &&\ + rm h5tutr_crtgrp &&\ + RunTest h5tutr_crtgrpar &&\ + rm h5tutr_crtgrpar &&\ + RunTest h5tutr_crtgrpd &&\ + rm h5tutr_crtgrpd &&\ + RunTest h5tutr_extend &&\ + rm h5tutr_extend &&\ + RunTest h5tutr_subset &&\ + rm h5tutr_subset &&\ + RunTest h5tutr_cmprss &&\ + rm h5tutr_cmprss ); then + EXIT_VALUE=${EXIT_SUCCESS} + else + EXIT_VALUE=${EXIT_FAILURE} + fi fi # Cleanup |