HDF5 C++ API Reference Manual

 

 

compound.cpp

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.
00001 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00002  * Copyright by The HDF Group.                                               *
00003  * Copyright by the Board of Trustees of the University of Illinois.         *
00004  * All rights reserved.                                                      *
00005  *                                                                           *
00006  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
00007  * terms governing use, modification, and redistribution, is contained in    *
00008  * the files COPYING and Copyright.html.  COPYING can be found at the root   *
00009  * of the source code distribution tree; Copyright.html can be found at the  *
00010  * root level of an installed copy of the electronic HDF5 document set and   *
00011  * is linked from the top-level documents page.  It can also be found at     *
00012  * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
00013  * access to either file, you may request a copy from [email protected].     *
00014  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00015 
00016 /*
00017  * This example shows how to create a compound datatype,
00018  * write an array which has the compound datatype to the file,
00019  * and read back fields' subsets.
00020  */
00021 
00022 #ifdef OLD_HEADER_FILENAME
00023 #include <iostream.h>
00024 #else
00025 #include <iostream>
00026 #endif
00027 #include <string>
00028 
00029 #ifndef H5_NO_NAMESPACE
00030 #ifndef H5_NO_STD
00031     using std::cout;
00032     using std::endl;
00033 #endif  // H5_NO_STD
00034 #endif
00035 
00036 #include "H5Cpp.h"
00037 
00038 #ifndef H5_NO_NAMESPACE
00039     using namespace H5;
00040 #endif
00041 
00042 const H5std_string FILE_NAME( "SDScompound.h5" );
00043 const H5std_string DATASET_NAME( "ArrayOfStructures" );
00044 const H5std_string MEMBER1( "a_name" );
00045 const H5std_string MEMBER2( "b_name" );
00046 const H5std_string MEMBER3( "c_name" );
00047 const int   LENGTH = 10;
00048 const int   RANK = 1;
00049 
00050 int main(void)
00051 {
00052    /* First structure  and dataset*/
00053    typedef struct s1_t {
00054         int    a;
00055         float  b;
00056         double c;
00057    } s1_t;
00058 
00059    /* Second structure (subset of s1_t)  and dataset*/
00060    typedef struct s2_t {
00061         double c;
00062         int    a;
00063    } s2_t;
00064 
00065    // Try block to detect exceptions raised by any of the calls inside it
00066    try
00067    {
00068       /*
00069        * Initialize the data
00070        */
00071       int  i;
00072       s1_t s1[LENGTH];
00073       for (i = 0; i< LENGTH; i++)
00074       {
00075          s1[i].a = i;
00076          s1[i].b = i*i;
00077          s1[i].c = 1./(i+1);
00078       }
00079 
00080       /*
00081        * Turn off the auto-printing when failure occurs so that we can
00082        * handle the errors appropriately
00083        */
00084       Exception::dontPrint();
00085 
00086       /*
00087        * Create the data space.
00088        */
00089       hsize_t dim[] = {LENGTH};   /* Dataspace dimensions */
00090       DataSpace space( RANK, dim );
00091 
00092       /*
00093        * Create the file.
00094        */
00095       H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
00096 
00097       /*
00098        * Create the memory datatype.
00099        */
00100       CompType mtype1( sizeof(s1_t) );
00101       mtype1.insertMember( MEMBER1, HOFFSET(s1_t, a), PredType::NATIVE_INT);
00102       mtype1.insertMember( MEMBER3, HOFFSET(s1_t, c), PredType::NATIVE_DOUBLE);
00103       mtype1.insertMember( MEMBER2, HOFFSET(s1_t, b), PredType::NATIVE_FLOAT);
00104 
00105       /*
00106        * Create the dataset.
00107        */
00108       DataSet* dataset;
00109       dataset = new DataSet(file->createDataSet(DATASET_NAME, mtype1, space));
00110 
00111       /*
00112        * Write data to the dataset;
00113        */
00114       dataset->write( s1, mtype1 );
00115 
00116       /*
00117        * Release resources
00118        */
00119       delete dataset;
00120       delete file;
00121 
00122       /*
00123        * Open the file and the dataset.
00124        */
00125       file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
00126       dataset = new DataSet (file->openDataSet( DATASET_NAME ));
00127 
00128       /*
00129        * Create a datatype for s2
00130        */
00131       CompType mtype2( sizeof(s2_t) );
00132 
00133       mtype2.insertMember( MEMBER3, HOFFSET(s2_t, c), PredType::NATIVE_DOUBLE);
00134       mtype2.insertMember( MEMBER1, HOFFSET(s2_t, a), PredType::NATIVE_INT);
00135 
00136       /*
00137        * Read two fields c and a from s1 dataset. Fields in the file
00138        * are found by their names "c_name" and "a_name".
00139        */
00140       s2_t s2[LENGTH];
00141       dataset->read( s2, mtype2 );
00142 
00143       /*
00144        * Display the fields
00145        */
00146       cout << endl << "Field c : " << endl;
00147       for( i = 0; i < LENGTH; i++)
00148          cout << s2[i].c << " ";
00149       cout << endl;
00150 
00151       cout << endl << "Field a : " << endl;
00152       for( i = 0; i < LENGTH; i++)
00153          cout << s2[i].a << " ";
00154       cout << endl;
00155 
00156       /*
00157        * Create a datatype for s3.
00158        */
00159       CompType mtype3( sizeof(float) );
00160 
00161       mtype3.insertMember( MEMBER2, 0, PredType::NATIVE_FLOAT);
00162 
00163       /*
00164        * Read field b from s1 dataset. Field in the file is found by its name.
00165        */
00166       float s3[LENGTH];  // Third "structure" - used to read float field of s1
00167       dataset->read( s3, mtype3 );
00168 
00169       /*
00170        * Display the field
00171        */
00172       cout << endl << "Field b : " << endl;
00173       for( i = 0; i < LENGTH; i++)
00174          cout << s3[i] << " ";
00175       cout << endl;
00176 
00177       /*
00178        * Release resources
00179        */
00180       delete dataset;
00181       delete file;
00182    }  // end of try block
00183 
00184    // catch failure caused by the H5File operations
00185    catch( FileIException error )
00186    {
00187       error.printError();
00188       return -1;
00189    }
00190 
00191    // catch failure caused by the DataSet operations
00192    catch( DataSetIException error )
00193    {
00194       error.printError();
00195       return -1;
00196    }
00197 
00198    // catch failure caused by the DataSpace operations
00199    catch( DataSpaceIException error )
00200    {
00201       error.printError();
00202       return -1;
00203    }
00204 
00205    // catch failure caused by the DataSpace operations
00206    catch( DataTypeIException error )
00207    {
00208       error.printError();
00209       return -1;
00210    }
00211 
00212    return 0;
00213 }

Generated on Tue May 10 09:27:56 2011 by  doxygen 1.4.7