1 #include "qdaqh5file.h"
3 #include "QDaqObject.h"
5 #include "QDaqDataBuffer.h"
9 #include "h5helper_v1_1.h"
11 #define CLASS_ATTR_NAME "Class"
13 QDaqH5File::QDaqH5File() : helper_(0)
18 QDaqH5File::~QDaqH5File()
20 if (helper_)
delete helper_;
23 bool QDaqH5File::h5write(
const QDaqObject* obj,
const QString& filename)
35 Exception::dontPrint();
41 file =
new H5File( filename.toLatin1(), H5F_ACC_TRUNC );
47 helper()->write(file,
"Timestamp", QDateTime::currentDateTime().toString(Qt::ISODate));
48 helper()->write(file,
"FileType",
"QDaq");
49 helper()->write(file,
"FileVersionMajor", QString::number(helper()->major()));
50 helper()->write(file,
"FileVersionMinor", QString::number(helper()->minor()));
53 writeRecursive(file,obj);
58 catch( FileIException error )
60 S =
"HDF5 File Error.";
62 S += error.getCFuncName();
64 S += error.getCDetailMsg();
68 catch( DataSetIException error )
70 S =
"HDF5 DataSet Error.";
72 S += error.getCFuncName();
74 S += error.getCDetailMsg();
78 catch( DataSpaceIException error )
80 S =
"HDF5 DataSpace Error.";
82 S += error.getCFuncName();
84 S += error.getCDetailMsg();
88 catch( AttributeIException error )
90 S =
"HDF5 Attribute Error.";
92 S += error.getCFuncName();
94 S += error.getCDetailMsg();
98 catch( DataTypeIException error )
100 S =
"HDF5 DataType Error.";
101 S +=
" In function ";
102 S += error.getCFuncName();
104 S += error.getCDetailMsg();
108 catch( PropListIException error )
110 S =
"HDF5 PropList Error.";
111 S +=
" In function ";
112 S += error.getCFuncName();
114 S += error.getCDetailMsg();
118 catch( GroupIException error )
120 S =
"HDF5 Group Error.";
121 S +=
" In function ";
122 S += error.getCFuncName();
124 S += error.getCDetailMsg();
127 if (file)
delete file;
139 QDaqObject *QDaqH5File::h5read(
const QString& filename)
152 Exception::dontPrint();
157 file =
new H5File( filename.toLatin1(), H5F_ACC_RDONLY );
162 QString fileType, vMajor, vMinor;
164 bool ret = helper()->read(file,
"FileType",fileType);
166 lastError_ =
"Error reading file: 'FileType' field not found";
169 if (fileType!=
"QDaq") {
170 lastError_ =
"Error reading file: 'FileType' is not 'QDaq'";
173 ret = helper()->read(file,
"FileVersionMajor",vMajor);
175 lastError_ =
"Error reading file: 'FileVersionMajor' field not found";
178 ret = helper()->read(file,
"FileVersionMinor",vMinor);
180 lastError_ =
"Error reading file: 'FileVersionMinor' field not found";
183 Version fileVersion = toVersion(vMajor.toInt(),vMinor.toInt());
184 if (fileVersion==V_INVALID) {
185 lastError_ = QString(
"Error reading file: Not a valid version, vMajor = %1, vMinor = %2").arg(vMajor).arg(vMinor);
189 newHelper(fileVersion);
190 readRecursive(file,obj);
195 catch( FileIException error )
197 S =
"HDF5 File Error.";
198 S +=
" In function ";
199 S += error.getCFuncName();
201 S += error.getCDetailMsg();
205 catch( DataSetIException error )
207 S =
"HDF5 DataSet Error.";
208 S +=
" In function ";
209 S += error.getCFuncName();
211 S += error.getCDetailMsg();
215 catch( DataSpaceIException error )
217 S =
"HDF5 DataSpace Error.";
218 S +=
" In function ";
219 S += error.getCFuncName();
221 S += error.getCDetailMsg();
225 catch( AttributeIException error )
227 S =
"HDF5 Attribute Error.";
228 S +=
" In function ";
229 S += error.getCFuncName();
231 S += error.getCDetailMsg();
235 catch( DataTypeIException error )
237 S =
"HDF5 DataType Error.";
238 S +=
" In function ";
239 S += error.getCFuncName();
241 S += error.getCDetailMsg();
245 catch( PropListIException error )
247 S =
"HDF5 PropList Error.";
248 S +=
" In function ";
249 S += error.getCFuncName();
251 S += error.getCDetailMsg();
255 catch( GroupIException error )
257 S =
"HDF5 Group Error.";
258 S +=
" In function ";
259 S += error.getCFuncName();
261 S += error.getCDetailMsg();
264 if (file)
delete file;
275 void QDaqH5File::writeRecursive(CommonFG* h5g,
const QDaqObject* obj)
277 H5::Group objGroup = helper()->createGroup(h5g, obj->objectName().toLatin1().constData());
284 void QDaqH5File::readRecursive(CommonFG* h5g,
QDaqObject* &parent_obj)
286 bool isRoot = parent_obj==0;
287 QByteArrayList groups = helper()->getGroupNames(h5g, isRoot);
289 foreach(QByteArray groupName, groups) {
290 Group g = h5g->openGroup(groupName);
292 if (helper()->read(&g,CLASS_ATTR_NAME,className))
294 if (className==
"QDaqRoot") className =
"QDaqObject";
305 readRecursive(&g, obj);
311 if (isRoot) helper()->connectDeferedPointers();
314 void QDaqH5File::newHelper(Version v)
323 helper_ =
new h5helper_v1_0(
this);
return;
325 helper_ =
new h5helper_v1_1(
this);
return;
327 helper_ =
new h5helper_v1_1(
this);
return;
333 bool h5helper::h5exist_ds(CommonFG* h5obj,
const char* name)
336 return H5Lexists(h5obj->getLocId(),name,0) &&
337 H5Oget_info_by_name(h5obj->getLocId(),name,&info,0)>=0 &&
338 info.type==H5O_TYPE_DATASET;
341 QString h5helper::groupName(CommonFG* h5obj)
343 int sz = H5Iget_name(h5obj->getLocId(),0,0);
345 QByteArray ba(sz+1,
char(0));
346 H5Iget_name(h5obj->getLocId(),ba.data(),sz+1);
348 }
else return QString();
351 void h5helper::deferObjPtrRead(
QDaqObject *obj,
const char *name,
const QString &path)
353 deferedPtrData d(obj,name,path);
357 void h5helper::deferObjPtrRead(
QDaqObject *obj,
const char *name,
const QStringList &paths)
359 deferedPtrData d(obj,name,paths);
Base class of all QDaq objects.
QDaqObject * createObject(const QString &name, const QString &className)
Create a QDaqObject with name name and class className.
static QDaqRoot * root()
Obtain a pointer to the one-and-only QDaqRoot object.
QDaqObjectList children() const
Return a list of children of this object.
virtual void writeh5(H5::Group *g, QDaqH5File *f) const
Write contents of the object to a H5 group.
QDaqObject * appendChild(QDaqObject *obj)
Adds a new child QDaqObject, as the last child and returns a pointer to it.
virtual void readh5(H5::Group *g, QDaqH5File *f)
Read contents of the object from a H5 group.