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

Last change on this file since 2031 was 2011, checked in by ansari, 23 years ago

correction bug oubli fits_unlock() ds FitsTOIReader - Reza 16/5/2002

File size: 9.3 KB
Line 
1// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
2// Eric Aubourg
3// Christophe Magneville
4// Reza Ansari
5// $Id: fitstoirdr.cc,v 1.28 2002-05-16 14:39:08 ansari Exp $
6
7#include "fitstoirdr.h"
8#include "toimanager.h"
9#include <sched.h>
10
11
12FITSTOIReader::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
26FITSTOIReader::~FITSTOIReader() {
27}
28
29void FITSTOIReader::setImplicitSN(long snStart) {
30 implicitSN = true;
31 implicitSNStart = snStart;
32}
33
34void FITSTOIReader::setBufferSize(int buffsz)
35{
36 Buff_Sz = (buffsz>0) ? buffsz: 1024;
37 return;
38}
39
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
51void 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
94void 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 if (!strncmp(colname, "fg_", 3)) {
110 colsinput[itoi].second=true;
111 } else {
112 declareOutput(colname);
113 itoi++;
114 colsinput[itoi] = pair<int,bool>(i,false);
115 }
116 }
117 fits_unlock();
118
119 snBegin = firstSn;
120 if (forcedMinIn > 0 && forcedMinIn > snBegin) {
121 snBegin = forcedMinIn;
122 }
123 openFile(allfn.back());
124 snEnd = firstSn+nrows-1;
125 if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
126 snEnd = forcedMaxIn;
127 }
128 cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
129}
130
131int FITSTOIReader::calcMinOut() {
132 chkinit();
133 TOIManager* mgr = TOIManager::getManager();
134 int firstReq = mgr->getRequestedBegin();
135 return snBegin > firstReq ? snBegin : firstReq;
136}
137
138int FITSTOIReader::calcMaxOut() {
139 chkinit();
140 TOIManager* mgr = TOIManager::getManager();
141 int lastReq = mgr->getRequestedEnd();
142 return snEnd < lastReq ? snEnd : lastReq;
143}
144
145void FITSTOIReader::addFile(string fn) {
146 allfn.push_back(fn);
147}
148
149void FITSTOIReader::run() {
150 for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
151 openFile(*i);
152 if (Buff_Sz > 1) run2(); // Lecture bufferise
153 else run1(); // Lecture un echantillon a la fois
154 }
155}
156
157// run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
158// ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter
159// implicitSNStart et switvh run1/run2
160// suivant buffersize
161
162void FITSTOIReader::run1() {
163 // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
164 // mais cela implique de gerer aussi bien echant uniforme que non.
165 // On pourrait aussi lire plusieurs elements d'un coup.
166 int ncols = outIx.size();
167 cout << "reader reading... NRows=" << nrows << " firstSn= "
168 << firstSn << endl;
169
170 double* tabdata = new double[getNOut()];
171 uint_8* tabflag = new uint_8[getNOut()];
172
173 for (long i=0; i<nrows; i++) {
174 int anyNul;
175 double y;
176 fits_lock();
177 long sn;
178 if (implicitSN) {
179 fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
180 sn = (long) (y+.1);
181 }
182 else sn = implicitSNStart+i;
183 //fits_unlock();
184 TOIManager* mgr = TOIManager::getManager();
185 if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
186 if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
187 // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
188 int k;
189 for (k=0; k<getNOut(); k++) {
190 int j = colsinput[k].first;
191 if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
192 //fits_lock();
193 fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
194 tabdata[k] = y;
195 long flg = 0;
196 if (colsinput[k].second) {
197 fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
198 }
199 tabflag[k] = flg;
200 // fits_unlock();
201 //putData(k, sn, y, flg);
202 }
203 fits_unlock();
204 for (k=0; k<getNOut(); k++) {
205 putData(k, sn, tabdata[k], tabflag[k]);
206 }
207 /* if (i%50==0) {
208 // fits_unlock();
209 sched_yield();
210 //fits_lock();
211 }*/
212 totnscount++;
213 }
214 //fits_unlock();
215 delete[] tabflag;
216 delete[] tabdata;
217 cout << "reader done reading... " << pthread_self() << endl;
218}
219
220void FITSTOIReader::run2()
221// ---- Version bufferisee (Christophe 20/10/2001)
222{
223 int ncols = outIx.size();
224 cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
225
226 //////// Prepare buffer, allocate memory
227 double *samplenum = new double[Buff_Sz];
228 vector<double*> colval;
229 vector<long*> colflg;
230 for(int k=0; k<getNOut(); k++) {
231 colval.push_back(NULL);
232 colflg.push_back(NULL);
233 if(!checkOutputTOIIndex(k)) continue;
234 colval[k] = new double[Buff_Sz];
235 if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
236 }
237 uint_8 * tmpflg = new uint_8[Buff_Sz];
238
239 TOIManager* mgr = TOIManager::getManager();
240
241 //////// Read data and put into pipe
242 long ideb=-1,ifin=-1;
243 for(long i=0; i<nrows; i+=Buff_Sz) {
244 fits_lock(); // lock en debut de boucle de lecture - Reza 13/05/2002
245 // faut-il lire dans le fichier fits ?
246 if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
247 ideb = i;
248 ifin = ideb+Buff_Sz-1;
249 if(ifin>=nrows) ifin=nrows-1;
250 long n = ifin-ideb+1;
251 int anyNul;
252 if (implicitSN) {
253 if (ideb+implicitSNStart > mgr->getRequestedEnd()
254 || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) {
255 fits_unlock(); // unlock avant de casser la boucle - Reza 16/05/2002
256 break;
257 }
258 if (ifin+implicitSNStart < mgr->getRequestedBegin()
259 || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) {
260 fits_unlock(); // unlock avant de continuer - Reza 16/05/2002
261 continue;
262 }
263 for (long j=0; j<Buff_Sz; j++) {
264 samplenum[j] = j+ideb+implicitSNStart;
265 }
266 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
267 } else {
268 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
269 fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
270 if (samplenum[0] > mgr->getRequestedEnd()
271 || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
272 fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
273 break;
274 }
275 if (samplenum[n-1] < mgr->getRequestedBegin()
276 || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
277 fits_unlock();
278 continue;
279 }
280 }
281 for(int k=0; k<getNOut(); k++) {
282 if(colval[k]==NULL) continue;
283 int j = colsinput[k].first;
284 fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
285 if(colflg[k]==NULL) continue;
286 fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
287 }
288 if(fstatus!=0) {
289 fits_report_error(stderr,fstatus);
290 fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
291 throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
292 }
293 }
294 fits_unlock(); // unlock en fin de boucle de lecture - Reza 13/05/2002
295
296 long ip = i-ideb; // pointeurs dans les buffers
297 long sn = (long) (samplenum[ip]+.1);
298 if (ip == 0) {
299 for(int k=0; k<getNOut(); k++) {
300 if(colval[k]==NULL) continue;
301 if (colflg[k] != NULL) {
302 for (int ii=0; ii<Buff_Sz; ii++) {
303 tmpflg[ii] = colflg[k][ii];
304 }
305 putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
306 } else {
307 putData(k, sn, ifin-ideb+1, colval[k]);
308 }
309 }
310 }
311 totnscount += (ifin-ideb+1);
312 }
313
314 //////// des-allocate buffers
315 delete [] samplenum;
316 delete [] tmpflg;
317 {for(int k=0; k<getNOut(); k++) {
318 if(colval[k]!=NULL) delete [] colval[k];
319 if(colflg[k]!=NULL) delete [] colflg[k];
320 }}
321 colval.resize(0); colflg.resize(0);
322
323 cout << "reader (buffered) done reading... " << pthread_self() << endl;
324}
Note: See TracBrowser for help on using the repository browser.