Ignore:
Timestamp:
Aug 24, 2000, 12:18:02 PM (25 years ago)
Author:
ansari
Message:

structuration a la ppersist+ convention sur hdu

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaExt/FitsIOServer/fitsfile.cc

    r1082 r1136  
    66#include "anydataobj.h"
    77#include "fitsspherehealpix.h"
    8 FitsFile::FitsFile()
    9 {
    10   InitNull();
    11 }
     8
     9
     10void   FitsIOHandler::Read(char flnm[],int hdunum)
     11{
     12  //  FitsInFile ifts(flnm, hdunum);
     13  FitsInFile ifts(flnm);
     14  Read(ifts, hdunum);
     15}
     16void FitsIOHandler::Read(FitsInFile& is, int hdunum)
     17{
     18  if (hdunum == 0)  is.moveToFollowingHeader();
     19  else is.ReadFInit(hdunum);
     20  ReadFromFits(is);
     21}
     22
     23
     24void FitsIOHandler::Write(char flnm[], bool OldFile)
     25
     26{
     27
     28  FitsOutFile of(flnm, OldFile);
     29  Write(of);
     30}
     31
     32void FitsIOHandler::Write(FitsOutFile& os)
     33{
     34  WriteToFits(os);
     35}
     36
     37
     38
    1239 
    1340FitsFile::~FitsFile()
    1441{
     42  cout << " destructeur FitsFile " << endl;
    1543  int status = 0;
    1644  if( fptr_ != NULL)
     
    2250}
    2351
    24 void FitsFile::InitNull()
    25 {
    26   fptr_= NULL;
    27   hdutype_= 0;
    28   hdunum_ = 0;
    29 
     52
     53void FitsFile::printerror(int &status)
     54  //*****************************************************/
     55  //* Print out cfitsio error messages and exit program */
     56  //*****************************************************/
     57{
     58  if( status )
     59    {
     60      fits_report_error(stderr,status);
     61      throw IOExc("FitsFile:: error FITSIO status");
     62    }
     63  return;
     64}
     65
     66void FitsFile::printerror(int& status, char* texte)
     67  //*****************************************************/
     68  //* Print out cfitsio error messages and exit program */
     69  //*****************************************************/
     70{
     71  // print out cfitsio error messages and exit program
     72  // print error report 
     73  fits_report_error(stderr, status);
     74  cout << " erreur:: " << texte << endl;
     75  throw IOExc("FitsFile:: error FITSIO status");
     76}
     77
     78void FitsFile::ResetStatus(int& status)
     79{
     80  fits_status_ = status;
     81  status = 0;
     82}
     83
     84string FitsFile::getErrStatus(int status)
     85{
     86  char text[31];
     87  fits_get_errstatus(status, text);
     88  return string(text);
     89}
     90
     91FitsInFile::FitsInFile()
     92{
     93  InitNull();
     94}
     95//FitsInFile::FitsInFile(char flnm[], int hdunum)
     96FitsInFile::FitsInFile(char flnm[])
     97{
     98  InitNull();
     99  int status = 0;
     100  fits_open_file(&fptr_,flnm,READONLY,&status);
     101  if( status ) printerror( status );
     102  //  ReadFInit(flnm, hdunum);
     103}
     104
     105
     106void FitsInFile::InitNull()
     107{
     108  cout << " init FitsInFile " << endl;
    30109  bitpix_ = 0;
    31110  naxis_ = 0;
     
    33112  nrows_ = 0;
    34113  nbcols_ = 0;
    35   fits_status_ = 0;
    36114  naxisn_.clear();
    37115  repeat_.clear();
     
    42120
    43121
    44 int FitsFile::NbBlocks(char flnm[])
     122int FitsInFile::NbBlocks(char flnm[])
    45123{
    46124  int status = 0;
     
    54132}
    55133
    56 void FitsFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
     134void FitsInFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
    57135{
    58136  int status = 0;
     
    123201}
    124202
    125 void FitsFile::ReadF(char flnm[],int hdunum)
    126 {
    127   ReadFInit(flnm, hdunum);
    128   //  ReadFromFits(*this);
    129     ReadFromFits();
    130 }
    131 
    132 
    133 //FitsFile* FitsFile::ReadFInit(char flnm[],int hdunum)
    134 void FitsFile::ReadFInit(char flnm[],int hdunum)
    135 {
    136   InitNull();
    137   int status = 0;
    138   //  hdutype_= 0;
     203void FitsInFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
     204{
     205  int hdunum=0;
     206  cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum)  << endl;
     207  int status= 0;
     208
     209  // bits per pixels
     210  fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
     211  if( status )  printerror( status );
     212
     213  // number of dimensions in the FITS array
     214  naxis= 0;
     215  fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
     216  if( status ) printerror( status );
     217  //  cout << " dimension lue " << naxis << endl;
     218  // read the NAXISn keywords to get image size
     219  long* naxes = new long[naxis] ;
     220  int nfound;
     221  fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
     222  if( status ) printerror( status );
     223  if (nfound != naxis )
     224    cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
     225  int k;
     226  for (k=0; k<naxis; k++)
     227    {
     228      naxisn.push_back( (int)naxes[k] );
     229    }
     230  delete [] naxes;
     231}
     232
     233
     234//void FitsInFile::ReadFInit(char flnm[],int hdunum)
     235void FitsInFile::ReadFInit(int hdunum)
     236{
     237  // InitNull();
     238  // int status = 0;
    139239 
    140   fits_open_file(&fptr_,flnm,READONLY,&status);
    141   if( status ) printerror( status );
    142   //
     240  //  fits_open_file(&fptr_,flnm,READONLY,&status);
     241  //  if( status ) printerror( status );
     242
     243  int status = 0;
     244
    143245  if (hdunum <= 1)
    144246    {
     
    171273      moveToFollowingHeader();
    172274    }
    173   //  return this;
    174 }
    175 
    176 void FitsFile::moveToFollowingHeader()
     275}
     276
     277
     278
     279void FitsInFile::moveToFollowingHeader()
    177280{
    178281  int status = 0;
     
    197300}
    198301
    199  
    200 void FitsFile::WriteF(char flnm[], bool OldFile)
    201 {
    202   int status = 0;
    203   hdutype_= 0;
    204   hdunum_ = 0;
    205 
    206  
    207   // create new FITS file
    208   if (!OldFile)
    209     {
    210       fits_create_file(&fptr_,flnm,&status);
    211       if( status ) printerror(status,"file already exists");
    212     }
     302
     303int  FitsInFile::NbColsFromFits() const
     304{
     305  if(hdutype_ ==  BINARY_TBL) return nbcols_;
    213306  else
    214     {
    215       fits_open_file(&fptr_,flnm,READWRITE,&status);
    216       if( status ) printerror(status,"file does not exist");
    217       fits_get_num_hdus(fptr_, &hdunum_, &status);
    218       int hdutype;
    219       fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
    220       if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
    221 
    222     }
    223   //  WriteToFits(*this);
    224   WriteToFits();
    225 }
    226 void FitsFile::GetSingleColumn(double* map, int nentries) const
     307    if(hdutype_ == ASCII_TBL ||  hdutype_ == IMAGE_HDU) return 1;
     308    else
     309      {
     310        cout << " hdutype= " << hdutype_ << endl;
     311        throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
     312      }
     313}
     314
     315int FitsInFile::NentriesFromFits(int nocol) const
     316{
     317  if(hdutype_ == BINARY_TBL  )   return nrows_*repeat_[nocol];
     318  else
     319    if(hdutype_ == ASCII_TBL) return nrows_;
     320    else
     321      if(hdutype_ == IMAGE_HDU) return nbData_;
     322      else
     323        {
     324          cout << "hdutype= " << hdutype_ << endl;
     325          throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
     326        }
     327}
     328
     329char FitsInFile::ColTypeFromFits(int nocol) const
     330{
     331  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
     332    {
     333      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
     334    }
     335  return types_[nocol];
     336}
     337string FitsInFile::ColNameFromFits(int nocol) const
     338{
     339  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
     340    {
     341      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
     342    }
     343  return noms_[nocol];
     344}
     345
     346int FitsInFile::ColStringLengthFromFits(int nocol) const
     347{
     348  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
     349    {
     350      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
     351    }
     352  int index=-1;
     353  int k;
     354  for (k=0; k<=nocol; k++)
     355    {
     356      if (types_[k] == 'S') index++;
     357    }
     358  return  taille_des_chaines_[index];
     359}
     360void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
     361{
     362  int status= 0;
     363  int    anull;
     364  double dnull= 0.;
     365  float fnull= 0.;
     366  int inull= 0;
     367  char* cnull= "";
     368  int dcount = 0.;
     369  int fcount = 0.;
     370  int icount = 0;
     371  int ccount =0;
     372  int ncol;
     373  long nels=1;
     374  for (ncol=0; ncol<nbcols_; ncol++)
     375    {
     376      switch (types_[ncol])
     377        {
     378        case 'D' :
     379          fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
     380            break;
     381        case 'E' :
     382          fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
     383          break;
     384        case 'I' :
     385          fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
     386                        &anull,&status);
     387          break;
     388        case 'S' :
     389          fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
     390          break;
     391        }
     392      if (status)
     393        {
     394          ResetStatus(status);
     395          break;
     396        }
     397    }
     398}
     399
     400void FitsInFile::GetBinTabLine(int NoLine, float* fdata)
     401{
     402  int status= 0;
     403  int    anull;
     404  float fnull= 0.;
     405  long nels=1;
     406  int ncol;
     407  for (ncol=0; ncol<nbcols_; ncol++)
     408    {
     409      fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
     410      if (status)
     411        {
     412          ResetStatus(status);
     413          break;
     414        }
     415    }
     416}
     417
     418
     419void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
     420    {
     421      int status= 0;
     422      int DTYPE;
     423      long repeat,width;
     424            fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
     425      if( DTYPE != TDOUBLE)
     426        {
     427          if (DTYPE == TFLOAT)  cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
     428          else
     429            throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
     430        }
     431      long nels=nentries;
     432      int    anull;
     433      // no checking for undefined pixels
     434      double dnull= 0.;
     435      //     fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
     436      //  if (status != 0)
     437      //        {
     438      //          dnull = -1.6375e30;  // default value
     439      //          status = 0;
     440      //        }
     441      if (nentries != nrows_*repeat)
     442        {
     443          cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
     444          throw PException(" FitsFile:::GetBinTabFCol ");
     445        }
     446      fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
     447                            &anull,&status);
     448      if( status ) printerror( status,"erreur lecture de colonne" );
     449     
     450    }
     451
     452void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
     453    {
     454      int status= 0;
     455      int DTYPE;
     456      long repeat,width;
     457      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
     458      if( DTYPE != TFLOAT)
     459        {
     460          if (DTYPE == TDOUBLE)  cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
     461          else
     462            throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
     463        }
     464      long nels=nentries;
     465      int    anull;
     466      // no checking for undefined pixels
     467      float fnull= 0.;
     468      //     fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
     469      //    if (status != 0)
     470      //        {
     471      //          fnull = -1.6375e30;  // default value
     472      //          status = 0;
     473      //        }
     474      if (nentries != nrows_*repeat)
     475        {
     476          cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
     477          throw PException(" FitsFile:::GetBinTabFCol ");
     478        }
     479      fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
     480                            &anull,&status);
     481      if( status ) printerror( status,"erreur lecture de colonne" );
     482    }
     483
     484void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
     485    {
     486      cout <<" entree GetBinTabFCol " << endl;
     487      int status= 0;
     488      int DTYPE;
     489      long repeat,width;
     490      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
     491      if( DTYPE != TLONG &&  DTYPE != TINT &&  DTYPE != TSHORT )
     492        {
     493          throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
     494        }
     495      long nels=nentries;
     496      // no checking for undefined pixels
     497      int    anull;
     498      int inull= 0;
     499      //     fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
     500      //     if (status != 0)
     501      //        {
     502      //          inull = -999999;  // default value
     503      //          status = 0;
     504      //        }
     505      if (nentries != nrows_*repeat)
     506        {
     507          cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
     508          throw PException(" FitsFile:::GetBinTabFCol ");
     509        }
     510      fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
     511                            &anull,&status);
     512      if( status ) printerror( status,"erreur lecture de colonne" );
     513    }
     514
     515void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
     516    {
     517      int status= 0;
     518      int DTYPE;
     519      long repeat,width;
     520      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
     521      if( DTYPE != TSTRING )
     522        {
     523          throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
     524        }
     525      long nels=nentries;
     526      // no checking for undefined pixels
     527      int    anull;
     528      char* cnull= "";
     529      if (nentries != nrows_*repeat/width)
     530        {
     531          cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat/width << endl;
     532          throw PException(" FitsFile:::GetBinTabFCol ");
     533        }
     534      long frow=1;
     535      long felem=1;
     536      fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
     537                    &anull,&status);
     538      if( status ) printerror( status,"erreur lecture de colonne" );
     539    }
     540
     541void FitsInFile::GetSingleColumn(double* map, int nentries) const
    227542{
    228543  int status = 0;
     
    256571}
    257572
    258 void FitsFile::GetSingleColumn(float* map, int nentries) const
     573void FitsInFile::GetSingleColumn(float* map, int nentries) const
    259574{
    260575  int status = 0;
     
    286601}
    287602
    288 void FitsFile::GetSingleColumn( int* map, int nentries) const
     603void FitsInFile::GetSingleColumn( int* map, int nentries) const
    289604{
    290605  int status = 0;
     
    315630      }
    316631}
    317 void FitsFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn)
    318 {
    319   int status = 0;
    320   long naxis = nbdim;
    321   long* naxes = new long[nbdim];
    322   if (hdunum_ == 0)
    323     {
    324       fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
    325       hdunum_ = 1;
    326     }
    327   int k;
    328   for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
    329   if (type == 'D')
    330     fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
    331   else
    332     if  (type == 'E')
    333       fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
    334     else
    335       if  (type == 'I')
    336         fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
    337       else
    338         {
    339           cout << " type of data: " << type << endl;
    340           throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
    341         }
    342 
    343   hdunum_++;
    344   delete [] naxes;
    345   if( status ) printerror( status, "erreur creation HDU IMAGE"  );
    346 
    347 }
    348 void FitsFile::putImageToFits(int nbData, double* map) const
    349 {
    350   int status = 0;
    351   long npix= nbData;
    352   fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
    353   if( status ) printerror( status, "erreur ecriture putImageToFits" );
    354   writeSignatureOnFits();
    355 }
    356 
    357 void FitsFile::putImageToFits(int nbData, float* map) const
    358 {
    359   int status = 0;
    360   long npix= nbData;
    361   fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
    362   if( status ) printerror( status, "erreur ecriture putImageToFits" );
    363   writeSignatureOnFits();
    364 
    365 }
    366 void FitsFile::putImageToFits( int nbData, int* map) const
    367 {
    368   int status = 0;
    369 
    370   long npix= nbData;
    371   fits_write_img(fptr_,TINT,1,npix,map,&status);
    372   if( status ) printerror( status, "erreur ecriture putImageToFits" );
    373   writeSignatureOnFits();
    374 }
    375 
    376 
    377 
    378 void FitsFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
    379 {
     632
     633void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
     634                                  vector<int>& repeat,
     635                                  vector<string>& noms,
     636                                  vector<char>& types,   
     637                                  vector<int>&  taille_des_chaines)
     638{
     639  int status= 0;
    380640  int hdunum=0;
    381   cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum)  << endl;
    382   int status= 0;
    383 
    384   // bits per pixels
    385   fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
    386   if( status )  printerror( status );
    387 
    388   // number of dimensions in the FITS array
    389   naxis= 0;
    390   fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
     641  int hdutype=0;
     642  fits_get_hdu_num(fileptr,&hdunum);
     643  fits_get_hdu_type(fileptr, &hdutype, &status);
     644
     645  if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
     646    {
     647      throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
     648    }
     649  if(hdutype == ASCII_TBL)
     650    cout << " Reading a FITS ascii table in HDU : " << hdunum << endl; 
     651  if(hdutype == BINARY_TBL)
     652    cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
     653
     654  // get the number of columns
     655  fits_get_num_cols(fileptr, &nbcols,&status);
    391656  if( status ) printerror( status );
    392657
    393   // read the NAXISn keywords to get image size
    394   long* naxes = new long[naxis] ;
     658  // get the number of rows
     659  long naxis2= 0;
     660  fits_get_num_rows(fileptr,&naxis2,&status);
     661  if( status ) printerror( status );
     662  nrows = (int)naxis2;
     663
     664  // get the datatype,  names and the repeat count
     665  noms.clear();
     666  noms.reserve(nbcols);
     667  types.clear();
     668  types.reserve(nbcols);
     669  repeat.clear();
     670  repeat.reserve(nbcols);
     671  taille_des_chaines.clear();
     672  char **ttype = new char*[nbcols];
     673  int ii;
     674  for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
    395675  int nfound;
    396   fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
    397   if( status ) printerror( status );
    398   if (nfound != naxis )
    399     cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
    400   int k;
    401   for (k=0; k<naxis; k++)
    402     {
    403       naxisn.push_back( (int)naxes[k] );
    404     }
    405   delete [] naxes;
    406 }
    407 
    408 void FitsFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
     676  fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
     677  if( status ) printerror( status,"erreur lecture des noms de colonne");
     678  int rept=0;
     679  for(ii = 0; ii <  nbcols; ii++)
     680    {
     681      int DTYPE;
     682      long width;
     683      long repete = 0;
     684      fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
     685      if( status ) printerror( status,"erreur lecture type de colonne");
     686      rept = repete;
     687      noms.push_back(string(ttype[ii]));
     688      switch (DTYPE)
     689        {
     690        case TDOUBLE :
     691          types.push_back('D');
     692          break;
     693        case TFLOAT :
     694          types.push_back('E');
     695          break;
     696        case TLONG :
     697          types.push_back('I');
     698          break;
     699        case TINT :
     700          types.push_back('I');
     701          break;
     702        case TSHORT :
     703          types.push_back('I');
     704          break;
     705        case TSTRING :
     706          types.push_back('S');
     707          taille_des_chaines.push_back(width);
     708          rept/=width;
     709          break;
     710        default :
     711          cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
     712          throw IOExc("FitsFile:: unknown type of field");
     713        }
     714      repeat.push_back(rept);
     715    }
     716  for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
     717  delete [] ttype;
     718}
     719
     720void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
    409721{
    410722  int status = 0;
     
    473785}
    474786
    475 
    476  
    477   void FitsFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
    478     {
    479       int status= 0;
    480       int DTYPE;
    481       long repeat,width;
    482             fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
    483       if( DTYPE != TDOUBLE)
    484         {
    485           if (DTYPE == TFLOAT)  cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
    486           else
    487             throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
    488         }
    489       long nels=nentries;
    490       int    anull;
    491       // no checking for undefined pixels
    492       double dnull= 0.;
    493       //     fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
    494       //  if (status != 0)
    495       //        {
    496       //          dnull = -1.6375e30;  // default value
    497       //          status = 0;
    498       //        }
    499       if (nentries != nrows_*repeat)
    500         {
    501           cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
    502           throw PException(" FitsFile:::GetBinTabFCol ");
    503         }
    504       fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
    505                             &anull,&status);
    506       if( status ) printerror( status,"erreur lecture de colonne" );
    507      
    508     }
    509 
    510   void FitsFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
    511     {
    512       int status= 0;
    513       int DTYPE;
    514       long repeat,width;
    515       fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
    516       if( DTYPE != TFLOAT)
    517         {
    518           if (DTYPE == TDOUBLE)  cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
    519           else
    520             throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
    521         }
    522       long nels=nentries;
    523       int    anull;
    524       // no checking for undefined pixels
    525       float fnull= 0.;
    526       //     fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
    527       //    if (status != 0)
    528       //        {
    529       //          fnull = -1.6375e30;  // default value
    530       //          status = 0;
    531       //        }
    532       if (nentries != nrows_*repeat)
    533         {
    534           cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
    535           throw PException(" FitsFile:::GetBinTabFCol ");
    536         }
    537       fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
    538                             &anull,&status);
    539       if( status ) printerror( status,"erreur lecture de colonne" );
    540     }
    541   void FitsFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
    542     {
    543       cout <<" entree GetBinTabFCol " << endl;
    544       int status= 0;
    545       int DTYPE;
    546       long repeat,width;
    547       fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
    548       if( DTYPE != TLONG &&  DTYPE != TINT &&  DTYPE != TSHORT )
    549         {
    550           throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
    551         }
    552       long nels=nentries;
    553       // no checking for undefined pixels
    554       int    anull;
    555       int inull= 0;
    556       //     fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
    557       //     if (status != 0)
    558       //        {
    559       //          inull = -999999;  // default value
    560       //          status = 0;
    561       //        }
    562       if (nentries != nrows_*repeat)
    563         {
    564           cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
    565           throw PException(" FitsFile:::GetBinTabFCol ");
    566         }
    567       fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
    568                             &anull,&status);
    569       if( status ) printerror( status,"erreur lecture de colonne" );
    570     }
    571   void FitsFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
    572     {
    573       int status= 0;
    574       int DTYPE;
    575       long repeat,width;
    576       fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
    577       if( DTYPE != TSTRING )
    578         {
    579           throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
    580         }
    581       long nels=nentries;
    582       // no checking for undefined pixels
    583       int    anull;
    584       char* cnull= "";
    585       if (nentries != nrows_*repeat/width)
    586         {
    587           cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat/width << endl;
    588           throw PException(" FitsFile:::GetBinTabFCol ");
    589         }
    590       long frow=1;
    591       long felem=1;
    592       fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
    593                     &anull,&status);
    594       if( status ) printerror( status,"erreur lecture de colonne" );
    595     }
    596 
    597 
    598 
    599 void FitsFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata)
    600 {
    601   int status= 0;
    602   int    anull;
    603   double dnull= 0.;
    604   float fnull= 0.;
    605   int inull= 0;
    606   char* cnull= "";
    607   int dcount = 0.;
    608   int fcount = 0.;
    609   int icount = 0;
    610   int ccount =0;
    611   int ncol;
    612   long nels=1;
    613   for (ncol=0; ncol<nbcols_; ncol++)
    614     {
    615       switch (types_[ncol])
    616         {
    617         case 'D' :
    618           fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
    619             break;
    620         case 'E' :
    621           fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
    622           break;
    623         case 'I' :
    624           fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
    625                         &anull,&status);
    626           break;
    627         case 'S' :
    628           fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
    629           break;
    630         }
    631       if (status)
    632         {
    633           ResetStatus(status);
    634           break;
    635         }
    636       //          if( status ) printerror( status,"GetBinTabLine : erreur lecture de colonne char" );
    637     }
    638 }
    639 
    640 void FitsFile::GetBinTabLine(int NoLine, float* fdata)
    641 {
    642   int status= 0;
    643   int    anull;
    644   float fnull= 0.;
    645   long nels=1;
    646   int ncol;
    647   for (ncol=0; ncol<nbcols_; ncol++)
    648     {
    649       fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
    650       if (status)
    651         {
    652           ResetStatus(status);
    653           break;
    654         }
    655        //      if( status ) printerror( status,"GetBinTabLine : erreur lecture de colonne float" );
    656     }
    657 }
    658 
    659      
    660 
    661 
    662 
    663 
    664 
    665 
    666 int  FitsFile::NbColsFromFits() const
    667 {
    668   if(hdutype_ ==  BINARY_TBL) return nbcols_;
     787FitsOutFile::FitsOutFile()
     788{
     789  InitNull();
     790}
     791
     792FitsOutFile::FitsOutFile(char flnm[], bool OldFile)
     793{
     794
     795  InitNull();
     796  int status = 0;
     797
     798  // create new FITS file
     799  if (!OldFile)
     800    {
     801      fits_create_file(&fptr_,flnm,&status);
     802      if( status ) printerror(status,"file already exists");
     803    }
    669804  else
    670     if(hdutype_ == ASCII_TBL ||  hdutype_ == IMAGE_HDU) return 1;
     805    {
     806      fits_open_file(&fptr_,flnm,READWRITE,&status);
     807      if( status ) printerror(status,"file does not exist");
     808      fits_get_num_hdus(fptr_, &hdunum_, &status);
     809      int hdutype;
     810      fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
     811      if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
     812
     813    }
     814
     815}
     816
     817
     818
     819void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn)
     820{
     821  int status = 0;
     822  long naxis = nbdim;
     823  long* naxes = new long[nbdim];
     824  if (hdunum_ == 1)
     825    {
     826      if (imageOnPrimary_ == false) fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
     827      else  hdunum_--;
     828    }
     829  int k;
     830  for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
     831  if (type == 'D')
     832    fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
     833  else
     834    if  (type == 'E')
     835      fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
    671836    else
    672       {
    673         cout << " hdutype= " << hdutype_ << endl;
    674         throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
    675       }
    676 }
    677 
    678 char FitsFile::ColTypeFromFits(int nocol) const
    679 {
    680   if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
    681     {
    682       throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
    683     }
    684   return types_[nocol];
    685 }
    686 int FitsFile::NentriesFromFits(int nocol) const
    687 {
    688   if(hdutype_ == BINARY_TBL  )   return nrows_*repeat_[nocol];
    689   else
    690     if(hdutype_ == ASCII_TBL) return nrows_;
    691     else
    692       if(hdutype_ == IMAGE_HDU) return nbData_;
     837      if  (type == 'I')
     838        fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
    693839      else
    694840        {
    695           cout << "hdutype= " << hdutype_ << endl;
    696           throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
    697         }
    698 }
    699 
    700 string FitsFile::ColNameFromFits(int nocol) const
    701 {
    702   if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
    703     {
    704       throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
    705     }
    706   return noms_[nocol];
    707 }
    708 int FitsFile::ColStringLengthFromFits(int nocol) const
    709 {
    710   if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
    711     {
    712       throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
    713     }
    714   int index=-1;
    715   int k;
    716   for (k=0; k<=nocol; k++)
    717     {
    718       if (types_[k] == 'S') index++;
    719     }
    720   return  taille_des_chaines_[index];
    721 }
    722 
    723 
    724 
    725 void FitsFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
    726                                   vector<int>& repeat,
    727                                   vector<string>& noms,
    728                                   vector<char>& types,   
    729                                   vector<int>&  taille_des_chaines)
    730 {
    731   int status= 0;
    732   int hdunum=0;
    733   int hdutype=0;
    734   fits_get_hdu_num(fileptr,&hdunum);
    735   fits_get_hdu_type(fileptr, &hdutype, &status);
    736 
    737   if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
    738     {
    739       throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
    740     }
    741   if(hdutype == ASCII_TBL)
    742     cout << " Reading a FITS ascii table in HDU : " << hdunum << endl; 
    743   if(hdutype == BINARY_TBL)
    744     cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
    745 
    746   // get the number of columns
    747   fits_get_num_cols(fileptr, &nbcols,&status);
    748   if( status ) printerror( status );
    749 
    750   // get the number of rows
    751   long naxis2= 0;
    752   fits_get_num_rows(fileptr,&naxis2,&status);
    753   if( status ) printerror( status );
    754   nrows = (int)naxis2;
    755 
    756   // get the datatype,  names and the repeat count
    757   noms.clear();
    758   noms.reserve(nbcols);
    759   types.clear();
    760   types.reserve(nbcols);
    761   repeat.clear();
    762   repeat.reserve(nbcols);
    763   taille_des_chaines.clear();
    764   char **ttype = new char*[nbcols];
    765   int ii;
    766   for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
    767   int nfound;
    768   fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
    769   if( status ) printerror( status,"erreur lecture des noms de colonne");
    770   int rept=0;
    771   for(ii = 0; ii <  nbcols; ii++)
    772     {
    773       int DTYPE;
    774       long width;
    775       long repete = 0;
    776       fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
    777       if( status ) printerror( status,"erreur lecture type de colonne");
    778       rept = repete;
    779       noms.push_back(string(ttype[ii]));
    780       switch (DTYPE)
    781         {
    782         case TDOUBLE :
    783           types.push_back('D');
    784           break;
    785         case TFLOAT :
    786           types.push_back('E');
    787           break;
    788         case TLONG :
    789           types.push_back('I');
    790           break;
    791         case TINT :
    792           types.push_back('I');
    793           break;
    794         case TSHORT :
    795           types.push_back('I');
    796           break;
    797         case TSTRING :
    798           types.push_back('S');
    799           taille_des_chaines.push_back(width);
    800           rept/=width;
    801           break;
    802         default :
    803           cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
    804           throw IOExc("FitsFile:: unknown type of field");
    805         }
    806       repeat.push_back(rept);
    807     }
    808 }
    809 
    810 
    811 
    812 void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
     841          cout << " type of data: " << type << endl;
     842          throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
     843        }
     844
     845  hdunum_++;
     846  delete [] naxes;
     847  if( status ) printerror( status, "erreur creation HDU IMAGE"  );
     848
     849}
     850void FitsOutFile::putImageToFits(int nbData, double* map) const
     851{
     852  int status = 0;
     853  long npix= nbData;
     854  fits_write_img(fptr_,TDOUBLE,1,npix,map,&status);
     855  if( status ) printerror( status, "erreur ecriture putImageToFits" );
     856  writeSignatureOnFits();
     857}
     858
     859void FitsOutFile::putImageToFits(int nbData, float* map) const
     860{
     861  int status = 0;
     862  long npix= nbData;
     863  fits_write_img(fptr_,TFLOAT,1,npix, map,&status);
     864  if( status ) printerror( status, "erreur ecriture putImageToFits" );
     865  writeSignatureOnFits();
     866
     867}
     868void FitsOutFile::putImageToFits( int nbData, int* map) const
     869{
     870  int status = 0;
     871
     872  long npix= nbData;
     873  fits_write_img(fptr_,TINT,1,npix,map,&status);
     874  if( status ) printerror( status, "erreur ecriture putImageToFits" );
     875  writeSignatureOnFits();
     876}
     877
     878
     879
     880void FitsOutFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
    813881{
    814882  int status = 0;
     
    817885    {
    818886      cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
    819               throw ParmError("FitsFile:: fields and types don't match");
     887      throw ParmError("FitsFile:: fields and types don't match");
    820888
    821889    }
     
    824892  char largeur[FLEN_VALUE];
    825893  int noColString=0;
    826 
    827 
    828894  int k;
    829895  for (k=0; k<tfields;k++)
     
    847913          if (fieldType[k] == 'A')
    848914            {
    849             char largaux[FLEN_VALUE];
    850             sprintf(largeur,"%d",taille_des_chaines[noColString]);
    851             sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
    852             noColString++;
    853             strcpy(format, largaux);   
     915              char largaux[FLEN_VALUE];
     916              sprintf(largeur,"%d",taille_des_chaines[noColString]);
     917              sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
     918              noColString++;
     919              strcpy(format, largaux);   
    854920            }
    855921          else strcpy(format,"1024");
     
    930996}
    931997
    932 void FitsFile::putColToFits(int nocol, int nentries, double* donnees) const
     998void FitsOutFile::putColToFits(int nocol, int nentries, double* donnees) const
    933999{
    9341000  int status = 0;
     
    9521018  if( status )  printerror( status,"erreur ecriture du fichier fits" );
    9531019}
    954 void FitsFile::putColToFits(int nocol, int nentries, float* donnees) const
     1020void FitsOutFile::putColToFits(int nocol, int nentries, float* donnees) const
    9551021{
    9561022  int status = 0;
     
    9781044  if( status )  printerror( status,"erreur ecriture du fichier fits" );
    9791045}
    980 void FitsFile::putColToFits(int nocol, int nentries, int* donnees) const
     1046void FitsOutFile::putColToFits(int nocol, int nentries, int* donnees) const
    9811047{
    9821048  int status = 0;
     
    10041070  if( status )  printerror( status," ecriture du fichier fits" );
    10051071}
    1006 void FitsFile::putColToFits(int nocol, int nentries, char** donnees) const
     1072void FitsOutFile::putColToFits(int nocol, int nentries, char** donnees) const
    10071073{
    10081074  int status = 0;
     
    10321098
    10331099
    1034 
    1035 
    1036 
    1037 
    1038 void FitsFile::writeSignatureOnFits() const
     1100void FitsOutFile::writeSignatureOnFits() const
    10391101{
    10401102  int status = 0;
     
    10601122
    10611123
    1062 void FitsFile::printerror(int &status)
    1063   //*****************************************************/
    1064   //* Print out cfitsio error messages and exit program */
    1065   //*****************************************************/
    1066 {
    1067   if( status )
    1068     {
    1069       fits_report_error(stderr,status);
    1070       throw IOExc("FitsFile:: error FITSIO status");
    1071     }
    1072   return;
    1073 }
    1074 
    1075 void FitsFile::printerror(int& status, char* texte)
    1076   //*****************************************************/
    1077   //* Print out cfitsio error messages and exit program */
    1078   //*****************************************************/
    1079 {
    1080   // print out cfitsio error messages and exit program
    1081   // print error report 
    1082   fits_report_error(stderr, status);
    1083   cout << " erreur:: " << texte << endl;
    1084   throw IOExc("FitsFile:: error FITSIO status");
    1085 }
    1086 
    1087 int FitsFile::statusF() const
    1088 {
    1089   return fits_status_;
    1090 }
    1091 void FitsFile::ResetStatus(int& status)
    1092 {
    1093   fits_status_ = status;
    1094   status = 0;
    1095 }
    1096 
    1097 string FitsFile::getErrStatus(int status)
    1098 {
    1099   char text[31];
    1100   fits_get_errstatus(status, text);
    1101   return string(text);
    1102 }
     1124
Note: See TracChangeset for help on using the changeset viewer.