[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 |
|
---|
[2453] | 8 | //////////////////////////////////////////////////////////////
|
---|
| 9 | //////////////////////////////////////////////////////////////
|
---|
| 10 | //////////////////////////////////////////////////////////////
|
---|
| 11 | //////////////////////////////////////////////////////////////
|
---|
[1654] | 12 | /*!
|
---|
[2453] | 13 | \class SOPHYA::FitsWriter
|
---|
| 14 | \ingroup FitsIOServer
|
---|
| 15 | Class for writing into a FITS file (DO NOT USE)
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | /*! Constructor (DO NOT USE). */
|
---|
| 19 | FitsWriter::FitsWriter(string fname,int lp)
|
---|
| 20 | {
|
---|
| 21 | cr_or_upd_fits(fname.c_str(),false,lp);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | /*! Constructor (DO NOT USE). */
|
---|
| 25 | FitsWriter::FitsWriter(const char* cfname,int lp)
|
---|
| 26 | {
|
---|
| 27 | cr_or_upd_fits(cfname,false,lp);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | /*! Constructor (DO NOT USE). */
|
---|
| 31 | FitsWriter::FitsWriter(string fname,bool update,int lp)
|
---|
| 32 | {
|
---|
| 33 | cr_or_upd_fits(fname.c_str(),update,lp);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /*! Constructor (DO NOT USE). */
|
---|
| 37 | FitsWriter::FitsWriter(const char* cfname,bool update,int lp)
|
---|
| 38 | {
|
---|
| 39 | cr_or_upd_fits(cfname,update,lp);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /*! See FitsWriter() */
|
---|
| 43 | void FitsWriter::cr_or_upd_fits(const char *cfname,bool update,int lp)
|
---|
| 44 | {
|
---|
| 45 | FitsPtr = NULL;
|
---|
| 46 | HduType = 0;
|
---|
| 47 | SetDebug(lp);
|
---|
| 48 | FitsFN = cfname;
|
---|
| 49 | NOverFlow = 0;
|
---|
| 50 | Update = update;
|
---|
| 51 | // Init key structure
|
---|
| 52 | DoubleKey.resize(0);
|
---|
| 53 | LongKey.resize(0);
|
---|
| 54 | StringKey.resize(0);
|
---|
| 55 |
|
---|
| 56 | if(DbgLevel)
|
---|
| 57 | cout<<"FitsWriter::cr_or_upd_fits FitsFN="<<FitsFN<<endl;
|
---|
| 58 |
|
---|
| 59 | if(FitsFN.size() <= 0 )
|
---|
| 60 | throw ParmError("FitsWriter::cr_or_upd_fits: Fits file name error\n");
|
---|
| 61 |
|
---|
| 62 | // create or update FITS file
|
---|
| 63 | int sta=0;
|
---|
| 64 | if(Update) {
|
---|
| 65 | if(fits_open_file(&FitsPtr,FitsFN.c_str(),READWRITE,&sta)) {
|
---|
| 66 | printerror(sta);
|
---|
| 67 | throw NullPtrError("FitsWriter::cr_or_upd_fits: Error opening Fits file for update\n");
|
---|
| 68 | }
|
---|
| 69 | if(DbgLevel) cout<<"FitsWriter::cr_or_upd_fits: fits file has been opened for update"<<endl;
|
---|
| 70 | } else {
|
---|
| 71 | if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) {
|
---|
| 72 | printerror(sta);
|
---|
| 73 | throw NullPtrError("FitsWriter::cr_or_upd_fits: Error creating new Fits file\n");
|
---|
| 74 | }
|
---|
| 75 | if(DbgLevel) cout<<"FitsWriter::cr_or_upd_fits: a new fits file has been created"<<endl;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // create d'un Primary HDU
|
---|
| 79 | //long naxes[1] = {0};
|
---|
| 80 | //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
|
---|
| 81 | // printerror(sta);
|
---|
| 82 | // throw NullPtrError("FitsWriter::cr_or_upd_fits: Error creating Primary extension\n");
|
---|
| 83 | //}
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /*! Destructor */
|
---|
| 87 | FitsWriter::~FitsWriter()
|
---|
| 88 | {
|
---|
| 89 | int sta = 0;
|
---|
| 90 | writekeys();
|
---|
| 91 | if(fits_close_file(FitsPtr,&sta)) printerror(sta);
|
---|
| 92 | FitsPtr = NULL;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /*! Flush the FitsIO buffer to set good Fits file in case of problems */
|
---|
| 96 | void FitsWriter::Flush(void)
|
---|
| 97 | {
|
---|
| 98 | if(FitsPtr==NULL) return;
|
---|
| 99 | int sta = 0;
|
---|
| 100 | if(fits_flush_file(FitsPtr,&sta)) printerror(sta);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | /*! Write a double value into Fits Header */
|
---|
| 105 | void FitsWriter::WriteKey(const char *keyname,double val,char* comment)
|
---|
| 106 | {
|
---|
| 107 | if(keyname==NULL || strlen(keyname)<=0) return;
|
---|
| 108 | KeyDouble k;
|
---|
| 109 | k.keyname=keyname;
|
---|
| 110 | k.val=val;
|
---|
| 111 | if(comment) k.comment=comment; else k.comment="";
|
---|
| 112 | DoubleKey.push_back(k);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /*! Write a long value into Fits Header */
|
---|
| 116 | void FitsWriter::WriteKey(const char *keyname,long val,char* comment)
|
---|
| 117 | {
|
---|
| 118 | if(keyname==NULL || strlen(keyname)<=0) return;
|
---|
| 119 | KeyLong k;
|
---|
| 120 | k.keyname=keyname;
|
---|
| 121 | k.val=val;
|
---|
| 122 | if(comment) k.comment=comment; else k.comment="";
|
---|
| 123 | LongKey.push_back(k);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /*! Write a string value into Fits Header */
|
---|
| 127 | void FitsWriter::WriteKey(const char *keyname,string val,char* comment)
|
---|
| 128 | {
|
---|
| 129 | if(keyname==NULL || strlen(keyname)<=0) return;
|
---|
| 130 | KeyString k;
|
---|
| 131 | k.keyname=keyname;
|
---|
| 132 | k.val=val;
|
---|
| 133 | if(comment) k.comment=comment; else k.comment="";
|
---|
| 134 | StringKey.push_back(k);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | void FitsWriter::writekeys(void)
|
---|
| 138 | // Ecriture effective des clefs
|
---|
| 139 | {
|
---|
| 140 | if(FitsPtr==NULL) return;
|
---|
| 141 | int sta=0;
|
---|
| 142 | if(DoubleKey.size()>0)
|
---|
| 143 | for(unsigned int i=0;i<DoubleKey.size();i++) {
|
---|
| 144 | char* key = const_cast<char*>(DoubleKey[i].keyname.c_str());
|
---|
| 145 | char* com = const_cast<char*>(DoubleKey[i].comment.c_str());
|
---|
| 146 | double val = DoubleKey[i].val;
|
---|
| 147 | if(fits_update_key(FitsPtr,TDOUBLE,key,&val,com,&sta))
|
---|
| 148 | printerror(sta);
|
---|
| 149 | }
|
---|
| 150 | if(LongKey.size()>0)
|
---|
| 151 | for(unsigned int i=0;i<LongKey.size();i++) {
|
---|
| 152 | char* key = const_cast<char*>(LongKey[i].keyname.c_str());
|
---|
| 153 | char* com = const_cast<char*>(LongKey[i].comment.c_str());
|
---|
| 154 | long val = LongKey[i].val;
|
---|
| 155 | if(fits_update_key(FitsPtr,TLONG,key,&val,com,&sta))
|
---|
| 156 | printerror(sta);
|
---|
| 157 | }
|
---|
| 158 | if(StringKey.size()>0)
|
---|
| 159 | for(unsigned int i=0;i<StringKey.size();i++) {
|
---|
| 160 | char* key = const_cast<char*>(StringKey[i].keyname.c_str());
|
---|
| 161 | char* com = const_cast<char*>(StringKey[i].comment.c_str());
|
---|
| 162 | char* val = const_cast<char*>(StringKey[i].val.c_str());
|
---|
| 163 | if(fits_update_key(FitsPtr,TSTRING,key,val,com,&sta))
|
---|
| 164 | printerror(sta);
|
---|
| 165 | }
|
---|
| 166 | DoubleKey.resize(0);
|
---|
| 167 | LongKey.resize(0);
|
---|
| 168 | StringKey.resize(0);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | void FitsWriter::printerrorwrite(const char* type,int col,long row,int sta)
|
---|
| 172 | {
|
---|
| 173 | if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
|
---|
| 174 | printerror(sta);
|
---|
| 175 | char str[256];
|
---|
| 176 | sprintf(str,"FitsWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
|
---|
| 177 | ,type,col,row,sta);
|
---|
| 178 | throw NotAvailableOperation(str);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | void FitsWriter::printerror(int sta) const
|
---|
| 182 | {
|
---|
| 183 | int stat = sta;
|
---|
| 184 | fits_report_error(stdout,stat);
|
---|
| 185 | fflush(stdout);
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | //////////////////////////////////////////////////////////////////
|
---|
| 190 | //////////////////////////////////////////////////////////////////
|
---|
| 191 | //////////////////////////////////////////////////////////////////
|
---|
| 192 | //////////////////////////////////////////////////////////////////
|
---|
| 193 | /*!
|
---|
[1654] | 194 | \class SOPHYA::FitsABTWriter
|
---|
| 195 | \ingroup FitsIOServer
|
---|
| 196 | Class for writing a FITS ASCII or BINARY table
|
---|
[1659] | 197 | \verbatim
|
---|
| 198 | //-----------------------------------------------------------
|
---|
[2450] | 199 | OPENING A NEW FILE AND WRITING INTO
|
---|
[1659] | 200 | -- Exemple 1: Writing element by element
|
---|
| 201 | FitsABTWriter fbtw(fitswrit,BINARY_TBL);
|
---|
[1654] | 202 | fbtw.SetExtName("MY_OWN_EXTENSION");
|
---|
[2450] | 203 | int c1 = fbtw.AddCol("vars",NULL,"km",TSHORT); // col=0
|
---|
| 204 | int c2 = fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=1
|
---|
| 205 | int c3 = fbtw.AddCol("varl",NULL,"Degre",TLONG); // col=2
|
---|
| 206 | int c4 = fbtw.AddCol("varf",NULL,"",TFLOAT); // col=3
|
---|
| 207 | int c5 = fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=4
|
---|
[1654] | 208 | fbtw.SetDebug(3);
|
---|
| 209 | for(long i=0;i<1000;i++) {
|
---|
| 210 | double x = i;
|
---|
[2450] | 211 | fbtw.Write(c1,i,1000.*x); // if overflow, managed by cfitsio
|
---|
[1654] | 212 | // Write(int,long,double) is called
|
---|
[2450] | 213 | fbtw.Write(c2,i,(short)(1000.*x));
|
---|
[1654] | 214 | // if overflow, managed by language
|
---|
| 215 | // Write(int,long,short) is called
|
---|
[2450] | 216 | fbtw.Write(c3,i,x);
|
---|
| 217 | fbtw.Write(c4,i,x);
|
---|
| 218 | fbtw.Write(c5,i,x);
|
---|
[1654] | 219 | }
|
---|
| 220 | cout<<"Number of Overflows when writing: "
|
---|
| 221 | <<fbtw.GetNOverFlow()<<endl;
|
---|
[1659] | 222 |
|
---|
| 223 | //-----------------------------------------------------------
|
---|
| 224 | -- Exemple 2: Writing into TVector
|
---|
| 225 | ...
|
---|
| 226 | TVector<double> datad(100);
|
---|
| 227 | TVector<float> dataf(100);
|
---|
| 228 | TVector<int_4> datal(100);
|
---|
| 229 | for(long i=0;i<9990;i+=100) {
|
---|
| 230 | long i2=i+100-1; if(i2>=9990) i2=9990-1;
|
---|
| 231 | for(long j=0;j<100;j++) datad(i) = ...;
|
---|
| 232 | for(long j=0;j<100;j++) dataf(i) = ...;
|
---|
| 233 | for(long j=0;j<100;j++) datal(i) = ...;
|
---|
| 234 | fbtw.Write(1,i,datal);
|
---|
| 235 | fbtw.Write(2,i,dataf);
|
---|
| 236 | fbtw.Write(3,i,datad);
|
---|
| 237 | }
|
---|
[1654] | 238 | \endverbatim
|
---|
[2450] | 239 | \verbatim
|
---|
| 240 | //-----------------------------------------------------------
|
---|
| 241 | //-----------------------------------------------------------
|
---|
| 242 | //-----------------------------------------------------------
|
---|
| 243 | OPENING A NEW FILE AND WRITING 2 TABLE EXTENSIONS SIMULTANEOUSLY INTO
|
---|
| 244 | try {
|
---|
| 245 |
|
---|
| 246 | cout<<">>>>> Creating a new fits file with FitsABTWriter"<<endl;
|
---|
| 247 | FitsABTWriter fbtw("!cmvtstfits3.fits",BINARY_TBL,3);
|
---|
| 248 | //FitsABTWriter fbtw("!cmvtstfits3.fits",false,BINARY_TBL,3);
|
---|
| 249 | fbtw.SetExtName("Test fits table 1");
|
---|
| 250 | cout<<"Writing Keys"<<endl;
|
---|
| 251 | fbtw.WriteKey("MYKEYL",(long)123456789,"my LONG key");
|
---|
| 252 | fbtw.WriteKey("MYKEYD",1.9999999,"my DOUBLE key");
|
---|
| 253 | fbtw.WriteKey("MYKEYC","how are you ?","my CHAR* key");
|
---|
| 254 | dum="do you feel better ?";
|
---|
| 255 | fbtw.WriteKey("MYKEYS",dum,"my STRING key");
|
---|
| 256 | i1 = fbtw.AddCol("x1",NULL,"unit1",TDOUBLE);
|
---|
| 257 | i2 = fbtw.AddCol("x2",NULL,"unit2",TDOUBLE);
|
---|
| 258 | i3 = fbtw.AddCol("x3",NULL,"unit3",TDOUBLE);
|
---|
| 259 | fbtw.Print();
|
---|
| 260 |
|
---|
| 261 | cout<<">>>>> Another extension fits table with FitsABTWriter"<<endl;
|
---|
| 262 | FitsABTWriter fbtw2("cmvtstfits3.fits",true,BINARY_TBL,3);
|
---|
| 263 | fbtw2.SetExtName("Test fits table 2");
|
---|
| 264 | cout<<"Writing Keys"<<endl;
|
---|
| 265 | fbtw2.WriteKey("MYKEYL",(long)-123456789,"my new LONG key");
|
---|
| 266 | fbtw2.WriteKey("MYKEYD",-1.9999999,"my new clef DOUBLE key");
|
---|
| 267 | fbtw2.WriteKey("MYKEYC","how are you NOW ?","my new CHAR* key");
|
---|
| 268 | dum="do you feel better NOW ?";
|
---|
| 269 | fbtw2.WriteKey("MYKEYS",dum,"my new STRING key");
|
---|
| 270 | i11 = fbtw2.AddCol("x11",NULL,"unit11",TDOUBLE);
|
---|
| 271 | i12 = fbtw2.AddCol("x12",NULL,"unit12",TDOUBLE);
|
---|
| 272 | i13 = fbtw2.AddCol("x13",NULL,"unit13",TDOUBLE);
|
---|
| 273 | fbtw2.Print();
|
---|
| 274 |
|
---|
| 275 | cout<<">>>>> Write into the 2 tables simultaneously"<<endl;
|
---|
| 276 | for(int i=0;i<NNN;i++) {
|
---|
| 277 | fbtw.Write(i1,i,i+1.);
|
---|
| 278 | fbtw.Write(i2,i,i+11.);
|
---|
| 279 | fbtw.Write(i3,i,i+101.);
|
---|
| 280 | fbtw2.Write(i11,i,-(i+1.));
|
---|
| 281 | fbtw2.Write(i12,i,-(i+11.));
|
---|
| 282 | fbtw2.Write(i13,i,-(i+101.));
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | } catch (PThrowable & exc) {
|
---|
| 286 | cout<<"Exception : "<<(string)typeid(exc).name()
|
---|
| 287 | <<" - Msg= "<<exc.Msg()<<endl;
|
---|
| 288 | return -2;
|
---|
| 289 | } catch (...) {
|
---|
| 290 | cout<<" some other exception was caught !"<<endl;
|
---|
| 291 | return -2;
|
---|
| 292 | }
|
---|
| 293 | \endverbatim
|
---|
[1654] | 294 | */
|
---|
| 295 |
|
---|
| 296 | //////////////////////////////////////////////////////////////
|
---|
| 297 | /*!
|
---|
| 298 | Constructor.
|
---|
[2450] | 299 | \param fname : FITS file name to be written (a new fits file is created)
|
---|
[1659] | 300 | \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
|
---|
| 301 | \param lp : debug level
|
---|
[1654] | 302 | */
|
---|
| 303 | FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
|
---|
[2453] | 304 | : FitsWriter(fname,lp)
|
---|
[1654] | 305 | {
|
---|
[2453] | 306 | FirstTime = true;
|
---|
| 307 | HduType = hdutype;
|
---|
| 308 | if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
|
---|
| 309 | throw
|
---|
| 310 | TypeMismatchExc("FitsABTWriter::FitsABTWriter: Only BINARY or ASCII table permitted\n");
|
---|
[1654] | 311 | }
|
---|
| 312 |
|
---|
[2450] | 313 | /*!
|
---|
| 314 | Constructor.
|
---|
| 315 | \param cfname : FITS file name to be written (a new fits file is created)
|
---|
| 316 | \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
|
---|
| 317 | \param lp : debug level
|
---|
| 318 | */
|
---|
[1654] | 319 | FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
|
---|
[2453] | 320 | : FitsWriter(cfname,lp)
|
---|
[1654] | 321 | {
|
---|
[2453] | 322 | FirstTime = true;
|
---|
| 323 | HduType = hdutype;
|
---|
| 324 | if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
|
---|
| 325 | throw
|
---|
| 326 | TypeMismatchExc("FitsABTWriter::FitsABTWriter: Only BINARY or ASCII table permitted\n");
|
---|
[1654] | 327 | }
|
---|
| 328 |
|
---|
[2450] | 329 | /*!
|
---|
| 330 | Constructor.
|
---|
| 331 | \param fname : FITS file name to be written (created or updated)
|
---|
| 332 | \param update : file is created if FALSE, open for update if TRUE
|
---|
| 333 | \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
|
---|
| 334 | \param lp : debug level
|
---|
| 335 | */
|
---|
| 336 | FitsABTWriter::FitsABTWriter(string fname,bool update,int hdutype,int lp)
|
---|
[2453] | 337 | : FitsWriter(fname,update,lp)
|
---|
[2450] | 338 | {
|
---|
[2453] | 339 | FirstTime = true;
|
---|
| 340 | HduType = hdutype;
|
---|
| 341 | if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
|
---|
| 342 | throw
|
---|
| 343 | TypeMismatchExc("FitsABTWriter::FitsABTWriter: Only BINARY or ASCII table permitted\n");
|
---|
[2450] | 344 | }
|
---|
| 345 |
|
---|
| 346 | /*!
|
---|
| 347 | Constructor.
|
---|
| 348 | \param cfname : FITS file name to be written (created or updated)
|
---|
| 349 | \param update : file is created if FALSE, open for update if TRUE
|
---|
| 350 | \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
|
---|
| 351 | \param lp : debug level
|
---|
| 352 | */
|
---|
| 353 | FitsABTWriter::FitsABTWriter(const char* cfname,bool update,int hdutype,int lp)
|
---|
[2453] | 354 | : FitsWriter(cfname,update,lp)
|
---|
[2450] | 355 | {
|
---|
[1654] | 356 | FirstTime = true;
|
---|
| 357 | HduType = hdutype;
|
---|
| 358 | if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
|
---|
[2453] | 359 | throw
|
---|
| 360 | TypeMismatchExc("FitsABTWriter::FitsABTWriter: Only BINARY or ASCII table permitted\n");
|
---|
[1654] | 361 | }
|
---|
| 362 |
|
---|
| 363 | /*! Destructor */
|
---|
| 364 | FitsABTWriter::~FitsABTWriter()
|
---|
| 365 | {
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | //////////////////////////////////////////////////////////////
|
---|
| 369 | /*!
|
---|
| 370 | Add a new column to the FITS table
|
---|
[1659] | 371 | \param label : column label
|
---|
[1660] | 372 | \param tform : fits tform definition ("1I","1J","1E","1J",...)
|
---|
| 373 | (can be automatically set as "datatype"
|
---|
| 374 | if BINARY_TBL and tform="" or tform=NULL)
|
---|
[1659] | 375 | \param tunit : fits tunit definition (optional)
|
---|
[2169] | 376 | \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TLONGLONG TFLOAT TDOUBLE
|
---|
[2493] | 377 | TUSHORT TULONG TSBYTE. That parameter is only use in case
|
---|
[1660] | 378 | of a BINARY_TBL table when tform is not defined).
|
---|
[1659] | 379 | \return The number of the new added column in the table.
|
---|
| 380 | \warning col = [0,ncol-1]
|
---|
[1654] | 381 | */
|
---|
[1660] | 382 | int FitsABTWriter::addcol(const char* label,const char* tform
|
---|
| 383 | ,const char* tunit,int datatype)
|
---|
[1654] | 384 | {
|
---|
[2453] | 385 | if(!FirstTime)
|
---|
| 386 | throw AllocationError("FitsABTWriter::addcol: table already created\n");
|
---|
[1654] | 387 |
|
---|
[1660] | 388 | // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
|
---|
| 389 | bool tformauto = false;
|
---|
[2174] | 390 | if(HduType==BINARY_TBL || HduType==ASCII_TBL) {
|
---|
[1669] | 391 | if(tform==NULL) tformauto = true;
|
---|
| 392 | else if(strlen(tform)<=0) tformauto = true;
|
---|
[1660] | 393 | }
|
---|
[2174] | 394 | if(tformauto && HduType==BINARY_TBL) {
|
---|
[1660] | 395 | char str[8];
|
---|
| 396 | if(datatype==TBYTE) strcpy(str,"1B");
|
---|
| 397 | else if(datatype==TSHORT) strcpy(str,"1I");
|
---|
| 398 | else if(datatype==TLONG) strcpy(str,"1J");
|
---|
[2169] | 399 | #ifdef TINT32BIT
|
---|
| 400 | else if(datatype==TINT32BIT) strcpy(str,"1J");
|
---|
| 401 | #endif
|
---|
| 402 | #ifdef TLONGLONG
|
---|
| 403 | else if(datatype==TLONGLONG) strcpy(str,"1K");
|
---|
| 404 | #endif
|
---|
[1657] | 405 | else if(datatype==TFLOAT) strcpy(str,"1E");
|
---|
| 406 | else if(datatype==TDOUBLE) strcpy(str,"1D");
|
---|
[1660] | 407 | else if(datatype==TUSHORT) strcpy(str,"1U");
|
---|
| 408 | else if(datatype==TULONG) strcpy(str,"1V");
|
---|
[2493] | 409 | #ifdef TSBYTE
|
---|
| 410 | else if(datatype==TSBYTE) strcpy(str,"1S");
|
---|
| 411 | #endif
|
---|
[1660] | 412 | else
|
---|
| 413 | throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
|
---|
[1654] | 414 | TForm.push_back(str);
|
---|
[2174] | 415 | } else if(tformauto && HduType==ASCII_TBL) {
|
---|
| 416 | char str[8];
|
---|
| 417 | if(datatype==TBYTE) strcpy(str,"I5");
|
---|
| 418 | else if(datatype==TSHORT) strcpy(str,"I7");
|
---|
| 419 | else if(datatype==TLONG) strcpy(str,"I11");
|
---|
| 420 | #ifdef TINT32BIT
|
---|
| 421 | else if(datatype==TINT32BIT) strcpy(str,"I11");
|
---|
| 422 | #endif
|
---|
| 423 | #ifdef TLONGLONG
|
---|
| 424 | else if(datatype==TLONGLONG) strcpy(str,"I21");
|
---|
| 425 | #endif
|
---|
| 426 | else if(datatype==TFLOAT) strcpy(str,"E29.20");
|
---|
| 427 | else if(datatype==TDOUBLE) strcpy(str,"E29.20");
|
---|
| 428 | else if(datatype==TUSHORT) strcpy(str,"I7");
|
---|
| 429 | else if(datatype==TULONG) strcpy(str,"I11");
|
---|
[2493] | 430 | #ifdef TSBYTE
|
---|
| 431 | else if(datatype==TSBYTE) strcpy(str,"I5");
|
---|
| 432 | #endif
|
---|
[2174] | 433 | else
|
---|
| 434 | throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
|
---|
| 435 | TForm.push_back(str);
|
---|
[1660] | 436 | } else {
|
---|
| 437 | if(tform) TForm.push_back(tform); else TForm.push_back("");
|
---|
| 438 | datatype = 0;
|
---|
| 439 | }
|
---|
| 440 | Label.push_back(label);
|
---|
| 441 | if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
|
---|
[1654] | 442 |
|
---|
| 443 | int n = (int) Label.size() -1;
|
---|
| 444 |
|
---|
| 445 | if(DbgLevel)
|
---|
| 446 | cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
|
---|
| 447 | <<" TForm="<<TForm[n]
|
---|
[1660] | 448 | <<" TUnit="<<TUnit[n]
|
---|
| 449 | <<" datatype="<<datatype<<endl;
|
---|
[1654] | 450 |
|
---|
| 451 | return n;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | /*! Create the table. Done at the first write request. */
|
---|
| 455 | void FitsABTWriter::createtbl(void)
|
---|
| 456 | {
|
---|
| 457 | if(!FirstTime) return;
|
---|
| 458 |
|
---|
| 459 | int tfields = Label.size();
|
---|
| 460 | if(tfields<=0)
|
---|
| 461 | throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
|
---|
| 462 |
|
---|
| 463 | long nrows = 0;
|
---|
| 464 | char *extname = NULL;
|
---|
| 465 | char **ttype = (char **) malloc(tfields*sizeof(char *));
|
---|
| 466 | char **tform = (char **) malloc(tfields*sizeof(char *));
|
---|
| 467 | char **tunit = (char **) malloc(tfields*sizeof(char *));
|
---|
| 468 |
|
---|
| 469 | if(ExtName.size()>0) {
|
---|
| 470 | extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
|
---|
| 471 | strcpy(extname,ExtName.c_str());
|
---|
| 472 | }
|
---|
[1664] | 473 | int i;
|
---|
| 474 | for(i=0;i<tfields;i++) {
|
---|
[1654] | 475 | ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
|
---|
| 476 | strcpy(ttype[i],Label[i].c_str());
|
---|
| 477 | tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
|
---|
| 478 | strcpy(tform[i],TForm[i].c_str());
|
---|
| 479 | tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
|
---|
| 480 | strcpy(tunit[i],TUnit[i].c_str());
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | // append a new empty binary/ascii table onto the FITS file
|
---|
| 484 | int sta=0;
|
---|
| 485 | if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
|
---|
| 486 | printerror(sta);
|
---|
| 487 | throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | // menage
|
---|
| 491 | if(extname) delete [] extname;
|
---|
[1664] | 492 | for(i=0;i<tfields;i++) {
|
---|
[1654] | 493 | if(ttype[i]) delete [] ttype[i];
|
---|
| 494 | if(tform[i]) delete [] tform[i];
|
---|
| 495 | if(tunit[i]) delete [] tunit[i];
|
---|
| 496 | }
|
---|
| 497 | if(ttype) delete [] ttype;
|
---|
| 498 | if(tform) delete [] tform;
|
---|
| 499 | if(tunit) delete [] tunit;
|
---|
| 500 |
|
---|
| 501 | FirstTime = false;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | //////////////////////////////////////////////////////////////
|
---|
| 505 | /*!
|
---|
[2173] | 506 | Write a data to FITS file.
|
---|
[1659] | 507 | \param col : column number [0,ncol[
|
---|
| 508 | \param row : row number [0,nrow[
|
---|
| 509 | \param val : value to be written
|
---|
| 510 | \warning that routine write a SHORT value into column "col"
|
---|
[1654] | 511 | which could have been defined with an other type.
|
---|
| 512 | Cast is performed by the cfitsio package.
|
---|
[1659] | 513 | \verbatim
|
---|
[1654] | 514 | WARNING: suppose that the column has be defined to be TSHORT
|
---|
[1660] | 515 | and suppose that you wanted to write a double value "val"
|
---|
| 516 | - If you write dummy.Write(col,row,(short)(val))
|
---|
| 517 | the Write(int,long,short) routine is called and
|
---|
[1654] | 518 | the cast is performed by the C++ language.
|
---|
| 519 | - If you write dummy.Write(col,row,val) where val is double
|
---|
[1660] | 520 | the Write(int,long,double) routine is called and
|
---|
[1654] | 521 | the cast is performed by the cfistio package.
|
---|
| 522 | \endverbatim
|
---|
| 523 | */
|
---|
[2173] | 524 |
|
---|
[2493] | 525 | /*! Write signed char (1 Byte) data to FITS file (see below) */
|
---|
| 526 | void FitsABTWriter::Write(int col,long row,int_1 val)
|
---|
| 527 | {
|
---|
| 528 | #ifdef TSBYTE
|
---|
| 529 | if(FirstTime) createtbl();
|
---|
| 530 | int sta=0;
|
---|
| 531 | if(fits_write_col(FitsPtr,TSBYTE,col+1,row+1,1,1,&val,&sta))
|
---|
| 532 | printerrorwrite("signed char",col,row,sta);
|
---|
| 533 | #else
|
---|
| 534 | throw PException("FitsABTWriter::Write(..,int_2) Not in that cfitsio version");
|
---|
| 535 | #endif
|
---|
| 536 | }
|
---|
| 537 |
|
---|
[2174] | 538 | /*! Write unsigned char (1 Byte) data to FITS file (see below) */
|
---|
| 539 | void FitsABTWriter::Write(int col,long row,uint_1 val)
|
---|
[2173] | 540 | {
|
---|
| 541 | if(FirstTime) createtbl();
|
---|
| 542 | int sta=0;
|
---|
| 543 | if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,1,1,&val,&sta))
|
---|
[2174] | 544 | printerrorwrite("unsigned char",col,row,sta);
|
---|
[2173] | 545 | }
|
---|
| 546 |
|
---|
| 547 | /*! Write short (2 Bytes) data to FITS file (see below) */
|
---|
[2170] | 548 | void FitsABTWriter::Write(int col,long row,int_2 val)
|
---|
[1654] | 549 | {
|
---|
| 550 | if(FirstTime) createtbl();
|
---|
| 551 | int sta=0;
|
---|
[1657] | 552 | if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 553 | printerrorwrite("short",col,row,sta);
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[2170] | 556 | /*! Write unsigned short (2 Bytes) data to FITS file (see below) */
|
---|
| 557 | void FitsABTWriter::Write(int col,long row,uint_2 val)
|
---|
| 558 | {
|
---|
| 559 | if(FirstTime) createtbl();
|
---|
| 560 | int sta=0;
|
---|
| 561 | if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
| 562 | printerrorwrite("unsigned short",col,row,sta);
|
---|
| 563 | }
|
---|
| 564 |
|
---|
[2169] | 565 | /*! Write long (4 Bytes) data to FITS file (see below) */
|
---|
[1657] | 566 | void FitsABTWriter::Write(int col,long row,int_4 val)
|
---|
[1654] | 567 | {
|
---|
| 568 | if(FirstTime) createtbl();
|
---|
| 569 | int sta=0;
|
---|
[1657] | 570 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 571 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 572 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 573 | printerrorwrite("long",col,row,sta);
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[2173] | 576 | /*! Write unsigned long (4 Bytes) data to FITS file (see below) */
|
---|
| 577 | void FitsABTWriter::Write(int col,long row,uint_4 val)
|
---|
| 578 | {
|
---|
| 579 | if(FirstTime) createtbl();
|
---|
| 580 | int sta=0;
|
---|
| 581 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 582 | int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
|
---|
| 583 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
| 584 | printerrorwrite("long",col,row,sta);
|
---|
| 585 | }
|
---|
| 586 |
|
---|
[2169] | 587 | /*! Write long long (8 Bytes) data to FITS file (see below) */
|
---|
| 588 | void FitsABTWriter::Write(int col,long row,int_8 val)
|
---|
| 589 | {
|
---|
| 590 | #ifdef TLONGLONG
|
---|
| 591 | if(FirstTime) createtbl();
|
---|
| 592 | int sta=0;
|
---|
| 593 | if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
|
---|
| 594 | printerrorwrite("long long",col,row,sta);
|
---|
| 595 | #else
|
---|
| 596 | throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
|
---|
| 597 | #endif
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[1654] | 600 | /*! Write float data to FITS file (see below) */
|
---|
| 601 | void FitsABTWriter::Write(int col,long row,float val)
|
---|
| 602 | {
|
---|
| 603 | if(FirstTime) createtbl();
|
---|
| 604 | int sta=0;
|
---|
[1657] | 605 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 606 | printerrorwrite("float",col,row,sta);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | /*! Write double data to FITS file (see below) */
|
---|
| 610 | void FitsABTWriter::Write(int col,long row,double val)
|
---|
| 611 | {
|
---|
| 612 | if(FirstTime) createtbl();
|
---|
| 613 | int sta=0;
|
---|
[1657] | 614 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
|
---|
[1654] | 615 | printerrorwrite("double",col,row,sta);
|
---|
| 616 | }
|
---|
| 617 |
|
---|
[1657] | 618 | //////////////////////////////////////////////////////////////
|
---|
| 619 | /*!
|
---|
[2173] | 620 | Write a vector of data to FITS file.
|
---|
[1659] | 621 | \param col : column number [0,ncol[
|
---|
| 622 | \param row : starting row number [0,nrow[
|
---|
| 623 | \param val : vector to be written
|
---|
| 624 | \return "N" = number of the next row to be written,
|
---|
| 625 | that is "N-1" is the number of the last row written.
|
---|
[1657] | 626 | */
|
---|
[2173] | 627 |
|
---|
[2170] | 628 | /*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
|
---|
| 629 | long FitsABTWriter::Write(int col,long row,TVector<uint_2>& val)
|
---|
| 630 | {
|
---|
| 631 | if(FirstTime) createtbl();
|
---|
| 632 | long nel = val.Size();
|
---|
| 633 | int sta=0;
|
---|
| 634 | if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 635 | printerrorwrite("long",col,row,sta);
|
---|
| 636 | return row+nel;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[2169] | 639 | /*! Write a vector of long (4 Bytes) data to FITS file (see below) */
|
---|
[1657] | 640 | long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
|
---|
| 641 | {
|
---|
| 642 | if(FirstTime) createtbl();
|
---|
| 643 | long nel = val.Size();
|
---|
| 644 | int sta=0;
|
---|
| 645 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
| 646 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 647 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 648 | printerrorwrite("long",col,row,sta);
|
---|
| 649 | return row+nel;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
[2169] | 652 | /*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
|
---|
| 653 | long FitsABTWriter::Write(int col,long row,TVector<int_8>& val)
|
---|
| 654 | {
|
---|
| 655 | #ifdef TLONGLONG
|
---|
| 656 | if(FirstTime) createtbl();
|
---|
| 657 | long nel = val.Size();
|
---|
| 658 | int sta=0;
|
---|
| 659 | if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 660 | printerrorwrite("long long",col,row,sta);
|
---|
| 661 | return row+nel;
|
---|
| 662 | #else
|
---|
[2170] | 663 | throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
|
---|
[2169] | 664 | #endif
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[1657] | 667 | /*! Write a vector of float data to FITS file (see below) */
|
---|
| 668 | long FitsABTWriter::Write(int col,long row,TVector<float>& val)
|
---|
| 669 | {
|
---|
| 670 | if(FirstTime) createtbl();
|
---|
| 671 | long nel = val.Size();
|
---|
| 672 | int sta=0;
|
---|
| 673 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 674 | printerrorwrite("float",col,row,sta);
|
---|
| 675 | return row+nel;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | /*! Write a vector of double data to FITS file (see below) */
|
---|
| 679 | long FitsABTWriter::Write(int col,long row,TVector<double>& val)
|
---|
| 680 | {
|
---|
| 681 | if(FirstTime) createtbl();
|
---|
| 682 | long nel = val.Size();
|
---|
| 683 | int sta=0;
|
---|
| 684 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
| 685 | printerrorwrite("double",col,row,sta);
|
---|
| 686 | return row+nel;
|
---|
| 687 | }
|
---|
| 688 |
|
---|
[1654] | 689 | /////////////////////////////////////////////////
|
---|
[1677] | 690 | void FitsABTWriter::Print(ostream& os,int lp) const
|
---|
[1673] | 691 | {
|
---|
| 692 | os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
|
---|
| 693 | <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
|
---|
| 694 | <<" NCol "<<Label.size()<<endl;
|
---|
| 695 | if(Label.size()>0 && lp>=1)
|
---|
| 696 | for(int i=0;i<(int)Label.size();i++)
|
---|
| 697 | os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
|
---|
| 698 | }
|
---|
[2453] | 699 |
|
---|
| 700 | //////////////////////////////////////////////////////////////////
|
---|
| 701 | //////////////////////////////////////////////////////////////////
|
---|
| 702 | //////////////////////////////////////////////////////////////////
|
---|
| 703 | //////////////////////////////////////////////////////////////////
|
---|
| 704 | /*!
|
---|
| 705 | \class SOPHYA::FitsImg2DWriter
|
---|
| 706 | \ingroup FitsIOServer
|
---|
| 707 | Class for writing an image into FITS file
|
---|
| 708 | */
|
---|
| 709 |
|
---|
| 710 | /*!
|
---|
| 711 | Constructor.
|
---|
| 712 | \param fname : FITS file name to be written (a new fits file is created)
|
---|
| 713 | \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
|
---|
| 714 | \param lp : debug level
|
---|
| 715 | */
|
---|
| 716 | FitsImg2DWriter::FitsImg2DWriter(string fname,int bitpix,int lp)
|
---|
| 717 | : FitsWriter(fname,lp)
|
---|
| 718 | {
|
---|
| 719 | BitPix = bitpix;
|
---|
| 720 | HduType = IMAGE_HDU;
|
---|
| 721 | FirstTime = true;
|
---|
| 722 | Naxis[0] = Naxis[1] = 0;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | /*!
|
---|
| 726 | Constructor.
|
---|
| 727 | \param cfname : FITS file name to be written (a new fits file is created)
|
---|
| 728 | \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
|
---|
| 729 | \param lp : debug level
|
---|
| 730 | */
|
---|
| 731 | FitsImg2DWriter::FitsImg2DWriter(const char* cfname,int bitpix,int lp)
|
---|
| 732 | : FitsWriter(cfname,lp)
|
---|
| 733 | {
|
---|
| 734 | BitPix = bitpix;
|
---|
| 735 | HduType = IMAGE_HDU;
|
---|
| 736 | FirstTime = true;
|
---|
| 737 | Naxis[0] = Naxis[1] = 0;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | /*!
|
---|
| 741 | Constructor.
|
---|
| 742 | \param fname : FITS file name to be written (created or updated)
|
---|
| 743 | \param update : file is created if FALSE, open for update if TRUE
|
---|
| 744 | \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
|
---|
| 745 | \param lp : debug level
|
---|
| 746 | */
|
---|
| 747 | FitsImg2DWriter::FitsImg2DWriter(string fname,bool update,int bitpix,int lp)
|
---|
| 748 | : FitsWriter(fname,update,lp)
|
---|
| 749 | {
|
---|
| 750 | BitPix = bitpix;
|
---|
| 751 | HduType = IMAGE_HDU;
|
---|
| 752 | FirstTime = true;
|
---|
| 753 | Naxis[0] = Naxis[1] = 0;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | /*!
|
---|
| 757 | Constructor.
|
---|
| 758 | \param cfname : FITS file name to be written (created or updated)
|
---|
| 759 | \param update : file is created if FALSE, open for update if TRUE
|
---|
| 760 | \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
|
---|
| 761 | \param lp : debug level
|
---|
| 762 | */
|
---|
| 763 | FitsImg2DWriter::FitsImg2DWriter(const char* cfname,bool update,int bitpix,int lp)
|
---|
| 764 | : FitsWriter(cfname,update,lp)
|
---|
| 765 | {
|
---|
| 766 | BitPix = bitpix;
|
---|
| 767 | HduType = IMAGE_HDU;
|
---|
| 768 | FirstTime = true;
|
---|
| 769 | Naxis[0] = Naxis[1] = 0;
|
---|
| 770 | }
|
---|
| 771 |
|
---|
| 772 | /*! Destructor */
|
---|
| 773 | FitsImg2DWriter::~FitsImg2DWriter()
|
---|
| 774 | {
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | /*! Create the image. Done at the first write request. */
|
---|
| 778 | void FitsImg2DWriter::createimg(void)
|
---|
| 779 | {
|
---|
| 780 | if(!FirstTime)
|
---|
| 781 | throw NotAvailableOperation("FitsImg2DWriter::createimg: already created image\n");
|
---|
| 782 | if(Naxis[0]<=0 || Naxis[1]<=0)
|
---|
| 783 | throw ParmError("FitsImg2DWriter::createimg: bad Naxis 1 or 2 value\n");
|
---|
| 784 |
|
---|
| 785 | int sta=0;
|
---|
| 786 | if(fits_create_img(FitsPtr,BitPix,2,Naxis,&sta)) {
|
---|
| 787 | printerror(sta);
|
---|
| 788 | throw NullPtrError("FitsImg2DWriter::createimg: Error creating image extension\n");
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | FirstTime = false;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | /*!
|
---|
[2455] | 795 | Write an unsigned short image to FITS file.
|
---|
| 796 | \warning TMatrix data(Naxis2,Naxis1) means Data(row,column)
|
---|
[2453] | 797 | */
|
---|
| 798 | void FitsImg2DWriter::Write(TMatrix<uint_2>& data)
|
---|
| 799 | {
|
---|
| 800 | Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
|
---|
| 801 | uint_2* arr = new uint_2[Naxis[0]];
|
---|
| 802 |
|
---|
[2455] | 803 | for(int l=0;l<Naxis[1];l++) {
|
---|
| 804 | for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
|
---|
| 805 | long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
|
---|
[2453] | 806 | fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
|
---|
| 807 | if(sta) {
|
---|
[2455] | 808 | printerrorwrite("uint_2",0,l,sta); delete [] arr;
|
---|
[2453] | 809 | throw
|
---|
| 810 | NotAvailableOperation("FitsImg2DRd::Write(TMatrix<uint_2>): Error Writing Fits file\n");
|
---|
| 811 | }
|
---|
| 812 | }
|
---|
| 813 |
|
---|
| 814 | delete [] arr;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
[2455] | 817 | /*! Write a long image to FITS file. */
|
---|
[2453] | 818 | void FitsImg2DWriter::Write(TMatrix<int_4>& data)
|
---|
| 819 | {
|
---|
| 820 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
| 821 | Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
|
---|
| 822 | int_4* arr = new int_4[Naxis[0]];
|
---|
| 823 |
|
---|
[2455] | 824 | for(int l=0;l<Naxis[1];l++) {
|
---|
| 825 | for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
|
---|
| 826 | long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
|
---|
[2453] | 827 | fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
|
---|
| 828 | if(sta) {
|
---|
[2455] | 829 | printerrorwrite("int_4",0,l,sta); delete [] arr;
|
---|
[2453] | 830 | throw
|
---|
| 831 | NotAvailableOperation("FitsImg2DRd::Write(TMatrix<int_4>): Error Writing Fits file\n");
|
---|
| 832 | }
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | delete [] arr;
|
---|
| 836 | }
|
---|
| 837 |
|
---|
[2455] | 838 | /*! Write a float image to FITS file. */
|
---|
[2453] | 839 | void FitsImg2DWriter::Write(TMatrix<float>& data)
|
---|
| 840 | {
|
---|
| 841 | Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
|
---|
| 842 | float* arr = new float[Naxis[0]];
|
---|
| 843 |
|
---|
[2455] | 844 | for(int l=0;l<Naxis[1];l++) {
|
---|
| 845 | for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
|
---|
| 846 | long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
|
---|
[2453] | 847 | fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
|
---|
| 848 | if(sta) {
|
---|
[2455] | 849 | printerrorwrite("float",0,l,sta); delete [] arr;
|
---|
[2453] | 850 | throw
|
---|
| 851 | NotAvailableOperation("FitsImg2DRd::Write(TMatrix<float>): Error Writing Fits file\n");
|
---|
| 852 | }
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | delete [] arr;
|
---|
| 856 | }
|
---|
| 857 |
|
---|
[2455] | 858 | /*! Write a double image to FITS file. */
|
---|
[2453] | 859 | void FitsImg2DWriter::Write(TMatrix<double>& data)
|
---|
| 860 | {
|
---|
| 861 | Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
|
---|
| 862 | double* arr = new double[Naxis[0]];
|
---|
| 863 |
|
---|
[2455] | 864 | for(int l=0;l<Naxis[1];l++) {
|
---|
| 865 | for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
|
---|
| 866 | long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
|
---|
[2453] | 867 | fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
|
---|
| 868 | if(sta) {
|
---|
[2455] | 869 | printerrorwrite("double",0,l,sta); delete [] arr;
|
---|
[2453] | 870 | throw
|
---|
| 871 | NotAvailableOperation("FitsImg2DRd::Write(TMatrix<double>): Error Writing Fits file\n");
|
---|
| 872 | }
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | delete [] arr;
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 | /*! Print infos. */
|
---|
| 879 | void FitsImg2DWriter::Print(ostream& os) const
|
---|
| 880 | {
|
---|
| 881 | os<<"FitsImg2DWriter::Print: FitsFN "<<FitsFN<<endl
|
---|
| 882 | <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow<<" BitPix "<<BitPix
|
---|
| 883 | <<" Naxis1 "<<Naxis[0]<<" Naxis2 "<<Naxis[1]<<endl;
|
---|
| 884 | }
|
---|