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);
80 fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status);
81 if (status != 0 || naxis != 2) {
97 : m_filename(filename), m_manager(manager), m_hdu_number(1) {
104 fitsfile* fptr =
nullptr;
105 fits_create_file(&fptr, (
"!"+filename).c_str(), &status);
109 assert(fptr !=
nullptr);
111 long naxes[2] = {
width, height};
112 fits_create_img(fptr,
getImageType(), 2, naxes, &status);
115 auto headers = coord_system->getFitsHeaders();
116 for (
auto& h : headers) {
121 auto str = serializer.
str();
123 fits_update_card(fptr, padded_key.
str().c_str(), str.c_str(), &status);
126 fits_get_errstatus(status, err_txt);
127 throw Elements::Exception() <<
"Couldn't write the WCS headers (" << err_txt <<
"): " << str;
133 for (
int i = 0; i<
height; i++) {
134 long first_pixel[2] = {1, i+1};
135 fits_write_pix(fptr,
getDataType(), first_pixel, width, &buffer[0], &status);
137 fits_close_file(fptr, &status);
144 auto fptr =
m_manager->getFitsFile(filename,
true);
151 auto fptr = m_manager->getFitsFile(m_filename);
152 switchHdu(fptr, m_hdu_number);
154 auto tile = std::make_shared<ImageTile<T>>((
const_cast<FitsImageSource *
>(
this))->shared_from_this(),
x,
y,
width,
157 long first_pixel[2] = {x + 1, y + 1};
158 long last_pixel[2] = {x +
width, y + height};
159 long increment[2] = {1, 1};
162 auto image = tile->getImage();
163 fits_read_subset(fptr, getDataType(), first_pixel, last_pixel, increment,
164 nullptr, &image->getData()[0],
nullptr, &status);
175 auto fptr = m_manager->getFitsFile(m_filename,
true);
176 switchHdu(fptr, m_hdu_number);
182 int width = image->getWidth();
183 int height = image->getHeight();
185 long first_pixel[2] = {x + 1, y + 1};
186 long last_pixel[2] = {x +
width, y + height};
189 fits_write_subset(fptr, getDataType(), first_pixel, last_pixel, &image->getData()[0], &status);
201 fits_movabs_hdu(fptr, hdu_number, &hdu_type, &status);
204 throw Elements::Exception() <<
"Could not switch to HDU # " << hdu_number <<
" in file " << m_filename;
206 if (hdu_type != IMAGE_HDU) {
214 auto filename = boost::filesystem::path(m_filename);
216 base_name.replace_extension(
".head");
217 auto head_filename =
filename.parent_path() / base_name;
221 if (boost::filesystem::exists(head_filename)) {
225 file.
open(head_filename.native());
230 while (file.
good()) {
234 line = boost::regex_replace(line, boost::regex(
"\\s*#.*"),
std::string(
""));
235 line = boost::regex_replace(line, boost::regex(
"\\s*$"),
std::string(
""));
237 if (line.
size() == 0) {
241 if (boost::to_upper_copy(line) ==
"END") {
244 else if (current_hdu == m_hdu_number) {
245 boost::smatch sub_matches;
246 if (boost::regex_match(line, sub_matches, boost::regex(
"(.+)=(.+)")) && sub_matches.size() == 3) {
247 auto keyword = boost::to_upper_copy(sub_matches[1].str());
248 m_header[keyword] = sub_matches[2];
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y