@@ -33,7 +33,7 @@ HDF5Reader::HDF5Reader()
33
33
HDF5Reader::~HDF5Reader () { close (); }
34
34
35
35
void HDF5Reader::decompose (std::string& filename, std::string& dataname,
36
- const std::string& pathname)
36
+ const std::string& pathname)
37
37
{
38
38
const std::string::size_type idx = pathname.find (' :' );
39
39
@@ -85,15 +85,15 @@ bool HDF5Reader::existsValidHdf5(const std::string& fname)
85
85
if (!exists)
86
86
{
87
87
std::cerr << " HDF5Reader::existsValidHdf5() : error, '" << fname
88
- << " ' does not exist" << std::endl;
88
+ << " ' does not exist" << std::endl;
89
89
return false ;
90
90
}
91
91
#else
92
92
FILE* pFile = fopen (fname.c_str (), " r" );
93
93
if (pFile == nullptr )
94
94
{
95
95
std::cerr << " HDF5Reader::existsValidHdf5() : error, '" << fname
96
- << " ' does not exist" << std::endl;
96
+ << " ' does not exist" << std::endl;
97
97
return false ;
98
98
}
99
99
else
@@ -118,7 +118,7 @@ int HDF5Reader::open(const std::string& fname)
118
118
if (!HDF5Reader::existsValidHdf5 (fname))
119
119
{
120
120
std::cerr << " HDF5Reader::open() : " << fname
121
- << " does not exist or is not a valid hdf5 file" << std::endl;
121
+ << " does not exist or is not a valid hdf5 file" << std::endl;
122
122
return 0 ;
123
123
}
124
124
file = H5Fopen (fname.c_str (), H5F_ACC_RDONLY, H5P_DEFAULT);
@@ -148,7 +148,7 @@ int HDF5Reader::close()
148
148
}
149
149
150
150
int HDF5Reader::getDatasetInfo (std::string& type, std::vector<size_type>& extents,
151
- const std::string& name)
151
+ const std::string& name)
152
152
{
153
153
if (file < 0 )
154
154
{
@@ -165,7 +165,7 @@ int HDF5Reader::getDatasetInfo(std::string& type, std::vector<size_type>& extent
165
165
{
166
166
if (loud)
167
167
std::cerr << " HDF5Reader::getDatasetInfo() : no such dataset: " << name
168
- << std::endl;
168
+ << std::endl;
169
169
type = " none" ;
170
170
return 0 ;
171
171
}
@@ -226,7 +226,7 @@ int HDF5Reader::getDatasetInfo(std::string& type, std::vector<size_type>& extent
226
226
else
227
227
{
228
228
std::cerr << " HDF5Reader::getDatasetInfo() : warning, unsupported native type"
229
- << std::endl;
229
+ << std::endl;
230
230
status = H5Tclose (native_type);
231
231
status = H5Tclose (datatype);
232
232
status = H5Dclose (dataset);
@@ -260,48 +260,88 @@ int HDF5Reader::getDatasetInfo(std::string& type, std::vector<size_type>& extent
260
260
template <typename T>
261
261
struct HdfType
262
262
{
263
- static hid_t type () { return -1 ; }
263
+ static hid_t type () { return -1 ; }
264
264
};
265
265
266
- template <> struct HdfType <double > { static hid_t type () { return H5T_NATIVE_DOUBLE; } };
267
- template <> struct HdfType <float > { static hid_t type () { return H5T_NATIVE_FLOAT; } };
268
- template <> struct HdfType <std::uint8_t > { static hid_t type () { return H5T_NATIVE_UINT8; } };
269
- template <> struct HdfType <std::uint16_t > { static hid_t type () { return H5T_NATIVE_UINT16; } };
270
- template <> struct HdfType <std::uint32_t > { static hid_t type () { return H5T_NATIVE_UINT32; } };
271
- template <> struct HdfType <std::uint64_t > { static hid_t type () { return H5T_NATIVE_UINT64; } };
272
- template <> struct HdfType <std::int8_t > { static hid_t type () { return H5T_NATIVE_INT8; } };
273
- template <> struct HdfType <std::int16_t > { static hid_t type () { return H5T_NATIVE_INT16; } };
274
- template <> struct HdfType <std::int32_t > { static hid_t type () { return H5T_NATIVE_INT32; } };
275
- template <> struct HdfType <std::int64_t > { static hid_t type () { return H5T_NATIVE_INT64; } };
266
+ template <>
267
+ struct HdfType <double >
268
+ {
269
+ static hid_t type () { return H5T_NATIVE_DOUBLE; }
270
+ };
271
+ template <>
272
+ struct HdfType <float >
273
+ {
274
+ static hid_t type () { return H5T_NATIVE_FLOAT; }
275
+ };
276
+ template <>
277
+ struct HdfType <std::uint8_t >
278
+ {
279
+ static hid_t type () { return H5T_NATIVE_UINT8; }
280
+ };
281
+ template <>
282
+ struct HdfType <std::uint16_t >
283
+ {
284
+ static hid_t type () { return H5T_NATIVE_UINT16; }
285
+ };
286
+ template <>
287
+ struct HdfType <std::uint32_t >
288
+ {
289
+ static hid_t type () { return H5T_NATIVE_UINT32; }
290
+ };
291
+ template <>
292
+ struct HdfType <std::uint64_t >
293
+ {
294
+ static hid_t type () { return H5T_NATIVE_UINT64; }
295
+ };
296
+ template <>
297
+ struct HdfType <std::int8_t >
298
+ {
299
+ static hid_t type () { return H5T_NATIVE_INT8; }
300
+ };
301
+ template <>
302
+ struct HdfType <std::int16_t >
303
+ {
304
+ static hid_t type () { return H5T_NATIVE_INT16; }
305
+ };
306
+ template <>
307
+ struct HdfType <std::int32_t >
308
+ {
309
+ static hid_t type () { return H5T_NATIVE_INT32; }
310
+ };
311
+ template <>
312
+ struct HdfType <std::int64_t >
313
+ {
314
+ static hid_t type () { return H5T_NATIVE_INT64; }
315
+ };
276
316
277
317
int HDF5Reader::read (double * _data, const std::string& name)
278
318
{
279
- return this ->readData (_data, name);
319
+ return this ->readData (_data, name);
280
320
}
281
321
282
322
int HDF5Reader::read (float * _data, const std::string& name)
283
323
{
284
- return this ->readData (_data, name);
324
+ return this ->readData (_data, name);
285
325
}
286
326
287
327
int HDF5Reader::read (int * _data, const std::string& name)
288
328
{
289
- return this ->readData (_data, name);
329
+ return this ->readData (_data, name);
290
330
}
291
331
292
332
int HDF5Reader::read (unsigned int * _data, const std::string& name)
293
333
{
294
- return this ->readData (_data, name);
334
+ return this ->readData (_data, name);
295
335
}
296
336
297
337
int HDF5Reader::read (long * _data, const std::string& name)
298
338
{
299
- return this ->readData (_data, name);
339
+ return this ->readData (_data, name);
300
340
}
301
341
302
342
int HDF5Reader::read (unsigned char * _data, const std::string& name)
303
343
{
304
- return this ->readData (_data, name);
344
+ return this ->readData (_data, name);
305
345
}
306
346
307
347
int HDF5Reader::read (unsigned short * _data, const std::string& name)
@@ -311,125 +351,125 @@ int HDF5Reader::read(unsigned short* _data, const std::string& name)
311
351
312
352
int HDF5Reader::read (std::vector<double >& v, const std::string& name)
313
353
{
314
- return readVec (v, name);
354
+ return readVec (v, name);
315
355
}
316
356
317
357
int HDF5Reader::read (std::vector<float >& v, const std::string& name)
318
358
{
319
- return readVec (v, name);
359
+ return readVec (v, name);
320
360
}
321
361
322
362
int HDF5Reader::read (std::vector<int >& v, const std::string& name)
323
363
{
324
- return readVec (v, name);
364
+ return readVec (v, name);
325
365
}
326
366
327
367
int HDF5Reader::read (std::vector<unsigned int >& v, const std::string& name)
328
368
{
329
- return readVec (v, name);
369
+ return readVec (v, name);
330
370
}
331
371
332
372
int HDF5Reader::read (std::vector<long >& v, const std::string& name)
333
373
{
334
- return readVec (v, name);
374
+ return readVec (v, name);
335
375
}
336
376
337
377
int HDF5Reader::read (std::vector<unsigned char >& v, const std::string& name)
338
378
{
339
- return readVec (v, name);
379
+ return readVec (v, name);
340
380
}
341
381
342
382
int HDF5Reader::read (std::vector<unsigned short >& v, const std::string& name)
343
383
{
344
- return readVec (v, name);
384
+ return readVec (v, name);
345
385
}
346
386
347
387
template <typename T>
348
388
int HDF5Reader::readVec (std::vector<T>& v, const std::string& name)
349
389
{
350
- std::string type;
351
- std::vector<size_type> dims;
352
- if (!getDatasetInfo (type, dims, name))
353
- return 0 ;
354
- if (dims.size () != 1 )
355
- {
356
- warning (" HDF5Reader::read() : rank must be 1 for std::vectors, returning" );
357
- return 0 ;
358
- }
359
- v.resize (dims[0 ]);
360
- return this ->readData (v.data (), name);
390
+ std::string type;
391
+ std::vector<size_type> dims;
392
+ if (!getDatasetInfo (type, dims, name))
393
+ return 0 ;
394
+ if (dims.size () != 1 )
395
+ {
396
+ warning (" HDF5Reader::read() : rank must be 1 for std::vectors, returning" );
397
+ return 0 ;
398
+ }
399
+ v.resize (dims[0 ]);
400
+ return this ->readData (v.data (), name);
361
401
}
362
402
363
403
int HDF5Reader::read (float * data, size_type offset, size_type length,
364
- const std::string& name)
404
+ const std::string& name)
365
405
{
366
406
return HDF5IO ().readData (file, name, offset, length, data) ? 1 : 0 ;
367
407
}
368
408
369
409
int HDF5Reader::read (unsigned short * data, size_type offset,
370
- size_type length, const std::string& name)
410
+ size_type length, const std::string& name)
371
411
{
372
412
return HDF5IO ().readData (file, name, offset, length, data) ? 1 : 0 ;
373
413
}
374
414
375
415
template <typename T>
376
416
int HDF5Reader::readData (T* Array, const std::string& name)
377
417
{
378
- if (file < 0 )
379
- {
380
- std::cerr << " HDF5Reader::readData() : no file open" << std::endl;
381
- return 0 ;
382
- }
383
- if (!Array)
384
- {
385
- std::cerr << " HDF5Reader::readData() : no pointer to data" << std::endl;
386
- return 0 ;
387
- }
388
-
389
- if (loud)
390
- std::cerr << " HDF5Reader::readData() : loading dataset " << name << std::endl;
391
-
392
- bool ok = false ;
393
-
394
- hid_t dataset = H5Dopen2 (file, name.c_str (), H5P_DEFAULT);
395
- if (dataset >= 0 )
396
- {
397
- hid_t dataspace = H5Dget_space (dataset);
398
- if (dataspace >= 0 )
399
- {
400
- int rank = H5Sget_simple_extent_ndims (dataspace);
401
- if (rank >= 0 )
402
- {
403
- std::vector<hsize_t > dims (rank);
404
- H5Sget_simple_extent_dims (dataspace, dims.data (), nullptr );
405
- H5Sselect_all (dataspace);
406
-
407
- if (H5Sselect_valid (dataspace) >= 0 )
408
- {
409
- // Define the memory space to read dataset.
410
- hid_t memoryspace = H5Screate_simple (rank, dims.data (), nullptr );
411
-
412
- hid_t type_id = HdfType<T>::type ();
413
- if (HdfType<T>::type < 0 )
414
- {
415
- // get data type from file
416
- type_id = H5Dget_type (dataset);
417
- }
418
-
419
- herr_t status = H5Dread (dataset, type_id, memoryspace, dataspace, H5P_DEFAULT, Array);
420
- ok = (status >= 0 );
421
-
422
- if (HdfType<T>::type () < 0 )
423
- H5Tclose (type_id);
424
- H5Sclose (memoryspace);
425
- }
426
- }
427
- H5Sclose (dataspace);
428
- }
429
- H5Dclose (dataset);
430
- }
431
-
432
- return ok ? 1 : 0 ;
418
+ if (file < 0 )
419
+ {
420
+ std::cerr << " HDF5Reader::readData() : no file open" << std::endl;
421
+ return 0 ;
422
+ }
423
+ if (!Array)
424
+ {
425
+ std::cerr << " HDF5Reader::readData() : no pointer to data" << std::endl;
426
+ return 0 ;
427
+ }
428
+
429
+ if (loud)
430
+ std::cerr << " HDF5Reader::readData() : loading dataset " << name << std::endl;
431
+
432
+ bool ok = false ;
433
+
434
+ hid_t dataset = H5Dopen2 (file, name.c_str (), H5P_DEFAULT);
435
+ if (dataset >= 0 )
436
+ {
437
+ hid_t dataspace = H5Dget_space (dataset);
438
+ if (dataspace >= 0 )
439
+ {
440
+ int rank = H5Sget_simple_extent_ndims (dataspace);
441
+ if (rank >= 0 )
442
+ {
443
+ std::vector<hsize_t > dims (rank);
444
+ H5Sget_simple_extent_dims (dataspace, dims.data (), nullptr );
445
+ H5Sselect_all (dataspace);
446
+
447
+ if (H5Sselect_valid (dataspace) >= 0 )
448
+ {
449
+ // Define the memory space to read dataset.
450
+ hid_t memoryspace = H5Screate_simple (rank, dims.data (), nullptr );
451
+
452
+ hid_t type_id = HdfType<T>::type ();
453
+ if (type_id < 0 )
454
+ {
455
+ // get data type from file
456
+ type_id = H5Dget_type (dataset);
457
+ }
458
+
459
+ herr_t status = H5Dread (dataset, type_id, memoryspace, dataspace, H5P_DEFAULT, Array);
460
+ ok = (status >= 0 );
461
+
462
+ if (HdfType<T>::type () < 0 )
463
+ H5Tclose (type_id);
464
+ H5Sclose (memoryspace);
465
+ }
466
+ }
467
+ H5Sclose (dataspace);
468
+ }
469
+ H5Dclose (dataset);
470
+ }
471
+
472
+ return ok ? 1 : 0 ;
433
473
}
434
474
435
475
} // namespace iseg
0 commit comments