HDF5 C++ API Reference Manual

- Main Page
- Namespaces
- Classes
- Files
- Examples

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