[1738] | 1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
| 2 | // Eric Aubourg
|
---|
| 3 | // Christophe Magneville
|
---|
| 4 | // Reza Ansari
|
---|
[1836] | 5 | // $Id: fitstoirdr.cc,v 1.26 2001-12-21 17:00:40 aubourg 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 |
|
---|
[1725] | 29 | void FITSTOIReader::setImplicitSN(int snStart) {
|
---|
| 30 | implicitSN = true;
|
---|
| 31 | implicitSNStart = snStart;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[1365] | 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 |
|
---|
[1480] | 45 | void FITSTOIReader::openFile(string fn) {
|
---|
[1365] | 46 | fits_lock();
|
---|
[1480] | 47 | if (fptr) {
|
---|
| 48 | fits_close_file(fptr,&fstatus);
|
---|
| 49 | fptr = NULL;
|
---|
| 50 | }
|
---|
| 51 | fname = fn;
|
---|
| 52 | cout << "FITSTOIReader::open FileName=" << fname << endl;
|
---|
[1365] | 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();
|
---|
[1721] | 59 | throw IOExc("fitsio error");
|
---|
[1365] | 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 |
|
---|
[1454] | 76 | cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
|
---|
[1725] | 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 | }
|
---|
[1480] | 85 | fits_unlock();
|
---|
| 86 | }
|
---|
[1365] | 87 |
|
---|
[1480] | 88 | void FITSTOIReader::init() {
|
---|
| 89 | openFile(allfn.front());
|
---|
| 90 |
|
---|
| 91 | fits_lock();
|
---|
[1725] | 92 | // si pas implicitSN, la premiere colonne est le sampleNum.
|
---|
| 93 | // Sinon, le samplenum est la fitsline + offset.
|
---|
[1527] | 94 | int itoi=-1;
|
---|
[1725] | 95 | int col1 = implicitSN ? 0 : 1;
|
---|
| 96 | for (int i=col1; i<ncols; i++) {
|
---|
[1365] | 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;
|
---|
[1527] | 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 | }
|
---|
[1365] | 110 | }
|
---|
| 111 | fits_unlock();
|
---|
[1480] | 112 |
|
---|
| 113 | snBegin = firstSn;
|
---|
[1800] | 114 | if (forcedMinIn > 0 && forcedMinIn > snBegin) {
|
---|
| 115 | snBegin = forcedMinIn;
|
---|
[1787] | 116 | }
|
---|
[1480] | 117 | openFile(allfn.back());
|
---|
| 118 | snEnd = firstSn+nrows-1;
|
---|
[1800] | 119 | if (forcedMaxIn > 0 && forcedMaxIn < snEnd) {
|
---|
| 120 | snEnd = forcedMaxIn;
|
---|
| 121 | }
|
---|
[1480] | 122 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
[1365] | 123 | }
|
---|
| 124 |
|
---|
| 125 | int FITSTOIReader::calcMinOut() {
|
---|
[1800] | 126 | chkinit();
|
---|
[1367] | 127 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 128 | int firstReq = mgr->getRequestedBegin();
|
---|
[1480] | 129 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
[1365] | 130 | }
|
---|
| 131 |
|
---|
| 132 | int FITSTOIReader::calcMaxOut() {
|
---|
[1800] | 133 | chkinit();
|
---|
[1367] | 134 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 135 | int lastReq = mgr->getRequestedEnd();
|
---|
[1480] | 136 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
[1365] | 137 | }
|
---|
| 138 |
|
---|
[1480] | 139 | void FITSTOIReader::addFile(string fn) {
|
---|
| 140 | allfn.push_back(fn);
|
---|
| 141 | }
|
---|
[1365] | 142 |
|
---|
| 143 | void FITSTOIReader::run() {
|
---|
[1480] | 144 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
| 145 | openFile(*i);
|
---|
[1717] | 146 | //run1();
|
---|
| 147 | run2();
|
---|
[1480] | 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[1725] | 151 | // run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN.
|
---|
[1480] | 152 | void FITSTOIReader::run1() {
|
---|
[1365] | 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();
|
---|
[1454] | 157 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
| 158 | << firstSn << endl;
|
---|
[1629] | 159 |
|
---|
[1721] | 160 | double* tabdata = new double[getNOut()];
|
---|
| 161 | uint_8* tabflag = new uint_8[getNOut()];
|
---|
[1712] | 162 |
|
---|
[1721] | 163 | for (long i=0; i<nrows; i++) {
|
---|
[1365] | 164 | int anyNul;
|
---|
| 165 | double y;
|
---|
[1710] | 166 | fits_lock();
|
---|
[1365] | 167 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
[1712] | 168 | //fits_unlock();
|
---|
[1721] | 169 | long sn = (long) (y+.1);
|
---|
[1365] | 170 | TOIManager* mgr = TOIManager::getManager();
|
---|
[1712] | 171 | if (sn > mgr->getRequestedEnd()) {fits_unlock(); break;}
|
---|
| 172 | if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;}
|
---|
[1454] | 173 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
[1716] | 174 | int k;
|
---|
| 175 | for (k=0; k<getNOut(); k++) {
|
---|
[1527] | 176 | int j = colsinput[k].first;
|
---|
| 177 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
[1712] | 178 | //fits_lock();
|
---|
[1365] | 179 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
[1712] | 180 | tabdata[k] = y;
|
---|
[1527] | 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 | }
|
---|
[1712] | 185 | tabflag[k] = flg;
|
---|
| 186 | // fits_unlock();
|
---|
| 187 | //putData(k, sn, y, flg);
|
---|
[1365] | 188 | }
|
---|
[1712] | 189 | fits_unlock();
|
---|
[1716] | 190 | for (k=0; k<getNOut(); k++) {
|
---|
[1712] | 191 | putData(k, sn, tabdata[k], tabflag[k]);
|
---|
| 192 | }
|
---|
| 193 | /* if (i%50==0) {
|
---|
| 194 | // fits_unlock();
|
---|
[1696] | 195 | sched_yield();
|
---|
[1712] | 196 | //fits_lock();
|
---|
| 197 | }*/
|
---|
[1629] | 198 | totnscount++;
|
---|
[1365] | 199 | }
|
---|
[1710] | 200 | //fits_unlock();
|
---|
[1721] | 201 | delete[] tabflag;
|
---|
| 202 | delete[] tabdata;
|
---|
[1365] | 203 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 204 | }
|
---|
[1717] | 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 | }
|
---|
[1744] | 223 | uint_8 * tmpflg = new uint_8[Buff_Sz];
|
---|
[1717] | 224 |
|
---|
[1801] | 225 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 226 |
|
---|
[1717] | 227 | //////// Read data and put into pipe
|
---|
| 228 | long ideb=-1,ifin=-1;
|
---|
[1836] | 229 | for(long i=0; i<nrows; i+=Buff_Sz) {
|
---|
[1717] | 230 |
|
---|
| 231 | // faut-il lire dans le fichier fits ?
|
---|
[1836] | 232 | if(i<ideb || i>ifin) { // Toujours vrai avec le += Buff_Sz
|
---|
[1717] | 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;
|
---|
[1725] | 238 | if (implicitSN) {
|
---|
[1801] | 239 | if (ideb+implicitSNStart > mgr->getRequestedEnd()
|
---|
[1818] | 240 | || (forcedMaxIn > 0 && ideb+implicitSNStart > forcedMaxIn)) break;
|
---|
[1801] | 241 | if (ifin+implicitSNStart < mgr->getRequestedBegin()
|
---|
[1818] | 242 | || (forcedMinIn > 0 && ifin+implicitSNStart < forcedMinIn)) continue;
|
---|
[1725] | 243 | for (long j=0; j<Buff_Sz; j++) {
|
---|
| 244 | samplenum[j] = j+ideb+implicitSNStart;
|
---|
| 245 | }
|
---|
[1801] | 246 | fits_lock();
|
---|
[1725] | 247 | } else {
|
---|
[1801] | 248 | fits_lock();
|
---|
[1725] | 249 | fits_read_col_dbl(fptr,1,ideb+1,1,n,0,samplenum,&anyNul,&fstatus);
|
---|
[1836] | 250 | if (samplenum[0] > mgr->getRequestedEnd()
|
---|
| 251 | || (forcedMaxIn > 0 && samplenum[0] > forcedMaxIn)) {
|
---|
[1801] | 252 | fits_unlock();
|
---|
| 253 | break;
|
---|
| 254 | }
|
---|
[1836] | 255 | if (samplenum[n-1] < mgr->getRequestedBegin()
|
---|
| 256 | || (forcedMinIn > 0 && samplenum[n-1] < forcedMinIn)) {
|
---|
[1801] | 257 | fits_unlock();
|
---|
| 258 | continue;
|
---|
| 259 | }
|
---|
[1725] | 260 | }
|
---|
[1717] | 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);
|
---|
[1744] | 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 | }
|
---|
[1717] | 290 | totnscount++;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | //////// des-allocate buffers
|
---|
| 294 | delete [] samplenum;
|
---|
[1744] | 295 | delete [] tmpflg;
|
---|
[1763] | 296 | {for(int k=0; k<getNOut(); k++) {
|
---|
[1717] | 297 | if(colval[k]!=NULL) delete [] colval[k];
|
---|
| 298 | if(colflg[k]!=NULL) delete [] colflg[k];
|
---|
[1763] | 299 | }}
|
---|
[1717] | 300 | colval.resize(0); colflg.resize(0);
|
---|
| 301 |
|
---|
| 302 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 303 | }
|
---|