hawki_cal_zpoint.c

00001 /* $Id: hawki_cal_zpoint.c,v 1.25 2010/11/30 23:53:00 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2010/11/30 23:53:00 $
00024  * $Revision: 1.25 $
00025  * $Name: hawki-1_8_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <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                     "cgarcia@eso.org",
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 size of the search window", "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 size of the search window", "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         return -1 ;
00419     }
00420 
00421     /* Exactly 4 images are expected */
00422     if (cpl_frameset_get_size(zpoint_frames) != 4) {
00423         cpl_msg_error(__func__, "4 input raw frames are expected, not %d",
00424                 cpl_frameset_get_size(zpoint_frames)) ;
00425         cpl_frameset_delete(zpoint_frames) ;
00426         return -1 ;
00427     }
00428 
00429     /* Create the statistics table */
00430     raw_zpoint_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00431     for(idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00432     {
00433         raw_zpoint_stats[idet] = cpl_table_new(
00434             cpl_frameset_get_size(zpoint_frames));
00435     }
00436     hawki_image_stats_initialize(raw_zpoint_stats);
00437 
00438     /* Compute the zpoint values */
00439     cpl_msg_info(__func__, "Reduce the data") ;
00440     cpl_msg_indent_more() ;
00441     labels = cpl_calloc(cpl_frameset_get_size(zpoint_frames), sizeof(int)) ;
00442     if ((zpoint_tables = hawki_cal_zpoint_reduce(zpoint_frames, stdstars, 
00443              bpm, flat, raw_zpoint_stats, labels, &std_star_images))==NULL)
00444     {
00445         cpl_msg_error(__func__, "Cannot reduce the data") ;
00446         cpl_frameset_delete(zpoint_frames) ;
00447         cpl_frameset_delete(calib_frames);
00448         hawki_table_delete(raw_zpoint_stats);
00449         cpl_free(labels);
00450         cpl_msg_indent_less() ;
00451         return -1 ;
00452     }
00453     cpl_msg_indent_less() ;
00454 
00455     /* Save the products */
00456     cpl_msg_info(__func__, "Save the products") ;
00457     cpl_msg_indent_more() ;
00458     if (hawki_cal_zpoint_save
00459             (zpoint_tables, labels, std_star_images, raw_zpoint_stats, 
00460              zpoint_frames, calib_frames, stars_frame,
00461              parlist, framelist) == -1)
00462     {
00463         cpl_msg_warning(__func__, "Data could not be saved. "
00464                         "Check permisions or disk space") ;
00465         cpl_frameset_delete(zpoint_frames);
00466         cpl_frameset_delete(calib_frames);
00467         hawki_table_delete(zpoint_tables) ;
00468         cpl_imagelist_delete(std_star_images) ;
00469         hawki_table_delete(raw_zpoint_stats);
00470         cpl_free(labels);
00471         cpl_msg_indent_less() ;
00472         return -1 ;
00473     }
00474     cpl_msg_indent_less() ;
00475 
00476     /* Free and return */
00477     cpl_frameset_delete(zpoint_frames);
00478     cpl_frameset_delete(calib_frames);
00479     cpl_imagelist_delete(std_star_images);
00480     hawki_table_delete(zpoint_tables);
00481     hawki_table_delete(raw_zpoint_stats);
00482     cpl_free(labels);
00483 
00484     /* Return */
00485     if (cpl_error_get_code()) return -1 ;
00486     else return 0 ;
00487 }
00488 
00489 /*----------------------------------------------------------------------------*/
00499 /*----------------------------------------------------------------------------*/
00500 static cpl_table ** hawki_cal_zpoint_reduce
00501 (cpl_frameset    *   set,
00502  const char      *   stdstars,
00503  const char      *   bpm,
00504  const char      *   flat,
00505  cpl_table       **  raw_zpoint_stats,
00506  int             *   labels,
00507  cpl_imagelist   **  star_images)
00508 {
00509     cpl_frame           *   cur_frame ;
00510     cpl_propertylist    *   plist ;
00511     const char          *   sval ;
00512     cpl_imagelist       *   star_images_frame_order ;
00513     int                     nima ;
00514     cpl_bivector        *   positions ;
00515     cpl_image           *   filt_ima ;
00516     cpl_matrix          *   kernel ;
00517     int                     size_x, size_y ;
00518     double                  pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist ;
00519     double                  equinox, epoch;
00520     cpl_apertures       *   aperts ;
00521     cpl_table           **  zpoint_tables;
00522     cpl_image           *   tmp_ima ;
00523     int                     iaper;
00524     int                     idet;
00525     int                     iframe;
00526     int                     iframe_star = -1;
00527     int                     nframes;
00528     cpl_errorstate          error_prevstate;
00529 
00530     /* Check inputs */
00531     if (set == NULL) return NULL ;
00532     if (stdstars == NULL) return NULL ;
00533     if (star_images == NULL) return NULL ;
00534 
00535     /* Initialize */
00536     *star_images = cpl_imagelist_new();
00537 
00538     /* Get the filter name, DIT, RA and DEC  */
00539     error_prevstate = cpl_errorstate_get();
00540     cur_frame = cpl_frameset_get_frame(set, 0) ;
00541     plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
00542     if ((sval = hawki_pfits_get_filter(plist)) == NULL) return NULL ;
00543     else sprintf(hawki_cal_zpoint_outputs.filter, sval) ;
00544     if (hawki_cal_zpoint_config.target_ra > 998.0)
00545     {
00546         hawki_cal_zpoint_config.target_ra = hawki_pfits_get_targ_alpha(plist);
00547         //hawki_cal_zpoint_config.target_ra = hawki_pfits_get_ra(plist) -
00548         //hawki_pfits_get_cumoffseta(plist) / 3600.;// Not valid before Nov 2008
00549         if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
00550         {
00551             cpl_propertylist_delete(plist);
00552             return NULL;
00553         }
00554     }
00555     if (hawki_cal_zpoint_config.target_dec > 998.0)
00556     {
00557         hawki_cal_zpoint_config.target_dec = hawki_pfits_get_targ_delta(plist);
00558         //hawki_cal_zpoint_config.target_dec = hawki_pfits_get_dec(plist) -
00559         //hawki_pfits_get_cumoffsetd(plist) / 3600.;// Not valid before Nov 2008
00560         if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
00561         {
00562             cpl_propertylist_delete(plist);
00563             return NULL;
00564         }
00565     }
00566     hawki_cal_zpoint_outputs.dit = hawki_pfits_get_dit(plist) ;
00567     hawki_cal_zpoint_outputs.pixscale = hawki_pfits_get_pixscale(plist) ;
00568     cpl_propertylist_delete(plist) ;
00569     if(!cpl_errorstate_is_equal(error_prevstate))
00570     {
00571         cpl_msg_error(__func__, "Cannot get some header informations") ;
00572         cpl_imagelist_delete(*star_images);
00573         return NULL ;
00574     }
00575     cpl_msg_info(__func__,"Searching catalog stars closest to target:");
00576     cpl_msg_indent_more();
00577     cpl_msg_info(__func__,"RA = %g ; DEC = %g",
00578                  hawki_cal_zpoint_config.target_ra,
00579                  hawki_cal_zpoint_config.target_dec);
00580     cpl_msg_info(__func__,"HAWK-I Filter: %s", hawki_cal_zpoint_outputs.filter);
00581     cpl_msg_indent_less();
00582 
00583     /* Get the band */
00584     if ((hawki_cal_zpoint_outputs.band =
00585                 hawki_get_band(hawki_cal_zpoint_outputs.filter)) ==
00586             HAWKI_BAND_UNKNOWN) {
00587         cpl_msg_error(__func__, "Cannot associate the filter %s to a band",
00588                 hawki_cal_zpoint_outputs.filter) ;
00589         cpl_imagelist_delete(*star_images);
00590         return NULL ;
00591     }
00592 
00593     /* Get the standard star information from database */
00594     if (hawki_cal_zpoint_config.magnitude > 98.0) {
00595         if (hawki_cal_zpoint_get_mag(stdstars, hawki_cal_zpoint_config.target_ra,
00596                                     hawki_cal_zpoint_config.target_dec,
00597                                     hawki_cal_zpoint_outputs.band) == -1)
00598         {
00599             cpl_msg_error(__func__,"Cannot get a suitable star from catalog");
00600             cpl_imagelist_delete(*star_images);
00601             return NULL ;
00602         }
00603     }
00604     cpl_msg_info(__func__, "Star magnitude in filter %s : %g",
00605             hawki_cal_zpoint_outputs.filter,
00606             hawki_cal_zpoint_config.magnitude) ;
00607     cpl_msg_info(__func__, "Star coordinates: RA = %g ; DEC = %g",
00608             hawki_cal_zpoint_outputs.stdstar_ra,
00609             hawki_cal_zpoint_outputs.stdstar_dec);
00610 
00611     /* Labelise frames */
00612     cpl_msg_info(__func__, "Guessing which frame the STD is in for each chip");
00613     hawki_detectors_locate_star
00614         (set, hawki_cal_zpoint_outputs.stdstar_ra,
00615          hawki_cal_zpoint_outputs.stdstar_dec, labels);
00616     if (labels == NULL)
00617     {
00618         cpl_msg_error(__func__, "Cannot determine which frame the STD is on") ;
00619         cpl_imagelist_delete(*star_images);
00620         return NULL ;
00621     }
00622 
00623     /* Compute the expected position of the star in pixels */
00624     /* This is stored in hawki_cal_zpoint_config.xcoord, ycoord */
00625     if(hawki_cal_zpoint_get_expected_pos(set, labels) != CPL_ERROR_NONE)
00626     {
00627         cpl_msg_error(__func__,"Could not determine where the star is located");
00628         cpl_imagelist_delete(*star_images);
00629         return NULL;
00630     }
00631     
00632     /* Fetch the airmass and humidity */
00633     hawki_cal_zpoint_compute_keywords(set, labels);
00634 
00635     /* Create the positions vector */
00636     nima = cpl_frameset_get_size(set) ;
00637     positions = cpl_bivector_new(nima) ;
00638 
00639     /* Loop on the detectors */
00640     nframes = cpl_frameset_get_size(set) ;
00641     cpl_msg_info(__func__,"Loop on the chips");
00642     cpl_msg_indent_more() ;
00643     for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00644     {
00645         cpl_imagelist * sky_images;
00646         cpl_image     * star_ima = NULL;
00647         cpl_image     * sky;
00648         cpl_image     * flat_im;
00649         int             ext_nb;
00650 
00651         cpl_msg_info(__func__, "Loading the chip %d", idet+1);
00652         cpl_msg_indent_more() ;
00653 
00654         /* Allocate */
00655         sky_images = cpl_imagelist_new();
00656 
00657         cpl_msg_indent_more() ;
00658         for (iframe=0 ; iframe<nframes ; iframe++)
00659         {
00660             cpl_image * ima_cur;
00661 
00662             /* Load the image */
00663             ima_cur = hawki_load_image(set, iframe, idet+1, CPL_TYPE_FLOAT) ;
00664             if(ima_cur == NULL)
00665             {
00666                 cpl_bivector_delete(positions) ;
00667                 cpl_imagelist_delete(*star_images);
00668                 cpl_imagelist_delete(sky_images) ;
00669                 cpl_msg_error(__func__, "Error reading image");
00670                 return NULL;
00671             }
00672             /* Get image statistics */
00673             size_x = cpl_image_get_size_x(ima_cur) ;
00674             size_y = cpl_image_get_size_y(ima_cur) ;
00675             if(hawki_image_stats_fill_from_image
00676                 (raw_zpoint_stats,
00677                  ima_cur,
00678                  1,
00679                  1,
00680                  size_x,
00681                  size_y,
00682                  idet,
00683                  iframe) !=0 )
00684             {
00685                 cpl_msg_error(__func__,"Cannot compute stats on ima %d det %d",
00686                               iframe+1, idet+1);
00687                 cpl_bivector_delete(positions) ;
00688                 cpl_imagelist_delete(*star_images);
00689                 cpl_imagelist_delete(sky_images) ;
00690                 cpl_image_delete(ima_cur);
00691                 cpl_msg_indent_less() ;
00692                 return NULL ;
00693             }
00694 
00695             /* Add the image to either the sky images or the star image */
00696             if(labels[iframe] == idet + 1)
00697             {
00698                 star_ima = ima_cur;
00699                 iframe_star = iframe;
00700             }
00701             else
00702                 cpl_imagelist_set(sky_images, ima_cur,
00703                                   cpl_imagelist_get_size(sky_images));
00704         }
00705         cpl_msg_indent_less();
00706 
00707         /* Create the sky */
00708         cpl_msg_info(__func__, "Correct for the sky");
00709         sky = cpl_imagelist_collapse_median_create(sky_images);
00710         cpl_imagelist_delete(sky_images) ;
00711 
00712         /* Subtract the sky */
00713         cpl_image_subtract(star_ima, sky) ;
00714         cpl_image_delete(sky) ;
00715 
00716         /* Divide by the flatfield if one is provided */
00717         if (flat) {
00718             cpl_msg_info(__func__, "Correct for the flat") ;
00719 
00720             /* Get the extension with the current chip */
00721             if ((ext_nb = hawki_get_ext_from_detector(flat, idet + 1)) == -1)
00722             {
00723                 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00724                               idet + 1);
00725                 cpl_imagelist_delete(*star_images) ;
00726                 cpl_bivector_delete(positions) ;
00727                 return NULL ;
00728             }
00729             /* Load */
00730             flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, ext_nb) ;
00731             cpl_image_divide(star_ima, flat_im) ;
00732             cpl_image_delete(flat_im) ;
00733         }
00734 
00735         /* Correct the bad pixels */
00736         if (bpm) {
00737             cpl_msg_info(__func__, "Correct for the bad pixels") ;
00738             if (hawki_bpm_calib(star_ima, bpm, idet + 1) == -1)
00739             {
00740                 cpl_msg_error(__func__, "Cannot correct the BPM for chip %d",
00741                               idet + 1);
00742                 cpl_imagelist_delete(*star_images) ;
00743                 cpl_bivector_delete(positions) ;
00744                 return NULL ;
00745             }
00746         }
00747 
00748         /* Put the result in the image list */
00749         cpl_imagelist_set(*star_images, star_ima,
00750                           cpl_imagelist_get_size(*star_images)) ;
00751 
00752         /* Object detection */
00753         cpl_msg_info(__func__,"For chip %d the STD is detected on frame %d",
00754                      idet + 1, iframe_star+1);
00755         pos_x_cen = pos_y_cen = -1.0 ;
00756         size_x = cpl_image_get_size_x(star_ima) ;
00757         size_y = cpl_image_get_size_y(star_ima) ;
00758 
00759         /* Filtering the image*/
00760         kernel = cpl_matrix_new(3, 3) ;
00761         cpl_matrix_fill(kernel, 1.0) ;
00762         filt_ima = cpl_image_filter_median(star_ima, kernel) ;
00763         cpl_matrix_delete(kernel) ;
00764 
00765         /* Looking for apertures */
00766         aperts = cpl_apertures_extract_sigma(filt_ima,
00767                 hawki_cal_zpoint_config.detect_sigma) ;
00768         cpl_image_delete(filt_ima) ;
00769         if (aperts == NULL)
00770         {
00771             cpl_msg_error(__func__, "Cannot find the central object") ;
00772             cpl_imagelist_delete(*star_images) ;
00773             cpl_bivector_delete(positions) ;
00774             return NULL ;
00775         }
00776         min_dist = size_x * size_x + size_y * size_y ;
00777         for (iaper=0 ; iaper<cpl_apertures_get_size(aperts) ; iaper++) {
00778             pos_x = cpl_apertures_get_centroid_x(aperts, iaper+1) ;
00779             pos_y = cpl_apertures_get_centroid_y(aperts, iaper+1) ;
00780             dist = (pos_x-hawki_cal_zpoint_config.xcoord[idet])*
00781                     (pos_x-hawki_cal_zpoint_config.xcoord[idet]) +
00782                    (pos_y-hawki_cal_zpoint_config.ycoord[idet])*
00783                     (pos_y-hawki_cal_zpoint_config.ycoord[idet]);
00784             if (dist<min_dist) {
00785                 min_dist = dist ;
00786                 pos_x_cen = pos_x ;
00787                 pos_y_cen = pos_y ;
00788             }
00789         }
00790         cpl_apertures_delete(aperts) ;
00791 
00792         cpl_vector_set(cpl_bivector_get_x(positions), iframe_star, pos_x_cen) ;
00793         cpl_vector_set(cpl_bivector_get_y(positions), iframe_star, pos_y_cen) ;
00794         cpl_msg_info(__func__, "Expected star position: %g %g",
00795                 hawki_cal_zpoint_config.xcoord[idet],
00796                 hawki_cal_zpoint_config.ycoord[idet]);
00797         cpl_msg_info(__func__, "Bright object position: %g %g",
00798                 pos_x_cen, pos_y_cen) ;
00799 
00800         /* Check that the star is within the search window */
00801         if(fabs(pos_x_cen - hawki_cal_zpoint_config.xcoord[idet]) >
00802                 hawki_cal_zpoint_config.sx                        ||
00803            fabs(pos_y_cen - hawki_cal_zpoint_config.ycoord[idet]) >
00804                 hawki_cal_zpoint_config.sy)
00805         {
00806             cpl_msg_error(__func__,"No star found within search window "
00807                           "[%d, %d] of expected position",
00808                           hawki_cal_zpoint_config.sx,
00809                           hawki_cal_zpoint_config.sy);
00810 
00811             cpl_imagelist_delete(*star_images) ;
00812             cpl_bivector_delete(positions) ;
00813             return NULL ;
00814         }
00815 
00816         /* Free */
00817         cpl_msg_indent_less() ;
00818     }
00819     cpl_msg_indent_less() ;
00820 
00821     /* Reorder the images (frame order) */
00822     star_images_frame_order = cpl_imagelist_new() ;
00823     for (iframe=0 ; iframe< nframes; iframe++)
00824     {
00825         tmp_ima = cpl_image_duplicate
00826             (cpl_imagelist_get(*star_images, labels[iframe] -1 ));
00827         cpl_imagelist_set(star_images_frame_order, tmp_ima, iframe);
00828     }
00829 
00830     /* Compute the photometry */
00831     cpl_msg_info(__func__, "Compute the photometry") ;
00832     cpl_msg_indent_more() ;
00833     if ((zpoint_tables = hawki_cal_zpoint_photom
00834             (star_images_frame_order, positions, labels))==NULL) {
00835         cpl_msg_error(__func__, "Cannot reduce") ;
00836         cpl_bivector_delete(positions) ;
00837         cpl_imagelist_delete(star_images_frame_order) ;
00838         cpl_msg_indent_less() ;
00839         return NULL ;
00840     }
00841 
00842 
00843     /* Free and exit */
00844     cpl_msg_indent_less() ;
00845     cpl_bivector_delete(positions) ;
00846     cpl_imagelist_delete(star_images_frame_order) ;
00847 
00848     return zpoint_tables;
00849 }
00850 
00851 /*----------------------------------------------------------------------------*/
00861 /*----------------------------------------------------------------------------*/
00862 static cpl_table ** hawki_cal_zpoint_photom(
00863         cpl_imagelist       *   ilist,
00864         cpl_bivector        *   pos,
00865         int                 *   labels)
00866 {
00867     cpl_table **        zpoint;
00868     int                 nframes;
00869     double              r, r1, r2, mag, dit, extinction, pixscale ;
00870     cpl_image       *   ima ;
00871     double          *   pos_x ;
00872     double          *   pos_y ;
00873     double              bgd, fl, zp, peak, fwhm_x, fwhm_y ;
00874     cpl_bivector    *   iqe_res ;
00875     int                 iframe;
00876     int                 idet;
00877 
00878     /* Test entries */
00879     if (ilist == NULL) return NULL ;
00880     if (pos == NULL) return NULL ;
00881 
00882     /* Initialise */
00883     nframes = cpl_imagelist_get_size(ilist) ;
00884     mag = hawki_cal_zpoint_config.magnitude ;
00885     dit = hawki_cal_zpoint_outputs.dit ;
00886     pixscale = hawki_cal_zpoint_outputs.pixscale ;
00887 
00888     /* Get extinction */
00889     switch (hawki_cal_zpoint_outputs.band) {
00890         case HAWKI_BAND_J:      extinction = 0.098 ; break ;
00891         case HAWKI_BAND_H:      extinction = 0.039 ; break ;
00892         case HAWKI_BAND_K:      extinction = 0.065 ; break ;
00893         case HAWKI_BAND_Y:      extinction = 0.00 ; break ;
00894         default:                extinction = 0.00 ; break ;
00895     }
00896     cpl_msg_info(__func__,"Using tabulated extinction for band %s: %f",
00897                  hawki_cal_zpoint_outputs.filter, extinction);
00898 
00899     /* Loop on the images */
00900     for (iframe=0 ; iframe<nframes ; iframe++) {
00901         /* Get the current image */
00902         ima = cpl_imagelist_get(ilist, iframe) ;
00903 
00904         /* Get the current position */
00905         pos_x = cpl_bivector_get_x_data(pos) ;
00906         pos_y = cpl_bivector_get_y_data(pos) ;
00907 
00908         /* FWHM_X / FWHM_Y */
00909         iqe_res = cpl_image_iqe
00910             (ima, (int)(pos_x[iframe]-10.0), (int)(pos_y[iframe]-10.0),
00911              (int)(pos_x[iframe]+10.0), (int)(pos_y[iframe]+10.0));
00912         if (iqe_res == NULL)
00913         {
00914             cpl_msg_debug(__func__,"Cannot compute FWHM for chip %d",
00915                           labels[iframe]);
00916             fwhm_x = fwhm_y = -1.0 ;
00917             cpl_error_reset() ;
00918         } else {
00919             fwhm_x = cpl_vector_get(cpl_bivector_get_x(iqe_res), 2) ;
00920             fwhm_y = cpl_vector_get(cpl_bivector_get_x(iqe_res), 3) ;
00921             cpl_bivector_delete(iqe_res) ;
00922         }
00923 
00924         /* Determine the radii */
00925         r = hawki_cal_zpoint_config.phot_star_radius ;
00926         if (r < 0) {
00927             if (fwhm_x>0 && fwhm_y>0)   r = 5*(fwhm_x+fwhm_y)/2.0 ;
00928             else                        r = HAWKI_PHOT_STAR_RADIUS ;
00929         }
00930         r1 = hawki_cal_zpoint_config.phot_bg_r1 ;
00931         r2 = hawki_cal_zpoint_config.phot_bg_r2 ;
00932         if (r1 < 0) r1 = r + 10.0 ;
00933         if (r2 < 0) r2 = r1 + 20.0 ;
00934         //cpl_msg_info(__func__, "Use radii for star: %g and background: %g, %g",
00935         //        r, r1, r2) ;
00936 
00937         /* Compute the photometry */
00938         /* Background */
00939         bgd = irplib_strehl_ring_background(ima, (int)(pos_x[iframe]),
00940                 (int)(pos_y[iframe]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN) ;
00941         /* Flux */
00942         fl = irplib_strehl_disk_flux(ima,
00943                 (int)(pos_x[iframe]), (int)(pos_y[iframe]), (int)r, bgd);
00944         /* Zero Point */
00945         if ((fl < 0) || (dit < 0))     zp = -1.0 ;
00946         else                    zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
00947         //cpl_msg_info(__func__, "Zero point in chip %d:   %g", labels[iframe], zp) ;
00948         /* Peak */
00949         peak = cpl_image_get_max_window(ima,
00950                 (int)(pos_x[iframe]-5), (int)(pos_y[iframe]-5),
00951                 (int)(pos_x[iframe]+5), (int)(pos_y[iframe]+5));
00952 
00953         hawki_cal_zpoint_outputs.zpoint[labels[iframe]-1] = zp;
00954         hawki_cal_zpoint_outputs.atx0[labels[iframe]-1] = zp + 
00955             extinction * hawki_cal_zpoint_outputs.airmass[labels[iframe]-1];
00956         hawki_cal_zpoint_outputs.posx[labels[iframe]-1] = pos_x[iframe];
00957         hawki_cal_zpoint_outputs.posy[labels[iframe]-1] = pos_y[iframe];
00958         hawki_cal_zpoint_outputs.flux[labels[iframe]-1] = fl;
00959         hawki_cal_zpoint_outputs.instrmag[labels[iframe]-1] = 2.5 * log10(fl/dit);
00960         hawki_cal_zpoint_outputs.peak[labels[iframe]-1] = peak;
00961         hawki_cal_zpoint_outputs.bgd[labels[iframe]-1] = bgd;
00962         hawki_cal_zpoint_outputs.fwhmx[labels[iframe]-1] = fwhm_x;
00963         hawki_cal_zpoint_outputs.fwhmy[labels[iframe]-1] = fwhm_y;
00964         if (fwhm_x > 0 && fwhm_y > 0)
00965             hawki_cal_zpoint_outputs.fwhm[labels[iframe]-1] = sqrt(fwhm_x*fwhm_y);
00966         else
00967             hawki_cal_zpoint_outputs.fwhm[labels[iframe]-1] = -1.0;
00968         hawki_cal_zpoint_outputs.fwhmx_as[labels[iframe]-1] = fwhm_x * pixscale;
00969         hawki_cal_zpoint_outputs.fwhmy_as[labels[iframe]-1] = fwhm_y * pixscale;
00970         if (fwhm_x > 0 && fwhm_y > 0)
00971             hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] =
00972                 sqrt(fwhm_x*fwhm_y*pixscale*pixscale);
00973         else
00974             hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] = -1.0;
00975 
00976     }
00977 
00978     /* Create the table */
00979     zpoint = hawki_table_new(1);
00980     //tab = cpl_table_new(nframes) ;
00981     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
00982         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT);
00983         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, CPL_TYPE_STRING);
00984         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE);
00985         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSX,"pix");
00986         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE);
00987         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSY,"pix");
00988         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE);
00989         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ZPOINT,"mag");
00990         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE);
00991         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ATX0,"mag");
00992         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE);
00993         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE);
00994         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FLUX,"ADU");
00995         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, CPL_TYPE_DOUBLE);
00996         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_INSTRMAG,"log(ADU/s)");
00997         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING);
00998         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE);
00999         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_PEAK,"ADU");
01000         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE);
01001         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_BGD,"ADU");
01002         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE);
01003         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX,"pix");
01004         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE);
01005         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY,"pix");
01006         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE);
01007         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM,"pix");
01008         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE);
01009         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX_AS,"arcsec");
01010         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE);
01011         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY_AS,"arcsec");
01012         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE);
01013         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM_AS,"arcsec");
01014         cpl_table_new_column(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, CPL_TYPE_DOUBLE);
01015         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_STARMAG,"mag");
01016         
01017         cpl_table_set_int(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, 0, idet+1) ;
01018         cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, 0,
01019                 hawki_cal_zpoint_outputs.starname) ;
01020         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSX, 0,
01021                 hawki_cal_zpoint_outputs.posx[idet]) ;
01022         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSY, 0,
01023                 hawki_cal_zpoint_outputs.posy[idet]) ;
01024         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, 0,
01025                 hawki_cal_zpoint_outputs.zpoint[idet]) ;
01026         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, 0,
01027                 hawki_cal_zpoint_outputs.atx0[idet]) ;
01028         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, 0,
01029                 hawki_cal_zpoint_outputs.airmass[idet]) ;
01030         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, 0,
01031                 hawki_cal_zpoint_outputs.flux[idet]) ;
01032         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, 0,
01033                 hawki_cal_zpoint_outputs.instrmag[idet]) ;
01034         cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, 0,
01035                 hawki_cal_zpoint_outputs.filter);
01036         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, 0,
01037                 hawki_cal_zpoint_outputs.peak[idet]) ;
01038         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_BGD, 0,
01039                 hawki_cal_zpoint_outputs.bgd[idet]) ;
01040         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, 0,
01041                 hawki_cal_zpoint_outputs.fwhmx[idet]) ;
01042         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, 0,
01043                 hawki_cal_zpoint_outputs.fwhmy[idet]) ;
01044         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, 0,
01045                 hawki_cal_zpoint_outputs.fwhm[idet]) ;
01046         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, 0,
01047                 hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
01048         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, 0,
01049                 hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
01050         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, 0,
01051                 hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
01052         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, 0,
01053                 hawki_cal_zpoint_config.magnitude);
01054     }
01055 
01056     /* Mean values */
01057     hawki_cal_zpoint_outputs.mean_zpoint = 0.0 ;
01058     hawki_cal_zpoint_outputs.mean_atx0 = 0.0 ;
01059     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01060         hawki_cal_zpoint_outputs.mean_zpoint +=
01061             hawki_cal_zpoint_outputs.zpoint[idet] ;
01062         hawki_cal_zpoint_outputs.mean_atx0  +=
01063             hawki_cal_zpoint_outputs.atx0[idet] ;
01064     }
01065     hawki_cal_zpoint_outputs.mean_zpoint /= HAWKI_NB_DETECTORS ;
01066     hawki_cal_zpoint_outputs.mean_atx0 /= HAWKI_NB_DETECTORS ;
01067 
01068     /* Output results */
01069     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01070     {
01071         cpl_msg_info(__func__, "Zero point [at airmass=1] in chip %d:", idet + 1);
01072         cpl_msg_indent_more();
01073         cpl_msg_info(__func__,"   ZP: %g [%g]",
01074                      hawki_cal_zpoint_outputs.zpoint[idet],
01075                      hawki_cal_zpoint_outputs.atx0[idet]);
01076         cpl_msg_info(__func__,"   Flux of star: %f",
01077                      hawki_cal_zpoint_outputs.flux[idet]);
01078         cpl_msg_indent_less();
01079     }
01080 
01081     return zpoint;
01082 }
01083 
01084 /*----------------------------------------------------------------------------*/
01092 /*----------------------------------------------------------------------------*/
01093 static int hawki_cal_zpoint_get_mag(
01094         const char  *       stdstars,
01095         double              pointing_ra,
01096         double              pointing_dec,
01097         hawki_band          band)
01098 {
01099     double           star_mag ;
01100     char             star_name[512] ;
01101     char             cat_name[512] ;
01102     char             star_type[32] ;
01103     double           stdstar_ra;
01104     double           stdstar_dec;
01105     int              ret ;
01106     cpl_errorstate   error_prevstate = cpl_errorstate_get();
01107 
01108     /* Initialise */
01109     ret = -1 ;
01110 
01111     irplib_stdstar_find_star
01112         (stdstars, pointing_ra, pointing_dec,
01113          hawki_std_band_name(HAWKI_BAND_H), "all", &hawki_cal_zpoint_config.mag_H,
01114          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01115     irplib_stdstar_find_star
01116         (stdstars, pointing_ra, pointing_dec,
01117          hawki_std_band_name(HAWKI_BAND_J), "all", &hawki_cal_zpoint_config.mag_J,
01118          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01119     irplib_stdstar_find_star
01120         (stdstars, pointing_ra, pointing_dec,
01121          hawki_std_band_name(HAWKI_BAND_K), "all", &hawki_cal_zpoint_config.mag_K,
01122          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01123     irplib_stdstar_find_star
01124         (stdstars, pointing_ra, pointing_dec,
01125          hawki_std_band_name(HAWKI_BAND_Y), "all", &hawki_cal_zpoint_config.mag_Y,
01126          star_name, star_type, &stdstar_ra, &stdstar_dec, 5.0);
01127 
01128     switch (band) {
01129         /* SW mode */
01130         case HAWKI_BAND_H:
01131             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_H;
01132             break ;
01133         case HAWKI_BAND_J:
01134             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_J;
01135             break ;
01136         case HAWKI_BAND_K:
01137             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_K;
01138             break ;
01139         case HAWKI_BAND_Y:
01140             hawki_cal_zpoint_config.magnitude = hawki_cal_zpoint_config.mag_Y;
01141             break ;
01142         default:
01143             cpl_msg_error(__func__, "cannot determine associated filter") ;
01144             return -1 ;
01145     }
01146 
01147     /* If the magnitude still is not known, abort */
01148     if(!cpl_errorstate_is_equal(error_prevstate))
01149     {
01150         cpl_msg_error(__func__, "%s", cpl_error_get_message());
01151         return -1 ;
01152     }
01153     cpl_msg_info(__func__, "Found catalog star %s", star_name) ;
01154 
01155     /* Store results */
01156     strcpy(hawki_cal_zpoint_outputs.starname, star_name) ;
01157     strcpy(hawki_cal_zpoint_outputs.sptype, star_type) ;
01158     strcpy(hawki_cal_zpoint_outputs.catalog, cat_name) ;
01159     hawki_cal_zpoint_outputs.stdstar_ra = stdstar_ra;
01160     hawki_cal_zpoint_outputs.stdstar_dec = stdstar_dec;
01161 
01162     return 0 ;
01163 }
01164 
01165 /*----------------------------------------------------------------------------*/
01174 /*----------------------------------------------------------------------------*/
01175 static int hawki_cal_zpoint_save
01176 (cpl_table           **  zpoint_tables,
01177  int                 *   labels,
01178  cpl_imagelist       *   images,
01179  cpl_table           **  raw_zpoint_stats,
01180  cpl_frameset        *   zpoint_frames,
01181  cpl_frameset        *   calib_frames,
01182  const cpl_frame     *   stars_frame,
01183  cpl_parameterlist   *   parlist,
01184  cpl_frameset        *   set)
01185 {
01186     cpl_propertylist    **  qclists ;
01187     cpl_propertylist    *   wcslist;
01188     cpl_frameset        *   used_frames;
01189     const char          *   ref_filename;
01190     cpl_propertylist    *   inputlist ;
01191     int                     ext_nb, nframes ;
01192     const char          *   recipe_name = "hawki_cal_zpoint" ;
01193     int                     idet;
01194     int                     iframe;
01195     cpl_errorstate          error_prevstate = cpl_errorstate_get();
01196 
01197 
01198     /* Initialise */
01199     nframes = cpl_frameset_get_size(set) ;
01200 
01201     /* Get the reference frame */
01202     ref_filename = hawki_get_extref_file(set);
01203 
01204     /* Create the QC lists for the extensions */
01205     qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
01206     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01207     {
01208         int            this_iframe = -1;
01209 
01210         qclists[idet] = cpl_propertylist_new() ;
01211         /* These QC are common to all extensions */
01212         cpl_propertylist_append_string(qclists[idet], "ESO QC FILTER OBS",
01213                 hawki_cal_zpoint_outputs.filter) ;
01214         cpl_propertylist_append_string(qclists[idet], "ESO QC FILTER REF",
01215                 hawki_std_band_name(hawki_cal_zpoint_outputs.band)) ;
01216         cpl_propertylist_append_double(qclists[idet], "ESO QC AMBI RHUM AVG",
01217                 hawki_cal_zpoint_outputs.humidity);
01218         cpl_propertylist_append_double(qclists[idet], "ESO QC AIRMASS MEAN",
01219                 hawki_cal_zpoint_outputs.mean_airmass) ;
01220         cpl_propertylist_append_string(qclists[idet], "ESO QC STDNAME",
01221                 hawki_cal_zpoint_outputs.starname) ;
01222         cpl_propertylist_append_string(qclists[idet], "ESO QC SPECTYPE",
01223                 hawki_cal_zpoint_outputs.sptype) ;
01224         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG",
01225                 hawki_cal_zpoint_config.magnitude) ;
01226         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG H",
01227                 hawki_cal_zpoint_config.mag_H) ;
01228         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG J",
01229                 hawki_cal_zpoint_config.mag_J) ;
01230         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG K",
01231                 hawki_cal_zpoint_config.mag_K) ;
01232         cpl_propertylist_append_double(qclists[idet], "ESO QC STARMAG Y",
01233                 hawki_cal_zpoint_config.mag_Y) ;
01234         cpl_propertylist_append_string(qclists[idet], "ESO QC CATNAME",
01235                 hawki_cal_zpoint_outputs.catalog) ;
01236         cpl_propertylist_append_double(qclists[idet], "ESO QC DATANCOM",
01237                 nframes) ;
01238         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT MEAN",
01239                                        hawki_cal_zpoint_outputs.mean_zpoint) ;
01240         cpl_propertylist_append_double(qclists[idet], "ESO QC ATX0 MEAN",
01241                                        hawki_cal_zpoint_outputs.mean_atx0) ;
01242         /* These QC are specific of the extension */
01243         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT",
01244                 hawki_cal_zpoint_outputs.zpoint[idet]) ;
01245         cpl_propertylist_append_double(qclists[idet], "ESO QC ATX0",
01246                 hawki_cal_zpoint_outputs.atx0[idet]) ;
01247         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT POSX",
01248                 hawki_cal_zpoint_outputs.posx[idet]) ;
01249         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT POSY",
01250                 hawki_cal_zpoint_outputs.posy[idet]) ;
01251         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FLUX",
01252                 hawki_cal_zpoint_outputs.flux[idet]) ;
01253         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT PEAK",
01254                 hawki_cal_zpoint_outputs.peak[idet]) ;
01255         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT BGD",
01256                 hawki_cal_zpoint_outputs.bgd[idet]) ;
01257         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMX",
01258                 hawki_cal_zpoint_outputs.fwhmx[idet]) ;
01259         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMY",
01260                 hawki_cal_zpoint_outputs.fwhmy[idet]) ;
01261         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHM",
01262                 hawki_cal_zpoint_outputs.fwhm[idet]) ;
01263         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMX_AS",
01264                 hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
01265         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHMY_AS",
01266                 hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
01267         cpl_propertylist_append_double(qclists[idet], "ESO QC ZPOINT FWHM_AS",
01268                 hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
01269 
01270         /* Propagate some keywords from input raw frame extensions */
01271         ext_nb=hawki_get_ext_from_detector(ref_filename, idet+1);
01272         inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
01273                 HAWKI_HEADER_EXT_FORWARD, 0) ;
01274         cpl_propertylist_append(qclists[idet], inputlist) ;
01275         cpl_propertylist_delete(inputlist) ;
01276 
01277         /* Propagate WCS keywords */
01278         for(iframe=0; iframe<cpl_frameset_get_size(zpoint_frames); iframe++)
01279             if(labels[iframe] == idet + 1)
01280                 this_iframe = iframe;
01281         wcslist = cpl_propertylist_load_regexp
01282             (cpl_frame_get_filename(cpl_frameset_get_frame(zpoint_frames, this_iframe)),
01283                                     ext_nb, HAWKI_HEADER_WCS, 0);
01284         cpl_propertylist_copy_property_regexp
01285             (qclists[idet], wcslist, HAWKI_HEADER_WCS, 0);
01286         cpl_propertylist_delete(wcslist) ;
01287     }
01288     
01289     /* Write the zpoint table  */
01290     used_frames = cpl_frameset_duplicate(zpoint_frames);
01291     for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
01292         cpl_frameset_insert(used_frames, cpl_frame_duplicate(
01293                 cpl_frameset_get_frame(calib_frames, iframe)));
01294     cpl_frameset_insert(used_frames, cpl_frame_duplicate(stars_frame));
01295     hawki_tables_save(set,
01296                       parlist,
01297                       used_frames,
01298                       (const cpl_table **)zpoint_tables,
01299                       recipe_name,
01300                       HAWKI_CALPRO_ZPOINT_TAB,
01301                       HAWKI_PROTYPE_ZPOINT_TAB,
01302                       NULL,
01303                       (const cpl_propertylist **)qclists,
01304                       "hawki_cal_zpoint.fits");
01305     cpl_frameset_delete(used_frames);
01306     
01307     /* Statistics of the raw images in the QC */
01308     hawki_image_stats_stats(raw_zpoint_stats, qclists);
01309 
01310     /* Write the table with the raw zpoint objects statistics */
01311     used_frames = cpl_frameset_duplicate(zpoint_frames);
01312     hawki_tables_save(set,
01313                       parlist,
01314                       used_frames,
01315                       (const cpl_table **)raw_zpoint_stats,
01316                       recipe_name,
01317                       HAWKI_CALPRO_ZPOINT_STATS,
01318                       HAWKI_PROTYPE_ZPOINT_STATS,
01319                       NULL,
01320                       (const cpl_propertylist **)qclists,
01321                       "hawki_cal_zpoint_stats.fits");
01322 
01323     /* Write the images  */
01324     for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
01325         cpl_frameset_insert(used_frames, cpl_frame_duplicate(
01326                 cpl_frameset_get_frame(calib_frames, iframe)));
01327     hawki_imagelist_save(set,
01328                          parlist,
01329                          used_frames,
01330                          images,
01331                          recipe_name,
01332                          HAWKI_CALPRO_ZPOINT_IMA,
01333                          HAWKI_PROTYPE_ZPOINT_IMA,
01334                          NULL,
01335                          (const cpl_propertylist**)qclists,
01336                          "hawki_cal_zpoint_check.fits") ;
01337     cpl_frameset_delete(used_frames);
01338 
01339     /* Remove the keywords for the FITS extensions */
01340     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01341         cpl_propertylist_erase_regexp(qclists[idet], HAWKI_HEADER_EXT_FORWARD, 0) ;
01342     }
01343 
01344     /* Free */
01345     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01346         cpl_propertylist_delete(qclists[idet]) ;
01347     }
01348     cpl_free(qclists) ;
01349 
01350     /* Free */
01351     if(!cpl_errorstate_is_equal(error_prevstate))
01352     {
01353         cpl_errorstate_set(CPL_ERROR_NONE);
01354         return -1;
01355     }
01356     return  0;
01357 }
01358 
01359 /*----------------------------------------------------------------------------*/
01365 /*----------------------------------------------------------------------------*/
01366 static int hawki_cal_zpoint_compute_keywords(
01367         cpl_frameset    *   set,
01368         int             *   labels)
01369 {
01370     int                     nframes ;
01371     cpl_vector          *   hum_vec ;
01372     cpl_frame           *   cur_frame ;
01373     cpl_propertylist    *   plist ;
01374     int                     iframe;
01375 
01376     /* Test inputs  */
01377     if (set == NULL) return -1 ;
01378 
01379     /* Initialize */
01380     nframes = cpl_frameset_get_size(set) ;
01381     hawki_cal_zpoint_outputs.mean_airmass   = 0.0 ;
01382 
01383     hum_vec = cpl_vector_new(nframes) ;
01384 
01385     for (iframe=0 ; iframe<nframes ; iframe++) {
01386         if (cpl_error_get_code()) {
01387             cpl_vector_delete(hum_vec) ;
01388             return -1 ;
01389         }
01390         cur_frame = cpl_frameset_get_frame(set, iframe) ;
01391         plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
01392         if (iframe==0) 
01393             hawki_cal_zpoint_outputs.mean_airmass += 
01394                 hawki_pfits_get_airmass_start(plist) ; 
01395         if (iframe==nframes-1) 
01396             hawki_cal_zpoint_outputs.mean_airmass += 
01397                 hawki_pfits_get_airmass_end(plist);
01398         hawki_cal_zpoint_outputs.airmass[labels[iframe] - 1] = 
01399             (hawki_pfits_get_airmass_start(plist) + 
01400              hawki_pfits_get_airmass_end(plist)) / 2.;
01401         cpl_vector_set(hum_vec,  iframe, hawki_pfits_get_humidity_level(plist));
01402         cpl_propertylist_delete(plist) ;
01403         if (cpl_error_get_code()) {
01404             cpl_vector_delete(hum_vec) ;
01405             cpl_error_reset() ;
01406             return -1 ;
01407         }
01408     }
01409     hawki_cal_zpoint_outputs.humidity  = cpl_vector_get_mean(hum_vec) ;
01410     hawki_cal_zpoint_outputs.mean_airmass /= 2 ;
01411 
01412     /* Free and return */
01413     cpl_vector_delete(hum_vec) ;
01414     if (cpl_error_get_code()) return -1 ;
01415     return 0 ;
01416 }
01417 
01418 static cpl_error_code hawki_cal_zpoint_get_expected_pos
01419 (cpl_frameset * set,
01420  int          * labels)
01421 {
01422     const char *  filename;
01423     int           iframe;
01424 
01425     for(iframe=0 ; iframe<HAWKI_NB_DETECTORS ; iframe++)
01426     {
01427         cpl_propertylist * wcs_plist;
01428         cpl_wcs          * wcs;
01429         int                idet;
01430 
01431         idet = labels[iframe];
01432         if(hawki_cal_zpoint_config.xcoord[idet - 1] == -1 ||
01433                 hawki_cal_zpoint_config.ycoord[idet - 1] == -1)
01434         {
01435             filename = cpl_frame_get_filename
01436                 (cpl_frameset_get_frame_const(set, iframe));
01437             wcs_plist = cpl_propertylist_load
01438                 (filename, hawki_get_ext_from_detector(filename, idet));
01439             wcs = cpl_wcs_new_from_propertylist(wcs_plist);
01440             cpl_propertylist_delete(wcs_plist);
01441             if(wcs == NULL)
01442             {
01443                 cpl_msg_error(__func__, "Could not get WCS info");
01444                 cpl_wcs_delete(wcs);
01445                 return CPL_ERROR_ILLEGAL_INPUT;
01446             }
01447             if(irplib_wcs_radectoxy(wcs,
01448                                     hawki_cal_zpoint_outputs.stdstar_ra,
01449                                     hawki_cal_zpoint_outputs.stdstar_dec,
01450                                     &(hawki_cal_zpoint_config.xcoord[idet - 1]),
01451                                     &(hawki_cal_zpoint_config.ycoord[idet - 1]))
01452                     != CPL_ERROR_NONE)
01453             {
01454                 cpl_msg_error(__func__,"Could not get the expected position of star");
01455                 cpl_wcs_delete(wcs);
01456                 return CPL_ERROR_UNSPECIFIED;
01457             }
01458 
01459             /* Free */
01460             cpl_wcs_delete(wcs);
01461         }
01462     }
01463 
01464     return 0;
01465 }
01466 
01467 
01468 static void hawki_cal_zpoint_output_init(void)
01469 {
01470     int idet;
01471 
01472     hawki_cal_zpoint_outputs.starname[0] = (char)0 ;
01473     hawki_cal_zpoint_outputs.sptype[0] = (char)0 ;
01474     hawki_cal_zpoint_outputs.filter[0] = (char)0 ;
01475     hawki_cal_zpoint_outputs.catalog[0] = (char)0 ;
01476     hawki_cal_zpoint_outputs.pixscale = -1.0 ;
01477     hawki_cal_zpoint_outputs.dit = -1.0 ;
01478     hawki_cal_zpoint_outputs.humidity = -1.0 ;
01479     hawki_cal_zpoint_outputs.mean_airmass = -1.0 ;
01480     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01481     {
01482         hawki_cal_zpoint_outputs.airmass[idet] = -1.0 ;
01483         hawki_cal_zpoint_outputs.zpoint[idet] = -1.0 ;
01484         hawki_cal_zpoint_outputs.atx0[idet] = -1.0 ;
01485         hawki_cal_zpoint_outputs.posx[idet] = -1.0 ;
01486         hawki_cal_zpoint_outputs.posy[idet] = -1.0 ;
01487         hawki_cal_zpoint_outputs.flux[idet] = -1.0 ;
01488         hawki_cal_zpoint_outputs.peak[idet] = -1.0 ;
01489         hawki_cal_zpoint_outputs.bgd[idet] = -1.0 ;
01490         hawki_cal_zpoint_outputs.fwhmx[idet] = -1.0 ;
01491         hawki_cal_zpoint_outputs.fwhmy[idet] = -1.0 ;
01492         hawki_cal_zpoint_outputs.fwhm[idet] = -1.0 ;
01493         hawki_cal_zpoint_outputs.fwhmx_as[idet] = -1.0 ;
01494         hawki_cal_zpoint_outputs.fwhmy_as[idet] = -1.0 ;
01495         hawki_cal_zpoint_outputs .fwhm_as[idet] = -1.0 ;
01496     }
01497 }
01498 
01499 int hawki_cal_zpoint_retrieve_input_param
01500 (cpl_parameterlist  *  parlist)
01501 {
01502     cpl_parameter   *   par ;
01503     const char      *   sval ;
01504 
01505     par = NULL ;
01506 
01507     /* --ra */
01508     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ra") ;
01509     hawki_cal_zpoint_config.target_ra = cpl_parameter_get_double(par) ;
01510     /* --dec */
01511     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.dec") ;
01512     hawki_cal_zpoint_config.target_dec = cpl_parameter_get_double(par) ;
01513     /* --mag */
01514     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.mag") ;
01515     hawki_cal_zpoint_config.magnitude = cpl_parameter_get_double(par) ;
01516     /* --detect_sigma */
01517     par = cpl_parameterlist_find(parlist,"hawki.hawki_cal_zpoint.detect_sigma");
01518     hawki_cal_zpoint_config.detect_sigma = cpl_parameter_get_double(par) ;
01519     /* --sx */
01520     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sx") ;
01521     hawki_cal_zpoint_config.sx = cpl_parameter_get_int(par) ;
01522     /* --sy */
01523     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sy") ;
01524     hawki_cal_zpoint_config.sy = cpl_parameter_get_int(par) ;
01525     /* --star_r */
01526     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.star_r") ;
01527     hawki_cal_zpoint_config.phot_star_radius = cpl_parameter_get_double(par) ;
01528     /* --bg_r1 */
01529     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r1") ;
01530     hawki_cal_zpoint_config.phot_bg_r1 = cpl_parameter_get_double(par) ;
01531     /* --bg_r2 */
01532     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r2") ;
01533     hawki_cal_zpoint_config.phot_bg_r2 = cpl_parameter_get_double(par) ;
01534     /* --xcoord */
01535     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.xcoord");
01536     sval = cpl_parameter_get_string(par);
01537     if (sscanf(sval, "%lf,%lf,%lf,%lf",
01538                hawki_cal_zpoint_config.xcoord,
01539                hawki_cal_zpoint_config.xcoord+1,
01540                hawki_cal_zpoint_config.xcoord+2,
01541                hawki_cal_zpoint_config.xcoord+3)!=4)
01542     {
01543         return -1;
01544     }
01545     /* --ycoord */
01546     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ycoord");
01547     sval = cpl_parameter_get_string(par);
01548     if (sscanf(sval, "%lf,%lf,%lf,%lf",
01549                hawki_cal_zpoint_config.ycoord,
01550                hawki_cal_zpoint_config.ycoord+1,
01551                hawki_cal_zpoint_config.ycoord+2,
01552                hawki_cal_zpoint_config.ycoord+3)!=4)
01553     {
01554         return -1;
01555     }
01556 
01557     return 0;
01558 }
01559 
01560 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist)
01561 {
01562     if(hawki_pfits_get_targ_epoch(plist) != 2000. ||
01563        hawki_pfits_get_targ_equinox(plist) != 2000.)
01564     {
01565         cpl_msg_error(__func__,"Epoch and equinox must be 2000.");
01566         return -1;
01567     }
01568     else
01569         return 0;
01570 }

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