Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
BinaryDataLoader Class Reference

A class to load a whole population from a binary data file. More...

#include <binarydataloader.h>

+ Collaboration diagram for BinaryDataLoader:

Public Member Functions

 BinaryDataLoader ()
 
 ~BinaryDataLoader ()
 
off_t extractOffsetTable (int FD)
 
MetapopextractPop (string &filename, unsigned int generation, SimBuilder *sim, Metapop *popPtr)
 
MetapopgetPop () const
 
SimBuildergetSim () const
 
unsigned int getGeneration () const
 
void setGeneration (unsigned int gen)
 
const BinaryStorageBuffergetBuffer () const
 
void clear ()
 

Private Attributes

SimBuilder_current_sim
 Pointer to the current (calling) simulation object. More...
 
BinaryStorageBuffer _buff
 The data countainer into which the data will be copied. More...
 
Metapop_in_pop
 Pointer to the a newly created Metapop object used to retrieve the data. More...
 
SimBuilder_new_sim
 Pointer to a new simulation builder to load the new simulation. More...
 
unsigned int _gen
 
std::string _filename
 
off_t _offsetDataStart
 
unsigned int _generation_in_file
 
BinaryFileParser _pExtractor
 

Detailed Description

A class to load a whole population from a binary data file.

Constructor & Destructor Documentation

◆ BinaryDataLoader()

BinaryDataLoader::BinaryDataLoader ( )
inline
69 : _current_sim(0),_buff(),_in_pop(0),
72 { }
unsigned int _generation_in_file
Definition: binarydataloader.h:63
BinaryStorageBuffer _buff
The data countainer into which the data will be copied.
Definition: binarydataloader.h:47
SimBuilder * _current_sim
Pointer to the current (calling) simulation object.
Definition: binarydataloader.h:44
BinaryFileParser _pExtractor
Definition: binarydataloader.h:65
std::string _filename
Definition: binarydataloader.h:57
Metapop * _in_pop
Pointer to the a newly created Metapop object used to retrieve the data.
Definition: binarydataloader.h:50
unsigned int _gen
Definition: binarydataloader.h:55
off_t _offsetDataStart
Definition: binarydataloader.h:61
SimBuilder * _new_sim
Pointer to a new simulation builder to load the new simulation.
Definition: binarydataloader.h:53

◆ ~BinaryDataLoader()

BinaryDataLoader::~BinaryDataLoader ( )
48{
49 if(_in_pop != NULL) delete _in_pop;
50 if(_new_sim != NULL) delete _new_sim;
51}

References _in_pop, and _new_sim.

Member Function Documentation

◆ clear()

void BinaryDataLoader::clear ( )
54{
55 if(_in_pop != NULL) delete _in_pop;
56 if(_new_sim != NULL) delete _new_sim;
57 _in_pop = NULL;
58 _new_sim = NULL;
59 _buff.clear();
60
61}
void clear()
Definition: binarystoragebuffer.h:63

References _buff, _in_pop, _new_sim, and BinaryStorageBuffer::clear().

◆ extractOffsetTable()

