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

Last change on this file since 2322 was 2174, checked in by cmv, 23 years ago

gestion defaut ASCII table cmv 9/8/02

File size: 15.6 KB
Line 
1/* Writer de table Fits (binaire ou ASCII) */
2#include "machdefs.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include "pexceptions.h"
6#include "fabtwriter.h"
7
8/*!
9 \class SOPHYA::FitsABTWriter
10 \ingroup FitsIOServer
11 Class for writing a FITS ASCII or BINARY table
12 \verbatim
13 //-----------------------------------------------------------
14 -- Exemple 1: Writing element by element
15 FitsABTWriter fbtw(fitswrit,BINARY_TBL);
16 fbtw.SetExtName("MY_OWN_EXTENSION");
17 fbtw.AddCol("vars",NULL,"km",TSHORT); // col=0
18 fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=1
19 fbtw.AddCol("varl",NULL,"Degre",TLONG); // col=2
20 fbtw.AddCol("varf",NULL,"",TFLOAT); // col=3
21 fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=4
22 fbtw.SetDebug(3);
23 for(long i=0;i<1000;i++) {
24 double x = i;
25 fbtw.Write(0,i,1000.*x); // if overflow, managed by cfitsio
26 // Write(int,long,double) is called
27 fbtw.Write(1,i,(short)(1000.*x));
28 // if overflow, managed by language
29 // Write(int,long,short) is called
30 fbtw.Write(2,i,x);
31 fbtw.Write(3,i,x);
32 fbtw.Write(4,i,x);
33 }
34 cout<<"Number of Overflows when writing: "
35 <<fbtw.GetNOverFlow()<<endl;
36
37 //-----------------------------------------------------------
38 -- Exemple 2: Writing into TVector
39 ...
40 TVector<double> datad(100);
41 TVector<float> dataf(100);
42 TVector<int_4> datal(100);
43 for(long i=0;i<9990;i+=100) {
44 long i2=i+100-1; if(i2>=9990) i2=9990-1;
45 for(long j=0;j<100;j++) datad(i) = ...;
46 for(long j=0;j<100;j++) dataf(i) = ...;
47 for(long j=0;j<100;j++) datal(i) = ...;
48 fbtw.Write(1,i,datal);
49 fbtw.Write(2,i,dataf);
50 fbtw.Write(3,i,datad);
51 }
52 \endverbatim
53*/
54
55//////////////////////////////////////////////////////////////
56/*!
57 Constructor.
58 \param fname : FITS file name to be written
59 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
60 \param lp : debug level
61*/
62FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
63{
64 createfits(fname.c_str(),hdutype,lp);
65}
66
67/*! See FitsABTWriter() */
68FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
69{
70 createfits(cfname,hdutype,lp);
71}
72
73/*! See FitsABTWriter() */
74void FitsABTWriter::createfits(const char *cfname,int hdutype,int lp)
75{
76 FirstTime = true;
77 FitsPtr = NULL;
78 HduType = hdutype;
79 SetDebug(lp);
80 FitsFN = cfname;
81 NOverFlow = 0;
82
83 if(DbgLevel)
84 cout<<"FitsABTWriter::createfits FitsFN="<<FitsFN
85 <<" HduType="<<HduType<<endl;
86
87 if(FitsFN.size() <= 0 )
88 throw ParmError("FitsABTWriter::createfits: Fits file name error\n");
89
90 if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
91 throw TypeMismatchExc("FitsABTWriter::createfits: Only BINARY or ASCII table permitted\n");
92
93 // create new FITS file
94 int sta=0;
95 if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) {
96 printerror(sta);
97 throw NullPtrError("FitsABTWriter::createfits: Error creating Fits file\n");
98 }
99
100 // create d'un Primary HDU
101 //long naxes[1] = {0};
102 //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
103 // printerror(sta);
104 // throw NullPtrError("FitsABTWriter::createfits: Error creating Primary extension\n");
105 //}
106
107}
108
109/*! Destructor */
110FitsABTWriter::~FitsABTWriter()
111{
112 int sta = 0;
113 writekeys();
114 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
115 FitsPtr = NULL;
116}
117
118/*! Flush the FitsIO buffer to set good Fits file in case of problems */
119void FitsABTWriter::Flush(void)
120{
121 if(FitsPtr==NULL) return;
122 int sta = 0;
123 if(fits_flush_file(FitsPtr,&sta)) printerror(sta);
124}
125
126/*! Write a double value into Fits Header */
127void FitsABTWriter::WriteKey(const char *keyname,double val,char* comment)
128{
129 if(keyname==NULL || strlen(keyname)<=0) return;
130 KeyDouble k;
131 k.keyname=keyname;
132 k.val=val;
133 if(comment) k.comment=comment; else k.comment="";
134 DoubleKey.push_back(k);
135}
136
137/*! Write a long value into Fits Header */
138void FitsABTWriter::WriteKey(const char *keyname,long val,char* comment)
139{
140 if(keyname==NULL || strlen(keyname)<=0) return;
141 KeyLong k;
142 k.keyname=keyname;
143 k.val=val;
144 if(comment) k.comment=comment; else k.comment="";
145 LongKey.push_back(k);
146}
147
148void FitsABTWriter::writekeys(void)
149// Ecriture effective des clefs
150{
151 if(FitsPtr==NULL) return;
152 int sta=0;
153 if(DoubleKey.size()>0)
154 for(unsigned int i=0;i<DoubleKey.size();i++) {
155 char* key = const_cast<char*>(DoubleKey[i].keyname.c_str());
156 char* com = const_cast<char*>(DoubleKey[i].comment.c_str());
157 double val = DoubleKey[i].val;
158 if(fits_update_key(FitsPtr,TDOUBLE,key,&val,com,&sta))
159 printerror(sta);
160 }
161 if(LongKey.size()>0)
162 for(unsigned int i=0;i<LongKey.size();i++) {
163 char* key = const_cast<char*>(LongKey[i].keyname.c_str());
164 char* com = const_cast<char*>(LongKey[i].comment.c_str());
165 long val = LongKey[i].val;
166 if(fits_update_key(FitsPtr,TLONG,key,&val,com,&sta))
167 printerror(sta);
168 }
169 DoubleKey.resize(0);
170 LongKey.resize(0);
171}
172
173//////////////////////////////////////////////////////////////
174/*!
175 Add a new column to the FITS table
176 \param label : column label
177 \param tform : fits tform definition ("1I","1J","1E","1J",...)
178 (can be automatically set as "datatype"
179 if BINARY_TBL and tform="" or tform=NULL)
180 \param tunit : fits tunit definition (optional)
181 \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TLONGLONG TFLOAT TDOUBLE
182 TUSHORT TULONG. That parameter is only use in case
183 of a BINARY_TBL table when tform is not defined).
184 \return The number of the new added column in the table.
185 \warning col = [0,ncol-1]
186*/
187int FitsABTWriter::addcol(const char* label,const char* tform
188 ,const char* tunit,int datatype)
189{
190 if(!FirstTime)
191 throw AllocationError("FitsABTWriter::addcol: table already created\n");
192
193 // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
194 bool tformauto = false;
195 if(HduType==BINARY_TBL || HduType==ASCII_TBL) {
196 if(tform==NULL) tformauto = true;
197 else if(strlen(tform)<=0) tformauto = true;
198 }
199 if(tformauto && HduType==BINARY_TBL) {
200 char str[8];
201 if(datatype==TBYTE) strcpy(str,"1B");
202 else if(datatype==TSHORT) strcpy(str,"1I");
203 else if(datatype==TLONG) strcpy(str,"1J");
204#ifdef TINT32BIT
205 else if(datatype==TINT32BIT) strcpy(str,"1J");
206#endif
207#ifdef TLONGLONG
208 else if(datatype==TLONGLONG) strcpy(str,"1K");
209#endif
210 else if(datatype==TFLOAT) strcpy(str,"1E");
211 else if(datatype==TDOUBLE) strcpy(str,"1D");
212 else if(datatype==TUSHORT) strcpy(str,"1U");
213 else if(datatype==TULONG) strcpy(str,"1V");
214 else
215 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
216 TForm.push_back(str);
217 } else if(tformauto && HduType==ASCII_TBL) {
218 char str[8];
219 if(datatype==TBYTE) strcpy(str,"I5");
220 else if(datatype==TSHORT) strcpy(str,"I7");
221 else if(datatype==TLONG) strcpy(str,"I11");
222#ifdef TINT32BIT
223 else if(datatype==TINT32BIT) strcpy(str,"I11");
224#endif
225#ifdef TLONGLONG
226 else if(datatype==TLONGLONG) strcpy(str,"I21");
227#endif
228 else if(datatype==TFLOAT) strcpy(str,"E29.20");
229 else if(datatype==TDOUBLE) strcpy(str,"E29.20");
230 else if(datatype==TUSHORT) strcpy(str,"I7");
231 else if(datatype==TULONG) strcpy(str,"I11");
232 else
233 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
234 TForm.push_back(str);
235 } else {
236 if(tform) TForm.push_back(tform); else TForm.push_back("");
237 datatype = 0;
238 }
239 Label.push_back(label);
240 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
241
242 int n = (int) Label.size() -1;
243
244 if(DbgLevel)
245 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
246 <<" TForm="<<TForm[n]
247 <<" TUnit="<<TUnit[n]
248 <<" datatype="<<datatype<<endl;
249
250 return n;
251}
252
253/*! Create the table. Done at the first write request. */
254void FitsABTWriter::createtbl(void)
255{
256 if(!FirstTime) return;
257
258 int tfields = Label.size();
259 if(tfields<=0)
260 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
261
262 long nrows = 0;
263 char *extname = NULL;
264 char **ttype = (char **) malloc(tfields*sizeof(char *));
265 char **tform = (char **) malloc(tfields*sizeof(char *));
266 char **tunit = (char **) malloc(tfields*sizeof(char *));
267
268 if(ExtName.size()>0) {
269 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
270 strcpy(extname,ExtName.c_str());
271 }
272 int i;
273 for(i=0;i<tfields;i++) {
274 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
275 strcpy(ttype[i],Label[i].c_str());
276 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
277 strcpy(tform[i],TForm[i].c_str());
278 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
279 strcpy(tunit[i],TUnit[i].c_str());
280 }
281
282 // append a new empty binary/ascii table onto the FITS file
283 int sta=0;
284 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
285 printerror(sta);
286 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
287 }
288
289 // Append Fits key
290 writekeys();
291
292 // menage
293 if(extname) delete [] extname;
294 for(i=0;i<tfields;i++) {
295 if(ttype[i]) delete [] ttype[i];
296 if(tform[i]) delete [] tform[i];
297 if(tunit[i]) delete [] tunit[i];
298 }
299 if(ttype) delete [] ttype;
300 if(tform) delete [] tform;
301 if(tunit) delete [] tunit;
302
303 FirstTime = false;
304}
305
306//////////////////////////////////////////////////////////////
307/*!
308 Write a data to FITS file.
309 \param col : column number [0,ncol[
310 \param row : row number [0,nrow[
311 \param val : value to be written
312 \warning that routine write a SHORT value into column "col"
313 which could have been defined with an other type.
314 Cast is performed by the cfitsio package.
315 \verbatim
316 WARNING: suppose that the column has be defined to be TSHORT
317 and suppose that you wanted to write a double value "val"
318 - If you write dummy.Write(col,row,(short)(val))
319 the Write(int,long,short) routine is called and
320 the cast is performed by the C++ language.
321 - If you write dummy.Write(col,row,val) where val is double
322 the Write(int,long,double) routine is called and
323 the cast is performed by the cfistio package.
324 \endverbatim
325*/
326
327/*! Write unsigned char (1 Byte) data to FITS file (see below) */
328void FitsABTWriter::Write(int col,long row,uint_1 val)
329{
330 if(FirstTime) createtbl();
331 int sta=0;
332 if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,1,1,&val,&sta))
333 printerrorwrite("unsigned char",col,row,sta);
334}
335
336/*! Write short (2 Bytes) data to FITS file (see below) */
337void FitsABTWriter::Write(int col,long row,int_2 val)
338{
339 if(FirstTime) createtbl();
340 int sta=0;
341 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
342 printerrorwrite("short",col,row,sta);
343}
344
345/*! Write unsigned short (2 Bytes) data to FITS file (see below) */
346void FitsABTWriter::Write(int col,long row,uint_2 val)
347{
348 if(FirstTime) createtbl();
349 int sta=0;
350 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
351 printerrorwrite("unsigned short",col,row,sta);
352}
353
354/*! Write long (4 Bytes) data to FITS file (see below) */
355void FitsABTWriter::Write(int col,long row,int_4 val)
356{
357 if(FirstTime) createtbl();
358 int sta=0;
359 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
360 int T = (sizeof(long)==4) ? TLONG: TINT;
361 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
362 printerrorwrite("long",col,row,sta);
363}
364
365/*! Write unsigned long (4 Bytes) data to FITS file (see below) */
366void FitsABTWriter::Write(int col,long row,uint_4 val)
367{
368 if(FirstTime) createtbl();
369 int sta=0;
370 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
371 int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
372 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
373 printerrorwrite("long",col,row,sta);
374}
375
376/*! Write long long (8 Bytes) data to FITS file (see below) */
377void FitsABTWriter::Write(int col,long row,int_8 val)
378{
379#ifdef TLONGLONG
380 if(FirstTime) createtbl();
381 int sta=0;
382 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
383 printerrorwrite("long long",col,row,sta);
384#else
385 throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
386#endif
387}
388
389/*! Write float data to FITS file (see below) */
390void FitsABTWriter::Write(int col,long row,float val)
391{
392 if(FirstTime) createtbl();
393 int sta=0;
394 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
395 printerrorwrite("float",col,row,sta);
396}
397
398/*! Write double data to FITS file (see below) */
399void FitsABTWriter::Write(int col,long row,double val)
400{
401 if(FirstTime) createtbl();
402 int sta=0;
403 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
404 printerrorwrite("double",col,row,sta);
405}
406
407//////////////////////////////////////////////////////////////
408/*!
409 Write a vector of data to FITS file.
410 \param col : column number [0,ncol[
411 \param row : starting row number [0,nrow[
412 \param val : vector to be written
413 \return "N" = number of the next row to be written,
414 that is "N-1" is the number of the last row written.
415*/
416
417/*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
418long FitsABTWriter::Write(int col,long row,TVector<uint_2>& val)
419{
420 if(FirstTime) createtbl();
421 long nel = val.Size();
422 int sta=0;
423 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
424 printerrorwrite("long",col,row,sta);
425 return row+nel;
426}
427
428/*! Write a vector of long (4 Bytes) data to FITS file (see below) */
429long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
430{
431 if(FirstTime) createtbl();
432 long nel = val.Size();
433 int sta=0;
434 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
435 int T = (sizeof(long)==4) ? TLONG: TINT;
436 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
437 printerrorwrite("long",col,row,sta);
438 return row+nel;
439}
440
441/*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
442long FitsABTWriter::Write(int col,long row,TVector<int_8>& val)
443{
444#ifdef TLONGLONG
445 if(FirstTime) createtbl();
446 long nel = val.Size();
447 int sta=0;
448 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
449 printerrorwrite("long long",col,row,sta);
450 return row+nel;
451#else
452 throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
453#endif
454}
455
456/*! Write a vector of float data to FITS file (see below) */
457long FitsABTWriter::Write(int col,long row,TVector<float>& val)
458{
459 if(FirstTime) createtbl();
460 long nel = val.Size();
461 int sta=0;
462 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
463 printerrorwrite("float",col,row,sta);
464 return row+nel;
465}
466
467/*! Write a vector of double data to FITS file (see below) */
468long FitsABTWriter::Write(int col,long row,TVector<double>& val)
469{
470 if(FirstTime) createtbl();
471 long nel = val.Size();
472 int sta=0;
473 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
474 printerrorwrite("double",col,row,sta);
475 return row+nel;
476}
477
478//////////////////////////////////////////////////////////////
479void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
480{
481 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
482 printerror(sta);
483 char str[256];
484 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
485 ,type,col,row,sta);
486 throw NotAvailableOperation(str);
487}
488
489/////////////////////////////////////////////////
490void FitsABTWriter::printerror(int sta) const
491{
492 int stat = sta;
493 fits_report_error(stdout,stat);
494 fflush(stdout);
495 return;
496}
497
498/////////////////////////////////////////////////
499void FitsABTWriter::Print(ostream& os,int lp) const
500{
501os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
502 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
503 <<" NCol "<<Label.size()<<endl;
504if(Label.size()>0 && lp>=1)
505 for(int i=0;i<(int)Label.size();i++)
506 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
507}
Note: See TracBrowser for help on using the repository browser.