QDaq  0.2.6
Qt-based Data Aqcuisition
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
h5helper_v1_1.cpp
1 #include "h5helper_v1_1.h"
2 
3 #include "QDaqObject.h"
4 
5 #include <QDebug>
6 
7 #define DYNAMIC_PROPERTY_TAG "DYNAMIC_PROPERTY"
8 
9 void h5helper_v1_1::write(CommonFG* h5obj, const char* name, const QDaqObject* obj)
10 {
11  if (!obj) {
12  h5helper_v1_0::write(h5obj,name,QString("0"));
13  return;
14  }
15 
16  QString path(obj->objectName());
17  const QDaqObject* top = file_->getTopObject();
18  const QDaqObject* p = obj;
19  const QDaqObject* r = (QDaqObject*)QDaqObject::root();
20  while(p && p!=top && p!=r) {
21  p = p->parent();
22  if (p) {
23  path.push_front('.');
24  path.push_front(p->objectName());
25  }
26  }
27  if (p==top) h5helper_v1_0::write(h5obj,name,path);
28  else {
29  h5helper_v1_0::write(h5obj,name,QString("0"));
30  pushWarning(QString("QDaqObject* property named: %1 of %2 could not be saved. Pointed object is outside of file scope.").arg(name).arg(path));
31  }
32 }
33 
34 void h5helper_v1_1::write(CommonFG *h5obj, const char *name, const QDaqObjectList &objList)
35 {
36  if (objList.isEmpty()) {
37  h5helper_v1_0::write(h5obj,name,QString("0"));
38  return;
39  }
40 
41  const QDaqObject* top = file_->getTopObject();
42  const QDaqObject* r = (QDaqObject*)QDaqObject::root();
43  QStringList pathList;
44 
45  foreach(const QDaqObject* p, objList)
46  {
47  QString path(p->objectName());
48  while(p && p!=top && p!=r) {
49  p = p->parent();
50  if (p) {
51  path.push_front('.');
52  path.push_front(p->objectName());
53  }
54  }
55  if (p==top) pathList << path;
56  else {
57  pathList << QString("0");
58  pushWarning(QString("A ptr in QDaqObjectList property named: %1 of %2 could not be saved. Pointed object is outside of file scope.").arg(name).arg(path));
59  }
60  }
61  h5helper_v1_0::write(h5obj,name,pathList);
62 }
63 
64 void h5helper_v1_1::connectDeferedPointers()
65 {
66  foreach(const deferedPtrData& d, deferedPtrs) {
67 
68  QDaqObjectList objList;
69 
70  foreach(const QString& path, d.pathList) {
71 
72  if (path.isEmpty() || path=="0") {
73  objList << (QDaqObject*)0;
74  continue;
75  }
76 
77  QStringList splitPath = path.split(QChar('.'));
78 
79  QString objName = splitPath.front(); splitPath.pop_front();
80  QDaqObject* p = const_cast<QDaqObject*>(file_->getTopObject());
81  if (objName != p->objectName()) {
82  pushWarning(QString("Error reading %1.%2 from file: Path %3 not found.")
83  .arg(d.obj->objectName())
84  .arg(d.propName)
85  .arg(path));
86  objList << (QDaqObject*)0;
87  continue;
88  }
89 
90  do
91  {
92  objName = splitPath.front();
93  splitPath.pop_front();
94  p = p->findChild(objName);
95  }
96  while (!splitPath.isEmpty() && p);
97 
98  if (p) objList << p;
99  else {
100  pushWarning(QString("Error reading %1.%2 from file: Unknown error.")
101  .arg(d.obj->objectName())
102  .arg(d.propName));
103  objList << (QDaqObject*)0;
104  }
105 
106  }
107 
108  if (d.isList) {
109  bool noNulls = true;
110  foreach(const QDaqObject* p, objList) {
111  if (!p) { noNulls = false; break; }
112  }
113  if (noNulls) d.obj->setProperty(d.propName,QVariant::fromValue(objList));
114  } else {
115  QDaqObject* p = 0;
116  if (!objList.isEmpty()) p = objList.at(0);
117  d.obj->setProperty(d.propName,QVariant::fromValue(p));
118  }
119  }
120 }
121 
122 
123 Group h5helper_v1_1::createGroup(CommonFG* loc, const char* name)
124 {
125  hid_t group_creation_plist;
126  group_creation_plist = H5Pcreate(H5P_GROUP_CREATE);
127  herr_t status = H5Pset_link_creation_order(group_creation_plist,
128  H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED);
129 
130  if (status<0) throw PropListIException("H5Pset_link_creation_order");
131 
132  hid_t group_id;
133  group_id = H5Gcreate(loc->getLocId(),
134  name,
135  H5P_DEFAULT,
136  group_creation_plist,
137  H5P_DEFAULT);
138 
139  Group h5g(group_id);
140 
141  H5Pclose(group_creation_plist);
142  //5Gclose(group_id);
143 
144  return h5g;
145 }
146 
147 QByteArrayList h5helper_v1_1::getGroupNames(CommonFG* h5g, bool isRoot)
148 {
149  QByteArrayList names;
150 
151  int n = h5g->getNumObjs();
152 
153  if (isRoot) {
154  for(int i=0; i<n; ++i) {
155  H5G_obj_t typ = h5g->getObjTypeByIdx(i);
156  if (typ==H5G_GROUP)
157  {
158  QByteArray groupName;
159  groupName.fill('\0',256);
160  h5g->getObjnameByIdx(i,groupName.data(),256);
161  names.push_back(groupName);
162  }
163  }
164  } else {
165  for(int i=0; i<n; ++i) {
166  QByteArray groupName;
167  groupName.fill('\0',256);
168 
169  herr_t ret = H5Lget_name_by_idx(h5g->getLocId(), ".", H5_INDEX_CRT_ORDER, H5_ITER_INC,
170  i, groupName.data(), 256, 0);
171  if (ret<0) break;
172  H5O_info_t object_info;
173  ret = H5Oget_info_by_name(h5g->getLocId(), groupName, &object_info, 0);
174  if (object_info.type == H5O_TYPE_GROUP) names.push_back(groupName);
175 
176  }
177  }
178 
179  return names;
180 }
181 
182 /*
183  * Dynamic Property Read/Write Conversion Rules
184  *
185  * QDaq <-> HDF5
186  * bool int
187  * number float
188  * string string
189  * stringlist string 2D
190  * vector array of float
191  *
192  */
193 
194 void h5helper_v1_1::writeDynamicProperties(CommonFG* h5obj, const QDaqObject* m_object)
195 {
196  if (m_object->dynamicPropertyNames().isEmpty()) return;
197 
198 
199  foreach(const QByteArray& propName, m_object->dynamicPropertyNames())
200  {
201  if (lockedPropertyList_.contains(propName)) continue;
202 
203  QVariant v = m_object->property(propName.constData());
204 
205  if (QDaqTypes::isBool(v)) h5helper_v1_0::write(h5obj,propName.constData(),(int)v.toBool());
206  else if (QDaqTypes::isNumeric(v)) h5helper_v1_0::write(h5obj,propName.constData(),v.toDouble());
207  else if (QDaqTypes::isString(v)) h5helper_v1_0::write(h5obj,propName.constData(),v.toString());
208  else if (QDaqTypes::isStringList(v)) h5helper_v1_0::write(h5obj,propName.constData(),QDaqTypes::toStringList(v));
209  else if (QDaqTypes::isVector(v)) h5helper_v1_0::write(h5obj,propName.constData(),QDaqTypes::toVector(v));
210  else continue;
211 
212  try {
213  DataSet ds = h5obj->openDataSet(propName.constData());
214  DataSpace dspace(H5S_SCALAR);
215  Attribute attr = ds.createAttribute(DYNAMIC_PROPERTY_TAG,PredType::NATIVE_INT,dspace);
216  int va = 1;
217  attr.write(PredType::NATIVE_INT,&va);
218  }
219  catch(FileIException& e)
220  {
221  qDebug() << "File exception writing dynamic property " << propName;
222 
223  }
224  catch(GroupIException& e)
225  {
226  qDebug() << "Group exception writing dynamic property " << propName;
227  }
228  catch(AttributeIException& e)
229  {
230  qDebug() << "Attribute exception writing dynamic property " << propName;
231  }
232 
233 
234  }
235 
236 }
237 
238 void h5helper_v1_1::readDynamicProperties(CommonFG* h5g, QDaqObject* m_object)
239 {
240  int n = h5g->getNumObjs();
241 
242  for(int i=0; i<n; ++i) {
243  H5G_obj_t typ = h5g->getObjTypeByIdx(i);
244  if (typ==H5G_DATASET)
245  {
246  QByteArray propName;
247  propName.fill('\0',256);
248  h5g->getObjnameByIdx(i,propName.data(),256);
249 
250  DataSet ds = h5g->openDataSet(propName.constData());
251 
252  if (ds.attrExists(DYNAMIC_PROPERTY_TAG))
253  {
254  H5T_class_t type_class = ds.getTypeClass();
255  DataSpace dspace = ds.getSpace();
256 
257  if (type_class==H5T_INTEGER) { // bool
258  int sz = dspace.getSimpleExtentNpoints();
259  if (sz>1) {
260  qDebug() << "Invalid dynamic prop in HDF5"; // only scalar is supported
261  } else {
262  int val;
263  ds.read(&val, ds.getDataType());
264  bool b = (bool)val;
265  m_object->setProperty(propName.constData(),QVariant::fromValue(b));
266  }
267  } else if (type_class==H5T_FLOAT) {
268  int sz = dspace.getSimpleExtentNpoints();
269  if (sz>1) {
270  QDaqVector val;
271  val.setCapacity(sz);
272  ds.read(val.data(), ds.getDataType());
273  m_object->setProperty(propName.constData(),QVariant::fromValue(val));
274  } else {
275  double val;
276  ds.read(&val, ds.getDataType());
277  m_object->setProperty(propName.constData(),QVariant::fromValue(val));
278  }
279  } else if (type_class==H5T_STRING) {
280  // get string type & size
281  StrType st = ds.getStrType();
282  int ncols = st.getSize();
283  hsize_t sz;
284  dspace.getSimpleExtentDims(&sz);
285  int nrows = sz;
286  QByteArray buff(nrows*ncols,'0');
287  ds.read(buff.data(),st,dspace);
288  if (nrows>1) {
289  QStringList val;
290  for(int i=0; i<nrows; i++)
291  {
292  QByteArray ba(buff.constData() + i*ncols, ncols);
293  val << QString(ba);
294  }
295  m_object->setProperty(propName.constData(),QVariant::fromValue(val));
296  } else {
297  QString val(buff);
298  m_object->setProperty(propName.constData(),QVariant::fromValue(val));
299  }
300  } else {
301  qDebug() << "Invalid dynamic prop in HDF5";
302  }
303  }
304  }
305  }
306 }
307 
308 
Base class of all QDaq objects.
Definition: QDaqObject.h:108
void setCapacity(int c)
Set the capacity.
Definition: QDaqVector.h:75
static QDaqRoot * root()
Obtain a pointer to the one-and-only QDaqRoot object.
Definition: QDaqObject.h:168
QDaqObject * findChild(const QString &name) const
Find the first child QDaqObject with objectName equal to name.
Definition: QDaqObject.h:275
A buffer for storing double numbers.
Definition: QDaqVector.h:40
QList< QDaqObject * > QDaqObjectList
A QList of QDaqObject pointers.
Definition: QDaqObject.h:23
double * data()
Return a pointer to the data.
Definition: QDaqVector.h:100
QDaqObject * parent() const
Definition: QDaqObject.h:256