42 #include "omega_wcscor.h"
43 #include "omega_catalog.h"
44 #include "omega_dfs.h"
45 #include "omega_pfits.h"
46 #include "omega_trim.h"
47 #include "omega_utils.h"
52 static char *my_strchr(
char *,
char);
55 static char * my_strchr(
char *
string,
char find)
57 while(*
string != find)
103 cpl_vector *region_vec;
104 cpl_propertylist *plist;
109 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
110 omega_wfi_correct_wcs(plist, xlist);
116 if(region_vec != NULL){
120 xoffset = (int)cpl_vector_get(region_vec, 0);
121 xoffset = xoffset -1;
122 yoffset = (int)cpl_vector_get(region_vec, 1);
123 yoffset = yoffset -1;
125 crpix1 = cpl_propertylist_get_double(xlist,
"CRPIX1");
126 crpix2 = cpl_propertylist_get_double(xlist,
"CRPIX2");
127 crpix1 = crpix1 - xoffset;
128 crpix2 = crpix2 - yoffset;
130 cpl_propertylist_update_double(xlist,
"CRPIX1", crpix1);
131 cpl_propertylist_update_double(xlist,
"CRPIX2", crpix2);
133 cpl_vector_delete(region_vec);
163 cpl_propertylist *
omega_match_points(
const char *reduced,
const char *reduced_weight,
const cpl_frame *catalog, cpl_parameterlist *pars,
164 int ext,
int *nmatches,
double *mratio)
167 int i, nsources=0, nsources_flag0=0, nstds, nstars, npattern;
171 int SextractorFlagLimit=0;
175 int nsources_firstpass=0;
176 double matchlimit=0.;
180 double pradius = 0.0;
181 double maxpradius = 0.0;
183 double astrom_thre = 0.0;
186 double SextractorClassStar=0.0;
187 double centralwl=0.0;
189 const char *text = NULL;
191 const char *sex_conf = NULL;
192 const char *sex_conv = NULL;
193 const char *sex_par = NULL;
194 const char *sex_nnw = NULL;
195 const char *srcs =
"omega_cat_astrom_sources.fits";
197 cpl_array *status, *matches;
198 cpl_table *sources=NULL, *sources_flag0=NULL, *stds, *extracted, *data;
199 cpl_table *dummy_table1=NULL;
200 cpl_table * dummy_table=NULL;
203 cpl_matrix *from, *to, *xymat, *rdmat, *stars, *pattern;
206 cpl_wcs_platesol_fitmode fitmode = CPL_WCS_PLATESOL_4;
207 cpl_propertylist *xlist = NULL;
208 cpl_propertylist *plist = NULL;
209 cpl_propertylist *filterlist = NULL;
211 cpl_propertylist *aplist = NULL;
215 npars = cpl_parameterlist_get_size(pars);
216 par = cpl_parameterlist_get_first(pars);
218 for(i=0; i<npars; i++) {
219 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
220 if(strcmp(
"bin-path", text) == 0) {
221 path = cpl_parameter_get_string(par) ;
223 else if(strcmp(
"sex-config", text) == 0) {
224 sex_conf = cpl_parameter_get_string(par) ;
226 else if(strcmp(
"sex-conv", text) == 0) {
227 sex_conv = cpl_parameter_get_string(par) ;
229 else if(strcmp(
"sex-param", text) == 0) {
230 sex_par = cpl_parameter_get_string(par) ;
232 else if(strcmp(
"sex-nnw", text) == 0) {
233 sex_nnw = cpl_parameter_get_string(par) ;
235 else if(strcmp(
"astrom-thre", text) == 0){
236 astrom_thre = cpl_parameter_get_double(par);
238 else if(strcmp(
"nstars", text) == 0){
239 nstars = cpl_parameter_get_int(par);
241 else if(strcmp(
"npattern", text) == 0){
242 npattern = cpl_parameter_get_int(par);
244 else if(strcmp(
"pradius", text) == 0){
245 pradius = cpl_parameter_get_double(par);
247 else if(strcmp(
"maxpradius", text) == 0){
248 maxpradius = cpl_parameter_get_double(par);
250 else if(strcmp(
"tol", text) == 0){
251 tol = cpl_parameter_get_double(par);
253 else if(strcmp(
"platesol", text) == 0){
254 mode = cpl_parameter_get_int(par);
255 if(mode == 6) fitmode = CPL_WCS_PLATESOL_6;
257 else if(strcmp(
"niter", text) == 0){
258 niter = cpl_parameter_get_int(par);
260 else if(strcmp(
"SextractorFlagLimit", text) == 0){
261 SextractorFlagLimit = cpl_parameter_get_int(par);
263 else if(strcmp(
"SextractorClassStar", text) == 0){
264 SextractorClassStar= cpl_parameter_get_double(par);
267 par = cpl_parameterlist_get_next(pars);
276 xlist = cpl_propertylist_load(reduced, 1);
277 if(cpl_propertylist_has(xlist,
"ESO QC SCI ZEROPNT"))
278 zp = cpl_propertylist_get_double(xlist,
"ESO QC SCI ZEROPNT");
280 if(cpl_propertylist_has(xlist,
"NAXIS1"))
281 imsizex = cpl_propertylist_get_int(xlist,
"NAXIS1");
282 if(cpl_propertylist_has(xlist,
"NAXIS2"))
283 imsizey = cpl_propertylist_get_int(xlist,
"NAXIS2");
286 plist = cpl_propertylist_load(reduced, 0);
287 if(cpl_propertylist_has(xlist,
"EXTNAME") &&
288 !cpl_propertylist_has(plist,
"EXTNAME"))
289 cpl_propertylist_copy_property(plist,xlist,
"EXTNAME");
293 if(cpl_propertylist_has(filterlist,
"CWL")){
294 centralwl=cpl_propertylist_get_double(filterlist,
"CWL");
300 freeplist(filterlist);
307 cpl_msg_debug(cpl_func,
"gain: %f",gain);
312 for(i = 0; i < 15; i++) {
315 if(reduced_weight != NULL){
316 cmd = cpl_sprintf(
"%s/sex %s -WEIGHT_TYPE MAP_WEIGHT -WEIGHT_IMAGE %s "
317 "-c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
318 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g -GAIN %g -MAG_ZEROPOINT %f "
319 "-CATALOG_TYPE FITS_1.0 -CATALOG_NAME %s -CHECKIMAGE_NAME %s "
320 "-CHECKIMAGE_TYPE OBJECTS -DEBLEND_NTHRESH 1 2>/dev/null",
321 path, reduced, reduced_weight, sex_conf, sex_par, sex_conv, sex_nnw, astrom_thre, astrom_thre,
322 gain, zp, srcs,
"omega_ima_detected_astrom_srcs.fits");
326 cmd = cpl_sprintf(
"%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
327 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g -GAIN %g -MAG_ZEROPOINT %f "
328 "-CATALOG_TYPE FITS_1.0 -CATALOG_NAME %s -CHECKIMAGE_NAME %s "
329 "-CHECKIMAGE_TYPE OBJECTS -DEBLEND_NTHRESH 1 2>/dev/null",
330 path, reduced, sex_conf, sex_par, sex_conv, sex_nnw, astrom_thre, astrom_thre,
331 gain, zp, srcs,
"omega_ima_detected_astrom_srcs.fits");
334 cpl_msg_debug(cpl_func,
"cmd: %s", cmd);
335 if (system(cmd) != 0) {
336 cpl_msg_error(cpl_func,
"Failed to detect sources in image using Sextractor");
343 data = cpl_table_load(srcs, 1, 0);
345 cpl_msg_error(cpl_func,
"Sources table is NULL");
352 CPL_NOT_GREATER_THAN, SextractorFlagLimit, &nsources_flag0);
354 CPL_NOT_LESS_THAN, SextractorClassStar, &nsources);
356 freetable(sources_flag0);
359 nsources_firstpass=nsources;
362 if(nsources > 1000) {
364 astrom_thre = astrom_thre + 30;
372 p = cpl_propertylist_new();
373 cpl_propertylist_append_bool(p,
"MAG_AUTO",0);
374 if (cpl_table_sort(sources, p) != CPL_ERROR_NONE) {
375 cpl_msg_error(cpl_func,
"Cannot sort sources table");
388 if(nsources_firstpass>500)
392 dummy_table=cpl_table_duplicate(sources);
394 cpl_table_and_selected_float(dummy_table,
"X_IMAGE",CPL_NOT_LESS_THAN,1.+500.);
395 cpl_table_and_selected_float(dummy_table,
"Y_IMAGE",CPL_NOT_LESS_THAN,1.+500.);
396 cpl_table_and_selected_float(dummy_table,
"X_IMAGE",CPL_NOT_GREATER_THAN,(
float)imsizex-500.);
397 cpl_table_and_selected_float(dummy_table,
"Y_IMAGE",CPL_NOT_GREATER_THAN,(
float)imsizey-500.);
399 sources= cpl_table_extract_selected(dummy_table);
401 cpl_table_not_selected(dummy_table);
402 dummy_table1=cpl_table_extract_selected(dummy_table);
404 cpl_table_insert(sources, dummy_table1, cpl_table_get_nrow(sources));
405 freetable(dummy_table);
406 freetable(dummy_table1);
407 nsources=cpl_table_get_nrow(sources);
410 cpl_msg_info(cpl_func,
"Number of sources in image: %d", nsources);
411 if(nsources < nstars){
412 nstars = nsources -1;
416 if(pradius > maxpradius)
417 pradius = maxpradius;
421 from = cpl_matrix_new(nsources, 2);
422 for (i=0; i<nsources; i++){
423 float xim = cpl_table_get_float(sources,
"X_IMAGE", i, NULL);
424 float yim = cpl_table_get_float(sources,
"Y_IMAGE", i, NULL);
425 cpl_matrix_set(from, i, 0, xim);
426 cpl_matrix_set(from, i, 1, yim);
430 stars = cpl_matrix_transpose_create(from);
434 cpl_msg_error(cpl_func,
"Cannot get coverage of image");
446 coords = cpl_vector_new(4);
447 cpl_vector_set(coords, 0, x1);
448 cpl_vector_set(coords, 1, x2);
449 cpl_vector_set(coords, 2, y1);
450 cpl_vector_set(coords, 3, y2);
454 cpl_msg_warning(cpl_func,
"Cannot find USNOA2 stars in this region: {RA-RA:DEC-DEC}={%g-%g:%g=%g}",
465 cpl_msg_info(cpl_func,
"Number of USNOA2 stars in this region: %d", nstds);
466 cpl_msg_debug(cpl_func,
"%d USNOA2 stars fall in region: {RA-RA:DEC-DEC}={%g--%g:%g--%g}", nstds,x1,x2,y1,y2);
469 from = cpl_matrix_new(nstds, 2);
470 for (i=0; i<nstds; i++){
471 cpl_matrix_set(from, i, 0, cpl_table_get_double(stds,
"RA2000", i, NULL));
472 cpl_matrix_set(from, i, 1, cpl_table_get_double(stds,
"Dec2000", i, NULL));
477 wcs = cpl_wcs_new_from_propertylist(xlist);
478 if(cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE){
487 cpl_msg_error(cpl_func,
"Error in cpl_wcs conversion. %s",cpl_error_get_message());
495 cpl_table_new_column(stds,
"xpredict",CPL_TYPE_FLOAT);
496 cpl_table_set_column_unit(stds,
"xpredict",
"pixels");
497 cpl_table_new_column(stds,
"ypredict",CPL_TYPE_FLOAT);
498 cpl_table_set_column_unit(stds,
"ypredict",
"pixels");
502 for(i=0; i< nstds; i++){
503 float xp = (float)cpl_matrix_get(to, i, 0);
504 float yp = (float)cpl_matrix_get(to, i, 1);
505 cpl_table_set_float(stds,
"xpredict", i, xp);
506 cpl_table_set_float(stds,
"ypredict", i, yp);
517 p = cpl_propertylist_new();
520 cpl_propertylist_append_bool(p,
"MagR",0);
521 cpl_msg_info(cpl_func,
"Sorting standard star catalogue by R-band mag");
524 cpl_propertylist_append_bool(p,
"MagB",0);
525 cpl_msg_info(cpl_func,
"Sorting standard star catalogue by B-band mag");
528 if (cpl_table_sort(stds, p) != CPL_ERROR_NONE) {
529 cpl_msg_error(cpl_func,
"Cannot sort stds table");
544 dummy_table=cpl_table_duplicate(stds);
547 if(nsources_firstpass>500){
548 cpl_table_and_selected_float(dummy_table,
"xpredict",CPL_NOT_LESS_THAN,1.+600.);
549 cpl_table_and_selected_float(dummy_table,
"ypredict",CPL_NOT_LESS_THAN,1.+600.);
550 cpl_table_and_selected_float(dummy_table,
"xpredict",CPL_NOT_GREATER_THAN,(
float)imsizex-600.);
551 cpl_table_and_selected_float(dummy_table,
"ypredict",CPL_NOT_GREATER_THAN,(
float)imsizey-600.);
554 cpl_table_and_selected_float(dummy_table,
"xpredict",CPL_NOT_LESS_THAN,1.+100.);
555 cpl_table_and_selected_float(dummy_table,
"ypredict",CPL_NOT_LESS_THAN,1.+100.);
556 cpl_table_and_selected_float(dummy_table,
"xpredict",CPL_NOT_GREATER_THAN,(
float)imsizex-100.);
557 cpl_table_and_selected_float(dummy_table,
"ypredict",CPL_NOT_GREATER_THAN,(
float)imsizey-100.);
560 stds= cpl_table_extract_selected(dummy_table);
561 cpl_table_not_selected(dummy_table);
562 dummy_table1=cpl_table_extract_selected(dummy_table);
564 cpl_table_insert(stds, dummy_table1, cpl_table_get_nrow(stds));
565 freetable(dummy_table);
566 freetable(dummy_table1);
569 cpl_msg_warning(cpl_func,
"Cannot find USNOA2 stars within the image "
586 nstds=cpl_table_get_nrow(stds);
587 cpl_msg_info(cpl_func,
"Number of USNOA2 stars falling on the image: %d", nstds);
591 pattern = cpl_matrix_new(2, nstds);
592 for(i=0; i<nstds; i++){
593 float x = cpl_table_get_float(stds,
"xpredict", i, NULL);
594 float y = cpl_table_get_float(stds,
"ypredict", i, NULL);
595 cpl_matrix_set(pattern, 0, i, x);
596 cpl_matrix_set(pattern, 1, i, y);
600 if(nstds < npattern){
604 cpl_msg_debug(cpl_func,
"stars: % " CPL_SIZE_FORMAT
" , nstars: %d pattern: % " CPL_SIZE_FORMAT
" npattern: %d",
605 cpl_matrix_get_ncol(stars), nstars, cpl_matrix_get_ncol(pattern),
656 matchlimit = nstds < nsources ? (double)nstds*0.25 : (
double)nsources*0.25;
657 if(matchlimit < 5) matchlimit=5;
658 cpl_msg_debug(cpl_func,
"Match limit: %f", matchlimit);
659 for (i = 0; i < 4; i++) {
663 cpl_msg_debug(cpl_func,
"nstars: %d - npattern: %d ", nstars, npattern);
664 matches = cpl_ppm_match_points(stars, nstars, 0.5, pattern, npattern,
665 15, tol, pradius, NULL, NULL, &scale, &angle);
667 *nmatches = cpl_array_get_size(matches) - cpl_array_count_invalid(matches);
669 if((matches == NULL) || (*nmatches < matchlimit )) {
670 cpl_msg_debug(cpl_func,
"nstars: %d npattern: %d i: %d",nstars,npattern,i );
687 cpl_msg_debug(cpl_func,
"Increasing number of stars in PPM; scale=%g angle=%g "
688 "nstars=%d npattern=%d nmatches=%d",scale,angle,nstars,npattern,*nmatches);
697 *mratio = (double )(*nmatches / (
double)nsources);
699 if((matches == NULL) || (*nmatches < matchlimit)){
700 cpl_msg_error(cpl_func,
"Error in PPM module: Not enough matches found");
711 cpl_msg_info(cpl_func,
"There are %d matches between USNOA2 and image", *nmatches);
714 cpl_table_new_column(sources,
"RA2000", CPL_TYPE_DOUBLE);
715 cpl_table_new_column(sources,
"Dec2000", CPL_TYPE_DOUBLE);
716 cpl_table_unselect_all(stds);
717 cpl_table_unselect_all(sources);
718 for(i=0; i<cpl_matrix_get_ncol(pattern); i++){
720 int index = cpl_array_get_int(matches,i, &null);
722 double ra = cpl_table_get_double(stds,
"RA2000", i, NULL);
723 double dec = cpl_table_get_double(stds,
"Dec2000", i, NULL);
724 cpl_table_set(sources,
"RA2000", index, ra);
725 cpl_table_set(sources,
"Dec2000", index, dec);
726 cpl_table_select_row(sources, index);
734 extracted = cpl_table_extract_selected(sources);
735 rdmat = cpl_matrix_new(cpl_table_get_nrow(extracted), 2);
736 xymat = cpl_matrix_new(cpl_table_get_nrow(extracted), 2);
737 for (i=0; i<cpl_table_get_nrow(extracted); i++){
738 double ra = cpl_table_get_double(extracted,
"RA2000", i, NULL);
739 double dec = cpl_table_get_double(extracted,
"Dec2000", i, NULL);
740 cpl_matrix_set(rdmat, i, 0, ra);
741 cpl_matrix_set(rdmat, i, 1, dec);
742 float x = cpl_table_get_float(extracted,
"X_IMAGE", i, NULL);
743 float y = cpl_table_get_float(extracted,
"Y_IMAGE", i, NULL);
744 cpl_matrix_set(xymat, i, 0, x);
745 cpl_matrix_set(xymat, i, 1, y);
747 freetable(extracted);
751 if(cpl_wcs_platesol(xlist, rdmat, xymat, niter, 3.0, fitmode,
752 CPL_WCS_MV_CRPIX, &aplist) != CPL_ERROR_NONE) {
753 cpl_msg_error(cpl_func,
"Cannot calculate plate solution. %s", cpl_error_get_message());
776 void omega_wfi_correct_wcs(cpl_propertylist *mplist, cpl_propertylist *xplist)
783 double pixsca = 0.238;
784 double rotskypa = 0.0;
800 char ccdstr[80] =
"";
801 const char *ccd = NULL;
804 if(cpl_propertylist_has(xplist,
"ESO DET CHIP ID") == 1){
805 ccd = cpl_propertylist_get_string(xplist,
"ESO DET CHIP ID");
809 sprintf(key,
"ESO DET CHIP%d ID", i);
810 if(cpl_propertylist_has(xplist,key)==1){
814 sprintf(ccdstr,
"ESO DET CHIP%d ID", i);
815 ccd = cpl_propertylist_get_string(xplist, ccdstr);
819 if(strcmp(ccd,
"ccd50")==0){
823 else if(strcmp(ccd,
"ccd51")==0){
827 else if(strcmp(ccd,
"ccd52")==0){
831 else if(strcmp(ccd,
"ccd53")==0){
835 else if(strcmp(ccd,
"ccd54")==0){
839 else if(strcmp(ccd,
"ccd55")==0){
843 else if(strcmp(ccd,
"ccd56")==0){
847 else if(strcmp(ccd,
"ccd57")==0){
853 tpa = cpl_propertylist_get_double(mplist,
"RA");
854 tpd = cpl_propertylist_get_double(mplist,
"DEC");
857 angle = (rotskypa-180.0)*PI/180.0;
862 secd = 1.0/cos(tpd*PI/180.0);
863 tpa = tpa - yapoff * secd * cos(angle) + xapoff *secd * sin(angle);
864 tpd = tpd + yapoff * sin(angle) + xapoff * cos(angle);
868 aap = -pixsca/3600.0;
878 aa = aap * cos(angle) + bbp * sin(angle);
879 bb =-aap * sin(angle) + bbp * cos(angle);
880 dd = ddp * cos(angle) + eep * sin(angle);
881 ee =-ddp * sin(angle) + eep * cos(angle);
889 cpl_propertylist_update_double(xplist,
"CRPIX1",cc);
890 cpl_propertylist_update_double(xplist,
"CRPIX2",ff);
891 cpl_propertylist_update_double(xplist,
"CRVAL1",tpa);
892 cpl_propertylist_update_double(xplist,
"CRVAL2",tpd);
893 cpl_propertylist_erase(xplist,
"CDELT1");
894 cpl_propertylist_erase(xplist,
"CDELT2");
895 cpl_propertylist_update_double(xplist,
"CD1_1",aa);
896 cpl_propertylist_update_double(xplist,
"CD1_2",bb);
897 cpl_propertylist_update_double(xplist,
"CD2_1",dd);
898 cpl_propertylist_update_double(xplist,
"CD2_2",ee);
901 cpl_propertylist_update_string(xplist,
"CTYPE1",
"RA---TAN");
902 cpl_propertylist_update_string(xplist,
"CTYPE2",
"DEC--TAN");