| 1 | #ifndef  MINIFITS_H_SEEN | 
|---|
| 2 | #define  MINIFITS_H_SEEN | 
|---|
| 3 |  | 
|---|
| 4 | #include <stdio.h> | 
|---|
| 5 | #include <exception> | 
|---|
| 6 | #include <string> | 
|---|
| 7 |  | 
|---|
| 8 | #include "brtypes.h" | 
|---|
| 9 |  | 
|---|
| 10 | /* | 
|---|
| 11 | Classe d'I/O simplifie FITS pour ACQ BAO-radio | 
|---|
| 12 | R. Ansari, C. Magneville -      Fev 2008 | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | using namespace std; | 
|---|
| 16 |  | 
|---|
| 17 | #define MFEX_MAXMSGLEN 160 | 
|---|
| 18 |  | 
|---|
| 19 | class MiniFITSException : public std::exception { | 
|---|
| 20 | public: | 
|---|
| 21 | explicit MiniFITSException(const char * m) throw() ; | 
|---|
| 22 | explicit MiniFITSException(const string& m) throw() ; | 
|---|
| 23 | virtual ~MiniFITSException() throw() ; | 
|---|
| 24 | //! Implementation of std::exception what() method, returning the exception message | 
|---|
| 25 | virtual const char* what() const throw(); | 
|---|
| 26 | virtual string const Msg() const ; | 
|---|
| 27 | private: | 
|---|
| 28 | char msg_[MFEX_MAXMSGLEN]; | 
|---|
| 29 | }; | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | enum MiniFITS_DT { MF_Byte, MF_Int16, MF_Float32 }; | 
|---|
| 33 | enum MiniFITS_Mode { MF_Read, MF_Write }; | 
|---|
| 34 |  | 
|---|
| 35 | class MiniFITSFile { | 
|---|
| 36 | public: | 
|---|
| 37 | MiniFITSFile(); | 
|---|
| 38 | MiniFITSFile(string const & nom, MiniFITS_Mode rwm); | 
|---|
| 39 | MiniFITSFile(const char* nom, MiniFITS_Mode rwm); | 
|---|
| 40 |  | 
|---|
| 41 | ~MiniFITSFile(); | 
|---|
| 42 |  | 
|---|
| 43 | void Open(const char* nom, MiniFITS_Mode rwm); | 
|---|
| 44 | inline void Open(string const & nom, MiniFITS_Mode rwm) | 
|---|
| 45 | { return Open(nom.c_str(), rwm); } | 
|---|
| 46 |  | 
|---|
| 47 | void Close(); | 
|---|
| 48 |  | 
|---|
| 49 | inline bool IsOpen() { return (fip!=NULL) ; } | 
|---|
| 50 | inline MiniFITS_DT DataType() { return dtype; } | 
|---|
| 51 | string DataTypeToString(); | 
|---|
| 52 |  | 
|---|
| 53 | inline size_t NAxes()  { return ((fgnax3)?3:2); } | 
|---|
| 54 | inline size_t NAxis1() { return nax1; } | 
|---|
| 55 | inline size_t NAxis2() { return nax2; } | 
|---|
| 56 | inline size_t NAxis3() { return ((fgnax3)?nax3:0); } | 
|---|
| 57 |  | 
|---|
| 58 | void setDTypeNaxis(MiniFITS_DT dt, size_t na1, size_t na2); | 
|---|
| 59 | void setDTypeNaxis(MiniFITS_DT dt, size_t na1, size_t na2, size_t na3); | 
|---|
| 60 |  | 
|---|
| 61 | // Lecture avec indication de la taille (nb d'elements) et offset | 
|---|
| 62 | int Read(void* data, size_t sz, size_t offset=0); | 
|---|
| 63 | inline int ReadB(Byte* data, size_t nel, size_t offsel=0) | 
|---|
| 64 | { return ( Read((void *)data, nel, offsel) ); } | 
|---|
| 65 | inline int ReadI(Int16* data, size_t nel, size_t offsel=0) | 
|---|
| 66 | { return ( Read((void *)data, nel*sizeof(Int16), offsel*sizeof(Int16)) ); } | 
|---|
| 67 | inline int ReadF(Float32* data, size_t nel, size_t offsel=0) | 
|---|
| 68 | { return ( Read((void *)data, nel*sizeof(Float32), offsel*sizeof(Float32)) ); } | 
|---|
| 69 |  | 
|---|
| 70 | // Ecriture en mode append (fin de fichier) | 
|---|
| 71 | int Write(void* data, size_t sz); | 
|---|
| 72 | inline int WriteB(Byte* data, size_t nel) | 
|---|
| 73 | { return ( Write((void *)data, nel) ); } | 
|---|
| 74 | inline int WriteI(Int16* data, size_t nel) | 
|---|
| 75 | { return ( Write((void *)data, nel*sizeof(Int16)) ); } | 
|---|
| 76 | inline int WriteF(Float32* data, size_t nel) | 
|---|
| 77 | { return ( Write((void *)data, nel*sizeof(Float32)) ); } | 
|---|
| 78 |  | 
|---|
| 79 | // Ajout de mots-cle a l'entete FITS | 
|---|
| 80 | int AddKeyI(const char* key, long val, const char* comm=NULL); | 
|---|
| 81 | inline int AddKeyI(string const& key, long val) | 
|---|
| 82 | { return AddKeyI(key.c_str(), val); } | 
|---|
| 83 | inline int AddKeyI(string const& key, long val, string const& comm) | 
|---|
| 84 | { return AddKeyI(key.c_str(), val, comm.c_str()); } | 
|---|
| 85 | int AddKeyD(const char* key, double val, const char* comm=NULL); | 
|---|
| 86 | inline int AddKeyD(string const& key, double val) | 
|---|
| 87 | { return AddKeyD(key.c_str(), val); } | 
|---|
| 88 | inline int AddKeyD(string const& key, double val, string const& comm) | 
|---|
| 89 | { return AddKeyD(key.c_str(), val, comm.c_str()); } | 
|---|
| 90 | int AddKeyS(const char* key, const char* val, const char* comm=NULL); | 
|---|
| 91 | inline int AddKeyS(string const& key, string const& val) | 
|---|
| 92 | { return AddKeyS(key.c_str(), val.c_str()); } | 
|---|
| 93 | inline int AddKeyS(string const& key, string const& val, string const& comm) | 
|---|
| 94 | { return AddKeyS(key.c_str(), val.c_str(), comm.c_str()); } | 
|---|
| 95 |  | 
|---|
| 96 | string GetKey(string& key) { return GetKey(key.c_str()) ; } | 
|---|
| 97 | string GetKey(const char* key); | 
|---|
| 98 | string GetKeyValue(const char* key); | 
|---|
| 99 |  | 
|---|
| 100 | protected: | 
|---|
| 101 | void FillHeader(); | 
|---|
| 102 | void DecodeHeader(); | 
|---|
| 103 | void Init(); | 
|---|
| 104 |  | 
|---|
| 105 | FILE* fip; | 
|---|
| 106 | MiniFITS_Mode rwmode; | 
|---|
| 107 | MiniFITS_DT dtype; | 
|---|
| 108 | size_t nax1, nax2, nax3; | 
|---|
| 109 | bool fgnax3;  // true -> NAXIS=3 3D file | 
|---|
| 110 | size_t totwsz;  // total bytes ecrits | 
|---|
| 111 | char* header; //  entete FITS | 
|---|
| 112 | int nkeya_; // | 
|---|
| 113 | }; | 
|---|
| 114 |  | 
|---|
| 115 | #endif | 
|---|