hawki_step_photom_2mass.c

00001 /* $Id: hawki_step_photom_2mass.c,v 1.13 2010/06/04 09:48:03 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: 2010/06/04 09:48:03 $
00024  * $Revision: 1.13 $
00025  * $Name: hawki-1_7_2 $
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 
00170     /* Return */
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     /* Get the recipe out of the plugin */
00186     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00187         recipe = (cpl_recipe *)plugin ;
00188     else return -1 ;
00189 
00190     /* Issue a banner */
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     /* Get the recipe out of the plugin */
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     /* Identify the RAW and CALIB frames in the input frameset */
00234     if (hawki_dfs_set_groups(frameset)) {
00235         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00236         return -1 ;
00237     }
00238 
00239     /* Retrieve 2MASS catalogue */
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     /* Retrieve obj param */
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     /* Retrieve reference image */
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     /* Compute the zpoints */
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     /* Get some QC */
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     /* Save the products */
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     /* Free and return */
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     if (cpl_error_get_code()) return -1 ;
00312     else return 0 ;
00313 }
00314 
00315 cpl_table ** hawki_step_photom_2mass_get_zpoints
00316 (cpl_frameset *     cat_2mass,
00317  cpl_frameset *     obj_param,
00318  cpl_frameset *     obj_ima)
00319 {
00320     cpl_table **       zpoint_table;
00321     cpl_table **       obj_det_param;
00322     cpl_propertylist * plist;
00323     int                idet;
00324     cpl_errorstate     error_prevstate = cpl_errorstate_get();
00325     
00326     /* Allocate zpoint_table */
00327     zpoint_table = cpl_malloc(sizeof(cpl_table *) * HAWKI_NB_DETECTORS);
00328 
00329     /* Load detected obj */
00330     obj_det_param = hawki_load_tables(cpl_frameset_get_first(obj_param));
00331      
00332     /* Read the main header */
00333     plist = cpl_propertylist_load
00334         (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)), 0);
00335     /* Loop on detectors */    
00336     for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00337     {
00338         cpl_propertylist * wcs_info;
00339         cpl_table        * stars_2mass;
00340         cpl_array        * matches;
00341         int                ext_nb;
00342 
00343         cpl_msg_info(__func__,"Working on chip %d", idet + 1);
00344         cpl_msg_indent_more();
00345          
00346         /* Retrieve stars */
00347         ext_nb=hawki_get_ext_from_detector
00348             (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)),idet+1);
00349         wcs_info = cpl_propertylist_load
00350             (cpl_frame_get_filename(cpl_frameset_get_first(obj_ima)), ext_nb);
00351         stars_2mass = 
00352             hawki_step_photom_2mass_retrieve_stars(cat_2mass, wcs_info);        
00353         if(stars_2mass == NULL)
00354         {
00355             int jdet;
00356             cpl_msg_error(__func__, "Cannot retrieve stars");
00357             cpl_propertylist_delete(plist);
00358             cpl_propertylist_delete(wcs_info);
00359             hawki_table_delete(obj_det_param);
00360             for(jdet = 0; jdet <idet; ++jdet)
00361                 cpl_table_delete(zpoint_table[jdet]);
00362             cpl_free(zpoint_table);
00363             return NULL;
00364         }
00365          
00366         /* Pattern matching btw stars in 2MASS and detected ones */
00367         cpl_msg_info(__func__,"Matching %d 2MASS stars and %d detections",
00368                      cpl_table_get_nrow(stars_2mass),
00369                      cpl_table_get_nrow(obj_det_param[idet]));
00370         matches = hawki_step_photom_2mass_ppm(stars_2mass, obj_det_param[idet]);
00371         
00372         /* Fill the zpoint table */
00373         cpl_msg_info(__func__,"Computing zero points");
00374         zpoint_table[idet] = hawki_step_photom_2mass_fill_zpoint_table
00375             (stars_2mass, obj_det_param[idet], matches, plist, idet);
00376         if(zpoint_table[idet] == NULL)
00377         {
00378             int jdet;
00379             cpl_msg_error(__func__, "Could not compute the zero points");
00380             cpl_propertylist_delete(plist);
00381             cpl_propertylist_delete(wcs_info);
00382             hawki_table_delete(obj_det_param);
00383             cpl_table_delete(stars_2mass);
00384             for(jdet = 0; jdet <idet; ++jdet)
00385                 cpl_table_delete(zpoint_table[jdet]);
00386             cpl_free(zpoint_table);
00387             cpl_array_delete(matches);
00388             return NULL;
00389         }
00390         
00391         /* Cleaning resources */
00392         cpl_msg_indent_less();
00393         cpl_propertylist_delete(wcs_info);
00394         cpl_table_delete(stars_2mass);
00395         cpl_array_delete(matches);
00396     }
00397     
00398     /* Free and return */
00399     hawki_table_delete(obj_det_param);
00400     cpl_propertylist_delete(plist);
00401     if(!cpl_errorstate_is_equal(error_prevstate))
00402     {
00403         int jdet;
00404         for(jdet = 0; jdet <HAWKI_NB_DETECTORS; ++jdet)
00405             cpl_table_delete(zpoint_table[jdet]);
00406         cpl_free(zpoint_table);
00407         cpl_msg_error(__func__, "A problem happened computing the zero point");
00408         cpl_errorstate_set(CPL_ERROR_NONE);
00409         return NULL;
00410     }
00411     return zpoint_table;
00412 }
00413 
00414 static cpl_table *  hawki_step_photom_2mass_retrieve_stars
00415 (cpl_frameset *     cat_2mass,
00416  cpl_propertylist * wcs_keywords)
00417 {
00418     char *          catpath;
00419     char *          catname;
00420     cpl_table *     stars;
00421     cpl_wcs *       wcs;
00422     int             nstars;
00423     int             istar;
00424     double          ra1;
00425     double          ra2;
00426     double          dec1;
00427     double          dec2;
00428     double          extra_search = 0.;
00429     cpl_matrix *    from_coord;
00430     cpl_matrix *    to_coord;
00431     cpl_array *     status;
00432     
00433 
00434     /* Extract the catalog path */
00435     if (irplib_2mass_get_catpars(cpl_frameset_get_first(cat_2mass),
00436                                  &catpath, &catname) != CPL_ERROR_NONE) 
00437         return NULL;
00438     
00439     /* Get the WCS info */
00440     wcs = cpl_wcs_new_from_propertylist(wcs_keywords);
00441     if(cpl_error_get_code() == CPL_ERROR_NO_WCS)
00442     {
00443         cpl_msg_error(__func__,"Not compiled with WCS support.");
00444         return NULL;
00445     }
00446     
00447     /* Get the limits to search */
00448     if(irplib_cat_get_image_limits(wcs, extra_search, 
00449                                    &ra1, &ra2, &dec1, &dec2) == 
00450                                        CPL_ERROR_DATA_NOT_FOUND)
00451     {
00452         cpl_msg_error(__func__,"No WCS information found");
00453         cpl_free(catname);
00454         cpl_free(catpath);
00455         cpl_wcs_delete(wcs);
00456         return NULL;
00457     }
00458     cpl_msg_info(__func__,"Searching stars in RA=[%f,%f] DEC=[%f,%f]",
00459                  ra1/15., ra2/15., dec1, dec2);
00460     
00461     
00462     /* Search the stars */
00463     stars = irplib_2mass_extract(catpath, ra1, ra2, dec1, dec2);
00464     if(stars == NULL)
00465     {
00466         cpl_msg_error(__func__,"Error retrieving 2mass stars: %s ",
00467                       cpl_error_get_message());
00468         cpl_free(catname);
00469         cpl_free(catpath);
00470         cpl_wcs_delete(wcs);
00471         return NULL;
00472     }
00473     nstars = cpl_table_get_nrow(stars);
00474     cpl_msg_indent_more();
00475     cpl_msg_info(__func__, "%d 2MASS stars found", nstars);
00476 
00477     /* Convert Ra, Dec to X,Y using the WCS information from image */
00478     from_coord = cpl_matrix_new(nstars, 2);
00479     for (istar=0; istar<nstars; istar++)
00480     {
00481         cpl_matrix_set(from_coord, istar, 0, cpl_table_get_float
00482                        (stars, HAWKI_COL_2MASS_RA, istar, NULL));
00483         cpl_matrix_set(from_coord, istar, 1, cpl_table_get_float
00484                        (stars, HAWKI_COL_2MASS_DEC, istar, NULL));
00485     }
00486 
00487     to_coord = NULL;
00488     status   = NULL;
00489     if(cpl_wcs_convert(wcs, from_coord, &to_coord, 
00490                        &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE)
00491     {
00492         cpl_array_delete(status);
00493         cpl_matrix_delete(from_coord);
00494         cpl_matrix_delete(to_coord);
00495         cpl_free(catname);
00496         cpl_free(catpath);
00497         cpl_wcs_delete(wcs);
00498         cpl_msg_error(cpl_func,"Error in cpl_wcs conversion. %s",
00499                       cpl_error_get_message());
00500         return NULL;
00501     }
00502 
00503     /* Add the predicted x,y coordinate columns to the  2MASS table */
00504     cpl_table_new_column(stars, HAWKI_COL_2MASS_XPREDICT, CPL_TYPE_FLOAT);
00505     cpl_table_set_column_unit(stars,HAWKI_COL_2MASS_XPREDICT, "pixels");
00506     cpl_table_new_column(stars, HAWKI_COL_2MASS_YPREDICT, CPL_TYPE_FLOAT);
00507     cpl_table_set_column_unit(stars, HAWKI_COL_2MASS_YPREDICT, "pixels");
00508     for(istar=0; istar< nstars; istar++)
00509     {
00510         float xpredict = (float)cpl_matrix_get(to_coord, istar, 0);
00511         float ypredict = (float)cpl_matrix_get(to_coord, istar, 1);
00512         cpl_table_set_float(stars,"xpredict", istar, xpredict);
00513         cpl_table_set_float(stars,"ypredict", istar, ypredict);
00514     }
00515     
00516     /* Free and return */
00517     cpl_array_delete(status);
00518     cpl_matrix_delete(from_coord);
00519     cpl_matrix_delete(to_coord);
00520     cpl_wcs_delete(wcs);
00521     cpl_free(catname);
00522     cpl_free(catpath);
00523     cpl_msg_indent_less();
00524     return stars;
00525 }
00526 
00527 /*----------------------------------------------------------------------------*/
00533 /*----------------------------------------------------------------------------*/
00534 static cpl_array *   hawki_step_photom_2mass_ppm
00535 (cpl_table *  stars_2mass,
00536  cpl_table *  obj_det)
00537 {
00538     int                istar;
00539     int                iobj;
00540     int                iter;
00541     int                nstars_2mass;
00542     int                nstars_2mass_used_match;
00543     int                nobj;
00544     int                nobj_used_match;
00545     int                nmatches;
00546     int                nmax_match_pattern = 30;
00547     cpl_matrix *       pattern;
00548     cpl_array *        matches;
00549     double             tol = 0.1;
00550     double             pradius = 30.0;
00551     double             mean_data_pos_err = 5.;
00552     double             scale;
00553     double             angle;
00554     cpl_matrix *       obj_pos;
00555     cpl_propertylist * sort_prop;
00556     
00557     
00558     /* Sort the detected objects */
00559     cpl_msg_indent_more();
00560     sort_prop = cpl_propertylist_new();
00561     cpl_propertylist_append_bool(sort_prop, HAWKI_COL_OBJ_FLUX, 1);
00562     if (cpl_table_sort(obj_det, sort_prop) != CPL_ERROR_NONE) 
00563     {
00564         cpl_msg_error(cpl_func,"Cannot sort detected sources table");
00565         cpl_propertylist_delete(sort_prop);
00566         return NULL;
00567     }
00568 
00569     /* Create matrix of X,Y coordinates of detected objects*/
00570     nobj = cpl_table_get_nrow(obj_det);
00571     obj_pos = cpl_matrix_new(2, nobj);
00572     for (iobj=0; iobj<nobj; iobj++)
00573     {
00574         float xim = cpl_table_get_double
00575             (obj_det, HAWKI_COL_OBJ_POSX, iobj, NULL); 
00576         float yim = cpl_table_get_double
00577             (obj_det, HAWKI_COL_OBJ_POSY, iobj, NULL);
00578         cpl_matrix_set(obj_pos, 0, iobj, xim);
00579         cpl_matrix_set(obj_pos, 1, iobj, yim);
00580     }
00581 
00582     /* Sort the 2MASS stars by magnitude */
00583     cpl_propertylist_empty(sort_prop);
00584     cpl_propertylist_append_bool(sort_prop, HAWKI_COL_2MASS_K_MAG, 0);
00585     if (cpl_table_sort(stars_2mass, sort_prop) != CPL_ERROR_NONE) 
00586     {
00587         cpl_msg_error(cpl_func,"Cannot sort 2MASS stars table");
00588         cpl_propertylist_delete(sort_prop);
00589         return NULL;
00590     }
00591 
00592     /* Prepare the 2MASS matrix for the pattern matching */
00593     nstars_2mass = cpl_table_get_nrow(stars_2mass);
00594     pattern = cpl_matrix_new(2, nstars_2mass);
00595     for(istar=0; istar<nstars_2mass ; istar++)
00596     {
00597         float x = cpl_table_get_float
00598             (stars_2mass, HAWKI_COL_2MASS_XPREDICT, istar, NULL);
00599         float y = cpl_table_get_float
00600             (stars_2mass, HAWKI_COL_2MASS_YPREDICT, istar, NULL);
00601         cpl_matrix_set(pattern, 0, istar, x);
00602         cpl_matrix_set(pattern, 1, istar, y);
00603     }
00604     
00605     /* Do the ppm */
00606     nstars_2mass_used_match = nmax_match_pattern;
00607     if(nstars_2mass < nmax_match_pattern)
00608         nstars_2mass_used_match = nstars_2mass;
00609     nobj_used_match = (int)(1.5 * nstars_2mass_used_match);
00610     if(nobj_used_match > nobj)
00611         nobj_used_match = nobj;
00612     if(nobj_used_match < nstars_2mass_used_match)
00613         nobj_used_match = nstars_2mass_used_match;
00614     for (iter = 0; iter < 4; iter++)
00615     {
00616         cpl_msg_info(__func__,"The first step match will use %d stars "
00617                      "and %d objects", nstars_2mass_used_match,nobj_used_match);
00618         nmatches = 0;
00619         matches = cpl_ppm_match_points(obj_pos, nobj_used_match, 
00620                                        mean_data_pos_err,
00621                                        pattern, nstars_2mass_used_match,
00622                                        1, tol, pradius,
00623                                        NULL, NULL, &scale, &angle);
00624         if(matches != NULL)
00625             nmatches = cpl_array_get_size(matches) -
00626                 cpl_array_count_invalid(matches);
00627         cpl_msg_info(cpl_func,"Total matches: %d. Valid matches: %d",
00628                      cpl_array_get_size(matches), nmatches);
00629         cpl_msg_info(cpl_func,"Scale=%g angle=%g", scale, angle);
00630         if((matches == NULL) || (nmatches < floor(nobj_used_match*0.1))) 
00631         {
00632             cpl_msg_info(cpl_func,
00633                          "Increasing number of detections used in PPM by 10");
00634             nobj_used_match = nobj_used_match + 10;
00635             continue;
00636         }
00637         else
00638             break;
00639     }
00640 
00641     /* Output debug messages */
00642     cpl_msg_indent_more();
00643     cpl_msg_debug(__func__,"Matched stars:");
00644     cpl_msg_indent_more();
00645     cpl_msg_debug(__func__,"X_OBJ  Y_OBJ    X_STAR Y_STAR   X_DIFF Y_DIFF:");
00646     for(istar=0; istar < nstars_2mass; ++istar)
00647     {
00648         int  null;
00649         double x_obj, y_obj, x_star, y_star, x_diff, y_diff;
00650         iobj  = cpl_array_get_int(matches, istar, &null);
00651         
00652         if(null != 0)
00653             continue;
00654         x_obj = cpl_matrix_get(obj_pos, 0, iobj);
00655         y_obj = cpl_matrix_get(obj_pos, 1, iobj);
00656         x_star = cpl_matrix_get(pattern, 0, istar);
00657         y_star = cpl_matrix_get(pattern, 1, istar);
00658         x_diff = x_obj - x_star;
00659         y_diff = y_obj - y_star;
00660         cpl_msg_debug(__func__,"%6.1f %6.1f  %6.1f %6.1f   %6.1f %6.1f\n",
00661                       x_obj, y_obj, x_star, y_star, x_diff, y_diff);
00662     }
00663     cpl_msg_indent_less();    
00664     cpl_msg_indent_less();
00665     
00666     cpl_matrix_delete(pattern);
00667     cpl_msg_info(cpl_func, "%d points matched", nmatches);
00668 
00669     if((matches == NULL) || (nmatches < floor(nobj_used_match*0.1)))
00670     {
00671         cpl_msg_error(cpl_func,"Error in PPM. %s",cpl_error_get_message());
00672         cpl_msg_indent_less();
00673         return NULL;
00674     }
00675     
00676     /* Free and return */
00677     cpl_matrix_delete(obj_pos);
00678     cpl_propertylist_delete(sort_prop);
00679     cpl_msg_indent_less();
00680     return matches;
00681 }
00682 
00683 /*----------------------------------------------------------------------------*/
00688 /*----------------------------------------------------------------------------*/
00689 static cpl_table * hawki_step_photom_2mass_fill_zpoint_table
00690 (cpl_table *        stars_2mass,
00691  cpl_table *        obj_det_param,
00692  cpl_array *        matches,
00693  cpl_propertylist * plist,
00694  int                idet)
00695 {
00696     cpl_table    * zpoints;
00697     int            nmatches;
00698     int            nstars;
00699     int            imatch;
00700     int            istar;
00701     const char   * filter;
00702     char           magcol_2mass[100];
00703     char           magerrcol_2mass[100];
00704     double         dit;
00705     double         airmass;
00706     double         pixscale;
00707     double         extinction;
00708     cpl_errorstate error_prevstate = cpl_errorstate_get();
00709     
00710    
00711     /* Read parameters from the propertylist */
00712     airmass  = (hawki_pfits_get_airmass_start(plist) +
00713                 hawki_pfits_get_airmass_end(plist)) / 2.;
00714     filter   = hawki_pfits_get_filter(plist);
00715     dit      = hawki_pfits_get_dit(plist);
00716     pixscale = hawki_pfits_get_pixscale(plist);
00717     switch (hawki_get_band(filter))
00718     {
00719         case HAWKI_BAND_J:    
00720             extinction = 0.098;
00721             strcpy(magcol_2mass, HAWKI_COL_2MASS_J_MAG);
00722             strcpy(magerrcol_2mass, HAWKI_COL_2MASS_J_MAGSIG);
00723             break ;
00724         case HAWKI_BAND_H:
00725             extinction = 0.039;
00726             strcpy(magcol_2mass, HAWKI_COL_2MASS_H_MAG);
00727             strcpy(magerrcol_2mass, HAWKI_COL_2MASS_H_MAGSIG);
00728             break ;
00729         case HAWKI_BAND_K:
00730             extinction = 0.065;
00731             strcpy(magcol_2mass, HAWKI_COL_2MASS_K_MAG);
00732             strcpy(magerrcol_2mass, HAWKI_COL_2MASS_K_MAGSIG);
00733             break ;
00734         default: 
00735             extinction = 0.00;
00736             cpl_msg_warning(__func__,"The filter %s does not exist in 2MASS. "
00737                             "The 2MASS K band will be used instead. "
00738                             "Columns %s, %s, %s and %s in product will not "
00739                             "be accurate", filter, 
00740                             HAWKI_COL_ZPOINT_MAG, HAWKI_COL_ZPOINT_ERRMAG,
00741                             HAWKI_COL_ZPOINT_ZPOINT, HAWKI_COL_ZPOINT_ATX0);
00742             strcpy(magcol_2mass, HAWKI_COL_2MASS_K_MAG);
00743             strcpy(magerrcol_2mass, HAWKI_COL_2MASS_K_MAGSIG);
00744             break ;
00745     }
00746                 
00747     /* Select only stars in 2MASS that have the given magnitude */
00748     //irplib_stdstar_select_stars_mag(stars_2mass, magcol_2mass);
00749     //cpl_msg_warning(__func__,"Paso irplib");
00750     
00751     /* Create the table */
00752     nstars = cpl_table_get_nrow(stars_2mass);
00753     nmatches = cpl_array_get_size(matches) - cpl_array_count_invalid(matches);
00754     zpoints = cpl_table_new(nmatches) ;
00755     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT) ;
00756     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE) ;
00757     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE) ;
00758     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE) ;
00759     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE) ;
00760     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_MAG, CPL_TYPE_DOUBLE) ;
00761     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_ERRMAG, CPL_TYPE_DOUBLE) ;
00762     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE) ;
00763     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE) ;
00764     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING) ;
00765 //    cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE) ;
00766 //    cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE) ;
00767     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE) ;
00768     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE) ;
00769     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE) ;
00770     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE) ;
00771     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE) ;
00772     cpl_table_new_column(zpoints, HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE) ;
00773     imatch = 0;
00774     
00775     for (istar=0; istar<nstars; istar++) 
00776     {
00777         int      iobj;
00778         int      null;
00779         double   zpoint;
00780         double   atx0;
00781         double   flux;
00782         double   mag;
00783         double   errmag;
00784         double   fwhm_x;
00785         double   fwhm_y;
00786         double   fwhm;
00787         
00788         iobj  = cpl_array_get_int(matches, istar, &null);
00789         
00790         if(null != 0)
00791             continue;
00792         if(!cpl_table_is_selected(stars_2mass, istar))
00793             continue;
00794        
00795         flux = cpl_table_get_double
00796             (obj_det_param, HAWKI_COL_OBJ_FLUX, iobj, NULL);
00797         mag = cpl_table_get_float
00798             (stars_2mass, magcol_2mass, istar, NULL); 
00799         errmag = cpl_table_get_float
00800             (stars_2mass, magerrcol_2mass, istar, NULL);
00801         zpoint = mag + 2.5 * log10(flux) - 2.5 * log10(dit);
00802         atx0 = zpoint + airmass * extinction;
00803         fwhm_x = cpl_table_get_double
00804             (obj_det_param, HAWKI_COL_OBJ_FWHM_MAJAX, iobj, NULL);
00805         fwhm_y = cpl_table_get_double
00806             (obj_det_param, HAWKI_COL_OBJ_FWHM_MINAX, iobj, NULL);
00807         fwhm   = sqrt(fwhm_x*fwhm_y); 
00808 
00809         cpl_table_set_int(zpoints, HAWKI_COL_ZPOINT_CHIP, imatch, idet + 1) ;
00810         cpl_table_set_double
00811             (zpoints, HAWKI_COL_ZPOINT_MAG, imatch, mag);
00812         cpl_table_set_double
00813             (zpoints, HAWKI_COL_ZPOINT_ERRMAG, imatch, errmag);
00814         cpl_table_set_string
00815             (zpoints, HAWKI_COL_ZPOINT_FILTER, imatch, filter);
00816         cpl_table_set_double
00817             (zpoints, HAWKI_COL_ZPOINT_AIRMASS, imatch, airmass);
00818         cpl_table_set_double
00819             (zpoints, HAWKI_COL_ZPOINT_POSX, imatch, cpl_table_get_double
00820              (obj_det_param, HAWKI_COL_OBJ_POSX, iobj, NULL));
00821         cpl_table_set_double
00822             (zpoints, HAWKI_COL_ZPOINT_POSY, imatch, cpl_table_get_double
00823              (obj_det_param, HAWKI_COL_OBJ_POSY, iobj, NULL));
00824         cpl_table_set_double(zpoints, HAWKI_COL_ZPOINT_ZPOINT, imatch, zpoint);
00825         cpl_table_set_double(zpoints, HAWKI_COL_ZPOINT_ATX0, imatch, atx0);
00826         cpl_table_set_double
00827             (zpoints, HAWKI_COL_ZPOINT_FLUX, imatch, flux);
00828 //        cpl_table_set_double(tab, HAWKI_COL_ZPOINT_PEAK, imatch, 
00829 //                hawki_img_zpoint_outputs.peak[labels[iframe]-1]) ;
00830 //        cpl_table_set_double(tab, HAWKI_COL_ZPOINT_BGD, imatch, 
00831 //                hawki_img_zpoint_outputs.bgd[labels[iframe]-1]) ;
00832         cpl_table_set_double
00833             (zpoints, HAWKI_COL_ZPOINT_FWHMX, imatch, fwhm_x);
00834         cpl_table_set_double
00835             (zpoints, HAWKI_COL_ZPOINT_FWHMY, imatch, fwhm_y);
00836         cpl_table_set_double
00837             (zpoints, HAWKI_COL_ZPOINT_FWHM, imatch, fwhm);
00838         cpl_table_set_double
00839             (zpoints, HAWKI_COL_ZPOINT_FWHMX_AS, imatch, fwhm_x * pixscale);
00840         cpl_table_set_double
00841             (zpoints, HAWKI_COL_ZPOINT_FWHMY_AS, imatch, fwhm_y * pixscale);
00842         cpl_table_set_double
00843             (zpoints, HAWKI_COL_ZPOINT_FWHM_AS, imatch, fwhm * pixscale);
00844         ++imatch;
00845     }
00846     
00847     /* Check error and return */
00848     if(!cpl_errorstate_is_equal(error_prevstate))
00849     {
00850         cpl_msg_error(__func__,"An error happened filling the zpoint table: %s",
00851                       cpl_error_get_message());
00852         cpl_table_delete(zpoints);
00853         return NULL;
00854     }
00855     return zpoints;
00856 }
00857 
00858 /*----------------------------------------------------------------------------*/
00863 /*----------------------------------------------------------------------------*/
00864 static cpl_propertylist ** hawki_step_photom_2mass_qc
00865 (cpl_table ** zpoint_table)
00866 {
00867     int idet;
00868     cpl_propertylist ** qclists;
00869     
00870     /* Allocate the qclists */
00871     qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00872     
00873     /* Loop on the detectors to get the mean zpoint */
00874     for(idet = 0 ; idet < HAWKI_NB_DETECTORS ; ++idet)
00875     {
00876         double mean_zpoint;
00877 
00878         /* Allocate this qclist */
00879         qclists[idet] = cpl_propertylist_new() ;
00880         
00881         /* Compute the mean zpoint */
00882         mean_zpoint = cpl_table_get_column_mean(zpoint_table[idet],
00883                                                 HAWKI_COL_ZPOINT_ZPOINT);
00884         
00885         cpl_propertylist_append_double
00886             (qclists[idet], "ESO QC ZPOINT", mean_zpoint);
00887     }
00888     
00889     return qclists;
00890 }
00891 
00892 /*----------------------------------------------------------------------------*/
00902 /*----------------------------------------------------------------------------*/
00903 static int hawki_step_photom_2mass_save
00904 (cpl_table **         zpoints,
00905  cpl_parameterlist *  parlist,
00906  cpl_propertylist **  qclists,
00907  cpl_frameset *       set)
00908 {
00909     cpl_propertylist *   protype;
00910     cpl_frame *          ref_frame ;
00911     cpl_frameset *       combinedframes;
00912     int                  nframes ;
00913     const char *         recipe_name = "hawki_step_photom_2mass" ;
00914 
00915     /* Initialise */
00916     nframes = cpl_frameset_get_size(set) ;
00917 
00918     /* Get the reference frame */
00919     combinedframes = hawki_extract_frameset(set, HAWKI_CALPRO_COMBINED);
00920     ref_frame = cpl_frameset_get_first(combinedframes);
00921 
00922     /* Create the protype lists */
00923     protype = cpl_propertylist_new();
00924     cpl_propertylist_append_string(protype, "ESO PRO TYPE",
00925             HAWKI_PROTYPE_ZPOINT_TAB);
00926 
00927     /* Write the zpoint table  */
00928     hawki_tables_save(set,
00929                       parlist,
00930                       set,
00931                       (const cpl_table **)zpoints,
00932                       recipe_name,
00933                       HAWKI_CALPRO_ZPOINT_TAB,
00934                       HAWKI_PROTYPE_ZPOINT_TAB,
00935                       protype,
00936                       (const cpl_propertylist **)qclists,
00937                       "hawki_step_photom_2mass.fits") ;
00938 
00939     /* Free and return */
00940     cpl_propertylist_delete(protype);
00941     cpl_frameset_delete(combinedframes);
00942     return  0;
00943 }

Generated on 10 Jun 2010 for HAWKI Pipeline Reference Manual by  doxygen 1.6.1