00001 #include "hdf5writer.h"
00002 #include "exception.h"
00003 #include <hdf5_hl.h>
00004
00005 using std::string;
00006
00007 HDF5Writer::HDF5Writer()
00008 {
00009 m_file =-1;
00010 H5Eset_auto2(H5E_DEFAULT,(herr_t (*)(hid_t,void*)) &H5Eprint,stdout);
00011
00012 }
00013
00014 void HDF5Writer::setOutputFile(string strFileName)
00015 {
00016 m_strFileName=strFileName;
00017 m_file = H5Fcreate(strFileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
00018 H5Gclose(H5Gcreate1(m_file,"/DataSets",0));
00019 H5Gclose(H5Gcreate1(m_file,"/Triangulations",0));
00020 }
00021
00022
00023 void HDF5Writer::close()
00024 {
00025 if (m_file != -1)
00026 {
00027 H5Fclose(m_file);
00028 }
00029 }
00030 HDF5Writer::~HDF5Writer()
00031 {
00032 close();
00033 }
00034
00035
00044 void HDF5Writer::validateField(string triaId, string fieldName)
00045 {
00046 if ( varsHeap.find(fieldName) != varsHeap.end())
00047 {
00048 printf("HDF5Writer: Field name %s already defined as a variable\n",fieldName.c_str());
00049 abort();
00050 }
00051 if (triaHeap.find(fieldName) != triaHeap.end())
00052 {
00053 printf("HDF5Writer: Field name %s already defined as a triangulation\n",fieldName.c_str());
00054 abort();
00055 }
00056 if (triaHeap.find(triaId) == triaHeap.end())
00057 {
00058 printf("HDF5Writer: Triangulation \"%s\" referenced by field %s not defined\n",triaId.c_str(),fieldName.c_str());
00059 abort();
00060 }
00061
00062 }
00063
00064
00065
00066 void HDF5Writer::setVariable(std::string var, std::string value)
00067 {
00068 varsHeap[var]=value;
00069 }
00070
00071 std::string HDF5Writer::getVariableValue(std::string var)
00072 {
00073 return varsHeap[var];
00074 }
00075
00076
00077 void HDF5Writer::setVariable(std::string var, double dValue)
00078 {
00079 char str[100];
00080 sprintf(str,"%g",dValue);
00081 setVariable(var,str);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00096 HDF5Writer::TriaInformation& HDF5Writer::getTriaInformation(string strName)
00097 {
00098 if (triaHeap.find(strName) == triaHeap.end())
00099 throw new Exception("HDF5Writer: Triangulation %s does not exist",strName.c_str());
00100 return triaHeap.find(strName)->second;
00101 }
00102 bool HDF5Writer::existTriaInformation(string strName)
00103 {
00104 if (triaHeap.find(strName) == triaHeap.end())
00105 return false;
00106 else
00107 return true;
00108 }
00109
00110 int HDF5Writer::getFieldCount(string fieldName)
00111 {
00112 if (fieldInfo.find(fieldName) == fieldInfo.end())
00113 {
00114 fieldInfo[fieldName]=FieldInformation();
00115 return 0;
00116 }
00117 else
00118 {
00119 return fieldInfo[fieldName].refCount;
00120 }
00121 }
00122
00123
00124 void HDF5Writer::incFieldCount(string fieldName)
00125 {
00126 if (fieldInfo.find(fieldName) == fieldInfo.end())
00127 {
00128 fieldInfo[fieldName]=FieldInformation();
00129 }
00130 fieldInfo[fieldName].refCount++;
00131
00132 }
00133
00141 HDF5Writer::VariableIterator HDF5Writer::getVariablesIterator()
00142 {
00143 return this->varsHeap.begin();
00144 }
00145
00146 HDF5Writer::VariableIterator HDF5Writer::getVariablesEnd()
00147 {
00148 return this->varsHeap.end();
00149 }
00150
00151
00152
00153 void HDF5Writer::writeContextVariables(std::string dataSetPath)
00154 {
00155 assert(m_file != -1);
00156
00157 for (HDF5Writer::VariableIterator it = getVariablesIterator();it != getVariablesEnd();it++)
00158 {
00159 H5LTset_attribute_string(m_file,dataSetPath.c_str(),it->first.c_str(),it->second.c_str());
00160 }
00161
00162 }
00163
00164
00165 void HDF5Writer::appendMeshInfo(string triaId,struct TriaInformation info)
00166 {
00167 triaHeap[triaId]=info;
00168 m_lastTriaName = triaId;
00169
00170 }
00171
00172
00173 void HDF5Writer::writeInChunks(hid_t dataset, hsize_t offset[], hsize_t count[],void *buff)
00174 {
00175 if (!count[0])
00176 return;
00177 hid_t dataspace = H5Dget_space(dataset);
00178 H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL,count, NULL);
00179
00180 hid_t datatype = H5Dget_type(dataset);
00181
00182 hsize_t dim[]={count[0],count[1]};
00183 hid_t memspace = H5Screate_simple(2,dim,NULL);
00184
00185 int error = H5Dwrite(dataset,datatype,memspace,dataspace,H5P_DEFAULT,buff);
00186 assert(error >=0);
00187
00188 H5Sclose(memspace);
00189 H5Sclose(dataspace);
00190 H5Tclose(datatype);
00191 }
00192
00200 void HDF5Writer::putMeshIntoDictionary(string triaId,unsigned verts,unsigned cells)
00201 {
00202 appendMeshInfo(triaId,TriaInformation(verts,cells));
00203
00204 }
00205
00206
00207
00208
00209 void HDF5Writer::setAtt(string objPath,string attName,string value)
00210 {
00211 herr_t error = H5LTset_attribute_string(getFile(),objPath.c_str(),attName.c_str(),value.c_str());
00212 if (error < 0)
00213 throw new Exception("HDF5OrthoWriter::setAtt error trying to set attribute \"%s/%s\"",objPath.c_str(),attName.c_str());
00214 }
00215
00216
00217
00218 void HDF5Writer::setAtt(string objPath,string attName,double dValue)
00219 {
00220 char value[100];
00221 sprintf(value,"%g",dValue);
00222 herr_t error = H5LTset_attribute_string(getFile(),objPath.c_str(),attName.c_str(),value);
00223 if (error < 0)
00224 throw new Exception("HDF5OrthoWriter::setAtt error trying to set attribute \"%s/%s\"",objPath.c_str(),attName.c_str());
00225 }
00226