off_t BinaryDataLoader::extractOffsetTable ( int  FD)
64{
65 int rout;
66
67 off_t current_pos = 0L;
68 off_t stop_pos = 0L;
69
70 current_pos = lseek(FD,0,SEEK_END); //this is eof
71
72 stop_pos = lseek(FD,current_pos -sizeof(int) - sizeof(off_t) - 3,SEEK_SET); // -3 to get the separator
73
74#ifdef _DEBUG_
75 cout<<"\n+++ input binary file: "<<_filename<<flush;
76 message(" position at the end of the file, at the position to read table info: %li\n", stop_pos);
77#endif
78
79 if(stop_pos == -1)
80 fatal("Binary file appears corrupted:\n \
81>>>> BinaryDataLoader::extractOffsetTable::lseek(1) failed on %s \n",strerror(errno));
82
83#ifdef _DEBUG_
84 message(" get the offset of the start of the recorded data: ");
85#endif
86
87// unsigned int tot=0;
88// do{
89// if((rout = read(FD,&nb_recgen,sizeof(int))) == -1)// || rout != sizeof(int))
90// fatal("Binary file appears corrupted:\n \
91//>>>> BinaryDataLoader::extractOffsetTable::read failed on %s\n",strerror(errno));
92// tot+=rout;
93// }while(tot < sizeof(int) && rout != 0);
94
95//#ifdef _DEBUG_
96// current_pos = lseek(FD,0,SEEK_CUR);
97// message("%i, bytes read:%i, current offset %li\n",nb_recgen, rout, current_pos);
98// message("get offset of the beginning of the offset table: ");
99//#endif
100
101// if((rout = read(FD, &off_table, sizeof(off_t))) == -1)// || rout != sizeof(int))
102// fatal("Binary file appears corrupted:\n \
103//>>>> BinaryDataLoader::extractOffsetTable::read failed on %s\n",strerror(errno));
104//
106// current_pos = lseek(FD,0,SEEK_CUR);
107// message("%li, bytes read:%i, current offset %li\n",off_table, rout, current_pos);
108// message("position at the beginning of the offset table:\n");
110//
111// current_pos = lseek(FD, off_table, SEEK_SET);
112//
113// if( current_pos == -1)
114// fatal("Binary file appears corrupted (pos = %li):\n \
115//>>>> BinaryDataLoader::extractOffsetTable::lseek(2) failed on %s \n", current_pos, strerror(errno));
116
117//#ifdef _DEBUG_
118// message("now at: %li\n",lseek(FD,0,SEEK_CUR));
119// message("check if we are correctly positioned, get the separator: ");
120//#endif
121 char separator[3];
122 if((rout = read(FD, &separator[0], 3)) == -1)// || rout != 3)
123 fatal("Binary file appears corrupted (rout = %d):\n \
124>>>> BinaryDataLoader::extractOffsetTable::read failed on %s\n",rout,strerror(errno));
125
126 if( separator[0] != '@' || separator[1] != 'O' || separator[2] != 'T')
127 fatal("Binary file appears corrupted:\n \
128>>>> BinaryDataLoader::extractOffsetTable:: wrong separator\n");
129
130//#ifdef _DEBUG_
131// message(" ok\n read the table elements:\n");
132//#endif
133
134// off_t table_elt[2];
135
136// _offset_table.clear();
137
138// unsigned int generation;
139//
140// for(int i = 0; i < nb_recgen; ++i) {
141
142 if((rout = read(FD, &_generation_in_file, sizeof(int))) == -1 || rout != sizeof(int))
143 fatal("Binary file appears corrupted:\n \
144>>>> BinaryDataLoader::extractOffsetTable::read of generation failed: %s\n",strerror(errno));
145
146 if((rout = read(FD, &_offsetDataStart, sizeof(off_t))) == -1 || rout != sizeof(off_t))
147 fatal("Binary file appears corrupted:\n \
148>>>> BinaryDataLoader::extractOffsetTable::read of offset failed: %s\n",strerror(errno));
149
150// _offset_table[table_elt[0]] = table_elt[1];
151
152#ifdef _DEBUG_
153 message("gen %i at %li\n", _generation_in_file, _offsetDataStart);
154#endif
155// }
156
157//#ifdef _DEBUG_
158// message("returning %li\n",stop_pos);
159//#endif
160 return stop_pos;
161}
void fatal(const char *str,...)
Definition: output.cc:96
void message(const char *message,...)
Definition: output.cc:40

References _filename, _generation_in_file, _offsetDataStart, fatal(), and message().

Referenced by extractPop().

+ Here is the caller graph for this function:

◆ extractPop()

