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 =
strchr(err_buffer,
'\n');
134 err_buffer = eol + 1;
148 static int wrapped_lincpy(
int alloc,
const struct linprm *linsrc,
struct linprm *lindst) {
152 return lincpy(alloc, linsrc, lindst);
157 int number_of_records = 0;
158 auto fits_headers = fits_image_source.
getFitsHeaders(number_of_records);
160 init(&(*fits_headers)[0], number_of_records);
168 int number_of_records;
171 if (wcshdo(WCSHDO_none, original.
m_wcs.
get(), &number_of_records, &raw_header) != 0) {
172 throw Elements::Exception() <<
"Failed to get the FITS headers for the WCS coordinate system when copying WCS";
175 init(raw_header, number_of_records);
184 int nreject = 0, nwcs = 0, nreject_strict = 0;
188 wcsprintf_set(
nullptr);
192 int ret = wcspih(headers, number_of_records, WCSHDR_strict, 2, &nreject_strict, &nwcs, &wcs);
197 ret = wcspih(headers, number_of_records, WCSHDR_all, 0, &nreject, &nwcs, &wcs);
204 m_wcs = decltype(
m_wcs)(wcs, [nwcs](wcsprm* wcs) {
205 int nwcs_copy = nwcs;
207 wcsvfree(&nwcs_copy, &wcs);
211 wcslib_version(wcsver);
212 if (wcsver[0] < 5 || (wcsver[0] == 5 && wcsver[1] < 18)) {
213 logger.info() <<
"wcslib " << wcsver[0] <<
"." << wcsver[1]
214 <<
" is not fully thread safe, using wrapped lincpy call!";
225 wcsprm wcs_copy = *
m_wcs;
226 wcs_copy.lin.flag = -1;
228 linset(&wcs_copy.lin);
231 double pc_array[2] {image_coordinate.
m_x + 1, image_coordinate.
m_y + 1};
233 double ic_array[2] {0, 0};
234 double wc_array[2] {0, 0};
238 wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
239 int ret_val = wcsp2s(&wcs_copy, 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
240 linfree(&wcs_copy.lin);
248 wcsprm wcs_copy = *
m_wcs;
249 wcs_copy.lin.flag = -1;
251 linset(&wcs_copy.lin);
253 double pc_array[2] {0, 0};
254 double ic_array[2] {0, 0};
255 double wc_array[2] {world_coordinate.
m_alpha, world_coordinate.
m_delta};
259 int ret_val = wcss2p(&wcs_copy, 1, 1, wc_array, &phi, &theta, ic_array, pc_array, &status);
260 linfree(&wcs_copy.lin);
270 if (wcshdo(WCSHDO_none,
m_wcs.
get(), &nkeyrec, &raw_header) != 0) {
271 throw Elements::Exception() <<
"Failed to get the FITS headers for the WCS coordinate system";
275 for (
int i = 0; i < nkeyrec; ++i) {
276 char *hptr = &raw_header[80 * i];
static Logging getLogger(const std::string &name="")