#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include std::size_t GetFileSize(const std::string &filename) { std::ifstream file(filename.c_str(), std::ios::binary | std::ios::ate); return file.tellg(); } template struct HDF5Attribute { hid_t attribHandler; htri_t attribExists; hid_t attribType; herr_t readError; T value; int readAttribute(const std::string& attribName, hid_t obj_id); }; /** * @brief The HDF5FileData class * HDF5 file image reader */ class HDF5FileData { std::vector data; public: typedef int HDF5_FILESTATE; static const HDF5_FILESTATE HDF5_FILESTATE_EXISTS_DIFF = -2; static const HDF5_FILESTATE HDF5_FILESTATE_EXISTS_SAME = -3; static const HDF5_FILESTATE HDF5_FILESTATE_FAIL = -1; static const HDF5_FILESTATE HDF5_FILESTATE_SUCCESS = 0; hid_t handle; hid_t magFieldsDSHandle; int isDateValid; int isSRValid; int isTimeValid; std::string year; std::string month; std::string day; std::string hour; std::string minute; std::string second; HDF5Attribute date; HDF5Attribute t0; HDF5Attribute t1; HDF5Attribute samplingRate; const char dateSeparator = '/'; const char timeSeparator = ':'; const unsigned int yearStrLength = 4; const unsigned int monthStrLength = 2; const unsigned int dayStrLength = 2; const unsigned int hourStrLength = 2; const unsigned int minuteStrLength = 2; public: //functions return < 0 if error occured int openImage(const std::string& fileImage); int getFileDir(const std::string &storeDir, const std::string &username, std::string& fullPath) const; herr_t closeImage(); int getFileName(const std::string &username, std::string &fileName) const; int getFullFilePath(const std::string &storeDir, const std::string &username, std::string& fullPath) const; }; void WriteStringAttribute(hid_t dataset_id, const std::string& name, const std::string& value) { hid_t AttributeType = H5Tcopy(H5T_C_S1); H5Tset_size(AttributeType, value.length()); hid_t dataspaceHandle = H5Screate(H5S_SCALAR); hid_t AttrHandle = H5Acreate(dataset_id, name.c_str(), AttributeType, dataspaceHandle, H5P_DEFAULT,H5P_DEFAULT); H5Awrite(AttrHandle, AttributeType, &value.front()); H5Aclose (AttrHandle); H5Sclose (dataspaceHandle); } void WriteIntAttribute(hid_t dataset_id, const std::string& name, int value) { hid_t dataspaceHandle = H5Screate(H5S_SCALAR); hid_t AttrHandle = H5Acreate(dataset_id, name.c_str(), H5T_NATIVE_INT, dataspaceHandle, H5P_DEFAULT,H5P_DEFAULT); H5Awrite(AttrHandle, H5T_NATIVE_INT, &value); H5Aclose (AttrHandle); H5Sclose (dataspaceHandle); } template void writeVectorToHDF5File(const std::vector& data, const std::string& filename) { //Write an HDF5 file. The file will be first created, and then in the outer loop a dataset will be created, and in each iteration a single row will be filled hid_t file = H5Fcreate (filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hsize_t dims[2] = {data.size(), 1}; hid_t dcpl = H5Pcreate (H5P_DATASET_CREATE); hid_t space = H5Screate_simple (2, dims, NULL); hid_t dset = H5Dcreate (file,"MagneticFields", H5T_NATIVE_DOUBLE, space, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &data.front()); WriteStringAttribute(dset,"date",("2015/07/23")); WriteStringAttribute(dset,"t0",("12:21:12.222")); WriteStringAttribute(dset,"t1",("21:22:11.444")); WriteIntAttribute(dset,"SamplingRate(Hz)",1000); H5Pclose (dcpl); H5Dclose (dset); H5Sclose (space); H5Fclose (file); } int ReadBinFileToString(const std::string &filename, std::string &data) { std::fstream fileObject(filename.c_str(),std::ios::in | std::ios::binary); if(!fileObject.good()) { return 1; } size_t filesize = GetFileSize(filename); data.resize(filesize); fileObject.read(&data.front(),filesize); fileObject.close(); return 0; } void writestuff(const std::string& filename) { typedef double T; std::vector data; long numOfPoints = 1000; for(long i = 0; i < numOfPoints; i++) { data.push_back( static_cast(i) ); } writeVectorToHDF5File(data,filename); } template <> int HDF5Attribute::readAttribute(const std::string &attribName, hid_t obj_id) { attribExists = H5Aexists(obj_id,attribName.c_str()); if(attribExists) { attribHandler = H5Aopen(obj_id,attribName.c_str(),H5P_DEFAULT); attribType = H5Aget_type(attribHandler); char* str; readError = H5Aread(attribHandler,attribType,&str); // int len = H5Aget_storage_size(attribHandler); value = str; std::cout< int HDF5Attribute::readAttribute(const std::string &attribName, hid_t obj_id) { attribExists = H5Aexists(obj_id,attribName.c_str()); if(attribExists) { attribHandler = H5Aopen(obj_id,attribName.c_str(),H5P_DEFAULT); attribType = H5Aget_type(attribHandler); T val; readError = H5Aread(attribHandler,attribType,&val); value = val; std::cout<= 0) { magFieldsDSHandle = H5Dopen2(handle,"MagneticFields", H5P_DEFAULT); } else { return -1; } if(magFieldsDSHandle >= 0) { date.readAttribute("Date",magFieldsDSHandle); t0.readAttribute("t0",magFieldsDSHandle); t1.readAttribute("t1",magFieldsDSHandle); samplingRate.readAttribute("SamplingRate(Hz)",magFieldsDSHandle); } else { return -2; } return 0; } herr_t HDF5FileData::closeImage() { H5Dclose(magFieldsDSHandle); return H5Fclose(handle); } int main() { //This program writes an hdf5 file, then reads it again and tries to extract info from it, but crashes //The program also has a commented line for opening the file Example.hdf5, which was created by a colleague, and is fine //Why is the new file creating a problem? Apparently H5LTopen_file_image() has a problem //write data std::string filename("file.h5"); writestuff(filename); //read data (choose the other file if you wish) std::string fileData; //THIS FILE CRASHES THE PROGRAM std::cout<<"Success read file into memory: "<< ReadBinFileToString(filename.c_str(),fileData)<