1 | #include "fitsfile.h"
|
---|
2 | #include "pexceptions.h"
|
---|
3 | #include "strutil.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | FitsFile::FitsFile()
|
---|
7 | {
|
---|
8 | fptr_= NULL;
|
---|
9 | hdutype_= 0;
|
---|
10 | hdunum_ = 0;
|
---|
11 |
|
---|
12 | nrows_ = 0;
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | FitsFile::~FitsFile()
|
---|
17 | {
|
---|
18 | int status = 0;
|
---|
19 | if( fptr_ != NULL) fits_close_file(fptr_,&status);
|
---|
20 | if( status ) printerror( status );
|
---|
21 | }
|
---|
22 | void FitsFile::ReadF(char flnm[],int hdunum)
|
---|
23 | {
|
---|
24 | int status = 0;
|
---|
25 | hdutype_= 0;
|
---|
26 | hdunum_ = 0;
|
---|
27 | fits_open_file(&fptr_,flnm,READONLY,&status);
|
---|
28 | if( status ) printerror( status );
|
---|
29 |
|
---|
30 | // move to the specified HDU number
|
---|
31 | int hdutype;
|
---|
32 | fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
|
---|
33 | if( status ) printerror( status );
|
---|
34 |
|
---|
35 | hdutype_= hdutype;
|
---|
36 | hdunum_ = hdunum;
|
---|
37 | if(hdutype_ == IMAGE_HDU)
|
---|
38 | {
|
---|
39 | read_image();
|
---|
40 | }
|
---|
41 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
42 | {
|
---|
43 | GetBinTabParameters();
|
---|
44 | }
|
---|
45 | ReadFromFits(*this);
|
---|
46 |
|
---|
47 | }
|
---|
48 | void FitsFile::WriteF(char flnm[],int hdunum)
|
---|
49 | {
|
---|
50 | int status = 0;
|
---|
51 | hdutype_= 0;
|
---|
52 | hdunum_ = hdunum;
|
---|
53 | // create new FITS file
|
---|
54 | fits_create_file(&fptr_,flnm,&status);
|
---|
55 | if( status ) printerror(status,"file already exists");
|
---|
56 | WriteToFits(*this);
|
---|
57 | }
|
---|
58 | void FitsFile::GetSingleColumn(double* map, int nentries) const
|
---|
59 | {
|
---|
60 | int status = 0;
|
---|
61 | if(hdutype_ == IMAGE_HDU)
|
---|
62 | {
|
---|
63 |
|
---|
64 | if(bitpix_ != DOUBLE_IMG)
|
---|
65 | {
|
---|
66 | cout << " The data type on fits file is not double...";
|
---|
67 | cout << " Conversion to double achieved by cfitsio lib" << endl;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // no checking for undefined pixels
|
---|
71 | int anull;
|
---|
72 | double dnull= 0.;
|
---|
73 |
|
---|
74 | long nels= nentries;
|
---|
75 | fits_read_img(fptr_,TDOUBLE,1,nels,&dnull,map,&anull,&status);
|
---|
76 | if( status ) printerror( status );
|
---|
77 | }
|
---|
78 | else
|
---|
79 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
80 | {
|
---|
81 | GetBinTabFCol(map,nentries, 0);
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | cout << " hdutype= " << hdutype_ << endl;
|
---|
86 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void FitsFile::GetSingleColumn(float* map, int nentries) const
|
---|
91 | {
|
---|
92 | int status = 0;
|
---|
93 | if(hdutype_ == IMAGE_HDU)
|
---|
94 | {
|
---|
95 | if(bitpix_ != FLOAT_IMG)
|
---|
96 | {
|
---|
97 | cout << " The data type on fits file is not float ";
|
---|
98 | cout << " Conversion to float achieved by cfitsio lib" << endl;
|
---|
99 | }
|
---|
100 | // no checking for undefined pixels
|
---|
101 | int anull;
|
---|
102 | float fnull= 0.;
|
---|
103 |
|
---|
104 | long nels= nentries;
|
---|
105 | fits_read_img(fptr_,TFLOAT,1,nels,&fnull, map,&anull,&status);
|
---|
106 | if( status ) printerror( status );
|
---|
107 | }
|
---|
108 | else
|
---|
109 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
110 | {
|
---|
111 | GetBinTabFCol(map,nentries, 0);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | cout << " hdutype= " << hdutype_ << endl;
|
---|
116 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | void FitsFile::GetSingleColumn( int* map, int nentries) const
|
---|
121 | {
|
---|
122 | int status = 0;
|
---|
123 | if(hdutype_ == IMAGE_HDU)
|
---|
124 | {
|
---|
125 | if(bitpix_ != LONG_IMG)
|
---|
126 | {
|
---|
127 | cout << " The data type on fits file is not int ";
|
---|
128 | cout << " Conversion to float achieved by cfitsio lib" << endl;
|
---|
129 | }
|
---|
130 | // no checking for undefined pixels
|
---|
131 | int anull;
|
---|
132 | float fnull= 0.;
|
---|
133 |
|
---|
134 | long nels= nentries;
|
---|
135 | fits_read_img(fptr_,TINT,1,nels,&fnull,map,&anull,&status);
|
---|
136 | if( status ) printerror( status );
|
---|
137 | }
|
---|
138 | else
|
---|
139 | if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
|
---|
140 | {
|
---|
141 | GetBinTabFCol(map,nentries, 0);
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | cout << " hdutype= " << hdutype_ << endl;
|
---|
146 | throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
|
---|
147 | }
|
---|
148 | }
|
---|
149 | void FitsFile::makeHeaderImageOnFits(char type, int naxis1,int naxis2,int naxis3) const
|
---|
150 | {
|
---|
151 | int status = 0;
|
---|
152 | long naxis = 3;
|
---|
153 | if (naxis3 <=1)
|
---|
154 | {
|
---|
155 | naxis--;
|
---|
156 | if (naxis2 <=1) naxis--;
|
---|
157 | }
|
---|
158 | long naxes[3];
|
---|
159 | naxes[0]= naxis1;
|
---|
160 | naxes[1]= naxis2;
|
---|
161 | naxes[2]= naxis3;
|
---|
162 | if (type == 'D')
|
---|
163 | fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
|
---|
164 | else
|
---|
165 | if (type == 'E')
|
---|
166 | fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
|
---|
167 | else
|
---|
168 | if (type == 'I')
|
---|
169 | fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
|
---|
170 | else
|
---|
171 | {
|
---|
172 | cout << " type of data: " << type << endl;
|
---|
173 | throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
|
---|
174 | }
|
---|
175 | if( status ) printerror( status );
|
---|
176 |
|
---|
177 | }
|
---|
178 | void FitsFile::putImageToFits(int nbData, double* map) const
|
---|
179 | {
|
---|
180 | int status = 0;
|
---|
181 | long npix= nbData;
|
---|
182 | fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
|
---|
183 | if( status ) printerror( status );
|
---|
184 | writeSignatureOnFits();
|
---|
185 | }
|
---|
186 |
|
---|
187 | void FitsFile::putImageToFits(int nbData, float* map) const
|
---|
188 | {
|
---|
189 | int status = 0;
|
---|
190 | long npix= nbData;
|
---|
191 | fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
|
---|
192 | if( status ) printerror( status );
|
---|
193 | writeSignatureOnFits();
|
---|
194 |
|
---|
195 | }
|
---|
196 | void FitsFile::putImageToFits( int nbData, int* map) const
|
---|
197 | {
|
---|
198 | int status = 0;
|
---|
199 |
|
---|
200 | long npix= nbData;
|
---|
201 | fits_write_img(fptr_,TINT,1,npix,map,&status);
|
---|
202 | if( status ) printerror( status );
|
---|
203 | writeSignatureOnFits();
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | void FitsFile::read_image()
|
---|
208 | {
|
---|
209 | cout << " Reading a FITS image in HDU : " << hdunum_ << endl;
|
---|
210 | int status= 0;
|
---|
211 |
|
---|
212 | // bits per pixels
|
---|
213 | fits_read_key(fptr_,TINT,"BITPIX",&bitpix_,NULL,&status);
|
---|
214 | if( status ) printerror( status );
|
---|
215 |
|
---|
216 | // number of dimensions in the FITS array
|
---|
217 | int naxis= 0;
|
---|
218 | fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
|
---|
219 | if( status ) printerror( status );
|
---|
220 | naxis_ = naxis;
|
---|
221 |
|
---|
222 | // read the NAXIS1 and NAXIS2 keyword to get image size
|
---|
223 | long naxes[3]= {0,0,0};
|
---|
224 | int nfound;
|
---|
225 | fits_read_keys_lng(fptr_,"NAXIS",1,naxis_,naxes,&nfound,&status);
|
---|
226 | if( status ) printerror( status );
|
---|
227 |
|
---|
228 | naxis1_ = (int)naxes[0];
|
---|
229 | naxis2_ = (int)naxes[1];
|
---|
230 | naxis3_ = (int)naxes[2];
|
---|
231 |
|
---|
232 | if(naxis_ == 3 && naxis3_ > 1)
|
---|
233 | {
|
---|
234 | cout << "multi-dimensional image =" << naxis << endl;
|
---|
235 | cout << "length Z axis = " << naxis3_ << endl;
|
---|
236 | throw IOExc("FitsFile::put_fits_image %%Expected 1-D data or 2-D data");
|
---|
237 | }
|
---|
238 |
|
---|
239 | nbData_ = naxis1_;
|
---|
240 | if(naxis >= 2) nbData_*= naxis2_;
|
---|
241 | if(naxis == 3) nbData_*= naxis3_;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int FitsFile::nbDimOfImage() const
|
---|
245 | {
|
---|
246 | int ndim=3;
|
---|
247 | if (naxis_ <3 || naxis3_ <= 1) ndim-- ;
|
---|
248 | if (naxis_ <2 || (naxis3_ <= 1 && naxis2_ <= 1) ) ndim--;
|
---|
249 | return ndim;
|
---|
250 | }
|
---|
251 |
|
---|
252 | void FitsFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
|
---|
253 | {
|
---|
254 | cout << "entree getbintablefcol double" << endl;
|
---|
255 | int status= 0;
|
---|
256 | int DTYPE;
|
---|
257 | long repeat,width;
|
---|
258 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
259 | if( DTYPE != TDOUBLE)
|
---|
260 | {
|
---|
261 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
|
---|
262 | }
|
---|
263 | long nels=nentries;
|
---|
264 | // no checking for undefined pixels
|
---|
265 | int anull;
|
---|
266 | float dnull= 0.;
|
---|
267 | cout << " avant lecture colonne, nels= " << nels << endl;
|
---|
268 | fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
|
---|
269 | &anull,&status);
|
---|
270 | cout << " colonne lue" << endl;
|
---|
271 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
272 | }
|
---|
273 |
|
---|
274 | void FitsFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
|
---|
275 | {
|
---|
276 | cout << "entree getbintablefcol float" << endl;
|
---|
277 | int status= 0;
|
---|
278 | int DTYPE;
|
---|
279 | long repeat,width;
|
---|
280 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
281 | if( DTYPE != TFLOAT)
|
---|
282 | {
|
---|
283 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
|
---|
284 | }
|
---|
285 | long nels=nentries;
|
---|
286 | // no checking for undefined pixels
|
---|
287 | int anull;
|
---|
288 | float fnull= 0.;
|
---|
289 | fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
|
---|
290 | &anull,&status);
|
---|
291 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
292 | }
|
---|
293 | void FitsFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
|
---|
294 | {
|
---|
295 | cout << "entree getbintablefcol int" << endl;
|
---|
296 | int status= 0;
|
---|
297 | int DTYPE;
|
---|
298 | long repeat,width;
|
---|
299 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
300 | if( DTYPE != TLONG && DTYPE != TINT && DTYPE != TSHORT )
|
---|
301 | {
|
---|
302 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
|
---|
303 | }
|
---|
304 | long nels=nentries;
|
---|
305 | // no checking for undefined pixels
|
---|
306 | int anull;
|
---|
307 | int inull= 0;
|
---|
308 | fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
|
---|
309 | &anull,&status);
|
---|
310 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
311 | cout << " tableau d'entiers relu: " << endl;
|
---|
312 | for (int ijk=0; ijk<nels;ijk++) cout << valeurs[ijk] << endl;
|
---|
313 | }
|
---|
314 | void FitsFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
|
---|
315 | {
|
---|
316 | cout << "entree getbintablefcol string" << endl;
|
---|
317 | int status= 0;
|
---|
318 | int DTYPE;
|
---|
319 | long repeat,width;
|
---|
320 | fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
|
---|
321 | if( DTYPE != TSTRING )
|
---|
322 | {
|
---|
323 | throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
|
---|
324 | }
|
---|
325 | cout << " repeat= " << repeat << " width= " << width << endl;
|
---|
326 | long nels=nentries;
|
---|
327 | // no checking for undefined pixels
|
---|
328 | int anull;
|
---|
329 | char* cnull= " ";
|
---|
330 | long frow=1;
|
---|
331 | long felem=1;
|
---|
332 | cout << " nb elements a lire" << nels << endl;
|
---|
333 | cout << " taille element tableau " << strlen(valeurs[0]) << endl;
|
---|
334 | cout << " numero de colonne a lire: " << NoCol+1 << endl;
|
---|
335 | fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
|
---|
336 | &anull,&status);
|
---|
337 | if( status ) printerror( status,"erreur lecture de colonne" );
|
---|
338 | cout << " fin lecture chaines" << endl;
|
---|
339 | }
|
---|
340 | int FitsFile::NbColsFromFits() const
|
---|
341 | {
|
---|
342 | int status= 0;
|
---|
343 | if(hdutype_ == BINARY_TBL) return nbcols_;
|
---|
344 | else
|
---|
345 | if(hdutype_ == ASCII_TBL || hdutype_ == IMAGE_HDU) return 1;
|
---|
346 | else
|
---|
347 | {
|
---|
348 | cout << " hdutype= " << hdutype_ << endl;
|
---|
349 | throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | char FitsFile::ColTypeFromFits(int nocol) const
|
---|
354 | {
|
---|
355 | int status= 0;
|
---|
356 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
357 | {
|
---|
358 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
359 | }
|
---|
360 | return types_[nocol];
|
---|
361 | }
|
---|
362 | int FitsFile::NentriesFromFits(int nocol) const
|
---|
363 | {
|
---|
364 | int status= 0;
|
---|
365 | if(hdutype_ == BINARY_TBL ) return nrows_*repeat_[nocol];
|
---|
366 | else
|
---|
367 | if(hdutype_ == ASCII_TBL) return nrows_;
|
---|
368 | else
|
---|
369 | if(hdutype_ == IMAGE_HDU) return nbData_;
|
---|
370 | else
|
---|
371 | {
|
---|
372 | cout << "hdutype= " << hdutype_ << endl;
|
---|
373 | throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | string FitsFile::ColNameFromFits(int nocol) const
|
---|
378 | {
|
---|
379 | int status= 0;
|
---|
380 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
381 | {
|
---|
382 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
383 | }
|
---|
384 | return noms_[nocol];
|
---|
385 | }
|
---|
386 | int FitsFile::ColStringLengthFromFits(int nocol) const
|
---|
387 | {
|
---|
388 | int status= 0;
|
---|
389 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
390 | {
|
---|
391 | throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
|
---|
392 | }
|
---|
393 | int index=-1;
|
---|
394 | for (int k=0; k<=nocol; k++)
|
---|
395 | {
|
---|
396 | if (types_[k] == 'S') index++;
|
---|
397 | }
|
---|
398 | return taille_des_chaines_[index];
|
---|
399 | }
|
---|
400 |
|
---|
401 | void FitsFile::GetBinTabParameters()
|
---|
402 | {
|
---|
403 | int status= 0;
|
---|
404 | if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
|
---|
405 | {
|
---|
406 | throw IOExc("FitsFile:: this HDU is not an ASCII table nor a binary table");
|
---|
407 | }
|
---|
408 | if(hdutype_ == ASCII_TBL)
|
---|
409 | cout << " Reading a FITS ascii table in HDU : " << hdunum_ << endl;
|
---|
410 | if(hdutype_ == BINARY_TBL)
|
---|
411 | cout << " Reading a FITS binary table in HDU : " << hdunum_ << endl;
|
---|
412 |
|
---|
413 | // get the number of columns
|
---|
414 | int nbcols = 0;
|
---|
415 | fits_get_num_cols(fptr_, &nbcols,&status);
|
---|
416 | if( status ) printerror( status );
|
---|
417 | nbcols_=nbcols;
|
---|
418 |
|
---|
419 | // get the number of rows
|
---|
420 | long naxis2= 0;
|
---|
421 | fits_get_num_rows(fptr_,&naxis2,&status);
|
---|
422 | if( status ) printerror( status );
|
---|
423 | nrows_= (int)naxis2;
|
---|
424 |
|
---|
425 | // get the datatype, names and the repeat count
|
---|
426 | noms_.clear();
|
---|
427 | noms_.reserve(nbcols);
|
---|
428 | types_.clear();
|
---|
429 | types_.reserve(nbcols);
|
---|
430 | repeat_.clear();
|
---|
431 | repeat_.reserve(nbcols);
|
---|
432 | taille_des_chaines_.clear();
|
---|
433 | char **ttype = new char*[nbcols];
|
---|
434 | for (int ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
|
---|
435 | int nfound;
|
---|
436 | fits_read_keys_str(fptr_, "TTYPE",1,nbcols,ttype,&nfound, &status);
|
---|
437 | if( status ) printerror( status,"erreur lecture des noms de colonne");
|
---|
438 | // int nentries = nrows_;
|
---|
439 | int rept=0;
|
---|
440 | for(int ii = 0; ii < nbcols; ii++)
|
---|
441 | {
|
---|
442 | int DTYPE;
|
---|
443 | long width;
|
---|
444 | long repeat = 0;
|
---|
445 | fits_get_coltype(fptr_,ii+1,&DTYPE,&repeat,&width,&status);
|
---|
446 | if( status ) printerror( status,"erreur lecture type de colonne");
|
---|
447 | rept = repeat;
|
---|
448 | noms_.push_back(string(ttype[ii]));
|
---|
449 | // cout << " getparam: nentries= " << nentries << " rept= " << rept << endl;
|
---|
450 | switch (DTYPE)
|
---|
451 | {
|
---|
452 | case TDOUBLE :
|
---|
453 | types_.push_back('D');
|
---|
454 | break;
|
---|
455 | case TFLOAT :
|
---|
456 | types_.push_back('E');
|
---|
457 | break;
|
---|
458 | case TLONG :
|
---|
459 | types_.push_back('I');
|
---|
460 | break;
|
---|
461 | case TINT :
|
---|
462 | types_.push_back('I');
|
---|
463 | break;
|
---|
464 | case TSHORT :
|
---|
465 | types_.push_back('I');
|
---|
466 | break;
|
---|
467 | case TSTRING :
|
---|
468 | types_.push_back('S');
|
---|
469 | taille_des_chaines_.push_back(width);
|
---|
470 | rept/=width;
|
---|
471 | cout << " j'ai lu taille_des_chaines= " << width << " rept= " << rept << endl;
|
---|
472 | break;
|
---|
473 | default :
|
---|
474 | cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
|
---|
475 | throw IOExc("FitsFile:: unknown type of field");
|
---|
476 | }
|
---|
477 | // nentries = max(nentries, nrows_*rept);
|
---|
478 | repeat_.push_back(rept);
|
---|
479 | cout << " getparam: repeat= " << repeat_[ii] << endl;
|
---|
480 | }
|
---|
481 | // get number of keywords
|
---|
482 | int nkeys,keypos;
|
---|
483 | fits_get_hdrpos(fptr_,&nkeys,&keypos,&status);
|
---|
484 | if( status ) printerror( status );
|
---|
485 |
|
---|
486 | // put keywords in a DVList object
|
---|
487 | char keyname[LEN_KEYWORD]= "";
|
---|
488 | char strval[FLEN_VALUE]= "";
|
---|
489 | char dtype;
|
---|
490 | char card[FLEN_CARD];
|
---|
491 | char *comkey = "COMMENT";
|
---|
492 |
|
---|
493 | // shift with the number of mandatory keywords
|
---|
494 | int num= 8;
|
---|
495 |
|
---|
496 | for(int j = num+1; j <= nkeys; j++)
|
---|
497 | {
|
---|
498 | fits_read_keyn(fptr_,j,card,strval,NULL,&status);
|
---|
499 | if(status) printerror(status);
|
---|
500 |
|
---|
501 | strncpy(keyname,card,LEN_KEYWORD-1);
|
---|
502 |
|
---|
503 | if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
|
---|
504 | && strlen(strval) != 0)
|
---|
505 | {
|
---|
506 | fits_get_keytype(strval,&dtype,&status);
|
---|
507 | if(status) printerror(status);
|
---|
508 |
|
---|
509 | strip(keyname, 'B',' ');
|
---|
510 | strip(strval, 'B',' ');
|
---|
511 | strip(strval, 'B','\'');
|
---|
512 |
|
---|
513 | switch( dtype )
|
---|
514 | {
|
---|
515 | case 'C':
|
---|
516 | dvl_[keyname]= strval;
|
---|
517 | break;
|
---|
518 | case 'I':
|
---|
519 | int ival;
|
---|
520 | fits_read_key(fptr_,TINT,keyname,&ival,NULL,&status);
|
---|
521 | dvl_[keyname]= (int_4) ival; // Portage mac DY
|
---|
522 | break;
|
---|
523 | case 'L':
|
---|
524 | int ilog;
|
---|
525 | if(strncmp(strval,"T",1) == 0) ilog= 1;
|
---|
526 | else ilog= 0;
|
---|
527 | dvl_[keyname]= (int_4) ilog;
|
---|
528 | break;
|
---|
529 | case 'F':
|
---|
530 | double dval;
|
---|
531 | fits_read_key(fptr_,TDOUBLE,keyname,&dval,NULL,&status);
|
---|
532 | dvl_[keyname]= dval;
|
---|
533 | break;
|
---|
534 | }
|
---|
535 |
|
---|
536 | }
|
---|
537 | }
|
---|
538 | cout << " fin lecture du dvlist : " << endl;
|
---|
539 | dvl_.Print();
|
---|
540 | }
|
---|
541 | void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) const
|
---|
542 | {
|
---|
543 | cout << " entree put_column_bntbl " << endl;
|
---|
544 | int status = 0;
|
---|
545 | long nrows;
|
---|
546 | cout << " nombre de types: " << strlen(fieldType) << endl;
|
---|
547 | if (strlen(fieldType) != tfields)
|
---|
548 | {
|
---|
549 | cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
|
---|
550 | throw ParmError("FitsFile:: fields and types don't match");
|
---|
551 |
|
---|
552 | }
|
---|
553 | char ** ttype= new char*[tfields];
|
---|
554 | char ** tform= new char*[tfields];
|
---|
555 | char largeur[FLEN_VALUE];
|
---|
556 | int noColString=0;
|
---|
557 |
|
---|
558 |
|
---|
559 | for (int k=0; k<tfields;k++)
|
---|
560 | {
|
---|
561 | char format[FLEN_VALUE];
|
---|
562 |
|
---|
563 | if(nentries < 1024)
|
---|
564 | {
|
---|
565 | nrows= nentries;
|
---|
566 | if (fieldType[k] == 'A')
|
---|
567 | {
|
---|
568 | sprintf(largeur,"%d",taille_des_chaines[noColString++]);
|
---|
569 | strcpy(format,largeur);
|
---|
570 | }
|
---|
571 | else strcpy(format,"1");
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | nrows = nentries/1024;
|
---|
576 | if(nentries%1024 != 0) nrows++;
|
---|
577 | if (fieldType[k] == 'A')
|
---|
578 | {
|
---|
579 | char largaux[FLEN_VALUE];
|
---|
580 | sprintf(largeur,"%d",taille_des_chaines[noColString]);
|
---|
581 | sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
|
---|
582 | noColString++;
|
---|
583 | strcpy(format, largaux);
|
---|
584 | }
|
---|
585 | else strcpy(format,"1024");
|
---|
586 | }
|
---|
587 | cout << " en ecriture fits : nrows= " << nrows << endl;
|
---|
588 | cout << " fitsfile : format= " << format << endl;
|
---|
589 | strncat(format,&fieldType[k],1);
|
---|
590 | if (fieldType[k] == 'A')
|
---|
591 | {
|
---|
592 | strcat(format,largeur);
|
---|
593 | }
|
---|
594 | cout << " fitsfile : format= " << format << endl;
|
---|
595 | ttype[k]= new char[FLEN_VALUE];
|
---|
596 | strcpy(ttype[k],Noms[k]);
|
---|
597 | tform[k]= new char[FLEN_VALUE];
|
---|
598 | strcpy(tform[k],format);
|
---|
599 | }
|
---|
600 | const char* TypeOfContent= dvl.GetS("TypeOfContent").c_str();
|
---|
601 | // value of the EXTNAME keyword
|
---|
602 | char extn[FLEN_VALUE];
|
---|
603 | strncpy(extn,extname,FLEN_VALUE);
|
---|
604 |
|
---|
605 | // create a new empty binary table onto the FITS file
|
---|
606 | // physical units if they exist, are defined in the DVList object
|
---|
607 | // so the NULL pointer is given for the tunit parameters.
|
---|
608 | nrows=0;
|
---|
609 | cout << " en ecriture fits : nrows avant create = " << nrows << endl;
|
---|
610 | fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
|
---|
611 | NULL,extn,&status);
|
---|
612 | if( status ) printerror( status );
|
---|
613 |
|
---|
614 | for(int ii = 0; ii < tfields; ii++)
|
---|
615 | {
|
---|
616 | delete [] ttype[ii];
|
---|
617 | delete [] tform[ii];
|
---|
618 | }
|
---|
619 | delete [] ttype;
|
---|
620 | delete [] tform;
|
---|
621 | //
|
---|
622 | // write supplementary keywords
|
---|
623 | //
|
---|
624 | // get names and values from the join DVList object
|
---|
625 | cout << " dvlist en ecriture..." << endl;
|
---|
626 | dvl.Print();
|
---|
627 | DVList::ValList::const_iterator it;
|
---|
628 | for(it = dvl.Begin(); it != dvl.End(); it++)
|
---|
629 | {
|
---|
630 | char keytype= (*it).second.elval.typ;
|
---|
631 | char keyname[10];
|
---|
632 | strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
|
---|
633 | char comment[FLEN_COMMENT];
|
---|
634 | cout << " keyname: " << keyname << " keytype: " << keytype << endl;
|
---|
635 | switch (keytype)
|
---|
636 | {
|
---|
637 | case 'I' :
|
---|
638 | {
|
---|
639 | int ival=(*it).second.elval.mtv.iv;
|
---|
640 | strcpy(comment,"I entier");
|
---|
641 | fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
|
---|
642 | cout << " keyname " << keyname << " = " << ival << " ecrit " << endl;
|
---|
643 | break;
|
---|
644 | }
|
---|
645 | case 'D' :
|
---|
646 | {
|
---|
647 | double dval=(*it).second.elval.mtv.dv;
|
---|
648 | strcpy(comment,"D double");
|
---|
649 | fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
|
---|
650 | cout << " keyname " << keyname << " = " << dval << " ecrit " << endl;
|
---|
651 | break;
|
---|
652 | }
|
---|
653 | case 'S' :
|
---|
654 | {
|
---|
655 | char strval[128];
|
---|
656 | strncpy(strval,(*it).second.elval.mtv.strv,127);
|
---|
657 | strcpy(comment,"S character string");
|
---|
658 | fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
|
---|
659 | cout << " keyname " << keyname << " = " << strval << " ecrit " << endl;
|
---|
660 | break;
|
---|
661 | }
|
---|
662 | }
|
---|
663 | if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
|
---|
664 | }
|
---|
665 |
|
---|
666 | cout << " fitsfile: le dvlist est ecrit" << endl;
|
---|
667 | }
|
---|
668 |
|
---|
669 | void FitsFile::putColToFits(int nocol, int nentries, double* donnees) const
|
---|
670 | {
|
---|
671 | int status = 0;
|
---|
672 | int hdutype, hdunum;
|
---|
673 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
674 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
675 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
676 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
677 | {
|
---|
678 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
679 | // throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
680 | }
|
---|
681 | // if(hdutype == ASCII_TBL && nocol>0)
|
---|
682 | // {
|
---|
683 | // throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
684 | // }
|
---|
685 | int code;
|
---|
686 | long repeat, width;
|
---|
687 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
688 | if( code != TDOUBLE)
|
---|
689 | {
|
---|
690 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
|
---|
691 | }
|
---|
692 | fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
|
---|
693 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
694 | }
|
---|
695 | void FitsFile::putColToFits(int nocol, int nentries, float* donnees) const
|
---|
696 | {
|
---|
697 | int status = 0;
|
---|
698 | int hdutype;
|
---|
699 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
700 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
701 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
702 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
703 | {
|
---|
704 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
705 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
706 | }
|
---|
707 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
708 | {
|
---|
709 | throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
710 | }
|
---|
711 | int code;
|
---|
712 | long repeat, width;
|
---|
713 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
714 | if( code != TFLOAT)
|
---|
715 | {
|
---|
716 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
|
---|
717 | }
|
---|
718 | fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
719 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
720 | }
|
---|
721 | void FitsFile::putColToFits(int nocol, int nentries, int* donnees) const
|
---|
722 | {
|
---|
723 | int status = 0;
|
---|
724 | int hdutype;
|
---|
725 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
726 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
727 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
728 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
729 | {
|
---|
730 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
731 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
732 | }
|
---|
733 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
734 | {
|
---|
735 | throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
736 | }
|
---|
737 | int code;
|
---|
738 | long repeat, width;
|
---|
739 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
740 | if( code != TLONG && code != TINT && code != TSHORT )
|
---|
741 | {
|
---|
742 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
|
---|
743 | }
|
---|
744 | fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
|
---|
745 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
746 | }
|
---|
747 | void FitsFile::putColToFits(int nocol, int nentries, char** donnees) const
|
---|
748 | {
|
---|
749 | int status = 0;
|
---|
750 | int hdutype;
|
---|
751 | fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
|
---|
752 | if( status ) printerror(status,"putColToFits: le movabs a foire");
|
---|
753 | fits_get_hdu_type(fptr_, &hdutype, &status);
|
---|
754 | if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
|
---|
755 | {
|
---|
756 | cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
|
---|
757 | throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
|
---|
758 | }
|
---|
759 | if(hdutype == ASCII_TBL && nocol>0)
|
---|
760 | {
|
---|
761 | throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
|
---|
762 | }
|
---|
763 | int code;
|
---|
764 | long repeat, width;
|
---|
765 | fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
|
---|
766 | if( code != TSTRING)
|
---|
767 | {
|
---|
768 | cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
|
---|
769 | }
|
---|
770 | fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
|
---|
771 | if( status ) printerror( status,"erreur ecriture du fichier fits" );
|
---|
772 | }
|
---|
773 |
|
---|
774 |
|
---|
775 |
|
---|
776 |
|
---|
777 | void FitsFile::readheader()
|
---|
778 | //*************************************/
|
---|
779 | //* Print out all the header keywords */
|
---|
780 | //*************************************/
|
---|
781 | {
|
---|
782 | // standard string lengths defined in fitsioc.h
|
---|
783 | char card[FLEN_CARD];
|
---|
784 |
|
---|
785 | int status = 0;
|
---|
786 |
|
---|
787 | // get the number of keywords
|
---|
788 | int nkeys, keypos;
|
---|
789 | if( fits_get_hdrpos(fptr_,&nkeys,&keypos,&status) )
|
---|
790 | printerror( status );
|
---|
791 |
|
---|
792 | cout << " Header listing for HDU : " << hdunum_ << endl;
|
---|
793 | for(int jj = 1; jj <= nkeys; jj++)
|
---|
794 | {
|
---|
795 | if( fits_read_record(fptr_,jj,card,&status) )
|
---|
796 | printerror( status );
|
---|
797 |
|
---|
798 | // print the keyword card
|
---|
799 | cout << card << endl;
|
---|
800 | }
|
---|
801 | cout << "END" << endl;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | void FitsFile::writeSignatureOnFits() const
|
---|
806 | {
|
---|
807 | int status = 0;
|
---|
808 | char keyname[LEN_KEYWORD];
|
---|
809 | char strval[FLEN_VALUE];
|
---|
810 | char comment[FLEN_COMMENT];
|
---|
811 |
|
---|
812 | strcpy(keyname,"CREATOR");
|
---|
813 | strcpy(strval,"SOPHYA");
|
---|
814 | strcpy(comment,"SOPHYA Package - FitsFile");
|
---|
815 | fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
|
---|
816 | }
|
---|
817 |
|
---|
818 | void FitsFile::printerror(int &status) const
|
---|
819 | //*****************************************************/
|
---|
820 | //* Print out cfitsio error messages and exit program */
|
---|
821 | //*****************************************************/
|
---|
822 | {
|
---|
823 | if( status )
|
---|
824 | {
|
---|
825 | fits_report_error(stderr,status);
|
---|
826 | throw IOExc("FitsFile:: error FITSIO status");
|
---|
827 | }
|
---|
828 | return;
|
---|
829 | }
|
---|
830 |
|
---|
831 | void FitsFile::printerror(int& status, char* texte) const
|
---|
832 | //*****************************************************/
|
---|
833 | //* Print out cfitsio error messages and exit program */
|
---|
834 | //*****************************************************/
|
---|
835 | {
|
---|
836 | // print out cfitsio error messages and exit program
|
---|
837 | // print error report
|
---|
838 | fits_report_error(stderr, status);
|
---|
839 | cout << " erreur:: " << texte << endl;
|
---|
840 | throw IOExc("FitsFile:: error FITSIO status");
|
---|
841 | }
|
---|