HDF5 C++ API Reference Manual

 

 

chunks.cpp

This example shows how to read data from a chunked dataset.
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 read data from a chunked dataset.
00018  *   We will read from the file created by extend.cpp
00019  */
00020 
00021 #ifdef OLD_HEADER_FILENAME
00022 #include <iostream.h>
00023 #else
00024 #include <iostream>
00025 #endif
00026 #include <string>
00027 
00028 #ifndef H5_NO_NAMESPACE
00029 #ifndef H5_NO_STD
00030     using std::cout;
00031     using std::endl;
00032 #endif  // H5_NO_STD
00033 #endif
00034 
00035 #include "H5Cpp.h"
00036 
00037 #ifndef H5_NO_NAMESPACE
00038     using namespace H5;
00039 #endif
00040 
00041 const H5std_string FILE_NAME( "SDSextendible.h5" );
00042 const H5std_string DATASET_NAME( "ExtendibleArray" );
00043 const int      NX = 10;
00044 const int      NY = 5;
00045 const int      RANK = 2;
00046 const int      RANKC = 1;
00047 
00048 int main (void)
00049 {
00050     hsize_t     i, j;
00051 
00052     // Try block to detect exceptions raised by any of the calls inside it
00053     try
00054     {
00055         /*
00056          * Turn off the auto-printing when failure occurs so that we can
00057          * handle the errors appropriately
00058          */
00059         Exception::dontPrint();
00060 
00061         /*
00062          * Open the file and the dataset.
00063          */
00064         H5File file( FILE_NAME, H5F_ACC_RDONLY );
00065         DataSet dataset = file.openDataSet( DATASET_NAME );
00066 
00067         /*
00068          * Get filespace for rank and dimension
00069          */
00070         DataSpace filespace = dataset.getSpace();
00071 
00072         /*
00073          * Get number of dimensions in the file dataspace
00074          */
00075         int rank = filespace.getSimpleExtentNdims();
00076 
00077         /*
00078          * Get and print the dimension sizes of the file dataspace
00079          */
00080         hsize_t dims[2];        // dataset dimensions
00081         rank = filespace.getSimpleExtentDims( dims );
00082         cout << "dataset rank = " << rank << ", dimensions "
00083              << (unsigned long)(dims[0]) << " x "
00084              << (unsigned long)(dims[1]) << endl;
00085 
00086         /*
00087          * Define the memory space to read dataset.
00088          */
00089         DataSpace mspace1(RANK, dims);
00090 
00091         /*
00092          * Read dataset back and display.
00093          */
00094         int data_out[NX][NY];  // buffer for dataset to be read
00095         dataset.read( data_out, PredType::NATIVE_INT, mspace1, filespace );
00096 
00097         cout << "\n";
00098         cout << "Dataset: \n";
00099         for (j = 0; j < dims[0]; j++)
00100         {
00101             for (i = 0; i < dims[1]; i++)
00102                 cout << data_out[j][i] << " ";
00103             cout << endl;
00104         }
00105 
00106         /*
00107          *          dataset rank 2, dimensions 10 x 5
00108          *          chunk rank 2, dimensions 2 x 5
00109 
00110          *          Dataset:
00111          *          1 1 1 3 3
00112          *          1 1 1 3 3
00113          *          1 1 1 0 0
00114          *          2 0 0 0 0
00115          *          2 0 0 0 0
00116          *          2 0 0 0 0
00117          *          2 0 0 0 0
00118          *          2 0 0 0 0
00119          *          2 0 0 0 0
00120          *          2 0 0 0 0
00121          */
00122 
00123         /*
00124          * Read the third column from the dataset.
00125          * First define memory dataspace, then define hyperslab
00126          * and read it into column array.
00127          */
00128         hsize_t col_dims[1];
00129         col_dims[0] = 10;
00130         DataSpace mspace2( RANKC, col_dims );
00131 
00132         /*
00133          * Define the column (hyperslab) to read.
00134          */
00135         hsize_t offset[2] = { 0, 2 };
00136         hsize_t  count[2] = { 10, 1 };
00137         int column[10];  // buffer for column to be read
00138 
00139         /*
00140          * Define hyperslab and read.
00141          */
00142         filespace.selectHyperslab( H5S_SELECT_SET, count, offset );
00143         dataset.read( column, PredType::NATIVE_INT, mspace2, filespace );
00144 
00145         cout << endl;
00146         cout << "Third column: " << endl;
00147         for (i = 0; i < 10; i++)
00148             cout << column[i] << endl;
00149 
00150         /*
00151          *          Third column:
00152          *          1
00153          *          1
00154          *          1
00155          *          0
00156          *          0
00157          *          0
00158          *          0
00159          *          0
00160          *          0
00161          *          0
00162          */
00163 
00164         /*
00165          * Get creation properties list.
00166          */
00167         DSetCreatPropList cparms = dataset.getCreatePlist();
00168 
00169         /*
00170          * Check if dataset is chunked.
00171          */
00172         hsize_t chunk_dims[2];
00173         int     rank_chunk;
00174         if( H5D_CHUNKED == cparms.getLayout() )
00175         {
00176             /*
00177              * Get chunking information: rank and dimensions
00178              */
00179             rank_chunk = cparms.getChunk( 2, chunk_dims);
00180             cout << "chunk rank " << rank_chunk << "dimensions "
00181                 << (unsigned long)(chunk_dims[0]) << " x "
00182                 << (unsigned long)(chunk_dims[1]) << endl;
00183 
00184             /*
00185              * Define the memory space to read a chunk.
00186              */
00187             DataSpace mspace3( rank_chunk, chunk_dims );
00188 
00189             /*
00190              * Define chunk in the file (hyperslab) to read.
00191              */
00192             offset[0] = 2;
00193             offset[1] = 0;
00194             count[0]  = chunk_dims[0];
00195             count[1]  = chunk_dims[1];
00196             filespace.selectHyperslab( H5S_SELECT_SET, count, offset );
00197 
00198             /*
00199              * Read chunk back and display.
00200              */
00201             int chunk_out[2][5];   // buffer for chunk to be read
00202             dataset.read( chunk_out, PredType::NATIVE_INT, mspace3, filespace );
00203             cout << endl;
00204             cout << "Chunk:" << endl;
00205             for (j = 0; j < chunk_dims[0]; j++)
00206             {
00207                 for (i = 0; i < chunk_dims[1]; i++)
00208                     cout << chunk_out[j][i] << " ";
00209                 cout << endl;
00210             }
00211             /*
00212              *   Chunk:
00213              *   1 1 1 0 0
00214              *   2 0 0 0 0
00215              */
00216         }
00217     }  // end of try block
00218 
00219     // catch failure caused by the H5File operations
00220     catch( FileIException error )
00221     {
00222         error.printError();
00223         return -1;
00224     }
00225 
00226     // catch failure caused by the DataSet operations
00227     catch( DataSetIException error )
00228     {
00229         error.printError();
00230         return -1;
00231     }
00232 
00233     // catch failure caused by the DataSpace operations
00234     catch( DataSpaceIException error )
00235     {
00236         error.printError();
00237         return -1;
00238     }
00239     return 0;
00240 }

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