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