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>
41 namespace SourceXtractor {
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;
124 fits_create_file(&ptr,
m_path.native().c_str(), &status);
140 fitsfile* ptr =
nullptr;
159 int original_hdu = 0;
160 fits_get_hdu_num(ptr, &original_hdu);
164 int number_of_hdus = 0;
165 if (fits_get_num_hdus(ptr, &number_of_hdus, &status) < 0) {
173 for (
int hdu_number = 1; hdu_number <= number_of_hdus; ++hdu_number) {
174 fits_movabs_hdu(ptr, hdu_number, &hdu_type, &status);
179 if (hdu_type == IMAGE_HDU) {
181 long naxes[2] = {1, 1};
183 fits_get_img_param(ptr, 2, &bitpix, &naxis, naxes, &status);
184 if (status == 0 && naxis == 2) {
191 fits_movabs_hdu(ptr, original_hdu, &hdu_type, &status);
203 int keynum = 1, status = 0;
205 fits_read_record(fptr, keynum, record, &status);
206 while (status == 0 &&
strncmp(record,
"END", 3) != 0) {
207 static boost::regex
regex(
"([^=]{8})=([^\\/]*)(\\/(.*))?");
210 boost::smatch sub_matches;
211 if (boost::regex_match(record_str, sub_matches, regex)) {
212 auto keyword = boost::to_upper_copy(sub_matches[1].str());
213 auto value = sub_matches[2].str();
214 auto comment = sub_matches[4].str();
215 boost::trim(keyword);
217 boost::trim(comment);
220 fits_read_record(fptr, ++keynum, record, &status);
230 int original_hdu = 0;
234 for (
unsigned int i = 0; i <
m_headers.size(); i++) {
235 fits_movabs_hdu(
m_fits_ptr.
get(), i + 1, &hdu_type, &status);
241 fits_movabs_hdu(
m_fits_ptr.
get(), original_hdu, &hdu_type, &status);
245 auto base_name =
m_path.stem();
246 base_name.replace_extension(
".head");
247 auto head_filename =
m_path.parent_path() / base_name;
249 if (!boost::filesystem::exists(head_filename)) {
257 file.
open(head_filename.native());
263 int current_hdu = *hdu_iter;
268 static boost::regex regex_blank_line(
"\\s*$");
269 line = boost::regex_replace(line, regex_blank_line,
std::string(
""));
270 if (line.
size() == 0) {
274 if (boost::to_upper_copy(line) ==
"END") {
275 current_hdu = *(++hdu_iter);
277 static boost::regex
regex(
"([^=]{1,8})=([^\\/]*)(\\/ (.*))?");
278 boost::smatch sub_matches;
279 if (boost::regex_match(line, sub_matches, regex) && sub_matches.size() >= 3) {
280 auto keyword = boost::to_upper_copy(sub_matches[1].str());
281 auto value = sub_matches[2].str();
282 auto comment = sub_matches[4].str();
283 boost::trim(keyword);
285 boost::trim(comment);
Elements::Path::Item path