HAWKI Pipeline Reference Manual 1.8.6
hawki_cal_zpoint.c
00001 /* $Id: hawki_cal_zpoint.c,v 1.33 2011/12/22 15:24:24 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/12/22 15:24:24 $
00024  * $Revision: 1.33 $
00025  * $Name: hawki-1_8_6 $
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 int hawki_cal_zpoint_compute_qc
00087 (cpl_propertylist *   qcmainparams, 
00088  cpl_propertylist **  qcextparams,
00089  cpl_frameset     *   set);
00090 static cpl_table ** hawki_cal_zpoint_photom
00091 (cpl_imagelist       *   ilist,
00092  cpl_bivector        *   pos,
00093  int                 *   labels);
00094 static int hawki_cal_zpoint_get_mag(const char *, double, double, hawki_band) ;
00095 static int hawki_cal_zpoint_compute_keywords
00096 (cpl_frameset * set, 
00097  int          * labels);
00098 static cpl_error_code  hawki_cal_zpoint_get_expected_pos
00099 (cpl_frameset * set,
00100  int          * labels);
00101 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist);
00102 
00103 /*-----------------------------------------------------------------------------
00104                             Static variables
00105  -----------------------------------------------------------------------------*/
00106 
00107 static struct {
00108     /* Inputs */
00109     double      xcoord[HAWKI_NB_DETECTORS] ;
00110     double      ycoord[HAWKI_NB_DETECTORS] ;
00111     double      target_ra;
00112     double      target_dec;
00113     double      stdstar_given_magnitude;
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      ext_coeff;
00151     double      stdstar_ra;
00152     double      stdstar_dec;
00153     double      stdstar_mag_filter;
00154     double      stdstar_mag_H;
00155     double      stdstar_mag_J;
00156     double      stdstar_mag_K;
00157     double      stdstar_mag_Y;
00158     int         stdstar_incat_found;
00159     int         stdstar_mag_available;
00160     int         stdstar_image_detected[HAWKI_NB_DETECTORS];
00161     int         zpoint_computable[HAWKI_NB_DETECTORS];
00162     int         zpoint_mean_computable;
00163 } hawki_cal_zpoint_outputs;
00164 
00165 static char hawki_cal_zpoint_description[] =
00166 "hawki_cal_zpoint -- Zero point recipe\n"
00167 "The input of the recipe files listed in the Set Of Frames (sof-file)\n"
00168 "must be tagged as:\n"
00169 "raw-file.fits "HAWKI_CAL_ZPOINT_RAW" or\n"
00170 "stdstars-file.fits "HAWKI_CALPRO_STDSTARS" or\n"
00171 "flat-file.fits "HAWKI_CALPRO_FLAT" or\n"
00172 "bpm-file.fits "HAWKI_CALPRO_BPM"\n"
00173 "The recipe creates as an output:\n"
00174 "hawki_cal_zpoint.fits ("HAWKI_CALPRO_ZPOINT_TAB"): Zero point solution table\n"
00175 "hawki_cal_zpoint_check.fits ("HAWKI_CALPRO_ZPOINT_IMA"): Standard star images corrected (for checking purposes)\n"
00176 "hawki_cal_zpoint_stats.fits ("HAWKI_CALPRO_ZPOINT_STATS"): Statistics of the raw standard star images\n"
00177 "Return code:\n"
00178 "esorex exits with an error code of 0 if the recipe completes successfully\n"
00179 "or 1 otherwise";
00180 
00181 /*-----------------------------------------------------------------------------
00182                                 Functions code
00183  -----------------------------------------------------------------------------*/
00184 
00185 /*----------------------------------------------------------------------------*/
00193 /*----------------------------------------------------------------------------*/
00194 int cpl_plugin_get_info(cpl_pluginlist * list)
00195 {
00196     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00197     cpl_plugin  *   plugin = &recipe->interface;
00198 
00199     cpl_plugin_init(plugin,
00200                     CPL_PLUGIN_API,
00201                     HAWKI_BINARY_VERSION,
00202                     CPL_PLUGIN_TYPE_RECIPE,
00203                     "hawki_cal_zpoint",
00204                     "Zero point computation recipe",
00205                     hawki_cal_zpoint_description,
00206                     "Cesar Enrique Garcia Dabo",
00207                     PACKAGE_BUGREPORT,
00208                     hawki_get_license(),
00209                     hawki_cal_zpoint_create,
00210                     hawki_cal_zpoint_exec,
00211                     hawki_cal_zpoint_destroy) ;
00212 
00213     cpl_pluginlist_append(list, plugin) ;
00214 
00215     return 0;
00216 }
00217 
00218 /*----------------------------------------------------------------------------*/
00227 /*----------------------------------------------------------------------------*/
00228 static int hawki_cal_zpoint_create(cpl_plugin * plugin)
00229 {
00230     cpl_recipe      * recipe ;
00231     cpl_parameter   * p ;
00232 
00233     /* Get the recipe out of the plugin */
00234     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00235         recipe = (cpl_recipe *)plugin ;
00236     else return -1 ;
00237 
00238     /* Create the parameters list in the cpl_recipe object */
00239     recipe->parameters = cpl_parameterlist_new();
00240     if (recipe->parameters == NULL)
00241         return 1;
00242 
00243     /* Fill the parameters list */
00244     /* --detect_sigma */
00245     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.detect_sigma",
00246             CPL_TYPE_DOUBLE, "the sigma value for object detection",
00247             "hawki.hawki_cal_zpoint", 7.0);
00248     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detect_sigma") ;
00249     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00250     cpl_parameterlist_append(recipe->parameters, p) ;
00251     /* --star_r */
00252     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.star_r",CPL_TYPE_DOUBLE,
00253             "the star radius", "hawki.hawki_cal_zpoint", -1.0) ;
00254     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "star_r") ;
00255     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00256     cpl_parameterlist_append(recipe->parameters, p) ;
00257     /* --bg_r1 */
00258     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.bg_r1", CPL_TYPE_DOUBLE,
00259             "the internal background radius", "hawki.hawki_cal_zpoint", -1.0) ;
00260     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r1") ;
00261     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00262     cpl_parameterlist_append(recipe->parameters, p) ;
00263     /* --bg_r2 */
00264     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.bg_r2", CPL_TYPE_DOUBLE,
00265             "the external background radius", "hawki.hawki_cal_zpoint", -1.0) ;
00266     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r2") ;
00267     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00268     cpl_parameterlist_append(recipe->parameters, p) ;
00269     /* --ra */
00270     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.ra", CPL_TYPE_DOUBLE,
00271             "RA in degrees", "hawki.hawki_cal_zpoint", 999.0) ;
00272     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ra") ;
00273     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00274     cpl_parameterlist_append(recipe->parameters, p) ;
00275     /* --dec */
00276     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.dec", CPL_TYPE_DOUBLE,
00277             "DEC in degrees", "hawki.hawki_cal_zpoint", 999.0) ;
00278     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dec") ;
00279     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00280     cpl_parameterlist_append(recipe->parameters, p) ;
00281     /* --mag */
00282     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.mag", CPL_TYPE_DOUBLE,
00283             "magnitude", "hawki.hawki_cal_zpoint", 99.0) ;
00284     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mag") ;
00285     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00286     cpl_parameterlist_append(recipe->parameters, p) ;
00287     /* --sx */
00288     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.sx", CPL_TYPE_INT,
00289             "x half-size of the search box", "hawki.hawki_cal_zpoint", 100) ;
00290     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sx") ;
00291     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00292     cpl_parameterlist_append(recipe->parameters, p) ;
00293     /* --sy */
00294     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.sy", CPL_TYPE_INT,
00295             "y half-size of the search box", "hawki.hawki_cal_zpoint", 100) ;
00296     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sy") ;
00297     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00298     cpl_parameterlist_append(recipe->parameters, p) ;
00299     /* --xcoord */
00300     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.xcoord", CPL_TYPE_STRING,
00301             "Coordinates in X where the standard star is located. If -1 use WCS",
00302             "hawki.hawki_cal_zpoint", "-1., -1., -1., -1.");
00303     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xcoord") ;
00304     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00305     cpl_parameterlist_append(recipe->parameters, p) ;
00306     /* --ycoord */
00307     p = cpl_parameter_new_value("hawki.hawki_cal_zpoint.ycoord", CPL_TYPE_STRING,
00308             "Coordinates in Y where the standard star is located. If -1 use WCS",
00309             "hawki.hawki_cal_zpoint", "-1., -1., -1., -1.") ;
00310     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ycoord") ;
00311     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
00312     cpl_parameterlist_append(recipe->parameters, p) ;
00313 
00314     /* Return */
00315     return 0;
00316 }
00317 
00318 /*----------------------------------------------------------------------------*/
00324 /*----------------------------------------------------------------------------*/
00325 static int hawki_cal_zpoint_exec(cpl_plugin * plugin)
00326 {
00327     cpl_recipe  *   recipe ;
00328 
00329     /* Get the recipe out of the plugin */
00330     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00331         recipe = (cpl_recipe *)plugin ;
00332     else return -1 ;
00333 
00334     /* Issue a banner */
00335     hawki_print_banner();
00336 
00337     return hawki_cal_zpoint(recipe->parameters, recipe->frames) ;
00338 }
00339 
00340 /*----------------------------------------------------------------------------*/
00346 /*----------------------------------------------------------------------------*/
00347 static int hawki_cal_zpoint_destroy(cpl_plugin * plugin)
00348 {
00349     cpl_recipe  *   recipe ;
00350 
00351     /* Get the recipe out of the plugin */
00352     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00353         recipe = (cpl_recipe *)plugin ;
00354     else return -1 ;
00355 
00356     cpl_parameterlist_delete(recipe->parameters) ;
00357     return 0 ;
00358 }
00359 
00360 /*----------------------------------------------------------------------------*/
00367 /*----------------------------------------------------------------------------*/
00368 static int hawki_cal_zpoint(
00369         cpl_parameterlist   *   parlist,
00370         cpl_frameset        *   framelist)
00371 {
00372     cpl_parameter   *   par ;
00373     const char      *   flat ;
00374     const char      *   bpm ;
00375     const char      *   stdstars ;
00376     cpl_frameset    *   zpoint_frames ;
00377     cpl_frameset    *   calib_frames ;
00378     const cpl_frame *   stars_frame;
00379     cpl_table       **  raw_zpoint_stats;
00380     cpl_table       **  zpoint_tables;
00381     cpl_imagelist   *   std_star_images ;
00382     int             *   labels;
00383     int                 idet;
00384 
00385     /* Initialise Output */
00386     hawki_cal_zpoint_output_init();
00387     zpoint_frames = NULL ;
00388     par = NULL ;
00389 
00390     /* Retrieve input parameters */
00391     if(hawki_cal_zpoint_retrieve_input_param(parlist))
00392     {
00393         cpl_msg_error(__func__, "Wrong parameters");
00394         return -1;
00395     }
00396 
00397     /* Identify the RAW and CALIB frames in the input frameset */
00398     if (hawki_dfs_set_groups(framelist)) {
00399         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00400         return -1 ;
00401     }
00402 
00403     /* Retrieve calibration data */
00404     calib_frames = cpl_frameset_new();
00405     flat = hawki_extract_first_filename(framelist, HAWKI_CALPRO_FLAT) ;
00406     if(flat)
00407         cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
00408                 cpl_frameset_find_const(framelist, HAWKI_CALPRO_FLAT)));
00409     bpm = hawki_extract_first_filename(framelist, HAWKI_CALPRO_BPM) ;
00410     if(bpm)
00411         cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
00412                 cpl_frameset_find_const(framelist, HAWKI_CALPRO_BPM)));
00413 
00414     /* STD stars catalog requested */
00415     stars_frame = cpl_frameset_find_const(framelist, HAWKI_CALPRO_STDSTARS);
00416     if (stars_frame  == NULL) 
00417     {
00418         cpl_msg_error(__func__,"Cannot find the catalog in the input list (%s)",
00419                       HAWKI_CALPRO_STDSTARS);
00420         cpl_frameset_delete(calib_frames);
00421         return -1 ;
00422     }
00423     stdstars = cpl_frame_get_filename(stars_frame);
00424 
00425     /* Retrieve raw frames */
00426     if ((zpoint_frames = hawki_extract_frameset(framelist,
00427                     HAWKI_CAL_ZPOINT_RAW)) != NULL) {
00428     } else {
00429         cpl_msg_error(__func__, "Cannot find raw frames in the input list (%s)",
00430                       HAWKI_CAL_ZPOINT_RAW);
00431         cpl_frameset_delete(calib_frames);
00432         return -1 ;
00433     }
00434 
00435     /* Exactly 4 images are expected */
00436     if (cpl_frameset_get_size(zpoint_frames) != 4) {
00437         cpl_msg_error(__func__, "4 input raw frames are expected, not %d",
00438                 cpl_frameset_get_size(zpoint_frames)) ;
00439         cpl_frameset_delete(zpoint_frames) ;
00440         cpl_frameset_delete(calib_frames);
00441         return -1 ;
00442     }
00443 
00444     /* Create the statistics table */
00445     raw_zpoint_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00446     for(idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00447     {
00448         raw_zpoint_stats[idet] = cpl_table_new(
00449             cpl_frameset_get_size(zpoint_frames));
00450     }
00451     hawki_image_stats_initialize(raw_zpoint_stats);
00452 
00453     /* Compute the zpoint values */
00454     cpl_msg_info(__func__, "Reduce the data") ;
00455     cpl_msg_indent_more() ;
00456     labels = cpl_calloc(cpl_frameset_get_size(zpoint_frames), sizeof(int)) ;
00457     if ((zpoint_tables = hawki_cal_zpoint_reduce(zpoint_frames, stdstars, 
00458              bpm, flat, raw_zpoint_stats, labels, &std_star_images))==NULL)
00459     {
00460         cpl_msg_error(__func__, "Cannot reduce the data") ;
00461         cpl_frameset_delete(zpoint_frames) ;
00462         cpl_frameset_delete(calib_frames);
00463         hawki_table_delete(raw_zpoint_stats);
00464         cpl_free(labels);
00465         cpl_msg_indent_less() ;
00466         return -1 ;
00467     }
00468     cpl_msg_indent_less() ;
00469 
00470     /* Save the products */
00471     cpl_msg_info(__func__, "Save the products") ;
00472     cpl_msg_indent_more() ;
00473     if (hawki_cal_zpoint_save
00474             (zpoint_tables, labels, std_star_images, raw_zpoint_stats, 
00475              zpoint_frames, calib_frames, stars_frame,
00476              parlist, framelist) == -1)
00477     {
00478         cpl_msg_warning(__func__, "Data could not be saved. "
00479                         "Check permisions or disk space") ;
00480         cpl_frameset_delete(zpoint_frames);
00481         hawki_table_delete(zpoint_tables) ;
00482         cpl_imagelist_delete(std_star_images) ;
00483         cpl_frameset_delete(calib_frames);
00484         hawki_table_delete(raw_zpoint_stats);
00485         cpl_free(labels);
00486         cpl_msg_indent_less() ;
00487         return -1 ;
00488     }
00489     cpl_msg_indent_less() ;
00490 
00491     /* Free and return */
00492     cpl_frameset_delete(zpoint_frames);
00493     cpl_frameset_delete(calib_frames);
00494     cpl_imagelist_delete(std_star_images);
00495     hawki_table_delete(zpoint_tables);
00496     hawki_table_delete(raw_zpoint_stats);
00497     cpl_free(labels);
00498 
00499     /* Return */
00500     if (cpl_error_get_code())
00501     {
00502         cpl_msg_error(__func__,
00503                       "HAWK-I pipeline could not recover from previous errors");
00504         return -1 ;
00505     }
00506     else return 0 ;
00507 }
00508 
00509 /*----------------------------------------------------------------------------*/
00519 /*----------------------------------------------------------------------------*/
00520 static cpl_table ** hawki_cal_zpoint_reduce
00521 (cpl_frameset    *   set,
00522  const char      *   stdstars,
00523  const char      *   bpm,
00524  const char      *   flat,
00525  cpl_table       **  raw_zpoint_stats,
00526  int             *   labels,
00527  cpl_imagelist   **  star_images)
00528 {
00529     cpl_frame           *   cur_frame ;
00530     cpl_propertylist    *   plist ;
00531     const char          *   sval ;
00532     cpl_imagelist       *   star_images_frame_order ;
00533     int                     nima ;
00534     cpl_bivector        *   positions ;
00535     cpl_image           *   filt_ima ;
00536     cpl_mask            *   kernel;
00537     int                     size_x, size_y ;
00538     double                  pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist ;
00539     cpl_apertures       *   aperts ;
00540     cpl_table           **  zpoint_tables;
00541     cpl_image           *   tmp_ima ;
00542     int                     iaper;
00543     int                     idet;
00544     int                     iframe;
00545     int                     iframe_star = -1;
00546     int                     nframes;
00547     char                    rastr[32];
00548     char                    decstr[32];
00549     cpl_errorstate          error_prevstate;
00550     int                     return_code;
00551 
00552     /* Check inputs */
00553     if (set == NULL) return NULL ;
00554     if (stdstars == NULL) return NULL ;
00555     if (star_images == NULL) return NULL ;
00556 
00557     /* Get the filter name, DIT, Target RA and DEC  */
00558     error_prevstate = cpl_errorstate_get();
00559     cur_frame = cpl_frameset_get_frame(set, 0) ;
00560     plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
00561     if ((sval = hawki_pfits_get_filter(plist)) == NULL) return NULL ;
00562     else sprintf(hawki_cal_zpoint_outputs.filter, sval) ;
00563     if (hawki_cal_zpoint_config.target_ra > 998.0)
00564     {
00565         hawki_cal_zpoint_config.target_ra = hawki_pfits_get_targ_alpha(plist);
00566         //hawki_cal_zpoint_config.target_ra = hawki_pfits_get_ra(plist) -
00567         //hawki_pfits_get_cumoffseta(plist) / 3600.;// Not valid before Nov 2008
00568         if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
00569         {
00570             cpl_propertylist_delete(plist);
00571             return NULL;
00572         }
00573     }
00574     if (hawki_cal_zpoint_config.target_dec > 998.0)
00575     {
00576         hawki_cal_zpoint_config.target_dec = hawki_pfits_get_targ_delta(plist);
00577         //hawki_cal_zpoint_config.target_dec = hawki_pfits_get_dec(plist) -
00578         //hawki_pfits_get_cumoffsetd(plist) / 3600.;// Not valid before Nov 2008
00579         if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
00580         {
00581             cpl_propertylist_delete(plist);
00582             return NULL;
00583         }
00584     }
00585     hawki_cal_zpoint_outputs.dit = hawki_pfits_get_dit(plist) ;
00586     hawki_cal_zpoint_outputs.pixscale = hawki_pfits_get_pixscale(plist) ;
00587     cpl_propertylist_delete(plist) ;
00588     if(!cpl_errorstate_is_equal(error_prevstate))
00589     {
00590         cpl_msg_error(__func__, "Cannot get keywords from main header:");
00591         cpl_msg_indent_more();
00592         cpl_msg_error(__func__, "%s",cpl_error_get_message());
00593         cpl_msg_indent_less();
00594         return NULL ;
00595     }
00596     cpl_msg_info(__func__,"Searching catalog stars closest to target:");
00597     cpl_msg_indent_more();
00598     hawki_utils_ra2str(rastr, 32, hawki_cal_zpoint_config.target_ra);
00599     hawki_utils_dec2str(decstr, 32, hawki_cal_zpoint_config.target_dec);
00600     cpl_msg_info(__func__,"RA = %g (%s); DEC = %g (%s)",
00601                  hawki_cal_zpoint_config.target_ra, rastr,
00602                  hawki_cal_zpoint_config.target_dec, decstr);
00603     cpl_msg_info(__func__,"HAWK-I Filter: %s", hawki_cal_zpoint_outputs.filter);
00604     cpl_msg_indent_less();
00605 
00606     /* Get the band */
00607     if ((hawki_cal_zpoint_outputs.band =
00608                 hawki_get_band(hawki_cal_zpoint_outputs.filter)) ==
00609             HAWKI_BAND_UNKNOWN) {
00610         cpl_msg_error(__func__, "Cannot associate the filter %s to a band",
00611                 hawki_cal_zpoint_outputs.filter) ;
00612         return NULL ;
00613     }
00614 
00615     /* Get the standard star information from database */
00616     cpl_msg_indent_more();
00617     return_code = hawki_cal_zpoint_get_mag(stdstars,
00618             hawki_cal_zpoint_config.target_ra,
00619             hawki_cal_zpoint_config.target_dec,
00620             hawki_cal_zpoint_outputs.band);
00621     if (return_code == -1)
00622     {
00623         cpl_msg_error(__func__, "Could not open star database");
00624         return NULL ;
00625     }   
00626     else if(return_code == 1)
00627     {
00628         cpl_msg_warning(__func__,"No suitable star found in catalog");
00629         cpl_msg_warning(__func__,"Using the target coordinates "
00630                         "as the standard star coordinates: ");
00631         hawki_cal_zpoint_outputs.stdstar_ra = 
00632                 hawki_cal_zpoint_config.target_ra;
00633         hawki_cal_zpoint_outputs.stdstar_dec = 
00634                 hawki_cal_zpoint_config.target_dec;
00635         hawki_utils_ra2str(rastr, 32, hawki_cal_zpoint_outputs.stdstar_ra);
00636         hawki_utils_dec2str(decstr, 32, hawki_cal_zpoint_outputs.stdstar_dec);
00637         cpl_msg_info(__func__, " RA = %g (%s) ; DEC = %g (%s)",
00638                      hawki_cal_zpoint_outputs.stdstar_ra, rastr,
00639                      hawki_cal_zpoint_outputs.stdstar_dec, decstr);
00640     }
00641     else
00642     {    
00643         cpl_msg_info(__func__, "Catalog where the star was found: %s",
00644                      hawki_cal_zpoint_outputs.catalog);
00645         cpl_msg_info(__func__, "Star name: %s",
00646                      hawki_cal_zpoint_outputs.starname);
00647         if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
00648         {
00649             cpl_msg_info(__func__, "Star magnitude in filter %s : %g [mag]",
00650                     hawki_cal_zpoint_outputs.filter,
00651                     hawki_cal_zpoint_outputs.stdstar_mag_filter);
00652         }
00653         hawki_utils_ra2str(rastr, 32, hawki_cal_zpoint_outputs.stdstar_ra);
00654         hawki_utils_dec2str(decstr, 32, hawki_cal_zpoint_outputs.stdstar_dec);
00655         cpl_msg_info(__func__, "Star coordinates: RA = %g (%s) ; DEC = %g (%s)",
00656                      hawki_cal_zpoint_outputs.stdstar_ra, rastr,
00657                      hawki_cal_zpoint_outputs.stdstar_dec, decstr);
00658     }
00659     if (hawki_cal_zpoint_config.stdstar_given_magnitude < 98.0) 
00660     {
00661         hawki_cal_zpoint_outputs.stdstar_mag_available = 1;
00662         hawki_cal_zpoint_outputs.stdstar_mag_filter = 
00663                 hawki_cal_zpoint_config.stdstar_given_magnitude;
00664         cpl_msg_info(__func__, "Using user defined "
00665                      "star magnitude in filter %s : %g [mag]",
00666                      hawki_cal_zpoint_outputs.filter,
00667                      hawki_cal_zpoint_outputs.stdstar_mag_filter);
00668     }
00669     cpl_msg_indent_less();
00670 
00671     /* Labelise frames */
00672     cpl_msg_info(__func__, "Guessing which frame the STD is in for each chip");
00673     hawki_detectors_locate_star
00674         (set, hawki_cal_zpoint_outputs.stdstar_ra,
00675          hawki_cal_zpoint_outputs.stdstar_dec, labels);
00676     if (labels == NULL)
00677     {
00678         cpl_msg_error(__func__, "Cannot determine which frame the STD is on") ;
00679         return NULL ;
00680     }
00681 
00682     /* Compute the expected position of the star in pixels */
00683     /* This is stored in hawki_cal_zpoint_config.xcoord, ycoord */
00684     cpl_msg_indent_more();
00685     if(hawki_cal_zpoint_get_expected_pos(set, labels) != CPL_ERROR_NONE)
00686     {
00687         cpl_msg_error(__func__,"Could not determine where the star is located");
00688         cpl_msg_indent_less();
00689         return NULL;
00690     }
00691     cpl_msg_indent_less();
00692     
00693     /* Fetch the airmass and humidity */
00694     hawki_cal_zpoint_compute_keywords(set, labels);
00695 
00696     /* Create the positions vector */
00697     nima = cpl_frameset_get_size(set) ;
00698     positions = cpl_bivector_new(nima) ;
00699 
00700     /* Initialize */
00701     *star_images = cpl_imagelist_new();
00702 
00703     /* Loop on the detectors */
00704     nframes = cpl_frameset_get_size(set) ;
00705     cpl_msg_info(__func__,"Loop on the chips");
00706     cpl_msg_indent_more() ;
00707     for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00708     {
00709         cpl_imagelist * sky_images;
00710         cpl_image     * star_ima = NULL;
00711         cpl_image     * sky;
00712         cpl_image     * flat_im;
00713         int             ext_nb;
00714 
00715         cpl_msg_info(__func__, "Loading the chip %d", idet+1);
00716         cpl_msg_indent_more() ;
00717 
00718         /* Allocate */
00719         sky_images = cpl_imagelist_new();
00720 
00721         cpl_msg_indent_more() ;
00722         for (iframe=0 ; iframe<nframes ; iframe++)
00723         {
00724             cpl_image * ima_cur;
00725 
00726             /* Load the image */
00727             ima_cur = hawki_load_image(set, iframe, idet+1, CPL_TYPE_FLOAT) ;
00728             if(ima_cur == NULL)
00729             {
00730                 cpl_bivector_delete(positions) ;
00731                 cpl_imagelist_delete(*star_images);
00732                 cpl_imagelist_delete(sky_images) ;
00733                 cpl_msg_error(__func__, "Error reading image");
00734                 return NULL;
00735             }
00736             /* Get image statistics */
00737             size_x = cpl_image_get_size_x(ima_cur) ;
00738             size_y = cpl_image_get_size_y(ima_cur) ;
00739             if(hawki_image_stats_fill_from_image
00740                 (raw_zpoint_stats,
00741                  ima_cur,
00742                  1,
00743                  1,
00744                  size_x,
00745                  size_y,
00746                  idet,
00747                  iframe) !=0 )
00748             {
00749                 cpl_msg_error(__func__,"Cannot compute stats on ima %d det %d",
00750                               iframe+1, idet+1);
00751                 cpl_bivector_delete(positions) ;
00752                 cpl_imagelist_delete(*star_images);
00753                 cpl_imagelist_delete(sky_images) ;
00754                 cpl_image_delete(ima_cur);
00755                 cpl_msg_indent_less() ;
00756                 return NULL ;
00757             }
00758 
00759             /* Add the image to either the sky images or the star image */
00760             if(labels[iframe] == idet + 1)
00761             {
00762                 star_ima = ima_cur;
00763                 iframe_star = iframe;
00764             }
00765             else
00766                 cpl_imagelist_set(sky_images, ima_cur,
00767                                   cpl_imagelist_get_size(sky_images));
00768         }
00769         cpl_msg_indent_less();
00770 
00771         /* Create the sky */
00772         cpl_msg_info(__func__, "Correct for the sky");
00773         sky = cpl_imagelist_collapse_median_create(sky_images);
00774         cpl_imagelist_delete(sky_images) ;
00775 
00776         /* Subtract the sky */
00777         cpl_image_subtract(star_ima, sky) ;
00778         cpl_image_delete(sky) ;
00779 
00780         /* Divide by the flatfield if one is provided */
00781         if (flat) {
00782             cpl_msg_info(__func__, "Correct for the flat") ;
00783 
00784             /* Get the extension with the current chip */
00785             if ((ext_nb = hawki_get_ext_from_detector(flat, idet + 1)) == -1)
00786             {
00787                 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00788                               idet + 1);
00789                 cpl_imagelist_delete(*star_images) ;
00790                 cpl_bivector_delete(positions) ;
00791                 return NULL ;
00792             }
00793             /* Load */
00794             flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, ext_nb) ;
00795             cpl_image_divide(star_ima, flat_im) ;
00796             cpl_image_delete(flat_im) ;
00797         }
00798 
00799         /* Correct the bad pixels */
00800         if (bpm) {
00801             cpl_msg_info(__func__, "Correct for the bad pixels") ;
00802             if (hawki_bpm_calib(star_ima, bpm, idet + 1) == -1)
00803             {
00804                 cpl_msg_error(__func__, "Cannot correct the BPM for chip %d",
00805                               idet + 1);
00806                 cpl_imagelist_delete(*star_images) ;
00807                 cpl_bivector_delete(positions) ;
00808                 return NULL ;
00809             }
00810         }
00811 
00812         /* Put the result in the image list */
00813         cpl_imagelist_set(*star_images, star_ima,
00814                           cpl_imagelist_get_size(*star_images)) ;
00815 
00816         /* Object detection */
00817         cpl_msg_info(__func__,"For chip %d the STD should be on frame %d",
00818                      idet + 1, iframe_star+1);
00819         pos_x_cen = pos_y_cen = -1.0 ;
00820         size_x = cpl_image_get_size_x(star_ima) ;
00821         size_y = cpl_image_get_size_y(star_ima) ;
00822 
00823         /* Filtering the image*/
00824         kernel = cpl_mask_new(3, 3);
00825         cpl_mask_not(kernel);
00826         filt_ima = cpl_image_new(cpl_image_get_size_x(star_ima),
00827                                  cpl_image_get_size_y(star_ima),
00828                                  cpl_image_get_type(star_ima));
00829         cpl_image_filter_mask(filt_ima, star_ima, kernel, CPL_FILTER_MEDIAN, 
00830                               CPL_BORDER_FILTER);
00831         cpl_mask_delete(kernel);
00832 
00833         /* Looking for apertures */
00834         aperts = cpl_apertures_extract_sigma(filt_ima,
00835                 hawki_cal_zpoint_config.detect_sigma) ;
00836         cpl_image_delete(filt_ima) ;
00837         if (aperts == NULL)
00838         {
00839             cpl_msg_error(__func__, "Cannot find the central object") ;
00840             cpl_imagelist_delete(*star_images) ;
00841             cpl_bivector_delete(positions) ;
00842             return NULL ;
00843         }
00844         min_dist = size_x * size_x + size_y * size_y ;
00845         for (iaper=0 ; iaper<cpl_apertures_get_size(aperts) ; iaper++) {
00846             pos_x = cpl_apertures_get_centroid_x(aperts, iaper+1) ;
00847             pos_y = cpl_apertures_get_centroid_y(aperts, iaper+1) ;
00848             dist = (pos_x-hawki_cal_zpoint_config.xcoord[idet])*
00849                     (pos_x-hawki_cal_zpoint_config.xcoord[idet]) +
00850                    (pos_y-hawki_cal_zpoint_config.ycoord[idet])*
00851                     (pos_y-hawki_cal_zpoint_config.ycoord[idet]);
00852             if (dist<min_dist) {
00853                 min_dist = dist ;
00854                 pos_x_cen = pos_x ;
00855                 pos_y_cen = pos_y ;
00856             }
00857         }
00858         cpl_apertures_delete(aperts) ;
00859 
00860         cpl_vector_set(cpl_bivector_get_x(positions), iframe_star, pos_x_cen) ;
00861         cpl_vector_set(cpl_bivector_get_y(positions), iframe_star, pos_y_cen) ;
00862         cpl_msg_info(__func__, "Expected star position: %g %g",
00863                 hawki_cal_zpoint_config.xcoord[idet],
00864                 hawki_cal_zpoint_config.ycoord[idet]);
00865         cpl_msg_info(__func__, "Bright object position: %g %g",
00866                 pos_x_cen, pos_y_cen) ;
00867 
00868         /* Check that the star is within the search window */
00869         if(fabs(pos_x_cen - hawki_cal_zpoint_config.xcoord[idet]) >
00870                 hawki_cal_zpoint_config.sx                        ||
00871            fabs(pos_y_cen - hawki_cal_zpoint_config.ycoord[idet]) >
00872                 hawki_cal_zpoint_config.sy)
00873         {
00874             hawki_cal_zpoint_outputs.stdstar_image_detected[idet] = 0;
00875             cpl_msg_warning(cpl_func,"No object has been found within the box"
00876                             "limits [%d, %d] around the expected position",
00877                             hawki_cal_zpoint_config.sx,
00878                             hawki_cal_zpoint_config.sy);
00879         }
00880         else
00881         {
00882             hawki_cal_zpoint_outputs.stdstar_image_detected[idet] = 1;
00883         }
00884 
00885         /* Free */
00886         cpl_msg_indent_less() ;
00887     }
00888     cpl_msg_indent_less() ;
00889 
00890     /* Reorder the images (frame order) */
00891     star_images_frame_order = cpl_imagelist_new() ;
00892     for (iframe=0 ; iframe< nframes; iframe++)
00893     {
00894         tmp_ima = cpl_image_duplicate
00895             (cpl_imagelist_get(*star_images, labels[iframe] -1 ));
00896         cpl_imagelist_set(star_images_frame_order, tmp_ima, iframe);
00897     }
00898 
00899     /* Compute the photometry */
00900     cpl_msg_info(__func__, "Compute the photometry") ;
00901     cpl_msg_indent_more() ;
00902     if ((zpoint_tables = hawki_cal_zpoint_photom
00903             (star_images_frame_order, positions, labels))==NULL) {
00904         cpl_msg_error(__func__, "Cannot reduce") ;
00905         cpl_bivector_delete(positions) ;
00906         cpl_imagelist_delete(star_images_frame_order) ;
00907         cpl_msg_indent_less() ;
00908         return NULL ;
00909     }
00910 
00911 
00912     /* Free and exit */
00913     cpl_msg_indent_less() ;
00914     cpl_bivector_delete(positions) ;
00915     cpl_imagelist_delete(star_images_frame_order) ;
00916 
00917     return zpoint_tables;
00918 }
00919 
00920 /*----------------------------------------------------------------------------*/
00930 /*----------------------------------------------------------------------------*/
00931 static cpl_table ** hawki_cal_zpoint_photom(
00932         cpl_imagelist       *   ilist,
00933         cpl_bivector        *   pos,
00934         int                 *   labels)
00935 {
00936     cpl_table **        zpoint;
00937     int                 nframes;
00938     double              r, r1, r2;
00939     double              stdstar_mag;
00940     double              dit;
00941     double              extinction;
00942     double              pixscale;
00943     cpl_image       *   ima ;
00944     double          *   pos_x ;
00945     double          *   pos_y ;
00946     double              bgd, fl, zp, peak, fwhm_x, fwhm_y ;
00947     cpl_bivector    *   iqe_res ;
00948     int                 iframe;
00949     int                 idet;
00950 
00951     /* Test entries */
00952     if (ilist == NULL) return NULL ;
00953     if (pos == NULL) return NULL ;
00954 
00955     /* Initialise */
00956     nframes = cpl_imagelist_get_size(ilist) ;
00957     stdstar_mag = hawki_cal_zpoint_outputs.stdstar_mag_filter;
00958     dit = hawki_cal_zpoint_outputs.dit ;
00959     pixscale = hawki_cal_zpoint_outputs.pixscale ;
00960 
00961     /* Get extinction */
00962     switch (hawki_cal_zpoint_outputs.band) {
00963         case HAWKI_BAND_J:      extinction = 0.098 ; break ;
00964         case HAWKI_BAND_H:      extinction = 0.039 ; break ;
00965         case HAWKI_BAND_K:      extinction = 0.065 ; break ;
00966         case HAWKI_BAND_Y:      extinction = 0.00 ; break ;
00967         default:                extinction = 0.00 ; break ;
00968     }
00969     hawki_cal_zpoint_outputs.ext_coeff = extinction;
00970     cpl_msg_info(__func__,"Using tabulated extinction for band %s: %f",
00971                  hawki_cal_zpoint_outputs.filter, extinction);
00972 
00973     /* Loop on the images */
00974     for (iframe=0 ; iframe<nframes ; iframe++) {
00975         idet = labels[iframe]-1;
00976         if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
00977         {
00978             /* Get the current image */
00979             ima = cpl_imagelist_get(ilist, iframe) ;
00980 
00981             /* Get the current position */
00982             pos_x = cpl_bivector_get_x_data(pos) ;
00983             pos_y = cpl_bivector_get_y_data(pos) ;
00984 
00985             /* FWHM_X / FWHM_Y */
00986             iqe_res = cpl_image_iqe
00987                     (ima, (int)(pos_x[iframe]-10.0), (int)(pos_y[iframe]-10.0),
00988                           (int)(pos_x[iframe]+10.0), (int)(pos_y[iframe]+10.0));
00989             if (iqe_res == NULL)
00990             {
00991                 cpl_msg_debug(__func__,"Cannot compute FWHM for chip %d",
00992                         idet + 1);
00993                 fwhm_x = fwhm_y = -1.0 ;
00994                 cpl_error_reset() ;
00995             } else {
00996                 fwhm_x = cpl_vector_get(cpl_bivector_get_x(iqe_res), 2) ;
00997                 fwhm_y = cpl_vector_get(cpl_bivector_get_x(iqe_res), 3) ;
00998                 cpl_bivector_delete(iqe_res) ;
00999             }
01000 
01001             /* Determine the radii */
01002             r = hawki_cal_zpoint_config.phot_star_radius ;
01003             if (r < 0) {
01004                 if (fwhm_x>0 && fwhm_y>0)   r = 5*(fwhm_x+fwhm_y)/2.0 ;
01005                 else                        r = HAWKI_PHOT_STAR_RADIUS ;
01006             }
01007             r1 = hawki_cal_zpoint_config.phot_bg_r1 ;
01008             r2 = hawki_cal_zpoint_config.phot_bg_r2 ;
01009             if (r1 < 0) r1 = r + 10.0 ;
01010             if (r2 < 0) r2 = r1 + 20.0 ;
01011             //cpl_msg_info(__func__, "Use radii for star: %g and background: %g, %g",
01012             //        r, r1, r2) ;
01013 
01014             /* Compute the photometry */
01015             /* Background */
01016             bgd = irplib_strehl_ring_background(ima, (int)(pos_x[iframe]),
01017                (int)(pos_y[iframe]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN);
01018             /* Flux */
01019             fl = irplib_strehl_disk_flux(ima,
01020                     (int)(pos_x[iframe]), (int)(pos_y[iframe]), (int)r, bgd);
01021             
01022             //cpl_msg_info(__func__, "Zero point in chip %d:   %g", labels[iframe], zp) ;
01023             /* Peak */
01024             peak = cpl_image_get_max_window(ima,
01025                     (int)(pos_x[iframe]-5), (int)(pos_y[iframe]-5),
01026                     (int)(pos_x[iframe]+5), (int)(pos_y[iframe]+5));
01027 
01028             /* Zero Point */
01029             if (hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
01030             {
01031                 if (fl > 0 && dit > 0)
01032                 {
01033                     hawki_cal_zpoint_outputs.zpoint_computable[idet] = 1;
01034                     zp = stdstar_mag + 2.5 * log10(fl) - 2.5 * log10(dit);
01035 
01036                     hawki_cal_zpoint_outputs.zpoint[idet] = zp;
01037                     hawki_cal_zpoint_outputs.atx0[idet] = zp + 
01038                             extinction * hawki_cal_zpoint_outputs.airmass[idet];
01039                 }
01040                 else
01041                     hawki_cal_zpoint_outputs.zpoint_computable[idet] = 0;
01042             }
01043             hawki_cal_zpoint_outputs.posx[idet] = pos_x[iframe];
01044             hawki_cal_zpoint_outputs.posy[idet] = pos_y[iframe];
01045             hawki_cal_zpoint_outputs.flux[idet] = fl;
01046             hawki_cal_zpoint_outputs.instrmag[idet] = 2.5 * log10(fl/dit);
01047             hawki_cal_zpoint_outputs.peak[idet] = peak;
01048             hawki_cal_zpoint_outputs.bgd[idet] = bgd;
01049             hawki_cal_zpoint_outputs.fwhmx[idet] = fwhm_x;
01050             hawki_cal_zpoint_outputs.fwhmy[idet] = fwhm_y;
01051             if (fwhm_x > 0 && fwhm_y > 0)
01052                 hawki_cal_zpoint_outputs.fwhm[idet] = sqrt(fwhm_x*fwhm_y);
01053             else
01054                 hawki_cal_zpoint_outputs.fwhm[idet] = -1.0;
01055             hawki_cal_zpoint_outputs.fwhmx_as[idet] = fwhm_x * pixscale;
01056             hawki_cal_zpoint_outputs.fwhmy_as[idet] = fwhm_y * pixscale;
01057             if (fwhm_x > 0 && fwhm_y > 0)
01058                 hawki_cal_zpoint_outputs.fwhm_as[idet] =
01059                         sqrt(fwhm_x*fwhm_y*pixscale*pixscale);
01060             else
01061                 hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] = -1.0;
01062         }
01063         else
01064         {
01065             cpl_msg_warning(cpl_func,"Standard star not detected in chip %d. "
01066                     "No zeropoint computed.", idet + 1);
01067         }
01068 
01069     }
01070 
01071     /* Create the table */
01072     zpoint = hawki_table_new(1);
01073     //tab = cpl_table_new(nframes) ;
01074     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01075         cpl_table_new_column(zpoint[idet], 
01076                              HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT);
01077         cpl_table_new_column(zpoint[idet], 
01078                              HAWKI_COL_ZPOINT_STARNAME, CPL_TYPE_STRING);
01079         cpl_table_new_column(zpoint[idet], 
01080                              HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE);
01081         cpl_table_set_column_unit(zpoint[idet],
01082                                   HAWKI_COL_ZPOINT_POSX,"pix");
01083         cpl_table_new_column(zpoint[idet], 
01084                              HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE);
01085         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSY,"pix");
01086         cpl_table_new_column(zpoint[idet], 
01087                              HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE);
01088         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ZPOINT,"mag");
01089         cpl_table_new_column(zpoint[idet], 
01090                              HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE);
01091         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ATX0,"mag");
01092         cpl_table_new_column(zpoint[idet], 
01093                              HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE);
01094         cpl_table_new_column(zpoint[idet], 
01095                              HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE);
01096         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FLUX,"ADU");
01097         cpl_table_new_column(zpoint[idet], 
01098                              HAWKI_COL_ZPOINT_INSTRMAG, CPL_TYPE_DOUBLE);
01099         cpl_table_set_column_unit(zpoint[idet],
01100                                   HAWKI_COL_ZPOINT_INSTRMAG,"log(ADU/s)");
01101         cpl_table_new_column(zpoint[idet], 
01102                              HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING);
01103         cpl_table_new_column(zpoint[idet], 
01104                              HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE);
01105         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_PEAK,"ADU");
01106         cpl_table_new_column(zpoint[idet], 
01107                              HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE);
01108         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_BGD,"ADU");
01109         cpl_table_new_column(zpoint[idet], 
01110                              HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE);
01111         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX,"pix");
01112         cpl_table_new_column(zpoint[idet], 
01113                              HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE);
01114         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY,"pix");
01115         cpl_table_new_column(zpoint[idet], 
01116                              HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE);
01117         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM,"pix");
01118         cpl_table_new_column(zpoint[idet], 
01119                              HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE);
01120         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX_AS,"arcsec");
01121         cpl_table_new_column(zpoint[idet], 
01122                              HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE);
01123         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY_AS,"arcsec");
01124         cpl_table_new_column(zpoint[idet], 
01125                              HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE);
01126         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM_AS,"arcsec");
01127         cpl_table_new_column(zpoint[idet], 
01128                              HAWKI_COL_ZPOINT_STARMAG, CPL_TYPE_DOUBLE);
01129         cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_STARMAG,"mag");
01130         
01131         cpl_table_set_int(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, 0, idet+1) ;
01132         cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, 0,
01133                 hawki_cal_zpoint_outputs.filter);
01134         cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, 0,
01135                 hawki_cal_zpoint_outputs.airmass[idet]) ;
01136         if(hawki_cal_zpoint_outputs.stdstar_incat_found== 1)
01137         {
01138             cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, 0,
01139                     hawki_cal_zpoint_outputs.starname) ;
01140             if(hawki_cal_zpoint_outputs.stdstar_mag_available== 1)
01141             {
01142                 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, 0,
01143                         hawki_cal_zpoint_outputs.stdstar_mag_filter);
01144             }
01145         }
01146         if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
01147         {
01148             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSX, 0,
01149                     hawki_cal_zpoint_outputs.posx[idet]) ;
01150             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSY, 0,
01151                                  hawki_cal_zpoint_outputs.posy[idet]) ;
01152             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, 0,
01153                                  hawki_cal_zpoint_outputs.flux[idet]) ;
01154             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, 0,
01155                                  hawki_cal_zpoint_outputs.instrmag[idet]) ;
01156             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, 0,
01157                                  hawki_cal_zpoint_outputs.peak[idet]) ;
01158             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_BGD, 0,
01159                                  hawki_cal_zpoint_outputs.bgd[idet]) ;
01160             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, 0,
01161                                  hawki_cal_zpoint_outputs.fwhmx[idet]) ;
01162             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, 0,
01163                                  hawki_cal_zpoint_outputs.fwhmy[idet]) ;
01164             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, 0,
01165                                  hawki_cal_zpoint_outputs.fwhm[idet]) ;
01166             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, 0,
01167                                  hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
01168             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, 0,
01169                                  hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
01170             cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, 0,
01171                                  hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
01172             if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
01173             {
01174                 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, 0,
01175                         hawki_cal_zpoint_outputs.zpoint[idet]);
01176                 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, 0,
01177                         hawki_cal_zpoint_outputs.atx0[idet]);
01178             }
01179         }
01180     }
01181 
01182     /* Mean values */
01183     if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
01184     {
01185         int nzpoint = 0;
01186         hawki_cal_zpoint_outputs.mean_zpoint = 0.0 ;
01187         hawki_cal_zpoint_outputs.mean_atx0 = 0.0 ;
01188         for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01189             if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
01190             {
01191 
01192                 hawki_cal_zpoint_outputs.mean_zpoint +=
01193                         hawki_cal_zpoint_outputs.zpoint[idet] ;
01194                 hawki_cal_zpoint_outputs.mean_atx0  +=
01195                         hawki_cal_zpoint_outputs.atx0[idet] ;
01196                 nzpoint++;
01197             }
01198         }
01199         if(nzpoint > 0)
01200         {
01201             hawki_cal_zpoint_outputs.zpoint_mean_computable = 1;
01202             hawki_cal_zpoint_outputs.mean_zpoint /= nzpoint ;
01203             hawki_cal_zpoint_outputs.mean_atx0 /= nzpoint ;
01204         }
01205     }
01206     
01207     /* Output results */
01208     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01209     {
01210         if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
01211         {
01212             cpl_msg_info(__func__, "Zero point [at airmass=1] in chip %d:",
01213                     idet + 1);
01214             cpl_msg_indent_more();
01215             cpl_msg_info(__func__,"   ZP: %g [%g]",
01216                          hawki_cal_zpoint_outputs.zpoint[idet],
01217                          hawki_cal_zpoint_outputs.atx0[idet]);
01218             cpl_msg_info(__func__,"   Flux of star: %f",
01219                          hawki_cal_zpoint_outputs.flux[idet]);
01220             cpl_msg_indent_less();
01221         }
01222         else
01223             cpl_msg_info(__func__, "Zero point not available for chip %d",
01224                     idet + 1);
01225 
01226     }
01227 
01228     return zpoint;
01229 }
01230 
01231 /*----------------------------------------------------------------------------*/
01239 /*----------------------------------------------------------------------------*/
01240 static int hawki_cal_zpoint_get_mag(
01241         const char  *       std_file,
01242         double              pointing_ra,
01243         double              pointing_dec,
01244         hawki_band          band)
01245 {
01246     cpl_table      * catalogue;
01247     char           * star_name;
01248     char           * used_catname;
01249     char           * star_type;
01250     double           stdstar_ra;
01251     double           stdstar_dec;
01252     double           mag_filter;
01253     double           mag_J;
01254     double           mag_H;
01255     double           mag_K;
01256     double           mag_Y;
01257     int              mag_invalid;
01258     int              star_ind;
01259 
01260     hawki_cal_zpoint_outputs.stdstar_incat_found = 0;    
01261     
01262     /* Load the catalog */
01263     if ((catalogue = irplib_stdstar_load_catalog(std_file, "all")) == NULL) {
01264         cpl_msg_error(cpl_func,"Cannot read catalogue file %s",std_file);
01265         return -1;
01266     }
01267 
01268     /* Check that the columns are there */
01269     if (irplib_stdstar_check_columns_exist(catalogue) != CPL_ERROR_NONE) {
01270         cpl_msg_error(cpl_func,"Note all the columns are present"
01271                       " in the  catalogue file %s",std_file);
01272         return -1;
01273     }    
01274     
01275     /* Select stars within a given distance */
01276     if (irplib_stdstar_select_stars_dist(catalogue, 
01277             pointing_ra, pointing_dec, 5.0) == -1) {
01278         cpl_table_delete(catalogue) ;
01279         return 1;
01280     }
01281 
01282     /* Take the closest */
01283     if ((star_ind=irplib_stdstar_find_closest(catalogue, 
01284             pointing_ra, pointing_dec)) < 0) {
01285         cpl_table_delete(catalogue) ;
01286         return 1;
01287     }
01288 
01289     /* Retrieve the star information */
01290     hawki_cal_zpoint_outputs.stdstar_incat_found = 1;    
01291     star_name = cpl_strdup(cpl_table_get_string(catalogue,
01292                                             IRPLIB_STDSTAR_STAR_COL, star_ind));
01293     used_catname = cpl_strdup(cpl_table_get_string
01294                               (catalogue, IRPLIB_STDSTAR_CAT_COL, star_ind));
01295     star_type = cpl_strdup(cpl_table_get_string(catalogue, 
01296             IRPLIB_STDSTAR_TYPE_COL, star_ind));
01297     stdstar_ra  = cpl_table_get_double(catalogue, 
01298             IRPLIB_STDSTAR_RA_COL, star_ind, NULL);
01299     stdstar_dec = cpl_table_get_double(catalogue, 
01300             IRPLIB_STDSTAR_DEC_COL, star_ind, NULL);
01301     mag_filter = cpl_table_get_double(catalogue, hawki_std_band_name(band), 
01302             star_ind, &mag_invalid);
01303     mag_H = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_H),
01304             star_ind, NULL);
01305     mag_J = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_J), 
01306             star_ind, NULL);
01307     mag_K = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_K), 
01308             star_ind, NULL);
01309     mag_Y = cpl_table_get_double(catalogue, hawki_std_band_name(HAWKI_BAND_Y), 
01310             star_ind, NULL);
01311     
01312     /* Store results */
01313     strcpy(hawki_cal_zpoint_outputs.starname, star_name) ;
01314     strcpy(hawki_cal_zpoint_outputs.sptype, star_type) ;
01315     strcpy(hawki_cal_zpoint_outputs.catalog, used_catname);
01316     hawki_cal_zpoint_outputs.stdstar_ra = stdstar_ra;
01317     hawki_cal_zpoint_outputs.stdstar_dec = stdstar_dec;
01318     if(mag_invalid == 0)
01319     {
01320         hawki_cal_zpoint_outputs.stdstar_mag_available = 1;
01321         hawki_cal_zpoint_outputs.stdstar_mag_filter = mag_filter;
01322     }
01323     else
01324         hawki_cal_zpoint_outputs.stdstar_mag_available = 0;
01325     hawki_cal_zpoint_outputs.stdstar_mag_H = mag_H;
01326     hawki_cal_zpoint_outputs.stdstar_mag_J = mag_J;
01327     hawki_cal_zpoint_outputs.stdstar_mag_K = mag_K;
01328     hawki_cal_zpoint_outputs.stdstar_mag_Y = mag_Y;
01329     cpl_free(star_name);
01330     cpl_free(star_type);
01331     cpl_free(used_catname);
01332     cpl_table_delete(catalogue);
01333     
01334     return 0;
01335 }
01336 
01337 /*----------------------------------------------------------------------------*/
01346 /*----------------------------------------------------------------------------*/
01347 static int hawki_cal_zpoint_save
01348 (cpl_table           **  zpoint_tables,
01349  int                 *   labels,
01350  cpl_imagelist       *   images,
01351  cpl_table           **  raw_zpoint_stats,
01352  cpl_frameset        *   zpoint_frames,
01353  cpl_frameset        *   calib_frames,
01354  const cpl_frame     *   stars_frame,
01355  cpl_parameterlist   *   parlist,
01356  cpl_frameset        *   set)
01357 {
01358     cpl_propertylist    *   qcmainparams;
01359     cpl_propertylist    **  qcextparams;
01360     cpl_propertylist    *   mainheader;
01361     cpl_propertylist    **  extheaders;
01362     cpl_propertylist    **  statsqcextparams;
01363     cpl_propertylist    **  statsextheaders;
01364     cpl_frameset        *   used_frames;
01365     const char          *   ref_filename;
01366     const char          *   recipe_name = "hawki_cal_zpoint" ;
01367     int                     idet;
01368     int                     iframe;
01369     cpl_errorstate          error_prevstate = cpl_errorstate_get();
01370 
01371 
01372     /* Get the reference frame */
01373     ref_filename = hawki_get_extref_file(set);
01374 
01375     /* Create QC parameters for the zpoint table */
01376     qcmainparams = cpl_propertylist_new();
01377     qcextparams = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
01378     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01379         qcextparams[idet] = cpl_propertylist_new();
01380     /* Write QC parameters */
01381     hawki_cal_zpoint_compute_qc(qcmainparams, qcextparams, set);
01382     
01383     /* Create QC parameters for the stats table: Statistics of the raw images */
01384     statsqcextparams = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
01385     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01386         statsqcextparams[idet] = cpl_propertylist_new();
01387     hawki_image_stats_stats(raw_zpoint_stats, statsqcextparams);    
01388     
01389     /* Create the main and extension headers for the zpoint table*/
01390     mainheader = cpl_propertylist_new();
01391     extheaders = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
01392     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01393         extheaders[idet] = cpl_propertylist_new();
01394     /* Copy QC params to the headers */
01395     cpl_propertylist_append(mainheader, qcmainparams);
01396     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01397         cpl_propertylist_append(extheaders[idet], qcextparams[idet]) ;
01398     
01399     /* Create the  extension headers for the stats table*/
01400     statsextheaders = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
01401     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01402         statsextheaders[idet] = cpl_propertylist_duplicate(statsqcextparams[idet]);
01403     
01404     /* Write additional keywords to the headers */
01405     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01406     {
01407         cpl_propertylist    *   inputlist;
01408         cpl_propertylist    *   wcslist;
01409         int                     this_iframe = -1;
01410         int                     ext_nb;
01411 
01412         /* Propagate some keywords from input raw frame extensions */
01413         ext_nb=hawki_get_ext_from_detector(ref_filename, idet+1);
01414         inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
01415                 HAWKI_HEADER_EXT_FORWARD, 0);
01416         cpl_propertylist_append(extheaders[idet], inputlist);
01417         cpl_propertylist_append(statsextheaders[idet], inputlist);
01418         cpl_propertylist_delete(inputlist);
01419 
01420         /* Propagate WCS keywords */
01421         for(iframe=0; iframe<cpl_frameset_get_size(zpoint_frames); iframe++)
01422             if(labels[iframe] == idet + 1)
01423                 this_iframe = iframe;
01424         wcslist = cpl_propertylist_load_regexp
01425             (cpl_frame_get_filename(cpl_frameset_get_frame(zpoint_frames, this_iframe)),
01426                                     ext_nb, HAWKI_HEADER_WCS, 0);
01427         cpl_propertylist_copy_property_regexp
01428             (extheaders[idet], wcslist, HAWKI_HEADER_WCS, 0);
01429         cpl_propertylist_delete(wcslist);
01430     }
01431     
01432     /* Write the zpoint table  */
01433     used_frames = cpl_frameset_duplicate(zpoint_frames);
01434     for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
01435         cpl_frameset_insert(used_frames, cpl_frame_duplicate(
01436                 cpl_frameset_get_frame(calib_frames, iframe)));
01437     cpl_frameset_insert(used_frames, cpl_frame_duplicate(stars_frame));
01438     hawki_tables_save(set,
01439                       parlist,
01440                       used_frames,
01441                       (const cpl_table **)zpoint_tables,
01442                       recipe_name,
01443                       HAWKI_CALPRO_ZPOINT_TAB,
01444                       HAWKI_PROTYPE_ZPOINT_TAB,
01445                       mainheader,
01446                       (const cpl_propertylist **)extheaders,
01447                       "hawki_cal_zpoint.fits");
01448     cpl_frameset_delete(used_frames);
01449     
01450     /* Write the table with the raw zpoint objects statistics */
01451     used_frames = cpl_frameset_duplicate(zpoint_frames);
01452     hawki_tables_save(set,
01453                       parlist,
01454                       used_frames,
01455                       (const cpl_table **)raw_zpoint_stats,
01456                       recipe_name,
01457                       HAWKI_CALPRO_ZPOINT_STATS,
01458                       HAWKI_PROTYPE_ZPOINT_STATS,
01459                       NULL,
01460                       (const cpl_propertylist **)statsextheaders,
01461                       "hawki_cal_zpoint_stats.fits");
01462 
01463     /* Write the images  */
01464     for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
01465         cpl_frameset_insert(used_frames, cpl_frame_duplicate(
01466                 cpl_frameset_get_frame(calib_frames, iframe)));
01467     hawki_imagelist_save(set,
01468                          parlist,
01469                          used_frames,
01470                          images,
01471                          recipe_name,
01472                          HAWKI_CALPRO_ZPOINT_IMA,
01473                          HAWKI_PROTYPE_ZPOINT_IMA,
01474                          NULL,
01475                          NULL,
01476                          "hawki_cal_zpoint_check.fits") ;
01477     cpl_frameset_delete(used_frames);
01478 
01479     /* Free */
01480     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
01481         cpl_propertylist_delete(extheaders[idet]) ;
01482         cpl_propertylist_delete(qcextparams[idet]) ;
01483         cpl_propertylist_delete(statsqcextparams[idet]);
01484         cpl_propertylist_delete(statsextheaders[idet]);
01485     }
01486     cpl_propertylist_delete(mainheader);
01487     cpl_propertylist_delete(qcmainparams);
01488     cpl_free(extheaders);
01489     cpl_free(qcextparams);
01490     cpl_free(statsqcextparams);
01491     cpl_free(statsextheaders);
01492 
01493     /* Free */
01494     if(!cpl_errorstate_is_equal(error_prevstate))
01495     {
01496         cpl_errorstate_set(CPL_ERROR_NONE);
01497         return -1;
01498     }
01499     return  0;
01500 }
01501 
01502 /*----------------------------------------------------------------------------*/
01509 /*----------------------------------------------------------------------------*/
01510 static int hawki_cal_zpoint_compute_qc
01511 (cpl_propertylist *   qcmainparams, 
01512  cpl_propertylist **  qcextparams,
01513  cpl_frameset     *   set)
01514 {
01515     int  idet;
01516     int  nframes;
01517 
01518     
01519     nframes = cpl_frameset_get_size(set) ;
01520 
01521     /* Check inputs */
01522     if (qcmainparams == NULL) return -1;
01523     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01524         if (qcextparams[idet] == NULL) return -1;
01525     
01526     /* Write the QC params common to all extensions in the main header */
01527     cpl_propertylist_append_string(qcmainparams, "ESO QC FILTER OBS",
01528             hawki_cal_zpoint_outputs.filter);
01529     cpl_propertylist_set_comment(qcmainparams, "ESO QC FILTER OBS",
01530                                  "Observation filter");
01531     cpl_propertylist_append_string(qcmainparams, "ESO QC FILTER REF",
01532             hawki_std_band_name(hawki_cal_zpoint_outputs.band)) ;
01533     cpl_propertylist_set_comment(qcmainparams, "ESO QC FILTER REF",
01534                                  "Reference filter");
01535     cpl_propertylist_append_double(qcmainparams, "ESO QC AMBI RHUM AVG",
01536             hawki_cal_zpoint_outputs.humidity);
01537     cpl_propertylist_set_comment(qcmainparams, "ESO QC AMBI RHUM AVG",
01538                      "(percent) ambient relative humidity @ 30/2 m");
01539     cpl_propertylist_append_double(qcmainparams, "ESO QC AIRMASS MEAN",
01540             hawki_cal_zpoint_outputs.mean_airmass) ;
01541     cpl_propertylist_set_comment(qcmainparams, "ESO QC AIRMASS MEAN",
01542                                  "Average airmass");
01543     cpl_propertylist_append_double(qcmainparams, "ESO QC DATANCOM",
01544                                    nframes);
01545     cpl_propertylist_set_comment(qcmainparams, "ESO QC DATANCOM",
01546                                  "Number of files used for the reduction");
01547     if(hawki_cal_zpoint_outputs.stdstar_incat_found == 1)
01548     {
01549         cpl_propertylist_append_string(qcmainparams, "ESO QC STDNAME",
01550                 hawki_cal_zpoint_outputs.starname) ;
01551         cpl_propertylist_set_comment(qcmainparams, "ESO QC STDNAME",
01552                                      "Standard star name");
01553         cpl_propertylist_append_string(qcmainparams, "ESO QC SPECTYPE",
01554                                        hawki_cal_zpoint_outputs.sptype) ;
01555         cpl_propertylist_set_comment(qcmainparams, "ESO QC SPECTYPE",
01556                                      "Standard star spectral type");
01557         cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG H",
01558                                        hawki_cal_zpoint_outputs.stdstar_mag_H) ;
01559         cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG H",
01560                                      "Standard star magnitude in H");
01561         cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG J",
01562                                        hawki_cal_zpoint_outputs.stdstar_mag_J) ;
01563         cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG J",
01564                                      "Standard star magnitude in J");
01565         cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG K",
01566                                        hawki_cal_zpoint_outputs.stdstar_mag_K) ;
01567         cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG K",
01568                                      "Standard star magnitude in K");
01569         cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG Y",
01570                                        hawki_cal_zpoint_outputs.stdstar_mag_Y) ;
01571         cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG Y",
01572                                      "Standard star magnitude in Y");
01573         cpl_propertylist_append_string(qcmainparams, "ESO QC CATNAME",
01574                                        hawki_cal_zpoint_outputs.catalog) ;
01575         cpl_propertylist_set_comment(qcmainparams, "ESO QC CATNAME",
01576                                      "Standard star catalogue name");
01577     }
01578     if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
01579     {
01580         cpl_propertylist_append_double(qcmainparams, "ESO QC STARMAG",
01581                 hawki_cal_zpoint_outputs.stdstar_mag_filter) ;
01582         cpl_propertylist_set_comment(qcmainparams, "ESO QC STARMAG",
01583                                  "Standard star magnitude in observed filter");
01584     }
01585     if(hawki_cal_zpoint_outputs.zpoint_mean_computable == 1)
01586     {
01587         cpl_propertylist_append_double(qcmainparams, "ESO QC ZPOINT MEAN",
01588                 hawki_cal_zpoint_outputs.mean_zpoint) ;
01589         cpl_propertylist_set_comment(qcmainparams, "ESO QC ZPOINT MEAN",
01590                             "Mean measured zero-point for all the chips [mag]");
01591         cpl_propertylist_append_double(qcmainparams, "ESO QC ATX0 MEAN",
01592                                        hawki_cal_zpoint_outputs.mean_atx0);
01593         cpl_propertylist_set_comment(qcmainparams, "ESO QC ATX0 MEAN",
01594                "Mean extinction corrected zero-point for all the chips [mag]");
01595         cpl_propertylist_append_double(qcmainparams, "ESO QC ZPOINT EXT COEFF",
01596                                        hawki_cal_zpoint_outputs.ext_coeff);
01597         cpl_propertylist_set_comment(qcmainparams, "ESO QC ZPOINT EXT COEFF",
01598                "Extinction coefficient used in the computation of ATX0");
01599     }
01600         
01601     /* Write QC params that are specific of the extension */
01602     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01603     {
01604         if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
01605         {
01606             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT",
01607                     hawki_cal_zpoint_outputs.zpoint[idet]) ;
01608             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT",
01609                                  "Measured zero-point for a given chip [mag]");
01610             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ATX0",
01611                                            hawki_cal_zpoint_outputs.atx0[idet]);
01612             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ATX0",
01613                      "Extinction corrected zero-point for a given chip [mag]");
01614         }
01615         if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
01616         {
01617             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT POSX",
01618                                            hawki_cal_zpoint_outputs.posx[idet]);
01619             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT POSX",
01620                                     "X position of the standard star [pixel]");
01621             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT POSY",
01622                                            hawki_cal_zpoint_outputs.posy[idet]);
01623             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT POSY",
01624                                     "Y position of the standard star [pixel]");
01625             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FLUX",
01626                                         hawki_cal_zpoint_outputs.flux[idet]);
01627             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FLUX",
01628                     "Flux of the standard star [ADU]");
01629             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT PEAK",
01630                                            hawki_cal_zpoint_outputs.peak[idet]) ;
01631             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT PEAK",
01632                                          "Peak of the standard starr [ADU]");
01633             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT BGD",
01634                                            hawki_cal_zpoint_outputs.bgd[idet]) ;
01635             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT BGD",
01636                                    "Background around the standard star [ADU]");
01637             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMX",
01638                                         hawki_cal_zpoint_outputs.fwhmx[idet]);
01639             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMX",
01640                                          "X FWHM of the standard star [pixel]");
01641             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMY",
01642                                          hawki_cal_zpoint_outputs.fwhmy[idet]);
01643             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMY",
01644                                          "Y FWHM of the standard star [pixel]");
01645             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHM",
01646                                            hawki_cal_zpoint_outputs.fwhm[idet]);
01647             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHM",
01648                                          "FWHM of the standard star [pixel]");
01649             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMX_AS",
01650                                       hawki_cal_zpoint_outputs.fwhmx_as[idet]);
01651             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMX_AS",
01652                                        "X FWHM of the standard star [arcsec]");
01653             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHMY_AS",
01654                                     hawki_cal_zpoint_outputs.fwhmy_as[idet]);
01655             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHMY_AS",
01656                                       "Y FWHM of the standard star [arcsec]");
01657             cpl_propertylist_append_double(qcextparams[idet], "ESO QC ZPOINT FWHM_AS",
01658                                        hawki_cal_zpoint_outputs.fwhm_as[idet]);
01659             cpl_propertylist_set_comment(qcextparams[idet], "ESO QC ZPOINT FWHM_AS",
01660                                          "FWHM of the standard star [arcsec]");
01661         }
01662     }
01663     return 0;
01664 }
01665 
01666 /*----------------------------------------------------------------------------*/
01672 /*----------------------------------------------------------------------------*/
01673 static int hawki_cal_zpoint_compute_keywords(
01674         cpl_frameset    *   set,
01675         int             *   labels)
01676 {
01677     int                     nframes ;
01678     cpl_vector          *   hum_vec ;
01679     cpl_frame           *   cur_frame ;
01680     cpl_propertylist    *   plist ;
01681     int                     iframe;
01682 
01683     /* Test inputs  */
01684     if (set == NULL) return -1 ;
01685 
01686     /* Initialize */
01687     nframes = cpl_frameset_get_size(set) ;
01688     hawki_cal_zpoint_outputs.mean_airmass   = 0.0 ;
01689 
01690     hum_vec = cpl_vector_new(nframes) ;
01691 
01692     for (iframe=0 ; iframe<nframes ; iframe++) {
01693         if (cpl_error_get_code()) {
01694             cpl_vector_delete(hum_vec) ;
01695             return -1 ;
01696         }
01697         cur_frame = cpl_frameset_get_frame(set, iframe) ;
01698         plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
01699         if (iframe==0) 
01700             hawki_cal_zpoint_outputs.mean_airmass += 
01701                 hawki_pfits_get_airmass_start(plist) ; 
01702         if (iframe==nframes-1) 
01703             hawki_cal_zpoint_outputs.mean_airmass += 
01704                 hawki_pfits_get_airmass_end(plist);
01705         hawki_cal_zpoint_outputs.airmass[labels[iframe] - 1] = 
01706             (hawki_pfits_get_airmass_start(plist) + 
01707              hawki_pfits_get_airmass_end(plist)) / 2.;
01708         cpl_vector_set(hum_vec,  iframe, hawki_pfits_get_humidity_level(plist));
01709         cpl_propertylist_delete(plist) ;
01710         if (cpl_error_get_code()) {
01711             cpl_vector_delete(hum_vec) ;
01712             cpl_error_reset() ;
01713             return -1 ;
01714         }
01715     }
01716     hawki_cal_zpoint_outputs.humidity  = cpl_vector_get_mean(hum_vec) ;
01717     hawki_cal_zpoint_outputs.mean_airmass /= 2 ;
01718 
01719     /* Free and return */
01720     cpl_vector_delete(hum_vec) ;
01721     if (cpl_error_get_code()) return -1 ;
01722     return 0 ;
01723 }
01724 
01725 static cpl_error_code hawki_cal_zpoint_get_expected_pos
01726 (cpl_frameset * set,
01727  int          * labels)
01728 {
01729     const char *  filename;
01730     int           iframe;
01731 
01732     for(iframe=0 ; iframe<HAWKI_NB_DETECTORS ; iframe++)
01733     {
01734         cpl_propertylist * wcs_plist;
01735         cpl_wcs          * wcs;
01736         int                idet;
01737 
01738         idet = labels[iframe];
01739         if(hawki_cal_zpoint_config.xcoord[idet - 1] == -1 ||
01740                 hawki_cal_zpoint_config.ycoord[idet - 1] == -1)
01741         {
01742             filename = cpl_frame_get_filename
01743                 (cpl_frameset_get_frame_const(set, iframe));
01744             wcs_plist = cpl_propertylist_load
01745                 (filename, hawki_get_ext_from_detector(filename, idet));
01746             wcs = cpl_wcs_new_from_propertylist(wcs_plist);
01747             cpl_propertylist_delete(wcs_plist);
01748             if(wcs == NULL)
01749             {
01750                 cpl_msg_error(__func__, "Could not get WCS info");
01751                 cpl_wcs_delete(wcs);
01752                 return CPL_ERROR_ILLEGAL_INPUT;
01753             }
01754             if(irplib_wcs_radectoxy(wcs,
01755                                     hawki_cal_zpoint_outputs.stdstar_ra,
01756                                     hawki_cal_zpoint_outputs.stdstar_dec,
01757                                     &(hawki_cal_zpoint_config.xcoord[idet - 1]),
01758                                     &(hawki_cal_zpoint_config.ycoord[idet - 1]))
01759                     != CPL_ERROR_NONE)
01760             {
01761                 cpl_msg_error(__func__,"Could not get the expected position of star");
01762                 cpl_wcs_delete(wcs);
01763                 return CPL_ERROR_UNSPECIFIED;
01764             }
01765             cpl_msg_info(cpl_func,
01766                          "Star expected position in detector %d is X=%f Y=%f",
01767                          idet, hawki_cal_zpoint_config.xcoord[idet - 1],
01768                          hawki_cal_zpoint_config.ycoord[idet - 1]);
01769 
01770             /* Free */
01771             cpl_wcs_delete(wcs);
01772         }
01773         else
01774         {
01775             cpl_msg_info(cpl_func,
01776                         "Using given star position in detector %d: X=%f Y=%f",
01777                          idet,
01778                          hawki_cal_zpoint_config.xcoord[idet - 1],
01779                          hawki_cal_zpoint_config.ycoord[idet - 1]);
01780 
01781         }
01782     }
01783 
01784     return 0;
01785 }
01786 
01787 
01788 static void hawki_cal_zpoint_output_init(void)
01789 {
01790     int idet;
01791 
01792     hawki_cal_zpoint_outputs.starname[0] = (char)0 ;
01793     hawki_cal_zpoint_outputs.sptype[0] = (char)0 ;
01794     hawki_cal_zpoint_outputs.filter[0] = (char)0 ;
01795     hawki_cal_zpoint_outputs.catalog[0] = (char)0 ;
01796     hawki_cal_zpoint_outputs.pixscale = -1.0 ;
01797     hawki_cal_zpoint_outputs.dit = -1.0 ;
01798     hawki_cal_zpoint_outputs.humidity = -1.0 ;
01799     hawki_cal_zpoint_outputs.mean_airmass = -1.0 ;
01800     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
01801     {
01802         hawki_cal_zpoint_outputs.airmass[idet] = -1.0 ;
01803         hawki_cal_zpoint_outputs.zpoint[idet] = -1.0 ;
01804         hawki_cal_zpoint_outputs.atx0[idet] = -1.0 ;
01805         hawki_cal_zpoint_outputs.posx[idet] = -1.0 ;
01806         hawki_cal_zpoint_outputs.posy[idet] = -1.0 ;
01807         hawki_cal_zpoint_outputs.flux[idet] = -1.0 ;
01808         hawki_cal_zpoint_outputs.peak[idet] = -1.0 ;
01809         hawki_cal_zpoint_outputs.bgd[idet] = -1.0 ;
01810         hawki_cal_zpoint_outputs.fwhmx[idet] = -1.0 ;
01811         hawki_cal_zpoint_outputs.fwhmy[idet] = -1.0 ;
01812         hawki_cal_zpoint_outputs.fwhm[idet] = -1.0 ;
01813         hawki_cal_zpoint_outputs.fwhmx_as[idet] = -1.0 ;
01814         hawki_cal_zpoint_outputs.fwhmy_as[idet] = -1.0 ;
01815         hawki_cal_zpoint_outputs .fwhm_as[idet] = -1.0 ;
01816     }
01817 }
01818 
01819 int hawki_cal_zpoint_retrieve_input_param
01820 (cpl_parameterlist  *  parlist)
01821 {
01822     cpl_parameter   *   par ;
01823     const char      *   sval ;
01824 
01825     par = NULL ;
01826 
01827     /* --ra */
01828     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ra") ;
01829     hawki_cal_zpoint_config.target_ra = cpl_parameter_get_double(par) ;
01830     /* --dec */
01831     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.dec") ;
01832     hawki_cal_zpoint_config.target_dec = cpl_parameter_get_double(par) ;
01833     /* --mag */
01834     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.mag") ;
01835     hawki_cal_zpoint_config.stdstar_given_magnitude = 
01836             cpl_parameter_get_double(par) ;
01837     /* --detect_sigma */
01838     par = cpl_parameterlist_find(parlist,"hawki.hawki_cal_zpoint.detect_sigma");
01839     hawki_cal_zpoint_config.detect_sigma = cpl_parameter_get_double(par) ;
01840     /* --sx */
01841     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sx") ;
01842     hawki_cal_zpoint_config.sx = cpl_parameter_get_int(par) ;
01843     /* --sy */
01844     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.sy") ;
01845     hawki_cal_zpoint_config.sy = cpl_parameter_get_int(par) ;
01846     /* --star_r */
01847     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.star_r") ;
01848     hawki_cal_zpoint_config.phot_star_radius = cpl_parameter_get_double(par) ;
01849     /* --bg_r1 */
01850     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r1") ;
01851     hawki_cal_zpoint_config.phot_bg_r1 = cpl_parameter_get_double(par) ;
01852     /* --bg_r2 */
01853     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.bg_r2") ;
01854     hawki_cal_zpoint_config.phot_bg_r2 = cpl_parameter_get_double(par) ;
01855     /* --xcoord */
01856     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.xcoord");
01857     sval = cpl_parameter_get_string(par);
01858     if (sscanf(sval, "%lf,%lf,%lf,%lf",
01859                hawki_cal_zpoint_config.xcoord,
01860                hawki_cal_zpoint_config.xcoord+1,
01861                hawki_cal_zpoint_config.xcoord+2,
01862                hawki_cal_zpoint_config.xcoord+3)!=4)
01863     {
01864         return -1;
01865     }
01866     /* --ycoord */
01867     par = cpl_parameterlist_find(parlist, "hawki.hawki_cal_zpoint.ycoord");
01868     sval = cpl_parameter_get_string(par);
01869     if (sscanf(sval, "%lf,%lf,%lf,%lf",
01870                hawki_cal_zpoint_config.ycoord,
01871                hawki_cal_zpoint_config.ycoord+1,
01872                hawki_cal_zpoint_config.ycoord+2,
01873                hawki_cal_zpoint_config.ycoord+3)!=4)
01874     {
01875         return -1;
01876     }
01877 
01878     return 0;
01879 }
01880 
01881 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist)
01882 {
01883     if(hawki_pfits_get_targ_epoch(plist) != 2000. ||
01884        hawki_pfits_get_targ_equinox(plist) != 2000.)
01885     {
01886         cpl_msg_error(__func__,"Epoch and equinox must be 2000.");
01887         return -1;
01888     }
01889     else
01890         return 0;
01891 }