-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdfTest.cpp
307 lines (283 loc) · 9.6 KB
/
hdfTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<limits>
#include<boost/multi_array.hpp>
#include <boost/any.hpp>
#include "Object.h"
#include "File.h"
using namespace std;
//template<typename T> class Vector {
// public:
// T x;
// T y;
// T z;
//
// Vector& operator=(const Vector& in) {
// if (this != &in) {
// x = in.x;
// y = in.y;
// z = in.z;
// }
// return *this;
// }
//
// static H5::CompType type() {
// H5::CompType vecType( sizeof(Vector<T>) );
// vecType.insertMember("X", HOFFSET(Vector<T>, x), H5::PredType::NATIVE_DOUBLE);
// vecType.insertMember("Y", HOFFSET(Vector<T>, y), H5::PredType::NATIVE_DOUBLE);
// vecType.insertMember("Z", HOFFSET(Vector<T>, z), H5::PredType::NATIVE_DOUBLE);
// return vecType;
// }
//
// void write(H5::H5File& file, const std::string& name) {
//
// };
//};
int main (int argc, char** argv) {
cout << "HDF5 Test program for c++ and compound datatypes" << endl;
hdf5::File file = hdf5::OpenFile("test.h5").readWrite();
cout << " ========= File has been opened ========= " << endl;
cout << "File size = " << file.getFileSize() << endl;
cout << "Free space = " << file.getFreeSpace() << endl;
cout << "# Attributes = " << file.getNumAttributes() << endl;
cout << "# Objects = " << file.getNumObjects() << endl;
cout << " ---" << endl;
const hdf5::Object& testTable = dynamic_cast<hdf5::Group&>(file("TestGroup"))("testData 2d");
cout << "DataSet: " << testTable.getName() << endl;
cout << " --> #Attributes=" << testTable.getNumAttributes() << endl;
for (hdf5::Object::AttributeConstIterator it = testTable.attributesBegin(); it != testTable.attributesEnd(); ++it) {
cout << " * " << it->first << ": " << it->second << endl;
}
const hdf5::Dataset& dsTest = dynamic_cast<const hdf5::Dataset&>(testTable);
boost::multi_array<int32_t, 2> array;
dsTest.read(array);
for (size_t x = 0; x < array.shape()[0]; ++x) {
for (size_t y = 0; y < array.shape()[0]; ++y) {
cout << array[x][y] << ", ";
}
cout << endl;
}
// group->getObject("testData 2d");
cout << "Creating boost::multi_array<double, 2>" << endl;
boost::multi_array<double, 2> arrayOut(boost::extents[10][10]);
for (size_t x = 0; x < arrayOut.shape()[0]; ++x) {
for (size_t y = 0; y < arrayOut.shape()[0]; ++y) {
arrayOut[x][y] = x*y;
}
}
try {
cout << "Delete /TestGroup/multArray_WriteTest if it exists" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).deleteObject("multArray_WriteTest");
}
catch (const hdf5::Exception& e) {
}
cout << "Writeout dataset" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).createDataset("multArray_WriteTest", arrayOut);
cout << "Writeout dataset --> DONE" << endl;
//
cout << " ########### writing compound data type" << endl;
boost::multi_array<hdf5::Vector, 2> arrayOutCompound(boost::extents[10][10]);
for (size_t x = 0; x < arrayOutCompound.shape()[0]; ++x) {
for (size_t y = 0; y < arrayOutCompound.shape()[0]; ++y) {
arrayOutCompound[x][y] = hdf5::Vector(x, y, x*y);
}
}
try {
cout << "Delete /TestGroup/multArray_WriteTestCompound if it exists" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).deleteObject("multArray_WriteTestCompound");
}
catch (const hdf5::Exception& e) {
}
cout << "Writeout compound dataset" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).createDataset("multArray_WriteTestCompound", arrayOutCompound);
cout << "Writeout dataset --> DONE" << endl;
// std::vector
cout << " ########### writing vector" << endl;
vector<double> testVec;
for (size_t x = 0; x < 10; ++x) {
testVec.push_back(x);
}
try {
cout << "Delete /TestGroup/testVec if it exists" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).deleteObject("testVec");
}
catch (const hdf5::Exception& e) {
}
cout << "Writeout" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).createDataset("testVec", testVec);
cout << "Writeout dataset --> DONE" << endl;
// std::list
cout << " ########### writing list" << endl;
list<double> testList;
for (size_t x = 0; x < 10; ++x) {
testList.push_back(x);
}
try {
cout << "Delete /TestGroup/testList if it exists" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).deleteObject("testList");
}
catch (const hdf5::Exception& e) {
}
cout << "Writeout" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).createDataset("testList", testList);
cout << "Writeout dataset --> DONE" << endl;
// std::map
// std::map
cout << " ########### writing map<double, int>" << endl;
map<double, int> testMap;
for (size_t x = 0; x < 10; ++x) {
testMap[double(x)] = x;
}
try {
cout << "Delete /TestGroup/testList if it exists" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).deleteObject("testMap");
}
catch (const hdf5::Exception& e) {
}
cout << "Writeout" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).createDataset("testMap", testMap);
cout << "Writeout dataset --> DONE" << endl;
cout << " ########### writing map<string, int>" << endl;
map<string, double> testMapStr;
for (size_t x = 0; x < 16; ++x) {
stringstream a;
a << "Diese Zahl ist in hex " << hex << x << dec;
testMapStr[a.str()] = x;
}
testMapStr["Katzenklo, Katzenkla. Ja das macht die Katze froh. Wir gruessen Helge S."] = 2222;
testMapStr["Die Antwort auf alles"] = 42;
testMapStr["zz top and la Grange"] = 23;
try {
cout << "Delete /TestGroup/testList if it exists" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).deleteObject("testMapStr");
}
catch (const hdf5::Exception& e) {
}
cout << "Writeout" << endl;
dynamic_cast<hdf5::Group&>(file("TestGroup")).createDataset("testMapStr", testMapStr);
cout << "Writeout dataset --> DONE" << endl;
cout << " --- Clearing map<string, int>" << endl;
testMapStr.clear();
dynamic_cast<hdf5::Dataset&>(dynamic_cast<hdf5::Group&>(file("TestGroup"))("testMapStr")).read(testMapStr);
for (map<string, double>::const_iterator it = testMapStr.begin(); it != testMapStr.end(); ++it) {
cout << " * " << it->first << " --> " << it->second << endl;
}
cout << " ======= THE END ======= " << endl;
// // opening file
// H5::H5File outFile("test.h5", H5F_ACC_TRUNC);
//
// typedef Vector<double> VecDouble;
// VecDouble v;
// v.x = 1;
// v.y = 2;
// v.z = 3;
//
// // store single value
// hsize_t dimensionsSingle[] = { 1, };
// H5::DataSpace dataSpaceSingle(1, dimensionsSingle);
// H5::DataSet dataSetSingle = outFile.createDataSet("single", v.type(), dataSpaceSingle);
// dataSetSingle.write(&v, v.type());
//
// // now we store an stl vector of vectors
// vector<VecDouble> vv;
// for (size_t i = 0; i < 20; ++i) {
// VecDouble x;
// x.x = static_cast<double>(i)+.1;
// x.y = static_cast<double>(i)+.2;
// x.z = static_cast<double>(i)+.3;
//
// vv.push_back(x);
// }
//
//// for (vector<VecDouble>::const_iterator s = vv.begin(); s != vv.end(); ++s) {
//// cout << s->x << ", " << s->y << ", " << s->z << endl;
//// }
// hsize_t dimensionsVec[] = { vv.size(), };
// H5::DataSpace dataSpaceVec(1, dimensionsVec);
// H5::DataSet dataSetVec = outFile.createDataSet("vec", v.type(), dataSpaceVec);
// dataSetVec.write(vv.data(), v.type());
//
// // boost multi_array
// boost::multi_array<VecDouble, 3> field;
// field.resize(boost::extents[4][4][4]);
// for (size_t ix = 0; ix < field.shape()[0]; ++ix) {
// for (size_t iy = 0; iy < field.shape()[1]; ++iy) {
// for (size_t iz = 0; iz < field.shape()[2]; ++iz) {
// VecDouble x;
// x.x = static_cast<double>(ix);
// x.y = static_cast<double>(iy);
// x.z = static_cast<double>(iz);
// field[ix][iy][iz] = x;
// }
// }
// }
// hsize_t dimensionsField[] = { field.shape()[0], field.shape()[1], field.shape()[2] };
// H5::DataSpace dataSpaceField(3, dimensionsField);
// H5::DataSet dataSetField = outFile.createDataSet("field", v.type(), dataSpaceField);
// dataSetField.write(field.data(), v.type());
//
// // closing file
// outFile.close();
//
// /*
// * Now reopen file for reading
// */
// cout << endl << " --- Now read elements from file" << endl;
// cout << " - Processing field:" << endl;
// H5::H5File infile("test.h5", H5F_ACC_RDONLY);
// H5::DataSet dsetRoField(infile.openDataSet("field"));
// H5::DataSpace dspaceRoField(dsetRoField.getSpace());
//
// // getting rank and dimension of field
// int rankField = dspaceRoField.getSimpleExtentDims(0);
// if (rankField < 1)
// abort();
// cout << " * rank=" << rankField << endl;
// hsize_t dimField[rankField];
// dspaceRoField.getSimpleExtentDims(dimField);
// cout << " * dims=";
// for (size_t i = 0; i < static_cast<size_t>(rankField); ++i) {
// cout << dimField[i] << "x";
// }
// cout << endl;
//
// // preparing field for data
// if (rankField > 3) {
// cerr << "Unsupported rank " << rankField << " should be 2" << endl;
// return 1;
// }
// boost::multi_array<VecDouble, 3> fieldRo;
// fieldRo.resize( boost::extents[ dimField[0] ][ dimField[1] ][ dimField[2] ] );
// dsetRoField.read(fieldRo.data(), v.type());
// infile.close();
// for (size_t iz = 0; iz < dimField[2]; ++iz) {
// cout << "* z=" << iz << endl;
// for (size_t ix = 0; ix < dimField[0]; ++ix) {
// for (size_t iy = 0; iy < dimField[1]; ++iy) {
// cout << "(" << fieldRo[ix][iy][iz].x << ", " << fieldRo[ix][iy][iz].y << ", " << fieldRo[ix][iy][iz].z << ") ";
// }
// cout << endl;
// }
// }
//
//
// return 0;
// for (unsigned int xx = 0; xx < numeric_limits<unsigned int>::max(); ++xx) {
//
// double* x = new double[66666];
// for (size_t i = 0; i < 66666; ++i) {
// double a = sin(static_cast<double>(i) * 3.14156 / 66666);
// double b = sin(static_cast<double>(i) * 3.14156 / 66666);
// double c = a*a + b*b;
// double d = sqrt(c);
//
// x[i] = d;
// fieldRo.resize( boost::extents[ 5 ][ 5 ][ 5 ] );
// }
// delete x;
//
//
// }
}