Changeset 1353 in Sophya for trunk/SophyaExt/FitsIOServer


Ignore:
Timestamp:
Dec 15, 2000, 11:58:21 AM (25 years ago)
Author:
lemeur
Message:

fonction de manipulation directe de headers

Location:
trunk/SophyaExt/FitsIOServer
Files:
4 edited

Legend:

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

    r1352 r1353  
    345345}
    346346
     347bool FitsInFile::hasKeyword(string keyw, int hdunum)
     348     {
     349       bool has=false;
     350       int status = 0;
     351       if (hdunum != hdunum_ )
     352         {
     353           int hdutype;
     354           fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
     355         }
     356
     357       char value[FLEN_VALUE];
     358       char* keyname= const_cast<char*>(keyw.c_str());
     359       fits_read_keyword(fptr_,keyname,value,NULL,&status);
     360       if (status == 0)
     361         has = true;
     362       else
     363         if (status == KEY_NO_EXIST ) status =0;
     364         else fits_report_error(stderr,status);
     365       if (hdunum != hdunum_ )
     366         {
     367           int hdutype;
     368           if (hdunum_ != 0)
     369             fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
     370           else              fits_movabs_hdu(fptr_,1,&hdutype,&status);
     371
     372         }
     373       return has;
     374     }
    347375
    348376void FitsInFile::GetImageParameters (fitsfile* fileptr,FitsDataType& dataType,int& naxis,vector<int>& naxisn)
     
    510538    else
    511539      {
    512         cout << " hdutype= " << (int) hdutype_ << endl;
     540        cout << " hdutype= " << hdutype_ << endl;
    513541        throw PException("FitsFile::NbColsFromFits, HDU  not supported");
    514542      }
     
    528556      else
    529557        {
    530           cout << "hdutype= " << (int) hdutype_ << endl;
     558          cout << "hdutype= " << hdutype_ << endl;
    531559          throw PException("FitsFile::NentriesFromFits, this HDU is not supported");
    532560        }
     
    712740\param <nentries>  number of data to be read
    713741*/
    714 void FitsInFile::GetBinTabFCol(r_8* valeurs,int nentries, int NoCol) const
     742void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
    715743    {
    716744      int status= 0;
     
    749777 same as previous method with float data
    750778*/
    751 void FitsInFile::GetBinTabFCol(r_4* valeurs,int nentries, int NoCol) const
     779void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
    752780    {
    753781      int status= 0;
     
    786814*/
    787815
    788 void FitsInFile::GetBinTabFCol(int_4* valeurs,int nentries, int NoCol) const
     816void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
    789817    {
    790818      int status= 0;
     
    853881\param <nentries>  number of data to be read
    854882*/
    855 void FitsInFile::GetSingleColumn(r_8* map, int nentries) const
     883void FitsInFile::GetSingleColumn(double* map, int nentries) const
    856884{
    857885  int status = 0;
     
    880908    else
    881909      {
    882       cout << " hdutype= " << (int) hdutype_ << endl;
     910      cout << " hdutype= " << hdutype_ << endl;
    883911      throw IOExc("FitsFile::GetSingleColumn, this HDU is unknown");
    884912      }
     
    888916same as above with float data
    889917*/
    890 void FitsInFile::GetSingleColumn(r_4* map, int nentries) const
     918void FitsInFile::GetSingleColumn(float* map, int nentries) const
    891919{
    892920  int status = 0;
     
    913941    else
    914942      {
    915       cout << " hdutype= " << (int) hdutype_ << endl;
     943      cout << " hdutype= " << hdutype_ << endl;
    916944      throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
    917945      }
     
    921949 same as above with int data
    922950*/
    923 void FitsInFile::GetSingleColumn( int_4* map, int nentries) const
     951void FitsInFile::GetSingleColumn( int* map, int nentries) const
    924952{
    925953  int status = 0;
     
    946974    else
    947975      {
    948       cout << " hdutype= " << (int) hdutype_ << endl;
     976      cout << " hdutype= " << hdutype_ << endl;
    949977      throw IOExc("FitsFile::GetSingleColumn this HDU is unknown");
    950978      }
     
    13051333          throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
    13061334        }
     1335
    13071336  // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
    13081337  hdunum_++;
     
    13131342    }
    13141343
    1315   // write supplementary keywords
     1344  // header format FITS
     1345   
     1346   writeAppendedHeaderOnFits();
     1347
     1348  // write supplementary keywords (from SOPHYA)
    13161349  //  dvl.Print();
    13171350  if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
     
    13281361\param <nbData>  number of data to be written
    13291362*/
    1330 void FitsOutFile::PutImageToFits(int nbData, r_8* map) const
     1363void FitsOutFile::PutImageToFits(int nbData, double* map) const
    13311364{
    13321365  int status = 0;
     
    13401373same as previous method with float data
    13411374*/
    1342 void FitsOutFile::PutImageToFits(int nbData, r_4* map) const
     1375void FitsOutFile::PutImageToFits(int nbData, float* map) const
    13431376{
    13441377  int status = 0;
     
    13521385
    13531386 same as previous method with int data */
    1354 void FitsOutFile::PutImageToFits( int nbData, int_4* map) const
     1387void FitsOutFile::PutImageToFits( int nbData, int* map) const
    13551388{
    13561389  int status = 0;
     
    14551488                  NULL,extn,&status);
    14561489  if( status ) printerror( status );
    1457   // on ajoute eventuellement un dvlist prepare
     1490
     1491  int ii;
     1492  for(ii = 0; ii < tfields; ii++)
     1493    {
     1494      delete [] tform[ii];
     1495    }
     1496  delete [] ttype;
     1497  delete [] tform;
     1498
     1499  // on ajoute eventuellement des mots-cles
     1500
    14581501  if ( hdunum_ == 0 )
    14591502    {
     
    14631506    }
    14641507  else  hdunum_++;
    1465   int ii;
    1466   for(ii = 0; ii < tfields; ii++)
    1467     {
    1468       delete [] tform[ii];
    1469     }
    1470   delete [] ttype;
    1471   delete [] tform;
    1472   //
    1473   // write supplementary keywords
     1508
     1509  // header format FITS
     1510   
     1511   writeAppendedHeaderOnFits();
     1512
     1513  // write SOPHYA keywords
    14741514  if (ptr_dvl != NULL) addKeywordsOfDVList(*ptr_dvl);
    14751515}
     1516
     1517 
    14761518
    14771519/*! \fn void SOPHYA::FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
     
    14801522\param <nentries>  number of data to be written
    14811523*/
    1482 void FitsOutFile::PutColToFits(int nocol, int nentries, r_8* donnees) const
     1524
     1525void FitsOutFile::PutColToFits(int nocol, int nentries, double* donnees) const
    14831526{
    14841527  int status = 0;
     
    15091552same as previous method with float data
    15101553*/
    1511 void FitsOutFile::PutColToFits(int nocol, int nentries, r_4* donnees) const
     1554void FitsOutFile::PutColToFits(int nocol, int nentries, float* donnees) const
    15121555{
    15131556  int status = 0;
     
    15411584same as previous method with int data
    15421585*/
    1543 void FitsOutFile::PutColToFits(int nocol, int nentries, int_4* donnees) const
     1586void FitsOutFile::PutColToFits(int nocol, int nentries, int* donnees) const
    15441587{
    15451588  int status = 0;
     
    17021745      char strval[FLEN_VALUE]= "";
    17031746      char *comkey = "COMMENT";
    1704       fits_read_keyword(fptr_, keyname, strval, NULL, &status);
    1705       if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
     1747      //      fits_read_keyword(fptr_, keyname, strval, NULL, &status);
     1748      //  if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 )
    17061749        {
    17071750          string coco = dvl.GetComment(key);
     
    17621805    }
    17631806
     1807
     1808/*! \fn void  FitsOutFile::appendInHeader(FitsInFile& infits, int hdunum)
     1809
     1810get a header from FitsInFile and append to the header beeing built
     1811(shifting mandatory keywords)
     1812*/
     1813
     1814void FitsOutFile::appendInputHeader(FitsInFile& infits, int hdunum)
     1815{
     1816  int status = 0;
     1817  int hdutype;
     1818  fitsfile* fptr=infits.fitsfilePtr();
     1819  fits_movabs_hdu(fptr,hdunum,&hdutype,&status);
     1820  if( status ) fits_report_error(stderr,status);
     1821
     1822  // get number of keywords
     1823  int nkeys,keypos;
     1824  fits_get_hdrpos(fptr,&nkeys,&keypos,&status);
     1825  if( status ) fits_report_error(stderr,status);
     1826  // shift with the number of mandatory keywords
     1827  int num= 0;
     1828  // if primary header
     1829    if (hdunum == 1)
     1830      {
     1831        // read NAXIS
     1832        int naxis=0;
     1833        fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
     1834        // number of mandatory keywords
     1835        num = naxis+3;
     1836      }
     1837    // extensions
     1838    else
     1839      {
     1840        if (hdutype == IMAGE_HDU)
     1841          {
     1842            // read NAXIS
     1843            int naxis=0;
     1844            fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status);
     1845            // number of mandatory keywords
     1846            num = naxis+5;
     1847          }
     1848        else
     1849          if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
     1850            {
     1851              // number of mandatory keywords
     1852              num = 8;
     1853            }
     1854      }
     1855  int j;
     1856  char keyname[LEN_KEYWORD]; 
     1857  char value[FLEN_VALUE];
     1858  char comment[FLEN_COMMENT];
     1859  for(j = num+1; j <= nkeys; j++)
     1860    {
     1861      char dtype;
     1862      fits_read_keyn(fptr,j,keyname,value,comment,&status);
     1863      if(status)
     1864        {
     1865          fits_report_error(stderr,status);
     1866          status=0;
     1867        }
     1868      string  kn(keyname);
     1869      string  cm(comment);
     1870      string  val(value);
     1871      FitsKeyword kw(kn, val, cm);
     1872      mots_cles_.push_back(kw);
     1873    }
     1874}
     1875void FitsOutFile::writeAppendedHeaderOnFits()
     1876{
     1877  for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
     1878    {
     1879      (*it).writeOnFits(fptr_);
     1880    }
     1881  mots_cles_.clear();
     1882}
     1883
     1884void FitsOutFile::insertKeywordOnHeader(string keyname, double value, string comment)
     1885{
     1886  char* cvalue = new char[16];
     1887  sprintf(cvalue,"%e",value);
     1888  //  char* kn = const_cast<char*>(keyname.c_str());
     1889  //  char* cm = const_cast<char*>(comment.c_str());
     1890  FitsKeyword kw(keyname, string(cvalue), comment);
     1891  mots_cles_.push_back(kw);
     1892}
     1893
     1894void FitsOutFile::insertCommentLineOnHeader(string comment)
     1895{
     1896  FitsKeyword kw(comment);
     1897  mots_cles_.push_back(kw);
     1898}
     1899
     1900void FitsOutFile::PrintHeaderToBeAppended()
     1901{
     1902  cout << " contenu du header en cours de fabrication " << endl;
     1903  for (list<FitsKeyword>::iterator it=mots_cles_.begin(); it !=mots_cles_.end(); it++)
     1904    {
     1905      (*it).Print();
     1906    }
     1907}
     1908
     1909
     1910FitsKeyword::FitsKeyword()
     1911  {
     1912    datatype_=' ';
     1913    keyname_ = string("");
     1914    dvalue_=0.;
     1915    ivalue_=1;
     1916    svalue_=string("");
     1917    comment_=string("");
     1918  }
     1919
     1920FitsKeyword::FitsKeyword(string comment)
     1921  {
     1922    datatype_=' ';
     1923    keyname_=string("COMMENT");
     1924    comment_=comment;
     1925  }
     1926
     1927FitsKeyword::FitsKeyword(string keyname, string value, string comment) :  keyname_(keyname), comment_(comment)
     1928  {
     1929    int status=0;
     1930    char dtype;
     1931    const char* val= value.c_str();
     1932    char* valk = const_cast<char*>(val);
     1933    fits_get_keytype(valk,&dtype,&status);
     1934    if(status)
     1935      {
     1936        status=0;
     1937        datatype_=' ';
     1938      }
     1939    else     datatype_=dtype;
     1940   
     1941    switch( datatype_ ) 
     1942      {
     1943      case 'C':
     1944        {
     1945          svalue_ = string(val);
     1946            break;
     1947        }
     1948      case 'I':
     1949        {
     1950          ivalue_ = atoi(val);
     1951          break;
     1952        }
     1953      case 'L':
     1954        {
     1955          //      cout << " ATTENTION : pb a regler avec logical!" << endl;
     1956          if (value[0] == 'T') ivalue_ = 1;
     1957          else ivalue_ = 0;
     1958          break;
     1959          }
     1960      case 'F':
     1961        {
     1962          dvalue_ = atof(val);
     1963          break;
     1964        }
     1965      case 'X':
     1966        {
     1967          throw IOExc("FitsKeyword , complex keyword value not supported");
     1968        }
     1969      }
     1970  }
     1971
     1972void FitsKeyword::writeOnFits(fitsfile* ptr)
     1973  {
     1974    int status=0;
     1975    //    char* keyname = new char[LEN_KEYWORD];
     1976    //   char* comment = new char[FLEN_COMMENT];
     1977    char* keyname;
     1978    char* comment;
     1979    keyname = const_cast<char*>(keyname_.c_str());
     1980    comment = const_cast<char*>(comment_.c_str());
     1981  // get number of keywords
     1982  int nkeys,keypos;
     1983  fits_get_hdrpos(ptr,&nkeys,&keypos,&status);
     1984    switch( datatype_ ) 
     1985      {
     1986      case 'C':
     1987        {
     1988          char value[FLEN_VALUE];
     1989          strncpy(value,svalue_.c_str(),FLEN_VALUE) ;
     1990          fits_write_key(ptr,TSTRING,keyname,&value, comment,&status);
     1991          fits_report_error(stderr,status);
     1992          break;
     1993        }
     1994      case 'I':
     1995        {
     1996          fits_write_key(ptr,TINT,keyname,&ivalue_, comment,&status);
     1997          fits_report_error(stderr,status);
     1998          break;
     1999        }
     2000      case 'L':
     2001        {
     2002          fits_write_key(ptr,TLOGICAL,keyname,&ivalue_, comment,&status);
     2003          fits_report_error(stderr,status);
     2004          break;
     2005        }
     2006      case 'F':
     2007        {
     2008          fits_write_key(ptr,TDOUBLE,keyname,&dvalue_, comment,&status);
     2009          fits_report_error(stderr,status);
     2010          break;
     2011        }
     2012      case 'X':
     2013        {
     2014          cout << "FitsKeyword : complex keyword value not supported" << endl;;
     2015        }
     2016      default :
     2017        {
     2018          char *comkey = "COMMENT";
     2019          if(strncmp(keyname,comkey,LEN_KEYWORD-1) == 0)
     2020            {
     2021              fits_write_comment(ptr,comment,&status);
     2022              fits_report_error(stderr,status);
     2023            }
     2024          else
     2025            {
     2026              cout << " WARNING  (FitsKeyword::writeOnFits) : unrecognized keyword : " << keyname_ << endl;
     2027            }
     2028        }
     2029      }
     2030  }
     2031
     2032void FitsKeyword::Print()
     2033  {
     2034    switch( datatype_ ) 
     2035      {
     2036      case 'C':
     2037        {
     2038          cout << " mot cle : " << keyname_ << " valeur : " << svalue_ << " commentaire : " << comment_ <<endl;
     2039          break;
     2040        }
     2041      case 'I':
     2042        {
     2043          cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
     2044          break;
     2045        }
     2046      case 'L':
     2047        {
     2048          cout << " mot cle : " << keyname_ << " valeur : " << ivalue_ << " commentaire : " << comment_ <<endl;
     2049          break;
     2050        }
     2051      case 'F':
     2052        {
     2053          cout << " mot cle : " << keyname_ << " valeur : " << dvalue_ << " commentaire : " << comment_ <<endl;
     2054          break;
     2055        }
     2056      case 'X':
     2057        {
     2058          cout << "FitsKeyword : complex keyword value not supported" << endl;;
     2059        }
     2060      default :
     2061        {
     2062          cout << " mot cle : " << keyname_ << " commentaire : "  << comment_ <<endl;
     2063        }
     2064      }
     2065  }
  • trunk/SophyaExt/FitsIOServer/fitsfile.h

    r1352 r1353  
    2020  class FitsInFile;
    2121  class FitsOutFile;
    22  
     22
     23
     24
     25class  FitsKeyword
     26{
     27
     28 public:
     29
     30FitsKeyword();
     31FitsKeyword(string comment);
     32FitsKeyword(string keyname, string value, string comment);
     33void writeOnFits(fitsfile* ptr);
     34
     35void Print();
     36
     37 private:
     38
     39  char datatype_;
     40  string keyname_;
     41  double dvalue_;
     42  int ivalue_;
     43  string svalue_;
     44  string comment_;
     45};
     46
     47
    2348
    2449//
    2550//! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
    2651
    27  class FitsIOHandler {
     52class FitsIOHandler {
    2853
    2954
     
    77102   inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;}
    78103   inline int currentHeaderIndex() {return hdunum_;}
     104   inline fitsfile* fitsfilePtr() {return fptr_;}
    79105 
    80106
     
    107133   int  NbBlocks();
    108134   static void GetBlockType(char flnm[], int hdunum, FitsExtensionType& typeOfExtension, int& naxis, vector<int>& naxisn, FitsDataType& dataType, DVList& dvl  );
    109    void        ReadHeader(int hdunum);
    110  
     135
     136   void  ReadHeader(int hdunum);
     137bool hasKeyword(string keyw, int hdunum);
     138
    111139  /*! \return a reference on a DVList containing the keywords from FITS file */
    112140  inline const DVList& DVListFromFits() const { return dvl_;}
     
    157185/*! \return true if the current header  corresponds to a FITS ASCII or BINTABLE extension */
    158186inline bool IsFitsTable() const {return (hdutype_ == FitsExtensionType_ASCII_TBL || hdutype_ == FitsExtensionType_BINARY_TBL);}
     187
     188
    159189
    160190
     
    195225  void    GetSingleColumn(int_4* map, int nentries) const;
    196226
     227
     228
     229
     230
    197231  private :
    198232
     
    253287
    254288   void makeHeaderBntblOnFits ( string fieldType, vector<string> Noms, int nentries, int tfields, DVList* dvl, string extname,  vector<int> taille_des_chaines) ;
     289   void appendInputHeader(FitsInFile& headerin, int hdunum);
     290   void writeAppendedHeaderOnFits();
     291   void PrintHeaderToBeAppended();
     292   void insertCommentLineOnHeader(string comment);
     293   void insertKeywordOnHeader(string keyname, double value, string comment); 
    255294   void PutColToFits(int nocol, int nentries, r_8* donnees) const;
    256295   void PutColToFits(int nocol, int nentries, r_4* donnees) const;
     
    277316
    278317  DVList* dvlToPrimary_; /**< for transferring keywords when creating primary header */
     318  list<FitsKeyword>  mots_cles_;
     319
    279320 };
    280321
     
    292333   vector<string>  cdata_;
    293334   vector<string> ColName_;
    294 
    295335 };
    296336
    297337
     338
     339
     340
    298341} // Fin du namespace
    299342
    300 
    301343#endif
  • trunk/SophyaExt/FitsIOServer/fitsspherehealpix.cc

    r1334 r1353  
    6363  ownobj_ = false;
    6464}
    65 
    6665
    6766
  • trunk/SophyaExt/FitsIOServer/fitsspherehealpix.h

    r1322 r1353  
    3030void Mollweide_picture_projection(char flnm[]);
    3131void sinus_picture_projection(char flnm[]);
     32 void ReWriteFromFitsToFits(FitsInFile& headerin, FitsOutFile& os);
    3233
    3334
Note: See TracChangeset for help on using the changeset viewer.