| 1 | #ifndef FITSINOUTFILE_H
 | 
|---|
| 2 | #define FITSINOUTFILE_H
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include "machdefs.h"
 | 
|---|
| 5 | #include "pexceptions.h"
 | 
|---|
| 6 | #include "dvlist.h"
 | 
|---|
| 7 | #include "FitsIO/fitsio.h" 
 | 
|---|
| 8 | #include <complex> 
 | 
|---|
| 9 | 
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | namespace SOPHYA {
 | 
|---|
| 13 | 
 | 
|---|
| 14 |   class FitsInOutFile;
 | 
|---|
| 15 |   //class FitsInStream;
 | 
|---|
| 16 |   //class FitsOutStream;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /*! 
 | 
|---|
| 19 |   \ingroup FitsIOServer
 | 
|---|
| 20 |   \brief Exception class used by  FITS file wrapper classes in FitsIOserver module
 | 
|---|
| 21 | */
 | 
|---|
| 22 | class FitsIOException : public IOExc {
 | 
|---|
| 23 |   public:
 | 
|---|
| 24 |   explicit FitsIOException(const char * m) : IOExc(m) {}
 | 
|---|
| 25 |   explicit FitsIOException(const string& m) : IOExc(m) {}
 | 
|---|
| 26 | };
 | 
|---|
| 27 | 
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /*! 
 | 
|---|
| 30 |   \ingroup FitsIOServer
 | 
|---|
| 31 |   \brief  for converting c/c++ types to FITS data types 
 | 
|---|
| 32 | */
 | 
|---|
| 33 | class FitsTypes {
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |  // Conversion de type en constante de type image FITS (XXX_IMG)
 | 
|---|
| 36 |  static int ImageType(uint_1 d) { return BYTE_IMG; }
 | 
|---|
| 37 |  static int ImageType(int_2  d) { return SHORT_IMG; }
 | 
|---|
| 38 |  static int ImageType(int_4  d) { return LONG_IMG; }
 | 
|---|
| 39 |  static int ImageType(r_4  d)   { return FLOAT_IMG; }
 | 
|---|
| 40 |  static int ImageType(r_8  d)   { return DOUBLE_IMG; }
 | 
|---|
| 41 | 
 | 
|---|
| 42 |  // Conversion de type en constante datatype FITS 
 | 
|---|
| 43 |  static int DataType(uint_1 d) { return TBYTE; }
 | 
|---|
| 44 |  static int DataType(int_2  d) { return TSHORT; }
 | 
|---|
| 45 |  static int DataType(uint_2 d) { return TUSHORT; }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |  static int DataType(const int_4  d) 
 | 
|---|
| 48 |    { return (sizeof(long)==4) ? TLONG: TINT; }
 | 
|---|
| 49 |  static int DataType(const uint_4 d) 
 | 
|---|
| 50 |    { return (sizeof(long)==4) ? TULONG: TUINT; }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #ifdef TLONGLONG
 | 
|---|
| 53 |  static int DataType(int_8 d) { return TLONGLONG; }
 | 
|---|
| 54 | #else 
 | 
|---|
| 55 |  static int DataType(int_8 d) 
 | 
|---|
| 56 |    { throw FitsIOException("FitsDataTypes: Unsupported data type int_8"); }
 | 
|---|
| 57 | #endif
 | 
|---|
| 58 | 
 | 
|---|
| 59 |  static int DataType(r_4  d)   { return TFLOAT; }
 | 
|---|
| 60 |  static int DataType(r_8  d)   { return TDOUBLE; }
 | 
|---|
| 61 | 
 | 
|---|
| 62 |  static int DataType(complex<r_4>  d)   { return TCOMPLEX; }
 | 
|---|
| 63 |  static int DataType(complex<r_8>  d)   { return TDBLCOMPLEX; }
 | 
|---|
| 64 | 
 | 
|---|
| 65 |  static int DataType(char*  d)   { return TSTRING; }
 | 
|---|
| 66 | 
 | 
|---|
| 67 |  // Conversion entre type FITS et chaine - exemple TFLOAT -> r_4 
 | 
|---|
| 68 |  static string ImageTypeToTypeString(int ityp);
 | 
|---|
| 69 |  static string DataTypeToTypeString(int ityp);
 | 
|---|
| 70 | };
 | 
|---|
| 71 | 
 | 
|---|
| 72 | //------ Wrapper class for cfitsio library functions
 | 
|---|
| 73 | class FitsInOutFile {
 | 
|---|
| 74 | public :
 | 
|---|
| 75 |   //! File access mode (ReadOnly, ReadWrite, Create)
 | 
|---|
| 76 |  enum FitsIOMode { Fits_RO, Fits_RW, Fits_Create };
 | 
|---|
| 77 | 
 | 
|---|
| 78 |                    FitsInOutFile(); 
 | 
|---|
| 79 |                    FitsInOutFile(string const & name, FitsIOMode mode);
 | 
|---|
| 80 |                    FitsInOutFile(const char* name, FitsIOMode mode);
 | 
|---|
| 81 |                    FitsInOutFile(FitsInOutFile const& fios); 
 | 
|---|
| 82 |  virtual           ~FitsInOutFile();
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 85 |  void              Open(const char* name, FitsIOMode mode);
 | 
|---|
| 86 |  void              Close();
 | 
|---|
| 87 | 
 | 
|---|
| 88 |  void              ShareFitsPtr(FitsInOutFile const& fios);
 | 
|---|
| 89 |  
 | 
|---|
| 90 |  inline fitsfile*  FitsPtr() const { return fptr_; }
 | 
|---|
| 91 |  static float      cfitsioVersion();
 | 
|---|
| 92 |  //! Return the SOPHYA FitsIOServer version 
 | 
|---|
| 93 |  static float      Version() { return 2.0; }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |  //! Return the file name as specified in the constructor (or Open) 
 | 
|---|
| 96 |  inline string     FileName() { return fname_; }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |  //---- Header manipulation methods
 | 
|---|
| 99 |  //! Return total number of HDU's in file
 | 
|---|
| 100 |  int               NbHDUs() const;
 | 
|---|
| 101 |  //! Return current HDU number - starting from 1 , not zero
 | 
|---|
| 102 |  int               CurrentHDU() const;
 | 
|---|
| 103 |  //! Return current HDU Type as IMAGE_HDU or BINARY_TBL or ASCII_TBL 
 | 
|---|
| 104 |  int               CurrentHDUType() const;
 | 
|---|
| 105 |  //! Return current HDU Type as a string IMAGE_HDU or BINARY_TBL or ASCII_TBL 
 | 
|---|
| 106 |  string            CurrentHDUTypeStr() const;
 | 
|---|
| 107 |  
 | 
|---|
| 108 |  //! Move to HDU specified by hdnum - Returns the newly opened HDU type
 | 
|---|
| 109 |  int               MoveAbsToHDU(int hdunum);
 | 
|---|
| 110 |  //! Move to HDU specified by relhdu , relative to the current HDU - Returns the newly opened HDU type
 | 
|---|
| 111 |  int               MoveRelToHDU(int relhdu);
 | 
|---|
| 112 |  //! Move to the next HDU specified by relhdu. Returns the newly opened HDU type (<0 at EOF)
 | 
|---|
| 113 |  int               MoveToNextHDU();
 | 
|---|
| 114 |  //! Skip the first HDU if it contains no data. 
 | 
|---|
| 115 |  bool              SkipEmptyFirstHDU();
 | 
|---|
| 116 | 
 | 
|---|
| 117 |  //---- IMAGE_HDU manipulation methods
 | 
|---|
| 118 |  //! Creates a new HDU of type image (see fits_create_img)
 | 
|---|
| 119 |  void              CreateImageHDU(int bitpix, int naxis, long* naxes);
 | 
|---|
| 120 |  //! Get information about the current image HDU. - return the image type TBYTE,TINT ...
 | 
|---|
| 121 |  int               GetImageHDUInfo(int& naxis, long* naxes) const;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |  //---- BINARY_TBL or ASCII_TBL 
 | 
|---|
| 124 |  //! Return number of rows in a table HDU
 | 
|---|
| 125 |  long              GetNbRows() const;
 | 
|---|
| 126 |  //! Return number of columns in a table HDU
 | 
|---|
| 127 |  int               GetNbCols() const;
 | 
|---|
| 128 |  //! Creation of a new table - tbltyp = BINARY_TBL or ASCII_TBL
 | 
|---|
| 129 |  void              CreateTable(int tbltyp, const char * extname, int ncols, 
 | 
|---|
| 130 |                                char * colnames[], char * tform[], 
 | 
|---|
| 131 |                                char * tunit[], long ininr=0);
 | 
|---|
| 132 |  //! Creation of a new table - tbltyp = BINARY_TBL or ASCII_TBL
 | 
|---|
| 133 |  void              CreateTable(int tbltyp, const string & extname, 
 | 
|---|
| 134 |                                const vector<string> & colnames, 
 | 
|---|
| 135 |                                const vector<string> & tform, 
 | 
|---|
| 136 |                                const vector<string> & tunit, 
 | 
|---|
| 137 |                                long ininr=0);
 | 
|---|
| 138 |  //! Return number of columns, names, types and repeat count
 | 
|---|
| 139 |  long              GetColInfo(vector<string> & colnames, 
 | 
|---|
| 140 |                               vector<int> & coltypes,
 | 
|---|
| 141 |                               vector<long> & repcnt,
 | 
|---|
| 142 |                               vector<long> & width);
 | 
|---|
| 143 | 
 | 
|---|
| 144 |  //! Defines the extension name for the next table creation 
 | 
|---|
| 145 |  inline void        SetNextExtensionName(string const & extname)
 | 
|---|
| 146 |    { next_extname_ = extname; }
 | 
|---|
| 147 |  //! Defines the extension name for the next table creation 
 | 
|---|
| 148 |  inline void        SetNextExtensionName(const char * extname)
 | 
|---|
| 149 |    { next_extname_ = extname; }
 | 
|---|
| 150 |  //! Return the default extension name 
 | 
|---|
| 151 |  inline string      NextExtensionName() const
 | 
|---|
| 152 |    { return next_extname_; }
 | 
|---|
| 153 | 
 | 
|---|
| 154 |  //! Defines default table type for created tables as BINARY_TBL
 | 
|---|
| 155 |  inline void        SetDef_BinTable() { def_tbltype = BINARY_TBL; }
 | 
|---|
| 156 |  //! Defines default table type for created tables as ASCII_TBL
 | 
|---|
| 157 |  inline void        SetDef_AscTable() { def_tbltype = ASCII_TBL; }
 | 
|---|
| 158 |  //! Return default table type
 | 
|---|
| 159 |  inline int         GetDef_TableType() { return def_tbltype; }
 | 
|---|
| 160 | 
 | 
|---|
| 161 |  //! Defines default column width for strings (Aw) 
 | 
|---|
| 162 |  inline void        SetDef_StrColWidth(long w=16) { def_strcolw = w; }
 | 
|---|
| 163 |  //! Return default column width for strings (Aw)
 | 
|---|
| 164 |  inline long        GetDef_StrColWidth() { return def_strcolw; } 
 | 
|---|
| 165 | 
 | 
|---|
| 166 |  //! Insert (add) a new column
 | 
|---|
| 167 |  void              InsertColumn(int numcol, const char* colname, const char* fmt);
 | 
|---|
| 168 |  //! Insert (add) a new column
 | 
|---|
| 169 |  inline void       InsertColumn(int numcol, const string& colname, const char* fmt)
 | 
|---|
| 170 |    { InsertColumn(numcol, colname.c_str(), fmt); }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | 
 | 
|---|
| 173 |  // Manipulation des informations de l'entete 
 | 
|---|
| 174 |  //! Retrieve a keyword value from the header 
 | 
|---|
| 175 |   inline string    KeyValue(string const & key)
 | 
|---|
| 176 |     { bool nosk; return KeyValue(key, nosk); }
 | 
|---|
| 177 |  //! Retrieve a keyword value from the header 
 | 
|---|
| 178 |   string           KeyValue(string const & key, bool& nosk);
 | 
|---|
| 179 |  //! Read header records and appends the information to dvl
 | 
|---|
| 180 |  int               GetHeaderRecords(DVList& dvl, 
 | 
|---|
| 181 |                                     bool stripkw= true, bool keepstkey=false);
 | 
|---|
| 182 |  //! Appends a keyword to FITS header
 | 
|---|
| 183 |  void              WriteKey(const char * kname, MuTyV const & val, 
 | 
|---|
| 184 |                             const char *comm=NULL);
 | 
|---|
| 185 |  inline void       WriteKey(string const & kname, MuTyV const & val, string const & comm) 
 | 
|---|
| 186 |    { WriteKey(kname.c_str(), val, comm.c_str()); }
 | 
|---|
| 187 |  //! Write dvl information to fits header 
 | 
|---|
| 188 |  int               WriteHeaderRecords(DVList & dvl);
 | 
|---|
| 189 | 
 | 
|---|
| 190 |  //! Prints information about the fits file on standard output stream (cout)
 | 
|---|
| 191 |  inline  void      Print(int lev=0) const  { Print(cout, lev); }  
 | 
|---|
| 192 |  //! Prints information about the fits file on stream os
 | 
|---|
| 193 |  virtual void      Print(ostream& os, int lev=0)  const;
 | 
|---|
| 194 | 
 | 
|---|
| 195 | protected:
 | 
|---|
| 196 |  fitsfile *fptr_;     //  pointer to the FITS file, defined in fitsio.h 
 | 
|---|
| 197 |  string fname_;       //  File name as passed to creator
 | 
|---|
| 198 |  FitsIOMode mode_;    
 | 
|---|
| 199 |  bool ownfptr;        //  If true, owns the FitsPointer, which will be closed by the destructor
 | 
|---|
| 200 | 
 | 
|---|
| 201 |  // Default extension name 
 | 
|---|
| 202 |  string next_extname_;
 | 
|---|
| 203 |  // Default table type 
 | 
|---|
| 204 |  int def_tbltype;
 | 
|---|
| 205 |  // default column width for strings 
 | 
|---|
| 206 |  long def_strcolw;
 | 
|---|
| 207 | };
 | 
|---|
| 208 | 
 | 
|---|
| 209 | /*! Prints FITS file information on stream \b s ( fio.Print(s) ) */
 | 
|---|
| 210 | 
 | 
|---|
| 211 | inline ostream& operator << (ostream& s, FitsInOutFile const & fio)
 | 
|---|
| 212 |   {  fio.Print(s);  return(s);  }
 | 
|---|
| 213 | 
 | 
|---|
| 214 | } // Fin du namespace
 | 
|---|
| 215 | 
 | 
|---|
| 216 | #endif
 | 
|---|