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 | */
|
---|
62 | FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
|
---|
63 | {
|
---|
64 | createfits(fname.c_str(),hdutype,lp);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /*! See FitsABTWriter() */
|
---|
68 | FitsABTWriter::FitsABTWriter(const char* cfname,int hdutype,int lp)
|
---|
69 | {
|
---|
70 | createfits(cfname,hdutype,lp);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*! See FitsABTWriter() */
|
---|
74 | void 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 */
|
---|
110 | FitsABTWriter::~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 | */
|
---|
131 | int 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. */
|
---|
176 | void 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 | int i;
|
---|
195 | for(i=0;i<tfields;i++) {
|
---|
196 | ttype[i] = (char *) malloc((strlen(Label[i].c_str())+1)*sizeof(char));
|
---|
197 | strcpy(ttype[i],Label[i].c_str());
|
---|
198 | tform[i] = (char *) malloc((strlen(TForm[i].c_str())+1)*sizeof(char));
|
---|
199 | strcpy(tform[i],TForm[i].c_str());
|
---|
200 | tunit[i] = (char *) malloc((strlen(TUnit[i].c_str())+1)*sizeof(char));
|
---|
201 | strcpy(tunit[i],TUnit[i].c_str());
|
---|
202 | }
|
---|
203 |
|
---|
204 | // append a new empty binary/ascii table onto the FITS file
|
---|
205 | int sta=0;
|
---|
206 | if(fits_create_tbl(FitsPtr,HduType,nrows,tfields,ttype,tform,tunit,extname,&sta)) {
|
---|
207 | printerror(sta);
|
---|
208 | throw NullPtrError("FitsABTWriter::createtbl: Error creating Table extension\n");
|
---|
209 | }
|
---|
210 |
|
---|
211 | // menage
|
---|
212 | if(extname) delete [] extname;
|
---|
213 | for(i=0;i<tfields;i++) {
|
---|
214 | if(ttype[i]) delete [] ttype[i];
|
---|
215 | if(tform[i]) delete [] tform[i];
|
---|
216 | if(tunit[i]) delete [] tunit[i];
|
---|
217 | }
|
---|
218 | if(ttype) delete [] ttype;
|
---|
219 | if(tform) delete [] tform;
|
---|
220 | if(tunit) delete [] tunit;
|
---|
221 |
|
---|
222 | FirstTime = false;
|
---|
223 | }
|
---|
224 |
|
---|
225 | //////////////////////////////////////////////////////////////
|
---|
226 | /*!
|
---|
227 | Write a short data to FITS file.
|
---|
228 | \param col : column number [0,ncol[
|
---|
229 | \param row : row number [0,nrow[
|
---|
230 | \param val : value to be written
|
---|
231 | \warning that routine write a SHORT value into column "col"
|
---|
232 | which could have been defined with an other type.
|
---|
233 | Cast is performed by the cfitsio package.
|
---|
234 | \verbatim
|
---|
235 | WARNING: suppose that the column has be defined to be TSHORT
|
---|
236 | and suppose that you wanted to write a double value "val"
|
---|
237 | - If you write dummy.Write(col,row,(short)(val))
|
---|
238 | the Write(int,long,short) routine is called and
|
---|
239 | the cast is performed by the C++ language.
|
---|
240 | - If you write dummy.Write(col,row,val) where val is double
|
---|
241 | the Write(int,long,double) routine is called and
|
---|
242 | the cast is performed by the cfistio package.
|
---|
243 | \endverbatim
|
---|
244 | */
|
---|
245 | void FitsABTWriter::Write(int col,long row,short val)
|
---|
246 | {
|
---|
247 | if(FirstTime) createtbl();
|
---|
248 | int sta=0;
|
---|
249 | if(fits_write_col(FitsPtr,TSHORT,col+1,row+1,1,1,&val,&sta))
|
---|
250 | printerrorwrite("short",col,row,sta);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*! Write long data to FITS file (see below) */
|
---|
254 | void FitsABTWriter::Write(int col,long row,int_4 val)
|
---|
255 | {
|
---|
256 | if(FirstTime) createtbl();
|
---|
257 | int sta=0;
|
---|
258 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
259 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
260 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,1,&val,&sta))
|
---|
261 | printerrorwrite("long",col,row,sta);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /*! Write float data to FITS file (see below) */
|
---|
265 | void FitsABTWriter::Write(int col,long row,float val)
|
---|
266 | {
|
---|
267 | if(FirstTime) createtbl();
|
---|
268 | int sta=0;
|
---|
269 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,1,&val,&sta))
|
---|
270 | printerrorwrite("float",col,row,sta);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*! Write double data to FITS file (see below) */
|
---|
274 | void FitsABTWriter::Write(int col,long row,double val)
|
---|
275 | {
|
---|
276 | if(FirstTime) createtbl();
|
---|
277 | int sta=0;
|
---|
278 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,1,&val,&sta))
|
---|
279 | printerrorwrite("double",col,row,sta);
|
---|
280 | }
|
---|
281 |
|
---|
282 | //////////////////////////////////////////////////////////////
|
---|
283 | /*!
|
---|
284 | Write a vector of long data to FITS file.
|
---|
285 | \param col : column number [0,ncol[
|
---|
286 | \param row : starting row number [0,nrow[
|
---|
287 | \param val : vector to be written
|
---|
288 | \return "N" = number of the next row to be written,
|
---|
289 | that is "N-1" is the number of the last row written.
|
---|
290 | */
|
---|
291 | /*! Write a vector of long data to FITS file (see below) */
|
---|
292 | long FitsABTWriter::Write(int col,long row,TVector<int_4>& val)
|
---|
293 | {
|
---|
294 | if(FirstTime) createtbl();
|
---|
295 | long nel = val.Size();
|
---|
296 | int sta=0;
|
---|
297 | // Bug ou inconsistence cfitsio sur machine ou long=8Bytes ?
|
---|
298 | int T = (sizeof(long)==4) ? TLONG: TINT;
|
---|
299 | if(fits_write_col(FitsPtr,T,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
300 | printerrorwrite("long",col,row,sta);
|
---|
301 | return row+nel;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*! Write a vector of float data to FITS file (see below) */
|
---|
305 | long FitsABTWriter::Write(int col,long row,TVector<float>& val)
|
---|
306 | {
|
---|
307 | if(FirstTime) createtbl();
|
---|
308 | long nel = val.Size();
|
---|
309 | int sta=0;
|
---|
310 | if(fits_write_col(FitsPtr,TFLOAT,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
311 | printerrorwrite("float",col,row,sta);
|
---|
312 | return row+nel;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /*! Write a vector of double data to FITS file (see below) */
|
---|
316 | long FitsABTWriter::Write(int col,long row,TVector<double>& val)
|
---|
317 | {
|
---|
318 | if(FirstTime) createtbl();
|
---|
319 | long nel = val.Size();
|
---|
320 | int sta=0;
|
---|
321 | if(fits_write_col(FitsPtr,TDOUBLE,col+1,row+1,1,nel,val.Data(),&sta))
|
---|
322 | printerrorwrite("double",col,row,sta);
|
---|
323 | return row+nel;
|
---|
324 | }
|
---|
325 |
|
---|
326 | //////////////////////////////////////////////////////////////
|
---|
327 | void FitsABTWriter::printerrorwrite(const char* type,int col,long row,int sta)
|
---|
328 | {
|
---|
329 | if(sta==NUM_OVERFLOW) {NOverFlow++; return;}
|
---|
330 | printerror(sta);
|
---|
331 | char str[256];
|
---|
332 | sprintf(str,"FitsABTWriter::Write_%s: Error Writing Fits c=%d r=%ld sta=%d"
|
---|
333 | ,type,col,row,sta);
|
---|
334 | throw NotAvailableOperation(str);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /////////////////////////////////////////////////
|
---|
338 | void FitsABTWriter::printerror(int sta) const
|
---|
339 | {
|
---|
340 | int stat = sta;
|
---|
341 | fits_report_error(stdout,stat);
|
---|
342 | fflush(stdout);
|
---|
343 | return;
|
---|
344 | }
|
---|