/* Lecteur de colonne de table Fits (binaire ou ASCII) */ #include "sopnamsp.h" #include "machdefs.h" #include #include #include "pexceptions.h" #include "fabtcolread.h" /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //! Class for opening a FITS file for reading /*! \class SOPHYA::FitsOpenFile \ingroup FitsIOServer Class for opening a FITS file for reading */ /*! Constructor. \param fname : FITS file name to be opened for reading */ FitsOpenFile::FitsOpenFile(string fname) { Init(fname.c_str()); } /*! Constructor. \param cfname : FITS file name to be opened for reading */ FitsOpenFile::FitsOpenFile(const char *cfname) { Init(cfname); } /*! Constructor by default. */ FitsOpenFile::FitsOpenFile() { FitsFN = ""; NHdu = IHdu = HduType = 0; HasBeenPos = false; FitsPtr = NULL; } /*! Constructor by copy. */ FitsOpenFile::FitsOpenFile(FitsOpenFile& fof) { Init(fof.FileName().c_str()); } /*! Destructor. */ FitsOpenFile::~FitsOpenFile() { Delete(); FitsFN = ""; NHdu = IHdu = HduType = 0; HasBeenPos = false; } /*! Delete routine called by the destructor. */ void FitsOpenFile::Delete(void) { if(FitsPtr != NULL) { int sta = 0; if(fits_close_file(FitsPtr,&sta)) printerror(sta); FitsPtr = NULL; } } /*! Init routine called by the constructor */ void FitsOpenFile::Init(const char* fname) { // Parametres Generaux FitsFN = fname; NHdu = IHdu = HduType = 0; HasBeenPos = false; FitsPtr = NULL; // Ouverture du fichier if(FitsFN.size() <= 0 ) throw ParmError("FitsOpenFile::Init: Fits file name error\n"); int sta = 0; if(fits_open_file(&FitsPtr,FitsFN.c_str(),READONLY,&sta)) { printerror(sta); FitsPtr = NULL; throw NullPtrError("FitsOpenFile::Init: Error opening Fits file\n"); } // Get number of hdu if(fits_get_num_hdus(FitsPtr,&NHdu,&sta)) { printerror(sta); NHdu = 0; Delete(); throw NotAvailableOperation("FitsOpenFile::Init: Error getting NHdu\n"); } if(NHdu<=0) { Delete(); throw SzMismatchError("FitsOpenFile::Init: Bad NHdu\n"); } MoveToHDU(1); } /*! Move to an HDU \param ihdu: hdu number to move \warning ihdu = [1,nhdu] \return 0 if positionning failed, ihdu if success */ int FitsOpenFile::MoveToHDU(int ihdu) { if(FitsPtr==NULL) throw NullPtrError("FitsOpenFile::MoveToHDU: no fits file open FitsPtr==NULL\n"); int ihdusave = IHdu; if(ihdu<=0) ihdu=1; if(ihdu>NHdu) ihdu=NHdu; int sta=0; if(fits_movabs_hdu(FitsPtr,ihdu,&HduType,&sta)) { printerror(sta); // On se repositionne ou on etait fits_movabs_hdu(FitsPtr,ihdusave,&HduType,&sta); IHdu = ihdusave; } else IHdu = ihdu; return IHdu; } /*! Move to the first HDU of a certain type \param hdutype: type of the hdu \param hdudeb: start at that hdu \return the type of HDU the file is positionned */ int FitsOpenFile::MoveToFirst(int hdutype,int ihdudeb) { if(ihdudeb<=0) ihdudeb=1; if(ihdudeb>NHdu) ihdudeb=NHdu; int ihdusave = IHdu; for(int ihdu=ihdudeb;ihdu<=NHdu;ihdu++) { MoveToHDU(ihdu); if(HduType==hdutype) break; } // Si echec, on se repositionne ou on etait if(HduType!=hdutype) MoveToHDU(ihdusave); return HduType; } /*! Move to the last HDU of a certain type \param hdutype: type of the hdu \param hdudeb: stop at that hdu \return the type of HDU the file is positionned */ int FitsOpenFile::MoveToLast(int hdutype,int ihdudeb) { if(ihdudeb<=0) ihdudeb=1; if(ihdudeb>NHdu) ihdudeb=NHdu; int ihdusave = IHdu; for(int ihdu=NHdu;ihdu>=ihdudeb;ihdu--) { MoveToHDU(ihdu); if(HduType==hdutype) break; } // Si echec, on se repositionne ou on etait if(HduType!=hdutype) MoveToHDU(ihdusave); return HduType; } /*! Print */ void FitsOpenFile::Print(void) { cout<<"FitsOpenFile::Print: "< data; LONGLONG n = fbt.Read(32,50,data); cout<<"Number of values read: "< data2; fbt2.Read(32,50,data); // Close the fits file delete fof; \endverbatim */ ////////////////////////////////////////////////////////////// /*! Constructor. \param fof : Pointer to the Class for opening the FITS file \param collabel : label of the column to be read \param ihdu : number of the HDU where the column is. \param lp : debug level \verbatim - if ihdu<=0 first BINARY or ASCII table is taken - if ihdu>nhdu ihdu is set to nhdu \endverbatim \warning ihdu = [1,nhdu] */ FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,string collabel,int ihdu,int lp) { Init(fof,collabel.c_str(),-1,ihdu,lp); } /*! Constructor. Same as before but the column is identified by its column number \param colnum : number of the column to be read \warning col = [0,ncol[ */ FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,int colnum,int ihdu,int lp) { Init(fof,"",colnum,ihdu,lp); } /*! Constructor by copy */ FitsABTColRd::FitsABTColRd(FitsABTColRd& fbt) { Init(fbt.GetFitsOpenFile(),fbt.GetColLabel().c_str(),fbt.GetColNum(),fbt.HDU(),fbt.DbgLevel); } /*! Constructor by default */ FitsABTColRd::FitsABTColRd() { ColLabel = ""; ColTUnit = ""; ColTForm = ""; ColNum = -1; ColTypeCode = 0; ColRepeat=0; ColDispWidth=0; StrBuff = NULL; NBcol = 0; NBline = 0; SetNulVal(); SetDebug(0); NFitsRead = 0; FitsOF = NULL; } /*! Init routine called by the constructor */ void FitsABTColRd::Init(FitsOpenFile* fof,const char* collabel,int colnum,int ihdu,int lp) { // Initialisation des Parametres Generaux ColLabel=collabel; ColTUnit=""; ColTForm=""; ColNum=colnum; ColTypeCode=0; ColRepeat=0; ColDispWidth=0; StrBuff = NULL; NBcol = 0; NBline = 0; SetNulVal(); SetDebug(lp); NFitsRead = 0; FitsOF = NULL; // Caracteristiques du FitsOpenFile FitsOF = fof; if(FitsOF==NULL) throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n"); if(GetFitsPtr()==NULL) throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n"); int sta = 0; if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU(); // Get HDU for bin/ascii table // ATTENTION: le fichier est ouvert mais non positionne sur un HDU, // une classe utilisant ce fichier doit le positionner sur un HDU. // Par contre, si une autre classe utilise ce meme FitsOpenFile, // elle ne peut le positionner que sur ce meme HDU ! if(FitsOF->GetPosStatus()==false) { if(ihdu==0) { // find the first BINARY then the first ASCII int rc = FitsOF->MoveToFirst(BINARY_TBL); if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL); } else { int rc = FitsOF->MoveToHDU(ihdu); if(rc!=ihdu) throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n"); } } else { // Fits file has already been positionned if(ihdu>0 && ihdu!=HDU()) throw RangeCheckError("FitsABTColRd::Init: file already posit. at another HDU\n"); } // Check HDUType and set position status to TRUE if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL) throw TypeMismatchExc("FitsABTColRd::Init: HDU not ASCII/BINARY table\n"); if(DbgLevel>1) cout<<"...Init ihdu="<1) cout<<"...Init NBcol="<1) cout<<"...Init NBline="< 0) { strcpy(labelcol,ColLabel.c_str()); if(fits_get_colnum(GetFitsPtr(),CASESEN,labelcol,&ColNum,&sta)) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd::Init: Error getting column number\n"); } ColNum--; // Convention [0,ncol[ } if(DbgLevel>1) cout<<"...Init ColNum="<=NBcol) throw RangeCheckError("FitsABTColRd::Init: Bad column number\n"); // Get column type if(fits_get_coltypell(GetFitsPtr(),ColNum+1,&ColTypeCode,NULL,NULL,&sta)) { FitsOpenFile::printerror(sta); throw ParmError("FitsABTColRd::Init: Error getting column type\n"); } if(DbgLevel>1) cout<<"...Init ColTypeCode="<=NBline) throw RangeCheckError("FitsABTColRd::Read try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat) throw RangeCheckError("FitsABTColRd::Read try to read outside element range: nfirstel>repeat\n"); double val; fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n"); } NFitsRead++; return val; } /*! Read row "n" element "nfirstel" and return a long long int */ int_8 FitsABTColRd::ReadLL(LONGLONG n,long nfirstel) { int sta=0; if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd::ReadLL try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat) throw RangeCheckError("FitsABTColRd::ReadLL try to read outside element range: nfirstel>repeat\n"); int_8 val; fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd::ReadLL: Error Reading Fits file\n"); } NFitsRead++; return val; } /*! Read row "n" element "nfirstel" and return a complex value */ complex FitsABTColRd::ReadComplex(LONGLONG n,long nfirstel) { int sta=0; if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd::ReadComplex try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat) throw RangeCheckError("FitsABTColRd::ReadComplex try to read outside element range: nfirstel>repeat\n"); r_8 val[2]; fits_read_col(GetFitsPtr(),TDBLCOMPLEX,ColNum+1,n+1,nfirstel+1,1,NULL,val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd::ReadComplex: Error Reading Fits file\n"); } NFitsRead++; return complex(val[0],val[1]); } /*! Read row "n" element "nfirstel" and return in string (internal pointer) */ char* FitsABTColRd::ReadInStr(LONGLONG n,long nfirstel) { int sta=0; if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd::ReadInStr try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat) throw RangeCheckError("FitsABTColRd::ReadInStr try to read outside element range: nfirstel>repeat\n"); if(StrBuff==NULL) StrBuff= new char[ColDispWidth+5]; char nullstr[]=""; fits_read_col(GetFitsPtr(),TSTRING,ColNum+1,n+1,nfirstel+1,1,nullstr,&StrBuff,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd::ReadInStr: Error Reading Fits file\n"); } NFitsRead++; return StrBuff; } /*! Read rows from "n1" to "n2" and return the values into TVector of double \return NREAD the number of values read (n2-n1+1). \warning row = [0,NRows[, the routine read [n1,n2] \verbatim - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)" - Last row read is ALWAYS: "n2 = n1 + NREAD -1" - The TVector is never resized if not necessary ------------------------------------------------------------------------- - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9] TVector V(5); bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==5 -> return 3 bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5 bt.Read(7,-1,V) -> read rows=7,8,9 -> V.Size()==5 -> return 3 bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5 bt.Read(-1,5,V) -> throw exception TVector V(5); bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7 TVector V(5); bt.Read(2,8,V) -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7 TVector V; bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==3 -> return 3 TVector V; bt.Read(3,-1,V) -> throw exception ------------------------------------------------------------------------- \endverbatim */ LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector& data) { if(ColRepeat>1) throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n"); if(n1<0 || n1>=NBline) throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n"); if(data.Size()<=0 && n2=NBline) n2 = NBline-1; LONGLONG nread = n2-n1+1; if(data.Size(): Error Reading Fits file\n"); } NFitsRead += nread; return nread; } /*! idem before but for TVector of float */ LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector& data) { if(ColRepeat>1) throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n"); if(n1<0 || n1>=NBline) throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n"); if(data.Size()<=0 && n2=NBline) n2 = NBline-1; LONGLONG nread = n2-n1+1; if(data.Size(): Error Reading Fits file\n"); } NFitsRead += nread; return nread; } /*! idem before but for TVector of unsigned short */ LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector& data) { if(ColRepeat>1) throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n"); if(n1<0 || n1>=NBline) throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n"); if(data.Size()<=0 && n2=NBline) n2 = NBline-1; LONGLONG nread = n2-n1+1; if(data.Size(): Error Reading Fits file\n"); } NFitsRead += nread; return nread; } /*! idem before but for TVector of int_4 */ LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector& data) { if(ColRepeat>1) throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n"); if(n1<0 || n1>=NBline) throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n"); if(data.Size()<=0 && n2=NBline) n2 = NBline-1; LONGLONG nread = n2-n1+1; if(data.Size(): Error Reading Fits file\n"); } NFitsRead += nread; return nread; } /*! idem before but for TVector of int_8 */ LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector& data) { #ifdef TLONGLONG if(ColRepeat>1) throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n"); if(n1<0 || n1>=NBline) throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n"); if(data.Size()<=0 && n2=NBline) n2 = NBline-1; LONGLONG nread = n2-n1+1; if(data.Size(): Error Reading Fits file\n"); } NFitsRead += nread; return nread; #else throw PException("FitsABTColRd::Read(..,TVector&) Not in that cfitsio version"); #endif } ///////////////////////////////////////////////// /*! Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart" for element "nfirstel" \verbatim - The search is performed from "rowstart" to the end in ascending order (from "rowstart" to nrows). - Warning: "rowstart<0" means "rowstart==0" (search all the table column) That is the default \endverbatim \return <0 means not found */ LONGLONG FitsABTColRd::FirstRow(long nfirstel,double val1,double val2,LONGLONG rowstart) { if(nfirstel<0 || nfirstel>=ColRepeat) throw RangeCheckError("FitsABTColRd::FirstRow try to read outside element range: nfirstel>repeat\n"); LONGLONG row = -1; if(NBline==0) return row; if(rowstart<0) rowstart = 0; if(rowstart>=NBline) rowstart = NBline-1; for(LONGLONG i=rowstart;ival2) continue; row = i; break; } return row; } /*! Return the number of the last row where val1<=val<=val2 starting at row rowstart for element "nfirstel" \return <0 means not found \verbatim - The search is performed from "rowstart" to the beginning in descending order (from "rowstart" to 0). - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column) That is the default \endverbatim */ LONGLONG FitsABTColRd::LastRow(long nfirstel,double val1,double val2,LONGLONG rowstart) { if(nfirstel<0 || nfirstel>=ColRepeat) throw RangeCheckError("FitsABTColRd::FirstRow try to read outside element range: nfirstel>repeat\n"); LONGLONG row = -1; if(NBline==0) return row; if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1; for(LONGLONG i=rowstart;i>=0;i--) { double val = Read(i,nfirstel); if(valval2) continue; row = i; break; } return row; } /*! Print on stream os */ void FitsABTColRd::Print(ostream& os,int lp) const { os<<"FitsABTColRd:Print ("< data; LONGLONG n = fbt.Read(32,50,data); cout<<"Number of values read: "<nhdu ihdu is set to nhdu \endverbatim \warning ihdu = [1,nhdu] */ FitsABTColRead::FitsABTColRead(string fname,string collabel,int ihdu,int lp) : FitsABTColRd(new FitsOpenFile(fname),collabel,ihdu,lp) { } /*! Constructor. Same as before but the column is identified by its column number \param colnum : number of the column to be read \warning col = [0,ncol[ */ FitsABTColRead::FitsABTColRead(string fname,int colnum,int ihdu,int lp) : FitsABTColRd(new FitsOpenFile(fname),colnum,ihdu,lp) { } /*! Constructor. see below */ FitsABTColRead::FitsABTColRead(const char * cfname,const char* collabel,int ihdu,int lp) : FitsABTColRd(new FitsOpenFile(cfname),collabel,ihdu,lp) { } /*! Constructor. see below */ FitsABTColRead::FitsABTColRead(const char * cfname,int colnum,int ihdu,int lp) : FitsABTColRd(new FitsOpenFile(cfname),colnum,ihdu,lp) { } /*! Constructor by default */ FitsABTColRead::FitsABTColRead() : FitsABTColRd() { } /*! Constructor by copy */ FitsABTColRead::FitsABTColRead(FitsABTColRead& fbt) { // --- ATTENTION --- // FitsABTColRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile FitsOpenFile* fof = new FitsOpenFile(*fbt.GetFitsOpenFile()); Init(fof,fbt.GetColLabel().c_str(),fbt.GetColNum(),fbt.HDU(),fbt.DbgLevel); } /*! Destructor. */ FitsABTColRead::~FitsABTColRead() { Delete(); // ?? inutile ?? // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS if(FitsOF!=NULL) delete FitsOF; } /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// FitsABTColRd1F::FitsABTColRd1F(FitsOpenFile* fof,int ihdu,int lp) { Init(fof,ihdu,lp); } /*! Init routine called by the constructor */ void FitsABTColRd1F::Init(FitsOpenFile* fof,int ihdu,int lp) { // Initialisation des Parametres Generaux ColLabel.resize(0); ColTUnit.resize(0); ColTForm.resize(0); ColTypeCode.resize(0); ColRepeat.resize(0); ColDispWidth.resize(0); StrBuff.resize(0); NBcol = 0; NBline = 0; SetNulVal(); SetDebug(lp); FitsOF = NULL; // Caracteristiques du FitsOpenFile FitsOF = fof; if(FitsOF==NULL) throw NullPtrError("FitsABTColRd1F::Init: FitsOpenFile pointer is NULL\n"); if(GetFitsPtr()==NULL) throw NullPtrError("FitsABTColRd1F::Init: FitsPtr pointer is NULL\n"); int sta = 0; if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU(); // Get HDU for bin/ascii table // ATTENTION: le fichier est ouvert mais non positionne sur un HDU, // une classe utilisant ce fichier doit le positionner sur un HDU. // Par contre, si une autre classe utilise ce meme FitsOpenFile, // elle ne peut le positionner que sur ce meme HDU ! if(FitsOF->GetPosStatus()==false) { if(ihdu==0) { // find the first BINARY then the first ASCII int rc = FitsOF->MoveToFirst(BINARY_TBL); if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL); } else { int rc = FitsOF->MoveToHDU(ihdu); if(rc!=ihdu) throw RangeCheckError("FitsABTColRd1F::Init: Error moving to requested HDU\n"); } } else { // Fits file has already been positionned if(ihdu>0 && ihdu!=HDU()) throw RangeCheckError("FitsABTColRd1F::Init: file already posit. at another HDU\n"); } // Check HDUType and set position status to TRUE if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL) throw TypeMismatchExc("FitsABTColRd1F::Init: HDU not ASCII/BINARY table\n"); if(DbgLevel>1) cout<<"...Init ihdu="<1) cout<<"...Init NBcol="<1) cout<<"...Init NBline="<1) cout<<"...Init ColTypeCode="<0) for(int ColNum=0; ColNum=NBcol) throw RangeCheckError("FitsABTColRd1F::Read try to read outside column range\n"); if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd1F::Read try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat[ColNum]) throw RangeCheckError("FitsABTColRd1F::Read try to read outside element range: nfirstel>repeat\n"); double val; fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd1F::Read: Error Reading Fits file\n"); } return val; } /*! Read row "n" element "nfirstel" and return a long long int */ int_8 FitsABTColRd1F::ReadLL(int ColNum,LONGLONG n,long nfirstel) { int sta=0; if(ColNum<0 || ColNum>=NBcol) throw RangeCheckError("FitsABTColRd1F::ReadLL try to read outside column range\n"); if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd1F::ReadLL try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat[ColNum]) throw RangeCheckError("FitsABTColRd1F::ReadLL try to read outside element range: nfirstel>repeat\n"); int_8 val; fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd1F::ReadLL: Error Reading Fits file\n"); } return val; } /*! Read row "n" element "nfirstel" and return a complex value */ complex FitsABTColRd1F::ReadComplex(int ColNum,LONGLONG n,long nfirstel) { int sta=0; if(ColNum<0 || ColNum>=NBcol) throw RangeCheckError("FitsABTColRd1F::ReadComplex try to read outside column range\n"); if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd1F::ReadComplex try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat[ColNum]) throw RangeCheckError("FitsABTColRd1F::ReadComplex try to read outside element range: nfirstel>repeat\n"); r_8 val[2]; fits_read_col(GetFitsPtr(),TDBLCOMPLEX,ColNum+1,n+1,nfirstel+1,1,NULL,val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd1F::ReadComplex: Error Reading Fits file\n"); } return complex(val[0],val[1]); } /*! Read row "n" element "nfirstel" and return in string (internal pointer) */ char* FitsABTColRd1F::ReadInStr(int ColNum,LONGLONG n,long nfirstel) { int sta=0; if(ColNum<0 || ColNum>=NBcol) throw RangeCheckError("FitsABTColRd1F::ReadInStr try to read outside column range\n"); if(n<0 || n>=NBline) throw RangeCheckError("FitsABTColRd1F::ReadInStr try to read outside line range\n"); if(nfirstel<0 || nfirstel>=ColRepeat[ColNum]) throw RangeCheckError("FitsABTColRd1F::ReadInStr try to read outside element range: nfirstel>repeat\n"); if(StrBuff[ColNum]==NULL) StrBuff[ColNum] = new char[ColDispWidth[ColNum]+5]; char nullstr[]=""; fits_read_col(GetFitsPtr(),TSTRING,ColNum+1,n+1,nfirstel+1,1,nullstr,&(StrBuff[ColNum]),NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsABTColRd::ReadInStr: Error Reading Fits file\n"); } return StrBuff[ColNum]; } /*! Print on stream os */ void FitsABTColRd1F::Print(ostream& os,int lp) const { os<<"FitsABTColRd1F:Print ("<0 && NBcol>0) { for(int ColNum=0;ColNumNHDU()) ihdu=NHDU(); // Get HDU 2D image // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init() if(FitsOF->GetPosStatus()==false) { if(ihdu==0) { // find the first IMAGE_HDU FitsOF->MoveToFirst(IMAGE_HDU); } else { int rc = FitsOF->MoveToHDU(ihdu); if(rc!=ihdu) throw RangeCheckError("FitsImg2DRd::Init: Error moving to requested HDU\n"); } } else { // Fits file has already been positionned if(ihdu>0 && ihdu!=HDU()) throw RangeCheckError("FitsImg2DRd::Init: file already posit. at another HDU\n"); } // Check HDUType and set position status to TRUE if(HDUType()!=IMAGE_HDU) throw TypeMismatchExc("FitsImg2DRd::Init: HDU not IMAGE_HDU\n"); FitsOF->SetPosStatus(true); // Get NAXIS 1 et 2 int nfound=0; // car fits_read_keys_lnglng n'est pas prototype dans longnam.h if(ffgknjj(GetFitsPtr(),"NAXIS",1,2,Naxis,&nfound,&sta)) { FitsOpenFile::printerror(sta); throw RangeCheckError("FitsImg2DRd::Init: Error reading NAXIS cards\n"); } if(DbgLevel>1) cout<<"...Init(hdu="< \warning TMatrix data(Naxis2,Naxis1) */ LONGLONG FitsImg2DRd::Read(TMatrix& data) { int sta=0; uint_2* arr = new uint_2[Naxis[0]]; data.ReSize(Naxis[1],Naxis[0]); for(LONGLONG j=0;j): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg2DRd::Read(TMatrix& data) { int sta=0; int_4* arr = new int_4[Naxis[0]]; data.ReSize(Naxis[1],Naxis[0]); int T = (sizeof(long)==4) ? TLONG: TINT; for(LONGLONG j=0;j): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg2DRd::Read(TMatrix& data) { int sta=0; int_8* arr = new int_8[Naxis[0]]; data.ReSize(Naxis[1],Naxis[0]); for(LONGLONG j=0;j): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg2DRd::Read(TMatrix& data) { int sta=0; float* arr = new float[Naxis[0]]; data.ReSize(Naxis[1],Naxis[0]); for(LONGLONG j=0;j): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg2DRd::Read(TMatrix& data) { int sta=0; double* arr = new double[Naxis[0]]; data.ReSize(Naxis[1],Naxis[0]); for(LONGLONG j=0;j): Error Reading Fits file\n"); } for(LONGLONG i=0;i=Naxis[0] || numrow>=Naxis[1]) throw NotAvailableOperation("FitsImg2DRd::Read(col,row): bad col/row number\n"); LONGLONG deb = numrow*Naxis[0] + numcol + 1; double val = 0.; fits_read_img(GetFitsPtr(),TDOUBLE,deb,1,&NulVal,&val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsImg2DRd::Read(col,num): Error Reading Fits file\n"); } return val; } /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //! Class for reading a 2D image from a FITS file /*! \class SOPHYA::FitsImg2DRead \ingroup FitsIOServer Class for reading a 2D image from a FITS file */ ////////////////////////////////////////////////////////////// /*! Constructor. \param fname : name of the FITS file \param ihdu : number of the HDU where the image is. \param lp : debug level \verbatim - if ihdu<=0 first IMAGE hdu is taken - if ihdu>nhdu ihdu is set to nhdu \endverbatim \warning ihdu = [1,nhdu] */ FitsImg2DRead::FitsImg2DRead(string fname,int ihdu,int lp) : FitsImg2DRd(new FitsOpenFile(fname),ihdu,lp) { } /*! Constructor. see below */ FitsImg2DRead::FitsImg2DRead(const char * cfname,int ihdu,int lp) : FitsImg2DRd(new FitsOpenFile(cfname),ihdu,lp) { } /*! Constructor by default */ FitsImg2DRead::FitsImg2DRead() : FitsImg2DRd() { } /*! Constructor by copy */ FitsImg2DRead::FitsImg2DRead(FitsImg2DRead& fimg) { // --- ATTENTION --- // FitsImg2DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile()); Init(fof,fimg.HDU(),fimg.DbgLevel); } /*! Destructor. */ FitsImg2DRead::~FitsImg2DRead() { // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS if(FitsOF!=NULL) delete FitsOF; } /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //! Class for reading a 3D image from a FITS file /*! \class SOPHYA::FitsImg3DRd \ingroup FitsIOServer Class for reading a 3D image from a FITS file */ ////////////////////////////////////////////////////////////// /*! Constructor. \param fof : Pointer to the Class for opening the FITS file \param ihdu : number of the HDU where the 3D image is. \param lp : debug level \verbatim - if ihdu<=0 first IMAGE hdu is taken - if ihdu>nhdu ihdu is set to nhdu \endverbatim \warning ihdu = [1,nhdu] */ FitsImg3DRd::FitsImg3DRd(FitsOpenFile* fof,int ihdu,int lp) { Init(fof,ihdu,lp); } /*! Constructor by copy */ FitsImg3DRd::FitsImg3DRd(FitsImg3DRd& fbt) { Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel); } /*! Constructor by default */ FitsImg3DRd::FitsImg3DRd() { Naxis[0] = Naxis[1] = Naxis[2] = 0; SetNulVal(); SetDebug(0); FitsOF = NULL; } /*! Init routine called by the constructor */ void FitsImg3DRd::Init(FitsOpenFile* fof,int ihdu,int lp) { // Initialisation des Parametres Generaux Naxis[0] = Naxis[1] = Naxis[2] = 0; SetNulVal(); SetDebug(lp); FitsOF = NULL; // Caracteristiques du FitsOpenFile FitsOF = fof; if(FitsOF==NULL) throw NullPtrError("FitsImg3DRd::Init: FitsOpenFile pointer is NULL\n"); if(GetFitsPtr()==NULL) throw NullPtrError("FitsImg3DRd::Init: FitsPtr pointer is NULL\n"); int sta = 0; if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU(); // Get HDU 3D image // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init() if(FitsOF->GetPosStatus()==false) { if(ihdu==0) { // find the first IMAGE_HDU FitsOF->MoveToFirst(IMAGE_HDU); } else { int rc = FitsOF->MoveToHDU(ihdu); if(rc!=ihdu) throw RangeCheckError("FitsImg3DRd::Init: Error moving to requested HDU\n"); } } else { // Fits file has already been positionned if(ihdu>0 && ihdu!=HDU()) throw RangeCheckError("FitsImg3DRd::Init: file already posit. at another HDU\n"); } // Check HDUType and set position status to TRUE if(HDUType()!=IMAGE_HDU) throw TypeMismatchExc("FitsImg3DRd::Init: HDU not IMAGE_HDU\n"); FitsOF->SetPosStatus(true); // Get NAXIS 1, 2 et 3 int nfound=0; // car fits_read_keys_lnglng n'est pas prototype dans longnam.h if(ffgknjj(GetFitsPtr(),"NAXIS",1,3,Naxis,&nfound,&sta)) { FitsOpenFile::printerror(sta); throw RangeCheckError("FitsImg3DRd::Init: Error reading NAXIS cards\n"); } if(DbgLevel>1) cout<<"...Init(hdu="< i varie le plus vite et k le moins vite */ /*! Read 3D image into a TArray */ LONGLONG FitsImg3DRd::Read(TArray& data) { int sta=0; uint_2* arr = new uint_2[Naxis[0]]; sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim); for(LONGLONG k=0;k): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg3DRd::Read(TArray& data) { int sta=0; int_4* arr = new int_4[Naxis[0]]; sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim); int T = (sizeof(long)==4) ? TLONG: TINT; for(LONGLONG k=0;k): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg3DRd::Read(TArray& data) { int sta=0; int_8* arr = new int_8[Naxis[0]]; sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim); for(LONGLONG k=0;k): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg3DRd::Read(TArray& data) { int sta=0; float* arr = new float[Naxis[0]]; sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim); for(LONGLONG k=0;k): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg3DRd::Read(TArray& data) { int sta=0; double* arr = new double[Naxis[0]]; sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim); for(LONGLONG k=0;k): Error Reading Fits file\n"); } for(LONGLONG i=0;i */ LONGLONG FitsImg3DRd::Read(LONGLONG j, LONGLONG k, TVector& data) { if(j<0 || k<0 || j>=Naxis[1] || k>=Naxis[2]) throw NotAvailableOperation("FitsImg3DRd::Read(TVector): bad j/k number\n"); int sta=0; if(data.Size() != Naxis[0]) data.ReSize(Naxis[0]); int T = (sizeof(long)==4) ? TLONG: TINT; LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,data.Data(),NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsImg3DRd::Read(TVector): Error Reading Fits file\n"); } return Naxis[0]; } /*! Read 3D image into a TVector */ LONGLONG FitsImg3DRd::Read(LONGLONG j, LONGLONG k, TVector& data) { if(j<0 || k<0 || j>=Naxis[1] || k>=Naxis[2]) throw NotAvailableOperation("FitsImg3DRd::Read(TVector): bad j/k number\n"); int sta=0; if(data.Size() != Naxis[0]) data.ReSize(Naxis[0]); LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,data.Data(),NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsImg3DRd::Read(TVector): Error Reading Fits file\n"); } return Naxis[0]; } /*! Read 3D image into a TVector */ LONGLONG FitsImg3DRd::Read(LONGLONG j, LONGLONG k, TVector& data) { if(j<0 || k<0 || j>=Naxis[1] || k>=Naxis[2]) throw NotAvailableOperation("FitsImg3DRd::Read(TVector): bad j/k number\n"); int sta=0; if(data.Size() != Naxis[0]) data.ReSize(Naxis[0]); LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,data.Data(),NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsImg3DRd::Read(TVector): Error Reading Fits file\n"); } return Naxis[0]; } /*! Read 3D image pixel i,j,k with i=[0,Naxis1[ , j=[0,Naxis2[ , k=[0,Naxis3[ */ double FitsImg3DRd::Read(LONGLONG i, LONGLONG j, LONGLONG k) { int sta=0; if(i<0 || j<0 || k<0 || i>=Naxis[0] || j>=Naxis[1] || k>=Naxis[2]) throw NotAvailableOperation("FitsImg3DRd::Read(i,j,k): bad i/j/k number\n"); LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+i+1; double val = 0.; fits_read_img(GetFitsPtr(),TDOUBLE,deb,1,&NulVal,&val,NULL,&sta); if(sta) { FitsOpenFile::printerror(sta); throw NotAvailableOperation("FitsImg3DRd::Read(i,j,k): Error Reading Fits file\n"); } return val; } /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //! Class for reading a 3D image from a FITS file /*! \class SOPHYA::FitsImg3DRead \ingroup FitsIOServer Class for reading a 3D image from a FITS file */ ////////////////////////////////////////////////////////////// /*! Constructor. \param fname : name of the FITS file \param ihdu : number of the HDU where the 3D image is. \param lp : debug level \verbatim - if ihdu<=0 first IMAGE hdu is taken - if ihdu>nhdu ihdu is set to nhdu \endverbatim \warning ihdu = [1,nhdu] */ FitsImg3DRead::FitsImg3DRead(string fname,int ihdu,int lp) : FitsImg3DRd(new FitsOpenFile(fname),ihdu,lp) { } /*! Constructor. see below */ FitsImg3DRead::FitsImg3DRead(const char * cfname,int ihdu,int lp) : FitsImg3DRd(new FitsOpenFile(cfname),ihdu,lp) { } /*! Constructor by default */ FitsImg3DRead::FitsImg3DRead() : FitsImg3DRd() { } /*! Constructor by copy */ FitsImg3DRead::FitsImg3DRead(FitsImg3DRead& fimg) { // --- ATTENTION --- // FitsImg3DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile()); Init(fof,fimg.HDU(),fimg.DbgLevel); } /*! Destructor. */ FitsImg3DRead::~FitsImg3DRead() { // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS if(FitsOF!=NULL) delete FitsOF; }