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

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