/* Writer de table Fits (binaire ou ASCII) */ #include "sopnamsp.h" #include "machdefs.h" #include #include #include "pexceptions.h" #include "fabtwriter.h" ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// /*! \class SOPHYA::FitsWriter \ingroup FitsIOServer Class for writing into a FITS file (DO NOT USE) */ /*! Constructor (DO NOT USE). */ FitsWriter::FitsWriter(string fname,int lp) { cr_or_upd_fits(fname.c_str(),false,lp); } /*! Constructor (DO NOT USE). */ FitsWriter::FitsWriter(const char* cfname,int lp) { cr_or_upd_fits(cfname,false,lp); } /*! Constructor (DO NOT USE). */ FitsWriter::FitsWriter(string fname,bool update,int lp) { cr_or_upd_fits(fname.c_str(),update,lp); } /*! Constructor (DO NOT USE). */ FitsWriter::FitsWriter(const char* cfname,bool update,int lp) { cr_or_upd_fits(cfname,update,lp); } /*! See FitsWriter() */ void FitsWriter::cr_or_upd_fits(const char *cfname,bool update,int lp) { FitsPtr = NULL; HduType = 0; SetDebug(lp); FitsFN = cfname; NOverFlow = 0; Update = update; // Init key structure DoubleKey.resize(0); LongKey.resize(0); StringKey.resize(0); if(DbgLevel) cout<<"FitsWriter::cr_or_upd_fits FitsFN="<0) for(unsigned int i=0;i(DoubleKey[i].keyname.c_str()); char* com = const_cast(DoubleKey[i].comment.c_str()); double val = DoubleKey[i].val; if(fits_update_key(FitsPtr,TDOUBLE,key,&val,com,&sta)) printerror(sta); } if(LongKey.size()>0) for(unsigned int i=0;i(LongKey[i].keyname.c_str()); char* com = const_cast(LongKey[i].comment.c_str()); long val = LongKey[i].val; if(fits_update_key(FitsPtr,TLONG,key,&val,com,&sta)) printerror(sta); } if(StringKey.size()>0) for(unsigned int i=0;i(StringKey[i].keyname.c_str()); char* com = const_cast(StringKey[i].comment.c_str()); char* val = const_cast(StringKey[i].val.c_str()); if(fits_update_key(FitsPtr,TSTRING,key,val,com,&sta)) printerror(sta); } DoubleKey.resize(0); LongKey.resize(0); StringKey.resize(0); } void FitsWriter::printerrorwrite(const char* type,int col,long row,int sta) { if(sta==NUM_OVERFLOW) {NOverFlow++; return;} printerror(sta); char str[256]; sprintf(str,"FitsWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d" ,type,col,row,sta); throw NotAvailableOperation(str); } void FitsWriter::printerror(int sta) const { int stat = sta; fits_report_error(stdout,stat); fflush(stdout); return; } ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// /*! \class SOPHYA::FitsABTWriter \ingroup FitsIOServer Class for writing a FITS ASCII or BINARY table \verbatim //----------------------------------------------------------- OPENING A NEW FILE AND WRITING INTO -- Exemple 1: Writing element by element FitsABTWriter fbtw(fitswrit,BINARY_TBL); fbtw.SetExtName("MY_OWN_EXTENSION"); int c1 = fbtw.AddCol("vars",NULL,"km",TSHORT); // col=0 int c2 = fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=1 int c3 = fbtw.AddCol("varl",NULL,"Degre",TLONG); // col=2 int c4 = fbtw.AddCol("varf",NULL,"",TFLOAT); // col=3 int c5 = fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=4 fbtw.SetDebug(3); for(long i=0;i<1000;i++) { double x = i; fbtw.Write(c1,i,1000.*x); // if overflow, managed by cfitsio // Write(int,long,double) is called fbtw.Write(c2,i,(short)(1000.*x)); // if overflow, managed by language // Write(int,long,short) is called fbtw.Write(c3,i,x); fbtw.Write(c4,i,x); fbtw.Write(c5,i,x); } cout<<"Number of Overflows when writing: " < datad(100); TVector dataf(100); TVector datal(100); for(long i=0;i<9990;i+=100) { long i2=i+100-1; if(i2>=9990) i2=9990-1; for(long j=0;j<100;j++) datad(i) = ...; for(long j=0;j<100;j++) dataf(i) = ...; for(long j=0;j<100;j++) datal(i) = ...; fbtw.Write(1,i,datal); fbtw.Write(2,i,dataf); fbtw.Write(3,i,datad); } \endverbatim \verbatim //----------------------------------------------------------- //----------------------------------------------------------- //----------------------------------------------------------- OPENING A NEW FILE AND WRITING 2 TABLE EXTENSIONS SIMULTANEOUSLY INTO try { cout<<">>>>> Creating a new fits file with FitsABTWriter"<>>>> Another extension fits table with FitsABTWriter"<>>>> Write into the 2 tables simultaneously"<0) { extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char)); strcpy(extname,ExtName.c_str()); } int i; for(i=0;i& val) { if(FirstTime) createtbl(); long nel = val.Size(); int sta=0; if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta)) printerrorwrite("long",col,row,sta); return row+nel; } /*! Write a vector of long (4 Bytes) data to FITS file (see below) */ long FitsABTWriter::Write(int col,long row,TVector& val) { if(FirstTime) createtbl(); long nel = val.Size(); int sta=0; // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ? int T = (sizeof(long)==4) ? TLONG: TINT; if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta)) printerrorwrite("long",col,row,sta); return row+nel; } /*! Write a vector of long long (8 Bytes) data to FITS file (see below) */ long FitsABTWriter::Write(int col,long row,TVector& val) { #ifdef TLONGLONG if(FirstTime) createtbl(); long nel = val.Size(); int sta=0; if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta)) printerrorwrite("long long",col,row,sta); return row+nel; #else throw PException("FitsABTWriter::Write(..,TVector&) Not in that cfitsio version"); #endif } /*! Write a vector of float data to FITS file (see below) */ long FitsABTWriter::Write(int col,long row,TVector& val) { if(FirstTime) createtbl(); long nel = val.Size(); int sta=0; if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta)) printerrorwrite("float",col,row,sta); return row+nel; } /*! Write a vector of double data to FITS file (see below) */ long FitsABTWriter::Write(int col,long row,TVector& val) { if(FirstTime) createtbl(); long nel = val.Size(); int sta=0; if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta)) printerrorwrite("double",col,row,sta); return row+nel; } ///////////////////////////////////////////////// void FitsABTWriter::Print(ostream& os,int lp) const { os<<"FitsABTWriter::Print: FitsFN "<0 && lp>=1) for(int i=0;i<(int)Label.size();i++) os<& data) { Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg(); uint_2* arr = new uint_2[Naxis[0]]; for(int l=0;l): Error Writing Fits file\n"); } } delete [] arr; } /*! Write a long image to FITS file. */ void FitsImg2DWriter::Write(TMatrix& data) { int T = (sizeof(long)==4) ? TLONG: TINT; Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg(); int_4* arr = new int_4[Naxis[0]]; for(int l=0;l): Error Writing Fits file\n"); } } delete [] arr; } /*! Write a float image to FITS file. */ void FitsImg2DWriter::Write(TMatrix& data) { Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg(); float* arr = new float[Naxis[0]]; for(int l=0;l): Error Writing Fits file\n"); } } delete [] arr; } /*! Write a double image to FITS file. */ void FitsImg2DWriter::Write(TMatrix& data) { Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg(); double* arr = new double[Naxis[0]]; for(int l=0;l): Error Writing Fits file\n"); } } delete [] arr; } /*! Print infos. */ void FitsImg2DWriter::Print(ostream& os) const { os<<"FitsImg2DWriter::Print: FitsFN "<