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

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

fits reader pour fichers sans samplenum, level s planck

File size: 7.1 KB
Line 
1#include "fitstoirdr.h"
2#include "toimanager.h"
3#include <sched.h>
4
5
6FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
7 fname = fn;
8 allfn.push_back(fn);
9 Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
10 cout<<"FITSTOIReader::FITSTOIReader"<<endl;
11 cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
12 name = "rdr";
13 fptr = NULL;
14 totnscount = 0;
15
16 implicitSN = false;
17 implicitSNStart = 0;
18}
19
20FITSTOIReader::~FITSTOIReader() {
21}
22
23void FITSTOIReader::setImplicitSN(int snStart) {
24 implicitSN = true;
25 implicitSNStart = snStart;
26}
27
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
39void FITSTOIReader::openFile(string fn) {
40 fits_lock();
41 if (fptr) {
42 fits_close_file(fptr,&fstatus);
43 fptr = NULL;
44 }
45 fname = fn;
46 cout << "FITSTOIReader::open FileName=" << fname << endl;
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();
53 throw IOExc("fitsio error");
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
70 cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
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 }
79 fits_unlock();
80}
81
82void FITSTOIReader::init() {
83 openFile(allfn.front());
84
85 fits_lock();
86 // si pas implicitSN, la premiere colonne est le sampleNum.
87 // Sinon, le samplenum est la fitsline + offset.
88 int itoi=-1;
89 int col1 = implicitSN ? 0 : 1;
90 for (int i=col1; i<ncols; i++) {
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;
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 }
104 }
105 fits_unlock();
106
107 snBegin = firstSn;
108
109 openFile(allfn.back());
110 snEnd = firstSn+nrows-1;
111 cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
112}
113
114int FITSTOIReader::calcMinOut() {
115 TOIManager* mgr = TOIManager::getManager();
116 int firstReq = mgr->getRequestedBegin();
117 return snBegin > firstReq ? snBegin : firstReq;
118}
119
120int FITSTOIReader::calcMaxOut() {
121 TOIManager* mgr = TOIManager::getManager();
122 int lastReq = mgr->getRequestedEnd();
123 return snEnd < lastReq ? snEnd : lastReq;
124}
125
126void FITSTOIReader::addFile(string fn) {
127 allfn.push_back(fn);
128}
129
130void FITSTOIReader::run() {
131 for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
132 openFile(*i);
133 //run1();
134 run2();
135 }
136}
137
138// run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
139void FITSTOIReader::run1() {
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();
144 cout << "reader reading... NRows=" << nrows << " firstSn= "
145 << firstSn << endl;
146
147 double* tabdata = new double[getNOut()];
148 uint_8* tabflag = new uint_8[getNOut()];
149
150 for (long i=0; i<nrows; i++) {
151 int anyNul;
152 double y;
153 fits_lock();
154 fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
155 //fits_unlock();
156 long sn = (long) (y+.1);
157 TOIManager* mgr = TOIManager::getManager();
158 if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
159 if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
160 // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
161 int k;
162 for (k=0; k<getNOut(); k++) {
163 int j = colsinput[k].first;
164 if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
165 //fits_lock();
166 fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
167 tabdata[k] = y;
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 }
172 tabflag[k] = flg;
173 // fits_unlock();
174 //putData(k, sn, y, flg);
175 }
176 fits_unlock();
177 for (k=0; k<getNOut(); k++) {
178 putData(k, sn, tabdata[k], tabflag[k]);
179 }
180 /* if (i%50==0) {
181 // fits_unlock();
182 sched_yield();
183 //fits_lock();
184 }*/
185 totnscount++;
186 }
187 //fits_unlock();
188 delete[] tabflag;
189 delete[] tabdata;
190 cout << "reader done reading... " << pthread_self() << endl;
191}
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();
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 }
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.