source: Sophya/trunk/AddOn/TAcq/brfitsrd.cc@ 3776

Last change on this file since 3776 was 3700, checked in by ansari, 16 years ago

Correction des paquets perdus lor s de la lecture (saut de 2 des numeros de fichier), Reza 4/12/09

File size: 12.5 KB
RevLine 
[3635]1//----------------------------------------------------------------
[3683]2// Projet BAORadio - (C) LAL/IRFU 2008-2010
3// Classes de threads pour lecture fichiers fits BAORadio
[3635]4//----------------------------------------------------------------
5
6
7#include "brfitsrd.h"
8
9#include <stdlib.h>
10#include <unistd.h>
11#include <fstream>
[3683]12#include <exception>
13
[3635]14#include "pexceptions.h"
15#include "timestamp.h"
16#include "ctimer.h"
17
18#include "brpaqu.h"
19
20#include "resusage.h" // Pour mesure temps elapsed/CPU ...
21#include <sys/time.h> // pour gettimeofday
22
[3683]23using namespace SOPHYA;
24
25//---------------------------------------------------------------------
26// Classe thread de lecture de Multi-fibres de fichiers FITS BAORadio
27//---------------------------------------------------------------------
28
29/* --Methode-- */
30BRMultiFitsReader::BRMultiFitsReader(RAcqMemZoneMgr& mem, vector<string>& dirs, bool rdsamefc,
31 uint_4 imin, uint_4 imax, uint_4 istep)
[3697]32 : memgr_(mem), dirs_(dirs), stop_(false), rdsamefc_(rdsamefc), imin_(imin), imax_(imax), istep_(istep)
[3683]33{
34 SetPrintLevel();
35 totnbytesrd_ = 0;
36 totsamefc_ = 0;
37 if (memgr_.NbFibres() > MAXANAFIB)
38 throw BAORadioException("BRMultiFitsReader::BRMultiFitsReader/ NbFibres>MAXANAFIB ");
39 if (dirs_.size() != memgr_.NbFibres())
40 throw BAORadioException("BRMultiFitsReader::BRMultiFitsReader/ NbFibres != Nb Data Directories");
41
42 packsize_=memgr_.PaqSize();
43 mid_=-2;
44 mmbuf_=NULL;
45 max_targ_npaq = memgr_.NbPaquets();
46 for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) mmbufib_[fib]=NULL;
47
48 char flnm[1024];
49 for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
50 sprintf(flnm,"%s/signal%d.fits",dirs_[fib].c_str(),imin_);
51 mff_[fib].Open(flnm, MF_Read);
52 if (mff_[fib].NAxis1() != memgr_.PaqSize()) {
53 cout << " BRMultiFitsReader::BRMultiFitsReader/ fib=" << fib << " File=" << flnm <<
54 " NAxis1()= " << mff_[fib].NAxis1() << " <> PaqSize()=" << memgr_.PaqSize() << endl;
55 throw BAORadioException("BRMultiFitsReader::BRMultiFitsReader/ mff.NAxis1() != memgr_.PaqSize() ");
56 }
57 vfilenum_.push_back(imin_);
58 vfpos_.push_back(0);
59 vpaq_.push_back(BRPaquet(NULL,memgr_.PaqSize()));
60 vpchk_.push_back(BRPaqChecker(true,0));
61 curfc_.push_back(0);
62 totnpqrd_.push_back(0);
63 totnpqok_.push_back(0);
64 }
65}
66
67
68/* --Methode-- */
69void BRMultiFitsReader::run()
70{
71 setRC(1);
72 try {
73 TimeStamp ts;
74 Timer tm("BRMultiFitsReader", false);
75 cout << " BRMultiFitsReader::run() - Starting " << ts << " NbFibres()=" << memgr_.NbFibres()
76 << " PaqSize() = " << memgr_.PaqSize() << endl;
77 cout << " ...ReadMode: " << ((rdsamefc_)?"Paquets With SameFrameCounter":"All OK paquets")
78 << " signalII.fits IMin=" << imin_ << " IMax=" << imax_ << " IStep=" << istep_ << endl;
79
80 Byte* nextpaq=NULL;
81 bool fgok=true;
82 while (fgok) {
83 if (stop_) break;
84 if ( MoveToNextTarget() ) {
85 cout << "BRMultiFitsReader::run()/Error-A- MoveToNextTarget() returned true ->STOP 9" << endl;
86 setRC(7); fgok=false; break;
87 }
88 for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
89 nextpaq=GetPaquetTarget(fib);
90 if (nextpaq == NULL) { // Cela ne devrait pas arriver
91 cout << "BRMultiFitsReader::run()/Error-A2- GetPaquetTarget(fib) returned NULL ->STOP 9" << endl;
92 setRC(9); fgok=false; break;
93 }
94 vpaq_[fib].Set(nextpaq);
95 }
96 if (ReadNextAllFibers()) { fgok=false; break; }
97 }
98
99 MoveToNextTarget(); // Pour faire traiter le dernier paquet si plein
100 MZoneManage(true); // Nettoyage final
101 usleep(50000); // Attente de traitement du dernier paquet
102 memgr_.Stop(); // Arret
103
104 cout << " ------------------ BRMultiFitsReader::run() END ----------------- " << endl;
105 ts.SetNow();
106 tm.SplitQ();
107 cout << " END reading : " << ts ;
108 if (rdsamefc_) cout << " NSameFC=" << totsamefc_ << endl;
109 else cout << endl;
110 for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
111 int perc=0;
112 if (totnpqrd_[fib]>0) perc=100*totsamefc_/totnpqrd_[fib];
113 cout << " Fiber" << fib << " TotNPaqRd=" << totnpqrd_[fib] << " TotNPaqOK=" << totnpqok_[fib]
114 << " FracSameFC=" << perc << " %" << endl;
[3699]115 if (prtlev_ > 0) vpchk_[fib].Print(cout);
[3683]116 }
117 cout << " TotalDiskRead= " << totnbytesrd_/(1024*1024) << " MBytes Disk-Read rate= "
118 << (double)(totnbytesrd_)/1024./tm.PartialElapsedTimems() << " MB/s" << endl;
119 cout << " BRMultiFitsReader::run()/Timing: \n";
120 tm.Print();
121 cout << " ---------------------------------------------------------- " << endl;
122
123 } // Fin du bloc try
124 catch (std::exception& exc) {
125 cout << " BRMultiFitsReader::run()/catched execption msg= " << exc.what() << endl;
126 setRC(3);
127 return;
128 }
129 catch(...) {
130 cout << " BRMultiFitsReader::run()/catched unknown ... exception " << endl;
131 setRC(4);
132 return;
133 }
134 setRC(0);
135 return;
136}
137
138/* --Methode-- */
139bool BRMultiFitsReader::ReadNextAllFibers()
140{
141 for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
142 if (ReadNext(fib)) return true; // probleme
143 }
144 if (!rdsamefc_ || (memgr_.NbFibres()<2)) {
145 totsamefc_++; return false; // c'est OK
146 }
147 uint_8 cfc=curfc_[0];
148 bool fgsamefc=true;
149 for(size_t fib=1; fib<memgr_.NbFibres(); fib++) {
150 if (curfc_[fib]!=cfc) {
151 fgsamefc=false;
152 if (curfc_[fib] > cfc) cfc=curfc_[fib];
153 }
154 }
155 if (fgsamefc) {
156 totsamefc_++; return false; // c'est OK , same framecounter
157 }
158 else { // else !fgsame
159 for(uint_4 fib=0; fib<memgr_.NbFibres(); fib++) {
160 while (curfc_[fib]<cfc) {
161 if (ReadNext(fib)) return true; // probleme
162 }
163 }
164 } // fin de else !fgsame
165 totsamefc_++;
166 return false; // c'est OK
167}
168
169/* --Methode-- */
170bool BRMultiFitsReader::ReadNext(int fib)
171{
172 if (!mff_[fib].IsOpen()) return true;
173 bool fggood=false;
174 while(!fggood) {
175 if (vfpos_[fib] >= mff_[fib].NAxis2()) {
176 mff_[fib].Close();
177 vfilenum_[fib]++;
178 if (vfilenum_[fib]>imax_) return true;
179 char flnm[1024];
[3700]180 sprintf(flnm,"%s/signal%d.fits",dirs_[fib].c_str(),vfilenum_[fib]);
[3699]181 if (prtlev_ > 1)
[3683]182 cout << " BRMultiFitsReader::ReadNext() opening" << flnm << endl;
183 mff_[fib].Open(flnm, MF_Read);
184 if (mff_[fib].NAxis1() != packsize_) {
185 cout << " BRMultiFitsReader::ReadNext(fib=" << fib << " File=" << flnm <<
186 " NAxis1()= " << mff_[fib].NAxis1() << " <> PaqSize()=" << packsize_ << endl;
187 throw BAORadioException("BRMultiFitsReader::ReadNext()/ mff.NAxis1() != memgr_.PaqSize() ");
188 }
189 vfpos_[fib]=0;
190 }
191 mff_[fib].ReadB(vpaq_[fib].Begin(), packsize_, vfpos_[fib]*packsize_);
192 vfpos_[fib]++;
193 totnbytesrd_+=packsize_;
194 totnpqrd_[fib]++;
195 fggood = vpchk_[fib].Check(vpaq_[fib],curfc_[fib]);
196 }
197 totnpqok_[fib]++;
198 return false;
199}
200
201/* --Methode-- */
202bool BRMultiFitsReader::MZoneManage(bool fgclean) // Retourne true si probleme
203{
204 /* Pour debug
205 cout << " BRMultiFitsReader::MZoneManage() mid_=" << mid_ << " arg_npaq_= " << targ_npaq_
206 << " max_targ_npaq=" << max_targ_npaq << endl;
207 */
208 if (mid_ >= 0) {
209 if (fgclean) memgr_.FreeMemZone(mid_, MemZS_Free);
[3686]210 else memgr_.FreeMemZone(mid_, MemZS_Filled);
[3683]211 }
212 mmbuf_ = NULL; targ_npaq_ = 0; mid_ = -2;
213 for (int fib=0;fib<(int)memgr_.NbFibres() ;fib++) mmbufib_[fib]=NULL;
214 if (fgclean) return false;
215 mid_ = memgr_.FindMemZoneId(MemZA_Fill);
216 mmbuf_ = memgr_.GetMemZone(mid_);
217 if (mmbuf_==NULL) return true;
218 for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)
219 mmbufib_[fib]=memgr_.GetMemZone(mid_,fib);
220 return false;
221}
222
[3635]223//-------------------------------------------------------
[3683]224// Classe thread de lecture de fichiers fits BAORadio
[3635]225//-------------------------------------------------------
226
[3683]227/* --Methode-- */
[3635]228BRFitsReader::BRFitsReader(RAcqMemZoneMgr& mem, vector<string>& infiles, bool fgnotrl)
229 : memgr(mem), infiles_(infiles), fgnotrl_(fgnotrl)
230{
231}
[3683]232
233/* --Methode-- */
[3635]234void BRFitsReader::run()
235{
236 setRC(1);
237
238 try {
239 TimeStamp ts;
[3640]240 Timer tm("BRFitsReader", false);
[3646]241 BRPaqChecker pcheck(!fgnotrl_); // Verification/comptage des paquets
[3640]242
[3635]243 size_t totnbytesrd = 0;
244 cout << " BRFitsReader::run() - Starting " << ts << " NbFiles=" << infiles_.size()
245 << " memgr.PaqSize() = " << memgr.PaqSize() << endl;
246
247 uint_4 nfileok = 0;
248 uint_8 nbytesrd = 0;
[3640]249 /* Variables pour la logique des zones memoire et numeros de paquets dans la zone memoire */
250 int mid = -2;
251 Byte* buff = NULL;
252 int kmp = 0;
253 int kmpmax=memgr.NbPaquets();
254
255 int paqsz = 0;
[3635]256 for(int ifile=0; ifile<infiles_.size(); ifile++) {
257 string ffname = infiles_[ifile];
258// -------------- Lecture de bytes
259 cout << "BRFitsReader::run() [" << ifile <<"]Ouverture/lecture fichier " << ffname << endl;
260 MiniFITSFile mff(ffname, MF_Read);
261 cout << "... Type=" << mff.DataTypeToString() << " NAxis1=" << mff.NAxis1()
262 << " NAxis2=" << mff.NAxis2() << endl;
263 if (mff.DataType() != MF_Byte) {
264 cout << "BRFitsReader::run() PB : DataType!=MF_Byte --> skipping " << endl;
265 continue;
266 }
267// Les fichier FITS contiennent l'entet (24 bytes), mais pas le trailer (16 bytes) si fgnotrl=true
268 int incpaqsz=0;
269 if (fgnotrl_) {
270 incpaqsz=16;
[3646]271 if (ifile==0) cout << " Warning : FITS files without frame trailers ..." << endl;
[3635]272 }
[3640]273 if (paqsz == 0) { // premier passage, on fixe la taille de paquet et on verifie compatibilite avec memgr
[3635]274 paqsz = mff.NAxis1()+incpaqsz;
275 if (paqsz != memgr.PaqSize()) {
276 cout << "BRFitsReader::run() mff.NAxis1() incompatible with memgr.PaqSize() -> exception " << endl;
277 throw SzMismatchError(" fits file size incompatible with memgr.PaqSize()");
278 }
279 }
280 else {
281 if (paqsz != mff.NAxis1()+incpaqsz) {
282 cout << " PB : paqsz=" << paqsz << " != mff.NAxis1()+" << incpaqsz << " --> skipping " << endl;
283 continue;
284 }
[3640]285 }
286 if (mid < 0) {
287 mid = memgr.FindMemZoneId(MemZA_Fill);
288 buff = memgr.GetMemZone(mid);
289 if (buff == NULL) {
290 cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
291 setRC(2);
292 return;
293 }
294 kmp=0;
[3635]295 }
296 size_t sx = mff.NAxis1();
[3640]297 size_t sy = mff.NAxis2();
298 int nprt=0;
299 for(int j=0; j<sy; j++) {
300 mff.ReadB(buff+kmp*paqsz, sx, j*sx);
301 BRPaquet paq(NULL, buff+kmp*paqsz, paqsz);
302 bool pqok = pcheck.Check(paq); // Verification du paquet / FrameCounter
303 if (!pqok && (nprt < 10)) {
304 cout << "--BUG-- i=" << ifile << " mid=" << mid << " j=" << j << " kmp=" << kmp
305 << " paqsz=" << paqsz << endl;
306 nprt++;
307 paq.Print();
308 }
309 kmp++;
310 if (kmp >= kmpmax) { // Zone memoire rempli !
311 memgr.FreeMemZone(mid, MemZS_Filled);
312 mid = -2;
313 if (j<sy) {
314 mid = memgr.FindMemZoneId(MemZA_Fill);
315 buff = memgr.GetMemZone(mid);
316 if (buff == NULL) {
317 cout << " BRFitsReader::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
318 setRC(2);
319 return;
320 }
321 kmp=0;
322 }
323 }
[3635]324 }
325 nfileok++;
326 size_t nbytesrd = sx*sy;
327 totnbytesrd += nbytesrd;
[3640]328 } // Fin de la boucle sur les fichiers
329// Gestion d'une zone partiellement remplie
330 if (mid>=0) {
331 for(int k=kmp;k<kmpmax;k++) {
332 Byte* bp=buff+k*paqsz;
333 for(int l=0;l<paqsz;l++) bp[l]=0;
334 }
[3635]335 memgr.FreeMemZone(mid, MemZS_Filled);
[3640]336 }
[3635]337
338// sprintf(fname,"%s/.log",path_.c_str());
339// ofstream filog(fname);
340// filog << " DataProc::run() - starting log file " << ts << endl;
341// filog << " NbFiles=" << nfiles_ << " NBloc/File=" << nblocperfile_ << " NMaxMemZones=" << nmax_ << endl;
342
343
344
345 cout << " ------------------ BRFitsReader::run() END ----------------- " << endl;
346 ts.SetNow();
[3640]347 tm.SplitQ();
[3635]348 cout << " END reading " << ts << " NFileOK=" << nfileok << endl;
349 cout << " TotalDiskRead= " << totnbytesrd/(1024*1024) << " MBytes Disk-Read rate= "
350 << (double)(totnbytesrd)/1024./tm.PartialElapsedTimems() << " MB/s" << endl;
351 pcheck.Print(cout);
[3640]352 cout << " BRFitsReader::run()/Timing: \n";
353 tm.Print();
[3635]354 cout << " ---------------------------------------------------------- " << endl;
355
356 } // Fin du bloc try
357 catch (MiniFITSException& exc) {
358 cout << " BRFitsReader::run()/catched MiniFITSException " << exc.Msg() << endl;
359 setRC(3);
360 return;
361 }
362 catch(...) {
363 cout << " BRFitsReader::run()/catched unknown ... exception " << endl;
364 setRC(4);
365 return;
366 }
367 setRC(0);
368 return;
369}
Note: See TracBrowser for help on using the repository browser.