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