Metapop * BinaryDataLoader::extractPop ( string &  filename,
unsigned int  generation,
SimBuilder sim,
Metapop popPtr 
)
164{
165 int FD;
166 off_t gen_offset=0, last_offset, byte_length;
167 string cmd, magic_name, old_name;
168 bool do_compress = 0;
169 _filename = file;
170 _gen = generation;
171 _pExtractor.setName(file.c_str());
172
173 if(_in_pop != NULL) delete _in_pop;
174 _in_pop = new Metapop();
175 _in_pop->setMPImanager( popPtr->_mpimgr );
176
177 //create new sim builder from copy of existing one, will copy templates
178 if(_new_sim != 0) delete _new_sim;
179 _new_sim = new SimBuilder();
181 _current_sim = sim;
182
183 //add the current metapop paramSet:
185
186 message("\n>>>> Reading binary file \"%s\"\n",_filename.c_str());
187
188 if((FD = open(_filename.c_str(),O_RDONLY)) == -1){
189 close(FD);
190 //check if we have to uncompress it:
191 string alt_name = _filename + ".bz2";
192 magic_name = _filename + tstring::int2str(RAND::Uniform(5477871));
193 int status;
194 if((FD = open(alt_name.c_str(),O_RDONLY)) == -1){
195 close(FD);
196 //check if gziped:
197 alt_name = _filename + ".gz";
198 if((FD = open(alt_name.c_str(),O_RDONLY)) == -1){
199 close(FD);
200 fatal("BinaryDataLoader::extractPop::open failed on %s: %s\n",_filename.c_str(),strerror(errno));
201 } else {
202 //try to ungzip it:
203 cmd = "gzip -cd " + alt_name + " > " + magic_name;
204
205 }
206 }
207 close(FD);
208 //try to bunzip2:
209 cmd = "bunzip2 -ck " + alt_name + " > " + magic_name;
210
211 status = system(cmd.c_str());
212 if(status != 0)
213 fatal("BinaryDataLoader::extractPop::bunzip2 failed (status %i) on %s\n",status, alt_name.c_str());
214
215 if((FD = open(magic_name.c_str(),O_RDONLY)) == -1) {//this should work now...
216 close(FD);
217 fatal("BinaryDataLoader::extractPop::open failed on %s: %s\n", _filename.c_str(), strerror(errno));
218 }
219 old_name = _filename;
220 _filename = magic_name;
221 _pExtractor.setName(magic_name.c_str());
222 do_compress = 1; //to remove the duplicated source file
223 }
224
225 //1. read the offset table:
226 last_offset = extractOffsetTable(FD);
227
228 //2. read the params and build the prototypes
229 //extract params from binary file and set the sim params
230 // !!! the param values in init part might not reflect pop state, esp if temporal arguments used !!!
232 error("Binary file appears corrupted:\n >>>> BinaryDataLoader::extractPop::could not set parameters from binary file\n");
233 return NULL;
234 }
235 //set Metapop params
236 //build the list of the selected trait templates, will init() the prototypes
237 //and load them into the pop, build the individual prototype
238// if( !(_in_pop->init(_new_sim->build_currentTraits(),_new_sim->build_currentLifeCycle())) )
239// return NULL;
240 if(!_in_pop->setParameters()) return NULL;
241
242 //build the patch structure:
244
245 //set the Individual and traits prototypes, will set the parameters (set_parameters called)
247
248 //3. check prototypes coherence with existing pop?
249 /* check prototypes coherence with existing pop, might have to add or remove traits? */
250
251 //4. load the pop
252 //compute the number of bytes to read:
253// std::map<unsigned int, unsigned int>::iterator OT = _offset_table.end();
254
255 //position within file, right after the parameters:
256 //check:
257// cout<<" currently at "<<lseek(FD,0,SEEK_CUR)<<"B in data file\n";
258 //starting point:
259// if(generation == 0) {
260// OT--;
261
262 gen_offset = _offsetDataStart; //load last generation recorded
264
265// } else {
266// if( generation != _generation_in_file )
267// fatal("Binary file appears corrupted:\n >>>> BinaryDataLoader::extractPop::generation %i not found in file \"%s\"\n",generation,_filename.c_str());
268// else
269// gen_offset = _offset_table;
270// }
271#ifdef _DEBUG_
272 message("\nBinaryDataLoader::extractPop::generation offset is: %li\n last offset is: %li ",gen_offset,last_offset);
273#endif
274
275 //length
276// if(_offset_table.size() == 1 || (++OT) == _offset_table.end())
277 byte_length = last_offset - gen_offset; //always only one generation recorded
278
279// else
280// gen_length = OT->second - gen_offset;
281
282 assert(byte_length > 0);
283
284 //+ 2 bytes to get the next separator: (@OT ?)
285 byte_length += 2;
286
287#ifdef _DEBUG_
288 message("--> nb bytes to read are: %li\n",byte_length);
289#endif
290
291 off_t current_pos = lseek(FD,_offsetDataStart,SEEK_SET);
292
293 if(current_pos == -1)
294 fatal("Binary file appears corrupted:\n >>>> BinaryDataLoader::extractPop::lseek failed on %s\n",strerror(errno));
295
296 off_t rout = -1, rcount = 0;
297 off_t rest = byte_length;
298 char *data = new char [byte_length];
299
300 while(rest != 0 && rout != 0) {
301 if( (rout = read(FD,&data[rcount],rest)) == -1)
302 fatal("Binary file appears corrupted:\n >>>> BinaryDataLoader::extractPop::read data %s (reading in %li bytes, read %li so far)\n",strerror(errno), rest, rout);
303 rcount += rout;
304 rest -= rout;
305#ifdef _DEBUG_
306 message("BinaryDataLoader::extractPop:read %i bytes from file\n",rout);
307#endif
308 }
309
310 close(FD);
311
312 _buff.set_buff(data, byte_length);
313
314 delete [] data;
315
316 unsigned char separator[2];
317
318 _buff.BSBread(&separator, 2 * sizeof(unsigned char));
319
320// cout<<"BinaryDataLoader::extractPop::generation separator: "<<separator<<endl;
321
322 if( separator[0] != '@' || separator[1] != 'G')
323 fatal("Binary file appears corrupted:\n >>>> BinaryDataLoader::extractPop:: wrong generation separator\n");
324
325// cout<<" retrieve generation number from the buffer: "<<flush;
326
327 unsigned int dummy;
328 _buff.BSBread(&dummy, sizeof(unsigned int));
329
330// cout<<dummy<<endl;
331
332 if(dummy != _gen)
333 fatal("Binary file appears corrupted:\n >>>> BinaryDataLoader::extractPop:: wrong generation in file\n");
334
335 bool status = _in_pop->retrieve_data(&_buff);
336
337
338 if(do_compress){
339 cmd = "rm -f " + magic_name;
340 if( system(cmd.c_str()) == 1)
341 warning("BinaryDataLoader::extractPop:: deleting duplicated source file failed on %s\n", _filename.c_str());
342 }
343
344 if( !status ) return NULL;
345
346 else return _in_pop;
347
348}
off_t extractOffsetTable(int FD)
Definition: binarydataloader.cc:63
void set_buff(BinaryDataSaver *owner)
Definition: binarystoragebuffer.h:83
void BSBread(void *out, unsigned int nb_bytes)
Definition: binarystoragebuffer.h:179
void makePrototype(map< trait_t, TraitPrototype * > TTlist)
Creates the individuals prototype from the selected trait prototypes.
Definition: indfactory.cc:50
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:80
void buildPatchArray()
Builds the new population from parameter values.
Definition: metapop.cc:308
void setMPImanager(MPImanager *mgr)
Definition: metapop.h:174
MPImanager * _mpimgr
Definition: metapop.h:85
virtual bool setParameters()
Definition: metapop.cc:115
virtual bool retrieve_data(BinaryStorageBuffer *reader)
Definition: metapop.cc:555
void add_paramset(ParamSet *paramset)
Adds a ParamSet to the list of the parameter sets of the simulation.
Definition: basicsimulation.h:95
void setName(const char *name)
Definition: paramsparser.h:57
map< string, string > & getParameters(const char *stream_name)
Definition: paramsparser.cc:78
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:125
static void loadDefaultComponents(SimBuilder *sim)
Definition: simenv.cc:53
Provides methods to build the user's selected set of life cycle events and traits from the parameters...
Definition: basicsimulation.h:168
bool build_currentParams(map< string, string > &simparams)
Builds the list of parameters from user's defined input parameters.
Definition: basicsimulation.cc:715
map< trait_t, TraitPrototype * > & build_currentTraits()
Selects the trait prototypes that have their parameters set.
Definition: basicsimulation.cc:741
virtual ParamSet * get_paramset()
ParamSet accessor.
Definition: simcomponent.h:108
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:95
int error(const char *str,...)
Definition: output.cc:77
void warning(const char *str,...)
Definition: output.cc:58

