HDF5 C++ API Reference Manual

 

 

create.cpp

This example shows how to create 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 writes a dataset to a new HDF5 file.
00018  */
00019 
00020 #ifdef OLD_HEADER_FILENAME
00021 #include <iostream.h>
00022 #else
00023 #include <iostream>
00024 #endif
00025 #include <string>
00026 
00027 #include "H5Cpp.h"
00028 
00029 #ifndef H5_NO_NAMESPACE
00030     using namespace H5;
00031 #endif
00032 
00033 const H5std_string      FILE_NAME( "SDS.h5" );
00034 const H5std_string      DATASET_NAME( "IntArray" );
00035 const int       NX = 5;                    // dataset dimensions
00036 const int       NY = 6;
00037 const int       RANK = 2;
00038 
00039 int main (void)
00040 {
00041    /*
00042     * Data initialization.
00043     */
00044    int i, j;
00045    int data[NX][NY];          // buffer for data to write
00046    for (j = 0; j < NX; j++)
00047    {
00048       for (i = 0; i < NY; i++)
00049          data[j][i] = i + j;
00050    }
00051    /*
00052     * 0 1 2 3 4 5
00053     * 1 2 3 4 5 6
00054     * 2 3 4 5 6 7
00055     * 3 4 5 6 7 8
00056     * 4 5 6 7 8 9
00057     */
00058 
00059    // Try block to detect exceptions raised by any of the calls inside it
00060    try
00061    {
00062       /*
00063        * Turn off the auto-printing when failure occurs so that we can
00064        * handle the errors appropriately
00065        */
00066       Exception::dontPrint();
00067 
00068       /*
00069        * Create a new file using H5F_ACC_TRUNC access,
00070        * default file creation properties, and default file
00071        * access properties.
00072        */
00073       H5File file( FILE_NAME, H5F_ACC_TRUNC );
00074 
00075       /*
00076        * Define the size of the array and create the data space for fixed
00077        * size dataset.
00078        */
00079       hsize_t     dimsf[2];              // dataset dimensions
00080       dimsf[0] = NX;
00081       dimsf[1] = NY;
00082       DataSpace dataspace( RANK, dimsf );
00083 
00084       /*
00085        * Define datatype for the data in the file.
00086        * We will store little endian INT numbers.
00087        */
00088       IntType datatype( PredType::NATIVE_INT );
00089       datatype.setOrder( H5T_ORDER_LE );
00090 
00091       /*
00092        * Create a new dataset within the file using defined dataspace and
00093        * datatype and default dataset creation properties.
00094        */
00095       DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
00096 
00097       /*
00098        * Write the data to the dataset using default memory space, file
00099        * space, and transfer properties.
00100        */
00101       dataset.write( data, PredType::NATIVE_INT );
00102    }  // end of try block
00103 
00104    // catch failure caused by the H5File operations
00105    catch( FileIException error )
00106    {
00107       error.printError();
00108       return -1;
00109    }
00110 
00111    // catch failure caused by the DataSet operations
00112    catch( DataSetIException error )
00113    {
00114       error.printError();
00115       return -1;
00116    }
00117 
00118    // catch failure caused by the DataSpace operations
00119    catch( DataSpaceIException error )
00120    {
00121       error.printError();
00122       return -1;
00123    }
00124 
00125    // catch failure caused by the DataSpace operations
00126    catch( DataTypeIException error )
00127    {
00128       error.printError();
00129       return -1;
00130    }
00131 
00132    return 0;  // successfully terminated
00133 }
00134 

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