[1365] | 1 | #include "fitstoirdr.h"
|
---|
| 2 | #include "toimanager.h"
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | FITSTOIReader::FITSTOIReader(string fn) {
|
---|
| 7 | fname = fn;
|
---|
[1480] | 8 | allfn.push_back(fn);
|
---|
[1365] | 9 | cout << "FITSTOIReader::FITSTOIReader" << endl;
|
---|
| 10 | cout << "FITSTOIReader::inited " << inited << endl;
|
---|
| 11 | name = "rdr";
|
---|
[1480] | 12 | fptr = NULL;
|
---|
[1365] | 13 | }
|
---|
| 14 |
|
---|
| 15 | FITSTOIReader::~FITSTOIReader() {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
| 19 | void fits_lock();
|
---|
| 20 | void fits_unlock();
|
---|
| 21 |
|
---|
| 22 | void fits_lock() {
|
---|
| 23 | pthread_mutex_lock(&fits_mutex);
|
---|
| 24 | }
|
---|
| 25 | void fits_unlock() {
|
---|
| 26 | pthread_mutex_unlock(&fits_mutex);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[1480] | 29 | void FITSTOIReader::openFile(string fn) {
|
---|
[1365] | 30 | fits_lock();
|
---|
[1480] | 31 | if (fptr) {
|
---|
| 32 | fits_close_file(fptr,&fstatus);
|
---|
| 33 | fptr = NULL;
|
---|
| 34 | }
|
---|
| 35 | fname = fn;
|
---|
| 36 | cout << "FITSTOIReader::open FileName=" << fname << endl;
|
---|
[1365] | 37 | fstatus = 0;
|
---|
| 38 | // Open file
|
---|
| 39 | fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus);
|
---|
| 40 | if (fstatus != 0) {
|
---|
| 41 | fits_report_error(stderr, fstatus);
|
---|
| 42 | fits_unlock();
|
---|
| 43 | exit (-1);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | // Go to first extension, which should be a BINTABLE
|
---|
| 47 |
|
---|
| 48 | int simple, bitpix, naxis;
|
---|
| 49 | long naxes;
|
---|
| 50 | long pcount, gcount;
|
---|
| 51 | int extend;
|
---|
| 52 | fits_read_imghdr(fptr, 1, &simple, &bitpix,
|
---|
| 53 | &naxis, &naxes, &pcount, &gcount, &extend, &fstatus);
|
---|
| 54 |
|
---|
| 55 | fits_movabs_hdu(fptr, 2, NULL, &fstatus);
|
---|
| 56 |
|
---|
| 57 | fits_get_num_cols(fptr,&ncols,&fstatus);
|
---|
| 58 | fits_get_num_rows(fptr,&nrows,&fstatus);
|
---|
| 59 |
|
---|
[1454] | 60 | cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl;
|
---|
[1480] | 61 | int anyNul;
|
---|
| 62 | double y;
|
---|
| 63 | fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus);
|
---|
| 64 | firstSn = (int) (y+.1);
|
---|
| 65 | fits_unlock();
|
---|
| 66 | }
|
---|
[1365] | 67 |
|
---|
[1480] | 68 | void FITSTOIReader::init() {
|
---|
| 69 | openFile(allfn.front());
|
---|
| 70 |
|
---|
| 71 | fits_lock();
|
---|
[1365] | 72 | // Dans cette version, on s'attend a ce que la premiere colonne soit le samplenum
|
---|
[1527] | 73 | int itoi=-1;
|
---|
[1365] | 74 | for (int i=1; i<ncols; i++) {
|
---|
| 75 | char templt[10];
|
---|
| 76 | sprintf(templt, "%d", i+1);
|
---|
| 77 | char colname[200];
|
---|
| 78 | int colnum;
|
---|
| 79 | fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
|
---|
| 80 | cout << "FITSTOIReader col " << colname << endl;
|
---|
[1527] | 81 | if (!strncmp(colname, "fg_", 3)) {
|
---|
| 82 | colsinput[itoi].second=true;
|
---|
| 83 | } else {
|
---|
| 84 | declareOutput(colname);
|
---|
| 85 | itoi++;
|
---|
| 86 | colsinput[itoi] = pair<int,bool>(i,false);
|
---|
| 87 | }
|
---|
[1365] | 88 | }
|
---|
| 89 | fits_unlock();
|
---|
[1480] | 90 |
|
---|
| 91 | snBegin = firstSn;
|
---|
| 92 |
|
---|
| 93 | openFile(allfn.back());
|
---|
| 94 | snEnd = firstSn+nrows-1;
|
---|
| 95 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
[1365] | 96 | }
|
---|
| 97 |
|
---|
| 98 | int FITSTOIReader::calcMinOut() {
|
---|
[1367] | 99 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 100 | int firstReq = mgr->getRequestedBegin();
|
---|
[1480] | 101 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
[1365] | 102 | }
|
---|
| 103 |
|
---|
| 104 | int FITSTOIReader::calcMaxOut() {
|
---|
[1367] | 105 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 106 | int lastReq = mgr->getRequestedEnd();
|
---|
[1480] | 107 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
[1365] | 108 | }
|
---|
| 109 |
|
---|
[1480] | 110 | void FITSTOIReader::addFile(string fn) {
|
---|
| 111 | allfn.push_back(fn);
|
---|
| 112 | }
|
---|
[1365] | 113 |
|
---|
| 114 | void FITSTOIReader::run() {
|
---|
[1480] | 115 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
| 116 | openFile(*i);
|
---|
| 117 | run1();
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void FITSTOIReader::run1() {
|
---|
[1365] | 122 | // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
|
---|
| 123 | // mais cela implique de gerer aussi bien echant uniforme que non.
|
---|
| 124 | // On pourrait aussi lire plusieurs elements d'un coup.
|
---|
| 125 | int ncols = outIx.size();
|
---|
[1454] | 126 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
| 127 | << firstSn << endl;
|
---|
[1365] | 128 | for (int i=0; i<nrows; i++) {
|
---|
| 129 | int anyNul;
|
---|
| 130 | double y;
|
---|
| 131 | fits_lock();
|
---|
| 132 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
| 133 | fits_unlock();
|
---|
| 134 | int sn = (int) (y+.1);
|
---|
| 135 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 136 | if (sn > mgr->getRequestedEnd()) break;
|
---|
| 137 | if (sn < mgr->getRequestedBegin()) continue;
|
---|
[1454] | 138 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
[1527] | 139 | for (int k=0; k<getNOut(); k++) {
|
---|
| 140 | int j = colsinput[k].first;
|
---|
| 141 | if ( !checkOutputTOIIndex(k) ) continue; // Reza - Si TOI non connecte
|
---|
[1365] | 142 | fits_lock();
|
---|
| 143 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
[1527] | 144 | long flg = 0;
|
---|
| 145 | if (colsinput[k].second) {
|
---|
| 146 | fits_read_col_lng(fptr,j+2,i+1,1,1,0,&flg,&anyNul,&fstatus);
|
---|
| 147 | }
|
---|
[1365] | 148 | fits_unlock();
|
---|
[1527] | 149 | putData(k, sn, y, flg);
|
---|
[1365] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 153 | }
|
---|