Skip to content

Commit d41e5e3

Browse files
committed
Slight refactor to reduce code repitition
1 parent ad68150 commit d41e5e3

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

src/io/APRFile.hpp

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class APRFile {
135135
//Advanced Parameters.
136136
int max_level_delta = 0;
137137

138+
hid_t open_dataset_location(std::string& particles_name, bool apr_or_tree, uint64_t t, std::string& channel_name);
139+
138140

139141
};
140142

@@ -285,6 +287,7 @@ bool APRFile::write_apr_append(APR &apr){
285287
template<typename DataType>
286288
bool APRFile::write_particles(const std::string particles_name,ParticleData<DataType>& particles,bool apr_or_tree,uint64_t t,std::string channel_name){
287289

290+
288291
if(fileStructure.isOpened()){
289292
} else {
290293
std::cerr << "File is not open!" << std::endl;
@@ -493,13 +496,9 @@ bool APRFile::read_apr(APR &apr,uint64_t t,std::string channel_name){
493496
template<typename DataType>
494497
bool APRFile::read_particles(APR &apr,std::string particles_name,ParticleData<DataType>& particles,bool apr_or_tree,uint64_t t,std::string channel_name){
495498

496-
if(!fileStructure.isOpened()){
497-
std::cerr << "File is not open!" << std::endl;
498-
return false;
499-
}
499+
hid_t part_location = open_dataset_location(particles_name, apr_or_tree,t,channel_name);
500500

501-
if(!fileStructure.open_time_point(t, with_tree_flag,channel_name)) {
502-
std::cerr << "Error reading APR file: could not open time point t=" << t << " in channel '" << channel_name << "'" << std::endl;
501+
if(part_location == 0){
503502
return false;
504503
}
505504

@@ -514,22 +513,6 @@ bool APRFile::read_particles(APR &apr,std::string particles_name,ParticleData<Da
514513
return false;
515514
}
516515

517-
//check if old or new file, for location of the properties. (The metadata moved to the time point.)
518-
hid_t part_location;
519-
520-
if(apr_or_tree){
521-
part_location = fileStructure.objectId;
522-
} else {
523-
part_location = fileStructure.objectIdTree;
524-
}
525-
526-
// Check that the dataset exists
527-
std::string data_n = particles_name;
528-
if(!data_exists(part_location,data_n.c_str())) {
529-
std::cerr << "Error reading APR file: particle dataset '" << particles_name << "' doesn't exist" << std::endl;
530-
return false;
531-
}
532-
533516
if(apr_or_tree){
534517
max_read_level = std::max((int)apr.level_min(),(int)(apr.level_max() - max_level_delta));
535518
parts_start = 0;
@@ -644,31 +627,42 @@ bool APRFile::read_particles(APR& apr,ParticleData<DataType>& particles,bool apr
644627
return read;
645628
}
646629

630+
hid_t APRFile::open_dataset_location(std::string& particles_name, bool apr_or_tree, uint64_t t, std::string& channel_name) {
647631

648-
std::string APRFile::get_particle_type(std::string particles_name, bool apr_or_tree, uint64_t t, std::string channel_name) {
649-
650-
if(!fileStructure.isOpened()){
632+
if (!fileStructure.isOpened()) {
651633
std::cerr << "File is not open!" << std::endl;
652-
return "";
634+
return 0;
653635
}
654636

655-
if(!fileStructure.open_time_point(t, with_tree_flag,channel_name)) {
656-
std::cerr << "Error reading APR file: could not open time point t=" << t << " in channel '" << channel_name << "'" << std::endl;
657-
return "";
637+
if (!fileStructure.open_time_point(t, with_tree_flag, channel_name)) {
638+
std::cerr << "Error reading APR file: could not open time point t=" << t << " in channel '" << channel_name
639+
<< "'" << std::endl;
640+
return 0;
658641
}
659642

660643
hid_t part_location;
661644

662-
if(apr_or_tree){
645+
if (apr_or_tree) {
663646
part_location = fileStructure.objectId;
664647
} else {
665648
part_location = fileStructure.objectIdTree;
666649
}
667650

668651
// Check that the dataset exists
669652
std::string data_n = particles_name;
670-
if(!data_exists(part_location,data_n.c_str())) {
653+
if (!data_exists(part_location, data_n.c_str())) {
671654
std::cerr << "Error reading APR file: particle dataset '" << particles_name << "' doesn't exist" << std::endl;
655+
return 0;
656+
}
657+
658+
return part_location;
659+
}
660+
661+
std::string APRFile::get_particle_type(std::string particles_name, bool apr_or_tree, uint64_t t, std::string channel_name) {
662+
663+
hid_t part_location = open_dataset_location(particles_name, apr_or_tree,t,channel_name);
664+
665+
if(part_location == 0){
672666
return "";
673667
}
674668

0 commit comments

Comments
 (0)