CARMA C++
FitsIo.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_FITSIO_H
2 #define SZA_UTIL_FITSIO_H
3 
11 #include <string>
12 
13 #include "carma/szautil/DataType.h"
14 #include "carma/szautil/DecAngle.h"
15 #include "carma/szautil/HourAngle.h"
16 #include "carma/szautil/Frequency.h"
17 #include "carma/szautil/VisIo.h"
18 
19 // Functions of the following type are called by put_phdu() to
20 // convert a passed string value to the appropriate type.
21 
22 #define WRITEFN(fn) void (fn)(char *buf, const char *val);
23 
24 // Functions of the following type are called by read_phdu() to
25 // convert a passed string value to the appropriate type.
26 
27 #define READFN(fn) void (fn)(FitsIo::FitsDataType& data, char *str);
28 
29 namespace sza {
30  namespace util {
31 
32  class FitsIo : public VisIo {
33  public:
34 
38  FitsIo();
39 
43  virtual ~FitsIo();
44 
45  //------------------------------------------------------------
46  // Some const members of this class
47  //------------------------------------------------------------
48 
49  // Length of an ASCII header
50 
51  static const unsigned nBytePerHeader_=80;
52 
53  // Number of headers per data record
54 
55  static const unsigned nHeaderPerDataRecord_ = 36;
56 
57  // Length of a data record
58 
59  static const unsigned nBytePerDataRecord_ =
60  nBytePerHeader_ * nHeaderPerDataRecord_;
61 
62  // Length of the header keyword.
63 
64  static const unsigned nBytePerKeyword_=8;
65 
66  // A logical value 'T' or 'F' for logical keywords must occur in
67  // this position in a string:
68 
69  static const unsigned logPos_=30;
70 
71  // The length of a single record of the frequency table
72 
73  static const unsigned nBytePerFrequencyTableEntry_ = 32;
74 
75  // The number of seconds per day
76 
77  static const unsigned secondsPerDay_ = 86400;
78 
79  // Enumerate recognized Fits axis types.
80 
81  enum FitsAxis {
82  AX_DEG, /* Degrees -- type of RA/DEC */
83  AX_RAD,
84  AX_UV,
85  AX_UNKNOWN
86  };
87 
88  // And a container for associating a header string with this
89  // unit.
90 
91  struct FitsAxisCard {
92  char *str_;
93  char *label_;
94  FitsIo::FitsAxis axis_;
95  };
96 
97  // A static list of recognized FITS axis types
98 
99  static FitsAxisCard fitsAxes_[];
100  static unsigned nFitsAxes_;
101 
102  // Enumerate recognized Fits bunits.
103 
104  enum FitsBunit {
105  BU_MJYSR, /* MJy/sr */
106  BU_JYBEAM, /* Jy/Beam */
107  BU_MUK, /* MicroKelvin */
108  BU_UNKNOWN
109  };
110 
111  // And a container for associating a header string with this
112  // bunit.
113 
114  struct FitsBunitCard {
115  char *str_;
116  char *label_;
117  FitsBunit bunit_;
118  };
119 
120  // A struct for handling all possible FITS data types
121 
122  struct FitsDataType {
123  sza::util::DataType stdVal_;
124  std::string stringVal_;
125  FitsIo::FitsBunit bunitVal_;
126  FitsIo::FitsAxis axisVal_;
127  };
128 
129  // A static list of recognized FITS axis types
130 
131  static FitsBunitCard fitsUnits_[];
132  static unsigned nFitsUnits_;
133 
134  // Declare a container for a single header card.
135 
136  struct Phdu {
137  char name_[FitsIo::nBytePerKeyword_+1];
138  READFN(*readfn_);
139  WRITEFN(*writefn_);
140  int required_;
141  };
142 
143  static FitsIo::Phdu phdus_[];
144  static unsigned nPhdus_;
145 
146  // Write a header key to a file.
147 
148  void putPhdu(const char *name, const char *val, const char *comment,
149  FILE *fp=0);
150 
151  // Methods for formatting various FITS keyword types
152 
153  static void nullStr(char *buf, const char *val);
154  static void logStr(char *buf, const char *val);
155  static void intStr(char *buf, const char *val);
156  static void fltStr(char *buf, const char *val);
157  static void strStr(char *buf, const char *val);
158 
159  // Methods for reading various FITS keyword types
160 
161  static void rdNull(FitsDataType& data, char *str);
162  static void rdLog(FitsDataType& data, char *str);
163  static void rdStr(FitsDataType& data, char *str);
164  static void rdInt(FitsDataType& data, char *str);
165  static void rdFlt(FitsDataType& data, char *str);
166  static void rdBunit(FitsDataType& data, char *str);
167  static void rdAxis(FitsDataType& data, char *str);
168 
169  //------------------------------------------------------------
170  // Methods to write headers
171  //------------------------------------------------------------
172 
173  void setDate();
174 
175  // All headers will be initialized and finished the same way
176 
177  void initHeader();
178  void finishHeader(FILE* fp=0);
179 
180  // Methods to write the bodies for different types of header
181 
182  void writeFileHeaderBody(FILE* fp=0);
183  void writeAntennaTableHeaderBody(FILE* fp=0);
184  void writeFrequencyTableHeaderBody(FILE* fp=0);
185 
186  // Methods to write the whole header, for different header types
187 
188  void writeFileHeader(FILE* fp=0);
189  void writeAntennaTableHeader(FILE* fp=0);
190  void writeFrequencyTableHeader(FILE* fp=0);
191 
192  //------------------------------------------------------------
193  // Methods to write the data segment
194  //------------------------------------------------------------
195 
196  void initVisibilityData();
197  void writeVisibilityDataBody(double* vis, double* date, double* uvw, FILE* fp=0);
198  void writeFakeVisibilityDataBody(double* vis, double* date, double* uvw, FILE* fp=0);
199  void finishVisibilityData(FILE* fp=0);
200 
201  void writeVisibilityData(double* vis, double* date, double* uvw, FILE* fp=0);
202  void writeFakeVisibilityData(double* vis, double* date, double* uvw, FILE* fp=0);
203 
204  //------------------------------------------------------------
205  // Methods to write the antenna table
206  //------------------------------------------------------------
207 
208  void initAntennaTableDataBody();
209  void writeAntennaTableDataBody(FILE* fp=0);
210  void finishAntennaTableDataBody(FILE* fp=0);
211 
212  // Write the data for an antenna table
213 
214  void writeAntennaTableData(FILE* fp=0);
215  void writeAntennaTableEntry(int i, double X, double Y, double Z, FILE* fp=0);
216 
217  // Write the whole antenna table
218 
219  void writeAntennaTable(FILE* fp=0);
220 
221  //------------------------------------------------------------
222  // Methods to write the frequency table
223  //------------------------------------------------------------
224 
225  void initFrequencyTableDataBody();
226  void writeFrequencyTableDataBody(FILE* fp=0);
227  void finishFrequencyTableDataBody(FILE* fp=0);
228 
229  void writeFrequencyTableData(FILE* fp=0);
230  void writeFrequencyTable(FILE* fp=0);
231 
232  //-----------------------------------------------------------------------
233  // Write a UVF file
234  //-----------------------------------------------------------------------
235 
236  void writeUvfFile(double* data, double* date, double* uvw, FILE* fp=0);
237  void writeFakeUvfFile(double* data, double* date, double* uvw, FILE* fp=0);
238 
239  // A method to return whichever is the current file descriptor
240 
241  FILE* getFptr(FILE* fp);
242 
243  void openFile(std::string fileName);
244  void closeFile();
245 
246  private:
247 
248  // Internal counters we will use when writing headers and data
249 
250  unsigned nHdu_;
251  unsigned nDataByte_;
252 
253  //------------------------------------------------------------
254  // Some information needed to write a FITS file
255  //------------------------------------------------------------
256 
257  FILE* fp_;
258 
259  // Utility functions for converting from one Endianness to the
260  // other
261 
262  void cp4r4(unsigned char *dest, unsigned char *orig, size_t nitem);
263 
264  // Orig: 8-byte datatype. Dest: 8-byte datatype with byte
265  // order reversed.
266 
267  void cp8r8(unsigned char *dest, unsigned char *orig, size_t nitem);
268 
269  void fwrite(unsigned int* ptr, size_t nel, FILE* stream);
270  void fwrite(float* ptr, size_t nel, FILE* stream);
271  void fwrite(double* ptr, size_t nel, FILE* stream);
272 
273  }; // End class FitsIo
274 
275  } // End namespace util
276 } // End namespace sza
277 
278 
279 
280 #endif // End #ifndef SZA_UTIL_FITSIO_H
Tagged: Tue Jun 22 22:32:16 UTC 2004.
Tagged: Mon Oct 3 15:32:27 PDT 2005.
Enumerate data types.
Definition: DataType.h:28