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