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

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

nouveau fits

File size: 6.9 KB
Line 
1#include "fitstoirdr.h"
2#include "toimanager.h"
3#include <sched.h>
4
5
6FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
7 fname = fn;
8 allfn.push_back(fn);
9 Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
10 cout<<"FITSTOIReader::FITSTOIReader"<<endl;
11 cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
12 name = "rdr";
13 fptr = NULL;
14 totnscount = 0;
15}
16
17FITSTOIReader::~FITSTOIReader() {
18}
19
20pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
21void fits_lock();
22void fits_unlock();
23
24void fits_lock() {
25 pthread_mutex_lock(&fits_mutex);
26}
27void fits_unlock() {
28 pthread_mutex_unlock(&fits_mutex);
29}
30
31void FITSTOIReader::openFile(string fn) {
32 fits_lock();
33 if (fptr) {
34 fits_close_file(fptr,&fstatus);
35 fptr = NULL;
36 }
37 fname = fn;
38 cout << "FITSTOIReader::open FileName=" << fname << endl;
39 fstatus = 0;
40 // Open file
41 fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus);
42 if (fstatus != 0) {
43 fits_report_error(stderr, fstatus);
44 fits_unlock();
45 exit (-1); // $CHECK EA: normallement ca devrait etre un "throw"
46 }
47
48 // Go to first extension, which should be a BINTABLE
49
50 int simple, bitpix, naxis;
51 long naxes;
52 long pcount, gcount;
53 int extend;
54 fits_read_imghdr(fptr, 1, &simple, &bitpix,
55 &naxis, &naxes, &pcount, &gcount, &extend, &fstatus);
56
57 fits_movabs_hdu(fptr, 2, NULL, &fstatus);
58
59 fits_get_num_cols(fptr,&ncols,&fstatus);
60 fits_get_num_rows(fptr,&nrows,&fstatus);
61
62 cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
63 int anyNul;
64 double y;
65 fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus);
66 firstSn = (int) (y+.1);
67 fits_unlock();
68}
69
70void FITSTOIReader::init() {
71 openFile(allfn.front());
72
73 fits_lock();
74 // Dans cette version, on s'attend a ce que la premiere colonne soit le samplenum
75 int itoi=-1;
76 for (int i=1; i<ncols; i++) {
77 char templt[10];
78 sprintf(templt, "%d", i+1);
79 char colname[200];
80 int colnum;
81 fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
82 cout << "FITSTOIReader col " << colname << endl;
83 if (!strncmp(colname, "fg_", 3)) {
84 colsinput[itoi].second=true;
85 } else {
86 declareOutput(colname);
87 itoi++;
88 colsinput[itoi] = pair<int,bool>(i,false);
89 }
90 }
91 fits_unlock();
92
93 snBegin = firstSn;
94
95 openFile(allfn.back());
96 snEnd = firstSn+nrows-1;
97 cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
98}
99
100int FITSTOIReader::calcMinOut() {
101 TOIManager* mgr = TOIManager::getManager();
102 int firstReq = mgr->getRequestedBegin();
103 return snBegin > firstReq ? snBegin : firstReq;
104}
105
106int FITSTOIReader::calcMaxOut() {
107 TOIManager* mgr = TOIManager::getManager();
108 int lastReq = mgr->getRequestedEnd();
109 return snEnd < lastReq ? snEnd : lastReq;
110}
111
112void FITSTOIReader::addFile(string fn) {
113 allfn.push_back(fn);
114}
115
116void FITSTOIReader::run() {
117 for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
118 openFile(*i);
119 //run1();
120 run2();
121 }
122}
123
124void FITSTOIReader::run1() {
125 // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
126 // mais cela implique de gerer aussi bien echant uniforme que non.
127 // On pourrait aussi lire plusieurs elements d'un coup.
128 int ncols = outIx.size();
129 cout << "reader reading... NRows=" << nrows << " firstSn= "
130 << firstSn << endl;
131
132 double* tabdata = new double[getNOut()]; // $CHECK EA: pas terrible le new never deleted
133 uint_8* tabflag = new uint_8[getNOut()]; // $CHECK EA: pas terrible le new never deleted
134
135 for (int i=0; i<nrows; i++) { // $CHECK EA: vaut mieux mettre long que int
136 int anyNul;
137 double y;
138 fits_lock();
139 fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
140 //fits_unlock();
141 int sn = (int) (y+.1); // $CHECK EA: vaut mieux mettre long que int SURTOUT POUR SNUM
142 TOIManager* mgr = TOIManager::getManager();
143 if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
144 if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
145 // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
146 int k;
147 for (k=0; k<getNOut(); k++) {
148 int j = colsinput[k].first;
149 if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
150 //fits_lock();
151 fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
152 tabdata[k] = y;
153 long flg = 0;
154 if (colsinput[k].second) {
155 fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
156 }
157 tabflag[k] = flg;
158 // fits_unlock();
159 //putData(k, sn, y, flg);
160 }
161 fits_unlock();
162 for (k=0; k<getNOut(); k++) {
163 putData(k, sn, tabdata[k], tabflag[k]);
164 }
165 /* if (i%50==0) {
166 // fits_unlock();
167 sched_yield();
168 //fits_lock();
169 }*/
170 totnscount++;
171 }
172 //fits_unlock();
173 cout << "reader done reading... " << pthread_self() << endl;
174}
175
176void FITSTOIReader::run2()
177// ---- Version bufferisee (Christophe 20/10/2001)
178{
179 int ncols = outIx.size();
180 cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
181
182 //////// Prepare buffer, allocate memory
183 double *samplenum = new double[Buff_Sz];
184 vector<double*> colval;
185 vector<long*> colflg;
186 for(int k=0; k<getNOut(); k++) {
187 colval.push_back(NULL);
188 colflg.push_back(NULL);
189 if(!checkOutputTOIIndex(k)) continue;
190 colval[k] = new double[Buff_Sz];
191 if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
192 }
193
194 //////// Read data and put into pipe
195 long ideb=-1,ifin=-1;
196 for(long i=0; i<nrows; i++) {
197
198 // faut-il lire dans le fichier fits ?
199 if(i<ideb || i>ifin) {
200 ideb = i;
201 ifin = ideb+Buff_Sz-1;
202 if(ifin>=nrows) ifin=nrows-1;
203 long n = ifin-ideb+1;
204 int anyNul;
205 fits_lock();
206 fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
207 for(int k=0; k<getNOut(); k++) {
208 if(colval[k]==NULL) continue;
209 int j = colsinput[k].first;
210 fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
211 if(colflg[k]==NULL) continue;
212 fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
213 }
214 if(fstatus!=0) {
215 fits_report_error(stderr,fstatus);
216 // $CHECK EA: attention j'ai mis un throw OK avec ca ?
217 throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
218 }
219 fits_unlock();
220 }
221
222 long ip = i-ideb; // pointeurs dans les buffers
223 long sn = (long) (samplenum[ip]+.1);
224 TOIManager* mgr = TOIManager::getManager();
225 if(sn > mgr->getRequestedEnd()) break;
226 if(sn < mgr->getRequestedBegin()) continue;
227 for(int k=0; k<getNOut(); k++) {
228 if(colval[k]==NULL) continue;
229 uint_8 flg = (colflg[k]==NULL)? 0: colflg[k][ip];
230 putData(k,sn,colval[k][ip],flg);
231 }
232 totnscount++;
233 }
234
235 //////// des-allocate buffers
236 delete [] samplenum;
237 for(int k=0; k<getNOut(); k++) {
238 if(colval[k]!=NULL) delete [] colval[k];
239 if(colflg[k]!=NULL) delete [] colflg[k];
240 }
241 colval.resize(0); colflg.resize(0);
242
243 cout << "reader done reading... " << pthread_self() << endl;
244}
Note: See TracBrowser for help on using the repository browser.