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