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