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