diff --git a/tools/hdf5_file_reader.cpp b/tools/hdf5_file_reader.cpp index 0c83188..3f8017f 100644 --- a/tools/hdf5_file_reader.cpp +++ b/tools/hdf5_file_reader.cpp @@ -54,7 +54,139 @@ bool HDF5_File_Reader::IsValid() return true; } +bool HDF5_File_Reader::OpenGroup(hid_t &file, hid_t &group, string groupName) +{ + file = H5Fopen( m_filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); + if (file==-1) + { + cerr << "HDF5_File_Reader::OpenGroup: opening the given file """ << m_filename << """ failed" << endl; + return 0; + } + if (H5Lexists(file, groupName.c_str(), H5P_DEFAULT)<=0) + { + H5Fclose(file); + return 0; + } + + group = H5Gopen(file, groupName.c_str() ); + if (group<0) + { + cerr << "HDF5_File_Reader::OpenGroup: can't open group """ << groupName << """" << endl; + H5Fclose(file); + return 0; + } + return true; +} + +bool HDF5_File_Reader::ReadAttribute(string grp_name, string attr_name, vector &attr_values) +{ + vector d_attr_values; + if (ReadAttribute(grp_name, attr_name, d_attr_values)==false) + return false; + attr_values.resize(d_attr_values.size(),0); + for (size_t n=0;n &attr_values) +{ + attr_values.clear(); + + hid_t hdf5_file = H5Fopen( m_filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); + if (hdf5_file==-1) + { + cerr << "HDF5_File_Reader::OpenGroup: opening the given file """ << m_filename << """ failed" << endl; + return 0; + } + + if (H5Lexists(hdf5_file, grp_name.c_str(), H5P_DEFAULT)<=0) + { + H5Fclose(hdf5_file); + return false; + } + + hid_t attr = H5Aopen_by_name(hdf5_file, grp_name.c_str(), attr_name.c_str(), H5P_DEFAULT, H5P_DEFAULT); + if (attr==-1) + { + cerr << "HDF5_File_Reader::ReadAttribute: Opening the given Attribute: """ << attr_name << """ failed" << endl; + H5Fclose(hdf5_file); + return false; + } + + hid_t type = H5Aget_type(attr); + if (type<0) + { + cerr << "HDF5_File_Reader::ReadAttribute: Dataset type error" << endl; + H5Aclose(attr); + H5Fclose(hdf5_file); + return false; + } + + attr_values.clear(); + if (H5Tget_class(type)==H5T_FLOAT) + { + size_t numVal = H5Aget_storage_size(attr)/H5Tget_size(type); + hid_t datatype=-1; + void *value=NULL; + float *f_value=NULL; + double *d_value=NULL; + if (H5Tget_size(type)==sizeof(float)) + { + f_value = new float[numVal]; + value = f_value; + datatype = H5T_NATIVE_FLOAT; + } + if (H5Tget_size(type)==sizeof(double)) + { + d_value = new double[numVal]; + value = d_value; + datatype = H5T_NATIVE_DOUBLE; + } + if (H5Aread(attr, datatype, value)<0) + { + cerr << "HDF5_File_Reader::ReadAttribute: Reading the given Attribute failed" << endl; + H5Aclose(attr); + H5Fclose(hdf5_file); + return false; + } + if (f_value) + for (size_t n=0;n ×tep, vector frequencies; + if (ReadFrequencies(frequencies)==false) + return 0; + return frequencies.size(); } bool HDF5_File_Reader::ReadFrequencies(vector &frequencies) { - cerr << "HDF5_File_Reader::ReadFrequencies(): not implemented yet!" << endl; - return false; + if (IsValid()==false) + return false; + + return ReadAttribute("/FieldData/FD","frequency",frequencies); } -complex**** HDF5_File_Reader::GetFDVectorData(size_t idx, float &frequency, unsigned int data_size[]) +complex**** HDF5_File_Reader::GetFDVectorData(size_t idx, unsigned int data_size[]) { - cerr << "HDF5_File_Reader::GetFDVectorData(): not implemented yet!" << endl; - return NULL; + hsize_t nDim; + hsize_t* dims=NULL; + double* data=NULL; + stringstream ds_name; + + // read real values + ds_name << "/FieldData/FD/f" << idx << "_real"; + if (ReadDataSet(ds_name.str(), nDim, dims, data) == false) + return NULL; + if (nDim!=4) + { + cerr << "HDF5_File_Reader::GetFDVectorData: data dimension invalid" << endl; + delete[] dims; + delete[] data; + return NULL; + } + if (dims[0]!=3) + { + cerr << "HDF5_File_Reader::GetFDVectorData: vector data dimension invalid" << endl; + delete[] dims; + delete[] data; + return NULL; + } + data_size[0]=dims[3]; + data_size[1]=dims[2]; + data_size[2]=dims[1]; + delete[] dims; + data_size[3]=3; + size_t pos = 0; + complex**** field = Create_N_3DArray >(data_size); + for (unsigned int d=0;d<3;++d) + for (unsigned int k=0;k >(field, data_size); + return NULL; + } + if (nDim!=4) + { + cerr << "HDF5_File_Reader::GetFDVectorData: data dimension invalid" << endl; + delete[] dims; + delete[] data; + Delete_N_3DArray >(field, data_size); + return NULL; + } + if (dims[0]!=3) + { + cerr << "HDF5_File_Reader::GetFDVectorData: vector data dimension invalid" << endl; + delete[] dims; + delete[] data; + Delete_N_3DArray >(field, data_size); + return NULL; + } + if ((data_size[0]!=dims[3]) || (data_size[1]!=dims[2]) || (data_size[2]!=dims[1])) + { + cerr << "HDF5_File_Reader::GetFDVectorData: data dimension mismatch" << endl; + delete[] dims; + delete[] data; + Delete_N_3DArray >(field, data_size); + return NULL; + } + delete[] dims; + + pos = 0; + complex I(0,1); + for (unsigned int d=0;d<3;++d) + for (unsigned int k=0;k &frequencies, vector****> &FD_data, unsigned int data_size[4]) diff --git a/tools/hdf5_file_reader.h b/tools/hdf5_file_reader.h index f5b6d3f..c29397d 100644 --- a/tools/hdf5_file_reader.h +++ b/tools/hdf5_file_reader.h @@ -46,19 +46,25 @@ public: unsigned int GetNumFrequencies(); bool ReadFrequencies(std::vector &frequencies); - std::complex**** GetFDVectorData(size_t idx, float &frequency, unsigned int data_size[4]); + std::complex**** GetFDVectorData(size_t idx, unsigned int data_size[4]); /*! Calculate */ bool CalcFDVectorData(std::vector &frequencies, std::vector****> &FD_data, unsigned int data_size[4]); + bool ReadAttribute(std::string grp_name, std::string attr_name, std::vector &attr_values); + bool ReadAttribute(std::string grp_name, std::string attr_name, std::vector &attr_values); + bool IsValid(); protected: std::string m_filename; + bool ReadDataSet(std::string ds_name, hsize_t &nDim, hsize_t* &dims, double* &data); bool ReadDataSet(std::string ds_name, hsize_t &nDim, hsize_t* &dims, float* &data); + + bool OpenGroup(hid_t &file, hid_t &group, std::string groupName); }; #endif // HDF5_FILE_READER_H