hawki_step_refine_offsets.c

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

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