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

Last change on this file since 3914 was 3909, checked in by ansari, 15 years ago

1/ Mise en place d'informations auxiliaires au niveau de RAcqMemZoneMgr ,

FillTime (DATEOBS) pour chaque zone et identificateur des fibres

2/ remplissage de FillTime(DATEOBS) Et FiberId's de MemZoneMgr par

les classes PCIEMultiReader et EthernetReader

3/ Ajout de mots-cle (ds les datacards) pour numeros d'identification des

fibres et parametres de controles du DMA (@fiberids @waitenddma)
et corrections/ameliorations des modes acq (raw1c ordfft2c ...)

4/ Parametrage des WaitEndDMA ds PCIEWrapperInterface et implementation

au niveau de DMAMgrV6 (dmamgrv6.h .cc)

5/ Gestion d'echec DMA -> arret acquisition ds PCIEMultiReader,EthernetReader

modification TimeOut DMA entre le premier DMA et les suivants pour permettre
un arret propre rapide

6/ Adaptation de la classe BRVisibilityCalculator pour utilisation

FillTime (DATEOBS) et FiberIds de MemZoneMgr - amelioration de
l'identification des numeros de canaux pour les matrices de visibilites

7/ Petites ameliorations (ecriture mot-cles) pour MultiDataSaver et

amelioration classe BRMultiFitsReader pour lecture FillTime (DATEOBS)
et FiberIds depuis les fichiers FITS et remplissage de RAcqMemZoneMgr

Reza, 21/11/2010

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