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

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

long long et uint_2 cmv 9/8/02

File size: 14.2 KB
RevLine 
[1654]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
[1659]12 \verbatim
13 //-----------------------------------------------------------
14 -- Exemple 1: Writing element by element
15 FitsABTWriter fbtw(fitswrit,BINARY_TBL);
[1654]16 fbtw.SetExtName("MY_OWN_EXTENSION");
[1660]17 fbtw.AddCol("vars",NULL,"km",TSHORT); // col=0
18 fbtw.AddCol("vars2",NULL,"km",TSHORT); // col=1
[1662]19 fbtw.AddCol("varl",NULL,"Degre",TLONG); // col=2
[1660]20 fbtw.AddCol("varf",NULL,"",TFLOAT); // col=3
21 fbtw.AddCol("vard","","arcmin",TDOUBLE); // col=4
[1654]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;
[1659]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 }
[1654]52 \endverbatim
53*/
54
55//////////////////////////////////////////////////////////////
56/*!
57 Constructor.
[1659]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
[1654]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
[1657]100 // create d'un Primary HDU
[1654]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;
[1814]113 writekeys();
[1654]114 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
115 FitsPtr = NULL;
116}
117
[1814]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{
[1822]151 if(FitsPtr==NULL) return;
[1814]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
[1654]173//////////////////////////////////////////////////////////////
174/*!
175 Add a new column to the FITS table
[1659]176 \param label : column label
[1660]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)
[1659]180 \param tunit : fits tunit definition (optional)
[2169]181 \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TLONGLONG TFLOAT TDOUBLE
[1660]182 TUSHORT TULONG. That parameter is only use in case
183 of a BINARY_TBL table when tform is not defined).
[1659]184 \return The number of the new added column in the table.
185 \warning col = [0,ncol-1]
[1654]186*/
[1660]187int FitsABTWriter::addcol(const char* label,const char* tform
188 ,const char* tunit,int datatype)
[1654]189{
190 if(!FirstTime)
191 throw AllocationError("FitsABTWriter::addcol: table already created\n");
192
[1660]193 // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
194 bool tformauto = false;
195 if(HduType==BINARY_TBL) {
[1669]196 if(tform==NULL) tformauto = true;
197 else if(strlen(tform)<=0) tformauto = true;
[1660]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");
[2169]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
[1657]210 else if(datatype==TFLOAT) strcpy(str,"1E");
211 else if(datatype==TDOUBLE) strcpy(str,"1D");
[1660]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");
[1654]216 TForm.push_back(str);
[1660]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("");
[1654]223
224 int n = (int) Label.size() -1;
225
226 if(DbgLevel)
227 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
228 <<" TForm="<<TForm[n]
[1660]229 <<" TUnit="<<TUnit[n]
230 <<" datatype="<<datatype<<endl;
[1654]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 }
[1664]254 int i;
255 for(i=0;i<tfields;i++) {
[1654]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
[1814]271 // Append Fits key
272 writekeys();
273
[1654]274 // menage
275 if(extname) delete [] extname;
[1664]276 for(i=0;i<tfields;i++) {
[1654]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 short data to FITS file.
[1659]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"
[1654]295 which could have been defined with an other type.
296 Cast is performed by the cfitsio package.
[1659]297 \verbatim
[1654]298 WARNING: suppose that the column has be defined to be TSHORT
[1660]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
[1654]302 the cast is performed by the C++ language.
303 - If you write dummy.Write(col,row,val) where val is double
[1660]304 the Write(int,long,double) routine is called and
[1654]305 the cast is performed by the cfistio package.
306 \endverbatim
307*/
[2170]308void FitsABTWriter::Write(int col,long row,int_2 val)
[1654]309{
310 if(FirstTime) createtbl();
311 int sta=0;
[1657]312 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
[1654]313 printerrorwrite("short",col,row,sta);
314}
315
[2170]316/*! Write unsigned short (2 Bytes) data to FITS file (see below) */
317void FitsABTWriter::Write(int col,long row,uint_2 val)
318{
319 if(FirstTime) createtbl();
320 int sta=0;
321 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,1,&val,&sta))
322 printerrorwrite("unsigned short",col,row,sta);
323}
324
[2169]325/*! Write long (4 Bytes) data to FITS file (see below) */
[1657]326void FitsABTWriter::Write(int col,long row,int_4 val)
[1654]327{
328 if(FirstTime) createtbl();
329 int sta=0;
[1657]330 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
331 int T = (sizeof(long)==4) ? TLONG: TINT;
332 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
[1654]333 printerrorwrite("long",col,row,sta);
334}
335
[2169]336/*! Write long long (8 Bytes) data to FITS file (see below) */
337void FitsABTWriter::Write(int col,long row,int_8 val)
338{
339#ifdef TLONGLONG
340 if(FirstTime) createtbl();
341 int sta=0;
342 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,1,&val,&sta))
343 printerrorwrite("long long",col,row,sta);
344#else
345 throw PException("FitsABTWriter::Write(..,int_8) Not in that cfitsio version");
346#endif
347}
348
[1654]349/*! Write float data to FITS file (see below) */
350void FitsABTWriter::Write(int col,long row,float val)
351{
352 if(FirstTime) createtbl();
353 int sta=0;
[1657]354 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
[1654]355 printerrorwrite("float",col,row,sta);
356}
357
358/*! Write double data to FITS file (see below) */
359void FitsABTWriter::Write(int col,long row,double val)
360{
361 if(FirstTime) createtbl();
362 int sta=0;
[1657]363 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
[1654]364 printerrorwrite("double",col,row,sta);
365}
366
[1657]367//////////////////////////////////////////////////////////////
368/*!
369 Write a vector of long data to FITS file.
[1659]370 \param col : column number [0,ncol[
371 \param row : starting row number [0,nrow[
372 \param val : vector to be written
373 \return "N" = number of the next row to be written,
374 that is "N-1" is the number of the last row written.
[1657]375*/
[2170]376/*! Write a vector of unsigned short (2 Bytes) data to FITS file (see below) */
377long FitsABTWriter::Write(int col,long row,TVector<uint_2>& val)
378{
379 if(FirstTime) createtbl();
380 long nel = val.Size();
381 int sta=0;
382 if(fits_write_col(FitsPtr,TUSHORT,col+1,row+1,1,nel,val.Data(),&sta))
383 printerrorwrite("long",col,row,sta);
384 return row+nel;
385}
386
[2169]387/*! Write a vector of long (4 Bytes) data to FITS file (see below) */
[1657]388long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
389{
390 if(FirstTime) createtbl();
391 long nel = val.Size();
392 int sta=0;
393 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
394 int T = (sizeof(long)==4) ? TLONG: TINT;
395 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
396 printerrorwrite("long",col,row,sta);
397 return row+nel;
398}
399
[2169]400/*! Write a vector of long long (8 Bytes) data to FITS file (see below) */
401long FitsABTWriter::Write(int col,long row,TVector<int_8>& val)
402{
403#ifdef TLONGLONG
404 if(FirstTime) createtbl();
405 long nel = val.Size();
406 int sta=0;
407 if(fits_write_col(FitsPtr,TLONGLONG,col+1,row+1,1,nel,val.Data(),&sta))
408 printerrorwrite("long long",col,row,sta);
409 return row+nel;
410#else
[2170]411 throw PException("FitsABTWriter::Write(..,TVector<int_8>&) Not in that cfitsio version");
[2169]412#endif
413}
414
[1657]415/*! Write a vector of float data to FITS file (see below) */
416long FitsABTWriter::Write(int col,long row,TVector<float>& val)
417{
418 if(FirstTime) createtbl();
419 long nel = val.Size();
420 int sta=0;
421 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
422 printerrorwrite("float",col,row,sta);
423 return row+nel;
424}
425
426/*! Write a vector of double data to FITS file (see below) */
427long FitsABTWriter::Write(int col,long row,TVector<double>& val)
428{
429 if(FirstTime) createtbl();
430 long nel = val.Size();
431 int sta=0;
432 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
433 printerrorwrite("double",col,row,sta);
434 return row+nel;
435}
436
437//////////////////////////////////////////////////////////////
[1660]438void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
[1654]439{
440 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
441 printerror(sta);
442 char str[256];
443 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
444 ,type,col,row,sta);
445 throw NotAvailableOperation(str);
446}
447
448/////////////////////////////////////////////////
449void FitsABTWriter::printerror(int sta) const
450{
451 int stat = sta;
452 fits_report_error(stdout,stat);
453 fflush(stdout);
454 return;
455}
[1673]456
457/////////////////////////////////////////////////
[1677]458void FitsABTWriter::Print(ostream& os,int lp) const
[1673]459{
460os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
461 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
462 <<" NCol "<<Label.size()<<endl;
463if(Label.size()>0 && lp>=1)
464 for(int i=0;i<(int)Label.size();i++)
465 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
466}
Note: See TracBrowser for help on using the repository browser.