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