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

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

modif lecture Header image

File size: 23.9 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 cout << "entree getbintablefcol double" << endl;
244 int status= 0;
245 int DTYPE;
246 long repeat,width;
247 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
248 if( DTYPE != TDOUBLE)
249 {
250 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
251 }
252 long nels=nentries;
253 // no checking for undefined pixels
254 int anull;
255 float dnull= 0.;
256 cout << " avant lecture colonne, nels= " << nels << endl;
257 fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
258 &anull,&status);
259 cout << " colonne lue" << endl;
260 if( status ) printerror( status,"erreur lecture de colonne" );
261 }
262
263 void FitsFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
264 {
265 cout << "entree getbintablefcol float" << endl;
266 int status= 0;
267 int DTYPE;
268 long repeat,width;
269 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
270 if( DTYPE != TFLOAT)
271 {
272 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
273 }
274 long nels=nentries;
275 // no checking for undefined pixels
276 int anull;
277 float fnull= 0.;
278 fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
279 &anull,&status);
280 if( status ) printerror( status,"erreur lecture de colonne" );
281 }
282 void FitsFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
283 {
284 cout << "entree getbintablefcol int" << endl;
285 int status= 0;
286 int DTYPE;
287 long repeat,width;
288 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
289 if( DTYPE != TLONG && DTYPE != TINT && DTYPE != TSHORT )
290 {
291 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
292 }
293 long nels=nentries;
294 // no checking for undefined pixels
295 int anull;
296 int inull= 0;
297 fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
298 &anull,&status);
299 if( status ) printerror( status,"erreur lecture de colonne" );
300 cout << " tableau d'entiers relu: " << endl;
301 for (int ijk=0; ijk<nels;ijk++) cout << valeurs[ijk] << endl;
302 }
303 void FitsFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
304 {
305 cout << "entree getbintablefcol string" << endl;
306 int status= 0;
307 int DTYPE;
308 long repeat,width;
309 fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
310 if( DTYPE != TSTRING )
311 {
312 throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
313 }
314 cout << " repeat= " << repeat << " width= " << width << endl;
315 long nels=nentries;
316 // no checking for undefined pixels
317 int anull;
318 char* cnull= " ";
319 long frow=1;
320 long felem=1;
321 cout << " nb elements a lire" << nels << endl;
322 cout << " taille element tableau " << strlen(valeurs[0]) << endl;
323 cout << " numero de colonne a lire: " << NoCol+1 << endl;
324 fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
325 &anull,&status);
326 if( status ) printerror( status,"erreur lecture de colonne" );
327 cout << " fin lecture chaines" << endl;
328 }
329int FitsFile::NbColsFromFits() const
330{
331 int status= 0;
332 if(hdutype_ == BINARY_TBL) return nbcols_;
333 else
334 if(hdutype_ == ASCII_TBL || hdutype_ == IMAGE_HDU) return 1;
335 else
336 {
337 cout << " hdutype= " << hdutype_ << endl;
338 throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
339 }
340}
341
342char FitsFile::ColTypeFromFits(int nocol) const
343{
344 int status= 0;
345 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
346 {
347 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
348 }
349 return types_[nocol];
350}
351int FitsFile::NentriesFromFits(int nocol) const
352{
353 int status= 0;
354 if(hdutype_ == BINARY_TBL ) return nrows_*repeat_[nocol];
355 else
356 if(hdutype_ == ASCII_TBL) return nrows_;
357 else
358 if(hdutype_ == IMAGE_HDU) return nbData_;
359 else
360 {
361 cout << "hdutype= " << hdutype_ << endl;
362 throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
363 }
364}
365
366string FitsFile::ColNameFromFits(int nocol) const
367{
368 int status= 0;
369 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
370 {
371 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
372 }
373 return noms_[nocol];
374}
375int FitsFile::ColStringLengthFromFits(int nocol) const
376{
377 int status= 0;
378 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
379 {
380 throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
381 }
382 int index=-1;
383 for (int k=0; k<=nocol; k++)
384 {
385 if (types_[k] == 'S') index++;
386 }
387 return taille_des_chaines_[index];
388}
389
390void FitsFile::GetBinTabParameters()
391{
392 int status= 0;
393 if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
394 {
395 throw IOExc("FitsFile:: this HDU is not an ASCII table nor a binary table");
396 }
397 if(hdutype_ == ASCII_TBL)
398 cout << " Reading a FITS ascii table in HDU : " << hdunum_ << endl;
399 if(hdutype_ == BINARY_TBL)
400 cout << " Reading a FITS binary table in HDU : " << hdunum_ << endl;
401
402 // get the number of columns
403 int nbcols = 0;
404 fits_get_num_cols(fptr_, &nbcols,&status);
405 if( status ) printerror( status );
406 nbcols_=nbcols;
407
408 // get the number of rows
409 long naxis2= 0;
410 fits_get_num_rows(fptr_,&naxis2,&status);
411 if( status ) printerror( status );
412 nrows_= (int)naxis2;
413
414 // get the datatype, names and the repeat count
415 noms_.clear();
416 noms_.reserve(nbcols);
417 types_.clear();
418 types_.reserve(nbcols);
419 repeat_.clear();
420 repeat_.reserve(nbcols);
421 taille_des_chaines_.clear();
422 char **ttype = new char*[nbcols];
423 for (int ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
424 int nfound;
425 fits_read_keys_str(fptr_, "TTYPE",1,nbcols,ttype,&nfound, &status);
426 if( status ) printerror( status,"erreur lecture des noms de colonne");
427 // int nentries = nrows_;
428 int rept=0;
429 for(int ii = 0; ii < nbcols; ii++)
430 {
431 int DTYPE;
432 long width;
433 long repeat = 0;
434 fits_get_coltype(fptr_,ii+1,&DTYPE,&repeat,&width,&status);
435 if( status ) printerror( status,"erreur lecture type de colonne");
436 rept = repeat;
437 noms_.push_back(string(ttype[ii]));
438 // cout << " getparam: nentries= " << nentries << " rept= " << rept << endl;
439 switch (DTYPE)
440 {
441 case TDOUBLE :
442 types_.push_back('D');
443 break;
444 case TFLOAT :
445 types_.push_back('E');
446 break;
447 case TLONG :
448 types_.push_back('I');
449 break;
450 case TINT :
451 types_.push_back('I');
452 break;
453 case TSHORT :
454 types_.push_back('I');
455 break;
456 case TSTRING :
457 types_.push_back('S');
458 taille_des_chaines_.push_back(width);
459 rept/=width;
460 cout << " j'ai lu taille_des_chaines= " << width << " rept= " << rept << endl;
461 break;
462 default :
463 cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
464 throw IOExc("FitsFile:: unknown type of field");
465 }
466 // nentries = max(nentries, nrows_*rept);
467 repeat_.push_back(rept);
468 cout << " getparam: repeat= " << repeat_[ii] << endl;
469 }
470 // get number of keywords
471 int nkeys,keypos;
472 fits_get_hdrpos(fptr_,&nkeys,&keypos,&status);
473 if( status ) printerror( status );
474
475 // put keywords in a DVList object
476 char keyname[LEN_KEYWORD]= "";
477 char strval[FLEN_VALUE]= "";
478 char dtype;
479 char card[FLEN_CARD];
480 char *comkey = "COMMENT";
481
482 // shift with the number of mandatory keywords
483 int num= 8;
484
485 for(int j = num+1; j <= nkeys; j++)
486 {
487 fits_read_keyn(fptr_,j,card,strval,NULL,&status);
488 if(status) printerror(status);
489
490 strncpy(keyname,card,LEN_KEYWORD-1);
491
492 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0
493 && strlen(strval) != 0)
494 {
495 fits_get_keytype(strval,&dtype,&status);
496 if(status) printerror(status);
497
498 strip(keyname, 'B',' ');
499 strip(strval, 'B',' ');
500 strip(strval, 'B','\'');
501
502 switch( dtype )
503 {
504 case 'C':
505 dvl_[keyname]= strval;
506 break;
507 case 'I':
508 int ival;
509 fits_read_key(fptr_,TINT,keyname,&ival,NULL,&status);
510 dvl_[keyname]= (int_4) ival; // Portage mac DY
511 break;
512 case 'L':
513 int ilog;
514 if(strncmp(strval,"T",1) == 0) ilog= 1;
515 else ilog= 0;
516 dvl_[keyname]= (int_4) ilog;
517 break;
518 case 'F':
519 double dval;
520 fits_read_key(fptr_,TDOUBLE,keyname,&dval,NULL,&status);
521 dvl_[keyname]= dval;
522 break;
523 }
524
525 }
526 }
527 cout << " fin lecture du dvlist : " << endl;
528 dvl_.Print();
529}
530void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) const
531{
532 cout << " entree put_column_bntbl " << endl;
533 int status = 0;
534 long nrows;
535 cout << " nombre de types: " << strlen(fieldType) << endl;
536 if (strlen(fieldType) != tfields)
537 {
538 cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
539 throw ParmError("FitsFile:: fields and types don't match");
540
541 }
542 char ** ttype= new char*[tfields];
543 char ** tform= new char*[tfields];
544 char largeur[FLEN_VALUE];
545 int noColString=0;
546
547
548 for (int k=0; k<tfields;k++)
549 {
550 char format[FLEN_VALUE];
551
552 if(nentries < 1024)
553 {
554 nrows= nentries;
555 if (fieldType[k] == 'A')
556 {
557 sprintf(largeur,"%d",taille_des_chaines[noColString++]);
558 strcpy(format,largeur);
559 }
560 else strcpy(format,"1");
561 }
562 else
563 {
564 nrows = nentries/1024;
565 if(nentries%1024 != 0) nrows++;
566 if (fieldType[k] == 'A')
567 {
568 char largaux[FLEN_VALUE];
569 sprintf(largeur,"%d",taille_des_chaines[noColString]);
570 sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
571 noColString++;
572 strcpy(format, largaux);
573 }
574 else strcpy(format,"1024");
575 }
576 cout << " en ecriture fits : nrows= " << nrows << endl;
577 cout << " fitsfile : format= " << format << endl;
578 strncat(format,&fieldType[k],1);
579 if (fieldType[k] == 'A')
580 {
581 strcat(format,largeur);
582 }
583 cout << " fitsfile : format= " << format << endl;
584 ttype[k]= new char[FLEN_VALUE];
585 strcpy(ttype[k],Noms[k]);
586 tform[k]= new char[FLEN_VALUE];
587 strcpy(tform[k],format);
588 }
589 const char* TypeOfContent= dvl.GetS("TypeOfContent").c_str();
590 // value of the EXTNAME keyword
591 char extn[FLEN_VALUE];
592 strncpy(extn,extname,FLEN_VALUE);
593
594 // create a new empty binary table onto the FITS file
595 // physical units if they exist, are defined in the DVList object
596 // so the NULL pointer is given for the tunit parameters.
597 nrows=0;
598 cout << " en ecriture fits : nrows avant create = " << nrows << endl;
599 fits_create_tbl(fptr_,BINARY_TBL,nrows,tfields,ttype,tform,
600 NULL,extn,&status);
601 if( status ) printerror( status );
602
603 for(int ii = 0; ii < tfields; ii++)
604 {
605 delete [] ttype[ii];
606 delete [] tform[ii];
607 }
608 delete [] ttype;
609 delete [] tform;
610 //
611 // write supplementary keywords
612 //
613 // get names and values from the join DVList object
614 cout << " dvlist en ecriture..." << endl;
615 dvl.Print();
616 DVList::ValList::const_iterator it;
617 for(it = dvl.Begin(); it != dvl.End(); it++)
618 {
619 char keytype= (*it).second.elval.typ;
620 char keyname[10];
621 strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
622 char comment[FLEN_COMMENT];
623 cout << " keyname: " << keyname << " keytype: " << keytype << endl;
624 switch (keytype)
625 {
626 case 'I' :
627 {
628 int ival=(*it).second.elval.mtv.iv;
629 strcpy(comment,"I entier");
630 fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
631 cout << " keyname " << keyname << " = " << ival << " ecrit " << endl;
632 break;
633 }
634 case 'D' :
635 {
636 double dval=(*it).second.elval.mtv.dv;
637 strcpy(comment,"D double");
638 fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
639 cout << " keyname " << keyname << " = " << dval << " ecrit " << endl;
640 break;
641 }
642 case 'S' :
643 {
644 char strval[128];
645 strncpy(strval,(*it).second.elval.mtv.strv,127);
646 strcpy(comment,"S character string");
647 fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
648 cout << " keyname " << keyname << " = " << strval << " ecrit " << endl;
649 break;
650 }
651 }
652 if( status ) printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
653 }
654
655 cout << " fitsfile: le dvlist est ecrit" << endl;
656}
657
658void FitsFile::putColToFits(int nocol, int nentries, double* donnees) const
659{
660 int status = 0;
661 int hdutype, hdunum;
662 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
663 if( status ) printerror(status,"putColToFits: le movabs a foire");
664 fits_get_hdu_type(fptr_, &hdutype, &status);
665 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
666 {
667 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
668 // throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
669 }
670 // if(hdutype == ASCII_TBL && nocol>0)
671 // {
672 // throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
673 // }
674 int code;
675 long repeat, width;
676 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
677 if( code != TDOUBLE)
678 {
679 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " to be written= DOUBLE " << endl;
680 }
681 fits_write_col(fptr_,TDOUBLE,nocol+1,1,1,nentries, donnees ,&status);
682 if( status ) printerror( status,"erreur ecriture du fichier fits" );
683}
684void FitsFile::putColToFits(int nocol, int nentries, float* donnees) const
685{
686 int status = 0;
687 int hdutype;
688 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
689 if( status ) printerror(status,"putColToFits: le movabs a foire");
690 fits_get_hdu_type(fptr_, &hdutype, &status);
691 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
692 {
693 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
694 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
695 }
696 if(hdutype == ASCII_TBL && nocol>0)
697 {
698 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
699 }
700 int code;
701 long repeat, width;
702 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
703 if( code != TFLOAT)
704 {
705 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
706 }
707 fits_write_col(fptr_,TFLOAT,nocol+1,1,1,nentries, donnees ,&status);
708 if( status ) printerror( status,"erreur ecriture du fichier fits" );
709}
710void FitsFile::putColToFits(int nocol, int nentries, int* donnees) const
711{
712 int status = 0;
713 int hdutype;
714 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
715 if( status ) printerror(status,"putColToFits: le movabs a foire");
716 fits_get_hdu_type(fptr_, &hdutype, &status);
717 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
718 {
719 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
720 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
721 }
722 if(hdutype == ASCII_TBL && nocol>0)
723 {
724 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
725 }
726 int code;
727 long repeat, width;
728 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
729 if( code != TLONG && code != TINT && code != TSHORT )
730 {
731 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
732 }
733 fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
734 if( status ) printerror( status,"erreur ecriture du fichier fits" );
735}
736void FitsFile::putColToFits(int nocol, int nentries, char** donnees) const
737{
738 int status = 0;
739 int hdutype;
740 fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
741 if( status ) printerror(status,"putColToFits: le movabs a foire");
742 fits_get_hdu_type(fptr_, &hdutype, &status);
743 if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
744 {
745 cout << " hdunum= " << hdunum_ << " hdutype= " << hdutype << endl;
746 throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
747 }
748 if(hdutype == ASCII_TBL && nocol>0)
749 {
750 throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0 forbidden");
751 }
752 int code;
753 long repeat, width;
754 fits_get_coltype(fptr_, nocol+1, &code, &repeat,&width, &status);
755 if( code != TSTRING)
756 {
757 cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= char** " << endl;
758 }
759 fits_write_col(fptr_,TSTRING,nocol+1,1,1,nentries, donnees ,&status);
760 if( status ) printerror( status,"erreur ecriture du fichier fits" );
761}
762
763
764
765
766void FitsFile::readheader()
767 //*************************************/
768 //* Print out all the header keywords */
769 //*************************************/
770{
771 // standard string lengths defined in fitsioc.h
772 char card[FLEN_CARD];
773
774 int status = 0;
775
776 // get the number of keywords
777 int nkeys, keypos;
778 if( fits_get_hdrpos(fptr_,&nkeys,&keypos,&status) )
779 printerror( status );
780
781 cout << " Header listing for HDU : " << hdunum_ << endl;
782 for(int jj = 1; jj <= nkeys; jj++)
783 {
784 if( fits_read_record(fptr_,jj,card,&status) )
785 printerror( status );
786
787 // print the keyword card
788 cout << card << endl;
789 }
790 cout << "END" << endl;
791}
792
793
794void FitsFile::writeSignatureOnFits() const
795{
796 int status = 0;
797 char keyname[LEN_KEYWORD];
798 char strval[FLEN_VALUE];
799 char comment[FLEN_COMMENT];
800
801 strcpy(keyname,"CREATOR");
802 strcpy(strval,"SOPHYA");
803 strcpy(comment,"SOPHYA Package - FitsFile");
804 fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
805}
806
807void FitsFile::printerror(int &status) const
808 //*****************************************************/
809 //* Print out cfitsio error messages and exit program */
810 //*****************************************************/
811{
812 if( status )
813 {
814 fits_report_error(stderr,status);
815 throw IOExc("FitsFile:: error FITSIO status");
816 }
817 return;
818}
819
820void FitsFile::printerror(int& status, char* texte) const
821 //*****************************************************/
822 //* Print out cfitsio error messages and exit program */
823 //*****************************************************/
824{
825 // print out cfitsio error messages and exit program
826 // print error report
827 fits_report_error(stderr, status);
828 cout << " erreur:: " << texte << endl;
829 throw IOExc("FitsFile:: error FITSIO status");
830}
Note: See TracBrowser for help on using the repository browser.