Skip to content

Commit ea59b2c

Browse files
committed
add read_particles method that doesn't require an input APR
1 parent 0c78a0b commit ea59b2c

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/io/APRFile.hpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class APRFile {
4444

4545
std::string get_particle_type(std::string particles_name, bool apr_or_tree=true, uint64_t t=0, std::string channel_name="t");
4646

47+
template<typename DataType>
48+
bool read_particles(std::string particles_name, ParticleData<DataType>& particles, bool apr_or_tree=true, uint64_t t=0, std::string channel_name="t");
49+
4750
//set helpers
4851
bool get_read_write_tree(){
4952
return with_tree_flag;
@@ -691,6 +694,97 @@ std::string APRFile::get_particle_type(std::string particles_name, bool apr_or_t
691694
}
692695

693696

697+
/**
698+
* Read particle dataset from file without requiring the corresponding APR.
699+
* @tparam DataType
700+
* @param particles_name
701+
* @param particles
702+
* @param apr_or_tree
703+
* @param t
704+
* @param channel_name
705+
* @return
706+
*/
707+
template<typename DataType>
708+
bool APRFile::read_particles(std::string particles_name, ParticleData<DataType>& particles, bool apr_or_tree, uint64_t t, std::string channel_name){
709+
710+
if(!fileStructure.isOpened()){
711+
std::cerr << "File is not open!" << std::endl;
712+
return false;
713+
}
714+
715+
if(!fileStructure.open_time_point(t, with_tree_flag,channel_name)) {
716+
std::cerr << "Error reading APR file: could not open time point t=" << t << " in channel '" << channel_name << "'" << std::endl;
717+
return false;
718+
}
719+
720+
//check if old or new file, for location of the properties. (The metadata moved to the time point.)
721+
hid_t part_location;
722+
if(apr_or_tree){
723+
part_location = fileStructure.objectId;
724+
} else {
725+
part_location = fileStructure.objectIdTree;
726+
}
727+
728+
// Check that the dataset exists
729+
std::string data_n = particles_name;
730+
if(!data_exists(part_location,data_n.c_str())) {
731+
std::cerr << "Error reading APR file: particle dataset '" << particles_name << "' doesn't exist" << std::endl;
732+
return false;
733+
}
734+
735+
hid_t meta_data = part_location;
736+
Hdf5DataSet dataset;
737+
dataset.init(part_location,particles_name.c_str());
738+
dataset.open();
739+
740+
// Get the size of the dataset
741+
std::vector<uint64_t> dims = dataset.get_dimensions();
742+
uint64_t number_particles_in_dataset = dims[0];
743+
744+
// Check the datatype
745+
hid_t parts_type = APRWriter::Hdf5Type<DataType>::type();
746+
hid_t dtype = dataset.get_type();
747+
if(!H5Tequal(parts_type, dtype)) {
748+
std::cerr << "Error reading particles: datatype does not match input particles" << std::endl;
749+
dataset.close();
750+
return false;
751+
}
752+
753+
particles.init(number_particles_in_dataset);
754+
755+
timer.start_timer("Read intensities");
756+
// ------------- read data ------------------------------
757+
if (particles.data.size() > 0) {
758+
dataset.read(particles.begin(), 0, number_particles_in_dataset);
759+
}
760+
timer.stop_timer();
761+
dataset.close();
762+
/*
763+
* Particle Compression Options
764+
*/
765+
int compress_type;
766+
APRWriter::readAttr(AprTypes::CompressionType, meta_data, &compress_type);
767+
float quantization_factor;
768+
APRWriter::readAttr(AprTypes::QuantizationFactorType, meta_data, &quantization_factor);
769+
float compress_background;
770+
//backwards support
771+
bool background_exists = attribute_exists(part_location,AprTypes::CompressBackgroundType.typeName);
772+
if((compress_type > 0) && (background_exists)){
773+
APRWriter::readAttr(AprTypes::CompressBackgroundType, meta_data, &compress_background);
774+
}
775+
timer.start_timer("decompress");
776+
// ------------ decompress if needed ---------------------
777+
if (compress_type > 0) {
778+
particles.compressor.set_compression_type(compress_type);
779+
particles.compressor.set_quantization_factor(quantization_factor);
780+
particles.compressor.set_background(compress_background);
781+
particles.compressor.decompress(particles.data);
782+
}
783+
timer.stop_timer();
784+
return true;
785+
}
786+
787+
694788
//get helpers
695789

696790
/**

0 commit comments

Comments
 (0)