Index: trunk/AddOn/TAcq/brfitsrd.cc
===================================================================
--- trunk/AddOn/TAcq/brfitsrd.cc	(revision 3646)
+++ trunk/AddOn/TAcq/brfitsrd.cc	(revision 3683)
@@ -1,5 +1,5 @@
 //----------------------------------------------------------------
-// ---- classes de threads pour lecture fichiers minifits
-// LAL -      R. Ansari - Mai 2009 
+// Projet BAORadio - (C) LAL/IRFU  2008-2010
+// Classes de threads pour lecture fichiers fits BAORadio
 //----------------------------------------------------------------
 
@@ -10,5 +10,6 @@
 #include <unistd.h>
 #include <fstream>
-#include <signal.h>
+#include <exception>
+
 #include "pexceptions.h"
 #include "timestamp.h"
@@ -16,23 +17,218 @@
 
 #include "brpaqu.h"
-#include "minifits.h"
 
 #include "resusage.h" // Pour mesure temps elapsed/CPU ...
-#include "datatable.h" // Pour sauver les entetes de paquet 
 #include <sys/time.h>  // pour gettimeofday
 
+using namespace SOPHYA;
+
+//---------------------------------------------------------------------
+// Classe thread de lecture de Multi-fibres de fichiers FITS BAORadio  
+//---------------------------------------------------------------------
+
+/* --Methode-- */
+BRMultiFitsReader::BRMultiFitsReader(RAcqMemZoneMgr& mem, vector<string>& dirs, bool rdsamefc, 
+				     uint_4 imin, uint_4 imax, uint_4 istep)
+  :  memgr_(mem), dirs_(dirs), rdsamefc_(rdsamefc), imin_(imin), imax_(imax), istep_(istep) 
+{
+  SetPrintLevel();
+  totnbytesrd_ = 0;
+  totsamefc_ = 0;
+  if (memgr_.NbFibres() > MAXANAFIB) 
+    throw BAORadioException("BRMultiFitsReader::BRMultiFitsReader/ NbFibres>MAXANAFIB "); 
+  if (dirs_.size() != memgr_.NbFibres()) 
+    throw BAORadioException("BRMultiFitsReader::BRMultiFitsReader/ NbFibres != Nb Data Directories"); 
+
+  packsize_=memgr_.PaqSize();
+  mid_=-2; 
+  mmbuf_=NULL;
+  max_targ_npaq = memgr_.NbPaquets();
+  for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) mmbufib_[fib]=NULL;
+
+  char flnm[1024];
+  for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+    sprintf(flnm,"%s/signal%d.fits",dirs_[fib].c_str(),imin_);
+    mff_[fib].Open(flnm, MF_Read);
+    if (mff_[fib].NAxis1() != memgr_.PaqSize())  {
+      cout << " BRMultiFitsReader::BRMultiFitsReader/ fib=" << fib << " File=" << flnm <<
+	" NAxis1()= " << mff_[fib].NAxis1() << " <> PaqSize()=" << memgr_.PaqSize() << endl;
+      throw BAORadioException("BRMultiFitsReader::BRMultiFitsReader/ mff.NAxis1() != memgr_.PaqSize() "); 
+    }
+    vfilenum_.push_back(imin_);
+    vfpos_.push_back(0);
+    vpaq_.push_back(BRPaquet(NULL,memgr_.PaqSize()));
+    vpchk_.push_back(BRPaqChecker(true,0)); 
+    curfc_.push_back(0);
+    totnpqrd_.push_back(0);
+    totnpqok_.push_back(0);    
+  }
+}
+
+
+/* --Methode-- */
+void BRMultiFitsReader::run()
+{
+  setRC(1); 
+  try {
+    TimeStamp ts; 
+    Timer tm("BRMultiFitsReader", false);
+    cout << " BRMultiFitsReader::run() - Starting " << ts << " NbFibres()=" << memgr_.NbFibres()
+	 << "  PaqSize() = " << memgr_.PaqSize() << endl;
+    cout << " ...ReadMode: " << ((rdsamefc_)?"Paquets With SameFrameCounter":"All OK paquets") 
+	 << " signalII.fits IMin=" << imin_ << " IMax=" << imax_ << " IStep=" << istep_ << endl;
+
+    Byte* nextpaq=NULL;
+    bool fgok=true;
+    while (fgok) {
+      if (stop_) break;
+      if ( MoveToNextTarget() ) {
+	cout << "BRMultiFitsReader::run()/Error-A- MoveToNextTarget() returned true ->STOP 9" << endl;
+	setRC(7);  fgok=false;  break;
+      }
+      for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+	nextpaq=GetPaquetTarget(fib);
+	if (nextpaq == NULL) { // Cela ne devrait pas arriver 
+	  cout << "BRMultiFitsReader::run()/Error-A2- GetPaquetTarget(fib)  returned NULL ->STOP 9" << endl;
+	  setRC(9);  fgok=false;  break;
+	}
+	vpaq_[fib].Set(nextpaq);
+      }
+      if (ReadNextAllFibers()) { fgok=false;  break; }
+    }
+
+    MoveToNextTarget();  // Pour faire traiter le dernier paquet si plein 
+    MZoneManage(true);   // Nettoyage final 
+    usleep(50000);       // Attente de traitement du dernier paquet
+    memgr_.Stop();        // Arret 
+
+    cout << " ------------------  BRMultiFitsReader::run() END ----------------- " << endl;
+    ts.SetNow();
+    tm.SplitQ();
+    cout << "  END reading : " << ts ;
+    if (rdsamefc_) cout << " NSameFC=" << totsamefc_ << endl;
+    else cout << endl;
+    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
+      int perc=0;
+      if (totnpqrd_[fib]>0) perc=100*totsamefc_/totnpqrd_[fib];
+      cout << " Fiber" << fib << " TotNPaqRd=" << totnpqrd_[fib] << " TotNPaqOK=" << totnpqok_[fib] 
+	   << " FracSameFC=" << perc << " %" << endl;
+    }
+    cout << "  TotalDiskRead= " << totnbytesrd_/(1024*1024) << " MBytes Disk-Read rate= " 
+         << (double)(totnbytesrd_)/1024./tm.PartialElapsedTimems() << " MB/s" << endl;    
+    cout << " BRMultiFitsReader::run()/Timing: \n"; 
+    tm.Print();
+    cout << " ---------------------------------------------------------- " << endl;
+
+  }  // Fin du bloc try 
+  catch (std::exception& exc) {
+    cout << " BRMultiFitsReader::run()/catched execption msg=  " << exc.what() << endl;
+    setRC(3);	
+    return; 
+  }
+  catch(...) {
+    cout << " BRMultiFitsReader::run()/catched unknown ... exception " << endl;
+    setRC(4);	
+    return; 
+  }
+  setRC(0);
+  return;
+}
+
+/* --Methode-- */
+bool BRMultiFitsReader::ReadNextAllFibers()
+{
+  for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
+    if (ReadNext(fib)) return true;  // probleme 
+  }
+  if (!rdsamefc_ || (memgr_.NbFibres()<2))  { 
+    totsamefc_++;  return false;  // c'est OK 
+  }
+  uint_8 cfc=curfc_[0];
+  bool fgsamefc=true;
+  for(size_t fib=1; fib<memgr_.NbFibres(); fib++) {
+    if (curfc_[fib]!=cfc) {
+      fgsamefc=false;
+      if (curfc_[fib] > cfc)  cfc=curfc_[fib]; 
+    }
+  }
+  if (fgsamefc) { 
+    totsamefc_++;  return false;  // c'est OK  , same framecounter
+  }
+  else {  // else !fgsame
+    for(uint_4 fib=0; fib<memgr_.NbFibres(); fib++) {
+      while (curfc_[fib]<cfc) {
+	if (ReadNext(fib)) return true;  // probleme 
+      }
+    }
+  }   // fin de  else !fgsame 
+  totsamefc_++; 
+  return false; // c'est OK
+}
+
+/* --Methode-- */
+bool BRMultiFitsReader::ReadNext(int fib)
+{
+  if (!mff_[fib].IsOpen())  return true;
+  bool fggood=false;
+  while(!fggood) { 
+    if (vfpos_[fib] >= mff_[fib].NAxis2()) {
+      mff_[fib].Close();
+      vfilenum_[fib]++;
+      if (vfilenum_[fib]>imax_)  return true;
+      char flnm[1024];
+      sprintf(flnm,"%s/signal%d.fits",dirs_[fib].c_str(),vfilenum_[fib]++);
+      if (prtlev_ > 0)
+	cout << " BRMultiFitsReader::ReadNext() opening" << flnm << endl;
+      mff_[fib].Open(flnm, MF_Read);
+      if (mff_[fib].NAxis1() != packsize_)  {
+	cout << " BRMultiFitsReader::ReadNext(fib=" << fib << " File=" << flnm <<
+	  " NAxis1()= " << mff_[fib].NAxis1() << " <> PaqSize()=" << packsize_ << endl;
+	throw BAORadioException("BRMultiFitsReader::ReadNext()/ mff.NAxis1() != memgr_.PaqSize() "); 
+      }
+      vfpos_[fib]=0;
+    }
+    mff_[fib].ReadB(vpaq_[fib].Begin(), packsize_, vfpos_[fib]*packsize_);
+    vfpos_[fib]++;
+    totnbytesrd_+=packsize_;
+    totnpqrd_[fib]++;
+    fggood = vpchk_[fib].Check(vpaq_[fib],curfc_[fib]);
+  }
+  totnpqok_[fib]++;
+  return false;
+}
+
+/* --Methode-- */
+bool BRMultiFitsReader::MZoneManage(bool fgclean)    // Retourne true si probleme 
+{
+  /* Pour debug 
+  cout << " BRMultiFitsReader::MZoneManage() mid_=" << mid_ << " arg_npaq_= " << targ_npaq_  
+       << " max_targ_npaq=" << max_targ_npaq << endl;
+  */
+  if (mid_ >= 0)  { 
+    if (fgclean) memgr_.FreeMemZone(mid_, MemZS_Free);    
+    memgr_.FreeMemZone(mid_, MemZS_Filled);
+  }
+  mmbuf_ = NULL;  targ_npaq_ = 0;  mid_ = -2;
+  for (int fib=0;fib<(int)memgr_.NbFibres() ;fib++) mmbufib_[fib]=NULL;
+  if (fgclean)  return false;
+  mid_ = memgr_.FindMemZoneId(MemZA_Fill);
+  mmbuf_ = memgr_.GetMemZone(mid_);
+  if (mmbuf_==NULL)   return true;
+  for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)
+    mmbufib_[fib]=memgr_.GetMemZone(mid_,fib);
+  return false;
+}
+
 //-------------------------------------------------------
-// Classe thread de sauvegarde sur fichiers
+// Classe thread de lecture de fichiers fits BAORadio 
 //-------------------------------------------------------
 
+/* --Methode-- */
 BRFitsReader::BRFitsReader(RAcqMemZoneMgr& mem, vector<string>& infiles, bool fgnotrl)
   :  memgr(mem), infiles_(infiles), fgnotrl_(fgnotrl)
 {
 }
-void BRFitsReader::Stop()
-{
-  // cout<< " BRFitsReader:Stop ........ " << endl;
-  stop_=true;
-}
+
+/* --Methode-- */
 void BRFitsReader::run()
 {
