cctw  0.2.1
cctwchunkeddata.cpp
Go to the documentation of this file.
1 #include "cctwchunkeddata.h"
2 #include "cctwdatachunk.h"
3 #include "cctwapplication.h"
4 #include <QDir>
5 #include "lzf_filter.h"
6 
7 
8 #if QT_VERSION >= 0x050000
9 #include <QUrlQuery>
10 #endif
11 
12 #include "cctwdebug.h"
13 #include "qcepmutexlocker.h"
14 
16 
18  (CctwApplication *application,
19  CctwIntVector3D dim,
20  CctwIntVector3D chunkSize,
21  bool isInput,
22  QString name,
23  QObject *parent)
24  :CctwObject(name, parent),
25  m_Application(application),
26  m_IsNeXus(false),
27  m_DimensionsCache(dim),
28  m_ChunkSizeCache(chunkSize),
29  m_ChunkCountCache((dim-CctwIntVector3D(1,1,1))/chunkSize + CctwIntVector3D(1,1,1)),
30  m_DataFileName(application->saver(), this, "dataFileName", "input.h5", "HDF5 Data File Name"),
31  m_DataSetName(application->saver(), this, "dataSetName", "data", "HDF5 Dataset name"),
32  m_MaskDataFileName(application->saver(), this, "maskDataFileName", "", "Mask Data File Name"),
33  m_MaskDataSetName(application->saver(), this, "maskDataSetName", "", "Mask Dataset Name"),
34  m_Mask(QcepSettingsSaverWPtr(), this, "mask", QcepIntVector(), "Mask Image"),
35  m_AnglesDataFileName(application->saver(), this, "anglesDataFileName", "", "Angles Data File Name"),
36  m_AnglesDataSetName(application->saver(), this, "anglesDataSetName", "", "Angles Dataset Name"),
37  m_Angles(QcepSettingsSaverWPtr(), this, "angles", QcepDoubleVector(), "Angles"),
38  m_WeightsDataFileName(application->saver(), this, "weightsDataFileName", "", "Weights Data File Name"),
39  m_WeightsDataSetName(application->saver(), this, "weightsDataSetName", "", "Weights Dataset Name"),
40  m_Weights(QcepSettingsSaverWPtr(), this, "weights", QcepDoubleVector(), "Weights"),
41  m_Dimensions(application->saver(), this, "dimensions", m_DimensionsCache, "Dataset Dimensions"),
42  m_ChunkSize(application->saver(), this, "chunkSize", m_ChunkSizeCache, "Chunk Size"),
43  m_ChunkCount(QcepSettingsSaverWPtr(), this, "chunkCount", m_ChunkCountCache, "Chunk Count"),
44  m_Compression(application->saver(), this, "compression", 0, "Compression Level"),
45  m_Normalization(QcepSettingsSaverWPtr(), this, "normalization", 1, "Normalization"),
46  m_HDFChunkSize(application->saver(), this, "hdfChunkSize", CctwIntVector3D(0,0,0), "HDF File Chunk Size"),
47  m_ChunksRead(QcepSettingsSaverWPtr(), this, "chunksRead", 0, "Chunks read from input"),
48  m_ChunksWritten(QcepSettingsSaverWPtr(), this, "chunksWritten", 0, "Chunks written to output"),
49  m_ChunksHeld(QcepSettingsSaverWPtr(), this, "chunksHeld", 0, "Chunks held on output"),
50  m_ChunksHeldMax(QcepSettingsSaverWPtr(), this, "chunksHeldMax", 0, "Max Chunks held on output"),
51  m_IsInput(isInput),
52  m_TransformOptions(0),
53  m_FileId(-1),
54  m_DatasetId(-1),
55  m_DataspaceId(-1),
56  m_Dataset2Id(-1),
57  m_Dataspace2Id(-1),
58  m_MaskSameFile(false),
59  m_MaskFileId(-1),
60  m_MaskDatasetId(-1),
61  m_MaskDataspaceId(-1),
62  m_AnglesSameFile(false),
63  m_AnglesFileId(-1),
64  m_AnglesDatasetId(-1),
65  m_AnglesDataspaceId(-1),
66  m_WeightsSameFile(false),
67  m_WeightsFileId(-1),
68  m_WeightsDatasetId(-1),
69  m_WeightsDataspaceId(-1)
70 {
71  allocateChunks();
72 // connect(prop_DataFileName(), SIGNAL(valueChanged(QString,int)), this, SLOT(onDataFileNameChanged()));
73 // connect(prop_DataSetName(), SIGNAL(valueChanged(QString,int)), this, SLOT(onDataFileNameChanged()));
74 // connect(prop_MaskDataFileName(), SIGNAL(valueChanged(QString,int)), this, SLOT(onMaskFileNameChanged()));
75 // connect(prop_MaskDataSetName(), SIGNAL(valueChanged(QString,int)), this, SLOT(onMaskFileNameChanged()));
76 // connect(prop_AnglesDataFileName(), SIGNAL(valueChanged(QString,int)), this, SLOT(onAnglesChanged()));
77 // connect(prop_AnglesDataSetName(), SIGNAL(valueChanged(QString,int)), this, SLOT(onAnglesChanged()));
78  connect(this, SIGNAL(dimensionsChanged(CctwIntVector3D)),
79  prop_Dimensions(), SLOT(setValue(CctwIntVector3D)), Qt::DirectConnection);
80  connect(this, SIGNAL(chunkSizeChanged(CctwIntVector3D)),
81  prop_ChunkSize(), SLOT(setValue(CctwIntVector3D)), Qt::DirectConnection);
82  connect(this, SIGNAL(chunkCountChanged(CctwIntVector3D)),
83  prop_ChunkCount(), SLOT(setValue(CctwIntVector3D)), Qt::DirectConnection);
84 
85  connect(prop_Dimensions(), SIGNAL(valueChanged(CctwIntVector3D,int)),
86  this, SLOT(sizingChanged()), Qt::DirectConnection);
87  connect(prop_ChunkSize(), SIGNAL(valueChanged(CctwIntVector3D,int)),
88  this, SLOT(sizingChanged()), Qt::DirectConnection);
89 }
90 
92 {
93 // printMessage("Allocate chunks");
94  QcepMutexLocker lock(__FILE__, __LINE__, &m_ChunkLock);
95 
96  int n = chunkCount().volume();
97 
98  for(int i=0; i<m_DataChunks.count(); i++) {
99  delete m_DataChunks[i];
100  m_DataChunks[i] = NULL;
101  }
102 
103  m_DataChunks.resize(n);
104 
105 // for (int i=0; i<n; i++) {
106 // m_DataChunks[i] = new CctwDataChunk(this, i, tr("chunk-%1").arg(i), parent());
107 // }
108 }
109 
111 {
113 }
114 
116 {
117  if (qcepDebug(DEBUG_PROPERTIES)) {
118  printMessage(tr("Dimensions set to %1").arg(dim.toString()));
119  }
120 
121  if (m_DimensionsCache != dim) {
122  m_DimensionsCache = dim;
123 
125 
126  allocateChunks();
127 
128  set_Dimensions(m_DimensionsCache);
129  set_ChunkCount(m_ChunkCountCache);
130  }
131 }
132 
134 {
135  if (qcepDebug(DEBUG_PROPERTIES)) {
136  printMessage(tr("Chunk Size set to %1").arg(cksz.toString()));
137  }
138 
139  if (m_ChunkSizeCache != cksz) {
140  m_ChunkSizeCache = cksz;
141 
143 
144  allocateChunks();
145 
146  set_ChunkSize(m_ChunkSizeCache);
147  set_ChunkCount(m_ChunkCountCache);
148  }
149 }
150 
152 {
153  QcepIntVector m = get_Mask();
154 
155  m.resize(mx*my);
156 
157  set_Mask(m);
158 }
159 
161 {
162  QcepDoubleVector a = get_Angles();
163 
164  a.resize(n);
165 
166  set_Angles(a);
167 }
168 
170 {
171  QcepDoubleVector a = get_Weights();
172 
173  a.resize(n);
174 
175  set_Weights(a);
176 }
177 
178 void CctwChunkedData::setAngle(int n, double v)
179 {
180  QcepDoubleVector a = get_Angles();
181 
182  if (n >= 0 && n < a.size()) {
183  a.replace(n, v);
184  }
185 
186  set_Angles(a);
187 }
188 
190 {
191  return get_Angles().value(n);
192 }
193 
195 {
196  return get_Weights().value(n);
197 }
198 
199 
200 void CctwChunkedData::setWeight(int n, double v)
201 {
202  QcepDoubleVector a = get_Weights();
203 
204  if (n >= 0 && n < a.size()) {
205  a.replace(n, v);
206  }
207 
208  set_Weights(a);
209 }
210 
212 {
213  if (qcepDebug(DEBUG_PROPERTIES)) {
214  printMessage("Sizing changed");
215  }
216 
217  if (m_DimensionsCache != get_Dimensions()) {
218  setDimensions(get_Dimensions());
219  }
220 
221  if (m_ChunkSizeCache != get_ChunkSize()) {
222  setChunkSize(get_ChunkSize());
223  }
224 
225  set_ChunkCount(m_ChunkCountCache);
226 }
227 
229 {
230  QUrl url(desc);
231 
232  if (qcepDebug(DEBUG_APP)) {
233  printMessage(tr("%1.setDataSource(\"%2\")").arg(get_Name()).arg(CctwApplication::addSlashes(desc)));
234 
235  printMessage(tr("scheme: %1").arg(url.scheme()));
236  printMessage(tr("host: %1").arg(url.host()));
237  printMessage(tr("path: %1").arg(url.path()));
238  printMessage(tr("filename: %1").arg(url.fileName()));
239 #if QT_VERSION >= 0x050000
240  printMessage(tr("query: %1").arg(url.query()));
241 #endif
242  printMessage(tr("fragment: %1").arg(url.fragment()));
243  }
244 
245  set_DataFileName(url.path());
246  set_DataSetName(url.fragment());
247 
248 #if QT_VERSION >= 0x050000
249  if (url.hasQuery()) {
250  QUrlQuery qry(url);
251 
252  QList <QPair <QString, QString> > l = qry.queryItems();
253 
254  QPair<QString, QString> v;
255  foreach (v, l) {
256  if (qcepDebug(DEBUG_APP)) {
257  printMessage(tr(" key: %1").arg(v.first));
258  printMessage(tr(" val: %1").arg(v.second));
259  }
260  }
261 
262  if (qry.hasQueryItem("size")) {
263  QString chunkSize = qry.queryItemValue("size");
264  setChunks(chunkSize);
265  }
266 
267  if (qry.hasQueryItem("dim")) {
268  QString dims = qry.queryItemValue("dim");
269  setDims(dims);
270  }
271 
272  if (qry.hasQueryItem("dataset")) {
273  QString dset = qry.queryItemValue("dataset");
274  setDataset(dset);
275  }
276  }
277 #endif
278 }
279 
281 {
282  if (m_IsInput) {
283  checkInputFile();
284  }
285 }
286 
288 {
289  if (m_IsInput) {
290  checkMaskFile();
291  }
292 }
293 
295 {
296  if (m_IsInput) {
297  checkAnglesFile();
298  }
299 }
300 
301 void CctwChunkedData::setDims(QString desc)
302 {
303  QRegExp r("(\\d+)[Xx,](\\d+)[Xx,](\\d+)", Qt::CaseInsensitive);
304 
305  if (r.exactMatch(desc)) {
306  CctwIntVector3D d(r.cap(1).toInt(), r.cap(2).toInt(), r.cap(3).toInt());
307 
308  setDimensions(d);
309 
310  printMessage(tr("Dataset dimensions set to [%1,%2,%3]").arg(d.x()).arg(d.y()).arg(d.z()));
311  } else {
312  bool ok;
313  int n = desc.toInt(&ok);
314 
315  if (ok) {
316  CctwIntVector3D d(n,n,n);
317 
318  setDimensions(d);
319 
320  printMessage(tr("Dataset dimensions set to [%1,%2,%3]").arg(d.x()).arg(d.y()).arg(d.z()));
321  }
322  }
323 }
324 
325 void CctwChunkedData::setChunks(QString desc)
326 {
327  QRegExp r("(\\d+)[Xx,](\\d+)[Xx,](\\d+)", Qt::CaseInsensitive);
328 
329  if (r.exactMatch(desc)) {
330  CctwIntVector3D cksz(r.cap(1).toInt(), r.cap(2).toInt(), r.cap(3).toInt());
331 
332  setChunkSize(cksz);
333 
334  printMessage(tr("Chunk size set to [%1,%2,%3]").arg(cksz.x()).arg(cksz.y()).arg(cksz.z()));
335  } else {
336  bool ok;
337  int n = desc.toInt(&ok);
338 
339  if (ok) {
340  CctwIntVector3D cksz(n,n,n);
341 
342  setChunkSize(cksz);
343 
344  printMessage(tr("Chunk size set to [%1,%2,%3]").arg(cksz.x()).arg(cksz.y()).arg(cksz.z()));
345  }
346  }
347 }
348 
349 void CctwChunkedData::setDataset(QString desc)
350 {
351  set_DataSetName(desc);
352 }
353 
355 {
356  QUrl url(desc);
357 
358  if (qcepDebug(DEBUG_APP)) {
359  printMessage(tr("%1.setMaskSource(\"%2\")").arg(get_Name()).arg(CctwApplication::addSlashes(desc)));
360 
361  printMessage(tr("scheme: %1").arg(url.scheme()));
362  printMessage(tr("host: %1").arg(url.host()));
363  printMessage(tr("path: %1").arg(url.path()));
364  printMessage(tr("filename: %1").arg(url.fileName()));
365 #if QT_VERSION >= 0x050000
366  printMessage(tr("query: %1").arg(url.query()));
367 #endif
368  printMessage(tr("fragment: %1").arg(url.fragment()));
369  }
370 
371  set_MaskDataFileName(url.path());
372  set_MaskDataSetName(url.fragment());
373 
374 #if QT_VERSION >= 0x050000
375  if (url.hasQuery()) {
376  QUrlQuery qry(url);
377 
378  QList <QPair <QString, QString> > l = qry.queryItems();
379 
380  QPair<QString, QString> v;
381  foreach (v, l) {
382  if (qcepDebug(DEBUG_APP)) {
383  printMessage(tr(" key: %1").arg(v.first));
384  printMessage(tr(" val: %1").arg(v.second));
385  }
386  }
387 
388 // if (qry.hasQueryItem("size")) {
389 // QString chunkSize = qry.queryItemValue("size");
390 // setChunks(chunkSize);
391 // }
392 
393 // if (qry.hasQueryItem("dim")) {
394 // QString dims = qry.queryItemValue("dim");
395 // setDims(dims);
396 // }
397 
398  if (qry.hasQueryItem("dataset")) {
399  QString dset = qry.queryItemValue("dataset");
400  setMaskDataset(dset);
401  }
402  }
403 #endif
404 }
405 
407 {
408  set_MaskDataSetName(desc);
409 }
410 
412 {
413  QUrl url(desc);
414 
415  if (qcepDebug(DEBUG_APP)) {
416  printMessage(tr("%1.setAnglesSource(\"%2\")").arg(get_Name()).arg(CctwApplication::addSlashes(desc)));
417 
418  printMessage(tr("scheme: %1").arg(url.scheme()));
419  printMessage(tr("host: %1").arg(url.host()));
420  printMessage(tr("path: %1").arg(url.path()));
421  printMessage(tr("filename: %1").arg(url.fileName()));
422 #if QT_VERSION >= 0x050000
423  printMessage(tr("query: %1").arg(url.query()));
424 #endif
425  printMessage(tr("fragment: %1").arg(url.fragment()));
426  }
427 
428  set_AnglesDataFileName(url.path());
429  set_AnglesDataSetName(url.fragment());
430 
431 #if QT_VERSION >= 0x050000
432  if (url.hasQuery()) {
433  QUrlQuery qry(url);
434 
435  QList <QPair <QString, QString> > l = qry.queryItems();
436 
437  QPair<QString, QString> v;
438  foreach (v, l) {
439  if (qcepDebug(DEBUG_APP)) {
440  printMessage(tr(" key: %1").arg(v.first));
441  printMessage(tr(" val: %1").arg(v.second));
442  }
443  }
444 
445 // if (qry.hasQueryItem("size")) {
446 // QString chunkSize = qry.queryItemValue("size");
447 // setChunks(chunkSize);
448 // }
449 
450 // if (qry.hasQueryItem("dim")) {
451 // QString dims = qry.queryItemValue("dim");
452 // setDims(dims);
453 // }
454 
455  if (qry.hasQueryItem("dataset")) {
456  QString dset = qry.queryItemValue("dataset");
457  setAnglesDataset(dset);
458  }
459  }
460 #endif
461 }
462 
464 {
465  set_AnglesDataSetName(desc);
466 }
467 
469 {
470  QUrl url(desc);
471 
472  if (qcepDebug(DEBUG_APP)) {
473  printMessage(tr("%1.setWeightsSource(\"%2\")").arg(get_Name()).arg(CctwApplication::addSlashes(desc)));
474 
475  printMessage(tr("scheme: %1").arg(url.scheme()));
476  printMessage(tr("host: %1").arg(url.host()));
477  printMessage(tr("path: %1").arg(url.path()));
478  printMessage(tr("filename: %1").arg(url.fileName()));
479 #if QT_VERSION >= 0x050000
480  printMessage(tr("query: %1").arg(url.query()));
481 #endif
482  printMessage(tr("fragment: %1").arg(url.fragment()));
483  }
484 
485  set_WeightsDataFileName(url.path());
486  set_WeightsDataSetName(url.fragment());
487 
488 #if QT_VERSION >= 0x050000
489  if (url.hasQuery()) {
490  QUrlQuery qry(url);
491 
492  QList <QPair <QString, QString> > l = qry.queryItems();
493 
494  QPair<QString, QString> v;
495  foreach (v, l) {
496  if (qcepDebug(DEBUG_APP)) {
497  printMessage(tr(" key: %1").arg(v.first));
498  printMessage(tr(" val: %1").arg(v.second));
499  }
500  }
501 
502 // if (qry.hasQueryItem("size")) {
503 // QString chunkSize = qry.queryItemValue("size");
504 // setChunks(chunkSize);
505 // }
506 
507 // if (qry.hasQueryItem("dim")) {
508 // QString dims = qry.queryItemValue("dim");
509 // setDims(dims);
510 // }
511 
512  if (qry.hasQueryItem("dataset")) {
513  QString dset = qry.queryItemValue("dataset");
514  setWeightsDataset(dset);
515  }
516  }
517 #endif
518 }
519 
521 {
522  set_WeightsDataSetName(desc);
523 }
524 
526 {
527  return pixelCoord.x() >= 0 &&
528  pixelCoord.y() >= 0 &&
529  pixelCoord.z() >= 0 &&
530  pixelCoord.x() < m_DimensionsCache.x() &&
531  pixelCoord.y() < m_DimensionsCache.y() &&
532  pixelCoord.z() < m_DimensionsCache.z();
533 }
534 
536 {
537  // Return pixel coords of start of chunk chunkIdx
538 
540 
541  return m_ChunkSizeCache*chunkIdx;
542 }
543 
545 {
546  // Return index of chunk containing given pixel
547 
548  CctwIntVector3D res = pixelCoord / m_ChunkSizeCache;
549 
550  return chunkNumberFromIndex(res);
551 }
552 
554 {
556 
557  return chunkNumberFromIndex(CctwIntVector3D(fchk.x(), fchk.y(), fchk.z()));
558 }
559 
561 {
562  return m_ChunkCountCache;
563 }
564 
565 bool CctwChunkedData::containsChunk(int ix, int iy, int iz)
566 {
567  return ix>=0 && ix<m_ChunkCountCache.x() &&
568  iy>=0 && iy<m_ChunkCountCache.y() &&
569  iz>=0 && iz<m_ChunkCountCache.z();
570 }
571 
573 {
574  if (containsChunk(chunkIdx.x(), chunkIdx.y(), chunkIdx.z())) {
575  int xstride = 1;
576  int ystride = m_ChunkCountCache.x();
577  int zstride = m_ChunkCountCache.x()*m_ChunkCountCache.y();
578 
579  return chunkIdx.x()*xstride + chunkIdx.y()*ystride + chunkIdx.z()*zstride;
580  } else {
581  return -1;
582  }
583 }
584 
586 {
587  if (n>=0 && n<m_ChunkCountCache.volume()) {
588  int xstride = 1;
589  int ystride = m_ChunkCountCache.x();
590  int zstride = m_ChunkCountCache.x()*m_ChunkCountCache.y();
591 
592  int z = n / zstride;
593 
594  n %= zstride;
595 
596  int y = n / ystride;
597 
598  n %= ystride;
599 
600  int x = n / xstride;
601 
602  return CctwIntVector3D(x,y,z);
603  } else {
604  return CctwIntVector3D(-1,-1,-1);
605  }
606 }
607 
609 {
610  foreach(CctwDataChunk* p, m_DataChunks) {
611  if (p) {
612  p->clearDependencies();
613  }
614  }
615 }
616 
618 {
619  if (f >= 0 && f < m_DataChunks.count()) {
620  chunk(f) -> addDependency(t);
621  }
622 }
623 
625 {
626  if (n >= 0 && n < m_DataChunks.count()) {
627  QcepMutexLocker lock(__FILE__, __LINE__, &m_ChunkLock);
628 
630 
631  if (chunk == NULL) {
632  chunk = new CctwDataChunk(this, n, tr("Chunk-%1").arg(n), NULL);
633 
634  if (parent()) {
635  chunk->moveToThread(parent()->thread());
636  chunk->setParent(parent());
637  }
638 
639  m_DataChunks[n] = chunk;
640  }
641 
642  if (chunk && chunk->index() != n) {
643  printMessage(tr("Chunk anomaly"));
644  }
645  return chunk;
646  } else {
647  return NULL;
648  }
649 }
650 
652 {
653  if (n >= 0 && n < m_DataChunks.count()) {
654  QcepMutexLocker lock(__FILE__, __LINE__, &m_ChunkLock);
655 
657 
658  if (chunk) {
659  delete chunk;
660  m_DataChunks[n] = NULL;
661  }
662  }
663 }
664 
666 {
667  CctwDataChunk *chk = chunk(n);
668 
669  if (chk) {
670  chk->deallocateData();
671  chk->deallocateWeights();
672  }
673 }
674 
676 {
677  return chunk(chunkNumberFromIndex(idx));
678 }
679 
681 {
682  if (chunk) {
683  int idx = chunk->index();
684 
685  CctwDataChunk *outchunk = this->chunk(idx);
686 
687  if (outchunk) {
688  outchunk -> mergeChunk(chunk);
689  }
690  }
691 }
692 
694 {
695  foreach (CctwDataChunk *dc, m_DataChunks) {
696  if (dc) {
697  dc -> clearMergeCounters();
698  }
699  }
700 
701  set_ChunksRead(0);
702  set_ChunksWritten(0);
703  set_ChunksHeld(0);
704  set_ChunksHeldMax(0);
705 }
706 
708 {
709  CctwDataChunk *chunk = this->chunk(n);
710 
711  if (chunk) {
712  chunk->normalizeChunk();
713  }
714 }
715 
717 {
718  /* Save old error handler */
719  herr_t (*old_func)(hid_t, void*);
720  void *old_client_data;
721  H5Eget_auto(H5E_DEFAULT, &old_func, &old_client_data);
722 
723  /* Turn off error handling */
724  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
725 
726  bool res = openInputFile(true);
727  closeInputFile();
728 
729  /* Restore previous error handler */
730  H5Eset_auto(H5E_DEFAULT, old_func, old_client_data);
731 
732  return res;
733 }
734 
736 {
737  set_Normalization(1);
738 
739  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
740 
741  if (m_FileId >= 0) {
742  return true;
743  }
744 
745  QString fileName = get_DataFileName();
746  QString dataSetName = get_DataSetName();
747 
748  if (fileName.length() == 0) {
749  printMessage("Input file name is empty");
750  return false;
751  }
752 
753  if (dataSetName.length() == 0) {
754  printMessage("Input dataset name is empty");
755  return false;
756  }
757 
758  if (!quietly) {
759  printMessage(tr("About to open input file: %1 dataset: %2")
760  .arg(fileName).arg(dataSetName));
761  }
762 
763  QFileInfo f(fileName);
764 
765  hid_t fileId = -1;
766  hid_t objId = -1;
767  hid_t dsetId = -1;
768  hid_t dspcId = -1;
769  hid_t plist = -1;
770  hid_t dset2Id = -1;
771  hid_t dspc2Id = -1;
772 
773  try
774  {
775  if (!f.exists())
776  throw tr("File %1 does not exist").arg(fileName);
777  if (H5Fis_hdf5(qPrintable(fileName)) <= 0)
778  throw tr("File %1 exists but is not an hdf file").arg(fileName);
779 
780  fileId = H5Fopen(qPrintable(fileName), H5F_ACC_RDONLY, H5P_DEFAULT);
781  if (fileId < 0)
782  throw tr("File %1 could not be opened").arg(fileName);
783 
784  objId = H5Oopen(fileId, qPrintable(dataSetName), H5P_DEFAULT);
785  if (objId <= 0)
786  throw tr("Could not open data object!");
787 
788  H5O_info_t info;
789  if (H5Oget_info(objId, &info) < 0) {
790  throw tr("Could not get object info");
791  }
792 
793  H5Oclose(objId);
794  objId = -1;
795 
796  if (info.type == H5O_TYPE_DATASET) {
797  // Input descriptor names a dataset - normalized data...
798 
799  dsetId = H5Dopen(fileId, qPrintable(dataSetName), H5P_DEFAULT);
800  if (dsetId <= 0)
801  throw tr("Could not open dataset!");
802 
803  dspcId = H5Dget_space(dsetId);
804  if (dspcId < 0)
805  throw tr("Could not get dataspace of existing dataset");
806 
807  hsize_t dims[3];
808  int ndims = H5Sget_simple_extent_ndims(dspcId);
809  if (ndims != 3)
810  throw tr("Dataspace is not 3-dimensional");
811 
812  int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
813  if (ndims2 != 3)
814  throw tr("Could not get dataspace dimensions");
815 
816  setDimensions(CctwIntVector3D(dims[2], dims[1], dims[0]));
817  plist = H5Dget_create_plist(dsetId);
818 
819  if (plist < 0)
820  throw tr("Could not get dataset create plist");
821 
822  hsize_t cksz[3];
823  if (H5Pget_chunk(plist, 3, cksz) < 0) {
824  printMessage(("Could not get dataset chunk size"));
825  set_HDFChunkSize(CctwIntVector3D(0,0,0));
826  setChunkSize(CctwIntVector3D(32,32,32));
827  } else {
828  set_HDFChunkSize(CctwIntVector3D(cksz[2], cksz[1], cksz[0]));
829  setChunkSize(get_HDFChunkSize());
830  }
831 
832  H5Pclose(plist); plist = -1;
833  } else if (info.type == H5O_TYPE_GROUP) {
834  // Input descriptor names a group - intermediate data...
835 
836  dsetId = H5Dopen(fileId, qPrintable(dataSetName+"/v"), H5P_DEFAULT);
837  if (dsetId <= 0)
838  throw tr("Could not open 'V' dataset!");
839 
840  dspcId = H5Dget_space(dsetId);
841  if (dspcId < 0)
842  throw tr("Could not get dataspace of existing dataset");
843 
844  hsize_t dims[3];
845  int ndims = H5Sget_simple_extent_ndims(dspcId);
846  if (ndims != 3)
847  throw tr("Dataspace is not 3-dimensional");
848 
849  int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
850  if (ndims2 != 3)
851  throw tr("Could not get dataspace dimensions");
852 
853  setDimensions(CctwIntVector3D(dims[2], dims[1], dims[0]));
854  plist = H5Dget_create_plist(dsetId);
855 
856  if (plist < 0)
857  throw tr("Could not get dataset create plist");
858 
859  hsize_t cksz[3];
860  if (H5Pget_chunk(plist, 3, cksz) < 0) {
861  printMessage(("Could not get dataset chunk size"));
862  set_HDFChunkSize(CctwIntVector3D(0,0,0));
863  setChunkSize(CctwIntVector3D(32,32,32));
864  } else {
865  set_HDFChunkSize(CctwIntVector3D(cksz[2], cksz[1], cksz[0]));
866  setChunkSize(get_HDFChunkSize());
867  }
868 
869  H5Pclose(plist); plist = -1;
870 
871  dset2Id = H5Dopen(fileId, qPrintable(dataSetName+"/n"), H5P_DEFAULT);
872  if (dset2Id < 0) {
873  printMessage(tr("Could not open 'N' dataset, assuming normalized"));
874  } else {
875  dspc2Id = H5Dget_space(dsetId);
876  if (dspc2Id < 0)
877  throw tr("Could not get dataspace of existing 'N' dataset");
878 
879  hsize_t dims[3];
880  int ndims = H5Sget_simple_extent_ndims(dspc2Id);
881  if (ndims != 3)
882  throw tr("Dataspace is not 3-dimensional");
883 
884  int ndims2 = H5Sget_simple_extent_dims(dspc2Id, dims, NULL);
885  if (ndims2 != 3)
886  throw tr("Could not get dataspace dimensions");
887 
888  if (dimensions() != CctwIntVector3D(dims[2],dims[1],dims[0])) {
889  throw tr("Dimension mismatch between 'V' and 'N' datasets");
890  }
891 
892  set_Normalization(0);
893  }
894  }
895 
896  m_FileId = fileId;
897  m_DatasetId = dsetId;
898  m_DataspaceId = dspcId;
899  m_Dataset2Id = dset2Id;
900  m_Dataspace2Id= dspc2Id;
901 
902  if (!quietly) {
903  printMessage(tr("Opened input file \"%1\" OK, DS \"%2\", Norm %3, DSID %4, DS2ID %5, CMPRS %6")
904  .arg(get_DataFileName()).arg(get_DataSetName())
905  .arg(get_Normalization()).arg(m_DatasetId).arg(m_Dataset2Id)
906  .arg(get_Compression()));
907 
908  CctwIntVector3D hdfChunkSize = get_HDFChunkSize();
909 
910  printMessage(tr("Input dimensions %1, HDF Chunk size %2, Input chunk size %3, Chunk counts %4")
911  .arg(get_Dimensions().toString())
912  .arg(hdfChunkSize.toString())
913  .arg(get_ChunkSize().toString())
914  .arg(get_ChunkCount().toString()));
915  }
916  }
917  catch (QString &msg )
918  {
919  if (!quietly) {
920  printMessage(tr("Anomaly in CctwChunkedData::openInputFile fileId=%1, dsetId=%2, dspcId=%3")
921  .arg(fileId).arg(dsetId).arg(dspcId));
922  printMessage(msg);
923  }
924 
925  if (dspcId >= 0) H5Sclose(dspcId);
926  if (dspc2Id >= 0) H5Sclose(dspc2Id);
927  if (dsetId >= 0) H5Dclose(dsetId);
928  if (dset2Id >= 0) H5Dclose(dset2Id);
929  if (objId >= 0) H5Oclose(objId);
930  if (fileId >= 0) H5Fclose(fileId);
931  if (plist >= 0) H5Pclose(plist);
932 
933  return false;
934  }
935  return true;
936 }
937 
939 {
940  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
941 
942  if (m_IsNeXus)
944 
945  if (m_DataspaceId >= 0) {
946  H5Sclose(m_DataspaceId);
947  m_DataspaceId = -1;
948  }
949 
950  if (m_Dataspace2Id >= 0) {
951  H5Sclose(m_Dataspace2Id);
952  m_Dataspace2Id = -1;
953  }
954 
955  if (m_DatasetId >= 0) {
956  H5Dclose(m_DatasetId);
957  m_DatasetId = -1;
958  }
959 
960  if (m_Dataset2Id >= 0) {
961  H5Dclose(m_Dataset2Id);
962  m_Dataset2Id = -1;
963  }
964 
965  if (m_FileId >= 0) {
966  H5Fclose(m_FileId);
967  m_FileId = -1;
968  }
969 
970  printMessage("Closed input file");
971 }
972 
973 #if NEXUS_ENABLED == 1
974 
976 {
977  bool res = true;
978 
979  m_IsNeXus = true;
980 
981  QString fileName = get_DataFileName();
982  QFileInfo f(fileName);
983 
984  if (!f.exists()) {
985  printMessage(tr("File %1 does not exist").arg(fileName));
986  res = false;
987  } else if (H5Fis_hdf5(qPrintable(fileName)) <= 0) {
988  printMessage(tr("File %1 exists but is not an hdf file").arg(fileName));
989  res = false;
990  }
991 
992  m_NeXusFile = new NeXus::File(qPrintable(fileName));
993  printMessage(tr("NeXus file opened successfully: %1").arg(fileName));
994 
995  return res;
996 }
997 
999 {
1000  m_NeXusFile->close();
1001 }
1002 
1003 #else
1005 {
1006  printMessage(tr("NeXus was not enabled at build time!"));
1007  return false;
1008 }
1010 {
1011  printMessage(tr("NeXus was not enabled at build time!"));
1012 }
1013 #endif
1014 
1016 {
1017  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1018 
1019  if (m_FileId >= 0) {
1020  return true;
1021  }
1022 
1023  QString fileName = get_DataFileName();
1024  QString dataSetName = get_DataSetName();
1025 
1026  if (fileName.length() == 0) {
1027  printMessage("Output file name is empty");
1028  return false;
1029  }
1030 
1031  if (dataSetName.length() == 0) {
1032  printMessage("Output dataset name is empty");
1033  return false;
1034  }
1035 
1036  printMessage(tr("About to open output file: %1 dataset: %2")
1037  .arg(fileName).arg(dataSetName));
1038 
1039  int res = true;
1040 
1041  QFileInfo f(fileName);
1042 
1043  hid_t fileId = -1;
1044  hid_t gplist2 = -1;
1045  hid_t gplist = -1;
1046  hid_t dsetId = -1;
1047  hid_t dspcId = -1;
1048  hid_t dset2Id = -1;
1049  hid_t dspc2Id = -1;
1050  hid_t plist = -1;
1051  hid_t plist2 = -1;
1052 
1053  if (!f.exists()) {
1054  fileId = H5Fcreate(qPrintable(fileName), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
1055 
1056  if (fileId < 0) {
1057  printMessage(tr("File %1 could not be created").arg(fileName));
1058  res = false;
1059  }
1060  } else if (H5Fis_hdf5(qPrintable(fileName)) <= 0) {
1061  printMessage(tr("File %1 exists but is not an hdf file").arg(fileName));
1062  res = false;
1063  } else {
1064  fileId = H5Fopen(qPrintable(fileName), H5F_ACC_RDWR, H5P_DEFAULT);
1065 
1066  if (fileId < 0) {
1067  printMessage(tr("File %1 could not be opened").arg(fileName));
1068  res = false;
1069  }
1070  }
1071 
1072  if (fileId >= 0) {
1073  herr_t (*old_func)(hid_t, void*);
1074  void *old_client_data;
1075  H5Eget_auto(H5E_DEFAULT, &old_func, &old_client_data);
1076 
1077  /* Turn off error handling */
1078  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
1079 
1080  if (get_Normalization()) {
1081  dsetId = H5Dopen(fileId, qPrintable(get_DataSetName()), H5P_DEFAULT);
1082  } else {
1083  dsetId = H5Dopen(fileId, qPrintable(get_DataSetName()+"/v"), H5P_DEFAULT);
1084  dset2Id = H5Dopen(fileId, qPrintable(get_DataSetName()+"/n"), H5P_DEFAULT);
1085  }
1086 
1087  /* Restore previous error handler */
1088  H5Eset_auto(H5E_DEFAULT, old_func, old_client_data);
1089 
1090  if (dsetId >= 0) {
1091  dspcId = H5Dget_space(dsetId);
1092 
1093  if (dspcId < 0) {
1094  printMessage("Couldn't get dataspace of existing dataset");
1095  res = false;
1096  } else {
1097  hsize_t dims[3];
1098  int ndims = H5Sget_simple_extent_ndims(dspcId);
1099 
1100  if (ndims != 3) {
1101  printMessage("Dataspace is not 3-dimensional");
1102  res = false;
1103  } else {
1104  int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
1105 
1106  if (ndims2 != 3) {
1107  printMessage("Couldnt get dataspace dimensions");
1108  res = false;
1109  } else {
1110  if (dims[0] != (hsize_t) dimensions().z() ||
1111  dims[1] != (hsize_t) dimensions().y() ||
1112  dims[2] != (hsize_t) dimensions().x()) {
1113  printMessage("Dataspace dimensions do not match output data");
1114  res = false;
1115  }
1116  }
1117  }
1118  }
1119  } else {
1120  hsize_t dims[3];
1121  dims[0] = dimensions().z();
1122  dims[1] = dimensions().y();
1123  dims[2] = dimensions().x();
1124 
1125  dspcId = H5Screate_simple(3, dims, NULL);
1126 
1127  CctwIntVector3D cksz = get_HDFChunkSize();
1128  int cmprs = get_Compression();
1129 
1130  plist = H5Pcreate(H5P_DATASET_CREATE);
1131 
1132  if (cksz.volume() > 0) {
1133  hsize_t c[3];
1134 
1135  c[0] = cksz.z();
1136  c[1] = cksz.y();
1137  c[2] = cksz.x();
1138 
1139  float zero = 0.0;
1140 
1141  if (H5Pset_chunk(plist, 3, c) < 0) {
1142  printMessage("Failed to set chunk size");
1143  res = false;
1144  } else if (H5Pset_fill_value(plist, H5T_NATIVE_FLOAT, &zero)) {
1145  printMessage("Failed to set fill value");
1146  res = false;
1147  } else if (cmprs > 0) {
1148  if (H5Pset_deflate(plist, cmprs) < 0) {
1149  printMessage("Set deflate failed");
1150  res = false;
1151  }
1152  } else if (cmprs) {
1153  if (H5Pset_filter(plist, H5PY_FILTER_LZF, H5Z_FLAG_OPTIONAL, 0, NULL) < 0) {
1154  printMessage("Set LZF Compress failed");
1155  res = false;
1156  }
1157  }
1158  }
1159 
1160  gplist = H5Pcreate(H5P_LINK_CREATE);
1161 
1162  if (H5Pset_create_intermediate_group(gplist, 1) < 0) {
1163  printMessage(tr("Failed to set create intermediate groups"));
1164  res = false;
1165  } else {
1166  if (get_Normalization()) {
1167  dsetId = H5Dcreate(fileId,
1168  qPrintable(get_DataSetName()),
1169  H5T_NATIVE_FLOAT,
1170  dspcId,
1171  gplist,
1172  plist,
1173  H5P_DEFAULT);
1174  } else {
1175  dsetId = H5Dcreate(fileId,
1176  qPrintable(get_DataSetName()+"/v"),
1177  H5T_NATIVE_FLOAT,
1178  dspcId,
1179  gplist,
1180  plist,
1181  H5P_DEFAULT);
1182  }
1183  }
1184  }
1185 
1186  if (dsetId < 0) {
1187  printMessage(tr("Could not create or find dataset %1").arg(get_DataSetName()));
1188  res = false;
1189  } else if (!get_Normalization()) {
1190  if (dset2Id >= 0) {
1191  dspc2Id = H5Dget_space(dset2Id);
1192 
1193  if (dspc2Id < 0) {
1194  printMessage("Couldn't get dataspace of existing 'N' dataset");
1195  res = false;
1196  } else {
1197  hsize_t dims[3];
1198  int ndims = H5Sget_simple_extent_ndims(dspc2Id);
1199 
1200  if (ndims != 3) {
1201  printMessage("'N' Dataspace is not 3-dimensional");
1202  res = false;
1203  } else {
1204  int ndims2 = H5Sget_simple_extent_dims(dspc2Id, dims, NULL);
1205 
1206  if (ndims2 != 3) {
1207  printMessage("Couldnt get 'N' dataspace dimensions");
1208  res = false;
1209  } else {
1210  if (dims[0] != (hsize_t) dimensions().z() ||
1211  dims[1] != (hsize_t) dimensions().y() ||
1212  dims[2] != (hsize_t) dimensions().x()) {
1213  printMessage("'N' Dataspace dimensions do not match output data");
1214  res = false;
1215  }
1216  }
1217  }
1218  }
1219  } else {
1220  hsize_t dims[3];
1221  dims[0] = dimensions().z();
1222  dims[1] = dimensions().y();
1223  dims[2] = dimensions().x();
1224 
1225  dspc2Id = H5Screate_simple(3, dims, NULL);
1226 
1227  CctwIntVector3D cksz = get_HDFChunkSize();
1228  int cmprs = get_Compression();
1229 
1230  plist2 = H5Pcreate(H5P_DATASET_CREATE);
1231 
1232  if (cksz.volume() > 0) {
1233  hsize_t c[3];
1234 
1235  c[0] = cksz.z();
1236  c[1] = cksz.y();
1237  c[2] = cksz.x();
1238 
1239  float zero = 0.0;
1240 
1241  if (H5Pset_chunk(plist2, 3, c) < 0) {
1242  printMessage("Failed to set chunk size");
1243  res = false;
1244  } else if (H5Pset_fill_value(plist2, H5T_NATIVE_FLOAT, &zero)) {
1245  printMessage("Failed to set fill value");
1246  res = false;
1247  } else if (cmprs > 0) {
1248  if (H5Pset_deflate(plist2, cmprs) < 0) {
1249  printMessage("Set deflate failed");
1250  res = false;
1251  }
1252  } else if (cmprs) {
1253  if (H5Pset_filter(plist2, H5PY_FILTER_LZF, H5Z_FLAG_OPTIONAL, 0, NULL) < 0) {
1254  printMessage("Set LZF Compress failed");
1255  res = false;
1256  }
1257  }
1258  }
1259 
1260  gplist2 = H5Pcreate(H5P_LINK_CREATE);
1261 
1262  if (H5Pset_create_intermediate_group(gplist2, 1) < 0) {
1263  printMessage(tr("Failed to set create intermediate groups"));
1264  res = false;
1265  } else {
1266  dset2Id = H5Dcreate(fileId,
1267  qPrintable(get_DataSetName()+"/n"),
1268  H5T_NATIVE_FLOAT,
1269  dspc2Id,
1270  gplist2,
1271  plist2,
1272  H5P_DEFAULT);
1273  }
1274  }
1275  }
1276  }
1277 
1278  if (plist >= 0) H5Pclose(plist);
1279  if (gplist >= 0) H5Pclose(gplist);
1280  if (plist2 >= 0) H5Pclose(plist2);
1281  if (gplist2 >= 0) H5Pclose(gplist2);
1282 
1283  if (res == false) {
1284  if (dspcId >= 0) H5Sclose(dspcId);
1285  if (dsetId >= 0) H5Dclose(dsetId);
1286  if (dspc2Id >= 0) H5Sclose(dspc2Id);
1287  if (dset2Id >= 0) H5Dclose(dset2Id);
1288  if (fileId >= 0) H5Fclose(fileId);
1289  } else {
1290  if (fileId < 0 || dsetId < 0 || dspcId < 0) {
1291  printMessage(tr("Error in CctwChunkedData::openOutputFile fileId=%1, dsetId=%2, dspcId=%3")
1292  .arg(fileId).arg(dsetId).arg(dspcId));
1293  }
1294 
1295  if (!get_Normalization() && (dset2Id < 0 || dspc2Id < 0)) {
1296  printMessage(tr("Error in CctwChunkedData::openOutputFile fileId=%1, dset2Id=%2, dspc2Id=%3")
1297  .arg(fileId).arg(dset2Id).arg(dspc2Id));
1298  }
1299 
1300  m_FileId = fileId;
1301  m_DatasetId = dsetId;
1302  m_DataspaceId = dspcId;
1303  m_Dataset2Id = dset2Id;
1304  m_Dataspace2Id= dspc2Id;
1305 
1306  printMessage(tr("Opened output file \"%1\" OK, DS \"%2\", Norm %3, DSID %4, DS2ID %5, CMPRS %6")
1307  .arg(get_DataFileName()).arg(get_DataSetName())
1308  .arg(get_Normalization()).arg(m_DatasetId).arg(m_Dataset2Id)
1309  .arg(get_Compression()));
1310 
1311  CctwIntVector3D hdfChunkSize = get_HDFChunkSize();
1312 
1313  printMessage(tr("Output dimensions %1, HDF Chunk size %2, Output chunk size %3, Output chunk count %4")
1314  .arg(get_Dimensions().toString())
1315  .arg(hdfChunkSize.toString())
1316  .arg(get_ChunkSize().toString())
1317  .arg(get_ChunkCount().toString()));
1318  }
1319 
1320  return res;
1321 }
1322 
1324 {
1325  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1326 
1327  if (m_DataspaceId >= 0) {
1328  H5Sclose(m_DataspaceId);
1329  m_DataspaceId = -1;
1330  }
1331 
1332  if (m_DatasetId >= 0) {
1333  H5Dclose(m_DatasetId);
1334  m_DatasetId = -1;
1335  }
1336 
1337  if (m_Dataspace2Id >= 0) {
1338  H5Sclose(m_Dataspace2Id);
1339  m_Dataspace2Id = -1;
1340  }
1341 
1342  if (m_Dataset2Id >= 0) {
1343  H5Dclose(m_Dataset2Id);
1344  m_Dataset2Id = -1;
1345  }
1346 
1347  if (m_FileId >= 0) {
1348  H5Fclose(m_FileId);
1349  m_FileId = -1;
1350  }
1351 
1352  printMessage("Closed output file");
1353 }
1354 
1356 {
1357  printMessage("\rWriting output file...");
1358 
1359  m_Application->set_Progress(0);
1360  m_Application->set_ProgressLimit(m_DataChunks.count());
1361 
1362  for (int i=0; i<m_DataChunks.count(); i++) {
1363  writeChunk(i);
1364  m_Application->prop_Progress()->incValue(1);
1365  }
1366 }
1367 
1369 {
1370  /* Save old error handler */
1371  herr_t (*old_func)(hid_t, void*);
1372  void *old_client_data;
1373  H5Eget_auto(H5E_DEFAULT, &old_func, &old_client_data);
1374 
1375  /* Turn off error handling */
1376  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
1377 
1378  bool res = openMaskFile(true);
1379  closeMaskFile();
1380 
1381  /* Restore previous error handler */
1382  H5Eset_auto(H5E_DEFAULT, old_func, old_client_data);
1383 
1384  return res;
1385 }
1386 
1388 {
1389  bool sameFile = false;
1390 
1391  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1392 
1393  if (m_MaskFileId >= 0) {
1394  return true;
1395  }
1396 
1397  QString fileName = get_MaskDataFileName();
1398  QString dataSetName = get_MaskDataSetName();
1399  QString inputFile = get_DataFileName();
1400 
1401  if (dataSetName.length()==0) {
1402  printMessage(tr("No dataset name given for mask file, skipping"));
1403  return true;
1404  }
1405 
1406  if (!quietly) {
1407  printMessage(tr("About to open mask input file: %1 dataset: %2")
1408  .arg(fileName).arg(dataSetName));
1409  }
1410 
1411  QFileInfo f(fileName);
1412 
1413  hid_t fileId = -1;
1414  hid_t dsetId = -1;
1415  hid_t dspcId = -1;
1416 
1417  try
1418  {
1419  if (fileName.length() > 0) {
1420  if (!f.exists())
1421  throw tr("Mask file %1 does not exist").arg(fileName);
1422  if (H5Fis_hdf5(qPrintable(fileName)) <= 0)
1423  throw tr("Mask file %1 exists but is not an hdf file").arg(fileName);
1424  }
1425 
1426  if (fileName.length() == 0) { // No file name given?
1427  printMessage("No mask file name");
1428  fileId = m_FileId;
1429  sameFile = true;
1430  } else if (fileName == inputFile) { // In same file as input data?
1431  printMessage("Mask file same as input file");
1432  fileId = m_FileId;
1433  sameFile = true;
1434  } else {
1435  fileId = H5Fopen(qPrintable(fileName), H5F_ACC_RDWR, H5P_DEFAULT);
1436  }
1437 
1438  if (fileId < 0)
1439  throw tr("Mask file %1 could not be opened").arg(fileName);
1440 
1441  dsetId = H5Dopen(fileId, qPrintable(dataSetName), H5P_DEFAULT);
1442  if (dsetId <= 0)
1443  throw tr("Could not open mask dataset!");
1444 
1445  dspcId = H5Dget_space(dsetId);
1446  if (dspcId < 0)
1447  throw tr("Could not get dataspace of existing mask dataset");
1448 
1449  hsize_t dims[3];
1450  int ndims = H5Sget_simple_extent_ndims(dspcId);
1451  if (ndims != 2)
1452  throw tr("Mask dataspace is not 2-dimensional");
1453 
1454  int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
1455  if (ndims2 != 2)
1456  throw tr("Could not get mask dataspace dimensions");
1457 
1458  setMaskDimensions(dims[1], dims[0]);
1459 
1460  m_MaskSameFile = sameFile;
1461  m_MaskFileId = fileId;
1462  m_MaskDatasetId = dsetId;
1463  m_MaskDataspaceId = dspcId;
1464 
1465  if (!quietly) {
1466  printMessage(tr("Opened mask input file OK"));
1467  }
1468  }
1469  catch (QString &msg )
1470  {
1471  if (!quietly) {
1472  printMessage(tr("Error in CctwChunkedData::openMaskFile fileId=%1, dsetId=%2, dspcId=%3")
1473  .arg(fileId).arg(dsetId).arg(dspcId));
1474  printMessage(msg);
1475  }
1476 
1477  if (dspcId >= 0) H5Sclose(dspcId);
1478  if (dsetId >= 0) H5Dclose(dsetId);
1479  if ((fileId >= 0) && (sameFile == false)) H5Fclose(fileId);
1480  return false;
1481  }
1482  return true;
1483 }
1484 
1486 {
1487  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1488 
1489  if (m_MaskDataspaceId >= 0) {
1490  H5Sclose(m_MaskDataspaceId);
1491  m_MaskDataspaceId = -1;
1492  }
1493 
1494  if (m_MaskDatasetId >= 0) {
1495  H5Dclose(m_MaskDatasetId);
1496  m_MaskDatasetId = -1;
1497  }
1498 
1499  if ((m_MaskFileId >= 0) && (m_MaskSameFile == false)) {
1500  H5Fclose(m_MaskFileId);
1501  m_MaskFileId = -1;
1502  m_MaskSameFile = false;
1503  }
1504 
1505  if (qcepDebug(DEBUG_APP)) {
1506  printMessage("Closed mask input file");
1507  }
1508 }
1509 
1511 {
1512  bool res = true;
1513 
1514  if (openMaskFile()) {
1515 
1516  if (m_MaskFileId >= 0) {
1517  res = false;
1518 
1519  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1520 
1521  hid_t memspace_id = -1;
1522  hsize_t offset[2], count[2], stride[2], block[2];
1523 
1524  QcepIntVector a= get_Mask();
1525 
1526  offset[0] = 0;
1527  offset[1] = 0;
1528 
1529  count[0] = get_Dimensions().y();
1530  count[1] = get_Dimensions().x();
1531 
1532  stride[0] = 1;
1533  stride[1] = 1;
1534 
1535  block[0] = 1;
1536  block[1] = 1;
1537 
1538  int *msk = a.data();
1539 
1540  memspace_id = H5Screate_simple(2, count, NULL);
1541  herr_t selerr = H5Sselect_hyperslab(m_MaskDataspaceId, H5S_SELECT_SET, offset, stride, count, block);
1542 
1543  if (selerr < 0) {
1544  printMessage(tr("ERROR: select_hyperslab failed!"));
1545  }
1546 
1547  herr_t rderr = H5Dread(m_MaskDatasetId, H5T_NATIVE_INT, memspace_id, m_MaskDataspaceId, H5P_DEFAULT, msk);
1548 
1549  if (selerr || rderr) {
1550  printMessage(tr("Error reading mask, selerr = %1, rderr = %2").arg(selerr).arg(rderr));
1551  } else {
1552  res = true;
1553 
1554  if (msk) {
1555  int sum=0;
1556  for (int i=0; i<a.size(); i++) {
1557  if (msk[i]) {
1558  sum += 1;
1559  }
1560  }
1561 
1562  printMessage(tr("Mask has %1 of %2 bits set").arg(sum).arg(a.size()));
1563  }
1564 
1565  set_Mask(a);
1566  }
1567 
1568  H5Sclose(memspace_id);
1569  }
1570 
1571  closeMaskFile();
1572  } else {
1573  res = false;
1574  }
1575 
1576  if (qcepDebug(DEBUG_APP)) {
1577  printMessage(tr("CctwChunkedData::readMaskFile returns %1").arg(res));
1578  }
1579 
1580  return res;
1581 }
1582 
1584 {
1585  /* Save old error handler */
1586  herr_t (*old_func)(hid_t, void*);
1587  void *old_client_data;
1588  H5Eget_auto(H5E_DEFAULT, &old_func, &old_client_data);
1589 
1590  /* Turn off error handling */
1591  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
1592 
1593  bool res = openAnglesFile(true);
1594  closeAnglesFile();
1595 
1596  /* Restore previous error handler */
1597  H5Eset_auto(H5E_DEFAULT, old_func, old_client_data);
1598 
1599  return res;
1600 }
1601 
1603 {
1604  bool sameFile = false;
1605 
1606  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1607 
1608  if (m_AnglesFileId >= 0) {
1609  return true;
1610  }
1611 
1612  QString fileName = get_AnglesDataFileName();
1613  QString dataSetName = get_AnglesDataSetName();
1614  QString inputFile = get_DataFileName();
1615 
1616  if (dataSetName.length()==0) {
1617  printMessage(tr("No dataset name given for angles file, skipping"));
1618  return true;
1619  }
1620 
1621  if (!quietly) {
1622  printMessage(tr("About to open angles input file: %1 dataset: %2")
1623  .arg(fileName).arg(dataSetName));
1624  }
1625 
1626  QFileInfo f(fileName);
1627 
1628  hid_t fileId = -1;
1629  hid_t dsetId = -1;
1630  hid_t dspcId = -1;
1631 
1632  try
1633  {
1634  if (fileName.length() > 0) {
1635  if (!f.exists())
1636  throw tr("Angles file %1 does not exist").arg(fileName);
1637  if (H5Fis_hdf5(qPrintable(fileName)) <= 0)
1638  throw tr("Angles file %1 exists but is not an hdf file").arg(fileName);
1639  }
1640 
1641  if (fileName.length() == 0) { // No file name given?
1642  fileId = m_FileId;
1643  sameFile = true;
1644  } else if (fileName == inputFile) { // In same file as input data?
1645  fileId = m_FileId;
1646  sameFile = true;
1647  } else {
1648  fileId = H5Fopen(qPrintable(fileName), H5F_ACC_RDWR, H5P_DEFAULT);
1649  }
1650 
1651  if (fileId < 0)
1652  throw tr("Angles file %1 could not be opened").arg(fileName);
1653 
1654  dsetId = H5Dopen(fileId, qPrintable(dataSetName), H5P_DEFAULT);
1655  if (dsetId <= 0)
1656  throw tr("Could not open angles dataset!");
1657 
1658  dspcId = H5Dget_space(dsetId);
1659  if (dspcId < 0)
1660  throw tr("Could not get dataspace of existing angles dataset");
1661 
1662  hsize_t dims[3];
1663  int ndims = H5Sget_simple_extent_ndims(dspcId);
1664  if (ndims != 1)
1665  throw tr("Angles Dataspace is not 1-dimensional");
1666 
1667  int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
1668  if (ndims2 != 1)
1669  throw tr("Could not get angles dataspace dimensions");
1670 
1671  setAnglesDimensions(dims[0]);
1672 
1673  m_AnglesSameFile = sameFile;
1674  m_AnglesFileId = fileId;
1675  m_AnglesDatasetId = dsetId;
1676  m_AnglesDataspaceId = dspcId;
1677 
1678  if (!quietly) {
1679  printMessage(tr("Opened Angles input file OK"));
1680  }
1681  }
1682  catch (QString &msg )
1683  {
1684  if (!quietly) {
1685  printMessage(tr("Error in CctwChunkedData::openAnglesFile fileId=%1, dsetId=%2, dspcId=%3")
1686  .arg(fileId).arg(dsetId).arg(dspcId));
1687  printMessage(msg);
1688  }
1689 
1690  if (dspcId >= 0) H5Sclose(dspcId);
1691  if (dsetId >= 0) H5Dclose(dsetId);
1692  if ((fileId >= 0) && (sameFile == false)) H5Fclose(fileId);
1693  return false;
1694  }
1695  return true;
1696 }
1697 
1699 {
1700  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1701 
1702  if (m_AnglesDataspaceId >= 0) {
1703  H5Sclose(m_AnglesDataspaceId);
1704  m_AnglesDataspaceId = -1;
1705  }
1706 
1707  if (m_AnglesDatasetId >= 0) {
1708  H5Dclose(m_AnglesDatasetId);
1709  m_AnglesDatasetId = -1;
1710  }
1711 
1712  if ((m_AnglesFileId >= 0) && (m_AnglesSameFile == false)) {
1713  H5Fclose(m_AnglesFileId);
1714  m_AnglesFileId = -1;
1715  m_AnglesSameFile = false;
1716  }
1717 
1718  if (qcepDebug(DEBUG_APP)) {
1719  printMessage("Closed Angles file");
1720  }
1721 }
1722 
1724 {
1725  bool res = true;
1726 
1727  if (openAnglesFile()) {
1728 
1729  if (m_AnglesFileId >= 0) {
1730  res = false;
1731 
1732  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1733 
1734  hid_t memspace_id = -1;
1735  hsize_t offset[1], count[1], stride[1], block[1];
1736 
1737  QcepDoubleVector a= get_Angles();
1738 
1739  offset[0] = 0;
1740  count[0] = a.size();
1741  stride[0] = 1;
1742  block[0] = 1;
1743 
1744  double *angles = a.data();
1745 
1746  memspace_id = H5Screate_simple(1, count, NULL);
1747  herr_t selerr = H5Sselect_hyperslab(m_AnglesDataspaceId, H5S_SELECT_SET, offset, stride, count, block);
1748 
1749  if (selerr < 0) {
1750  printMessage(tr("ERROR: select_hyperslab failed!"));
1751  }
1752 
1753  herr_t rderr = H5Dread(m_AnglesDatasetId, H5T_NATIVE_DOUBLE, memspace_id, m_AnglesDataspaceId, H5P_DEFAULT, angles);
1754 
1755  if (selerr || rderr) {
1756  printMessage(tr("Error reading angles, selerr = %1, rderr = %2").arg(selerr).arg(rderr));
1757  } else {
1758  res = true;
1759 
1760  set_Angles(a);
1761  }
1762 
1763  H5Sclose(memspace_id);
1764  }
1765 
1766  closeAnglesFile();
1767  } else {
1768  res = false;
1769  }
1770 
1771  if (qcepDebug(DEBUG_APP)) {
1772  printMessage(tr("CctwChunkedData::readAnglesFile returns %1").arg(res));
1773  }
1774 
1775  return res;
1776 }
1777 
1779 {
1780  /* Save old error handler */
1781  herr_t (*old_func)(hid_t, void*);
1782  void *old_client_data;
1783  H5Eget_auto(H5E_DEFAULT, &old_func, &old_client_data);
1784 
1785  /* Turn off error handling */
1786  H5Eset_auto(H5E_DEFAULT, NULL, NULL);
1787 
1788  bool res = openWeightsFile(true);
1789  closeWeightsFile();
1790 
1791  /* Restore previous error handler */
1792  H5Eset_auto(H5E_DEFAULT, old_func, old_client_data);
1793 
1794  return res;
1795 }
1796 
1798 {
1799  bool sameFile = false;
1800 
1801  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1802 
1803  if (m_WeightsFileId >= 0) {
1804  return true;
1805  }
1806 
1807  QString fileName = get_WeightsDataFileName();
1808  QString dataSetName = get_WeightsDataSetName();
1809  QString inputFile = get_DataFileName();
1810 
1811  if (dataSetName.length()==0) {
1812  printMessage(tr("No dataset name given for weights file, skipping"));
1813  return true;
1814  }
1815 
1816  if (!quietly) {
1817  printMessage(tr("About to open Weights input file: %1 dataset: %2")
1818  .arg(fileName).arg(dataSetName));
1819  }
1820 
1821  QFileInfo f(fileName);
1822 
1823  hid_t fileId = -1;
1824  hid_t dsetId = -1;
1825  hid_t dspcId = -1;
1826 
1827  try
1828  {
1829  if (fileName.length() > 0) {
1830  if (!f.exists())
1831  throw tr("Weights File %1 does not exist").arg(fileName);
1832  if (H5Fis_hdf5(qPrintable(fileName)) <= 0)
1833  throw tr("Weights File %1 exists but is not an hdf file").arg(fileName);
1834  }
1835 
1836  if (fileName.length() == 0) { // No file name given?
1837  fileId = m_FileId;
1838  sameFile = true;
1839  } else if (fileName == inputFile) { // In same file as input data?
1840  fileId = m_FileId;
1841  sameFile = true;
1842  } else {
1843  fileId = H5Fopen(qPrintable(fileName), H5F_ACC_RDWR, H5P_DEFAULT);
1844  }
1845 
1846  if (fileId < 0)
1847  throw tr("Weights File %1 could not be opened").arg(fileName);
1848 
1849  dsetId = H5Dopen(fileId, qPrintable(dataSetName), H5P_DEFAULT);
1850  if (dsetId <= 0)
1851  throw tr("Could not open weights dataset!");
1852 
1853  dspcId = H5Dget_space(dsetId);
1854  if (dspcId < 0)
1855  throw tr("Could not get dataspace of existing weights dataset");
1856 
1857  hsize_t dims[3];
1858  int ndims = H5Sget_simple_extent_ndims(dspcId);
1859  if (ndims != 1)
1860  throw tr("Weights Dataspace is not 1-dimensional");
1861 
1862  int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
1863  if (ndims2 != 1)
1864  throw tr("Could not get weights dataspace dimensions");
1865 
1866  setWeightsDimensions(dims[0]);
1867 
1868  m_WeightsSameFile = sameFile;
1869  m_WeightsFileId = fileId;
1870  m_WeightsDatasetId = dsetId;
1871  m_WeightsDataspaceId = dspcId;
1872 
1873  if (!quietly) {
1874  printMessage(tr("Opened Weights input file OK"));
1875  }
1876  }
1877  catch (QString &msg )
1878  {
1879  if (!quietly) {
1880  printMessage(tr("Error in CctwChunkedData::openWeightsFile fileId=%1, dsetId=%2, dspcId=%3")
1881  .arg(fileId).arg(dsetId).arg(dspcId));
1882  printMessage(msg);
1883  }
1884 
1885  if (dspcId >= 0) H5Sclose(dspcId);
1886  if (dsetId >= 0) H5Dclose(dsetId);
1887  if ((fileId >= 0) && (sameFile == false)) H5Fclose(fileId);
1888  return false;
1889  }
1890  return true;
1891 }
1892 
1894 {
1895  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1896 
1897  if (m_WeightsDataspaceId >= 0) {
1898  H5Sclose(m_WeightsDataspaceId);
1899  m_WeightsDataspaceId = -1;
1900  }
1901 
1902  if (m_WeightsDatasetId >= 0) {
1903  H5Dclose(m_WeightsDatasetId);
1904  m_WeightsDatasetId = -1;
1905  }
1906 
1907  if ((m_WeightsFileId >= 0) && (m_WeightsSameFile == false)) {
1908  H5Fclose(m_WeightsFileId);
1909  m_WeightsFileId = -1;
1910  m_WeightsSameFile = false;
1911  }
1912 
1913  if (qcepDebug(DEBUG_APP)) {
1914  printMessage("Closed Weights file");
1915  }
1916 }
1917 
1919 {
1920  bool res = true;
1921 
1922  if (openWeightsFile()) {
1923 
1924  if (m_WeightsFileId >= 0) {
1925  res = false;
1926 
1927  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
1928 
1929  hid_t memspace_id = -1;
1930  hsize_t offset[1], count[1], stride[1], block[1];
1931 
1932  QcepDoubleVector a= get_Weights();
1933 
1934  offset[0] = 0;
1935  count[0] = a.size();
1936  stride[0] = 1;
1937  block[0] = 1;
1938 
1939  double *Weights = a.data();
1940 
1941  memspace_id = H5Screate_simple(1, count, NULL);
1942  herr_t selerr = H5Sselect_hyperslab(m_WeightsDataspaceId, H5S_SELECT_SET, offset, stride, count, block);
1943 
1944  if (selerr < 0) {
1945  printMessage(tr("ERROR: select_hyperslab failed!"));
1946  }
1947 
1948  herr_t rderr = H5Dread(m_WeightsDatasetId, H5T_NATIVE_DOUBLE, memspace_id, m_WeightsDataspaceId, H5P_DEFAULT, Weights);
1949 
1950  if (selerr || rderr) {
1951  printMessage(tr("Error reading Weights, selerr = %1, rderr = %2").arg(selerr).arg(rderr));
1952  } else {
1953  res = true;
1954 
1955  set_Weights(a);
1956  }
1957 
1958  H5Sclose(memspace_id);
1959  }
1960 
1961  closeWeightsFile();
1962  } else {
1963  res = false;
1964  }
1965 
1966  if (qcepDebug(DEBUG_APP)) {
1967  printMessage(tr("CctwChunkedData::readWeightsFile returns %1").arg(res));
1968  }
1969 
1970  return res;
1971 }
1972 
1973 //CctwDataChunk *CctwChunkedData::readChunk(int n)
1974 //{
1975 // CctwDataChunk *chk = chunk(n);
1976 
1977 // if (chk) {
1978 // chk->allocateData();
1979 // chk->allocateWeights();
1980 
1981 // CctwIntVector3D size = chunkSize();
1982 // CctwIntVector3D start = chunkStart(n);
1983 
1984 // for (int k=0; k<size.z(); k++) {
1985 // for (int j=0; j<size.y(); j++) {
1986 // for (int i=0; i<size.x(); i++) {
1987 // CctwIntVector3D coords(i,j,k);
1988 
1989 // int val = ((start.x()+i)/16 & 1) ^ ((start.y()+j)/16 & 1) ^ ((start.z()+k)/16 & 1);
1990 
1991 // chk -> setData(i, j, k, val+0.75);
1992 // chk -> setWeight(i, j, k, 1.0);
1993 // }
1994 // }
1995 // }
1996 // }
1997 
1998 // printMessage(tr("CctwChunkedData::readChunk(%1)").arg(n));
1999 
2000 // return chk;
2001 //}
2002 
2004 {
2005  CctwDataChunk *chk = NULL;
2006  if (openInputFile()) {
2007 
2008  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
2009 
2010  chk = chunk(n);
2011 
2012  if (chk) {
2013  prop_ChunksRead()->incValue(1);
2014 
2015  chk->allocateData();
2016  chk->allocateWeights();
2017 
2018 // printMessage(tr("About to read chunk %1").arg(n));
2019 
2020  if (m_FileId >= 0) {
2021  hid_t memspace_id = -1;
2022  hsize_t offset[3], count[3], stride[3], block[3];
2023  CctwIntVector3D st = chk -> chunkStart();
2024  CctwIntVector3D sz = chk -> chunkSize();
2025 
2026 // printMessage(tr("Reading chunk %1 [%2..%3, %4..%5, %6..%7]")
2027 // .arg(n)
2028 // .arg(st.x())
2029 // .arg(st.x()+sz.x()-1)
2030 // .arg(st.y())
2031 // .arg(st.y()+sz.y()-1)
2032 // .arg(st.z())
2033 // .arg(st.z()+sz.z()-1)
2034 // );
2035 
2036  count[0] = sz.z();
2037  count[1] = sz.y();
2038  count[2] = sz.x();
2039 
2040  stride[0] = 1;
2041  stride[1] = 1;
2042  stride[2] = 1;
2043 
2044  block[0] = 1;
2045  block[1] = 1;
2046  block[2] = 1;
2047 
2048  offset[0] = st.z();
2049  offset[1] = st.y();
2050  offset[2] = st.x();
2051 
2052  CctwChunkedData::MergeDataType *chunkData = chk->dataPointer();
2053  CctwChunkedData::MergeDataType *weightData = chk->weightsPointer();
2054 
2055  if (chunkData == NULL) {
2056  printMessage(tr("Anomaly reading chunk %1, data == NULL").arg(n));
2057  } else if ((m_TransformOptions & 4) == 0){
2058  memspace_id = H5Screate_simple(3, count, NULL);
2059  herr_t selerr = H5Sselect_hyperslab(m_DataspaceId, H5S_SELECT_SET, offset, stride, count, block);
2060  if (selerr < 0) {
2061  printMessage(tr("ERROR: select_hyperslab failed!"));
2062  }
2063  herr_t rderr = H5Dread(m_DatasetId, CCTW_H5T_INTERNAL_TYPE, memspace_id, m_DataspaceId, H5P_DEFAULT, chunkData);
2064 
2065  if (selerr || rderr) {
2066  printMessage(tr("Error reading chunk %1, selerr = %2, rderr = %3")
2067  .arg(n)
2068  .arg(selerr)
2069  .arg(rderr));
2070  }
2071 
2072  if ((m_Dataspace2Id >= 0) && (m_Dataset2Id >= 0)) {
2073  herr_t selerr = H5Sselect_hyperslab(m_Dataspace2Id, H5S_SELECT_SET, offset, stride, count, block);
2074  if (selerr < 0) {
2075  printMessage(tr("ERROR: select_hyperslab failed!"));
2076  }
2077 
2078  herr_t rderr = H5Dread(m_Dataset2Id, CCTW_H5T_INTERNAL_TYPE, memspace_id, m_Dataspace2Id, H5P_DEFAULT, weightData);
2079 
2080  if (selerr || rderr) {
2081  printMessage(tr("Error reading weight chunk %1, selerr = %2, rderr = %3")
2082  .arg(n)
2083  .arg(selerr)
2084  .arg(rderr));
2085  }
2086  }
2087 
2088  H5Sclose(memspace_id);
2089  }
2090 
2091  if (m_Dataspace2Id < 0) {
2092  if (weightData && chunkData) {
2093  int s = sz.volume();
2094 
2095  for (int i=0; i<s; i++) {
2096  if (chunkData[i] == chunkData[i]) { // Test for NaN
2097  weightData[i] = 1;
2098  } else {
2099  weightData[i] = 0;
2100  }
2101  }
2102  }
2103  }
2104  }
2105  }
2106  }
2107 
2108 // printMessage(tr("CctwChunkedData::readChunk(%1)").arg(n));
2109 
2110  return chk;
2111 }
2112 
2114 {
2115  if (openOutputFile()) {
2116  QcepMutexLocker lock(__FILE__, __LINE__, &m_FileAccessMutex);
2117 
2118 // printMessage(tr("Writing chunk %1").arg(n));
2119 
2120  CctwDataChunk *chk = chunk(n);
2121 
2122  if (chk) {
2123 // prop_ChunksWritten()->incValue(1);
2124 
2125  if (get_Normalization()) {
2126  normalizeChunk(n);
2127  }
2128 
2129  if (m_FileId >= 0) {
2130 
2131  hsize_t offset[3], count[3], stride[3], block[3];
2132 
2133  CctwIntVector3D st = chk -> chunkStart();
2134  CctwIntVector3D sz = chk -> chunkSize();
2135 
2136 // printMessage(tr("Writing chunk %1 [%2..%3, %4..%5, %6..%7]")
2137 // .arg(n)
2138 // .arg(st.x())
2139 // .arg(st.x()+sz.x()-1)
2140 // .arg(st.y())
2141 // .arg(st.y()+sz.y()-1)
2142 // .arg(st.z())
2143 // .arg(st.z()+sz.z()-1)
2144 // );
2145 
2146  count[0] = sz.z();
2147  count[1] = sz.y();
2148  count[2] = sz.x();
2149 
2150  stride[0] = 1;
2151  stride[1] = 1;
2152  stride[2] = 1;
2153 
2154  block[0] = 1;
2155  block[1] = 1;
2156  block[2] = 1;
2157 
2158  offset[0] = st.z();
2159  offset[1] = st.y();
2160  offset[2] = st.x();
2161 
2162  CctwChunkedData::MergeDataType *chunkData = chk->dataPointer();
2163  CctwChunkedData::MergeDataType *weightData = chk->weightsPointer();
2164 
2165  if (chunkData == NULL) {
2166 // printMessage(tr("Anomaly writing chunk %1, data == NULL").arg(n));
2167  } else if ((m_TransformOptions & 8) == 0){
2168  hid_t memspace_id = -1;
2169  memspace_id = H5Screate_simple(3, count, NULL);
2170  herr_t selerr = H5Sselect_hyperslab(m_DataspaceId, H5S_SELECT_SET, offset, stride, count, block);
2171  herr_t wrterr = H5Dwrite(m_DatasetId, CCTW_H5T_INTERNAL_TYPE, memspace_id, m_DataspaceId, H5P_DEFAULT, chunkData);
2172 
2173  if (selerr || wrterr) {
2174  printMessage(tr("Error writing chunk %1, selerr = %2, wrterr = %3")
2175  .arg(n)
2176  .arg(selerr)
2177  .arg(wrterr));
2178  }
2179 
2180  if ((m_Dataspace2Id >= 0) && (m_Dataset2Id >= 0)) {
2181  // Write out the 'weight' chunk
2182  herr_t selerr = H5Sselect_hyperslab(m_Dataspace2Id, H5S_SELECT_SET, offset, stride, count, block);
2183  herr_t wrterr = H5Dwrite(m_Dataset2Id, CCTW_H5T_INTERNAL_TYPE, memspace_id, m_Dataspace2Id, H5P_DEFAULT, weightData);
2184 
2185  if (selerr || wrterr) {
2186  printMessage(tr("Error writing chunk %1, selerr = %2, wrterr = %3")
2187  .arg(n)
2188  .arg(selerr)
2189  .arg(wrterr));
2190  }
2191  }
2192  H5Sclose(memspace_id);
2193  }
2194  }
2195  }
2196 
2197  releaseChunkData(n);
2198  }
2199 }
2200 
2201 bool CctwChunkedData::beginTransform(bool isInput, int transformOptions)
2202 {
2203  bool res = false;
2204 
2205  m_IsInput = isInput;
2206  m_TransformOptions = transformOptions;
2207 
2208 // printMessage("CctwChunkedData::beginTransform()");
2209 
2210  if (m_IsInput) {
2211  if (openInputFile()) {
2212 
2213 // printMessage("Reading mask data");
2214  if (readMaskFile()) {
2215 
2216 // printMessage("Reading angles data");
2217  if (readAnglesFile()) {
2218 
2219 // printMessage("Reading weights data");
2220  if (readWeightsFile()) {
2221  res = true;
2222  }
2223  }
2224  }
2225  }
2226  } else {
2227  res = openOutputFile();
2228  }
2229 
2230  return res;
2231 }
2232 
2234 {
2235 // printMessage("CctwChunkedData::endTransform()");
2236 
2237  if (m_IsInput) {
2238  closeInputFile();
2239  } else {
2240  closeOutputFile();
2241  }
2242 }
2243 
2245 {
2246  prop_ChunksRead()->incValue(n);
2247 }
2248 
2250 {
2251  prop_ChunksWritten()->incValue(n);
2252 }
2253 
2255 {
2256  prop_ChunksHeld()->incValue(n);
2257 
2258  if (get_ChunksHeld() > get_ChunksHeldMax()) {
2259  set_ChunksHeldMax(get_ChunksHeld());
2260  }
2261 }
void incChunksHeld(int n)
bool beginTransform(bool isInput, int transformOptions)
int index() const
virtual void setMaskDataset(QString desc)
virtual void setWeightsSource(QString desc)
double angle(int n)
CctwIntVector3D chunkIndexFromNumber(int n)
static QString addSlashes(QString str)
virtual void setAnglesSource(QString desc)
CctwIntVector3D chunkStart(int n)
void releaseChunkData(int n)
void writeChunk(int n)
virtual void setWeightsDataset(QString desc)
void clearDependencies()
CctwDataChunk * chunk(int n)
bool containsChunk(int ix, int iy, int iz)
QString toString()
void releaseChunk(int n)
void setMaskDimensions(int mx, int my)
void addDependency(int f, int t)
CctwIntVector3D m_DimensionsCache
void setDimensions(CctwIntVector3D dim)
virtual void setChunks(QString desc)
bool openInputFile(bool quietly=false)
CctwIntVector3D chunkSize() const
virtual void setDataset(QString desc)
void setWeightsDimensions(int n)
bool openMaskFile(bool quietly=false)
virtual void printMessage(QString msg, QDateTime dt=QDateTime::currentDateTime())
Definition: cctwobject.cpp:25
void mergeChunk(CctwDataChunk *chunk)
void setAnglesDimensions(int n)
#define CCTW_H5T_INTERNAL_TYPE
virtual void setDataSource(QString desc)
CctwIntVector3D m_ChunkCountCache
virtual void setDims(QString desc)
void normalizeChunk(int n)
T x() const
Definition: cctwvector3d.h:17
CctwIntVector3D dimensions() const
static int allocatedChunkCount()
int chunkContaining(CctwIntVector3D pixelCoord)
CctwChunkedData(CctwApplication *application, CctwIntVector3D dim, CctwIntVector3D chunkSize, bool isInput, QString name, QObject *parent)
void setChunkSize(CctwIntVector3D cksz)
CctwChunkedData::MergeDataType * weightsPointer()
CctwIntVector3D chunkCount() const
void incChunksRead(int n)
bool openWeightsFile(bool quietly=false)
QVector< CctwDataChunk * > m_DataChunks
bool containsPixel(CctwIntVector3D pixelCoord)
T z() const
Definition: cctwvector3d.h:19
CctwVector3D< int > CctwIntVector3D
Definition: cctwvector3d.h:70
QcepDoubleVector angles
bool openAnglesFile(bool quietly=false)
CctwIntVector3D hdfChunkSize
CctwChunkedData::MergeDataType * dataPointer()
CctwIntVector3D m_ChunkSizeCache
void incChunksWritten(int n)
double weight(int n)
void setWeight(int n, double v)
static QMutex m_FileAccessMutex
QcepSettingsSaverWPtr saver() const
void setAngle(int n, double v)
T volume() const
CctwApplication * m_Application
virtual void setMaskSource(QString desc)
int chunkNumberFromIndex(CctwIntVector3D chunkIdx)
virtual void setAnglesDataset(QString desc)
T y() const
Definition: cctwvector3d.h:18
CctwVector3D< double > CctwDoubleVector3D
Definition: cctwvector3d.h:71
CctwDataChunk * readChunk(int n)