mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
fitsUtils.cpp
Go to the documentation of this file.
1 /** \file fitsUtils.cpp
2  * \brief Implementation of 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 
28 
29 namespace mx
30 {
31 namespace fits
32 {
33 
34 int fitsStripApost(std::string & s)
35 {
36  int stripped = 0;
37  int p = s.find_first_not_of(" \t\n\t");
38  if(s[p] == '\'')
39  {
40  s.erase(0,p+1);
41  ++stripped;
42  }
43 
44  p = s.find_last_not_of(" \t\n\t");
45  if(s[p] == '\'')
46  {
47  s.erase(p);
48  ++stripped;
49  }
50 
51  --p;
52 
53  while(s[p] == ' ' && p >=0)
54  {
55  s.erase(p);
56  --p;
57  ++stripped;
58  }
59 
60  return stripped;
61 }
62 
63 void fitsPopulateCard( char headStr[81],
64  char *keyword,
65  char *value,
66  char *comment
67  )
68 {
69  memset(headStr, ' ', 80);
70  headStr[80] = '\0';
71 
72  int rpos = 0;
73 
74  if(strlen(keyword) > 8)
75  {
76  rpos += snprintf(headStr, 81, "HIERARCH ");
77 
78  rpos += snprintf(headStr + rpos, 81-rpos, "%s =", keyword);
79  }
80  else
81  {
82  rpos += snprintf(headStr, 81, "%-8s=", keyword);
83  }
84 
85  if(strlen(value) < stdValWidth)
86  {
87  char fstr[10];
88  snprintf(fstr, 10, "%%+%ds", stdValWidth);
89  rpos += snprintf(headStr + rpos, 81-rpos, fstr, value);
90  }
91  else
92  {
93  rpos += snprintf(headStr + rpos, 81-rpos, "%s", value);
94  }
95 
96  headStr[rpos] = ' ';
97  ++rpos;
98 
99  headStr[rpos] = '/';
100  ++rpos;
101 
102  headStr[rpos] = ' ';
103  ++rpos;
104 
105  snprintf(headStr + rpos, 81-rpos, "%s", comment);
106 }
107 
108 template<>
109 int fits_write_key<char *>(fitsfile * fptr, char * keyword, void * value, char * comment)
110 {
111  int fstatus = 0;
112 
113  fits_write_key_longwarn(fptr, &fstatus);
114 
115 
116  fits_write_key_longstr(fptr, keyword, (const char *) value, comment, &fstatus);
117 
118  return fstatus;
119 
120 }
121 
122 template<>
123 int fits_write_key<std::string>(fitsfile * fptr, char * keyword, void * value, char * comment)
124 {
125  return fits_write_key<char *>(fptr, keyword, value, comment);
126 }
127 
128 template<>
129 int fits_write_key<bool>(fitsfile * fptr, char * keyword, void * value, char * comment)
130 {
131  unsigned char bc = *((bool *)value);
132 
133  int fstatus = 0;
134 
135  fits_write_key(fptr, fitsType<unsigned char>(), keyword, &bc, comment, &fstatus);
136 
137  return fstatus;
138 }
139 
140 template<>
141 int fits_write_key<fitsUnknownType>(fitsfile * fptr, char * keyword, void * value, char * comment)
142 {
143 
144  int fstatus = 0;
145 
146  char *vstr = (char *) value;
147 
148  //If type is unkown, do it verbatim
149  char headStr[81];
150 
151  fitsPopulateCard(headStr, keyword, vstr, comment);
152 
153  fits_write_record(fptr, headStr, &fstatus);
154  return fstatus;
155 }
156 
157 int fits_write_comment(fitsfile *fptr, char * comment)
158 {
159  int fstatus = 0;
160 
161  fits_write_comment(fptr, comment, &fstatus);
162 
163  return fstatus;
164 }
165 
166 int fits_write_history(fitsfile *fptr, char * history)
167 {
168  int fstatus = 0;
169 
170  fits_write_history(fptr, history, &fstatus);
171 
172  return fstatus;
173 }
174 
175 void fitsErrText( std::string & explan,
176  const std::string & filename,
177  int fstatus
178  )
179 {
180  char emnem[31];
181 
182  fits_get_errstatus(fstatus, emnem);
183 
184  explan += ": ";
185  explan += filename;
186  explan += ". CFITSIO: ";
187 
188  explan += emnem;
189  explan += " (";
190  explan += ioutils::convertToString(fstatus);
191  explan += ")";
192 
193 }
194 
195 } //namespace fits
196 } //namespace mx
197 
Declares and defines utilities to work with FITS files.
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
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
std::string convertToString(const typeT &value, int precision=0)
Convert a numerical value to a string.
Definition: stringUtils.hpp:82
The mxlib c++ namespace.
Definition: mxError.hpp:107