26 std::ifstream f(name.c_str());
41 hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
48 fileID = H5Fcreate(
filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
76 void HDF5File::Copy(std::string
const& dstName, std::shared_ptr<HDF5File> srcFile, std::string
const& srcName)
81 assert(srcFile->fileID>0);
84 err = H5Ocopy(srcFile->fileID, srcName.c_str(),
fileID, dstName.c_str(), H5P_DEFAULT, H5P_DEFAULT);
88 std::cerr <<
"WARNING: HDF5 could not copy " << srcName <<
" to " << dstName << std::endl;
96 if( (name.compare(
"/")==0) || (name.compare(
"")==0) || (name.compare(
"/.")==0) ) {
129 return Eigen::VectorXi();
136 hid_t dataset = H5Dopen2(
fileID, name.c_str(), H5P_DEFAULT);
139 hid_t space_id = H5Dget_space(dataset);
142 int rank = H5Sget_simple_extent_ndims(space_id);
145 hsize_t* dims = (hsize_t*)malloc(rank*
sizeof(hsize_t));
146 hsize_t* max_dims = (hsize_t*)malloc(rank*
sizeof(hsize_t));
147 H5Sget_simple_extent_dims(space_id, dims, max_dims);
154 Eigen::VectorXi output(rank);
155 for(
int i=0; i<rank; ++i ) {
172 if( (
DoesGroupExist(name))||(name.compare(
"")==0)||(name.compare(
"/")==0) ) {
return; }
182 hid_t newgroup = H5Gcreate2(
fileID, name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
191 std::string
const& attributeName,
192 std::string
const& attribute)
202 H5LTset_attribute_string(
fileID, datasetName.c_str(), attributeName.c_str(), attribute.c_str());
220 H5LTget_attribute_string(
fileID, datasetName.c_str(), attributeName.c_str(), tempStr);
223 return std::string(tempStr);
229 H5Fflush(
fileID, H5F_SCOPE_GLOBAL);
233 struct DataFileInfo {
234 DataFileInfo(std::shared_ptr<HDF5File>
const& hdf5file) : hdf5file(hdf5file) {}
236 const std::shared_ptr<HDF5File> hdf5file;
240 std::string nameBuffer(name);
241 std::string fullGroupName =
"/" + nameBuffer;
244 DataFileInfo* fileInfo =
static_cast<DataFileInfo*
>(op_data);
246 if( info->type==H5O_TYPE_DATASET ) {
247 if( !fileInfo->hdf5file->DoesDataSetExist(fullGroupName) ) {
249 H5Ocopy(o_id, name, fileInfo->hdf5file->fileID, fullGroupName.c_str(), H5P_DEFAULT, H5P_DEFAULT);
251 }
else if( info->type == H5O_TYPE_GROUP ) {
252 if( !fileInfo->hdf5file->DoesGroupExist(fullGroupName) ) {
254 H5Ocopy(o_id, name, fileInfo->hdf5file->fileID, fullGroupName.c_str(), H5P_DEFAULT, H5P_DEFAULT);
264 assert(otherFile->fileID>0);
270 const std::string rootGroupName =
"/";
271 const hid_t otherRootGroup = H5Gopen2(otherFile->fileID, rootGroupName.c_str(), H5P_DEFAULT);
273 auto dataInfo = std::make_shared<DataFileInfo>(shared_from_this());
276 const herr_t status = H5Ovisit(otherRootGroup, H5_INDEX_NAME, H5_ITER_NATIVE, &
CopyObjectToGlobalFile,
static_cast<void*
>(dataInfo.get()));
281 H5Gclose(otherRootGroup);
294 status = H5Oget_info_by_name(
fileID, name.c_str(), &info, H5P_DEFAULT);
299 return info.type == H5O_TYPE_DATASET;
311 status = H5Oget_info_by_name(
fileID, name.c_str(), &info, H5P_DEFAULT);
316 return info.type == H5O_TYPE_GROUP;
325 return std::vector<std::string>();
331 hid_t gid = H5Gopen2(
fileID, base.c_str(), H5P_DEFAULT);
338 herr_t status = H5Gget_num_objs(gid, &nobj);
341 std::vector<std::string> output(nobj);
344 for(
int i = 0; i < nobj; i++)
346 len = H5Gget_objname_by_idx(gid, (hsize_t)i, name, (
size_t)1024);
347 output.at(i) = std::string(name,name +
len);
herr_t CopyObjectToGlobalFile(hid_t o_id, const char *name, const H5O_info_t *info, void *op_data)
void Open(std::string const &filename_)
Opens or creates the file.
HDF5File(std::string const &filename_)
Open or create the file.
bool DoesGroupExist(std::string const &name) const
Check to see if a group exists.
virtual ~HDF5File()
If HDF5File is destroyed, the file should be closed.
void Close()
Close the file.
hid_t fileID
The HDF5 file ID.
void FlushFile()
Flush any data in the HDF5 buffer to the file.
void CreateGroup(std::string const &name)
Create a new group in the file.
bool DoesFileExist(const std::string &name) const
void WriteStringAttribute(std::string const &datasetName, std::string const &attributeName, std::string const &attribute)
Write a string attribute to a dataset or group.
std::string GetStringAttribute(std::string const &datasetName, std::string const &attributeName) const
Read a string attribute from the HDF5 file.
bool IsDataSet(std::string const &name) const
void Copy(std::string const &destName, std::shared_ptr< HDF5File > srcFile, std::string const &srcName)
Copy the contents of one dataset into another.
bool IsGroup(std::string const &name) const
std::vector< std::string > GetChildren(std::string base="/") const
Get a list of immediate children of a group.
Eigen::VectorXi GetDataSetSize(std::string const name) const
Get the size of a dataset (rows,cols)
void MergeFile(std::shared_ptr< HDF5File > const &otherFile)
Merge another file into this file.
bool DoesDataSetExist(std::string const &name) const
Check to see if a data set exists.
std::string filename
The name of the file.
std::string GetParentPath(std::string const &base)