Nemo  2.4.2
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
67  : _current_sim(0),_buff(),_in_pop(0),
70  { }
unsigned int _generation_in_file
Definition: binarydataloader.h:61
BinaryStorageBuffer _buff
The data countainer into which the data will be copied.
Definition: binarydataloader.h:45
SimBuilder * _current_sim
Pointer to the current (calling) simulation object.
Definition: binarydataloader.h:42
BinaryFileParser _pExtractor
Definition: binarydataloader.h:63
std::string _filename
Definition: binarydataloader.h:55
Metapop * _in_pop
Pointer to the a newly created Metapop object used to retrieve the data.
Definition: binarydataloader.h:48
unsigned int _gen
Definition: binarydataloader.h:53
off_t _offsetDataStart
Definition: binarydataloader.h:59
SimBuilder * _new_sim
Pointer to a new simulation builder to load the new simulation.
Definition: binarydataloader.h:51

◆ ~BinaryDataLoader()

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

References _in_pop, and _new_sim.

Member Function Documentation

◆ clear()

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

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

◆ extractOffsetTable()

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

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

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(), BinaryStorageBuffer::clear(), error(), extractOffsetTable(), fatal(), SimComponent::get_paramset(), BinaryStorageBuffer::getBuffer(), ParamsParser::getParameters(), tstring::int2str(), 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
86 {return &_buff;}

References _buff.

◆ getGeneration()

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

References _gen.

◆ getPop()

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

References _in_pop.

Referenced by Metapop::loadPopFromBinarySource().

+ Here is the caller graph for this function:

◆ getSim()

SimBuilder* BinaryDataLoader::getSim ( ) const
inline
80 {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
84 {_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.4.2 by  doxygen 1.9.1

Catalogued on GSR