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

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

mise a jour 27/04/00 GLM

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