28 #include <boost/filesystem/path.hpp>
29 #include <boost/filesystem/operations.hpp>
30 #include <boost/regex.hpp>
31 #include <boost/algorithm/string/case_conv.hpp>
32 #include <boost/algorithm/string/trim.hpp>
35 namespace SourceXtractor {
41 int keynum = 1, status = 0;
43 fits_read_record(fptr, keynum, record, &status);
44 while (status == 0 &&
strncmp(record,
"END", 3) != 0) {
45 static boost::regex
regex(
"(.+)=([^\\/]*)(.*)");
48 boost::smatch sub_matches;
49 if (boost::regex_match(record_str, sub_matches, regex)) {
50 auto keyword = boost::to_upper_copy(sub_matches[1].str());
52 auto i = headers.
emplace(keyword, sub_matches[2]);
53 boost::trim(i.first->second);
55 fits_read_record(fptr, ++keynum, record, &status);
64 : m_filename(filename), m_manager(manager), m_hdu_number(hdu_number) {
67 long naxes[2] = {1,1};
69 auto fptr =
m_manager->getFitsFile(filename);
73 fits_get_num_hdus(fptr, &hdunum, &status);
82 fits_get_hdu_type(fptr, &hdutype, &status);
83 if (status == 0 && hdutype == IMAGE_HDU) {
84 fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status);
90 }
while (
m_hdu_number <=hdunum && !(hdutype == IMAGE_HDU && naxis == 2));
91 if (hdutype != IMAGE_HDU) {
99 fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status);
100 if (status != 0 || naxis != 2) {
116 : m_filename(filename), m_manager(manager), m_hdu_number(1) {
123 fitsfile* fptr =
nullptr;
124 fits_create_file(&fptr, (
"!"+filename).c_str(), &status);
128 assert(fptr !=
nullptr);
130 long naxes[2] = {width, height};
131 fits_create_img(fptr,
getImageType(), 2, naxes, &status);
134 auto headers = coord_system->getFitsHeaders();
135 for (
auto& h : headers) {
140 auto str = serializer.
str();
142 fits_update_card(fptr, padded_key.
str().c_str(), str.c_str(), &status);
145 fits_get_errstatus(status, err_txt);
146 throw Elements::Exception() <<
"Couldn't write the WCS headers (" << err_txt <<
"): " << str;
152 for (
int i = 0; i<height; i++) {
153 long first_pixel[2] = {1, i+1};
154 fits_write_pix(fptr,
getDataType(), first_pixel, width, &buffer[0], &status);
156 fits_close_file(fptr, &status);
163 auto fptr =
m_manager->getFitsFile(filename,
true);
170 auto fptr = m_manager->getFitsFile(m_filename);
171 switchHdu(fptr, m_hdu_number);
173 auto tile = std::make_shared<ImageTile<T>>((
const_cast<FitsImageSource *
>(
this))->shared_from_this(),
x,
y, width,
176 long first_pixel[2] = {x + 1, y + 1};
177 long last_pixel[2] = {x + width, y + height};
178 long increment[2] = {1, 1};
181 auto image = tile->getImage();
182 fits_read_subset(fptr, getDataType(), first_pixel, last_pixel, increment,
183 nullptr, &image->getData()[0],
nullptr, &status);
194 auto fptr = m_manager->getFitsFile(m_filename,
true);
195 switchHdu(fptr, m_hdu_number);
201 int width = image->getWidth();
202 int height = image->getHeight();
204 long first_pixel[2] = {x + 1, y + 1};
205 long last_pixel[2] = {x + width, y + height};
208 fits_write_subset(fptr, getDataType(), first_pixel, last_pixel, &image->getData()[0], &status);
220 fits_movabs_hdu(fptr, hdu_number, &hdu_type, &status);
223 throw Elements::Exception() <<
"Could not switch to HDU # " << hdu_number <<
" in file " << m_filename;
225 if (hdu_type != IMAGE_HDU) {
233 auto filename = boost::filesystem::path(m_filename);
235 base_name.replace_extension(
".head");
236 auto head_filename =
filename.parent_path() / base_name;
240 if (boost::filesystem::exists(head_filename)) {
244 file.
open(head_filename.native());
249 while (file.
good()) {
253 line = boost::regex_replace(line, boost::regex(
"\\s*#.*"),
std::string(
""));
254 line = boost::regex_replace(line, boost::regex(
"\\s*$"),
std::string(
""));
256 if (line.
size() == 0) {
260 if (boost::to_upper_copy(line) ==
"END") {
263 else if (current_hdu == m_hdu_number) {
264 boost::smatch sub_matches;
265 if (boost::regex_match(line, sub_matches, boost::regex(
"(.+)=(.+)")) && sub_matches.size() == 3) {
266 auto keyword = boost::to_upper_copy(sub_matches[1].str());
267 m_header[keyword] = sub_matches[2];
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y