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

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

modifs pour introduction lecteur de fits par lignes

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