Changeset 2450 in Sophya for trunk/SophyaExt/FitsIOServer
- Timestamp:
- Oct 29, 2003, 1:51:17 PM (22 years ago)
- Location:
- trunk/SophyaExt/FitsIOServer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaExt/FitsIOServer/fabtwriter.cc
r2174 r2450 12 12 \verbatim 13 13 //----------------------------------------------------------- 14 OPENING A NEW FILE AND WRITING INTO 14 15 -- Exemple 1: Writing element by element 15 16 FitsABTWriter fbtw(fitswrit,BINARY_TBL); 16 17 fbtw.SetExtName("MY_OWN_EXTENSION"); 17 fbtw.AddCol("vars",NULL,"km",TSHORT); // col=018 fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=119 fbtw.AddCol("varl",NULL,"Degre",TLONG); // col=220 fbtw.AddCol("varf",NULL,"",TFLOAT); // col=321 fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=418 int c1 = fbtw.AddCol("vars",NULL,"km",TSHORT); // col=0 19 int c2 = fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=1 20 int c3 = fbtw.AddCol("varl",NULL,"Degre",TLONG); // col=2 21 int c4 = fbtw.AddCol("varf",NULL,"",TFLOAT); // col=3 22 int c5 = fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=4 22 23 fbtw.SetDebug(3); 23 24 for(long i=0;i<1000;i++) { 24 25 double x = i; 25 fbtw.Write( 0,i,1000.*x); // if overflow, managed by cfitsio26 fbtw.Write(c1,i,1000.*x); // if overflow, managed by cfitsio 26 27 // Write(int,long,double) is called 27 fbtw.Write( 1,i,(short)(1000.*x));28 fbtw.Write(c2,i,(short)(1000.*x)); 28 29 // if overflow, managed by language 29 30 // Write(int,long,short) is called 30 fbtw.Write( 2,i,x);31 fbtw.Write( 3,i,x);32 fbtw.Write( 4,i,x);31 fbtw.Write(c3,i,x); 32 fbtw.Write(c4,i,x); 33 fbtw.Write(c5,i,x); 33 34 } 34 35 cout<<"Number of Overflows when writing: " … … 51 52 } 52 53 \endverbatim 54 \verbatim 55 //----------------------------------------------------------- 56 //----------------------------------------------------------- 57 //----------------------------------------------------------- 58 OPENING A NEW FILE AND WRITING 2 TABLE EXTENSIONS SIMULTANEOUSLY INTO 59 try { 60 61 cout<<">>>>> Creating a new fits file with FitsABTWriter"<<endl; 62 FitsABTWriter fbtw("!cmvtstfits3.fits",BINARY_TBL,3); 63 //FitsABTWriter fbtw("!cmvtstfits3.fits",false,BINARY_TBL,3); 64 fbtw.SetExtName("Test fits table 1"); 65 cout<<"Writing Keys"<<endl; 66 fbtw.WriteKey("MYKEYL",(long)123456789,"my LONG key"); 67 fbtw.WriteKey("MYKEYD",1.9999999,"my DOUBLE key"); 68 fbtw.WriteKey("MYKEYC","how are you ?","my CHAR* key"); 69 dum="do you feel better ?"; 70 fbtw.WriteKey("MYKEYS",dum,"my STRING key"); 71 i1 = fbtw.AddCol("x1",NULL,"unit1",TDOUBLE); 72 i2 = fbtw.AddCol("x2",NULL,"unit2",TDOUBLE); 73 i3 = fbtw.AddCol("x3",NULL,"unit3",TDOUBLE); 74 fbtw.Print(); 75 76 cout<<">>>>> Another extension fits table with FitsABTWriter"<<endl; 77 FitsABTWriter fbtw2("cmvtstfits3.fits",true,BINARY_TBL,3); 78 fbtw2.SetExtName("Test fits table 2"); 79 cout<<"Writing Keys"<<endl; 80 fbtw2.WriteKey("MYKEYL",(long)-123456789,"my new LONG key"); 81 fbtw2.WriteKey("MYKEYD",-1.9999999,"my new clef DOUBLE key"); 82 fbtw2.WriteKey("MYKEYC","how are you NOW ?","my new CHAR* key"); 83 dum="do you feel better NOW ?"; 84 fbtw2.WriteKey("MYKEYS",dum,"my new STRING key"); 85 i11 = fbtw2.AddCol("x11",NULL,"unit11",TDOUBLE); 86 i12 = fbtw2.AddCol("x12",NULL,"unit12",TDOUBLE); 87 i13 = fbtw2.AddCol("x13",NULL,"unit13",TDOUBLE); 88 fbtw2.Print(); 89 90 cout<<">>>>> Write into the 2 tables simultaneously"<<endl; 91 for(int i=0;i<NNN;i++) { 92 fbtw.Write(i1,i,i+1.); 93 fbtw.Write(i2,i,i+11.); 94 fbtw.Write(i3,i,i+101.); 95 fbtw2.Write(i11,i,-(i+1.)); 96 fbtw2.Write(i12,i,-(i+11.)); 97 fbtw2.Write(i13,i,-(i+101.)); 98 } 99 100 } catch (PThrowable & exc) { 101 cout<<"Exception : "<<(string)typeid(exc).name() 102 <<" - Msg= "<<exc.Msg()<<endl; 103 return -2; 104 } catch (...) { 105 cout<<" some other exception was caught !"<<endl; 106 return -2; 107 } 108 \endverbatim 53 109 */ 54 110 … … 56 112 /*! 57 113 Constructor. 58 \param fname : FITS file name to be written 114 \param fname : FITS file name to be written (a new fits file is created) 59 115 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL) 60 116 \param lp : debug level … … 62 118 FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp) 63 119 { 64 createfits(fname.c_str(),hdutype,lp); 120 cr_or_upd_fits(fname.c_str(),false,hdutype,lp); 121 } 122 123 /*! 124 Constructor. 125 \param cfname : FITS file name to be written (a new fits file is created) 126 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL) 127 \param lp : debug level 128 */ 129 FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp) 130 { 131 cr_or_upd_fits(cfname,false,hdutype,lp); 132 } 133 134 /*! 135 Constructor. 136 \param fname : FITS file name to be written (created or updated) 137 \param update : file is created if FALSE, open for update if TRUE 138 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL) 139 \param lp : debug level 140 */ 141 FitsABTWriter::FitsABTWriter(string fname,bool update,int hdutype,int lp) 142 { 143 cr_or_upd_fits(fname.c_str(),update,hdutype,lp); 144 } 145 146 /*! 147 Constructor. 148 \param cfname : FITS file name to be written (created or updated) 149 \param update : file is created if FALSE, open for update if TRUE 150 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL) 151 \param lp : debug level 152 */ 153 FitsABTWriter::FitsABTWriter(const char* cfname,bool update,int hdutype,int lp) 154 { 155 cr_or_upd_fits(cfname,update,hdutype,lp); 65 156 } 66 157 67 158 /*! See FitsABTWriter() */ 68 FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp) 69 { 70 createfits(cfname,hdutype,lp); 71 } 72 73 /*! See FitsABTWriter() */ 74 void FitsABTWriter::createfits(const char *cfname,int hdutype,int lp) 159 void FitsABTWriter::cr_or_upd_fits(const char *cfname,bool update,int hdutype,int lp) 75 160 { 76 161 FirstTime = true; … … 80 165 FitsFN = cfname; 81 166 NOverFlow = 0; 167 Update = update; 82 168 83 169 if(DbgLevel) 84 cout<<"FitsABTWriter::cr eatefits FitsFN="<<FitsFN170 cout<<"FitsABTWriter::cr_or_upd_fits FitsFN="<<FitsFN 85 171 <<" HduType="<<HduType<<endl; 86 172 87 173 if(FitsFN.size() <= 0 ) 88 throw ParmError("FitsABTWriter::cr eatefits: Fits file name error\n");174 throw ParmError("FitsABTWriter::cr_or_upd_fits: Fits file name error\n"); 89 175 90 176 if(HduType!=BINARY_TBL && HduType!=ASCII_TBL) 91 throw TypeMismatchExc("FitsABTWriter::cr eatefits: Only BINARY or ASCII table permitted\n");92 93 // create newFITS file177 throw TypeMismatchExc("FitsABTWriter::cr_or_upd_fits: Only BINARY or ASCII table permitted\n"); 178 179 // create or update FITS file 94 180 int sta=0; 95 if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) { 96 printerror(sta); 97 throw NullPtrError("FitsABTWriter::createfits: Error creating Fits file\n"); 181 if(Update) { 182 if(fits_open_file(&FitsPtr,FitsFN.c_str(),READWRITE,&sta)) { 183 printerror(sta); 184 throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error opening Fits file for update\n"); 185 } 186 if(DbgLevel) cout<<"FitsABTWriter::cr_or_upd_fits: fits file has been opened for update"<<endl; 187 } else { 188 if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) { 189 printerror(sta); 190 throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error creating new Fits file\n"); 191 } 192 if(DbgLevel) cout<<"FitsABTWriter::cr_or_upd_fits: a new fits file has been created"<<endl; 98 193 } 99 194 … … 102 197 //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) { 103 198 // printerror(sta); 104 // throw NullPtrError("FitsABTWriter::cr eatefits: Error creating Primary extension\n");199 // throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error creating Primary extension\n"); 105 200 //} 106 201 … … 144 239 if(comment) k.comment=comment; else k.comment=""; 145 240 LongKey.push_back(k); 241 } 242 243 /*! Write a string value into Fits Header */ 244 void FitsABTWriter::WriteKey(const char *keyname,string val,char* comment) 245 { 246 if(keyname==NULL || strlen(keyname)<=0) return; 247 KeyString k; 248 k.keyname=keyname; 249 k.val=val; 250 if(comment) k.comment=comment; else k.comment=""; 251 StringKey.push_back(k); 146 252 } 147 253 … … 167 273 printerror(sta); 168 274 } 275 if(StringKey.size()>0) 276 for(unsigned int i=0;i<StringKey.size();i++) { 277 char* key = const_cast<char*>(StringKey[i].keyname.c_str()); 278 char* com = const_cast<char*>(StringKey[i].comment.c_str()); 279 char* val = const_cast<char*>(StringKey[i].val.c_str()); 280 if(fits_update_key(FitsPtr,TSTRING,key,val,com,&sta)) 281 printerror(sta); 282 } 169 283 DoubleKey.resize(0); 170 284 LongKey.resize(0); 285 StringKey.resize(0); 171 286 } 172 287 -
trunk/SophyaExt/FitsIOServer/fabtwriter.h
r2322 r2450 21 21 FitsABTWriter(string fname,int hdutype=BINARY_TBL,int lp=0); 22 22 FitsABTWriter(const char* cfname,int hdutype=BINARY_TBL,int lp=0); 23 FitsABTWriter(string fname,bool update,int hdutype=BINARY_TBL,int lp=0); 24 FitsABTWriter(const char* cfname,bool update,int hdutype=BINARY_TBL,int lp=0); 23 25 virtual ~FitsABTWriter(); 24 26 … … 29 31 //! Write a long value in Fits header. 30 32 void WriteKey(const char *keyname,long val,char* comment=NULL); 33 //! Write a string value in Fits header. 34 void WriteKey(const char *keyname,string val,char* comment=NULL); 35 //! Write a character string value in Fits header. 36 inline void WriteKey(const char *keyname,char* val,char* comment=NULL) 37 {string dum=val; WriteKey(keyname,dum,comment);} 31 38 32 39 //! Add a new column to the FITS table and return its number (see addcol). … … 73 80 struct KeyDouble {string keyname; double val; string comment;}; 74 81 struct KeyLong {string keyname; long val; string comment;}; 82 struct KeyString {string keyname; string val; string comment;}; 75 83 76 void cr eatefits(const char *cfname,int hdutype,int lp);84 void cr_or_upd_fits(const char *cfname,bool update,int hdutype,int lp); 77 85 int addcol(const char* label,const char* tform 78 86 ,const char* tunit,int datatype); … … 83 91 84 92 string FitsFN,ExtName; 93 bool Update; 85 94 int HduType; 86 95 unsigned short DbgLevel; … … 94 103 vector<struct KeyDouble> DoubleKey; 95 104 vector<struct KeyLong> LongKey; 105 vector<struct KeyString> StringKey; 96 106 }; 97 107
Note:
See TracChangeset
for help on using the changeset viewer.