37 #include "vircam_wcsutils.h"
38 #include "vircam_pfits.h"
39 #include "vircam_utils.h"
41 #define WCS_FATAL_ERR(_p) {cpl_msg_error(fctid,"Unable to find keyword %s",_p); cpl_error_reset(); return(wcs);}
47 static const char *notabkeys[NNOTABKEYS] = {
"^CRVAL[1-2]*$",
"^CRPIX[1-2]*",
48 "^CD[1-2]*_[1-2]*",
"^CDELT[1-2]*",
49 "^CTYPE[1-2]*",
"^PV2_[1-5]*"};
102 from = cpl_matrix_new(1,2);
103 xy = cpl_matrix_get_data(from);
109 cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_PHYS2WORLD);
113 radec = cpl_matrix_get_data(to);
119 cpl_matrix_delete(from);
120 cpl_matrix_delete(to);
121 cpl_array_delete(status);
155 cpl_matrix *from,*to;
160 from = cpl_matrix_new(1,2);
161 radec = cpl_matrix_get_data(from);
167 cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_WORLD2PHYS);
171 xy = cpl_matrix_get_data(to);
177 cpl_matrix_delete(from);
178 cpl_matrix_delete(to);
179 cpl_array_delete(status);
214 double *x_1,
double *y_1,
double *x_2,
217 cpl_matrix *from,*radec;
223 from = cpl_matrix_new(nc,2);
224 xy1 = cpl_matrix_get_data(from);
225 for (i = 0; i < nc; i++) {
232 cpl_wcs_convert(wcs1,from,&radec,&status,CPL_WCS_PHYS2WORLD);
233 cpl_array_delete(status);
234 cpl_matrix_delete(from);
238 cpl_wcs_convert(wcs2,radec,&from,&status,CPL_WCS_WORLD2PHYS);
239 cpl_array_delete(status);
240 cpl_matrix_delete(radec);
244 xy1 = cpl_matrix_get_data(from);
245 for (i = 0; i < nc; i++) {
252 cpl_matrix_delete(from);
284 double *xi,
double *eta) {
287 cpl_matrix *from,*to;
292 from = cpl_matrix_new(1,2);
293 radec = cpl_matrix_get_data(from);
299 cpl_wcs_convert(wcs,from,&to,&status,CPL_WCS_WORLD2STD);
303 xy = cpl_matrix_get_data(to);
309 cpl_matrix_delete(from);
310 cpl_matrix_delete(to);
311 cpl_array_delete(status);
348 double *ra2,
double *dec1,
double *dec2,
352 double ra,dec,x,y,dra,ddec,boxfudge,min_4q,max_1q;
353 int first_quad,fourth_quad,ni,nj,istep;
364 if (*status != VIR_OK)
369 wcs = cpl_wcs_new_from_propertylist(plist);
375 a = cpl_wcs_get_image_dims(wcs);
376 naxes = cpl_array_get_data_int_const(a);
389 nj = naxes[1]/istep + 2;
390 ni = naxes[0]/istep + 2;
391 for (j = 1; j <= nj; j++) {
392 y = (double)min(naxes[1],(istep*(j-1)+1));
393 for (i = 1; i <= ni; i++) {
394 x = (double)min(naxes[0],(istep*(i-1)+1));
396 if (ra >= 0.0 && ra <= 90.0) {
398 max_1q = max(ra,max_1q);
399 }
else if (ra >= 270.0 && ra <= 360.0) {
401 min_4q = min((ra-360.0),min_4q);
405 *dec1 = min(*dec1,dec);
406 *dec2 = max(*dec2,dec);
416 if (first_quad && fourth_quad) {
424 boxfudge = 0.01*(float)fudge;
425 dra = 0.5*boxfudge*(*ra2 - *ra1);
428 ddec = 0.5*boxfudge*(*dec2 - *dec1);
472 const char *fctid =
"vircam_crpixshift";
476 if (scalefac == 0.0) {
477 cpl_msg_error(fctid,
"Scale factor is zero!");
483 for (i = 1; i <= 2; i++) {
484 snprintf(key,SZKEY,
"CRPIX%d",i);
488 if (cpl_propertylist_has(p,key) == 0) {
489 cpl_msg_error(fctid,
"Header is missing WCS key %s",key);
495 type = cpl_propertylist_get_type(p,key);
502 val = (double)cpl_propertylist_get_float(p,key);
504 case CPL_TYPE_DOUBLE:
505 val = cpl_propertylist_get_double(p,key);
508 cpl_msg_error(fctid,
"Header has WCS key %s as non-floating point!",
515 val = (val - sh[i-1])/scalefac - 1.0;
521 cpl_propertylist_update_float(p,key,(
float)val);
523 case CPL_TYPE_DOUBLE:
524 cpl_propertylist_update_double(p,key,val);
561 const char *fctid =
"vircam_rescalecd";
566 if (scalefac == 0.0) {
567 cpl_msg_error(fctid,
"Scale factor is zero!");
573 for (i = 1; i <= 2; i++) {
574 for (j = 1; j <= 2; j++) {
575 snprintf(key,SZKEY,
"CD%d_%d",i,j);
579 if (cpl_propertylist_has(p,key) == 0) {
580 cpl_msg_error(fctid,
"Header is missing WCS key %s",key);
586 type = cpl_propertylist_get_type(p,key);
593 val = (double)cpl_propertylist_get_float(p,key);
595 case CPL_TYPE_DOUBLE:
596 val = cpl_propertylist_get_double(p,key);
599 cpl_msg_error(fctid,
"Header has WCS key %s as non-floating point!",
612 cpl_propertylist_update_float(p,key,(
float)val);
614 case CPL_TYPE_DOUBLE:
615 cpl_propertylist_update_double(p,key,val);
654 float *yoff,
int *status) {
655 double xc,yc,ra,dec,xnew,ynew;
658 const char *fctid =
"vircam_diffxywcs";
664 if (*status != VIR_OK)
669 if (wcs == NULL || wcsref == NULL) {
670 cpl_msg_error(fctid,
"NULL wcs information");
676 a = cpl_wcs_get_image_dims(wcsref);
677 dims = cpl_array_get_data_int_const(a);
678 xc = 0.5*(double)dims[0];
679 yc = 0.5*(double)dims[1];
689 *xoff = (float)(xc - xnew);
690 *yoff = (float)(yc - ynew);
718 const char *fctid =
"vircam_removewcs";
722 if (*status != VIR_OK)
725 cpl_msg_error(fctid,
"Propertylist passed is NULL\nProgramming error");
731 for (i = 0; i < NNOTABKEYS; i++)
732 cpl_propertylist_erase_regexp(p,notabkeys[i],0);
768 const char *fctid=
"vircam_tabwcs";
772 if (*status != VIR_OK)
775 cpl_msg_error(fctid,
"Propertylist passed is NULL\nProgramming error");
782 if (xcol == -1 || ycol == -1) {
790 (void)snprintf(key,8,
"TCTYP%d",xcol);
792 (void)snprintf(key,8,
"TCTYP%d",ycol);
798 (void)snprintf(key,8,
"TCRVL%d",xcol);
800 (void)snprintf(key,8,
"TCRVL%d",ycol);
805 (void)snprintf(key,8,
"TCRPX%d",xcol);
807 (void)snprintf(key,8,
"TCRPX%d",ycol);
812 for (i = 1; i <= 5; i++) {
813 (void)snprintf(key2,8,
"PV2_%d",i);
814 (void)snprintf(key,8,
"TV%d_%d",ycol,i);
815 if (cpl_propertylist_has(p,key2))
821 (void)snprintf(key,8,
"TC%d_%d",xcol,xcol);
823 (void)snprintf(key,8,
"TC%d_%d",xcol,ycol);
825 (void)snprintf(key,8,
"TC%d_%d",ycol,xcol);
827 (void)snprintf(key,8,
"TC%d_%d",ycol,ycol);
void vircam_radectoxieta(cpl_wcs *wcs, double ra, double dec, double *xi, double *eta)
void vircam_xytoxy_list(cpl_wcs *wcs1, cpl_wcs *wcs2, int nc, double *x_1, double *y_1, double *x_2, double *y_2)
int vircam_tabwcs(cpl_propertylist *p, int xcol, int ycol, int *status)
void vircam_rename_property(cpl_propertylist *p, const char *oldname, char *newname)
void vircam_xytoradec(cpl_wcs *wcs, double x, double y, double *ra, double *dec)
void vircam_radectoxy(cpl_wcs *wcs, double ra, double dec, double *x, double *y)
int vircam_rescalecd(cpl_propertylist *p, double scalefac)
int vircam_crpixshift(cpl_propertylist *p, double scalefac, double sh[])
int vircam_coverage(cpl_propertylist *plist, int fudge, double *ra1, double *ra2, double *dec1, double *dec2, int *status)
int vircam_removewcs(cpl_propertylist *p, int *status)
int vircam_diffxywcs(cpl_wcs *wcs, cpl_wcs *wcsref, float *xoff, float *yoff, int *status)