37#include "casu_wcsutils.h"
38#include "casu_utils.h"
40#define WCS_FATAL_ERR(_p) {cpl_msg_error(fctid,"Unable to find keyword %s",_p); cpl_error_reset(); return(wcs);}
46static const char *notabkeys[NNOTABKEYS] = {
"^CRVAL[1-2]*",
"^CRPIX[1-2]*",
47 "^CD[1-2]*_[1-2]*",
"^CDELT[1-2]*",
48 "^CTYPE[1-2]*",
"^PV[1-9]*_[1-9]*"};
101 from = cpl_matrix_new(1,2);
102 xy = cpl_matrix_get_data(from);
108 cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_PHYS2WORLD);
112 radec = cpl_matrix_get_data(to);
118 cpl_matrix_delete(from);
119 cpl_matrix_delete(to);
120 cpl_array_delete(status);
154 cpl_matrix *from,*to;
159 from = cpl_matrix_new(1,2);
160 radec = cpl_matrix_get_data(from);
166 cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_WORLD2PHYS);
170 xy = cpl_matrix_get_data(to);
176 cpl_matrix_delete(from);
177 cpl_matrix_delete(to);
178 cpl_array_delete(status);
213 double *y_1,
double *x_2,
double *y_2) {
215 cpl_matrix *from,*radec;
221 from = cpl_matrix_new(nc,2);
222 xy1 = cpl_matrix_get_data(from);
223 for (i = 0; i < nc; i++) {
230 cpl_wcs_convert(wcs1,from,&radec,&status,CPL_WCS_PHYS2WORLD);
231 cpl_array_delete(status);
232 cpl_matrix_delete(from);
236 cpl_wcs_convert(wcs2,radec,&from,&status,CPL_WCS_WORLD2PHYS);
237 cpl_array_delete(status);
238 cpl_matrix_delete(radec);
242 xy1 = cpl_matrix_get_data(from);
243 for (i = 0; i < nc; i++) {
250 cpl_matrix_delete(from);
282 double *xi,
double *eta) {
285 cpl_matrix *from,*to;
290 from = cpl_matrix_new(1,2);
291 radec = cpl_matrix_get_data(from);
297 cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_WORLD2STD);
301 xy = cpl_matrix_get_data(to);
307 cpl_matrix_delete(from);
308 cpl_matrix_delete(to);
309 cpl_array_delete(status);
346 double *ra2,
double *dec1,
double *dec2,
350 double ra,dec,x,y,dra,ddec,boxfudge,min_4q,max_1q;
351 int first_quad,fourth_quad,ni,nj,istep;
362 if (*status != CASU_OK)
367 wcs = cpl_wcs_new_from_propertylist(plist);
373 a = cpl_wcs_get_image_dims(wcs);
374 naxes = cpl_array_get_data_int_const(a);
387 nj = naxes[1]/istep + 2;
388 ni = naxes[0]/istep + 2;
389 for (j = 1; j <= nj; j++) {
390 y = (double)min(naxes[1],(istep*(j-1)+1));
391 for (i = 1; i <= ni; i++) {
392 x = (double)min(naxes[0],(istep*(i-1)+1));
394 if (ra >= 0.0 && ra <= 90.0) {
396 max_1q = max(ra,max_1q);
397 }
else if (ra >= 270.0 && ra <= 360.0) {
399 min_4q = min((ra-360.0),min_4q);
403 *dec1 = min(*dec1,dec);
404 *dec2 = max(*dec2,dec);
414 if (first_quad && fourth_quad) {
422 boxfudge = 0.01*(float)fudge;
423 dra = 0.5*boxfudge*(*ra2 - *ra1);
426 ddec = 0.5*boxfudge*(*dec2 - *dec1);
469 const char *fctid =
"casu_crpixshift";
473 if (scalefac == 0.0) {
474 cpl_msg_error(fctid,
"Scale factor is zero!");
480 for (i = 1; i <= 2; i++) {
481 snprintf(key,SZKEY,
"CRPIX%d",i);
485 if (cpl_propertylist_has(p,key) == 0) {
486 cpl_msg_error(fctid,
"Header is missing WCS key %s",key);
492 type = cpl_propertylist_get_type(p,key);
499 val = (double)cpl_propertylist_get_float(p,key);
501 case CPL_TYPE_DOUBLE:
502 val = cpl_propertylist_get_double(p,key);
505 cpl_msg_error(fctid,
"Header has WCS key %s as non-floating point!",
512 val = (val - sh[i-1])/scalefac - 1.0;
518 cpl_propertylist_update_float(p,key,(
float)val);
520 case CPL_TYPE_DOUBLE:
521 cpl_propertylist_update_double(p,key,val);
558 const char *fctid =
"casu_rescalecd";
563 if (scalefac == 0.0) {
564 cpl_msg_error(fctid,
"Scale factor is zero!");
570 for (i = 1; i <= 2; i++) {
571 for (j = 1; j <= 2; j++) {
572 snprintf(key,SZKEY,
"CD%d_%d",i,j);
576 if (cpl_propertylist_has(p,key) == 0) {
577 cpl_msg_error(fctid,
"Header is missing WCS key %s",key);
583 type = cpl_propertylist_get_type(p,key);
590 val = (double)cpl_propertylist_get_float(p,key);
592 case CPL_TYPE_DOUBLE:
593 val = cpl_propertylist_get_double(p,key);
596 cpl_msg_error(fctid,
"Header has WCS key %s as non-floating point!",
609 cpl_propertylist_update_float(p,key,(
float)val);
611 case CPL_TYPE_DOUBLE:
612 cpl_propertylist_update_double(p,key,val);
651 float *yoff,
int *status) {
652 double xc,yc,ra,dec,xnew,ynew;
655 const char *fctid =
"casu_diffxywcs";
661 if (*status != CASU_OK)
666 if (wcs == NULL || wcsref == NULL) {
667 cpl_msg_error(fctid,
"NULL wcs information");
673 a = cpl_wcs_get_image_dims(wcsref);
674 dims = cpl_array_get_data_int_const(a);
675 xc = 0.5*(double)dims[0];
676 yc = 0.5*(double)dims[1];
686 *xoff = (float)(xc - xnew);
687 *yoff = (float)(yc - ynew);
715 const char *fctid =
"casu_removewcs";
719 if (*status != CASU_OK)
722 cpl_msg_error(fctid,
"Propertylist passed is NULL\nProgramming error");
728 for (i = 0; i < NNOTABKEYS; i++)
729 cpl_propertylist_erase_regexp(p,notabkeys[i],0);
761extern int casu_tabwcs(cpl_propertylist *p,
int xcol,
int ycol,
int *status) {
764 const char *fctid=
"casu_tabwcs";
768 if (*status != CASU_OK)
771 cpl_msg_error(fctid,
"Propertylist passed is NULL\nProgramming error");
778 if (xcol == -1 || ycol == -1) {
786 (void)snprintf(key,8,
"TCTYP%d",xcol);
788 (void)snprintf(key,8,
"TCTYP%d",ycol);
794 (void)snprintf(key,8,
"TCRVL%d",xcol);
796 (void)snprintf(key,8,
"TCRVL%d",ycol);
801 (void)snprintf(key,8,
"TCRPX%d",xcol);
803 (void)snprintf(key,8,
"TCRPX%d",ycol);
808 for (i = 1; i <= 5; i++) {
809 (void)snprintf(key2,8,
"PV2_%d",i);
810 (void)snprintf(key,8,
"TV%d_%d",ycol,i);
811 if (cpl_propertylist_has(p,key2))
817 (void)snprintf(key,8,
"TC%d_%d",xcol,xcol);
819 (void)snprintf(key,8,
"TC%d_%d",xcol,ycol);
821 (void)snprintf(key,8,
"TC%d_%d",ycol,xcol);
823 (void)snprintf(key,8,
"TC%d_%d",ycol,ycol);
void casu_rename_property(cpl_propertylist *p, const char *oldname, char *newname)
Rename a property in a given propertylist.
int casu_removewcs(cpl_propertylist *p, int *status)
void casu_radectoxieta(cpl_wcs *wcs, double ra, double dec, double *xi, double *eta)
int casu_diffxywcs(cpl_wcs *wcs, cpl_wcs *wcsref, float *xoff, float *yoff, int *status)
int casu_rescalecd(cpl_propertylist *p, double scalefac)
void casu_radectoxy(cpl_wcs *wcs, double ra, double dec, double *x, double *y)
int casu_tabwcs(cpl_propertylist *p, int xcol, int ycol, int *status)
void casu_xytoradec(cpl_wcs *wcs, double x, double y, double *ra, double *dec)
void casu_xytoxy_list(cpl_wcs *wcs1, cpl_wcs *wcs2, int nc, double *x_1, double *y_1, double *x_2, double *y_2)
int casu_crpixshift(cpl_propertylist *p, double scalefac, double sh[])
int casu_coverage(cpl_propertylist *plist, int fudge, double *ra1, double *ra2, double *dec1, double *dec2, int *status)