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

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

1/ Ajout classe CGT (Compagnie Generale des Tuyaux) - Assistance a l'assemblage
des TOIProcessors .
2/ Corrections mineures ds FitsTOIReader, correction de smkmflib

Reza 13/5/2002

File size: 9.2 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.27 2002-05-13 13:11:32 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)) break;
255 if (ifin+implicitSNStart < mgr->getRequestedBegin()
256 || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) continue;
257 for (long j=0; j<Buff_Sz; j++) {
258 samplenum[j] = j+ideb+implicitSNStart;
259 }
260 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
261 } else {
262 //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
263 fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
264 if (samplenum[0] > mgr->getRequestedEnd()
265 || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
266 fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
267 break;
268 }
269 if (samplenum[n-1] < mgr->getRequestedBegin()
270 || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
271 //---- Pas la peine, fait en fin de boucle - Reza 13/05/2002 fits_unlock();
272 continue;
273 }
274 }
275 for(int k=0; k<getNOut(); k++) {
276 if(colval[k]==NULL) continue;
277 int j = colsinput[k].first;
278 fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
279 if(colflg[k]==NULL) continue;
280 fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
281 }
282 if(fstatus!=0) {
283 fits_report_error(stderr,fstatus);
284 fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
285 throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
286 }
287 }
288 fits_unlock(); // unlock en debut de boucle de lecture - Reza 13/05/2002
289
290 long ip = i-ideb; // pointeurs dans les buffers
291 long sn = (long) (samplenum[ip]+.1);
292 if (ip == 0) {
293 for(int k=0; k<getNOut(); k++) {
294 if(colval[k]==NULL) continue;
295 if (colflg[k] != NULL) {
296 for (int ii=0; ii<Buff_Sz; ii++) {
297 tmpflg[ii] = colflg[k][ii];
298 }
299 putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
300 } else {
301 putData(k, sn, ifin-ideb+1, colval[k]);
302 }
303 }
304 }
305 totnscount += (ifin-ideb+1);
306 }
307
308 //////// des-allocate buffers
309 delete [] samplenum;
310 delete [] tmpflg;
311 {for(int k=0; k<getNOut(); k++) {
312 if(colval[k]!=NULL) delete [] colval[k];
313 if(colflg[k]!=NULL) delete [] colflg[k];
314 }}
315 colval.resize(0); colflg.resize(0);
316
317 cout << "reader (buffered) done reading... " << pthread_self() << endl;
318}
Note: See TracBrowser for help on using the repository browser.