diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2013-09-20 12:16:23 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2013-09-20 12:16:23 (GMT) |
commit | 10a843ff7fab2e14ac2451b0ef15134126ff462d (patch) | |
tree | 2e5ce1457846528c3a89503c3974a32feb84cda8 | |
parent | c965b5503b845c92fd8807dec81a21504dfc0b06 (diff) | |
download | hdf5-10a843ff7fab2e14ac2451b0ef15134126ff462d.zip hdf5-10a843ff7fab2e14ac2451b0ef15134126ff462d.tar.gz hdf5-10a843ff7fab2e14ac2451b0ef15134126ff462d.tar.bz2 |
[svn-r24174] Purpose: Add examples
Description:
Added tutorial examples that Barbara made following the C tutorial examples.
They will be configured for daily test.
Platforms tested:
Linux/32 2.6 (jam) - tested by running a script that compiled them with
h5c++ and executing the examples; output are checked manually
-rw-r--r-- | c++/examples/h5tutr_cmprss.cpp | 162 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtatt.cpp | 104 | ||||
-rw-r--r-- | c++/examples/h5tutr_crtdat.cpp | 91 | ||||
-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 | 84 | ||||
-rw-r--r-- | c++/examples/h5tutr_subset.cpp | 185 |
9 files changed, 1104 insertions, 0 deletions
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..3d5b558 --- /dev/null +++ b/c++/examples/h5tutr_crtatt.cpp @@ -0,0 +1,104 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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..592cc0c --- /dev/null +++ b/c++/examples/h5tutr_crtdat.cpp @@ -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 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); + + // Define the datatype to be used + //IntType datatype(PredType::STD_I32BE); + + // 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..6437fb8 --- /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]; + + file.openFile(FILE_NAME, H5F_ACC_RDONLY); + + dataset = new DataSet(file.openDataSet( DATASETNAME)); + + filespace = new DataSpace(dataset->getSpace()); + + prop = dataset->getCreatePlist(); + 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..a0c1996 --- /dev/null +++ b/c++/examples/h5tutr_rdwt.cpp @@ -0,0 +1,84 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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..76e63fd --- /dev/null +++ b/c++/examples/h5tutr_subset.cpp @@ -0,0 +1,185 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 +} + |