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

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

lecture/ecriture clefs fits cmv 14/12/01

File size: 12.7 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{
151 int sta=0;
152 if(DoubleKey.size()>0)
153 for(unsigned int i=0;i<DoubleKey.size();i++) {
154 char* key = const_cast<char*>(DoubleKey[i].keyname.c_str());
155 char* com = const_cast<char*>(DoubleKey[i].comment.c_str());
156 double val = DoubleKey[i].val;
157 if(fits_update_key(FitsPtr,TDOUBLE,key,&val,com,&sta))
158 printerror(sta);
159 }
160 if(LongKey.size()>0)
161 for(unsigned int i=0;i<LongKey.size();i++) {
162 char* key = const_cast<char*>(LongKey[i].keyname.c_str());
163 char* com = const_cast<char*>(LongKey[i].comment.c_str());
164 long val = LongKey[i].val;
165 if(fits_update_key(FitsPtr,TLONG,key,&val,com,&sta))
166 printerror(sta);
167 }
168 DoubleKey.resize(0);
169 LongKey.resize(0);
170}
171
[1654]172//////////////////////////////////////////////////////////////
173/*!
174 Add a new column to the FITS table
[1659]175 \param label : column label
[1660]176 \param tform : fits tform definition ("1I","1J","1E","1J",...)
177 (can be automatically set as "datatype"
178 if BINARY_TBL and tform="" or tform=NULL)
[1659]179 \param tunit : fits tunit definition (optional)
[1662]180 \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TFLOAT TDOUBLE
[1660]181 TUSHORT TULONG. That parameter is only use in case
182 of a BINARY_TBL table when tform is not defined).
[1659]183 \return The number of the new added column in the table.
184 \warning col = [0,ncol-1]
[1654]185*/
[1660]186int FitsABTWriter::addcol(const char* label,const char* tform
187 ,const char* tunit,int datatype)
[1654]188{
189 if(!FirstTime)
190 throw AllocationError("FitsABTWriter::addcol: table already created\n");
191
[1660]192 // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
193 bool tformauto = false;
194 if(HduType==BINARY_TBL) {
[1669]195 if(tform==NULL) tformauto = true;
196 else if(strlen(tform)<=0) tformauto = true;
[1660]197 }
198 if(tformauto) {
199 char str[8];
200 if(datatype==TBYTE) strcpy(str,"1B");
201 else if(datatype==TSHORT) strcpy(str,"1I");
[1662]202 //CMV$CHECK Commentaire temporaire (il faut au moins cfitsio 2204)
203 //else if(datatype==TINT32BIT) strcpy(str,"1J");
[1660]204 else if(datatype==TLONG) strcpy(str,"1J");
[1657]205 else if(datatype==TFLOAT) strcpy(str,"1E");
206 else if(datatype==TDOUBLE) strcpy(str,"1D");
[1660]207 else if(datatype==TUSHORT) strcpy(str,"1U");
208 else if(datatype==TULONG) strcpy(str,"1V");
209 else
210 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
[1654]211 TForm.push_back(str);
[1660]212 } else {
213 if(tform) TForm.push_back(tform); else TForm.push_back("");
214 datatype = 0;
215 }
216 Label.push_back(label);
217 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
[1654]218
219 int n = (int) Label.size() -1;
220
221 if(DbgLevel)
222 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
223 <<" TForm="<<TForm[n]
[1660]224 <<" TUnit="<<TUnit[n]
225 <<" datatype="<<datatype<<endl;
[1654]226
227 return n;
228}
229
230/*! Create the table. Done at the first write request. */
231void FitsABTWriter::createtbl(void)
232{
233 if(!FirstTime) return;
234
235 int tfields = Label.size();
236 if(tfields<=0)
237 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
238
239 long nrows = 0;
240 char *extname = NULL;
241 char **ttype = (char **) malloc(tfields*sizeof(char *));
242 char **tform = (char **) malloc(tfields*sizeof(char *));
243 char **tunit = (char **) malloc(tfields*sizeof(char *));
244
245 if(ExtName.size()>0) {
246 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
247 strcpy(extname,ExtName.c_str());
248 }
[1664]249 int i;
250 for(i=0;i<tfields;i++) {
[1654]251 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
252 strcpy(ttype[i],Label[i].c_str());
253 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
254 strcpy(tform[i],TForm[i].c_str());
255 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
256 strcpy(tunit[i],TUnit[i].c_str());
257 }
258
259 // append a new empty binary/ascii table onto the FITS file
260 int sta=0;
261 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
262 printerror(sta);
263 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
264 }
265
[1814]266 // Append Fits key
267 writekeys();
268
[1654]269 // menage
270 if(extname) delete [] extname;
[1664]271 for(i=0;i<tfields;i++) {
[1654]272 if(ttype[i]) delete [] ttype[i];
273 if(tform[i]) delete [] tform[i];
274 if(tunit[i]) delete [] tunit[i];
275 }
276 if(ttype) delete [] ttype;
277 if(tform) delete [] tform;
278 if(tunit) delete [] tunit;
279
280 FirstTime = false;
281}
282
283//////////////////////////////////////////////////////////////
284/*!
285 Write a short data to FITS file.
[1659]286 \param col : column number [0,ncol[
287 \param row : row number [0,nrow[
288 \param val : value to be written
289 \warning that routine write a SHORT value into column "col"
[1654]290 which could have been defined with an other type.
291 Cast is performed by the cfitsio package.
[1659]292 \verbatim
[1654]293 WARNING: suppose that the column has be defined to be TSHORT
[1660]294 and suppose that you wanted to write a double value "val"
295 - If you write dummy.Write(col,row,(short)(val))
296 the Write(int,long,short) routine is called and
[1654]297 the cast is performed by the C++ language.
298 - If you write dummy.Write(col,row,val) where val is double
[1660]299 the Write(int,long,double) routine is called and
[1654]300 the cast is performed by the cfistio package.
301 \endverbatim
302*/
303void FitsABTWriter::Write(int col,long row,short val)
304{
305 if(FirstTime) createtbl();
306 int sta=0;
[1657]307 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
[1654]308 printerrorwrite("short",col,row,sta);
309}
310
311/*! Write long data to FITS file (see below) */
[1657]312void FitsABTWriter::Write(int col,long row,int_4 val)
[1654]313{
314 if(FirstTime) createtbl();
315 int sta=0;
[1657]316 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
317 int T = (sizeof(long)==4) ? TLONG: TINT;
318 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
[1654]319 printerrorwrite("long",col,row,sta);
320}
321
322/*! Write float data to FITS file (see below) */
323void FitsABTWriter::Write(int col,long row,float val)
324{
325 if(FirstTime) createtbl();
326 int sta=0;
[1657]327 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
[1654]328 printerrorwrite("float",col,row,sta);
329}
330
331/*! Write double data to FITS file (see below) */
332void FitsABTWriter::Write(int col,long row,double val)
333{
334 if(FirstTime) createtbl();
335 int sta=0;
[1657]336 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
[1654]337 printerrorwrite("double",col,row,sta);
338}
339
[1657]340//////////////////////////////////////////////////////////////
341/*!
342 Write a vector of long data to FITS file.
[1659]343 \param col : column number [0,ncol[
344 \param row : starting row number [0,nrow[
345 \param val : vector to be written
346 \return "N" = number of the next row to be written,
347 that is "N-1" is the number of the last row written.
[1657]348*/
349/*! Write a vector of long data to FITS file (see below) */
350long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
351{
352 if(FirstTime) createtbl();
353 long nel = val.Size();
354 int sta=0;
355 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
356 int T = (sizeof(long)==4) ? TLONG: TINT;
357 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
358 printerrorwrite("long",col,row,sta);
359 return row+nel;
360}
361
362/*! Write a vector of float data to FITS file (see below) */
363long FitsABTWriter::Write(int col,long row,TVector<float>& val)
364{
365 if(FirstTime) createtbl();
366 long nel = val.Size();
367 int sta=0;
368 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
369 printerrorwrite("float",col,row,sta);
370 return row+nel;
371}
372
373/*! Write a vector of double data to FITS file (see below) */
374long FitsABTWriter::Write(int col,long row,TVector<double>& val)
375{
376 if(FirstTime) createtbl();
377 long nel = val.Size();
378 int sta=0;
379 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
380 printerrorwrite("double",col,row,sta);
381 return row+nel;
382}
383
384//////////////////////////////////////////////////////////////
[1660]385void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
[1654]386{
387 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
388 printerror(sta);
389 char str[256];
390 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
391 ,type,col,row,sta);
392 throw NotAvailableOperation(str);
393}
394
395/////////////////////////////////////////////////
396void FitsABTWriter::printerror(int sta) const
397{
398 int stat = sta;
399 fits_report_error(stdout,stat);
400 fflush(stdout);
401 return;
402}
[1673]403
404/////////////////////////////////////////////////
[1677]405void FitsABTWriter::Print(ostream& os,int lp) const
[1673]406{
407os<<"FitsABTWriter::Print: FitsFN "<<FitsFN<<endl
408 <<" HduType "<<HduType<<" NOverFlow "<<NOverFlow
409 <<" NCol "<<Label.size()<<endl;
410if(Label.size()>0 && lp>=1)
411 for(int i=0;i<(int)Label.size();i++)
412 os<<i<<"... Label="<<Label[i]<<" TForm="<<TForm[i]<<" TUnit="<<TUnit[i]<<endl;
413}
Note: See TracBrowser for help on using the repository browser.