HDF5 C++ API Reference Manual

 

 

writedata.cpp

This example shows how to write 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 program shows how the select_hyperslab and select_elements
00018  *  functions are used to write selected data from memory to the file.
00019  *  Program takes 48 elements from the linear buffer and writes them into
00020  *  the matrix using 3x2 blocks, (4,3) stride and (2,4) count.
00021  *  Then four elements  of the matrix are overwritten with the new values and
00022  *  file is closed. Program reopens the file and reads and displays the result.
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( "Select.h5" );
00046 const H5std_string DATASET_NAME( "Matrix in file" );
00047 const int   MSPACE1_RANK = 1;   // Rank of the first dataset in memory
00048 const int   MSPACE1_DIM = 50;   // Dataset size in memory
00049 const int   MSPACE2_RANK = 1;   // Rank of the second dataset in memory
00050 const int   MSPACE2_DIM = 4;    // Dataset size in memory
00051 const int   FSPACE_RANK = 2;    // Dataset rank as it is stored in the file
00052 const int   FSPACE_DIM1 = 8;    // Dimension sizes of the dataset as it is
00053 const int   FSPACE_DIM2 = 12;   //      stored in the file
00054 const int   MSPACE_RANK = 2;    // Rank of the first dataset in memory
00055 const int   MSPACE_DIM1 = 8;    // We will read dataset back from the file
00056 const int   MSPACE_DIM2 = 9;    //      to the dataset in memory with these
00057                                 //      dataspace parameters
00058 const int   NPOINTS = 4;        // Number of points that will be selected
00059                                 //      and overwritten
00060 
00061 int main (void)
00062 {
00063     int   i,j; // loop indices */
00064 
00065     /*
00066      * Try block to detect exceptions raised by any of the calls inside it
00067      */
00068     try
00069     {
00070         /*
00071          * Turn off the auto-printing when failure occurs so that we can
00072          * handle the errors appropriately
00073          */
00074         Exception::dontPrint();
00075 
00076         /*
00077          * Create a file.
00078          */
00079         H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
00080 
00081         /*
00082         * Create property list for a dataset and set up fill values.
00083         */
00084         int fillvalue = 0;   /* Fill value for the dataset */
00085         DSetCreatPropList plist;
00086         plist.setFillValue(PredType::NATIVE_INT, &fillvalue);
00087 
00088         /*
00089          * Create dataspace for the dataset in the file.
00090          */
00091         hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; // dim sizes of ds (on disk)
00092         DataSpace fspace( FSPACE_RANK, fdim );
00093 
00094         /*
00095          * Create dataset and write it into the file.
00096          */
00097         DataSet* dataset = new DataSet(file->createDataSet(
00098                 DATASET_NAME, PredType::NATIVE_INT, fspace, plist));
00099 
00100         /*
00101          * Select hyperslab for the dataset in the file, using 3x2 blocks,
00102          * (4,3) stride and (2,4) count starting at the position (0,1).
00103          */
00104         hsize_t start[2]; // Start of hyperslab
00105         hsize_t stride[2]; // Stride of hyperslab
00106         hsize_t count[2];  // Block count
00107         hsize_t block[2];  // Block sizes
00108         start[0]  = 0; start[1]  = 1;
00109         stride[0] = 4; stride[1] = 3;
00110         count[0]  = 2; count[1]  = 4;
00111         block[0]  = 3; block[1]  = 2;
00112         fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
00113 
00114         /*
00115          * Create dataspace for the first dataset.
00116          */
00117         hsize_t dim1[] = {MSPACE1_DIM};  /* Dimension size of the first dataset
00118                                            (in memory) */
00119         DataSpace mspace1( MSPACE1_RANK, dim1 );
00120 
00121         /*
00122          * Select hyperslab.
00123          * We will use 48 elements of the vector buffer starting at the
00124          * second element.  Selected elements are 1 2 3 . . . 48
00125          */
00126         start[0]  = 1;
00127         stride[0] = 1;
00128         count[0]  = 48;
00129         block[0]  = 1;
00130         mspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
00131 
00132         /*
00133          * Write selection from the vector buffer to the dataset in the file.
00134          *
00135          * File dataset should look like this:
00136          *                    0  1  2  0  3  4  0  5  6  0  7  8
00137          *                    0  9 10  0 11 12  0 13 14  0 15 16
00138          *                    0 17 18  0 19 20  0 21 22  0 23 24
00139          *                    0  0  0  0  0  0  0  0  0  0  0  0
00140          *                    0 25 26  0 27 28  0 29 30  0 31 32
00141          *                    0 33 34  0 35 36  0 37 38  0 39 40
00142          *                    0 41 42  0 43 44  0 45 46  0 47 48
00143          *                    0  0  0  0  0  0  0  0  0  0  0  0
00144          */
00145         int    vector[MSPACE1_DIM];     // vector buffer for dset
00146 
00147         /*
00148          * Buffer initialization.
00149          */
00150         vector[0] = vector[MSPACE1_DIM - 1] = -1;
00151         for (i = 1; i < MSPACE1_DIM - 1; i++)
00152             vector[i] = i;
00153 
00154         dataset->write( vector, PredType::NATIVE_INT, mspace1, fspace );
00155 
00156         /*
00157          * Reset the selection for the file dataspace fid.
00158          */
00159         fspace.selectNone();
00160 
00161         /*
00162          * Create dataspace for the second dataset.
00163          */
00164         hsize_t dim2[] = {MSPACE2_DIM};  /* Dimension size of the second dataset
00165                                            (in memory */
00166         DataSpace mspace2( MSPACE2_RANK, dim2 );
00167 
00168         /*
00169          * Select sequence of NPOINTS points in the file dataspace.
00170          */
00171         hsize_t coord[NPOINTS][FSPACE_RANK]; /* Array to store selected points
00172                                                 from the file dataspace */
00173         coord[0][0] = 0; coord[0][1] = 0;
00174         coord[1][0] = 3; coord[1][1] = 3;
00175         coord[2][0] = 3; coord[2][1] = 5;
00176         coord[3][0] = 5; coord[3][1] = 6;
00177 
00178         fspace.selectElements( H5S_SELECT_SET, NPOINTS, (const hsize_t *)coord);
00179 
00180         /*
00181          * Write new selection of points to the dataset.
00182          */
00183         int    values[] = {53, 59, 61, 67};  /* New values to be written */
00184         dataset->write( values, PredType::NATIVE_INT, mspace2, fspace );
00185 
00186         /*
00187          * File dataset should look like this:
00188          *                   53  1  2  0  3  4  0  5  6  0  7  8
00189          *                    0  9 10  0 11 12  0 13 14  0 15 16
00190          *                    0 17 18  0 19 20  0 21 22  0 23 24
00191          *                    0  0  0 59  0 61  0  0  0  0  0  0
00192          *                    0 25 26  0 27 28  0 29 30  0 31 32
00193          *                    0 33 34  0 35 36 67 37 38  0 39 40
00194          *                    0 41 42  0 43 44  0 45 46  0 47 48
00195          *                    0  0  0  0  0  0  0  0  0  0  0  0
00196          *
00197          */
00198 
00199         /*
00200          * Close the dataset and the file.
00201          */
00202         delete dataset;
00203         delete file;
00204 
00205         /*
00206          * Open the file.
00207          */
00208         file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
00209 
00210         /*
00211          * Open the dataset.
00212          */
00213         dataset = new DataSet( file->openDataSet( DATASET_NAME ));
00214 
00215         /*
00216          * Get dataspace of the dataset.
00217          */
00218         fspace = dataset->getSpace();
00219 
00220         /*
00221          * Select first hyperslab for the dataset in the file. The following
00222          * elements are selected:
00223          *                     10  0 11 12
00224          *                     18  0 19 20
00225          *                      0 59  0 61
00226          *
00227          */
00228         start[0] = 1; start[1] = 2;
00229         block[0] = 1; block[1] = 1;
00230         stride[0] = 1; stride[1] = 1;
00231         count[0]  = 3; count[1]  = 4;
00232         fspace.selectHyperslab(H5S_SELECT_SET, count, start, stride, block);
00233 
00234         /*
00235          * Add second selected hyperslab to the selection.
00236          * The following elements are selected:
00237          *                    19 20  0 21 22
00238          *                     0 61  0  0  0
00239          *                    27 28  0 29 30
00240          *                    35 36 67 37 38
00241          *                    43 44  0 45 46
00242          *                     0  0  0  0  0
00243          * Note that two hyperslabs overlap. Common elements are:
00244          *                                              19 20
00245          *                                               0 61
00246          */
00247         start[0] = 2; start[1] = 4;
00248         block[0] = 1; block[1] = 1;
00249         stride[0] = 1; stride[1] = 1;
00250         count[0]  = 6; count[1]  = 5;
00251         fspace.selectHyperslab(H5S_SELECT_OR, count, start, stride, block);
00252 
00253         /*
00254          * Create memory dataspace.
00255          */
00256         hsize_t mdim[] = {MSPACE_DIM1, MSPACE_DIM2}; /* Dimension sizes of the
00257                                                    dataset in memory when we
00258                                                    read selection from the
00259                                                    dataset on the disk */
00260         DataSpace mspace(MSPACE_RANK, mdim);
00261 
00262         /*
00263          * Select two hyperslabs in memory. Hyperslabs has the same
00264          * size and shape as the selected hyperslabs for the file dataspace.
00265          */
00266         start[0] = 0; start[1] = 0;
00267         block[0] = 1; block[1] = 1;
00268         stride[0] = 1; stride[1] = 1;
00269         count[0]  = 3; count[1]  = 4;
00270         mspace.selectHyperslab(H5S_SELECT_SET, count, start, stride, block);
00271         start[0] = 1; start[1] = 2;
00272         block[0] = 1; block[1] = 1;
00273         stride[0] = 1; stride[1] = 1;
00274         count[0]  = 6; count[1]  = 5;
00275         mspace.selectHyperslab(H5S_SELECT_OR, count, start, stride, block);
00276 
00277         /*
00278          * Initialize data buffer.
00279          */
00280         int matrix_out[MSPACE_DIM1][MSPACE_DIM2];
00281         for (i = 0; i < MSPACE_DIM1; i++)
00282             for (j = 0; j < MSPACE_DIM2; j++)
00283                 matrix_out[i][j] = 0;
00284 
00285         /*
00286          * Read data back to the buffer matrix.
00287          */
00288         dataset->read(matrix_out, PredType::NATIVE_INT, mspace, fspace);
00289 
00290         /*
00291          * Display the result.  Memory dataset is:
00292          *
00293          *                    10  0 11 12  0  0  0  0  0
00294          *                    18  0 19 20  0 21 22  0  0
00295          *                     0 59  0 61  0  0  0  0  0
00296          *                     0  0 27 28  0 29 30  0  0
00297          *                     0  0 35 36 67 37 38  0  0
00298          *                     0  0 43 44  0 45 46  0  0
00299          *                     0  0  0  0  0  0  0  0  0
00300          *                     0  0  0  0  0  0  0  0  0
00301          */
00302         for (i=0; i < MSPACE_DIM1; i++)
00303         {
00304             for(j=0; j < MSPACE_DIM2; j++)
00305                 cout << matrix_out[i][j] << "  ";
00306             cout << endl;
00307         }
00308 
00309         /*
00310          * Close the dataset and the file.
00311          */
00312         delete dataset;
00313         delete file;
00314    }  // end of try block
00315 
00316    // catch failure caused by the H5File operations
00317    catch( FileIException error )
00318    {
00319         error.printError();
00320         return -1;
00321    }
00322 
00323    // catch failure caused by the DataSet operations
00324    catch( DataSetIException error )
00325    {
00326         error.printError();
00327         return -1;
00328    }
00329 
00330    // catch failure caused by the DataSpace operations
00331    catch( DataSpaceIException error )
00332    {
00333         error.printError();
00334         return -1;
00335    }
00336 
00337    return 0;
00338 }

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