cctw  0.2.1
cctwinputdatah5.cpp
Go to the documentation of this file.
1 #include "cctwinputdatah5.h"
2 #include <QFileInfo>
3 
4 CctwInputDataH5::CctwInputDataH5(QString filePath, QString datasetName, QString name, CctwObject *parent) :
5  CctwObject(name, parent),
6  m_InputFilePath(filePath),
7  m_DatasetName(datasetName),
8  m_FileId(-1),
9  m_DatasetId(-1),
10  m_DataspaceId(-1)
11 {
12  openInputFile();
13 }
14 
16 {
17  QFileInfo f(m_InputFilePath);
18 
19  if (!f.exists()) {
20  printMessage(tr("File %1 does not exist").arg(m_InputFilePath));
21  return false;
22  }
23 
24  if (!H5Fis_hdf5(qPrintable(m_InputFilePath))) {
25  printMessage(tr("File %1 exists but is not an hdf file").arg(m_InputFilePath));
26  return false;
27  }
28 
29  m_FileId = H5Fopen(qPrintable(m_InputFilePath), H5F_ACC_RDONLY, H5P_DEFAULT);
30 
31  if (m_FileId < 0) {
32  printMessage(tr("File %1 could not be opened").arg(m_InputFilePath));
33  return false;
34  }
35 
36  m_DatasetId = H5Dopen1(m_FileId, qPrintable(m_DatasetName));
37 
38  if (m_DatasetId < 0) {
39  printMessage(tr("Dataset %1 not found in file %2").arg(m_DatasetName).arg(m_InputFilePath));
40  return false;
41  }
42 
43  m_DataspaceId = H5Dget_space(m_DatasetId);
44 
45  if (m_DataspaceId < 0) {
46  printMessage(tr("Couldn't get dataspace of dataset %1").arg(m_DatasetName));
47  return false;
48  }
49 
50  int ndims = H5Sget_simple_extent_ndims(m_DataspaceId);
51 
52  if (ndims != 3) {
53  printMessage(tr("Dataspace is not 3 dimensional (%1)").arg(ndims));
54  return false;
55  }
56 
57  hsize_t dims[3];
58 
59  int ndims2 = H5Sget_simple_extent_dims(m_DataspaceId, dims, NULL);
60 
61  if (ndims2 != 3) {
62  printMessage("Couldnt get dataspace dimensions");
63  return false;
64  }
65 
66  setDimensions(CctwIntVector3D(dims[2], dims[1], dims[0]));
67 
68  hid_t pl = H5Dget_create_plist(m_DatasetId);
69 
70  if (pl < 0) {
71  printMessage("Couldn't get dataset property list");
72  return false;
73  }
74 
75  hsize_t cksz[3];
76 
77  int chnks = H5Pget_chunk(pl, 3, cksz);
78 
79  if (chnks != 3) {
80  printMessage("Chunks are not 3 dimensional");
81  return false;
82  }
83 
84  setChunkSize(CctwIntVector3D(cksz[2], cksz[1], cksz[0]));
85 
86  return true;
87 }
88 
89 double CctwInputDataH5::readData(int dx, int dy, int dz)
90 {
91  if (m_FileId >= 0) {
92  hid_t memspace_id;
93  hsize_t offset[3], count[3], stride[3], block[3];
94  double data[10];
95 
96  count[0] = 1;
97  count[1] = 1;
98  count[2] = 1;
99 
100  stride[0] = 1;
101  stride[1] = 1;
102  stride[2] = 1;
103 
104  block[0] = 1;
105  block[1] = 1;
106  block[2] = 1;
107 
108  offset[0] = dz;
109  offset[1] = dy;
110  offset[2] = dx;
111 
112 
113  memspace_id = H5Screate_simple(3, count, NULL);
114 
115  herr_t selerr = H5Sselect_hyperslab(m_DataspaceId, H5S_SELECT_SET, offset, stride, count, block);
116  herr_t rderr = H5Dread(m_DatasetId, H5T_NATIVE_DOUBLE, memspace_id, m_DataspaceId, H5P_DEFAULT, data);
117 
118  if (selerr || rderr) {
119  printMessage(tr("Error reading x:%1, y:%2, z:%3, selerr = %4, wrterr = %5")
120  .arg(dx).arg(dy).arg(dz).arg(selerr).arg(rderr));
121  }
122 
123  return data[0];
124  }
125 
126  return 0;
127 }
128 
129 QVector<double> CctwInputDataH5::readChunk(int ix, int iy, int iz, int nx, int ny, int nz)
130 {
131  QVector<double> data(nx*ny*nz);
132 
133  if (m_FileId >= 0) {
134  hid_t memspace_id;
135  hsize_t offset[3], count[3], stride[3], block[3];
136 
137  count[0] = nz;
138  count[1] = ny;
139  count[2] = nx;
140 
141  stride[0] = 1;
142  stride[1] = 1;
143  stride[2] = 1;
144 
145  block[0] = 1;
146  block[1] = 1;
147  block[2] = 1;
148 
149  offset[0] = iz;
150  offset[1] = iy;
151  offset[2] = ix;
152 
153 
154  memspace_id = H5Screate_simple(3, count, NULL);
155 
156  herr_t selerr = H5Sselect_hyperslab(m_DataspaceId, H5S_SELECT_SET, offset, stride, count, block);
157  herr_t rderr = H5Dread(m_DatasetId, H5T_NATIVE_DOUBLE, memspace_id, m_DataspaceId, H5P_DEFAULT, data.data());
158 
159  if (selerr || rderr) {
160  printMessage(tr("Error reading x:%1, y:%2, z:%3, selerr = %4, wrterr = %5")
161  .arg(ix).arg(iy).arg(iz).arg(selerr).arg(rderr));
162  }
163  }
164 
165  return data;
166 }
167 
169 {
170  m_Dimensions = dims;
171 }
172 
174 {
175  m_ChunkSize = cksz;
176 }
177 
179 {
180  return m_Dimensions;
181 }
182 
184 {
185  return m_ChunkSize;
186 }
187 
188 //CctwIntVector3D CctwInputDataH5::datasetDimensionsH5(QString filePath, QString datasetName)
189 //{
190 // return CctwIntVector3D();
191 //}
192 
193 //CctwIntVector3D CctwInputDataH5::datasetChunkSizeH5(QString filePath, QString datasetName)
194 //{
195 // return CctwIntVector3D();
196 //}
QVector< double > readChunk(int ix, int iy, int iz, int nx, int ny, int nz)
QString m_InputFilePath
virtual void printMessage(QString msg, QDateTime dt=QDateTime::currentDateTime())
Definition: cctwobject.cpp:25
virtual double readData(int dx, int dy, int dz)
CctwIntVector3D chunkSize() const
CctwIntVector3D dimensions() const
void setChunkSize(CctwIntVector3D cksz)
CctwInputDataH5(QString filePath, QString datasetName, QString name, CctwObject *parent)
CctwVector3D< int > CctwIntVector3D
Definition: cctwvector3d.h:70
CctwIntVector3D m_ChunkSize
void setDimensions(CctwIntVector3D dims)
CctwIntVector3D m_Dimensions