32#include <boost/algorithm/string/case_conv.hpp>
33#include <boost/algorithm/string/trim.hpp>
34#include <boost/filesystem/operations.hpp>
35#include <boost/filesystem/path.hpp>
36#include <boost/regex.hpp>
51 boost::regex float_regex(
"^[-+]?(\\d*\\.?\\d+|\\d+\\.?\\d*)([eE][-+]?\\d+)?$");
52 boost::regex int_regex(
"^[-+]?\\d+$");
57 }
else if (boost::regex_match(value, int_regex)) {
59 }
else if (boost::regex_match(value, float_regex)) {
61 }
else if (value.
size() == 1) {
74 for (
auto i = value.
begin(); i != value.
end(); ++i) {
75 if (*i ==
'\'' && !escape) {
89 fits_close_file(ptr, &status);
94 : m_path(
path), m_is_writeable(writeable), m_fits_ptr(nullptr,
close_fits) {
116 fitsfile* ptr =
nullptr;
125 fits_create_file(&ptr,
m_path.native().c_str(), &status);
129 char error_message[32];
130 fits_get_errstatus(status, error_message);
132 <<
"Can't open FITS file: " <<
m_path <<
" status: " << status <<
" = " << error_message;
135 assert(ptr->Fptr->open_count == 1);
144 fitsfile* ptr =
nullptr;
150 char error_message[32];
151 fits_get_errstatus(status, error_message);
153 <<
"Can't close and reopen FITS file: " <<
m_path <<
" status: " << status <<
" = " << error_message;
155 assert(ptr->Fptr->open_count == 1);
167 int original_hdu = 0;
168 fits_get_hdu_num(ptr, &original_hdu);
172 int number_of_hdus = 0;
173 if (fits_get_num_hdus(ptr, &number_of_hdus, &status) < 0) {
174 char error_message[32];
175 fits_get_errstatus(status, error_message);
177 <<
" status: " << status <<
" = " << error_message;
184 for (
int hdu_number = 1; hdu_number <= number_of_hdus; ++hdu_number) {
185 fits_movabs_hdu(ptr, hdu_number, &hdu_type, &status);
187 char error_message[32];
188 fits_get_errstatus(status, error_message);
190 <<
" status: " << status <<
" = " << error_message;
193 if (hdu_type == IMAGE_HDU) {
195 long naxes[3] = {1, 1, 1};
197 fits_get_img_param(ptr, 3, &bitpix, &naxis, naxes, &status);
198 if (status == 0 && (naxis == 2 || naxis == 3)) {
205 fits_movabs_hdu(ptr, original_hdu, &hdu_type, &status);
217 int keynum = 1, status = 0;
219 fits_read_record(fptr, keynum, record, &status);
220 while (status == 0 &&
strncmp(record,
"END", 3) != 0) {
221 static boost::regex
regex(
"([^=]{8})=([^\\/]*)(\\/(.*))?");
224 boost::smatch sub_matches;
225 if (boost::regex_match(record_str, sub_matches,
regex)) {
226 auto keyword = boost::to_upper_copy(sub_matches[1].str());
227 auto value = sub_matches[2].str();
228 auto comment = sub_matches[4].str();
229 boost::trim(keyword);
231 boost::trim(comment);
234 fits_read_record(fptr, ++keynum, record, &status);
244 int original_hdu = 0;
248 for (
unsigned int i = 0; i <
m_headers.size(); i++) {
249 fits_movabs_hdu(
m_fits_ptr.
get(), i + 1, &hdu_type, &status);
255 fits_movabs_hdu(
m_fits_ptr.
get(), original_hdu, &hdu_type, &status);
259 auto base_name =
m_path.stem();
260 base_name.replace_extension(
".head");
261 auto head_filename =
m_path.parent_path() / base_name;
263 if (!boost::filesystem::exists(head_filename)) {
271 file.
open(head_filename.native());
277 int current_hdu = *hdu_iter;
282 static boost::regex regex_blank_line(
"\\s*$");
283 line = boost::regex_replace(line, regex_blank_line,
std::string(
""));
284 if (line.
size() == 0) {
288 if (boost::to_upper_copy(line) ==
"END") {
289 current_hdu = *(++hdu_iter);
291 static boost::regex
regex(
"([^=]{1,8})=([^\\/]*)(\\/ (.*))?");
292 boost::smatch sub_matches;
293 if (boost::regex_match(line, sub_matches,
regex) && sub_matches.size() >= 3) {
294 auto keyword = boost::to_upper_copy(sub_matches[1].str());
295 auto value = sub_matches[2].str();
296 auto comment = sub_matches[4].str();
297 boost::trim(keyword);
299 boost::trim(comment);
311 int original_hdu = 0;
319 long naxes[3] = {1, 1, 1};
322 fits_get_img_param(
m_fits_ptr.
get(), 3, &bitpix, &naxis, naxes, &status);
323 if (status != 0 || (naxis != 2 && naxis != 3)) {
324 char error_message[32];
325 fits_get_errstatus(status, error_message);
327 <<
"Can't find 2D image or data cube in FITS file: " <<
m_path <<
"[" << hdu <<
"]"
328 <<
" status: " << status <<
" = " << error_message;
339 fits_movabs_hdu(
m_fits_ptr.
get(), original_hdu, &hdu_type, &status);
T emplace_back(T... args)