[1654] | 1 | /* Writer de table Fits (binaire ou ASCII) */
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <stdlib.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include "pexceptions.h"
|
---|
| 6 | #include "fabtwriter.h"
|
---|
| 7 |
|
---|
| 8 | /*!
|
---|
| 9 | \class SOPHYA::FitsABTWriter
|
---|
| 10 | \ingroup FitsIOServer
|
---|
| 11 | Class for writing a FITS ASCII or BINARY table
|
---|
[1659] | 12 | \verbatim
|
---|
| 13 | //-----------------------------------------------------------
|
---|
[2450] | 14 | OPENING A NEW FILE AND WRITING INTO
|
---|
[1659] | 15 | -- Exemple 1: Writing element by element
|
---|
| 16 | FitsABTWriter fbtw(fitswrit,BINARY_TBL);
|
---|
[1654] | 17 | fbtw.SetExtName("MY_OWN_EXTENSION");
|
---|
[2450] | 18 | 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
|
---|
[1654] | 23 | fbtw.SetDebug(3);
|
---|
| 24 | for(long i=0;i<1000;i++) {
|
---|
| 25 | double x = i;
|
---|
[2450] | 26 | fbtw.Write(c1,i,1000.*x); // if overflow, managed by cfitsio
|
---|
[1654] | 27 | // Write(int,long,double) is called
|
---|
[2450] | 28 | fbtw.Write(c2,i,(short)(1000.*x));
|
---|
[1654] | 29 | // if overflow, managed by language
|
---|
| 30 | // Write(int,long,short) is called
|
---|
[2450] | 31 | fbtw.Write(c3,i,x);
|
---|
| 32 | fbtw.Write(c4,i,x);
|
---|
| 33 | fbtw.Write(c5,i,x);
|
---|
[1654] | 34 | }
|
---|
| 35 | cout<<"Number of Overflows when writing: "
|
---|
| 36 | <<fbtw.GetNOverFlow()<<endl;
|
---|
[1659] | 37 |
|
---|
| 38 | //-----------------------------------------------------------
|
---|
| 39 | -- Exemple 2: Writing into TVector
|
---|
| 40 | ...
|
---|
| 41 | TVector<double> datad(100);
|
---|
| 42 | TVector<float> dataf(100);
|
---|
| 43 | TVector<int_4> datal(100);
|
---|
| 44 | for(long i=0;i<9990;i+=100) {
|
---|
| 45 | long i2=i+100-1; if(i2>=9990) i2=9990-1;
|
---|
| 46 | for(long j=0;j<100;j++) datad(i) = ...;
|
---|
| 47 | for(long j=0;j<100;j++) dataf(i) = ...;
|
---|
| 48 | for(long j=0;j<100;j++) datal(i) = ...;
|
---|
| 49 | fbtw.Write(1,i,datal);
|
---|
| 50 | fbtw.Write(2,i,dataf);
|
---|
| 51 | fbtw.Write(3,i,datad);
|
---|
| 52 | }
|
---|
[1654] | 53 | \endverbatim
|
---|
[2450] | 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
|
---|
[1654] | 109 | */
|
---|
| 110 |
|
---|
| 111 | //////////////////////////////////////////////////////////////
|
---|
| 112 | /*!
|
---|
| 113 | Constructor.
|
---|
[2450] | 114 | \param fname : FITS file name to be written (a new fits file is created)
|
---|
[1659] | 115 | \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
|
---|
| 116 | \param lp : debug level
|
---|
[1654] | 117 | */
|
---|
| 118 | FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
|
---|
| 119 | {
|
---|
[2450] | 120 | cr_or_upd_fits(fname.c_str(),false,hdutype,lp);
|
---|
[1654] | 121 | }
|
---|
| 122 |
|
---|
[2450] | 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 | */
|
---|
[1654] | 129 | FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
|
---|
| 130 | {
|
---|
[2450] | 131 | cr_or_upd_fits(cfname,false,hdutype,lp);
|
---|
[1654] | 132 | }
|
---|
| 133 |
|
---|
[2450] | 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);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[1654] | 158 | /*! See FitsABTWriter() */
|
---|
[2450] | 159 | void FitsABTWriter::cr_or_upd_fits(const char *cfname,bool update,int hdutype,int lp)
|
---|
[1654] | 160 | {
|
---|
| 161 | FirstTime = true;
|
---|
| 162 | FitsPtr = NULL;
|
---|
| 163 | HduType = hdutype;
|
---|
| 164 | SetDebug(lp);
|
---|
| 165 | FitsFN = cfname;
|
---|
| 166 | NOverFlow = 0;
|
---|
[2450] | 167 | Update = update;
|
---|
[1654] | 168 |
|
---|
| 169 | if(DbgLevel)
|
---|
[2450] | 170 | cout<<"FitsABTWriter::cr_or_upd_fits FitsFN="<<FitsFN
|
---|
[1654] | 171 | <<" HduType="<<HduType<<endl;
|
---|
| 172 |
|
---|
| 173 | if(FitsFN.size() <= 0 )
|
---|
[2450] | 174 | throw ParmError("FitsABTWriter::cr_or_upd_fits: Fits file name error\n");
|
---|
[1654] | 175 |
|
---|
| 176 | if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
|
---|
[2450] | 177 | throw TypeMismatchExc("FitsABTWriter::cr_or_upd_fits: Only BINARY or ASCII table permitted\n");
|
---|
[1654] | 178 |
|
---|
[2450] | 179 | // create or update FITS file
|
---|
[1654] | 180 | int sta=0;
|
---|
[2450] | 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;
|
---|
[1654] | 193 | }
|
---|
| 194 |
|
---|
[1657] | 195 | // create d'un Primary HDU
|
---|
[1654] | 196 | //long naxes[1] = {0};
|
---|
| 197 | //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
|
---|
| 198 | // printerror(sta);
|
---|
[2450] | 199 | // throw NullPtrError("FitsABTWriter::cr_or_upd_fits: Error creating Primary extension\n");
|
---|
[1654] | 200 | //}
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /*! Destructor */
|
---|
| 205 | FitsABTWriter::~FitsABTWriter()
|
---|
| 206 | {
|
---|
| 207 | int sta = 0;
|
---|
[1814] | 208 | writekeys();
|
---|
[1654] | 209 | if(fits_close_file(FitsPtr,&sta)) printerror(sta);
|
---|
| 210 | FitsPtr = NULL;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[1814] | 213 | /*! Flush the FitsIO buffer to set good Fits file in case of problems */
|
---|
| 214 | void FitsABTWriter::Flush(void)
|
---|
| 215 | {
|
---|
| 216 | if(FitsPtr==NULL) return;
|
---|
| 217 | int sta = 0;
|
---|
| 218 | if(fits_flush_file(FitsPtr,&sta)) printerror(sta);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /*! Write a double value into Fits Header */
|
---|
| 222 | void FitsABTWriter::WriteKey(const char *keyname,double val,char* comment)
|
---|
| 223 | {
|
---|
| 224 | if(keyname==NULL || strlen(keyname)<=0) return;
|
---|
| 225 | KeyDouble k;
|
---|
| 226 | k.keyname=keyname;
|
---|
| 227 | k.val=val;
|
---|
| 228 | if(comment) k.comment=comment; else k.comment="";
|
---|
| 229 | DoubleKey.push_back(k);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /*! Write a long value into Fits Header */
|
---|
| 233 | void FitsABTWriter::WriteKey(const char *keyname,long val,char* comment)
|
---|
| 234 | {
|
---|
| 235 | if(keyname==NULL || strlen(keyname)<=0) return;
|
---|
| 236 | KeyLong k;
|
---|
| 237 | k.keyname=keyname;
|
---|
| 238 | k.val=val;
|
---|
| 239 | if(comment) k.comment=comment; else k.comment="";
|
---|
| 240 | LongKey.push_back(k);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[2450] | 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);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[1814] | 254 | void FitsABTWriter::writekeys(void)
|
---|
| 255 | // Ecriture effective des clefs
|
---|
| 256 | {
|
---|
[1822] | 257 | if(FitsPtr==NULL) return;
|
---|
[1814] | 258 | int sta=0;
|
---|
| 259 | if(DoubleKey.size()>0)
|
---|
| 260 | for(unsigned int i=0;i<DoubleKey.size();i++) {
|
---|
| 261 | char* key = const_cast<char*>(DoubleKey[i].keyname.c_str());
|
---|
| 262 | char* com = const_cast<char*>(DoubleKey[i].comment.c_str());
|
---|
| 263 | double val = DoubleKey[i].val;
|
---|
| 264 | if(fits_update_key(FitsPtr,TDOUBLE,key,&val,com,&sta))
|
---|
| 265 | printerror(sta);
|
---|
| 266 | }
|
---|
| 267 | if(LongKey.size()>0)
|
---|
| 268 | for(unsigned int i=0;i<LongKey.size();i++) {
|
---|
| 269 | char* key = const_cast<char*>(LongKey[i].keyname.c_str());
|
---|
| 270 | char* com = const_cast<char*>(LongKey[i].comment.c_str());
|
---|
| 271 | long val = LongKey[i].val;
|
---|
| 272 | if(fits_update_key(FitsPtr,TLONG,key,&val,com,&sta))
|
---|
| 273 | printerror(sta);
|
---|
| 274 | }
|
---|
[2450] | 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 | }
|
---|
[1814] | 283 | DoubleKey.resize(0);
|
---|
| 284 | LongKey.resize(0);
|
---|
[2450] | 285 | StringKey.resize(0);
|
---|
[1814] | 286 | }
|
---|
| 287 |
|
---|
[1654] | 288 | //////////////////////////////////////////////////////////////
|
---|
| 289 | /*!
|
---|
| 290 | Add a new column to the FITS table
|
---|
[1659] | 291 | \param label : column label
|
---|
[1660] | 292 | \param tform : fits tform definition ("1I","1J","1E","1J",...)
|
---|
| 293 | (can be automatically set as "datatype"
|
---|
| 294 | if BINARY_TBL and tform="" or tform=NULL)
|
---|
[1659] | 295 | \param tunit : fits tunit definition (optional)
|
---|
[2169] | 296 | \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TLONGLONG TFLOAT TDOUBLE
|
---|
[1660] | 297 | TUSHORT TULONG. That parameter is only use in case
|
---|
| 298 | of a BINARY_TBL table when tform is not defined).
|
---|
[1659] | 299 | \return The number of the new added column in the table.
|
---|
| 300 | \warning col = [0,ncol-1]
|
---|
[1654] | 301 | */
|
---|
[1660] | 302 | int FitsABTWriter::addcol(const char* label,const char* tform
|
---|
| 303 | ,const char* tunit,int datatype)
|
---|
[1654] | 304 | {
|
---|
| 305 | if(!FirstTime)
|
---|
| 306 | throw AllocationError("FitsABTWriter::addcol: table already created\n");
|
---|
| 307 |
|
---|
[1660] | 308 | // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
|
---|
| 309 | bool tformauto = false;
|
---|
[2174] | 310 | if(HduType==BINARY_TBL || HduType==ASCII_TBL) {
|
---|
[1669] | 311 | if(tform==NULL) tformauto = true;
|
---|
| 312 | else if(strlen(tform)<=0) tformauto = true;
|
---|
[1660] | 313 | }
|
---|
[2174] | 314 | if(tformauto && HduType==BINARY_TBL) {
|
---|
[1660] | 315 | char str[8];
|
---|
| 316 | if(datatype==TBYTE) strcpy(str,"1B");
|
---|
| 317 | else if(datatype==TSHORT) strcpy(str,"1I");
|
---|
| 318 | else if(datatype==TLONG) strcpy(str,"1J");
|
---|
[2169] | 319 | #ifdef TINT32BIT
|
---|
| 320 | else if(datatype==TINT32BIT) strcpy(str,"1J");
|
---|
| 321 | #endif
|
---|
| 322 | #ifdef TLONGLONG
|
---|
| 323 | else if(datatype==TLONGLONG) strcpy(str,"1K");
|
---|
| 324 | #endif
|
---|
[1657] | 325 | else if(datatype==TFLOAT) strcpy(str,"1E");
|
---|
| 326 | else if(datatype==TDOUBLE) strcpy(str,"1D");
|
---|
[1660] | 327 | else if(datatype==TUSHORT) strcpy(str,"1U");
|
---|
| 328 | else if(datatype==TULONG) strcpy(str,"1V");
|
---|
| 329 | else
|
---|
| 330 | throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
|
---|
[1654] | 331 | TForm.push_back(str);
|
---|
[2174] | 332 | } else if(tformauto && HduType==ASCII_TBL) {
|
---|
| 333 | char str[8];
|
---|
| 334 | if(datatype==TBYTE) strcpy(str,"I5");
|
---|
| 335 | else if(datatype==TSHORT) strcpy(str,"I7");
|
---|
| 336 | else if(datatype==TLONG) strcpy(str,"I11");
|
---|
| 337 | #ifdef TINT32BIT
|
---|
| 338 | else if(datatype==TINT32BIT) strcpy(str,"I11");
|
---|
| 339 | #endif
|
---|
| 340 | #ifdef TLONGLONG
|
---|
| 341 | else if(datatype==TLONGLONG) strcpy(str,"I21");
|
---|
| 342 | #endif
|
---|
| 343 | else if(datatype==TFLOAT) strcpy(str,"E29.20");
|
---|
| 344 | else if(datatype==TDOUBLE) strcpy(str,"E29.20");
|
---|
| 345 | else if(datatype==TUSHORT) strcpy(str,"I7");
|
---|
| 346 | else if(datatype==TULONG) strcpy(str,"I11");
|
---|
| 347 | else
|
---|
| 348 | throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
|
---|
| 349 | TForm.push_back(str);
|
---|
[1660] | 350 | } else {
|
---|
| 351 | if(tform) TForm.push_back(tform); else TForm.push_back("");
|
---|
| 352 | datatype = 0;
|
---|
| 353 | }
|
---|
| 354 | Label.push_back(label);
|
---|
| 355 | if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
|
---|
[1654] | 356 |
|
---|
| 357 | int n = (int) Label.size() -1;
|
---|
| 358 |
|
---|
| 359 | if(DbgLevel)
|
---|
| 360 | cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
|
---|
| 361 | <<" TForm="<<TForm[n]
|
---|
[1660] | 362 | <<" TUnit="<<TUnit[n]
|
---|
| 363 | <<" datatype="<<datatype<<endl;
|
---|
[1654] | 364 |
|
---|
| 365 | return n;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /*! Create the table. Done at the first write request. */
|
---|
| 369 | void FitsABTWriter::createtbl(void)
|
---|
| 370 | {
|
---|
| 371 | if(!FirstTime) return;
|
---|
| 372 |
|
---|
| 373 | int tfields = Label.size();
|
---|
| 374 | if(tfields<=0)
|
---|
| 375 | throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
|
---|
| 376 |
|
---|
| 377 | long nrows = 0;
|
---|
| 378 | char *extname = NULL;
|
---|
| 379 | char **ttype = (char **) malloc(tfields*sizeof(char *));
|
---|
| 380 | char **tform = (char **) malloc(tfields*sizeof(char *));
|
---|
| 381 | char **tunit = (char **) malloc(tfields*sizeof(char *));
|
---|
| 382 |
|
---|
| 383 | if(ExtName.size()>0) {
|
---|
| 384 | extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
|
---|
| 385 | strcpy(extname,ExtName.c_str());
|
---|
| 386 | }
|
---|
[1664] | 387 | int i;
|
---|
| 388 | for(i=0;i<tfields;i++) {
|
---|
[1654] | 389 | ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
|
---|
| 390 | strcpy(ttype[i],Label[i].c_str());
|
---|
| 391 | tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
|
---|
| 392 | strcpy(tform[i],TForm[i].c_str());
|
---|
| 393 | tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
|
---|
| 394 | strcpy(tunit[i],TUnit[i].c_str());
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | // append a new empty binary/ascii table onto the FITS file
|
---|
| 398 | int sta=0;
|
---|
| 399 | if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
|
---|
| 400 | printerror(sta);
|
---|
| 401 | throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[1814] | 404 | // Append Fits key
|
---|
| 405 | writekeys();
|
---|
| 406 |
|
---|
[1654] | 407 | // menage
|
---|
| 408 | if(extname) delete [] extname;
|
---|
[1664] | 409 | for(i=0;i<tfields;i++) {
|
---|
[1654] | 410 | if(ttype[i]) delete [] ttype[i];
|
---|
| 411 | if(tform[i]) delete [] tform[i];
|
---|
| 412 | if(tunit[i]) delete [] tunit[i];
|
---|
| 413 | }
|
---|
| 414 | if(ttype) delete [] ttype;
|
---|
| 415 | if(tform) delete [] tform;
|
---|
| 416 | if(tunit) delete [] tunit;
|
---|
| 417 |
|
---|
| 418 | FirstTime = false;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | //////////////////////////////////////////////////////////////
|
---|
| 422 | /*!
|
---|
[2173] | 423 | Write a data to FITS file.
|
---|
[1659] | 424 | \param col : column number [0,ncol[
|
---|
| 425 | \param row : row number [0,nrow[
|
---|
| 426 | \param val : value to be written
|
---|
| 427 | \warning that routine write a SHORT value into column "col"
|
---|
[1654] | 428 | which could have been defined with an other type.
|
---|
| 429 | Cast is performed by the cfitsio package.
|
---|
[1659] | 430 | \verbatim
|
---|
[1654] | 431 | WARNING: suppose that the column has be defined to be TSHORT
|
---|
[1660] | 432 | and suppose that you wanted to write a double value "val"
|
---|
| 433 | - If you write dummy.Write(col,row,(short)(val))
|
---|
| 434 | the Write(int,long,short) routine is called and
|
---|
[1654] | 435 | the cast is performed by the C++ language.
|
---|
| 436 | - If you write dummy.Write(col,row,val) where val is double
|
---|
[1660] | 437 | the Write(int,long,double) routine is called and
|
---|
[1654] | 438 | the cast is performed by the cfistio package.
|
---|
| 439 | \endverbatim
|
---|
| 440 | */
|
---|
[2173] | 441 |
|
---|
[2174] | 442 | /*! Write unsigned char (1 Byte) data to FITS file (see below) */
|
---|
| 443 | void FitsABTWriter::Write(int col,long row,uint_1 val)
|
---|
[2173] | 444 | {
|
---|
| 445 | if(FirstTime) createtbl();
|
---|
| 446 | int sta=0;
|
---|
| 447 | if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,1,1,&val,&sta))
|
---|
[2174] | 448 | printerrorwrite("unsigned char",col,row,sta);
|
---|
[2173] | 449 | }
|
---|
| 450 |
|
---|
| 451 | /*! Write short (2 Bytes) data to FITS file (see below) */
|
---|
[2170] | 452 | void FitsABTWriter::Write(int col,long row,int_2 val)
|
---|
[1654] | 453 | {
|
---|
| 454 | if(FirstTime) createtbl();
|
---|
| 455 | int sta=0;
|
---|
[1657] | 456 | if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 457 | printerrorwrite("short",col,row,sta);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[2170] | 460 | /*! Write unsigned short (2 Bytes) data to FITS file (see below) */
|
---|
| 461 | void FitsABTWriter::Write(int col,long row,uint_2 val)
|
---|
| 462 | {
|
---|
| 463 | if(FirstTime) createtbl();
|
---|
| 464 | int sta=0;
|
---|
| 465 | if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
| 466 | printerrorwrite("unsigned short",col,row,sta);
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[2169] | 469 | /*! Write long (4 Bytes) data to FITS file (see below) */
|
---|
[1657] | 470 | void FitsABTWriter::Write(int col,long row,int_4 val)
|
---|
[1654] | 471 | {
|
---|
| 472 | if(FirstTime) createtbl();
|
---|
| 473 | int sta=0;
|
---|
[1657] | 474 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 475 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 476 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 477 | printerrorwrite("long",col,row,sta);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[2173] | 480 | /*! Write unsigned long (4 Bytes) data to FITS file (see below) */
|
---|
| 481 | void FitsABTWriter::Write(int col,long row,uint_4 val)
|
---|
| 482 | {
|
---|
| 483 | if(FirstTime) createtbl();
|
---|
| 484 | int sta=0;
|
---|
| 485 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 486 | int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
|
---|
| 487 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
| 488 | printerrorwrite("long",col,row,sta);
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[2169] | 491 | /*! Write long long (8 Bytes) data to FITS file (see below) */
|
---|
| 492 | void FitsABTWriter::Write(int col,long row,int_8 val)
|
---|
| 493 | {
|
---|
| 494 | #ifdef TLONGLONG
|
---|
| 495 | if(FirstTime) createtbl();
|
---|
| 496 | int sta=0;
|
---|
| 497 | if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
|
---|
| 498 | printerrorwrite("long long",col,row,sta);
|
---|
| 499 | #else
|
---|
| 500 | throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
|
---|
| 501 | #endif
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[1654] | 504 | /*! Write float data to FITS file (see below) */
|
---|
| 505 | void FitsABTWriter::Write(int col,long row,float val)
|
---|
| 506 | {
|
---|
| 507 | if(FirstTime) createtbl();
|
---|
| 508 | int sta=0;
|
---|
[1657] | 509 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 510 | printerrorwrite("float",col,row,sta);
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | /*! Write double data to FITS file (see below) */
|
---|
| 514 | void FitsABTWriter::Write(int col,long row,double val)
|
---|
| 515 | {
|
---|
| 516 | if(FirstTime) createtbl();
|
---|
| 517 | int sta=0;
|
---|
[1657] | 518 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 519 | printerrorwrite("double",col,row,sta);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
[1657] | 522 | //////////////////////////////////////////////////////////////
|
---|
| 523 | /*!
|
---|
[2173] | 524 | Write a vector of data to FITS file.
|
---|
[1659] | 525 | \param col : column number [0,ncol[
|
---|
| 526 | \param row : starting row number [0,nrow[
|
---|
| 527 | \param val : vector to be written
|
---|
| 528 | \return "N" = number of the next row to be written,
|
---|
| 529 | that is "N-1" is the number of the last row written.
|
---|
[1657] | 530 | */
|
---|
[2173] | 531 |
|
---|
[2170] | 532 | /*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
|
---|
| 533 | long FitsABTWriter::Write(int col,long row,TVector<uint_2>& val)
|
---|
| 534 | {
|
---|
| 535 | if(FirstTime) createtbl();
|
---|
| 536 | long nel = val.Size();
|
---|
| 537 | int sta=0;
|
---|
| 538 | if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 539 | printerrorwrite("long",col,row,sta);
|
---|
| 540 | return row+nel;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[2169] | 543 | /*! Write a vector of long (4 Bytes) data to FITS file (see below) */
|
---|
[1657] | 544 | long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
|
---|
| 545 | {
|
---|
| 546 | if(FirstTime) createtbl();
|
---|
| 547 | long nel = val.Size();
|
---|
| 548 | int sta=0;
|
---|
| 549 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 550 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 551 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 552 | printerrorwrite("long",col,row,sta);
|
---|
| 553 | return row+nel;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[2169] | 556 | /*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
|
---|
| 557 | long FitsABTWriter::Write(int col,long row,TVector<int_8>& val)
|
---|
| 558 | {
|
---|
| 559 | #ifdef TLONGLONG
|
---|
| 560 | if(FirstTime) createtbl();
|
---|
| 561 | long nel = val.Size();
|
---|
| 562 | int sta=0;
|
---|
| 563 | if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 564 | printerrorwrite("long long",col,row,sta);
|
---|
| 565 | return row+nel;
|
---|
| 566 | #else
|
---|
[2170] | 567 | throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
|
---|
[2169] | 568 | #endif
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[1657] | 571 | /*! Write a vector of float data to FITS file (see below) */
|
---|
| 572 | long FitsABTWriter::Write(int col,long row,TVector<float>& val)
|
---|
| 573 | {
|
---|
| 574 | if(FirstTime) createtbl();
|
---|
| 575 | long nel = val.Size();
|
---|
| 576 | int sta=0;
|
---|
| 577 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 578 | printerrorwrite("float",col,row,sta);
|
---|
| 579 | return row+nel;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | /*! Write a vector of double data to FITS file (see below) */
|
---|
| 583 | long FitsABTWriter::Write(int col,long row,TVector<double>& val)
|
---|
| 584 | {
|
---|
| 585 | if(FirstTime) createtbl();
|
---|
| 586 | long nel = val.Size();
|
---|
| 587 | int sta=0;
|
---|
| 588 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 589 | printerrorwrite("double",col,row,sta);
|
---|
| 590 | return row+nel;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | //////////////////////////////////////////////////////////////
|
---|
[1660] | 594 | void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
|
---|
[1654] | 595 | {
|
---|
| 596 | if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
|
---|
| 597 | printerror(sta);
|
---|
| 598 | char str[256];
|
---|
| 599 | sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
|
---|
| 600 | ,type,col,row,sta);
|
---|
| 601 | throw NotAvailableOperation(str);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | /////////////////////////////////////////////////
|
---|
| 605 | void FitsABTWriter::printerror(int sta) const
|
---|
| 606 | {
|
---|
| 607 | int stat = sta;
|
---|
| 608 | fits_report_error(stdout,stat);
|
---|
| 609 | fflush(stdout);
|
---|
| 610 | return;
|
---|
| 611 | }
|
---|
[1673] | 612 |
|
---|
| 613 | /////////////////////////////////////////////////
|
---|
[1677] | 614 | void FitsABTWriter::Print(ostream& os,int lp) const
|
---|
[1673] | 615 | {
|
---|
| 616 | os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
|
---|
| 617 | <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
|
---|
| 618 | <<" NCol "<<Label.size()<<endl;
|
---|
| 619 | if(Label.size()>0 && lp>=1)
|
---|
| 620 | for(int i=0;i<(int)Label.size();i++)
|
---|
| 621 | os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
|
---|
| 622 | }
|
---|