| [3635] | 1 | //----------------------------------------------------------------
 | 
|---|
 | 2 | // ---- classes de threads pour lecture fichiers minifits
 | 
|---|
 | 3 | // LAL -      R. Ansari - Mai 2009 
 | 
|---|
 | 4 | //----------------------------------------------------------------
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | 
 | 
|---|
 | 7 | #include "brfitsrd.h"
 | 
|---|
 | 8 | 
 | 
|---|
 | 9 | #include <stdlib.h>
 | 
|---|
 | 10 | #include <unistd.h>
 | 
|---|
 | 11 | #include <fstream>
 | 
|---|
 | 12 | #include <signal.h>
 | 
|---|
 | 13 | #include "pexceptions.h"
 | 
|---|
 | 14 | #include "timestamp.h"
 | 
|---|
 | 15 | #include "ctimer.h"
 | 
|---|
 | 16 | 
 | 
|---|
 | 17 | #include "brpaqu.h"
 | 
|---|
 | 18 | #include "minifits.h"
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "resusage.h" // Pour mesure temps elapsed/CPU ...
 | 
|---|
 | 21 | #include "datatable.h" // Pour sauver les entetes de paquet 
 | 
|---|
 | 22 | #include <sys/time.h>  // pour gettimeofday
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | //-------------------------------------------------------
 | 
|---|
 | 25 | // Classe thread de sauvegarde sur fichiers
 | 
|---|
 | 26 | //-------------------------------------------------------
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | BRFitsReader::BRFitsReader(RAcqMemZoneMgr& mem, vector<string>& infiles, bool fgnotrl)
 | 
|---|
 | 29 |   :  memgr(mem), infiles_(infiles), fgnotrl_(fgnotrl)
 | 
|---|
 | 30 | {
 | 
|---|
 | 31 | }
 | 
|---|
 | 32 | void BRFitsReader::Stop()
 | 
|---|
 | 33 | {
 | 
|---|
 | 34 |   // cout<< " BRFitsReader:Stop ........ " << endl;
 | 
|---|
 | 35 |   stop_=true;
 | 
|---|
 | 36 | }
 | 
|---|
 | 37 | void BRFitsReader::run()
 | 