References _buff, _current_sim, _filename, _gen, _generation_in_file, _in_pop, Metapop::_mpimgr, _new_sim, _offsetDataStart, _pExtractor, ParamManager::add_paramset(), BinaryStorageBuffer::BSBread(), SimBuilder::build_currentParams(), SimBuilder::build_currentTraits(), Metapop::buildPatchArray(), error(), extractOffsetTable(), fatal(), SimComponent::get_paramset(), ParamsParser::getParameters(), tstring::int2str(), SIMenv::loadDefaultComponents(), IndFactory::makePrototype(), message(), Metapop::retrieve_data(), BinaryStorageBuffer::set_buff(), Metapop::setMPImanager(), ParamsParser::setName(), Metapop::setParameters(), RAND::Uniform(), and warning().

Referenced by Metapop::loadPopFromBinarySource().

+ Here is the caller graph for this function:

◆ getBuffer()

const BinaryStorageBuffer * BinaryDataLoader::getBuffer ( ) const
inline
88{return &_buff;}

References _buff.

◆ getGeneration()

unsigned int BinaryDataLoader::getGeneration ( ) const
inline
84{return _gen;}

References _gen.

◆ getPop()

Metapop * BinaryDataLoader::getPop ( ) const
inline
80{return _in_pop;}

References _in_pop.

