HDF5 with C++

Hi,

I try to implement HDF structures in C++ for using the datasets with the Boost libraries. Is there any good tutorial for using HDF with C++ class structures, because it's hard for me to translate C examples to C++. I found only the HDF API reference under http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html

At this time I had written code to read/write numeric vector and matrix structure. I have some trouble with arrays especially string types.

Does anyone know a How-To or anything else?

Thanks

Phil

Hi Phil,

Have you looked at
http://www.hdfgroup.org/HDF5/doc/cpplus_RM/examples.html?

Binh-Minh

···

-----Original Message-----
From: hdf-forum-bounces@hdfgroup.org [mailto:hdf-forum-bounces@hdfgroup.org]
On Behalf Of Kraus Philipp
Sent: Sunday, June 06, 2010 11:48 AM
To: hdf-forum@hdfgroup.org
Subject: [Hdf-forum] HDF5 with C++

Hi,

I try to implement HDF structures in C++ for using the datasets with
the Boost libraries. Is there any good tutorial for using HDF with C++
class structures, because it's hard for me to translate C examples to C
++. I found only the HDF API reference under
http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html

At this time I had written code to read/write numeric vector and
matrix structure. I have some trouble with arrays especially string
types.

Does anyone know a How-To or anything else?

Thanks

Phil
  
_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

--
Internal Virus Database is out-of-date.
Checked by AVG.
Version: 7.5.549 / Virus Database: 270.9.0/1778 - Release Date: 11/9/2008
2:14 PM

Hi Phil,

You can take a look at the classes in casa/HDF5 of casacore
(casacore.google.com). It can read or write (hyperslabs of) N-dim arrays
of various basic numeric types (including complex) in datasets. It can
also read/write (small) arrays in attributes.
The classes also deal with exception-safe handling of hid_t variables.

Cheers,
Ger

Kraus Philipp <philipp.kraus@flashpixx.de> 6/6/2010 5:47 PM >>>

Hi,

I try to implement HDF structures in C++ for using the datasets with
the Boost libraries. Is there any good tutorial for using HDF with C++

class structures, because it's hard for me to translate C examples to
C
++. I found only the HDF API reference under
http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html

At this time I had written code to read/write numeric vector and
matrix structure. I have some trouble with arrays especially string
types.

Does anyone know a How-To or anything else?

Thanks

Phil

···

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

I know these examples and they help me to understand the structures, but I
don't find string examples and (string) array examples. I have a hdf5 file
(created with Matlab) and I would like to read in my C++ program. My code:

std::vector<std::string> hdf::readStringVector( const std::string& p_path )
const
    {
  H5::DataSet l_dataset = m_file.openDataSet( p_path.c_str() );
        H5::DataSpace l_dataspace = l_dataset.getSpace();
        
  hsize_t l_size[1];
        l_dataspace.getSimpleExtentDims( l_size );
        l_dataspace.close();

  std::vector<std::string> l_vec( l_size[0] );

  *** How can I read the array data
        
        return l_vec;
    }

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html
Sent from the hdf-forum mailing list archive at Nabble.com.

This is what I use:

//

···

-----------------------------------------------------------------------------
// Reads a String dataset
// -----------------------------------------------------------------------------
herr_t H5Lite::readStringDataset(hid_t loc_id, const std::string& dsetName, std::string &data) {
   hid_t did; // dataset id
   hid_t tid; //type id
   herr_t err = 0;
   herr_t retErr = 0;
   hsize_t size;

   did = H5Dopen(loc_id, dsetName.c_str() );
   if (did < 0) {
     std::cout << "Error Opening Dataset" << std::endl;
     return -1;
   }
   tid = H5Dget_type(did);
   if ( tid >= 0 ) {

     size = H5Dget_storage_size(did);
     std::vector<char> buf(static_cast<int>(size+1), 0x00); //Allocate and Zero the array
     err = H5Dread(did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &(buf.front()) );
     if (err<0) {
       std::cout << "Error Reading string dataset." << std::endl;
       retErr = err;
     } else {
       data.append( &(buf.front()) ); //Append the string to the given string
     }
   }
   CloseH5D(did, err, retErr);
   CloseH5T(tid, err, retErr);
   return retErr;
}

___________________________________________________________
Mike Jackson www.bluequartz.net
Principal Software Engineer mike.jackson@bluequartz.net
BlueQuartz Software Dayton, Ohio

On Jun 7, 2010, at 11:55 AM, Philipp Kraus wrote:

I know these examples and they help me to understand the structures, but I
don't find string examples and (string) array examples. I have a hdf5 file
(created with Matlab) and I would like to read in my C++ program. My code:

std::vector<std::string> hdf::readStringVector( const std::string& p_path )
const
   {
  H5::DataSet l_dataset = m_file.openDataSet( p_path.c_str() );
       H5::DataSpace l_dataspace = l_dataset.getSpace();

  hsize_t l_size[1];
       l_dataspace.getSimpleExtentDims( l_size );
       l_dataspace.close();

  std::vector<std::string> l_vec( l_size[0] );

*** How can I read the array data

       return l_vec;
   }

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html
Sent from the hdf-forum mailing list archive at Nabble.com.

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Hi Phil,

You can read the string into an std::string or a char* buffer then assign it
to a vector element, or read directly to the vector element, I think. Here
is an example using the char* buffer to read a string:

        char *string_ds;
        dataset.read(&string_ds, vlst); // string is variable length.
or
        char string_ds[size_of_your_string]; // or dynamically allocate it
        dataset.read(string_ds, vlst); // string is fixed length.

I hope that helps.
Binh-Minh

···

