[1365] | 1 | #include "fitstoirdr.h"
|
---|
| 2 | #include "toimanager.h"
|
---|
[1696] | 3 | #include <sched.h>
|
---|
[1365] | 4 |
|
---|
| 5 |
|
---|
[1717] | 6 | FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
|
---|
[1365] | 7 | fname = fn;
|
---|
[1480] | 8 | allfn.push_back(fn);
|
---|
[1717] | 9 | Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
|
---|
| 10 | cout<<"FITSTOIReader::FITSTOIReader"<<endl;
|
---|
| 11 | cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
|
---|
[1365] | 12 | name = "rdr";
|
---|
[1480] | 13 | fptr = NULL;
|
---|
[1629] | 14 | totnscount = 0;
|
---|
[1365] | 15 | }
|
---|
| 16 |
|
---|
| 17 | FITSTOIReader::~FITSTOIReader() {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
| 21 | void fits_lock();
|
---|
| 22 | void fits_unlock();
|
---|
| 23 |
|
---|
| 24 | void fits_lock() {
|
---|
| 25 | pthread_mutex_lock(&fits_mutex);
|
---|
| 26 | }
|
---|
| 27 | void fits_unlock() {
|
---|
| 28 | pthread_mutex_unlock(&fits_mutex);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[1480] | 31 | void FITSTOIReader::openFile(string fn) {
|
---|
[1365] | 32 | fits_lock();
|
---|
[1480] | 33 | if (fptr) {
|
---|
| 34 | fits_close_file(fptr,&fstatus);
|
---|
| 35 | fptr = NULL;
|
---|
| 36 | }
|
---|
| 37 | fname = fn;
|
---|
| 38 | cout << "FITSTOIReader::open FileName=" << fname << endl;
|
---|
[1365] | 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();
|
---|
[1721] | 45 | throw IOExc("fitsio error");
|
---|
[1365] | 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 |
|
---|
[1454] | 62 | cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
|
---|
[1480] | 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 | }
|
---|
[1365] | 69 |
|
---|
[1480] | 70 | void FITSTOIReader::init() {
|
---|
| 71 | openFile(allfn.front());
|
---|
| 72 |
|
---|
| 73 | fits_lock();
|
---|
[1365] | 74 | // Dans cette version, on s'attend a ce que la premiere colonne soit le samplenum
|
---|
[1527] | 75 | int itoi=-1;
|
---|
[1365] | 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;
|
---|
[1527] | 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 | }
|
---|
[1365] | 90 | }
|
---|
| 91 | fits_unlock();
|
---|
[1480] | 92 |
|
---|
| 93 | snBegin = firstSn;
|
---|
| 94 |
|
---|
| 95 | openFile(allfn.back());
|
---|
| 96 | snEnd = firstSn+nrows-1;
|
---|
| 97 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
[1365] | 98 | }
|
---|
| 99 |
|
---|
| 100 | int FITSTOIReader::calcMinOut() {
|
---|
[1367] | 101 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 102 | int firstReq = mgr->getRequestedBegin();
|
---|
[1480] | 103 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
[1365] | 104 | }
|
---|
| 105 |
|
---|
| 106 | int FITSTOIReader::calcMaxOut() {
|
---|
[1367] | 107 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 108 | int lastReq = mgr->getRequestedEnd();
|
---|
[1480] | 109 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
[1365] | 110 | }
|
---|
| 111 |
|
---|
[1480] | 112 | void FITSTOIReader::addFile(string fn) {
|
---|
| 113 | allfn.push_back(fn);
|
---|
| 114 | }
|
---|
[1365] | 115 |
|
---|
| 116 | void FITSTOIReader::run() {
|
---|
[1480] | 117 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
| 118 | openFile(*i);
|
---|
[1717] | 119 | //run1();
|
---|
| 120 | run2();
|
---|
[1480] | 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | void FITSTOIReader::run1() {
|
---|
[1365] | 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();
|
---|
[1454] | 129 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
| 130 | << firstSn << endl;
|
---|
[1629] | 131 |
|
---|
[1721] | 132 | double* tabdata = new double[getNOut()];
|
---|
| 133 | uint_8* tabflag = new uint_8[getNOut()];
|
---|
[1712] | 134 |
|
---|
[1721] | 135 | for (long i=0; i<nrows; i++) {
|
---|
[1365] | 136 | int anyNul;
|
---|
| 137 | double y;
|
---|
[1710] | 138 | fits_lock();
|
---|
[1365] | 139 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
[1712] | 140 | //fits_unlock();
|
---|
[1721] | 141 | long sn = (long) (y+.1);
|
---|
[1365] | 142 | TOIManager* mgr = TOIManager::getManager();
|
---|
[1712] | 143 | if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
|
---|
| 144 | if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
|
---|
[1454] | 145 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
[1716] | 146 | int k;
|
---|
| 147 | for (k=0; k<getNOut(); k++) {
|
---|
[1527] | 148 | int j = colsinput[k].first;
|
---|
| 149 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
[1712] | 150 | //fits_lock();
|
---|
[1365] | 151 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
[1712] | 152 | tabdata[k] = y;
|
---|
[1527] | 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 | }
|
---|
[1712] | 157 | tabflag[k] = flg;
|
---|
| 158 | // fits_unlock();
|
---|
| 159 | //putData(k, sn, y, flg);
|
---|
[1365] | 160 | }
|
---|
[1712] | 161 | fits_unlock();
|
---|
[1716] | 162 | for (k=0; k<getNOut(); k++) {
|
---|
[1712] | 163 | putData(k, sn, tabdata[k], tabflag[k]);
|
---|
| 164 | }
|
---|
| 165 | /* if (i%50==0) {
|
---|
| 166 | // fits_unlock();
|
---|
[1696] | 167 | sched_yield();
|
---|
[1712] | 168 | //fits_lock();
|
---|
| 169 | }*/
|
---|
[1629] | 170 | totnscount++;
|
---|
[1365] | 171 | }
|
---|
[1710] | 172 | //fits_unlock();
|
---|
[1721] | 173 | delete[] tabflag;
|
---|
| 174 | delete[] tabdata;
|
---|
[1365] | 175 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 176 | }
|
---|
[1717] | 177 |
|
---|
| 178 | void FITSTOIReader::run2()
|
---|
| 179 | // ---- Version bufferisee (Christophe 20/10/2001)
|
---|
| 180 | {
|
---|
| 181 | int ncols = outIx.size();
|
---|
| 182 | cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
|
---|
| 183 |
|
---|
| 184 | //////// Prepare buffer, allocate memory
|
---|
| 185 | double *samplenum = new double[Buff_Sz];
|
---|
| 186 | vector<double*> colval;
|
---|
| 187 | vector<long*> colflg;
|
---|
| 188 | for(int k=0; k<getNOut(); k++) {
|
---|
| 189 | colval.push_back(NULL);
|
---|
| 190 | colflg.push_back(NULL);
|
---|
| 191 | if(!checkOutputTOIIndex(k)) continue;
|
---|
| 192 | colval[k] = new double[Buff_Sz];
|
---|
| 193 | if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | //////// Read data and put into pipe
|
---|
| 197 | long ideb=-1,ifin=-1;
|
---|
| 198 | for(long i=0; i<nrows; i++) {
|
---|
| 199 |
|
---|
| 200 | // faut-il lire dans le fichier fits ?
|
---|
| 201 | if(i<ideb || i>ifin) {
|
---|
| 202 | ideb = i;
|
---|
| 203 | ifin = ideb+Buff_Sz-1;
|
---|
| 204 | if(ifin>=nrows) ifin=nrows-1;
|
---|
| 205 | long n = ifin-ideb+1;
|
---|
| 206 | int anyNul;
|
---|
| 207 | fits_lock();
|
---|
| 208 | fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
|
---|
| 209 | for(int k=0; k<getNOut(); k++) {
|
---|
| 210 | if(colval[k]==NULL) continue;
|
---|
| 211 | int j = colsinput[k].first;
|
---|
| 212 | fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
|
---|
| 213 | if(colflg[k]==NULL) continue;
|
---|
| 214 | fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
|
---|
| 215 | }
|
---|
| 216 | if(fstatus!=0) {
|
---|
| 217 | fits_report_error(stderr,fstatus);
|
---|
| 218 | throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
|
---|
| 219 | }
|
---|
| 220 | fits_unlock();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | long ip = i-ideb; // pointeurs dans les buffers
|
---|
| 224 | long sn = (long) (samplenum[ip]+.1);
|
---|
| 225 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 226 | if(sn > mgr->getRequestedEnd()) break;
|
---|
| 227 | if(sn < mgr->getRequestedBegin()) continue;
|
---|
| 228 | for(int k=0; k<getNOut(); k++) {
|
---|
| 229 | if(colval[k]==NULL) continue;
|
---|
| 230 | uint_8 flg = (colflg[k]==NULL)? 0: colflg[k][ip];
|
---|
| 231 | putData(k,sn,colval[k][ip],flg);
|
---|
| 232 | }
|
---|
| 233 | totnscount++;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | //////// des-allocate buffers
|
---|
| 237 | delete [] samplenum;
|
---|
| 238 | for(int k=0; k<getNOut(); k++) {
|
---|
| 239 | if(colval[k]!=NULL) delete [] colval[k];
|
---|
| 240 | if(colflg[k]!=NULL) delete [] colflg[k];
|
---|
| 241 | }
|
---|
| 242 | colval.resize(0); colflg.resize(0);
|
---|
| 243 |
|
---|
| 244 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 245 | }
|
---|