hawki_step_detect_obj.c

00001 /* $Id: hawki_step_detect_obj.c,v 1.19 2010/06/04 09:47:53 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:47:53 $
00024  * $Revision: 1.19 $
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 #include "irplib_calib.h"
00042 
00043 #include "hawki_utils.h"
00044 #include "hawki_obj_det.h"
00045 #include "hawki_mask.h"
00046 #include "hawki_image_stats.h"
00047 #include "hawki_calib.h"
00048 #include "hawki_load.h"
00049 #include "hawki_save.h"
00050 #include "hawki_pfits.h"
00051 #include "hawki_dfs.h"
00052 
00053 /*-----------------------------------------------------------------------------
00054                             Functions prototypes
00055  -----------------------------------------------------------------------------*/
00056 
00057 static int hawki_step_detect_obj_create(cpl_plugin *) ;
00058 static int hawki_step_detect_obj_exec(cpl_plugin *) ;
00059 static int hawki_step_detect_obj_destroy(cpl_plugin *) ;
00060 static int hawki_step_detect_obj(cpl_parameterlist *, cpl_frameset *) ;
00061 
00062 static void hawki_step_detect_obj_init_output(void);
00063 static void hawki_step_detect_obj_get_pscale
00064 (cpl_frameset * combframes);
00065 static int hawki_step_detect_obj_retrieve_input_param
00066 (cpl_parameterlist  *  parlist);
00067 static cpl_apertures  ** hawki_step_detect_obj_mask_and_apertures
00068 (cpl_frameset    *  combframes,
00069  cpl_image       ** mask_image,
00070  cpl_image       ** comb_image);
00071 static int hawki_step_detect_obj_aper_params
00072 (cpl_image      **  combined_images, 
00073  cpl_apertures  **  apertures,
00074  cpl_table      **  obj_charac);
00075 static int hawki_step_detect_obj_save
00076 (cpl_image           **  mask_images,
00077  cpl_table           **  obj_charac,
00078  cpl_propertylist    **  obj_stats,
00079  cpl_parameterlist   *   parlist,
00080  cpl_frameset        *   framelist);
00081 
00082 /*-----------------------------------------------------------------------------
00083                             Static variables
00084  -----------------------------------------------------------------------------*/
00085 
00086 static struct 
00087 {
00088     /* Inputs */
00089     double sigma_det;
00090     int    growing_radius;
00091 } hawki_step_detect_obj_config;
00092 
00093 static struct 
00094 {
00095     /* Outputs */
00096     double          pixscale;
00097     double          iq[HAWKI_NB_DETECTORS] ;
00098     int             nbobjs[HAWKI_NB_DETECTORS] ;
00099     double          fwhm_pix[HAWKI_NB_DETECTORS] ;
00100     double          fwhm_arcsec[HAWKI_NB_DETECTORS] ;
00101     double          fwhm_mode[HAWKI_NB_DETECTORS] ;
00102     double          pos_x[HAWKI_NB_DETECTORS] ;
00103     double          pos_y[HAWKI_NB_DETECTORS] ;
00104 } hawki_step_detect_obj_output;
00105 
00106 static char hawki_step_detect_obj_description[] =
00107 "hawki_step_detect_obj -- hawki detect objects recipe.\n"
00108 "This recipe detects objects from the combined image creating a mask\n"
00109 "and a list of object properties\n"
00110 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00111 "combined.fits "HAWKI_CALPRO_COMBINED"\n";
00112 
00113 /*-----------------------------------------------------------------------------
00114                                 Functions code
00115  -----------------------------------------------------------------------------*/
00116 
00117 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 int cpl_plugin_get_info(cpl_pluginlist * list)
00127 {
00128     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00129     cpl_plugin  *   plugin = &recipe->interface ;
00130 
00131     cpl_plugin_init(plugin,
00132                     CPL_PLUGIN_API,
00133                     HAWKI_BINARY_VERSION,
00134                     CPL_PLUGIN_TYPE_RECIPE,
00135                     "hawki_step_detect_obj",
00136                     "Object detection recipe",
00137                     hawki_step_detect_obj_description,
00138                     "Cesar Enrique Garcia Dabo",
00139                     PACKAGE_BUGREPORT,  
00140                     hawki_get_license(),
00141                     hawki_step_detect_obj_create,
00142                     hawki_step_detect_obj_exec,
00143                     hawki_step_detect_obj_destroy) ;
00144 
00145     cpl_pluginlist_append(list, plugin) ;
00146     
00147     return 0;
00148 }
00149 
00150 /*----------------------------------------------------------------------------*/
00159 /*----------------------------------------------------------------------------*/
00160 static int hawki_step_detect_obj_create(cpl_plugin * plugin)
00161 {
00162     cpl_recipe      * recipe ;
00163     cpl_parameter   * p ;
00164 
00165     /* Get the recipe out of the plugin */
00166     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00167         recipe = (cpl_recipe *)plugin ;
00168     else return -1 ;
00169 
00170     /* Create the parameters list in the cpl_recipe object */
00171     recipe->parameters = cpl_parameterlist_new() ;
00172 
00173     /* Fill the parameters list */
00174     /* --sigma_det */
00175     p = cpl_parameter_new_value("hawki.hawki_step_detect_obj.sigma_det", 
00176                                 CPL_TYPE_DOUBLE, "detection level",
00177                                 "hawki.hawki_step_detect_obj", 6.);
00178     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_det");
00179     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00180     cpl_parameterlist_append(recipe->parameters, p);
00181 
00182     /* --growing_radius */
00183     p = cpl_parameter_new_value("hawki.hawki_step_detect_obj.growing_radius", 
00184                                 CPL_TYPE_INT,
00185                                 "radius of convolution kernel to apply to objects",
00186                                 "hawki.hawki_step_detect_obj", 5);
00187     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "growing_radius");
00188     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00189     cpl_parameterlist_append(recipe->parameters, p);
00190 
00191     /* Return */
00192     return 0;
00193 }
00194 
00195 /*----------------------------------------------------------------------------*/
00201 /*----------------------------------------------------------------------------*/
00202 static int hawki_step_detect_obj_exec(cpl_plugin * plugin)
00203 {
00204     cpl_recipe  *   recipe ;
00205 
00206     /* Get the recipe out of the plugin */
00207     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00208         recipe = (cpl_recipe *)plugin ;
00209     else return -1 ;
00210 
00211     /* Issue a banner */
00212     hawki_print_banner();
00213 
00214     return hawki_step_detect_obj(recipe->parameters, recipe->frames) ;
00215 }
00216 
00217 /*----------------------------------------------------------------------------*/
00223 /*----------------------------------------------------------------------------*/
00224 static int hawki_step_detect_obj_destroy(cpl_plugin * plugin)
00225 {
00226     cpl_recipe  *   recipe ;
00227 
00228     /* Get the recipe out of the plugin */
00229     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00230         recipe = (cpl_recipe *)plugin ;
00231     else return -1 ;
00232 
00233     cpl_parameterlist_delete(recipe->parameters) ;
00234     return 0 ;
00235 }
00236 
00237 /*----------------------------------------------------------------------------*/
00244 /*----------------------------------------------------------------------------*/
00245 static int hawki_step_detect_obj(
00246         cpl_parameterlist   *   parlist, 
00247         cpl_frameset        *   framelist)
00248 {
00249     cpl_frameset     *   combframes;
00250     cpl_image        **  mask_image;
00251     cpl_image        **  comb_image;
00252     cpl_apertures    **  apertures;
00253     cpl_table        **  obj_charac;
00254     cpl_propertylist **  obj_stats;
00255     int                  idet;
00256     
00257     /* Initialise */
00258     hawki_step_detect_obj_init_output();
00259 
00260     /* Retrieve input parameters */
00261     if(hawki_step_detect_obj_retrieve_input_param(parlist))
00262     {
00263         cpl_msg_error(__func__, "Wrong parameters");
00264         return -1;
00265     }
00266 
00267     /* Identify the RAW and CALIB frames in the input frameset */
00268     if (hawki_dfs_set_groups(framelist)) {
00269         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00270         return -1 ;
00271     }
00272 
00273     /* Retrieve raw frames */
00274     combframes = hawki_extract_frameset(framelist, HAWKI_CALPRO_COMBINED) ;
00275     if (combframes == NULL) 
00276     {
00277         cpl_msg_error(__func__, "Cannot find combined images in the input (%s)",
00278                 HAWKI_CALPRO_COMBINED);
00279         return -1 ;
00280     }
00281     if (cpl_frameset_get_size(combframes) != 1)
00282     {
00283         cpl_msg_error(__func__, "Only one combined image must be provided");
00284         return -1 ;
00285     }
00286 
00287     /* Get info from the headers */
00288     hawki_step_detect_obj_get_pscale(combframes);
00289     
00290     /* Get the mask with the points above the background 
00291      * and the associated apertures*/
00292     cpl_msg_info(__func__, "Getting the object masks") ;
00293     mask_image = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_image *));
00294     comb_image = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_image *));
00295     apertures =  hawki_step_detect_obj_mask_and_apertures
00296         (combframes, mask_image, comb_image);
00297     if(apertures == NULL)
00298     {
00299         cpl_msg_error(__func__,"Could not detect objects in image");
00300         cpl_frameset_delete(combframes);
00301         cpl_free(mask_image);
00302         cpl_free(comb_image);
00303         return -1;
00304     }
00305     
00306     /* Get object characterizations and statistics */
00307     cpl_msg_info(__func__, "Getting object parameters") ;
00308     obj_charac = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *)) ;
00309     obj_stats  = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist *));
00310     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00311     {
00312         obj_charac[idet] = cpl_table_new
00313             (cpl_apertures_get_size(apertures[idet]));
00314         obj_stats[idet] = cpl_propertylist_new();
00315     }
00316     hawki_step_detect_obj_aper_params(comb_image, apertures, obj_charac);
00317  
00318     /* Statistics of the detected objects in the QC */
00319     hawki_obj_prop_stats(obj_charac, obj_stats);
00320 
00321     /* Save the products */
00322     cpl_msg_info(__func__, "Save the products") ;
00323     if (hawki_step_detect_obj_save(mask_image, obj_charac, obj_stats,
00324                                    parlist, framelist) == -1)
00325     {
00326         cpl_msg_warning(__func__, "Some data could not be saved. "
00327                         "Check permisions or disk space") ;
00328         for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00329         {
00330             cpl_table_delete(obj_charac[idet]);
00331             cpl_propertylist_delete(obj_stats[idet]);
00332             cpl_apertures_delete(apertures[idet]);
00333         }
00334         cpl_free(apertures);
00335         cpl_free(obj_charac);
00336         cpl_free(obj_stats);
00337         cpl_frameset_delete(combframes);
00338         for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00339         {
00340             cpl_image_delete(mask_image[idet]);
00341             cpl_image_delete(comb_image[idet]);
00342         }
00343         cpl_free(mask_image);
00344         cpl_free(comb_image);
00345         return -1 ;
00346     }
00347     
00348     /* Return */
00349     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00350     {
00351         cpl_table_delete(obj_charac[idet]);
00352         cpl_propertylist_delete(obj_stats[idet]);
00353         cpl_apertures_delete(apertures[idet]);
00354         cpl_image_delete(mask_image[idet]);
00355         cpl_image_delete(comb_image[idet]);
00356     }
00357     cpl_free(apertures);
00358     cpl_free(obj_charac);
00359     cpl_free(obj_stats);
00360     cpl_frameset_delete(combframes);
00361     cpl_free(mask_image);
00362     cpl_free(comb_image);
00363 
00364     if (cpl_error_get_code()) return -1 ;
00365     else return 0 ;
00366 }
00367 
00368 int hawki_step_detect_obj_retrieve_input_param
00369 (cpl_parameterlist  *  parlist)
00370 {
00371     cpl_parameter   *   par ;
00372 
00373     par = NULL ;
00374     par = cpl_parameterlist_find
00375         (parlist, "hawki.hawki_step_detect_obj.sigma_det");
00376     hawki_step_detect_obj_config.sigma_det = cpl_parameter_get_double(par);
00377     par = cpl_parameterlist_find
00378         (parlist, "hawki.hawki_step_detect_obj.growing_radius");
00379     hawki_step_detect_obj_config.growing_radius = cpl_parameter_get_int(par);
00380     if(hawki_step_detect_obj_config.growing_radius > 100)
00381     {
00382         cpl_msg_error(__func__,"The maximum radius allowed is 100");
00383         return -1;
00384     }
00385     if(hawki_step_detect_obj_config.sigma_det <= 0 )
00386     {
00387         cpl_msg_error(__func__,"Detection sigma has to be greater than 0");
00388         return -1;
00389     }
00390 
00391     return 0;
00392 }
00393 
00394 
00395 
00396 /*----------------------------------------------------------------------------*/
00406 /*----------------------------------------------------------------------------*/
00407 static cpl_apertures  ** hawki_step_detect_obj_mask_and_apertures
00408 (cpl_frameset    *  combframes,
00409  cpl_image       ** mask_image,
00410  cpl_image       ** comb_image)
00411 {
00412     cpl_apertures   **  apertures;
00413     int                 idet;
00414 
00415     /* Create output object */
00416     apertures = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_apertures *));
00417   
00418     /* Loop on the detectors */
00419     cpl_msg_indent_more();
00420     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00421     {
00422         cpl_image  * chip_image;
00423         cpl_image  * chip_image_sort;
00424         cpl_mask   * object_mask;
00425         cpl_matrix * kernel;
00426         cpl_image  * labels;
00427         int          nobj;
00428         double       bkg_level;
00429         double       bkg_noise;
00430         double       threshold;
00431         int          kernel_size;
00432         int          ix;
00433         int          iy;
00434 
00435         cpl_msg_info(__func__, "Detecting objects on chip number %d", idet+1) ;
00436         cpl_msg_indent_more();
00437         
00438         /* Load the input data */
00439         cpl_msg_info(__func__, "Load the input data") ;
00440         chip_image = hawki_load_image(combframes, 0, idet+1, CPL_TYPE_FLOAT);
00441         if (chip_image == NULL) 
00442         {
00443             cpl_msg_error(__func__, "Cannot load chip %d", idet+1) ;
00444             cpl_msg_indent_less() ;
00445             cpl_free(apertures);
00446             return NULL ;
00447         }
00448         
00449         /* Subtract the median of the frame first */
00450         chip_image_sort = cpl_image_duplicate(chip_image);
00451         bkg_level = cpl_image_get_median(chip_image);
00452         bkg_noise = hawki_image_float_get_sigma_from_quartile(chip_image_sort); 
00453         cpl_image_delete(chip_image_sort);
00454         threshold = bkg_level + hawki_step_detect_obj_config.sigma_det * bkg_noise;        
00455         cpl_msg_info(__func__, "Background:       %f",bkg_level);
00456         cpl_msg_info(__func__, "Background noise: %f",bkg_noise);
00457         
00458         /* Create the mask */
00459         cpl_msg_info(__func__, "Mask creation with threshold: %f",threshold);
00460         object_mask = cpl_mask_threshold_image_create
00461             (chip_image, threshold, DBL_MAX);
00462 
00463         /* Apply a morphological opening to remove single pixel detections */
00464         cpl_msg_info(__func__, "Removing single pixel detections");
00465         kernel = cpl_matrix_new(3, 3);
00466         cpl_matrix_fill(kernel, 1.00) ;
00467         if (cpl_mask_opening(object_mask, kernel) != CPL_ERROR_NONE) {
00468             cpl_mask_delete(object_mask) ;
00469             cpl_matrix_delete(kernel) ;
00470             return NULL;
00471         }
00472         cpl_matrix_delete(kernel) ;
00473         
00474         /* Apply dilation to the mask */
00475         if(hawki_step_detect_obj_config.growing_radius>0)
00476         {
00477             cpl_msg_info(__func__, "Growing the mask with radius %d",
00478                     hawki_step_detect_obj_config.growing_radius);
00479             kernel_size = hawki_step_detect_obj_config.growing_radius*2+1;
00480             kernel = cpl_matrix_new(kernel_size, kernel_size);
00481             for(ix=0;ix<kernel_size;++ix)
00482                 for(iy=0;iy<kernel_size;++iy)
00483                 {
00484                     double xpos = ix+0.5-kernel_size/2.;
00485                     double ypos = iy+0.5-kernel_size/2.;
00486                     double kernel_func = 1-sqrt(xpos*xpos+ypos*ypos)/
00487                     hawki_step_detect_obj_config.growing_radius;
00488                     if(kernel_func<0)
00489                         kernel_func = 0;
00490                     cpl_matrix_set(kernel, ix, iy, kernel_func);
00491                 }
00492             if (hawki_mask_convolve(object_mask, kernel) != CPL_ERROR_NONE) {
00493                 cpl_mask_delete(object_mask) ;
00494                 cpl_matrix_delete(kernel) ;
00495                 return NULL;
00496             }
00497             cpl_matrix_delete(kernel);
00498         }
00499     
00500         /* Put the mask and the chip image in the imagelist */
00501         mask_image[idet] =  cpl_image_new_from_mask(object_mask);
00502         comb_image[idet] =  chip_image;
00503         
00504         /* Labelise the different detected apertures */
00505         cpl_msg_info(__func__, "Labelise mask") ;
00506         labels = cpl_image_labelise_mask_create(object_mask, &nobj);
00507         if (labels == NULL) 
00508         {
00509             int jdet;
00510             cpl_free(apertures);
00511             cpl_mask_delete(object_mask);
00512             for (jdet=0 ; jdet<idet + 1 ; jdet++)
00513             {
00514                 cpl_image_delete(mask_image[jdet]);
00515                 cpl_image_delete(comb_image[jdet]);
00516             }
00517         }
00518         cpl_msg_info(__func__, "Number of objects detected: %d", nobj) ;
00519 
00520         /* Create the detected apertures list */
00521         cpl_msg_info(__func__, "Create apertures") ;
00522         apertures[idet] = cpl_apertures_new_from_image(chip_image, labels);
00523         if (apertures[idet] == NULL)
00524         {
00525             int jdet;
00526             cpl_free(apertures);
00527             cpl_mask_delete(object_mask);
00528             for (jdet=0 ; jdet<idet + 1 ; jdet++)
00529             {
00530                 cpl_image_delete(mask_image[jdet]);
00531                 cpl_image_delete(comb_image[jdet]);
00532             }
00533             return NULL;
00534         }
00535         
00536         /* Free */
00537         cpl_mask_delete(object_mask);
00538         cpl_image_delete(labels);
00539         cpl_msg_indent_less();
00540     }
00541 
00542     /* Free and return */
00543     cpl_msg_indent_less();
00544     return apertures;
00545 }
00546 
00547 /*----------------------------------------------------------------------------*/
00554 /*----------------------------------------------------------------------------*/
00555 static int hawki_step_detect_obj_aper_params
00556 (cpl_image      **  combined_images, 
00557  cpl_apertures  **  apertures,
00558  cpl_table      **  obj_charac)
00559 {
00560     int                 nb_objs ;
00561     double              angle ;
00562     double          *   fwhms_x ;
00563     double          *   fwhms_y ;
00564     cpl_bivector    *   iqe ;
00565     int                 nb_good ;
00566     cpl_vector      *   fwhms_good ;
00567     double          *   fwhms_good_data ;
00568     double              f_min, f_max, fr, fx, fy ;
00569     int                 chip;
00570     int                 iobj;
00571     int                 j;
00572     
00573     /* Initialise */
00574     double              seeing_min_arcsec = 0.1 ;
00575     double              seeing_max_arcsec = 5.0 ;
00576     double              seeing_fwhm_var   = 0.2 ;
00577 
00578     /* Check entries */
00579     if (combined_images  == NULL) return -1 ;
00580     if (obj_charac       == NULL) return -1 ;
00581 
00582     /* Loop on the HAWK-I detectors */
00583     cpl_msg_indent_more();
00584     for (chip=0 ; chip<HAWKI_NB_DETECTORS ; chip++) 
00585     {
00586 
00587         /* Number of detected objects */
00588         nb_objs = cpl_apertures_get_size(apertures[chip]);
00589         cpl_msg_info(__func__, "%d objects detected on chip %d",nb_objs,chip+1);
00590         hawki_step_detect_obj_output.nbobjs[chip] = nb_objs ;
00591         fwhms_x = cpl_malloc(nb_objs * sizeof(double)) ;
00592         fwhms_y = cpl_malloc(nb_objs * sizeof(double)) ;
00593         
00594         /* Initialize the output table */
00595         cpl_table_set_size(obj_charac[chip], nb_objs);
00596         cpl_table_new_column
00597             (obj_charac[chip], HAWKI_COL_OBJ_POSX, CPL_TYPE_DOUBLE) ;
00598         cpl_table_new_column
00599             (obj_charac[chip], HAWKI_COL_OBJ_POSY, CPL_TYPE_DOUBLE) ;
00600         cpl_table_new_column
00601             (obj_charac[chip], HAWKI_COL_OBJ_ANGLE, CPL_TYPE_DOUBLE) ;
00602         cpl_table_new_column
00603             (obj_charac[chip], HAWKI_COL_OBJ_FWHM_MAJAX, CPL_TYPE_DOUBLE) ;
00604         cpl_table_new_column
00605             (obj_charac[chip], HAWKI_COL_OBJ_FWHM_MINAX, CPL_TYPE_DOUBLE) ;
00606         cpl_table_new_column
00607             (obj_charac[chip], HAWKI_COL_OBJ_ELLIP, CPL_TYPE_DOUBLE);
00608         cpl_table_new_column
00609             (obj_charac[chip], HAWKI_COL_OBJ_FLUX, CPL_TYPE_DOUBLE);
00610         for (iobj=0 ; iobj<nb_objs ; iobj++)
00611         {
00612             /* Fill with the already known information */
00613             cpl_table_set_double(obj_charac[chip], HAWKI_COL_OBJ_POSX, iobj, 
00614                                  cpl_apertures_get_centroid_x(apertures[chip],
00615                                                               iobj+1));
00616             cpl_table_set_double(obj_charac[chip], HAWKI_COL_OBJ_POSY, iobj, 
00617                                  cpl_apertures_get_centroid_y(apertures[chip],
00618                                                               iobj+1));
00619             cpl_table_set_double(obj_charac[chip], HAWKI_COL_OBJ_FLUX, iobj, 
00620                                  cpl_apertures_get_flux(apertures[chip],
00621                                                         iobj+1));
00622             /* Compute the FWHM informations */
00623             iqe = cpl_image_iqe(combined_images[chip], 
00624                 (int)cpl_apertures_get_centroid_x(apertures[chip], iobj+1)- 10,
00625                 (int)cpl_apertures_get_centroid_y(apertures[chip], iobj+1)- 10,
00626                 (int)cpl_apertures_get_centroid_x(apertures[chip], iobj+1)+ 10,
00627                 (int)cpl_apertures_get_centroid_y(apertures[chip], iobj+1)+ 10);
00628             if (iqe == NULL)
00629             {
00630                 cpl_error_reset() ;
00631                 cpl_msg_debug(__func__, "Cannot get FWHM for obj at pos %g %g",
00632                               cpl_apertures_get_centroid_x(apertures[chip],
00633                                                            iobj+1),
00634                               cpl_apertures_get_centroid_y(apertures[chip],
00635                                                            iobj+1)) ;
00636                 fwhms_x[iobj] = -1.0 ;
00637                 fwhms_y[iobj] = -1.0 ;
00638                 angle = 0.0 ;
00639             }
00640             else 
00641             {
00642                 fwhms_x[iobj] = cpl_vector_get(cpl_bivector_get_x(iqe), 2) ;
00643                 fwhms_y[iobj] = cpl_vector_get(cpl_bivector_get_x(iqe), 3) ;
00644                 angle = cpl_vector_get(cpl_bivector_get_x(iqe), 4) ;
00645                 cpl_bivector_delete(iqe) ;
00646                 cpl_msg_debug(__func__,
00647                               "FWHM for obj at pos %g %g: %g x %g (%g)",
00648                               cpl_apertures_get_centroid_x(apertures[chip],
00649                                                            iobj+1),
00650                               cpl_apertures_get_centroid_y(apertures[chip],
00651                                                            iobj+1),
00652                               fwhms_x[iobj], fwhms_y[iobj], angle) ;
00653             }
00654             cpl_table_set_double
00655                 (obj_charac[chip], HAWKI_COL_OBJ_ANGLE, iobj, angle) ;
00656             cpl_table_set_double
00657                 (obj_charac[chip], HAWKI_COL_OBJ_FWHM_MAJAX, iobj,
00658                  fwhms_x[iobj]);
00659             cpl_table_set_double
00660                 (obj_charac[chip], HAWKI_COL_OBJ_FWHM_MINAX, iobj,
00661                  fwhms_y[iobj]);
00662             cpl_table_set_double
00663                 (obj_charac[chip], HAWKI_COL_OBJ_ELLIP, iobj,
00664                  1 - fwhms_y[iobj] / fwhms_x[iobj]);
00665         }
00666 
00667         /* Get the number of good values */
00668         nb_good = 0 ;
00669         for (iobj=0 ; iobj<nb_objs ; iobj++) 
00670         {
00671             if ((fwhms_x[iobj] > 0.0) && (fwhms_y[iobj] > 0.0)) nb_good++ ;
00672         }
00673         if (nb_good == 0) 
00674         {
00675             cpl_msg_warning
00676                 (__func__, "No objects to compute mean FWHM on chip %d",chip+1);
00677             cpl_free(fwhms_x) ;
00678             cpl_free(fwhms_y) ;
00679             continue;
00680         }
00681     
00682         /* Get the good values */
00683         fwhms_good = cpl_vector_new(nb_good) ;
00684         fwhms_good_data = cpl_vector_get_data(fwhms_good) ;
00685         j=0 ;
00686         for (iobj=0 ; iobj<nb_objs ; iobj++) 
00687         {
00688             if ((fwhms_x[iobj] > 0.0) && (fwhms_y[iobj] > 0.0)) 
00689             {
00690                 fwhms_good_data[j] = (fwhms_x[iobj]+fwhms_y[iobj])/2.0 ;
00691                 j++ ;
00692             }
00693         }
00694    
00695         /* Compute the fwhm */
00696         if (nb_good < 3) 
00697         {
00698             /* Too few values to compute the median */
00699             hawki_step_detect_obj_output.fwhm_pix[chip] = fwhms_good_data[0] ;
00700             cpl_msg_warning
00701                 (__func__, "Fewer than 3 objects, using the first object FWHM");
00702         } 
00703         else 
00704         {
00705             /* Compute the median */
00706             hawki_step_detect_obj_output.fwhm_pix[chip] =
00707                 cpl_vector_get_median_const(fwhms_good);
00708         }
00709         hawki_step_detect_obj_output.fwhm_arcsec[chip] = 
00710             hawki_step_detect_obj_output.fwhm_pix[chip] *
00711                 hawki_step_detect_obj_output.pixscale ;
00712 
00713         /* Compute the mode of the FWHMs */
00714         if (nb_good > 5)
00715         {
00716             hawki_step_detect_obj_output.fwhm_mode[chip] =
00717                 hawki_vector_get_mode(fwhms_good);
00718             hawki_step_detect_obj_output.fwhm_mode[chip] *= 
00719                 hawki_step_detect_obj_output.pixscale;
00720         }
00721         cpl_vector_delete(fwhms_good);
00722     
00723         /* IQ is the median of the (fwhm_x+fwhm_y/2) of the good stars */
00724         /* Compute f_min and f_max */
00725         f_min = seeing_min_arcsec / hawki_step_detect_obj_output.pixscale;
00726         f_max = seeing_max_arcsec / hawki_step_detect_obj_output.pixscale; 
00727 
00728         /* Get the number of good values */
00729         nb_good = 0 ;
00730         for (iobj=0 ; iobj<nb_objs ; iobj++) 
00731         {
00732             fx = fwhms_x[iobj] ;
00733             fy = fwhms_y[iobj] ;
00734             fr = 2.0 * fabs(fx-fy) / (fx+fy) ;
00735             if ((fx > f_min) && (fx < f_max) && (fy > f_min) && (fy < f_max) &&
00736                     (fr < seeing_fwhm_var)) nb_good++ ;
00737         }
00738         if (nb_good == 0) 
00739         {
00740             cpl_msg_warning(__func__, "No objects to compute IQ on chip %d",
00741                             chip+1);
00742             cpl_free(fwhms_x) ;
00743             cpl_free(fwhms_y) ;
00744             continue;
00745         }
00746 
00747         /* Get the good values */
00748         fwhms_good = cpl_vector_new(nb_good) ;
00749         fwhms_good_data = cpl_vector_get_data(fwhms_good) ;
00750         j=0 ;
00751         for (iobj=0 ; iobj<nb_objs ; iobj++) 
00752         {
00753             fx = fwhms_x[iobj] ;
00754             fy = fwhms_y[iobj] ;
00755             fr = 2.0 * fabs(fx-fy) / (fx+fy) ;
00756             if ((fx > f_min) && (fx < f_max) && (fy > f_min) && (fy < f_max) &&
00757                     (fr < seeing_fwhm_var)) 
00758             {
00759                 fwhms_good_data[j] = (fx + fy)/2.0 ;
00760                 j++ ;
00761             }
00762         }
00763         cpl_free(fwhms_x) ;
00764         cpl_free(fwhms_y) ;
00765     
00766         /* Compute the fwhm */
00767         if (nb_good < 3) 
00768         {
00769             /* Too few values to compute the median */
00770             hawki_step_detect_obj_output.iq[chip] = fwhms_good_data[0] ;
00771         }
00772         else 
00773         {
00774             /* Compute the median */
00775             hawki_step_detect_obj_output.iq[chip] = 
00776                 cpl_vector_get_median_const(fwhms_good) ;
00777         }
00778         cpl_vector_delete(fwhms_good);
00779         hawki_step_detect_obj_output.iq[chip] *= 
00780             hawki_step_detect_obj_output.pixscale;
00781     }
00782     cpl_msg_indent_less();
00783     
00784     return 0;
00785 }
00786         
00787         
00788 /*----------------------------------------------------------------------------*/
00797 /*----------------------------------------------------------------------------*/
00798 static int hawki_step_detect_obj_save
00799 (cpl_image           **  mask_images,
00800  cpl_table           **  obj_charac,
00801  cpl_propertylist    **  obj_stats,
00802  cpl_parameterlist   *   parlist,
00803  cpl_frameset        *   framelist)
00804 {
00805     const cpl_frame     *   ref_frame ;
00806     cpl_propertylist    **  qclists;
00807     int                     ext_nb ;
00808     const char          *   recipe_name = "hawki_step_detect_obj" ;
00809     int                     i;
00810     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00811     
00812 
00813 
00814     /* Load the WCS keys */
00815     ref_frame = irplib_frameset_get_first_from_group
00816         (framelist, CPL_FRAME_GROUP_RAW);
00817 
00818     /* Create the QC lists */
00819     qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00820     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++)
00821     {
00822         cpl_propertylist    *   inputlist;
00823         cpl_propertylist    *   offsetlist;
00824         cpl_propertylist    *   wcslist;
00825 
00826         /* Get the extension number */
00827         ext_nb=hawki_get_ext_from_detector(cpl_frame_get_filename(ref_frame), i+1);
00828         qclists[i] = cpl_propertylist_new() ;
00829 
00830         /* Fill the QC */
00831         cpl_propertylist_append_int
00832             (qclists[i], "ESO QC NBOBJS", 
00833              hawki_step_detect_obj_output.nbobjs[i]);
00834         cpl_propertylist_append_double
00835             (qclists[i], "ESO QC IQ", hawki_step_detect_obj_output.iq[i]);
00836         cpl_propertylist_append_double
00837             (qclists[i], "ESO QC FWHM PIX",
00838              hawki_step_detect_obj_output.fwhm_pix[i]);
00839         cpl_propertylist_append_double
00840             (qclists[i], "ESO QC FWHM ARCSEC",
00841              hawki_step_detect_obj_output.fwhm_arcsec[i]);
00842         cpl_propertylist_append_double
00843             (qclists[i], "ESO QC FWHM MODE",
00844              hawki_step_detect_obj_output.fwhm_mode[i]);
00845 
00846         /* Propagate some keywords from input raw frame extensions */
00847         inputlist = cpl_propertylist_load_regexp(
00848                 cpl_frame_get_filename(ref_frame), ext_nb,
00849                 HAWKI_HEADER_EXT_FORWARD, 0);
00850         offsetlist = cpl_propertylist_load_regexp(
00851                 cpl_frame_get_filename(ref_frame), ext_nb,
00852                 HAWKI_HEADER_COMB_OFFSETS, 0);
00853         wcslist = cpl_propertylist_load_regexp(
00854                 cpl_frame_get_filename(ref_frame), ext_nb,
00855                 HAWKI_HEADER_WCS, 0);
00856         cpl_propertylist_append(qclists[i], inputlist);
00857         cpl_propertylist_append(qclists[i], offsetlist);
00858         cpl_propertylist_append(qclists[i], wcslist);
00859         cpl_propertylist_delete(inputlist);
00860         cpl_propertylist_delete(offsetlist);
00861         cpl_propertylist_delete(wcslist);
00862         
00863         /* Add the object statistics keywords */
00864         cpl_propertylist_append(qclists[i], obj_stats[i]);
00865     }
00866 
00867 
00868     /* Write the object mask */
00869     hawki_images_save(framelist,
00870                       parlist,
00871                       framelist, 
00872                       (const cpl_image**)mask_images, 
00873                       recipe_name,
00874                       HAWKI_CALPRO_OBJ_MASK, 
00875                       HAWKI_PROTYPE_OBJ_MASK,
00876                       NULL,
00877                       (const cpl_propertylist**)qclists,
00878                       "hawki_step_detect_obj_mask.fits") ;
00879 
00880     /* Write the FITS table with the objects statistics */
00881     hawki_tables_save(framelist,
00882                       parlist,
00883                       framelist,    
00884                       (const cpl_table **)obj_charac,
00885                       recipe_name,
00886                       HAWKI_CALPRO_OBJ_PARAM,
00887                       HAWKI_PROTYPE_OBJ_PARAM,
00888                       NULL,
00889                       (const cpl_propertylist**)qclists,
00890                       "hawki_step_detect_obj_stars.fits") ;
00891 
00892 
00893     /* Free and return */
00894     for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
00895         cpl_propertylist_delete(qclists[i]) ;
00896     }
00897     cpl_free(qclists) ;
00898     if(!cpl_errorstate_is_equal(error_prevstate))
00899     {
00900         cpl_errorstate_set(CPL_ERROR_NONE);
00901         return -1;
00902     }
00903     return  0;
00904 }
00905 
00906 static void hawki_step_detect_obj_init_output(void)
00907 {
00908     int    idet;
00909     
00910     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00911     {
00912         hawki_step_detect_obj_output.iq[idet] = -1.0 ;
00913         hawki_step_detect_obj_output.nbobjs[idet] = -1 ;
00914         hawki_step_detect_obj_output.fwhm_pix[idet] = -1.0 ;
00915         hawki_step_detect_obj_output.fwhm_arcsec[idet] = -1.0 ;
00916         hawki_step_detect_obj_output.fwhm_mode[idet] = -1.0 ;
00917         hawki_step_detect_obj_output.pos_x[idet] = -1.0 ;
00918         hawki_step_detect_obj_output.pos_y[idet] = -1.0 ;
00919     }
00920     hawki_step_detect_obj_output.pixscale = -1.0;
00921 }
00922 
00923 static void hawki_step_detect_obj_get_pscale
00924 (cpl_frameset * combframes)
00925 {
00926     cpl_propertylist  * plist;
00927     cpl_frame         * firstframe;
00928     cpl_errorstate      error_prevstate = cpl_errorstate_get();
00929     
00930     /* Get the header infos */
00931     firstframe = cpl_frameset_get_frame(combframes, 0) ;
00932     plist=cpl_propertylist_load(cpl_frame_get_filename(firstframe), 0) ;
00933     hawki_step_detect_obj_output.pixscale = hawki_pfits_get_pixscale(plist);
00934     cpl_propertylist_delete(plist) ;
00935     if(!cpl_errorstate_is_equal(error_prevstate))
00936     {
00937         cpl_msg_error(__func__, "Missing PIXSCALE keyword in FITS header") ;
00938         cpl_errorstate_set(CPL_ERROR_NONE);
00939         return;
00940     }
00941 }
00942 

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