hawki_step_refine_offsets.c

00001 /* $Id: hawki_step_refine_offsets.c,v 1.14 2010/11/30 20:50:08 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/11/30 20:50:08 $
00024  * $Revision: 1.14 $
00025  * $Name: hawki-1_8_0 $
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_calib.h"
00045 #include "hawki_load.h"
00046 #include "hawki_save.h"
00047 #include "hawki_pfits.h"
00048 #include "hawki_dfs.h"
00049 #include "hawki_saa.h"
00050 #include "hawki_bkg.h"
00051 #include "hawki_distortion.h"
00052 #include "hawki_properties_tel.h"
00053 #include "hawki_image_stats.h"
00054 
00055 /*-----------------------------------------------------------------------------
00056                                 Define
00057  -----------------------------------------------------------------------------*/
00058 
00059 #define NEGLIG_OFF_DIFF     0.1
00060 #define SQR(x) ((x)*(x))
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Functions prototypes
00064  -----------------------------------------------------------------------------*/
00065 
00066 static int hawki_step_refine_offsets_create(cpl_plugin *) ;
00067 static int hawki_step_refine_offsets_exec(cpl_plugin *) ;
00068 static int hawki_step_refine_offsets_destroy(cpl_plugin *) ;
00069 static int hawki_step_refine_offsets(cpl_parameterlist *, cpl_frameset *) ;
00070 
00071 static int hawki_step_refine_offsets_retrieve_input_param
00072 (cpl_parameterlist  *  parlist);
00073 static int hawki_step_refine_offsets_fine
00074 (const cpl_frameset  *  science_objects_frames,
00075  const cpl_frameset  *  reference_objects_frames,
00076  cpl_bivector        ** refined_offsets,
00077  cpl_vector          ** correl);
00078 
00079 static int hawki_step_refine_offsets_save
00080 (cpl_bivector      ** refined_offsets,
00081  cpl_vector        ** correlations,
00082  cpl_parameterlist *  recipe_parlist,
00083  cpl_frameset      *  recipe_frameset);
00084 
00085 static cpl_bivector ** hawki_step_refine_offsets_read_select_objects
00086 (const cpl_frameset *  reference_obj_frames,
00087  double                first_image_off_x,
00088  double                first_image_off_y,
00089  int                   nx,
00090  int                   ny);
00091 
00092 /*-----------------------------------------------------------------------------
00093                             Static variables
00094  -----------------------------------------------------------------------------*/
00095 
00096 static struct 
00097 {
00098     /* Inputs */
00099     int                 nbrightest;
00100     int                 sx ;
00101     int                 sy ;
00102     int                 mx ;
00103     int                 my ;
00104 } hawki_step_refine_offsets_config;
00105 
00106 static char hawki_step_refine_offsets_description[] =
00107 "hawki_step_refine_offsets -- utility to refine the nominal offsets.\n"
00108 "This utility will take the offsets in the header as a first approach\n"
00109 "and tries to refine them correlating the input images at the points\n"
00110 "given by the detected objects.\n"
00111 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00112 "images.fits "HAWKI_CALPRO_DIST_CORRECTED" or\n"
00113 "images.fits "HAWKI_CALPRO_BKG_SUBTRACTED" and\n"
00114 "det_obj_stats.fits "HAWKI_CALPRO_OBJ_PARAM"\n" ;
00115 
00116 /*-----------------------------------------------------------------------------
00117                                 Functions code
00118  -----------------------------------------------------------------------------*/
00119 
00120 /*----------------------------------------------------------------------------*/
00128 /*----------------------------------------------------------------------------*/
00129 int cpl_plugin_get_info(cpl_pluginlist * list)
00130 {
00131     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00132     cpl_plugin  *   plugin = &recipe->interface ;
00133 
00134     cpl_plugin_init(plugin,
00135                     CPL_PLUGIN_API,
00136                     HAWKI_BINARY_VERSION,
00137                     CPL_PLUGIN_TYPE_RECIPE,
00138                     "hawki_step_refine_offsets",
00139                     "Jitter recipe",
00140                     hawki_step_refine_offsets_description,
00141                     "Cesar Enrique Garcia Dabo",
00142                     PACKAGE_BUGREPORT,  
00143                     hawki_get_license(),
00144                     hawki_step_refine_offsets_create,
00145                     hawki_step_refine_offsets_exec,
00146                     hawki_step_refine_offsets_destroy) ;
00147 
00148     cpl_pluginlist_append(list, plugin) ;
00149     
00150     return 0;
00151 }
00152 
00153 /*----------------------------------------------------------------------------*/
00162 /*----------------------------------------------------------------------------*/
00163 static int hawki_step_refine_offsets_create(cpl_plugin * plugin)
00164 {
00165     cpl_recipe      * recipe ;
00166     cpl_parameter   * p ;
00167 
00168     /* Get the recipe out of the plugin */
00169     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00170         recipe = (cpl_recipe *)plugin ;
00171     else return -1 ;
00172 
00173     /* Create the parameters list in the cpl_recipe object */
00174     recipe->parameters = cpl_parameterlist_new() ;
00175 
00176     /* Fill the parameters list */
00177     /* --xcorr */
00178     p = cpl_parameter_new_value("hawki.hawki_step_refine_offsets.xcorr",
00179                                 CPL_TYPE_STRING,
00180                                 "Cross correlation search and measure sizes",
00181                                 "hawki.hawki_step_refine_offsets",
00182                                 "20,20,25,25") ;
00183     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xcorr") ;
00184     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00185     cpl_parameterlist_append(recipe->parameters, p) ;
00186 
00187     /* --nbrightest */
00188     p = cpl_parameter_new_value("hawki.hawki_step_refine_offsets.nbrightest",
00189                                 CPL_TYPE_INT,
00190                                 "Number of brightest objects to use",
00191                                 "hawki.hawki_step_refine_offsets",
00192                                 3);
00193     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "nbrightest") ;
00194     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00195     cpl_parameterlist_append(recipe->parameters, p) ;
00196 
00197     /* Return */
00198     return 0;
00199 }
00200 
00201 /*----------------------------------------------------------------------------*/
00207 /*----------------------------------------------------------------------------*/
00208 static int hawki_step_refine_offsets_exec(cpl_plugin * plugin)
00209 {
00210     cpl_recipe  *   recipe ;
00211 
00212     /* Get the recipe out of the plugin */
00213     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00214         recipe = (cpl_recipe *)plugin ;
00215     else return -1 ;
00216 
00217     /* Issue a banner */
00218     hawki_print_banner();
00219 
00220     return hawki_step_refine_offsets(recipe->parameters, recipe->frames) ;
00221 }
00222 
00223 /*----------------------------------------------------------------------------*/
00229 /*----------------------------------------------------------------------------*/
00230 static int hawki_step_refine_offsets_destroy(cpl_plugin * plugin)
00231 {
00232     cpl_recipe  *   recipe ;
00233 
00234     /* Get the recipe out of the plugin */
00235     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00236         recipe = (cpl_recipe *)plugin ;
00237     else return -1 ;
00238 
00239     cpl_parameterlist_delete(recipe->parameters) ;
00240     return 0 ;
00241 }
00242 
00243 /*----------------------------------------------------------------------------*/
00250 /*----------------------------------------------------------------------------*/
00251 static int hawki_step_refine_offsets(
00252         cpl_parameterlist   *   parlist, 
00253         cpl_frameset        *   framelist)
00254 {
00255     cpl_frameset    *   science_obj_frames ;
00256     cpl_frameset    *   reference_obj_frames ;
00257     cpl_bivector    **  refined_offsets;
00258     cpl_vector      **  correl;
00259     int                 idet;
00260 
00261     /* Retrieve input parameters */
00262     if(hawki_step_refine_offsets_retrieve_input_param(parlist))
00263     {
00264         cpl_msg_error(__func__, "Wrong parameters");
00265         return -1;
00266     }
00267 
00268     /* Identify the RAW and CALIB frames in the input frameset */
00269     if (hawki_dfs_set_groups(framelist)) {
00270         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00271         return -1 ;
00272     }
00273 
00274     /* Get the object images frames */
00275     cpl_msg_info(__func__, "Identifying input frames");
00276     science_obj_frames = 
00277         hawki_extract_frameset(framelist, HAWKI_CALPRO_DIST_CORRECTED);
00278     if (science_obj_frames == NULL)
00279     {
00280         science_obj_frames =
00281                 hawki_extract_frameset(framelist, HAWKI_CALPRO_BKG_SUBTRACTED);
00282         if (science_obj_frames == NULL)
00283         {
00284             cpl_msg_error(__func__, "No science object frames provided (%s)",
00285                     HAWKI_CALPRO_DIST_CORRECTED);
00286             return -1 ;
00287         }
00288     }
00289     
00290     /* Get the detected "objects" frame */
00291     reference_obj_frames = 
00292         hawki_extract_frameset(framelist, HAWKI_CALPRO_OBJ_PARAM);
00293     if(cpl_frameset_get_size(reference_obj_frames) != 1)
00294     {
00295         cpl_msg_error(__func__, "One object parameters frame must be provided (%s)",
00296                 HAWKI_CALPRO_OBJ_PARAM);
00297         cpl_frameset_delete(science_obj_frames) ;
00298         if(reference_obj_frames != NULL)
00299             cpl_frameset_delete(reference_obj_frames);
00300         return -1 ;
00301     }
00302     
00303     /* Get the offsets refinement */
00304     refined_offsets = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_bivector *));
00305     correl          = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_vector *));
00306     for(idet = 0 ; idet < HAWKI_NB_DETECTORS; ++idet)
00307     {
00308         refined_offsets[idet] = cpl_bivector_new
00309             (cpl_frameset_get_size(science_obj_frames));
00310         correl[idet] = cpl_vector_new
00311             (cpl_frameset_get_size(science_obj_frames));
00312     }
00313     if (hawki_step_refine_offsets_fine
00314             (science_obj_frames, reference_obj_frames, 
00315              refined_offsets, correl) == -1) 
00316    {
00317         cpl_msg_error(__func__, "Cannot refine the objects") ;
00318         cpl_frameset_delete(reference_obj_frames) ;
00319         cpl_frameset_delete(science_obj_frames) ;
00320         for(idet = 0 ; idet < HAWKI_NB_DETECTORS; ++idet)
00321         {
00322             cpl_bivector_delete(refined_offsets[idet]);
00323             cpl_vector_delete(correl[idet]);
00324         }
00325         cpl_free(refined_offsets);
00326         cpl_free(correl);
00327         return -1 ;
00328     }
00329 
00330     /* Save the products */
00331     cpl_msg_info(__func__, "Save the products") ;
00332     if (hawki_step_refine_offsets_save
00333             (refined_offsets, correl, parlist, framelist) == -1)
00334     {
00335         cpl_msg_warning(__func__,"Some data could not be saved. "
00336                                  "Check permisions or disk space");
00337         cpl_frameset_delete(science_obj_frames);
00338         cpl_frameset_delete(reference_obj_frames);
00339         for(idet = 0 ; idet < HAWKI_NB_DETECTORS; ++idet)
00340         {
00341             cpl_bivector_delete(refined_offsets[idet]);
00342             cpl_vector_delete(correl[idet]);
00343         }
00344         cpl_free(refined_offsets);
00345         cpl_free(correl);
00346         return -1 ;
00347     }
00348     
00349     /* Free and return */
00350     cpl_frameset_delete(science_obj_frames);
00351     cpl_frameset_delete(reference_obj_frames);
00352     for(idet = 0 ; idet < HAWKI_NB_DETECTORS; ++idet)
00353     {
00354         cpl_bivector_delete(refined_offsets[idet]);
00355         cpl_vector_delete(correl[idet]);
00356     }
00357     cpl_free(refined_offsets);
00358     cpl_free(correl);
00359     
00360     /* Return */
00361     if (cpl_error_get_code())
00362     {
00363         cpl_msg_error(__func__,
00364                       "HAWK-I pipeline could not recover from previous errors");
00365         return -1 ;
00366     }
00367     else return 0 ;
00368 }
00369 
00370 int hawki_step_refine_offsets_retrieve_input_param
00371 (cpl_parameterlist  *  parlist)
00372 {
00373     cpl_parameter   *   par ;
00374     const char      *   sval ;
00375     par = NULL ;
00376     par = cpl_parameterlist_find
00377         (parlist, "hawki.hawki_step_refine_offsets.nbrightest");
00378     hawki_step_refine_offsets_config.nbrightest =
00379         cpl_parameter_get_int(par);
00380     par = cpl_parameterlist_find
00381         (parlist, "hawki.hawki_step_refine_offsets.xcorr");
00382     sval = cpl_parameter_get_string(par);
00383     if (sscanf(sval, "%d,%d,%d,%d",
00384                &hawki_step_refine_offsets_config.sx,
00385                &hawki_step_refine_offsets_config.sy,
00386                &hawki_step_refine_offsets_config.mx,
00387                &hawki_step_refine_offsets_config.my)!=4)
00388     {
00389         return -1;
00390     }
00391     return 0;
00392 }
00393 
00394 
00395 
00396 /*----------------------------------------------------------------------------*/
00401 /*----------------------------------------------------------------------------*/
00402 static int hawki_step_refine_offsets_fine
00403 (const cpl_frameset  *  science_obj_frames,
00404  const cpl_frameset  *  reference_obj_frames,
00405  cpl_bivector        ** refined_offsets,
00406  cpl_vector          ** correl)
00407 {
00408     cpl_imagelist    *   in ;
00409     cpl_bivector     *   nominal_offsets ;
00410     cpl_bivector     **  reference_objects;
00411     double           *   offs_est_x ;
00412     double           *   offs_est_y ;
00413     double               off_0_x;
00414     double               off_0_y;
00415     double               max_x, max_y ;
00416     int                  idet;
00417     int                  ioff;
00418     cpl_propertylist *   header;
00419     int                  nx;
00420     int                  ny;
00421 
00422     /* Get the nominal offsets from the header */
00423     cpl_msg_info(__func__,"Getting the nominal offsets");
00424     nominal_offsets = hawki_get_header_tel_offsets(science_obj_frames); 
00425     if (nominal_offsets  == NULL) 
00426     {
00427         cpl_msg_error(__func__, "Cannot load the header offsets") ;
00428         return -1;
00429     }
00430     offs_est_x = cpl_bivector_get_x_data(nominal_offsets);
00431     offs_est_y = cpl_bivector_get_y_data(nominal_offsets);
00432     
00433     /* Print the header offsets */
00434     cpl_msg_indent_more();
00435     for (ioff=0 ; ioff<cpl_bivector_get_size(nominal_offsets) ; ioff++) 
00436     {
00437         cpl_msg_info(__func__, "Telescope offsets (Frame %d): %g %g", ioff+1,
00438                 offs_est_x[ioff], offs_est_y[ioff]) ;
00439     }
00440     cpl_msg_indent_less();
00441     
00442     /* Get the size of the detectors of the first extension */
00443     header = cpl_propertylist_load
00444         (cpl_frame_get_filename
00445                 (cpl_frameset_get_first_const(science_obj_frames)), 1);
00446     nx = hawki_pfits_get_naxis1(header);
00447     ny = hawki_pfits_get_naxis2(header);
00448     cpl_propertylist_delete(header);
00449 
00450     /* Get the first offset to all offsets */
00451     off_0_x = offs_est_x[0];
00452     off_0_y = offs_est_y[0];
00453 
00454     /* Get the objects (anchor points)*/
00455     /* They are already in the "first image" reference system */
00456     reference_objects = 
00457         hawki_step_refine_offsets_read_select_objects(reference_obj_frames,
00458                                                       off_0_x,
00459                                                       off_0_y,
00460                                                       nx,
00461                                                       ny);
00462     if(reference_objects == NULL)
00463     {
00464         cpl_msg_error(__func__,"Error reading the reference objects");
00465         cpl_bivector_delete(nominal_offsets);
00466         return -1;
00467     }
00468 
00469     /* Subtract the first offset to all offsets */
00470     max_x = max_y = 0.0 ;
00471     for (ioff=1 ; ioff<cpl_bivector_get_size(nominal_offsets) ; ioff++) 
00472     {
00473         offs_est_x[ioff] -= off_0_x;
00474         offs_est_y[ioff] -= off_0_y;
00475         if (fabs(offs_est_x[ioff]) > max_x) max_x = fabs(offs_est_x[ioff]) ;
00476         if (fabs(offs_est_y[ioff]) > max_y) max_y = fabs(offs_est_y[ioff]) ;
00477     }
00478     offs_est_x[0] = offs_est_y[0] = 0.00 ;
00479 
00480     /* Get the opposite offsets. This is to change from 
00481      * telescope convention to cpl convention */ 
00482     cpl_vector_multiply_scalar(cpl_bivector_get_x(nominal_offsets), -1.0);
00483     cpl_vector_multiply_scalar(cpl_bivector_get_y(nominal_offsets), -1.0);
00484 
00485     /* Loop on the detectors */
00486     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00487     {
00488         cpl_msg_info(__func__, "Working on detector number %d", idet+1) ;
00489         cpl_msg_indent_more();
00490         
00491         /* Load the input data */
00492         cpl_msg_info(__func__, "Loading the input data") ;
00493         cpl_msg_indent_more() ;
00494         if ((in = hawki_load_detector(science_obj_frames,
00495                                   idet+1, CPL_TYPE_FLOAT)) == NULL) 
00496         {
00497             cpl_msg_error(__func__, "Cannot load chip %d", idet+1) ;
00498             cpl_bivector_delete(nominal_offsets) ;
00499             for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
00500             {
00501                 cpl_bivector_delete(reference_objects[idet]);
00502             }
00503             cpl_free(reference_objects);
00504             cpl_msg_indent_less() ;
00505             cpl_msg_indent_less() ;
00506             return -1;
00507         }
00508 
00509         /* Get the refinement */
00510         cpl_msg_info(__func__, "Getting the refinement");
00511         cpl_msg_indent_more() ;
00512         if (hawki_geom_refine_images_offsets
00513                 (in, 
00514                  nominal_offsets,
00515                  reference_objects[idet],
00516                  hawki_step_refine_offsets_config.sx,
00517                  hawki_step_refine_offsets_config.sy,
00518                  hawki_step_refine_offsets_config.mx,
00519                  hawki_step_refine_offsets_config.my,
00520                  refined_offsets[idet],
00521                  correl[idet]) == -1)
00522         {
00523             cpl_msg_error(__func__, "Cannot apply the shift and add") ;
00524             cpl_imagelist_delete(in) ;
00525             cpl_bivector_delete(nominal_offsets) ;
00526             for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
00527                 cpl_bivector_delete(reference_objects[idet]);
00528             cpl_free(reference_objects);
00529             cpl_msg_indent_less() ;
00530             cpl_msg_indent_less() ;
00531             return -1;
00532         }
00533         
00534         /* Convert to "telescope criteria" */
00535         /* Also add the offset of the first image */
00536         cpl_vector_multiply_scalar
00537             (cpl_bivector_get_x(refined_offsets[idet]), -1.0);
00538         cpl_vector_multiply_scalar
00539             (cpl_bivector_get_y(refined_offsets[idet]), -1.0);
00540         cpl_vector_add_scalar
00541             (cpl_bivector_get_x(refined_offsets[idet]), off_0_x);
00542         cpl_vector_add_scalar
00543             (cpl_bivector_get_y(refined_offsets[idet]), off_0_y);
00544 
00545         /* Print the new offsets */
00546         for (ioff=0 ; ioff<cpl_bivector_get_size(refined_offsets[idet]); ioff++) 
00547         {
00548             cpl_msg_info(__func__,"Refined telescope offsets (Frame %d): %g %g",
00549                          ioff+1,
00550                          cpl_vector_get(cpl_bivector_get_x
00551                                         (refined_offsets[idet]), ioff),
00552                          cpl_vector_get(cpl_bivector_get_y
00553                                         (refined_offsets[idet]), ioff));
00554         }
00555         cpl_imagelist_delete(in) ;
00556         cpl_msg_indent_less() ;
00557         cpl_msg_indent_less() ;
00558     }
00559     
00560     /* Freeing */
00561     cpl_bivector_delete(nominal_offsets);
00562     for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
00563         cpl_bivector_delete(reference_objects[idet]);
00564     cpl_free(reference_objects);
00565 
00566     return 0;
00567 }
00568 
00569 /*----------------------------------------------------------------------------*/
00577 /*----------------------------------------------------------------------------*/
00578 
00579 static int hawki_step_refine_offsets_save
00580 (cpl_bivector      ** refined_offsets,
00581  cpl_vector        ** correlations,
00582  cpl_parameterlist *  recipe_parlist,
00583  cpl_frameset      *  recipe_frameset)
00584 {
00585     cpl_table        ** offset_tables;
00586     int                 ioff;
00587     int                 idet;
00588     int                 noff;
00589     const char       *  recipe_name = "hawki_step_refine_offsets";
00590     cpl_errorstate      error_prevstate = cpl_errorstate_get();
00591     
00592     
00593     /* Convert the offsets to a table */
00594     offset_tables = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00595     for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00596     {
00597         offset_tables[idet] = cpl_table_new
00598             (cpl_bivector_get_size(refined_offsets[idet]));
00599         cpl_table_new_column(offset_tables[idet], 
00600                              HAWKI_COL_OFFSET_X, CPL_TYPE_FLOAT);
00601         cpl_table_set_column_unit(offset_tables[idet],HAWKI_COL_OFFSET_X,"pix");
00602         cpl_table_new_column(offset_tables[idet], 
00603                              HAWKI_COL_OFFSET_Y, CPL_TYPE_FLOAT);
00604         cpl_table_set_column_unit(offset_tables[idet],HAWKI_COL_OFFSET_Y,"pix");
00605         cpl_table_new_column(offset_tables[idet], 
00606                              HAWKI_COL_CORRELATION, CPL_TYPE_FLOAT);
00607         noff = cpl_bivector_get_size(refined_offsets[idet]);
00608         for(ioff = 0; ioff < noff; ++ioff)
00609         {
00610             double xoffset, yoffset, corr;
00611             xoffset = cpl_vector_get
00612                 (cpl_bivector_get_x(refined_offsets[idet]), ioff);
00613             yoffset = cpl_vector_get
00614                 (cpl_bivector_get_y(refined_offsets[idet]), ioff);
00615             corr    = cpl_vector_get(correlations[idet], ioff);
00616             cpl_table_set
00617                 (offset_tables[idet], HAWKI_COL_OFFSET_X, ioff, xoffset);
00618             cpl_table_set
00619                 (offset_tables[idet], HAWKI_COL_OFFSET_Y, ioff, yoffset);
00620             cpl_table_set
00621                 (offset_tables[idet], HAWKI_COL_CORRELATION, ioff, corr);
00622         }
00623     }
00624     
00625     /* Write the table with the statistics */
00626     if (hawki_tables_save(recipe_frameset, 
00627                           recipe_parlist,
00628                           recipe_frameset,
00629                           (const cpl_table **)offset_tables,
00630                           recipe_name,
00631                           HAWKI_CALPRO_OFFSETS,
00632                           HAWKI_PROTYPE_OFFSETS,
00633                           NULL,
00634                           NULL,
00635                           "hawki_step_refine_offsets.fits") != CPL_ERROR_NONE) 
00636     {
00637         cpl_msg_error(__func__, "Cannot save the first extension table") ;
00638         for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00639             cpl_table_delete(offset_tables[idet]);
00640         cpl_free(offset_tables);
00641         return -1 ;
00642     }
00643 
00644     /* Free and return */
00645     for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00646         cpl_table_delete(offset_tables[idet]);
00647     cpl_free(offset_tables);
00648     if(!cpl_errorstate_is_equal(error_prevstate))
00649     {
00650         cpl_errorstate_set(CPL_ERROR_NONE);
00651         return -1;
00652     }
00653     return  0;
00654 }
00655 
00656 /*----------------------------------------------------------------------------*/
00671 /*----------------------------------------------------------------------------*/
00672 static cpl_bivector ** hawki_step_refine_offsets_read_select_objects
00673 (const cpl_frameset *  reference_obj_frames,
00674  double                first_image_off_x,
00675  double                first_image_off_y,
00676  int                   nx,
00677  int                   ny)
00678 {
00679     const cpl_frame        *  reference_obj_frame;
00680     cpl_table              ** obj_param;
00681     cpl_propertylist       *  sort_column;
00682     cpl_bivector           ** reference_objects;
00683     int                    idet;
00684     
00685     /* Get the objects */
00686     cpl_msg_info(__func__,"Getting the reference object positions");
00687     reference_obj_frame = cpl_frameset_get_first_const(reference_obj_frames);
00688     obj_param = hawki_load_tables(reference_obj_frame);
00689     if(obj_param == NULL)
00690     {
00691         cpl_msg_error(__func__,"Could not read the reference objects parameters");
00692         return NULL;
00693     }
00694     
00695     /* Create the sorting criteria: by flux */
00696     sort_column = cpl_propertylist_new();
00697     cpl_propertylist_append_bool(sort_column, HAWKI_COL_OBJ_FLUX, CPL_TRUE);
00698 
00699     /* Allocate partially the reference objects */
00700     reference_objects = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_bivector *));
00701     
00702     /* Loop on detectors */
00703     cpl_msg_indent_more();
00704     for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
00705     {
00706         cpl_propertylist * objects_plist;
00707         cpl_vector       * obj_x;
00708         cpl_vector       * obj_y;
00709         int                nobj;
00710         int                nselect;
00711         int                iobj;
00712         int                ext_nb;
00713         double             reference_offset_x;
00714         double             reference_offset_y;
00715         
00716         /* Get the global offset */
00717         /* This allows to know which is the reference point of the detected 
00718          * objects positions (which are referred in general to the combined image */
00719         ext_nb=hawki_get_ext_from_detector
00720             (cpl_frame_get_filename(reference_obj_frame), idet + 1);
00721         objects_plist = cpl_propertylist_load
00722              (cpl_frame_get_filename(reference_obj_frame), ext_nb);
00723         reference_offset_x =
00724             hawki_pfits_get_comb_cumoffsetx(objects_plist);
00725         reference_offset_y = 
00726             hawki_pfits_get_comb_cumoffsety(objects_plist);
00727         if(cpl_error_get_code() != CPL_ERROR_NONE)
00728         {
00729             cpl_msg_error(__func__,"Could not find keywords "
00730                           "ESO QC COMBINED CUMOFFSETX,Y in reference objects frame");            
00731             cpl_propertylist_delete(objects_plist);
00732             cpl_propertylist_delete(sort_column);
00733             return NULL;
00734         }
00735         cpl_msg_info(__func__,"Objects offsets wrt telescope: %f %f", 
00736                      reference_offset_x, reference_offset_y);
00737         cpl_propertylist_delete(objects_plist);
00738         
00739         /* Sort the table by flux */
00740         cpl_table_sort(obj_param[idet], sort_column);
00741         nobj = cpl_table_get_nrow(obj_param[idet]); 
00742         
00743         /* Allocate objects vector */
00744         reference_objects[idet] = cpl_bivector_new(nobj);
00745         obj_x = cpl_bivector_get_x(reference_objects[idet]);
00746         obj_y = cpl_bivector_get_y(reference_objects[idet]);
00747         cpl_msg_info(__func__, "Number of objects in chip %d: %d", idet+1,nobj);
00748         
00749         /* Keep only those objects within the first image */
00750         cpl_table_unselect_all(obj_param[idet]);
00751         for(iobj = 0 ; iobj < nobj; ++iobj)
00752         {
00753             double xpos_orig = cpl_table_get
00754                 (obj_param[idet], HAWKI_COL_OBJ_POSX, iobj, NULL);
00755             double ypos_orig = cpl_table_get
00756                 (obj_param[idet], HAWKI_COL_OBJ_POSY, iobj, NULL);
00757             double xpos_rel = xpos_orig - reference_offset_x + first_image_off_x;
00758             double ypos_rel = ypos_orig - reference_offset_y + first_image_off_y;
00759             if(xpos_rel < 0.0 || xpos_rel >= nx ||
00760                ypos_rel < 0.0 || ypos_rel >= ny)
00761             {
00762                 cpl_table_select_row(obj_param[idet], iobj);
00763             }
00764         }
00765         cpl_table_erase_selected(obj_param[idet]);
00766         nobj = cpl_table_get_nrow(obj_param[idet]);
00767         cpl_msg_info(__func__, "Number of objects within limits of detector "
00768                                "in chip %d: %d", idet+1,nobj);
00769         
00770         /* Apply the flux criteria */
00771         nselect = hawki_step_refine_offsets_config.nbrightest;
00772         if(nselect < 0 || nselect > nobj)
00773             nselect = nobj;
00774         cpl_msg_info(__func__, "Number of selected objects: %d", nselect);
00775         for(iobj = 0 ; iobj < nselect; ++iobj)
00776         {
00777             double xpos_orig = cpl_table_get
00778                 (obj_param[idet], HAWKI_COL_OBJ_POSX, iobj, NULL);
00779             double ypos_orig = cpl_table_get
00780                 (obj_param[idet], HAWKI_COL_OBJ_POSY, iobj, NULL);
00781 
00782             cpl_vector_set
00783                 (obj_x, iobj, xpos_orig - reference_offset_x + first_image_off_x);
00784             cpl_vector_set
00785                 (obj_y, iobj, ypos_orig - reference_offset_y + first_image_off_y);
00786             cpl_msg_debug(__func__,"Using anchor point at %f,%f",
00787                           cpl_vector_get(obj_x,iobj),
00788                           cpl_vector_get(obj_y,iobj));
00789             
00790         }
00791         cpl_vector_set_size(obj_x, nselect);
00792         cpl_vector_set_size(obj_y, nselect);
00793         
00794     }
00795     cpl_msg_indent_less();
00796     
00797     /* Freeing */
00798     for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++) 
00799         cpl_table_delete(obj_param[idet]);
00800     cpl_free(obj_param);
00801     cpl_propertylist_delete(sort_column);
00802     
00803     return reference_objects;
00804 }

Generated on Thu Feb 17 17:13:08 2011 for HAWKI Pipeline Reference Manual by  doxygen 1.4.7