41#include "casu_stats.h"
42#include "catalogue/casu_utils.h"
47#define NGRIDMAX_STD 31
49static cpl_table *casu_mkmstd_table(cpl_table *objtab, cpl_table *stdstab);
105extern int casu_matchxy(cpl_table *progtab, cpl_table *
template,
float srad,
106 float *xoffset,
float *yoffset,
int *nm,
107 cpl_table **outtab,
int *status) {
109 float *xprog,*yprog,*xtemp,*ytemp,aveden,errlim,xoffbest,yoffbest,xoff;
110 float yoff,x,y,*xoffs,*yoffs;
111 const char *fctid =
"casu_matchxy";
112 int nprog,ntemp,ngrid,ngrid2,ibest,ig,jg,nmatch,k,jm;
120 if (*status != CASU_OK)
125 nprog = (int)cpl_table_get_nrow(progtab);
126 ntemp = (int)cpl_table_get_nrow(
template);
128 cpl_msg_warning(fctid,
"Program table has no rows");
130 }
else if (ntemp == 0) {
131 cpl_msg_warning(fctid,
"Template table has no rows");
137 p = cpl_propertylist_new();
138 cpl_propertylist_append_bool(p,
"Y_coordinate",0);
139 if (cpl_table_sort(progtab,p) != CPL_ERROR_NONE) {
140 cpl_propertylist_delete(p);
143 if (cpl_table_sort(
template,p) != CPL_ERROR_NONE) {
144 cpl_propertylist_delete(p);
147 cpl_propertylist_delete(p);
151 xprog = cpl_table_get_data_float(progtab,
"X_coordinate");
152 yprog = cpl_table_get_data_float(progtab,
"Y_coordinate");
153 xtemp = cpl_table_get_data_float(
template,
"X_coordinate");
154 ytemp = cpl_table_get_data_float(
template,
"Y_coordinate");
155 if (xprog == NULL || yprog == NULL || xtemp == NULL || ytemp == NULL)
160 aveden = (float)ntemp/(
float)(NX*NY);
161 errlim = 1.0/sqrt(4.0*CPL_MATH_PI*aveden);
162 errlim = min(errlim,15.0);
163 ngrid = (int)(srad/errlim);
164 ngrid = (ngrid/2)*2 + 1;
165 ngrid = max(5,min(NGRIDMAX_XY,ngrid));
166 ngrid2 = ngrid/2 + 1;
173 for (ig = -ngrid2; ig <= ngrid2; ig++) {
174 xoff = (float)ig*errlim*CPL_MATH_SQRT2;
175 for (jg = -ngrid2; jg <= ngrid2; jg++) {
176 yoff = (float)jg*errlim*CPL_MATH_SQRT2;
178 for (k = 0; k < nprog; k++) {
184 if (nmatch > ibest) {
195 xoffs = cpl_malloc(nprog*
sizeof(*xoffs));
196 yoffs = cpl_malloc(nprog*
sizeof(*yoffs));
201 for (k = 0; k < nprog; k++) {
202 x = xprog[k] + xoffbest;
203 y = yprog[k] + yoffbest;
206 xoffs[nmatch] = xtemp[jm] - xprog[k];
207 yoffs[nmatch] = ytemp[jm] - yprog[k];
212 *xoffset =
casu_med(xoffs,NULL,nmatch);
213 *yoffset =
casu_med(yoffs,NULL,nmatch);
222 *outtab = cpl_table_new((cpl_size)nprog);
223 cpl_table_new_column(*outtab,
"X_coordinate_1",CPL_TYPE_FLOAT);
224 cpl_table_new_column(*outtab,
"Y_coordinate_1",CPL_TYPE_FLOAT);
225 cpl_table_new_column(*outtab,
"X_coordinate_2",CPL_TYPE_FLOAT);
226 cpl_table_new_column(*outtab,
"Y_coordinate_2",CPL_TYPE_FLOAT);
228 for (k = 0; k < nprog; k++) {
229 x = xprog[k] + *xoffset;
230 y = yprog[k] + *yoffset;
233 cpl_table_set_float(*outtab,
"X_coordinate_1",(cpl_size)nmatch,
235 cpl_table_set_float(*outtab,
"Y_coordinate_1",(cpl_size)nmatch,
237 cpl_table_set_float(*outtab,
"X_coordinate_2",(cpl_size)nmatch,
239 cpl_table_set_float(*outtab,
"Y_coordinate_2",(cpl_size)nmatch,
244 cpl_table_set_size(*outtab,(cpl_size)nmatch);
301 cpl_table **outtab,
int *status) {
302 const char *fctid =
"casu_matchstds";
304 int nobj,nstd,ngrid,ngrid2,ibest,ig,jg,nmatch,k,*matches,jm,l,dont,null,k2;
305 float *xstd,*ystd,*xobj,*yobj,aveden,errlim,xoffbest,yoffbest,*xoffs;
306 float *yoffs,x,y,xx2,yy2,r2,xx1,yy1,r1,xoffmed,sigx,yoffmed,sigy,xoff,yoff;
314 if (*status != CASU_OK)
319 nobj = (int)cpl_table_get_nrow(objtab);
320 nstd = (int)cpl_table_get_nrow(stdstab);
322 cpl_msg_warning(fctid,
"Object table has no rows");
323 mstds = casu_mkmstd_table(objtab,stdstab);
324 *outtab = cpl_table_extract_selected(mstds);
325 cpl_table_delete(mstds);
327 }
else if (nstd == 0) {
328 cpl_msg_warning(fctid,
"Standards RA/DEC table has no rows");
329 mstds = casu_mkmstd_table(objtab,stdstab);
330 *outtab = cpl_table_extract_selected(mstds);
331 cpl_table_delete(mstds);
337 p = cpl_propertylist_new();
338 cpl_propertylist_append_bool(p,
"Y_coordinate",0);
339 if (cpl_table_sort(objtab,p) != CPL_ERROR_NONE) {
340 cpl_propertylist_delete(p);
343 cpl_propertylist_erase(p,
"Y_coordinate");
344 cpl_propertylist_append_bool(p,
"ypredict",0);
345 if (cpl_table_sort(stdstab,p) != CPL_ERROR_NONE) {
346 cpl_propertylist_delete(p);
349 cpl_propertylist_delete(p);
353 xobj = cpl_table_get_data_float(objtab,
"X_coordinate");
354 yobj = cpl_table_get_data_float(objtab,
"Y_coordinate");
355 xstd = cpl_table_get_data_float(stdstab,
"xpredict");
356 ystd = cpl_table_get_data_float(stdstab,
"ypredict");
357 if (xstd == NULL || ystd == NULL || xobj == NULL || yobj == NULL)
364 aveden = (float)max(nstd,nobj)/(float)(NX*NY);
366 errlim = 1.0/sqrt(4.0*CPL_MATH_PI*aveden);
367 errlim = min(errlim,15.0);
369 ngrid = (int)(srad/errlim);
370 ngrid = (ngrid/2)*2 + 1;
372 ngrid = max(5,min(NGRIDMAX_STD,ngrid));
373 ngrid2 = ngrid/2 + 1;
375 cpl_msg_info(fctid,
"Search radius is %f pixels. Using a %d x %d grid, with error limit of %f", srad, ngrid, ngrid, errlim);
382 for (ig = -ngrid2; ig <= ngrid2; ig++) {
383 xoff = (float)ig*errlim*CPL_MATH_SQRT2;
384 for (jg = -ngrid2; jg <= ngrid2; jg++) {
385 yoff = (float)jg*errlim*CPL_MATH_SQRT2;
387 for (k = 0; k < nobj; k++) {
393 if (nmatch > ibest) {
404 xoffs = cpl_malloc(nstd*
sizeof(*xoffs));
405 yoffs = cpl_malloc(nstd*
sizeof(*yoffs));
406 matches = cpl_malloc(nstd*
sizeof(*matches));
407 for (k = 0; k < nstd; k++)
413 for (k = 0; k < nstd; k++) {
414 x = xstd[k] - xoffbest;
415 y = ystd[k] - yoffbest;
421 r2 = sqrt(xx2*xx2 + yy2*yy2);
422 for (l = 0; l < nstd; l++) {
423 if (matches[l] == jm) {
424 xx1 = xobj[jm] - (xstd[l] - xoffbest);
425 yy1 = yobj[jm] - (ystd[l] - yoffbest);
426 r1 = sqrt(xx1*xx1 + yy1*yy1);
441 for (k = 0; k < nstd; k++) {
444 xoffs[nmatch] = xobj[jm] - xstd[k];
445 yoffs[nmatch] = yobj[jm] - ystd[k];
461 cpl_msg_info(fctid,
"Median offset x=%f y=%f, sigx=%f sigy=%f", xoffmed, yoffmed, sigx, sigy);
466 errlim = 3.0*max(sigx,sigy);
467 for (k = 0; k < nstd; k++)
469 for (k = 0; k < nstd; k++) {
470 x = xstd[k] + xoffmed;
471 y = ystd[k] + yoffmed;
477 r2 = sqrt(xx2*xx2 + yy2*yy2);
478 for (l = 0; l < nstd; l++) {
479 if (matches[l] == jm) {
480 xx1 = xobj[jm] - (xstd[l] + xoffmed);
481 yy1 = yobj[jm] - (ystd[l] + yoffmed);
482 r1 = sqrt(xx1*xx1 + yy1*yy1);
500 mstds = cpl_table_duplicate(stdstab);
501 colnames = cpl_table_get_column_names(objtab);
502 ig = (int)cpl_array_get_size(colnames);
503 for (k = 0; k < ig; k++) {
504 colname = (
char *)cpl_array_get_string(colnames,(cpl_size)k);
505 if (strcasecmp(colname,
"RA") && strcasecmp(colname,
"DEC"))
506 cpl_table_new_column(mstds,colname,
507 cpl_table_get_column_type(objtab,colname));
509 cpl_table_unselect_all(mstds);
510 cpl_array_delete(colnames);
517 colnames = cpl_table_get_column_names(objtab);
518 ig = (int)cpl_array_get_size(colnames);
519 for (k = 0; k < nstd; k++) {
522 for (k2 = 0; k2 < ig; k2++) {
523 colname = (
char *)cpl_array_get_string(colnames,
525 if (!strcasecmp(colname,
"RA") || !strcasecmp(colname,
"DEC"))
528 switch (cpl_table_get_column_type(objtab,colname)) {
530 cpl_table_set_int(mstds,colname,(cpl_size)k,
531 cpl_table_get_int(objtab,colname,
532 (cpl_size)jm,&null));
535 cpl_table_set_float(mstds,colname,(cpl_size)k,
536 cpl_table_get_float(objtab,colname,
537 (cpl_size)jm,&null));
539 case CPL_TYPE_DOUBLE:
540 cpl_table_set_double(mstds,colname,(cpl_size)k,
541 cpl_table_get_double(objtab,colname,
542 (cpl_size)jm,&null));
544 case CPL_TYPE_STRING:
545 cpl_table_set_string(mstds,colname,(cpl_size)k,
546 cpl_table_get_string(objtab,colname,
550 cpl_msg_info(fctid,
"Ignoring column %s of type %d", colname, cpl_table_get_column_type(objtab,colname));
554 cpl_table_select_row(mstds,(cpl_size)k);
557 cpl_array_delete(colnames);
561 *outtab = cpl_table_extract_selected(mstds);
562 cpl_table_delete(mstds);
594static cpl_table *casu_mkmstd_table(cpl_table *objtab, cpl_table *stdstab) {
602 mstds = cpl_table_duplicate(stdstab);
607 colnames = cpl_table_get_column_names((
const cpl_table *)objtab);
608 ncol = cpl_array_get_size(colnames);
609 for (i = 0; i < ncol; i++) {
610 colname = (
char *)cpl_array_get_string(colnames,i);
611 if (strcmp(colname,
"RA") && strcmp(colname,
"DEC"))
612 cpl_table_new_column(mstds,colname,
613 cpl_table_get_column_type(objtab,colname));
615 cpl_array_delete(colnames);
616 cpl_table_unselect_all(mstds);
int casu_matchstds(cpl_table *objtab, cpl_table *stdstab, float srad, cpl_table **outtab, int *status)
Match object and standard star tables by their xy coordinates.
int casu_matchxy(cpl_table *progtab, cpl_table *template, float srad, float *xoffset, float *yoffset, int *nm, cpl_table **outtab, int *status)
Match two lists of x,y coordinates from two tables to find the cartesian offset between them.
void casu_medmad(float *data, unsigned char *bpm, long np, float *med, float *mad)
float casu_med(float *data, unsigned char *bpm, long npts)
int casu_fndmatch(float x, float y, float *xlist, float *ylist, int nlist, float err)
Match the x,y coordinates of an object to one from a list.