00001 #include "hdf5orthoreader.h"
00002 #include "hdf5_hl.h"
00003
00004 HDF5OrthoReader::HDF5OrthoReader(bool debuginfo)
00005 {
00006 m_file =-1;
00007 if (!debuginfo)
00008 {
00009 H5Eset_auto(H5E_DEFAULT,NULL,stderr);
00010 H5Eset_auto(H5E_DEFAULT,NULL,stdout);
00011 }
00012
00013 }
00014
00015
00016
00017 OrthoMesh& HDF5OrthoReader::getMesh(string strMeshName)
00018 {
00019 if (_meshCash.find(strMeshName) != _meshCash.end())
00020 {
00021 return *(_meshCash[strMeshName]);
00022 }
00023 else
00024 {
00025 _meshCash[strMeshName]=loadOrthoMesh(strMeshName);
00026 return *(_meshCash[strMeshName]);
00027 }
00028 }
00029
00030
00031 OrthoMesh* HDF5OrthoReader::loadOrthoMesh(std::string strMeshName)
00032 {
00033 char path[500];
00034 sprintf(path,"Triangulations/%s",strMeshName.c_str());
00035 hid_t Gtria = H5Gopen1(getFile(),path);
00036 if (Gtria < 0)
00037 throw new Exception("HDF5OrthoReader::loadOrthoMesh Could not find mesh \"%s\"",path);
00038 HDF5OrthoInfo info;
00039 info.readOrthoMeshInfo(Gtria);
00040 OrthoMesh* pMesh = new OrthoMesh(info.P0,info.P1,info.nElems[0],info.nElems[1],info.nElems[2],info.wells);
00041 return pMesh;
00042 }
00043
00044
00045 hid_t HDF5OrthoReader::getFile()
00046 {
00047 assert(m_file > 0);
00048 return m_file;
00049 }
00050
00051
00052 void HDF5OrthoReader::list()
00053 {
00054 hid_t triaG = openGroup("Triangulations");
00055 hid_t test = H5Gopen1(triaG,"./tria0");
00056 char name2[1000];
00057 H5Iget_name(test,name2,1000);
00058 printf("Test %s\n",name2);
00059 hsize_t numObjs;
00060 H5Gget_num_objs(triaG,&numObjs);
00061 for (unsigned i =0;i<numObjs;i++)
00062 {
00063 char name[1000];
00064 H5Gget_objname_by_idx(triaG,i,name,1000);
00065 printf("%s\n",name);
00066 }
00067
00068 }
00069
00070
00071 #define STR_SIZE 1000
00072 void HDF5OrthoReader::iterateTree(hid_t id,HDF5Operator &op)
00073 {
00074
00075 switch(H5Iget_type(id))
00076 {
00077 case H5I_GROUP:
00078 {
00079 char strElem[STR_SIZE];
00080 hsize_t nElems;
00081
00082 op.processGroup(id);
00083
00084
00085 unsigned nAtts = H5Aget_num_attrs(id);
00086 for (unsigned i=0;i<nAtts;i++)
00087 {
00088 hid_t attId = H5Aopen_idx(id,i);
00089 iterateTree(attId,op);
00090 H5Aclose(attId);
00091 }
00092 H5Gget_num_objs(id,&nElems);
00093 for(unsigned i=0;i<nElems;i++)
00094 {
00095 H5Gget_objname_by_idx(id,i,strElem,STR_SIZE);
00096 hsize_t type =H5Gget_objtype_by_idx(id,i);
00097 switch(type)
00098 {
00099 case H5G_LINK:
00100 {
00101 printf("Ignoring link %s\n",strElem);
00102 break;
00103 }
00104 case H5G_GROUP:
00105 {
00106 hid_t idChild=H5Gopen(id,strElem,H5P_DEFAULT);
00107 iterateTree(idChild,op);
00108 closeId(idChild);
00109 break;
00110 }
00111 case H5G_DATASET:
00112 {
00113 hid_t idChild=H5Dopen(id,strElem,H5P_DEFAULT);
00114 iterateTree(idChild,op);
00115 closeId(idChild);
00116 break;
00117 }
00118 case H5G_TYPE:
00119 {
00120 printf("Ignoring type %s\n",strElem);
00121 break;
00122 }
00123 }
00124 }
00125 break;
00126 }
00127 case H5I_DATASET:
00128 {
00129 op.processDataSet(id);
00130
00131 unsigned nAtts = H5Aget_num_attrs(id);
00132 for (unsigned i=0;i<nAtts;i++)
00133 {
00134 hid_t attId = H5Aopen_idx(id,i);
00135 iterateTree(attId,op);
00136 H5Aclose(attId);
00137 }
00138 break;
00139 }
00140 case H5I_ATTR:
00141 {
00142 op.processAtt(id);
00143 break;
00144 }
00145 default:
00146 {
00147 throw new Exception("HDF5OrthoReader::iterateTree() Expect HDF5 object to be a dataset, a group or attribute");
00148 }
00149 }
00150 }
00151
00152
00153
00154
00155
00156 std::string HDF5Reader::printType(hsize_t type)
00157 {
00158 switch(type)
00159 {
00160 case H5I_ATTR:
00161 return "ATTRIBUTE";
00162 break;
00163 case H5I_GROUP:
00164 return "GROUP";
00165 break;
00166 case H5I_DATASET:
00167 return "DATASET";
00168 break;
00169 case H5I_FILE:
00170 return "FILE";
00171 break;
00172 case H5I_DATASPACE:
00173 return "DATASPACE";
00174 break;
00175 case H5I_DATATYPE:
00176 return "DATATYPE";
00177 break;
00178 }
00179 return "UNKNOWN";
00180 }
00181
00182
00183 void HDF5OrthoReader::readScalars(VecDouble &vec,std::string dataSetName)
00184 {
00185 hid_t dataset=H5Dopen(getFile(),dataSetName.c_str(),H5P_DEFAULT);
00186 if (dataset < 0)
00187 throw new Exception("Could not read dataset \"%s\"",dataSetName.c_str());
00188
00189 hid_t dataspace=H5Dget_space(dataset);
00190 unsigned rank=H5Sget_simple_extent_ndims(dataspace);
00191 hsize_t dims[rank],maxdims[rank];
00192 H5Sget_simple_extent_dims(dataspace,dims,maxdims);
00193 unsigned nElems=dims[0];
00194 for (unsigned i=1;i<rank;i++)
00195 {
00196 nElems*=dims[i];
00197 }
00198 if (vec.size() != nElems)
00199 vec.reinit(nElems);
00200
00201 H5Dread(dataset,H5T_NATIVE_DOUBLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,vec.begin());
00202 H5Sclose(dataspace);
00203 H5Dclose(dataset);
00204
00205 }
00206
00207