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

Last change on this file since 4023 was 4023, checked in by cmv, 14 years ago

bintable avec elements vecteur, cmv 02/10/2011

File size: 41.9 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 nfirstel : element number [0,repeat[
535 \param val : value to be written
536 \warning that routine write a SHORT value into column "col"
537 which could have been defined with an other type.
538 Cast is performed by the cfitsio package.
539 \verbatim
540 WARNING: suppose that the column has be defined to be TSHORT
541 and suppose that you wanted to write a double value "val"
542 - If you write dummy.Write(col,row,nfirstel,(short)(val))
543 the Write(int,long,long,short) routine is called and
544 the cast is performed by the C++ language.
545 - If you write dummy.Write(col,row,nfirstel,val) where val is double
546 the Write(int,long,long,double) routine is called and
547 the cast is performed by the cfistio package.
548 \endverbatim
549*/
550
551/*! Write signed char (1 Byte) data to FITS file (see below) */
552void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,int_1 val)
553{
554#ifdef TSBYTE
555 if(FirstTime) createtbl();
556 int sta=0;
557 if(fits_write_col(FitsPtr,TSBYTE,col+1,row+1,nfirstel+1,1,&val,&sta))
558 printerrorwrite("signed char",col,row,sta);
559#else
560 throw PException("FitsABTWriter::Write(..,int_1) Not in that cfitsio version");
561#endif
562}
563
564/*! Write unsigned char (1 Byte) data to FITS file (see below) */
565void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,uint_1 val)
566{
567 if(FirstTime) createtbl();
568 int sta=0;
569 if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,nfirstel+1,1,&val,&sta))
570 printerrorwrite("unsigned char",col,row,sta);
571}
572
573/*! Write short (2 Bytes) data to FITS file (see below) */
574void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,int_2 val)
575{
576 if(FirstTime) createtbl();
577 int sta=0;
578 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,nfirstel+1,1,&val,&sta))
579 printerrorwrite("short",col,row,sta);
580}
581
582/*! Write unsigned short (2 Bytes) data to FITS file (see below) */
583void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,uint_2 val)
584{
585 if(FirstTime) createtbl();
586 int sta=0;
587 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,nfirstel+1,1,&val,&sta))
588 printerrorwrite("unsigned short",col,row,sta);
589}
590
591/*! Write long (4 Bytes) data to FITS file (see below) */
592void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,int_4 val)
593{
594 if(FirstTime) createtbl();
595 int sta=0;
596 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
597 int T = (sizeof(long)==4) ? TLONG: TINT;
598 if(fits_write_col(FitsPtr,T,col+1,row+1,nfirstel+1,1,&val,&sta))
599 printerrorwrite("long",col,row,sta);
600}
601
602/*! Write unsigned long (4 Bytes) data to FITS file (see below) */
603void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,uint_4 val)
604{
605 if(FirstTime) createtbl();
606 int sta=0;
607 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
608 int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
609 if(fits_write_col(FitsPtr,T,col+1,row+1,nfirstel+1,1,&val,&sta))
610 printerrorwrite("long",col,row,sta);
611}
612
613/*! Write long long (8 Bytes) data to FITS file (see below) */
614void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,int_8 val)
615{
616#ifdef TLONGLONG
617 if(FirstTime) createtbl();
618 int sta=0;
619 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,nfirstel+1,1,&val,&sta))
620 printerrorwrite("long long",col,row,sta);
621#else
622 throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
623#endif
624}
625
626/*! Write float data to FITS file (see below) */
627void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,float val)
628{
629 if(FirstTime) createtbl();
630 int sta=0;
631 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,nfirstel+1,1,&val,&sta))
632 printerrorwrite("float",col,row,sta);
633}
634
635/*! Write double data to FITS file (see below) */
636void FitsABTWriter::Write(int col,LONGLONG row,long nfirstel,double val)
637{
638 if(FirstTime) createtbl();
639 int sta=0;
640 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,nfirstel+1,1,&val,&sta))
641 printerrorwrite("double",col,row,sta);
642}
643
644//////////////////////////////////////////////////////////////
645/*!
646 Write a vector of data to FITS file.
647 \param col : column number [0,ncol[
648 \param row : starting row number [0,nrow[
649 \param val : vector to be written
650 \return "N" = number of the next row to be written,
651 that is "N-1" is the number of the last row written.
652 \warning : be carefull if "repeat" not egal to 1
653*/
654
655/*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
656LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<uint_2>& val)
657{
658 if(FirstTime) createtbl();
659 LONGLONG nel = val.Size();
660 int sta=0;
661 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
662 printerrorwrite("long",col,row,sta);
663 return row+nel;
664}
665
666/*! Write a vector of long (4 Bytes) data to FITS file (see below) */
667LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<int_4>& val)
668{
669 if(FirstTime) createtbl();
670 LONGLONG nel = val.Size();
671 int sta=0;
672 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
673 int T = (sizeof(long)==4) ? TLONG: TINT;
674 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
675 printerrorwrite("long",col,row,sta);
676 return row+nel;
677}
678
679/*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
680LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<int_8>& val)
681{
682#ifdef TLONGLONG
683 if(FirstTime) createtbl();
684 LONGLONG nel = val.Size();
685 int sta=0;
686 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
687 printerrorwrite("long long",col,row,sta);
688 return row+nel;
689#else
690 throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
691#endif
692}
693
694/*! Write a vector of float data to FITS file (see below) */
695LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<float>& val)
696{
697 if(FirstTime) createtbl();
698 LONGLONG nel = val.Size();
699 int sta=0;
700 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
701 printerrorwrite("float",col,row,sta);
702 return row+nel;
703}
704
705/*! Write a vector of double data to FITS file (see below) */
706LONGLONG FitsABTWriter::Write(int col,LONGLONG row,TVector<double>& val)
707{
708 if(FirstTime) createtbl();
709 LONGLONG nel = val.Size();
710 int sta=0;
711 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
712 printerrorwrite("double",col,row,sta);
713 return row+nel;
714}
715
716/////////////////////////////////////////////////
717void FitsABTWriter::Print(ostream& os,int lp) const
718{
719os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
720 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
721 <<" NCol "<<Label.size()<<endl;
722if(Label.size()>0 && lp>=1)
723 for(int i=0;i<(int)Label.size();i++)
724 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
725}
726
727//////////////////////////////////////////////////////////////////
728//////////////////////////////////////////////////////////////////
729//////////////////////////////////////////////////////////////////
730//////////////////////////////////////////////////////////////////
731/*!
732 \class SOPHYA::FitsImg2DWriter
733 \ingroup FitsIOServer
734 Class for writing an image into FITS file
735*/
736
737/*!
738 Constructor.
739 \param fname : FITS file name to be written (a new fits file is created)
740 \param bitpix standard: image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
741 \param bitpix extended: image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
742 \param lp : debug level
743*/
744FitsImg2DWriter::FitsImg2DWriter(string fname,int bitpix,int lp)
745: FitsWriter(fname,lp)
746{
747 BitPix = bitpix;
748 HduType = IMAGE_HDU;
749 FirstTime = true;
750 Naxis[0] = Naxis[1] = 0;
751}
752
753/*!
754 Constructor.
755 \param cfname : FITS file name to be written (a new fits file is created)
756 \param bitpix standard: image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
757 \param bitpix extended: image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
758 \param lp : debug level
759*/
760FitsImg2DWriter::FitsImg2DWriter(const char* cfname,int bitpix,int lp)
761: FitsWriter(cfname,lp)
762{
763 BitPix = bitpix;
764 HduType = IMAGE_HDU;
765 FirstTime = true;
766 Naxis[0] = Naxis[1] = 0;
767}
768
769/*!
770 Constructor.
771 \param fname : FITS file name to be written (created or updated)
772 \param update : file is created if FALSE, open for update if TRUE
773 \param bitpix standard: image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
774 \param bitpix extended: image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
775 \param lp : debug level
776*/
777FitsImg2DWriter::FitsImg2DWriter(string fname,bool update,int bitpix,int lp)
778: FitsWriter(fname,update,lp)
779{
780 BitPix = bitpix;
781 HduType = IMAGE_HDU;
782 FirstTime = true;
783 Naxis[0] = Naxis[1] = 0;
784}
785
786/*!
787 Constructor.
788 \param cfname : FITS file name to be written (created or updated)
789 \param update : file is created if FALSE, open for update if TRUE
790 \param bitpix standard: image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
791 \param bitpix extended: image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
792 \param lp : debug level
793*/
794FitsImg2DWriter::FitsImg2DWriter(const char* cfname,bool update,int bitpix,int lp)
795: FitsWriter(cfname,update,lp)
796{
797 BitPix = bitpix;
798 HduType = IMAGE_HDU;
799 FirstTime = true;
800 Naxis[0] = Naxis[1] = 0;
801}
802
803/*! Destructor */
804FitsImg2DWriter::~FitsImg2DWriter()
805{
806}
807
808/*! Create the image. Done at the first write request. */
809void FitsImg2DWriter::createimg(void)
810{
811 if(!FirstTime)
812 throw NotAvailableOperation("FitsImg2DWriter::createimg: already created image\n");
813 if(Naxis[0]<=0 || Naxis[1]<=0)
814 throw ParmError("FitsImg2DWriter::createimg: bad Naxis 1 or 2 value\n");
815
816 int sta=0;
817 if(fits_create_imgll(FitsPtr,BitPix,2,Naxis,&sta)) {
818 printerror(sta);
819 throw NullPtrError("FitsImg2DWriter::createimg: Error creating image extension\n");
820 }
821
822 FirstTime = false;
823}
824
825/*!
826 Write an unsigned byte image to FITS file.
827 \warning TMatrix data(Naxis2,Naxis1) means Data(row,column)
828*/
829void FitsImg2DWriter::Write(TMatrix<uint_1>& data)
830{
831 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
832 uint_1* arr = new uint_1[Naxis[0]];
833
834 for(LONGLONG l=0;l<Naxis[1];l++) {
835 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
836 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
837 fits_write_img(FitsPtr,TBYTE,deb,nel,arr,&sta);
838 if(sta) {
839 printerrorwrite("uint_1",0,l,sta); delete [] arr;
840 throw
841 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<uint_1>): Error Writing Fits file\n");
842 }
843 }
844
845 delete [] arr;
846}
847
848/*!
849 Write an unsigned short image to FITS file.
850 \warning TMatrix data(Naxis2,Naxis1) means Data(row,column)
851*/
852void FitsImg2DWriter::Write(TMatrix<uint_2>& data)
853{
854 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
855 uint_2* arr = new uint_2[Naxis[0]];
856
857 for(LONGLONG l=0;l<Naxis[1];l++) {
858 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
859 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
860 fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
861 if(sta) {
862 printerrorwrite("uint_2",0,l,sta); delete [] arr;
863 throw
864 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<uint_2>): Error Writing Fits file\n");
865 }
866 }
867
868 delete [] arr;
869}
870
871/*! Write a long image to FITS file. */
872void FitsImg2DWriter::Write(TMatrix<int_4>& data)
873{
874 int T = (sizeof(long)==4) ? TLONG: TINT;
875 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
876 int_4* arr = new int_4[Naxis[0]];
877
878 for(LONGLONG l=0;l<Naxis[1];l++) {
879 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
880 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
881 fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
882 if(sta) {
883 printerrorwrite("int_4",0,l,sta); delete [] arr;
884 throw
885 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<int_4>): Error Writing Fits file\n");
886 }
887 }
888
889 delete [] arr;
890}
891
892/*! Write a float image to FITS file. */
893void FitsImg2DWriter::Write(TMatrix<float>& data)
894{
895 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
896 float* arr = new float[Naxis[0]];
897
898 for(LONGLONG l=0;l<Naxis[1];l++) {
899 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
900 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
901 fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
902 if(sta) {
903 printerrorwrite("float",0,l,sta); delete [] arr;
904 throw
905 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<float>): Error Writing Fits file\n");
906 }
907 }
908
909 delete [] arr;
910}
911
912/*! Write a double image to FITS file. */
913void FitsImg2DWriter::Write(TMatrix<double>& data)
914{
915 Naxis[0]=data.NCols(); Naxis[1]=data.NRows(); createimg();
916 double* arr = new double[Naxis[0]];
917
918 for(LONGLONG l=0;l<Naxis[1];l++) {
919 for(LONGLONG c=0;c<Naxis[0];c++) arr[c] = data(l,c);
920 LONGLONG deb = l*Naxis[0]+1, nel = Naxis[0]; int sta=0;
921 fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
922 if(sta) {
923 printerrorwrite("double",0,l,sta); delete [] arr;
924 throw
925 NotAvailableOperation("FitsImg2DRd::Write(TMatrix<double>): Error Writing Fits file\n");
926 }
927 }
928
929 delete [] arr;
930}
931
932/*! Print infos. */
933void FitsImg2DWriter::Print(ostream& os) const
934{
935os<<"FitsImg2DWriter::Print: FitsFN "<<FitsFN<<endl
936 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow<<" BitPix "<<BitPix
937 <<" Naxis1 "<<Naxis[0]<<" Naxis2 "<<Naxis[1]<<endl;
938}
939
940
941//////////////////////////////////////////////////////////////////
942//////////////////////////////////////////////////////////////////
943//////////////////////////////////////////////////////////////////
944//////////////////////////////////////////////////////////////////
945/*!
946 \class SOPHYA::FitsImg3DWriter
947 \ingroup FitsIOServer
948 Class for writing an 3D image into FITS file
949*/
950
951/*!
952 Constructor.
953 \param fname : FITS file name to be written (a new fits file is created)
954 \param bitpix standard: 3D image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
955 \param bitpix extended: 3D image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
956 \param lp : debug level
957*/
958FitsImg3DWriter::FitsImg3DWriter(string fname,int bitpix,int lp)
959: FitsWriter(fname,lp)
960{
961 BitPix = bitpix;
962 HduType = IMAGE_HDU;
963 FirstTime = true;
964 Naxis[0] = Naxis[1] = Naxis[2] = 0;
965}
966
967/*!
968 Constructor.
969 \param cfname : FITS file name to be written (a new fits file is created)
970 \param bitpix standard: 3D image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
971 \param bitpix extended: 3D image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
972 \param lp : debug level
973*/
974FitsImg3DWriter::FitsImg3DWriter(const char* cfname,int bitpix,int lp)
975: FitsWriter(cfname,lp)
976{
977 BitPix = bitpix;
978 HduType = IMAGE_HDU;
979 FirstTime = true;
980 Naxis[0] = Naxis[1] = Naxis[2] = 0;
981}
982
983/*!
984 Constructor.
985 \param fname : FITS file name to be written (created or updated)
986 \param update : file is created if FALSE, open for update if TRUE
987 \param bitpix standard: 3D image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
988 \param bitpix extended: 3D image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
989 \param lp : debug level
990*/
991FitsImg3DWriter::FitsImg3DWriter(string fname,bool update,int bitpix,int lp)
992: FitsWriter(fname,update,lp)
993{
994 BitPix = bitpix;
995 HduType = IMAGE_HDU;
996 FirstTime = true;
997 Naxis[0] = Naxis[1] = Naxis[2] = 0;
998}
999
1000/*!
1001 Constructor.
1002 \param cfname : FITS file name to be written (created or updated)
1003 \param update : file is created if FALSE, open for update if TRUE
1004 \param bitpix standard: 3D image type (BYTE_IMG,SHORT_IMG,LONG_IMG,LONGLONG_IMG,FLOAT_IMG,DOUBLE_IMG)
1005 \param bitpix extended: 3D image type (SBYTE_IMG,USHORT_IMG,ULONG_IMG)
1006 \param lp : debug level
1007*/
1008FitsImg3DWriter::FitsImg3DWriter(const char* cfname,bool update,int bitpix,int lp)
1009: FitsWriter(cfname,update,lp)
1010{
1011 BitPix = bitpix;
1012 HduType = IMAGE_HDU;
1013 FirstTime = true;
1014 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1015}
1016
1017/*! Destructor */
1018FitsImg3DWriter::~FitsImg3DWriter()
1019{
1020}
1021
1022/*! Create the 3D image. Done at the first write request. */
1023void FitsImg3DWriter::createimg(void)
1024{
1025 if(!FirstTime)
1026 throw NotAvailableOperation("FitsImg3DWriter::createimg: already created 3D image\n");
1027 if(Naxis[0]<=0 || Naxis[1]<=0 || Naxis[2]<=0)
1028 throw ParmError("FitsImg3DWriter::createimg: bad Naxis 1 or 2 or 3 value\n");
1029
1030 int sta=0;
1031 if(fits_create_imgll(FitsPtr,BitPix,3,Naxis,&sta)) {
1032 printerror(sta);
1033 throw NullPtrError("FitsImg3DWriter::createimg: Error creating 3D image extension\n");
1034 }
1035
1036 FirstTime = false;
1037}
1038
1039/*! Create the 3D imageby yourself, ONLY use it with Write(TVector<>& data) */
1040void FitsImg3DWriter::CreateImg(LONGLONG naxis1,LONGLONG naxis2,LONGLONG naxis3)
1041{
1042 Naxis[0]=naxis1;
1043 Naxis[1]=naxis2;
1044 Naxis[2]=naxis3;
1045 createimg();
1046}
1047
1048/*!
1049 Write an unsigned byte 3D image to FITS file.
1050*/
1051void FitsImg3DWriter::Write(TArray<uint_1>& data)
1052{
1053 if(data.Rank()!=3)
1054 throw ParmError("FitsImg3DRd::Write(TArray<uint_1>): not a 3D array\n");
1055
1056 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1057 uint_1* arr = new uint_1[Naxis[0]];
1058
1059 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1060 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1061 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1062 fits_write_img(FitsPtr,TBYTE,deb,nel,arr,&sta);
1063 if(sta) {
1064 printerrorwrite("uint_1",j,k,sta); delete [] arr;
1065 throw
1066 NotAvailableOperation("FitsImg3DRd::Write(TArray<uint_1>): Error Writing Fits file\n");
1067 }
1068 }
1069
1070 delete [] arr;
1071}
1072
1073/*!
1074 Write an unsigned short 3D image to FITS file.
1075*/
1076void FitsImg3DWriter::Write(TArray<uint_2>& data)
1077{
1078 if(data.Rank()!=3)
1079 throw ParmError("FitsImg3DRd::Write(TArray<uint_2>): not a 3D array\n");
1080
1081 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1082 uint_2* arr = new uint_2[Naxis[0]];
1083
1084 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1085 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1086 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1087 fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
1088 if(sta) {
1089 printerrorwrite("uint_2",j,k,sta); delete [] arr;
1090 throw
1091 NotAvailableOperation("FitsImg3DRd::Write(TArray<uint_2>): Error Writing Fits file\n");
1092 }
1093 }
1094
1095 delete [] arr;
1096}
1097
1098/*! Write a long 3D image to FITS file. */
1099void FitsImg3DWriter::Write(TArray<int_4>& data)
1100{
1101 if(data.Rank()!=3)
1102 throw ParmError("FitsImg3DRd::Write(TArray<int_4>): not a 3D array\n");
1103
1104 int T = (sizeof(long)==4) ? TLONG: TINT;
1105 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1106 int_4* arr = new int_4[Naxis[0]];
1107
1108 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1109 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1110 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1111 fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
1112 if(sta) {
1113 printerrorwrite("int_4",j,k,sta); delete [] arr;
1114 throw
1115 NotAvailableOperation("FitsImg3DRd::Write(TArray<int_4>): Error Writing Fits file\n");
1116 }
1117 }
1118
1119 delete [] arr;
1120}
1121
1122/*! Write a float 3D image to FITS file. */
1123void FitsImg3DWriter::Write(TArray<float>& data)
1124{
1125 if(data.Rank()!=3)
1126 throw ParmError("FitsImg3DRd::Write(TArray<float>): not a 3D array\n");
1127
1128 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1129 float* arr = new float[Naxis[0]];
1130
1131 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1132 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1133 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1134 fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
1135 if(sta) {
1136 printerrorwrite("float",j,k,sta); delete [] arr;
1137 throw
1138 NotAvailableOperation("FitsImg3DRd::Write(TArray<float>): Error Writing Fits file\n");
1139 }
1140 }
1141
1142 delete [] arr;
1143}
1144
1145/*! Write a double 3D image to FITS file. */
1146void FitsImg3DWriter::Write(TArray<double>& data)
1147{
1148 if(data.Rank()!=3)
1149 throw ParmError("FitsImg3DRd::Write(TArray<double>): not a 3D array\n");
1150
1151 Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
1152 double* arr = new double[Naxis[0]];
1153
1154 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1155 for(LONGLONG i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
1156 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1157 fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
1158 if(sta) {
1159 printerrorwrite("double",j,k,sta); delete [] arr;
1160 throw
1161 NotAvailableOperation("FitsImg3DRd::Write(TArray<double>): Error Writing Fits file\n");
1162 }
1163 }
1164
1165 delete [] arr;
1166}
1167
1168/*!
1169 Write an unsigned byte Vector<> image to 3D FITS file.
1170 Vector is stored in Naxis1, j (resp. k) indicate place within Naxis2 (resp. Naxis3):
1171 so IMG(i,j,k) = Vector(i)
1172*/
1173void FitsImg3DWriter::Write(LONGLONG j, LONGLONG k,TVector<uint_1>& data)
1174{
1175 if(FirstTime)
1176 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_1>&): use CreateImg first !\n");
1177 if(data.Size()!=Naxis[0])
1178 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_1>&): wrong vector size\n");
1179 if(j<0 || j>=Naxis[1] || k<0 || k>=Naxis[2])
1180 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_1>&): j or k out of range\n");
1181 uint_1* arr = const_cast<uint_1 *>(data.Data());
1182 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1183 fits_write_img(FitsPtr,TBYTE,deb,nel,arr,&sta);
1184 if(sta) {
1185 printerrorwrite("uint_1",j,k,sta); delete [] arr;
1186 throw
1187 NotAvailableOperation("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_1>&): Error Writing Fits file\n");
1188 }
1189}
1190
1191/*!
1192 Write an unsigned short Vector<> image to 3D FITS file.
1193 Vector is stored in Naxis1, j (resp. k) indicate place within Naxis2 (resp. Naxis3):
1194 so IMG(i,j,k) = Vector(i)
1195*/
1196void FitsImg3DWriter::Write(LONGLONG j, LONGLONG k,TVector<uint_2>& data)
1197{
1198 if(FirstTime)
1199 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_2>&): use CreateImg first !\n");
1200 if(data.Size()!=Naxis[0])
1201 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_2>&): wrong vector size\n");
1202 if(j<0 || j>=Naxis[1] || k<0 || k>=Naxis[2])
1203 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_2>&): j or k out of range\n");
1204 uint_2* arr = const_cast<uint_2 *>(data.Data());
1205 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1206 fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
1207 if(sta) {
1208 printerrorwrite("uint_2",j,k,sta); delete [] arr;
1209 throw
1210 NotAvailableOperation("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<uint_2>&): Error Writing Fits file\n");
1211 }
1212}
1213
1214/*!
1215 Write an long Vector<> image to 3D FITS file.
1216 Vector is stored in Naxis1, j (resp. k) indicate place within Naxis2 (resp. Naxis3):
1217 so IMG(i,j,k) = Vector(i)
1218*/
1219void FitsImg3DWriter::Write(LONGLONG j, LONGLONG k,TVector<int_4>& data)
1220{
1221 if(FirstTime)
1222 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<int_4>&): use CreateImg first !\n");
1223 if(data.Size()!=Naxis[0])
1224 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<int_4>&): wrong vector size\n");
1225 if(j<0 || j>=Naxis[1] || k<0 || k>=Naxis[2])
1226 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<int_4>&): j or k out of range\n");
1227 int T = (sizeof(long)==4) ? TLONG: TINT;
1228 int_4* arr = const_cast<int_4 *>(data.Data());
1229 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1230 fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
1231 if(sta) {
1232 printerrorwrite("int_4",j,k,sta); delete [] arr;
1233 throw
1234 NotAvailableOperation("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<int_4>&): Error Writing Fits file\n");
1235 }
1236}
1237
1238/*!
1239 Write an float Vector<> image to 3D FITS file.
1240 Vector is stored in Naxis1, j (resp. k) indicate place within Naxis2 (resp. Naxis3):
1241 so IMG(i,j,k) = Vector(i)
1242*/
1243void FitsImg3DWriter::Write(LONGLONG j, LONGLONG k,TVector<float>& data)
1244{
1245 if(FirstTime)
1246 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<float>&): use CreateImg first !\n");
1247 if(data.Size()!=Naxis[0])
1248 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<float>&): wrong vector size\n");
1249 if(j<0 || j>=Naxis[1] || k<0 || k>=Naxis[2])
1250 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<float>&): j or k out of range\n");
1251 float* arr = const_cast<float *>(data.Data());
1252 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1253 fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
1254 if(sta) {
1255 printerrorwrite("float",j,k,sta); delete [] arr;
1256 throw
1257 NotAvailableOperation("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<float>&): Error Writing Fits file\n");
1258 }
1259}
1260
1261/*!
1262 Write an double Vector<> image to 3D FITS file.
1263 Vector is stored in Naxis1, j (resp. k) indicate place within Naxis2 (resp. Naxis3):
1264 so IMG(i,j,k) = Vector(i)
1265*/
1266void FitsImg3DWriter::Write(LONGLONG j, LONGLONG k,TVector<double>& data)
1267{
1268 if(FirstTime)
1269 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<double>&): use CreateImg first !\n");
1270 if(data.Size()!=Naxis[0])
1271 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<double>&): wrong vector size\n");
1272 if(j<0 || j>=Naxis[1] || k<0 || k>=Naxis[2])
1273 throw ParmError("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<double>&): j or k out of range\n");
1274 double* arr = const_cast<double *>(data.Data());
1275 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
1276 fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
1277 if(sta) {
1278 printerrorwrite("double",j,k,sta); delete [] arr;
1279 throw
1280 NotAvailableOperation("FitsImg3DRd::Write(LONGLONG,LONGLONG,TVector<double>&): Error Writing Fits file\n");
1281 }
1282}
1283
1284/*! Print infos. */
1285void FitsImg3DWriter::Print(ostream& os) const
1286{
1287os<<"FitsImg3DWriter::Print: FitsFN "<<FitsFN<<endl
1288 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow<<" BitPix "<<BitPix
1289 <<" Naxis1 "<<Naxis[0]<<" Naxis2 "<<Naxis[1]<<" Naxis3 "<<Naxis[2]<<endl;
1290}
Note: See TracBrowser for help on using the repository browser.