1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
2 | // Eric Aubourg
|
---|
3 | // Christophe Magneville
|
---|
4 | // Reza Ansari
|
---|
5 | // $Id: fitstoirdr.cc,v 1.22 2001-11-26 15:13:48 aubourg 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(int snStart) {
|
---|
30 | implicitSN = true;
|
---|
31 | implicitSNStart = snStart;
|
---|
32 | }
|
---|
33 |
|
---|
34 | pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
35 | void fits_lock();
|
---|
36 | void fits_unlock();
|
---|
37 |
|
---|
38 | void fits_lock() {
|
---|
39 | pthread_mutex_lock(&fits_mutex);
|
---|
40 | }
|
---|
41 | void fits_unlock() {
|
---|
42 | pthread_mutex_unlock(&fits_mutex);
|
---|
43 | }
|
---|
44 |
|
---|
45 | void 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 |
|
---|
88 | void 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 (forceMinIn > 0 && forceMinIn > snBegin) {
|
---|
115 | snBegin = forceMinIn;
|
---|
116 | }
|
---|
117 | */
|
---|
118 | openFile(allfn.back());
|
---|
119 | snEnd = firstSn+nrows-1;
|
---|
120 | /* if (forceMaxIn > 0 && forceMaxIn < snEnd) {
|
---|
121 | snEnd = forceMaxIn;
|
---|
122 | }*/
|
---|
123 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
124 | }
|
---|
125 |
|
---|
126 | int FITSTOIReader::calcMinOut() {
|
---|
127 | TOIManager* mgr = TOIManager::getManager();
|
---|
128 | int firstReq = mgr->getRequestedBegin();
|
---|
129 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
130 | }
|
---|
131 |
|
---|
132 | int FITSTOIReader::calcMaxOut() {
|
---|
133 | TOIManager* mgr = TOIManager::getManager();
|
---|
134 | int lastReq = mgr->getRequestedEnd();
|
---|
135 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
136 | }
|
---|
137 |
|
---|
138 | void FITSTOIReader::addFile(string fn) {
|
---|
139 | allfn.push_back(fn);
|
---|
140 | }
|
---|
141 |
|
---|
142 | void FITSTOIReader::run() {
|
---|
143 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
144 | openFile(*i);
|
---|
145 | //run1();
|
---|
146 | run2();
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | // run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
|
---|
151 | void FITSTOIReader::run1() {
|
---|
152 | // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
|
---|
153 | // mais cela implique de gerer aussi bien echant uniforme que non.
|
---|
154 | // On pourrait aussi lire plusieurs elements d'un coup.
|
---|
155 | int ncols = outIx.size();
|
---|
156 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
157 | << firstSn << endl;
|
---|
158 |
|
---|
159 | double* tabdata = new double[getNOut()];
|
---|
160 | uint_8* tabflag = new uint_8[getNOut()];
|
---|
161 |
|
---|
162 | for (long i=0; i<nrows; i++) {
|
---|
163 | int anyNul;
|
---|
164 | double y;
|
---|
165 | fits_lock();
|
---|
166 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
167 | //fits_unlock();
|
---|
168 | long sn = (long) (y+.1);
|
---|
169 | TOIManager* mgr = TOIManager::getManager();
|
---|
170 | if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
|
---|
171 | if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
|
---|
172 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
173 | int k;
|
---|
174 | for (k=0; k<getNOut(); k++) {
|
---|
175 | int j = colsinput[k].first;
|
---|
176 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
177 | //fits_lock();
|
---|
178 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
179 | tabdata[k] = y;
|
---|
180 | long flg = 0;
|
---|
181 | if (colsinput[k].second) {
|
---|
182 | fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
|
---|
183 | }
|
---|
184 | tabflag[k] = flg;
|
---|
185 | // fits_unlock();
|
---|
186 | //putData(k, sn, y, flg);
|
---|
187 | }
|
---|
188 | fits_unlock();
|
---|
189 | for (k=0; k<getNOut(); k++) {
|
---|
190 | putData(k, sn, tabdata[k], tabflag[k]);
|
---|
191 | }
|
---|
192 | /* if (i%50==0) {
|
---|
193 | // fits_unlock();
|
---|
194 | sched_yield();
|
---|
195 | //fits_lock();
|
---|
196 | }*/
|
---|
197 | totnscount++;
|
---|
198 | }
|
---|
199 | //fits_unlock();
|
---|
200 | delete[] tabflag;
|
---|
201 | delete[] tabdata;
|
---|
202 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
203 | }
|
---|
204 |
|
---|
205 | void FITSTOIReader::run2()
|
---|
206 | // ---- Version bufferisee (Christophe 20/10/2001)
|
---|
207 | {
|
---|
208 | int ncols = outIx.size();
|
---|
209 | cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
|
---|
210 |
|
---|
211 | //////// Prepare buffer, allocate memory
|
---|
212 | double *samplenum = new double[Buff_Sz];
|
---|
213 | vector<double*> colval;
|
---|
214 | vector<long*> colflg;
|
---|
215 | for(int k=0; k<getNOut(); k++) {
|
---|
216 | colval.push_back(NULL);
|
---|
217 | colflg.push_back(NULL);
|
---|
218 | if(!checkOutputTOIIndex(k)) continue;
|
---|
219 | colval[k] = new double[Buff_Sz];
|
---|
220 | if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
|
---|
221 | }
|
---|
222 | uint_8 * tmpflg = new uint_8[Buff_Sz];
|
---|
223 |
|
---|
224 | //////// Read data and put into pipe
|
---|
225 | long ideb=-1,ifin=-1;
|
---|
226 | for(long i=0; i<nrows; i++) {
|
---|
227 |
|
---|
228 | // faut-il lire dans le fichier fits ?
|
---|
229 | if(i<ideb || i>ifin) {
|
---|
230 | ideb = i;
|
---|
231 | ifin = ideb+Buff_Sz-1;
|
---|
232 | if(ifin>=nrows) ifin=nrows-1;
|
---|
233 | long n = ifin-ideb+1;
|
---|
234 | int anyNul;
|
---|
235 | fits_lock();
|
---|
236 | if (implicitSN) {
|
---|
237 | for (long j=0; j<Buff_Sz; j++) {
|
---|
238 | samplenum[j] = j+ideb+implicitSNStart;
|
---|
239 | }
|
---|
240 | } else {
|
---|
241 | fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
|
---|
242 | }
|
---|
243 | for(int k=0; k<getNOut(); k++) {
|
---|
244 | if(colval[k]==NULL) continue;
|
---|
245 | int j = colsinput[k].first;
|
---|
246 | fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
|
---|
247 | if(colflg[k]==NULL) continue;
|
---|
248 | fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
|
---|
249 | }
|
---|
250 | if(fstatus!=0) {
|
---|
251 | fits_report_error(stderr,fstatus);
|
---|
252 | throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
|
---|
253 | }
|
---|
254 | fits_unlock();
|
---|
255 | }
|
---|
256 |
|
---|
257 | long ip = i-ideb; // pointeurs dans les buffers
|
---|
258 | long sn = (long) (samplenum[ip]+.1);
|
---|
259 | TOIManager* mgr = TOIManager::getManager();
|
---|
260 | if(samplenum[0] > mgr->getRequestedEnd()) break;
|
---|
261 | if(samplenum[ifin-ideb] < mgr->getRequestedBegin()) continue;
|
---|
262 | if (ip == 0) {
|
---|
263 | for(int k=0; k<getNOut(); k++) {
|
---|
264 | if(colval[k]==NULL) continue;
|
---|
265 | if (colflg[k] != NULL) {
|
---|
266 | for (int ii=0; ii<Buff_Sz; ii++) {
|
---|
267 | tmpflg[ii] = colflg[k][ii];
|
---|
268 | }
|
---|
269 | putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
|
---|
270 | } else {
|
---|
271 | putData(k, sn, ifin-ideb+1, colval[k]);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 | totnscount++;
|
---|
276 | }
|
---|
277 |
|
---|
278 | //////// des-allocate buffers
|
---|
279 | delete [] samplenum;
|
---|
280 | delete [] tmpflg;
|
---|
281 | {for(int k=0; k<getNOut(); k++) {
|
---|
282 | if(colval[k]!=NULL) delete [] colval[k];
|
---|
283 | if(colflg[k]!=NULL) delete [] colflg[k];
|
---|
284 | }}
|
---|
285 | colval.resize(0); colflg.resize(0);
|
---|
286 |
|
---|
287 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
288 | }
|
---|