BinaryDataLoader Class Reference

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

#include <binarydataloader.h>

Collaboration diagram for BinaryDataLoader:

Collaboration graph
[legend]

List of all members.

Public Member Functions

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

Private Attributes

SimBuilder_current_sim
BinaryStorageBuffer _buff
Metapop_in_pop
SimBuilder_new_sim
unsigned int _gen
std::string _filename
std::map< unsigned int,
unsigned int > 
_offset_table
BinaryFileParser _pExtractor


Detailed Description

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

Definition at line 40 of file binarydataloader.h.


Constructor & Destructor Documentation

BinaryDataLoader::BinaryDataLoader (  )  [inline]

Definition at line 60 of file binarydataloader.h.

00060                      : _current_sim(0),_buff(),_in_pop(0),
00061                        _new_sim(0),_gen(0),_filename(),_pExtractor(0)
00062     { }

BinaryDataLoader::~BinaryDataLoader (  ) 

Definition at line 44 of file binarydataloader.cc.

00045 {
00046   if(_in_pop != NULL) delete _in_pop;
00047   if(_new_sim != NULL) delete _new_sim;
00048 }


Member Function Documentation

void BinaryDataLoader::clear (  ) 

Definition at line 49 of file binarydataloader.cc.

00050 {
00051   if(_in_pop != NULL) delete _in_pop;
00052   if(_new_sim != NULL) delete _new_sim;
00053   _in_pop = NULL;
00054   _new_sim = NULL;
00055   _buff.clear();
00056   
00057 }

int BinaryDataLoader::extractOffsetTable ( int  FD  ) 

Definition at line 58 of file binarydataloader.cc.

Referenced by extractPop().

00059 {
00060   int current_pos = 0, rout, nb_recgen = 0, off_table = 0;
00061   lseek(FD,0,SEEK_END);
00062   current_pos = lseek(FD,0,SEEK_CUR);
00063 //#ifdef _DEBUG_ 
00064 //  message("eof at: %i\n",current_pos);
00065 //  message("position at the end of the file, at the position to read table info: ");
00066 //#endif
00067   if((current_pos = lseek(FD,current_pos -2*sizeof(int),SEEK_SET)) == -1) 
00068     fatal("BinaryDataLoader::extractOffsetTable::lseek %s\n",strerror(errno));
00069   
00070 //#ifdef _DEBUG_ 
00071 //  message("now at: %i\n",current_pos);
00072 //  message("get the number of generations stored in the file: "); 
00073 //#endif
00074   unsigned int tot=0;
00075   do{
00076   if((rout = read(FD,&nb_recgen,sizeof(int))) == -1)// || rout != sizeof(int)) 
00077     fatal("BinaryDataLoader::extractOffsetTable::read %s\n",strerror(errno));
00078     tot+=rout;
00079     }while(tot < sizeof(int) && rout != 0);
00080 //#ifdef _DEBUG_ 
00081 //  current_pos = lseek(FD,0,SEEK_CUR);
00082 //  message("%i, bytes read:%i, current offset %i\n",nb_recgen, rout, current_pos);
00083 //  message("get offset of the beginning of the offset table: "); 
00084 //#endif
00085   if((rout = read(FD,&off_table,sizeof(int))) == -1)// || rout != sizeof(int)) 
00086     fatal("BinaryDataLoader::extractOffsetTable::read %s\n",strerror(errno));
00087 //#ifdef _DEBUG_ 
00088 //  current_pos = lseek(FD,0,SEEK_CUR);
00089 //  message("%i, bytes read:%i, current offset %i\n",off_table, rout, current_pos);
00090 //  message("position at the beginning of the offset table:\n"); 
00091 //#endif
00092   if((current_pos = lseek(FD,off_table,SEEK_SET)) == -1) 
00093     fatal("BinaryDataLoader::extractOffsetTable::lseek %s",strerror(errno));
00094  
00095 //#ifdef _DEBUG_ 
00096 //  message("now at: %i\n",current_pos);
00097 //  message("check if we are correctly positionned, get the separator: "); 
00098 //#endif
00099   unsigned char separator[3];
00100   if((rout = read(FD, &separator, 3)) == -1)// || rout != 3) 
00101     fatal("BinaryDataLoader::extractOffsetTable::read %s\n",strerror(errno));
00102   
00103   if( separator[0] != '@' || separator[1] != 'O' || separator[2] != 'T')
00104     fatal("BinaryDataLoader::extractOffsetTable:: wrong separator\n");
00105 //#ifdef _DEBUG_ 
00106 //  message(" ok\n read the table elements:\n"); 
00107 //#endif
00108   int table_elt[2];
00109   _offset_table.clear();
00110   for(int i = 0; i < nb_recgen; ++i) {
00111     
00112     if((rout = read(FD,&table_elt,2*sizeof(int))) == -1 || rout != 2*sizeof(int)) 
00113       fatal("BinaryDataLoader::extractOffsetTable::read %s\n",strerror(errno));
00114     
00115     _offset_table[table_elt[0]] = table_elt[1];
00116 //#ifdef _DEBUG_ 
00117 //    message("gen %i at %i\n",table_elt[0], table_elt[1]); 
00118 //#endif
00119   }
00120 //#ifdef _DEBUG_ 
00121 //    message("returning %i\n",off_table); 
00122 //#endif
00123   return off_table;
00124 }

