[1738] | 1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
| 2 | // Eric Aubourg
|
---|
| 3 | // Christophe Magneville
|
---|
| 4 | // Reza Ansari
|
---|
[2011] | 5 | // $Id: fitstoirdr.cc,v 1.28 2002-05-16 14:39:08 ansari Exp $
|
---|
[1738] | 6 |
|
---|
[1365] | 7 | #include "fitstoirdr.h"
|
---|
| 8 | #include "toimanager.h"
|
---|
[1696] | 9 | #include <sched.h>
|
---|
[1365] | 10 |
|
---|
| 11 |
|
---|
[1717] | 12 | FITSTOIReader::FITSTOIReader(string fn,int buff_sz) {
|
---|
[1365] | 13 | fname = fn;
|
---|
[1480] | 14 | allfn.push_back(fn);
|
---|
[1717] | 15 | Buff_Sz = (buff_sz>0) ? buff_sz: 1000;
|
---|
| 16 | cout<<"FITSTOIReader::FITSTOIReader"<<endl;
|
---|
| 17 | cout<<"FITSTOIReader::inited "<<inited<<" bsz="<<Buff_Sz<<endl;
|
---|
[1365] | 18 | name = "rdr";
|
---|
[1480] | 19 | fptr = NULL;
|
---|
[1629] | 20 | totnscount = 0;
|
---|
[1725] | 21 |
|
---|
| 22 | implicitSN = false;
|
---|
| 23 | implicitSNStart = 0;
|
---|
[1365] | 24 | }
|
---|
| 25 |
|
---|
| 26 | FITSTOIReader::~FITSTOIReader() {
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[1994] | 29 | void FITSTOIReader::setImplicitSN(long snStart) {
|
---|
[1725] | 30 | implicitSN = true;
|
---|
| 31 | implicitSNStart = snStart;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[1994] | 34 | void FITSTOIReader::setBufferSize(int buffsz)
|
---|
| 35 | {
|
---|
| 36 | Buff_Sz = (buffsz>0) ? buffsz: 1024;
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[1365] | 40 | pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
| 41 | void fits_lock();
|
---|
| 42 | void fits_unlock();
|
---|
| 43 |
|
---|
| 44 | void fits_lock() {
|
---|
| 45 | pthread_mutex_lock(&fits_mutex);
|
---|
| 46 | }
|
---|
| 47 | void fits_unlock() {
|
---|
| 48 | pthread_mutex_unlock(&fits_mutex);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[1480] | 51 | void FITSTOIReader::openFile(string fn) {
|
---|
[1365] | 52 | fits_lock();
|
---|
[1480] | 53 | if (fptr) {
|
---|
| 54 | fits_close_file(fptr,&fstatus);
|
---|
| 55 | fptr = NULL;
|
---|
| 56 | }
|
---|
| 57 | fname = fn;
|
---|
| 58 | cout << "FITSTOIReader::open FileName=" << fname << endl;
|
---|
[1365] | 59 | fstatus = 0;
|
---|
| 60 | // Open file
|
---|
| 61 | fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus);
|
---|
| 62 | if (fstatus != 0) {
|
---|
| 63 | fits_report_error(stderr, fstatus);
|
---|
| 64 | fits_unlock();
|
---|
[1721] | 65 | throw IOExc("fitsio error");
|
---|
[1365] | 66 | }
|
---|
| 67 |
|
---|
| 68 | // Go to first extension, which should be a BINTABLE
|
---|
| 69 |
|
---|
| 70 | int simple, bitpix, naxis;
|
---|
| 71 | long naxes;
|
---|
| 72 | long pcount, gcount;
|
---|
| 73 | int extend;
|
---|
| 74 | fits_read_imghdr(fptr, 1, &simple, &bitpix,
|
---|
| 75 | &naxis, &naxes, &pcount, &gcount, &extend, &fstatus);
|
---|
| 76 |
|
---|
| 77 | fits_movabs_hdu(fptr, 2, NULL, &fstatus);
|
---|
| 78 |
|
---|
| 79 | fits_get_num_cols(fptr,&ncols,&fstatus);
|
---|
| 80 | fits_get_num_rows(fptr,&nrows,&fstatus);
|
---|
| 81 |
|
---|
[1454] | 82 | cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
|
---|
[1725] | 83 | if (implicitSN) {
|
---|
| 84 | firstSn = implicitSNStart;
|
---|
| 85 | } else {
|
---|
| 86 | int anyNul;
|
---|
| 87 | double y;
|
---|
| 88 | fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus);
|
---|
| 89 | firstSn = (int) (y+.1);
|
---|
| 90 | }
|
---|
[1480] | 91 | fits_unlock();
|
---|
| 92 | }
|
---|
[1365] | 93 |
|
---|
[1480] | 94 | void FITSTOIReader::init() {
|
---|
| 95 | openFile(allfn.front());
|
---|
| 96 |
|
---|
| 97 | fits_lock();
|
---|
[1725] | 98 | // si pas implicitSN, la premiere colonne est le sampleNum.
|
---|
| 99 | // Sinon, le samplenum est la fitsline + offset.
|
---|
[1527] | 100 | int itoi=-1;
|
---|
[1725] | 101 | int col1 = implicitSN ? 0 : 1;
|
---|
| 102 | for (int i=col1; i<ncols; i++) {
|
---|
[1365] | 103 | char templt[10];
|
---|
| 104 | sprintf(templt, "%d", i+1);
|
---|
| 105 | char colname[200];
|
---|
| 106 | int colnum;
|
---|
| 107 | fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
|
---|
| 108 | cout << "FITSTOIReader col " << colname << endl;
|
---|
[1527] | 109 | if (!strncmp(colname, "fg_", 3)) {
|
---|
| 110 | colsinput[itoi].second=true;
|
---|
| 111 | } else {
|
---|
| 112 | declareOutput(colname);
|
---|
| 113 | itoi++;
|
---|
| 114 | colsinput[itoi] = pair<int,bool>(i,false);
|
---|
| 115 | }
|
---|
[1365] | 116 | }
|
---|
| 117 | fits_unlock();
|
---|
[1480] | 118 |
|
---|
| 119 | snBegin = firstSn;
|
---|
[1800] | 120 | if (forcedMinIn > 0 && forcedMinIn > snBegin) {
|
---|
| 121 | snBegin = forcedMinIn;
|
---|
[1787] | 122 | }
|
---|
[1480] | 123 | openFile(allfn.back());
|
---|
| 124 | snEnd = firstSn+nrows-1;
|
---|
[1800] | 125 | if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
|
---|
| 126 | snEnd = forcedMaxIn;
|
---|
| 127 | }
|
---|
[1480] | 128 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
[1365] | 129 | }
|
---|
| 130 |
|
---|
| 131 | int FITSTOIReader::calcMinOut() {
|
---|
[1800] | 132 | chkinit();
|
---|
[1367] | 133 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 134 | int firstReq = mgr->getRequestedBegin();
|
---|
[1480] | 135 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
[1365] | 136 | }
|
---|
| 137 |
|
---|
| 138 | int FITSTOIReader::calcMaxOut() {
|
---|
[1800] | 139 | chkinit();
|
---|
[1367] | 140 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 141 | int lastReq = mgr->getRequestedEnd();
|
---|
[1480] | 142 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
[1365] | 143 | }
|
---|
| 144 |
|
---|
[1480] | 145 | void FITSTOIReader::addFile(string fn) {
|
---|
| 146 | allfn.push_back(fn);
|
---|
| 147 | }
|
---|
[1365] | 148 |
|
---|
| 149 | void FITSTOIReader::run() {
|
---|
[1480] | 150 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
| 151 | openFile(*i);
|
---|
[1994] | 152 | if (Buff_Sz > 1) run2(); // Lecture bufferise
|
---|
| 153 | else run1(); // Lecture un echantillon a la fois
|
---|
[1480] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[1725] | 157 | // run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
|
---|
[1994] | 158 | // ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter
|
---|
| 159 | // implicitSNStart et switvh run1/run2
|
---|
| 160 | // suivant buffersize
|
---|
| 161 |
|
---|
[1480] | 162 | void FITSTOIReader::run1() {
|
---|
[1365] | 163 | // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
|
---|
| 164 | // mais cela implique de gerer aussi bien echant uniforme que non.
|
---|
| 165 | // On pourrait aussi lire plusieurs elements d'un coup.
|
---|
| 166 | int ncols = outIx.size();
|
---|
[1454] | 167 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
| 168 | << firstSn << endl;
|
---|
[1629] | 169 |
|
---|
[1721] | 170 | double* tabdata = new double[getNOut()];
|
---|
| 171 | uint_8* tabflag = new uint_8[getNOut()];
|
---|
[1712] | 172 |
|
---|
[1721] | 173 | for (long i=0; i<nrows; i++) {
|
---|
[1365] | 174 | int anyNul;
|
---|
| 175 | double y;
|
---|
[1710] | 176 | fits_lock();
|
---|
[1994] | 177 | long sn;
|
---|
| 178 | if (implicitSN) {
|
---|
| 179 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
| 180 | sn = (long) (y+.1);
|
---|
| 181 | }
|
---|
| 182 | else sn = implicitSNStart+i;
|
---|
[1712] | 183 | //fits_unlock();
|
---|
[1365] | 184 | TOIManager* mgr = TOIManager::getManager();
|
---|
[1712] | 185 | if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
|
---|
| 186 | if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
|
---|
[1454] | 187 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
[1716] | 188 | int k;
|
---|
| 189 | for (k=0; k<getNOut(); k++) {
|
---|
[1527] | 190 | int j = colsinput[k].first;
|
---|
| 191 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
[1712] | 192 | //fits_lock();
|
---|
[1365] | 193 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
[1712] | 194 | tabdata[k] = y;
|
---|
[1527] | 195 | long flg = 0;
|
---|
| 196 | if (colsinput[k].second) {
|
---|
| 197 | fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
|
---|
| 198 | }
|
---|
[1712] | 199 | tabflag[k] = flg;
|
---|
| 200 | // fits_unlock();
|
---|
| 201 | //putData(k, sn, y, flg);
|
---|
[1365] | 202 | }
|
---|
[1712] | 203 | fits_unlock();
|
---|
[1716] | 204 | for (k=0; k<getNOut(); k++) {
|
---|
[1712] | 205 | putData(k, sn, tabdata[k], tabflag[k]);
|
---|
| 206 | }
|
---|
| 207 | /* if (i%50==0) {
|
---|
| 208 | // fits_unlock();
|
---|
[1696] | 209 | sched_yield();
|
---|
[1712] | 210 | //fits_lock();
|
---|
| 211 | }*/
|
---|
[1629] | 212 | totnscount++;
|
---|
[1365] | 213 | }
|
---|
[1710] | 214 | //fits_unlock();
|
---|
[1721] | 215 | delete[] tabflag;
|
---|
| 216 | delete[] tabdata;
|
---|
[1365] | 217 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 218 | }
|
---|
[1717] | 219 |
|
---|
| 220 | void FITSTOIReader::run2()
|
---|
| 221 | // ---- Version bufferisee (Christophe 20/10/2001)
|
---|
| 222 | {
|
---|
| 223 | int ncols = outIx.size();
|
---|
| 224 | cout<<"reader reading... NRows="<<nrows<<" firstSn= " <<firstSn<<endl;
|
---|
| 225 |
|
---|
| 226 | //////// Prepare buffer, allocate memory
|
---|
| 227 | double *samplenum = new double[Buff_Sz];
|
---|
| 228 | vector<double*> colval;
|
---|
| 229 | vector<long*> colflg;
|
---|
| 230 | for(int k=0; k<getNOut(); k++) {
|
---|
| 231 | colval.push_back(NULL);
|
---|
| 232 | colflg.push_back(NULL);
|
---|
| 233 | if(!checkOutputTOIIndex(k)) continue;
|
---|
| 234 | colval[k] = new double[Buff_Sz];
|
---|
| 235 | if(colsinput[k].second) colflg[k] = new long[Buff_Sz];
|
---|
| 236 | }
|
---|
[1744] | 237 | uint_8 * tmpflg = new uint_8[Buff_Sz];
|
---|
[1717] | 238 |
|
---|
[1801] | 239 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 240 |
|
---|
[1717] | 241 | //////// Read data and put into pipe
|
---|
| 242 | long ideb=-1,ifin=-1;
|
---|
[1836] | 243 | for(long i=0; i<nrows; i+=Buff_Sz) {
|
---|
[1994] | 244 | fits_lock(); // lock en debut de boucle de lecture - Reza 13/05/2002
|
---|
[1717] | 245 | // faut-il lire dans le fichier fits ?
|
---|
[1836] | 246 | if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
|
---|
[1717] | 247 | ideb = i;
|
---|
| 248 | ifin = ideb+Buff_Sz-1;
|
---|
| 249 | if(ifin>=nrows) ifin=nrows-1;
|
---|
| 250 | long n = ifin-ideb+1;
|
---|
| 251 | int anyNul;
|
---|
[1725] | 252 | if (implicitSN) {
|
---|
[1801] | 253 | if (ideb+implicitSNStart > mgr->getRequestedEnd()
|
---|
[2011] | 254 | || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) {
|
---|
| 255 | fits_unlock(); // unlock avant de casser la boucle - Reza 16/05/2002
|
---|
| 256 | break;
|
---|
| 257 | }
|
---|
[1801] | 258 | if (ifin+implicitSNStart < mgr->getRequestedBegin()
|
---|
[2011] | 259 | || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) {
|
---|
| 260 | fits_unlock(); // unlock avant de continuer - Reza 16/05/2002
|
---|
| 261 | continue;
|
---|
| 262 | }
|
---|
[1725] | 263 | for (long j=0; j<Buff_Sz; j++) {
|
---|
| 264 | samplenum[j] = j+ideb+implicitSNStart;
|
---|
| 265 | }
|
---|
[1994] | 266 | //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
|
---|
[1725] | 267 | } else {
|
---|
[1994] | 268 | //---- Pas la peine, fait en debut de boucle - Reza 13/05/2002 fits_lock();
|
---|
[1725] | 269 | fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
|
---|
[1836] | 270 | if (samplenum[0] > mgr->getRequestedEnd()
|
---|
| 271 | || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
|
---|
[1994] | 272 | fits_unlock(); // unlock avant de casser la boucle - Reza 13/05/2002
|
---|
[1801] | 273 | break;
|
---|
| 274 | }
|
---|
[1836] | 275 | if (samplenum[n-1] < mgr->getRequestedBegin()
|
---|
| 276 | || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
|
---|
[2011] | 277 | fits_unlock();
|
---|
[1801] | 278 | continue;
|
---|
| 279 | }
|
---|
[1725] | 280 | }
|
---|
[1717] | 281 | for(int k=0; k<getNOut(); k++) {
|
---|
| 282 | if(colval[k]==NULL) continue;
|
---|
| 283 | int j = colsinput[k].first;
|
---|
| 284 | fits_read_col_dbl(fptr,j+1,ideb+1,1,n,0,colval[k],&anyNul,&fstatus);
|
---|
| 285 | if(colflg[k]==NULL) continue;
|
---|
| 286 | fits_read_col_lng(fptr,j+2,ideb+1,1,n,0,colflg[k],&anyNul,&fstatus);
|
---|
| 287 | }
|
---|
| 288 | if(fstatus!=0) {
|
---|
| 289 | fits_report_error(stderr,fstatus);
|
---|
[1994] | 290 | fits_unlock(); // On supprime le lock avant le throw - Reza 13/05/2002
|
---|
[1717] | 291 | throw RangeCheckError("FITSTOIReader::run2: Error Reading Fits file\n");
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
[2011] | 294 | fits_unlock(); // unlock en fin de boucle de lecture - Reza 13/05/2002
|
---|
[1717] | 295 |
|
---|
| 296 | long ip = i-ideb; // pointeurs dans les buffers
|
---|
| 297 | long sn = (long) (samplenum[ip]+.1);
|
---|
[1744] | 298 | if (ip == 0) {
|
---|
| 299 | for(int k=0; k<getNOut(); k++) {
|
---|
| 300 | if(colval[k]==NULL) continue;
|
---|
| 301 | if (colflg[k] != NULL) {
|
---|
| 302 | for (int ii=0; ii<Buff_Sz; ii++) {
|
---|
| 303 | tmpflg[ii] = colflg[k][ii];
|
---|
| 304 | }
|
---|
| 305 | putData(k, sn, ifin-ideb+1, colval[k], tmpflg);
|
---|
| 306 | } else {
|
---|
| 307 | putData(k, sn, ifin-ideb+1, colval[k]);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
[1994] | 311 | totnscount += (ifin-ideb+1);
|
---|
[1717] | 312 | }
|
---|
| 313 |
|
---|
| 314 | //////// des-allocate buffers
|
---|
| 315 | delete [] samplenum;
|
---|
[1744] | 316 | delete [] tmpflg;
|
---|
[1763] | 317 | {for(int k=0; k<getNOut(); k++) {
|
---|
[1717] | 318 | if(colval[k]!=NULL) delete [] colval[k];
|
---|
| 319 | if(colflg[k]!=NULL) delete [] colflg[k];
|
---|
[1763] | 320 | }}
|
---|
[1717] | 321 | colval.resize(0); colflg.resize(0);
|
---|
| 322 |
|
---|
[1994] | 323 | cout << "reader (buffered) done reading... " << pthread_self() << endl;
|
---|
[1717] | 324 | }
|
---|