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

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

Compil SGI-CC , Probleme avec for(int i) repete - Reza 13/4/2000

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