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

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

fits reader pour fichers sans samplenum, level s planck

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