/**
* @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 [email protected]
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.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org