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>
35 #include <wcslib/dis.h>
53 case WCSHDRERR_SUCCESS:
55 case WCSHDRERR_MEMORY:
57 case WCSHDRERR_PARSER:
67 logger.error() << err->file <<
":" << err->line_no <<
" " << err->function;
68 logger.error() << err->msg;
76 if (ret_code != WCSERR_SUCCESS) {
79 if (wcs->lin.dispre) {
91 for (
const char *p = header; *p !=
'\0' && number_of_records; --number_of_records, p += 80) {
101 static void wcsCheckHeaders(
const wcsprm *wcs,
const char *headers_str,
int number_of_records) {
106 if (wcs->lin.dispre) {
107 bool sip_used =
false, sip_specified =
false;
108 for (
int i = 0; i < wcs->naxis; ++i) {
109 sip_used |= (
strncmp(wcs->lin.dispre->dtype[i],
"SIP", 3) == 0);
110 size_t ctype_len =
strlen(wcs->ctype[i]);
111 sip_specified |= (
strncmp(wcs->ctype[i] + ctype_len - 4,
"-SIP", 4) == 0);
113 if (sip_used && !sip_specified) {
114 logger.warn() <<
"SIP coefficients present, but CTYPE has not the '-SIP' suffix";
115 logger.warn() <<
"SIP distortion will be applied, but this may not be desired";
116 logger.warn() <<
"To suppress this warning, explicitly add the '-SIP' suffix to the CTYPE,";
117 logger.warn() <<
"or remove the SIP distortion coefficients";
127 logger.warn() <<
"WCS generated some errors in strict mode. This may be OK.";
128 logger.warn() <<
"Will run in relaxed mode.";
131 eol = strchrnul(err_buffer,
'\n');
133 err_buffer = eol + 1;
143 static int wrapped_lincpy(
int alloc,
const struct linprm *linsrc,
struct linprm *lindst) {
147 return lincpy(alloc, linsrc, lindst);
152 int number_of_records = 0;
153 auto fits_headers = fits_image_source.
getFitsHeaders(number_of_records);
155 init(&(*fits_headers)[0], number_of_records);
163 int number_of_records;
166 if (wcshdo(WCSHDO_none, original.
m_wcs.
get(), &number_of_records, &raw_header) != 0) {
167 throw Elements::Exception() <<
"Failed to get the FITS headers for the WCS coordinate system when copying WCS";
170 init(raw_header, number_of_records);
179 int nreject = 0, nwcs = 0, nreject_strict = 0;
183 wcsprintf_set(
nullptr);
187 int ret = wcspih(headers, number_of_records, WCSHDR_strict, 2, &nreject_strict, &nwcs, &wcs);
192 ret = wcspih(headers, number_of_records, WCSHDR_all, 0, &nreject, &nwcs, &wcs);
199 m_wcs = decltype(
m_wcs)(wcs, [nwcs](wcsprm* wcs) {
200 int nwcs_copy = nwcs;
202 wcsvfree(&nwcs_copy, &wcs);
206 wcslib_version(wcsver);
207 if (wcsver[0] < 5 || (wcsver[0] == 5 && wcsver[1] < 18)) {
208 logger.info() <<
"wcslib " << wcsver[0] <<
"." << wcsver[1]
209 <<
" is not fully thread safe, using wrapped lincpy call!";
220 wcsprm wcs_copy = *
m_wcs;
221 wcs_copy.lin.flag = -1;
223 linset(&wcs_copy.lin);
226 double pc_array[2] {image_coordinate.
m_x + 1, image_coordinate.
m_y + 1};
228 double ic_array[2] {0, 0};
229 double wc_array[2] {0, 0};
233 wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
234 int ret_val = wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
235 linfree(&wcs_copy.lin);
243 wcsprm wcs_copy = *
m_wcs;
244 wcs_copy.lin.flag = -1;
246 linset(&wcs_copy.lin);
248 double pc_array[2] {0, 0};
249 double ic_array[2] {0, 0};
250 double wc_array[2] {world_coordinate.
m_alpha, world_coordinate.
m_delta};
254 int ret_val = wcss2p(&wcs_copy, 1, 1, wc_array, &phi, &theta, ic_array, pc_array, &status);
255 linfree(&wcs_copy.lin);
265 if (wcshdo(WCSHDO_none,
m_wcs.
get(), &nkeyrec, &raw_header) != 0) {
266 throw Elements::Exception() <<
"Failed to get the FITS headers for the WCS coordinate system";
270 for (
int i = 0; i < nkeyrec; ++i) {
271 char *hptr = &raw_header[80 * i];