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  * 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 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_DIM1 = 8;    // We will read dataset back from the file...
00055 const int   MSPACE_DIM2 = 12;   // ...to the dataset in memory with these ...
00056                                 // ...dataspace parameters
00057 const int   NPOINTS = 4;        // Number of points that will be selected...
00058                                 //    ...and overwritten
00059 
00060 int main (void)
00061 {
00062 #ifdef SKIP_UNTIL_APRIL_2009
00063    /*
00064    * Buffers' initialization.
00065    */
00066    int   i,j;
00067    int    vector[MSPACE1_DIM];
00068    vector[0] = vector[MSPACE1_DIM - 1] = -1;
00069    for (i = 1; i < MSPACE1_DIM - 1; i++)
00070       vector[i] = i;
00071 
00072    int    matrix[MSPACE_DIM1][MSPACE_DIM2];
00073    for (i = 0; i < MSPACE_DIM1; i++)
00074    {
00075       for (j = 0; j < MSPACE_DIM2; j++)
00076          matrix[i][j] = 0;
00077    }
00078 
00079    // Try block to detect exceptions raised by any of the calls inside it
00080    try
00081    {
00082       /*
00083        * Turn off the auto-printing when failure occurs so that we can
00084        * handle the errors appropriately
00085        */
00086       Exception::dontPrint();
00087 
00088       /*
00089        * Create a file.
00090        */
00091       H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
00092 
00093       /*
00094        * Create dataspace for the dataset in the file.
00095        */
00096       hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; // dim sizes of ds (on disk)
00097       DataSpace fspace( FSPACE_RANK, fdim );
00098 
00099       /*
00100        * Create dataset and write it into the file.
00101        */
00102       DataSet* dataset = new DataSet(
00103             file->createDataSet( DATASET_NAME, PredType::NATIVE_INT, fspace ));
00104       dataset->write( matrix, PredType::NATIVE_INT );
00105 
00106       /*
00107        * Select hyperslab for the dataset in the file, using 3x2 blocks,
00108        * (4,3) stride and (2,4) count starting at the position (0,1).
00109        */
00110       hsize_t start[2]; // Start of hyperslab
00111       hsize_t stride[2]; // Stride of hyperslab
00112       hsize_t count[2];  // Block count
00113       hsize_t block[2];  // Block sizes
00114       start[0]  = 0; start[1]  = 1;
00115       stride[0] = 4; stride[1] = 3;
00116       count[0]  = 2; count[1]  = 4;
00117       block[0]  = 3; block[1]  = 2;
00118       fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
00119 
00120       /*
00121        * Create dataspace for the first dataset.
00122        */
00123       hsize_t dim1[] = {MSPACE1_DIM};  /* Dimension size of the first dataset
00124                                          (in memory) */
00125       DataSpace mspace1( MSPACE1_RANK, dim1 );
00126 
00127       /*
00128        * Select hyperslab.
00129        * We will use 48 elements of the vector buffer starting at the
00130        * second element.  Selected elements are 1 2 3 . . . 48
00131        */
00132       start[0]  = 1;
00133       stride[0] = 1;
00134       count[0]  = 48;
00135       block[0]  = 1;
00136       mspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
00137 
00138       /*
00139        * Write selection from the vector buffer to the dataset in the file.
00140        *
00141        * File dataset should look like this:
00142        *                    0  1  2  0  3  4  0  5  6  0  7  8
00143        *                    0  9 10  0 11 12  0 13 14  0 15 16
00144        *                    0 17 18  0 19 20  0 21 22  0 23 24
00145        *                    0  0  0  0  0  0  0  0  0  0  0  0
00146        *                    0 25 26  0 27 28  0 29 30  0 31 32
00147        *                    0 33 34  0 35 36  0 37 38  0 39 40
00148        *                    0 41 42  0 43 44  0 45 46  0 47 48
00149        *                    0  0  0  0  0  0  0  0  0  0  0  0
00150        */
00151        dataset->write( vector, PredType::NATIVE_INT, mspace1, fspace );
00152 
00153       /*
00154        * Reset the selection for the file dataspace fid.
00155        */
00156       fspace.selectNone();
00157 
00158       /*
00159        * Create dataspace for the second dataset.
00160        */
00161       hsize_t dim2[] = {MSPACE2_DIM};  /* Dimension size of the second dataset
00162                                          (in memory */
00163       DataSpace mspace2( MSPACE2_RANK, dim2 );
00164 
00165       /*
00166        * Select sequence of NPOINTS points in the file dataspace.
00167        */
00168       hsize_t coord[NPOINTS][FSPACE_RANK]; /* Array to store selected points
00169                                               from the file dataspace */
00170       coord[0][0] = 0; coord[0][1] = 0;
00171       coord[1][0] = 3; coord[1][1] = 3;
00172       coord[2][0] = 3; coord[2][1] = 5;
00173       coord[3][0] = 5; coord[3][1] = 6;
00174 
00175       fspace.selectElements( H5S_SELECT_SET, NPOINTS, (const hsize_t *)coord);
00176 
00177       /*
00178        * Write new selection of points to the dataset.
00179        */
00180       int    values[] = {53, 59, 61, 67};  /* New values to be written */
00181       dataset->write( values, PredType::NATIVE_INT, mspace2, fspace );
00182 
00183       /*
00184        * File dataset should look like this:
00185        *                   53  1  2  0  3  4  0  5  6  0  7  8
00186        *                    0  9 10  0 11 12  0 13 14  0 15 16
00187        *                    0 17 18  0 19 20  0 21 22  0 23 24
00188        *                    0  0  0 59  0 61  0  0  0  0  0  0
00189        *                    0 25 26  0 27 28  0 29 30  0 31 32
00190        *                    0 33 34  0 35 36 67 37 38  0 39 40
00191        *                    0 41 42  0 43 44  0 45 46  0 47 48
00192        *                    0  0  0  0  0  0  0  0  0  0  0  0
00193        *
00194        */
00195 
00196       /*
00197        * Close the dataset and the file.
00198        */
00199       delete dataset;
00200       delete file;
00201 
00202       /*
00203        * Open the file.
00204        */
00205       file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
00206 
00207       /*
00208        * Open the dataset.
00209        */
00210       dataset = new DataSet( file->openDataSet( DATASET_NAME ));
00211 
00212       /*
00213        * Read data back to the buffer matrix.
00214        */
00215       dataset->read( matrix, PredType::NATIVE_INT );
00216 
00217       /*
00218        * Display the result.
00219        */
00220       for (i=0; i < MSPACE_DIM1; i++)
00221       {
00222           for(j=0; j < MSPACE_DIM2; j++)
00223              cout << matrix[i][j] << "  ";
00224           cout << endl;
00225       }
00226 
00227       /*
00228        * Close the dataset and the file.
00229        */
00230       delete dataset;
00231       delete file;
00232    }  // end of try block
00233 
00234    // catch failure caused by the H5File operations
00235    catch( FileIException error )
00236    {
00237       error.printError();
00238       return -1;
00239    }
00240 
00241    // catch failure caused by the DataSet operations
00242    catch( DataSetIException error )
00243    {
00244       error.printError();
00245       return -1;
00246    }
00247 
00248    // catch failure caused by the DataSpace operations
00249    catch( DataSpaceIException error )
00250    {
00251       error.printError();
00252       return -1;
00253    }
00254 #endif
00255    return 0;
00256 }

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