source: Sophya/trunk/SophyaExt/FitsIOServer/fabtwriter.cc@ 2456

Last change on this file since 2456 was 2455, checked in by cmv, 22 years ago

modifs mineures cmv 17/11/03

File size: 26.6 KB
RevLine 
[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). */
19FitsWriter::FitsWriter(string fname,int lp)
20{
21 cr_or_upd_fits(fname.c_str(),false,lp);
22}
23
24/*! Constructor (DO NOT USE). */
25FitsWriter::FitsWriter(const char* cfname,int lp)
26{
27 cr_or_upd_fits(cfname,false,lp);
28}
29
30/*! Constructor (DO NOT USE). */
31FitsWriter::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). */
37FitsWriter::FitsWriter(const char* cfname,bool update,int lp)
38{
39 cr_or_upd_fits(cfname,update,lp);
40}
41
42/*! See FitsWriter() */
43void 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 */
87FitsWriter::~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 */
96void 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 */
105void 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 */
116void 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 */
127void 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
137void 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
171void 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
181void 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*/
303FitsABTWriter::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]319FitsABTWriter::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*/
336FitsABTWriter::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*/
353FitsABTWriter::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 */
364FitsABTWriter::~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
[1660]377 TUSHORT TULONG. That parameter is only use in case
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]382int 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");
409 else
410 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
[1654]411 TForm.push_back(str);
[2174]412 } else if(tformauto && HduType==ASCII_TBL) {
413 char str[8];
414 if(datatype==TBYTE) strcpy(str,"I5");
415 else if(datatype==TSHORT) strcpy(str,"I7");
416 else if(datatype==TLONG) strcpy(str,"I11");
417#ifdef TINT32BIT
418 else if(datatype==TINT32BIT) strcpy(str,"I11");
419#endif
420#ifdef TLONGLONG
421 else if(datatype==TLONGLONG) strcpy(str,"I21");
422#endif
423 else if(datatype==TFLOAT) strcpy(str,"E29.20");
424 else if(datatype==TDOUBLE) strcpy(str,"E29.20");
425 else if(datatype==TUSHORT) strcpy(str,"I7");
426 else if(datatype==TULONG) strcpy(str,"I11");
427 else
428 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
429 TForm.push_back(str);
[1660]430 } else {
431 if(tform) TForm.push_back(tform); else TForm.push_back("");
432 datatype = 0;
433 }
434 Label.push_back(label);
435 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
[1654]436
437 int n = (int) Label.size() -1;
438
439 if(DbgLevel)
440 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
441 <<" TForm="<<TForm[n]
[1660]442 <<" TUnit="<<TUnit[n]
443 <<" datatype="<<datatype<<endl;
[1654]444
445 return n;
446}
447
448/*! Create the table. Done at the first write request. */
449void FitsABTWriter::createtbl(void)
450{
451 if(!FirstTime) return;
452
453 int tfields = Label.size();
454 if(tfields<=0)
455 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
456
457 long nrows = 0;
458 char *extname = NULL;
459 char **ttype = (char **) malloc(tfields*sizeof(char *));
460 char **tform = (char **) malloc(tfields*sizeof(char *));
461 char **tunit = (char **) malloc(tfields*sizeof(char *));
462
463 if(ExtName.size()>0) {
464 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
465 strcpy(extname,ExtName.c_str());
466 }
[1664]467 int i;
468 for(i=0;i<tfields;i++) {
[1654]469 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
470 strcpy(ttype[i],Label[i].c_str());
471 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
472 strcpy(tform[i],TForm[i].c_str());
473 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
474 strcpy(tunit[i],TUnit[i].c_str());
475 }
476
477 // append a new empty binary/ascii table onto the FITS file
478 int sta=0;
479 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
480 printerror(sta);
481 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
482 }
483
484 // menage
485 if(extname) delete [] extname;
[1664]486 for(i=0;i<tfields;i++) {
[1654]487 if(ttype[i]) delete [] ttype[i];
488 if(tform[i]) delete [] tform[i];
489 if(tunit[i]) delete [] tunit[i];
490 }
491 if(ttype) delete [] ttype;
492 if(tform) delete [] tform;
493 if(tunit) delete [] tunit;
494
495 FirstTime = false;
496}
497
498//////////////////////////////////////////////////////////////
499/*!
[2173]500 Write a data to FITS file.
[1659]501 \param col : column number [0,ncol[
502 \param row : row number [0,nrow[
503 \param val : value to be written
504 \warning that routine write a SHORT value into column "col"
[1654]505 which could have been defined with an other type.
506 Cast is performed by the cfitsio package.
[1659]507 \verbatim
[1654]508 WARNING: suppose that the column has be defined to be TSHORT
[1660]509 and suppose that you wanted to write a double value "val"
510 - If you write dummy.Write(col,row,(short)(val))
511 the Write(int,long,short) routine is called and
[1654]512 the cast is performed by the C++ language.
513 - If you write dummy.Write(col,row,val) where val is double
[1660]514 the Write(int,long,double) routine is called and
[1654]515 the cast is performed by the cfistio package.
516 \endverbatim
517*/
[2173]518
[2174]519/*! Write unsigned char (1 Byte) data to FITS file (see below) */
520void FitsABTWriter::Write(int col,long row,uint_1 val)
[2173]521{
522 if(FirstTime) createtbl();
523 int sta=0;
524 if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,1,1,&val,&sta))
[2174]525 printerrorwrite("unsigned char",col,row,sta);
[2173]526}
527
528/*! Write short (2 Bytes) data to FITS file (see below) */
[2170]529void FitsABTWriter::Write(int col,long row,int_2 val)
[1654]530{
531 if(FirstTime) createtbl();
532 int sta=0;
[1657]533 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
[1654]534 printerrorwrite("short",col,row,sta);
535}
536
[2170]537/*! Write unsigned short (2 Bytes) data to FITS file (see below) */
538void FitsABTWriter::Write(int col,long row,uint_2 val)
539{
540 if(FirstTime) createtbl();
541 int sta=0;
542 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
543 printerrorwrite("unsigned short",col,row,sta);
544}
545
[2169]546/*! Write long (4 Bytes) data to FITS file (see below) */
[1657]547void FitsABTWriter::Write(int col,long row,int_4 val)
[1654]548{
549 if(FirstTime) createtbl();
550 int sta=0;
[1657]551 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
552 int T = (sizeof(long)==4) ? TLONG: TINT;
553 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
[1654]554 printerrorwrite("long",col,row,sta);
555}
556
[2173]557/*! Write unsigned long (4 Bytes) data to FITS file (see below) */
558void FitsABTWriter::Write(int col,long row,uint_4 val)
559{
560 if(FirstTime) createtbl();
561 int sta=0;
562 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
563 int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
564 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
565 printerrorwrite("long",col,row,sta);
566}
567
[2169]568/*! Write long long (8 Bytes) data to FITS file (see below) */
569void FitsABTWriter::Write(int col,long row,int_8 val)
570{
571#ifdef TLONGLONG
572 if(FirstTime) createtbl();
573 int sta=0;
574 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
575 printerrorwrite("long long",col,row,sta);
576#else
577 throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
578#endif
579}
580
[1654]581/*! Write float data to FITS file (see below) */
582void FitsABTWriter::Write(int col,long row,float val)
583{
584 if(FirstTime) createtbl();
585 int sta=0;
[1657]586 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
[1654]587 printerrorwrite("float",col,row,sta);
588}
589
590/*! Write double data to FITS file (see below) */
591void FitsABTWriter::Write(int col,long row,double val)
592{
593 if(FirstTime) createtbl();
594 int sta=0;
[1657]595 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
[1654]596 printerrorwrite("double",col,row,sta);
597}
598
[1657]599//////////////////////////////////////////////////////////////
600/*!
[2173]601 Write a vector of data to FITS file.
[1659]602 \param col : column number [0,ncol[
603 \param row : starting row number [0,nrow[
604 \param val : vector to be written
605 \return "N" = number of the next row to be written,
606 that is "N-1" is the number of the last row written.
[1657]607*/
[2173]608
[2170]609/*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
610long FitsABTWriter::Write(int col,long row,TVector<uint_2>& val)
611{
612 if(FirstTime) createtbl();
613 long nel = val.Size();
614 int sta=0;
615 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
616 printerrorwrite("long",col,row,sta);
617 return row+nel;
618}
619
[2169]620/*! Write a vector of long (4 Bytes) data to FITS file (see below) */
[1657]621long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
622{
623 if(FirstTime) createtbl();
624 long nel = val.Size();
625 int sta=0;
626 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
627 int T = (sizeof(long)==4) ? TLONG: TINT;
628 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
629 printerrorwrite("long",col,row,sta);
630 return row+nel;
631}
632
[2169]633/*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
634long FitsABTWriter::Write(int col,long row,TVector<int_8>& val)
635{
636#ifdef TLONGLONG
637 if(FirstTime) createtbl();
638 long nel = val.Size();
639 int sta=0;
640 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
641 printerrorwrite("long long",col,row,sta);
642 return row+nel;
643#else
[2170]644 throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
[2169]645#endif
646}
647
[1657]648/*! Write a vector of float data to FITS file (see below) */
649long FitsABTWriter::Write(int col,long row,TVector<float>& val)
650{
651 if(FirstTime) createtbl();
652 long nel = val.Size();
653 int sta=0;
654 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
655 printerrorwrite("float",col,row,sta);
656 return row+nel;
657}
658
659/*! Write a vector of double data to FITS file (see below) */
660long FitsABTWriter::Write(int col,long row,TVector<double>& val)
661{
662 if(FirstTime) createtbl();
663 long nel = val.Size();
664 int sta=0;
665 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
666 printerrorwrite("double",col,row,sta);
667 return row+nel;
668}
669
[1654]670/////////////////////////////////////////////////
[1677]671void FitsABTWriter::Print(ostream& os,int lp) const
[1673]672{
673os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
674 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
675 <<" NCol "<<Label.size()<<endl;
676if(Label.size()>0 && lp>=1)
677 for(int i=0;i<(int)Label.size();i++)
678 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
679}
[2453]680
681//////////////////////////////////////////////////////////////////
682//////////////////////////////////////////////////////////////////
683//////////////////////////////////////////////////////////////////
684//////////////////////////////////////////////////////////////////
685/*!
686 \class SOPHYA::FitsImg2DWriter
687 \ingroup FitsIOServer
688 Class for writing an image into FITS file
689*/
690
691/*!
692 Constructor.
693 \param fname : FITS file name to be written (a new fits file is created)
694 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
695 \param lp : debug level
696*/
697FitsImg2DWriter::FitsImg2DWriter(string fname,int bitpix,int lp)
698: FitsWriter(fname,lp)
699{
700 BitPix = bitpix;
701 HduType = IMAGE_HDU;
702 FirstTime = true;
703 Naxis[0] = Naxis[1] = 0;
704}
705
706/*!
707 Constructor.
708 \param cfname : FITS file name to be written (a new fits file is created)
709 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
710 \param lp : debug level
711*/
712FitsImg2DWriter::FitsImg2DWriter(const char* cfname,int bitpix,int lp)
713: FitsWriter(cfname,lp)
714{
715 BitPix = bitpix;
716 HduType = IMAGE_HDU;
717 FirstTime = true;
718 Naxis[0] = Naxis[1] = 0;
719}
720
721/*!
722 Constructor.
723 \param fname : FITS file name to be written (created or updated)
724 \param update : file is created if FALSE, open for update if TRUE
725 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
726 \param lp : debug level
727*/
728FitsImg2DWriter::FitsImg2DWriter(string fname,bool update,int bitpix,int lp)
729: FitsWriter(fname,update,lp)
730{
731 BitPix = bitpix;
732 HduType = IMAGE_HDU;
733 FirstTime = true;
734 Naxis[0] = Naxis[1] = 0;
735}
736
737/*!
738 Constructor.
739 \param cfname : FITS file name to be written (created or updated)
740 \param update : file is created if FALSE, open for update if TRUE
741 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
742 \param lp : debug level
743*/
744FitsImg2DWriter::FitsImg2DWriter(const char* cfname,bool update,int bitpix,int lp)
745: FitsWriter(cfname,update,lp)
746{
747 BitPix = bitpix;
748 HduType = IMAGE_HDU;
749 FirstTime = true;
750 Naxis[0] = Naxis[1] = 0;
751}
752
753/*! Destructor */
754FitsImg2DWriter::~FitsImg2DWriter()
755{
756}
757
758/*! Create the image. Done at the first write request. */
759void FitsImg2DWriter::createimg(void)
760{
761 if(!FirstTime)
762 throw NotAvailableOperation("FitsImg2DWriter::createimg: already created image\n");
763 if(Naxis[0]<=0 || Naxis[1]<=0)
764 throw ParmError("FitsImg2DWriter::createimg: bad Naxis 1 or 2 value\n");
765
766 int sta=0;
767 if(fits_create_img(FitsPtr,BitPix,2,Naxis,&sta)) {
768 printerror(sta);
769 throw NullPtrError("FitsImg2DWriter::createimg: Error creating image extension\n");
770 }
771
772 FirstTime = false;
773}
774
775/*!
[2455]776 Write an unsigned short image to FITS file.
777 \warning TMatrix data(Naxis2,Naxis1) means Data(row,column)
[2453]778*/
779void FitsImg2DWriter::Write(TMatrix<uint_2>& data)
780{
781 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
782 uint_2* arr = new uint_2[Naxis[0]];
783
[2455]784 for(int l=0;l<Naxis[1];l++) {
785 for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
786 long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
[2453]787 fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
788 if(sta) {
[2455]789 printerrorwrite("uint_2",0,l,sta); delete [] arr;
[2453]790 throw
791 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<uint_2>): Error Writing Fits file\n");
792 }
793 }
794
795 delete [] arr;
796}
797
[2455]798/*! Write a long image to FITS file. */
[2453]799void FitsImg2DWriter::Write(TMatrix<int_4>& data)
800{
801 int T = (sizeof(long)==4) ? TLONG: TINT;
802 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
803 int_4* arr = new int_4[Naxis[0]];
804
[2455]805 for(int l=0;l<Naxis[1];l++) {
806 for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
807 long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
[2453]808 fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
809 if(sta) {
[2455]810 printerrorwrite("int_4",0,l,sta); delete [] arr;
[2453]811 throw
812 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<int_4>): Error Writing Fits file\n");
813 }
814 }
815
816 delete [] arr;
817}
818
[2455]819/*! Write a float image to FITS file. */
[2453]820void FitsImg2DWriter::Write(TMatrix<float>& data)
821{
822 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
823 float* arr = new float[Naxis[0]];
824
[2455]825 for(int l=0;l<Naxis[1];l++) {
826 for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
827 long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
[2453]828 fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
829 if(sta) {
[2455]830 printerrorwrite("float",0,l,sta); delete [] arr;
[2453]831 throw
832 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<float>): Error Writing Fits file\n");
833 }
834 }
835
836 delete [] arr;
837}
838
[2455]839/*! Write a double image to FITS file. */
[2453]840void FitsImg2DWriter::Write(TMatrix<double>& data)
841{
842 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
843 double* arr = new double[Naxis[0]];
844
[2455]845 for(int l=0;l<Naxis[1];l++) {
846 for(int c=0;c<Naxis[0];c++) arr[c] = data(l,c);
847 long deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
[2453]848 fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
849 if(sta) {
[2455]850 printerrorwrite("double",0,l,sta); delete [] arr;
[2453]851 throw
852 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<double>): Error Writing Fits file\n");
853 }
854 }
855
856 delete [] arr;
857}
858
859/*! Print infos. */
860void FitsImg2DWriter::Print(ostream& os) const
861{
862os<<"FitsImg2DWriter::Print: FitsFN "<<FitsFN<<endl
863 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow<<" BitPix "<<BitPix
864 <<" Naxis1 "<<Naxis[0]<<" Naxis2 "<<Naxis[1]<<endl;
865}
Note: See TracBrowser for help on using the repository browser.