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

Last change on this file since 2058 was 2058, checked in by ansari, 23 years ago

Ajout methode FITSTOIReader::setFlagFile() pour fichiers flag separes
de Level2 - Reza 18/6/2002

File size: 12.2 KB
RevLine 
[1738]1// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
2// Eric Aubourg
3// Christophe Magneville
4// Reza Ansari
[2058]5// $Id: fitstoirdr.cc,v 1.30 2002-06-18 12:21:08 ansari Exp $
[1738]6
[1365]7#include "fitstoirdr.h"
8#include "toimanager.h"
[1696]9#include <sched.h>
[1365]10
11
[1717]12FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
[1365]13 fname = fn;
[1480]14 allfn.push_back(fn);
[1717]15 Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
16 cout<<"FITSTOIReader::FITSTOIReader"<<endl;
17 cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
[1365]18 name = "rdr";
[1480]19 fptr = NULL;
[1629]20 totnscount = 0;
[1725]21
22 implicitSN = false;
23 implicitSNStart = 0;
[2058]24
25 // Variables rajoutee pour gerer les fichiers de flag de LevelS Reza 18/6/2002
26 sepFlagfile = false;
27 fptrflg = NULL;
[1365]28}
29
30FITSTOIReader::~FITSTOIReader() {
31}
32
[1994]33void FITSTOIReader::setImplicitSN(long snStart) {
[1725]34 implicitSN = true;
35 implicitSNStart = snStart;
36}
37
[1994]38void FITSTOIReader::setBufferSize(int buffsz)
39{
40 Buff_Sz = (buffsz>0) ? buffsz: 1024;
41 return;
42}
43
[1365]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
[1480]55void FITSTOIReader::openFile(string fn) {
[1365]56 fits_lock();
[1480]57 if (fptr) {
58 fits_close_file(fptr,&fstatus);
59 fptr = NULL;
60 }
61 fname = fn;
62 cout << "FITSTOIReader::open FileName=" << fname << endl;
[1365]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();
[2058]69 throw IOExc("FITSTOIReader::openFile() fitsio error");
[1365]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
[1454]86 cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
[1725]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 }
[2058]95
96 // Ouverture fichier de flag separe de LevelS (Reza 18/6/2002)
97 if (sepFlagfile) {
98 fits_open_file(&fptrflg,sepFlagFileName.c_str(),READONLY,&fstatus);
99 if (fstatus != 0) {
100 fits_report_error(stderr, fstatus);
101 fits_unlock();
102 throw IOExc("FITSTOIReader::openFile() - sepFlagfile open fitsio error");
103 }
104 fits_movabs_hdu(fptrflg, 2, NULL, &fstatus);
105 if (fstatus != 0) {
106 fits_report_error(stderr, fstatus);
107 fits_unlock();
108 throw IOExc("FITSTOIReader::openFile() - sepFlagfile fits_movabs_hdu(2) fitsio error");
109 }
110 long nrowsflg;
111 fits_get_num_rows(fptrflg,&nrowsflg,&fstatus);
112 if (nrows != nrowsflg) {
113 cerr << " FITSTOIReader::openFile()/Error: Different NRows in flag and data files!" << endl;
114 fits_unlock();
115 throw ParmError("FITSTOIReader::openFile() Different NRows in flag and data files");
116 }
117 }
118 //
[1480]119 fits_unlock();
120}
[1365]121
[1480]122void FITSTOIReader::init() {
[2058]123
124 // Modif pour fichiers de flag separe de LevelS (Reza 18/6/2002)
125 if (sepFlagfile && (allfn.size() > 1) ) {
126 cerr << "FITSTOIReader::init()/Error- Multiple files and separate flag file not allowed !"<<endl;
127 throw ParmError("FITSTOIReader::init() Multiple files and separate flag file not allowed");
128 }
129
[1480]130 openFile(allfn.front());
131
132 fits_lock();
[1725]133 // si pas implicitSN, la premiere colonne est le sampleNum.
134 // Sinon, le samplenum est la fitsline + offset.
[1527]135 int itoi=-1;
[1725]136 int col1 = implicitSN ? 0 : 1;
137 for (int i=col1; i<ncols; i++) {
[1365]138 char templt[10];
139 sprintf(templt, "%d", i+1);
140 char colname[200];
141 int colnum;
142 fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
143 cout << "FITSTOIReader col " << colname << endl;
[2041]144 // On verifie si c'est une colonne de flag
145 if (!strncmp(colname, "fg_", 3) || !strncmp(colname, "FLAG_", 5) ) {
[1527]146 colsinput[itoi].second=true;
147 } else {
148 declareOutput(colname);
149 itoi++;
150 colsinput[itoi] = pair<int,bool>(i,false);
151 }
[1365]152 }
153 fits_unlock();
[1480]154
155 snBegin = firstSn;
[1800]156 if (forcedMinIn > 0 && forcedMinIn > snBegin) {
157 snBegin = forcedMinIn;
[1787]158 }
[1480]159 openFile(allfn.back());
160 snEnd = firstSn+nrows-1;
[1800]161 if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
162 snEnd = forcedMaxIn;
163 }
[1480]164 cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
[1365]165}
166
167int FITSTOIReader::calcMinOut() {
[1800]168 chkinit();
[1367]169 TOIManager* mgr = TOIManager::getManager();
170 int firstReq = mgr->getRequestedBegin();
[1480]171 return snBegin > firstReq ? snBegin : firstReq;
[1365]172}
173
174int FITSTOIReader::calcMaxOut() {
[1800]175 chkinit();
[1367]176 TOIManager* mgr = TOIManager::getManager();
177 int lastReq = mgr->getRequestedEnd();
[1480]178 return snEnd < lastReq ? snEnd : lastReq;
[1365]179}
180
[1480]181void FITSTOIReader::addFile(string fn) {
182 allfn.push_back(fn);
183}
[1365]184
[2058]185void FITSTOIReader::setFlagFile(string fn, vector<FlagToiDef> flags)
186{
187 if (flags.size() < 1) {
188 cerr << " FITSTOIReader::setFlagFile()/Error flag.size() = 0 ! " << endl;
189 throw ParmError("FITSTOIReader::setFlagFile() flag.size() = 0");
190 }
191
192 sepFlagfile = true;
193 sepFlagFileName = fn;
194 sepFlagCols = flags;
195}
196
[1365]197void FITSTOIReader::run() {
[1480]198 for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
199 openFile(*i);
[1994]200 if (Buff_Sz > 1) run2(); // Lecture bufferise
201 else run1(); // Lecture un echantillon a la fois
[1480]202 }
[2058]203 fits_lock();
204 if (fptr) {
205 fits_close_file(fptr,&fstatus);
206 fptr = NULL;
207 }
208 if (sepFlagfile && fptrflg) {
209 fits_close_file(fptrflg,&fstatus);
210 fptrflg = NULL;
211 }
212 fits_unlock();
[1480]213}
214
[1725]215// run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
[1994]216// ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter
217// implicitSNStart et switvh run1/run2
218// suivant buffersize
219
[1480]220void FITSTOIReader::run1() {
[1365]221 // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
222 // mais cela implique de gerer aussi bien echant uniforme que non.
223 // On pourrait aussi lire plusieurs elements d'un coup.
224 int ncols = outIx.size();
[1454]225 cout << "reader reading... NRows=" << nrows << " firstSn= "
226 << firstSn << endl;
[1629]227
[1721]228 double* tabdata = new double[getNOut()];
229 uint_8* tabflag = new uint_8[getNOut()];
[1712]230
[1721]231 for (long i=0; i<nrows; i++) {
[1365]232 int anyNul;
233 double y;
[1710]234 fits_lock();
[1994]235 long sn;
236 if (implicitSN) {
237 fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
238 sn = (long) (y+.1);
239 }
240 else sn = implicitSNStart+i;
[1712]241 //fits_unlock();
[1365]242 TOIManager* mgr = TOIManager::getManager();
[1712]243 if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
244 if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
[1454]245 // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
[1716]246 int k;
247 for (k=0; k<getNOut(); k++) {
[1527]248 int j = colsinput[k].first;
249 if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
[1712]250 //fits_lock();
[1365]251 fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
[1712]252 tabdata[k] = y;
[1527]253 long flg = 0;
254 if (colsinput[k].second) {
255 fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
256 }
[2058]257
258 if (sepFlagfile) { // Ajout Reza (18/6/2002) pour fichier de flags separe
259 int sflg;
260 flg = 0;
261 for(int skf=0; skf<sepFlagCols.size(); skf++) {
262 fits_read_col_int(fptrflg, skf+1, i+1,1,1,0,&sflg,&anyNul,&fstatus);
263 if (sflg) flg |= sepFlagCols[skf];
264 }
265 } // Fin modif pour fichier de flags separe (18/6/2002)
266
[1712]267 tabflag[k] = flg;
268 // fits_unlock();
269 //putData(k, sn, y, flg);
[1365]270 }
[1712]271 fits_unlock();
[1716]272 for (k=0; k<getNOut(); k++) {
[1712]273 putData(k, sn, tabdata[k], tabflag[k]);
274 }
275 /* if (i%50==0) {
276 // fits_unlock();
[1696]277 sched_yield();
[1712]278 //fits_lock();
279 }*/
[1629]280 totnscount++;
[1365]281 }
[1710]282 //fits_unlock();
[1721]283 delete[] tabflag;
284 delete[] tabdata;
[1365]285 cout << "reader done reading... " << pthread_self() << endl;
286}
[1717]287
288void FITSTOIReader::run2()
289// ---- Version bufferisee (Christophe 20/10/2001)
290{
291 int ncols = outIx.size();
292 cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
293
294 //////// Prepare buffer, allocate memory
295 double *samplenum = new double[Buff_Sz];
296 vector<double*> colval;
297 vector<long*> colflg;
298 for(int k=0; k<getNOut(); k++) {
299 colval.push_back(NULL);
300 colflg.push_back(NULL);
301 if(!checkOutputTOIIndex(k)) continue;
302 colval[k] = new double[Buff_Sz];
303 if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
304 }
[1744]305 uint_8 * tmpflg = new uint_8[Buff_Sz];
[1717]306
[2058]307 // Ajout Reza (18/6/2002) pour fichier de flags separe
308 int* stmpflg = NULL;
309 if (sepFlagfile) stmpflg = new int[Buff_Sz];
310 // Fin modif pour fichier de flags separe (18/6/2002)
311
312
[1801]313 TOIManager* mgr = TOIManager::getManager();
314
[1717]315 //////// Read data and put into pipe
316 long ideb=-1,ifin=-1;
[1836]317 for(long i=0; i<nrows; i+=Buff_Sz) {
[1994]318 fits_lock(); // lock en debut de boucle de lecture - Reza 13/05/2002
[1717]319 // faut-il lire dans le fichier fits ?
[1836]320 if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
[1717]321 ideb = i;
322 ifin = ideb+Buff_Sz-1;
323 if(ifin>=nrows) ifin=nrows-1;
324 long n = ifin-ideb+1;
325 int anyNul;
[1725]326 if (implicitSN) {
[1801]327 if (ideb+implicitSNStart > mgr->getRequestedEnd()
[2011]328 || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) {
329 fits_unlock(); // unlock avant de casser la boucle - Reza 16/05/2002
330 break;
331 }
[1801]332 if (ifin+implicitSNStart < mgr->getRequestedBegin()
[2011]333 || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) {
334 fits_unlock(); // unlock avant de continuer - Reza 16/05/2002
335 continue;
336 }
[1725]337 for (long j=0; j<Buff_Sz; j++) {
338 samplenum[j] = j+ideb+implicitSNStart;
339 }
[1994]340 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
[1725]341 } else {
[1994]342 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
[1725]343 fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
[1836]344 if (samplenum[0] > mgr->getRequestedEnd()
345 || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
[1994]346 fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
[1801]347 break;
348 }
[1836]349 if (samplenum[n-1] < mgr->getRequestedBegin()
350 || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
[2011]351 fits_unlock();
[1801]352 continue;
353 }
[1725]354 }
[1717]355 for(int k=0; k<getNOut(); k++) {
356 if(colval[k]==NULL) continue;
357 int j = colsinput[k].first;
358 fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
359 if(colflg[k]==NULL) continue;
360 fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
[2058]361 // Ajout Reza (18/6/2002) pour fichier de flags separe
362 if (sepFlagfile) {
363 uint_8 sflg = 0;
364 int sjj;
365 for(sjj=0; sjj<n; sjj++) colflg[k][sjj] = 0;
366 for(int skf=0; skf<sepFlagCols.size(); skf++) {
367 fits_read_col_int(fptrflg, skf+1, ideb+1,1,n,0,stmpflg,&anyNul,&fstatus);
368 for(sjj=0; sjj<n; sjj++)
369 if (stmpflg) colflg[k][sjj] |= sepFlagCols[skf];
370 }
371 } // Fin modif pour fichier de flags separe (18/6/2002)
372
[1717]373 }
374 if(fstatus!=0) {
375 fits_report_error(stderr,fstatus);
[1994]376 fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
[1717]377 throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
378 }
379 }
[2011]380 fits_unlock(); // unlock en fin de boucle de lecture - Reza 13/05/2002
[1717]381
382 long ip = i-ideb; // pointeurs dans les buffers
383 long sn = (long) (samplenum[ip]+.1);
[1744]384 if (ip == 0) {
385 for(int k=0; k<getNOut(); k++) {
386 if(colval[k]==NULL) continue;
387 if (colflg[k] != NULL) {
388 for (int ii=0; ii<Buff_Sz; ii++) {
389 tmpflg[ii] = colflg[k][ii];
390 }
391 putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
392 } else {
393 putData(k, sn, ifin-ideb+1, colval[k]);
394 }
395 }
396 }
[1994]397 totnscount += (ifin-ideb+1);
[1717]398 }
399
400 //////// des-allocate buffers
401 delete [] samplenum;
[1744]402 delete [] tmpflg;
[2058]403 if (sepFlagfile) delete [] stmpflg;
[1763]404 {for(int k=0; k<getNOut(); k++) {
[1717]405 if(colval[k]!=NULL) delete [] colval[k];
406 if(colflg[k]!=NULL) delete [] colflg[k];
[1763]407 }}
[1717]408 colval.resize(0); colflg.resize(0);
409
[1994]410 cout << "reader (buffered) done reading... " << pthread_self() << endl;
[1717]411}
Note: See TracBrowser for help on using the repository browser.