38 #include "fors_cpl_wcs.h"
48 cpl_propertylist *cpl_propertylist_from_fitsfile(fitsfile *file);
57 cpl_error_code cpl_propertylist_to_fitsfile(fitsfile *file,
const cpl_propertylist *
self,
const char *);
90 struct wcsprm *wcsptr;
103 #define DEGRAD 57.2957795130823229
107 #define WCSLIB_ERRCODE_MAX 9
108 static char *wcslib_errmsgs[WCSLIB_ERRCODE_MAX+1] = {
110 "WCSLIB undefined input structure pointer",
111 "WCSLIB unable to allocate required memory",
112 "WCSLIB linear transformation matrix is singular",
113 "WCSLIB invalid coordinate axis types",
114 "WCSLIB invalid parameter value",
115 "WCSLIB invalid coordinate transformation parameters",
116 "WCSLIB Ill-conditioned coordinate transformation parameters",
117 "WCSLIB One or more input coordinates invalid",
118 "WCSLIB One or more input coordinates invalid"};
130 static int fors_ffhdr2str(
151 fits_get_version(&version);
153 if (version >= 3.030)
154 return ffhdr2str(fptr, exclude_comm, exclist, nexc, header, nkeys, status);
157 int casesn, match, exact, totkeys;
159 char keybuf[162], keyname[FLEN_KEYWORD], *headptr;
167 if (ffghsp(fptr, &totkeys, NULL, status) > 0)
171 *header = (
char *) calloc ( (totkeys + 1) * 80 + 1, 1);
174 *status = MEMORY_ALLOCATION;
175 ffpmsg(
"failed to allocate memory to hold all the header keywords");
183 for (ii = 1; ii <= totkeys; ii++)
185 ffgrec(fptr, ii, keybuf, status);
191 strncat(keyname, keybuf, 8);
195 if (!FSTRCMP(
"COMMENT ", keyname) ||
196 !FSTRCMP(
"HISTORY ", keyname) ||
197 !FSTRCMP(
" ", keyname) )
202 for (jj = 0; jj < nexc; jj++ )
204 ffcmps(exclist[jj], keyname, casesn, &match, &exact);
212 strcpy(headptr, keybuf);
226 *header = realloc(*header, (*nkeys *80) + 1);
277 fors_cpl_wcs *fors_cpl_wcs_new_from_propertylist(
const cpl_propertylist *plist) {
278 const char *_id =
"fors_cpl_wcs_new";
281 int retval,nrej,nwcs,np,i;
282 struct wcsprm *wwcs = NULL;
287 cpl_error_set(_id,CPL_ERROR_NULL_INPUT);
305 cpl_error_set_where(_id);
311 retval = wcspih(shdr,np,0,0,&nrej,&nwcs,&wwcs);
314 cpl_msg_warning(_id,
"Cannot parse WCS header");
316 wcsvfree(&nwcs,&wwcs);
324 wcs->wcsptr = (
struct wcsprm *)cpl_calloc(1,
sizeof(
struct wcsprm));
325 (wcs->wcsptr)->flag = -1;
326 wcscopy(1,wwcs,wcs->wcsptr);
328 wcsvfree(&nwcs,&wwcs);
332 if (cpl_propertylist_has(plist,
"NAXIS")) {
333 wcs->naxis = cpl_propertylist_get_int(plist,
"NAXIS");
334 if (cpl_error_get_code() != CPL_ERROR_NONE) {
338 wcs->dims = cpl_calloc(wcs->naxis,
sizeof(
int));
339 for (i = 1; i <= wcs->naxis; i++) {
340 (void)snprintf(nax,9,
"NAXIS%d",i);
341 wcs->dims[i-1] = cpl_propertylist_get_int(plist,nax);
342 if (cpl_error_get_code() != CPL_ERROR_NONE) {
373 if (wcs->wcsptr != NULL) {
374 (void)wcsfree(wcs->wcsptr);
375 cpl_free(wcs->wcsptr);
377 if (wcs->dims != NULL)
433 cpl_error_code fors_cpl_wcs_convert(
const fors_cpl_wcs *wcs,
const cpl_matrix *from,
434 cpl_matrix **to, cpl_array **status,
435 fors_cpl_wcs_trans_mode transform) {
436 int nrows,ncols,*sdata,retval,i;
440 double *fdata,*tdata,*x1data,*x2data,*x3data;
441 char *_id =
"fors_cpl_wcs_convert";
450 if (wcs == NULL || wcs->wcsptr == NULL || from == NULL) {
451 cpl_error_set(_id,CPL_ERROR_NULL_INPUT);
452 return(CPL_ERROR_NULL_INPUT);
457 nrows = cpl_matrix_get_nrow(from);
458 ncols = cpl_matrix_get_ncol(from);
459 if (nrows == 0 || ncols == 0) {
460 cpl_error_set(_id,CPL_ERROR_UNSPECIFIED);
461 return(CPL_ERROR_UNSPECIFIED);
466 *to = cpl_matrix_new(nrows,ncols);
467 *status = cpl_array_new(nrows,CPL_TYPE_INT);
468 x1 = cpl_matrix_new(nrows,ncols);
469 x2 = cpl_array_new(nrows,CPL_TYPE_DOUBLE);
470 x3 = cpl_array_new(nrows,CPL_TYPE_DOUBLE);
474 fdata = cpl_matrix_get_data(from);
475 tdata = cpl_matrix_get_data(*to);
476 sdata = cpl_array_get_data_int(*status);
477 x1data = cpl_matrix_get_data(x1);
478 x2data = cpl_array_get_data_double(x2);
479 x3data = cpl_array_get_data_double(x3);
485 case FORS_CPL_WCS_PHYS2WORLD:
486 retval = wcsp2s(wcs->wcsptr,nrows,wcs->naxis,fdata,x1data,x2data,
489 case FORS_CPL_WCS_WORLD2PHYS:
490 retval = wcss2p(wcs->wcsptr,nrows,wcs->naxis,fdata,x2data,x3data,
493 case FORS_CPL_WCS_WORLD2STD:
494 retval = wcss2p(wcs->wcsptr,nrows,wcs->naxis,fdata,x2data,x3data,
497 case FORS_CPL_WCS_PHYS2STD:
498 retval = wcsp2s(wcs->wcsptr,nrows,wcs->naxis,fdata,tdata,x2data,
499 x3data,x1data,sdata);
502 cpl_error_set(_id,CPL_ERROR_UNSUPPORTED_MODE);
503 cpl_matrix_delete(*to);
505 cpl_array_delete(*status);
507 cpl_matrix_delete(x1);
508 return(CPL_ERROR_UNSUPPORTED_MODE);
513 cpl_matrix_delete(x1);
514 cpl_array_delete(x2);
515 cpl_array_delete(x3);
521 code = CPL_ERROR_NONE;
524 code = CPL_ERROR_NULL_INPUT;
525 cpl_msg_warning(__func__,wcslib_errmsgs[1]);
526 cpl_error_set(__func__,code);
536 code = CPL_ERROR_UNSPECIFIED;
537 cpl_error_set(__func__,code);
538 cpl_msg_warning(__func__,wcslib_errmsgs[retval]);
541 code = CPL_ERROR_UNSPECIFIED;
542 cpl_error_set(__func__,code);
543 cpl_msg_warning(__func__,
"WCSLIB found an unspecified error: %d",
615 const char *_id =
"fors_cpl_wcs_plist2fitsstr";
616 char *header,*h,cval[2];
627 cpl_error_set(_id,CPL_ERROR_NULL_INPUT);
633 n = cpl_propertylist_get_size(
self);
638 (void)fits_create_file(&fptr,
"mem://",&status);
639 (void)fits_create_img(fptr,BYTE_IMG,0,NULL,&status);
643 cpl_propertylist_to_fitsfile(fptr,
self,NULL);
648 (void)fors_ffhdr2str(fptr,1,NULL,0,&header,nkeys,&status);
649 (void)fits_close_file(fptr,&status);
685 char *f,*_id=
"fors_cpl_wcs_fitsstr2plist";
690 if (fitsstr == NULL) {
691 cpl_error_set(_id,CPL_ERROR_NULL_INPUT);
699 (void)fits_create_file(&fptr,
"mem://",&status);
700 (void)fits_create_img(fptr,BYTE_IMG,0,NULL,&status);
706 while (strncmp(f,
"END ",8)) {
707 (void)fits_insert_card(fptr,f,&status);
708 f += (FLEN_CARD - 1);
710 (void)fits_insert_card(fptr,f,&status);
714 p = cpl_propertylist_from_fitsfile(fptr);
718 (void)fits_close_file(fptr,&status);
static char * fors_cpl_wcs_plist2fitsstr(const cpl_propertylist *self, int *nkeys)
Convert a propertylist to a FITS string.
static cpl_propertylist * fors_cpl_wcs_fitsstr2plist(char *fitsstr)
Convert a FITS string to a propertylist.
void fors_cpl_wcs_delete(fors_cpl_wcs *wcs)
Destroy a WCS structure.
static fors_cpl_wcs * fors_cpl_wcs_init(void)
Create an empty wcs structure.