Referenced by Metapop::loadPopFromBinarySource().

+ Here is the caller graph for this function:

◆ getSim()

SimBuilder * BinaryDataLoader::getSim ( ) const
inline
82{return _new_sim;}

References _new_sim.

Referenced by Metapop::loadPopFromBinarySource().

+ Here is the caller graph for this function:

◆ setGeneration()

void BinaryDataLoader::setGeneration ( unsigned int  gen)
inline
86{_gen = gen;}

References _gen.

Member Data Documentation

◆ _buff

BinaryStorageBuffer BinaryDataLoader::_buff
private

The data countainer into which the data will be copied.

Referenced by clear(), extractPop(), and getBuffer().

◆ _current_sim

SimBuilder* BinaryDataLoader::_current_sim
private

Pointer to the current (calling) simulation object.

Referenced by extractPop().

◆ _filename

std::string BinaryDataLoader::_filename
private

Referenced by extractOffsetTable(), and extractPop().

◆ _gen

unsigned int BinaryDataLoader::_gen
private

◆ _generation_in_file

unsigned int BinaryDataLoader::_generation_in_file
private

Referenced by extractOffsetTable(), and extractPop().

◆ _in_pop

Metapop* BinaryDataLoader::_in_pop
private

Pointer to the a newly created Metapop object used to retrieve the data.

Referenced by clear(), extractPop(), getPop(), and ~BinaryDataLoader().

◆ _new_sim

SimBuilder* BinaryDataLoader::_new_sim
private

Pointer to a new simulation builder to load the new simulation.

Referenced by clear(), extractPop(), getSim(), and ~BinaryDataLoader().

◆ _offsetDataStart

off_t BinaryDataLoader::_offsetDataStart
private

Referenced by extractOffsetTable(), and extractPop().

◆ _pExtractor

BinaryFileParser BinaryDataLoader::_pExtractor
private

Referenced by extractPop().


The documentation for this class was generated from the following files:

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR