source: Sophya/trunk/SophyaExt/FitsIOServer/fitsfile.cc@ 1045

Last change on this file since 1045 was 1045, checked in by ansari, 25 years ago

ecriture premiere image en HDU 2

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