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

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