26 #include <boost/algorithm/string/trim.hpp>
30 #include <wcslib/wcs.h>
31 #include <wcslib/wcshdr.h>
32 #include <wcslib/wcsfix.h>
33 #include <wcslib/wcsprintf.h>
34 #include <wcslib/getwcstab.h>
52 static int wrapped_lincpy(
int alloc,
const struct linprm *linsrc,
struct linprm *lindst) {
56 return lincpy(alloc, linsrc, lindst);
61 int number_of_records = 0;
62 auto fits_headers = fits_image_source.
getFitsHeaders(number_of_records);
64 int nreject = 0, nwcs = 0;
66 wcspih(&(*fits_headers)[0], number_of_records, WCSHDR_all, 0, &nreject, &nwcs, &wcs);
69 m_wcs = decltype(
m_wcs)(wcs, [nwcs](wcsprm* wcs) {
72 wcsvfree(&nwcs_copy, &wcs);
77 wcslib_version(wcsver);
78 if (wcsver[0] < 5 || (wcsver[0] == 5 && wcsver[1] < 18)) {
79 logger.
info() <<
"wcslib " << wcsver[0] <<
"." << wcsver[1]
80 <<
" is not fully thread safe, using wrapped lincpy call!";
90 wcsprm wcs_copy = *
m_wcs;
91 wcs_copy.lin.flag = -1;
93 linset(&wcs_copy.lin);
96 double pc_array[2] {image_coordinate.
m_x + 1, image_coordinate.
m_y + 1};
98 double ic_array[2] {0, 0};
99 double wc_array[2] {0, 0};
103 wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
104 int ret_val = wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
105 linfree(&wcs_copy.lin);
107 logger.
error() <<
"wcslib's wcsp2s returned with error code: " << ret_val;
116 wcsprm wcs_copy = *
m_wcs;
117 wcs_copy.lin.flag = -1;
119 linset(&wcs_copy.lin);
121 double pc_array[2] {0, 0};
122 double ic_array[2] {0, 0};
123 double wc_array[2] {world_coordinate.
m_alpha, world_coordinate.
m_delta};
127 int ret_val = wcss2p(&wcs_copy, 1, 1, wc_array, &phi, &theta, ic_array, pc_array, &status);
128 linfree(&wcs_copy.lin);
130 logger.
error() <<
"wcslib's wcss2p returned with error code: " << ret_val;
141 if (wcshdo(WCSHDO_none,
m_wcs.
get(), &nkeyrec, &raw_header) != 0) {
142 throw Elements::Exception() <<
"Failed to get the FITS headers for the WCS coordinate system";
146 for (
int i = 0; i < nkeyrec; ++i) {
147 char *hptr = &raw_header[80 * i];