HDF5 C++ API Reference Manual

 

 

readdata.cpp

This example shows how to read datasets.
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 reads hyperslab from the SDS.h5 file into
00018 //      two-dimensional plane of a three-dimensional array.  Various
00019 //      information about the dataset in the SDS.h5 file is obtained.
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( "SDS.h5" );
00043 const H5std_string DATASET_NAME( "IntArray" );
00044 const int    NX_SUB = 3;        // hyperslab dimensions
00045 const int    NY_SUB = 4;
00046 const int    NX = 7;            // output buffer dimensions
00047 const int    NY = 7;
00048 const int    NZ = 3;
00049 const int    RANK_OUT = 3;
00050 
00051 int main (void)
00052 {
00053    /*
00054     * Output buffer initialization.
00055     */
00056    int i, j, k;
00057    int         data_out[NX][NY][NZ ]; /* output buffer */
00058    for (j = 0; j < NX; j++)
00059    {
00060       for (i = 0; i < NY; i++)
00061       {
00062          for (k = 0; k < NZ ; k++)
00063             data_out[j][i][k] = 0;
00064       }
00065    }
00066 
00067    /*
00068     * Try block to detect exceptions raised by any of the calls inside it
00069     */
00070    try
00071    {
00072       /*
00073        * Turn off the auto-printing when failure occurs so that we can
00074        * handle the errors appropriately
00075        */
00076       Exception::dontPrint();
00077 
00078       /*
00079        * Open the specified file and the specified dataset in the file.
00080        */
00081       H5File file( FILE_NAME, H5F_ACC_RDONLY );
00082       DataSet dataset = file.openDataSet( DATASET_NAME );
00083 
00084       /*
00085        * Get the class of the datatype that is used by the dataset.
00086        */
00087       H5T_class_t type_class = dataset.getTypeClass();
00088 
00089       /*
00090        * Get class of datatype and print message if it's an integer.
00091        */
00092       if( type_class == H5T_INTEGER )
00093       {
00094          cout << "Data set has INTEGER type" << endl;
00095 
00096          /*
00097           * Get the integer datatype
00098           */
00099          IntType intype = dataset.getIntType();
00100 
00101          /*
00102           * Get order of datatype and print message if it's a little endian.
00103           */
00104          H5std_string order_string;
00105          H5T_order_t order = intype.getOrder( order_string );
00106          cout << order_string << endl;
00107 
00108          /*
00109           * Get size of the data element stored in file and print it.
00110           */
00111          size_t size = intype.getSize();
00112          cout << "Data size is " << size << endl;
00113       }
00114 
00115       /*
00116        * Get dataspace of the dataset.
00117        */
00118       DataSpace dataspace = dataset.getSpace();
00119 
00120       /*
00121        * Get the number of dimensions in the dataspace.
00122        */
00123       int rank = dataspace.getSimpleExtentNdims();
00124 
00125       /*
00126        * Get the dimension size of each dimension in the dataspace and
00127        * display them.
00128        */
00129       hsize_t dims_out[2];
00130       int ndims = dataspace.getSimpleExtentDims( dims_out, NULL);
00131       cout << "rank " << rank << ", dimensions " <<
00132               (unsigned long)(dims_out[0]) << " x " <<
00133               (unsigned long)(dims_out[1]) << endl;
00134 
00135       /*
00136        * Define hyperslab in the dataset; implicitly giving strike and
00137        * block NULL.
00138        */
00139       hsize_t      offset[2];   // hyperslab offset in the file
00140       hsize_t      count[2];    // size of the hyperslab in the file
00141       offset[0] = 1;
00142       offset[1] = 2;
00143       count[0]  = NX_SUB;
00144       count[1]  = NY_SUB;
00145       dataspace.selectHyperslab( H5S_SELECT_SET, count, offset );
00146 
00147       /*
00148        * Define the memory dataspace.
00149        */
00150       hsize_t     dimsm[3];              /* memory space dimensions */
00151       dimsm[0] = NX;
00152       dimsm[1] = NY;
00153       dimsm[2] = NZ ;
00154       DataSpace memspace( RANK_OUT, dimsm );
00155 
00156       /*
00157        * Define memory hyperslab.
00158        */
00159       hsize_t      offset_out[3];       // hyperslab offset in memory
00160       hsize_t      count_out[3];        // size of the hyperslab in memory
00161       offset_out[0] = 3;
00162       offset_out[1] = 0;
00163       offset_out[2] = 0;
00164       count_out[0]  = NX_SUB;
00165       count_out[1]  = NY_SUB;
00166       count_out[2]  = 1;
00167       memspace.selectHyperslab( H5S_SELECT_SET, count_out, offset_out );
00168 
00169       /*
00170        * Read data from hyperslab in the file into the hyperslab in
00171        * memory and display the data.
00172        */
00173       dataset.read( data_out, PredType::NATIVE_INT, memspace, dataspace );
00174 
00175       for (j = 0; j < NX; j++)
00176       {
00177         for (i = 0; i < NY; i++)
00178            cout << data_out[j][i][0] << " ";
00179         cout << endl;
00180       }
00181       /*
00182        * 0 0 0 0 0 0 0
00183        * 0 0 0 0 0 0 0
00184        * 0 0 0 0 0 0 0
00185        * 3 4 5 6 0 0 0
00186        * 4 5 6 7 0 0 0
00187        * 5 6 7 8 0 0 0
00188        * 0 0 0 0 0 0 0
00189        */
00190    }  // end of try block
00191 
00192    // catch failure caused by the H5File operations
00193    catch( FileIException error )
00194    {
00195       error.printError();
00196       return -1;
00197    }
00198 
00199    // catch failure caused by the DataSet operations
00200    catch( DataSetIException error )
00201    {
00202       error.printError();
00203       return -1;
00204    }
00205 
00206    // catch failure caused by the DataSpace operations
00207    catch( DataSpaceIException error )
00208    {
00209       error.printError();
00210       return -1;
00211    }
00212 
00213    // catch failure caused by the DataSpace operations
00214    catch( DataTypeIException error )
00215    {
00216       error.printError();
00217       return -1;
00218    }
00219 
00220    return 0;  // successfully terminated
00221 }
00222 

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