isaac_img_zpoint.c

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

Generated on Mon Feb 6 14:44:02 2012 for ISAAC Pipeline Reference Manual by  doxygen 1.5.8