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

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

Modifs fg_ --> FLAG_ ds FITSTOIReader et Writer , Reza 03/06/2002

File size: 9.4 KB
RevLine 
[1738]1// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
2// Eric Aubourg
3// Christophe Magneville
4// Reza Ansari
[2041]5// $Id: fitstoirdr.cc,v 1.29 2002-06-03 14:23:39 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;
[1365]24}
25
26FITSTOIReader::~FITSTOIReader() {
27}
28
[1994]29void FITSTOIReader::setImplicitSN(long snStart) {
[1725]30 implicitSN = true;
31 implicitSNStart = snStart;
32}
33
[1994]34void FITSTOIReader::setBufferSize(int buffsz)
35{
36 Buff_Sz = (buffsz>0) ? buffsz: 1024;
37 return;
38}
39
[1365]40pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
41void fits_lock();
42void fits_unlock();
43
44void fits_lock() {
45 pthread_mutex_lock(&fits_mutex);
46}
47void fits_unlock() {
48 pthread_mutex_unlock(&fits_mutex);
49}
50
[1480]51void FITSTOIReader::openFile(string fn) {
[1365]52 fits_lock();
[1480]53 if (fptr) {
54 fits_close_file(fptr,&fstatus);
55 fptr = NULL;
56 }
57 fname = fn;
58 cout << "FITSTOIReader::open FileName=" << fname << endl;
[1365]59 fstatus = 0;
60 // Open file
61 fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus);
62 if (fstatus != 0) {
63 fits_report_error(stderr, fstatus);
64 fits_unlock();
[1721]65 throw IOExc("fitsio error");
[1365]66 }
67
68 // Go to first extension, which should be a BINTABLE
69
70 int simple, bitpix, naxis;
71 long naxes;
72 long pcount, gcount;
73 int extend;
74 fits_read_imghdr(fptr, 1, &simple, &bitpix,
75 &naxis, &naxes, &pcount, &gcount, &extend, &fstatus);
76
77 fits_movabs_hdu(fptr, 2, NULL, &fstatus);
78
79 fits_get_num_cols(fptr,&ncols,&fstatus);
80 fits_get_num_rows(fptr,&nrows,&fstatus);
81
[1454]82 cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
[1725]83 if (implicitSN) {
84 firstSn = implicitSNStart;
85 } else {
86 int anyNul;
87 double y;
88 fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus);
89 firstSn = (int) (y+.1);
90 }
[1480]91 fits_unlock();
92}
[1365]93
[1480]94void FITSTOIReader::init() {
95 openFile(allfn.front());
96
97 fits_lock();
[1725]98 // si pas implicitSN, la premiere colonne est le sampleNum.
99 // Sinon, le samplenum est la fitsline + offset.
[1527]100 int itoi=-1;
[1725]101 int col1 = implicitSN ? 0 : 1;
102 for (int i=col1; i<ncols; i++) {
[1365]103 char templt[10];
104 sprintf(templt, "%d", i+1);
105 char colname[200];
106 int colnum;
107 fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
108 cout << "FITSTOIReader col " << colname << endl;
[2041]109 // On verifie si c'est une colonne de flag
110 if (!strncmp(colname, "fg_", 3) || !strncmp(colname, "FLAG_", 5) ) {
[1527]111 colsinput[itoi].second=true;
112 } else {
113 declareOutput(colname);
114 itoi++;
115 colsinput[itoi] = pair<int,bool>(i,false);
116 }
[1365]117 }
118 fits_unlock();
[1480]119
120 snBegin = firstSn;
[1800]121 if (forcedMinIn > 0 && forcedMinIn > snBegin) {
122 snBegin = forcedMinIn;
[1787]123 }
[1480]124 openFile(allfn.back());
125 snEnd = firstSn+nrows-1;
[1800]126 if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
127 snEnd = forcedMaxIn;
128 }
[1480]129 cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
[1365]130}
131
132int FITSTOIReader::calcMinOut() {
[1800]133 chkinit();
[1367]134 TOIManager* mgr = TOIManager::getManager();
135 int firstReq = mgr->getRequestedBegin();
[1480]136 return snBegin > firstReq ? snBegin : firstReq;
[1365]137}
138
139int FITSTOIReader::calcMaxOut() {
[1800]140 chkinit();
[1367]141 TOIManager* mgr = TOIManager::getManager();
142 int lastReq = mgr->getRequestedEnd();
[1480]143 return snEnd < lastReq ? snEnd : lastReq;
[1365]144}
145
[1480]146void FITSTOIReader::addFile(string fn) {
147 allfn.push_back(fn);
148}
[1365]149
150void FITSTOIReader::run() {
[1480]151 for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
152 openFile(*i);
[1994]153 if (Buff_Sz > 1) run2(); // Lecture bufferise
154 else run1(); // Lecture un echantillon a la fois
[1480]155 }
156}
157
[1725]158// run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
[1994]159// ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter
160// implicitSNStart et switvh run1/run2
161// suivant buffersize
162
[1480]163void FITSTOIReader::run1() {
[1365]164 // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
165 // mais cela implique de gerer aussi bien echant uniforme que non.
166 // On pourrait aussi lire plusieurs elements d'un coup.
167 int ncols = outIx.size();
[1454]168 cout << "reader reading... NRows=" << nrows << " firstSn= "
169 << firstSn << endl;
[1629]170
[1721]171 double* tabdata = new double[getNOut()];
172 uint_8* tabflag = new uint_8[getNOut()];
[1712]173
[1721]174 for (long i=0; i<nrows; i++) {
[1365]175 int anyNul;
176 double y;
[1710]177 fits_lock();
[1994]178 long sn;
179 if (implicitSN) {
180 fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
181 sn = (long) (y+.1);
182 }
183 else sn = implicitSNStart+i;
[1712]184 //fits_unlock();
[1365]185 TOIManager* mgr = TOIManager::getManager();
[1712]186 if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
187 if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
[1454]188 // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
[1716]189 int k;
190 for (k=0; k<getNOut(); k++) {
[1527]191 int j = colsinput[k].first;
192 if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
[1712]193 //fits_lock();
[1365]194 fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
[1712]195 tabdata[k] = y;
[1527]196 long flg = 0;
197 if (colsinput[k].second) {
198 fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
199 }
[1712]200 tabflag[k] = flg;
201 // fits_unlock();
202 //putData(k, sn, y, flg);
[1365]203 }
[1712]204 fits_unlock();
[1716]205 for (k=0; k<getNOut(); k++) {
[1712]206 putData(k, sn, tabdata[k], tabflag[k]);
207 }
208 /* if (i%50==0) {
209 // fits_unlock();
[1696]210 sched_yield();
[1712]211 //fits_lock();
212 }*/
[1629]213 totnscount++;
[1365]214 }
[1710]215 //fits_unlock();
[1721]216 delete[] tabflag;
217 delete[] tabdata;
[1365]218 cout << "reader done reading... " << pthread_self() << endl;
219}
[1717]220
221void FITSTOIReader::run2()
222// ---- Version bufferisee (Christophe 20/10/2001)
223{
224 int ncols = outIx.size();
225 cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
226
227 //////// Prepare buffer, allocate memory
228 double *samplenum = new double[Buff_Sz];
229 vector<double*> colval;
230 vector<long*> colflg;
231 for(int k=0; k<getNOut(); k++) {
232 colval.push_back(NULL);
233 colflg.push_back(NULL);
234 if(!checkOutputTOIIndex(k)) continue;
235 colval[k] = new double[Buff_Sz];
236 if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
237 }
[1744]238 uint_8 * tmpflg = new uint_8[Buff_Sz];
[1717]239
[1801]240 TOIManager* mgr = TOIManager::getManager();
241
[1717]242 //////// Read data and put into pipe
243 long ideb=-1,ifin=-1;
[1836]244 for(long i=0; i<nrows; i+=Buff_Sz) {
[1994]245 fits_lock(); // lock en debut de boucle de lecture - Reza 13/05/2002
[1717]246 // faut-il lire dans le fichier fits ?
[1836]247 if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
[1717]248 ideb = i;
249 ifin = ideb+Buff_Sz-1;
250 if(ifin>=nrows) ifin=nrows-1;
251 long n = ifin-ideb+1;
252 int anyNul;
[1725]253 if (implicitSN) {
[1801]254 if (ideb+implicitSNStart > mgr->getRequestedEnd()
[2011]255 || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) {
256 fits_unlock(); // unlock avant de casser la boucle - Reza 16/05/2002
257 break;
258 }
[1801]259 if (ifin+implicitSNStart < mgr->getRequestedBegin()
[2011]260 || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) {
261 fits_unlock(); // unlock avant de continuer - Reza 16/05/2002
262 continue;
263 }
[1725]264 for (long j=0; j<Buff_Sz; j++) {
265 samplenum[j] = j+ideb+implicitSNStart;
266 }
[1994]267 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
[1725]268 } else {
[1994]269 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
[1725]270 fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
[1836]271 if (samplenum[0] > mgr->getRequestedEnd()
272 || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
[1994]273 fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
[1801]274 break;
275 }
[1836]276 if (samplenum[n-1] < mgr->getRequestedBegin()
277 || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
[2011]278 fits_unlock();
[1801]279 continue;
280 }
[1725]281 }
[1717]282 for(int k=0; k<getNOut(); k++) {
283 if(colval[k]==NULL) continue;
284 int j = colsinput[k].first;
285 fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
286 if(colflg[k]==NULL) continue;
287 fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
288 }
289 if(fstatus!=0) {
290 fits_report_error(stderr,fstatus);
[1994]291 fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
[1717]292 throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
293 }
294 }
[2011]295 fits_unlock(); // unlock en fin de boucle de lecture - Reza 13/05/2002
[1717]296
297 long ip = i-ideb; // pointeurs dans les buffers
298 long sn = (long) (samplenum[ip]+.1);
[1744]299 if (ip == 0) {
300 for(int k=0; k<getNOut(); k++) {
301 if(colval[k]==NULL) continue;
302 if (colflg[k] != NULL) {
303 for (int ii=0; ii<Buff_Sz; ii++) {
304 tmpflg[ii] = colflg[k][ii];
305 }
306 putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
307 } else {
308 putData(k, sn, ifin-ideb+1, colval[k]);
309 }
310 }
311 }
[1994]312 totnscount += (ifin-ideb+1);
[1717]313 }
314
315 //////// des-allocate buffers
316 delete [] samplenum;
[1744]317 delete [] tmpflg;
[1763]318 {for(int k=0; k<getNOut(); k++) {
[1717]319 if(colval[k]!=NULL) delete [] colval[k];
320 if(colflg[k]!=NULL) delete [] colflg[k];
[1763]321 }}
[1717]322 colval.resize(0); colflg.resize(0);
323
[1994]324 cout << "reader (buffered) done reading... " << pthread_self() << endl;
[1717]325}
Note: See TracBrowser for help on using the repository browser.