mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
fitsUtils.hpp
Go to the documentation of this file.
1 /** \file fitsUtils.hpp
2  * \brief Declares and defines utilities to work with FITS files
3  * \ingroup fits_processing_files
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015-2022 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef ioutils_fits_fitsUtils_hpp
28 #define ioutils_fits_fitsUtils_hpp
29 
30 
31 #include <iostream>
32 #include <cstdlib>
33 #include <cstring>
34 #include <complex>
35 
36 #include <fitsio.h>
37 
38 #include "../stringUtils.hpp"
39 
40 namespace mx
41 {
42 namespace fits
43 {
44 
45 ///The standard width of the value entry in a header card
46 #define stdValWidth (20)
47 
48 //#define fitsTUNKNOWN (-5000)
49 struct fitsUnknownType
50 {
51 };
52 
53 //#define fitsTCOMMENT (-5001)
54 struct fitsCommentType
55 {
56  fitsCommentType()
57  {
58  }
59 
60  explicit fitsCommentType(char * v)
61  {
62  static_cast<void>(v);
63  }
64 
65  explicit fitsCommentType(const char * v)
66  {
67  static_cast<void>(v);
68  }
69 };
70 
71 //#define fitsTHISTORY (-5002)
72 
73 struct fitsHistoryType
74 {
75  fitsHistoryType()
76  {
77  }
78 
79  explicit fitsHistoryType(char * v)
80  {
81  static_cast<void>(v);
82  }
83 
84  explicit fitsHistoryType(const char * v)
85  {
86  static_cast<void>(v);
87  }
88 
89 };
90 
91 
92 /** \ingroup fits_utils
93  * @{
94  */
95 
96 /// Return the cfitsio constant for a given data type.
97 /**
98  *
99  * \tparam scalarT is the type
100  *
101  * \returns a constant defined in cfitsio corresponding to the native type
102  * \returns -1 if not a define type in cfitsio
103  *
104  * \ingroup fits_utils
105  */
106 template<typename scalarT>
107 constexpr int fitsType()
108 {
109  return -5000; //This is the same as unknownType
110 }
111 
112 template<>
113 constexpr int fitsType<char *>()
114 {
115  return TSTRING;
116 }
117 
118 template<>
119 constexpr int fitsType<std::string>()
120 {
121  return TSTRING;
122 }
123 
124 template<>
125 constexpr int fitsType<bool>() //mxlib extension, treated as uchar.
126 {
127  return -14002;
128 }
129 
130 template<>
131 constexpr int fitsType<char>()
132 {
133  return TSBYTE;
134 }
135 
136 template<>
137 constexpr int fitsType<unsigned char>()
138 {
139  return TBYTE;
140 }
141 
142 template<>
143 constexpr int fitsType<short>()
144 {
145  return TSHORT;
146 }
147 
148 template<>
149 constexpr int fitsType<unsigned short>()
150 {
151  return TUSHORT;
152 }
153 
154 template<>
155 constexpr int fitsType<int>()
156 {
157  return TINT;
158 }
159 
160 template<>
161 constexpr int fitsType<unsigned int>()
162 {
163  return TUINT;
164 }
165 
166 template<>
167 constexpr int fitsType<long>()
168 {
169  return TLONG;
170 }
171 
172 template<>
173 constexpr int fitsType<unsigned long>()
174 {
175  return TULONG;
176 }
177 
178 template<>
179 constexpr int fitsType<long long>()
180 {
181  return TLONGLONG;
182 }
183 
184 template<>
185 constexpr int fitsType<unsigned long long>()
186 {
187  return TULONGLONG;
188 }
189 
190 template<>
191 constexpr int fitsType<float>()
192 {
193  return TFLOAT;
194 }
195 
196 template<>
197 constexpr int fitsType<std::complex<float>>()
198 {
199  return TCOMPLEX;
200 }
201 
202 template<>
203 constexpr int fitsType<double>()
204 {
205  return TDOUBLE;
206 }
207 
208 template<>
209 constexpr int fitsType<std::complex<double>>()
210 {
211  return TDBLCOMPLEX;
212 }
213 
214 template<>
215 constexpr int fitsType<fitsUnknownType>()
216 {
217  return -5000;
218 }
219 
220 template<>
221 constexpr int fitsType<fitsCommentType>()
222 {
223  return -5001;
224 }
225 
226 template<>
227 constexpr int fitsType<fitsHistoryType>()
228 {
229  return -5002;
230 }
231 
232 
233 /** Return the cfitsio BITPIX value for a given data type.
234  *
235  * \tparam scalarT is the type
236  * \retval int > 0 if a constant is defined in cfitsio corresponding to the native type
237  * \retval -1 if not a defined type in cfitsio
238  */
239 template<typename scalarT>
240 constexpr int fitsBITPIX();
241 
242 template<>
243 constexpr int fitsBITPIX<char>()
244 {
245  return SBYTE_IMG;
246 }
247 
248 template<>
249 constexpr int fitsBITPIX<signed char>()
250 {
251  return SBYTE_IMG;
252 }
253 
254 template<>
255 constexpr int fitsBITPIX<unsigned char>()
256 {
257  return BYTE_IMG;
258 }
259 
260 template<>
261 constexpr int fitsBITPIX<short>()
262 {
263  return SHORT_IMG;
264 }
265 
266 template<>
267 constexpr int fitsBITPIX<unsigned short>()
268 {
269  return USHORT_IMG;
270 }
271 
272 template<>
273 constexpr int fitsBITPIX<int>()
274 {
275  return LONG_IMG; //Yes, this is right. This returns 32
276 }
277 
278 template<>
279 constexpr int fitsBITPIX<unsigned int>()
280 {
281  return ULONG_IMG; //Yes, this is right, this returns 40
282 }
283 
284 template<>
285 constexpr int fitsBITPIX<long>()
286 {
287  return LONGLONG_IMG; //Yes, this is right, this returns 64
288 }
289 
290 template<>
291 constexpr int fitsBITPIX<unsigned long>()
292 {
293  return ULONGLONG_IMG; //Yes, this is right, this returns 64
294 }
295 
296 template<>
297 constexpr int fitsBITPIX<long long>()
298 {
299  return LONGLONG_IMG; //Yes, this is right, this returns 64
300 }
301 
302 template<>
303 constexpr int fitsBITPIX<unsigned long long>()
304 {
305  return ULONGLONG_IMG; //Yes, this is right, this returns 64
306 }
307 
308 template<>
309 constexpr int fitsBITPIX<float>()
310 {
311  return FLOAT_IMG;
312 }
313 
314 template<>
315 constexpr int fitsBITPIX<double>()
316 {
317  return DOUBLE_IMG;
318 }
319 
320 
321 /// Strip the apostrophes from a FITS value string
322 /** The argument is modified if the first and/or last non-whitespace character is '
323  *
324  * \param s is the string from which to strip apostrophes
325  *
326  * \retval int containing the number of stripped apostrophes
327  */
328 int fitsStripApost(std::string & s);
329 
330 ///Populate a fits header card with the value string copied verbatim
331 /**
332  * \param headStr is a c-string which must be 81 characters in length, including the '\n'
333  * \param keyword is the keyword name
334  * \param value is the value string
335  * \param comment is the comment string
336  */
337 void fitsPopulateCard(char headStr[81], char *keyword, char *value, char *comment);
338 
339 /// Write a header card to a file
340 /** This is a templatized wrapper for the cfitsio routine fits_write_key.
341  *
342  * \tparam typeT is the type of the value
343  *
344  * \param fptr is a pointer to an open fits file
345  * \param keyword is a c-string containing the keyword
346  * \param value is a pointer to the memory location of the value
347  * \param comment is a c-string, possibly NULL, containing a comment string
348  *
349  * \retval int containing the status returned by the cfitsio routine.
350  *
351  * \ingroup fits_utils
352  */
353 template<typename typeT>
354 int fits_write_key( fitsfile * fptr,
355  char * keyword,
356  void * value,
357  char * comment
358  )
359 {
360  int fstatus = 0;
361 
362  fits_write_key(fptr, fitsType<typeT>(), keyword, value, comment, &fstatus);
363 
364  return fstatus;
365 }
366 
367 template<>
368 int fits_write_key<char *>( fitsfile * fptr,
369  char * keyword,
370  void * value,
371  char * comment
372  );
373 
374 template<>
375 int fits_write_key<std::string>( fitsfile * fptr,
376  char * keyword,
377  void * value,
378  char * comment
379  );
380 
381 /// Specialization to handle the case bool
382 /** This gets converted to unsigned char.
383  */
384 template<>
385 int fits_write_key<bool>( fitsfile * fptr,
386  char * keyword,
387  void * value,
388  char * comment
389  );
390 
391 template<>
392 int fits_write_key<fitsUnknownType>( fitsfile * fptr,
393  char * keyword,
394  void * value,
395  char * comment
396  );
397 
398 int fits_write_comment( fitsfile *fptr,
399  char * comment
400  );
401 
402 int fits_write_history( fitsfile *fptr,
403  char * history
404  );
405 
406 /// Generate a rich error meesage from a FITS status code.
407 void fitsErrText( std::string & explan, ///< [out] the explanatory message
408  const std::string & filename, ///< [in] the FITS file's name which generated the problem
409  int fstatus ///< [in] the cfitstio status code
410  );
411 ///@}
412 
413 } //namespace fits
414 } //namespace mx
415 
416 #endif //ioutils_fits_fitsUtils_hpp
417 
constexpr int fitsType()
Return the cfitsio constant for a given data type.
Definition: fitsUtils.hpp:107
int fitsStripApost(std::string &s)
Strip the apostrophes from a FITS value string.
Definition: fitsUtils.cpp:34
void fitsErrText(std::string &explan, const std::string &filename, int fstatus)
Generate a rich error meesage from a FITS status code.
Definition: fitsUtils.cpp:175
int fits_write_key(fitsfile *fptr, char *keyword, void *value, char *comment)
Write a header card to a file.
Definition: fitsUtils.hpp:354
constexpr int fitsBITPIX()
void fitsPopulateCard(char headStr[81], char *keyword, char *value, char *comment)
Populate a fits header card with the value string copied verbatim.
Definition: fitsUtils.cpp:63
int fits_write_key< bool >(fitsfile *fptr, char *keyword, void *value, char *comment)
Specialization to handle the case bool.
Definition: fitsUtils.cpp:129
The mxlib c++ namespace.
Definition: mxError.hpp:107