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

Last change on this file since 3660 was 3660, checked in by cmv, 16 years ago

intro FABTColRead1F, cmv 19/10/2009

File size: 34.3 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 LongLongKey.resize(0);
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
81 //LONGLONG naxes[1] = {0};
82 //if(fits_create_imgll(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
83 // printerror(sta);
84 // throw NullPtrError("FitsWriter::cr_or_upd_fits: Error creating Primary extension\n");
85 //}
86}
87
88/*! Destructor */
89FitsWriter::~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 */
98void 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 */
107void FitsWriter::WriteKey(const char *keyname,double val,const 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 */
118void FitsWriter::WriteKey(const char *keyname,long val,const 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
128/*! Write a long long value into Fits Header */
129void FitsWriter::WriteKey(const char *keyname,LONGLONG val,const 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
139/*! Write a string value into Fits Header */
140void FitsWriter::WriteKey(const char *keyname,string val,const 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
150void FitsWriter::writekeys(void)
151// Ecriture effective des clefs
152{
153 if(FitsPtr==NULL) return;
154 int sta=0;
155 if(DoubleKey.size()>0)
156 for(unsigned long i=0;i<DoubleKey.size();i++) {
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)
164 for(unsigned long i=0;i<LongKey.size();i++) {
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 }
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 }
179 if(StringKey.size()>0)
180 for(unsigned long i=0;i<StringKey.size();i++) {
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);
189 LongLongKey.resize(0);
190 StringKey.resize(0);
191}
192
193void FitsWriter::printerrorwrite(const char* type,int col,LONGLONG row,int sta)
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"
199 ,type,col,(long)row,sta);
200 throw NotAvailableOperation(str);
201}
202
203void 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/*!
216 \class SOPHYA::FitsABTWriter
217 \ingroup FitsIOServer
218 Class for writing a FITS ASCII or BINARY table
219 \verbatim
220 //-----------------------------------------------------------
221 OPENING A NEW FILE AND WRITING INTO
222 -- Exemple 1: Writing element by element
223 FitsABTWriter fbtw(fitswrit,BINARY_TBL);
224 fbtw.SetExtName("MY_OWN_EXTENSION");
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
230 fbtw.SetDebug(3);
231 for(LONGLONG i=0;i<1000;i++) {
232 double x = i;
233 fbtw.Write(c1,i,1000.*x); // if overflow, managed by cfitsio
234 // Write(int,long,double) is called
235 fbtw.Write(c2,i,(short)(1000.*x));
236 // if overflow, managed by language
237 // Write(int,long,short) is called
238 fbtw.Write(c3,i,x);
239 fbtw.Write(c4,i,x);
240 fbtw.Write(c5,i,x);
241 }
242 cout<<"Number of Overflows when writing: "
243 <<fbtw.GetNOverFlow()<<endl;
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);
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) = ...;
256 fbtw.Write(1,i,datal);
257 fbtw.Write(2,i,dataf);
258 fbtw.Write(3,i,datad);
259 }
260 \endverbatim
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");
274 fbtw.WriteKey("MYKEYLL",(LONGLONG)123456789,"my LONGLONG key");
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");
289 fbtw2.WriteKey("MYKEYLL",(LONGLONG)-123456789,"my new LONGLONG key");
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;
300 for(LONGLONG i=0;i<NNN;i++) {
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
318*/
319
320//////////////////////////////////////////////////////////////
321/*!
322 Constructor.
323 \param fname : FITS file name to be written (a new fits file is created)
324 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
325 \param lp : debug level
326*/
327FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
328: FitsWriter(fname,lp)
329{
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");
335}
336
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*/
343FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
344: FitsWriter(cfname,lp)
345{
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");
351}
352
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*/
360FitsABTWriter::FitsABTWriter(string fname,bool update,int hdutype,int lp)
361: FitsWriter(fname,update,lp)
362{
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");
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*/
377FitsABTWriter::FitsABTWriter(const char* cfname,bool update,int hdutype,int lp)
378: FitsWriter(cfname,update,lp)
379{
380 FirstTime = true;
381 HduType = hdutype;
382 if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
383 throw
384 TypeMismatchExc("FitsABTWriter::FitsABTWriter: Only BINARY or ASCII table permitted\n");
385}
386
387/*! Destructor */
388FitsABTWriter::~FitsABTWriter()
389{
390 // On cree la table avant de fermer le fichier si pas deja fait
391 if(FirstTime) createtbl();
392}
393
394//////////////////////////////////////////////////////////////
395/*!
396 Add a new column to the FITS table
397 \param label : column label
398 \param tform : fits tform definition ("1I","1J","1E","1J",...)
399 (can be automatically set as "datatype"
400 if BINARY_TBL and tform="" or tform=NULL)
401 \param tunit : fits tunit definition (optional)
402 \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TLONGLONG TFLOAT TDOUBLE
403 TUSHORT TULONG TSBYTE. That parameter is only use in case
404 of a BINARY_TBL table when tform is not defined).
405 \return The number of the new added column in the table.
406 \warning col = [0,ncol-1]
407*/
408int FitsABTWriter::addcol(const char* label,const char* tform
409 ,const char* tunit,int datatype)
410{
411 if(!FirstTime)
412 throw AllocationError("FitsABTWriter::addcol: table already created\n");
413
414 // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
415 bool tformauto = false;
416 if(HduType==BINARY_TBL || HduType==ASCII_TBL) {
417 if(tform==NULL) tformauto = true;
418 else if(strlen(tform)<=0) tformauto = true;
419 }
420 if(tformauto && HduType==BINARY_TBL) {
421 char str[8];
422 if(datatype==TBYTE) strcpy(str,"1B");
423 else if(datatype==TSHORT) strcpy(str,"1I");
424 else if(datatype==TLONG) strcpy(str,"1J");
425#ifdef TINT32BIT
426 else if(datatype==TINT32BIT) strcpy(str,"1J");
427#endif
428#ifdef TLONGLONG
429 else if(datatype==TLONGLONG) strcpy(str,"1K");
430#endif
431 else if(datatype==TFLOAT) strcpy(str,"1E");
432 else if(datatype==TDOUBLE) strcpy(str,"1D");
433 else if(datatype==TUSHORT) strcpy(str,"1U");
434 else if(datatype==TULONG) strcpy(str,"1V");
435#ifdef TSBYTE
436 else if(datatype==TSBYTE) strcpy(str,"1S");
437#endif
438 else
439 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
440 TForm.push_back(str);
441 } else if(tformauto && HduType==ASCII_TBL) {
442 char str[8];
443 if(datatype==TBYTE) strcpy(str,"I5");
444 else if(datatype==TSHORT) strcpy(str,"I7");
445 else if(datatype==TLONG) strcpy(str,"I11");
446#ifdef TINT32BIT
447 else if(datatype==TINT32BIT) strcpy(str,"I11");
448#endif
449#ifdef TLONGLONG
450 else if(datatype==TLONGLONG) strcpy(str,"I21");
451#endif
452 else if(datatype==TFLOAT) strcpy(str,"E29.20");
453 else if(datatype==TDOUBLE) strcpy(str,"E29.20");
454 else if(datatype==TUSHORT) strcpy(str,"I7");
455 else if(datatype==TULONG) strcpy(str,"I11");
456#ifdef TSBYTE
457 else if(datatype==TSBYTE) strcpy(str,"I5");
458#endif
459 else
460 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
461 TForm.push_back(str);
462 } else {
463 if(tform) TForm.push_back(tform); else TForm.push_back("");
464 datatype = 0;
465 }
466 Label.push_back(label);
467 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
468
469 int n = (int) Label.size() -1;
470
471 if(DbgLevel)
472 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
473 <<" TForm="<<TForm[n]
474 <<" TUnit="<<TUnit[n]
475 <<" datatype="<<datatype<<endl;
476
477 return n;
478}
479
480/*! Create the table. Done at the first write request. */
481void FitsABTWriter::createtbl(void)
482{
483 if(!FirstTime) return;
484
485 int tfields = Label.size();
486 if(tfields<=0)
487 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
488
489 LONGLONG nrows = 0;
490 char *extname = NULL;
491 char **ttype = (char **) malloc(tfields*sizeof(char *));
492 char **tform = (char **) malloc(tfields*sizeof(char *));
493 char **tunit = (char **) malloc(tfields*sizeof(char *));
494
495 if(ExtName.size()>0) {
496 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
497 strcpy(extname,ExtName.c_str());
498 }
499 for(int i=0;i<tfields;i++) {
500 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
501 strcpy(ttype[i],Label[i].c_str());
502 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
503 strcpy(tform[i],TForm[i].c_str());
504 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
505 strcpy(tunit[i],TUnit[i].c_str());
506 }
507
508 // append a new empty binary/ascii table onto the FITS file
509 int sta=0;
510 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
511 printerror(sta);
512 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
513 }
514
515 // menage
516 if(extname) delete [] extname;
517 for(int i=0;i<tfields;i++) {
518 if(ttype[i]) delete [] ttype[i];
519 if(tform[i]) delete [] tform[i];
520 if(tunit[i]) delete [] tunit[i];
521 }
522 if(ttype) delete [] ttype;
523 if(tform) delete [] tform;
524 if(tunit) delete [] tunit;
525
526 FirstTime = false;
527}
528
529//////////////////////////////////////////////////////////////
530/*!
531 Write a data to FITS file.
532 \param col : column number [0,ncol[
533 \param row : row number [0,nrow[
534 \param val : value to be written
535 \warning that routine write a SHORT value into column "col"
536 which could have been defined with an other type.
537 Cast is performed by the cfitsio package.
538 \verbatim
539 WARNING: suppose that the column has be defined to be TSHORT
540 and suppose that you wanted to write a double value "val"
541 - If you write dummy.Write(col,row,(short)(val))
542 the Write(int,long,short) routine is called and
543 the cast is performed by the C++ language.
544 - If you write dummy.Write(col,row,val) where val is double
545 the Write(int,long,double) routine is called and
546 the cast is performed by the cfistio package.
547 \endverbatim
548*/
549
550/*! Write signed char (1 Byte) data to FITS file (see below) */
551void FitsABTWriter::Write(int col,LONGLONG row,int_1 val)
552{
553#ifdef TSBYTE
554 if(FirstTime) createtbl();
555 int sta=0;
556 if(fits_write_col(FitsPtr,TSBYTE,col+1,row+1,1,1,&val,&sta))
557 printerrorwrite("signed char",col,row,sta);
558#else
559 throw PException("FitsABTWriter::Write(..,int_1) Not in that cfitsio version");
560#endif
561}
562
563/*! Write unsigned char (1 Byte) data to FITS file (see below) */
564void FitsABTWriter::Write(int col,LONGLONG row,uint_1 val)
565{
566 if(FirstTime) createtbl();
567 int sta=0;
568 if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,1,1,&val,&sta))
569 printerrorwrite("unsigned char",col,row,sta);
570}
571
572/*! Write short (2 Bytes) data to FITS file (see below) */
573void FitsABTWriter::Write(int col,LONGLONG row,int_2 val)
574{
575 if(FirstTime) createtbl();
576 int sta=0;
577 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
578 printerrorwrite("short",col,row,sta);
579}
580
581/*! Write unsigned short (2 Bytes) data to FITS file (see below) */
582void FitsABTWriter::Write(int col,LONGLONG row,uint_2 val)
583{
584 if(FirstTime) createtbl();
585 int sta=0;
586 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
587 printerrorwrite("unsigned short",col,row,sta);
588}
589
590/*! Write long (4 Bytes) data to FITS file (see below) */
591void FitsABTWriter::Write(int col,LONGLONG row,int_4 val)
592{
593 if(FirstTime) createtbl();
594 int sta=0;
595 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
596 int T = (sizeof(long)==4) ? TLONG: TINT;
597 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
598 printerrorwrite("long",col,row,sta);
599}
600
601/*! Write unsigned long (4 Bytes) data to FITS file (see below) */
602void FitsABTWriter::Write(int col,LONGLONG row,uint_4 val)
603{
604 if(FirstTime) createtbl();
605 int sta=0;
606 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
607 int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
608 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
609 printerrorwrite("long",col,row,sta);
610}
611
612/*! Write long long (8 Bytes) data to FITS file (see below) */
613void FitsABTWriter::Write(int col,LONGLONG row,int_8 val)
614{
615#ifdef TLONGLONG
616 if(FirstTime) createtbl();
617 int sta=0;
618 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
619 printerrorwrite("long long",col,row,sta);
620#else
621 throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
622#endif
623}
624
625/*! Write float data to FITS file (see below) */
626void FitsABTWriter::Write(int col,LONGLONG row,float val)
627{
628 if(FirstTime) createtbl();
629 int sta=0;
630 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
631 printerrorwrite("float",col,row,sta);
632}
633
634/*! Write double data to FITS file (see below) */
635void FitsABTWriter::Write(int col,LONGLONG row,double val)
636{
637 if(FirstTime) createtbl();
638 int sta=0;
639 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
640 printerrorwrite("double",col,row,sta);
641}
642
643//////////////////////////////////////////////////////////////
644/*!
645 Write a vector of data to FITS file.
646 \param col : column number [0,ncol[
647 \param row : starting row number [0,nrow[
648 \param val : vector to be written
649 \return "N" = number of the next row to be written,
650 that is "N-1" is the number of the last row written.
651*/
652
653/*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
654LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<uint_2>& val)
655{
656 if(FirstTime) createtbl();
657 LONGLONG nel = val.Size();
658 int sta=0;
659 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
660 printerrorwrite("long",col,row,sta);
661 return row+nel;
662}
663
664/*! Write a vector of long (4 Bytes) data to FITS file (see below) */
665LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<int_4>& val)
666{
667 if(FirstTime) createtbl();
668 LONGLONG nel = val.Size();
669 int sta=0;
670 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
671 int T = (sizeof(long)==4) ? TLONG: TINT;
672 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
673 printerrorwrite("long",col,row,sta);
674 return row+nel;
675}
676
677/*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
678LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<int_8>& val)
679{
680#ifdef TLONGLONG
681 if(FirstTime) createtbl();
682 LONGLONG nel = val.Size();
683 int sta=0;
684 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
685 printerrorwrite("long long",col,row,sta);
686 return row+nel;
687#else
688 throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
689#endif
690}
691
692/*! Write a vector of float data to FITS file (see below) */
693LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<float>& val)
694{
695 if(FirstTime) createtbl();
696 LONGLONG nel = val.Size();
697 int sta=0;
698 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
699 printerrorwrite("float",col,row,sta);
700 return row+nel;
701}
702
703/*! Write a vector of double data to FITS file (see below) */
704LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<double>& val)
705{
706 if(FirstTime) createtbl();
707 LONGLONG nel = val.Size();
708 int sta=0;
709 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
710 printerrorwrite("double",col,row,sta);
711 return row+nel;
712}
713
714/////////////////////////////////////////////////
715void FitsABTWriter::Print(ostream& os,int lp) const
716{
717os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
718 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
719 <<" NCol "<<Label.size()<<endl;
720if(Label.size()>0 && lp>=1)
721 for(int i=0;i<(int)Label.size();i++)
722 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
723}
724
725//////////////////////////////////////////////////////////////////
726//////////////////////////////////////////////////////////////////
727//////////////////////////////////////////////////////////////////
728//////////////////////////////////////////////////////////////////
729/*!
730 \class SOPHYA::FitsImg2DWriter
731 \ingroup FitsIOServer
732 Class for writing an image into FITS file
733*/
734
735/*!
736 Constructor.
737 \param fname : FITS file name to be written (a new fits file is created)
738 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
739 \param lp : debug level
740*/
741FitsImg2DWriter::FitsImg2DWriter(string fname,int bitpix,int lp)
742: FitsWriter(fname,lp)
743{
744 BitPix = bitpix;
745 HduType = IMAGE_HDU;
746 FirstTime = true;
747 Naxis[0] = Naxis[1] = 0;
748}
749
750/*!
751 Constructor.
752 \param cfname : FITS file name to be written (a new fits file is created)
753 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
754 \param lp : debug level
755*/
756FitsImg2DWriter::FitsImg2DWriter(const char* cfname,int bitpix,int lp)
757: FitsWriter(cfname,lp)
758{
759 BitPix = bitpix;
760 HduType = IMAGE_HDU;
761 FirstTime = true;
762 Naxis[0] = Naxis[1] = 0;
763}
764
765/*!
766 Constructor.
767 \param fname : FITS file name to be written (created or updated)
768 \param update : file is created if FALSE, open for update if TRUE
769 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
770 \param lp : debug level
771*/
772FitsImg2DWriter::FitsImg2DWriter(string fname,bool update,int bitpix,int lp)
773: FitsWriter(fname,update,lp)
774{
775 BitPix = bitpix;
776 HduType = IMAGE_HDU;
777 FirstTime = true;
778 Naxis[0] = Naxis[1] = 0;
779}
780
781/*!
782 Constructor.
783 \param cfname : FITS file name to be written (created or updated)
784 \param update : file is created if FALSE, open for update if TRUE
785 \param bitpix : image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
786 \param lp : debug level
787*/
788FitsImg2DWriter::FitsImg2DWriter(const char* cfname,bool update,int bitpix,int lp)
789: FitsWriter(cfname,update,lp)
790{
791 BitPix = bitpix;
792 HduType = IMAGE_HDU;
793 FirstTime = true;
794 Naxis[0] = Naxis[1] = 0;
795}
796
797/*! Destructor */
798FitsImg2DWriter::~FitsImg2DWriter()
799{
800}
801
802/*! Create the image. Done at the first write request. */
803void FitsImg2DWriter::createimg(void)
804{
805 if(!FirstTime)
806 throw NotAvailableOperation("FitsImg2DWriter::createimg: already created image\n");
807 if(Naxis[0]<=0 || Naxis[1]<=0)
808 throw ParmError("FitsImg2DWriter::createimg: bad Naxis 1 or 2 value\n");
809
810 int sta=0;
811 if(fits_create_imgll(FitsPtr,BitPix,2,Naxis,&sta)) {
812 printerror(sta);
813 throw NullPtrError("FitsImg2DWriter::createimg: Error creating image extension\n");
814 }
815
816 FirstTime = false;
817}
818
819/*!
820 Write an unsigned short image to FITS file.
821 \warning TMatrix data(Naxis2,Naxis1) means Data(row,column)
822*/
823void FitsImg2DWriter::Write(TMatrix<uint_2>& data)
824{
825 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
826 uint_2* arr = new uint_2[Naxis[0]];
827
828 for(LONGLONG l=0;l<Naxis[1];l++) {
829 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
830 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
831 fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
832 if(sta) {
833 printerrorwrite("uint_2",0,l,sta); delete [] arr;
834 throw
835 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<uint_2>): Error Writing Fits file\n");
836 }
837 }
838
839 delete [] arr;
840}
841
842/*! Write a long image to FITS file. */
843void FitsImg2DWriter::Write(TMatrix<int_4>& data)
844{
845 int T = (sizeof(long)==4) ? TLONG: TINT;
846 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
847 int_4* arr = new int_4[Naxis[0]];
848
849 for(LONGLONG l=0;l<Naxis[1];l++) {
850 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
851 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
852 fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
853 if(sta) {
854 printerrorwrite("int_4",0,l,sta); delete [] arr;
855 throw
856 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<int_4>): Error Writing Fits file\n");
857 }
858 }
859
860 delete [] arr;
861}
862
863/*! Write a float image to FITS file. */
864void FitsImg2DWriter::Write(TMatrix<float>& data)
865{
866 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
867 float* arr = new float[Naxis[0]];
868
869 for(LONGLONG l=0;l<Naxis[1];l++) {
870 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
871 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
872 fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
873 if(sta) {
874 printerrorwrite("float",0,l,sta); delete [] arr;
875 throw
876 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<float>): Error Writing Fits file\n");
877 }
878 }
879
880 delete [] arr;
881}
882
883/*! Write a double image to FITS file. */
884void FitsImg2DWriter::Write(TMatrix<double>& data)
885{
886 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
887 double* arr = new double[Naxis[0]];
888
889 for(LONGLONG l=0;l<Naxis[1];l++) {
890 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
891 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
892 fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
893 if(sta) {
894 printerrorwrite("double",0,l,sta); delete [] arr;
895 throw
896 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<double>): Error Writing Fits file\n");
897 }
898 }
899
900 delete [] arr;
901}
902
903/*! Print infos. */
904void FitsImg2DWriter::Print(ostream& os) const
905{
906os<<"FitsImg2DWriter::Print: FitsFN "<<FitsFN<<endl
907 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow<<" BitPix "<<BitPix
908 <<" Naxis1 "<<Naxis[0]<<" Naxis2 "<<Naxis[1]<<endl;
909}
910
911
912//////////////////////////////////////////////////////////////////
913//////////////////////////////////////////////////////////////////
914//////////////////////////////////////////////////////////////////
915//////////////////////////////////////////////////////////////////
916/*!
917 \class SOPHYA::FitsImg3DWriter
918 \ingroup FitsIOServer
919 Class for writing an 3D image into FITS file
920*/
921
922/*!
923 Constructor.
924 \param fname : FITS file name to be written (a new fits file is created)
925 \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
926 \param lp : debug level
927*/
928FitsImg3DWriter::FitsImg3DWriter(string fname,int bitpix,int lp)
929: FitsWriter(fname,lp)
930{
931 BitPix = bitpix;
932 HduType = IMAGE_HDU;
933 FirstTime = true;
934 Naxis[0] = Naxis[1] = Naxis[2] = 0;
935}
936
937/*!
938 Constructor.
939 \param cfname : FITS file name to be written (a new fits file is created)
940 \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
941 \param lp : debug level
942*/
943FitsImg3DWriter::FitsImg3DWriter(const char* cfname,int bitpix,int lp)
944: FitsWriter(cfname,lp)
945{
946 BitPix = bitpix;
947 HduType = IMAGE_HDU;
948 FirstTime = true;
949 Naxis[0] = Naxis[1] = Naxis[2] = 0;
950}
951
952/*!
953 Constructor.
954 \param fname : FITS file name to be written (created or updated)
955 \param update : file is created if FALSE, open for update if TRUE
956 \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
957 \param lp : debug level
958*/
959FitsImg3DWriter::FitsImg3DWriter(string fname,bool update,int bitpix,int lp)
960: FitsWriter(fname,update,lp)
961{
962 BitPix = bitpix;
963 HduType = IMAGE_HDU;
964 FirstTime = true;
965 Naxis[0] = Naxis[1] = Naxis[2] = 0;
966}
967
968/*!
969 Constructor.
970 \param cfname : FITS file name to be written (created or updated)
971 \param update : file is created if FALSE, open for update if TRUE
972 \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
973 \param lp : debug level
974*/
975FitsImg3DWriter::FitsImg3DWriter(const char* cfname,bool update,int bitpix,int lp)
976: FitsWriter(cfname,update,lp)
977{
978 BitPix = bitpix;
979 HduType = IMAGE_HDU;
980 FirstTime = true;
981 Naxis[0] = Naxis[1] = Naxis[2] = 0;
982}
983
984/*! Destructor */
985FitsImg3DWriter::~FitsImg3DWriter()
986{
987}
988
989/*! Create the 3D image. Done at the first write request. */
990void FitsImg3DWriter::createimg(void)
991{
992 if(!FirstTime)
993 throw NotAvailableOperation("FitsImg3DWriter::createimg: already created 3D image\n");
994 if(Naxis[0]<=0 || Naxis[1]<=0 || Naxis[2]<=0)
995 throw ParmError("FitsImg3DWriter::createimg: bad Naxis 1 or 2 or 3 value\n");
996
997 int sta=0;
998 if(fits_create_imgll(FitsPtr,BitPix,3,Naxis,&sta)) {
999 printerror(sta);
1000 throw NullPtrError("FitsImg3DWriter::createimg: Error creating 3D image extension\n");
1001 }
1002
1003 FirstTime = false;
1004}
1005
1006/*!
1007 Write an unsigned short 3D image to FITS file.
1008*/
1009void FitsImg3DWriter::Write(TArray<uint_2>& data)
1010{
1011 if(data.Rank()!=3)
1012 throw ParmError("FitsImg3DRd::Write(TArray<uint_2>): not a 3D array\n");
1013
1014 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1015 uint_2* arr = new uint_2[Naxis[0]];
1016
1017 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1018 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1019 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1020 fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
1021 if(sta) {
1022 printerrorwrite("uint_2",j,k,sta); delete [] arr;
1023 throw
1024 NotAvailableOperation("FitsImg3DRd::Write(TArray<uint_2>): Error Writing Fits file\n");
1025 }
1026 }
1027
1028 delete [] arr;
1029}
1030
1031/*! Write a long 3D image to FITS file. */
1032void FitsImg3DWriter::Write(TArray<int_4>& data)
1033{
1034 if(data.Rank()!=3)
1035 throw ParmError("FitsImg3DRd::Write(TArray<int_4>): not a 3D array\n");
1036
1037 int T = (sizeof(long)==4) ? TLONG: TINT;
1038 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1039 int_4* arr = new int_4[Naxis[0]];
1040
1041 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1042 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1043 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1044 fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
1045 if(sta) {
1046 printerrorwrite("int_4",j,k,sta); delete [] arr;
1047 throw
1048 NotAvailableOperation("FitsImg3DRd::Write(TArray<int_4>): Error Writing Fits file\n");
1049 }
1050 }
1051
1052 delete [] arr;
1053}
1054
1055/*! Write a float 3D image to FITS file. */
1056void FitsImg3DWriter::Write(TArray<float>& data)
1057{
1058 if(data.Rank()!=3)
1059 throw ParmError("FitsImg3DRd::Write(TArray<float>): not a 3D array\n");
1060
1061 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1062 float* arr = new float[Naxis[0]];
1063
1064 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1065 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1066 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1067 fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
1068 if(sta) {
1069 printerrorwrite("float",j,k,sta); delete [] arr;
1070 throw
1071 NotAvailableOperation("FitsImg3DRd::Write(TArray<float>): Error Writing Fits file\n");
1072 }
1073 }
1074
1075 delete [] arr;
1076}
1077
1078/*! Write a double 3D image to FITS file. */
1079void FitsImg3DWriter::Write(TArray<double>& data)
1080{
1081 if(data.Rank()!=3)
1082 throw ParmError("FitsImg3DRd::Write(TArray<double>): not a 3D array\n");
1083
1084 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1085 double* arr = new double[Naxis[0]];
1086
1087 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1088 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1089 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1090 fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
1091 if(sta) {
1092 printerrorwrite("double",j,k,sta); delete [] arr;
1093 throw
1094 NotAvailableOperation("FitsImg3DRd::Write(TArray<double>): Error Writing Fits file\n");
1095 }
1096 }
1097
1098 delete [] arr;
1099}
1100
1101/*! Print infos. */
1102void FitsImg3DWriter::Print(ostream& os) const
1103{
1104os<<"FitsImg3DWriter::Print: FitsFN "<<FitsFN<<endl
1105 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow<<" BitPix "<<BitPix
1106 <<" Naxis1 "<<Naxis[0]<<" Naxis2 "<<Naxis[1]<<" Naxis3 "<<Naxis[2]<<endl;
1107}
Note: See TracBrowser for help on using the repository browser.