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

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

je vire TINT32BIT en attendant cfitsio2204 sur toutes machines cmv 1/9/01

File size: 10.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 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
114 FitsPtr = NULL;
115}
116
117//////////////////////////////////////////////////////////////
118/*!
119 Add a new column to the FITS table
120 \param label : column label
121 \param tform : fits tform definition ("1I","1J","1E","1J",...)
122 (can be automatically set as "datatype"
123 if BINARY_TBL and tform="" or tform=NULL)
124 \param tunit : fits tunit definition (optional)
125 \param datatype : TBYTE TSHORT TLONG (TINT32BIT) TFLOAT TDOUBLE
126 TUSHORT TULONG. That parameter is only use in case
127 of a BINARY_TBL table when tform is not defined).
128 \return The number of the new added column in the table.
129 \warning col = [0,ncol-1]
130*/
131int FitsABTWriter::addcol(const char* label,const char* tform
132 ,const char* tunit,int datatype)
133{
134 if(!FirstTime)
135 throw AllocationError("FitsABTWriter::addcol: table already created\n");
136
137 // Gestion auto du tform pour les tables binaires avec le datatype (si non-definie)
138 bool tformauto = false;
139 if(HduType==BINARY_TBL) {
140 if(tform==NULL) tformauto = true;
141 if(strlen(tform)<=0) tformauto = true;
142 }
143 if(tformauto) {
144 char str[8];
145 if(datatype==TBYTE) strcpy(str,"1B");
146 else if(datatype==TSHORT) strcpy(str,"1I");
147 //CMV$CHECK Commentaire temporaire (il faut au moins cfitsio 2204)
148 //else if(datatype==TINT32BIT) strcpy(str,"1J");
149 else if(datatype==TLONG) strcpy(str,"1J");
150 else if(datatype==TFLOAT) strcpy(str,"1E");
151 else if(datatype==TDOUBLE) strcpy(str,"1D");
152 else if(datatype==TUSHORT) strcpy(str,"1U");
153 else if(datatype==TULONG) strcpy(str,"1V");
154 else
155 throw ParmError("FitsABTWriter::addcol: datatype not allowed\n");
156 TForm.push_back(str);
157 } else {
158 if(tform) TForm.push_back(tform); else TForm.push_back("");
159 datatype = 0;
160 }
161 Label.push_back(label);
162 if(tunit) TUnit.push_back(tunit); else TUnit.push_back("");
163
164 int n = (int) Label.size() -1;
165
166 if(DbgLevel)
167 cout<<"FitsABTWriter::addcol["<<n<<"] Label="<<Label[n]
168 <<" TForm="<<TForm[n]
169 <<" TUnit="<<TUnit[n]
170 <<" datatype="<<datatype<<endl;
171
172 return n;
173}
174
175/*! Create the table. Done at the first write request. */
176void FitsABTWriter::createtbl(void)
177{
178 if(!FirstTime) return;
179
180 int tfields = Label.size();
181 if(tfields<=0)
182 throw ParmError("FitsABTWriter::createtbl: Zero column asked !\n");
183
184 long nrows = 0;
185 char *extname = NULL;
186 char **ttype = (char **) malloc(tfields*sizeof(char *));
187 char **tform = (char **) malloc(tfields*sizeof(char *));
188 char **tunit = (char **) malloc(tfields*sizeof(char *));
189
190 if(ExtName.size()>0) {
191 extname = (char *) malloc((strlen(ExtName.c_str())+1)*sizeof(char));
192 strcpy(extname,ExtName.c_str());
193 }
194 for(int i=0;i<tfields;i++) {
195 ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
196 strcpy(ttype[i],Label[i].c_str());
197 tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
198 strcpy(tform[i],TForm[i].c_str());
199 tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
200 strcpy(tunit[i],TUnit[i].c_str());
201 }
202
203 // append a new empty binary/ascii table onto the FITS file
204 int sta=0;
205 if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
206 printerror(sta);
207 throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
208 }
209
210 // menage
211 if(extname) delete [] extname;
212 for(int i=0;i<tfields;i++) {
213 if(ttype[i]) delete [] ttype[i];
214 if(tform[i]) delete [] tform[i];
215 if(tunit[i]) delete [] tunit[i];
216 }
217 if(ttype) delete [] ttype;
218 if(tform) delete [] tform;
219 if(tunit) delete [] tunit;
220
221 FirstTime = false;
222}
223
224//////////////////////////////////////////////////////////////
225/*!
226 Write a short data to FITS file.
227 \param col : column number [0,ncol[
228 \param row : row number [0,nrow[
229 \param val : value to be written
230 \warning that routine write a SHORT value into column "col"
231 which could have been defined with an other type.
232 Cast is performed by the cfitsio package.
233 \verbatim
234 WARNING: suppose that the column has be defined to be TSHORT
235 and suppose that you wanted to write a double value "val"
236 - If you write dummy.Write(col,row,(short)(val))
237 the Write(int,long,short) routine is called and
238 the cast is performed by the C++ language.
239 - If you write dummy.Write(col,row,val) where val is double
240 the Write(int,long,double) routine is called and
241 the cast is performed by the cfistio package.
242 \endverbatim
243*/
244void FitsABTWriter::Write(int col,long row,short val)
245{
246 if(FirstTime) createtbl();
247 int sta=0;
248 if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
249 printerrorwrite("short",col,row,sta);
250}
251
252/*! Write long data to FITS file (see below) */
253void FitsABTWriter::Write(int col,long row,int_4 val)
254{
255 if(FirstTime) createtbl();
256 int sta=0;
257 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
258 int T = (sizeof(long)==4) ? TLONG: TINT;
259 if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
260 printerrorwrite("long",col,row,sta);
261}
262
263/*! Write float data to FITS file (see below) */
264void FitsABTWriter::Write(int col,long row,float val)
265{
266 if(FirstTime) createtbl();
267 int sta=0;
268 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
269 printerrorwrite("float",col,row,sta);
270}
271
272/*! Write double data to FITS file (see below) */
273void FitsABTWriter::Write(int col,long row,double val)
274{
275 if(FirstTime) createtbl();
276 int sta=0;
277 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
278 printerrorwrite("double",col,row,sta);
279}
280
281//////////////////////////////////////////////////////////////
282/*!
283 Write a vector of long data to FITS file.
284 \param col : column number [0,ncol[
285 \param row : starting row number [0,nrow[
286 \param val : vector to be written
287 \return "N" = number of the next row to be written,
288 that is "N-1" is the number of the last row written.
289*/
290/*! Write a vector of long data to FITS file (see below) */
291long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
292{
293 if(FirstTime) createtbl();
294 long nel = val.Size();
295 int sta=0;
296 // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
297 int T = (sizeof(long)==4) ? TLONG: TINT;
298 if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
299 printerrorwrite("long",col,row,sta);
300 return row+nel;
301}
302
303/*! Write a vector of float data to FITS file (see below) */
304long FitsABTWriter::Write(int col,long row,TVector<float>& val)
305{
306 if(FirstTime) createtbl();
307 long nel = val.Size();
308 int sta=0;
309 if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
310 printerrorwrite("float",col,row,sta);
311 return row+nel;
312}
313
314/*! Write a vector of double data to FITS file (see below) */
315long FitsABTWriter::Write(int col,long row,TVector<double>& val)
316{
317 if(FirstTime) createtbl();
318 long nel = val.Size();
319 int sta=0;
320 if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
321 printerrorwrite("double",col,row,sta);
322 return row+nel;
323}
324
325//////////////////////////////////////////////////////////////
326void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
327{
328 if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
329 printerror(sta);
330 char str[256];
331 sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
332 ,type,col,row,sta);
333 throw NotAvailableOperation(str);
334}
335
336/////////////////////////////////////////////////
337void FitsABTWriter::printerror(int sta) const
338{
339 int stat = sta;
340 fits_report_error(stdout,stat);
341 fflush(stdout);
342 return;
343}
Note: See TracBrowser for help on using the repository browser.