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

Last change on this file since 1657 was 1657, checked in by cmv, 24 years ago

possibilite d'ecrire directement des TVector fabtwriter cmv 27/9/01

File size: 9.3 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 Exemple:
14 FitsABTWriter fbtw("myfits.fits",BINARY_TBL,3);
15 fbtw.SetExtName("MY_OWN_EXTENSION");
16 fbtw.AddCol("vars",TSHORT,"","km"); // col=0
17 fbtw.AddCol("vars2",TSHORT,"","km"); // col=1
18 fbtw.AddCol("varl",TINT32BIT,"","Degre"); // col=2
19 fbtw.AddCol("varf",TFLOAT,"",""); // col=3
20 fbtw.AddCol("vard",TDOUBLE,"","arcmin"); // col=4
21 fbtw.SetDebug(3);
22 for(long i=0;i<1000;i++) {
23 double x = i;
24 fbtw.Write(0,i,1000.*x); // if overflow, managed by cfitsio
25 // Write(int,long,double) is called
26 fbtw.Write(1,i,(short)(1000.*x));
27 // if overflow, managed by language
28 // Write(int,long,short) is called
29 fbtw.Write(2,i,x);
30 fbtw.Write(3,i,x);
31 fbtw.Write(4,i,x);
32 }
33 cout<<"Number of Overflows when writing: "
34 <<fbtw.GetNOverFlow()<<endl;
35 \endverbatim
36*/
37
38//////////////////////////////////////////////////////////////
39/*!
40 Constructor.
41 \verbatim
42 fname : FITS file name to be written
43 hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
44 lp : debug level
45 \endverbatim
46*/
47FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
48{
49 createfits(fname.c_str(),hdutype,lp);
50}
51
52/*! See FitsABTWriter() */
53FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
54{
55 createfits(cfname,hdutype,lp);
56}
57
58/*! See FitsABTWriter() */
59void FitsABTWriter::createfits(const char *cfname,int hdutype,int lp)
60{
61 FirstTime = true;
62 FitsPtr = NULL;
63 HduType = hdutype;
64 SetDebug(lp);
65 FitsFN = cfname;
66 NOverFlow = 0;
67
68 if(DbgLevel)
69 cout<<"FitsABTWriter::createfits FitsFN="<<FitsFN
70 <<" HduType="<<HduType<<endl;
71
72 if(FitsFN.size() <= 0 )
73 throw ParmError("FitsABTWriter::createfits: Fits file name error\n");
74
75 if(HduType!=BINARY_TBL && HduType!=ASCII_TBL)
76 throw TypeMismatchExc("FitsABTWriter::createfits: Only BINARY or ASCII table permitted\n");
77
78 // create new FITS file
79 int sta=0;
80 if(fits_create_file(&FitsPtr,FitsFN.c_str(),&sta)) {
81 printerror(sta);
82 throw NullPtrError("FitsABTWriter::createfits: Error creating Fits file\n");
83 }
84
85 // create d'un Primary HDU
86 //long naxes[1] = {0};
87 //if(fits_create_img(FitsPtr,BYTE_IMG,0,naxes,&sta)) {
88 // printerror(sta);
89 // throw NullPtrError("FitsABTWriter::createfits: Error creating Primary extension\n");
90 //}
91
92}
93
94/*! Destructor */
95FitsABTWriter::~FitsABTWriter()
96{
97 int sta = 0;
98 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
99 FitsPtr = NULL;
100}
101
102//////////////////////////////////////////////////////////////
103/*!
104 Add a new column to the FITS table
105 \verbatim
106 label : column label
107 datatype : TSHORT TINT32BIT TFLOAT or TDOUBLE
108 tform : fits tform definition
109 (can be automatically set if BINARY_TBL and tform="")
110 tunit : fits tunit definition (optional)
111 \endverbatim
112*/
113int FitsABTWriter::addcol(const char* label,int datatype
114 ,const char* tform,const char* tunit)
115{
116 if(!FirstTime)
117 throw AllocationError("FitsABTWriter::addcol: table already created\n");
118
119 if( datatype!=TSHORT && datatype!=TINT32BIT
120 && datatype!=TFLOAT && datatype!=TDOUBLE )
121 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
122
123 Label.push_back(label);
124 DataType.push_back(datatype);
125 // Gestion auto du tform par defaut pour les tables binaires
126 if(HduType==BINARY_TBL && strlen(tform)<=0) {
127 char str[16];
128 if(datatype==TSHORT) strcpy(str,"1I");
129 else if(datatype==TINT32BIT) strcpy(str,"1J");
130 else if(datatype==TFLOAT) strcpy(str,"1E");
131 else if(datatype==TDOUBLE) strcpy(str,"1D");
132 TForm.push_back(str);
133 } else TForm.push_back(tform);
134 TUnit.push_back(tunit);
135
136 int n = (int) Label.size() -1;
137
138 if(DbgLevel)
139 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
140 <<" datatype="<<datatype
141 <<" TForm="<<TForm[n]
142 <<" TUnit="<<TUnit[n]<<endl;
143
144 return n;
145}
146
147/*! Create the table. Done at the first write request. */
148void FitsABTWriter::createtbl(void)
149{
150 if(!FirstTime) return;
151
152 int tfields = Label.size();
153 if(tfields<=0)
154 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
155
156 long nrows = 0;
157 char *extname = NULL;
158 char **ttype = (char **) malloc(tfields*sizeof(char *));
159 char **tform = (char **) malloc(tfields*sizeof(char *));
160 char **tunit = (char **) malloc(tfields*sizeof(char *));
161
162 if(ExtName.size()>0) {
163 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
164 strcpy(extname,ExtName.c_str());
165 }
166 for(int i=0;i<tfields;i++) {
167 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
168 strcpy(ttype[i],Label[i].c_str());
169 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
170 strcpy(tform[i],TForm[i].c_str());
171 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
172 strcpy(tunit[i],TUnit[i].c_str());
173 }
174
175 // append a new empty binary/ascii table onto the FITS file
176 int sta=0;
177 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
178 printerror(sta);
179 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
180 }
181
182 // menage
183 if(extname) delete [] extname;
184 for(int i=0;i<tfields;i++) {
185 if(ttype[i]) delete [] ttype[i];
186 if(tform[i]) delete [] tform[i];
187 if(tunit[i]) delete [] tunit[i];
188 }
189 if(ttype) delete [] ttype;
190 if(tform) delete [] tform;
191 if(tunit) delete [] tunit;
192
193 FirstTime = false;
194}
195
196//////////////////////////////////////////////////////////////
197/*!
198 Write a short data to FITS file.
199 \verbatim
200 col : column number [0,ncol[
201 row : row number [0,nrow[
202 val : value to be written
203 WARNING: that routine write a SHORT value into column "col"
204 which could have been defined with an other type.
205 Cast is performed by the cfitsio package.
206 WARNING: suppose that the column has be defined to be TSHORT
207 and suppose that you wanted to write a double value
208 - If you write dummy.Write(col,row,(short(val))
209 you call the Write(int,long,short) routine and
210 the cast is performed by the C++ language.
211 - If you write dummy.Write(col,row,val) where val is double
212 you call theWrite(int,long,double) routine and
213 the cast is performed by the cfistio package.
214 \endverbatim
215*/
216void FitsABTWriter::Write(int col,long row,short val)
217{
218 if(FirstTime) createtbl();
219 int sta=0;
220 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
221 printerrorwrite("short",col,row,sta);
222}
223
224/*! Write long data to FITS file (see below) */
225void FitsABTWriter::Write(int col,long row,int_4 val)
226{
227 if(FirstTime) createtbl();
228 int sta=0;
229 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
230 int T = (sizeof(long)==4) ? TLONG: TINT;
231 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
232 printerrorwrite("long",col,row,sta);
233}
234
235/*! Write float data to FITS file (see below) */
236void FitsABTWriter::Write(int col,long row,float val)
237{
238 if(FirstTime) createtbl();
239 int sta=0;
240 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
241 printerrorwrite("float",col,row,sta);
242}
243
244/*! Write double data to FITS file (see below) */
245void FitsABTWriter::Write(int col,long row,double val)
246{
247 if(FirstTime) createtbl();
248 int sta=0;
249 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
250 printerrorwrite("double",col,row,sta);
251}
252
253//////////////////////////////////////////////////////////////
254/*!
255 Write a vector of long data to FITS file.
256 \verbatim
257 col : column number [0,ncol[
258 row : starting row number [0,nrow[
259 val : vector to be written
260 Return: number of the last written row (remember row=[0,nrow[)
261 \endverbatim
262*/
263/*! Write a vector of long data to FITS file (see below) */
264long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
265{
266 if(FirstTime) createtbl();
267 long nel = val.Size();
268 int sta=0;
269 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
270 int T = (sizeof(long)==4) ? TLONG: TINT;
271 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
272 printerrorwrite("long",col,row,sta);
273 return row+nel;
274}
275
276/*! Write a vector of float data to FITS file (see below) */
277long FitsABTWriter::Write(int col,long row,TVector<float>& val)
278{
279 if(FirstTime) createtbl();
280 long nel = val.Size();
281 int sta=0;
282 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
283 printerrorwrite("float",col,row,sta);
284 return row+nel;
285}
286
287/*! Write a vector of double data to FITS file (see below) */
288long FitsABTWriter::Write(int col,long row,TVector<double>& val)
289{
290 if(FirstTime) createtbl();
291 long nel = val.Size();
292 int sta=0;
293 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
294 printerrorwrite("double",col,row,sta);
295 return row+nel;
296}
297
298//////////////////////////////////////////////////////////////
299void FitsABTWriter::printerrorwrite(char* type,int col,long row,int sta)
300{
301 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
302 printerror(sta);
303 char str[256];
304 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
305 ,type,col,row,sta);
306 throw NotAvailableOperation(str);
307}
308
309/////////////////////////////////////////////////
310void FitsABTWriter::printerror(int sta) const
311{
312 int stat = sta;
313 fits_report_error(stdout,stat);
314 fflush(stdout);
315 return;
316}
Note: See TracBrowser for help on using the repository browser.