Index: trunk/AddOn/TAcq/brfitsrd.cc
===================================================================
--- trunk/AddOn/TAcq/brfitsrd.cc	(revision 3635)
+++ trunk/AddOn/TAcq/brfitsrd.cc	(revision 3640)
@@ -38,9 +38,10 @@
 {
   setRC(1);	
-  BRPaqChecker pcheck;  // Verification/comptage des paquets 
 
   try {
     TimeStamp ts; 
-    Timer tm("BRFitsReader");
+    Timer tm("BRFitsReader", false);
+    BRPaqChecker pcheck(~fgnotrl_);  // Verification/comptage des paquets 
+    
     size_t totnbytesrd = 0;
     cout << " BRFitsReader::run() - Starting " << ts << " NbFiles=" << infiles_.size() 
@@ -49,4 +50,11 @@
     uint_4  nfileok = 0; 
     uint_8 nbytesrd = 0;  
+    /*  Variables pour la logique des zones memoire et numeros de paquets dans la zone memoire */
+    int mid = -2;
+    Byte* buff = NULL;  
+    int kmp = 0;   
+    int kmpmax=memgr.NbPaquets(); 
+
+    int paqsz = 0;
     for(int ifile=0; ifile<infiles_.size(); ifile++) {
       string ffname = infiles_[ifile];
@@ -62,18 +70,13 @@
 // Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) si fgnotrl=true
       int incpaqsz=0;
-      int paqsz = 0;
       if (fgnotrl_) {
         incpaqsz=16;
         cout << " Warning : FITS files without frame trailers ..." << endl;
       }
-      if (paqsz == 0)  {  // premier passage, on fixe la taille de paquet et on alloue le buffer
+      if (paqsz == 0)  {  // premier passage, on fixe la taille de paquet et on verifie compatibilite avec memgr
         paqsz = mff.NAxis1()+incpaqsz;
         if (paqsz != memgr.PaqSize()) {
          cout << "BRFitsReader::run() mff.NAxis1() incompatible with memgr.PaqSize() -> exception " << endl;
          throw SzMismatchError(" fits file size incompatible with memgr.PaqSize()"); 
-        }
-       if (mff.NAxis2() != memgr.NbPaquets())  {
-         cout << "BRFitsReader::run() mff.NAxis2() <> memgr.NbPaquets() -> exception " << endl;
-         throw SzMismatchError(" fits file NAxies2() <> memgr.NbPaquets()"); 
         }
       }
@@ -83,31 +86,56 @@
         continue;
         }
-        if (memgr.NbPaquets() != mff.NAxis2()) {
-        cout << " PB memgr.NbPaquets() != mff.NAxis2()  --> skipping " << endl;
-        continue;
-        }
-      }
-      int mid = memgr.FindMemZoneId(MemZA_Fill);  
-      Byte* buff = memgr.GetMemZone(mid);
-      if (buff == NULL) {
-	cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
-	setRC(2);	
-	return;	
+      }   
+      if (mid < 0) {
+        mid = memgr.FindMemZoneId(MemZA_Fill);  
+        buff = memgr.GetMemZone(mid);
+        if (buff == NULL) {
+	      cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
+	      setRC(2);	
+	       return;	
+         }
+         kmp=0;
       }
       size_t sx = mff.NAxis1(); 
-      size_t sy = mff.NAxis2();
-      
-      for(int j=0; j<mff.NAxis2(); j++) {
-        mff.ReadB(buff+j*paqsz, sx, j*sx);
-	BRPaquet paq(NULL, buff+j*paqsz, paqsz);
-	pcheck.Check(paq);   // Verification du paquet / FrameCounter 
-
+      size_t sy = mff.NAxis2();      
+      int nprt=0;
+      for(int j=0; j<sy; j++) {
+        mff.ReadB(buff+kmp*paqsz, sx, j*sx);
+        BRPaquet paq(NULL, buff+kmp*paqsz, paqsz);
+        bool pqok = pcheck.Check(paq);   // Verification du paquet / FrameCounter
+        if (!pqok && (nprt < 10)) {
+          cout << "--BUG-- i=" << ifile << " mid=" << mid << " j=" << j << " kmp=" << kmp 
+               << " paqsz=" << paqsz << endl;
+          nprt++; 
+          paq.Print();	
+        }
+        kmp++;
+        if (kmp >= kmpmax)  {   // Zone memoire rempli !
+          memgr.FreeMemZone(mid, MemZS_Filled);
+	      mid = -2;
+          if (j<sy) { 
+            mid = memgr.FindMemZoneId(MemZA_Fill);  
+            buff = memgr.GetMemZone(mid);
+            if (buff == NULL) {
+	          cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
+	          setRC(2);	
+	           return;	
+             }
+             kmp=0;
+          }
+        } 
       }
       nfileok++;   
       size_t nbytesrd = sx*sy;
       totnbytesrd += nbytesrd;
+    }   // Fin de la boucle sur les fichiers
+// Gestion d'une zone partiellement remplie
+    if (mid>=0) {
+      for(int k=kmp;k<kmpmax;k++) {
+      	Byte* bp=buff+k*paqsz;
+      	for(int l=0;l<paqsz;l++) bp[l]=0;
+      }
       memgr.FreeMemZone(mid, MemZS_Filled);
-    }   // Fin de la boucle sur les fichiers
-
+    }
 
 //    sprintf(fname,"%s/.log",path_.c_str());
@@ -120,9 +148,11 @@
     cout << " ------------------  BRFitsReader::run() END ----------------- " << endl;
     ts.SetNow();
-    tm.Split();
+    tm.SplitQ();
     cout << "  END reading " << ts << " NFileOK=" << nfileok << endl; 
     cout << "  TotalDiskRead= " << totnbytesrd/(1024*1024) << " MBytes Disk-Read rate= " 
          << (double)(totnbytesrd)/1024./tm.PartialElapsedTimems() << " MB/s" << endl;    
     pcheck.Print(cout);
+    cout << " BRFitsReader::run()/Timing: \n"; 
+    tm.Print();
     cout << " ---------------------------------------------------------- " << endl;
 
