HDF5 C++ API Reference Manual

 

 

extend_ds.cpp

This example shows how to work with extendible 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 shows how to work with extendible dataset.
00018  *   In the current version of the library dataset MUST be
00019  *   chunked.
00020  *
00021  */
00022 
00023 #ifdef OLD_HEADER_FILENAME
00024 #include <iostream.h>
00025 #else
00026 #include <iostream>
00027 #endif
00028 #include <string>
00029 
00030 #ifndef H5_NO_NAMESPACE
00031 #ifndef H5_NO_STD
00032     using std::cout;
00033     using std::endl;
00034 #endif  // H5_NO_STD
00035 #endif
00036 
00037 #include "H5Cpp.h"
00038 
00039 #ifndef H5_NO_NAMESPACE
00040     using namespace H5;
00041 #endif
00042 
00043 const H5std_string FILE_NAME( "SDSextendible.h5" );
00044 const H5std_string DATASET_NAME( "ExtendibleArray" );
00045 const int      NX = 10;
00046 const int      NY = 5;
00047 const int      RANK = 2;
00048 
00049 int main (void)
00050 {
00051    /*
00052     * Try block to detect exceptions raised by any of the calls inside it
00053     */
00054    try
00055    {
00056       /*
00057        * Turn off the auto-printing when failure occurs so that we can
00058        * handle the errors appropriately
00059        */
00060       Exception::dontPrint();
00061 
00062       /*
00063        * Create the data space with unlimited dimensions.
00064        */
00065       hsize_t      dims[2]  = { 3, 3};  // dataset dimensions at creation
00066       hsize_t      maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
00067       DataSpace mspace1( RANK, dims, maxdims);
00068 
00069       /*
00070        * Create a new file. If file exists its contents will be overwritten.
00071        */
00072       H5File file( FILE_NAME, H5F_ACC_TRUNC );
00073 
00074       /*
00075        * Modify dataset creation properties, i.e. enable chunking.
00076        */
00077       DSetCreatPropList cparms;
00078 
00079       hsize_t      chunk_dims[2] ={2, 5};
00080       cparms.setChunk( RANK, chunk_dims );
00081 
00082       /*
00083        * Set fill value for the dataset
00084        */
00085       int fill_val = 0;
00086       cparms.setFillValue( PredType::NATIVE_INT, &fill_val);
00087 
00088       /*
00089        * Create a new dataset within the file using cparms
00090        * creation properties.
00091        */
00092       DataSet dataset = file.createDataSet( DATASET_NAME, PredType::NATIVE_INT, mspace1, cparms);
00093 
00094       /*
00095        * Extend the dataset. This call assures that dataset is at least 3 x 3.
00096        */
00097       hsize_t      size[2];
00098       size[0]   = 3;
00099       size[1]   = 3;
00100       dataset.extend( size );
00101 
00102       /*
00103        * Select a hyperslab.
00104        */
00105       DataSpace fspace1 = dataset.getSpace ();
00106       hsize_t     offset[2];
00107       offset[0] = 0;
00108       offset[1] = 0;
00109       hsize_t      dims1[2] = { 3, 3};            /* data1 dimensions */
00110       fspace1.selectHyperslab( H5S_SELECT_SET, dims1, offset );
00111 
00112       /*
00113        * Write the data to the hyperslab.
00114        */
00115       int       data1[3][3] = { {1, 1, 1},       /* data to write */
00116                                 {1, 1, 1},
00117                                 {1, 1, 1} };
00118       dataset.write( data1, PredType::NATIVE_INT, mspace1, fspace1 );
00119 
00120       /*
00121        * Extend the dataset. Dataset becomes 10 x 3.
00122        */
00123       hsize_t   dims2[2] = { 7, 1};            /* data2 dimensions */
00124       dims[0]   = dims1[0] + dims2[0];
00125       size[0]   = dims[0];
00126       size[1]   = dims[1];
00127       dataset.extend( size );
00128 
00129       /*
00130        * Select a hyperslab.
00131        */
00132       DataSpace fspace2 = dataset.getSpace ();
00133       offset[0] = 3;
00134       offset[1] = 0;
00135       fspace2.selectHyperslab( H5S_SELECT_SET, dims2, offset );
00136 
00137       /*
00138        * Define memory space
00139        */
00140       DataSpace mspace2( RANK, dims2 );
00141 
00142       /*
00143        * Write the data to the hyperslab.
00144        */
00145       int  data2[7]    = { 2, 2, 2, 2, 2, 2, 2};
00146       dataset.write( data2, PredType::NATIVE_INT, mspace2, fspace2 );
00147 
00148       /*
00149        * Extend the dataset. Dataset becomes 10 x 5.
00150        */
00151       hsize_t   dims3[2] = { 2, 2};            /* data3 dimensions */
00152       dims[1]   = dims1[1] + dims3[1];
00153       size[0]   = dims[0];
00154       size[1]   = dims[1];
00155       dataset.extend( size );
00156 
00157       /*
00158        * Select a hyperslab
00159        */
00160       DataSpace fspace3 = dataset.getSpace ();
00161       offset[0] = 0;
00162       offset[1] = 3;
00163       fspace3.selectHyperslab( H5S_SELECT_SET, dims3, offset );
00164 
00165       /*
00166        * Define memory space.
00167        */
00168       DataSpace mspace3( RANK, dims3 );
00169 
00170       /*
00171        * Write the data to the hyperslab.
00172        */
00173       int         data3[2][2] = { {3, 3}, {3, 3} };
00174       dataset.write( data3, PredType::NATIVE_INT, mspace3, fspace3 );
00175 
00176       /*
00177        * Read the data from this dataset and display it.
00178        */
00179       int i, j;
00180       int data_out[NX][NY];
00181       for (i = 0; i < NX; i++)
00182       {
00183          for (j = 0; j < NY; j++)
00184             data_out[i][j] = 0;
00185       }
00186       dataset.read( data_out, PredType::NATIVE_INT );
00187       /*
00188        * Resulting dataset
00189        *
00190        *         1 1 1 3 3
00191        *         1 1 1 3 3
00192        *         1 1 1 0 0
00193        *         2 0 0 0 0
00194        *         2 0 0 0 0
00195        *         2 0 0 0 0
00196        *         2 0 0 0 0
00197        *         2 0 0 0 0
00198        *         2 0 0 0 0
00199        *         2 0 0 0 0
00200        */
00201       /*
00202        * Display the result.
00203        */
00204       for (i=0; i < NX; i++)
00205       {
00206           for(j=0; j < NY; j++)
00207              cout << data_out[i][j] << "  ";
00208           cout << endl;
00209       }
00210    }  // end of try block
00211 
00212    // catch failure caused by the H5File operations
00213    catch( FileIException error )
00214    {
00215       error.printError();
00216       return -1;
00217    }
00218 
00219    // catch failure caused by the DataSet operations
00220    catch( DataSetIException error )
00221    {
00222       error.printError();
00223       return -1;
00224    }
00225 
00226    // catch failure caused by the DataSpace operations
00227    catch( DataSpaceIException error )
00228    {
00229       error.printError();
00230       return -1;
00231    }
00232 
00233    // catch failure caused by the DataSpace operations
00234    catch( DataTypeIException 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