4 #include <QtConcurrentRun>
5 #include "qcepmutexlocker.h"
10 m_Application(application),
11 m_BacklogSemaphore(8),
12 m_CompletionSemaphore(0),
17 m_InputBufferCount(0),
18 m_DarkImage(new
QcepImageData<double>(QcepSettingsSaverWPtr(),0,0)),
19 m_DataFormat(m_Application->saver(), this,
"dataFormat", 0,
"Imported data format (0=auto)"),
20 m_DarkImagePath(m_Application->saver(), this,
"darkImagePath",
"",
"Dark image path"),
21 m_ImagePaths(m_Application->saver(), this,
"imagePaths", QcepStringList(),
"Imported image paths"),
22 m_ImageDirectory(m_Application->saver(), this,
"imageDirectory",
"",
"Image directory"),
23 m_ImagePattern(m_Application->saver(), this,
"imagePattern",
"",
"Image pattern"),
24 m_OutputPath(m_Application->saver(), this,
"outputPath",
"./output.h5",
"Destination for imported data"),
25 m_OutputDataset(m_Application->saver(), this,
"outputDataset",
"/data",
"Destination Dataset for imported data"),
26 m_ChunkSize(m_Application->saver(), this,
"chunkSize",
CctwIntVector3D(32,32,32),
"output chunk size"),
27 m_Compression(m_Application->saver(), this,
"compression", 0,
"output compression type"),
28 m_XDimension(QcepSettingsSaverWPtr(), this,
"xDimension", 0,
"X Dimension of output"),
29 m_YDimension(QcepSettingsSaverWPtr(), this,
"yDimension", 0,
"Y Dimension of output"),
30 m_ZDimension(QcepSettingsSaverWPtr(), this,
"zDimension", 0,
"Z Dimension of output"),
31 m_InputDataBuffering(m_Application->saver(), this,
"inputDataBuffering", 0,
"Input Data Buffering"),
32 m_CheckRigorously(m_Application->saver(), this,
"checkRigorously", false,
"Perform rigorous check (every element) of imported data"),
33 m_CheckApproximately(m_Application->saver(), this,
"checkApproximately", true,
"Perform approximate check (about 30 seconds) of imported data")
44 prop_ImagePaths()->clear();
49 set_ImageDirectory(path);
54 prop_ImagePaths()->appendValue(path);
59 set_ImagePattern(pattern);
61 QDir d(get_ImageDirectory());
62 d.setFilter(QDir::Files);
64 QStringList patts(pattern);
65 d.setNameFilters(patts);
67 QStringList newFiles = d.entryList();
69 QStringList l = get_ImagePaths();
78 QString dkPath = get_DarkImagePath();
80 if (dkPath.length() > 0) {
82 printMessage(tr(
"Reading dark image from %1").arg(dkPath));
85 printMessage(tr(
"Failed to read dark image from %1").arg(dkPath));
97 QStringList paths = get_ImagePaths();
98 int n = paths.count();
99 QVector< QFuture<void> > results(n);
100 QDir inp(get_ImageDirectory());
115 if (get_InputDataBuffering() <= 1) {
116 for (
int i=0; i<n; i++) {
124 i, inp.filePath(paths[i]));
127 int nb = get_InputDataBuffering();
131 for (
int i=0; i<n; i++) {
150 printMessage(tr(
"Import Cancelled after %1 sec").arg(startAt.elapsed()/1000.0));
152 printMessage(tr(
"Import Completed after %1 sec").arg(startAt.elapsed()/1000.0));
168 QString fileName = get_OutputPath();
169 QFileInfo f(fileName);
179 fileId = H5Fcreate(qPrintable(fileName), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
182 printMessage(tr(
"File %1 could not be created").arg(fileName));
185 }
else if (H5Fis_hdf5(qPrintable(get_OutputPath())) <= 0) {
186 printMessage(tr(
"File %1 exists but is not an hdf file").arg(fileName));
189 fileId = H5Fopen(qPrintable(fileName), H5F_ACC_RDWR, H5P_DEFAULT);
192 printMessage(tr(
"File %1 could not be opened").arg(fileName));
200 dsetId = H5Dopen(fileId, qPrintable(get_OutputDataset()), H5P_DEFAULT);
203 dspcId = H5Dget_space(dsetId);
206 printMessage(
"Couldn't get dataspace of existing dataset");
210 int ndims = H5Sget_simple_extent_ndims(dspcId);
216 int ndims2 = H5Sget_simple_extent_dims(dspcId, dims, NULL);
222 if (dims[0] != (hsize_t) get_ZDimension() ||
223 dims[1] != (hsize_t) get_YDimension() ||
224 dims[2] != (hsize_t) get_XDimension()) {
225 printMessage(
"Dataspace dimensions do not match imported data");
233 dims[0] = get_ZDimension();
234 dims[1] = get_YDimension();
235 dims[2] = get_XDimension();
237 dspcId = H5Screate_simple(3, dims, NULL);
240 int cmprs = get_Compression();
242 plist = H5Pcreate(H5P_DATASET_CREATE);
251 if (H5Pset_chunk(plist, 3, c) < 0) {
257 if (H5Pset_deflate(plist, cmprs) < 0) {
264 gplist = H5Pcreate(H5P_LINK_CREATE);
266 if (H5Pset_create_intermediate_group(gplist, 1) < 0) {
267 printMessage(tr(
"Failed to set create intermediate groups"));
270 dsetId = H5Dcreate(fileId,
271 qPrintable(get_OutputDataset()),
286 if (plist >= 0) H5Pclose(plist);
287 if (gplist >= 0) H5Pclose(gplist);
290 if (dspcId >= 0) H5Sclose(dspcId);
291 if (dsetId >= 0) H5Dclose(dsetId);
292 if (fileId >= 0) H5Fclose(fileId);
294 if (fileId < 0 || dsetId < 0 || dspcId < 0) {
295 printMessage(tr(
"Anomaly in CctwImporter::createOutputFile fileId=%1, dsetId=%2, dspcId=%3")
296 .arg(fileId).arg(dsetId).arg(dspcId));
338 hsize_t offset[3], count[3], stride[3], block[3];
341 count[1] = get_YDimension();
342 count[2] = get_XDimension();
356 memspace_id = H5Screate_simple(3, count, NULL);
358 herr_t selerr = H5Sselect_hyperslab(
m_DataspaceId, H5S_SELECT_SET, offset, stride, count, block);
361 if (selerr || wrterr) {
362 printMessage(tr(
"Error writing frame %1, selerr = %2, wrterr = %3").arg(num).arg(selerr).arg(wrterr));
372 if (path.length() > 0) {
376 if (m.readImage(path)) {
379 printMessage(tr(
"Imported frame %1 from %2").arg(num).arg(path));
383 set_XDimension(m.get_Width());
384 set_YDimension(m.get_Height());
434 if (path.length() > 0) {
438 if (m.readImage(path)) {
441 printMessage(tr(
"Imported frame %1 from %2").arg(i).arg(path));
446 set_XDimension(m.get_Width());
447 set_YDimension(m.get_Height());
456 hsize_t ibuf = i % nb;
462 double *frame = m.data();
492 hsize_t offset[3], count[3], stride[3], block[3];
495 count[1] = get_YDimension();
496 count[2] = get_XDimension();
510 memspace_id = H5Screate_simple(3, count, NULL);
512 herr_t selerr = H5Sselect_hyperslab(
m_DataspaceId, H5S_SELECT_SET, offset, stride, count, block);
515 if (selerr || wrterr) {
516 printMessage(tr(
"Error writing frames %1 to %2, selerr = %3, wrterr = %4")
534 if (get_CheckRigorously()) {
538 if (get_CheckApproximately()) {
545 CctwInputDataH5 data(get_OutputPath(), get_OutputDataset(),
"h5import",
this);
557 CctwInputDataH5 data(get_OutputPath(), get_OutputDataset(),
"h5import",
this);
559 printMessage(
"Checking imported data approximately...");
564 QStringList paths = get_ImagePaths();
565 QDir inp(get_ImageDirectory());
568 for (
int nz = 0; nz<5; nz++) {
571 if (m.readImage(inp.filePath(paths[nz]))) {
575 for (
int nx = 0; nx<5; nx++) {
576 for (
int ny = 0; ny<5; ny++) {
577 double fromTiff = m.getImageData(nx, ny);
578 double fromHDF = data.
readData(nx, ny, nz);
581 .arg(nx).arg(ny).arg(nz).arg(fromTiff).arg(fromHDF));
587 while (startAt.elapsed() < 60*1000 && !
m_Application->get_Halting() && n < 100) {
592 if (m.readImage(inp.filePath(paths[nz]))) {
597 for (
int i=0; i<100 && !
m_Application->get_Halting() && n < 100; i++) {
601 double fromTiff = m.getImageData(nx, ny);
602 double fromHDF = data.
readData(nx, ny, nz);
605 if (fromTiff != fromHDF) {
607 printMessage(tr(
"Mismatch at [%1,%2,%3]: %4 <=> %5, %6")
608 .arg(nx).arg(ny).arg(nz)
609 .arg(fromTiff).arg(fromHDF).arg(fromHDFt));
614 .arg(nz).arg(paths[nz]));
void readDataFrameToBuffer(int i, int nb, QString path)
QSemaphore m_CompletionSemaphore
QSharedPointer< QcepImageData< double > > m_DarkImage
hsize_t m_InputBufferSize
QSemaphore m_BacklogSemaphore
void writeOutputFrame(int num, QcepImageData< double > *img)
virtual void printMessage(QString msg, QDateTime dt=QDateTime::currentDateTime())
void checkImportedDataRigorously()
CctwApplication * m_Application
void addWorkOutstanding(int amt)
void checkImportedDataApproximately()
hsize_t m_InputBufferStride
void initializeDataBuffer()
void outputDataFromBuffer(int i)
void workCompleted(int amt)
virtual void printLine(QString line)
void importDataFrame(int num, QString path)
void changeDirectory(QString path)
static int randomIndex(int n)
void appendInputFile(QString path)
CctwImporter(CctwApplication *application, QString name, QObject *parent=0)
void allocateDataBuffer(hsize_t dimx, hsize_t dimy, hsize_t dimz)
void appendMatchingFiles(QString pattern)