summaryrefslogtreecommitdiffstats
path: root/c++
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2000-11-14 21:36:14 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2000-11-14 21:36:14 (GMT)
commit083fa734a39f774224b75702e401c494676c4c93 (patch)
tree5b8b64ede432af11be3a82181ef7d5455e2aabec /c++
parent92041a68656c59813619ae1f26ed211b7f028e86 (diff)
downloadhdf5-083fa734a39f774224b75702e401c494676c4c93.zip
hdf5-083fa734a39f774224b75702e401c494676c4c93.tar.gz
hdf5-083fa734a39f774224b75702e401c494676c4c93.tar.bz2
[svn-r2898]
Purpose: C++ API for 1.3.x branch Description: The *.C files are the sample code that perform some common operations to an HDF5 file and its components. The *.h5 files are the HDF5 files that are generated/used by the sample programs. Platforms tested: Solaris (arabica) and Linux
Diffstat (limited to 'c++')
-rw-r--r--c++/examples/Attributes.h5bin0 -> 2076 bytes
-rw-r--r--c++/examples/Group.h5bin0 -> 6712 bytes
-rw-r--r--c++/examples/SDS.h5bin0 -> 2168 bytes
-rw-r--r--c++/examples/SDScompound.h5bin0 -> 2208 bytes
-rw-r--r--c++/examples/SDSextendible.h5bin0 -> 4864 bytes
-rw-r--r--c++/examples/Select.h5bin0 -> 2432 bytes
-rw-r--r--c++/examples/chunks.C206
-rw-r--r--c++/examples/compound.C194
-rw-r--r--c++/examples/create.C112
-rw-r--r--c++/examples/extend_ds.C204
-rw-r--r--c++/examples/h5group.C196
-rw-r--r--c++/examples/readdata.C167
-rw-r--r--c++/examples/writedata.C227
13 files changed, 1306 insertions, 0 deletions
diff --git a/c++/examples/Attributes.h5 b/c++/examples/Attributes.h5
new file mode 100644
index 0000000..54dd9c0
--- /dev/null
+++ b/c++/examples/Attributes.h5
Binary files differ
diff --git a/c++/examples/Group.h5 b/c++/examples/Group.h5
new file mode 100644
index 0000000..d8f7e89
--- /dev/null
+++ b/c++/examples/Group.h5
Binary files differ
diff --git a/c++/examples/SDS.h5 b/c++/examples/SDS.h5
new file mode 100644
index 0000000..483dc89
--- /dev/null
+++ b/c++/examples/SDS.h5
Binary files differ
diff --git a/c++/examples/SDScompound.h5 b/c++/examples/SDScompound.h5
new file mode 100644
index 0000000..c72307f
--- /dev/null
+++ b/c++/examples/SDScompound.h5
Binary files differ
diff --git a/c++/examples/SDSextendible.h5 b/c++/examples/SDSextendible.h5
new file mode 100644
index 0000000..b0f178a
--- /dev/null
+++ b/c++/examples/SDSextendible.h5
Binary files differ
diff --git a/c++/examples/Select.h5 b/c++/examples/Select.h5
new file mode 100644
index 0000000..93ba46b
--- /dev/null
+++ b/c++/examples/Select.h5
Binary files differ
diff --git a/c++/examples/chunks.C b/c++/examples/chunks.C
new file mode 100644
index 0000000..f560b16
--- /dev/null
+++ b/c++/examples/chunks.C
@@ -0,0 +1,206 @@
+/*
+ * This example shows how to read data from a chunked dataset.
+ * We will read from the file created by extend.C
+ */
+
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "SDSextendible.h5" );
+const string DATASET_NAME( "ExtendibleArray" );
+const int NX = 10;
+const int NY = 5;
+const int RANK = 2;
+const int RANKC = 1;
+
+int main (void)
+{
+ hsize_t i, j;
+
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ /*
+ * Open the file and the dataset.
+ */
+ H5File file( FILE_NAME, H5F_ACC_RDONLY );
+ DataSet dataset = file.openDataSet( DATASET_NAME );
+
+ /*
+ * Get dataset rank and dimension.
+ */
+ // Get filespace first.
+ DataSpace filespace = dataset.getSpace();
+
+ // Get number of dimensions in the file dataspace
+ int rank = filespace.getSimpleExtentNdims();
+
+ // Get and print the dimension sizes of the file dataspace
+ hsize_t dims[2]; // dataset dimensions
+ rank = filespace.getSimpleExtentDims( dims );
+ cout << "dataset rank = " << rank << ", dimensions "
+ << (unsigned long)(dims[0]) << " x " << (unsigned long)(dims[1])
+ << endl;
+
+ /*
+ * Get creation properties list.
+ */
+ DSetCreatPropList cparms = dataset.getCreatePlist();
+
+ /*
+ * Check if dataset is chunked.
+ */
+ hsize_t chunk_dims[2];
+ int rank_chunk;
+ if( H5D_CHUNKED == cparms.getLayout() )
+ {
+ /*
+ * Get chunking information: rank and dimensions
+ */
+ rank_chunk = cparms.getChunk( 2, chunk_dims);
+ cout << "chunk rank " << rank_chunk << "dimensions "
+ << (unsigned long)(chunk_dims[0]) << " x "
+ << (unsigned long)(chunk_dims[1]) << endl;
+ }
+
+ /*
+ * Define the memory space to read dataset.
+ */
+ DataSpace mspace1( RANK, dims );
+
+ /*
+ * Read dataset back and display.
+ */
+ int data_out[NX][NY]; // buffer for dataset to be read
+ dataset.read( data_out, PredType::NATIVE_INT, mspace1, filespace );
+ cout << "\n";
+ cout << "Dataset: \n";
+ for (j = 0; j < dims[0]; j++)
+ {
+ for (i = 0; i < dims[1]; i++)
+ cout << data_out[j][i] << " ";
+ cout << endl;
+ }
+
+ /*
+ * dataset rank 2, dimensions 10 x 5
+ * chunk rank 2, dimensions 2 x 5
+
+ * Dataset:
+ * 1 1 1 3 3
+ * 1 1 1 3 3
+ * 1 1 1 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ */
+
+ /*
+ * Read the third column from the dataset.
+ * First define memory dataspace, then define hyperslab
+ * and read it into column array.
+ */
+ hsize_t col_dims[1];
+ col_dims[0] = 10;
+ DataSpace mspace2( RANKC, col_dims );
+
+ /*
+ * Define the column (hyperslab) to read.
+ */
+ hssize_t offset[2] = { 0, 2 };
+ hsize_t count[2] = { 10, 1 };
+ int column[10]; // buffer for column to be read
+
+ filespace.selectHyperslab( H5S_SELECT_SET, count, offset );
+ dataset.read( column, PredType::NATIVE_INT, mspace2, filespace );
+
+ cout << endl;
+ cout << "Third column: " << endl;
+ for (i = 0; i < 10; i++)
+ cout << column[i] << endl;
+
+ /*
+ * Third column:
+ * 1
+ * 1
+ * 1
+ * 0
+ * 0
+ * 0
+ * 0
+ * 0
+ * 0
+ * 0
+ */
+
+ /*
+ * Define the memory space to read a chunk.
+ */
+ DataSpace mspace3( rank_chunk, chunk_dims );
+
+ /*
+ * Define chunk in the file (hyperslab) to read.
+ */
+ offset[0] = 2;
+ offset[1] = 0;
+ count[0] = chunk_dims[0];
+ count[1] = chunk_dims[1];
+ filespace.selectHyperslab( H5S_SELECT_SET, count, offset );
+
+ /*
+ * Read chunk back and display.
+ */
+ int chunk_out[2][5]; // buffer for chunk to be read
+ dataset.read( chunk_out, PredType::NATIVE_INT, mspace3, filespace );
+ cout << endl;
+ cout << "Chunk:" << endl;
+ for (j = 0; j < chunk_dims[0]; j++)
+ {
+ for (i = 0; i < chunk_dims[1]; i++)
+ cout << chunk_out[j][i] << " ";
+ cout << endl;
+ }
+ /*
+ * Chunk:
+ * 1 1 1 0 0
+ * 2 0 0 0 0
+ */
+ } // end of try block
+
+ // catch failure caused by the H5File operations
+ catch( FileIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ return 0;
+}
diff --git a/c++/examples/compound.C b/c++/examples/compound.C
new file mode 100644
index 0000000..ba7f68f
--- /dev/null
+++ b/c++/examples/compound.C
@@ -0,0 +1,194 @@
+/*
+ * This example shows how to create a compound datatype,
+ * write an array which has the compound datatype to the file,
+ * and read back fields' subsets.
+ */
+
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "SDScompound.h5" );
+const string DATASET_NAME( "ArrayOfStructures" );
+const string MEMBER1( "a_name" );
+const string MEMBER2( "b_name" );
+const string MEMBER3( "c_name" );
+const int LENGTH = 10;
+const int RANK = 1;
+
+int main(void)
+{
+ /* First structure and dataset*/
+ typedef struct s1_t {
+ int a;
+ float b;
+ double c;
+ } s1_t;
+
+ /* Second structure (subset of s1_t) and dataset*/
+ typedef struct s2_t {
+ double c;
+ int a;
+ } s2_t;
+
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ /*
+ * Initialize the data
+ */
+ int i;
+ s1_t s1[LENGTH];
+ for (i = 0; i< LENGTH; i++)
+ {
+ s1[i].a = i;
+ s1[i].b = i*i;
+ s1[i].c = 1./(i+1);
+ }
+
+ /*
+ * Create the data space.
+ */
+ hsize_t dim[] = {LENGTH}; /* Dataspace dimensions */
+ DataSpace space( RANK, dim );
+
+ /*
+ * Create the file.
+ */
+ H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
+
+ /*
+ * Create the memory datatype.
+ */
+ CompType mtype1( sizeof(s1_t) );
+ mtype1.insertMember( MEMBER1, HOFFSET(s1_t, a), PredType::NATIVE_INT);
+ mtype1.insertMember( MEMBER3, HOFFSET(s1_t, c), PredType::NATIVE_DOUBLE);
+ mtype1.insertMember( MEMBER2, HOFFSET(s1_t, b), PredType::NATIVE_FLOAT);
+
+ /*
+ * Create the dataset.
+ */
+ DataSet* dataset;
+ dataset = new DataSet( file->createDataSet( DATASET_NAME, mtype1, space ));
+
+ /*
+ * Wtite data to the dataset;
+ */
+ dataset->write( s1, mtype1 );
+
+ /*
+ * Release resources
+ */
+ delete dataset;
+ delete file;
+
+ // Get the class of the first member in mtype1, then get its type
+ H5T_class_t member1_class = mtype1.getMemberClass( 2 );
+ if( member1_class == H5T_FLOAT )
+ {
+ FloatType member2 = mtype1.getMemberFloatType( 2 );
+ string norm_string;
+ H5T_norm_t norm = member2.getNorm( norm_string );
+ cout << "Normalization type is " << norm_string << endl;
+ }
+
+ /*
+ * Open the file and the dataset.
+ */
+ file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
+ dataset = new DataSet (file->openDataSet( DATASET_NAME ));
+
+ /*
+ * Create a datatype for s2
+ */
+ CompType mtype2( sizeof(s2_t) );
+
+ mtype2.insertMember( MEMBER3, HOFFSET(s2_t, c), PredType::NATIVE_DOUBLE);
+ mtype2.insertMember( MEMBER1, HOFFSET(s2_t, a), PredType::NATIVE_INT);
+
+ /*
+ * Read two fields c and a from s1 dataset. Fields in the file
+ * are found by their names "c_name" and "a_name".
+ */
+ s2_t s2[LENGTH];
+ dataset->read( s2, mtype2 );
+
+ /*
+ * Display the fields
+ */
+ cout << endl << "Field c : " << endl;
+ for( i = 0; i < LENGTH; i++)
+ cout << s2[i].c << " ";
+ cout << endl;
+
+ cout << endl << "Field a : " << endl;
+ for( i = 0; i < LENGTH; i++)
+ cout << s2[i].a << " ";
+ cout << endl;
+
+ /*
+ * Create a datatype for s3.
+ */
+ CompType mtype3( sizeof(float) );
+
+ mtype3.insertMember( MEMBER2, 0, PredType::NATIVE_FLOAT);
+
+ /*
+ * Read field b from s1 dataset. Field in the file is found by its name.
+ */
+ float s3[LENGTH]; // Third "structure" - used to read float field of s1)
+ dataset->read( s3, mtype3 );
+
+ /*
+ * Display the field
+ */
+ cout << endl << "Field b : " << endl;
+ for( i = 0; i < LENGTH; i++)
+ cout << s3[i] << " ";
+ cout << endl;
+
+ /*
+ * Release resources
+ */
+ delete dataset;
+ delete file;
+ } // end of try block
+
+ // catch failure caused by the H5File operations
+ catch( FileIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataTypeIException error )
+ {
+ error.printError();
+ }
+
+ return 0;
+}
diff --git a/c++/examples/create.C b/c++/examples/create.C
new file mode 100644
index 0000000..f17bd09
--- /dev/null
+++ b/c++/examples/create.C
@@ -0,0 +1,112 @@
+//
+// This example writes a dataset to a new HDF5 file.
+//
+
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "SDS.h5" );
+const string DATASET_NAME( "IntArray" );
+const int NX = 5; // dataset dimensions
+const int NY = 6;
+const int RANK = 2;
+
+int main (void)
+{
+ //
+ // Data initialization.
+ //
+ int i, j;
+ int data[NX][NY]; // buffer for data to write
+ for (j = 0; j < NX; j++)
+ {
+ for (i = 0; i < NY; i++)
+ data[j][i] = i + j;
+ }
+ //
+ // 0 1 2 3 4 5
+ // 1 2 3 4 5 6
+ // 2 3 4 5 6 7
+ // 3 4 5 6 7 8
+ // 4 5 6 7 8 9
+ //
+
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ //
+ // Create a new file using H5F_ACC_TRUNC access,
+ // default file creation properties, and default file
+ // access properties.
+ //
+ H5File file( FILE_NAME, H5F_ACC_TRUNC );
+
+ //
+ // Define the size of the array and create the data space for fixed
+ // size dataset.
+ //
+ hsize_t dimsf[2]; // dataset dimensions
+ dimsf[0] = NX;
+ dimsf[1] = NY;
+ DataSpace dataspace( RANK, dimsf );
+
+ //
+ // Define datatype for the data in the file.
+ // We will store little endian INT numbers.
+ //
+ IntType datatype( PredType::NATIVE_INT );
+ datatype.setOrder( H5T_ORDER_LE );
+
+ //
+ // Create a new dataset within the file using defined dataspace and
+ // datatype and default dataset creation properties.
+ //
+ DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
+
+ //
+ // 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();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataTypeIException error )
+ {
+ error.printError();
+ }
+
+ return 0; // successfully terminated
+}
+
diff --git a/c++/examples/extend_ds.C b/c++/examples/extend_ds.C
new file mode 100644
index 0000000..e0eb877
--- /dev/null
+++ b/c++/examples/extend_ds.C
@@ -0,0 +1,204 @@
+/*
+ * This example shows how to work with extendible dataset.
+ * In the current version of the library dataset MUST be
+ * chunked.
+ *
+ */
+
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "SDSextendible.h5" );
+const string DATASET_NAME( "ExtendibleArray" );
+const int NX = 10;
+const int NY = 5;
+const int RANK = 2;
+
+int main (void)
+{
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ /*
+ * Create the data space with unlimited dimensions.
+ */
+ hsize_t dims[2] = { 3, 3}; // dataset dimensions at creation
+ hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
+ DataSpace mspace1( RANK, dims, maxdims);
+
+ /*
+ * Create a new file. If file exists its contents will be overwritten.
+ */
+ H5File file( FILE_NAME, H5F_ACC_TRUNC );
+
+ /*
+ * Modify dataset creation properties, i.e. enable chunking.
+ */
+ DSetCreatPropList cparms;
+
+ hsize_t chunk_dims[2] ={2, 5};
+ cparms.setChunk( RANK, chunk_dims );
+
+ /*
+ * Create a new dataset within the file using cparms
+ * creation properties.
+ */
+
+ DataSet dataset = file.createDataSet( DATASET_NAME, PredType::NATIVE_INT, mspace1, cparms);
+
+ /*
+ * Extend the dataset. This call assures that dataset is at least 3 x 3.
+ */
+ hsize_t size[2];
+ size[0] = 3;
+ size[1] = 3;
+ dataset.extend( size );
+
+ /*
+ * Select a hyperslab.
+ */
+ DataSpace fspace1 = dataset.getSpace ();
+ hssize_t offset[2];
+ offset[0] = 0;
+ offset[1] = 0;
+ hsize_t dims1[2] = { 3, 3}; /* data1 dimensions */
+ fspace1.selectHyperslab( H5S_SELECT_SET, dims1, offset );
+
+ /*
+ * Write the data to the hyperslab.
+ */
+ int data1[3][3] = { {1, 1, 1}, /* data to write */
+ {1, 1, 1},
+ {1, 1, 1} };
+ dataset.write( data1, PredType::NATIVE_INT, mspace1, fspace1 );
+
+ /*
+ * Extend the dataset. Dataset becomes 10 x 3.
+ */
+ hsize_t dims2[2] = { 7, 1}; /* data2 dimensions */
+ dims[0] = dims1[0] + dims2[0];
+ size[0] = dims[0];
+ size[1] = dims[1];
+ dataset.extend( size );
+
+ /*
+ * Select a hyperslab.
+ */
+ DataSpace fspace2 = dataset.getSpace ();
+ offset[0] = 3;
+ offset[1] = 0;
+ fspace2.selectHyperslab( H5S_SELECT_SET, dims2, offset );
+
+ /*
+ * Define memory space
+ */
+ DataSpace mspace2( RANK, dims2 );
+
+ /*
+ * Write the data to the hyperslab.
+ */
+ int data2[7] = { 2, 2, 2, 2, 2, 2, 2};
+ dataset.write( data2, PredType::NATIVE_INT, mspace2, fspace2 );
+
+ /*
+ * Extend the dataset. Dataset becomes 10 x 5.
+ */
+ hsize_t dims3[2] = { 2, 2}; /* data3 dimensions */
+ dims[1] = dims1[1] + dims3[1];
+ size[0] = dims[0];
+ size[1] = dims[1];
+ dataset.extend( size );
+
+ /*
+ * Select a hyperslab
+ */
+ DataSpace fspace3 = dataset.getSpace ();
+ offset[0] = 0;
+ offset[1] = 3;
+ fspace3.selectHyperslab( H5S_SELECT_SET, dims3, offset );
+
+ /*
+ * Define memory space.
+ */
+ DataSpace mspace3( RANK, dims3 );
+
+ /*
+ * Write the data to the hyperslab.
+ */
+ int data3[2][2] = { {3, 3}, {3, 3} };
+ dataset.write( data3, PredType::NATIVE_INT, mspace3, fspace3 );
+
+ /*
+ * Read the data from this dataset and display it.
+ */
+ int i, j;
+ int data_out[NX][NY];
+ for (i = 0; i < NX; i++)
+ {
+ for (j = 0; j < NY; j++)
+ data_out[i][j] = 0;
+ }
+ dataset.read( data_out, PredType::NATIVE_INT );
+ /*
+ * Resulting dataset
+ *
+ * 1 1 1 3 3
+ * 1 1 1 3 3
+ * 1 1 1 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ * 2 0 0 0 0
+ */
+ /*
+ * Display the result.
+ */
+ for (i=0; i < NX; i++)
+ {
+ for(j=0; j < NY; j++)
+ cout << data_out[i][j] << " ";
+ cout << endl;
+ }
+ } // end of try block
+
+ // catch failure caused by the H5File operations
+ catch( FileIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataTypeIException error )
+ {
+ error.printError();
+ }
+ return 0;
+}
diff --git a/c++/examples/h5group.C b/c++/examples/h5group.C
new file mode 100644
index 0000000..514e002
--- /dev/null
+++ b/c++/examples/h5group.C
@@ -0,0 +1,196 @@
+//
+// This example creates a group in the file and dataset in the group.
+// Hard link to the group object is created and the dataset is accessed
+// under different names.
+// Iterator function is used to find the object names in the root group.
+// Note that the C++ API iterator function is not completed yet, thus
+// the C version is used in this example.
+//
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "Group.h5" );
+const int RANK = 2;
+
+// Operator function
+extern "C" herr_t file_info(hid_t loc_id, const char *name, void *opdata);
+
+int main(void)
+{
+
+ hsize_t dims[2];
+ hsize_t cdims[2];
+
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ // Create the named file, truncating the existing one if any,
+ // using default create and access property lists.
+ H5File *file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
+
+ // Create a group in the file
+ Group* group = new Group( file->createGroup( "/Data" ));
+
+ //
+ // Create dataset "Compressed Data" in the group using absolute
+ // name. Dataset creation property list is modified to use
+ // GZIP compression with the compression effort set to 6.
+ // Note that compression can be used only when dataset is chunked.
+ //
+ dims[0] = 1000;
+ dims[1] = 20;
+ cdims[0] = 20;
+ cdims[1] = 20;
+ DataSpace dataspace( RANK, dims ); // create the new dataspace
+ // for the dataset
+
+ DSetCreatPropList ds_creatplist; // create dataset creation prop list
+ ds_creatplist.setChunk( 2, cdims ); // then modify it for compression
+ ds_creatplist.setDeflate( 6 );
+
+ DataSet* dataset = new DataSet( file->createDataSet( "/Data/Compressed_Data", PredType::NATIVE_INT, dataspace, ds_creatplist ));
+
+ //
+ // Close the dataset and the file.
+ //
+ delete dataset;
+ delete group;
+ delete file;
+
+ //
+ // Now reopen the file and group in the file.
+ //
+ file = new H5File( FILE_NAME, H5F_ACC_RDWR );
+ group = new Group( file->openGroup( "Data" ));
+
+ //
+ // Access "Compressed_Data" dataset in the group.
+ //
+ try { // to determine if the dataset exists in the group
+ dataset = new DataSet( group->openDataSet( "Compressed_Data" ));
+ }
+ catch( GroupIException not_found_error )
+ {
+ cout << " Dataset is not found." << endl;
+ }
+ cout << "dataset \"/Data/Compressed_Data\" is open" << endl;
+
+ //
+ // Close the dataset.
+ //
+ delete dataset;
+
+ //
+ // Create hard link to the Data group.
+ //
+ file->link( H5G_LINK_HARD, "Data", "Data_new" );
+
+ //
+ // We can access "Compressed_Data" dataset using created
+ // hard link "Data_new".
+ //
+ try { // to determine if the dataset exists in the file
+ dataset = new DataSet( file->openDataSet( "/Data_new/Compressed_Data" ));
+ }
+ catch( FileIException not_found_error )
+ {
+ cout << " Dataset is not found." << endl;
+ }
+ cout << "dataset \"/Data_new/Compressed_Data\" is open" << endl;
+
+ //
+ // Close the dataset.
+ //
+ delete dataset;
+
+ //
+ // Use iterator to see the names of the objects in the file
+ // root directory.
+ //
+ cout << endl << "Iterating over elements in the file" << endl;
+ herr_t idx = H5Giterate(file->getId(), "/", NULL, file_info, NULL);
+ cout << endl;
+
+ //
+ // Unlink name "Data" and use iterator to see the names
+ // of the objects in the file root direvtory.
+ //
+ cout << "Unlinking..." << endl;
+ try { // attempt to unlink the dataset
+ file->unlink( "Data" );
+ }
+ catch( FileIException unlink_error )
+ {
+ cout << " unlink failed." << endl;
+ }
+ cout << "\"Data\" is unlinked" << endl;
+
+ cout << endl << "Iterating over elements in the file again" << endl;
+ idx = H5Giterate(file->getId(), "/", NULL, file_info, NULL);
+ cout << endl;
+
+ // Close the file.
+ delete file;
+
+ } // end of try block
+
+ // catch failure caused by the H5File operations
+ catch( FileIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the Attribute operations
+ catch( AttributeIException error )
+ {
+ error.printError();
+ }
+ return 0;
+}
+
+//
+// Operator function.
+//
+herr_t
+file_info(hid_t loc_id, const char *name, void *opdata)
+{
+ hid_t group;
+ //
+ // Open the group using its name.
+ //
+ group = H5Gopen(loc_id, name);
+
+ //
+ // Display group name.
+ //
+ cout << "Name : " << name << endl;
+
+ H5Gclose(group);
+ return 0;
+ }
+
diff --git a/c++/examples/readdata.C b/c++/examples/readdata.C
new file mode 100644
index 0000000..d98602b
--- /dev/null
+++ b/c++/examples/readdata.C
@@ -0,0 +1,167 @@
+//
+// This example reads hyperslab from the SDS.h5 file into
+// two-dimensional plane of a three-dimensional array. Various
+// information about the dataset in the SDS.h5 file is obtained.
+//
+
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "SDS.h5" );
+const string DATASET_NAME( "IntArray" );
+const int NX_SUB = 3; // hyperslab dimensions
+const int NY_SUB = 4;
+const int NX = 7; // output buffer dimensions
+const int NY = 7;
+const int NZ = 3;
+const int RANK_OUT = 3;
+
+int main (void)
+{
+ //
+ // Output buffer initialization.
+ //
+ int i, j, k;
+ int data_out[NX][NY][NZ ]; /* output buffer */
+ for (j = 0; j < NX; j++)
+ {
+ for (i = 0; i < NY; i++)
+ {
+ for (k = 0; k < NZ ; k++)
+ data_out[j][i][k] = 0;
+ }
+ }
+
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ //
+ // Open the specified file and the specified dataset in the file.
+ //
+ H5File file( FILE_NAME, H5F_ACC_RDONLY );
+ DataSet dataset = file.openDataSet( DATASET_NAME );
+
+ // Get the class of the datatype that is used by the dataset.
+ H5T_class_t type_class = dataset.getTypeClass();
+
+ // Get class of datatype and print message if it's an integer.
+ if( type_class == H5T_INTEGER )
+ {
+ cout << "Data set has INTEGER type" << endl;
+
+ // Get the integer datatype
+ IntType intype = dataset.getIntType();
+
+ // Get order of datatype and print message if it's a little endian.
+ string order_string;
+ H5T_order_t order = intype.getOrder( order_string );
+ cout << order_string << endl;
+
+ // Get size of the data element stored in file and print it.
+ size_t size = intype.getSize();
+ cout << "Data size is " << size << endl;
+ }
+
+ // Get dataspace of the dataset.
+ DataSpace dataspace = dataset.getSpace();
+
+ // Get the number of dimensions in the dataspace.
+ int rank = dataspace.getSimpleExtentNdims();
+
+ // Get the dimension size of each dimension in the dataspace and
+ // display them.
+ hsize_t dims_out[2];
+ int ndims = dataspace.getSimpleExtentDims( dims_out, NULL);
+ cout << "rank " << rank << ", dimensions " <<
+ (unsigned long)(dims_out[0]) << " x " <<
+ (unsigned long)(dims_out[1]) << endl;
+
+ // Define hyperslab in the dataset; implicitly giving strike and
+ // block NULL.
+ hssize_t offset[2]; // hyperslab offset in the file
+ hsize_t count[2]; // size of the hyperslab in the file
+ offset[0] = 1;
+ offset[1] = 2;
+ count[0] = NX_SUB;
+ count[1] = NY_SUB;
+ dataspace.selectHyperslab( H5S_SELECT_SET, count, offset );
+
+ // Define the memory dataspace.
+ hsize_t dimsm[3]; /* memory space dimensions */
+ dimsm[0] = NX;
+ dimsm[1] = NY;
+ dimsm[2] = NZ ;
+ DataSpace memspace( RANK_OUT, dimsm );
+
+ // Define memory hyperslab.
+ hssize_t offset_out[3]; // hyperslab offset in memory
+ hsize_t count_out[3]; // size of the hyperslab in memory
+ offset_out[0] = 3;
+ offset_out[1] = 0;
+ offset_out[2] = 0;
+ count_out[0] = NX_SUB;
+ count_out[1] = NY_SUB;
+ count_out[2] = 1;
+ memspace.selectHyperslab( H5S_SELECT_SET, count_out, offset_out );
+
+ // Read data from hyperslab in the file into the hyperslab in
+ // memory and display the data.
+ dataset.read( data_out, PredType::NATIVE_INT, memspace, dataspace );
+
+ for (j = 0; j < NX; j++)
+ {
+ for (i = 0; i < NY; i++)
+ cout << data_out[j][i][0] << " ";
+ cout << endl;
+ }
+ /*
+ * 0 0 0 0 0 0 0
+ * 0 0 0 0 0 0 0
+ * 0 0 0 0 0 0 0
+ * 3 4 5 6 0 0 0
+ * 4 5 6 7 0 0 0
+ * 5 6 7 8 0 0 0
+ * 0 0 0 0 0 0 0
+ */
+ } // end of try block
+
+ // catch failure caused by the H5File operations
+ catch( FileIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataTypeIException error )
+ {
+ error.printError();
+ }
+
+ return 0; // successfully terminated
+}
+
diff --git a/c++/examples/writedata.C b/c++/examples/writedata.C
new file mode 100644
index 0000000..8179113
--- /dev/null
+++ b/c++/examples/writedata.C
@@ -0,0 +1,227 @@
+/*
+ * This program shows how the select_hyperslab and select_elements
+ * functions are used to write selected data from memory to the file.
+ * Program takes 48 elements from the linear buffer and writes them into
+ * the matrix using 3x2 blocks, (4,3) stride and (2,4) count.
+ * Then four elements of the matrix are overwritten with the new values and
+ * file is closed. Program reopens the file and reads and displays the result.
+ */
+
+#include <string>
+#ifndef H5_NO_NAMESPACE
+using namespace std;
+#endif
+
+#ifdef OLD_HEADER_FILENAME
+#include <iostream.h>
+#else
+#include <iostream>
+#endif
+#include "H5Cpp.h"
+
+#ifndef H5_NO_NAMESPACE
+using namespace H5;
+#endif
+
+const string FILE_NAME( "Select.h5" );
+const string DATASET_NAME( "Matrix in file" );
+const int MSPACE1_RANK = 1; // Rank of the first dataset in memory
+const int MSPACE1_DIM = 50; // Dataset size in memory
+const int MSPACE2_RANK = 1; // Rank of the second dataset in memory
+const int MSPACE2_DIM = 4; // Dataset size in memory
+const int FSPACE_RANK = 2; // Dataset rank as it is stored in the file
+const int FSPACE_DIM1 = 8; // Dimension sizes of the dataset as it is...
+const int FSPACE_DIM2 = 12; // ...stored in the file
+const int MSPACE_DIM1 = 8; // We will read dataset back from the file...
+const int MSPACE_DIM2 = 12; // ...to the dataset in memory with these ...
+ // ...dataspace parameters
+const int NPOINTS = 4; // Number of points that will be selected...
+ // ...and overwritten
+
+int main (void)
+{
+ /*
+ * Buffers' initialization.
+ */
+ uint i,j;
+ int vector[MSPACE1_DIM];
+ vector[0] = vector[MSPACE1_DIM - 1] = -1;
+ for (i = 1; i < MSPACE1_DIM - 1; i++)
+ vector[i] = i;
+
+ int matrix[MSPACE_DIM1][MSPACE_DIM2];
+ for (i = 0; i < MSPACE_DIM1; i++)
+ {
+ for (j = 0; j < MSPACE_DIM2; j++)
+ matrix[i][j] = 0;
+ }
+
+ // Try block to detect exceptions raised by any of the calls inside it
+ try
+ {
+ //
+ //Create a file.
+ //
+ H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
+
+ //
+ //Create dataspace for the dataset in the file.
+ //
+ hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; // dim sizes of ds (on disk)
+ DataSpace fspace( FSPACE_RANK, fdim );
+
+ //
+ // Create dataset and write it into the file.
+ //
+ DataSet* dataset = new DataSet(
+ file->createDataSet( DATASET_NAME, PredType::NATIVE_INT, fspace ));
+ dataset->write( matrix, PredType::NATIVE_INT );
+
+ //
+ // Select hyperslab for the dataset in the file, using 3x2 blocks,
+ // (4,3) stride and (2,4) count starting at the position (0,1).
+ //
+ hssize_t start[2]; // Start of hyperslab
+ hsize_t stride[2]; // Stride of hyperslab
+ hsize_t count[2]; // Block count
+ hsize_t block[2]; // Block sizes
+ start[0] = 0; start[1] = 1;
+ stride[0] = 4; stride[1] = 3;
+ count[0] = 2; count[1] = 4;
+ block[0] = 3; block[1] = 2;
+ fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
+
+ //
+ // Create dataspace for the first dataset.
+ //
+ hsize_t dim1[] = {MSPACE1_DIM}; /* Dimension size of the first dataset
+ (in memory) */
+ DataSpace mspace1( MSPACE1_RANK, dim1 );
+
+ /*
+ * Select hyperslab.
+ * We will use 48 elements of the vector buffer starting at the
+ * second element. Selected elements are 1 2 3 . . . 48
+ */
+ start[0] = 1;
+ stride[0] = 1;
+ count[0] = 48;
+ block[0] = 1;
+ mspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
+
+ /*
+ * Write selection from the vector buffer to the dataset in the file.
+ *
+ * File dataset should look like this:
+ * 0 1 2 0 3 4 0 5 6 0 7 8
+ * 0 9 10 0 11 12 0 13 14 0 15 16
+ * 0 17 18 0 19 20 0 21 22 0 23 24
+ * 0 0 0 0 0 0 0 0 0 0 0 0
+ * 0 25 26 0 27 28 0 29 30 0 31 32
+ * 0 33 34 0 35 36 0 37 38 0 39 40
+ * 0 41 42 0 43 44 0 45 46 0 47 48
+ * 0 0 0 0 0 0 0 0 0 0 0 0
+ */
+ dataset->write( vector, PredType::NATIVE_INT, mspace1, fspace );
+
+ /*
+ * Reset the selection for the file dataspace fid.
+ */
+ fspace.selectNone();
+
+ /*
+ * Create dataspace for the second dataset.
+ */
+ hsize_t dim2[] = {MSPACE2_DIM}; /* Dimension size of the second dataset
+ (in memory */
+ DataSpace mspace2( MSPACE2_RANK, dim2 );
+
+ /*
+ * Select sequence of NPOINTS points in the file dataspace.
+ */
+ hssize_t coord[NPOINTS][FSPACE_RANK]; /* Array to store selected points
+ from the file dataspace */
+ coord[0][0] = 0; coord[0][1] = 0;
+ coord[1][0] = 3; coord[1][1] = 3;
+ coord[2][0] = 3; coord[2][1] = 5;
+ coord[3][0] = 5; coord[3][1] = 6;
+
+ fspace.selectElements( H5S_SELECT_SET, NPOINTS, (const hssize_t **)coord);
+
+ /*
+ * Write new selection of points to the dataset.
+ */
+ int values[] = {53, 59, 61, 67}; /* New values to be written */
+ dataset->write( values, PredType::NATIVE_INT, mspace2, fspace );
+
+ /*
+ * File dataset should look like this:
+ * 53 1 2 0 3 4 0 5 6 0 7 8
+ * 0 9 10 0 11 12 0 13 14 0 15 16
+ * 0 17 18 0 19 20 0 21 22 0 23 24
+ * 0 0 0 59 0 61 0 0 0 0 0 0
+ * 0 25 26 0 27 28 0 29 30 0 31 32
+ * 0 33 34 0 35 36 67 37 38 0 39 40
+ * 0 41 42 0 43 44 0 45 46 0 47 48
+ * 0 0 0 0 0 0 0 0 0 0 0 0
+ *
+ */
+
+ /*
+ * Close the dataset and the file.
+ */
+ delete dataset;
+ delete file;
+
+ /*
+ * Open the file.
+ */
+ file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
+
+ /*
+ * Open the dataset.
+ */
+ dataset = new DataSet( file->openDataSet( DATASET_NAME ));
+
+ /*
+ * Read data back to the buffer matrix.
+ */
+ dataset->read( matrix, PredType::NATIVE_INT );
+
+ /*
+ * Display the result.
+ */
+ for (i=0; i < MSPACE_DIM1; i++)
+ {
+ for(j=0; j < MSPACE_DIM2; j++)
+ cout << matrix[i][j] << " ";
+ cout << endl;
+ }
+
+ /*
+ * Close the dataset and the file.
+ */
+ delete dataset;
+ delete file;
+ } // end of try block
+
+ // catch failure caused by the H5File operations
+ catch( FileIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSet operations
+ catch( DataSetIException error )
+ {
+ error.printError();
+ }
+
+ // catch failure caused by the DataSpace operations
+ catch( DataSpaceIException error )
+ {
+ error.printError();
+ }
+
+ return 0;
+}