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);
132 assert(ptr->Fptr->open_count == 1);
141 fitsfile* ptr =
nullptr;
149 assert(ptr->Fptr->open_count == 1);
161 int original_hdu = 0;
162 fits_get_hdu_num(ptr, &original_hdu);
166 int number_of_hdus = 0;
167 if (fits_get_num_hdus(ptr, &number_of_hdus, &status) < 0) {
175 for (
int hdu_number = 1; hdu_number <= number_of_hdus; ++hdu_number) {
176 fits_movabs_hdu(ptr, hdu_number, &hdu_type, &status);
181 if (hdu_type == IMAGE_HDU) {
183 long naxes[2] = {1, 1};
185 fits_get_img_param(ptr, 2, &bitpix, &naxis, naxes, &status);
186 if (status == 0 && naxis == 2) {
193 fits_movabs_hdu(ptr, original_hdu, &hdu_type, &status);
205 int keynum = 1, status = 0;
207 fits_read_record(fptr, keynum, record, &status);
208 while (status == 0 &&
strncmp(record,
"END", 3) != 0) {
209 static boost::regex
regex(
"([^=]{8})=([^\\/]*)(\\/(.*))?");
212 boost::smatch sub_matches;
213 if (boost::regex_match(record_str, sub_matches,
regex)) {
214 auto keyword = boost::to_upper_copy(sub_matches[1].str());
215 auto value = sub_matches[2].str();
216 auto comment = sub_matches[4].str();
217 boost::trim(keyword);
219 boost::trim(comment);
222 fits_read_record(fptr, ++keynum, record, &status);
232 int original_hdu = 0;
236 for (
unsigned int i = 0; i <
m_headers.size(); i++) {
237 fits_movabs_hdu(
m_fits_ptr.
get(), i + 1, &hdu_type, &status);
243 fits_movabs_hdu(
m_fits_ptr.
get(), original_hdu, &hdu_type, &status);
247 auto base_name =
m_path.stem();
248 base_name.replace_extension(
".head");
249 auto head_filename =
m_path.parent_path() / base_name;
251 if (!boost::filesystem::exists(head_filename)) {
259 file.
open(head_filename.native());
265 int current_hdu = *hdu_iter;
270 static boost::regex regex_blank_line(
"\\s*$");
271 line = boost::regex_replace(line, regex_blank_line,
std::string(
""));
272 if (line.
size() == 0) {
276 if (boost::to_upper_copy(line) ==
"END") {
277 current_hdu = *(++hdu_iter);
279 static boost::regex
regex(
"([^=]{1,8})=([^\\/]*)(\\/ (.*))?");
280 boost::smatch sub_matches;
281 if (boost::regex_match(line, sub_matches,
regex) && sub_matches.size() >= 3) {
282 auto keyword = boost::to_upper_copy(sub_matches[1].str());
283 auto value = sub_matches[2].str();
284 auto comment = sub_matches[4].str();
285 boost::trim(keyword);
287 boost::trim(comment);
T emplace_back(T... args)
Elements::Path::Item path