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

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

impressions inutiles

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