HAWKI Pipeline Reference Manual 1.8.1

hawki_cal_zpoint.c

00001 /* $Id: hawki_cal_zpoint.c,v 1.26 2011/02/17 12:48:02 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: 2011/02/17 12:48:02 $
00024  * $Revision: 1.26 $
00025  * $Name: hawki-1_8_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_utils.h"
00041 #include "irplib_calib.h"
00042 #include "irplib_strehl.h"
00043 #include "irplib_stdstar.h"
00044 #include "irplib_cat.h"
00045 #include "irplib_wcs.h"
00046 
00047 #include "hawki_image_stats.h"
00048 #include "hawki_utils.h"
00049 #include "hawki_calib.h"
00050 #include "hawki_load.h"
00051 #include "hawki_save.h"
00052 #include "hawki_pfits.h"
00053 #include "hawki_dfs.h"
00054 #include "hawki_alloc.h"
00055 
00056 /*-----------------------------------------------------------------------------
00057                             Functions prototypes
00058  -----------------------------------------------------------------------------*/
00059 
00060 static int hawki_cal_zpoint_create(cpl_plugin *) ;
00061 static int hawki_cal_zpoint_exec(cpl_plugin *) ;
00062 static int hawki_cal_zpoint_destroy(cpl_plugin *) ;
00063 static int hawki_cal_zpoint(cpl_parameterlist *, cpl_frameset *) ;
00064 
00065 static void hawki_cal_zpoint_output_init(void);
00066 static int hawki_cal_zpoint_retrieve_input_param
00067 (cpl_parameterlist  *  parlist);
00068 static cpl_table ** hawki_cal_zpoint_reduce
00069 (cpl_frameset    *   set,
00070  const char      *   stdstars,
00071  const char      *   bpm,
00072  const char      *   flat,
00073  cpl_table       **  raw_zpoint_stats,
00074  int             *   labels,
00075  cpl_imagelist   **  images);
00076 static int hawki_cal_zpoint_save
00077 (cpl_table           **  zpoint_tables,
00078  int                 *   labels,
00079  cpl_imagelist       *   images,
00080  cpl_table           **  raw_zpoint_stats,
00081  cpl_frameset        *   zpoint_frames,
00082  cpl_frameset        *   calib_frames,
00083  const cpl_frame     *   stars_frame,
00084  cpl_parameterlist   *   parlist,
00085  cpl_frameset        *   set);
00086 static cpl_table ** hawki_cal_zpoint_photom
00087 (cpl_imagelist       *   ilist,
00088  cpl_bivector        *   pos,
00089  int                 *   labels);
00090 static int hawki_cal_zpoint_get_mag(const char *, double, double, hawki_band) ;
00091 static int hawki_cal_zpoint_compute_keywords
00092 (cpl_frameset * set, 
00093  int          * labels);
00094 static cpl_error_code  hawki_cal_zpoint_get_expected_pos
00095 (cpl_frameset * set,
00096  int          * labels);
00097 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist);
00098 
00099 /*-----------------------------------------------------------------------------
00100                             Static variables
00101  -----------------------------------------------------------------------------*/
00102 
00103 static struct {
00104     /* Inputs */
00105     double      xcoord[HAWKI_NB_DETECTORS] ;
00106     double      ycoord[HAWKI_NB_DETECTORS] ;
00107     double      target_ra;
00108     double      target_dec;
00109     double      magnitude;
00110     double      mag_H;
00111     double      mag_J;
00112     double      mag_K;
00113     double      mag_Y;
00114     double      detect_sigma ;
00115     int         sx ;
00116     int         sy ;
00117     double      phot_star_radius ;
00118     double      phot_bg_r1 ;
00119     double      phot_bg_r2 ;
00120 } hawki_cal_zpoint_config;
00121 
00122 static struct {
00123     /* Outputs */
00124     double      dit;
00125     double      pixscale;
00126     char        filter[512];
00127     hawki_band  band;
00128     char        starname[512];
00129     char        sptype[512];
00130     char        catalog[512];
00131     double      humidity;
00132     double      airmass[HAWKI_NB_DETECTORS];
00133     double      zpoint[HAWKI_NB_DETECTORS];
00134     double      atx0[HAWKI_NB_DETECTORS];
00135     double      posx[HAWKI_NB_DETECTORS];
00136     double      posy[HAWKI_NB_DETECTORS];
00137     double      flux[HAWKI_NB_DETECTORS];
00138     double      instrmag[HAWKI_NB_DETECTORS];
00139     double      peak[HAWKI_NB_DETECTORS];
00140     double      bgd[HAWKI_NB_DETECTORS];
00141     double      fwhmx[HAWKI_NB_DETECTORS];
00142     double      fwhmy[HAWKI_NB_DETECTORS];
00143     double      fwhm[HAWKI_NB_DETECTORS];
00144     double      fwhmx_as[HAWKI_NB_DETECTORS];
00145     double      fwhmy_as[HAWKI_NB_DETECTORS];
00146     double      fwhm_as[HAWKI_NB_DETECTORS];
00147     double      mean_zpoint;
00148     double      mean_atx0;
00149     double      mean_airmass;
00150     double      stdstar_ra;
00151     double      stdstar_dec;
00152 } hawki_cal_zpoint_outputs;
00153 
00154 static char hawki_cal_zpoint_description[] =
00155 "hawki_cal_zpoint -- Zero point recipe\n"
00156 "The input of the recipe files listed in the Set Of Frames (sof-file)\n"
00157 "must be tagged as:\n"
00158 "raw-file.fits "HAWKI_CAL_ZPOINT_RAW" or\n"
00159 "stdstars-file.fits "HAWKI_CALPRO_STDSTARS" or\n"
00160 "flat-file.fits "HAWKI_CALPRO_FLAT" or\n"
00161 "bpm-file.fits "HAWKI_CALPRO_BPM"\n"
00162 "The recipe creates as an output:\n"
00163 "hawki_cal_zpoint.fits ("HAWKI_CALPRO_ZPOINT_TAB"): Zero point solution table\n"
00164 "hawki_cal_zpoint_check.fits ("HAWKI_CALPRO_ZPOINT_IMA"): Standard star images corrected (for checking purposes)\n"
00165 "hawki_cal_zpoint_stats.fits ("HAWKI_CALPRO_ZPOINT_STATS"): Statistics of the raw standard star images\n"
00166 "Return code:\n"
00167 "esorex exits with an error code of 0 if the recipe completes successfully\n"
00168 "or 1 otherwise";
00169 
00170 /*-----------------------------------------------------------------------------
00171                                 Functions code
00172  -----------------------------------------------------------------------------*/
00173 
00174 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 int cpl_plugin_get_info(cpl_pluginlist * list)
00184 {
00185     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00186     cpl_plugin  *   plugin = &recipe->interface ;
00187 
00188     cpl_plugin_init(plugin,
00189                     CPL_PLUGIN_API,
00190                     HAWKI_BINARY_VERSION,
00191                     CPL_PLUGIN_TYPE_RECIPE,
00192                     "hawki_cal_zpoint",
00193                     "Zero point computation recipe",
00194                     hawki_cal_zpoint_description,
00195                     "Cesar Enrique Garcia Dabo",
00196                     PACKAGE_BUGREPORT,
00197                     hawki_get_license(),
00198                     hawki_cal_zpoint_create,
00199                     hawki_cal_zpoint_exec,
00200                     hawki_cal_zpoint_destroy) ;
00201 
00202     cpl_pluginlist_append(list, plugin) ;
00203 
00204     return 0;
00205 }
00206 
00207 /*----------------------------------------------------------------------------*/
00216 /*----------------------------------------------------------------------------*/
00217 static int hawki_cal_zpoint_create(cpl_plugin * plugin)
00218 {
00219     cpl_recipe      * recipe ;
00220     cpl_parameter   * p ;
00221 
00222     /* Get the recipe out of the plugin */
00223     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00224         recipe = (cpl_recipe *)plugin ;
00225     else return -1 ;
00226 
00227     /* Create the parameters list in the cpl_recipe object */
00228     recipe->parameters = cpl_parameterlist_new() ;
00229 
00230     /* Fill the parameters list */
00231     /* --detect_sigma */
00232     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.detect_sigma",
00233             CPL_TYPE_DOUBLE, "the sigma value for object detection",
00234             "hawki.hawki_cal_zpoint", 7.0);
00235     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detect_sigma") ;
00236     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00237     cpl_parameterlist_append(recipe->parameters, p) ;
00238     /* --star_r */
00239     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.star_r",CPL_TYPE_DOUBLE,
00240             "the star radius", "hawki.hawki_cal_zpoint", -1.0) ;
00241     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "star_r") ;
00242     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00243     cpl_parameterlist_append(recipe->parameters, p) ;
00244     /* --bg_r1 */
00245     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.bg_r1", CPL_TYPE_DOUBLE,
00246             "the internal background radius", "hawki.hawki_cal_zpoint", -1.0) ;
00247     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r1") ;
00248     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00249     cpl_parameterlist_append(recipe->parameters, p) ;
00250     /* --bg_r2 */
00251     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.bg_r2", CPL_TYPE_DOUBLE,
00252             "the external background radius", "hawki.hawki_cal_zpoint", -1.0) ;
00253     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r2") ;
00254     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00255     cpl_parameterlist_append(recipe->parameters, p) ;
00256     /* --ra */
00257     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.ra", CPL_TYPE_DOUBLE,
00258             "RA in degrees", "hawki.hawki_cal_zpoint", 999.0) ;
00259     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ra") ;
00260     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00261     cpl_parameterlist_append(recipe->parameters, p) ;
00262     /* --dec */
00263     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.dec", CPL_TYPE_DOUBLE,
00264             "DEC in degrees", "hawki.hawki_cal_zpoint", 999.0) ;
00265     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dec") ;
00266     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00267     cpl_parameterlist_append(recipe->parameters, p) ;
00268     /* --mag */
00269     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.mag", CPL_TYPE_DOUBLE,
00270             "magnitude", "hawki.hawki_cal_zpoint", 99.0) ;
00271     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mag") ;
00272     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00273     cpl_parameterlist_append(recipe->parameters, p) ;
00274     /* --sx */
00275     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.sx", CPL_TYPE_INT,
00276             "x half-size of the search box", "hawki.hawki_cal_zpoint", 100) ;
00277     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sx") ;
00278     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00279     cpl_parameterlist_append(recipe->parameters, p) ;
00280     /* --sy */
00281     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.sy", CPL_TYPE_INT,
00282             "y half-size of the search box", "hawki.hawki_cal_zpoint", 100) ;
00283     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sy") ;
00284     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00285     cpl_parameterlist_append(recipe->parameters, p) ;
00286     /* --xcoord */
00287     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.xcoord", CPL_TYPE_STRING,
00288             "Coordinates in X where the standard star is located. If -1 use WCS",
00289             "hawki.hawki_cal_zpoint", "-1., -1., -1., -1.");
00290     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xcoord") ;
00291     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00292     cpl_parameterlist_append(recipe->parameters, p) ;
00293     /* --ycoord */
00294     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.ycoord", CPL_TYPE_STRING,
00295             "Coordinates in Y where the standard star is located. If -1 use WCS",
00296             "hawki.hawki_cal_zpoint", "-1., -1., -1., -1.") ;
00297     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ycoord") ;
00298     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00299     cpl_parameterlist_append(recipe->parameters, p) ;
00300 
00301     /* Return */
00302     return 0;
00303 }
00304 
00305 /*----------------------------------------------------------------------------*/
00311 /*----------------------------------------------------------------------------*/
00312 static int hawki_cal_zpoint_exec(cpl_plugin * plugin)
00313 {
00314     cpl_recipe  *   recipe ;
00315 
00316     /* Get the recipe out of the plugin */
00317     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00318         recipe = (cpl_recipe *)plugin ;
00319     else return -1 ;
00320 
00321     /* Issue a banner */
00322     hawki_print_banner();
00323 
00324     return hawki_cal_zpoint(recipe->parameters, recipe->frames) ;
00325 }
00326 
00327 /*----------------------------------------------------------------------------*/
00333 /*----------------------------------------------------------------------------*/
00334 static int hawki_cal_zpoint_destroy(cpl_plugin * plugin)
00335 {
00336     cpl_recipe  *   recipe ;
00337 
00338     /* Get the recipe out of the plugin */
00339     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00340         recipe = (cpl_recipe *)plugin ;
00341     else return -1 ;
00342 
00343     cpl_parameterlist_delete(recipe->parameters) ;
00344     return 0 ;
00345 }
00346 
00347 /*----------------------------------------------------------------------------*/
00354 /*----------------------------------------------------------------------------*/
00355 static int hawki_cal_zpoint(
00356         cpl_parameterlist   *   parlist,
00357         cpl_frameset        *   framelist)
00358 {
00359     cpl_parameter   *   par ;
00360     const char      *   flat ;
00361     const char      *   bpm ;
00362     const char      *   stdstars ;
00363     cpl_frameset    *   zpoint_frames ;
00364     cpl_frameset    *   calib_frames ;
00365     const cpl_frame *   stars_frame;
00366     cpl_table       **  raw_zpoint_stats;
00367     cpl_table       **  zpoint_tables;
00368     cpl_imagelist   *   std_star_images ;
00369     int             *   labels;
00370     int                 idet;
00371 
00372     /* Initialise Output */
00373     hawki_cal_zpoint_output_init();
00374     zpoint_frames = NULL ;
00375     par = NULL ;
00376 
00377     /* Retrieve input parameters */
00378     if(hawki_cal_zpoint_retrieve_input_param(parlist))
00379     {
00380         cpl_msg_error(__func__, "Wrong parameters");
00381         return -1;
00382     }
00383 
00384     /* Identify the RAW and CALIB frames in the input frameset */
00385     if (hawki_dfs_set_groups(framelist)) {
00386         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00387         return -1 ;
00388     }
00389 
00390     /* Retrieve calibration data */
00391     calib_frames = cpl_frameset_new();
00392     flat = hawki_extract_first_filename(framelist, HAWKI_CALPRO_FLAT) ;
00393     if(flat)
00394         cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
00395                 cpl_frameset_find_const(framelist, HAWKI_CALPRO_FLAT)));
00396     bpm = hawki_extract_first_filename(framelist, HAWKI_CALPRO_BPM) ;
00397     if(bpm)
00398         cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
00399                 cpl_frameset_find_const(framelist, HAWKI_CALPRO_BPM)));
00400 
00401     /* STD stars catalog requested */
00402     stars_frame = cpl_frameset_find_const(framelist, HAWKI_CALPRO_STDSTARS);
00403     if (stars_frame  == NULL) 
00404     {
00405         cpl_msg_error(__func__,"Cannot find the catalog in the input list (%s)",
00406                       HAWKI_CALPRO_STDSTARS);
00407         cpl_frameset_delete(calib_frames);
00408         return -1 ;
00409     }
00410     stdstars = cpl_frame_get_filename(stars_frame);
00411 
00412     /* Retrieve raw frames */
00413     if ((zpoint_frames = hawki_extract_frameset(framelist,
00414                     HAWKI_CAL_ZPOINT_RAW)) != NULL) {
00415     } else {
00416         cpl_msg_error(__func__, "Cannot find raw frames in the input list (%s)",
00417                       HAWKI_CAL_ZPOINT_RAW);
00418         cpl_frameset_delete(calib_frames);
00419         return -1 ;
00420     }
00421 
00422     /* Exactly 4 images are expected */
00423     if (cpl_frameset_get_size(zpoint_frames) != 4) {
00424         cpl_msg_error(__func__, "4 input raw frames are expected, not %d",
00425                 cpl_frameset_get_size(zpoint_frames)) ;
00426         cpl_frameset_delete(zpoint_frames) ;
00427         cpl_frameset_delete(calib_frames);
00428         return -1 ;
00429     }
00430 
00431     /* Create the statistics table */
00432     raw_zpoint_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00433     for(idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00434     {
00435         raw_zpoint_stats[idet] = cpl_table_new(
00436             cpl_frameset_get_size(zpoint_frames));
00437     }
00438     hawki_image_stats_initialize(raw_zpoint_stats);
00439 
00440     /* Compute the zpoint values */
00441     cpl_msg_info(__func__, "Reduce the data") ;
00442     cpl_msg_indent_more() ;
00443     labels = cpl_calloc(cpl_frameset_get_size(zpoint_frames), sizeof(int)) ;
00444     if ((zpoint_tables = hawki_cal_zpoint_reduce(zpoint_frames, stdstars, 
00445              bpm, flat, raw_zpoint_stats, labels, &std_star_images))==NULL)
00446     {
00447         cpl_msg_error(__func__, "Cannot reduce the data") ;
00448         cpl_frameset_delete(zpoint_frames) ;
00449         cpl_frameset_delete(calib_frames);
00450         hawki_table_delete(raw_zpoint_stats);
00451         cpl_free(labels);
00452         cpl_msg_indent_less() ;
00453         return -1 ;
00454     }
00455     cpl_msg_indent_less() ;
00456 
00457     /* Save the products */
00458     cpl_msg_info(__func__, "Save the products") ;
00459     cpl_msg_indent_more() ;
00460     if (hawki_cal_zpoint_save
00461             (zpoint_tables, labels, std_star_images, raw_zpoint_stats, 
00462              zpoint_frames, calib_frames, stars_frame,
00463              parlist, framelist) == -1)
00464     {
00465         cpl_msg_warning(__func__, "Data could not be saved. "
00466                         "Check permisions or disk space") ;
00467         cpl_frameset_delete(zpoint_frames);
00468         cpl_frameset_delete(calib_frames);
00469         hawki_table_delete(zpoint_tables) ;
00470         cpl_imagelist_delete(std_star_images) ;
00471         cpl_frameset_delete(calib_frames);
00472         hawki_table_delete(raw_zpoint_stats);
00473         cpl_free(labels);
00474         cpl_msg_indent_less() ;
00475         return -1 ;
00476     }
00477     cpl_msg_indent_less() ;
00478 
00479     /* Free and return */
00480     cpl_frameset_delete(zpoint_frames);
00481     cpl_frameset_delete(calib_frames);
00482     cpl_imagelist_delete(std_star_images);
00483     hawki_table_delete(zpoint_tables);
00484     hawki_table_delete(raw_zpoint_stats);
00485     cpl_free(labels);
00486 
00487     /* Return */
00488     if (cpl_error_get_code())
00489     {
00490         cpl_msg_error(__func__,
00491                       "HAWK-I pipeline could not recover from previous errors");
00492         return -1 ;
00493     }
00494     else return 0 ;
00495 }
00496 
00497 /*----------------------------------------------------------------------------*/
00507 /*----------------------------------------------------------------------------*/
00508 static cpl_table ** hawki_cal_zpoint_reduce
00509 (cpl_frameset    *   set,
00510  const char      *   stdstars,
00511  const char      *   bpm,
00512  const char      *   flat,
00513  cpl_table       **  raw_zpoint_stats,
00514  int             *   labels,
00515  cpl_imagelist   **  star_images)
00516 {
00517     cpl_frame           *   cur_frame ;
00518     cpl_propertylist    *   plist ;
00519     const char          *   sval ;
00520     cpl_imagelist       *   star_images_frame_order ;
00521     int                     nima ;
00522     cpl_bivector        *   positions ;
00523     cpl_image           *   filt_ima ;
00524     cpl_mask            *   kernel;
00525     int                     size_x, size_y ;
00526     double                  pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist ;
00527     cpl_apertures       *   aperts ;
00528     cpl_table           **  zpoint_tables;
00529     cpl_image           *   tmp_ima ;
00530     int                     iaper;
00531     int                     idet;
00532     int                     iframe;
00533     int                     iframe_star = -1;
00534     int                     nframes;
00535     cpl_errorstate          error_prevstate;
00536 
00537     /* Check inputs */
00538     if (set == NULL) return NULL ;
00539     if (stdstars == NULL) return NULL ;
00540     if (star_images == NULL) return NULL ;
00541 
00542     /* Initialize */
00543     *star_images = cpl_imagelist_new();
00544 
00545     /* Get the filter name, DIT, RA and DEC  */
00546     error_prevstate = cpl_errorstate_get();
00547     cur_frame = cpl_frameset_get_frame(set, 0) ;
00548     plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
00549     if ((sval = hawki_pfits_get_filter(plist)) == NULL) return NULL ;
00550     else sprintf(hawki_cal_zpoint_outputs.filter, sval) ;
00551     if (hawki_cal_zpoint_config.target_ra > 998.0)
00552     {
00553         hawki_cal_zpoint_config.target_ra = hawki_pfits_get_targ_alpha(plist);
00554         //hawki_cal_zpoint_config.target_ra = hawki_pfits_get_ra(plist) -
00555         //hawki_pfits_get_cumoffseta(plist) / 3600.;// Not valid before Nov 2008
00556         if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
00557         {
00558             cpl_propertylist_delete(plist);
00559             return NULL;
00560         }
00561     }
00562     if (hawki_cal_zpoint_config.target_dec > 998.0)
00563     {
00564         hawki_cal_zpoint_config.target_dec = hawki_pfits_get_targ_delta(plist);
00565         //hawki_cal_zpoint_config.target_dec = hawki_pfits_get_dec(plist) -
00566         //hawki_pfits_get_cumoffsetd(plist) / 3600.;// Not valid before Nov 2008
00567         if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
00568         {
00569             cpl_propertylist_delete(plist);
00570             return NULL;
00571         }
00572     }
00573     hawki_cal_zpoint_outputs.dit = hawki_pfits_get_dit(plist) ;
00574     hawki_cal_zpoint_outputs.pixscale = hawki_pfits_get_pixscale(plist) ;
00575     cpl_propertylist_delete(plist) ;
00576     if(!cpl_errorstate_is_equal(error_prevstate))
00577     {
00578         cpl_msg_error(__func__, "Cannot get some header informations") ;
00579         cpl_imagelist_delete(*star_images);
00580         return NULL ;
00581     }
00582     cpl_msg_info(__func__,"Searching catalog stars closest to target:");
00583     cpl_msg_indent_more();
00584     cpl_msg_info(__func__,"RA = %g ; DEC = %g",
00585                  hawki_cal_zpoint_config.target_ra,
00586                  hawki_cal_zpoint_config.target_dec);
00587     cpl_msg_info(__func__,"HAWK-I Filter: %s", hawki_cal_zpoint_outputs.filter);
00588     cpl_msg_indent_less();
00589 
00590     /* Get the band */
00591     if ((hawki_cal_zpoint_outputs.band =
00592                 hawki_get_band(hawki_cal_zpoint_outputs.filter)) ==
00593             HAWKI_BAND_UNKNOWN) {
00594         cpl_msg_error(__func__, "Cannot associate the filter %s to a band",
00595                 hawki_cal_zpoint_outputs.filter) ;
00596         cpl_imagelist_delete(*star_images);
00597         return NULL ;
00598     }
00599 
00600     /* Get the standard star information from database */
00601     if (hawki_cal_zpoint_config.magnitude > 98.0) {
00602         if (hawki_cal_zpoint_get_mag(stdstars, hawki_cal_zpoint_config.target_ra,
00603                                     hawki_cal_zpoint_config.target_dec,
00604                                     hawki_cal_zpoint_outputs.band) == -1)
00605         {
00606             cpl_msg_error(__func__,"Cannot get a suitable star from catalog");
00607             cpl_imagelist_delete(*star_images);
00608             return NULL ;
00609         }
00610     }
00611     cpl_msg_info(__func__, "Star magnitude in filter %s : %g",
00612             hawki_cal_zpoint_outputs.filter,
00613             hawki_cal_zpoint_config.magnitude) ;
00614     cpl_msg_info(__func__, "Star coordinates: RA = %g ; DEC = %g",
00615             hawki_cal_zpoint_outputs.stdstar_ra,
00616             hawki_cal_zpoint_outputs.stdstar_dec);
00617 
00618     /* Labelise frames */
00619     cpl_msg_info(__func__, "Guessing which frame the STD is in for each chip");
00620     hawki_detectors_locate_star
00621         (set, hawki_cal_zpoint_outputs.stdstar_ra,
00622          hawki_cal_zpoint_outputs.stdstar_dec, labels);
00623     if (labels == NULL)
00624     {
00625         cpl_msg_error(__func__, "Cannot determine which frame the STD is on") ;
00626         cpl_imagelist_delete(*star_images);
00627         return NULL ;
00628     }
00629 
00630     /* Compute the expected position of the star in pixels */
00631     /* This is stored in hawki_cal_zpoint_config.xcoord, ycoord */
00632     if(hawki_cal_zpoint_get_expected_pos(set, labels) != CPL_ERROR_NONE)
00633     {
00634         cpl_msg_error(__func__,"Could not determine where the star is located");
00635         cpl_imagelist_delete(*star_images);
00636         return NULL;
00637     }
00638     
00639     /* Fetch the airmass and humidity */
00640     hawki_cal_zpoint_compute_keywords(set, labels);
00641 
00642     /* Create the positions vector */
00643     nima = cpl_frameset_get_size(set) ;
00644     positions = cpl_bivector_new(nima) ;
00645 
00646     /* Loop on the detectors */
00647     nframes = cpl_frameset_get_size(set) ;
00648     cpl_msg_info(__func__,"Loop on the chips");
00649     cpl_msg_indent_more() ;
00650     for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00651     {
00652         cpl_imagelist * sky_images;
00653         cpl_image     * star_ima = NULL;
00654         cpl_image     * sky;
00655         cpl_image     * flat_im;
00656         int             ext_nb;
00657 
00658         cpl_msg_info(__func__, "Loading the chip %d", idet+1);
00659         cpl_msg_indent_more() ;
00660 
00661         /* Allocate */
00662         sky_images = cpl_imagelist_new();
00663 
00664         cpl_msg_indent_more() ;
00665         for (iframe=0 ; iframe<nframes ; iframe++)
00666         {
00667             cpl_image * ima_cur;
00668 
00669             /* Load the image */
00670             ima_cur = hawki_load_image(set, iframe, idet+1, CPL_TYPE_FLOAT) ;
00671             if(ima_cur == NULL)
00672             {
00673                 cpl_bivector_delete(positions) ;
00674                 cpl_imagelist_delete(*star_images);
00675                 cpl_imagelist_delete(sky_images) ;
00676                 cpl_msg_error(__func__, "Error reading image");
00677                 return NULL;
00678             }
00679             /* Get image statistics */
00680             size_x = cpl_image_get_size_x(ima_cur) ;
00681             size_y = cpl_image_get_size_y(ima_cur) ;
00682             if(hawki_image_stats_fill_from_image
00683                 (raw_zpoint_stats,
00684                  ima_cur,
00685                  1,
00686                  1,
00687                  size_x,
00688                  size_y,
00689                  idet,
00690                  iframe) !=0 )
00691             {
00692                 cpl_msg_error(__func__,"Cannot compute stats on ima %d det %d",
00693                               iframe+1, idet+1);
00694                 cpl_bivector_delete(positions) ;
00695                 cpl_imagelist_delete(*star_images);
00696                 cpl_imagelist_delete(sky_images) ;
00697                 cpl_image_delete(ima_cur);
00698                 cpl_msg_indent_less() ;
00699                 return NULL ;
00700             }
00701 
00702             /* Add the image to either the sky images or the star image */
00703             if(labels[iframe] == idet + 1)
00704             {
00705                 star_ima = ima_cur;
00706                 iframe_star = iframe;
00707             }
00708             else
00709                 cpl_imagelist_set(sky_images, ima_cur,
00710                                   cpl_imagelist_get_size(sky_images));
00711         }
00712         cpl_msg_indent_less();
00713 
00714         /* Create the sky */
00715         cpl_msg_info(__func__, "Correct for the sky");
00716         sky = cpl_imagelist_collapse_median_create(sky_images);
00717         cpl_imagelist_delete(sky_images) ;
00718 
00719         /* Subtract the sky */
00720         cpl_image_subtract(star_ima, sky) ;
00721         cpl_image_delete(sky) ;
00722 
00723         /* Divide by the flatfield if one is provided */
00724         if (flat) {
00725             cpl_msg_info(__func__, "Correct for the flat") ;
00726 
00727             /* Get the extension with the current chip */
00728             if ((ext_nb = hawki_get_ext_from_detector(flat, idet + 1)) == -1)
00729             {
00730                 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00731                               idet + 1);
00732                 cpl_imagelist_delete(*star_images) ;
00733                 cpl_bivector_delete(positions) ;
00734                 return NULL ;
00735             }
00736             /* Load */
00737             flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, ext_nb) ;
00738             cpl_image_divide(star_ima, flat_im) ;
00739             cpl_image_delete(flat_im) ;
00740         }
00741 
00742         /* Correct the bad pixels */
00743         if (bpm) {
00744             cpl_msg_info(__func__, "Correct for the bad pixels") ;
00745             if (hawki_bpm_calib(star_ima, bpm, idet + 1) == -1)
00746             {
00747                 cpl_msg_error(__func__, "Cannot correct the BPM for chip %d",
00748                               idet + 1);
00749                 cpl_imagelist_delete(*star_images) ;
00750                 cpl_bivector_delete(positions) ;
00751                 return NULL ;
00752             }
00753         }
00754 
00755         /* Put the result in the image list */
00756         cpl_imagelist_set(*star_images, star_ima,
00757                           cpl_imagelist_get_size(*star_images)) ;
00758 
00759         /* Object detection */
00760         cpl_msg_info(__func__,"For chip %d the STD is detected on frame %d",
00761                      idet + 1, iframe_star+1);
00762         pos_x_cen = pos_y_cen = -1.0 ;
00763         size_x = cpl_image_get_size_x(star_ima) ;
00764         size_y = cpl_image_get_size_y(star_ima) ;
00765 
00766         /* Filtering the image*/
00767         kernel = cpl_mask_new(3, 3);
00768         cpl_mask_not(kernel);
00769         filt_ima = cpl_image_new(cpl_image_get_size_x(star_ima),
00770                                  cpl_image_get_size_y(star_ima),
00771                                  cpl_image_get_type(star_ima));
00772         cpl_image_filter_mask(filt_ima, star_ima, kernel, CPL_FILTER_MEDIAN, 
00773                               CPL_BORDER_FILTER);
00774         cpl_mask_delete(kernel);
00775 
00776         /* Looking for apertures */
00777         aperts = cpl_apertures_extract_sigma(filt_ima,
00778                 hawki_cal_zpoint_config.detect_sigma) ;
00779         cpl_image_delete(filt_ima) ;
00780         if (aperts == NULL)
00781         {
00782             cpl_msg_error(__func__, "Cannot find the central object") ;
00783             cpl_imagelist_delete(*star_images) ;
00784             cpl_bivector_delete(positions) ;
00785             return NULL ;
00786         }
00787         min_dist = size_x * size_x + size_y * size_y ;
00788         for (iaper=0 ; iaper<cpl_apertures_get_size(aperts) ; iaper++) {
00789             pos_x = cpl_apertures_get_centroid_x(aperts, iaper+1) ;
00790             pos_y = cpl_apertures_get_centroid_y(aperts, iaper+1) ;
00791             dist = (pos_x-hawki_cal_zpoint_config.xcoord[idet])*
00792                     (pos_x-hawki_cal_zpoint_config.xcoord[idet]) +
00793                    (pos_y-hawki_cal_zpoint_config.ycoord[idet])*
00794                     (pos_y-hawki_cal_zpoint_config.ycoord[idet]);
00795             if (dist<min_dist) {
00796                 min_dist = dist ;
00797                 pos_x_cen = pos_x ;
00798                 pos_y_cen = pos_y ;
00799             }
00800         }
00801         cpl_apertures_delete(aperts) ;
00802 
00803         cpl_vector_set(cpl_bivector_get_x(positions), iframe_star, pos_x_cen) ;
00804         cpl_vector_set(cpl_bivector_get_y(positions), iframe_star, pos_y_cen) ;
00805         cpl_msg_info(__func__, "Expected star position: %g %g",
00806                 hawki_cal_zpoint_config.xcoord[idet],
00807                 hawki_cal_zpoint_config.ycoord[idet]);
00808         cpl_msg_info(__func__, "Bright object position: %g %g",
00809                 pos_x_cen, pos_y_cen) ;
00810 
00811         /* Check that the star is within the search window */
00812         if(fabs(pos_x_cen - hawki_cal_zpoint_config.xcoord[idet]) >
00813                 hawki_cal_zpoint_config.sx                        ||
00814            fabs(pos_y_cen - hawki_cal_zpoint_config.ycoord[idet]) >
00815                 hawki_cal_zpoint_config.sy)
00816         {
00817             cpl_msg_error(__func__,"No star found within search window "
00818                           "[%d, %d] of expected position",
00819                           hawki_cal_zpoint_config.sx,
00820                           hawki_cal_zpoint_config.sy);
00821 
00822             cpl_imagelist_delete(*star_images) ;
00823             cpl_bivector_delete(positions) ;
00824             return NULL ;
00825         }
00826 
00827         /* Free */
00828         cpl_msg_indent_less() ;
00829     }
00830     cpl_msg_indent_less() ;
00831 
00832     /* Reorder the images (frame order) */
00833     star_images_frame_order = cpl_imagelist_new() ;
00834     for (iframe=0 ; iframe< nframes; iframe++)
00835     {
00836         tmp_ima = cpl_image_duplicate
00837             (cpl_imagelist_get(*star_images, labels[iframe] -1 ));
00838         cpl_imagelist_set(star_images_frame_order, tmp_ima, iframe);
00839     }
00840 
00841     /* Compute the photometry */
00842     cpl_msg_info(__func__, "Compute the photometry") ;
00843     cpl_msg_indent_more() ;
00844     if ((zpoint_tables = hawki_cal_zpoint_photom
00845             (star_images_frame_order, positions, labels))==NULL) {
00846         cpl_msg_error(__func__, "Cannot reduce") ;
00847         cpl_bivector_delete(positions) ;
00848         cpl_imagelist_delete(star_images_frame_order) ;
00849         cpl_msg_indent_less() ;
00850         return NULL ;
00851     }
00852 
00853 
00854     /* Free and exit */
00855     cpl_msg_indent_less() ;
00856     cpl_bivector_delete(positions) ;
00857     cpl_imagelist_delete(star_images_frame_order) ;
00858 
00859     return zpoint_tables;
00860 }
00861 
00862 /*----------------------------------------------------------------------------*/
00872 /*----------------------------------------------------------------------------*/
00873 static cpl_table ** hawki_cal_zpoint_photom(
00874         cpl_imagelist       *   ilist,
00875         cpl_bivector        *   pos,
00876         int                 *   labels)
00877 {
00878     cpl_table **        zpoint;
00879     int                 nframes;
00880     double              r, r1, r2, mag, dit, extinction, pixscale ;
00881     cpl_image       *   ima ;
00882     double          *   pos_x ;
00883     double          *   pos_y ;
00884     double              bgd, fl, zp, peak, fwhm_x, fwhm_y ;
00885     cpl_bivector    *   iqe_res ;
00886     int                 iframe;
00887     int                 idet;
00888 
00889     /* Test entries */
00890     if (ilist == NULL) return NULL ;
00891     if (pos == NULL) return NULL ;
00892 
00893     /* Initialise */
00894     nframes = cpl_imagelist_get_size(ilist) ;
00895     mag = hawki_cal_zpoint_config.magnitude ;
00896     dit = hawki_cal_zpoint_outputs.dit ;
00897     pixscale = hawki_cal_zpoint_outputs.pixscale ;
00898 
00899     /* Get extinction */
00900     switch (hawki_cal_zpoint_outputs.band) {
00901         case HAWKI_BAND_J:      extinction = 0.098 ; break ;
00902         case HAWKI_BAND_H:      extinction = 0.039 ; break ;
00903         case HAWKI_BAND_K:      extinction = 0.065 ; break ;
00904         case HAWKI_BAND_Y:      extinction = 0.00 ; break ;
00905         default:                extinction = 0.00 ; break ;
00906     }
00907     cpl_msg_info(__func__,"Using tabulated extinction for band %s: %f",
00908                  hawki_cal_zpoint_outputs.filter, extinction);
00909 
00910     /* Loop on the images */
00911     for (iframe=0 ; iframe<nframes ; iframe++) {
00912         /* Get the current image */
00913         ima = cpl_imagelist_get(ilist, iframe) ;
00914 
00915         /* Get the current position */
00916         pos_x = cpl_bivector_get_x_data(pos) ;
00917         pos_y = cpl_bivector_get_y_data(pos) ;
00918 
00919         /* FWHM_X / FWHM_Y */
00920         iqe_res = cpl_image_iqe
00921             (ima, (int)(pos_x[iframe]-10.0), (int)(pos_y[iframe]-10.0),
00922              (int)(pos_x[iframe]+10.0), (int)(pos_y[iframe]+10.0));
00923         if (iqe_res == NULL)
00924         {
00925             cpl_msg_debug(__func__,"Cannot compute FWHM for chip %d",
00926                           labels[iframe]);
00927             fwhm_x = fwhm_y = -1.0 ;
00928             cpl_error_reset() ;
00929         } else {
00930             fwhm_x = cpl_vector_get(cpl_bivector_get_x(iqe_res), 2) ;
00931             fwhm_y = cpl_vector_get(cpl_bivector_get_x(iqe_res), 3) ;
00932             cpl_bivector_delete(iqe_res) ;
00933         }
00934 
00935         /* Determine the radii */
00936         r = hawki_cal_zpoint_config.phot_star_radius ;
00937         if (r < 0) {
00938             if (fwhm_x>0 && fwhm_y>0)   r = 5*(fwhm_x+fwhm_y)/2.0 ;
00939             else                        r = HAWKI_PHOT_STAR_RADIUS ;
00940         }
00941         r1 = hawki_cal_zpoint_config.phot_bg_r1 ;
00942         r2 = hawki_cal_zpoint_config.phot_bg_r2 ;
00943         if (r1 < 0) r1 = r + 10.0 ;
00944         if (r2 < 0) r2 = r1 + 20.0 ;
00945         //cpl_msg_info(__func__, "Use radii for star: %g and background: %g, %g",
00946         //        r, r1, r2) ;
00947 
00948         /* Compute the photometry */
00949         /* Background */
00950         bgd = irplib_strehl_ring_background(ima, (int)(pos_x[iframe]),
00951                 (int)(pos_y[iframe]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN) ;
00952         /* Flux */
00953         fl = irplib_strehl_disk_flux(ima,
00954                 (int)(pos_x[iframe]), (int)(pos_y[iframe]), (int)r, bgd);
00955         /* Zero Point */
00956         if ((fl < 0) || (dit < 0))     zp = -1.0 ;
00957         else                    zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
00958         //cpl_msg_info(__func__, "Zero point in chip %d:   %g", labels[iframe], zp) ;
00959         /* Peak */
00960         peak = cpl_image_get_max_window(ima,
00961                 (int)(pos_x[iframe]-5), (int)(pos_y[iframe]-5),
00962                 (int)(pos_x[iframe]+5), (int)(pos_y[iframe]+5));
00963 
00964         hawki_cal_zpoint_outputs.zpoint[labels[iframe]-1] = zp;
00965         hawki_cal_zpoint_outputs.atx0[labels[iframe]-1] = zp + 
00966             extinction * hawki_cal_zpoint_outputs.airmass[labels[iframe]-1];
00967         hawki_cal_zpoint_outputs.posx[labels[iframe]-1] = pos_x[iframe];
00968         hawki_cal_zpoint_outputs.posy[labels[iframe]-1] = pos_y[iframe];
00969         hawki_cal_zpoint_outputs.flux[labels[iframe]-1] = fl;
00970         hawki_cal_zpoint_outputs.instrmag[labels[iframe]-1] = 2.5 * log10(fl/dit);
00971         hawki_cal_zpoint_outputs.peak[labels[iframe]-1] = peak;
00972         hawki_cal_zpoint_outputs.bgd[labels[iframe]-1] = bgd;
00973         hawki_cal_zpoint_outputs.fwhmx[labels[iframe]-1] = fwhm_x;
00974         hawki_cal_zpoint_outputs.fwhmy[labels[iframe]-1] = fwhm_y;
00975         if (fwhm_x > 0 && fwhm_y > 0)
00976             hawki_cal_zpoint_outputs.fwhm[labels[iframe]-1] = sqrt(fwhm_x*fwhm_y);
00977         else
00978             hawki_cal_zpoint_outputs.fwhm[labels[iframe]-1] = -1.0;
00979         hawki_cal_zpoint_outputs.fwhmx_as[labels[iframe]-1] = fwhm_x * pixscale;
00980         hawki_cal_zpoint_outputs.fwhmy_as[labels[iframe]-1] = fwhm_y * pixscale;
00981         if (fwhm_x > 0 && fwhm_y > 0)
00982             hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] =
00983                 sqrt(fwhm_x*fwhm_y*pixscale*pixscale);
00984         else
00985             hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] = -1.0;
00986 
00987     }
00988 
00989     /* Create the table */
00990     zpoint = hawki_table_new(1);
00991     //tab = cpl_table_new(nframes) ;
00992     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
00993         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT);
00994         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, CPL_TYPE_STRING);
00995         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE);
00996         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSX,"pix");
00997         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE);
00998         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSY,"pix");
00999         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE);
01000         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ZPOINT,"mag");
01001         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE);
01002         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ATX0,"mag");
01003         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE);
01004         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE);
01005         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FLUX,"ADU");
01006         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, CPL_TYPE_DOUBLE);
01007         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_INSTRMAG,"log(ADU/s)");
01008         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING);
01009         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE);
01010         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_PEAK,"ADU");
01011         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE);
01012         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_BGD,"ADU");
01013         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE);
01014         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX,"pix");
01015         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE);
01016         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY,"pix");
01017         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE);
01018         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM,"pix");
01019         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE);
01020         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX_AS,"arcsec");
01021         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE);
01022         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY_AS,"arcsec");
01023         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE);
01024         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM_AS,"arcsec");
01025         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, CPL_TYPE_DOUBLE);
01026         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_STARMAG,"mag");
01027         
01028         cpl_table_set_int(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, 0, idet+1) ;
01029         cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, 0,
01030                 hawki_cal_zpoint_outputs.starname) ;
01031         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSX, 0,
01032                 hawki_cal_zpoint_outputs.posx[idet]) ;
01033         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSY, 0,
01034                 hawki_cal_zpoint_outputs.posy[idet]) ;
01035         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, 0,
01036                 hawki_cal_zpoint_outputs.zpoint[idet]) ;
01037         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, 0,
01038                 hawki_cal_zpoint_outputs.atx0[idet]) ;
01039         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, 0,
01040                 hawki_cal_zpoint_outputs.airmass[idet]) ;
01041         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, 0,
01042                 hawki_cal_zpoint_outputs.flux[idet]) ;
01043         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, 0,
01044                 hawki_cal_zpoint_outputs.instrmag[idet]) ;
01045         cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, 0,
01046                 hawki_cal_zpoint_outputs.filter);
01047         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, 0,
01048                 hawki_cal_zpoint_outputs.peak[idet]) ;
01049         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_BGD, 0,
01050                 hawki_cal_zpoint_outputs.bgd[idet]) ;
01051         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, 0,
01052                 hawki_cal_zpoint_outputs.fwhmx[idet]) ;
01053         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, 0,
01054                 hawki_cal_zpoint_outputs.fwhmy[idet]) ;
01055         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, 0,
01056                 hawki_cal_zpoint_outputs.fwhm[idet]) ;
01057         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, 0,
01058                 hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
01059         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, 0,
01060                 hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
01061         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, 0,
01062                 hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
01063         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, 0,
01064                 hawki_cal_zpoint_config.magnitude);
01065     }
01066 
01067     /* Mean values */
01068     hawki_cal_zpoint_outputs.mean_zpoint = 0.0 ;
01069     hawki_cal_zpoint_outputs.mean_atx0 = 0.0 ;
01070     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01071         hawki_cal_zpoint_outputs.mean_zpoint +=
01072             hawki_cal_zpoint_outputs.zpoint[idet] ;
01073         hawki_cal_zpoint_outputs.mean_atx0  +=
01074             hawki_cal_zpoint_outputs.atx0[idet] ;
01075     }
01076     hawki_cal_zpoint_outputs.mean_zpoint /= HAWKI_NB_DETECTORS ;
01077     hawki_cal_zpoint_outputs.mean_atx0 /= HAWKI_NB_DETECTORS ;
01078 
01079     /* Output results */
01080     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01081     {
01082         cpl_msg_info(__func__, "Zero point [at airmass=1] in chip %d:", idet + 1);
01083         cpl_msg_indent_more();
01084         cpl_msg_info(__func__,"   ZP: %g [%g]",
01085                      hawki_cal_zpoint_outputs.zpoint[idet],
01086                      hawki_cal_zpoint_outputs.atx0[idet]);
01087         cpl_msg_info(__func__,"   Flux of star: %f",
01088                      hawki_cal_zpoint_outputs.flux[idet]);
01089         cpl_msg_indent_less();
01090     }
01091 
01092     return zpoint;
01093 }
01094 
01095 /*----------------------------------------------------------------------------*/
01103 /*----------------------------------------------------------------------------*/
01104 static int hawki_cal_zpoint_get_mag(
01105         const char  *       stdstars,
01106         double              pointing_ra,
01107         double              pointing_dec,
01108         hawki_band          band)
01109 {
01110     char             star_name[512] ;
01111     char             cat_name[512] ;
01112     char             star_type[32] ;
01113     double           stdstar_ra;
01114     double           stdstar_dec;
01115     int              ret ;
01116     cpl_errorstate   error_prevstate = cpl_errorstate_get();
01117 
01118     /* Initialise */
01119     ret = -1 ;
01120 
01121     irplib_stdstar_find_star
01122         (stdstars, pointing_ra, pointing_dec,
01123          hawki_std_band_name(HAWKI_BAND_H), "all", &hawki_cal_zpoint_config.mag_H,
01124          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01125     irplib_stdstar_find_star
01126         (stdstars, pointing_ra, pointing_dec,
01127          hawki_std_band_name(HAWKI_BAND_J), "all", &hawki_cal_zpoint_config.mag_J,
01128          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01129     irplib_stdstar_find_star
01130         (stdstars, pointing_ra, pointing_dec,
01131          hawki_std_band_name(HAWKI_BAND_K), "all", &hawki_cal_zpoint_config.mag_K,
01132          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01133     irplib_stdstar_find_star
01134         (stdstars, pointing_ra, pointing_dec,
01135          hawki_std_band_name(HAWKI_BAND_Y), "all", &hawki_cal_zpoint_config.mag_Y,
01136          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01137 
01138     switch (band) {
01139         /* SW mode */
01140         case HAWKI_BAND_H:
01141             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_H;
01142             break ;
01143         case HAWKI_BAND_J:
01144             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_J;
01145             break ;
01146         case HAWKI_BAND_K:
01147             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_K;
01148             break ;
01149         case HAWKI_BAND_Y:
01150             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_Y;
01151             break ;
01152         default:
01153             cpl_msg_error(__func__, "cannot determine associated filter") ;
01154             return -1 ;
01155     }
01156 
01157     /* If the magnitude still is not known, abort */
01158     if(!cpl_errorstate_is_equal(error_prevstate))
01159     {
01160         cpl_msg_error(__func__, "%s", cpl_error_get_message());
01161         return -1 ;
01162     }
01163     cpl_msg_info(__func__, "Found catalog star %s", star_name) ;
01164 
01165     /* Store results */
01166     strcpy(hawki_cal_zpoint_outputs.starname, star_name) ;
01167     strcpy(hawki_cal_zpoint_outputs.sptype, star_type) ;
01168     strcpy(hawki_cal_zpoint_outputs.catalog, cat_name) ;
01169     hawki_cal_zpoint_outputs.stdstar_ra = stdstar_ra;
01170     hawki_cal_zpoint_outputs.stdstar_dec = stdstar_dec;
01171 
01172     return 0 ;
01173 }
01174 
01175 /*----------------------------------------------------------------------------*/
01184 /*----------------------------------------------------------------------------*/
01185 static int hawki_cal_zpoint_save
01186 (cpl_table           **  zpoint_tables,
01187  int                 *   labels,
01188  cpl_imagelist       *   images,
01189  cpl_table           **  raw_zpoint_stats,
01190  cpl_frameset        *   zpoint_frames,
01191  cpl_frameset        *   calib_frames,
01192  const cpl_frame     *   stars_frame,
01193  cpl_parameterlist   *   parlist,
01194  cpl_frameset        *   set)
01195 {
01196     cpl_propertylist    **  qclists ;
01197     cpl_propertylist    *   wcslist;
01198     cpl_frameset        *   used_frames;
01199     const char          *   ref_filename;
01200     cpl_propertylist    *   inputlist ;
01201     int                     ext_nb, nframes ;
01202     const char          *   recipe_name = "hawki_cal_zpoint" ;
01203     int                     idet;
01204     int                     iframe;
01205     cpl_errorstate          error_prevstate = cpl_errorstate_get();
01206 
01207 
01208     /* Initialise */
01209     nframes = cpl_frameset_get_size(set) ;
01210 
01211     /* Get the reference frame */
01212     ref_filename = hawki_get_extref_file(set);
01213 
01214     /* Create the QC lists for the extensions */
01215     qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
01216     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01217     {
01218         int            this_iframe = -1;
01219 
01220         qclists[idet] = cpl_propertylist_new() ;
01221         /* These QC are common to all extensions */
01222         cpl_propertylist_append_string(qclists[idet], "ESO QC FILTER OBS",
01223                 hawki_cal_zpoint_outputs.filter) ;
01224         cpl_propertylist_append_string(qclists[idet], "ESO QC FILTER REF",
01225                 hawki_std_band_name(hawki_cal_zpoint_outputs.band)) ;
01226         cpl_propertylist_append_double(qclists[idet], "ESO QC AMBI RHUM AVG",
01227                 hawki_cal_zpoint_outputs.humidity);
01228         cpl_propertylist_append_double(qclists[idet], "ESO QC AIRMASS MEAN",
01229                 hawki_cal_zpoint_outputs.mean_airmass) ;
01230         cpl_propertylist_append_string(qclists[idet], "ESO QC STDNAME",
01231                 hawki_cal_zpoint_outputs.starname) ;
01232         cpl_propertylist_append_string(qclists[idet], "ESO QC SPECTYPE",
01233                 hawki_cal_zpoint_outputs.sptype) ;
01234         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG",
01235                 hawki_cal_zpoint_config.magnitude) ;
01236         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG H",
01237                 hawki_cal_zpoint_config.mag_H) ;
01238         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG J",
01239                 hawki_cal_zpoint_config.mag_J) ;
01240         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG K",
01241                 hawki_cal_zpoint_config.mag_K) ;
01242         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG Y",
01243                 hawki_cal_zpoint_config.mag_Y) ;
01244         cpl_propertylist_append_string(qclists[idet], "ESO QC CATNAME",
01245                 hawki_cal_zpoint_outputs.catalog) ;
01246         cpl_propertylist_append_double(qclists[idet], "ESO QC DATANCOM",
01247                 nframes) ;
01248         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT MEAN",
01249                                        hawki_cal_zpoint_outputs.mean_zpoint) ;
01250         cpl_propertylist_append_double(qclists[idet], "ESO QC ATX0 MEAN",
01251                                        hawki_cal_zpoint_outputs.mean_atx0) ;
01252         /* These QC are specific of the extension */
01253         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT",
01254                 hawki_cal_zpoint_outputs.zpoint[idet]) ;
01255         cpl_propertylist_append_double(qclists[idet], "ESO QC ATX0",
01256                 hawki_cal_zpoint_outputs.atx0[idet]) ;
01257         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT POSX",
01258                 hawki_cal_zpoint_outputs.posx[idet]) ;
01259         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT POSY",
01260                 hawki_cal_zpoint_outputs.posy[idet]) ;
01261         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FLUX",
01262                 hawki_cal_zpoint_outputs.flux[idet]) ;
01263         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT PEAK",
01264                 hawki_cal_zpoint_outputs.peak[idet]) ;
01265         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT BGD",
01266                 hawki_cal_zpoint_outputs.bgd[idet]) ;
01267         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMX",
01268                 hawki_cal_zpoint_outputs.fwhmx[idet]) ;
01269         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMY",
01270                 hawki_cal_zpoint_outputs.fwhmy[idet]) ;
01271         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHM",
01272                 hawki_cal_zpoint_outputs.fwhm[idet]) ;
01273         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMX_AS",
01274                 hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
01275         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMY_AS",
01276                 hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
01277         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHM_AS",
01278                 hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
01279 
01280         /* Propagate some keywords from input raw frame extensions */
01281         ext_nb=hawki_get_ext_from_detector(ref_filename, idet+1);
01282         inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
01283                 HAWKI_HEADER_EXT_FORWARD, 0) ;
01284         cpl_propertylist_append(qclists[idet], inputlist) ;
01285         cpl_propertylist_delete(inputlist) ;
01286 
01287         /* Propagate WCS keywords */
01288         for(iframe=0; iframe<cpl_frameset_get_size(zpoint_frames); iframe++)
01289             if(labels[iframe] == idet + 1)
01290                 this_iframe = iframe;
01291         wcslist = cpl_propertylist_load_regexp
01292             (cpl_frame_get_filename(cpl_frameset_get_frame(zpoint_frames, this_iframe)),
01293                                     ext_nb, HAWKI_HEADER_WCS, 0);
01294         cpl_propertylist_copy_property_regexp
01295             (qclists[idet], wcslist, HAWKI_HEADER_WCS, 0);
01296         cpl_propertylist_delete(wcslist) ;
01297     }
01298     
01299     /* Write the zpoint table  */
01300     used_frames = cpl_frameset_duplicate(zpoint_frames);
01301     for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
01302         cpl_frameset_insert(used_frames, cpl_frame_duplicate(
01303                 cpl_frameset_get_frame(calib_frames, iframe)));
01304     cpl_frameset_insert(used_frames, cpl_frame_duplicate(stars_frame));
01305     hawki_tables_save(set,
01306                       parlist,
01307                       used_frames,
01308                       (const cpl_table **)zpoint_tables,
01309                       recipe_name,
01310                       HAWKI_CALPRO_ZPOINT_TAB,
01311                       HAWKI_PROTYPE_ZPOINT_TAB,
01312                       NULL,
01313                       (const cpl_propertylist **)qclists,
01314                       "hawki_cal_zpoint.fits");
01315     cpl_frameset_delete(used_frames);
01316     
01317     /* Statistics of the raw images in the QC */
01318     hawki_image_stats_stats(raw_zpoint_stats, qclists);
01319 
01320     /* Write the table with the raw zpoint objects statistics */
01321     used_frames = cpl_frameset_duplicate(zpoint_frames);
01322     hawki_tables_save(set,
01323                       parlist,
01324                       used_frames,
01325                       (const cpl_table **)raw_zpoint_stats,
01326                       recipe_name,
01327                       HAWKI_CALPRO_ZPOINT_STATS,
01328                       HAWKI_PROTYPE_ZPOINT_STATS,
01329                       NULL,
01330                       (const cpl_propertylist **)qclists,
01331                       "hawki_cal_zpoint_stats.fits");
01332 
01333     /* Write the images  */
01334     for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
01335         cpl_frameset_insert(used_frames, cpl_frame_duplicate(
01336                 cpl_frameset_get_frame(calib_frames, iframe)));
01337     hawki_imagelist_save(set,
01338                          parlist,
01339                          used_frames,
01340                          images,
01341                          recipe_name,
01342                          HAWKI_CALPRO_ZPOINT_IMA,
01343                          HAWKI_PROTYPE_ZPOINT_IMA,
01344                          NULL,
01345                          (const cpl_propertylist**)qclists,
01346                          "hawki_cal_zpoint_check.fits") ;
01347     cpl_frameset_delete(used_frames);
01348 
01349     /* Remove the keywords for the FITS extensions */
01350     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01351         cpl_propertylist_erase_regexp(qclists[idet], HAWKI_HEADER_EXT_FORWARD, 0) ;
01352     }
01353 
01354     /* Free */
01355     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01356         cpl_propertylist_delete(qclists[idet]) ;
01357     }
01358     cpl_free(qclists) ;
01359 
01360     /* Free */
01361     if(!cpl_errorstate_is_equal(error_prevstate))
01362     {
01363         cpl_errorstate_set(CPL_ERROR_NONE);
01364         return -1;
01365     }
01366     return  0;
01367 }
01368 
01369 /*----------------------------------------------------------------------------*/
01375 /*----------------------------------------------------------------------------*/
01376 static int hawki_cal_zpoint_compute_keywords(
01377         cpl_frameset    *   set,
01378         int             *   labels)
01379 {
01380     int                     nframes ;
01381     cpl_vector          *   hum_vec ;
01382     cpl_frame           *   cur_frame ;
01383     cpl_propertylist    *   plist ;
01384     int                     iframe;
01385 
01386     /* Test inputs  */
01387     if (set == NULL) return -1 ;
01388 
01389     /* Initialize */
01390     nframes = cpl_frameset_get_size(set) ;
01391     hawki_cal_zpoint_outputs.mean_airmass   = 0.0 ;
01392 
01393     hum_vec = cpl_vector_new(nframes) ;
01394 
01395     for (iframe=0 ; iframe<nframes ; iframe++) {
01396         if (cpl_error_get_code()) {
01397             cpl_vector_delete(hum_vec) ;
01398             return -1 ;
01399         }
01400         cur_frame = cpl_frameset_get_frame(set, iframe) ;
01401         plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
01402         if (iframe==0) 
01403             hawki_cal_zpoint_outputs.mean_airmass += 
01404                 hawki_pfits_get_airmass_start(plist) ; 
01405         if (iframe==nframes-1) 
01406             hawki_cal_zpoint_outputs.mean_airmass += 
01407                 hawki_pfits_get_airmass_end(plist);
01408         hawki_cal_zpoint_outputs.airmass[labels[iframe] - 1] = 
01409             (hawki_pfits_get_airmass_start(plist) + 
01410              hawki_pfits_get_airmass_end(plist)) / 2.;
01411         cpl_vector_set(hum_vec,  iframe, hawki_pfits_get_humidity_level(plist));
01412         cpl_propertylist_delete(plist) ;
01413         if (cpl_error_get_code()) {
01414             cpl_vector_delete(hum_vec) ;
01415             cpl_error_reset() ;
01416             return -1 ;
01417         }
01418     }
01419     hawki_cal_zpoint_outputs.humidity  = cpl_vector_get_mean(hum_vec) ;
01420     hawki_cal_zpoint_outputs.mean_airmass /= 2 ;
01421 
01422     /* Free and return */
01423     cpl_vector_delete(hum_vec) ;
01424     if (cpl_error_get_code()) return -1 ;
01425     return 0 ;
01426 }
01427 
01428 static cpl_error_code hawki_cal_zpoint_get_expected_pos
01429 (cpl_frameset * set,
01430  int          * labels)
01431 {
01432     const char *  filename;
01433     int           iframe;
01434 
01435     for(iframe=0 ; iframe<HAWKI_NB_DETECTORS ; iframe++)
01436     {
01437         cpl_propertylist * wcs_plist;
01438         cpl_wcs          * wcs;
01439         int                idet;
01440 
01441         idet = labels[iframe];
01442         if(hawki_cal_zpoint_config.xcoord[idet - 1] == -1 ||
01443                 hawki_cal_zpoint_config.ycoord[idet - 1] == -1)
01444         {
01445             filename = cpl_frame_get_filename
01446                 (cpl_frameset_get_frame_const(set, iframe));
01447             wcs_plist = cpl_propertylist_load
01448                 (filename, hawki_get_ext_from_detector(filename, idet));
01449             wcs = cpl_wcs_new_from_propertylist(wcs_plist);
01450             cpl_propertylist_delete(wcs_plist);
01451             if(wcs == NULL)
01452             {
01453                 cpl_msg_error(__func__, "Could not get WCS info");
01454                 cpl_wcs_delete(wcs);
01455                 return CPL_ERROR_ILLEGAL_INPUT;
01456             }
01457             if(irplib_wcs_radectoxy(wcs,
01458                                     hawki_cal_zpoint_outputs.stdstar_ra,
01459                                     hawki_cal_zpoint_outputs.stdstar_dec,
01460                                     &(hawki_cal_zpoint_config.xcoord[idet - 1]),
01461                                     &(hawki_cal_zpoint_config.ycoord[idet - 1]))
01462                     != CPL_ERROR_NONE)
01463             {
01464                 cpl_msg_error(__func__,"Could not get the expected position of star");
01465                 cpl_wcs_delete(wcs);
01466                 return CPL_ERROR_UNSPECIFIED;
01467             }
01468 
01469             /* Free */
01470             cpl_wcs_delete(wcs);
01471         }
01472     }
01473 
01474     return 0;
01475 }
01476 
01477 
01478 static void hawki_cal_zpoint_output_init(void)
01479 {
01480     int idet;
01481 
01482     hawki_cal_zpoint_outputs.starname[0] = (char)0 ;
01483     hawki_cal_zpoint_outputs.sptype[0] = (char)0 ;
01484     hawki_cal_zpoint_outputs.filter[0] = (char)0 ;
01485     hawki_cal_zpoint_outputs.catalog[0] = (char)0 ;
01486     hawki_cal_zpoint_outputs.pixscale = -1.0 ;
01487     hawki_cal_zpoint_outputs.dit = -1.0 ;
01488     hawki_cal_zpoint_outputs.humidity = -1.0 ;
01489     hawki_cal_zpoint_outputs.mean_airmass = -1.0 ;
01490     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01491     {
01492         hawki_cal_zpoint_outputs.airmass[idet] = -1.0 ;
01493         hawki_cal_zpoint_outputs.zpoint[idet] = -1.0 ;
01494         hawki_cal_zpoint_outputs.atx0[idet] = -1.0 ;
01495         hawki_cal_zpoint_outputs.posx[idet] = -1.0 ;
01496         hawki_cal_zpoint_outputs.posy[idet] = -1.0 ;
01497         hawki_cal_zpoint_outputs.flux[idet] = -1.0 ;
01498         hawki_cal_zpoint_outputs.peak[idet] = -1.0 ;
01499         hawki_cal_zpoint_outputs.bgd[idet] = -1.0 ;
01500         hawki_cal_zpoint_outputs.fwhmx[idet] = -1.0 ;
01501         hawki_cal_zpoint_outputs.fwhmy[idet] = -1.0 ;
01502         hawki_cal_zpoint_outputs.fwhm[idet] = -1.0 ;
01503         hawki_cal_zpoint_outputs.fwhmx_as[idet] = -1.0 ;
01504         hawki_cal_zpoint_outputs.fwhmy_as[idet] = -1.0 ;
01505         hawki_cal_zpoint_outputs .fwhm_as[idet] = -1.0 ;
01506     }
01507 }
01508 
01509 int hawki_cal_zpoint_retrieve_input_param
01510 (cpl_parameterlist  *  parlist)
01511 {
01512     cpl_parameter   *   par ;
01513     const char      *   sval ;
01514 
01515     par = NULL ;
01516 
01517     /* --ra */
01518     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ra") ;
01519     hawki_cal_zpoint_config.target_ra = cpl_parameter_get_double(par) ;
01520     /* --dec */
01521     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.dec") ;
01522     hawki_cal_zpoint_config.target_dec = cpl_parameter_get_double(par) ;
01523     /* --mag */
01524     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.mag") ;
01525     hawki_cal_zpoint_config.magnitude = cpl_parameter_get_double(par) ;
01526     /* --detect_sigma */
01527     par = cpl_parameterlist_find(parlist,"hawki.hawki_cal_zpoint.detect_sigma");
01528     hawki_cal_zpoint_config.detect_sigma = cpl_parameter_get_double(par) ;
01529     /* --sx */
01530     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sx") ;
01531     hawki_cal_zpoint_config.sx = cpl_parameter_get_int(par) ;
01532     /* --sy */
01533     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sy") ;
01534     hawki_cal_zpoint_config.sy = cpl_parameter_get_int(par) ;
01535     /* --star_r */
01536     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.star_r") ;
01537     hawki_cal_zpoint_config.phot_star_radius = cpl_parameter_get_double(par) ;
01538     /* --bg_r1 */
01539     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r1") ;
01540     hawki_cal_zpoint_config.phot_bg_r1 = cpl_parameter_get_double(par) ;
01541     /* --bg_r2 */
01542     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r2") ;
01543     hawki_cal_zpoint_config.phot_bg_r2 = cpl_parameter_get_double(par) ;
01544     /* --xcoord */
01545     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.xcoord");
01546     sval = cpl_parameter_get_string(par);
01547     if (sscanf(sval, "%lf,%lf,%lf,%lf",
01548                hawki_cal_zpoint_config.xcoord,
01549                hawki_cal_zpoint_config.xcoord+1,
01550                hawki_cal_zpoint_config.xcoord+2,
01551                hawki_cal_zpoint_config.xcoord+3)!=4)
01552     {
01553         return -1;
01554     }
01555     /* --ycoord */
01556     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ycoord");
01557     sval = cpl_parameter_get_string(par);
01558     if (sscanf(sval, "%lf,%lf,%lf,%lf",
01559                hawki_cal_zpoint_config.ycoord,
01560                hawki_cal_zpoint_config.ycoord+1,
01561                hawki_cal_zpoint_config.ycoord+2,
01562                hawki_cal_zpoint_config.ycoord+3)!=4)
01563     {
01564         return -1;
01565     }
01566 
01567     return 0;
01568 }
01569 
01570 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist)
01571 {
01572     if(hawki_pfits_get_targ_epoch(plist) != 2000. ||
01573        hawki_pfits_get_targ_equinox(plist) != 2000.)
01574     {
01575         cpl_msg_error(__func__,"Epoch and equinox must be 2000.");
01576         return -1;
01577     }
01578     else
01579         return 0;
01580 }