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 help@hdfgroup.org.     *
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 #ifdef SKIP_UNTIL_APRIL_2009
00053    /* First structure  and dataset*/
00054    typedef struct s1_t {
00055         int      a;
00056         float  b;
00057         double c;
00058    } s1_t;
00059 
00060    /* Second structure (subset of s1_t)  and dataset*/
00061    typedef struct s2_t {
00062         double c;
00063         int      a;
00064    } s2_t;
00065 
00066    // Try block to detect exceptions raised by any of the calls inside it
00067    try
00068    {
00069       /*
00070        * Initialize the data
00071        */
00072       int        i;
00073       s1_t       s1[LENGTH];
00074       for (i = 0; i< LENGTH; i++)
00075       {
00076          s1[i].a = i;
00077          s1[i].b = i*i;
00078          s1[i].c = 1./(i+1);
00079       }
00080 
00081       /*
00082        * Turn off the auto-printing when failure occurs so that we can
00083        * handle the errors appropriately
00084        */
00085       Exception::dontPrint();
00086 
00087       /*
00088        * Create the data space.
00089        */
00090       hsize_t    dim[] = {LENGTH};   /* Dataspace dimensions */
00091       DataSpace space( RANK, dim );
00092 
00093       /*
00094        * Create the file.
00095        */
00096       H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
00097 
00098       /*
00099        * Create the memory datatype.
00100        */
00101       CompType mtype1( sizeof(s1_t) );
00102       mtype1.insertMember( MEMBER1, HOFFSET(s1_t, a), PredType::NATIVE_INT);
00103       mtype1.insertMember( MEMBER3, HOFFSET(s1_t, c), PredType::NATIVE_DOUBLE);
00104       mtype1.insertMember( MEMBER2, HOFFSET(s1_t, b), PredType::NATIVE_FLOAT);
00105 
00106       /*
00107        * Create the dataset.
00108        */
00109       DataSet* dataset;
00110       dataset = new DataSet( file->createDataSet( DATASET_NAME, mtype1, space ));
00111 
00112       /*
00113        * Wtite data to the dataset;
00114        */
00115       dataset->write( s1, mtype1 );
00116 
00117       /*
00118        * Release resources
00119        */
00120       delete dataset;
00121       delete file;
00122 
00123       // Get the class of the first member in mtype1, then get its type
00124       H5T_class_t member1_class = mtype1.getMemberClass( 2 );
00125       if( member1_class == H5T_FLOAT )
00126       {
00127          FloatType member2 = mtype1.getMemberFloatType( 2 );
00128          H5std_string norm_string;
00129          H5T_norm_t norm = member2.getNorm( norm_string );
00130          cout << "Normalization type is " << norm_string << endl;
00131       }
00132 
00133       /*
00134        * Open the file and the dataset.
00135        */
00136       file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
00137       dataset = new DataSet (file->openDataSet( DATASET_NAME ));
00138 
00139       /*
00140        * Create a datatype for s2
00141        */
00142       CompType mtype2( sizeof(s2_t) );
00143 
00144       mtype2.insertMember( MEMBER3, HOFFSET(s2_t, c), PredType::NATIVE_DOUBLE);
00145       mtype2.insertMember( MEMBER1, HOFFSET(s2_t, a), PredType::NATIVE_INT);
00146 
00147       /*
00148        * Read two fields c and a from s1 dataset. Fields in the file
00149        * are found by their names "c_name" and "a_name".
00150        */
00151       s2_t       s2[LENGTH];
00152       dataset->read( s2, mtype2 );
00153 
00154       /*
00155        * Display the fields
00156        */
00157       cout << endl << "Field c : " << endl;
00158       for( i = 0; i < LENGTH; i++)
00159          cout << s2[i].c << " ";
00160       cout << endl;
00161 
00162       cout << endl << "Field a : " << endl;
00163       for( i = 0; i < LENGTH; i++)
00164          cout << s2[i].a << " ";
00165       cout << endl;
00166 
00167       /*
00168        * Create a datatype for s3.
00169        */
00170       CompType mtype3( sizeof(float) );
00171 
00172       mtype3.insertMember( MEMBER2, 0, PredType::NATIVE_FLOAT);
00173 
00174       /*
00175        * Read field b from s1 dataset. Field in the file is found by its name.
00176        */
00177       float s3[LENGTH];  // Third "structure" - used to read float field of s1)
00178       dataset->read( s3, mtype3 );
00179 
00180       /*
00181        * Display the field
00182        */
00183       cout << endl << "Field b : " << endl;
00184       for( i = 0; i < LENGTH; i++)
00185          cout << s3[i] << " ";
00186       cout << endl;
00187 
00188       /*
00189        * Release resources
00190        */
00191       delete dataset;
00192       delete file;
00193    }  // end of try block
00194 
00195    // catch failure caused by the H5File operations
00196    catch( FileIException error )
00197    {
00198       error.printError();
00199       return -1;
00200    }
00201 
00202    // catch failure caused by the DataSet operations
00203    catch( DataSetIException error )
00204    {
00205       error.printError();
00206       return -1;
00207    }
00208 
00209    // catch failure caused by the DataSpace operations
00210    catch( DataSpaceIException error )
00211    {
00212       error.printError();
00213       return -1;
00214    }
00215 
00216    // catch failure caused by the DataSpace operations
00217    catch( DataTypeIException error )
00218    {
00219       error.printError();
00220       return -1;
00221    }
00222 #endif
00223    return 0;
00224 }

Generated on Wed Nov 4 14:13:07 2009 by  doxygen 1.4.7