HDF5 C++ API Reference Manual

 

 

 

h5group.cpp

This example shows how to work with groups.
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 creates a group in the file and dataset in the group.
00018  * Hard link to the group object is created and the dataset is accessed
00019  * under different names.
00020  * Iterator function is used to find the object names in the root group.
00021  * Note that the C++ API iterator function is not completed yet, thus
00022  * the C version is used in this example.
00023  */
00024 #ifdef OLD_HEADER_FILENAME
00025 #include <iostream.h>
00026 #else
00027 #include <iostream>
00028 #endif
00029 #include <string>
00030 
00031 #ifndef H5_NO_NAMESPACE
00032 #ifndef H5_NO_STD
00033     using std::cout;
00034     using std::endl;
00035 #endif  // H5_NO_STD
00036 #endif
00037 
00038 #include "H5Cpp.h"
00039 
00040 #ifndef H5_NO_NAMESPACE
00041 using namespace H5;
00042 #endif
00043 
00044 const H5std_string      FILE_NAME( "Group.h5" );
00045 const int       RANK = 2;
00046 
00047 // Operator function
00048 extern "C" herr_t file_info(hid_t loc_id, const char *name, void *opdata);
00049 
00050 int main(void)
00051 {
00052 #ifdef SKIP_UNTIL_APRIL_2009
00053     hsize_t  dims[2];
00054     hsize_t  cdims[2];
00055 
00056    // Try block to detect exceptions raised by any of the calls inside it
00057    try
00058    {
00059       /*
00060        * Turn off the auto-printing when failure occurs so that we can
00061        * handle the errors appropriately
00062        */
00063       Exception::dontPrint();
00064 
00065       /*
00066        * Create the named file, truncating the existing one if any,
00067        * using default create and access property lists.
00068        */
00069       H5File *file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
00070 
00071       /*
00072        * Create a group in the file
00073        */
00074       Group* group = new Group( file->createGroup( "/Data" ));
00075 
00076       /*
00077        * Create dataset "Compressed Data" in the group using absolute
00078        * name. Dataset creation property list is modified to use
00079        * GZIP compression with the compression effort set to 6.
00080        * Note that compression can be used only when dataset is chunked.
00081        */
00082       dims[0] = 1000;
00083       dims[1] = 20;
00084       cdims[0] = 20;
00085       cdims[1] = 20;
00086       DataSpace dataspace( RANK, dims ); // create the new dataspace
00087                                          // for the dataset
00088 
00089       DSetCreatPropList ds_creatplist;  // create dataset creation prop list
00090       ds_creatplist.setChunk( 2, cdims );  // then modify it for compression
00091       ds_creatplist.setDeflate( 6 );
00092 
00093       DataSet* dataset = new DataSet( file->createDataSet( "/Data/Compressed_Data", PredType::NATIVE_INT, dataspace, ds_creatplist ));
00094 
00095       /*
00096        * Close the dataset and the file.
00097        */
00098       delete dataset;
00099       delete group;
00100       delete file;
00101 
00102       /*
00103        * Now reopen the file and group in the file.
00104        */
00105       file = new H5File( FILE_NAME, H5F_ACC_RDWR );
00106       group = new Group( file->openGroup( "Data" ));
00107 
00108       /*
00109        * Access "Compressed_Data" dataset in the group.
00110        */
00111       try {  // to determine if the dataset exists in the group
00112          dataset = new DataSet( group->openDataSet( "Compressed_Data" ));
00113       }
00114       catch( GroupIException not_found_error )
00115       {
00116          cout << " Dataset is not found." << endl;
00117       }
00118       cout << "dataset \"/Data/Compressed_Data\" is open" << endl;
00119 
00120       /*
00121        * Close the dataset.
00122        */
00123       delete dataset;
00124 
00125       /*
00126        * Create hard link to the Data group.
00127        */
00128       file->link( H5G_LINK_HARD, "Data", "Data_new" );
00129 
00130       /*
00131        * We can access "Compressed_Data" dataset using created
00132        * hard link "Data_new".
00133        */
00134       try {  // to determine if the dataset exists in the file
00135          dataset = new DataSet( file->openDataSet( "/Data_new/Compressed_Data" ));
00136       }
00137       catch( FileIException not_found_error )
00138       {
00139          cout << " Dataset is not found." << endl;
00140       }
00141       cout << "dataset \"/Data_new/Compressed_Data\" is open" << endl;
00142 
00143       /*
00144        * Close the dataset.
00145        */
00146       delete dataset;
00147 
00148       /*
00149        * Use iterator to see the names of the objects in the file
00150        * root directory.
00151        */
00152       cout << endl << "Iterating over elements in the file" << endl;
00153       herr_t idx = H5Giterate(file->getId(), "/", NULL, file_info, NULL);
00154       cout << endl;
00155 
00156       /*
00157        * Unlink  name "Data" and use iterator to see the names
00158        * of the objects in the file root direvtory.
00159        */
00160       cout << "Unlinking..." << endl;
00161       try {  // attempt to unlink the dataset
00162          file->unlink( "Data" );
00163       }
00164       catch( FileIException unlink_error )
00165       {
00166          cout << " unlink failed." << endl;
00167       }
00168       cout << "\"Data\" is unlinked" << endl;
00169 
00170       cout << endl << "Iterating over elements in the file again" << endl;
00171       idx = H5Giterate(file->getId(), "/", NULL, file_info, NULL);
00172       cout << endl;
00173 
00174       /*
00175        * Close the file.
00176        */
00177       delete file;
00178 
00179    }  // end of try block
00180 
00181    // catch failure caused by the H5File operations
00182    catch( FileIException error )
00183    {
00184       error.printError();
00185       return -1;
00186    }
00187 
00188    // catch failure caused by the DataSet operations
00189    catch( DataSetIException error )
00190    {
00191       error.printError();
00192       return -1;
00193    }
00194 
00195    // catch failure caused by the DataSpace operations
00196    catch( DataSpaceIException error )
00197    {
00198       error.printError();
00199       return -1;
00200    }
00201 
00202    // catch failure caused by the Attribute operations
00203    catch( AttributeIException error )
00204    {
00205       error.printError();
00206       return -1;
00207    }
00208 #endif
00209    return 0;
00210 }
00211 
00212 /*
00213  * Operator function.
00214  */
00215 herr_t
00216 file_info(hid_t loc_id, const char *name, void *opdata)
00217 {
00218     hid_t group;
00219     /*
00220      * Open the group using its name.
00221      */
00222     group = H5Gopen(loc_id, name);
00223 
00224     /*
00225      * Display group name.
00226      */
00227     cout << "Name : " << name << endl;
00228 
00229     H5Gclose(group);
00230     return 0;
00231  }
00232 

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