1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
2 | // Eric Aubourg
|
---|
3 | // Christophe Magneville
|
---|
4 | // Reza Ansari
|
---|
5 | // $Id: fitstoirdr.cc,v 1.29 2002-06-03 14:23:39 ansari Exp $
|
---|
6 |
|
---|
7 | #include "fitstoirdr.h"
|
---|
8 | #include "toimanager.h"
|
---|
9 | #include <sched.h>
|
---|
10 |
|
---|
11 |
|
---|
12 | FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
|
---|
13 | fname = fn;
|
---|
14 | allfn.push_back(fn);
|
---|
15 | Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
|
---|
16 | cout<<"FITSTOIReader::FITSTOIReader"<<endl;
|
---|
17 | cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
|
---|
18 | name = "rdr";
|
---|
19 | fptr = NULL;
|
---|
20 | totnscount = 0;
|
---|
21 |
|
---|
22 | implicitSN = false;
|
---|
23 | implicitSNStart = 0;
|
---|
24 | }
|
---|
25 |
|
---|
26 | FITSTOIReader::~FITSTOIReader() {
|
---|
27 | }
|
---|
28 |
|
---|
29 | void FITSTOIReader::setImplicitSN(long snStart) {
|
---|
30 | implicitSN = true;
|
---|
31 | implicitSNStart = snStart;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void FITSTOIReader::setBufferSize(int buffsz)
|
---|
35 | {
|
---|
36 | Buff_Sz = (buffsz>0) ? buffsz: 1024;
|
---|
37 | return;
|
---|
38 | }
|
---|
39 |
|
---|
40 | pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
41 | void fits_lock();
|
---|
42 | void fits_unlock();
|
---|
43 |
|
---|
44 | void fits_lock() {
|
---|
45 | pthread_mutex_lock(&fits_mutex);
|
---|
46 | }
|
---|
47 | void fits_unlock() {
|
---|
48 | pthread_mutex_unlock(&fits_mutex);
|
---|
49 | }
|
---|
50 |
|
---|
51 | void FITSTOIReader::openFile(string fn) {
|
---|
52 | fits_lock();
|
---|
53 | if (fptr) {
|
---|
54 | fits_close_file(fptr,&fstatus);
|
---|
55 | fptr = NULL;
|
---|
56 | }
|
---|
57 | fname = fn;
|
---|
58 | cout << "FITSTOIReader::open FileName=" << fname << endl;
|
---|
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();
|
---|
65 | throw IOExc("fitsio error");
|
---|
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 |
|
---|
82 | cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
|
---|
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 | }
|
---|
91 | fits_unlock();
|
---|
92 | }
|
---|
93 |
|
---|
94 | void FITSTOIReader::init() {
|
---|
95 | openFile(allfn.front());
|
---|
96 |
|
---|
97 | fits_lock();
|
---|
98 | // si pas implicitSN, la premiere colonne est le sampleNum.
|
---|
99 | // Sinon, le samplenum est la fitsline + offset.
|
---|
100 | int itoi=-1;
|
---|
101 | int col1 = implicitSN ? 0 : 1;
|
---|
102 | for (int i=col1; i<ncols; i++) {
|
---|
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;
|
---|
109 | // On verifie si c'est une colonne de flag
|
---|
110 | if (!strncmp(colname, "fg_", 3) || !strncmp(colname, "FLAG_", 5) ) {
|
---|
111 | colsinput[itoi].second=true;
|
---|
112 | } else {
|
---|
113 | declareOutput(colname);
|
---|
114 | itoi++;
|
---|
115 | colsinput[itoi] = pair<int,bool>(i,false);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | fits_unlock();
|
---|
119 |
|
---|
120 | snBegin = firstSn;
|
---|
121 | if (forcedMinIn > 0 && forcedMinIn > snBegin) {
|
---|
122 | snBegin = forcedMinIn;
|
---|
123 | }
|
---|
124 | openFile(allfn.back());
|
---|
125 | snEnd = firstSn+nrows-1;
|
---|
126 | if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
|
---|
127 | snEnd = forcedMaxIn;
|
---|
128 | }
|
---|
129 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
130 | }
|
---|
131 |
|
---|
132 | int FITSTOIReader::calcMinOut() {
|
---|
133 | chkinit();
|
---|
134 | TOIManager* mgr = TOIManager::getManager();
|
---|
135 | int firstReq = mgr->getRequestedBegin();
|
---|
136 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
137 | }
|
---|
138 |
|
---|
139 | int FITSTOIReader::calcMaxOut() {
|
---|
140 | chkinit();
|
---|
141 | TOIManager* mgr = TOIManager::getManager();
|
---|
142 | int lastReq = mgr->getRequestedEnd();
|
---|
143 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
144 | }
|
---|
145 |
|
---|
146 | void FITSTOIReader::addFile(string fn) {
|
---|
147 | allfn.push_back(fn);
|
---|
148 | }
|
---|
149 |
|
---|
150 | void FITSTOIReader::run() {
|
---|
151 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
152 | openFile(*i);
|
---|
153 | if (Buff_Sz > 1) run2(); // Lecture bufferise
|
---|
154 | else run1(); // Lecture un echantillon a la fois
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
|
---|
159 | // ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter
|
---|
160 | // implicitSNStart et switvh run1/run2
|
---|
161 | // suivant buffersize
|
---|
162 |
|
---|
163 | void FITSTOIReader::run1() {
|
---|
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();
|
---|
168 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
169 | << firstSn << endl;
|
---|
170 |
|
---|
171 | double* tabdata = new double[getNOut()];
|
---|
172 | uint_8* tabflag = new uint_8[getNOut()];
|
---|
173 |
|
---|
174 | for (long i=0; i<nrows; i++) {
|
---|
175 | int anyNul;
|
---|
176 | double y;
|
---|
177 | fits_lock();
|
---|
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;
|
---|
184 | //fits_unlock();
|
---|
185 | TOIManager* mgr = TOIManager::getManager();
|
---|
186 | if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
|
---|
187 | if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
|
---|
188 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
189 | int k;
|
---|
190 | for (k=0; k<getNOut(); k++) {
|
---|
191 | int j = colsinput[k].first;
|
---|
192 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
193 | //fits_lock();
|
---|
194 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
195 | tabdata[k] = y;
|
---|
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 | }
|
---|
200 | tabflag[k] = flg;
|
---|
201 | // fits_unlock();
|
---|
202 | //putData(k, sn, y, flg);
|
---|
203 | }
|
---|
204 | fits_unlock();
|
---|
205 | for (k=0; k<getNOut(); k++) {
|
---|
206 | putData(k, sn, tabdata[k], tabflag[k]);
|
---|
207 | }
|
---|
208 | /* if (i%50==0) {
|
---|
209 | // fits_unlock();
|
---|
210 | sched_yield();
|
---|
211 | //fits_lock();
|
---|
212 | }*/
|
---|
213 | totnscount++;
|
---|
214 | }
|
---|
215 | //fits_unlock();
|
---|
216 | delete[] tabflag;
|
---|
217 | delete[] tabdata;
|
---|
218 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
219 | }
|
---|
220 |
|
---|
221 | void 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 | }
|
---|
238 | uint_8 * tmpflg = new uint_8[Buff_Sz];
|
---|
239 |
|
---|
240 | TOIManager* mgr = TOIManager::getManager();
|
---|
241 |
|
---|
242 | //////// Read data and put into pipe
|
---|
243 | long ideb=-1,ifin=-1;
|
---|
244 | for(long i=0; i<nrows; i+=Buff_Sz) {
|
---|
245 | fits_lock(); // lock en debut de boucle de lecture - Reza 13/05/2002
|
---|
246 | // faut-il lire dans le fichier fits ?
|
---|
247 | if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
|
---|
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;
|
---|
253 | if (implicitSN) {
|
---|
254 | if (ideb+implicitSNStart > mgr->getRequestedEnd()
|
---|
255 | || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) {
|
---|
256 | fits_unlock(); // unlock avant de casser la boucle - Reza 16/05/2002
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | if (ifin+implicitSNStart < mgr->getRequestedBegin()
|
---|
260 | || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) {
|
---|
261 | fits_unlock(); // unlock avant de continuer - Reza 16/05/2002
|
---|
262 | continue;
|
---|
263 | }
|
---|
264 | for (long j=0; j<Buff_Sz; j++) {
|
---|
265 | samplenum[j] = j+ideb+implicitSNStart;
|
---|
266 | }
|
---|
267 | //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
|
---|
268 | } else {
|
---|
269 | //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
|
---|
270 | fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
|
---|
271 | if (samplenum[0] > mgr->getRequestedEnd()
|
---|
272 | || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
|
---|
273 | fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
|
---|
274 | break;
|
---|
275 | }
|
---|
276 | if (samplenum[n-1] < mgr->getRequestedBegin()
|
---|
277 | || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
|
---|
278 | fits_unlock();
|
---|
279 | continue;
|
---|
280 | }
|
---|
281 | }
|
---|
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);
|
---|
291 | fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
|
---|
292 | throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
|
---|
293 | }
|
---|
294 | }
|
---|
295 | fits_unlock(); // unlock en fin de boucle de lecture - Reza 13/05/2002
|
---|
296 |
|
---|
297 | long ip = i-ideb; // pointeurs dans les buffers
|
---|
298 | long sn = (long) (samplenum[ip]+.1);
|
---|
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 | }
|
---|
312 | totnscount += (ifin-ideb+1);
|
---|
313 | }
|
---|
314 |
|
---|
315 | //////// des-allocate buffers
|
---|
316 | delete [] samplenum;
|
---|
317 | delete [] tmpflg;
|
---|
318 | {for(int k=0; k<getNOut(); k++) {
|
---|
319 | if(colval[k]!=NULL) delete [] colval[k];
|
---|
320 | if(colflg[k]!=NULL) delete [] colflg[k];
|
---|
321 | }}
|
---|
322 | colval.resize(0); colflg.resize(0);
|
---|
323 |
|
---|
324 | cout << "reader (buffered) done reading... " << pthread_self() << endl;
|
---|
325 | }
|
---|