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

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

modifs cmv 9/8/02

File size: 14.9 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) {
196 if(tform==NULL) tformauto = true;
197 else if(strlen(tform)<=0) tformauto = true;
198 }
199 if(tformauto) {
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 {
218 if(tform) TForm.push_back(tform); else TForm.push_back("");
219 datatype = 0;
220 }
221 Label.push_back(label);
222 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
223
224 int n = (int) Label.size() -1;
225
226 if(DbgLevel)
227 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
228 <<" TForm="<<TForm[n]
229 <<" TUnit="<<TUnit[n]
230 <<" datatype="<<datatype<<endl;
231
232 return n;
233}
234
235/*! Create the table. Done at the first write request. */
236void FitsABTWriter::createtbl(void)
237{
238 if(!FirstTime) return;
239
240 int tfields = Label.size();
241 if(tfields<=0)
242 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
243
244 long nrows = 0;
245 char *extname = NULL;
246 char **ttype = (char **) malloc(tfields*sizeof(char *));
247 char **tform = (char **) malloc(tfields*sizeof(char *));
248 char **tunit = (char **) malloc(tfields*sizeof(char *));
249
250 if(ExtName.size()>0) {
251 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
252 strcpy(extname,ExtName.c_str());
253 }
254 int i;
255 for(i=0;i<tfields;i++) {
256 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
257 strcpy(ttype[i],Label[i].c_str());
258 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
259 strcpy(tform[i],TForm[i].c_str());
260 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
261 strcpy(tunit[i],TUnit[i].c_str());
262 }
263
264 // append a new empty binary/ascii table onto the FITS file
265 int sta=0;
266 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
267 printerror(sta);
268 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
269 }
270
271 // Append Fits key
272 writekeys();
273
274 // menage
275 if(extname) delete [] extname;
276 for(i=0;i<tfields;i++) {
277 if(ttype[i]) delete [] ttype[i];
278 if(tform[i]) delete [] tform[i];
279 if(tunit[i]) delete [] tunit[i];
280 }
281 if(ttype) delete [] ttype;
282 if(tform) delete [] tform;
283 if(tunit) delete [] tunit;
284
285 FirstTime = false;
286}
287
288//////////////////////////////////////////////////////////////
289/*!
290 Write a data to FITS file.
291 \param col : column number [0,ncol[
292 \param row : row number [0,nrow[
293 \param val : value to be written
294 \warning that routine write a SHORT value into column "col"
295 which could have been defined with an other type.
296 Cast is performed by the cfitsio package.
297 \verbatim
298 WARNING: suppose that the column has be defined to be TSHORT
299 and suppose that you wanted to write a double value "val"
300 - If you write dummy.Write(col,row,(short)(val))
301 the Write(int,long,short) routine is called and
302 the cast is performed by the C++ language.
303 - If you write dummy.Write(col,row,val) where val is double
304 the Write(int,long,double) routine is called and
305 the cast is performed by the cfistio package.
306 \endverbatim
307*/
308
309/*! Write signed char (1 Byte) data to FITS file (see below) */
310void FitsABTWriter::Write(int col,long row,int_1 val)
311{
312 if(FirstTime) createtbl();
313 int sta=0;
314 if(fits_write_col(FitsPtr,TBYTE,col+1,row+1,1,1,&val,&sta))
315 printerrorwrite("char",col,row,sta);
316}
317
318/*! Write short (2 Bytes) data to FITS file (see below) */
319void FitsABTWriter::Write(int col,long row,int_2 val)
320{
321 if(FirstTime) createtbl();
322 int sta=0;
323 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
324 printerrorwrite("short",col,row,sta);
325}
326
327/*! Write unsigned short (2 Bytes) data to FITS file (see below) */
328void FitsABTWriter::Write(int col,long row,uint_2 val)
329{
330 if(FirstTime) createtbl();
331 int sta=0;
332 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
333 printerrorwrite("unsigned short",col,row,sta);
334}
335
336/*! Write long (4 Bytes) data to FITS file (see below) */
337void FitsABTWriter::Write(int col,long row,int_4 val)
338{
339 if(FirstTime) createtbl();
340 int sta=0;
341 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
342 int T = (sizeof(long)==4) ? TLONG: TINT;
343 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
344 printerrorwrite("long",col,row,sta);
345}
346
347/*! Write unsigned long (4 Bytes) data to FITS file (see below) */
348void FitsABTWriter::Write(int col,long row,uint_4 val)
349{
350 if(FirstTime) createtbl();
351 int sta=0;
352 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
353 int T = (sizeof(unsigned long)==4) ? TULONG: TUINT;
354 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
355 printerrorwrite("long",col,row,sta);
356}
357
358/*! Write long long (8 Bytes) data to FITS file (see below) */
359void FitsABTWriter::Write(int col,long row,int_8 val)
360{
361#ifdef TLONGLONG
362 if(FirstTime) createtbl();
363 int sta=0;
364 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
365 printerrorwrite("long long",col,row,sta);
366#else
367 throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
368#endif
369}
370
371/*! Write float data to FITS file (see below) */
372void FitsABTWriter::Write(int col,long row,float val)
373{
374 if(FirstTime) createtbl();
375 int sta=0;
376 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
377 printerrorwrite("float",col,row,sta);
378}
379
380/*! Write double data to FITS file (see below) */
381void FitsABTWriter::Write(int col,long row,double val)
382{
383 if(FirstTime) createtbl();
384 int sta=0;
385 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
386 printerrorwrite("double",col,row,sta);
387}
388
389//////////////////////////////////////////////////////////////
390/*!
391 Write a vector of data to FITS file.
392 \param col : column number [0,ncol[
393 \param row : starting row number [0,nrow[
394 \param val : vector to be written
395 \return "N" = number of the next row to be written,
396 that is "N-1" is the number of the last row written.
397*/
398
399/*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
400long FitsABTWriter::Write(int col,long row,TVector<uint_2>& val)
401{
402 if(FirstTime) createtbl();
403 long nel = val.Size();
404 int sta=0;
405 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
406 printerrorwrite("long",col,row,sta);
407 return row+nel;
408}
409
410/*! Write a vector of long (4 Bytes) data to FITS file (see below) */
411long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
412{
413 if(FirstTime) createtbl();
414 long nel = val.Size();
415 int sta=0;
416 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
417 int T = (sizeof(long)==4) ? TLONG: TINT;
418 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
419 printerrorwrite("long",col,row,sta);
420 return row+nel;
421}
422
423/*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
424long FitsABTWriter::Write(int col,long row,TVector<int_8>& val)
425{
426#ifdef TLONGLONG
427 if(FirstTime) createtbl();
428 long nel = val.Size();
429 int sta=0;
430 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
431 printerrorwrite("long long",col,row,sta);
432 return row+nel;
433#else
434 throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
435#endif
436}
437
438/*! Write a vector of float data to FITS file (see below) */
439long FitsABTWriter::Write(int col,long row,TVector<float>& val)
440{
441 if(FirstTime) createtbl();
442 long nel = val.Size();
443 int sta=0;
444 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
445 printerrorwrite("float",col,row,sta);
446 return row+nel;
447}
448
449/*! Write a vector of double data to FITS file (see below) */
450long FitsABTWriter::Write(int col,long row,TVector<double>& val)
451{
452 if(FirstTime) createtbl();
453 long nel = val.Size();
454 int sta=0;
455 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
456 printerrorwrite("double",col,row,sta);
457 return row+nel;
458}
459
460//////////////////////////////////////////////////////////////
461void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
462{
463 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
464 printerror(sta);
465 char str[256];
466 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
467 ,type,col,row,sta);
468 throw NotAvailableOperation(str);
469}
470
471/////////////////////////////////////////////////
472void FitsABTWriter::printerror(int sta) const
473{
474 int stat = sta;
475 fits_report_error(stdout,stat);
476 fflush(stdout);
477 return;
478}
479
480/////////////////////////////////////////////////
481void FitsABTWriter::Print(ostream& os,int lp) const
482{
483os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
484 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
485 <<" NCol "<<Label.size()<<endl;
486if(Label.size()>0 && lp>=1)
487 for(int i=0;i<(int)Label.size();i++)
488 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
489}
Note: See TracBrowser for help on using the repository browser.