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 <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
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
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
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
00163 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00164 recipe = (cpl_recipe *)plugin ;
00165 else return -1 ;
00166
00167
00168 recipe->parameters = cpl_parameterlist_new() ;
00169
00170
00171 return 0;
00172 }
00173
00174
00180
00181 static int hawki_step_photom_2mass_exec(cpl_plugin * plugin)
00182 {
00183 cpl_recipe * recipe ;
00184
00185
00186 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00187 recipe = (cpl_recipe *)plugin ;
00188 else return -1 ;
00189
00190
00191 hawki_print_banner();
00192
00193 return hawki_step_photom_2mass(recipe->parameters, recipe->frames) ;
00194 }
00195
00196
00202
00203 static int hawki_step_photom_2mass_destroy(cpl_plugin * plugin)
00204 {
00205 cpl_recipe * recipe ;
00206
00207
00208 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00209 recipe = (cpl_recipe *)plugin ;
00210 else return -1 ;
00211
00212 cpl_parameterlist_delete(recipe->parameters) ;
00213 return 0 ;
00214 }
00215
00216
00222
00223 static int hawki_step_photom_2mass(cpl_parameterlist * parlist,
00224 cpl_frameset * frameset)
00225 {
00226 cpl_frameset * cat_2mass;
00227 cpl_frameset * obj_param;
00228 cpl_frameset * obj_ima;
00229 cpl_table ** zpoint_table;
00230 cpl_propertylist ** qclists;
00231 int idet;
00232
00233
00234 if (hawki_dfs_set_groups(frameset)) {
00235 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00236 return -1 ;
00237 }
00238
00239
00240 if ((cat_2mass = hawki_extract_frameset(frameset,
00241 HAWKI_UTIL_CAT_2MASS)) == NULL)
00242 {
00243 cpl_msg_error(__func__, "Cannot find 2MASS catalogue (%s)",
00244 HAWKI_UTIL_CAT_2MASS);
00245 return -1 ;
00246 }
00247
00248
00249 if ((obj_param = hawki_extract_frameset
00250 (frameset, HAWKI_CALPRO_OBJ_PARAM)) == NULL)
00251 {
00252 cpl_msg_error(__func__, "Cannot find object parameters (%s)",
00253 HAWKI_CALPRO_OBJ_PARAM);
00254 return -1 ;
00255 }
00256
00257
00258 if ((obj_ima = hawki_extract_frameset
00259 (frameset, HAWKI_CALPRO_COMBINED)) == NULL)
00260 {
00261 cpl_msg_error(__func__, "Cannot find combined image (%s) ",
00262 HAWKI_CALPRO_COMBINED);
00263 return -1 ;
00264 }
00265
00266
00267 zpoint_table = hawki_step_photom_2mass_get_zpoints
00268 (cat_2mass, obj_param, obj_ima);
00269 if(zpoint_table == NULL)
00270 {
00271 cpl_msg_error(__func__,"Could not get the zpoints");
00272 cpl_frameset_delete(cat_2mass);
00273 cpl_frameset_delete(obj_param);
00274 cpl_frameset_delete(obj_ima);
00275 return -1;
00276 }
00277
00278
00279 qclists = hawki_step_photom_2mass_qc(zpoint_table);
00280 if(zpoint_table == NULL)
00281 {
00282 cpl_msg_error(__func__,"Could not compute quality controls");
00283 cpl_frameset_delete(cat_2mass);
00284 cpl_frameset_delete(obj_param);
00285 cpl_frameset_delete(obj_ima);
00286 hawki_table_delete(zpoint_table);
00287 return -1;
00288 }
00289
00290
00291 cpl_msg_info(__func__,"Saving products");
00292 if(hawki_step_photom_2mass_save(zpoint_table, parlist, qclists, frameset) == -1)
00293 {
00294 cpl_msg_error(__func__,"Could not save products");
00295 cpl_frameset_delete(cat_2mass);
00296 cpl_frameset_delete(obj_param);
00297 cpl_frameset_delete(obj_ima);
00298 hawki_table_delete(zpoint_table);
00299 return -1;
00300 }
00301
00302
00303 cpl_frameset_delete(cat_2mass);
00304 cpl_frameset_delete(obj_param);
00305 cpl_frameset_delete(obj_ima);
00306 hawki_table_delete(zpoint_table);
00307 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00308 cpl_propertylist_delete(qclists[idet]);
00309 cpl_free(qclists);
00310
00311
00312 if (cpl_error_get_code())
00313 {
00314 cpl_msg_error(__func__,
00315 "HAWK-I pipeline could not recover from previous errors");
00316 return -1 ;
00317 }
00318 else return 0 ;
00319 }
00320
00321 cpl_table ** hawki_step_photom_2mass_get_zpoints
00322 (cpl_frameset * cat_2mass,
00323 cpl_frameset * obj_param,
00324 cpl_frameset * obj_ima)
00325 {
00326 cpl_table ** zpoint_table;
00327 cpl_table ** obj_det_param;
00328 cpl_propertylist * plist;
00329 int idet;
00330 cpl_errorstate error_prevstate = cpl_errorstate_get();
00331
00332
00333 zpoint_table = cpl_malloc(sizeof(cpl_table *) * HAWKI_NB_DETECTORS);
00334
00335
00336 obj_det_param = hawki_load_tables(cpl_frameset_get_first(obj_param));
00337
00338
00339 plist = cpl_propertylist_load
00340 (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)), 0);
00341
00342 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00343 {
00344 cpl_propertylist * wcs_info;
00345 cpl_table * stars_2mass;
00346 cpl_array * matches;
00347 int ext_nb;
00348
00349 cpl_msg_info(__func__,"Working on chip %d", idet + 1);
00350 cpl_msg_indent_more();
00351
00352
00353 ext_nb=hawki_get_ext_from_detector
00354 (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)),idet+1);
00355 wcs_info = cpl_propertylist_load
00356 (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)), ext_nb);
00357 stars_2mass =
00358 hawki_step_photom_2mass_retrieve_stars(cat_2mass, wcs_info);
00359 if(stars_2mass == NULL)
00360 {
00361 int jdet;
00362 cpl_msg_error(__func__, "Cannot retrieve stars");
00363 cpl_propertylist_delete(plist);
00364 cpl_propertylist_delete(wcs_info);
00365 hawki_table_delete(obj_det_param);
00366 for(jdet = 0; jdet <idet; ++jdet)
00367 cpl_table_delete(zpoint_table[jdet]);
00368 cpl_free(zpoint_table);
00369 return NULL;
00370 }
00371
00372
00373 cpl_msg_info(__func__,"Matching %d 2MASS stars and %d detections",
00374 cpl_table_get_nrow(stars_2mass),
00375 cpl_table_get_nrow(obj_det_param[idet]));
00376 matches = hawki_step_photom_2mass_ppm(stars_2mass, obj_det_param[idet]);
00377
00378
00379 cpl_msg_info(__func__,"Computing zero points");
00380 zpoint_table[idet] = hawki_step_photom_2mass_fill_zpoint_table
00381 (stars_2mass, obj_det_param[idet], matches, plist, idet);
00382 if(zpoint_table[idet] == NULL)
00383 {
00384 int jdet;
00385 cpl_msg_error(__func__, "Could not compute the zero points");
00386 cpl_propertylist_delete(plist);
00387 cpl_propertylist_delete(wcs_info);
00388 hawki_table_delete(obj_det_param);
00389 cpl_table_delete(stars_2mass);
00390 for(jdet = 0; jdet <idet; ++jdet)
00391 cpl_table_delete(zpoint_table[jdet]);
00392 cpl_free(zpoint_table);
00393 cpl_array_delete(matches);
00394 return NULL;
00395 }
00396
00397
00398 cpl_msg_indent_less();
00399 cpl_propertylist_delete(wcs_info);
00400 cpl_table_delete(stars_2mass);
00401 cpl_array_delete(matches);
00402 }
00403
00404
00405 hawki_table_delete(obj_det_param);
00406 cpl_propertylist_delete(plist);
00407 if(!cpl_errorstate_is_equal(error_prevstate))
00408 {
00409 int jdet;
00410 for(jdet = 0; jdet <HAWKI_NB_DETECTORS; ++jdet)
00411 cpl_table_delete(zpoint_table[jdet]);
00412 cpl_free(zpoint_table);
00413 cpl_msg_error(__func__, "A problem happened computing the zero point");
00414 cpl_errorstate_set(CPL_ERROR_NONE);
00415 return NULL;
00416 }
00417 return zpoint_table;
00418 }
00419
00420 static cpl_table * hawki_step_photom_2mass_retrieve_stars
00421 (cpl_frameset * cat_2mass,
00422 cpl_propertylist * wcs_keywords)
00423 {
00424 char * catpath;
00425 char * catname;
00426 cpl_table * stars;
00427 cpl_wcs * wcs;
00428 int nstars;
00429 int istar;
00430 double ra1;
00431 double ra2;
00432 double dec1;
00433 double dec2;
00434 double extra_search = 0.;
00435 cpl_matrix * from_coord;
00436 cpl_matrix * to_coord;
00437 cpl_array * status;
00438
00439
00440
00441 if (irplib_2mass_get_catpars(cpl_frameset_get_first(cat_2mass),
00442 &catpath, &catname) != CPL_ERROR_NONE)
00443 return NULL;
00444
00445
00446 wcs = cpl_wcs_new_from_propertylist(wcs_keywords);
00447 if(cpl_error_get_code() == CPL_ERROR_NO_WCS)
00448 {
00449 cpl_msg_error(__func__,"Not compiled with WCS support.");
00450 return NULL;
00451 }
00452
00453
00454 if(irplib_cat_get_image_limits(wcs, extra_search,
00455 &ra1, &ra2, &dec1, &dec2) ==
00456 CPL_ERROR_DATA_NOT_FOUND)
00457 {
00458 cpl_msg_error(__func__,"No WCS information found");
00459 cpl_free(catname);
00460 cpl_free(catpath);
00461 cpl_wcs_delete(wcs);
00462 return NULL;
00463 }
00464 cpl_msg_info(__func__,"Searching stars in RA=[%f,%f] DEC=[%f,%f]",
00465 ra1/15., ra2/15., dec1, dec2);
00466
00467
00468
00469 stars = irplib_2mass_extract(catpath, ra1, ra2, dec1, dec2);
00470 if(stars == NULL)
00471 {
00472 cpl_msg_error(__func__,"Error retrieving 2mass stars: %s ",
00473 cpl_error_get_message());
00474 cpl_free(catname);
00475 cpl_free(catpath);
00476 cpl_wcs_delete(wcs);
00477 return NULL;
00478 }
00479 nstars = cpl_table_get_nrow(stars);
00480 cpl_msg_indent_more();
00481 cpl_msg_info(__func__, "%d 2MASS stars found", nstars);
00482
00483
00484 from_coord = cpl_matrix_new(nstars, 2);
00485 for (istar=0; istar<nstars; istar++)
00486 {
00487 cpl_matrix_set(from_coord, istar, 0, cpl_table_get_float
00488 (stars, HAWKI_COL_2MASS_RA, istar, NULL));
00489 cpl_matrix_set(from_coord, istar, 1, cpl_table_get_float
00490 (stars, HAWKI_COL_2MASS_DEC, istar, NULL));
00491 }
00492
00493 to_coord = NULL;
00494 status = NULL;
00495 if(cpl_wcs_convert(wcs, from_coord, &to_coord,
00496 &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE)
00497 {
00498 cpl_array_delete(status);
00499 cpl_matrix_delete(from_coord);
00500 cpl_matrix_delete(to_coord);
00501 cpl_free(catname);
00502 cpl_free(catpath);
00503 cpl_wcs_delete(wcs);
00504 cpl_msg_error(cpl_func,"Error in cpl_wcs conversion. %s",
00505 cpl_error_get_message());
00506 return NULL;
00507 }
00508
00509
00510 cpl_table_new_column(stars, HAWKI_COL_2MASS_XPREDICT, CPL_TYPE_FLOAT);
00511 cpl_table_set_column_unit(stars,HAWKI_COL_2MASS_XPREDICT, "pixels");
00512 cpl_table_new_column(stars, HAWKI_COL_2MASS_YPREDICT, CPL_TYPE_FLOAT);
00513 cpl_table_set_column_unit(stars, HAWKI_COL_2MASS_YPREDICT, "pixels");
00514 for(istar=0; istar< nstars; istar++)
00515 {
00516 float xpredict = (float)cpl_matrix_get(to_coord, istar, 0);
00517 float ypredict = (float)cpl_matrix_get(to_coord, istar, 1);
00518 cpl_table_set_float(stars,"xpredict", istar, xpredict);
00519 cpl_table_set_float(stars,"ypredict", istar, ypredict);
00520 }
00521
00522
00523 cpl_array_delete(status);
00524 cpl_matrix_delete(from_coord);
00525 cpl_matrix_delete(to_coord);
00526 cpl_wcs_delete(wcs);
00527 cpl_free(catname);
00528 cpl_free(catpath);
00529 cpl_msg_indent_less();
00530 return stars;
00531 }
00532
00533
00539
00540 static cpl_array * hawki_step_photom_2mass_ppm
00541 (cpl_table * stars_2mass,
00542 cpl_table * obj_det)
00543 {
00544 int istar;
00545 int iobj;
00546 int iter;
00547 int nstars_2mass;
00548 int nstars_2mass_used_match;
00549 int nobj;
00550 int nobj_used_match;
00551 int nmatches;
00552 int nmax_match_pattern = 30;
00553 cpl_matrix * pattern;
00554 cpl_array * matches;
00555 double tol = 0.1;
00556 double pradius = 30.0;
00557 double mean_data_pos_err = 5.;
00558 int ppm_max_iter = 5;
00559 double scale = 1;
00560 double angle;
00561 cpl_matrix * obj_pos;
00562 cpl_propertylist * sort_prop;
00563
00564
00565
00566 cpl_msg_indent_more();
00567 sort_prop = cpl_propertylist_new();
00568 cpl_propertylist_append_bool(sort_prop, HAWKI_COL_OBJ_FLUX, 1);
00569 if (cpl_table_sort(obj_det, sort_prop) != CPL_ERROR_NONE)
00570 {
00571 cpl_msg_error(cpl_func,"Cannot sort detected sources table");
00572 cpl_propertylist_delete(sort_prop);
00573 return NULL;
00574 }
00575
00576
00577 nobj = cpl_table_get_nrow(obj_det);
00578 obj_pos = cpl_matrix_new(2, nobj);
00579 for (iobj=0; iobj<nobj; iobj++)
00580 {
00581 float xim = cpl_table_get_double
00582 (obj_det, HAWKI_COL_OBJ_POSX, iobj, NULL);
00583 float yim = cpl_table_get_double
00584 (obj_det, HAWKI_COL_OBJ_POSY, iobj, NULL);
00585 cpl_matrix_set(obj_pos, 0, iobj, xim);
00586 cpl_matrix_set(obj_pos, 1, iobj, yim);
00587 }
00588
00589
00590 cpl_propertylist_empty(sort_prop);
00591 cpl_propertylist_append_bool(sort_prop, HAWKI_COL_2MASS_K_MAG, 0);
00592 if (cpl_table_sort(stars_2mass, sort_prop) != CPL_ERROR_NONE)
00593 {
00594 cpl_msg_error(cpl_func,"Cannot sort 2MASS stars table");
00595 cpl_propertylist_delete(sort_prop);
00596 return NULL;
00597 }
00598
00599
00600 nstars_2mass = cpl_table_get_nrow(stars_2mass);
00601 pattern = cpl_matrix_new(2, nstars_2mass);
00602 for(istar=0; istar<nstars_2mass ; istar++)
00603 {
00604 float x = cpl_table_get_float
00605 (stars_2mass, HAWKI_COL_2MASS_XPREDICT, istar, NULL);
00606 float y = cpl_table_get_float
00607 (stars_2mass, HAWKI_COL_2MASS_YPREDICT, istar, NULL);
00608 cpl_matrix_set(pattern, 0, istar, x);
00609 cpl_matrix_set(pattern, 1, istar, y);
00610 }
00611
00612
00613 nstars_2mass_used_match = nmax_match_pattern;
00614 if(nstars_2mass < nmax_match_pattern)
00615 nstars_2mass_used_match = nstars_2mass;
00616 nobj_used_match = (int)(1.7 * nstars_2mass_used_match);
00617 if(nobj_used_match > nobj)
00618 nobj_used_match = nobj;
00619 if(nobj_used_match < nstars_2mass_used_match)
00620 nobj_used_match = nstars_2mass_used_match;
00621 cpl_msg_info(__func__,"The first step match will use %d stars "
00622 "and %d objects", nstars_2mass_used_match,nobj_used_match);
00623 for (iter = 0; iter < ppm_max_iter; iter++)
00624 {
00625 int nmatchsize;
00626 nmatches = 0;
00627 matches = cpl_ppm_match_points(obj_pos, nobj_used_match,
00628 mean_data_pos_err,
00629 pattern, nstars_2mass_used_match,
00630 1, tol, pradius,
00631 NULL, NULL, &scale, &angle);
00632 if(matches != NULL)
00633 {
00634 nmatchsize = cpl_array_get_size(matches);
00635 nmatches = nmatchsize -
00636 cpl_array_count_invalid(matches);
00637 }
00638 else
00639 {
00640 nmatchsize = 0;
00641 nmatches = 0;
00642 }
00643
00644 cpl_msg_info(cpl_func,"Total matches: %d. Valid matches: %d",
00645 nmatchsize, nmatches);
00646 cpl_msg_info(cpl_func,"Scale=%g angle=%g", scale, angle);
00647 if((matches == NULL) || (nmatches < floor(nobj_used_match*0.1)))
00648 {
00649 nobj_used_match = nobj_used_match + 10;
00650 cpl_msg_info(cpl_func,
00651 "Increasing number of detections used in PPM to %d",
00652 nobj_used_match);
00653 continue;
00654 }
00655 else
00656 break;
00657 }
00658
00659
00660 cpl_msg_indent_more();
00661 cpl_msg_debug(__func__,"Matched stars:");
00662 cpl_msg_indent_more();
00663 cpl_msg_debug(__func__,"X_OBJ Y_OBJ X_STAR Y_STAR X_DIFF Y_DIFF:");
00664 for(istar=0; istar < nstars_2mass; ++istar)
00665 {
00666 int null;
00667 double x_obj, y_obj, x_star, y_star, x_diff, y_diff;
00668 iobj = cpl_array_get_int(matches, istar, &null);
00669
00670 if(null != 0)
00671 continue;
00672 x_obj = cpl_matrix_get(obj_pos, 0, iobj);
00673 y_obj = cpl_matrix_get(obj_pos, 1, iobj);
00674 x_star = cpl_matrix_get(pattern, 0, istar);
00675 y_star = cpl_matrix_get(pattern, 1, istar);
00676 x_diff = x_obj - x_star;
00677 y_diff = y_obj - y_star;
00678 cpl_msg_debug(__func__,"%6.1f %6.1f %6.1f %6.1f %6.1f %6.1f\n",
00679 x_obj, y_obj, x_star, y_star, x_diff, y_diff);
00680 }
00681 cpl_msg_indent_less();
00682 cpl_msg_indent_less();
00683
00684 cpl_matrix_delete(pattern);
00685 cpl_msg_info(cpl_func, "%d points matched", nmatches);
00686
00687 if(matches == NULL || nmatches == 0)
00688 {
00689 if(nmatches == 0)
00690 cpl_array_delete(matches);
00691 cpl_msg_error(cpl_func,"Error in PPM. %s",cpl_error_get_message());
00692 cpl_matrix_delete(obj_pos);
00693 cpl_propertylist_delete(sort_prop);
00694 cpl_msg_indent_less();
00695 return NULL;
00696 }
00697
00698 if(nmatches < floor(nobj_used_match*0.1))
00699 {
00700 cpl_msg_warning(cpl_func,"PPM detected matched only %d objects."
00701 " Results could be unreliable",nmatches);
00702 }
00703
00704
00705 cpl_matrix_delete(obj_pos);
00706 cpl_propertylist_delete(sort_prop);
00707 cpl_msg_indent_less();
00708 return matches;
00709 }
00710
00711
00716
00717 static cpl_table * hawki_step_photom_2mass_fill_zpoint_table
00718 (cpl_table * stars_2mass,
00719 cpl_table * obj_det_param,
00720 cpl_array * matches,
00721 cpl_propertylist * plist,
00722 int idet)
00723 {
00724 cpl_table * zpoints;
00725 int nmatches;
00726 int nstars;
00727 int imatch;
00728 int istar;
00729 const char * filter;
00730 char magcol_2mass[100];
00731 char magerrcol_2mass[100];
00732 double dit;
00733 double airmass;
00734 double pixscale;
00735 double extinction;
00736 cpl_errorstate error_prevstate = cpl_errorstate_get();
00737
00738
00739 airmass = (hawki_pfits_get_airmass_start(plist) +
00740 hawki_pfits_get_airmass_end(plist)) / 2.;
00741 filter = hawki_pfits_get_filter(plist);
00742 dit = hawki_pfits_get_dit(plist);
00743 pixscale = hawki_pfits_get_pixscale(plist);
00744 switch (hawki_get_band(filter))
00745 {
00746 case HAWKI_BAND_J:
00747 extinction = 0.098;
00748 strcpy(magcol_2mass, HAWKI_COL_2MASS_J_MAG);
00749 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_J_MAGSIG);
00750 break ;
00751 case HAWKI_BAND_H:
00752 extinction = 0.039;
00753 strcpy(magcol_2mass, HAWKI_COL_2MASS_H_MAG);
00754 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_H_MAGSIG);
00755 break ;
00756 case HAWKI_BAND_K:
00757 extinction = 0.065;
00758 strcpy(magcol_2mass, HAWKI_COL_2MASS_K_MAG);
00759 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_K_MAGSIG);
00760 break ;
00761 default:
00762 extinction = 0.00;
00763 cpl_msg_warning(__func__,"The filter %s does not exist in 2MASS. "
00764 "The 2MASS K band will be used instead. "
00765 "Columns %s, %s, %s and %s in product will not "
00766 "be accurate", filter,
00767 HAWKI_COL_ZPOINT_MAG, HAWKI_COL_ZPOINT_ERRMAG,
00768 HAWKI_COL_ZPOINT_ZPOINT, HAWKI_COL_ZPOINT_ATX0);
00769 strcpy(magcol_2mass, HAWKI_COL_2MASS_K_MAG);
00770 strcpy(magerrcol_2mass, HAWKI_COL_2MASS_K_MAGSIG);
00771 break ;
00772 }
00773
00774
00775
00776
00777
00778
00779 nstars = cpl_table_get_nrow(stars_2mass);
00780 nmatches = cpl_array_get_size(matches) - cpl_array_count_invalid(matches);
00781 zpoints = cpl_table_new(nmatches) ;
00782 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT) ;
00783 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE) ;
00784 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE) ;
00785 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE) ;
00786 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE) ;
00787 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_MAG, CPL_TYPE_DOUBLE) ;
00788 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ERRMAG, CPL_TYPE_DOUBLE) ;
00789 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE) ;
00790 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE) ;
00791 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING) ;
00792
00793
00794 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE) ;
00795 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE) ;
00796 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE) ;
00797 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE) ;
00798 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE) ;
00799 cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE) ;
00800 imatch = 0;
00801
00802 for (istar=0; istar<nstars; istar++)
00803 {
00804 int iobj;
00805 int null;
00806 double zpoint;
00807 double atx0;
00808 double flux;
00809 double mag;
00810 double errmag;
00811 double fwhm_x;
00812 double fwhm_y;
00813 double fwhm;
00814
00815 iobj = cpl_array_get_int(matches, istar, &null);
00816
00817 if(null != 0)
00818 continue;
00819 if(!cpl_table_is_selected(stars_2mass, istar))
00820 continue;
00821
00822 flux = cpl_table_get_double
00823 (obj_det_param, HAWKI_COL_OBJ_FLUX, iobj, NULL);
00824 mag = cpl_table_get_float
00825 (stars_2mass, magcol_2mass, istar, NULL);
00826 errmag = cpl_table_get_float
00827 (stars_2mass, magerrcol_2mass, istar, NULL);
00828 zpoint = mag + 2.5 * log10(flux) - 2.5 * log10(dit);
00829 atx0 = zpoint + airmass * extinction;
00830 fwhm_x = cpl_table_get_double
00831 (obj_det_param, HAWKI_COL_OBJ_FWHM_MAJAX, iobj, NULL);
00832 fwhm_y = cpl_table_get_double
00833 (obj_det_param, HAWKI_COL_OBJ_FWHM_MINAX, iobj, NULL);
00834 fwhm = sqrt(fwhm_x*fwhm_y);
00835
00836 cpl_table_set_int(zpoints, HAWKI_COL_ZPOINT_CHIP, imatch, idet + 1) ;
00837 cpl_table_set_double
00838 (zpoints, HAWKI_COL_ZPOINT_MAG, imatch, mag);
00839 cpl_table_set_double
00840 (zpoints, HAWKI_COL_ZPOINT_ERRMAG, imatch, errmag);
00841 cpl_table_set_string
00842 (zpoints, HAWKI_COL_ZPOINT_FILTER, imatch, filter);
00843 cpl_table_set_double
00844 (zpoints, HAWKI_COL_ZPOINT_AIRMASS, imatch, airmass);
00845 cpl_table_set_double
00846 (zpoints, HAWKI_COL_ZPOINT_POSX, imatch, cpl_table_get_double
00847 (obj_det_param, HAWKI_COL_OBJ_POSX, iobj, NULL));
00848 cpl_table_set_double
00849 (zpoints, HAWKI_COL_ZPOINT_POSY, imatch, cpl_table_get_double
00850 (obj_det_param, HAWKI_COL_OBJ_POSY, iobj, NULL));
00851 cpl_table_set_double(zpoints, HAWKI_COL_ZPOINT_ZPOINT, imatch, zpoint);
00852 cpl_table_set_double(zpoints, HAWKI_COL_ZPOINT_ATX0, imatch, atx0);
00853 cpl_table_set_double
00854 (zpoints, HAWKI_COL_ZPOINT_FLUX, imatch, flux);
00855
00856
00857
00858
00859 cpl_table_set_double
00860 (zpoints, HAWKI_COL_ZPOINT_FWHMX, imatch, fwhm_x);
00861 cpl_table_set_double
00862 (zpoints, HAWKI_COL_ZPOINT_FWHMY, imatch, fwhm_y);
00863 cpl_table_set_double
00864 (zpoints, HAWKI_COL_ZPOINT_FWHM, imatch, fwhm);
00865 cpl_table_set_double
00866 (zpoints, HAWKI_COL_ZPOINT_FWHMX_AS, imatch, fwhm_x * pixscale);
00867 cpl_table_set_double
00868 (zpoints, HAWKI_COL_ZPOINT_FWHMY_AS, imatch, fwhm_y * pixscale);
00869 cpl_table_set_double
00870 (zpoints, HAWKI_COL_ZPOINT_FWHM_AS, imatch, fwhm * pixscale);
00871 ++imatch;
00872 }
00873
00874
00875 if(!cpl_errorstate_is_equal(error_prevstate))
00876 {
00877 cpl_msg_error(__func__,"An error happened filling the zpoint table: %s",
00878 cpl_error_get_message());
00879 cpl_table_delete(zpoints);
00880 return NULL;
00881 }
00882 return zpoints;
00883 }
00884
00885
00890
00891 static cpl_propertylist ** hawki_step_photom_2mass_qc
00892 (cpl_table ** zpoint_table)
00893 {
00894 int idet;
00895 cpl_propertylist ** qclists;
00896
00897
00898 qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00899
00900
00901 for(idet = 0 ; idet < HAWKI_NB_DETECTORS ; ++idet)
00902 {
00903 double mean_zpoint;
00904
00905
00906 qclists[idet] = cpl_propertylist_new() ;
00907
00908
00909 mean_zpoint = cpl_table_get_column_mean(zpoint_table[idet],
00910 HAWKI_COL_ZPOINT_ZPOINT);
00911
00912 cpl_propertylist_append_double
00913 (qclists[idet], "ESO QC ZPOINT", mean_zpoint);
00914 }
00915
00916 return qclists;
00917 }
00918
00919
00929
00930 static int hawki_step_photom_2mass_save
00931 (cpl_table ** zpoints,
00932 cpl_parameterlist * parlist,
00933 cpl_propertylist ** qclists,
00934 cpl_frameset * set)
00935 {
00936 cpl_propertylist * protype;
00937 cpl_frame * ref_frame ;
00938 cpl_frameset * combinedframes;
00939 int nframes ;
00940 const char * recipe_name = "hawki_step_photom_2mass" ;
00941
00942
00943 nframes = cpl_frameset_get_size(set) ;
00944
00945
00946 combinedframes = hawki_extract_frameset(set, HAWKI_CALPRO_COMBINED);
00947 ref_frame = cpl_frameset_get_first(combinedframes);
00948
00949
00950 protype = cpl_propertylist_new();
00951 cpl_propertylist_append_string(protype, "ESO PRO TYPE",
00952 HAWKI_PROTYPE_ZPOINT_TAB);
00953
00954
00955 hawki_tables_save(set,
00956 parlist,
00957 set,
00958 (const cpl_table **)zpoints,
00959 recipe_name,
00960 HAWKI_CALPRO_ZPOINT_TAB,
00961 HAWKI_PROTYPE_ZPOINT_TAB,
00962 protype,
00963 (const cpl_propertylist **)qclists,
00964 "hawki_step_photom_2mass.fits") ;
00965
00966
00967 cpl_propertylist_delete(protype);
00968 cpl_frameset_delete(combinedframes);
00969 return 0;
00970 }