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

Last change on this file since 1774 was 1772, checked in by aubourg, 24 years ago

bug requested end pour samplenum integres dans fichier

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