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

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

protect. contre pointeur null cmv 18/12/01

File size: 12.7 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) 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 //CMV$CHECK Commentaire temporaire (il faut au moins cfitsio 2204)
204 //else if(datatype==TINT32BIT) strcpy(str,"1J");
205 else if(datatype==TLONG) strcpy(str,"1J");
206 else if(datatype==TFLOAT) strcpy(str,"1E");
207 else if(datatype==TDOUBLE) strcpy(str,"1D");
208 else if(datatype==TUSHORT) strcpy(str,"1U");
209 else if(datatype==TULONG) strcpy(str,"1V");
210 else
211 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
212 TForm.push_back(str);
213 } else {
214 if(tform) TForm.push_back(tform); else TForm.push_back("");
215 datatype = 0;
216 }
217 Label.push_back(label);
218 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
219
220 int n = (int) Label.size() -1;
221
222 if(DbgLevel)
223 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
224 <<" TForm="<<TForm[n]
225 <<" TUnit="<<TUnit[n]
226 <<" datatype="<<datatype<<endl;
227
228 return n;
229}
230
231/*! Create the table. Done at the first write request. */
232void FitsABTWriter::createtbl(void)
233{
234 if(!FirstTime) return;
235
236 int tfields = Label.size();
237 if(tfields<=0)
238 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
239
240 long nrows = 0;
241 char *extname = NULL;
242 char **ttype = (char **) malloc(tfields*sizeof(char *));
243 char **tform = (char **) malloc(tfields*sizeof(char *));
244 char **tunit = (char **) malloc(tfields*sizeof(char *));
245
246 if(ExtName.size()>0) {
247 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
248 strcpy(extname,ExtName.c_str());
249 }
250 int i;
251 for(i=0;i<tfields;i++) {
252 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
253 strcpy(ttype[i],Label[i].c_str());
254 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
255 strcpy(tform[i],TForm[i].c_str());
256 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
257 strcpy(tunit[i],TUnit[i].c_str());
258 }
259
260 // append a new empty binary/ascii table onto the FITS file
261 int sta=0;
262 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
263 printerror(sta);
264 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
265 }
266
267 // Append Fits key
268 writekeys();
269
270 // menage
271 if(extname) delete [] extname;
272 for(i=0;i<tfields;i++) {
273 if(ttype[i]) delete [] ttype[i];
274 if(tform[i]) delete [] tform[i];
275 if(tunit[i]) delete [] tunit[i];
276 }
277 if(ttype) delete [] ttype;
278 if(tform) delete [] tform;
279 if(tunit) delete [] tunit;
280
281 FirstTime = false;
282}
283
284//////////////////////////////////////////////////////////////
285/*!
286 Write a short data to FITS file.
287 \param col : column number [0,ncol[
288 \param row : row number [0,nrow[
289 \param val : value to be written
290 \warning that routine write a SHORT value into column "col"
291 which could have been defined with an other type.
292 Cast is performed by the cfitsio package.
293 \verbatim
294 WARNING: suppose that the column has be defined to be TSHORT
295 and suppose that you wanted to write a double value "val"
296 - If you write dummy.Write(col,row,(short)(val))
297 the Write(int,long,short) routine is called and
298 the cast is performed by the C++ language.
299 - If you write dummy.Write(col,row,val) where val is double
300 the Write(int,long,double) routine is called and
301 the cast is performed by the cfistio package.
302 \endverbatim
303*/
304void FitsABTWriter::Write(int col,long row,short val)
305{
306 if(FirstTime) createtbl();
307 int sta=0;
308 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
309 printerrorwrite("short",col,row,sta);
310}
311
312/*! Write long data to FITS file (see below) */
313void FitsABTWriter::Write(int col,long row,int_4 val)
314{
315 if(FirstTime) createtbl();
316 int sta=0;
317 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
318 int T = (sizeof(long)==4) ? TLONG: TINT;
319 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
320 printerrorwrite("long",col,row,sta);
321}
322
323/*! Write float data to FITS file (see below) */
324void FitsABTWriter::Write(int col,long row,float val)
325{
326 if(FirstTime) createtbl();
327 int sta=0;
328 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
329 printerrorwrite("float",col,row,sta);
330}
331
332/*! Write double data to FITS file (see below) */
333void FitsABTWriter::Write(int col,long row,double val)
334{
335 if(FirstTime) createtbl();
336 int sta=0;
337 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
338 printerrorwrite("double",col,row,sta);
339}
340
341//////////////////////////////////////////////////////////////
342/*!
343 Write a vector of long data to FITS file.
344 \param col : column number [0,ncol[
345 \param row : starting row number [0,nrow[
346 \param val : vector to be written
347 \return "N" = number of the next row to be written,
348 that is "N-1" is the number of the last row written.
349*/
350/*! Write a vector of long data to FITS file (see below) */
351long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
352{
353 if(FirstTime) createtbl();
354 long nel = val.Size();
355 int sta=0;
356 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
357 int T = (sizeof(long)==4) ? TLONG: TINT;
358 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
359 printerrorwrite("long",col,row,sta);
360 return row+nel;
361}
362
363/*! Write a vector of float data to FITS file (see below) */
364long FitsABTWriter::Write(int col,long row,TVector<float>& val)
365{
366 if(FirstTime) createtbl();
367 long nel = val.Size();
368 int sta=0;
369 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
370 printerrorwrite("float",col,row,sta);
371 return row+nel;
372}
373
374/*! Write a vector of double data to FITS file (see below) */
375long FitsABTWriter::Write(int col,long row,TVector<double>& val)
376{
377 if(FirstTime) createtbl();
378 long nel = val.Size();
379 int sta=0;
380 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
381 printerrorwrite("double",col,row,sta);
382 return row+nel;
383}
384
385//////////////////////////////////////////////////////////////
386void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
387{
388 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
389 printerror(sta);
390 char str[256];
391 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
392 ,type,col,row,sta);
393 throw NotAvailableOperation(str);
394}
395
396/////////////////////////////////////////////////
397void FitsABTWriter::printerror(int sta) const
398{
399 int stat = sta;
400 fits_report_error(stdout,stat);
401 fflush(stdout);
402 return;
403}
404
405/////////////////////////////////////////////////
406void FitsABTWriter::Print(ostream& os,int lp) const
407{
408os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
409 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
410 <<" NCol "<<Label.size()<<endl;
411if(Label.size()>0 && lp>=1)
412 for(int i=0;i<(int)Label.size();i++)
413 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
414}
Note: See TracBrowser for help on using the repository browser.