source: Sophya/trunk/ArchTOIPipe/Kernel/fitstoirdr.cc@ 2337

Last change on this file since 2337 was 2194, checked in by aubourg, 23 years ago

bornes

File size: 15.1 KB
Line 
1// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
2// Eric Aubourg
3// Christophe Magneville
4// Reza Ansari
5// $Id: fitstoirdr.cc,v 1.42 2002-09-16 13:28:29 aubourg Exp $
6
7#include "fitstoirdr.h"
8#include "toimanager.h"
9#include <sched.h>
10
11
12FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
13 fname = fn;
14 allfn.push_back(fn);
15 Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
16 cout<<"FITSTOIReader::FITSTOIReader"<<endl;
17 cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
18 name = "rdr";
19 fptr = NULL;
20 totnscount = 0;
21
22 implicitSN = false;
23 implicitSNStart = 0;
24
25 // Variables rajoutee pour gerer les fichiers de flag de LevelS Reza 18/6/2002
26 sepFlagfile = false;
27 fptrflg = NULL;
28}
29
30FITSTOIReader::~FITSTOIReader() {
31}
32
33void FITSTOIReader::setImplicitSN(long snStart) {
34 implicitSN = true;
35 implicitSNStart = snStart;
36}
37
38void FITSTOIReader::setBufferSize(int buffsz)
39{
40 Buff_Sz = (buffsz>0) ? buffsz: 1024;
41 return;
42}
43
44pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
45void fits_lock();
46void fits_unlock();
47
48void fits_lock() {
49 pthread_mutex_lock(&fits_mutex);
50}
51void fits_unlock() {
52 pthread_mutex_unlock(&fits_mutex);
53}
54
55void FITSTOIReader::openFile(string fn) {
56 fits_lock();
57 if (fptr) {
58 fits_close_file(fptr,&fstatus);
59 fptr = NULL;
60 }
61 fname = fn;
62 cout << "FITSTOIReader::open FileName=" << fname << endl;
63 fstatus = 0;
64 // Open file
65 fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus);
66 if (fstatus != 0) {
67 fits_report_error(stderr, fstatus);
68 fits_unlock();
69 throw IOExc("FITSTOIReader::openFile() fitsio error");
70 }
71
72 // Go to first extension, which should be a BINTABLE
73
74 int simple, bitpix, naxis;
75 long naxes;
76 long pcount, gcount;
77 int extend;
78 fits_read_imghdr(fptr, 1, &simple, &bitpix,
79 &naxis, &naxes, &pcount, &gcount, &extend, &fstatus);
80
81 fits_movabs_hdu(fptr, 2, NULL, &fstatus);
82
83 fits_get_num_cols(fptr,&ncols,&fstatus);
84 fits_get_num_rows(fptr,&nrows,&fstatus);
85
86 cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
87 if (implicitSN) {
88 firstSn = implicitSNStart;
89 } else {
90 int anyNul;
91 double y;
92 fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus);
93 firstSn = (int) (y+.1);
94 }
95
96 // Ouverture fichier de flag separe de LevelS (Reza 18/6/2002)
97 if (sepFlagfile) {
98 cout << " FITSTOIReader::openFile() - Opening separate flag file " << sepFlagFileName
99 << " NColFlags= " << sepFlagCols.size() << endl;
100 fits_open_file(&fptrflg,sepFlagFileName.c_str(),READONLY,&fstatus);
101 if (fstatus != 0) {
102 fits_report_error(stderr, fstatus);
103 fits_unlock();
104 throw IOExc("FITSTOIReader::openFile() - sepFlagfile open fitsio error");
105 }
106 fits_movabs_hdu(fptrflg, 2, NULL, &fstatus);
107 if (fstatus != 0) {
108 fits_report_error(stderr, fstatus);
109 fits_unlock();
110 throw IOExc("FITSTOIReader::openFile() - sepFlagfile fits_movabs_hdu(2) fitsio error");
111 }
112 long nrowsflg = 0;
113 fits_get_num_rows(fptrflg,&nrowsflg,&fstatus);
114 if (nrows != nrowsflg) {
115 cerr << " FITSTOIReader::openFile()/Error: Different NRows in flag and data files!" << endl;
116 fits_unlock();
117 throw ParmError("FITSTOIReader::openFile() Different NRows in flag and data files");
118 }
119 int ncolsflg = 0;
120 fits_get_num_cols(fptrflg,&ncolsflg,&fstatus);
121 if (ncolsflg < sepFlagCols.size()) {
122 cerr << " FITSTOIReader::openFile()/Error: ncolsflg (=" << ncolsflg
123 << ") < sepFlagCols.size() (=" << sepFlagCols.size() << ")" << endl;
124 fits_unlock();
125 throw ParmError("FITSTOIReader::openFile() Different NRows in flag and data files");
126 }
127 }
128 //
129 fits_unlock();
130}
131
132void FITSTOIReader::init() {
133
134 // Modif pour fichiers de flag separe de LevelS (Reza 18/6/2002)
135 if (sepFlagfile && (allfn.size() > 1) ) {
136 cerr << "FITSTOIReader::init()/Error- Multiple files and separate flag file not allowed !"<<endl;
137 throw ParmError("FITSTOIReader::init() Multiple files and separate flag file not allowed");
138 }
139
140 openFile(allfn.front());
141
142 fits_lock();
143 // si pas implicitSN, la premiere colonne est le sampleNum.
144 // Sinon, le samplenum est la fitsline + offset.
145 int itoi=-1;
146 int col1 = implicitSN ? 0 : 1;
147 for (int i=col1; i<ncols; i++) {
148 char templt[10];
149 sprintf(templt, "%d", i+1);
150 char colname[200];
151 int colnum;
152 fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
153 cout << "FITSTOIReader col " << colname << endl;
154 // On verifie si c'est une colonne de flag
155 if (!strncmp(colname, "fg_", 3) || !strncmp(colname, "FLAG_", 5) ) {
156 colsinput[itoi].second=true;
157 } else {
158 declareOutput(colname);
159 itoi++;
160 colsinput[itoi] = pair<int,bool>(i,false);
161 }
162 }
163 fits_unlock();
164
165 snBegin = firstSn;
166 if (forcedMinIn > 0 && forcedMinIn > snBegin) {
167 snBegin = forcedMinIn;
168 }
169 openFile(allfn.back());
170 snEnd = firstSn+nrows-1;
171 if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
172 snEnd = forcedMaxIn;
173 }
174 /* init */
175 // snMin=snBegin; NNNNNOOOOOONNNNNN !!!!
176 // snMax=snEnd;
177 cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
178}
179
180int FITSTOIReader::calcMinOut() {
181 chkinit();
182 TOIManager* mgr = TOIManager::getManager();
183 int firstReq = mgr->getRequestedBegin();
184 return snBegin > firstReq ? snBegin : firstReq;
185}
186
187int FITSTOIReader::calcMaxOut() {
188 chkinit();
189 TOIManager* mgr = TOIManager::getManager();
190 int lastReq = mgr->getRequestedEnd();
191 return snEnd < lastReq ? snEnd : lastReq;
192}
193
194
195// ajout vf 31/07/2002
196bool FITSTOIReader::checkSampleLimits(long& min, long& max, int pass)
197{
198 bool sample_ok=true;
199 chkinit();
200 return TOIProcessor::checkSampleLimits(min, max, pass);
201 /*
202 cout << "check " << name << " in " << min << " - " << max << " ; " << snMin << " - " << snMax << endl;
203
204 // on verifie qu'on peut effectivement produire
205
206 if (min < snBegin) {
207 min = snBegin;
208 }
209
210 if (max > snEnd) {
211 max = snEnd;
212 }
213
214
215 // si intersection des intervals vide on leve une exception
216 //if (min >= snEnd || max <= snBegin) {
217 //cerr << " FITSTOIReader::checkSampleLimits(long& min long& max)/Error Sample out of bounds" << endl;
218 //throw ParmError(" FITSTOIReader::checkSampleLimits(long& min long& max) min >= snEnd || max <= snBegin");
219 //}
220
221 // ajustements si necessaires
222 if (min < snMin) {
223 snMin = min;
224 }
225 if (max > snMax) {
226 snMax = max;
227 }
228
229
230 min=min<snMin?snMin:min;
231 max=max>snMax?snMax:max;
232
233 // cas sans contraintes, on retourne nos bornes
234 if (min>max) {
235 min = snBegin;
236 max = snEnd;
237 }
238
239 cout << "check " << name << " out " << min << " - " << max << " ; " << snMin << " - " << snMax << endl;
240 //cout << "fitstoirdr : limites verifiees : " << snBegin << " , " << snEnd << " : " << sample_ok << endl;
241 return sample_ok;
242*/
243}
244
245void FITSTOIReader::calcSampleLimits(long& min, long& max)
246{
247 chkinit();
248 cout << "calc " << name << " in " << min << " - " << max << " ; " << snBegin << " - " << snEnd << endl;
249
250 min=min<snMin?snMin:min;
251 max=max>snMax?snMax:max;
252 snBegin=snMin;
253 snEnd=snMax;
254 cout << "calc " << name << " out " << min << " - " << max << " ; " << snBegin << " - " << snEnd << endl;
255 //cout << "fitstoirdr : limites temporaires calculees : " << snBegin << " , " << snEnd << endl;
256
257}
258// fin ajout vf
259
260
261
262void FITSTOIReader::addFile(string fn) {
263 allfn.push_back(fn);
264}
265
266void FITSTOIReader::setFlagFile(string fn, vector<FlagToiDef> flags)
267{
268 if (flags.size() < 1) {
269 cerr << " FITSTOIReader::setFlagFile()/Error flag.size() = 0 ! " << endl;
270 throw ParmError("FITSTOIReader::setFlagFile() flag.size() = 0");
271 }
272
273 sepFlagfile = true;
274 sepFlagFileName = fn;
275 sepFlagCols = flags;
276}
277
278void FITSTOIReader::run() {
279 for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
280 openFile(*i);
281 if (Buff_Sz > 1) run2(); // Lecture bufferise
282 else run1(); // Lecture un echantillon a la fois
283 }
284 fits_lock();
285 if (fptr) {
286 fits_close_file(fptr,&fstatus);
287 fptr = NULL;
288 }
289 if (sepFlagfile && fptrflg) {
290 fits_close_file(fptrflg,&fstatus);
291 fptrflg = NULL;
292 }
293 fits_unlock();
294}
295
296// run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
297// ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter
298// implicitSNStart et switvh run1/run2
299// suivant buffersize
300
301void FITSTOIReader::run1() {
302 // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
303 // mais cela implique de gerer aussi bien echant uniforme que non.
304 // On pourrait aussi lire plusieurs elements d'un coup.
305 int ncols = outIx.size();
306 cout << "FITSTOIReader::run1() reader reading... NRows=" << nrows << " firstSn= "
307 << firstSn << endl;
308
309 double* tabdata = new double[getNOut()];
310 uint_8* tabflag = new uint_8[getNOut()];
311
312 for (long i=0; i<nrows; i++) {
313 int anyNul;
314 double y;
315 fits_lock();
316 long sn;
317 if (implicitSN) {
318 fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
319 sn = (long) (y+.1);
320 }
321 else sn = implicitSNStart+i;
322 //fits_unlock();
323 TOIManager* mgr = TOIManager::getManager();
324 if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
325 if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
326 // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
327 int k;
328 for (k=0; k<getNOut(); k++) {
329 int j = colsinput[k].first;
330 if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
331 //fits_lock();
332 fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
333 tabdata[k] = y;
334 long flg = 0;
335 if (colsinput[k].second) {
336 fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
337 }
338
339 if (sepFlagfile) { // Ajout Reza (18/6/2002) pour fichier de flags separe
340 unsigned char sflg;
341 flg = 0;
342 for(int skf=0; skf<sepFlagCols.size(); skf++) {
343 fits_read_col_byt(fptrflg, skf+1, i+1,1,1,0,&sflg,&anyNul,&fstatus);
344 if (sflg) flg |= sepFlagCols[skf];
345 }
346 } // Fin modif pour fichier de flags separe (18/6/2002)
347
348 tabflag[k] = flg;
349 // fits_unlock();
350 //putData(k, sn, y, flg);
351 }
352 fits_unlock();
353 for (k=0; k<getNOut(); k++) {
354 putData(k, sn, tabdata[k], tabflag[k]);
355 }
356 /* if (i%50==0) {
357 // fits_unlock();
358 sched_yield();
359 //fits_lock();
360 }*/
361 totnscount++;
362 }
363 //fits_unlock();
364 delete[] tabflag;
365 delete[] tabdata;
366 cout << "reader done reading... " << pthread_self() << endl;
367}
368
369void FITSTOIReader::run2()
370// ---- Version bufferisee (Christophe 20/10/2001)
371{
372 int ncols = outIx.size();
373 cout<<"FITSTOIReader::run2() reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
374
375 //////// Prepare buffer, allocate memory
376 double *samplenum = new double[Buff_Sz];
377 vector<double*> colval;
378 vector<long*> colflg;
379 for(int k=0; k<getNOut(); k++) {
380 colval.push_back(NULL);
381 colflg.push_back(NULL);
382 if(!checkOutputTOIIndex(k)) continue;
383 colval[k] = new double[Buff_Sz];
384 // modif || sepFlagfile pour fichier de flag separe (Reza 18/6/2002)
385 if((colsinput[k].second) || sepFlagfile) colflg[k] = new long[Buff_Sz];
386 }
387 uint_8 * tmpflg = new uint_8[Buff_Sz];
388
389 // Ajout Reza (18/6/2002) pour fichier de flags separe
390 unsigned char* stmpflg = NULL;
391 if (sepFlagfile) stmpflg = new unsigned char[Buff_Sz];
392 // Fin modif pour fichier de flags separe (18/6/2002)
393
394
395 TOIManager* mgr = TOIManager::getManager();
396
397 //////// Read data and put into pipe
398 long ideb=-1,ifin=-1;
399 for(long i=0; i<nrows; i+=Buff_Sz) {
400 fits_lock(); // lock en debut de boucle de lecture - Reza 13/05/2002
401 // faut-il lire dans le fichier fits ?
402 if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
403 ideb = i;
404 ifin = ideb+Buff_Sz-1;
405 if(ifin>=nrows) ifin=nrows-1;
406 long n = ifin-ideb+1;
407 int anyNul;
408 if (implicitSN) {
409 if (ideb+implicitSNStart > snEnd
410 || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) {
411 fits_unlock(); // unlock avant de casser la boucle - Reza 16/05/2002
412 break;
413 }
414 if (ifin+implicitSNStart < snBegin
415 || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) {
416 fits_unlock(); // unlock avant de continuer - Reza 16/05/2002
417 continue;
418 }
419 for (long j=0; j<Buff_Sz; j++) {
420 samplenum[j] = j+ideb+implicitSNStart;
421 }
422 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
423 } else {
424 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
425 fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
426 if (samplenum[0] > snEnd
427 || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
428 fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
429 break;
430 }
431 if (samplenum[n-1] < snBegin
432 || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
433 fits_unlock();
434 continue;
435 }
436 }
437 for(int k=0; k<getNOut(); k++) {
438 if(colval[k]==NULL) continue;
439 int j = colsinput[k].first;
440 fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
441 if (fstatus !=0) {
442 cerr << " RzBug - j+1" << j+1 << " ideb= " << ideb << endl;
443 fits_report_error(stderr,fstatus);
444 }
445
446 //--- Ajout Reza (18/6/2002) pour fichier de flags separe
447 if (sepFlagfile) {
448 int sjj;
449 for(sjj=0; sjj<n; sjj++) colflg[k][sjj] = 0;
450 for(int skf=0; skf<sepFlagCols.size(); skf++) {
451 fits_read_col_byt(fptrflg, skf+1, ideb+1,1,n,0,stmpflg,&anyNul,&fstatus);
452 if (fstatus !=0) {
453 cerr << " RzBug2 - skf=" << skf << " ideb= " << ideb << endl;
454 fits_report_error(stderr,fstatus);
455 }
456 for(sjj=0; sjj<n; sjj++) {
457 if (stmpflg[sjj]) colflg[k][sjj] |= sepFlagCols[skf];
458 }
459 }
460 }
461 else if(colflg[k]!=NULL)
462 fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
463 //--- Fin modif pour fichier de flags separe (18/6/2002)
464 }
465 if(fstatus!=0) {
466 fits_report_error(stderr,fstatus);
467 fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
468 throw IOExc("FITSTOIReader::run2: Error Reading Fits file\n");
469 }
470 }
471 fits_unlock(); // unlock en fin de boucle de lecture - Reza 13/05/2002
472
473 long ip = i-ideb; // pointeurs dans les buffers
474 long sn = (long) (samplenum[ip]+.1);
475 if (ip == 0) {
476 for(int k=0; k<getNOut(); k++) {
477 if(colval[k]==NULL) continue;
478 if (colflg[k] != NULL) {
479 for (int ii=0; ii<Buff_Sz; ii++) {
480 tmpflg[ii] = colflg[k][ii];
481 }
482 putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
483 } else {
484 putData(k, sn, ifin-ideb+1, colval[k]);
485 }
486 }
487 }
488 totnscount += (ifin-ideb+1);
489 }
490
491 //////// des-allocate buffers
492 delete [] samplenum;
493 delete [] tmpflg;
494 if (sepFlagfile) delete [] stmpflg;
495 {for(int k=0; k<getNOut(); k++) {
496 if(colval[k]!=NULL) delete [] colval[k];
497 if(colflg[k]!=NULL) delete [] colflg[k];
498 }}
499 colval.resize(0); colflg.resize(0);
500
501 cout << "reader (buffered) done reading... " << pthread_self() << endl;
502
503}
504
505
506// affichage des limites
507void FITSTOIReader::printLimits()
508{
509 cout << "fitstoirdr " << name <<" : limites calculees : " << snBegin << " , " << snEnd << endl;
510}
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
Note: See TracBrowser for help on using the repository browser.