30 #include <boost/algorithm/string/case_conv.hpp>
31 #include <boost/algorithm/string/trim.hpp>
32 #include <boost/filesystem/operations.hpp>
33 #include <boost/filesystem/path.hpp>
34 #include <boost/regex.hpp>
40 namespace SourceXtractor {
77 : m_filename(filename)
78 , m_file_manager(std::
move(manager))
79 , m_handler(m_file_manager->getFileHandler(filename))
80 , m_hdu_number(hdu_number) {
83 long naxes[3] = {1, 1, 1};
97 fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status);
98 if (status != 0 || (naxis != 2 && naxis != 3)) {
100 <<
"Can't find 2D image or data cube in FITS file: " << filename <<
"[" <<
m_hdu_number <<
"]";
105 m_depth = naxis >= 3 ? naxes[2] : 1;
107 if (image_type < 0) {
118 : m_filename(filename)
119 , m_file_manager(std::
move(manager))
120 , m_handler(m_file_manager->getFileHandler(filename))
123 , m_image_type(image_type) {
126 fitsfile* fptr =
nullptr;
137 assert(fptr !=
nullptr);
139 if (empty_primary && acc->m_fd.getImageHdus().empty()) {
140 fits_create_img(fptr, FLOAT_IMG, 0,
nullptr, &status);
142 throw Elements::Exception() <<
"Can't create empty hdu: " << filename <<
" status: " << status;
146 long naxes[2] = {
width, height};
147 fits_create_img(fptr,
getImageType(), 2, naxes, &status);
150 throw Elements::Exception() <<
"Can't get the active HDU from the FITS file: " << filename <<
" status: " << status;
157 auto headers = coord_system->getFitsHeaders();
158 for (
const auto& h : headers) {
163 auto str = serializer.
str();
165 fits_update_card(fptr, padded_key.
str().c_str(), str.c_str(), &status);
168 fits_get_errstatus(status, err_txt);
169 throw Elements::Exception() <<
"Couldn't write the WCS headers (" << err_txt <<
"): " << str <<
" status: " << status;
175 for (
int i = 0; i <
height; i++) {
176 long first_pixel[2] = {1, i + 1};
177 fits_write_pix(fptr,
getDataType(), first_pixel, width, &buffer[0], &status);
181 throw Elements::Exception() <<
"Couldn't allocate space for new FITS file: " << filename <<
" status: " << status;
206 long increment[3] = {1, 1, 1};
209 fits_read_subset(fptr,
getDataType(), first_pixel, last_pixel, increment,
210 nullptr, tile->getDataPtr(),
nullptr, &status);
228 long first_pixel[2] = {x + 1, y + 1};
229 long last_pixel[2] = {x +
width, y + height};
236 fits_flush_buffer(fptr, 0, &status);
243 fits_movabs_hdu(fptr, hdu_number, &hdu_type, &status);
249 if (hdu_type != IMAGE_HDU) {
255 if (layer < 0 && layer >=
m_depth) {
262 number_of_records = 0;
266 for (
const auto& record : headers) {
267 const auto& key = record.first;
270 if (record_string.
size() > 8) {
273 else if (record_string.
size() < 8) {
277 if (headers.at(key).m_value.type() ==
typeid(
std::string)) {
278 record_string +=
"= '" + VariantCast<std::string>(headers.at(key).m_value) +
"'";
281 record_string +=
"= " + VariantCast<std::string>(headers.at(key).m_value);
284 if (record_string.
size() > 80) {
289 if (record_string.
size() < 80) {
293 records += record_string;
299 records += record_string;
301 auto buffer = make_unique<std::vector<char>>(records.
begin(), records.
end());
302 buffer->emplace_back(0);
320 << VariantCast<std::string>(value.
m_value);
321 auto str = serializer.
str();
324 fits_update_card(fptr, padded_key.
str().c_str(), str.c_str(), &status);
328 fits_get_errstatus(status, err_txt);
T shared_from_this(T...args)
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
std::unique_ptr< T > make_unique(Args &&...args)