41 #include "omega_catalog.h"
42 #include "omega_dfs.h"
43 #include "omega_pfits.h"
44 #include "omega_utils.h"
66 static float kselect(
float *a,
int n,
int k) {
75 w = a[i]; a[i] = a[j]; a[j] = w;
85 a += i; n -= i; k -= i;
92 static float omega_med(
float *data,
unsigned char *bpm,
long npts) {
93 int i,j,is_even,ilevel;
98 buf = cpl_malloc(npts*
sizeof(*buf));
100 is_even = !(npts & 1);
101 memmove((
char *)buf,(
char *)data,npts*
sizeof(
float));
104 value = kselect(buf,npts,ilevel);
106 value = 0.5*(value + kselect(buf,npts,ilevel));
109 value = kselect(buf,npts,ilevel);
116 for (i = 0; i < npts; i++) {
128 value = kselect(buf,j,ilevel);
130 value = 0.5*(value + kselect(buf,j,ilevel));
133 value = kselect(buf,j,ilevel);
140 static void omega_medmad(
float *data,
unsigned char *bpm,
long np,
float *med,
147 *med = omega_med(data,bpm,np);
152 work = cpl_malloc(np*
sizeof(*work));
153 for (i = 0; i < np; i++)
154 work[i] = (
float)fabs((
double)(data[i] - *med));
158 *mad = omega_med(work,bpm,np);
165 static int omega_fndmatch(
float x,
float y,
float *xlist,
float *ylist,
166 int nlist,
float err)
169 float errsq,errmin,dx,dy,poserr;
176 index = (isp + ifp)/2;
177 while (ifp-isp >= 2) {
178 if (ylist[index] < y - err) {
180 index = (index+ifp)/2;
181 }
else if (ylist[index] > y - err) {
183 index = (index+isp)/2;
194 for (i = isp; i < nlist; i++) {
195 if (ylist[i] > y+err)
199 poserr = dx*dx + dy*dy;
200 if (poserr < errsq) {
201 if (poserr <= errmin) {
227 double *ra2,
double *dec1,
double *dec2)
231 double ra,dec,dra,ddec,boxfudge,min_4q,max_1q;
232 int first_quad=0,fourth_quad=0, ns=0;
235 cpl_array *status = NULL;
245 wcs = cpl_wcs_new_from_propertylist(plist);
247 cpl_msg_error(cpl_func,
"Cannot create wcs. %s",cpl_error_get_message());
252 naxes[0] = (long)cpl_propertylist_get_int(plist,
"NAXIS1");
253 naxes[1] = (long)cpl_propertylist_get_int(plist,
"NAXIS2");
266 cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_PHYS2WORLD);
268 ns = cpl_matrix_get_nrow(to);
271 ra = (double)cpl_matrix_get(to, i, 0);
272 dec = (double)cpl_matrix_get(to, i, 1);
273 if (ra >= 0.0 && ra <= 90.0) {
275 max_1q = omega_max(ra,max_1q);
277 else if (ra >= 270.0 && ra <= 360.0) {
279 min_4q = omega_min((ra-360.0),min_4q);
282 *ra1 = omega_min(*ra1,ra);
283 *ra2 = omega_max(*ra2,ra);
284 *dec1 = omega_min(*dec1,dec);
285 *dec2 = omega_max(*dec2,dec);
315 if (first_quad && fourth_quad) {
323 boxfudge = 0.01*(float)fudge;
324 dra = 0.5*boxfudge*(*ra2 - *ra1);
327 ddec = 0.5*boxfudge*(*dec2 - *dec1);
358 int nobj,nstd,ngrid,ngrid2,ibest,ig,jg,nmatch,k,*matches,jm,l,dont,null;
359 float *xstd,*ystd,*xobj,*yobj,aveden,errlim,xoffbest,yoffbest,*xoffs;
360 float *yoffs,x,y,x2,y2,r2,x1,y1,r1,xoffmed,sigx,yoffmed,sigy,xoff,yoff;
367 nobj = cpl_table_get_nrow(objtab);
368 nstd = cpl_table_get_nrow(stdstab);
370 cpl_msg_error(cpl_func,
"Object table has no rows");
373 else if (nstd == 0) {
374 cpl_msg_error(cpl_func,
"Standards RA/DEC table has no rows");
379 xobj = cpl_table_get_data_float(objtab,
"X_IMAGE");
380 yobj = cpl_table_get_data_float(objtab,
"Y_IMAGE");
381 xstd = cpl_table_get_data_float(stdstab,
"xpredict");
382 ystd = cpl_table_get_data_float(stdstab,
"ypredict");
383 if (xstd == NULL || ystd == NULL || xobj == NULL || yobj == NULL){
384 cpl_msg_error(cpl_func,
"NULL input data");
389 aveden = (float)nstd/(
float)(NX*NY);
390 errlim = 1.0/sqrt(4.0*CPL_MATH_PI*aveden);
391 errlim = omega_min(errlim,5.0);
392 ngrid = (int)(srad/errlim);
393 ngrid = (ngrid/2)*2 + 1;
394 ngrid = omega_max(5,omega_min(NGRIDMAX,ngrid));
395 ngrid2 = ngrid/2 + 1;
401 for (ig = -ngrid2; ig <= ngrid2; ig++) {
402 xoff = (float)ig*errlim*CPL_MATH_SQRT2;
403 for (jg = -ngrid2; jg <= ngrid2; jg++) {
404 yoff = (float)jg*errlim*CPL_MATH_SQRT2;
406 for (k = 0; k < nobj; k++) {
409 if (omega_fndmatch(x,y,xstd,ystd,nstd,errlim) > -1)
412 if (nmatch > ibest) {
422 xoffs = cpl_malloc(nstd*
sizeof(*xoffs));
423 yoffs = cpl_malloc(nstd*
sizeof(*yoffs));
424 matches = cpl_malloc(nstd*
sizeof(*matches));
425 for (k = 0; k < nstd; k++)
430 for (k = 0; k < nstd; k++) {
431 x = xstd[k] - xoffbest;
432 y = ystd[k] - yoffbest;
433 jm = omega_fndmatch(x,y,xobj,yobj,nobj,errlim);
438 r2 = sqrt(x2*x2 + y2*y2);
439 for (l = 0; l < nstd; l++) {
440 if (matches[l] == jm) {
441 x1 = xobj[jm] - (xstd[l] - xoffbest);
442 y1 = yobj[jm] - (ystd[l] - yoffbest);
443 r1 = sqrt(x1*x1 + y1*y1);
457 for (k = 0; k < nstd; k++) {
460 xoffs[nmatch] = xobj[jm] - xstd[k];
461 yoffs[nmatch] = yobj[jm] - ystd[k];
471 omega_medmad(xoffs,NULL,nmatch,&xoffmed,&sigx);
473 omega_medmad(yoffs,NULL,nmatch,&yoffmed,&sigy);
479 errlim = 3.0*omega_max(sigx,sigy);
480 for (k = 0; k < nstd; k++)
482 for (k = 0; k < nstd; k++) {
483 x = xstd[k] + xoffmed;
484 y = ystd[k] + yoffmed;
485 jm = omega_fndmatch(x,y,xobj,yobj,nobj,errlim);
490 r2 = sqrt(x2*x2 + y2*y2);
491 for (l = 0; l < nstd; l++) {
492 if (matches[l] == jm) {
493 x1 = xobj[jm] - (xstd[l] + xoffmed);
494 y1 = yobj[jm] - (ystd[l] + yoffmed);
495 r1 = sqrt(x1*x1 + y1*y1);
513 mstds = cpl_table_duplicate(stdstab);
514 colname = (
char *)cpl_table_get_column_name(objtab);
515 while (colname != NULL) {
516 if (strcmp(colname,
"X_WORLD") && strcmp(colname,
"Y_WORLD"))
517 cpl_table_new_column(mstds,colname,
518 cpl_table_get_column_type(objtab,colname));
520 colname = (
char *)cpl_table_get_column_name(NULL);
522 cpl_table_unselect_all(mstds);
526 for (k = 0; k < nstd; k++) {
529 colname = (
char *)cpl_table_get_column_name(objtab);
530 while (colname != NULL) {
531 if (!strcmp(colname,
"X_WORLD") || !strcmp(colname,
"Y_WORLD")) {
532 colname = (
char *)cpl_table_get_column_name(NULL);
536 switch (cpl_table_get_column_type(objtab,colname)) {
538 cpl_table_set_int(mstds,colname,k,
539 cpl_table_get_int(objtab,colname,jm,
543 cpl_table_set_float(mstds,colname,k,
544 cpl_table_get_float(objtab,colname,jm,
547 case CPL_TYPE_DOUBLE:
548 cpl_table_set_double(mstds,colname,k,
549 cpl_table_get_double(objtab,colname,
553 cpl_table_set_float(mstds,colname,k,
554 cpl_table_get_float(objtab,colname,jm,
558 colname = (
char *)cpl_table_get_column_name(NULL);
560 cpl_table_select_row(mstds,k);
566 *outtab = cpl_table_extract_selected(mstds);
567 cpl_table_delete(mstds);
604 char *pathname = NULL;
607 cpl_table *table = NULL;
608 cpl_table *extracted;
612 table = cpl_table_load(cpl_frame_get_filename(catalog), 1, 0);
621 ramin = cpl_vector_get(coords, 0);
622 ramax = cpl_vector_get(coords, 1);
623 decmin = cpl_vector_get(coords, 2);
624 decmax = cpl_vector_get(coords, 3);
627 cpl_table_select_all(table);
628 cpl_table_and_selected_double(table,
"Dec_max",CPL_NOT_LESS_THAN,decmin);
629 number = cpl_table_and_selected_double(table,
"Dec_min",CPL_NOT_GREATER_THAN,decmax);
635 extracted = cpl_table_extract_selected(table);
639 for(i=0; i<number; i++){
640 const char *name = cpl_table_get_string(extracted,
"USNOA2_CAT",i);
641 char *tname = cpl_sprintf(
"%s/%s",pathname,name);
642 cpl_table *t = cpl_table_load(tname, 1, 0);
645 cpl_table_select_all(t);
646 cpl_table_and_selected_double(t,
"RA2000",CPL_NOT_LESS_THAN, ramin);
647 cpl_table_and_selected_double(t,
"RA2000", CPL_NOT_GREATER_THAN, ramax);
650 cpl_table_and_selected_double(t,
"Dec2000", CPL_NOT_LESS_THAN, decmin);
651 int number = cpl_table_and_selected_double(t,
"Dec2000", CPL_NOT_GREATER_THAN, decmax);
655 freetable(extracted);
660 cpl_table *ext = cpl_table_extract_selected(t);
662 all = cpl_table_new(0);
663 cpl_table_copy_structure(all, ext);
666 int irow = cpl_table_get_nrow(all);
668 cpl_table_insert(all, ext, irow+1);
674 *nstds = cpl_table_get_nrow(all);
677 freetable(extracted);
708 cpl_table *table = NULL;
709 cpl_table *extracted;
712 table = cpl_table_load(stdcat, 1, 0);
714 cpl_msg_warning(cpl_func,
"Cannot load reference catalogue %s",stdcat);
719 ramin = cpl_vector_get(coords, 0);
720 ramax = cpl_vector_get(coords, 1);
721 decmin = cpl_vector_get(coords, 2);
722 decmax = cpl_vector_get(coords, 3);
725 cpl_table_select_all(table);
727 cpl_table_and_selected_double(table,
"Dec",CPL_NOT_LESS_THAN,decmin);
729 cpl_table_and_selected_double(table,
"Dec",CPL_NOT_GREATER_THAN,decmax);
731 cpl_table_and_selected_double(table,
"Ra",CPL_NOT_LESS_THAN,ramin);
733 number = cpl_table_and_selected_double(table,
"Ra",CPL_NOT_GREATER_THAN,ramax);
736 cpl_msg_debug(cpl_func,
"There are no stars in catalogue in given region");
741 extracted = cpl_table_extract_selected(table);
744 *nstds = cpl_table_get_nrow(extracted);
767 int value,
int *nsrc)
771 if(cpl_table_has_column(src, col) != 1)
774 if(cpl_table_get_column_type(src, col) != CPL_TYPE_INT)
777 cpl_table_unselect_all(src);
778 cpl_table_or_selected_int(src, col,
operator, value);
779 out = cpl_table_extract_selected(src);
780 *nsrc = cpl_table_get_nrow(out);
801 double value,
int *nsrc)
805 if(cpl_table_has_column(src, col) != 1)
808 if(cpl_table_get_column_type(src, col) != CPL_TYPE_DOUBLE)
811 cpl_table_unselect_all(src);
812 cpl_table_or_selected_double(src, col,
operator, value);
813 out = cpl_table_extract_selected(src);
814 *nsrc = cpl_table_get_nrow(out);
835 const char *value,
int *nsrc)
839 if(cpl_table_has_column(src, col) != 1)
842 if(cpl_table_get_column_type(src, col) != CPL_TYPE_STRING)
845 cpl_table_unselect_all(src);
846 cpl_table_or_selected_string(src, col,
operator, value);
847 out = cpl_table_extract_selected(src);
848 *nsrc = cpl_table_get_nrow(out);
869 float value,
int *nsrc)
873 if(cpl_table_has_column(src, col) != 1)
876 if(cpl_table_get_column_type(src, col) != CPL_TYPE_FLOAT)
879 cpl_table_unselect_all(src);
880 cpl_table_or_selected_float(src, col,
operator, value);
881 out = cpl_table_extract_selected(src);
882 *nsrc = cpl_table_get_nrow(out);
903 cpl_size lastextension=0;
904 double cat_thre = 1.0;
906 const char *path = NULL;
907 const char *sex_conf = NULL;
908 const char *sex_conv = NULL;
909 const char *sex_nnw = NULL;
910 const char *sex_par = NULL;
911 const char *text = NULL;
912 const char *srcs =
"omega_sci_cat.fits";
914 cpl_table *table, *sources;
919 npars = cpl_parameterlist_get_size(pars);
920 par = cpl_parameterlist_get_first(pars);
922 for(i=0; i<npars; i++) {
923 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
924 if(strcmp(
"bin-path", text) == 0) {
925 path = cpl_parameter_get_string(par) ;
927 else if(strcmp(
"sex-config", text) == 0) {
928 sex_conf = cpl_parameter_get_string(par) ;
930 else if(strcmp(
"sex-conv", text) == 0) {
931 sex_conv = cpl_parameter_get_string(par) ;
933 else if(strcmp(
"sex-param", text) == 0) {
934 sex_par = cpl_parameter_get_string(par) ;
936 else if(strcmp(
"sex-nnw", text) == 0) {
937 sex_nnw = cpl_parameter_get_string(par) ;
939 else if(strcmp(
"cat-thre", text) == 0){
940 cat_thre = cpl_parameter_get_double(par);
942 par = cpl_parameterlist_get_next(pars);
946 cpl_msg_debug(cpl_func,
"zeropoint_final: %f",zeropoint_final);
950 cmd = cpl_sprintf(
"%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
951 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g "
952 "-WEIGHT_TYPE MAP_WEIGHT -WEIGHT_IMAGE %s -CATALOG_TYPE FITS_1.0 -CATALOG_NAME %s -MAG_ZEROPOINT %g "
953 "-BACKPHOTO_TYPE LOCAL",
966 cpl_msg_debug(cpl_func,
"Sextractor call: %s",cmd);
968 if (system(cmd) != 0) {
969 cpl_msg_debug(cpl_func,
"Failed to create catalogue using Sextractor");
975 lastextension=cpl_fits_count_extensions(srcs);
976 table = cpl_table_load(srcs, lastextension, 0);
977 if((table == NULL) || (cpl_table_get_nrow(table) <= 0)){
978 cpl_msg_debug(cpl_func,
"Science catalogue table is empty or NULL");
997 cpl_table * wcscor_matchstds(cpl_table *objtab, cpl_table *stdstab,
float srad)
1008 const char *colname;
1009 cpl_table *matches, *temp;
1010 cpl_propertylist *p;
1012 nobj = cpl_table_get_nrow(objtab);
1013 nstd = cpl_table_get_nrow(stdstab);
1015 cpl_msg_warning(cpl_func,
"Object table has no rows");
1016 }
else if (nstd == 0) {
1017 cpl_msg_warning(cpl_func,
"Reference standards table has no rows");
1037 cpl_table_unselect_all(objtab);
1038 cpl_table_unselect_all(stdstab);
1041 xobj = cpl_table_get_data_float(objtab,
"X_IMAGE");
1042 xstd = cpl_table_get_data_float(stdstab,
"xpredict");
1043 yobj = cpl_table_get_data_float(objtab,
"Y_IMAGE");
1044 ystd = cpl_table_get_data_float(stdstab,
"ypredict");
1048 if (xstd == NULL || ystd == NULL || xobj == NULL || yobj == NULL){
1049 cpl_msg_error(cpl_func,
"Fatal error in wcscor_matchstds");
1055 for (i=0; i<nobj; i++){
1056 for(j=0; j<nstd; j++){
1057 if((xstd[j] <= xobj[i] +srad) && (xstd[j] >= xobj[i] -srad) &&
1058 (ystd[j] <= yobj[i] +srad) && (ystd[j] >= yobj[i] -srad)) {
1059 cpl_table_select_row(stdstab, j);
1060 cpl_table_select_row(objtab, i);
1068 matches = cpl_table_extract_selected(stdstab);
1069 temp = cpl_table_extract_selected(objtab);
1070 for (i=0; i<cpl_table_get_ncol(temp); i++){
1071 colname = cpl_table_get_column_name(temp);
1072 cpl_table_move_column(matches, colname, temp);