I'm having some trouble writing and reading HDF5-files using C++, and
hope someone can help
me out, as I'm quite stuck now.
I'm trying to write a data set containing some dates (as a compound
type) to a hdf5 file in one application,
which will be read from another application. (The file will later
contain more than only dates)
I use the following code to write the dates to a hdf5-file:
class Date
{
private:
uint32_t year;
uint32_t month;
uint32_t day;
}
HF::H5File histogramsFile(filename.c_str(), H5F_ACC_TRUNC)
std::vector<Date> dates = getDates();
hsize_t datasetDim[] = {dates.size()};
int rank = 1;
H5::DataSpace dateDataspace(rank, datasetDim);
// Create the memory type
H5::CompType dateType(sizeof(Date));
size_t size_of_date = sizeof(Date);
H5std_string year("year");
H5std_string month("month");
H5std_string day("day");
dateType.insertMember(year, sizeof(uint32_t)*0,
H5::PredType::NATIVE_INT32);
dateType.insertMember(month, sizeof(uint32_t)*1,
H5::PredType::NATIVE_INT32);
dateType.insertMember(day, sizeof(uint32_t)*2,
H5::PredType::NATIVE_INT32);
H5::DataSet dateDataSet = histogramsFile.createDataSet("dates",
dateType, dateDataspace);
dateDataSet.write(&dates[0], dateType);
histograms.close()
This generates a file named histograms.h5 which looks like this when
using h5dump:
HDF5 ".\histograms.h5" {
GROUP "/" {
DATASET "dates" {
DATATYPE H5T_COMPOUND {
H5T_STD_I32LE "@+-";
H5T_STD_I32LE "++-";
H5T_STD_I32LE "@--";
}
DATASPACE SIMPLE { ( 2 ) / ( 2 ) }
DATA {
(0): {
1990,
1,
1
},
(1): {
2000,
6,
15
}
}
}
}
}
My first question is what you think might cause the strange names in
the compound datatype. The names
should have been "year", "month", "day".
After the file has been written I try to read it again, to verify that
the data is written correctly and
can be retreived:
// Structure for storing dates
struct histogram_date_t{
int day;
int month;
int year;
};
H5::H5File histograms(stdhistogrampath.c_str(), H5F_ACC_RDONLY);
H5::CompType date_type(sizeof(histogram_date_t));
date_type.insertMember("year", HOFFSET(histogram_date_t, year),
H5::PredType::NATIVE_INT32);
date_type.insertMember("month", HOFFSET(histogram_date_t, month),
H5::PredType::NATIVE_INT32);
date_type.insertMember("day", HOFFSET(histogram_date_t, day),
H5::PredType::NATIVE_INT32);
H5::DataSet dates_set = histograms.openDataSet("dates");
H5::DataSpace dates_space = dates_set.getSpace();
hsize_t tmpsize[1];
dates_space.getSimpleExtentDims(tmpsize, NULL);
hssize_t numgroups = tmpsize[0];
std::vector<Date> seqnums_cache;
std::vector<histogram_date_t> dates_vec(numgroups);
seqnums_cache.reserve(numgroups);
dates_set.read(&dates_vec[0], date_type);
for(std::vector<histogram_date_t>::iterator it = dates_vec.begin();
it!=dates_vec.end(); ++it){
seqnums_cache.push_back(Date(it->year, it->month, it->day));
}
This code produces an H5::DataTypeIException at the line:
date_type.insertMember("month", HOFFSET(histogram_date_t, month),
H5::PredType::NATIVE_INT32);
HDF5-DIAG: Error detected in HDF5 (1.8.1) thread 0:
#000: ..\..\..\src\H5Tcompound.c line 347 in H5Tinsert(): member name
is not unique
major: Datatype
minor: Unable to insert object
I get the same error when using an H5std_string, or std::string as
first argument to insertMember.
However, when I use std::string.c_str() I get the same exception but
the error message:
HDF5-DIAG: Error detected in HDF5 (1.8.1) thread 0:
#000: ..\..\..\src\H5Tcompound.c line 353 in H5Tinsert(): unable to
insert member
major: Datatype
minor: Unable to insert object
#001: ..\..\..\src\H5Tcompound.c line 433 in H5T_insert(): member
name is not unique
major: Datatype
minor: Unable to insert object
Any idea of what goes wrong here? It seems to me that the two issues
(strange characters in h5dump when
writing and error on creating compound type) might be related to
something strange going on when
the methods accept H5std_string.
My environment is Windows 7 64-bit using C++ and Visual Studio 2012.
I'm currently using HDF5 version 1.8.1,
but I have tried upgrading to 1.8.10 without any change. I've
reproduced these errors on two windows machines
, but the strange thing is that on Ubuntu it works just fine (h5dump
shows the correct names in the
compound type, and the file is read correctly back).
All help and hints to what might be wrong is greatly appreciated.
Lars
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org