[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
|
---|
| 73 | for (int i=1; i<ncols; i++) {
|
---|
| 74 | char templt[10];
|
---|
| 75 | sprintf(templt, "%d", i+1);
|
---|
| 76 | char colname[200];
|
---|
| 77 | int colnum;
|
---|
| 78 | fits_get_colname(fptr, CASESEN, templt, colname, &colnum, &fstatus);
|
---|
| 79 | cout << "FITSTOIReader col " << colname << endl;
|
---|
| 80 | declareOutput(colname);
|
---|
| 81 | }
|
---|
| 82 | fits_unlock();
|
---|
[1480] | 83 |
|
---|
| 84 | snBegin = firstSn;
|
---|
| 85 |
|
---|
| 86 | openFile(allfn.back());
|
---|
| 87 | snEnd = firstSn+nrows-1;
|
---|
| 88 | cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl;
|
---|
[1365] | 89 | }
|
---|
| 90 |
|
---|
| 91 | int FITSTOIReader::calcMinOut() {
|
---|
[1367] | 92 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 93 | int firstReq = mgr->getRequestedBegin();
|
---|
[1480] | 94 | return snBegin > firstReq ? snBegin : firstReq;
|
---|
[1365] | 95 | }
|
---|
| 96 |
|
---|
| 97 | int FITSTOIReader::calcMaxOut() {
|
---|
[1367] | 98 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 99 | int lastReq = mgr->getRequestedEnd();
|
---|
[1480] | 100 | return snEnd < lastReq ? snEnd : lastReq;
|
---|
[1365] | 101 | }
|
---|
| 102 |
|
---|
[1480] | 103 | void FITSTOIReader::addFile(string fn) {
|
---|
| 104 | allfn.push_back(fn);
|
---|
| 105 | }
|
---|
[1365] | 106 |
|
---|
| 107 | void FITSTOIReader::run() {
|
---|
[1480] | 108 | for (vector<string>::iterator i=allfn.begin(); i!=allfn.end(); i++) {
|
---|
| 109 | openFile(*i);
|
---|
| 110 | run1();
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void FITSTOIReader::run1() {
|
---|
[1365] | 115 | // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum,
|
---|
| 116 | // mais cela implique de gerer aussi bien echant uniforme que non.
|
---|
| 117 | // On pourrait aussi lire plusieurs elements d'un coup.
|
---|
| 118 | int ncols = outIx.size();
|
---|
[1454] | 119 | cout << "reader reading... NRows=" << nrows << " firstSn= "
|
---|
| 120 | << firstSn << endl;
|
---|
[1365] | 121 | for (int i=0; i<nrows; i++) {
|
---|
| 122 | int anyNul;
|
---|
| 123 | double y;
|
---|
| 124 | fits_lock();
|
---|
| 125 | fits_read_col_dbl(fptr,1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
| 126 | fits_unlock();
|
---|
| 127 | int sn = (int) (y+.1);
|
---|
| 128 | TOIManager* mgr = TOIManager::getManager();
|
---|
| 129 | if (sn > mgr->getRequestedEnd()) break;
|
---|
| 130 | if (sn < mgr->getRequestedBegin()) continue;
|
---|
[1454] | 131 | // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl;
|
---|
[1365] | 132 | for (int j=1; j<=ncols; j++) {
|
---|
[1437] | 133 | if ( !checkOutputTOIIndex(j-1) ) continue; // Reza - Si TOI non connecte
|
---|
[1365] | 134 | fits_lock();
|
---|
| 135 | fits_read_col_dbl(fptr,j+1,i+1,1,1,0,&y,&anyNul,&fstatus);
|
---|
| 136 | fits_unlock();
|
---|
| 137 | putData(j-1, sn, y);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | cout << "reader done reading... " << pthread_self() << endl;
|
---|
| 141 | }
|
---|