Metapop * BinaryDataLoader::extractPop ( string &  filename,
unsigned int  generation,
SimBuilder sim,
Metapop popPtr 
)

Definition at line 126 of file binarydataloader.cc.

Referenced by Metapop::loadPopFromBinarySource().

00127 {
00128   int FD, gen_length, gen_offset=0, otab_offset;
00129   string cmd;
00130   bool do_compress = 0;
00131   _filename = file;
00132   _gen = generation;
00133   _pExtractor.setName(file.c_str());
00134   
00135   if(_in_pop != NULL) delete _in_pop;
00136   _in_pop = new Metapop();
00137   _in_pop->setMPImanager( popPtr->_mpimgr );
00138   //create new sim builder from copy of existing one, will copy templates
00139   if(_current_sim != sim || _new_sim == 0) {
00140     if(_new_sim != 0) delete _new_sim;
00141     _new_sim = new SimBuilder(*sim);
00142     _current_sim = sim;
00143   } else
00144     _new_sim->build_allParams(); //reset the list of params, get rid of the old population paramset.
00145   //add the current metapop paramSet:
00146   _new_sim->add_paramset(_in_pop->get_paramset());
00147 
00148   if((FD = open(_filename.c_str(),O_RDONLY)) == -1){
00149     close(FD);
00150     //check if we have to uncompress it:
00151     string alt_name = _filename + ".bz2";
00152     int status;
00153     if((FD = open(alt_name.c_str(),O_RDONLY)) == -1){
00154       close(FD);
00155       //check if gziped:
00156       alt_name = _filename + ".gz";
00157       if((FD = open(alt_name.c_str(),O_RDONLY)) == -1){
00158         close(FD);
00159         fatal("BinaryDataLoader::extractPop::open %s: %s\n",_filename.c_str(),strerror(errno));
00160       } else {
00161         //try to ungzip it:
00162         cmd = "gunzip " + alt_name;
00163         status = system(cmd.c_str());
00164         if(status == 1 || status < 0)
00165           fatal("BinaryDataLoader::extractPop::gunzip failed on %s\n", alt_name.c_str());
00166         else {
00167           do_compress = 1;
00168           cmd = "gzip " + _filename;
00169         }
00170       }
00171     } else {
00172       //try to bunzip2:
00173       cmd = "bunzip2 " + alt_name;
00174       status = system(cmd.c_str());
00175       if(status != 0)
00176         fatal("BinaryDataLoader::extractPop::bunzip2 failed on %s\n", alt_name.c_str());
00177       else {
00178         do_compress = 1;
00179         cmd = "bzip2 " + _filename;
00180       }
00181     }
00182     
00183     if((FD = open(_filename.c_str(),O_RDONLY)) == -1) {//this should work now...
00184       close(FD);
00185       fatal("BinaryDataLoader::extractPop::open %s failed: %s\n", _filename.c_str(), strerror(errno));
00186     }
00187   }
00188   
00189   //1. read the offset table:
00190   otab_offset = extractOffsetTable(FD);
00191 
00192   //2. read the params and build the prototypes
00193   //extract params from binary file and set the sim params
00194   if( !(_new_sim->build_currentParams(_pExtractor.getParameters(NULL))) ){
00195     error("BinaryDataLoader::extractPop::could not set params\n");
00196     return NULL;
00197   }
00198   //set Metapop params
00199   //build the list of the selected trait templates, will init() the prototypes
00200   //and load them into the pop, build the individual prototype
00201 //  if( !(_in_pop->init(_new_sim->build_currentTraits(),_new_sim->build_currentLifeCycle())) )
00202 //    return NULL;
00203   if(!_in_pop->setParameters()) return NULL;  
00204   _in_pop->buildPatchArray();
00205   _in_pop->makePrototype(_new_sim->build_currentTraits());
00206   
00207   //3. check prototypes coherence with existing pop?
00208   /* check prototypes coherence with existing pop, might have to add or remove traits? */
00209   
00210   //4. load the pop
00211   //compute the number of bytes to read:
00212   std::map<unsigned int, unsigned int>::iterator OT = _offset_table.end(); 
00213   //starting point:
00214   if(generation == 0) {
00215     OT--;
00216     gen_offset = OT->second; //load last generation recorded
00217     _gen = OT->first;
00218   } else {
00219     if( (OT = _offset_table.find(generation)) == _offset_table.end() )
00220       fatal("BinaryDataLoader::extractPop::generation %i not found in file \"%s\"\n",generation,_filename.c_str());
00221     else
00222        gen_offset = OT->second;
00223   }
00224 //#ifdef _DEBUG_ 
00225 //  message("\ngeneration offset is: %i\notab offset is: %i\n",gen_offset,otab_offset); 
00226 //#endif
00227   //length
00228   if(_offset_table.size() == 1 || (++OT) == _offset_table.end())
00229     gen_length = otab_offset - gen_offset;
00230   else
00231     gen_length = OT->second - gen_offset;
00232   
00233   assert(gen_length > 0);
00234 
00235   //+ 2 bytes to get the next separator:
00236   gen_length += 2;
00237 
00238 //#ifdef _DEBUG_ 
00239 //  message("nb bytes to mmap are: %i\n",gen_length); 
00240 //#endif
00241   int current_pos = 0;
00242   
00243   if((current_pos = lseek(FD,gen_offset,SEEK_SET)) == -1) 
00244     fatal("BinaryDataLoader::extractPop::lseek %s",strerror(errno));  
00245   
00246   int rout = -1, rcount = 0, rest = gen_length;
00247   char *data = new char [gen_length];
00248   
00249   while(rest != 0 && rout != 0) {
00250     if( (rout = read(FD,&data[rcount],rest)) == -1) fatal("BinaryDataLoader::extractPop::read data %s\n",strerror(errno));
00251     rcount += rout;
00252     rest -= rout;
00253 #ifdef _DEBUG_ 
00254     message("BinaryDataLoader::extractPop:read %i bytes from file\n",rout);
00255 #endif
00256   }
00257   
00258   _buff.set_buff(data, gen_length);
00259   
00260   delete [] data;
00261   
00262   unsigned char separator[2];
00263   
00264   _buff.BSBread(&separator, 2 * sizeof(unsigned char));
00265   
00266   if( separator[0] != '@' || separator[1] != 'G')
00267     fatal("BinaryDataLoader::extractPop:: wrong generation separator\n");
00268   
00269   _buff.BSBread(&generation, sizeof(unsigned int));
00270   if(generation != _gen)
00271     fatal("BinaryDataLoader::extractPop:: wrong generation in file\n");
00272   
00273   bool status = true;
00274   //retrieve data from the buffer:
00275   status = _in_pop->retrieve_data(&_buff);
00276   
00277   close(FD);
00278   
00279   if(do_compress){
00280     if( system(cmd.c_str()) == 1)
00281       warning("BinaryDataLoader::extractPop:: compress source file failed on %s\n", _filename.c_str());
00282   }
00283   
00284   if( !status ) return NULL;
00285 
00286   else return _in_pop;
00287 
00288 }

