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

Last change on this file since 3087 was 2789, checked in by cmv, 20 years ago

ajouts de commentaires cmv 31/05/2005

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