|---|
 | 38 | {
 | 
|---|
 | 39 |   setRC(1);     
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 |   try {
 | 
|---|
 | 42 |     TimeStamp ts; 
 | 
|---|
| [3640] | 43 |     Timer tm("BRFitsReader", false);
 | 
|---|
| [3646] | 44 |     BRPaqChecker pcheck(!fgnotrl_);  // Verification/comptage des paquets 
 | 
|---|
| [3640] | 45 |     
 | 
|---|
| [3635] | 46 |     size_t totnbytesrd = 0;
 | 
|---|
 | 47 |     cout << " BRFitsReader::run() - Starting " << ts << " NbFiles=" << infiles_.size() 
 | 
|---|
 | 48 |          << "  memgr.PaqSize() = " << memgr.PaqSize() << endl;
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 |     uint_4  nfileok = 0; 
 | 
|---|
 | 51 |     uint_8 nbytesrd = 0;  
 | 
|---|
| [3640] | 52 |     /*  Variables pour la logique des zones memoire et numeros de paquets dans la zone memoire */
 | 
|---|
 | 53 |     int mid = -2;
 | 
|---|
 | 54 |     Byte* buff = NULL;  
 | 
|---|
 | 55 |     int kmp = 0;   
 | 
|---|
 | 56 |     int kmpmax=memgr.NbPaquets(); 
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 |     int paqsz = 0;
 | 
|---|
| [3635] | 59 |     for(int ifile=0; ifile<infiles_.size(); ifile++) {
 | 
|---|
 | 60 |       string ffname = infiles_[ifile];
 | 
|---|
 | 61 | // -------------- Lecture de bytes      
 | 
|---|
 | 62 |       cout << "BRFitsReader::run() [" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
 | 
|---|
 | 63 |       MiniFITSFile mff(ffname, MF_Read);
 | 
|---|
 | 64 |       cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1() 
 | 
|---|
 | 65 |            << " NAxis2=" << mff.NAxis2() << endl;
 | 
|---|
 | 66 |       if (mff.DataType() != MF_Byte) {
 | 
|---|
 | 67 |         cout << "BRFitsReader::run() PB : DataType!=MF_Byte --> skipping " << endl;
 | 
|---|
 | 68 |         continue;
 | 
|---|
 | 69 |       }
 | 
|---|
 | 70 | // Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) si fgnotrl=true
 | 
|---|
 | 71 |       int incpaqsz=0;
 | 
|---|
 | 72 |       if (fgnotrl_) {
 | 
|---|
 | 73 |         incpaqsz=16;
 | 
|---|
| [3646] | 74 |         if (ifile==0) cout << " Warning : FITS files without frame trailers ..." << endl;
 | 
|---|
| [3635] | 75 |       }
 | 
|---|
| [3640] | 76 |       if (paqsz == 0)  {  // premier passage, on fixe la taille de paquet et on verifie compatibilite avec memgr
 | 
|---|
| [3635] | 77 |         paqsz = mff.NAxis1()+incpaqsz;
 | 
|---|
 | 78 |         if (paqsz != memgr.PaqSize()) {
 | 
|---|
 | 79 |          cout << "BRFitsReader::run() mff.NAxis1() incompatible with memgr.PaqSize() -> exception " << endl;
 | 
|---|
 | 80 |          throw SzMismatchError(" fits file size incompatible with memgr.PaqSize()"); 
 | 
|---|
 | 81 |         }
 | 
|---|
 | 82 |       }
 | 
|---|
 | 83 |       else {
 | 
|---|
 | 84 |         if (paqsz != mff.NAxis1()+incpaqsz) {
 | 
|---|
 | 85 |         cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+" << incpaqsz << " --> skipping " << endl;
 | 
|---|
 | 86 |         continue;
 | 
|---|
 | 87 |         }
 | 
|---|
| [3640] | 88 |       }   
 | 
|---|
 | 89 |       if (mid < 0) {
 | 
|---|
 | 90 |         mid = memgr.FindMemZoneId(MemZA_Fill);  
 | 
|---|
 | 91 |         buff = memgr.GetMemZone(mid);
 | 
|---|
 | 92 |         if (buff == NULL) {
 | 
|---|
 | 93 |               cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
 | 
|---|
 | 94 |               setRC(2); 
 | 
|---|
 | 95 |                return;  
 | 
|---|
 | 96 |          }
 | 
|---|
 | 97 |          kmp=0;
 | 
|---|
| [3635] | 98 |       }
 | 
|---|
 | 99 |       size_t sx = mff.NAxis1(); 
 | 
|---|
| [3640] | 100 |       size_t sy = mff.NAxis2();      
 | 
|---|
 | 101 |       int nprt=0;
 | 
|---|
 | 102 |       for(int j=0; j<sy; j++) {
 | 
|---|
 | 103 |         mff.ReadB(buff+kmp*paqsz, sx, j*sx);
 | 
|---|
 | 104 |         BRPaquet paq(NULL, buff+kmp*paqsz, paqsz);
 | 
|---|
 | 105 |         bool pqok = pcheck.Check(paq);   // Verification du paquet / FrameCounter
 | 
|---|
 | 106 |         if (!pqok && (nprt < 10)) {
 | 
|---|
 | 107 |           cout << "--BUG-- i=" << ifile << " mid=" << mid << " j=" << j << " kmp=" << kmp 
 | 
|---|
 | 108 |                << " paqsz=" << paqsz << endl;
 | 
|---|
 | 109 |           nprt++; 
 | 
|---|
 | 110 |           paq.Print();  
 | 
|---|
 | 111 |         }
 | 
|---|
 | 112 |         kmp++;
 | 
|---|
 | 113 |         if (kmp >= kmpmax)  {   // Zone memoire rempli !
 | 
|---|
 | 114 |           memgr.FreeMemZone(mid, MemZS_Filled);
 | 
|---|
 | 115 |               mid = -2;
 | 
|---|
 | 116 |           if (j<sy) { 
 | 
|---|
 | 117 |             mid = memgr.FindMemZoneId(MemZA_Fill);  
 | 
|---|
 | 118 |             buff = memgr.GetMemZone(mid);
 | 
|---|
 | 119 |             if (buff == NULL) {
 | 
|---|
 | 120 |                   cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
 | 
|---|
 | 121 |                   setRC(2);     
 | 
|---|
 | 122 |                    return;      
 | 
|---|
 | 123 |              }
 | 
|---|
 | 124 |              kmp=0;
 | 
|---|
 | 125 |           }
 | 
|---|
 | 126 |         } 
 | 
|---|
| [3635] | 127 |       }
 | 
|---|
 | 128 |       nfileok++;   
 | 
|---|
 | 129 |       size_t nbytesrd = sx*sy;
 | 
|---|
 | 130 |       totnbytesrd += nbytesrd;
 | 
|---|
| [3640] | 131 |     }   // Fin de la boucle sur les fichiers
 | 
|---|
 | 132 | // Gestion d'une zone partiellement remplie
 | 
|---|
 | 133 |     if (mid>=0) {
 | 
|---|
 | 134 |       for(int k=kmp;k<kmpmax;k++) {
 | 
|---|
 | 135 |         Byte* bp=buff+k*paqsz;
 | 
|---|
 | 136 |         for(int l=0;l<paqsz;l++) bp[l]=0;
 | 
|---|
 | 137 |       }
 | 
|---|
| [3635] | 138 |       memgr.FreeMemZone(mid, MemZS_Filled);
 | 
|---|
| [3640] | 139 |     }
 | 
|---|
| [3635] | 140 | 
 | 
|---|
 | 141 | //    sprintf(fname,"%s/.log",path_.c_str());
 | 
|---|
 | 142 | //    ofstream filog(fname);
 | 
|---|
 | 143 | //    filog << " DataProc::run() - starting log file " << ts << endl;                  
 | 
|---|
 | 144 | //    filog << " NbFiles=" << nfiles_ << " NBloc/File="  << nblocperfile_ << " NMaxMemZones=" << nmax_ << endl;
 | 
|---|
 | 145 |         
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 |     cout << " ------------------  BRFitsReader::run() END ----------------- " << endl;
 | 
|---|
 | 149 |     ts.SetNow();
 | 
|---|
| [3640] | 150 |     tm.SplitQ();
 | 
|---|
| [3635] | 151 |     cout << "  END reading " << ts << " NFileOK=" << nfileok << endl; 
 | 
|---|
 | 152 |     cout << "  TotalDiskRead= " << totnbytesrd/(1024*1024) << " MBytes Disk-Read rate= " 
 | 
|---|
 | 153 |          << (double)(totnbytesrd)/1024./tm.PartialElapsedTimems() << " MB/s" << endl;    
 | 
|---|
 | 154 |     pcheck.Print(cout);
 | 
|---|
| [3640] | 155 |     cout << " BRFitsReader::run()/Timing: \n"; 
 | 
|---|
 | 156 |     tm.Print();
 | 
|---|
| [3635] | 157 |     cout << " ---------------------------------------------------------- " << endl;
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 |   }  // Fin du bloc try 
 | 
|---|
 | 160 |   catch (MiniFITSException& exc) {
 | 
|---|
 | 161 |     cout << " BRFitsReader::run()/catched MiniFITSException " << exc.Msg() << endl;
 | 
|---|
 | 162 |     setRC(3);   
 | 
|---|
 | 163 |     return; 
 | 
|---|
 | 164 |   }
 | 
|---|
 | 165 |   catch(...) {
 | 
|---|
 | 166 |     cout << " BRFitsReader::run()/catched unknown ... exception " << endl;
 | 
|---|
 | 167 |     setRC(4);   
 | 
|---|
 | 168 |     return; 
 | 
|---|
 | 169 |   }
 | 
|---|
 | 170 |   setRC(0);
 | 
|---|
 | 171 |   return;
 | 
|---|
 | 172 | }   
 | 
|---|