const BinaryStorageBuffer* BinaryDataLoader::getBuffer (  )  const [inline]

Definition at line 78 of file binarydataloader.h.

00078 {return &_buff;}

unsigned int BinaryDataLoader::getGeneration (  )  const [inline]

Definition at line 74 of file binarydataloader.h.

00074 {return _gen;}

Metapop* BinaryDataLoader::getPop (  )  const [inline]

Definition at line 72 of file binarydataloader.h.

00072 {return _in_pop;}

void BinaryDataLoader::setGeneration ( unsigned int  gen  )  [inline]

Definition at line 76 of file binarydataloader.h.

00076 {_gen = gen;}


Member Data Documentation

Definition at line 44 of file binarydataloader.h.

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

Definition at line 42 of file binarydataloader.h.

Referenced by extractPop().

std::string BinaryDataLoader::_filename [private]

Definition at line 52 of file binarydataloader.h.

Referenced by extractPop().

unsigned int BinaryDataLoader::_gen [private]

Definition at line 50 of file binarydataloader.h.

Referenced by extractPop(), getGeneration(), and setGeneration().

Definition at line 46 of file binarydataloader.h.

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

Definition at line 48 of file binarydataloader.h.

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

std::map<unsigned int, unsigned int> BinaryDataLoader::_offset_table [private]

Definition at line 54 of file binarydataloader.h.

Referenced by extractOffsetTable(), and extractPop().

Definition at line 56 of file binarydataloader.h.

Referenced by extractPop().


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

Generated for Nemo v2.1.2 by  doxygen 1.5.8 -- Nemo is hosted by  SourceForge.net Logo