isaac_img_zpoint.c

00001 /* $Id: isaac_img_zpoint.c,v 1.56 2010/03/02 13:26:12 llundin Exp $
00002  *
00003  * This file is part of the ISAAC 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: llundin $
00023  * $Date: 2010/03/02 13:26:12 $
00024  * $Revision: 1.56 $
00025  * $Name: HEAD $
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 
00045 #include "isaac_utils.h"
00046 #include "isaac_pfits.h"
00047 #include "isaac_dfs.h"
00048 
00049 /*-----------------------------------------------------------------------------
00050                                 Define
00051  -----------------------------------------------------------------------------*/
00052 
00053 #define PHOT_STAR_RADIUS                30.0
00054 #define PHOT_BACKGROUND_R1              40.0
00055 #define PHOT_BACKGROUND_R2              60.0
00056 
00057 #define DEF_LOCATE_SX                   10
00058 #define DEF_LOCATE_SY                   10
00059 
00060 /*-----------------------------------------------------------------------------
00061                             Functions prototypes
00062  -----------------------------------------------------------------------------*/
00063 
00064 static int isaac_img_zpoint_create(cpl_plugin *);
00065 static int isaac_img_zpoint_exec(cpl_plugin *);
00066 static int isaac_img_zpoint_destroy(cpl_plugin *);
00067 static int isaac_img_zpoint(cpl_parameterlist *, cpl_frameset *);
00068 static cpl_table * isaac_img_zpoint_reduce(cpl_frameset *, const char *,
00069         const char *, const char *, const char *, const char *, cpl_image **);
00070 static cpl_imagelist * isaac_img_zpoint_load(cpl_frameset *, const char *, 
00071         const char *, const char *, const char *);
00072 static int isaac_img_zpoint_save(cpl_table *, cpl_image *, cpl_frameset *, 
00073         cpl_parameterlist *, cpl_frameset *);
00074 static cpl_table * isaac_img_zpoint_photom(cpl_imagelist *, cpl_bivector *);
00075 static int isaac_img_zpoint_get_mag(const char *, double, double, isaac_band);
00076 static int isaac_img_zpoint_gradients(cpl_vector *, cpl_bivector *, double *, 
00077         double *, double *, double *);
00078 static cpl_bivector * isaac_img_zpoint_get_offsets(cpl_frameset *);
00079 static int isaac_img_zpoint_compute_keywords(cpl_frameset *, double *,double *);
00080 static cpl_image * isaac_img_zpoint_check_im(cpl_imagelist *,
00081         cpl_bivector *, double, double, double);
00082 
00083 /*-----------------------------------------------------------------------------
00084                             Static variables
00085  -----------------------------------------------------------------------------*/
00086 
00087 static struct {
00088     /* Inputs */
00089     double      ra;
00090     double      dec;
00091     double      magnitude;
00092     int         sx;
00093     int         sy;
00094     double      phot_star_radius;
00095     double      phot_bg_r1;
00096     double      phot_bg_r2;
00097     int         chopping;
00098     int         check_im;
00099     /* Outputs */
00100     double      dit;
00101     char        filter[512];
00102     isaac_band  band;
00103     char        starname[512];
00104     char        sptype[512];
00105     char        catalog[512];
00106     double      zpoint;
00107     double      zpointrms;
00108     double      flux_med;
00109     double      gradx;
00110     double      grady;
00111     double      graddx;
00112     double      graddy;
00113     double      fwhm_mean;
00114 } isaac_img_zpoint_config;
00115 
00116 static char isaac_img_zpoint_description[] = 
00117 "isaac_img_zpoint -- Zero point recipe\n"
00118 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00119 "raw-file.fits "ISAAC_IMG_ZPOINT_RAW" or\n"
00120 "raw-file.fits "ISAAC_IMG_ZPOINT_CHOPPING_RAW" or\n"
00121 "stdstars.fits "ISAAC_CALIB_STDSTARS" or\n"
00122 "flat-file.fits "ISAAC_CALIB_FLAT" or\n"
00123 "detlin-a-file.fits "ISAAC_CALIB_DETLIN_A" or\n"
00124 "detlin-b-file.fits "ISAAC_CALIB_DETLIN_B" or\n"
00125 "detlin-c-file.fits "ISAAC_CALIB_DETLIN_C"\n";
00126 
00127 /*-----------------------------------------------------------------------------
00128                                 Functions code
00129  -----------------------------------------------------------------------------*/
00130 
00131 /*----------------------------------------------------------------------------*/
00139 /*----------------------------------------------------------------------------*/
00140 int cpl_plugin_get_info(cpl_pluginlist * list)
00141 {
00142     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00143     cpl_plugin  *   plugin = &recipe->interface;
00144 
00145     cpl_plugin_init(plugin,
00146                     CPL_PLUGIN_API,
00147                     ISAAC_BINARY_VERSION,
00148                     CPL_PLUGIN_TYPE_RECIPE,
00149                     "isaac_img_zpoint",
00150                     "Zero point computation recipe",
00151                     isaac_img_zpoint_description,
00152                     "Lars Lundin",
00153                     PACKAGE_BUGREPORT,
00154                     isaac_get_license(),
00155                     isaac_img_zpoint_create,
00156                     isaac_img_zpoint_exec,
00157                     isaac_img_zpoint_destroy);
00158 
00159     cpl_pluginlist_append(list, plugin);
00160     
00161     return 0;
00162 }
00163 
00164 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 static int isaac_img_zpoint_create(cpl_plugin * plugin)
00175 {
00176     cpl_recipe      * recipe;
00177     cpl_parameter   * p;
00178 
00179     /* Get the recipe out of the plugin */
00180     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00181         recipe = (cpl_recipe *)plugin;
00182     else return -1;
00183 
00184     /* Create the parameters list in the cpl_recipe object */
00185     recipe->parameters = cpl_parameterlist_new();
00186 
00187     /* Fill the parameters list */
00188     /* --star_r */
00189     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.star_r",CPL_TYPE_DOUBLE,
00190             "the star radius", "isaac.isaac_img_zpoint", PHOT_STAR_RADIUS);
00191     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "star_r");
00192     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00193     cpl_parameterlist_append(recipe->parameters, p);
00194     /* --bg_r1 */
00195     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.bg_r1", CPL_TYPE_DOUBLE,
00196             "the internal background radius", "isaac.isaac_img_zpoint", 
00197             PHOT_BACKGROUND_R1);
00198     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r1");
00199     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00200     cpl_parameterlist_append(recipe->parameters, p);
00201     /* --bg_r2 */
00202     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.bg_r2", CPL_TYPE_DOUBLE,
00203             "the external background radius", "isaac.isaac_img_zpoint", 
00204             PHOT_BACKGROUND_R2);
00205     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bg_r2");
00206     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00207     cpl_parameterlist_append(recipe->parameters, p);
00208     /* --ra */
00209     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.ra", CPL_TYPE_DOUBLE,
00210             "RA in degrees", "isaac.isaac_img_zpoint", 999.0);
00211     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ra");
00212     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00213     cpl_parameterlist_append(recipe->parameters, p);
00214     /* --dec */
00215     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.dec", CPL_TYPE_DOUBLE,
00216             "DEC in degrees", "isaac.isaac_img_zpoint", 999.0);
00217     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dec");
00218     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00219     cpl_parameterlist_append(recipe->parameters, p);
00220     /* --mag */
00221     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.mag", CPL_TYPE_DOUBLE,
00222             "magnitude", "isaac.isaac_img_zpoint", 99.0);
00223     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "mag");
00224     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00225     cpl_parameterlist_append(recipe->parameters, p);
00226     /* --sx */
00227     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.sx", CPL_TYPE_INT, 
00228             "x size of the search window", "isaac.isaac_img_zpoint",
00229             DEF_LOCATE_SX);
00230     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sx");
00231     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00232     cpl_parameterlist_append(recipe->parameters, p);
00233     /* --sy */
00234     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.sy", CPL_TYPE_INT, 
00235             "y size of the search window", "isaac.isaac_img_zpoint",
00236             DEF_LOCATE_SY);
00237     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sy");
00238     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00239     cpl_parameterlist_append(recipe->parameters, p);
00240     /* --check_im */
00241     p = cpl_parameter_new_value("isaac.isaac_img_zpoint.check_im",
00242             CPL_TYPE_BOOL, "flag to create the check image",
00243             "isaac.isaac_img_zpoint", FALSE);
00244     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "check_im");
00245     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00246     cpl_parameterlist_append(recipe->parameters, p);
00247 
00248     /* Return */
00249     return 0;
00250 }
00251 
00252 /*----------------------------------------------------------------------------*/
00258 /*----------------------------------------------------------------------------*/
00259 static int isaac_img_zpoint_exec(cpl_plugin * plugin)
00260 {
00261     cpl_recipe  *   recipe;
00262 
00263     /* Get the recipe out of the plugin */
00264     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00265         recipe = (cpl_recipe *)plugin;
00266     else return -1;
00267 
00268     return isaac_img_zpoint(recipe->parameters, recipe->frames);
00269 }
00270 
00271 /*----------------------------------------------------------------------------*/
00277 /*----------------------------------------------------------------------------*/
00278 static int isaac_img_zpoint_destroy(cpl_plugin * plugin)
00279 {
00280     cpl_recipe  *   recipe;
00281 
00282     /* Get the recipe out of the plugin */
00283     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00284         recipe = (cpl_recipe *)plugin;
00285     else return -1;
00286 
00287     cpl_parameterlist_delete(recipe->parameters);
00288     return 0;
00289 }
00290 
00291 /*----------------------------------------------------------------------------*/
00298 /*----------------------------------------------------------------------------*/
00299 static int isaac_img_zpoint(
00300         cpl_parameterlist   *   parlist, 
00301         cpl_frameset        *   framelist)
00302 {
00303     cpl_parameter   *   par;
00304     const char      *   stdstars;
00305     const char      *   flat;
00306     const char      *   detlin_a;
00307     const char      *   detlin_b;
00308     const char      *   detlin_c;
00309     cpl_frameset    *   rawframes;
00310     cpl_table       *   tab;
00311     cpl_image       *   check_im;
00312     
00313     /* Initialise */
00314     rawframes = NULL;
00315     par = NULL;
00316     isaac_img_zpoint_config.starname[0] = (char)0;
00317     isaac_img_zpoint_config.sptype[0] = (char)0;
00318     isaac_img_zpoint_config.filter[0] = (char)0;
00319     isaac_img_zpoint_config.catalog[0] = (char)0;
00320     isaac_img_zpoint_config.gradx = -1.0;
00321     isaac_img_zpoint_config.grady = -1.0;
00322     isaac_img_zpoint_config.graddx = -1.0;
00323     isaac_img_zpoint_config.graddy = -1.0;
00324     isaac_img_zpoint_config.fwhm_mean = -1.0;
00325 
00326     /* Retrieve input parameters */
00327     /* --ra */
00328     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.ra");
00329     isaac_img_zpoint_config.ra = cpl_parameter_get_double(par);
00330     /* --dec */
00331     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.dec");
00332     isaac_img_zpoint_config.dec = cpl_parameter_get_double(par);
00333     /* --mag */
00334     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.mag");
00335     isaac_img_zpoint_config.magnitude = cpl_parameter_get_double(par);
00336     /* --sx */
00337     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.sx");
00338     isaac_img_zpoint_config.sx = cpl_parameter_get_int(par);
00339     /* --sy */
00340     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.sy");
00341     isaac_img_zpoint_config.sy = cpl_parameter_get_int(par);
00342     /* --star_r */
00343     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.star_r");
00344     isaac_img_zpoint_config.phot_star_radius = cpl_parameter_get_double(par);
00345     /* --bg_r1 */
00346     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.bg_r1");
00347     isaac_img_zpoint_config.phot_bg_r1 = cpl_parameter_get_double(par);
00348     /* --bg_r2 */
00349     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.bg_r2");
00350     isaac_img_zpoint_config.phot_bg_r2 = cpl_parameter_get_double(par);
00351     /* --check_im */
00352     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_zpoint.check_im");
00353     isaac_img_zpoint_config.check_im = cpl_parameter_get_bool(par);
00354 
00355     /* Identify the RAW and CALIB frames in the input frameset */
00356     if (isaac_dfs_set_groups(framelist)) {
00357         cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
00358         return -1;
00359     }
00360 
00361     /* Retrieve calibration data */
00362     detlin_a = isaac_extract_filename(framelist, ISAAC_CALIB_DETLIN_A);
00363     detlin_b = isaac_extract_filename(framelist, ISAAC_CALIB_DETLIN_B);
00364     detlin_c = isaac_extract_filename(framelist, ISAAC_CALIB_DETLIN_C);
00365     flat     = isaac_extract_filename(framelist, ISAAC_CALIB_FLAT);
00366     stdstars = isaac_extract_filename(framelist, ISAAC_CALIB_STDSTARS);
00367 
00368     /* Retrieve raw frames */
00369     if ((rawframes = isaac_extract_frameset(framelist,
00370                     ISAAC_IMG_ZPOINT_RAW)) != NULL) {
00371         isaac_img_zpoint_config.chopping = 0;
00372     } else if ((rawframes = isaac_extract_frameset(framelist,
00373                     ISAAC_IMG_ZPOINT_CHOPPING_RAW)) != NULL) {
00374         isaac_img_zpoint_config.chopping = 1;
00375     } else {
00376         cpl_msg_error(cpl_func, "Cannot find raw frames in the input list");
00377         return -1;
00378     }
00379 
00380     /* Compute the zpoint values */
00381     cpl_msg_info(cpl_func, "Reduce the data");
00382     cpl_msg_indent_more();
00383     if ((tab = isaac_img_zpoint_reduce(rawframes, stdstars, flat, detlin_a, 
00384                     detlin_b, detlin_c, &check_im)) == NULL) {
00385         cpl_msg_error(cpl_func, "Cannot reduce the data");
00386         cpl_frameset_delete(rawframes);
00387         cpl_msg_indent_less();
00388         return -1;
00389     }
00390     cpl_msg_indent_less();
00391 
00392     /* Save the products */
00393     cpl_msg_info(cpl_func, "Save the paf file");
00394     cpl_msg_indent_more();
00395     if (isaac_img_zpoint_save(tab, check_im, rawframes, parlist, 
00396                 framelist) == -1) {
00397         cpl_msg_error(cpl_func, "Cannot save the paf file");
00398         if (check_im) cpl_image_delete(check_im);
00399         cpl_frameset_delete(rawframes);
00400         cpl_table_delete(tab);
00401         cpl_msg_indent_less();
00402         return -1;
00403     }
00404     cpl_frameset_delete(rawframes);
00405     cpl_msg_indent_less();
00406 
00407     /* Free and return */
00408     if (check_im) cpl_image_delete(check_im);
00409     cpl_table_delete(tab);
00410     return 0;
00411 }
00412 
00413 /*----------------------------------------------------------------------------*/
00425 /*----------------------------------------------------------------------------*/
00426 static cpl_table * isaac_img_zpoint_reduce(
00427         cpl_frameset    *   set,
00428         const char      *   stdstars,
00429         const char      *   flat,
00430         const char      *   detlin_a,
00431         const char      *   detlin_b,
00432         const char      *   detlin_c,
00433         cpl_image       **  check_im)
00434 {
00435     cpl_frame           *   cur_frame;
00436     cpl_propertylist    *   plist;
00437     const char          *   sval;
00438     cpl_imagelist       *   imlist;
00439     cpl_image           *   ima;
00440     cpl_mask            *   kernel;
00441     int                     niter;
00442     int                     size_x, size_y;
00443     double                  pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist;
00444     cpl_apertures       *   aperts;
00445     cpl_bivector        *   offsets,
00446                         *   positions;
00447     double                  off_x, off_y;
00448     int                     llx, lly, urx, ury;
00449     double                  val, sqsum, zprms, avg_zp;
00450     cpl_table           *   tab_res;
00451     cpl_vector          *   tmp_vec;
00452     int                     nb_ok;
00453     int                     i;
00454 
00455     /* Initialise */
00456     *check_im = NULL;
00457     pos_x_cen = pos_y_cen = -1.0;
00458 
00459     /* Get the filter name, DIT, RA and DEC  */
00460     cur_frame = cpl_frameset_get_frame(set, 0);
00461     plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00462     if ((sval = isaac_pfits_get_filter(plist)) == NULL) {
00463         cpl_propertylist_delete(plist);
00464         cpl_ensure(0, cpl_error_get_code(), NULL);
00465     }
00466     (void)strncpy(isaac_img_zpoint_config.filter, sval, 512);
00467     isaac_img_zpoint_config.filter[511] = (char)0;
00468     if (isaac_img_zpoint_config.ra > 998.0) 
00469         isaac_img_zpoint_config.ra = isaac_pfits_get_ra(plist);
00470     if (isaac_img_zpoint_config.dec > 998.0) 
00471         isaac_img_zpoint_config.dec = isaac_pfits_get_dec(plist);
00472     isaac_img_zpoint_config.dit = isaac_pfits_get_dit(plist);
00473     cpl_propertylist_delete(plist);
00474     if (cpl_error_get_code()) {
00475         cpl_msg_error(cpl_func, "Cannot get some header informations");
00476         return NULL;
00477     }
00478     cpl_msg_info(cpl_func, "Using star at position: RA = %g; DEC = %g", 
00479             isaac_img_zpoint_config.ra, isaac_img_zpoint_config.dec);
00480    
00481     /* Get the band */
00482     if ((isaac_img_zpoint_config.band = 
00483                 isaac_get_bbfilter(isaac_img_zpoint_config.filter)) ==
00484             ISAAC_BAND_UNKNOWN) {
00485         cpl_msg_error(cpl_func, "Cannot associate the filter to a BB one");
00486         return NULL;
00487     }
00488     
00489     /* Get the standard star information from database */
00490     if (isaac_img_zpoint_config.magnitude > 98.0) {
00491         cpl_msg_info(cpl_func, "Get the star magnitude");
00492         if (isaac_img_zpoint_get_mag(stdstars, isaac_img_zpoint_config.ra,
00493                                     isaac_img_zpoint_config.dec,
00494                                     isaac_img_zpoint_config.band) == -1) {
00495             cpl_msg_error(cpl_func,"Cannot get the magnitude from the catalog");
00496             return NULL;
00497         }
00498     }
00499     cpl_msg_info(cpl_func, "Star magnitude with filter %s : %g",
00500             isaac_img_zpoint_config.filter,
00501             isaac_img_zpoint_config.magnitude);
00502     
00503     /* Load the images */
00504     cpl_msg_info(cpl_func, "Load images");
00505     if ((imlist = isaac_img_zpoint_load(set, flat, detlin_a, detlin_b,
00506                     detlin_c)) == NULL) {
00507         cpl_msg_error(cpl_func, "Cannot load the images"); 
00508         return NULL;
00509     }
00510        
00511     /* Detect the central object in the first frame */
00512     cpl_msg_info(cpl_func, "Detect a bright object in the first frame");
00513     size_x = cpl_image_get_size_x(cpl_imagelist_get(imlist, 0));
00514     size_y = cpl_image_get_size_y(cpl_imagelist_get(imlist, 0));
00515     kernel = cpl_mask_new(3, 3);
00516     cpl_mask_not(kernel);
00517     ima = cpl_image_new(size_x, size_y,
00518                         cpl_image_get_type(cpl_imagelist_get(imlist, 0)));
00519     cpl_image_filter_mask(ima, cpl_imagelist_get(imlist, 0), kernel,
00520                           CPL_FILTER_MEDIAN, CPL_BORDER_FILTER);
00521     cpl_mask_delete(kernel);
00522     aperts = cpl_apertures_extract_sigma(ima, 5.0);
00523     cpl_image_delete(ima);
00524     min_dist = size_x * size_x + size_y * size_y;
00525     for (i=0; i<cpl_apertures_get_size(aperts); i++) {
00526         pos_x = cpl_apertures_get_centroid_x(aperts, i+1);
00527         pos_y = cpl_apertures_get_centroid_y(aperts, i+1);
00528         dist = (pos_x-size_x/2)*(pos_x-size_x/2) +
00529                (pos_y-size_y/2)*(pos_y-size_y/2);
00530         if (dist<min_dist) {
00531             min_dist = dist;
00532             pos_x_cen = pos_x;
00533             pos_y_cen = pos_y;
00534         }
00535     }
00536     cpl_apertures_delete(aperts);
00537     if (cpl_error_get_code()) {
00538         cpl_msg_error(cpl_func, "Cannot find the central object");
00539         cpl_imagelist_delete(imlist);
00540         return NULL;
00541     }
00542     cpl_msg_info(cpl_func, "Bright object position: %g %g", 
00543             pos_x_cen, pos_y_cen);
00544    
00545     /* Get the offsets */
00546     cpl_msg_info(cpl_func, "Read the offsets in the header");
00547     offsets = isaac_img_zpoint_get_offsets(set);
00548     
00549     /* Deduce the objects positions  */
00550     niter = cpl_imagelist_get_size(imlist);
00551     positions   = cpl_bivector_new(niter);
00552     for (i=0; i<niter; i++) {
00553         off_x = cpl_vector_get(cpl_bivector_get_x(offsets), i) -
00554             cpl_vector_get(cpl_bivector_get_x(offsets), 0);
00555         off_y = cpl_vector_get(cpl_bivector_get_y(offsets), i) -
00556             cpl_vector_get(cpl_bivector_get_y(offsets), 0);
00557         pos_x = pos_x_cen + off_x;
00558         pos_y = pos_y_cen + off_y;
00559         cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
00560         cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
00561     }
00562     cpl_bivector_delete(offsets);
00563 
00564     /* Refine the positions */
00565     cpl_msg_info(cpl_func, "Refine the star positions");
00566     for (i=1; i<niter; i++) {
00567         pos_x = cpl_vector_get(cpl_bivector_get_x(positions), i);
00568         pos_y = cpl_vector_get(cpl_bivector_get_y(positions), i);
00569         llx = pos_x - isaac_img_zpoint_config.sx;
00570         urx = pos_x + isaac_img_zpoint_config.sx;
00571         lly = pos_y - isaac_img_zpoint_config.sy;
00572         ury = pos_y + isaac_img_zpoint_config.sy;
00573         ima = cpl_imagelist_get(imlist, i);
00574         pos_x = cpl_image_get_centroid_x_window(ima, llx, lly, urx, ury);
00575         pos_y = cpl_image_get_centroid_y_window(ima, llx, lly, urx, ury);
00576         cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
00577         cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
00578     }
00579     if (cpl_error_get_code()) {
00580         cpl_msg_error(cpl_func, "Cannot refine the positions");
00581         cpl_imagelist_delete(imlist);
00582         cpl_bivector_delete(positions);
00583         return NULL;
00584     }
00585 
00586     /* Create the check image if requested */
00587     if (isaac_img_zpoint_config.check_im) {
00588         *check_im = isaac_img_zpoint_check_im(imlist, positions, 
00589                 isaac_img_zpoint_config.phot_star_radius, 
00590                 isaac_img_zpoint_config.phot_bg_r1,
00591                 isaac_img_zpoint_config.phot_bg_r2);
00592     }
00593 
00594     /* Reduce the images */
00595     cpl_msg_info(cpl_func, "Compute the photometry");
00596     cpl_msg_indent_more();
00597     if ((tab_res = isaac_img_zpoint_photom(imlist, positions)) == NULL) {
00598         cpl_msg_error(cpl_func, "Cannot reduce");
00599         cpl_bivector_delete(positions);
00600         cpl_imagelist_delete(imlist);
00601         cpl_image_delete(*check_im);
00602         *check_im = NULL;
00603         cpl_msg_indent_less();
00604         return NULL;
00605     } 
00606     cpl_msg_indent_less();
00607     cpl_imagelist_delete(imlist);
00608 
00609     /* Compute the median of the flux */
00610     isaac_img_zpoint_config.flux_med =
00611         cpl_table_get_column_median(tab_res, "FLUX"); 
00612 
00613     /* Get the mean FWHM  */
00614     isaac_img_zpoint_config.fwhm_mean =
00615         cpl_table_get_column_mean(tab_res, "FWHMX");
00616     isaac_img_zpoint_config.fwhm_mean +=
00617         cpl_table_get_column_mean(tab_res, "FWHMY");
00618     isaac_img_zpoint_config.fwhm_mean /= 2.0;
00619 
00620     /* Compute the flux gradient */
00621     tmp_vec = cpl_vector_new(niter);
00622     for (i=0; i<niter; i++) {
00623         cpl_vector_set(tmp_vec, i, 
00624                 cpl_table_get_double(tab_res, "FLUX", i, NULL));
00625     }
00626     isaac_img_zpoint_gradients(tmp_vec, positions,
00627             &(isaac_img_zpoint_config.gradx),
00628             &(isaac_img_zpoint_config.grady),
00629             &(isaac_img_zpoint_config.graddx),
00630             &(isaac_img_zpoint_config.graddy));
00631     cpl_vector_delete(tmp_vec);
00632     cpl_bivector_delete(positions);
00633     
00634     /* Compute the averages of the results for the zero point */
00635     tmp_vec = cpl_vector_new(niter);
00636     for (i=0; i<niter; i++) {
00637         cpl_vector_set(tmp_vec, i, 
00638                 cpl_table_get_double(tab_res, "ZPOINT", i, NULL));
00639     }
00640     nb_ok = 0;
00641     avg_zp = sqsum = 0.0;
00642     if (isaac_img_zpoint_config.chopping == 0) {
00643         cpl_vector_sort(tmp_vec, 1);
00644         /* Reject highest and lowest value */
00645         for (i=1; i<niter-1; i++) {
00646             val = cpl_vector_get(tmp_vec, i);
00647             if (val > 0.0) {
00648                 avg_zp += val;
00649                 sqsum += val * val;
00650                 nb_ok ++;
00651             }
00652         }
00653     } else if (isaac_img_zpoint_config.chopping == 1) {
00654         for (i=0; i<niter; i++) {
00655             val = cpl_vector_get(tmp_vec, i);
00656             if (val > 0.0) {
00657                 avg_zp += val;
00658                 sqsum += val * val;
00659                 nb_ok ++;
00660             }
00661         }
00662     } else {
00663         cpl_msg_error(cpl_func, "unsupported mode"); 
00664         cpl_table_delete(tab_res);
00665         cpl_vector_delete(tmp_vec);
00666         cpl_image_delete(*check_im);
00667         *check_im = NULL;
00668         cpl_msg_indent_less();
00669         return NULL;
00670     }
00671     cpl_vector_delete(tmp_vec);
00672     if (nb_ok < 1) {
00673         cpl_msg_error(cpl_func, "no valid zpoint measurement: cannot compute"); 
00674         isaac_img_zpoint_config.zpoint    = -1.0;
00675         isaac_img_zpoint_config.zpointrms = -1.0;
00676     } else {
00677         avg_zp /= (double)nb_ok;
00678         sqsum  /= (double)nb_ok;
00679         zprms = sqsum - avg_zp * avg_zp;
00680         zprms = zprms > 0 ? sqrt(zprms) : 0;
00681         isaac_img_zpoint_config.zpoint    = avg_zp;
00682         isaac_img_zpoint_config.zpointrms = zprms;
00683     }
00684 
00685     /* Print final results */
00686     cpl_msg_info(cpl_func, "***** FINAL RESULTS *****");
00687     cpl_msg_info(cpl_func, "Zero point :   %g", isaac_img_zpoint_config.zpoint);
00688     cpl_msg_info(cpl_func, "Zero p. RMS:   %g", isaac_img_zpoint_config.zpointrms);
00689     
00690     return tab_res;
00691 }
00692 
00693 /*----------------------------------------------------------------------------*/
00708 /*----------------------------------------------------------------------------*/
00709 static cpl_image * isaac_img_zpoint_check_im(
00710         cpl_imagelist   *   imlist,
00711         cpl_bivector    *   positions,
00712         double              r1,
00713         double              r2,
00714         double              r3)
00715 {
00716     int                     nima, in_nx, in_ny, nx, ny, box_sz, llx, lly;
00717     cpl_image           *   in_ima;
00718     float               *   pin_ima;
00719     cpl_image           *   out_ima;
00720     float               *   pout_ima;
00721     int                     out_pos, in_pos;
00722     double              *   pos_x;
00723     double              *   pos_y;
00724     double                  dist;
00725     int                     i, j, k;
00726  
00727     /* Check entries */
00728     if (imlist == NULL) return NULL;
00729     if (positions == NULL) return NULL;
00730     nima = cpl_imagelist_get_size(imlist);
00731     if (cpl_bivector_get_size(positions) != nima) return NULL;
00732     
00733     /* Initialise */
00734     in_ima = cpl_imagelist_get(imlist, 0);
00735     in_nx = cpl_image_get_size_x(in_ima);
00736     in_ny = cpl_image_get_size_y(in_ima);
00737     pos_x = cpl_bivector_get_x_data(positions);
00738     pos_y = cpl_bivector_get_y_data(positions);
00739 
00740     /* Create the output image */
00741     box_sz = 2 * (int)r3 + 1;
00742     nx = nima * box_sz;    
00743     ny = box_sz;
00744     out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
00745     pout_ima = cpl_image_get_data_float(out_ima);
00746 
00747     /* Loop on the input images */
00748     for (k=0; k<nima; k++) {
00749         /* Get the current input image */
00750         in_ima = cpl_imagelist_get(imlist, k);
00751         pin_ima = cpl_image_get_data_float(in_ima);
00752 
00753         /* Get the sub image position */
00754         llx = (int)(pos_x[k] - r3);
00755         lly = (int)(pos_y[k] - r3);
00756         
00757         for (i=0; i<box_sz; i++) {
00758             for (j=0; j<box_sz; j++) {
00759                 out_pos = (box_sz * k) + i + j * nx; 
00760                 in_pos = llx + i + (lly+j) * in_nx;
00761                 if (in_pos >= 0 && in_pos < in_nx*in_ny)
00762                     pout_ima[out_pos] = pin_ima[in_pos];
00763             }
00764         }
00765     }
00766 
00767     /* Draw circles */
00768     for (i=0; i<box_sz; i++) {
00769         for (j=0; j<box_sz; j++) {
00770             dist = sqrt((i - (box_sz/2)) * (i - (box_sz/2)) +
00771                 (j - (box_sz/2)) * (j - (box_sz/2)));
00772             if ((fabs(dist-sqrt(r1*r1))) < 0.5) pout_ima[i + j * nx] = 10000;
00773             if ((fabs(dist-sqrt(r2*r2))) < 0.5) pout_ima[i + j * nx] = 10000;
00774             if ((fabs(dist-sqrt(r3*r3))) < 0.5) pout_ima[i + j * nx] = 10000;
00775         }
00776     }
00777 
00778     /* Return  */
00779     return out_ima;
00780 }
00781 
00782 /*----------------------------------------------------------------------------*/
00788 /*----------------------------------------------------------------------------*/
00789 static cpl_imagelist * isaac_img_zpoint_load(
00790         cpl_frameset    *   set,
00791         const char      *   flat,
00792         const char      *   detlin_a,
00793         const char      *   detlin_b,
00794         const char      *   detlin_c)
00795 {
00796     cpl_imagelist       *   imlist;
00797     cpl_imagelist       *   imlist2;
00798     cpl_imagelist       *   diffs;
00799     cpl_image           *   ima;
00800     int                     niter;
00801     int                     i;
00802     
00803     /* Load the images */
00804     if (isaac_img_zpoint_config.chopping == 0) {
00805         if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
00806                         0)) == NULL) {
00807             cpl_msg_error(cpl_func, "Cannot load the images"); 
00808             return NULL;
00809         }
00810     } else if (isaac_img_zpoint_config.chopping == 1) {
00811         if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
00812                         0)) == NULL) {
00813             cpl_msg_error(cpl_func, "Cannot load the images"); 
00814             return NULL;
00815         }
00816         if ((imlist2 = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 2,
00817                         0)) == NULL) {
00818             cpl_msg_error(cpl_func, "Cannot load the images"); 
00819             cpl_imagelist_delete(imlist);
00820             return NULL;
00821         }
00822         cpl_imagelist_subtract(imlist, imlist2);
00823         cpl_imagelist_delete(imlist2);
00824     } else {
00825         cpl_msg_error(cpl_func, "Unsupported mode"); 
00826         return NULL;
00827     }
00828 
00829     /* Correct the detector linearity */
00830     if (detlin_a && detlin_b && detlin_c) {
00831         cpl_msg_info(cpl_func, "Correct for non-linearity");
00832         if (irplib_detlin_correct(imlist, detlin_a, detlin_b, detlin_c) == -1) {
00833             cpl_msg_error(cpl_func, "Cannot correct for non-linearity");
00834         }
00835     }
00836 
00837     /* Divide by the flatfield if one is provided */
00838     if (flat) {
00839         cpl_image * flat_im = cpl_image_load(flat, CPL_TYPE_UNSPECIFIED, 0, 0);
00840         cpl_error_code error = flat_im ? CPL_ERROR_NONE : cpl_error_get_code();
00841 
00842         cpl_msg_info(cpl_func, "Divide by the flat field");
00843 
00844         /* Threshold all pixels below the "mid-point" of FLT_MIN and 1, and
00845            all pixels above the "mid-point" of 1 and FLT_MAX. */
00846         error |= cpl_image_threshold(flat_im, sqrt(FLT_MIN), sqrt(FLT_MAX),
00847                                      1.0, 1.0);
00848         error |= cpl_imagelist_divide_image(imlist, flat_im);
00849         cpl_image_delete(flat_im);
00850 
00851         if (error) {
00852             cpl_imagelist_delete(imlist);
00853             (void)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00854                                         "Flat field correction failed");
00855             return NULL;
00856         }
00857     }
00858 
00859     /* Different in chopping and non-chopping modes */
00860     if (isaac_img_zpoint_config.chopping == 1) {
00861         diffs = imlist;
00862         imlist = NULL;
00863     } else if (isaac_img_zpoint_config.chopping == 0) {
00864         /* Build the difference image list */
00865         niter = 2 * cpl_imagelist_get_size(imlist) - 2;
00866         diffs = cpl_imagelist_new();
00867         for (i=0; i<cpl_imagelist_get_size(imlist)-1; i++) {
00868             ima = cpl_image_subtract_create(
00869                     cpl_imagelist_get(imlist, i),
00870                     cpl_imagelist_get(imlist, i+1));
00871             cpl_imagelist_set(diffs, ima, 2*i);
00872             ima = cpl_image_subtract_create(
00873                     cpl_imagelist_get(imlist, i+1),
00874                     cpl_imagelist_get(imlist, i));
00875             cpl_imagelist_set(diffs, ima, 2*i+1);
00876         }
00877         cpl_imagelist_delete(imlist);
00878         if (cpl_error_get_code()) {
00879             cpl_msg_error(cpl_func, "Cannot build the difference images");
00880             cpl_imagelist_delete(diffs);
00881             return NULL;
00882         }
00883     } else {
00884         cpl_msg_error(cpl_func, "Unsupported mode - abort");
00885         cpl_imagelist_delete(imlist);
00886         return NULL;
00887     }
00888     return diffs;
00889 }
00890 
00891 /*----------------------------------------------------------------------------*/
00900 /*----------------------------------------------------------------------------*/
00901 static cpl_table * isaac_img_zpoint_photom(
00902         cpl_imagelist       *   ilist,
00903         cpl_bivector        *   pos)
00904 {
00905     cpl_table       *   tab;
00906     int                 nbima;
00907     double              r, r1, r2, mag, dit;
00908     cpl_image       *   ima;
00909     double          *   pos_x;
00910     double          *   pos_y;
00911     double              bgd, fl, zp, peak, fwhm_x, fwhm_y;
00912     int                 i;
00913 
00914     /* Test entries */
00915     if (ilist == NULL) return NULL;
00916     if (pos == NULL) return NULL;
00917 
00918     /* Initialise */
00919     nbima = cpl_imagelist_get_size(ilist);
00920     mag = isaac_img_zpoint_config.magnitude;
00921     dit = isaac_img_zpoint_config.dit;
00922     r = isaac_img_zpoint_config.phot_star_radius;
00923     r1 = isaac_img_zpoint_config.phot_bg_r1;
00924     r2 = isaac_img_zpoint_config.phot_bg_r2;
00925 
00926     /* Create the output table */
00927     tab = cpl_table_new(nbima);
00928     cpl_table_new_column(tab, "POSX", CPL_TYPE_DOUBLE);
00929     cpl_table_new_column(tab, "POSY", CPL_TYPE_DOUBLE);
00930     cpl_table_new_column(tab, "ZPOINT", CPL_TYPE_DOUBLE);
00931     cpl_table_new_column(tab, "FLUX", CPL_TYPE_DOUBLE);
00932     cpl_table_new_column(tab, "PEAK", CPL_TYPE_DOUBLE);
00933     cpl_table_new_column(tab, "BGD", CPL_TYPE_DOUBLE);
00934     cpl_table_new_column(tab, "FWHMX", CPL_TYPE_DOUBLE);
00935     cpl_table_new_column(tab, "FWHMY", CPL_TYPE_DOUBLE);
00936 
00937     /* Loop on the images */
00938     pos_x = cpl_bivector_get_x_data(pos);
00939     pos_y = cpl_bivector_get_y_data(pos);
00940     for (i=0; i<nbima; i++) {
00941         /* Get the current image */
00942         ima = cpl_imagelist_get(ilist, i);
00943         /* Compute the photometry */
00944         /* Background */
00945         bgd = irplib_strehl_ring_background(ima, (int)(pos_x[i]), 
00946                 (int)(pos_y[i]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN);
00947         /* Flux */
00948         fl = irplib_strehl_disk_flux(ima, 
00949                 (int)(pos_x[i]), (int)(pos_y[i]), (int)r, bgd); 
00950         /* Zero Point */
00951         zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
00952         cpl_msg_info(cpl_func, "Zero point nb %d:   %g", i+1, zp);
00953         /* Peak */
00954         peak = cpl_image_get_max_window(ima,
00955                 (int)(pos_x[i]-5), (int)(pos_y[i]-5), 
00956                 (int)(pos_x[i]+5), (int)(pos_y[i]+5)); 
00957 
00958         /* FWHM_X / FWHM_Y */
00959         if (cpl_image_get_fwhm(ima, (int)(pos_x[i]), (int)(pos_y[i]),
00960                 &fwhm_x, &fwhm_y) != CPL_ERROR_NONE) {
00961             cpl_msg_debug(cpl_func, "Cannot compute FWHM for image %d", i+1);
00962             cpl_error_reset();
00963         }
00964                 
00965         /* Put the results in the table */
00966         cpl_table_set_double(tab, "POSX", i, pos_x[i]);
00967         cpl_table_set_double(tab, "POSY", i, pos_y[i]);
00968         cpl_table_set_double(tab, "ZPOINT", i, zp);
00969         cpl_table_set_double(tab, "FLUX", i, fl);
00970         cpl_table_set_double(tab, "PEAK", i, peak);
00971         cpl_table_set_double(tab, "BGD", i, bgd);
00972         cpl_table_set_double(tab, "FWHMX", i, fwhm_x);
00973         cpl_table_set_double(tab, "FWHMY", i, fwhm_y);
00974     }
00975     return tab;
00976 }
00977 
00978 /*----------------------------------------------------------------------------*/
00979 /*
00980   @brief    Compute the flux gradients
00981   @param    flux        a vector with 8 fluxes
00982   @param    positions   a bivector with the 8 positions
00983   @param    gradx       the x gradient
00984   @param    grady       the y gradient
00985   @param    graddx      the x gradient error
00986   @param    graddy      the y gradient error
00987   @return   0 if ok, -1 otherwise
00988      3    2
00989        1
00990      4    5
00991  */
00992 /*----------------------------------------------------------------------------*/
00993 static int isaac_img_zpoint_gradients(
00994         cpl_vector      *   flux,
00995         cpl_bivector    *   positions,
00996         double          *   gradx,
00997         double          *   grady,
00998         double          *   graddx,
00999         double          *   graddy) 
01000 {
01001     int                 nflux;
01002     double              two, three, four, five, mean;
01003     double              two_pos_x, three_pos_x, four_pos_x, five_pos_x,
01004                         two_pos_y, three_pos_y, four_pos_y, five_pos_y;
01005     double          *   pflux;
01006     double          *   ppos_x;
01007     double          *   ppos_y;
01008 
01009     /* Test entries */
01010     if (flux == NULL) return -1;
01011 
01012     /* Initialise */
01013     nflux = cpl_vector_get_size(flux);
01014     if (nflux != 8) {
01015         *gradx = -1.0;
01016         *grady = -1.0;
01017         *graddx = -1.0;
01018         *graddy = -1.0;
01019         return 0;
01020     }
01021     mean = cpl_vector_get_mean(flux);
01022 
01023     /* Get the 4 positions fluxes */
01024     pflux = cpl_vector_get_data(flux);
01025     ppos_x = cpl_bivector_get_x_data(positions);
01026     ppos_y = cpl_bivector_get_y_data(positions);
01027     /* Flux */
01028     two = (pflux[1] + pflux[2]) / 2.0; 
01029     three = (pflux[3] + pflux[4]) / 2.0; 
01030     four = (pflux[5] + pflux[6]) / 2.0; 
01031     five = pflux[7];
01032     /* Positions */
01033     two_pos_x = (ppos_x[1] + ppos_x[2]) / 2.0;
01034     two_pos_y = (ppos_y[1] + ppos_y[2]) / 2.0;
01035     three_pos_x = (ppos_x[3] + ppos_x[4]) / 2.0; 
01036     three_pos_y = (ppos_y[3] + ppos_y[4]) / 2.0; 
01037     four_pos_x = (ppos_x[5] + ppos_x[6]) / 2.0; 
01038     four_pos_y = (ppos_y[5] + ppos_y[6]) / 2.0; 
01039     five_pos_x = ppos_x[7];
01040     five_pos_y = ppos_y[7];
01041 
01042     /* Compute grad */
01043     *gradx = (two + five - three - four) / 2;
01044     *grady = (three + two - four - five) / 2;
01045 
01046     /* Normalise to the nominal distance */
01047     *gradx *= (512 / ((two_pos_x + five_pos_x - three_pos_x - four_pos_x)/2));
01048     *grady *= (512 / ((three_pos_y + two_pos_y - four_pos_y - five_pos_y)/2));
01049 
01050     /* Compute error */
01051     *graddx = sqrt((two-five)*(two-five) + (three-four)*(three-four))/2.0;
01052     *graddy = sqrt((three-two)*(three-two) + (four-five)*(four-five))/2.0;
01053     
01054     /* Normalise */
01055     *gradx /= mean;
01056     *grady /= mean;
01057     *graddx /= mean;
01058     *graddy /= mean;
01059 
01060     return 0;
01061 }
01062 
01063 /*----------------------------------------------------------------------------*/
01069 /*----------------------------------------------------------------------------*/
01070 static cpl_bivector * isaac_img_zpoint_get_offsets(cpl_frameset * set) 
01071 {
01072     int                     nframes;
01073     int                     noffsets;
01074     cpl_bivector        *   offs;
01075     cpl_frame           *   cur_frame;
01076     cpl_propertylist    *   plist;
01077     double                  off_x, off_y;
01078     int                     i;
01079 
01080     /* Test entries */
01081     if (set == NULL) return NULL;
01082     
01083     /* Initialize */
01084     nframes = cpl_frameset_get_size(set);
01085     
01086     /* Different in chopping and non-chopping modes */
01087     if (isaac_img_zpoint_config.chopping == 1) {
01088         offs = isaac_get_offsets(set);
01089     } else if (isaac_img_zpoint_config.chopping == 0) {
01090         noffsets = 2 * (nframes-1);
01091 
01092         /* Check error code */
01093         if (cpl_error_get_code()) return NULL;
01094         
01095         /* Create the bivector */
01096         offs = cpl_bivector_new(noffsets);
01097         
01098         /* Fill it */
01099         for (i=0; i<nframes; i++) {
01100             cur_frame = cpl_frameset_get_frame(set, i);
01101             plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
01102             off_x = isaac_pfits_get_cumoffsetx(plist);
01103             off_y = isaac_pfits_get_cumoffsety(plist);
01104             cpl_propertylist_delete(plist);
01105             if (i==0) {
01106                 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
01107                 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
01108             } else if (i==nframes-1) {
01109                 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
01110                 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
01111             } else {
01112                 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
01113                 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
01114                 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
01115                 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
01116             }
01117         }
01118     } else {
01119         cpl_msg_error(cpl_func, "Unsupported mode - abort");
01120         return NULL;
01121     }
01122 
01123     /* Check if it went ok */
01124     if (cpl_error_get_code()) {
01125         cpl_bivector_delete(offs);
01126         cpl_msg_error(cpl_func, "Cannot find offsets in headers");
01127         return NULL;
01128     }
01129     return offs;
01130 }
01131 
01132 /*----------------------------------------------------------------------------*/
01140 /*----------------------------------------------------------------------------*/
01141 static int isaac_img_zpoint_get_mag(
01142         const char  *       stdstars,
01143         double              ra,
01144         double              dec,
01145         isaac_band          band)
01146 {
01147     double              star_mag;
01148     char                star_name[512];
01149     char                cat_name[512];
01150     char                star_type[32];
01151     int                 ret;
01152 
01153     /* Initialise */
01154     ret = -1;
01155 
01156     switch (band) {
01157         /* SW mode */
01158         case ISAAC_BAND_J:
01159         case ISAAC_BAND_H:
01160         case ISAAC_BAND_K:
01161         case ISAAC_BAND_KS:
01162             cpl_msg_info(cpl_func, "Try in LCO-Palomar");
01163             strcpy(cat_name, "LCO-Palomar");
01164             if ((ret = irplib_stdstar_get_mag(stdstars, ra, dec,
01165                         isaac_std_band_name(band), "LCO-Palomar.txt", 
01166                         &star_mag, star_name, star_type, 2.0)) == -1) {
01167                 cpl_msg_info(cpl_func, "Try in LCO-Palomar-NICMOS-Red-Stars");
01168                 strcpy(cat_name, "LCO-Palomar-NICMOS-Red-Stars");
01169                 if ((ret = irplib_stdstar_get_mag(stdstars, ra, dec,
01170                             isaac_std_band_name(band),
01171                         "LCO-Palomar-NICMOS-Red-Stars.txt", &star_mag,
01172                         star_name, star_type, 2.0)) == -1) {
01173                     cpl_msg_info(cpl_func, "Try in all catalogs");
01174                     strcpy(cat_name, "all");
01175                     ret=irplib_stdstar_get_mag(stdstars, ra, dec,
01176                             isaac_std_band_name(band), "all", &star_mag,
01177                             star_name, star_type, 2.0);
01178                 }
01179             }
01180             break;
01181         /* LW mode  */
01182         case ISAAC_BAND_L:
01183         case ISAAC_BAND_M:
01184             cpl_msg_info(cpl_func, "Try in ESO-VanDerBliek");
01185             strcpy(cat_name, "ESO-VanDerBliek");
01186             if ((ret=irplib_stdstar_get_mag(stdstars, ra, dec,
01187                         isaac_std_band_name(band), "ESO-VanDerBliek.txt", 
01188                         &star_mag, star_name, star_type, 2.0)) == -1) {
01189                 cpl_msg_info(cpl_func, "Try in MSSSO-Photometric");
01190                 strcpy(cat_name, "MSSSO-Photometric");
01191                 if ((ret = irplib_stdstar_get_mag(stdstars, ra, dec,
01192                             isaac_std_band_name(band), "MSSSO-Photometric.txt",
01193                             &star_mag, star_name, star_type, 2.0)) == -1) {
01194                     cpl_msg_info(cpl_func, "Try in MSSSO-Spectroscopic");
01195                     strcpy(cat_name, "MSSSO-Spectroscopic");
01196                     if ((ret = irplib_stdstar_get_mag(stdstars, ra, dec,
01197                                 isaac_std_band_name(band),
01198                             "MSSSO-Spectroscopic.txt", &star_mag,
01199                             star_name, star_type, 2.0)) == -1) {
01200                         cpl_msg_info(cpl_func, "Try in all catalogs");
01201                         strcpy(cat_name, "all");
01202                         ret=irplib_stdstar_get_mag(stdstars, ra, dec,
01203                                 isaac_std_band_name(band), "all",
01204                                 &star_mag, star_name, star_type, 2.0);
01205                     }
01206                 }
01207             }
01208             break;
01209         default:
01210             cpl_msg_error(cpl_func, "cannot determine associated filter");
01211             return -1;
01212     }
01213 
01214     /* Special case: swap K and Ks if needed */
01215     if ((ret==-1) && (band==ISAAC_BAND_K)) {
01216         strcpy(cat_name, "all");
01217         ret = irplib_stdstar_get_mag(stdstars, ra, dec,
01218                 isaac_std_band_name(ISAAC_BAND_KS), "all", &star_mag,
01219                 star_name, star_type, 2.0);
01220     } else if ((ret==-1) && (band==ISAAC_BAND_KS)) {
01221         strcpy(cat_name, "all");
01222         ret = irplib_stdstar_get_mag(stdstars, ra, dec,
01223                 isaac_std_band_name(ISAAC_BAND_K), "all", &star_mag,
01224                 star_name, star_type, 2.0);
01225     }
01226 
01227     /* If the magnitude still is not know, abort */
01228     if (ret == -1) {
01229         cpl_msg_error(cpl_func, "star magnitude not found in database");
01230         return -1;
01231     }
01232 
01233     /* Store results */
01234     isaac_img_zpoint_config.magnitude = star_mag;
01235     strcpy(isaac_img_zpoint_config.starname, star_name);
01236     strcpy(isaac_img_zpoint_config.sptype, star_type);
01237     strcpy(isaac_img_zpoint_config.catalog, cat_name);
01238     return 0;
01239 }
01240 
01241 /*----------------------------------------------------------------------------*/
01251 /*----------------------------------------------------------------------------*/
01252 static int isaac_img_zpoint_save(
01253         cpl_table           *   tab,
01254         cpl_image           *   check_im,
01255         cpl_frameset        *   raw,
01256         cpl_parameterlist   *   parlist,
01257         cpl_frameset        *   set)
01258 {
01259     cpl_propertylist    *   plist;
01260     cpl_propertylist    *   paflist;
01261     cpl_propertylist    *   qclist;
01262     const cpl_frame           *   ref_frame;
01263     const char          *   sval;
01264     double                  hum, airm, extinction;
01265 
01266     /* Initialise */
01267     airm = 0;
01268 
01269     /* Get the QC params in qclist */
01270     qclist = cpl_propertylist_new();
01271 
01272     /* Get the reference frame */
01273     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
01274     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
01275                     0)) == NULL) {
01276         cpl_msg_error(cpl_func, "getting header from reference frame");
01277         cpl_propertylist_delete(qclist);
01278         return -1;
01279     }
01280     /* Test the status */
01281     if (cpl_error_get_code()) {
01282         cpl_propertylist_delete(qclist);
01283         cpl_propertylist_delete(plist);
01284         return -1;
01285     }
01286     sval = isaac_pfits_get_filter(plist);
01287     if (cpl_error_get_code()) cpl_error_reset();
01288     else cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
01289     cpl_propertylist_append_string(qclist, "ESO QC FILTER REF",
01290             isaac_std_band_name(isaac_img_zpoint_config.band));
01291     if (isaac_img_zpoint_compute_keywords(raw, &hum, &airm) != -1) {
01292         cpl_propertylist_append_double(qclist, "ESO QC AMBI RHUM AVG", hum);
01293         cpl_propertylist_append_double(qclist, "ESO QC AIRMASS", airm);
01294     }
01295     cpl_propertylist_delete(plist);
01296     /* Get extinction */
01297     switch (isaac_img_zpoint_config.band) {
01298         case ISAAC_BAND_J:    extinction = 0.09; break;
01299         case ISAAC_BAND_JS:   extinction = 0.05; break;
01300         case ISAAC_BAND_H:    extinction = 0.04; break;
01301         case ISAAC_BAND_KS:   extinction = 0.06; break;
01302         default:        extinction = 0.00; break;
01303     }
01304     cpl_propertylist_append_double(qclist, "ESO QC ZPOINT",
01305             isaac_img_zpoint_config.zpoint);
01306     cpl_propertylist_append_double(qclist, "ESO QC ZPOINT ATX0",
01307             isaac_img_zpoint_config.zpoint + airm * extinction);
01308     cpl_propertylist_append_double(qclist, "ESO QC ZPOINTRMS",
01309             isaac_img_zpoint_config.zpointrms);
01310     cpl_propertylist_append_double(qclist, "ESO QC FLUX MED",
01311             isaac_img_zpoint_config.flux_med);
01312     cpl_propertylist_append_string(qclist, "ESO QC STDNAME",
01313             isaac_img_zpoint_config.starname);
01314     cpl_propertylist_append_string(qclist, "ESO QC SPECTYPE",
01315             isaac_img_zpoint_config.sptype);
01316     cpl_propertylist_append_double(qclist, "ESO QC STARMAG",
01317             isaac_img_zpoint_config.magnitude);
01318     cpl_propertylist_append_string(qclist, "ESO QC CATNAME",
01319             isaac_img_zpoint_config.catalog);
01320     cpl_propertylist_append_double(qclist, "ESO QC GRADX",
01321             isaac_img_zpoint_config.gradx);
01322     cpl_propertylist_append_double(qclist, "ESO QC GRADY",
01323             isaac_img_zpoint_config.grady);
01324     cpl_propertylist_append_double(qclist, "ESO QC GRADDX",
01325             isaac_img_zpoint_config.graddx);
01326     cpl_propertylist_append_double(qclist, "ESO QC GRADDY",
01327             isaac_img_zpoint_config.graddy);
01328     cpl_propertylist_append_double(qclist, "ESO QC FWHM MEAN",
01329             isaac_img_zpoint_config.fwhm_mean);
01330 
01331     /* Write the zpoint table  */
01332     irplib_dfs_save_table(set,
01333             parlist,
01334             set,
01335             tab,
01336             NULL,
01337             "isaac_img_zpoint",
01338             ISAAC_IMG_ZPOINT_TAB,
01339             qclist,
01340             NULL,
01341             PACKAGE "/" PACKAGE_VERSION,
01342             "isaac_img_zpoint.fits");
01343     /* Write the check image   */
01344     if (check_im) {
01345         irplib_dfs_save_image(set,
01346                 parlist,
01347                 set,
01348                 check_im,
01349                 CPL_BPP_IEEE_FLOAT,
01350                 "isaac_img_zpoint",
01351                 ISAAC_IMG_ZPOINT_CHECK,
01352                 qclist,
01353                 NULL,
01354                 PACKAGE "/" PACKAGE_VERSION,
01355                 "isaac_img_zpoint_check.fits");
01356     }
01357     
01358     /* Get the reference frame */
01359     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
01360 
01361     /* Get FITS header from reference file */
01362     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
01363                     0)) == NULL) {
01364         cpl_msg_error(cpl_func, "getting header from reference frame");
01365         cpl_propertylist_delete(qclist);
01366         return -1;
01367     }
01368 
01369     /* Get the keywords for the paf file */
01370     paflist = cpl_propertylist_new();
01371     cpl_propertylist_copy_property_regexp(paflist, plist,
01372         "^(ARCFILE|ESO TPL ID|DATE-OBS|MJD-OBS|ESO INS MODE|"
01373         "ESO OBS ID|ESO DET DIT|ESO INS PIXSCALE|RA|DEC)$", 0);
01374     cpl_propertylist_delete(plist);
01375 
01376     /* Get the QC params in qclist and keys for paf in paflist */
01377     cpl_propertylist_copy_property_regexp(paflist, qclist, "ESO QC", 0);
01378     cpl_propertylist_delete(qclist);
01379 
01380     /* PRO.CATG */
01381     cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
01382                                    ISAAC_IMG_ZPOINT_TAB);
01383 
01384     /* Save the PAF file */
01385     cpl_dfs_save_paf("ISAAC",
01386             "isaac_img_zpoint",
01387             paflist,
01388             "isaac_img_zpoint.paf");
01389     cpl_propertylist_delete(paflist);
01390     return  0;
01391 }
01392 
01393 /*----------------------------------------------------------------------------*/
01399 /*----------------------------------------------------------------------------*/
01400 static int isaac_img_zpoint_compute_keywords(
01401         cpl_frameset    *   set,
01402         double          *   hum,
01403         double          *   airm)
01404 {
01405     int                     nframes;
01406     cpl_vector          *   hum_vec;
01407     cpl_frame           *   cur_frame;
01408     cpl_propertylist    *   plist;
01409     int                     i;
01410 
01411     /* Test inputs  */
01412     if (set == NULL) return -1;
01413 
01414     /* Initialize */
01415     nframes = cpl_frameset_get_size(set);
01416     *airm   = 0.0;
01417 
01418     hum_vec = cpl_vector_new(nframes);
01419 
01420     for (i=0; i<nframes; i++) {
01421         if (cpl_error_get_code()) {
01422             cpl_vector_delete(hum_vec);
01423             return -1;
01424         }
01425         cur_frame = cpl_frameset_get_frame(set, i);
01426         plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
01427         if (i==0) *airm += isaac_pfits_get_airmass_start(plist);
01428         if (i==nframes-1) *airm += isaac_pfits_get_airmass_end(plist);
01429         cpl_vector_set(hum_vec,  i, isaac_pfits_get_humidity_level(plist));
01430         cpl_propertylist_delete(plist);
01431         if (cpl_error_get_code()) {
01432             cpl_vector_delete(hum_vec);
01433             cpl_error_reset();
01434             return -1;
01435         }
01436     }
01437     *hum  = cpl_vector_get_mean(hum_vec);
01438     *airm /= 2;
01439 
01440     /* Free and return */
01441     cpl_vector_delete(hum_vec);
01442     if (cpl_error_get_code()) return -1;
01443     return 0;
01444 }
01445 
01446 

Generated on Wed Mar 9 15:43:10 2011 for ISAAC Pipeline Reference Manual by  doxygen 1.5.8