00001 #include "hdf5reader.h"
00002 #include <hdf5_hl.h>
00003 #include "exception.h"
00004
00005
00006
00007 std::string getFileNameFromPath(std::string path)
00008 {
00009 unsigned i=path.size()-1;
00010 while (i>=0 && path[i]!='/')
00011 i--;
00012 return path.substr(i,path.size());
00013
00014 }
00015
00016
00017 std::string HDF5Reader::getDataSetTriangulation(std::string datasetName)
00018 {
00019 char strTria[1000];
00020 if (H5LTget_attribute_string(m_file, datasetName.c_str(), "Triangulation", strTria) < 0)
00021 throw new Exception("Dataset %s not found\n", datasetName.c_str());
00022 else
00023 return strTria;
00024 }
00025
00026
00027 std::vector<std::string> HDF5Reader::getMeshList()
00028 {
00029 std::vector<std::string> stringList;
00030
00031 hid_t group_id = H5Gopen1(m_file,"/Triangulations");
00032
00033
00034 H5G_info_t group_info;
00035 H5Gget_info( group_id, &group_info );
00036
00037 char name[1000];
00038 for (hsize_t i=0;i<group_info.nlinks;i++)
00039 {
00040 H5Lget_name_by_idx(group_id,".",H5_INDEX_NAME,H5_ITER_INC,i,name,1000,H5P_DEFAULT);
00041 stringList.push_back(name);
00042 }
00043 return stringList;
00044 }
00045
00046
00047
00048
00049 std::vector<std::string> HDF5Reader::getFieldList()
00050 {
00051 std::vector<std::string> stringList;
00052
00053 hid_t group_id = H5Gopen1(m_file,"/DataSets");
00054
00055
00056 H5G_info_t group_info;
00057 H5Gget_info( group_id, &group_info );
00058
00059 char name[1000];
00060 for (hsize_t i=0;i<group_info.nlinks;i++)
00061 {
00062 H5Lget_name_by_idx(group_id,".",H5_INDEX_NAME,H5_ITER_INC,i,name,1000,H5P_DEFAULT);
00063 stringList.push_back(name);
00064 }
00065 return stringList;
00066 }
00067
00068
00069 std::vector<std::string> HDF5Reader::ls(std::string path)
00070 {
00071 std::vector<std::string> stringList;
00072
00073 hid_t group_id = H5Gopen1(m_file,path.c_str());
00074
00075
00076 H5G_info_t group_info;
00077 H5Gget_info( group_id, &group_info );
00078
00079 char name[1000];
00080 for (hsize_t i=0;i<group_info.nlinks;i++)
00081 {
00082 H5Lget_name_by_idx(group_id,".",H5_INDEX_NAME,H5_ITER_INC,i,name,1000,H5P_DEFAULT);
00083 stringList.push_back(name);
00084 }
00085 return stringList;
00086
00087 }
00088
00089
00090 HDF5Reader::HDF5Reader()
00091 {
00092 m_file = -1;
00093 }
00094
00095 void HDF5Reader::open(std::string strFileName)
00096 {
00097 close();
00098 _fileName=strFileName;
00099 m_file = H5Fopen(strFileName.c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
00100 if (m_file < 0)
00101 throw new Exception("File name \"%s\" not found",strFileName.c_str());
00102 }
00103
00104
00105
00106 void HDF5Reader::close()
00107 {
00108 if (m_file >=0)
00109 {
00110 H5Fclose(m_file);
00111 }
00112 m_file=-1;
00113 }
00114
00115
00116 HDF5Reader::~HDF5Reader()
00117 {
00118 close();
00119 }
00120
00121
00122
00123 hid_t HDF5Reader::openGroup(std::string groupName)
00124 {
00125 hid_t grp = H5Gopen1(m_file,groupName.c_str());
00126 if (grp <= 0)
00127 throw new Exception("HDF5OrthoReader::openGroup Fail to open group %s",groupName.c_str());
00128 return grp;
00129 }
00130
00131
00132 hid_t HDF5Reader::openId(hid_t rootId, std::string path,hsize_t type)
00133 {
00134 hid_t result;
00135 switch(type)
00136 {
00137 case H5I_ATTR:
00138 result= H5Aopen(rootId,path.c_str(),H5P_DEFAULT);
00139 break;
00140 case H5I_GROUP:
00141 result= H5Gopen1(rootId,path.c_str());
00142 break;
00143 case H5I_DATASET:
00144 result= H5Dopen1(rootId,path.c_str());
00145 break;
00146 case H5I_BADID:
00147 throw new Exception("HDF5OrthoRead::openId Invalid object at \"%s\" not recognized",path.c_str());
00148 default:
00149 throw new Exception("HDF5OrthoRead::openId Data type \"%s %d\" in path \"%s\" not recognized",printType(type).c_str(),type,path.c_str());
00150 result=-1;
00151 }
00152 if (result <= 0)
00153 throw new Exception("HDF5OrthoRead::openId Can't open %s",path.c_str());
00154 return result;
00155
00156 }
00157
00158
00159 void HDF5Reader::closeId(hid_t id)
00160 {
00161 switch(H5Iget_type(id))
00162 {
00163 case H5I_ATTR:
00164 H5Aclose(id);
00165 break;
00166 case H5I_GROUP:
00167 H5Gclose(id);
00168 break;
00169 case H5I_DATASET:
00170 H5Dclose(id);
00171 break;
00172 default:
00173 throw new Exception("HDF5Read::openId Data type not recognized");
00174 }
00175
00176 }
00177
00178
00186 std::string HDF5Reader::readAtt(std::string strPath,std::string attName,bool mustexist)
00187 {
00188
00189 if (m_file == -1)
00190 {
00191 throw new Exception("HDF5Reader::readAtt error, open the file first.");
00192 }
00193
00194 hid_t att = H5Aopen_by_name(m_file,strPath.c_str(), attName.c_str(),H5P_DEFAULT,H5P_DEFAULT);
00195 if (att < 0 && mustexist)
00196 throw new Exception("Attribute %s/%s not found",strPath.c_str(),attName.c_str());
00197 if (att<0 && !mustexist)
00198 return std::string();
00199
00200
00201 hid_t datatype = H5Aget_type(att);
00202 if (H5Tget_class(datatype) != H5T_STRING)
00203 throw new Exception("Attribute %s/%s expected to be a string",strPath.c_str(),attName.c_str());
00204
00205 size_t size=H5Tget_size(datatype);
00206 char value[size];
00207 H5Aclose(att);
00208 H5Tclose(datatype);
00209
00210 hid_t error=H5LTget_attribute_string(m_file,strPath.c_str(),attName.c_str(),value);
00211 if (error>=0)
00212 return value;
00213 else
00214 return "";
00215 }
00216
00217
00218 VecIndex HDF5Reader::getDataSetDim(std::string path)
00219 {
00220 if (m_file == -1)
00221 throw new Exception("HDF5Reader::getDataSetDim: Open the file first");
00222 VecIndex resp;
00223 int rank;
00224 H5LTget_dataset_ndims(m_file,path.c_str(),&rank);
00225 resp.resize(rank);
00226
00227 hsize_t dims[rank];
00228 H5T_class_t class_id;
00229 size_t type_size;
00230 H5LTget_dataset_info(m_file,path.c_str(),dims,&class_id,&type_size);
00231
00232 for (int i=0;i<rank;i++)
00233 resp[i]=(Index) dims[i];
00234 return resp;
00235 }
00236
00237