// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL // Eric Aubourg // Christophe Magneville // Reza Ansari // $Id: fitstoirdr.cc,v 1.41 2002-09-09 15:33:14 aubourg Exp $ #include "fitstoirdr.h" #include "toimanager.h" #include FITSTOIReader::FITSTOIReader(string fn,int buff_sz) { fname = fn; allfn.push_back(fn); Buff_Sz = (buff_sz>0) ? buff_sz: 1000; cout<<"FITSTOIReader::FITSTOIReader"<0) ? buffsz: 1024; return; } pthread_mutex_t fits_mutex = PTHREAD_MUTEX_INITIALIZER; void fits_lock(); void fits_unlock(); void fits_lock() { pthread_mutex_lock(&fits_mutex); } void fits_unlock() { pthread_mutex_unlock(&fits_mutex); } void FITSTOIReader::openFile(string fn) { fits_lock(); if (fptr) { fits_close_file(fptr,&fstatus); fptr = NULL; } fname = fn; cout << "FITSTOIReader::open FileName=" << fname << endl; fstatus = 0; // Open file fits_open_file(&fptr,fname.c_str(),READONLY,&fstatus); if (fstatus != 0) { fits_report_error(stderr, fstatus); fits_unlock(); throw IOExc("FITSTOIReader::openFile() fitsio error"); } // Go to first extension, which should be a BINTABLE int simple, bitpix, naxis; long naxes; long pcount, gcount; int extend; fits_read_imghdr(fptr, 1, &simple, &bitpix, &naxis, &naxes, &pcount, &gcount, &extend, &fstatus); fits_movabs_hdu(fptr, 2, NULL, &fstatus); fits_get_num_cols(fptr,&ncols,&fstatus); fits_get_num_rows(fptr,&nrows,&fstatus); cout << "FITSTOIReader cols = " << ncols << " rows=" << nrows << endl; if (implicitSN) { firstSn = implicitSNStart; } else { int anyNul; double y; fits_read_col_dbl(fptr,1,1,1,1,0,&y,&anyNul,&fstatus); firstSn = (int) (y+.1); } // Ouverture fichier de flag separe de LevelS (Reza 18/6/2002) if (sepFlagfile) { cout << " FITSTOIReader::openFile() - Opening separate flag file " << sepFlagFileName << " NColFlags= " << sepFlagCols.size() << endl; fits_open_file(&fptrflg,sepFlagFileName.c_str(),READONLY,&fstatus); if (fstatus != 0) { fits_report_error(stderr, fstatus); fits_unlock(); throw IOExc("FITSTOIReader::openFile() - sepFlagfile open fitsio error"); } fits_movabs_hdu(fptrflg, 2, NULL, &fstatus); if (fstatus != 0) { fits_report_error(stderr, fstatus); fits_unlock(); throw IOExc("FITSTOIReader::openFile() - sepFlagfile fits_movabs_hdu(2) fitsio error"); } long nrowsflg = 0; fits_get_num_rows(fptrflg,&nrowsflg,&fstatus); if (nrows != nrowsflg) { cerr << " FITSTOIReader::openFile()/Error: Different NRows in flag and data files!" << endl; fits_unlock(); throw ParmError("FITSTOIReader::openFile() Different NRows in flag and data files"); } int ncolsflg = 0; fits_get_num_cols(fptrflg,&ncolsflg,&fstatus); if (ncolsflg < sepFlagCols.size()) { cerr << " FITSTOIReader::openFile()/Error: ncolsflg (=" << ncolsflg << ") < sepFlagCols.size() (=" << sepFlagCols.size() << ")" << endl; fits_unlock(); throw ParmError("FITSTOIReader::openFile() Different NRows in flag and data files"); } } // fits_unlock(); } void FITSTOIReader::init() { // Modif pour fichiers de flag separe de LevelS (Reza 18/6/2002) if (sepFlagfile && (allfn.size() > 1) ) { cerr << "FITSTOIReader::init()/Error- Multiple files and separate flag file not allowed !"<(i,false); } } fits_unlock(); snBegin = firstSn; if (forcedMinIn > 0 && forcedMinIn > snBegin) { snBegin = forcedMinIn; } openFile(allfn.back()); snEnd = firstSn+nrows-1; if (forcedMaxIn > 0 && forcedMaxIn < snEnd) { snEnd = forcedMaxIn; } /* init */ // snMin=snBegin; NNNNNOOOOOONNNNNN !!!! // snMax=snEnd; cout << "FITSTOIReader range " << snBegin << " -> " << snEnd << endl; } int FITSTOIReader::calcMinOut() { chkinit(); TOIManager* mgr = TOIManager::getManager(); int firstReq = mgr->getRequestedBegin(); return snBegin > firstReq ? snBegin : firstReq; } int FITSTOIReader::calcMaxOut() { chkinit(); TOIManager* mgr = TOIManager::getManager(); int lastReq = mgr->getRequestedEnd(); return snEnd < lastReq ? snEnd : lastReq; } // ajout vf 31/07/2002 bool FITSTOIReader::checkSampleLimits(long& min, long& max, int pass) { bool sample_ok=true; chkinit(); return TOIProcessor::checkSampleLimits(min, max, pass); /* cout << "check " << name << " in " << min << " - " << max << " ; " << snMin << " - " << snMax << endl; // on verifie qu'on peut effectivement produire if (min < snBegin) { min = snBegin; } if (max > snEnd) { max = snEnd; } // si intersection des intervals vide on leve une exception //if (min >= snEnd || max <= snBegin) { //cerr << " FITSTOIReader::checkSampleLimits(long& min long& max)/Error Sample out of bounds" << endl; //throw ParmError(" FITSTOIReader::checkSampleLimits(long& min long& max) min >= snEnd || max <= snBegin"); //} // ajustements si necessaires if (min < snMin) { snMin = min; } if (max > snMax) { snMax = max; } min=minsnMax?snMax:max; // cas sans contraintes, on retourne nos bornes if (min>max) { min = snBegin; max = snEnd; } cout << "check " << name << " out " << min << " - " << max << " ; " << snMin << " - " << snMax << endl; //cout << "fitstoirdr : limites verifiees : " << snBegin << " , " << snEnd << " : " << sample_ok << endl; return sample_ok; */ } void FITSTOIReader::calcSampleLimits(long& min, long& max) { chkinit(); cout << "calc " << name << " in " << min << " - " << max << " ; " << snBegin << " - " << snEnd << endl; min=minsnMax?snMax:max; snBegin=snMin; snEnd=snMax; cout << "calc " << name << " out " << min << " - " << max << " ; " << snBegin << " - " << snEnd << endl; //cout << "fitstoirdr : limites temporaires calculees : " << snBegin << " , " << snEnd << endl; } // fin ajout vf void FITSTOIReader::addFile(string fn) { allfn.push_back(fn); } void FITSTOIReader::setFlagFile(string fn, vector flags) { if (flags.size() < 1) { cerr << " FITSTOIReader::setFlagFile()/Error flag.size() = 0 ! " << endl; throw ParmError("FITSTOIReader::setFlagFile() flag.size() = 0"); } sepFlagfile = true; sepFlagFileName = fn; sepFlagCols = flags; } void FITSTOIReader::run() { for (vector::iterator i=allfn.begin(); i!=allfn.end(); i++) { openFile(*i); if (Buff_Sz > 1) run2(); // Lecture bufferise else run1(); // Lecture un echantillon a la fois } fits_lock(); if (fptr) { fits_close_file(fptr,&fstatus); fptr = NULL; } if (sepFlagfile && fptrflg) { fits_close_file(fptrflg,&fstatus); fptrflg = NULL; } fits_unlock(); } // run 1 : deprecated. NON MAINTENU. Incompatible avec implicit SN. // ^^^^^^^^^^^^ Reza , 13/5/2002 , Je viens de rajouter // implicitSNStart et switvh run1/run2 // suivant buffersize void FITSTOIReader::run1() { // Il faudrait optimiser en fonction de ce qui a ete demande comme samplenum, // mais cela implique de gerer aussi bien echant uniforme que non. // On pourrait aussi lire plusieurs elements d'un coup. int ncols = outIx.size(); cout << "FITSTOIReader::run1() reader reading... NRows=" << nrows << " firstSn= " << firstSn << endl; double* tabdata = new double[getNOut()]; uint_8* tabflag = new uint_8[getNOut()]; for (long i=0; i mgr->getRequestedEnd()) {fits_unlock(); break;} if (sn < mgr->getRequestedBegin()) {fits_unlock(); continue;} // if (sn < mgr->getRequestedBegin()+10) cout << "rdr out " << sn << endl; int k; for (k=0; k