26 #include <wcslib/wcs.h>
27 #include <wcslib/wcshdr.h>
28 #include <wcslib/wcsfix.h>
29 #include <wcslib/wcsprintf.h>
30 #include <wcslib/getwcstab.h>
35 #include <boost/algorithm/string/trim.hpp>
49 static int wrapped_lincpy(
int alloc,
const struct linprm *linsrc,
struct linprm *lindst) {
53 return lincpy(alloc, linsrc, lindst);
58 fitsfile *fptr = NULL;
60 fits_open_file(&fptr, fits_file_path.
c_str(), READONLY, &status);
63 fits_movabs_hdu(fptr, hdu_number, &hdu_type, &status);
65 if (status != 0 || hdu_type != IMAGE_HDU) {
66 throw Elements::Exception() <<
"Can't read WCS information from " << fits_file_path <<
" HDU " << hdu_number;
71 fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status);
73 if (hdu_type == IMAGE_HDU) {
74 int nreject = 0, nwcs = 0;
76 wcspih(header, nkeyrec, WCSHDR_all, 0, &nreject, &nwcs, &wcs);
79 m_wcs = decltype(
m_wcs)(wcs, [nwcs](wcsprm* wcs) {
82 wcsvfree(&nwcs_copy, &wcs);
87 fits_close_file(fptr, &status);
90 wcslib_version(wcsver);
91 if (wcsver[0] < 5 || (wcsver[0] == 5 && wcsver[1] < 18)) {
92 logger.
info() <<
"wcslib " << wcsver[0] <<
"." << wcsver[1]
93 <<
" is not fully thread safe, using wrapped lincpy call!";
103 wcsprm wcs_copy = *
m_wcs;
104 wcs_copy.lin.flag = -1;
106 linset(&wcs_copy.lin);
109 double pc_array[2] {image_coordinate.
m_x + 1, image_coordinate.
m_y + 1};
111 double ic_array[2] {0, 0};
112 double wc_array[2] {0, 0};
116 wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
117 linfree(&wcs_copy.lin);
124 wcsprm wcs_copy = *
m_wcs;
125 wcs_copy.lin.flag = -1;
127 linset(&wcs_copy.lin);
129 double pc_array[2] {0, 0};
130 double ic_array[2] {0, 0};
131 double wc_array[2] {world_coordinate.
m_alpha, world_coordinate.
m_delta};
135 wcss2p(&wcs_copy, 1, 1, wc_array, &phi, &theta, ic_array, pc_array, &status);
136 linfree(&wcs_copy.lin);
145 if (wcshdo(WCSHDO_none,
m_wcs.
get(), &nkeyrec, &raw_header) != 0) {
146 throw Elements::Exception() <<
"Failed to get the FITS headers for the WCS coordinate system";
150 for (
int i = 0; i < nkeyrec; ++i) {
151 char *hptr = &raw_header[80 * i];