Ignore:
Timestamp:
Apr 27, 2000, 6:09:34 PM (25 years ago)
Author:
ansari
Message:

mise a jour 27/04/00 GLM

File:
1 edited

Legend:

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

    r949 r971  
    7474        vector<char> types;   
    7575        vector<int>  taille_des_chaines;
    76         GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines); 
    77         for (int k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
     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;
    7879        if(hdutype == ASCII_TBL)
    7980          {
     
    104105      }
    105106 
    106   fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
    107   if( status ) printerror( status,"getBlockType: erreur movabs, pour dvlist");
    108   KeywordsIntoDVList(fileptr, dvl);
     107  KeywordsIntoDVList(fileptr, dvl, hdunum);
    109108  fits_close_file(fileptr,&status);
    110109}
     
    114113  int status = 0;
    115114  hdutype_= 0;
    116   hdunum_ = 0;
     115 
    117116  fits_open_file(&fptr_,flnm,READONLY,&status);
    118117  if( status ) printerror( status );
    119 
    120   // move to the specified HDU number
     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}
     151
     152
     153void FitsFile::moveToFollowingHeader()
     154{
     155  int status = 0;
    121156  int hdutype;
    122   fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
    123   if( status ) printerror( status );
    124  
     157  fits_movrel_hdu(fptr_, 1,&hdutype,&status);
     158  if( status ) printerror( status," lecture du header suivant" );
     159  hdunum_++;
    125160  hdutype_= hdutype;
    126   hdunum_ = hdunum;
    127161  if(hdutype_ == IMAGE_HDU)
    128162    {
    129163      GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
    130164      nbData_ =  1;
    131       for (int k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
    132       fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
    133       if( status ) printerror( status);
    134       KeywordsIntoDVList(fptr_, dvl_);
     165      int k;
     166      for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
     167      KeywordsIntoDVList(fptr_, dvl_,hdunum_);
    135168    }
    136169  if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
    137170    {
    138171      GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
    139   fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
    140   if( status ) printerror( status);
    141   KeywordsIntoDVList(fptr_, dvl_);
    142     }
    143   ReadFromFits(*this);
    144 
    145 }
    146 void FitsFile::WriteF(char flnm[],int hdunum)
     172      KeywordsIntoDVList(fptr_, dvl_, hdunum_);
     173    }
     174}
     175
     176 
     177void FitsFile::WriteF(char flnm[], bool OldFile)
    147178{
    148179  int status = 0;
    149180  hdutype_= 0;
    150   hdunum_ = hdunum;
     181  hdunum_ = 0;
     182
     183 
    151184  // create new FITS file
    152   fits_create_file(&fptr_,flnm,&status);
    153   if( status ) printerror(status,"file already exists");
     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    }
    154200  WriteToFits(*this);
    155201}
     
    245291      }
    246292}
    247 void FitsFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn) const
     293void FitsFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn)
    248294{
    249295  int status = 0;
    250296  long naxis = nbdim;
    251297  long* naxes = new long[nbdim];
    252   for (int k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
     298  int k;
     299  for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
    253300  if (type == 'D')
    254301    fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
     
    264311          throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
    265312        }
     313
     314  hdunum_++;
    266315  delete [] naxes;
    267316  if( status ) printerror( status );
     
    320369  if (nfound != naxis )
    321370    cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
    322   for (int k=0; k<naxis; k++)
     371  int k;
     372  for (k=0; k<naxis; k++)
    323373    {
    324374      naxisn.push_back( (int)naxes[k] );
     
    327377}
    328378
    329 void FitsFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl)
    330 {
     379void FitsFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
     380{
     381  int status = 0;
     382  int hdutype;
     383  fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
     384  if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
    331385  // get number of keywords
    332386  int nkeys,keypos;
    333   int status = 0;
    334387  fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
    335388  if( status ) printerror( status );
     
    345398  int num= 8;
    346399
    347   for(int j = num+1; j <= nkeys; j++)
     400  int j;
     401  for(j = num+1; j <= nkeys; j++)
    348402    {
    349403      fits_read_keyn(fileptr,j,card,strval,NULL,&status);
     
    403457        }
    404458      long nels=nentries;
     459      int    anull;
    405460      // no checking for undefined pixels
    406       int    anull;
    407       float dnull= 0.;
     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        }
    408473      fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
    409474                            &anull,&status);
    410475      if( status ) printerror( status,"erreur lecture de colonne" );
     476     
    411477    }
    412478
     
    422488        }
    423489      long nels=nentries;
     490      int    anull;
    424491      // no checking for undefined pixels
    425       int    anull;
    426492      float fnull= 0.;
     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        }
    427504      fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
    428505                            &anull,&status);
     
    431508  void FitsFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
    432509    {
     510      cout <<" entree GetBinTabFCol " << endl;
    433511      int status= 0;
    434512      int DTYPE;
     
    443521      int    anull;
    444522      int inull= 0;
     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        }
    445534      fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
    446535                            &anull,&status);
     
    460549      // no checking for undefined pixels
    461550      int    anull;
    462       char* cnull= " ";
     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        }
    463557      long frow=1;
    464558      long felem=1;
     
    521615    }
    522616  int index=-1;
    523   for (int k=0; k<=nocol; k++)
     617  int k;
     618  for (k=0; k<=nocol; k++)
    524619    {
    525620      if (types_[k] == 'S') index++;
     
    617712
    618713
    619 void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) const
     714void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines)
    620715{
    621716  int status = 0;
     
    633728
    634729
    635   for (int k=0; k<tfields;k++)
     730  int k;
     731  for (k=0; k<tfields;k++)
    636732    {
    637733      char  format[FLEN_VALUE];
     
    671767      strcpy(tform[k],format);
    672768    }
    673   const char* TypeOfContent= dvl.GetS("TypeOfContent").c_str();
    674769  // value of the EXTNAME keyword
    675770  char extn[FLEN_VALUE];
     
    684779  if( status ) printerror( status );
    685780
    686   for(int ii = 0; ii < tfields; ii++)
     781  hdunum_++;
     782  int ii;
     783  for(ii = 0; ii < tfields; ii++)
    687784    {
    688785      delete [] ttype[ii];
     
    696793  // get names and values from the join DVList object
    697794  //  dvl.Print();
     795  fits_write_comment(fptr_,"--------------------------------------", &status);
    698796  DVList::ValList::const_iterator it;
    699797  for(it = dvl.Begin(); it != dvl.End(); it++)
     
    708806          {
    709807            int ival=(*it).second.elval.mtv.iv;
    710             strcpy(comment,"I entier");
     808            strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
    711809            fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
    712810            break;
     
    715813          {
    716814            double dval=(*it).second.elval.mtv.dv;
    717             strcpy(comment,"D double");
     815            strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
    718816            fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
    719817            break;
     
    723821            char strval[128];
    724822            strncpy(strval,(*it).second.elval.mtv.strv,127);
    725             strcpy(comment,"S character string");
     823            strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
    726824            fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
    727825            break;
     
    730828  if( status )  printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
    731829    }
     830  fits_write_comment(fptr_,"--------------------------------------", &status);
    732831
    733832}
     
    736835{
    737836  int status = 0;
    738   int hdutype, hdunum;
     837  int hdutype;
    739838  fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
    740839  if( status ) printerror(status,"putColToFits: le movabs a foire");
     
    745844     throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
    746845          }
    747   //  if(hdutype == ASCII_TBL && nocol>0)
    748   //  {
    749   //   throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0  forbidden");
    750   //  }
    751846  int code;
    752847  long repeat, width;
     
    806901  if( code != TLONG &&  code != TINT &&  code != TSHORT )
    807902    {
    808       cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
     903      cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
    809904    }
    810905  fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
    811   if( status )  printerror( status,"erreur ecriture du fichier fits" );
     906  if( status )  printerror( status," ecriture du fichier fits" );
    812907}
    813908void FitsFile::putColToFits(int nocol, int nentries, char** donnees) const
     
    857952
    858953  cout << " Header listing for HDU : " << hdunum_ << endl;
    859   for(int jj = 1; jj <= nkeys; jj++) 
     954  int jj;
     955  for(jj = 1; jj <= nkeys; jj++) 
    860956    {
    861957      if( fits_read_record(fptr_,jj,card,&status) )
     
    875971  char strval[FLEN_VALUE];
    876972  char comment[FLEN_COMMENT];
    877  
    878   strcpy(keyname,"CREATOR");
    879   strcpy(strval,"SOPHYA");
    880   strcpy(comment,"SOPHYA Package - FitsFile");
    881   fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
     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 );
    882986}
    883987
Note: See TracChangeset for help on using the changeset viewer.