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