My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
FitsIo.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_FITSIO_H
2 #define GCP_UTIL_FITSIO_H
3 
11 #include <string>
12 
13 #include "gcp/util/common/DataType.h"
14 #include "gcp/util/common/DecAngle.h"
15 #include "gcp/util/common/HourAngle.h"
16 #include "gcp/util/common/Frequency.h"
17 
18 // Functions of the following type are called by put_phdu() to
19 // convert a passed string value to the appropriate type.
20 
21 #define WRITEFN(fn) void (fn)(char *buf, const char *val);
22 
23 // Functions of the following type are called by read_phdu() to
24 // convert a passed string value to the appropriate type.
25 
26 #define READFN(fn) void (fn)(FitsIo::FitsDataType& data, char *str);
27 
28 namespace gcp {
29  namespace util {
30 
31  class FitsIo {
32  public:
33 
37  FitsIo();
38 
42  virtual ~FitsIo();
43 
44  //------------------------------------------------------------
45  // Some const members of this class
46  //------------------------------------------------------------
47 
48  // Length of an ASCII header
49 
50  static const unsigned nBytePerHeader_=80;
51 
52  // Number of headers per data record
53 
54  static const unsigned nHeaderPerDataRecord_ = 36;
55 
56  // Length of a data record
57 
58  static const unsigned nBytePerDataRecord_ =
59  nBytePerHeader_ * nHeaderPerDataRecord_;
60 
61  // Length of the header keyword.
62 
63  static const unsigned nBytePerKeyword_=8;
64 
65  // A logical value 'T' or 'F' for logical keywords must occur in
66  // this position in a string:
67 
68  static const unsigned logPos_=30;
69 
70  // The length of a single record of the frequency table
71 
72  static const unsigned nBytePerFrequencyTableEntry_ = 32;
73 
74  // The number of seconds per day
75 
76  static const unsigned secondsPerDay_ = 86400;
77 
78  // Enumerate recognized Fits axis types.
79 
80  enum FitsAxis {
81  AX_DEG, /* Degrees -- type of RA/DEC */
82  AX_RAD,
83  AX_UV,
84  AX_UNKNOWN
85  };
86 
87  // And a container for associating a header string with this
88  // unit.
89 
90  struct FitsAxisCard {
91  char *str_;
92  char *label_;
93  FitsIo::FitsAxis axis_;
94  };
95 
96  // A static list of recognized FITS axis types
97 
98  static FitsAxisCard fitsAxes_[];
99  static unsigned nFitsAxes_;
100 
101  // Enumerate recognized Fits bunits.
102 
103  enum FitsBunit {
104  BU_MJYSR, /* MJy/sr */
105  BU_JYBEAM, /* Jy/Beam */
106  BU_MUK, /* MicroKelvin */
107  BU_UNKNOWN
108  };
109 
110  // And a container for associating a header string with this
111  // bunit.
112 
113  struct FitsBunitCard {
114  char *str_;
115  char *label_;
116  FitsBunit bunit_;
117  };
118 
119  // A struct for handling all possible FITS data types
120 
121  struct FitsDataType {
122  gcp::util::DataType stdVal_;
123  std::string stringVal_;
124  FitsIo::FitsBunit bunitVal_;
125  FitsIo::FitsAxis axisVal_;
126  };
127 
128  // A static list of recognized FITS axis types
129 
130  static FitsBunitCard fitsUnits_[];
131  static unsigned nFitsUnits_;
132 
133  // Declare a container for a single header card.
134 
135  struct Phdu {
136  char name_[FitsIo::nBytePerKeyword_+1];
137  READFN(*readfn_);
138  WRITEFN(*writefn_);
139  int required_;
140  };
141 
142  static FitsIo::Phdu phdus_[];
143  static unsigned nPhdus_;
144 
145  // Write a header key to a file.
146 
147  void putPhdu(const char *name, const char *val, const char *comment,
148  FILE *fp=0);
149 
150  // Methods for formatting various FITS keyword types
151 
152  static void nullStr(char *buf, const char *val);
153  static void logStr(char *buf, const char *val);
154  static void intStr(char *buf, const char *val);
155  static void fltStr(char *buf, const char *val);
156  static void strStr(char *buf, const char *val);
157 
158  // Methods for reading various FITS keyword types
159 
160  static void rdNull(FitsDataType& data, char *str);
161  static void rdLog(FitsDataType& data, char *str);
162  static void rdStr(FitsDataType& data, char *str);
163  static void rdInt(FitsDataType& data, char *str);
164  static void rdFlt(FitsDataType& data, char *str);
165  static void rdBunit(FitsDataType& data, char *str);
166  static void rdAxis(FitsDataType& data, char *str);
167 
168  //------------------------------------------------------------
169  // Methods to set some information needed to write the FITS file
170  //------------------------------------------------------------
171 
172  void setObsRa(HourAngle ra) {
173  obsRa_ = ra;
174  }
175 
176  void setObsDec(DecAngle dec) {
177  obsDec_ = dec;
178  }
179 
180  void setTelescopeName(std::string telescope) {
181  telescope_ = telescope;
182  }
183 
184  // Install a set of telescope locations
185 
186  void setTelescopeLocations(std::vector<std::vector<double> >& locations);
187 
188  void setInstrument(std::string instrument) {
189  instrument_ = instrument;
190  }
191 
192  void setSourceName(std::string srcName) {
193  srcName_ = srcName;
194  }
195 
196  void setNumberOfIfs(unsigned nIf) {
197  nIf_ = nIf;
198  ifFrequencies_.resize(0);
199  }
200 
201  // Install a set of IF frequencies
202 
203  void setIfFrequencies(std::vector<Frequency>& frequencies);
204 
205  void setDeltaIfFrequencies(std::vector<Frequency>& frequencies);
206 
207  // An alternate method for specifying IF frequencies is to use a
208  // starting frequency and a delta
209 
210  void setStartingIfFrequency(Frequency frequency) {
211  startingIfFrequency_ = frequency;
212  }
213 
214  void setDeltaIfFrequency(Frequency frequency) {
215  deltaIfFrequency_ = frequency;
216  deltaIfFrequencies_.resize(0);
217  }
218 
219  void setNumberOfChannelsPerIf(unsigned nChan) {
220  nChannel_ = nChan;
221  }
222 
223  void setDeltaChannelFrequency(Frequency frequency) {
224  deltaChannelFrequency_ = frequency;
225  }
226 
227  void setNumberOfFrames(unsigned nFrame) {
228  nFrame_ = nFrame;
229  }
230 
231  void setNumberOfBaselines(unsigned nBaseline) {
232  nBaseline_ = nBaseline;
233  }
234 
235  void setNumberOfStokesParameters(unsigned nStokes) {
236  nStokes_ = nStokes;
237  }
238 
239  void setDate();
240 
241  void checkParameters();
242 
243  //------------------------------------------------------------
244  // Methods to write the file
245  //------------------------------------------------------------
246 
247  void openFile(std::string fileName);
248 
249  //------------------------------------------------------------
250  // Methods to write headers
251  //------------------------------------------------------------
252 
253  // All headers will be initialized and finished the same way
254 
255  void initHeader();
256  void finishHeader(FILE* fp=0);
257 
258  // Methods to write the bodies for different types of header
259 
260  void writeFileHeaderBody(FILE* fp=0);
261  void writeAntennaTableHeaderBody(FILE* fp=0);
262  void writeFrequencyTableHeaderBody(FILE* fp=0);
263 
264  // Methods to write the whole header, for different header types
265 
266  void writeFileHeader(FILE* fp=0);
267  void writeAntennaTableHeader(FILE* fp=0);
268  void writeFrequencyTableHeader(FILE* fp=0);
269 
270  //------------------------------------------------------------
271  // Methods to write the data segment
272  //------------------------------------------------------------
273 
274  void initVisibilityData();
275  void writeVisibilityDataBody(double* vis, double* date, double* uvw, FILE* fp=0);
276  void finishVisibilityData(FILE* fp=0);
277 
278  void writeVisibilityData(double* vis, double* date, double* uvw, FILE* fp=0);
279 
280  //------------------------------------------------------------
281  // Methods to write the antenna table
282  //------------------------------------------------------------
283 
285  void writeAntennaTableDataBody(FILE* fp=0);
286  void finishAntennaTableDataBody(FILE* fp=0);
287 
288  // Write the data for an antenna table
289 
290  void writeAntennaTableData(FILE* fp=0);
291  void writeAntennaTableEntry(int i, double X, double Y, double Z, FILE* fp=0);
292 
293  // Write the whole antenna table
294 
295  void writeAntennaTable(FILE* fp=0);
296 
297  //------------------------------------------------------------
298  // Methods to write the frequency table
299  //------------------------------------------------------------
300 
301  void initFrequencyTableDataBody();
302  void writeFrequencyTableDataBody(FILE* fp=0);
303  void finishFrequencyTableDataBody(FILE* fp=0);
304 
305  void writeFrequencyTableData(FILE* fp=0);
306  void writeFrequencyTable(FILE* fp=0);
307 
308  //-----------------------------------------------------------------------
309  // Write a UVF file
310  //-----------------------------------------------------------------------
311 
312  void writeUvfFile(double* data, double* date, double* uvw, FILE* fp=0);
313 
314  // A method to return whichever is the current file descriptor
315 
316  FILE* getFptr(FILE* fp);
317 
318  private:
319 
320  // Internal counters we will use when writing headers and data
321 
322  unsigned nHdu_;
323  unsigned nDataByte_;
324 
325  //------------------------------------------------------------
326  // Some information needed to write a FITS file
327  //------------------------------------------------------------
328 
329  // The name of the telescope:
330 
331  std::string telescope_;
332  std::string instrument_;
333  std::string date_;
334 
335  // Information about the source
336 
337  HourAngle obsRa_;
338  DecAngle obsDec_;
339  std::string srcName_;
340 
341  // Information about the frequency
342 
343  // The number of IFs
344 
345  unsigned nIf_;
346  Frequency startingIfFrequency_;
347  Frequency deltaIfFrequency_;
348  std::vector<Frequency> ifFrequencies_;
349  std::vector<Frequency> deltaIfFrequencies_;
350 
351  // The number of channels per IF
352 
353  unsigned nChannel_;
354  Frequency startingFrequency_;
355  Frequency deltaChannelFrequency_;
356 
357  // The number of telescopes in our antenna table
358 
359  unsigned nTelescope_;
360  std::vector<std::vector<double> > locations_;
361 
362  // The number of frames
363 
364  unsigned nFrame_;
365 
366  // The number of baselines
367 
368  unsigned nBaseline_;
369 
370  // The number of Stokes parameters
371 
372  unsigned nStokes_;
373 
374  FILE* fp_;
375 
376  // Utility functions for converting from one Endianness to the
377  // other
378 
379  void cp4r4(unsigned char *dest, unsigned char *orig, size_t nitem);
380 
381  // Orig: 8-byte datatype. Dest: 8-byte datatype with byte
382  // order reversed.
383 
384  void cp8r8(unsigned char *dest, unsigned char *orig, size_t nitem);
385 
386  void fwrite(unsigned int* ptr, size_t nel, FILE* stream);
387  void fwrite(float* ptr, size_t nel, FILE* stream);
388  void fwrite(double* ptr, size_t nel, FILE* stream);
389 
390  public:
391 
392  // Return the antenna indices associated with a given visibility
393  // index.
394 
395  void getTelescopeIndices(unsigned baslineIndex,
396  unsigned* iRow, unsigned* iCol,
397  unsigned nTel);
398 
399  }; // End class FitsIo
400 
401  } // End namespace util
402 } // End namespace gcp
403 
404 
405 
406 
407 #endif // End #ifndef GCP_UTIL_FITSIO_H
static void strStr(char *buf, const char *val)
Definition: FitsIo.cc:222
static void nullStr(char *buf, const char *val)
Definition: FitsIo.cc:184
Definition: FitsIo.h:113
static void rdStr(FitsDataType &data, char *str)
Definition: FitsIo.cc:274
void writeVisibilityData(double *vis, double *date, double *uvw, FILE *fp=0)
Definition: FitsIo.cc:869
void initAntennaTableDataBody()
Definition: FitsIo.cc:885
void finishHeader(FILE *fp=0)
Definition: FitsIo.cc:329
void initVisibilityData()
Definition: FitsIo.cc:731
void setDeltaIfFrequencies(std::vector< Frequency > &frequencies)
Definition: FitsIo.cc:1240
Definition: DecAngle.h:16
Definition: FitsIo.h:90
static void rdLog(FitsDataType &data, char *str)
Definition: FitsIo.cc:266
void finishAntennaTableDataBody(FILE *fp=0)
Definition: FitsIo.cc:923
static void fltStr(char *buf, const char *val)
Definition: FitsIo.cc:189
Definition: FitsIo.h:121
void putPhdu(const char *name, const char *val, const char *comment, FILE *fp=0)
Definition: FitsIo.cc:124
static void logStr(char *buf, const char *val)
Definition: FitsIo.cc:211
void writeFileHeader(FILE *fp=0)
Definition: FitsIo.cc:656
Definition: HourAngle.h:17
void setIfFrequencies(std::vector< Frequency > &frequencies)
Definition: FitsIo.cc:1225
static void intStr(char *buf, const char *val)
Definition: FitsIo.cc:200
void finishVisibilityData(FILE *fp=0)
Definition: FitsIo.cc:847
void getTelescopeIndices(unsigned baslineIndex, unsigned *iRow, unsigned *iCol, unsigned nTel)
Definition: FitsIo.cc:1059
void writeAntennaTableHeader(FILE *fp=0)
Definition: FitsIo.cc:668
static void rdInt(FitsDataType &data, char *str)
Definition: FitsIo.cc:258
void setTelescopeLocations(std::vector< std::vector< double > > &locations)
Definition: FitsIo.cc:1211
FitsIo()
Definition: FitsIo.cc:104
Definition: FitsIo.h:135
Definition: DataType.h:27
void writeAntennaTableEntry(int i, double X, double Y, double Z, FILE *fp=0)
Definition: FitsIo.cc:906
static void rdNull(FitsDataType &data, char *str)
Definition: FitsIo.cc:245
FILE * getFptr(FILE *fp)
Definition: FitsIo.cc:1099
static void rdBunit(FitsDataType &data, char *str)
Definition: FitsIo.cc:282
void initHeader()
Definition: FitsIo.cc:321
static void rdAxis(FitsDataType &data, char *str)
Definition: FitsIo.cc:301
void writeVisibilityDataBody(double *vis, double *date, double *uvw, FILE *fp=0)
Definition: FitsIo.cc:739
void writeFrequencyTableHeader(FILE *fp=0)
Definition: FitsIo.cc:680
void writeAntennaTableData(FILE *fp=0)
Definition: FitsIo.cc:943
static void rdFlt(FitsDataType &data, char *str)
Definition: FitsIo.cc:250
void writeAntennaTable(FILE *fp=0)
Definition: FitsIo.cc:955
Definition: FitsIo.h:31
virtual ~FitsIo()
Definition: FitsIo.cc:112
void writeAntennaTableDataBody(FILE *fp=0)
Definition: FitsIo.cc:893