-----Original Message-----
From: hdf-forum-bounces@hdfgroup.org [mailto:hdf-forum-bounces@hdfgroup.org]
On Behalf Of Philipp Kraus
Sent: Monday, June 07, 2010 11:56 AM
To: hdf-forum@hdfgroup.org
Subject: Re: [Hdf-forum] HDF5 with C++

I know these examples and they help me to understand the structures, but I
don't find string examples and (string) array examples. I have a hdf5 file
(created with Matlab) and I would like to read in my C++ program. My code:

std::vector<std::string> hdf::readStringVector( const std::string& p_path )
const
    {
  H5::DataSet l_dataset = m_file.openDataSet( p_path.c_str() );
        H5::DataSpace l_dataspace = l_dataset.getSpace();
        
  hsize_t l_size[1];
        l_dataspace.getSimpleExtentDims( l_size );
        l_dataspace.close();

  std::vector<std::string> l_vec( l_size[0] );

  *** How can I read the array data
        
        return l_vec;
    }

--
View this message in context:
http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html
Sent from the hdf-forum mailing list archive at Nabble.com.

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

--
Internal Virus Database is out-of-date.
Checked by AVG.
Version: 7.5.549 / Virus Database: 270.9.0/1778 - Release Date: 11/9/2008
2:14 PM

You mix C and C++ code, but I prefere only C++.

Michael Jackson wrote:

This is what I use:

herr_t H5Lite::readStringDataset(hid_t loc_id, const std::string&
     std::vector<char> buf(static_cast<int>(size+1), 0x00); //Allocate
and Zero the array
     err = H5Dread(did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
&(buf.front()) );
     if (err<0) {
       std::cout << "Error Reading string dataset." << std::endl;
       retErr = err;
     } else {
       data.append( &(buf.front()) ); //Append the string to the given
string

I don't find any native C++ code to read data into the std::vector.

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p880263.html
Sent from the hdf-forum mailing list archive at Nabble.com.

/**
  * @brief Reads data from the HDF5 File into an std::vector<T> object. If the dataset
  * is very large this can be an expensive method to use. It is here for convenience
  * using STL with hdf5.
  * @param loc_id The parent location that contains the dataset to read
  * @param dsetName The name of the dataset to read
  * @param data A std::vector<T>. Note the vector WILL be resized to fit the data.
  * The best idea is to just allocate the vector but not to size it. The method
  * will size it for you.
  * @return Standard HDF error condition
  */
template <typename T>
static herr_t readVectorDataset(hid_t loc_id,
                           const std::string& dsetName,
                           std::vector<T> &data)
{
   hid_t did;
   herr_t err = 0;
   herr_t retErr = 0;
   hid_t spaceId;
   hid_t dataType;
   T test = static_cast<T>(0x00);
   dataType = H5Lite::HDFTypeForPrimitive(test);
   if (dataType == -1)
   {
     return -1;
   }
   //std::cout << "HDF5 Data Type: " << H5Lite::HDFTypeForPrimitiveAsStr(test) << std::endl;
  /* Open the dataset. */
// std::cout << " Opening " << dsetName << " for data Retrieval. " << std::endl;
   did = H5Dopen( loc_id, dsetName.c_str() );
   if ( did < 0 ) {
     std::cout << " Error opening Dataset: " << did << std::endl;
     return -1;
   }
   if ( did >= 0 ) {
     spaceId = H5Dget_space(did);
     if ( spaceId > 0 ) {
       int32_t rank = H5Sget_simple_extent_ndims(spaceId);
       if (rank > 0) {
         std::vector<hsize_t> dims;
         dims.resize(rank);// Allocate enough room for the dims
         rank = H5Sget_simple_extent_dims(spaceId, &(dims.front()), NULL);
         hsize_t numElements = 1;
         for (std::vector<hsize_t>::iterator iter = dims.begin(); iter < dims.end(); ++iter ) {
           numElements = numElements * (*iter);
         }
        // std::cout << "NumElements: " << numElements << std::endl;
         //Resize the vector
         data.resize( static_cast<int>(numElements) );
        // for (uint32_t i = 0; i<numElements; ++i) { data[i] = 55555555; }
         err = H5Dread(did, dataType, H5S_ALL, H5S_ALL, H5P_DEFAULT, &( data.front() ) );
         if (err < 0) {
           std::cout << "Error Reading Data." << std::endl;
           retErr = err;
         }
       }
       err = H5Sclose(spaceId);
       if (err < 0 ) {
         std::cout << "Error Closing Data Space" << std::endl;
         retErr = err;
       }
     }
     else
     {
       std::cout << "Error Opening SpaceID" << std::endl;
       retErr = spaceId;
     }
     err = H5Dclose( did );
     if (err < 0 ) {
       std::cout << "Error Closing Dataset" << std::endl;
       retErr = err;
     }
   }
   return retErr;
}

What is the problem mixing C and C++. If you are using HDF5 then you are by definition mixing C and C++.

···

___________________________________________________________
Mike Jackson www.bluequartz.net
Principal Software Engineer mike.jackson@bluequartz.net
BlueQuartz Software Dayton, Ohio

On Jun 8, 2010, at 3:15 PM, Philipp Kraus wrote:

You mix C and C++ code, but I prefere only C++.

Michael Jackson wrote:

This is what I use:

herr_t H5Lite::readStringDataset(hid_t loc_id, const std::string&
    std::vector<char> buf(static_cast<int>(size+1), 0x00); //Allocate
and Zero the array
    err = H5Dread(did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
&(buf.front()) );
    if (err<0) {
      std::cout << "Error Reading string dataset." << std::endl;
      retErr = err;
    } else {
      data.append( &(buf.front()) ); //Append the string to the given
string

I don't find any native C++ code to read data into the std::vector.
--
View this message in context: http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p880263.html
Sent from the hdf-forum mailing list archive at Nabble.com.

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org