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 #include <cpl.h>
00041
00042 #include "omega_wcscor.h"
00043 #include "omega_catalog.h"
00044 #include "omega_dfs.h"
00045 #include "omega_pfits.h"
00046 #include "omega_trim.h"
00047 #include "omega_utils.h"
00048
00049
00050
00051
00052 static char *my_strchr(char *, char);
00053
00054
00055 static char * my_strchr(char *string, char find)
00056 {
00057 while(*string != find)
00058 {
00059 if(*string == '\0')
00060 return NULL;
00061
00062 ++string;
00063 }
00064
00065 return (string);
00066 }
00067
00068
00097 int omega_shift_refpix(const cpl_frame *frame, int xn, cpl_propertylist *xlist)
00098 {
00099 int xoffset = 0;
00100 int yoffset = 0;
00101 double crpix1 = 0.0;
00102 double crpix2 = 0.0;
00103 cpl_vector *region_vec;
00104 cpl_propertylist *plist;
00105
00106
00107 if(omega_pfits_check_instrume(frame) == 1){
00108
00109 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
00110 omega_wfi_correct_wcs(plist, xlist);
00111 freeplist(plist);
00112 }
00113
00114 region_vec = get_trim_region(frame, xn);
00115
00116 if(region_vec != NULL){
00117
00118
00119
00120 xoffset = (int)cpl_vector_get(region_vec, 0);
00121 xoffset = xoffset -1;
00122 yoffset = (int)cpl_vector_get(region_vec, 1);
00123 yoffset = yoffset -1;
00124
00125 crpix1 = cpl_propertylist_get_double(xlist, "CRPIX1");
00126 crpix2 = cpl_propertylist_get_double(xlist, "CRPIX2");
00127 crpix1 = crpix1 - xoffset;
00128 crpix2 = crpix2 - yoffset;
00129
00130 cpl_propertylist_update_double(xlist, "CRPIX1", crpix1);
00131 cpl_propertylist_update_double(xlist, "CRPIX2", crpix2);
00132
00133 cpl_vector_delete(region_vec);
00134 }
00135
00136 return 0;
00137 }
00138
00139
00163 cpl_propertylist *omega_match_points(const char *reduced, const char *reduced_weight, const cpl_frame *catalog, cpl_parameterlist *pars,
00164 int ext, int *nmatches, double *mratio)
00165 {
00166
00167 int i, nsources=0, nsources_flag0=0, nstds, nstars, npattern;
00168 int mode = 0;
00169 int niter = 1;
00170 int npars = 0;
00171 int SextractorFlagLimit=0;
00172 int imsizex=0;
00173 int imsizey=0;
00174
00175 int nsources_firstpass=0;
00176 double matchlimit=0.;
00177 double zp = 0.0;
00178 double gain = 1.0;
00179 double x1,x2,y1,y2;
00180 double pradius = 0.0;
00181 double maxpradius = 0.0;
00182 double tol = 0.0;
00183 double astrom_thre = 0.0;
00184 double scale = 0.0;
00185 double angle = 0.0;
00186 double SextractorClassStar=0.0;
00187 double centralwl=0.0;
00188 char *cmd = NULL;
00189 const char *text = NULL;
00190 const char *path;
00191 const char *sex_conf = NULL;
00192 const char *sex_conv = NULL;
00193 const char *sex_par = NULL;
00194 const char *sex_nnw = NULL;
00195 const char *srcs = "omega_cat_astrom_sources.fits";
00196 cpl_parameter *par;
00197 cpl_array *status, *matches;
00198 cpl_table *sources=NULL, *sources_flag0=NULL, *stds, *extracted, *data;
00199 cpl_table *dummy_table1=NULL;
00200 cpl_table * dummy_table=NULL;
00201
00202
00203 cpl_matrix *from, *to, *xymat, *rdmat, *stars, *pattern;
00204 cpl_wcs *wcs;
00205 cpl_vector *coords;
00206 cpl_wcs_platesol_fitmode fitmode = CPL_WCS_PLATESOL_4;
00207 cpl_propertylist *xlist = NULL;
00208 cpl_propertylist *plist = NULL;
00209 cpl_propertylist *filterlist = NULL;
00210
00211 cpl_propertylist *aplist = NULL;
00212 cpl_propertylist *p;
00213
00214
00215 npars = cpl_parameterlist_get_size(pars);
00216 par = cpl_parameterlist_get_first(pars);
00217
00218 for(i=0; i<npars; i++) {
00219 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00220 if(strcmp("bin-path", text) == 0) {
00221 path = cpl_parameter_get_string(par) ;
00222 }
00223 else if(strcmp("sex-config", text) == 0) {
00224 sex_conf = cpl_parameter_get_string(par) ;
00225 }
00226 else if(strcmp("sex-conv", text) == 0) {
00227 sex_conv = cpl_parameter_get_string(par) ;
00228 }
00229 else if(strcmp("sex-param", text) == 0) {
00230 sex_par = cpl_parameter_get_string(par) ;
00231 }
00232 else if(strcmp("sex-nnw", text) == 0) {
00233 sex_nnw = cpl_parameter_get_string(par) ;
00234 }
00235 else if(strcmp("astrom-thre", text) == 0){
00236 astrom_thre = cpl_parameter_get_double(par);
00237 }
00238 else if(strcmp("nstars", text) == 0){
00239 nstars = cpl_parameter_get_int(par);
00240 }
00241 else if(strcmp("npattern", text) == 0){
00242 npattern = cpl_parameter_get_int(par);
00243 }
00244 else if(strcmp("pradius", text) == 0){
00245 pradius = cpl_parameter_get_double(par);
00246 }
00247 else if(strcmp("maxpradius", text) == 0){
00248 maxpradius = cpl_parameter_get_double(par);
00249 }
00250 else if(strcmp("tol", text) == 0){
00251 tol = cpl_parameter_get_double(par);
00252 }
00253 else if(strcmp("platesol", text) == 0){
00254 mode = cpl_parameter_get_int(par);
00255 if(mode == 6) fitmode = CPL_WCS_PLATESOL_6;
00256 }
00257 else if(strcmp("niter", text) == 0){
00258 niter = cpl_parameter_get_int(par);
00259 }
00260 else if(strcmp("SextractorFlagLimit", text) == 0){
00261 SextractorFlagLimit = cpl_parameter_get_int(par);
00262 }
00263 else if(strcmp("SextractorClassStar", text) == 0){
00264 SextractorClassStar= cpl_parameter_get_double(par);
00265 }
00266
00267 par = cpl_parameterlist_get_next(pars);
00268
00269 }
00270
00271
00272
00273
00274
00275
00276 xlist = cpl_propertylist_load(reduced, 1);
00277 if(cpl_propertylist_has(xlist,"ESO QC SCI ZEROPNT"))
00278 zp = cpl_propertylist_get_double(xlist,"ESO QC SCI ZEROPNT");
00279
00280 if(cpl_propertylist_has(xlist,"NAXIS1"))
00281 imsizex = cpl_propertylist_get_int(xlist,"NAXIS1");
00282 if(cpl_propertylist_has(xlist,"NAXIS2"))
00283 imsizey = cpl_propertylist_get_int(xlist,"NAXIS2");
00284
00285
00286 plist = cpl_propertylist_load(reduced, 0);
00287 if(cpl_propertylist_has(xlist,"EXTNAME") &&
00288 !cpl_propertylist_has(plist,"EXTNAME"))
00289 cpl_propertylist_copy_property(plist,xlist,"EXTNAME");
00290
00291 filterlist=omega_pfits_get_filter_info(plist);
00292
00293 if(cpl_propertylist_has(filterlist,"CWL")){
00294 centralwl=cpl_propertylist_get_double(filterlist, "CWL");
00295 }
00296 else{
00297 centralwl=0.;
00298 }
00299 freeplist(plist);
00300 freeplist(filterlist);
00301
00302
00303
00304
00305
00306 omega_pfits_get_conad(xlist, &gain);
00307 cpl_msg_debug(cpl_func,"gain: %f",gain);
00308
00309
00310 sources = NULL;
00311
00312 for(i = 0; i < 15; i++) {
00313
00314
00315 if(reduced_weight != NULL){
00316 cmd = cpl_sprintf("%s/sex %s -WEIGHT_TYPE MAP_WEIGHT -WEIGHT_IMAGE %s "
00317 "-c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00318 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g -GAIN %g -MAG_ZEROPOINT %f "
00319 "-CATALOG_TYPE FITS_1.0 -CATALOG_NAME %s -CHECKIMAGE_NAME %s "
00320 "-CHECKIMAGE_TYPE OBJECTS -DEBLEND_NTHRESH 1 2>/dev/null",
00321 path, reduced, reduced_weight, sex_conf, sex_par, sex_conv, sex_nnw, astrom_thre, astrom_thre,
00322 gain, zp, srcs, "omega_ima_detected_astrom_srcs.fits");
00323 }
00324 else
00325 {
00326 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00327 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g -GAIN %g -MAG_ZEROPOINT %f "
00328 "-CATALOG_TYPE FITS_1.0 -CATALOG_NAME %s -CHECKIMAGE_NAME %s "
00329 "-CHECKIMAGE_TYPE OBJECTS -DEBLEND_NTHRESH 1 2>/dev/null",
00330 path, reduced, sex_conf, sex_par, sex_conv, sex_nnw, astrom_thre, astrom_thre,
00331 gain, zp, srcs, "omega_ima_detected_astrom_srcs.fits");
00332 }
00333
00334 cpl_msg_debug(cpl_func,"cmd: %s", cmd);
00335 if (system(cmd) != 0) {
00336 cpl_msg_error(cpl_func,"Failed to detect sources in image using Sextractor");
00337 freespace(cmd);
00338 freeplist(xlist);
00339 return NULL;
00340 }
00341 freespace(cmd);
00342
00343 data = cpl_table_load(srcs, 1, 0);
00344 if (data == NULL) {
00345 cpl_msg_error(cpl_func,"Sources table is NULL");
00346 freeplist(xlist);
00347 return NULL;
00348 }
00349
00350 freetable(sources);
00351 sources_flag0 = omega_filter_table_int(data, "FLAGS",
00352 CPL_NOT_GREATER_THAN, SextractorFlagLimit, &nsources_flag0);
00353 sources = omega_filter_table_float(sources_flag0, "CLASS_STAR",
00354 CPL_NOT_LESS_THAN, SextractorClassStar, &nsources);
00355 freetable(data);
00356 freetable(sources_flag0);
00357
00358 if (i==0){
00359 nsources_firstpass=nsources;
00360 }
00361
00362 if(nsources > 1000) {
00363
00364 astrom_thre = astrom_thre + 30;
00365 }
00366 else
00367 break;
00368 }
00369
00370
00371
00372 p = cpl_propertylist_new();
00373 cpl_propertylist_append_bool(p,"MAG_AUTO",0);
00374 if (cpl_table_sort(sources, p) != CPL_ERROR_NONE) {
00375 cpl_msg_error(cpl_func,"Cannot sort sources table");
00376 freeplist(p);
00377 freeplist(xlist);
00378 freetable(sources);
00379 return NULL;
00380 }
00381 freeplist(p);
00382
00383
00384
00385
00386
00387
00388 if(nsources_firstpass>500)
00389 {
00390
00391
00392 dummy_table=cpl_table_duplicate(sources);
00393 freetable(sources);
00394 cpl_table_and_selected_float(dummy_table,"X_IMAGE",CPL_NOT_LESS_THAN,1.+500.);
00395 cpl_table_and_selected_float(dummy_table,"Y_IMAGE",CPL_NOT_LESS_THAN,1.+500.);
00396 cpl_table_and_selected_float(dummy_table,"X_IMAGE",CPL_NOT_GREATER_THAN,(float)imsizex-500.);
00397 cpl_table_and_selected_float(dummy_table,"Y_IMAGE",CPL_NOT_GREATER_THAN,(float)imsizey-500.);
00398
00399 sources= cpl_table_extract_selected(dummy_table);
00400
00401 cpl_table_not_selected(dummy_table);
00402 dummy_table1=cpl_table_extract_selected(dummy_table);
00403
00404 cpl_table_insert(sources, dummy_table1, cpl_table_get_nrow(sources));
00405 freetable(dummy_table);
00406 freetable(dummy_table1);
00407 nsources=cpl_table_get_nrow(sources);
00408 }
00409
00410 cpl_msg_info(cpl_func,"Number of sources in image: %d", nsources);
00411 if(nsources < nstars){
00412 nstars = nsources -1;
00413 }
00414
00415
00416 if(pradius > maxpradius)
00417 pradius = maxpradius;
00418
00419
00420
00421 from = cpl_matrix_new(nsources, 2);
00422 for (i=0; i<nsources; i++){
00423 float xim = cpl_table_get_float(sources, "X_IMAGE", i, NULL);
00424 float yim = cpl_table_get_float(sources, "Y_IMAGE", i, NULL);
00425 cpl_matrix_set(from, i, 0, xim);
00426 cpl_matrix_set(from, i, 1, yim);
00427 }
00428
00429
00430 stars = cpl_matrix_transpose_create(from);
00431
00432
00433 if(omega_get_coverage(xlist, 0, from, &x1, &x2, &y1, &y2) != 0){
00434 cpl_msg_error(cpl_func,"Cannot get coverage of image");
00435 freematrix(from);
00436 freematrix(stars);
00437 freeplist(xlist);
00438 freetable(sources);
00439 return NULL;
00440 }
00441 freematrix(from);
00442
00443
00444
00445
00446 coords = cpl_vector_new(4);
00447 cpl_vector_set(coords, 0, x1);
00448 cpl_vector_set(coords, 1, x2);
00449 cpl_vector_set(coords, 2, y1);
00450 cpl_vector_set(coords, 3, y2);
00451
00452 stds = omega_get_usnoa_table(catalog, coords, &nstds);
00453 if (stds == NULL) {
00454 cpl_msg_warning(cpl_func,"Cannot find USNOA2 stars in this region: {RA-RA:DEC-DEC}={%g-%g:%g=%g}",
00455 x1,x2,y1,y2);
00456 freeplist(xlist);
00457 freematrix(stars);
00458 freetable(sources);
00459 freevector(coords);
00460 return NULL;
00461 }
00462 freevector(coords);
00463
00464
00465 cpl_msg_info(cpl_func,"Number of USNOA2 stars in this region: %d", nstds);
00466 cpl_msg_debug(cpl_func,"%d USNOA2 stars fall in region: {RA-RA:DEC-DEC}={%g--%g:%g--%g}", nstds,x1,x2,y1,y2);
00467
00468
00469 from = cpl_matrix_new(nstds, 2);
00470 for (i=0; i<nstds; i++){
00471 cpl_matrix_set(from, i, 0, cpl_table_get_double(stds, "RA2000", i, NULL));
00472 cpl_matrix_set(from, i, 1, cpl_table_get_double(stds, "Dec2000", i, NULL));
00473 }
00474
00475 to = NULL;
00476 status = NULL;
00477 wcs = cpl_wcs_new_from_propertylist(xlist);
00478 if(cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE){
00479 freewcs(wcs);
00480 freearray(status);
00481 freematrix(from);
00482 freematrix(stars);
00483 freematrix(to);
00484 freeplist(xlist);
00485 freetable(sources);
00486 freetable(stds);
00487 cpl_msg_error(cpl_func,"Error in cpl_wcs conversion. %s",cpl_error_get_message());
00488 return NULL;
00489 }
00490 freewcs(wcs);
00491 freematrix(from);
00492 freearray(status);
00493
00494
00495 cpl_table_new_column(stds,"xpredict",CPL_TYPE_FLOAT);
00496 cpl_table_set_column_unit(stds,"xpredict","pixels");
00497 cpl_table_new_column(stds,"ypredict",CPL_TYPE_FLOAT);
00498 cpl_table_set_column_unit(stds,"ypredict","pixels");
00499
00500
00501
00502 for(i=0; i< nstds; i++){
00503 float xp = (float)cpl_matrix_get(to, i, 0);
00504 float yp = (float)cpl_matrix_get(to, i, 1);
00505 cpl_table_set_float(stds,"xpredict", i, xp);
00506 cpl_table_set_float(stds,"ypredict", i, yp);
00507 }
00508 freematrix(to);
00509
00510
00511
00512
00513
00514
00515
00516
00517 p = cpl_propertylist_new();
00518
00519 if (centralwl>6000){
00520 cpl_propertylist_append_bool(p,"MagR",0);
00521 cpl_msg_info(cpl_func,"Sorting standard star catalogue by R-band mag");
00522 }
00523 else{
00524 cpl_propertylist_append_bool(p,"MagB",0);
00525 cpl_msg_info(cpl_func,"Sorting standard star catalogue by B-band mag");
00526 }
00527
00528 if (cpl_table_sort(stds, p) != CPL_ERROR_NONE) {
00529 cpl_msg_error(cpl_func,"Cannot sort stds table");
00530 freeplist(p);
00531 freetable(sources);
00532 freematrix(stars);
00533 freetable(stds);
00534 freeplist(xlist);
00535 return NULL;
00536 }
00537 freeplist(p);
00538
00539
00540
00541
00542
00543
00544 dummy_table=cpl_table_duplicate(stds);
00545 freetable(stds);
00546
00547 if(nsources_firstpass>500){
00548 cpl_table_and_selected_float(dummy_table,"xpredict",CPL_NOT_LESS_THAN,1.+600.);
00549 cpl_table_and_selected_float(dummy_table,"ypredict",CPL_NOT_LESS_THAN,1.+600.);
00550 cpl_table_and_selected_float(dummy_table,"xpredict",CPL_NOT_GREATER_THAN,(float)imsizex-600.);
00551 cpl_table_and_selected_float(dummy_table,"ypredict",CPL_NOT_GREATER_THAN,(float)imsizey-600.);
00552 }
00553 else {
00554 cpl_table_and_selected_float(dummy_table,"xpredict",CPL_NOT_LESS_THAN,1.+100.);
00555 cpl_table_and_selected_float(dummy_table,"ypredict",CPL_NOT_LESS_THAN,1.+100.);
00556 cpl_table_and_selected_float(dummy_table,"xpredict",CPL_NOT_GREATER_THAN,(float)imsizex-100.);
00557 cpl_table_and_selected_float(dummy_table,"ypredict",CPL_NOT_GREATER_THAN,(float)imsizey-100.);
00558 }
00559
00560 stds= cpl_table_extract_selected(dummy_table);
00561 cpl_table_not_selected(dummy_table);
00562 dummy_table1=cpl_table_extract_selected(dummy_table);
00563
00564 cpl_table_insert(stds, dummy_table1, cpl_table_get_nrow(stds));
00565 freetable(dummy_table);
00566 freetable(dummy_table1);
00567
00568 if (stds == NULL) {
00569 cpl_msg_warning(cpl_func,"Cannot find USNOA2 stars within the image "
00570 "borders");
00571 freewcs(wcs);
00572 freearray(status);
00573 freematrix(from);
00574 freematrix(stars);
00575 freematrix(to);
00576 freeplist(xlist);
00577 freetable(sources);
00578 return NULL;
00579 }
00580
00581
00582
00583
00584
00585
00586 nstds=cpl_table_get_nrow(stds);
00587 cpl_msg_info(cpl_func,"Number of USNOA2 stars falling on the image: %d", nstds);
00588
00589
00590
00591 pattern = cpl_matrix_new(2, nstds);
00592 for(i=0; i<nstds; i++){
00593 float x = cpl_table_get_float(stds, "xpredict", i, NULL);
00594 float y = cpl_table_get_float(stds, "ypredict", i, NULL);
00595 cpl_matrix_set(pattern, 0, i, x);
00596 cpl_matrix_set(pattern, 1, i, y);
00597 }
00598
00599
00600 if(nstds < npattern){
00601 npattern = nstds -1;
00602 }
00603
00604 cpl_msg_debug(cpl_func,"stars: % " CPL_SIZE_FORMAT " , nstars: %d pattern: % " CPL_SIZE_FORMAT " npattern: %d",
00605 cpl_matrix_get_ncol(stars), nstars, cpl_matrix_get_ncol(pattern),
00606 npattern);
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 matchlimit = nstds < nsources ? (double)nstds*0.25 : (double)nsources*0.25;
00657 if(matchlimit < 5) matchlimit=5;
00658 cpl_msg_debug(cpl_func,"Match limit: %f", matchlimit);
00659 for (i = 0; i < 4; i++) {
00660
00661 *nmatches = 0;
00662 matches = NULL;
00663 cpl_msg_debug(cpl_func,"nstars: %d - npattern: %d ", nstars, npattern);
00664 matches = cpl_ppm_match_points(stars, nstars, 0.5, pattern, npattern,
00665 15, tol, pradius, NULL, NULL, &scale, &angle);
00666 if(matches != NULL){
00667 *nmatches = cpl_array_get_size(matches) - cpl_array_count_invalid(matches);
00668 }
00669 if((matches == NULL) || (*nmatches < matchlimit )) {
00670 cpl_msg_debug(cpl_func,"nstars: %d npattern: %d i: %d",nstars,npattern,i );
00671
00672 if (i==1){
00673 npattern=20;
00674 nstars=35;
00675 }
00676
00677 if (i==2){
00678 npattern=25;
00679 nstars=50;
00680 }
00681
00682 if (i==3){
00683 freearray(matches);
00684 break;
00685 }
00686
00687 cpl_msg_debug(cpl_func,"Increasing number of stars in PPM; scale=%g angle=%g "
00688 "nstars=%d npattern=%d nmatches=%d",scale,angle,nstars,npattern,*nmatches);
00689
00690 freearray(matches);
00691 continue;
00692 }
00693 else
00694 break;
00695
00696 }
00697 *mratio = (double )(*nmatches / (double)nsources);
00698
00699 if((matches == NULL) || (*nmatches < matchlimit)){
00700 cpl_msg_error(cpl_func,"Error in PPM module: Not enough matches found");
00701 freematrix(stars);
00702 freematrix(pattern);
00703 freetable(sources);
00704 freetable(stds);
00705 freeplist(xlist);
00706 freearray(matches);
00707 return NULL;
00708 }
00709 freematrix(stars);
00710
00711 cpl_msg_info(cpl_func, "There are %d matches between USNOA2 and image", *nmatches);
00712
00713
00714 cpl_table_new_column(sources, "RA2000", CPL_TYPE_DOUBLE);
00715 cpl_table_new_column(sources, "Dec2000", CPL_TYPE_DOUBLE);
00716 cpl_table_unselect_all(stds);
00717 cpl_table_unselect_all(sources);
00718 for(i=0; i<cpl_matrix_get_ncol(pattern); i++){
00719 int null;
00720 int index = cpl_array_get_int(matches,i, &null);
00721 if(!null){
00722 double ra = cpl_table_get_double(stds, "RA2000", i, NULL);
00723 double dec = cpl_table_get_double(stds, "Dec2000", i, NULL);
00724 cpl_table_set(sources, "RA2000", index, ra);
00725 cpl_table_set(sources, "Dec2000", index, dec);
00726 cpl_table_select_row(sources, index);
00727 }
00728 }
00729
00730 freearray(matches);
00731 freematrix(pattern);
00732
00733
00734 extracted = cpl_table_extract_selected(sources);
00735 rdmat = cpl_matrix_new(cpl_table_get_nrow(extracted), 2);
00736 xymat = cpl_matrix_new(cpl_table_get_nrow(extracted), 2);
00737 for (i=0; i<cpl_table_get_nrow(extracted); i++){
00738 double ra = cpl_table_get_double(extracted, "RA2000", i, NULL);
00739 double dec = cpl_table_get_double(extracted, "Dec2000", i, NULL);
00740 cpl_matrix_set(rdmat, i, 0, ra);
00741 cpl_matrix_set(rdmat, i, 1, dec);
00742 float x = cpl_table_get_float(extracted, "X_IMAGE", i, NULL);
00743 float y = cpl_table_get_float(extracted, "Y_IMAGE", i, NULL);
00744 cpl_matrix_set(xymat, i, 0, x);
00745 cpl_matrix_set(xymat, i, 1, y);
00746 }
00747 freetable(extracted);
00748 freetable(stds);
00749 freetable(sources);
00750
00751 if(cpl_wcs_platesol(xlist, rdmat, xymat, niter, 3.0, fitmode,
00752 CPL_WCS_MV_CRPIX, &aplist) != CPL_ERROR_NONE) {
00753 cpl_msg_error(cpl_func,"Cannot calculate plate solution. %s", cpl_error_get_message());
00754 freematrix(xymat);
00755 freematrix(rdmat);
00756 freeplist(xlist);
00757 return NULL;
00758 }
00759 freematrix(xymat);
00760 freematrix(rdmat);
00761 freeplist(xlist);
00762
00763
00764
00765 return aplist;
00766
00767 }
00768
00769
00770
00771
00772
00773
00774
00775
00776 void omega_wfi_correct_wcs(cpl_propertylist *mplist, cpl_propertylist *xplist)
00777 {
00778 int i = 0;
00779 double xoff = 0.;
00780 double yoff = 0.;
00781 double tpa = 0.;
00782 double tpd = 0;
00783 double pixsca = 0.238;
00784 double rotskypa = 0.0;
00785 double angle = 0.0;
00786 double xapoff = 0.0;
00787 double yapoff = 0.0;
00788 double secd = 0.0;
00789 double aa = 0.0;
00790 double aap = 0.0;
00791 double bb = 0.0;
00792 double bbp = 0.0;
00793 double dd = 0.0;
00794 double ddp = 0.0;
00795 double eep = 0.0;
00796 double ee = 0.0;
00797 double cc = 0.0;
00798 double ff = 0.0;
00799 char key[80]= "";
00800 char ccdstr[80] = "";
00801 const char *ccd = NULL;
00802
00803
00804 if(cpl_propertylist_has(xplist, "ESO DET CHIP ID") == 1){
00805 ccd = cpl_propertylist_get_string(xplist, "ESO DET CHIP ID");
00806 }
00807 else {
00808 for(i=1; i<=8; i++){
00809 sprintf(key,"ESO DET CHIP%d ID", i);
00810 if(cpl_propertylist_has(xplist,key)==1){
00811 break;
00812 }
00813 }
00814 sprintf(ccdstr,"ESO DET CHIP%d ID", i);
00815 ccd = cpl_propertylist_get_string(xplist, ccdstr);
00816 }
00817
00818
00819 if(strcmp(ccd, "ccd50")==0){
00820 xoff = 4127.;
00821 yoff = -240.;
00822 }
00823 else if(strcmp(ccd, "ccd51")==0){
00824 xoff = 1990.;
00825 yoff = -233.;
00826 }
00827 else if(strcmp(ccd, "ccd52")==0){
00828 xoff = -154.;
00829 yoff = -233.;
00830 }
00831 else if(strcmp(ccd, "ccd53")==0){
00832 xoff = -2295.;
00833 yoff = -233.;
00834 }
00835 else if(strcmp(ccd, "ccd54")==0){
00836 xoff = -2294.;
00837 yoff = 3917.;
00838 }
00839 else if(strcmp(ccd, "ccd55")==0){
00840 xoff = -151.;
00841 yoff = 3919.;
00842 }
00843 else if(strcmp(ccd, "ccd56")==0){
00844 xoff = 1988.;
00845 yoff = 3920.;
00846 }
00847 else if(strcmp(ccd, "ccd57")==0){
00848 xoff = 4131.;
00849 yoff = 3912.;
00850 }
00851
00852
00853 tpa = cpl_propertylist_get_double(mplist, "RA");
00854 tpd = cpl_propertylist_get_double(mplist, "DEC");
00855
00856 rotskypa = 180.0;
00857 angle = (rotskypa-180.0)*PI/180.0;
00858 xapoff = 0.0;
00859 yapoff = 0.0;
00860
00861
00862 secd = 1.0/cos(tpd*PI/180.0);
00863 tpa = tpa - yapoff * secd * cos(angle) + xapoff *secd * sin(angle);
00864 tpd = tpd + yapoff * sin(angle) + xapoff * cos(angle);
00865
00866
00867
00868 aap = -pixsca/3600.0;
00869 bbp = 0.0;
00870 ddp = 0.0;
00871 eep = pixsca/3600.0;
00872
00873
00874
00875
00876
00877
00878 aa = aap * cos(angle) + bbp * sin(angle);
00879 bb =-aap * sin(angle) + bbp * cos(angle);
00880 dd = ddp * cos(angle) + eep * sin(angle);
00881 ee =-ddp * sin(angle) + eep * cos(angle);
00882
00883 cc = xoff;
00884 ff = yoff;
00885
00886
00887
00888
00889 cpl_propertylist_update_double(xplist,"CRPIX1",cc);
00890 cpl_propertylist_update_double(xplist,"CRPIX2",ff);
00891 cpl_propertylist_update_double(xplist,"CRVAL1",tpa);
00892 cpl_propertylist_update_double(xplist,"CRVAL2",tpd);
00893 cpl_propertylist_erase(xplist,"CDELT1");
00894 cpl_propertylist_erase(xplist,"CDELT2");
00895 cpl_propertylist_update_double(xplist,"CD1_1",aa);
00896 cpl_propertylist_update_double(xplist,"CD1_2",bb);
00897 cpl_propertylist_update_double(xplist,"CD2_1",dd);
00898 cpl_propertylist_update_double(xplist,"CD2_2",ee);
00899
00900
00901 cpl_propertylist_update_string(xplist,"CTYPE1", "RA---TAN");
00902 cpl_propertylist_update_string(xplist,"CTYPE2","DEC--TAN");
00903
00904 }
00905