HAWKI Pipeline Reference Manual 1.8.6
|
00001 /* $Id: hawki_step_photom_2mass.c,v 1.16 2011/10/24 10:42:23 cgarcia Exp $ 00002 * 00003 * This file is part of the HAWKI Pipeline 00004 * Copyright (C) 2002,2003 European Southern Observatory 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 /* 00022 * $Author: cgarcia $ 00023 * $Date: 2011/10/24 10:42:23 $ 00024 * $Revision: 1.16 $ 00025 * $Name: hawki-1_8_6 $ 00026 */ 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 00032 /*----------------------------------------------------------------------------- 00033 Includes 00034 -----------------------------------------------------------------------------*/ 00035 00036 #include <math.h> 00037 #include <cpl.h> 00038 #include <string.h> 00039 00040 #include "irplib_utils.h" 00041 00042 #include "hawki_alloc.h" 00043 #include "hawki_utils.h" 00044 #include "hawki_load.h" 00045 #include "hawki_save.h" 00046 #include "hawki_pfits.h" 00047 #include "hawki_dfs.h" 00048 #include "irplib_cat.h" 00049 #include "irplib_stdstar.h" 00050 00051 /*----------------------------------------------------------------------------- 00052 Functions prototypes 00053 -----------------------------------------------------------------------------*/ 00054 00055 static int hawki_step_photom_2mass_create(cpl_plugin *) ; 00056 static int hawki_step_photom_2mass_exec(cpl_plugin *) ; 00057 static int hawki_step_photom_2mass_destroy(cpl_plugin *) ; 00058 static int hawki_step_photom_2mass(cpl_parameterlist * parlist, 00059 cpl_frameset * frameset); 00060 00061 00062 cpl_table ** hawki_step_photom_2mass_get_zpoints 00063 (cpl_frameset * cat_2mass, 00064 cpl_frameset * obj_param, 00065 cpl_frameset * obj_ima); 00066 00067 static cpl_table * hawki_step_photom_2mass_retrieve_stars 00068 (cpl_frameset * cat_2mass, 00069 cpl_propertylist * wcs_keywords); 00070 00071 static cpl_array * hawki_step_photom_2mass_ppm 00072 (cpl_table * stars_2mass, 00073 cpl_table * obj_det); 00074 00075 static cpl_table * hawki_step_photom_2mass_fill_zpoint_table 00076 (cpl_table * stars_2mass, 00077 cpl_table * obj_det_param, 00078 cpl_array * matches, 00079 cpl_propertylist * plist, 00080 int idet); 00081 00082 static cpl_propertylist ** hawki_step_photom_2mass_qc 00083 (cpl_table ** zpoint_table); 00084 00085 static int hawki_step_photom_2mass_save 00086 (cpl_table ** zpoints, 00087 cpl_parameterlist * parlist, 00088 cpl_propertylist ** qclists, 00089 cpl_frameset * set); 00090 00091 /*----------------------------------------------------------------------------- 00092 Static variables 00093 -----------------------------------------------------------------------------*/ 00094 00095 static char hawki_step_photom_2mass_description[] = 00096 "hawki_step_photom_2mass -- HAWK-I photometric autocalibration using 2MASS.\n" 00097 "The input files must be tagged:\n" 00098 "obj_param.fits "HAWKI_CALPRO_OBJ_PARAM"\n" 00099 "image.fits "HAWKI_CALPRO_COMBINED"\n" 00100 "2mass_master_index.fits "HAWKI_UTIL_CAT_2MASS"\n" 00101 "The recipe creates as an output:\n" 00102 "hawki_cal_photom_2mass.fits ("HAWKI_CALPRO_ZPOINT_TAB"): \n" 00103 "The recipe does the following steps:\n" 00104 "-Search the 2MASS catalogue for stars in the FOV\n" 00105 "-Matches the input detected object catalogue and the 2MASS stars\n" 00106 "-Computes photometric characteristics for each matched star\n" 00107 "Return code:\n" 00108 "esorex exits with an error code of 0 if the recipe completes successfully\n" 00109 "or 1 otherwise"; 00110 00111 /*----------------------------------------------------------------------------- 00112 Functions code 00113 -----------------------------------------------------------------------------*/ 00114 00115 /*----------------------------------------------------------------------------*/ 00123 /*----------------------------------------------------------------------------*/ 00124 int cpl_plugin_get_info(cpl_pluginlist * list) 00125 { 00126 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ; 00127 cpl_plugin * plugin = &recipe->interface ; 00128 00129 cpl_plugin_init(plugin, 00130 CPL_PLUGIN_API, 00131 HAWKI_BINARY_VERSION, 00132 CPL_PLUGIN_TYPE_RECIPE, 00133 "hawki_step_photom_2mass", 00134 "2MASS photometric calibration", 00135 hawki_step_photom_2mass_description, 00136 "Cesar Enrique Garcia Dabo", 00137 PACKAGE_BUGREPORT, 00138 hawki_get_license(), 00139 hawki_step_photom_2mass_create, 00140 hawki_step_photom_2mass_exec, 00141 hawki_step_photom_2mass_destroy); 00142 00143 cpl_pluginlist_append(list, plugin) ; 00144 00145 return 0; 00146 } 00147 00148 /*----------------------------------------------------------------------------*/ 00157 /*----------------------------------------------------------------------------*/ 00158 static int hawki_step_photom_2mass_create(cpl_plugin * plugin) 00159 { 00160 cpl_recipe * recipe ; 00161 00162 /* Get the recipe out of the plugin */ 00163 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00164 recipe = (cpl_recipe *)plugin ; 00165 else return -1 ; 00166 00167 /* Create the parameters list in the cpl_recipe object */ 00168 recipe->parameters = cpl_parameterlist_new() ; 00169 if (recipe->parameters == NULL) 00170 return 1; 00171 00172 /* Return */ 00173 return 0; 00174 } 00175 00176 /*----------------------------------------------------------------------------*/ 00182 /*----------------------------------------------------------------------------*/ 00183 static int hawki_step_photom_2mass_exec(cpl_plugin * plugin) 00184 { 00185 cpl_recipe * recipe ; 00186 00187 /* Get the recipe out of the plugin */ 00188 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00189 recipe = (cpl_recipe *)plugin ; 00190 else return -1 ; 00191 00192 /* Issue a banner */ 00193 hawki_print_banner(); 00194 00195 return hawki_step_photom_2mass(recipe->parameters, recipe->frames) ; 00196 } 00197 00198 /*----------------------------------------------------------------------------*/ 00204 /*----------------------------------------------------------------------------*/ 00205 static int hawki_step_photom_2mass_destroy(cpl_plugin * plugin) 00206 { 00207 cpl_recipe * recipe ; 00208 00209 /* Get the recipe out of the plugin */ 00210 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00211 recipe = (cpl_recipe *)plugin ; 00212 else return -1 ; 00213 00214 cpl_parameterlist_delete(recipe->parameters) ; 00215 return 0 ; 00216 } 00217 00218 /*----------------------------------------------------------------------------*/ 00224 /*----------------------------------------------------------------------------*/ 00225 static int hawki_step_photom_2mass(cpl_parameterlist * parlist, 00226 cpl_frameset * frameset) 00227 { 00228 cpl_frameset * cat_2mass; 00229 cpl_frameset * obj_param; 00230 cpl_frameset * obj_ima; 00231 cpl_table ** zpoint_table; 00232 cpl_propertylist ** qclists; 00233 int idet; 00234 00235 /* Identify the RAW and CALIB frames in the input frameset */ 00236 if (hawki_dfs_set_groups(frameset)) { 00237 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ; 00238 return -1 ; 00239 } 00240 00241 /* Retrieve 2MASS catalogue */ 00242 if ((cat_2mass = hawki_extract_frameset(frameset, 00243 HAWKI_UTIL_CAT_2MASS)) == NULL) 00244 { 00245 cpl_msg_error(__func__, "Cannot find 2MASS catalogue (%s)", 00246 HAWKI_UTIL_CAT_2MASS); 00247 return -1 ; 00248 } 00249 00250 /* Retrieve obj param */ 00251 if ((obj_param = hawki_extract_frameset 00252 (frameset, HAWKI_CALPRO_OBJ_PARAM)) == NULL) 00253 { 00254 cpl_msg_error(__func__, "Cannot find object parameters (%s)", 00255 HAWKI_CALPRO_OBJ_PARAM); 00256 return -1 ; 00257 } 00258 00259 /* Retrieve reference image */ 00260 if ((obj_ima = hawki_extract_frameset 00261 (frameset, HAWKI_CALPRO_COMBINED)) == NULL) 00262 { 00263 cpl_msg_error(__func__, "Cannot find combined image (%s) ", 00264 HAWKI_CALPRO_COMBINED); 00265 return -1 ; 00266 } 00267 00268 /* Compute the zpoints */ 00269 zpoint_table = hawki_step_photom_2mass_get_zpoints 00270 (cat_2mass, obj_param, obj_ima); 00271 if(zpoint_table == NULL) 00272 { 00273 cpl_msg_error(__func__,"Could not get the zpoints"); 00274 cpl_frameset_delete(cat_2mass); 00275 cpl_frameset_delete(obj_param); 00276 cpl_frameset_delete(obj_ima); 00277 return -1; 00278 } 00279 00280 /* Get some QC */ 00281 qclists = hawki_step_photom_2mass_qc(zpoint_table); 00282 if(zpoint_table == NULL) 00283 { 00284 cpl_msg_error(__func__,"Could not compute quality controls"); 00285 cpl_frameset_delete(cat_2mass); 00286 cpl_frameset_delete(obj_param); 00287 cpl_frameset_delete(obj_ima); 00288 hawki_table_delete(zpoint_table); 00289 return -1; 00290 } 00291 00292 /* Save the products */ 00293 cpl_msg_info(__func__,"Saving products"); 00294 if(hawki_step_photom_2mass_save(zpoint_table, parlist, qclists, frameset) == -1) 00295 { 00296 cpl_msg_error(__func__,"Could not save products"); 00297 cpl_frameset_delete(cat_2mass); 00298 cpl_frameset_delete(obj_param); 00299 cpl_frameset_delete(obj_ima); 00300 hawki_table_delete(zpoint_table); 00301 return -1; 00302 } 00303 00304 /* Free and return */ 00305 cpl_frameset_delete(cat_2mass); 00306 cpl_frameset_delete(obj_param); 00307 cpl_frameset_delete(obj_ima); 00308 hawki_table_delete(zpoint_table); 00309 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 00310 cpl_propertylist_delete(qclists[idet]); 00311 cpl_free(qclists); 00312 00313 /* Return */ 00314 if (cpl_error_get_code()) 00315 { 00316 cpl_msg_error(__func__, 00317 "HAWK-I pipeline could not recover from previous errors"); 00318 return -1 ; 00319 } 00320 else return 0 ; 00321 } 00322 00323 cpl_table ** hawki_step_photom_2mass_get_zpoints 00324 (cpl_frameset * cat_2mass, 00325 cpl_frameset * obj_param, 00326 cpl_frameset * obj_ima) 00327 { 00328 cpl_table ** zpoint_table; 00329 cpl_table ** obj_det_param; 00330 cpl_propertylist * plist; 00331 int idet; 00332 cpl_errorstate error_prevstate = cpl_errorstate_get(); 00333 00334 /* Allocate zpoint_table */ 00335 zpoint_table = cpl_malloc(sizeof(cpl_table *) * HAWKI_NB_DETECTORS); 00336 00337 /* Load detected obj */ 00338 obj_det_param = hawki_load_tables(cpl_frameset_get_first(obj_param)); 00339 00340 /* Read the main header */ 00341 plist = cpl_propertylist_load 00342 (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)), 0); 00343 /* Loop on detectors */ 00344 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet) 00345 { 00346 cpl_propertylist * wcs_info; 00347 cpl_table * stars_2mass; 00348 cpl_array * matches; 00349 int ext_nb; 00350 00351 cpl_msg_info(__func__,"Working on chip %d", idet + 1); 00352 cpl_msg_indent_more(); 00353 00354 /* Retrieve stars */ 00355 ext_nb=hawki_get_ext_from_detector 00356 (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)),idet+1); 00357 wcs_info = cpl_propertylist_load 00358 (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)), ext_nb); 00359 stars_2mass = 00360 hawki_step_photom_2mass_retrieve_stars(cat_2mass, wcs_info); 00361 if(stars_2mass == NULL) 00362 { 00363 int jdet; 00364 cpl_msg_error(__func__, "Cannot retrieve stars"); 00365 cpl_propertylist_delete(plist); 00366 cpl_propertylist_delete(wcs_info); 00367 hawki_table_delete(obj_det_param); 00368 for(jdet = 0; jdet <idet; ++jdet) 00369 cpl_table_delete(zpoint_table[jdet]); 00370 cpl_free(zpoint_table); 00371 return NULL; 00372 } 00373 00374 /* Pattern matching btw stars in 2MASS and detected ones */ 00375 cpl_msg_info(__func__,"Matching %d 2MASS stars and %d detections", 00376 cpl_table_get_nrow(stars_2mass), 00377 cpl_table_get_nrow(obj_det_param[idet])); 00378 matches = hawki_step_photom_2mass_ppm(stars_2mass, obj_det_param[idet]); 00379 00380 /* Fill the zpoint table */ 00381 cpl_msg_info(__func__,"Computing zero points"); 00382 zpoint_table[idet] = hawki_step_photom_2mass_fill_zpoint_table 00383 (stars_2mass, obj_det_param[idet], matches, plist, idet); 00384 if(zpoint_table[idet] == NULL) 00385 { 00386 int jdet; 00387 cpl_msg_error(__func__, "Could not compute the zero points"); 00388 cpl_propertylist_delete(plist); 00389 cpl_propertylist_delete(wcs_info); 00390 hawki_table_delete(obj_det_param); 00391 cpl_table_delete(stars_2mass); 00392 for(jdet = 0; jdet <idet; ++jdet) 00393 cpl_table_delete(zpoint_table[jdet]); 00394 cpl_free(zpoint_table); 00395 cpl_array_delete(matches); 00396 return NULL; 00397 } 00398 00399 /* Cleaning resources */ 00400 cpl_msg_indent_less(); 00401 cpl_propertylist_delete(wcs_info); 00402 cpl_table_delete(stars_2mass); 00403 cpl_array_delete(matches); 00404 } 00405 00406 /* Free and return */ 00407 hawki_table_delete(obj_det_param); 00408 cpl_propertylist_delete(plist); 00409 if(!cpl_errorstate_is_equal(error_prevstate)) 00410 { 00411 int jdet; 00412 for(jdet = 0; jdet <HAWKI_NB_DETECTORS; ++jdet) 00413 cpl_table_delete(zpoint_table[jdet]); 00414 cpl_free(zpoint_table); 00415 cpl_msg_error(__func__, "A problem happened computing the zero point"); 00416 cpl_errorstate_set(CPL_ERROR_NONE); 00417 return NULL; 00418 } 00419 return zpoint_table; 00420 } 00421 00422 static cpl_table * hawki_step_photom_2mass_retrieve_stars 00423 (cpl_frameset * cat_2mass, 00424 cpl_propertylist * wcs_keywords) 00425 { 00426 char * catpath; 00427 char * catname; 00428 cpl_table * stars; 00429 cpl_wcs * wcs; 00430 int nstars; 00431 int istar; 00432 double ra1; 00433 double ra2; 00434 double dec1; 00435 double dec2; 00436 double extra_search = 0.; 00437 cpl_matrix * from_coord; 00438 cpl_matrix * to_coord; 00439 cpl_array * status; 00440 00441 00442 /* Extract the catalog path */ 00443 if (irplib_2mass_get_catpars(cpl_frameset_get_first(cat_2mass), 00444 &catpath, &catname) != CPL_ERROR_NONE) 00445 return NULL; 00446 00447 /* Get the WCS info */ 00448 wcs = cpl_wcs_new_from_propertylist(wcs_keywords); 00449 if(cpl_error_get_code() == CPL_ERROR_NO_WCS) 00450 { 00451 cpl_msg_error(__func__,"Not compiled with WCS support."); 00452 return NULL; 00453 } 00454 00455 /* Get the limits to search */ 00456 if(irplib_cat_get_image_limits(wcs, extra_search, 00457 &ra1, &ra2, &dec1, &dec2) == 00458 CPL_ERROR_DATA_NOT_FOUND) 00459 { 00460 cpl_msg_error(__func__,"No WCS information found"); 00461 cpl_free(catname); 00462 cpl_free(catpath); 00463 cpl_wcs_delete(wcs); 00464 return NULL; 00465 } 00466 cpl_msg_info(__func__,"Searching stars in RA=[%f,%f] DEC=[%f,%f]", 00467 ra1/15., ra2/15., dec1, dec2); 00468 00469 00470 /* Search the stars */ 00471 stars = irplib_2mass_extract(catpath, ra1, ra2, dec1, dec2); 00472 if(stars == NULL) 00473 { 00474 cpl_msg_error(__func__,"Error retrieving 2mass stars: %s ", 00475 cpl_error_get_message()); 00476 cpl_free(catname); 00477 cpl_free(catpath); 00478 cpl_wcs_delete(wcs); 00479 return NULL; 00480 } 00481 nstars = cpl_table_get_nrow(stars); 00482 cpl_msg_indent_more(); 00483 cpl_msg_info(__func__, "%d 2MASS stars found", nstars); 00484 00485 /* Convert Ra, Dec to X,Y using the WCS information from image */ 00486 from_coord = cpl_matrix_new(nstars, 2); 00487 for (istar=0; istar<nstars; istar++) 00488 { 00489 cpl_matrix_set(from_coord, istar, 0, cpl_table_get_float 00490 (stars, HAWKI_COL_2MASS_RA, istar, NULL)); 00491 cpl_matrix_set(from_coord, istar, 1, cpl_table_get_float 00492 (stars, HAWKI_COL_2MASS_DEC, istar, NULL)); 00493 } 00494 00495 to_coord = NULL; 00496 status = NULL; 00497 if(cpl_wcs_convert(wcs, from_coord, &to_coord, 00498 &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE) 00499 { 00500 cpl_array_delete(status); 00501 cpl_matrix_delete(from_coord); 00502 cpl_matrix_delete(to_coord); 00503 cpl_free(catname); 00504 cpl_free(catpath); 00505 cpl_wcs_delete(wcs); 00506 cpl_msg_error(cpl_func,"Error in cpl_wcs conversion. %s", 00507 cpl_error_get_message()); 00508 return NULL; 00509 } 00510 00511 /* Add the predicted x,y coordinate columns to the 2MASS table */ 00512 cpl_table_new_column(stars, HAWKI_COL_2MASS_XPREDICT, CPL_TYPE_FLOAT); 00513 cpl_table_set_column_unit(stars,HAWKI_COL_2MASS_XPREDICT, "pixels"); 00514 cpl_table_new_column(stars, HAWKI_COL_2MASS_YPREDICT, CPL_TYPE_FLOAT); 00515 cpl_table_set_column_unit(stars, HAWKI_COL_2MASS_YPREDICT, "pixels"); 00516 for(istar=0; istar< nstars; istar++) 00517 { 00518 float xpredict = (float)cpl_matrix_get(to_coord, istar, 0); 00519 float ypredict = (float)cpl_matrix_get(to_coord, istar, 1); 00520 cpl_table_set_float(stars,"xpredict", istar, xpredict); 00521 cpl_table_set_float(stars,"ypredict", istar, ypredict); 00522 } 00523 00524 /* Free and return */ 00525 cpl_array_delete(status); 00526 cpl_matrix_delete(from_coord); 00527 cpl_matrix_delete(to_coord); 00528 cpl_wcs_delete(wcs); 00529 cpl_free(catname); 00530 cpl_free(catpath); 00531 cpl_msg_indent_less(); 00532 return stars; 00533 } 00534 00535 /*----------------------------------------------------------------------------*/ 00541 /*----------------------------------------------------------------------------*/ 00542 static cpl_array * hawki_step_photom_2mass_ppm 00543 (cpl_table * stars_2mass, 00544 cpl_table * obj_det) 00545 { 00546 int istar; 00547 int iobj; 00548 int iter; 00549 int nstars_2mass; 00550 int nstars_2mass_used_match; 00551 int nobj; 00552 int nobj_used_match; 00553 int nmatches; 00554 int nmax_match_pattern = 30; 00555 cpl_matrix * pattern; 00556 cpl_array * matches; 00557 double tol = 0.1; 00558 double pradius = 30.0; 00559 double mean_data_pos_err = 5.; 00560 int ppm_max_iter = 5; 00561 double scale = 1; 00562 double angle; 00563 cpl_matrix * obj_pos; 00564 cpl_propertylist * sort_prop; 00565 00566 00567 /* Sort the detected objects */ 00568 cpl_msg_indent_more(); 00569 sort_prop = cpl_propertylist_new(); 00570 cpl_propertylist_append_bool(sort_prop, HAWKI_COL_OBJ_FLUX, 1); 00571 if (cpl_table_sort(obj_det, sort_prop) != CPL_ERROR_NONE) 00572 { 00573 cpl_msg_error(cpl_func,"Cannot sort detected sources table"); 00574 cpl_propertylist_delete(sort_prop); 00575 return NULL; 00576 } 00577 00578 /* Create matrix of X,Y coordinates of detected objects*/ 00579 nobj = cpl_table_get_nrow(obj_det); 00580 obj_pos = cpl_matrix_new(2, nobj); 00581 for (iobj=0; iobj<nobj; iobj++) 00582 { 00583 float xim = cpl_table_get_double 00584 (obj_det, HAWKI_COL_OBJ_POSX, iobj, NULL); 00585 float yim = cpl_table_get_double 00586 (obj_det, HAWKI_COL_OBJ_POSY, iobj, NULL); 00587 cpl_matrix_set(obj_pos, 0, iobj, xim); 00588 cpl_matrix_set(obj_pos, 1, iobj, yim); 00589 } 00590 00591 /* Sort the 2MASS stars by magnitude */ 00592 cpl_propertylist_empty(sort_prop); 00593 cpl_propertylist_append_bool(sort_prop, HAWKI_COL_2MASS_K_MAG, 0); 00594 if (cpl_table_sort(stars_2mass, sort_prop) != CPL_ERROR_NONE) 00595 { 00596 cpl_msg_error(cpl_func,"Cannot sort 2MASS stars table"); 00597 cpl_propertylist_delete(sort_prop); 00598 return NULL; 00599 } 00600 00601 /* Prepare the 2MASS matrix for the pattern matching */ 00602 nstars_2mass = cpl_table_get_nrow(stars_2mass); 00603 pattern = cpl_matrix_new(2, nstars_2mass); 00604 for(istar=0; istar<nstars_2mass ; istar++) 00605 { 00606 float x = cpl_table_get_float 00607 (stars_2mass, HAWKI_COL_2MASS_XPREDICT, istar, NULL); 00608 float y = cpl_table_get_float 00609 (stars_2mass, HAWKI_COL_2MASS_YPREDICT, istar, NULL); 00610 cpl_matrix_set(pattern, 0, istar, x); 00611 cpl_matrix_set(pattern, 1, istar, y); 00612 } 00613 00614 /* Do the ppm */ 00615 nstars_2mass_used_match = nmax_match_pattern; 00616 if(nstars_2mass < nmax_match_pattern) 00617 nstars_2mass_used_match = nstars_2mass; 00618 nobj_used_match = (int)(1.7 * nstars_2mass_used_match); 00619 if(nobj_used_match > nobj) 00620 nobj_used_match = nobj; 00621 if(nobj_used_match < nstars_2mass_used_match) 00622 nobj_used_match = nstars_2mass_used_match; 00623 cpl_msg_info(__func__,"The first step match will use %d stars " 00624 "and %d objects", nstars_2mass_used_match,nobj_used_match); 00625 for (iter = 0; iter < ppm_max_iter; iter++) 00626 { 00627 int nmatchsize; 00628 nmatches = 0; 00629 matches = cpl_ppm_match_points(obj_pos, nobj_used_match, 00630 mean_data_pos_err, 00631 pattern, nstars_2mass_used_match, 00632 1, tol, pradius, 00633 NULL, NULL, &scale, &angle); 00634 if(matches != NULL) 00635 { 00636 nmatchsize = cpl_array_get_size(matches); 00637 nmatches = nmatchsize - 00638 cpl_array_count_invalid(matches); 00639 } 00640 else 00641 { 00642 nmatchsize = 0; 00643 nmatches = 0; 00644 } 00645 00646 cpl_msg_info(cpl_func,"Total matches: %d. Valid matches: %d", 00647 nmatchsize, nmatches); 00648 cpl_msg_info(cpl_func,"Scale=%g angle=%g", scale, angle); 00649 if((matches == NULL) || (nmatches < floor(nobj_used_match*0.1))) 00650 { 00651 nobj_used_match = nobj_used_match + 10; 00652 cpl_msg_info(cpl_func, 00653 "Increasing number of detections used in PPM to %d", 00654 nobj_used_match); 00655 continue; 00656 } 00657 else 00658 break; 00659 } 00660 00661 /* Output debug messages */ 00662 cpl_msg_indent_more(); 00663 cpl_msg_debug(__func__,"Matched stars:"); 00664 cpl_msg_indent_more(); 00665 cpl_msg_debug(__func__,"X_OBJ Y_OBJ X_STAR Y_STAR X_DIFF Y_DIFF:"); 00666 for(istar=0; istar < nstars_2mass; ++istar) 00667 { 00668 int null; 00669 double x_obj, y_obj, x_star, y_star, x_diff, y_diff; 00670 iobj = cpl_array_get_int(matches, istar, &null); 00671 00672 if(null != 0) 00673 continue; 00674 x_obj = cpl_matrix_get(obj_pos, 0, iobj); 00675 y_obj = cpl_matrix_get(obj_pos, 1, iobj); 00676 x_star = cpl_matrix_get(pattern, 0, istar); 00677 y_star = cpl_matrix_get(pattern, 1, istar); 00678 x_diff = x_obj - x_star; 00679 y_diff = y_obj - y_star; 00680 cpl_msg_debug(__func__,"%6.1f %6.1f %6.1f %6.1f %6.1f %6.1f\n", 00681 x_obj, y_obj, x_star, y_star, x_diff, y_diff); 00682 } 00683 cpl_msg_indent_less(); 00684 cpl_msg_indent_less(); 00685 00686 cpl_matrix_delete(pattern); 00687 cpl_msg_info(cpl_func, "%d points matched", nmatches); 00688 00689 if(matches == NULL || nmatches == 0) 00690 { 00691 if(nmatches == 0) 00692 cpl_array_delete(matches); 00693 cpl_msg_error(cpl_func,"Error in PPM. %s",cpl_error_get_message()); 00694 cpl_matrix_delete(obj_pos); 00695 cpl_propertylist_delete(sort_prop); 00696 cpl_msg_indent_less(); 00697 return NULL; 00698 } 00699 00700 if(nmatches < floor(nobj_used_match*0.1)) 00701 { 00702 cpl_msg_warning(cpl_func,"PPM detected matched only %d objects." 00703 " Results could be unreliable",nmatches); 00704 } 00705 00706 /* Free and return */ 00707 cpl_matrix_delete(obj_pos); 00708 cpl_propertylist_delete(sort_prop); 00709 cpl_msg_indent_less(); 00710 return matches; 00711 } 00712 00713 /*----------------------------------------------------------------------------*/ 00718 /*----------------------------------------------------------------------------*/ 00719 static cpl_table * hawki_step_photom_2mass_fill_zpoint_table 00720 (cpl_table * stars_2mass, 00721 cpl_table * obj_det_param, 00722 cpl_array * matches, 00723 cpl_propertylist * plist, 00724 int idet) 00725 { 00726 cpl_table * zpoints; 00727 int nmatches; 00728 int nstars; 00729 int imatch; 00730 int istar; 00731 const char * filter; 00732 char magcol_2mass[100]; 00733 char magerrcol_2mass[100]; 00734 double dit; 00735 double airmass; 00736 double pixscale; 00737 double extinction; 00738 cpl_errorstate error_prevstate = cpl_errorstate_get(); 00739 00740 /* Read parameters from the propertylist */ 00741 airmass = (hawki_pfits_get_airmass_start(plist) + 00742 hawki_pfits_get_airmass_end(plist)) / 2.; 00743 filter = hawki_pfits_get_filter(plist); 00744 dit = hawki_pfits_get_dit(plist); 00745 pixscale = hawki_pfits_get_pixscale(plist); 00746 switch (hawki_get_band(filter)) 00747 { 00748 case HAWKI_BAND_J: 00749 extinction = 0.098; 00750 strcpy(magcol_2mass, HAWKI_COL_2MASS_J_MAG); 00751 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_J_MAGSIG); 00752 break ; 00753 case HAWKI_BAND_H: 00754 extinction = 0.039; 00755 strcpy(magcol_2mass, HAWKI_COL_2MASS_H_MAG); 00756 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_H_MAGSIG); 00757 break ; 00758 case HAWKI_BAND_K: 00759 extinction = 0.065; 00760 strcpy(magcol_2mass, HAWKI_COL_2MASS_K_MAG); 00761 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_K_MAGSIG); 00762 break ; 00763 default: 00764 extinction = 0.00; 00765 cpl_msg_warning(__func__,"The filter %s does not exist in 2MASS. " 00766 "The 2MASS K band will be used instead. " 00767 "Columns %s, %s, %s and %s in product will not " 00768 "be accurate", filter, 00769 HAWKI_COL_ZPOINT_MAG, HAWKI_COL_ZPOINT_ERRMAG, 00770 HAWKI_COL_ZPOINT_ZPOINT, HAWKI_COL_ZPOINT_ATX0); 00771 strcpy(magcol_2mass, HAWKI_COL_2MASS_K_MAG); 00772 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_K_MAGSIG); 00773 break ; 00774 } 00775 00776 /* Select only stars in 2MASS that have the given magnitude */ 00777 //irplib_stdstar_select_stars_mag(stars_2mass, magcol_2mass); 00778 //cpl_msg_warning(__func__,"Paso irplib"); 00779 00780 /* Create the table */ 00781 nstars = cpl_table_get_nrow(stars_2mass); 00782 nmatches = cpl_array_get_size(matches) - cpl_array_count_invalid(matches); 00783 zpoints = cpl_table_new(nmatches) ; 00784 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT) ; 00785 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE) ; 00786 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE) ; 00787 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE) ; 00788 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE) ; 00789 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_MAG, CPL_TYPE_DOUBLE) ; 00790 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ERRMAG, CPL_TYPE_DOUBLE) ; 00791 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE) ; 00792 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE) ; 00793 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING) ; 00794 // cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE) ; 00795 // cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE) ; 00796 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE) ; 00797 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE) ; 00798 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE) ; 00799 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE) ; 00800 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE) ; 00801 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE) ; 00802 imatch = 0; 00803 00804 for (istar=0; istar<nstars; istar++) 00805 { 00806 int iobj; 00807 int null; 00808 double zpoint; 00809 double atx0; 00810 double flux; 00811 double mag; 00812 double errmag; 00813 double fwhm_x; 00814 double fwhm_y; 00815 double fwhm; 00816 00817 iobj = cpl_array_get_int(matches, istar, &null); 00818 00819 if(null != 0) 00820 continue; 00821 if(!cpl_table_is_selected(stars_2mass, istar)) 00822 continue; 00823 00824 flux = cpl_table_get_double 00825 (obj_det_param, HAWKI_COL_OBJ_FLUX, iobj, NULL); 00826 mag = cpl_table_get_float 00827 (stars_2mass, magcol_2mass, istar, NULL); 00828 errmag = cpl_table_get_float 00829 (stars_2mass, magerrcol_2mass, istar, NULL); 00830 zpoint = mag + 2.5 * log10(flux) - 2.5 * log10(dit); 00831 atx0 = zpoint + airmass * extinction; 00832 fwhm_x = cpl_table_get_double 00833 (obj_det_param, HAWKI_COL_OBJ_FWHM_MAJAX, iobj, NULL); 00834 fwhm_y = cpl_table_get_double 00835 (obj_det_param, HAWKI_COL_OBJ_FWHM_MINAX, iobj, NULL); 00836 fwhm = sqrt(fwhm_x*fwhm_y); 00837 00838 cpl_table_set_int(zpoints, HAWKI_COL_ZPOINT_CHIP, imatch, idet + 1) ; 00839 cpl_table_set_double 00840 (zpoints, HAWKI_COL_ZPOINT_MAG, imatch, mag); 00841 cpl_table_set_double 00842 (zpoints, HAWKI_COL_ZPOINT_ERRMAG, imatch, errmag); 00843 cpl_table_set_string 00844 (zpoints, HAWKI_COL_ZPOINT_FILTER, imatch, filter); 00845 cpl_table_set_double 00846 (zpoints, HAWKI_COL_ZPOINT_AIRMASS, imatch, airmass); 00847 cpl_table_set_double 00848 (zpoints, HAWKI_COL_ZPOINT_POSX, imatch, cpl_table_get_double 00849 (obj_det_param, HAWKI_COL_OBJ_POSX, iobj, NULL)); 00850 cpl_table_set_double 00851 (zpoints, HAWKI_COL_ZPOINT_POSY, imatch, cpl_table_get_double 00852 (obj_det_param, HAWKI_COL_OBJ_POSY, iobj, NULL)); 00853 cpl_table_set_double(zpoints, HAWKI_COL_ZPOINT_ZPOINT, imatch, zpoint); 00854 cpl_table_set_double(zpoints, HAWKI_COL_ZPOINT_ATX0, imatch, atx0); 00855 cpl_table_set_double 00856 (zpoints, HAWKI_COL_ZPOINT_FLUX, imatch, flux); 00857 // cpl_table_set_double(tab, HAWKI_COL_ZPOINT_PEAK, imatch, 00858 // hawki_img_zpoint_outputs.peak[labels[iframe]-1]) ; 00859 // cpl_table_set_double(tab, HAWKI_COL_ZPOINT_BGD, imatch, 00860 // hawki_img_zpoint_outputs.bgd[labels[iframe]-1]) ; 00861 cpl_table_set_double 00862 (zpoints, HAWKI_COL_ZPOINT_FWHMX, imatch, fwhm_x); 00863 cpl_table_set_double 00864 (zpoints, HAWKI_COL_ZPOINT_FWHMY, imatch, fwhm_y); 00865 cpl_table_set_double 00866 (zpoints, HAWKI_COL_ZPOINT_FWHM, imatch, fwhm); 00867 cpl_table_set_double 00868 (zpoints, HAWKI_COL_ZPOINT_FWHMX_AS, imatch, fwhm_x * pixscale); 00869 cpl_table_set_double 00870 (zpoints, HAWKI_COL_ZPOINT_FWHMY_AS, imatch, fwhm_y * pixscale); 00871 cpl_table_set_double 00872 (zpoints, HAWKI_COL_ZPOINT_FWHM_AS, imatch, fwhm * pixscale); 00873 ++imatch; 00874 } 00875 00876 /* Check error and return */ 00877 if(!cpl_errorstate_is_equal(error_prevstate)) 00878 { 00879 cpl_msg_error(__func__,"An error happened filling the zpoint table: %s", 00880 cpl_error_get_message()); 00881 cpl_table_delete(zpoints); 00882 return NULL; 00883 } 00884 return zpoints; 00885 } 00886 00887 /*----------------------------------------------------------------------------*/ 00892 /*----------------------------------------------------------------------------*/ 00893 static cpl_propertylist ** hawki_step_photom_2mass_qc 00894 (cpl_table ** zpoint_table) 00895 { 00896 int idet; 00897 cpl_propertylist ** qclists; 00898 00899 /* Allocate the qclists */ 00900 qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ; 00901 00902 /* Loop on the detectors to get the mean zpoint */ 00903 for(idet = 0 ; idet < HAWKI_NB_DETECTORS ; ++idet) 00904 { 00905 double mean_zpoint; 00906 00907 /* Allocate this qclist */ 00908 qclists[idet] = cpl_propertylist_new() ; 00909 00910 /* Compute the mean zpoint */ 00911 mean_zpoint = cpl_table_get_column_mean(zpoint_table[idet], 00912 HAWKI_COL_ZPOINT_ZPOINT); 00913 00914 cpl_propertylist_append_double 00915 (qclists[idet], "ESO QC ZPOINT", mean_zpoint); 00916 } 00917 00918 return qclists; 00919 } 00920 00921 /*----------------------------------------------------------------------------*/ 00931 /*----------------------------------------------------------------------------*/ 00932 static int hawki_step_photom_2mass_save 00933 (cpl_table ** zpoints, 00934 cpl_parameterlist * parlist, 00935 cpl_propertylist ** qclists, 00936 cpl_frameset * set) 00937 { 00938 cpl_propertylist * protype; 00939 cpl_frame * ref_frame ; 00940 cpl_frameset * combinedframes; 00941 int nframes ; 00942 const char * recipe_name = "hawki_step_photom_2mass" ; 00943 00944 /* Initialise */ 00945 nframes = cpl_frameset_get_size(set) ; 00946 00947 /* Get the reference frame */ 00948 combinedframes = hawki_extract_frameset(set, HAWKI_CALPRO_COMBINED); 00949 ref_frame = cpl_frameset_get_first(combinedframes); 00950 00951 /* Create the protype lists */ 00952 protype = cpl_propertylist_new(); 00953 cpl_propertylist_append_string(protype, "ESO PRO TYPE", 00954 HAWKI_PROTYPE_ZPOINT_TAB); 00955 00956 /* Write the zpoint table */ 00957 hawki_tables_save(set, 00958 parlist, 00959 set, 00960 (const cpl_table **)zpoints, 00961 recipe_name, 00962 HAWKI_CALPRO_ZPOINT_TAB, 00963 HAWKI_PROTYPE_ZPOINT_TAB, 00964 protype, 00965 (const cpl_propertylist **)qclists, 00966 "hawki_step_photom_2mass.fits") ; 00967 00968 /* Free and return */ 00969 cpl_propertylist_delete(protype); 00970 cpl_frameset_delete(combinedframes); 00971 return 0; 00972 }