1 | #include "fitstoirdr.h"
|
---|
2 | #include "toimanager.h"
|
---|
3 | #include <sched.h>
|
---|
4 |
|
---|
5 |
|
---|
6 | FITSTOIReader::FITSTOIReader(string fn) {
|
---|
7 | fname = fn;
|
---|
8 | allfn.push_back(fn);
|
---|
9 | cout << "FITSTOIReader::FITSTOIReader" << endl;
|
---|
10 | cout << "FITSTOIReader::inited " << inited << endl;
|
---|
11 | name = "rdr";
|
---|
12 | fptr = NULL;
|
---|
13 | totnscount = 0;
|
---|
14 | }
|
---|
15 |
|
---|
16 | FITSTOIReader::~FITSTOIReader() {
|
---|
17 | }
|
---|
18 |
|
---|
19 | pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
20 | void fits_lock();
|
---|
21 | void fits_unlock();
|
---|
22 |
|
---|
23 | void fits_lock() {
|
---|
24 | pthread_mutex_lock(&fits_mutex);
|
---|
25 | }
|
---|
26 | void fits_unlock() {
|
---|
27 | pthread_mutex_unlock(&fits_mutex);
|
---|
28 | }
|
---|
29 |
|
---|
30 | void FITSTOIReader::openFile(string fn) {
|
---|
31 | fits_lock();
|
---|
32 | if (fptr) {
|
---|
33 | fits_close_file(fptr,&fstatus);
|
---|
34 | fptr = NULL;
|
---|
35 | }
|
---|
36 | fname = fn;
|
---|
37 | cout << "FITSTOIReader::open FileName=" << fname << endl;
|
---|
38 | fstatus = 0;
|
---|
39 | // Open file
|
---|
40 | fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus);
|
---|
41 | if (fstatus != 0) {
|
---|
42 | fits_report_error(stderr, fstatus);
|
---|
43 | fits_unlock();
|
---|
44 | exit (-1);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // Go to first extension, which should be a BINTABLE
|
---|
48 |
|
---|
49 | int simple, bitpix, naxis;
|
---|
50 | long naxes;
|
---|
51 | long pcount, gcount;
|
---|
52 | int extend;
|
---|
53 | fits_read_imghdr(fptr, 1, &simple, &bitpix,
|
---|
54 | &naxis, &naxes, &pcount, &gcount, &extend, &fstatus);
|
---|
55 |
|
---|
56 | fits_movabs_hdu(fptr, 2, NULL, &fstatus);
|
---|
57 |
|
---|
58 | fits_get_num_cols(fptr,&ncols,&fstatus);
|
---|
59 | fits_get_num_rows(fptr,&nrows,&fstatus);
|
---|
60 |
|
---|
61 | cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
|
---|
62 | int anyNul;
|
---|
63 | double y;
|
---|
64 | fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus);
|
---|
65 | firstSn = (int) (y+.1);
|
---|
66 | fits_unlock();
|
---|
67 | }
|
---|
68 |
|
---|
69 | void FITSTOIReader::init() {
|
---|
70 | openFile(allfn.front());
|
---|
71 |
|
---|
72 | fits_lock();
|
---|
73 | // Dans cette version, on s'attend a ce que la premiere colonne soit le samplenum
|
---|
74 | int itoi=-1;
|
---|
75 | for (int i=1; i<ncols; i++) {
|
---|
76 | char templt[10];
|
---|
77 | sprintf(templt, "%d", i+1);
|
---|
78 | char colname[200];
|
---|
79 | int colnum;
|
---|
80 | fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
|
---|
81 | cout << "FITSTOIReader col " << colname << endl;
|
---|
82 | if (!strncmp(colname, "fg_", 3)) {
|
---|
83 | colsinput[itoi].second=true;
|
---|
84 | } else {
|
---|
85 | declareOutput(colname);
|
---|
86 | itoi++;
|
---|
87 | colsinput[itoi] = pair<int,bool>(i,false);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | fits_unlock();
|
---|
91 |
|
---|
92 | snBegin = firstSn;
|
---|
93 |
|
---|
94 | openFile(allfn.back());
|
---|
95 | snEnd = firstSn+nrows-1;
|
---|
96 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
97 | }
|
---|
98 |
|
---|
99 | int FITSTOIReader::calcMinOut() {
|
---|
100 | TOIManager* mgr = TOIManager::getManager();
|
---|
101 | int firstReq = mgr->getRequestedBegin();
|
---|
102 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
103 | }
|
---|
104 |
|
---|
105 | int FITSTOIReader::calcMaxOut() {
|
---|
106 | TOIManager* mgr = TOIManager::getManager();
|
---|
107 | int lastReq = mgr->getRequestedEnd();
|
---|
108 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void FITSTOIReader::addFile(string fn) {
|
---|
112 | allfn.push_back(fn);
|
---|
113 | }
|
---|
114 |
|
---|
115 | void FITSTOIReader::run() {
|
---|
116 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
117 | openFile(*i);
|
---|
118 | run1();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void FITSTOIReader::run1() {
|
---|
123 | // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
|
---|
124 | // mais cela implique de gerer aussi bien echant uniforme que non.
|
---|
125 | // On pourrait aussi lire plusieurs elements d'un coup.
|
---|
126 | int ncols = outIx.size();
|
---|
127 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
128 | << firstSn << endl;
|
---|
129 |
|
---|
130 | double* tabdata = new double[getNOut()];
|
---|
131 | uint_8* tabflag = new uint_8[getNOut()];
|
---|
132 |
|
---|
133 | for (int i=0; i<nrows; i++) {
|
---|
134 | int anyNul;
|
---|
135 | double y;
|
---|
136 | fits_lock();
|
---|
137 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
138 | //fits_unlock();
|
---|
139 | int sn = (int) (y+.1);
|
---|
140 | TOIManager* mgr = TOIManager::getManager();
|
---|
141 | if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
|
---|
142 | if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
|
---|
143 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
144 | for (int k=0; k<getNOut(); k++) {
|
---|
145 | int j = colsinput[k].first;
|
---|
146 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
147 | //fits_lock();
|
---|
148 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
149 | tabdata[k] = y;
|
---|
150 | long flg = 0;
|
---|
151 | if (colsinput[k].second) {
|
---|
152 | fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
|
---|
153 | }
|
---|
154 | tabflag[k] = flg;
|
---|
155 | // fits_unlock();
|
---|
156 | //putData(k, sn, y, flg);
|
---|
157 | }
|
---|
158 | fits_unlock();
|
---|
159 | for (int k=0; k<getNOut(); k++) {
|
---|
160 | putData(k, sn, tabdata[k], tabflag[k]);
|
---|
161 | }
|
---|
162 | /* if (i%50==0) {
|
---|
163 | // fits_unlock();
|
---|
164 | sched_yield();
|
---|
165 | //fits_lock();
|
---|
166 | }*/
|
---|
167 | totnscount++;
|
---|
168 | }
|
---|
169 | //fits_unlock();
|
---|
170 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
171 | }
|
---|