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