Skip to content

Commit f9caf63

Browse files
committed
add datatype check to read_particles + minor polish
1 parent c68625d commit f9caf63

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/io/APRFile.hpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -489,26 +489,27 @@ bool APRFile::read_apr(APR &apr,uint64_t t,std::string channel_name){
489489
template<typename DataType>
490490
bool APRFile::read_particles(APR &apr,std::string particles_name,ParticleData<DataType>& particles,bool apr_or_tree,uint64_t t,std::string channel_name){
491491

492-
if(fileStructure.isOpened()){
493-
} else {
492+
if(!fileStructure.isOpened()){
494493
std::cerr << "File is not open!" << std::endl;
495494
return false;
496495
}
497496

498-
fileStructure.open_time_point(t,with_tree_flag,channel_name);
497+
if(!fileStructure.open_time_point(t, with_tree_flag,channel_name)) {
498+
std::cerr << "Error reading APR file: could not open time point t=" << t << " in channel '" << channel_name << "'" << std::endl;
499+
return false;
500+
}
499501

500502
int max_read_level;
501503
uint64_t parts_start = 0;
502504
uint64_t parts_end = 0;
503505

504-
/*
505-
* Check that the APR is initialized.
506-
*/
506+
507+
// Check that the APR is initialized.
507508
if(!apr.is_initialized()){
509+
std::cerr << "Error reading particles: input APR is not initialized" << std::endl;
508510
return false;
509511
}
510512

511-
512513
//check if old or new file, for location of the properties. (The metadata moved to the time point.)
513514
hid_t part_location;
514515

@@ -518,13 +519,10 @@ bool APRFile::read_particles(APR &apr,std::string particles_name,ParticleData<Da
518519
part_location = fileStructure.objectIdTree;
519520
}
520521

521-
522-
/*
523-
* Check the dataset exists
524-
*/
522+
// Check that the dataset exists
525523
std::string data_n = particles_name;
526-
if(!data_exists(part_location,data_n.c_str())){
527-
std::cerr << "Particle dataset doesn't exist" << std::endl;
524+
if(!data_exists(part_location,data_n.c_str())) {
525+
std::cerr << "Error reading APR file: particle dataset '" << particles_name << "' doesn't exist" << std::endl;
528526
return false;
529527
}
530528

@@ -543,44 +541,43 @@ bool APRFile::read_particles(APR &apr,std::string particles_name,ParticleData<Da
543541
parts_end = it_tree.total_number_particles(max_read_level);
544542
}
545543

546-
547-
//backwards support
548-
bool new_parts = attribute_exists(part_location,AprTypes::CompressionType.typeName);
549-
550544
hid_t meta_data = part_location;
551545

552-
if(!new_parts) {
546+
// for backwards compatibility
547+
if(!attribute_exists(part_location,AprTypes::CompressionType.typeName)) {
553548
meta_data = fileStructure.groupId;
554549
}
555550

556-
/*
557-
* Check the dataset and the APR align.
558-
*
559-
*/
560-
561-
//HERE
562551
Hdf5DataSet dataset;
563552
dataset.init(part_location,particles_name.c_str());
564553
dataset.open();
565554

555+
// Check the size of the dataset
566556
std::vector<uint64_t> dims = dataset.get_dimensions();
567-
568557
uint64_t number_particles_in_dataset = dims[0];
569558

570559
if(number_particles_in_dataset < parts_end){
571-
std::cerr << "Dataset is not correct size" << std::endl;
560+
std::cerr << "Error reading particles: dataset is not correct size" << std::endl;
572561
dataset.close();
573562
return false;
574563
}
575564

565+
// Check the datatype
566+
hid_t parts_type = APRWriter::Hdf5Type<DataType>::type();
567+
hid_t dtype = dataset.get_type();
568+
569+
if(!H5Tequal(parts_type, dtype)) {
570+
std::cerr << "Error reading particles: datatype does not match input particles" << std::endl;
571+
dataset.close();
572+
return false;
573+
}
576574

577575
timer.start_timer("Read intensities");
578576

579577
// ------------- read data ------------------------------
580578
particles.data.resize(parts_end - parts_start);
581579
if (particles.data.size() > 0) {
582580
dataset.read(particles.data.data() + parts_start,parts_start,parts_end);
583-
584581
}
585582

586583
timer.stop_timer();

0 commit comments

Comments
 (0)