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

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

GPH 424.1

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