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  * https://support.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 read data from a chunked dataset.
00018  *   We will read from the file created by extend.C
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 " << (unsigned long)(dims[1])
00084         << endl;
00085 
00086       /*
00087        * Get creation properties list.
00088        */
00089       DSetCreatPropList cparms = dataset.getCreatePlist();
00090 
00091       /*
00092        * Check if dataset is chunked.
00093        */
00094       hsize_t     chunk_dims[2];
00095       int         rank_chunk;
00096       if( H5D_CHUNKED == cparms.getLayout() )
00097       {
00098          /*
00099           * Get chunking information: rank and dimensions
00100           */
00101          rank_chunk = cparms.getChunk( 2, chunk_dims);
00102          cout << "chunk rank " << rank_chunk << "dimensions "
00103            << (unsigned long)(chunk_dims[0]) << " x "
00104            << (unsigned long)(chunk_dims[1]) << endl;
00105       }
00106 
00107       /*
00108        * Define the memory space to read dataset.
00109        */
00110       DataSpace mspace1( RANK, dims );
00111 
00112       /*
00113        * Read dataset back and display.
00114        */
00115       int data_out[NX][NY];  // buffer for dataset to be read
00116       dataset.read( data_out, PredType::NATIVE_INT, mspace1, filespace );
00117       cout << "\n";
00118       cout << "Dataset: \n";
00119       for (j = 0; j < dims[0]; j++)
00120       {
00121          for (i = 0; i < dims[1]; i++)
00122             cout << data_out[j][i] << " ";
00123          cout << endl;
00124       }
00125 
00126       /*
00127        *            dataset rank 2, dimensions 10 x 5
00128        *            chunk rank 2, dimensions 2 x 5
00129 
00130        *            Dataset:
00131        *            1 1 1 3 3
00132        *            1 1 1 3 3
00133        *            1 1 1 0 0
00134        *            2 0 0 0 0
00135        *            2 0 0 0 0
00136        *            2 0 0 0 0
00137        *            2 0 0 0 0
00138        *            2 0 0 0 0
00139        *            2 0 0 0 0
00140        *            2 0 0 0 0
00141        */
00142 
00143       /*
00144        * Read the third column from the dataset.
00145        * First define memory dataspace, then define hyperslab
00146        * and read it into column array.
00147        */
00148       hsize_t     col_dims[1];
00149       col_dims[0] = 10;
00150       DataSpace mspace2( RANKC, col_dims );
00151 
00152       /*
00153        * Define the column (hyperslab) to read.
00154        */
00155       hsize_t offset[2] = { 0, 2 };
00156       hsize_t  count[2] = { 10, 1 };
00157       int      column[10];        // buffer for column to be read
00158 
00159       filespace.selectHyperslab( H5S_SELECT_SET, count, offset );
00160       dataset.read( column, PredType::NATIVE_INT, mspace2, filespace );
00161 
00162       cout << endl;
00163       cout << "Third column: " << endl;
00164       for (i = 0; i < 10; i++)
00165          cout << column[i] << endl;
00166 
00167       /*
00168        *            Third column:
00169        *            1
00170        *            1
00171        *            1
00172        *            0
00173        *            0
00174        *            0
00175        *            0
00176        *            0
00177        *            0
00178        *            0
00179        */
00180 
00181       /*
00182        * Define the memory space to read a chunk.
00183        */
00184       DataSpace mspace3( rank_chunk, chunk_dims );
00185 
00186       /*
00187        * Define chunk in the file (hyperslab) to read.
00188        */
00189       offset[0] = 2;
00190       offset[1] = 0;
00191       count[0]  = chunk_dims[0];
00192       count[1]  = chunk_dims[1];
00193       filespace.selectHyperslab( H5S_SELECT_SET, count, offset );
00194 
00195       /*
00196        * Read chunk back and display.
00197        */
00198       int chunk_out[2][5];   // buffer for chunk to be read
00199       dataset.read( chunk_out, PredType::NATIVE_INT, mspace3, filespace );
00200       cout << endl;
00201       cout << "Chunk:" << endl;
00202       for (j = 0; j < chunk_dims[0]; j++)
00203       {
00204          for (i = 0; i < chunk_dims[1]; i++)
00205             cout << chunk_out[j][i] << " ";
00206          cout << endl;
00207       }
00208       /*
00209        *         Chunk:
00210        *         1 1 1 0 0
00211        *         2 0 0 0 0
00212        */
00213    }  // end of try block
00214 
00215    // catch failure caused by the H5File operations
00216    catch( FileIException error )
00217    {
00218       error.printError();
00219       return -1;
00220    }
00221 
00222    // catch failure caused by the DataSet operations
00223    catch( DataSetIException error )
00224    {
00225       error.printError();
00226       return -1;
00227    }
00228 
00229    // catch failure caused by the DataSpace operations
00230    catch( DataSpaceIException error )
00231    {
00232       error.printError();
00233       return -1;
00234    }
00235 
00236    return 0;
00237 }

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