00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <math.h>
00040
00041 #include "omega_catalog.h"
00042 #include "omega_dfs.h"
00043 #include "omega_pfits.h"
00044 #include "omega_utils.h"
00045
00059
00062
00063
00064
00065
00066 static float kselect(float *a, int n, int k) {
00067 while (n > 1) {
00068 int i = 0, j = n - 1;
00069 float x = a[j/2], w;
00070
00071 do {
00072 while (a[i] < x) i++;
00073 while (a[j] > x) j--;
00074 if (i < j) {
00075 w = a[i]; a[i] = a[j]; a[j] = w;
00076 } else {
00077 if (i == j) i++;
00078 break;
00079 }
00080 } while (++i <= --j);
00081
00082 if (k < i)
00083 n = i;
00084 else {
00085 a += i; n -= i; k -= i;
00086 }
00087 }
00088
00089 return a[0];
00090 }
00091
00092 static float omega_med(float *data, unsigned char *bpm, long npts) {
00093 int i,j,is_even,ilevel;
00094 float *buf,value;
00095
00096
00097
00098 buf = cpl_malloc(npts*sizeof(*buf));
00099 if (bpm == NULL) {
00100 is_even = !(npts & 1);
00101 memmove((char *)buf,(char *)data,npts*sizeof(float));
00102 if (is_even) {
00103 ilevel = npts/2 - 1;
00104 value = kselect(buf,npts,ilevel);
00105 ilevel = npts/2;
00106 value = 0.5*(value + kselect(buf,npts,ilevel));
00107 } else {
00108 ilevel = npts/2;
00109 value = kselect(buf,npts,ilevel);
00110 }
00111
00112
00113
00114 } else {
00115 j = 0;
00116 for (i = 0; i < npts; i++) {
00117 if (bpm[i] == 0)
00118 buf[j++] = data[i];
00119 }
00120 if (j == 0) {
00121 cpl_free(buf);
00122 value = CX_MAXFLOAT;
00123 return(value);
00124 }
00125 is_even = !(j & 1);
00126 if (is_even) {
00127 ilevel = j/2 - 1;
00128 value = kselect(buf,j,ilevel);
00129 ilevel = j/2;
00130 value = 0.5*(value + kselect(buf,j,ilevel));
00131 } else {
00132 ilevel = j/2;
00133 value = kselect(buf,j,ilevel);
00134 }
00135 }
00136 cpl_free(buf);
00137 return(value);
00138 }
00139
00140 static void omega_medmad(float *data, unsigned char *bpm, long np, float *med,
00141 float *mad) {
00142 int i;
00143 float *work;
00144
00145
00146
00147 *med = omega_med(data,bpm,np);
00148
00149
00150
00151
00152 work = cpl_malloc(np*sizeof(*work));
00153 for (i = 0; i < np; i++)
00154 work[i] = (float)fabs((double)(data[i] - *med));
00155
00156
00157
00158 *mad = omega_med(work,bpm,np);
00159
00160
00161
00162 cpl_free(work);
00163 }
00164
00165 static int omega_fndmatch(float x, float y, float *xlist, float *ylist,
00166 int nlist, float err)
00167 {
00168 int isp,ifp,index,i;
00169 float errsq,errmin,dx,dy,poserr;
00170
00171
00172
00173 isp = 0;
00174 ifp = nlist - 1;
00175 errsq = err*err;
00176 index = (isp + ifp)/2;
00177 while (ifp-isp >= 2) {
00178 if (ylist[index] < y - err) {
00179 isp = index;
00180 index = (index+ifp)/2;
00181 } else if (ylist[index] > y - err) {
00182 ifp = index;
00183 index = (index+isp)/2;
00184 } else {
00185 isp = index;
00186 break;
00187 }
00188 }
00189
00190
00191
00192 index = -1;
00193 errmin = errsq;
00194 for (i = isp; i < nlist; i++) {
00195 if (ylist[i] > y+err)
00196 break;
00197 dx = x - xlist[i];
00198 dy = y - ylist[i];
00199 poserr = dx*dx + dy*dy;
00200 if (poserr < errsq) {
00201 if (poserr <= errmin) {
00202 index = i;
00203 errmin = poserr;
00204 }
00205 }
00206 }
00207 return(index);
00208 }
00209
00210
00211
00225
00226 int omega_get_coverage(cpl_propertylist *plist, int fudge, cpl_matrix *from, double *ra1,
00227 double *ra2, double *dec1, double *dec2)
00228 {
00229
00230 cpl_wcs *wcs;
00231 double ra,dec,dra,ddec,boxfudge,min_4q,max_1q;
00232 int first_quad=0,fourth_quad=0, ns=0;
00233 long i=0,naxes[2];
00234 cpl_matrix *to=NULL;
00235 cpl_array *status = NULL;
00236
00237
00238
00239 *ra1 = 0.0;
00240 *ra2 = 0.0;
00241 *dec1 = 0.0;
00242 *dec2 = 0.0;
00243
00244
00245 wcs = cpl_wcs_new_from_propertylist(plist);
00246 if(wcs == NULL){
00247 cpl_msg_error(cpl_func,"Cannot create wcs. %s",cpl_error_get_message());
00248 return -1;
00249 }
00250
00251
00252 naxes[0] = (long)cpl_propertylist_get_int(plist, "NAXIS1");
00253 naxes[1] = (long)cpl_propertylist_get_int(plist, "NAXIS2");
00254
00255
00256
00257 *ra1 = 370.0;
00258 *ra2 = -370.0;
00259 *dec1 = 95.0;
00260 *dec2 = -95.0;
00261 first_quad = 0;
00262 fourth_quad = 0;
00263 min_4q = 370.0;
00264 max_1q = 0.0;
00265
00266 cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_PHYS2WORLD);
00267 freearray(status);
00268 ns = cpl_matrix_get_nrow(to);
00269
00270 for(i=0; i<ns; i++){
00271 ra = (double)cpl_matrix_get(to, i, 0);
00272 dec = (double)cpl_matrix_get(to, i, 1);
00273 if (ra >= 0.0 && ra <= 90.0) {
00274 first_quad = 1;
00275 max_1q = omega_max(ra,max_1q);
00276 }
00277 else if (ra >= 270.0 && ra <= 360.0) {
00278 fourth_quad = 1;
00279 min_4q = omega_min((ra-360.0),min_4q);
00280 }
00281
00282 *ra1 = omega_min(*ra1,ra);
00283 *ra2 = omega_max(*ra2,ra);
00284 *dec1 = omega_min(*dec1,dec);
00285 *dec2 = omega_max(*dec2,dec);
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 freewcs(wcs);
00309
00310
00311
00312
00313
00314
00315 if (first_quad && fourth_quad) {
00316 *ra1 = min_4q;
00317 *ra2 = max_1q;
00318 }
00319
00320
00321
00322 if (fudge) {
00323 boxfudge = 0.01*(float)fudge;
00324 dra = 0.5*boxfudge*(*ra2 - *ra1);
00325 *ra1 -= dra;
00326 *ra2 += dra;
00327 ddec = 0.5*boxfudge*(*dec2 - *dec1);
00328 *dec1 -= ddec;
00329 *dec2 += ddec;
00330 }
00331
00332 freematrix(to);
00333
00334
00335
00336 return 0;
00337 }
00338
00339
00351
00352
00353 int omega_matchstds(cpl_table *objtab, cpl_table *stdstab, float srad,
00354 cpl_table **outtab)
00355 {
00356
00357 char *colname;
00358 int nobj,nstd,ngrid,ngrid2,ibest,ig,jg,nmatch,k,*matches,jm,l,dont,null;
00359 float *xstd,*ystd,*xobj,*yobj,aveden,errlim,xoffbest,yoffbest,*xoffs;
00360 float *yoffs,x,y,x2,y2,r2,x1,y1,r1,xoffmed,sigx,yoffmed,sigy,xoff,yoff;
00361 cpl_propertylist *p;
00362 cpl_table *mstds;
00363
00364 *outtab = NULL;
00365
00366
00367 nobj = cpl_table_get_nrow(objtab);
00368 nstd = cpl_table_get_nrow(stdstab);
00369 if (nobj == 0) {
00370 cpl_msg_error(cpl_func,"Object table has no rows");
00371 return -1;
00372 }
00373 else if (nstd == 0) {
00374 cpl_msg_error(cpl_func,"Standards RA/DEC table has no rows");
00375 return -1;
00376 }
00377
00378
00379 xobj = cpl_table_get_data_float(objtab,"X_IMAGE");
00380 yobj = cpl_table_get_data_float(objtab,"Y_IMAGE");
00381 xstd = cpl_table_get_data_float(stdstab,"xpredict");
00382 ystd = cpl_table_get_data_float(stdstab,"ypredict");
00383 if (xstd == NULL || ystd == NULL || xobj == NULL || yobj == NULL){
00384 cpl_msg_error(cpl_func,"NULL input data");
00385 return -1;
00386 }
00387
00388
00389 aveden = (float)nstd/(float)(NX*NY);
00390 errlim = 1.0/sqrt(4.0*CPL_MATH_PI*aveden);
00391 errlim = omega_min(errlim,5.0);
00392 ngrid = (int)(srad/errlim);
00393 ngrid = (ngrid/2)*2 + 1;
00394 ngrid = omega_max(5,omega_min(NGRIDMAX,ngrid));
00395 ngrid2 = ngrid/2 + 1;
00396
00397
00398 ibest = 0;
00399 xoffbest = 0.0;
00400 yoffbest = 0.0;
00401 for (ig = -ngrid2; ig <= ngrid2; ig++) {
00402 xoff = (float)ig*errlim*CPL_MATH_SQRT2;
00403 for (jg = -ngrid2; jg <= ngrid2; jg++) {
00404 yoff = (float)jg*errlim*CPL_MATH_SQRT2;
00405 nmatch = 0;
00406 for (k = 0; k < nobj; k++) {
00407 x = xobj[k] + xoff;
00408 y = yobj[k] + yoff;
00409 if (omega_fndmatch(x,y,xstd,ystd,nstd,errlim) > -1)
00410 nmatch++;
00411 }
00412 if (nmatch > ibest) {
00413 ibest = nmatch;
00414 xoffbest = xoff;
00415 yoffbest = yoff;
00416 }
00417 }
00418 }
00419
00420
00421
00422 xoffs = cpl_malloc(nstd*sizeof(*xoffs));
00423 yoffs = cpl_malloc(nstd*sizeof(*yoffs));
00424 matches = cpl_malloc(nstd*sizeof(*matches));
00425 for (k = 0; k < nstd; k++)
00426 matches[k] = -1;
00427
00428
00429 nmatch = 0;
00430 for (k = 0; k < nstd; k++) {
00431 x = xstd[k] - xoffbest;
00432 y = ystd[k] - yoffbest;
00433 jm = omega_fndmatch(x,y,xobj,yobj,nobj,errlim);
00434 if (jm > -1) {
00435 dont = 0;
00436 x2 = xobj[jm] - x;
00437 y2 = yobj[jm] - y;
00438 r2 = sqrt(x2*x2 + y2*y2);
00439 for (l = 0; l < nstd; l++) {
00440 if (matches[l] == jm) {
00441 x1 = xobj[jm] - (xstd[l] - xoffbest);
00442 y1 = yobj[jm] - (ystd[l] - yoffbest);
00443 r1 = sqrt(x1*x1 + y1*y1);
00444 if (r2 < r1)
00445 matches[l] = -1;
00446 else
00447 dont = 1;
00448 break;
00449 }
00450 }
00451 if (dont == 0)
00452 matches[k] = jm;
00453 }
00454 }
00455
00456
00457 for (k = 0; k < nstd; k++) {
00458 jm = matches[k];
00459 if (jm != -1) {
00460 xoffs[nmatch] = xobj[jm] - xstd[k];
00461 yoffs[nmatch] = yobj[jm] - ystd[k];
00462 nmatch++;
00463 }
00464 }
00465 if (nmatch == 0) {
00466 xoffmed = 0.0;
00467 sigx = 1.0;
00468 yoffmed = 0.0;
00469 sigy = 1.0;
00470 } else {
00471 omega_medmad(xoffs,NULL,nmatch,&xoffmed,&sigx);
00472 sigx *= 1.48;
00473 omega_medmad(yoffs,NULL,nmatch,&yoffmed,&sigy);
00474 sigy *= 1.48;
00475 }
00476
00477
00478
00479 errlim = 3.0*omega_max(sigx,sigy);
00480 for (k = 0; k < nstd; k++)
00481 matches[k] = -1;
00482 for (k = 0; k < nstd; k++) {
00483 x = xstd[k] + xoffmed;
00484 y = ystd[k] + yoffmed;
00485 jm = omega_fndmatch(x,y,xobj,yobj,nobj,errlim);
00486 if (jm > -1) {
00487 dont = 0;
00488 x2 = xobj[jm] - x;
00489 y2 = yobj[jm] - y;
00490 r2 = sqrt(x2*x2 + y2*y2);
00491 for (l = 0; l < nstd; l++) {
00492 if (matches[l] == jm) {
00493 x1 = xobj[jm] - (xstd[l] + xoffmed);
00494 y1 = yobj[jm] - (ystd[l] + yoffmed);
00495 r1 = sqrt(x1*x1 + y1*y1);
00496 if (r2 < r1)
00497 matches[l] = -1;
00498 else
00499 dont = 1;
00500
00501 }
00502 }
00503 if (dont == 0)
00504 matches[k] = jm;
00505 }
00506 }
00507 jm = matches[1];
00508
00509
00510
00511
00512
00513 mstds = cpl_table_duplicate(stdstab);
00514 colname = (char *)cpl_table_get_column_name(objtab);
00515 while (colname != NULL) {
00516 if (strcmp(colname,"X_WORLD") && strcmp(colname,"Y_WORLD"))
00517 cpl_table_new_column(mstds,colname,
00518 cpl_table_get_column_type(objtab,colname));
00519
00520 colname = (char *)cpl_table_get_column_name(NULL);
00521 }
00522 cpl_table_unselect_all(mstds);
00523
00524
00525
00526 for (k = 0; k < nstd; k++) {
00527 jm = matches[k];
00528 if (jm != -1) {
00529 colname = (char *)cpl_table_get_column_name(objtab);
00530 while (colname != NULL) {
00531 if (!strcmp(colname,"X_WORLD") || !strcmp(colname,"Y_WORLD")) {
00532 colname = (char *)cpl_table_get_column_name(NULL);
00533 continue;
00534 }
00535 null = 0;
00536 switch (cpl_table_get_column_type(objtab,colname)) {
00537 case CPL_TYPE_INT:
00538 cpl_table_set_int(mstds,colname,k,
00539 cpl_table_get_int(objtab,colname,jm,
00540 &null));
00541 break;
00542 case CPL_TYPE_FLOAT:
00543 cpl_table_set_float(mstds,colname,k,
00544 cpl_table_get_float(objtab,colname,jm,
00545 &null));
00546 break;
00547 case CPL_TYPE_DOUBLE:
00548 cpl_table_set_double(mstds,colname,k,
00549 cpl_table_get_double(objtab,colname,
00550 jm,&null));
00551 break;
00552 default:
00553 cpl_table_set_float(mstds,colname,k,
00554 cpl_table_get_float(objtab,colname,jm,
00555 &null));
00556 break;
00557 }
00558 colname = (char *)cpl_table_get_column_name(NULL);
00559 }
00560 cpl_table_select_row(mstds,k);
00561 }
00562 }
00563
00564
00565
00566 *outtab = cpl_table_extract_selected(mstds);
00567 cpl_table_delete(mstds);
00568
00569
00570
00571 cpl_free(matches);
00572 cpl_free(xoffs);
00573 cpl_free(yoffs);
00574
00575 return 0;
00576 }
00577
00594
00595 cpl_table *omega_get_usnoa_table(const cpl_frame *catalog, cpl_vector *coords, int *nstds)
00596 {
00597 int number = 0;
00598 int i = 0;
00599
00600 double ramin = 0.0;
00601 double ramax = 0.0;
00602 double decmin = 0.0;
00603 double decmax = 0.0;
00604 char *pathname = NULL;
00605
00606
00607 cpl_table *table = NULL;
00608 cpl_table *extracted;
00609 cpl_table *all;
00610
00611
00612 table = cpl_table_load(cpl_frame_get_filename(catalog), 1, 0);
00613 if( table == NULL){
00614 return NULL;
00615 }
00616
00617
00618 pathname = omega_get_pathname(cpl_frame_get_filename(catalog));
00619
00620
00621 ramin = cpl_vector_get(coords, 0);
00622 ramax = cpl_vector_get(coords, 1);
00623 decmin = cpl_vector_get(coords, 2);
00624 decmax = cpl_vector_get(coords, 3);
00625
00626
00627 cpl_table_select_all(table);
00628 cpl_table_and_selected_double(table,"Dec_max",CPL_NOT_LESS_THAN,decmin);
00629 number = cpl_table_and_selected_double(table,"Dec_min",CPL_NOT_GREATER_THAN,decmax);
00630 if(number == 0){
00631 freetable(table);
00632 return NULL;
00633 }
00634
00635 extracted = cpl_table_extract_selected(table);
00636 freetable(table);
00637
00638
00639 for(i=0; i<number; i++){
00640 const char *name = cpl_table_get_string(extracted,"USNOA2_CAT",i);
00641 char *tname = cpl_sprintf("%s/%s",pathname,name);
00642 cpl_table *t = cpl_table_load(tname, 1, 0);
00643
00644
00645 cpl_table_select_all(t);
00646 cpl_table_and_selected_double(t, "RA2000",CPL_NOT_LESS_THAN, ramin);
00647 cpl_table_and_selected_double(t, "RA2000", CPL_NOT_GREATER_THAN, ramax);
00648
00649
00650 cpl_table_and_selected_double(t, "Dec2000", CPL_NOT_LESS_THAN, decmin);
00651 int number = cpl_table_and_selected_double(t, "Dec2000", CPL_NOT_GREATER_THAN, decmax);
00652 if (number <= 0) {
00653 freetable(t);
00654 freespace(tname);
00655 freetable(extracted);
00656 return NULL;
00657 }
00658
00659
00660 cpl_table *ext = cpl_table_extract_selected(t);
00661 if(i==0){
00662 all = cpl_table_new(0);
00663 cpl_table_copy_structure(all, ext);
00664 }
00665
00666 int irow = cpl_table_get_nrow(all);
00667
00668 cpl_table_insert(all, ext, irow+1);
00669 freetable(ext);
00670 freetable(t);
00671 freespace(tname);
00672 }
00673
00674 *nstds = cpl_table_get_nrow(all);
00675
00676
00677 freetable(extracted);
00678
00679 return all;
00680 }
00681
00682
00700
00701 cpl_table *omega_get_stds_table(const char *stdcat, cpl_vector *coords, int *nstds)
00702 {
00703 int number = 0;
00704 double ramin = 0.0;
00705 double ramax = 0.0;
00706 double decmin = 0.0;
00707 double decmax = 0.0;
00708 cpl_table *table = NULL;
00709 cpl_table *extracted;
00710
00711
00712 table = cpl_table_load(stdcat, 1, 0);
00713 if( table == NULL){
00714 cpl_msg_warning(cpl_func,"Cannot load reference catalogue %s",stdcat);
00715 return NULL;
00716 }
00717
00718
00719 ramin = cpl_vector_get(coords, 0);
00720 ramax = cpl_vector_get(coords, 1);
00721 decmin = cpl_vector_get(coords, 2);
00722 decmax = cpl_vector_get(coords, 3);
00723
00724
00725 cpl_table_select_all(table);
00726
00727 cpl_table_and_selected_double(table,"Dec",CPL_NOT_LESS_THAN,decmin);
00728
00729 cpl_table_and_selected_double(table,"Dec",CPL_NOT_GREATER_THAN,decmax);
00730
00731 cpl_table_and_selected_double(table,"Ra",CPL_NOT_LESS_THAN,ramin);
00732
00733 number = cpl_table_and_selected_double(table,"Ra",CPL_NOT_GREATER_THAN,ramax);
00734
00735 if(number <= 0){
00736 cpl_msg_debug(cpl_func,"There are no stars in catalogue in given region");
00737 freetable(table);
00738 return NULL;
00739 }
00740
00741 extracted = cpl_table_extract_selected(table);
00742 freetable(table);
00743
00744 *nstds = cpl_table_get_nrow(extracted);
00745
00746
00747 return extracted;
00748 }
00749
00750
00766 cpl_table *omega_filter_table_int(cpl_table *src, const char *col, cpl_table_select_operator operator,
00767 int value, int *nsrc)
00768 {
00769 cpl_table *out;
00770
00771 if(cpl_table_has_column(src, col) != 1)
00772 return NULL;
00773
00774 if(cpl_table_get_column_type(src, col) != CPL_TYPE_INT)
00775 return NULL;
00776
00777 cpl_table_unselect_all(src);
00778 cpl_table_or_selected_int(src, col, operator, value);
00779 out = cpl_table_extract_selected(src);
00780 *nsrc = cpl_table_get_nrow(out);
00781
00782 return out;
00783
00784 }
00785
00786
00800 cpl_table *omega_filter_table_double(cpl_table *src, const char *col, cpl_table_select_operator operator,
00801 double value, int *nsrc)
00802 {
00803 cpl_table *out;
00804
00805 if(cpl_table_has_column(src, col) != 1)
00806 return NULL;
00807
00808 if(cpl_table_get_column_type(src, col) != CPL_TYPE_DOUBLE)
00809 return NULL;
00810
00811 cpl_table_unselect_all(src);
00812 cpl_table_or_selected_double(src, col, operator, value);
00813 out = cpl_table_extract_selected(src);
00814 *nsrc = cpl_table_get_nrow(out);
00815
00816 return out;
00817
00818 }
00819
00820
00834 cpl_table *omega_filter_table_string(cpl_table *src, const char *col, cpl_table_select_operator operator,
00835 const char *value, int *nsrc)
00836 {
00837 cpl_table *out;
00838
00839 if(cpl_table_has_column(src, col) != 1)
00840 return NULL;
00841
00842 if(cpl_table_get_column_type(src, col) != CPL_TYPE_STRING)
00843 return NULL;
00844
00845 cpl_table_unselect_all(src);
00846 cpl_table_or_selected_string(src, col, operator, value);
00847 out = cpl_table_extract_selected(src);
00848 *nsrc = cpl_table_get_nrow(out);
00849
00850 return out;
00851
00852 }
00853
00854
00868 cpl_table *omega_filter_table_float(cpl_table *src, const char *col, cpl_table_select_operator operator,
00869 float value, int *nsrc)
00870 {
00871 cpl_table *out;
00872
00873 if(cpl_table_has_column(src, col) != 1)
00874 return NULL;
00875
00876 if(cpl_table_get_column_type(src, col) != CPL_TYPE_FLOAT)
00877 return NULL;
00878
00879 cpl_table_unselect_all(src);
00880 cpl_table_or_selected_float(src, col, operator, value);
00881 out = cpl_table_extract_selected(src);
00882 *nsrc = cpl_table_get_nrow(out);
00883
00884 return out;
00885
00886 }
00887
00898 cpl_table *omega_science_catalogue(const char *sci, const char *conf, double zeropoint_final, cpl_parameterlist *pars)
00899 {
00900 int i = 0;
00901 int nsources = 0;
00902 int npars = 0;
00903 double cat_thre = 1.0;
00904 char *cmd = NULL;
00905 const char *path = NULL;
00906 const char *sex_conf = NULL;
00907 const char *sex_conv = NULL;
00908 const char *sex_nnw = NULL;
00909 const char *sex_par = NULL;
00910 const char *text = NULL;
00911 const char *srcs = "omega_sci_cat.fits";
00912
00913 cpl_table *table, *sources;
00914 cpl_parameter *par;
00915
00916
00917
00918 npars = cpl_parameterlist_get_size(pars);
00919 par = cpl_parameterlist_get_first(pars);
00920
00921 for(i=0; i<npars; i++) {
00922 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00923 if(strcmp("bin-path", text) == 0) {
00924 path = cpl_parameter_get_string(par) ;
00925 }
00926 else if(strcmp("sex-config", text) == 0) {
00927 sex_conf = cpl_parameter_get_string(par) ;
00928 }
00929 else if(strcmp("sex-conv", text) == 0) {
00930 sex_conv = cpl_parameter_get_string(par) ;
00931 }
00932 else if(strcmp("sex-param", text) == 0) {
00933 sex_par = cpl_parameter_get_string(par) ;
00934 }
00935 else if(strcmp("sex-nnw", text) == 0) {
00936 sex_nnw = cpl_parameter_get_string(par) ;
00937 }
00938 else if(strcmp("cat-thre", text) == 0){
00939 cat_thre = cpl_parameter_get_double(par);
00940 }
00941 par = cpl_parameterlist_get_next(pars);
00942
00943 }
00944
00945 cpl_msg_debug(cpl_func,"zeropoint_final: %f",zeropoint_final);
00946
00947
00948
00949 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00950 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g "
00951 "-WEIGHT_TYPE MAP_WEIGHT -WEIGHT_IMAGE %s -CATALOG_TYPE FITS_1.0 -CATALOG_NAME %s -MAG_ZEROPOINT %g "
00952 "-BACKPHOTO_TYPE LOCAL",
00953 path,
00954 sci,
00955 sex_conf,
00956 sex_par,
00957 sex_conv,
00958 sex_nnw,
00959 cat_thre,
00960 cat_thre,
00961 conf,
00962 srcs,
00963 zeropoint_final);
00964
00965 cpl_msg_debug(cpl_func,"Sextractor call: %s",cmd);
00966
00967 if (system(cmd) != 0) {
00968 cpl_msg_debug(cpl_func,"Failed to create catalogue using Sextractor");
00969 freespace(cmd);
00970 return NULL;
00971 }
00972 freespace(cmd);
00973
00974 table = cpl_table_load(srcs, 1, 0);
00975 if((table == NULL) || (cpl_table_get_nrow(table) <= 0)){
00976 cpl_msg_debug(cpl_func,"Science catalogue table is empty or NULL");
00977 return NULL;
00978 }
00979
00980
00981 sources = omega_filter_table_int(table, "FLAGS", CPL_EQUAL_TO, 0, &nsources);
00982
00983 freetable(table);
00984
00985
00986 return sources;
00987 }
00988
00991
00992
00993
00994
00995 cpl_table * wcscor_matchstds(cpl_table *objtab, cpl_table *stdstab, float srad)
00996 {
00997
00998 int i = 0;
00999 int j = 0;
01000 int k = 0;
01001 int nobj, nstd;
01002 float *xobj;
01003 float *yobj;
01004 float *xstd;
01005 float *ystd;
01006 const char *colname;
01007 cpl_table *matches, *temp;
01008 cpl_propertylist *p;
01009
01010 nobj = cpl_table_get_nrow(objtab);
01011 nstd = cpl_table_get_nrow(stdstab);
01012 if (nobj == 0) {
01013 cpl_msg_warning(cpl_func,"Object table has no rows");
01014 } else if (nstd == 0) {
01015 cpl_msg_warning(cpl_func,"Reference standards table has no rows");
01016 }
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035 cpl_table_unselect_all(objtab);
01036 cpl_table_unselect_all(stdstab);
01037
01038
01039 xobj = cpl_table_get_data_float(objtab,"X_IMAGE");
01040 xstd = cpl_table_get_data_float(stdstab, "xpredict");
01041 yobj = cpl_table_get_data_float(objtab,"Y_IMAGE");
01042 ystd = cpl_table_get_data_float(stdstab, "ypredict");
01043
01044
01045
01046 if (xstd == NULL || ystd == NULL || xobj == NULL || yobj == NULL){
01047 cpl_msg_error(cpl_func,"Fatal error in wcscor_matchstds");
01048 return NULL;
01049 }
01050
01051 k = 0;
01052
01053 for (i=0; i<nobj; i++){
01054 for(j=0; j<nstd; j++){
01055 if((xstd[j] <= xobj[i] +srad) && (xstd[j] >= xobj[i] -srad) &&
01056 (ystd[j] <= yobj[i] +srad) && (ystd[j] >= yobj[i] -srad)) {
01057 cpl_table_select_row(stdstab, j);
01058 cpl_table_select_row(objtab, i);
01059 k++;
01060
01061 break;
01062 }
01063 }
01064 }
01065
01066 matches = cpl_table_extract_selected(stdstab);
01067 temp = cpl_table_extract_selected(objtab);
01068 for (i=0; i<cpl_table_get_ncol(temp); i++){
01069 colname = cpl_table_get_column_name(temp);
01070 cpl_table_move_column(matches, colname, temp);
01071 }
01072
01073
01074 return matches;
01075 }