naco_img_zpoint.c

00001 /* $Id: naco_img_zpoint.c,v 1.99 2011/12/22 11:09:36 llundin Exp $
00002  *
00003  * This file is part of the NACO 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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2011/12/22 11:09:36 $
00024  * $Revision: 1.99 $
00025  * $Name: naco-4_3_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 
00038 #include "naco_recipe.h"
00039 #include <irplib_strehl.h>
00040 #include <irplib_wcs.h>
00041 #include <irplib_stdstar.h>
00042 
00043 /*-----------------------------------------------------------------------------
00044                             Defines
00045  -----------------------------------------------------------------------------*/
00046 
00047 #define RECIPE_STRING "naco_img_zpoint"
00048 
00049 /* @cond */
00050 typedef enum _NACO_BAND_ {
00051     BAND_J,
00052     BAND_JS,
00053     BAND_JBLOCK,
00054     BAND_H,
00055     BAND_K,
00056     BAND_KS,
00057     BAND_L,
00058     BAND_M,
00059     BAND_LP,
00060     BAND_MP,
00061     BAND_Z,
00062     BAND_SZ,
00063     BAND_SH,
00064     BAND_SK,
00065     BAND_SL,
00066     BAND_UNKNOWN
00067 } naco_band;
00068 /* @endcond */
00069 
00070 /*-----------------------------------------------------------------------------
00071                             Private Functions prototypes
00072  -----------------------------------------------------------------------------*/
00073 
00074 static cpl_table * naco_img_zpoint_reduce(cpl_propertylist *,
00075                                           const irplib_framelist *,
00076                                           const char *, const char *,
00077                                           cpl_image **);
00078 
00079 static cpl_error_code naco_img_zpoint_qc(cpl_propertylist *,
00080                                          cpl_propertylist *,
00081                                          const irplib_framelist *);
00082 
00083 static cpl_error_code naco_img_zpoint_save(cpl_frameset *,
00084                                            const cpl_parameterlist *,
00085                                            const cpl_propertylist *,
00086                                            const cpl_propertylist *,
00087                                            const cpl_table *,
00088                                            const cpl_image *);
00089 
00090 static cpl_error_code naco_img_zpoint_reduce_one(cpl_table *, cpl_vector *, int,
00091                                                  const cpl_image *,
00092                                                  const char *, double, double,
00093                                                  double, double *, double *,
00094                                                  double *, double *, double *);
00095 
00096 static double naco_img_zpoint_find_mag(cpl_propertylist *, double, double,
00097                                        const char *, const char *);
00098 
00099 static cpl_table * naco_img_zpoint_load_std_star(const char *, double, double, double);
00100 
00101 static cpl_error_code naco_img_zpoint_find_std_star(cpl_table *,
00102                                                     const char *[],
00103                                                     double, double,
00104                                                     const char *,
00105                                                     cpl_boolean,
00106                                                     int *);
00107 
00108 static cpl_error_code naco_img_zpoint_qc_all(cpl_propertylist *,
00109                                              cpl_propertylist *,
00110                                              const irplib_framelist *);
00111 
00112 static cpl_error_code naco_img_zpoint_check_im(cpl_image **, const cpl_image *,
00113                                                int, int, double, double,
00114                                                double, double, double);
00115 
00116 static naco_band naco_get_bbfilter(const char *);
00117 
00118 static const char * naco_std_band_name(naco_band);
00119 
00120 
00121 NACO_RECIPE_DEFINE(naco_img_zpoint,
00122                    NACO_PARAM_STAR_R  |                        
00123                    NACO_PARAM_BG_RINT |                        
00124                    NACO_PARAM_BG_REXT |                        
00125                    NACO_PARAM_RA      |                        
00126                    NACO_PARAM_DEC     |                        
00127                    NACO_PARAM_PIXSCALE|                        
00128                    NACO_PARAM_MAGNITD |                        
00129                    NACO_PARAM_SX      |                        
00130                    NACO_PARAM_SY      |                        
00131                    NACO_PARAM_CHK_IMG,
00132                    "Zero point computation recipe",
00133                    RECIPE_STRING " -- Zero point recipe\n"                     
00134                    "The files listed in the Set Of Frames (sof-file) " 
00135                    "must be tagged:\n"
00136                    "NACO-raw-file.fits " NACO_IMG_ZPOINT_CHOP " or\n"           
00137                    "NACO-raw-file.fits " NACO_IMG_ZPOINT_JITTER ".\n"           
00138                    "NACO-Imaging-Standard-Star-Catalog.fits "
00139                     NACO_IMG_STD_CAT "\n"
00140                    "Optionally, a flat field frame may be inluded:\n"       
00141                    "NACO-flat-file.fits " NACO_CALIB_FLAT "\n");
00142 
00143 /*-----------------------------------------------------------------------------
00144   Static variables
00145  -----------------------------------------------------------------------------*/
00146 
00147 static struct {
00148     /* Inputs */
00149     double      ra;
00150     double      dec;
00151     double      magnitude;
00152     int         sx;
00153     int         sy;
00154     double      phot_star_radius;
00155     double      phot_bg_r1;
00156     double      phot_bg_r2;
00157     double      pscale;
00158     int         check_im;
00159 
00160 } naco_img_zpoint_config;
00161 
00162 
00163 /*----------------------------------------------------------------------------*/
00167 /*----------------------------------------------------------------------------*/
00168 
00169 /*-----------------------------------------------------------------------------
00170                                 Functions code
00171  -----------------------------------------------------------------------------*/
00172 
00173 /*----------------------------------------------------------------------------*/
00181 /*----------------------------------------------------------------------------*/
00182 static int naco_img_zpoint(cpl_frameset            * framelist,
00183                            const cpl_parameterlist * parlist)
00184 {
00185     irplib_framelist* allframes = NULL;
00186     irplib_framelist* rawframes = NULL;
00187     cpl_propertylist* qclist    = cpl_propertylist_new();
00188     cpl_propertylist* paflist   = cpl_propertylist_new();
00189     const char      * flat;
00190     const char      * star_cat;
00191     cpl_table       * tab = NULL;
00192     cpl_image       * check_im = NULL;
00193     
00194     /* Retrieve input parameters */
00195     /* --ra */
00196     naco_img_zpoint_config.ra
00197         = naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_RA);
00198     /* --dec */
00199     naco_img_zpoint_config.dec
00200         = naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_DEC);
00201     /* --pscale */
00202     naco_img_zpoint_config.pscale
00203         = naco_parameterlist_get_double(parlist,RECIPE_STRING,NACO_PARAM_PIXSCALE);
00204     /* --mag */
00205     naco_img_zpoint_config.magnitude
00206         = naco_parameterlist_get_double(parlist, RECIPE_STRING,NACO_PARAM_MAGNITD);
00207     /* --sx */
00208     naco_img_zpoint_config.sx
00209         = naco_parameterlist_get_int(parlist, RECIPE_STRING, NACO_PARAM_SX);
00210 
00211     skip_if_lt(naco_img_zpoint_config.sx, 1,
00212                "for the size of the search window in X-direction [pixel]");
00213 
00214     /* --sy */
00215     naco_img_zpoint_config.sy
00216         = naco_parameterlist_get_int(parlist, RECIPE_STRING, NACO_PARAM_SY);
00217 
00218     skip_if_lt(naco_img_zpoint_config.sy, 1,
00219                "for the size of the search window in Y-direction [pixel]");
00220 
00221     /* --star_r */
00222     naco_img_zpoint_config.phot_star_radius
00223         = naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_STAR_R);
00224 
00225     /* --bg_r1 */
00226     naco_img_zpoint_config.phot_bg_r1
00227         = naco_parameterlist_get_double(parlist, RECIPE_STRING,NACO_PARAM_BG_RINT);
00228 
00229     /* --bg_r2 */
00230     naco_img_zpoint_config.phot_bg_r2
00231         = naco_parameterlist_get_double(parlist, RECIPE_STRING,NACO_PARAM_BG_REXT);
00232 
00233     /* --check_im */
00234     naco_img_zpoint_config.check_im
00235         = naco_parameterlist_get_bool(parlist, RECIPE_STRING, NACO_PARAM_CHK_IMG);
00236 
00237     /* Identify the RAW and CALIB frames in the input frameset */
00238     skip_if (naco_dfs_set_groups(framelist));
00239 
00240     allframes = irplib_framelist_cast(framelist);
00241     skip_if(allframes == NULL);
00242 
00243     rawframes = irplib_framelist_extract_regexp(allframes,
00244                                                 "^(" NACO_IMG_ZPOINT_JITTER
00245                                                 "|"  NACO_IMG_ZPOINT_CHOP ")$",
00246                                                 CPL_FALSE);
00247     skip_if(rawframes == NULL);
00248     irplib_framelist_empty(allframes);
00249 
00250     /* Standard star catalog */
00251     star_cat = irplib_frameset_find_file(framelist, NACO_IMG_STD_CAT);
00252     error_if (star_cat == NULL, CPL_ERROR_DATA_NOT_FOUND, "The input "
00253               "file(s) have no star catalog tagged %s", NACO_IMG_STD_CAT);
00254 
00255     /* Retrieve calibration data */
00256     flat = irplib_frameset_find_file(framelist, NACO_CALIB_FLAT);
00257     bug_if(0);
00258    
00259     skip_if( irplib_framelist_load_propertylist(rawframes, 0, 0, "^("
00260                                                 IRPLIB_PFITS_REGEXP_RECAL "|"
00261                                                 NACO_PFITS_REGEXP_ZPOINT "|"
00262                                                 NACO_PFITS_REGEXP_ZPOINT_PAF "|"
00263                                                 NACO_PFITS_REGEXP_ZPOINT_REF
00264                                                 ")$", CPL_FALSE));
00265 
00266     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
00267                                                    NACO_PFITS_REGEXP_ZPOINT
00268                                                    ")$", CPL_FALSE));
00269 
00270     /* Compute the strehl/zpoint values */
00271     tab = naco_img_zpoint_reduce(qclist, rawframes, star_cat, flat, &check_im);
00272     skip_if (tab == NULL);
00273 
00274     skip_if(naco_img_zpoint_qc(qclist, paflist, rawframes));
00275 
00276     /* Reduce maximum number of pointers used */
00277     irplib_framelist_empty(rawframes);
00278 
00279     /* PRO.CATG */
00280     bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00281                                            NACO_IMG_ZPOINT_RES));
00282 
00283     /* Save the products */
00284     skip_if (naco_img_zpoint_save(framelist, parlist, qclist, paflist, tab,
00285                                   check_im));
00286 
00287     end_skip;
00288 
00289     cpl_table_delete(tab);
00290     cpl_image_delete(check_im);
00291     irplib_framelist_delete(allframes);
00292     irplib_framelist_delete(rawframes);
00293     cpl_propertylist_delete(qclist);
00294     cpl_propertylist_delete(paflist);
00295 
00296     return cpl_error_get_code();
00297 }
00298 
00299 /*----------------------------------------------------------------------------*/
00310 /*----------------------------------------------------------------------------*/
00311 static cpl_table * naco_img_zpoint_reduce(cpl_propertylist       * qclist,
00312                                           const irplib_framelist * rawframes,
00313                                           const char             * star_cat,
00314                                           const char             * flat,
00315                                           cpl_image             ** pcheck_im)
00316 {
00317     const cpl_propertylist * plist
00318         = irplib_framelist_get_propertylist_const(rawframes, 0);
00319     cpl_image              * flat_im = NULL;
00320     cpl_image              * rawimage = NULL;
00321     cpl_image              * imdiff = NULL;
00322     const char             * filter;
00323     const int                ndiff = 2 * irplib_framelist_get_size(rawframes)-2;
00324     double                   pos_x_cen = DBL_MAX; /* Avoid uninit warning */
00325     double                   pos_y_cen = DBL_MAX; /* Avoid uninit warning */
00326     cpl_apertures          * aperts = NULL;
00327     double                   psigmas[] = {20.0, 10.0, 8.0, 6.0, 5.0};
00328     const int                nsigmas = (int)(sizeof(psigmas)/sizeof(double));
00329     cpl_size                 isigma;
00330     cpl_vector             * sigmas = NULL;
00331     cpl_table              * out_tab = NULL;
00332     cpl_vector             * zpoints = NULL;
00333     double                   dit;
00334     double                   off_x0 = DBL_MAX; /* Avoid uninit warning */
00335     double                   off_y0 = DBL_MAX; /* Avoid uninit warning */
00336     double                   off_x  = DBL_MAX; /* Avoid uninit warning */
00337     double                   off_y  = DBL_MAX; /* Avoid uninit warning */
00338     double                   str_sum = 0.0;
00339     double                   str_err_sum = 0.0;
00340     double                   pe_sum = 0.0;
00341     double                   fl_sum = 0.0;
00342     double                   bg_no_sum = 0.0;
00343     double                   sqsum = 0.0;
00344     double                   avg_zp = 0.0;
00345     int                      nok_zp = 0;
00346     int                      nok_strehl = 0;
00347     int                      i;
00348 
00349     /* QC Parameters */
00350     double      zpointrms;
00351     double      strehl;
00352     double      strehl_err;
00353     double      strehl_rms;
00354     double      star_peak;
00355     double      star_flux;
00356 
00357 
00358 
00359     bug_if (0);
00360     bug_if (pcheck_im == NULL);
00361     bug_if (*pcheck_im != NULL);
00362 
00363     skip_if_lt(ndiff/2+1, 3, "raw frames");
00364 
00365     /* Get the filter name, DIT, RA, DEC and pixelscale */
00366 
00367     dit = naco_pfits_get_dit(plist);
00368     skip_if (dit <= 0.0);
00369     if (naco_img_zpoint_config.ra > 998.0)
00370         naco_img_zpoint_config.ra = naco_pfits_get_ra(plist);
00371     if (naco_img_zpoint_config.dec > 998.0) 
00372         naco_img_zpoint_config.dec = naco_pfits_get_dec(plist);
00373     skip_if(0);
00374 
00375     if (naco_img_zpoint_config.pscale <= 0.0) {
00376         naco_img_zpoint_config.pscale = naco_pfits_get_pixscale(plist);
00377         skip_if(naco_img_zpoint_config.pscale <= 0.0);
00378     }
00379 
00380     cpl_msg_info(cpl_func, "Using star at position: RA = %g ; DEC = %g", 
00381             naco_img_zpoint_config.ra, naco_img_zpoint_config.dec);
00382    
00383     filter = naco_pfits_get_filter(plist);
00384     skip_if(0);
00385 
00386     /* Get the standard star information from database */
00387     if (naco_img_zpoint_config.magnitude > IRPLIB_STDSTAR_LIMIT) {
00388         naco_img_zpoint_config.magnitude
00389             = naco_img_zpoint_find_mag(qclist,
00390                                        naco_img_zpoint_config.ra,
00391                                        naco_img_zpoint_config.dec,
00392                                        star_cat, filter);
00393         if (cpl_error_get_code()) {
00394             const cpl_propertylist * reflist
00395                 = irplib_framelist_get_propertylist_const(rawframes, 0);
00396             const char * starname = naco_pfits_get_object(reflist);
00397 
00398             if (starname != NULL) {
00399                 error_if (0, cpl_error_get_code(), "Star '%s' has "
00400                           "no magnitude for filter '%s' in catalogue '%s'",
00401                           starname, filter, star_cat);
00402             }
00403         }
00404     }
00405     cpl_msg_info(cpl_func, "Star magnitude with filter %s : %g", filter,
00406                  naco_img_zpoint_config.magnitude);
00407     bug_if(cpl_propertylist_append_double(qclist, "ESO QC STARMAG",
00408                                           naco_img_zpoint_config.magnitude));
00409     
00410     zpoints = cpl_vector_new(ndiff);
00411     out_tab = cpl_table_new(ndiff);
00412     cpl_table_new_column(out_tab, "POSX",       CPL_TYPE_DOUBLE);
00413     cpl_table_new_column(out_tab, "POSY",       CPL_TYPE_DOUBLE);
00414     cpl_table_new_column(out_tab, "ZPOINT",     CPL_TYPE_DOUBLE);
00415     cpl_table_new_column(out_tab, "PEAK",       CPL_TYPE_DOUBLE);
00416     cpl_table_new_column(out_tab, "FLUX",       CPL_TYPE_DOUBLE);
00417     cpl_table_new_column(out_tab, "BGD_NOISE",  CPL_TYPE_DOUBLE);
00418     cpl_table_new_column(out_tab, "STREHL",     CPL_TYPE_DOUBLE);
00419     cpl_table_new_column(out_tab, "STREHL_ERR", CPL_TYPE_DOUBLE);
00420     cpl_table_new_column(out_tab, "BGD",        CPL_TYPE_DOUBLE);
00421     cpl_table_new_column(out_tab, "FWHMX",      CPL_TYPE_DOUBLE);
00422     cpl_table_new_column(out_tab, "FWHMY",      CPL_TYPE_DOUBLE);
00423     bug_if(0);
00424 
00425     /* Load the the flatfield if one is provided */
00426     if (flat != NULL) {
00427         cpl_msg_info(cpl_func, "Loading the flat field from %s", flat);
00428         flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0);
00429 
00430         if (flat_im == NULL) {
00431             cpl_msg_error(cpl_func, "Could not load flat field");
00432             skip_if(1);
00433         }
00434     }
00435 
00436     cpl_msg_info(cpl_func, "Loading the image from raw frame %d",1);
00437     rawimage = cpl_image_load(cpl_frame_get_filename(
00438                      irplib_framelist_get_const(rawframes, 0)),
00439                               CPL_TYPE_FLOAT, 0, 0);
00440     if (rawimage == NULL) {
00441         cpl_msg_error(cpl_func, "Could not load image from first raw frame"); 
00442         skip_if(1);
00443     }
00444 
00445     for (i=0; i < ndiff; i++) {
00446 
00447         double pos_x, pos_y;
00448         double str, str_err, pe, fl, bg_no;
00449 
00450 
00451         bug_if(0);
00452 
00453         if (i == 0 || i % 2 == 1) {
00454             /* Get the offsets */
00455 
00456             off_x = naco_pfits_get_cumoffsetx(plist);
00457             off_y = naco_pfits_get_cumoffsety(plist);
00458 
00459             skip_if(0);
00460 
00461             if (i == 0) {
00462                 off_x0 = off_x;
00463                 off_y0 = off_y;
00464             }
00465 
00466             off_x -= off_x0;
00467             off_y -= off_y0;
00468 
00469             cpl_msg_info(cpl_func, "Offsets for difference image %d: (%g,%g)",
00470                          i, off_x, off_y);
00471 
00472             /* Update the plist to that of the next frame */
00473             if (i + 1 < ndiff)
00474                 plist = irplib_framelist_get_propertylist_const(rawframes,
00475                                                                 1 + (i+1)/2);
00476         }
00477 
00478         if (i % 2 == 0) {
00479             const char * filename = cpl_frame_get_filename(
00480                                   irplib_framelist_get_const(rawframes, 1+i/2));
00481 
00482             bug_if(0);
00483 
00484             cpl_image_delete(imdiff);
00485             imdiff = rawimage;
00486 
00487             cpl_msg_info(cpl_func, "Loading the image from raw frame %d",2+i/2);
00488             rawimage = cpl_image_load(filename, CPL_TYPE_FLOAT, 0, 0);
00489             if (rawimage == NULL) {
00490                 cpl_msg_error(cpl_func, "Could not load image %d", 2+i/2); 
00491                 skip_if(1);
00492             }
00493             bug_if(cpl_image_subtract(imdiff, rawimage));
00494             if (flat_im != NULL) skip_if(cpl_image_divide(imdiff, flat_im));
00495         } else {
00496             bug_if(cpl_image_multiply_scalar(imdiff, -1.0));
00497         }
00498 
00499         if (i == 0) {
00500             double    min_dist = DBL_MAX; /* Avoid (false) uninit warning */
00501             const int size_x = cpl_image_get_size_x(imdiff);
00502             const int size_y = cpl_image_get_size_y(imdiff);
00503             int       iap;
00504 
00505             /* Detect the central object in the first frame */
00506             cpl_msg_info(cpl_func, "Detecting a bright object in the first "
00507                          "difference image using %d sigma-levels ranging from "
00508                          "%g down to %g", nsigmas, psigmas[0],
00509                          psigmas[nsigmas-1]);
00510             sigmas = cpl_vector_wrap(nsigmas, psigmas);
00511            
00512             aperts = cpl_apertures_extract(imdiff, sigmas, &isigma);
00513             if (aperts == NULL || cpl_apertures_get_size(aperts) < 1) {
00514                 cpl_msg_error(cpl_func, "Could not detect any bright object");
00515                 skip_if(1);
00516             }
00517             for (iap=0; iap < cpl_apertures_get_size(aperts); iap++) {
00518                 const double d_x
00519                     = cpl_apertures_get_centroid_x(aperts, iap+1) - size_x/2.0;
00520                 const double d_y
00521                     = cpl_apertures_get_centroid_y(aperts, iap+1) - size_y/2.0;
00522                 const double dist = d_x * d_x + d_y * d_y;
00523                 if (iap == 0 || dist < min_dist) {
00524                     min_dist = dist;
00525                     pos_x_cen = d_x;
00526                     pos_y_cen = d_y;
00527                 }
00528             }
00529             cpl_apertures_delete(aperts);
00530             aperts = NULL;
00531             bug_if(0);
00532             pos_x_cen += size_x/2.0;
00533             pos_y_cen += size_y/2.0;
00534             cpl_msg_info(cpl_func, "Detected a bright object at sigma=%g, at "
00535                          "position: %g %g", psigmas[isigma], pos_x_cen,
00536                          pos_y_cen);
00537 
00538         }
00539 
00540         pos_x = pos_x_cen + off_x;
00541         pos_y = pos_y_cen + off_y;
00542 
00543         if (i > 0) {
00544             /* Refine the positions */
00545             const int llx = (int)pos_x - naco_img_zpoint_config.sx;
00546             const int urx = (int)pos_x + naco_img_zpoint_config.sx;
00547             const int lly = (int)pos_y - naco_img_zpoint_config.sy;
00548             const int ury = (int)pos_y + naco_img_zpoint_config.sy;
00549             pos_x = cpl_image_get_centroid_x_window(imdiff, llx, lly, urx, ury);
00550             pos_y = cpl_image_get_centroid_y_window(imdiff, llx, lly, urx, ury);
00551             error_if (0, cpl_error_get_code(), "Could not refine the positions "
00552                       "of difference image %d", i+1);
00553         }
00554 
00555         bug_if(0);
00556 
00557         /* Create the check image if requested */
00558         if (naco_img_zpoint_config.check_im) {
00559             const double r1 = naco_img_zpoint_config.phot_star_radius
00560                 /naco_img_zpoint_config.pscale;
00561             const double r2 = naco_img_zpoint_config.phot_bg_r1
00562                 /naco_img_zpoint_config.pscale;
00563             const double r3 = naco_img_zpoint_config.phot_bg_r2
00564                 /naco_img_zpoint_config.pscale;
00565 
00566             skip_if(naco_img_zpoint_check_im(pcheck_im, imdiff, i, ndiff,
00567                                              pos_x, pos_y, r1, r2, r3));
00568         }
00569 
00570         /* Compute the flux/strehl in the current image */
00571         skip_if (naco_img_zpoint_reduce_one(out_tab, zpoints, i, imdiff, filter,
00572                                             pos_x, pos_y, dit,
00573                                             &str, &str_err, &pe, &fl, &bg_no));
00574 
00575         /* Compute the averages of the results for the strehl */
00576         if (str <= 0.0 || str >= 1.0) continue;
00577         nok_strehl++;
00578 
00579         str_sum     += str;
00580         str_err_sum += str_err;
00581         bg_no_sum   += bg_no;
00582         pe_sum      += pe;
00583         fl_sum      += fl;
00584     }
00585 
00586     skip_if_lt(nok_strehl, 1, "valid strehl measurements");
00587 
00588     strehl     = str_sum     / (double)nok_strehl;
00589     strehl_err = str_err_sum / (double)nok_strehl;
00590     strehl_rms = bg_no_sum   / (double)nok_strehl;
00591     star_peak  = pe_sum      / (double)nok_strehl;
00592     star_flux  = fl_sum      / (double)nok_strehl;
00593     
00594     /* Compute the averages of the results for the zero point */
00595     cpl_vector_sort(zpoints, 1);
00596     /* Reject highest and lowest value */
00597     for (i=1; i < ndiff-1; i++) {
00598         const double zp = cpl_vector_get(zpoints, i);
00599         if (zp > 0.0) {
00600             avg_zp += zp;
00601             sqsum += zp * zp;
00602             nok_zp ++;
00603         }
00604     }
00605     cpl_vector_delete(zpoints);
00606     zpoints = NULL;
00607 
00608     skip_if_lt(nok_zp, 1, "valid zpoint measurements");
00609 
00610     avg_zp /= (double)nok_zp;
00611     sqsum  /= (double)nok_zp;
00612     zpointrms = sqsum - avg_zp * avg_zp;
00613     zpointrms = zpointrms > 0.0 ? sqrt(zpointrms) : 0.0;
00614 
00615     /* Print final results */
00616     cpl_msg_info(cpl_func, "***** FINAL RESULTS *****");
00617     cpl_msg_info(cpl_func, "Strehl :       %g", strehl);
00618     cpl_msg_info(cpl_func, "Strehl error : %g", strehl_err);
00619     cpl_msg_info(cpl_func, "Peak :         %g", star_peak);
00620     cpl_msg_info(cpl_func, "Flux :         %g", star_flux);
00621     cpl_msg_info(cpl_func, "Strehl RMS     %g", strehl_rms);
00622     cpl_msg_info(cpl_func, "Zero point :   %g", avg_zp);
00623     cpl_msg_info(cpl_func, "Zero p. RMS:   %g", zpointrms);
00624 
00625     cpl_propertylist_append_double(qclist, "ESO QC ZPOINT", avg_zp);
00626     cpl_propertylist_append_double(qclist, "ESO QC ZPOINTRMS", zpointrms);
00627     cpl_propertylist_append_double(qclist, "ESO QC STREHL", strehl);
00628     cpl_propertylist_append_double(qclist, "ESO QC STREHL ERROR", strehl_err);
00629     cpl_propertylist_append_double(qclist, "ESO QC STREHL RMS", strehl_rms);
00630     cpl_propertylist_append_double(qclist, "ESO QC STREHL PEAK", star_peak);
00631     cpl_propertylist_append_double(qclist, "ESO QC STREHL FLUX", star_flux);
00632     cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", filter);
00633 
00634     bug_if(0);
00635 
00636     end_skip;
00637     
00638     cpl_image_delete(rawimage);
00639     cpl_image_delete(imdiff);
00640     cpl_image_delete(flat_im);
00641     cpl_apertures_delete(aperts);
00642     cpl_vector_unwrap(sigmas);
00643     cpl_vector_delete(zpoints);
00644 
00645     if (cpl_error_get_code()) {
00646         cpl_image_delete(*pcheck_im);
00647         *pcheck_im = NULL;
00648         cpl_table_delete(out_tab);
00649         out_tab = NULL;
00650     }
00651 
00652     return out_tab;
00653 }
00654 
00655 /*----------------------------------------------------------------------------*/
00675 /*----------------------------------------------------------------------------*/
00676 static cpl_error_code naco_img_zpoint_check_im(cpl_image          ** self,
00677                                                const cpl_image     * imdiff,
00678                                                int                   idiff,
00679                                                int                   nima,
00680                                                double                pos_x,
00681                                                double                pos_y,
00682                                                double                r1,
00683                                                double                r2,
00684                                                double                r3)
00685 {
00686     const float * pin_ima = cpl_image_get_data_float_const(imdiff);
00687     float       * pout_ima;
00688     const int     in_nx = cpl_image_get_size_x(imdiff);
00689     const int     in_ny = cpl_image_get_size_y(imdiff);
00690     const int     box_sz = 2 * (int)r3 + 1;
00691     const int     nx = box_sz * nima;
00692     const int     ny = box_sz;
00693     /* Get the sub image position */
00694     const int     llx = (int)(pos_x - r3);
00695     const int     lly = (int)(pos_y - r3);
00696     int           i, j;
00697 
00698 
00699     bug_if(0);
00700 
00701     bug_if(self == NULL);
00702 
00703     skip_if (r1 > r2);
00704     skip_if (r2 >= r3);
00705 
00706     if (idiff == 0) {
00707         bug_if(*self != NULL);
00708 
00709         cpl_msg_info(cpl_func, "Creating %d X %d X %d check-image",
00710                      nima, box_sz, box_sz);
00711 
00712         /* Create the output image */
00713         *self = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
00714     }
00715 
00716     pout_ima = cpl_image_get_data_float(*self);
00717 
00718     bug_if(0);
00719 
00720     for (j=0; j < box_sz; j++) {
00721         if (j + lly < 0 || j + lly >= in_ny) continue;
00722         for (i=0; i < box_sz; i++) {
00723             if (i + llx >= 0 && i + llx < in_nx) {
00724                 const int in_pos = llx + i + (lly+j) * in_nx;
00725                 const int out_pos = (box_sz * idiff) + i + j * nx;
00726 
00727                 bug_if ( in_pos < 0 ||  in_pos >= in_nx*in_ny);
00728                 bug_if (out_pos < 0 || out_pos >=    nx*   ny);
00729 
00730                 pout_ima[out_pos] = pin_ima[in_pos];
00731             }
00732         }
00733     }
00734 
00735     if (idiff == 0) {
00736         /* Draw the two circles */
00737         const double sqr1 = sqrt(r1*r1);
00738         const double sqr2 = sqrt(r2*r2);
00739         const double sqr3 = sqrt(r3*r3);
00740         for (j=0; j < box_sz; j++) {
00741             const int jdist = j - box_sz/2;
00742             for (i=0; i < box_sz; i++) {
00743                 const int idist = i - box_sz/2;
00744                 const double dist
00745                     = sqrt((double)(idist * idist + jdist * jdist));
00746                 if (fabs(dist-sqr1) < 0.5 ||
00747                     fabs(dist-sqr2) < 0.5 ||
00748                     fabs(dist-sqr3) < 0.5) pout_ima[i + j * nx] = 10000;
00749             }
00750         }
00751     }
00752 
00753     end_skip;
00754 
00755     return cpl_error_get_code();
00756 }
00757 
00758 /*----------------------------------------------------------------------------*/
00778 /*----------------------------------------------------------------------------*/
00779 static cpl_error_code naco_img_zpoint_reduce_one(cpl_table       * zptab,
00780                                                  cpl_vector      * zpoints,
00781                                                  int               idiff,
00782                                                  const cpl_image * ima,
00783                                                  const char      * filter,
00784                                                  double            pos_x,
00785                                                  double            pos_y,
00786                                                  double            dit,
00787                                                  double          * pstrehl,
00788                                                  double          * pstrehl_err,
00789                                                  double          * pstar_peak,
00790                                                  double          * pstar_flux,
00791                                                  double          * pbg_noise)
00792 {
00793     cpl_errorstate cleanstate = cpl_errorstate_get();
00794     double zeropoint, background;
00795     double r, r1, r2;
00796     double lam, dlam;
00797     double star_bg, psf_peak, psf_flux;
00798     double fwhm_x, fwhm_y;
00799 
00800 
00801     bug_if(pstar_flux == NULL);
00802     bug_if(filter     == NULL);
00803 
00804     /* Compute the photometry */
00805     r = naco_img_zpoint_config.phot_star_radius/naco_img_zpoint_config.pscale;
00806     r1 = naco_img_zpoint_config.phot_bg_r1/naco_img_zpoint_config.pscale;
00807     r2 = naco_img_zpoint_config.phot_bg_r2/naco_img_zpoint_config.pscale;
00808     background = irplib_strehl_ring_background(ima, pos_x, pos_y, r1, r2,
00809             IRPLIB_BG_METHOD_AVER_REJ);
00810     skip_if(0);
00811     *pstar_flux = irplib_strehl_disk_flux(ima, pos_x, pos_y, r, background);
00812     skip_if(0);
00813 
00814     /* Get lam and dlam from the filter name for the Strehl computation */
00815     irplib_check(naco_get_filter_infos(filter, &lam, &dlam),
00816                  "Cannot get filter infos [%s]", filter);
00817     
00818     /* Compute the strehl */
00819     irplib_check(irplib_strehl_compute(ima, STREHL_M1, STREHL_M2, lam, dlam, 
00820                 naco_img_zpoint_config.pscale, STREHL_BOX_SIZE,
00821                 pos_x, pos_y, 
00822                 STREHL_STAR_RADIUS,
00823                 STREHL_BACKGROUND_R1,
00824                 STREHL_BACKGROUND_R2,
00825                 -1, -1,
00826                 pstrehl, pstrehl_err, &star_bg, pstar_peak, pstar_flux,
00827                                        &psf_peak, &psf_flux, pbg_noise),
00828                  "Could not compute the strehl");
00829 
00830     /* FWHM_X / FWHM_Y */
00831     if (cpl_image_get_fwhm(ima, (int)pos_x, (int)pos_y, &fwhm_x, &fwhm_y)) {
00832         irplib_error_recover(cleanstate, "Could not compute FWHM for image %d:",
00833                           idiff+1);
00834         fwhm_x = fwhm_y = 0.0;
00835     } else {
00836         if (fwhm_x <= 0.0) {
00837             cpl_msg_warning(cpl_func, "Could not compute FWHM in x for image "
00838                             "%d:", idiff+1);
00839             fwhm_x = 0.0;
00840         }
00841         if (fwhm_y <= 0.0) {
00842             cpl_msg_warning(cpl_func, "Could not compute FWHM in y for image "
00843                             "%d:", idiff+1);
00844             fwhm_y = 0.0;
00845         }
00846     }
00847 
00848     zeropoint = naco_img_zpoint_config.magnitude + 2.5 * log10(*pstar_flux/dit);
00849 
00850     cpl_msg_info(cpl_func, "Strehl :       %g", *pstrehl);
00851     cpl_msg_info(cpl_func, "Strehl error : %g", *pstrehl_err);
00852     cpl_msg_info(cpl_func, "Peak :         %g", *pstar_peak);
00853     cpl_msg_info(cpl_func, "Flux :         %g", *pstar_flux);
00854     cpl_msg_info(cpl_func, "Background :   %g", background);
00855     cpl_msg_info(cpl_func, "Bg noise :     %g", *pbg_noise);
00856     cpl_msg_info(cpl_func, "Zero point :   %g", zeropoint);
00857     cpl_msg_info(cpl_func, "FWHM in x :    %g", fwhm_x);
00858     cpl_msg_info(cpl_func, "FWHM in y :    %g", fwhm_y);
00859 
00860     /* Save the results in the vectors */
00861     cpl_vector_set(zpoints, idiff, zeropoint);
00862     cpl_table_set_double(zptab, "POSX",       idiff, pos_x);
00863     cpl_table_set_double(zptab, "POSY",       idiff, pos_y);
00864     cpl_table_set_double(zptab, "ZPOINT",     idiff, zeropoint);
00865     cpl_table_set_double(zptab, "PEAK",       idiff, *pstar_peak);
00866     cpl_table_set_double(zptab, "FLUX",       idiff, *pstar_flux);
00867     cpl_table_set_double(zptab, "BGD_NOISE",  idiff, *pbg_noise);
00868     cpl_table_set_double(zptab, "STREHL",     idiff, *pstrehl);
00869     cpl_table_set_double(zptab, "STREHL_ERR", idiff, *pstrehl_err);
00870     cpl_table_set_double(zptab, "BGD",        idiff, background);
00871     cpl_table_set_double(zptab, "FWHMX",      idiff, fwhm_x);
00872     cpl_table_set_double(zptab, "FWHMY",      idiff, fwhm_y);
00873 
00874     bug_if(0);
00875 
00876     end_skip;
00877 
00878     if (cpl_error_get_code())
00879         cpl_msg_error(cpl_func, "Cannot reduce the image %d", idiff+1);
00880 
00881     return cpl_error_get_code();
00882 }
00883 
00884 /*----------------------------------------------------------------------------*/
00895 /*----------------------------------------------------------------------------*/
00896 static double naco_img_zpoint_find_mag(cpl_propertylist * qclist,
00897                                        double             ra,
00898                                        double             dec,
00899                                        const char       * star_cat,
00900                                        const char       * filter)
00901 {
00902     cpl_error_code       error;
00903     const char         * star_name;
00904     const char         * star_type;
00905     const char         * cat_name;
00906     int                  istar;
00907     double               star_mag = 0.0;
00908     naco_band            band;
00909     const char         * bandname = NULL;
00910     cpl_table          * stdstars = NULL;
00911     const char * sw_cat[] = {"LCO-Palomar", "LCO-Palomar-NICMOS-Red-Stars", 
00912                              "ESO-VanDerBliek", "UKIRT-Extended",
00913                              "UKIRT-Fundamental", "SAAO-Carter", NULL};
00914     const char * lw_cat[] = {"ESO-VanDerBliek", "UKIRT-Standards",
00915                              "UKIRT-LM", NULL};
00916 
00917     bug_if (filter == NULL);
00918 
00919     cpl_msg_info(cpl_func, "Get the star magnitude with filter: %s", filter);
00920 
00921     band = naco_get_bbfilter(filter); /* Get the band */
00922 
00923     skip_if(0);
00924 
00925     /* Get the nearby standard stars */ 
00926     stdstars = naco_img_zpoint_load_std_star(star_cat, ra, dec, /* degrees */
00927                                              (IRPLIB_STDSTAR_MAXDIST)/60.0);
00928 
00929     skip_if(stdstars == NULL);
00930     skip_if(cpl_table_get_nrow(stdstars) == 0);
00931 
00932     istar = -1;
00933 
00934     switch (band) {
00935         /* SW mode */
00936     case BAND_J:
00937     case BAND_H:
00938     case BAND_K:
00939     case BAND_KS: 
00940 
00941         bandname = naco_std_band_name(band);
00942 
00943         error = naco_img_zpoint_find_std_star(stdstars, sw_cat, ra, dec,
00944                                               bandname, CPL_FALSE, &istar);
00945 
00946         if (error) break;
00947         if (istar >= 0) break;
00948 
00949 
00950         /* Special case: swap K and Ks if needed */
00951         if (band == BAND_K) {
00952             bandname = naco_std_band_name(BAND_KS);
00953 
00954             cpl_msg_info(cpl_func, "Retrying with alternative band: %s",
00955                          bandname);
00956 
00957             error = naco_img_zpoint_find_std_star(stdstars, sw_cat, ra, dec,
00958                                                   bandname, CPL_TRUE, &istar);
00959 
00960         } else if (band == BAND_KS) {
00961             bandname = naco_std_band_name(BAND_K);
00962 
00963             cpl_msg_info(cpl_func, "Retrying with alternative band: %s",
00964                          bandname);
00965 
00966             error = naco_img_zpoint_find_std_star(stdstars, sw_cat, ra, dec,
00967                                                   bandname, CPL_TRUE, &istar);
00968         }
00969         break;
00970 
00971         /* LW mode  */
00972     case BAND_L:
00973     case BAND_M:
00974     case BAND_LP:
00975     case BAND_MP:
00976 
00977         bandname = naco_std_band_name(band);
00978 
00979         error = naco_img_zpoint_find_std_star(stdstars, lw_cat, ra, dec,
00980                                               bandname, CPL_FALSE, &istar);
00981 
00982         if (error) break;
00983         if (istar < 0) {
00984             naco_band other = BAND_UNKNOWN;
00985 
00986             switch(band) {
00987             case BAND_L:
00988                 other = BAND_LP;
00989                 break;
00990             case BAND_M:
00991                 other = BAND_MP;
00992                 break;
00993             case BAND_LP:
00994                 other = BAND_L;
00995                 break;
00996             case BAND_MP:
00997                 other = BAND_M;
00998                 break;
00999             default:
01000                 bug_if (1);
01001             }
01002 
01003             bandname = naco_std_band_name(other);
01004 
01005             cpl_msg_info(cpl_func, "Retrying with alternative band: %s",
01006                          bandname);
01007 
01008             error = naco_img_zpoint_find_std_star(stdstars, lw_cat, ra, dec,
01009                                                   bandname, CPL_TRUE, &istar);
01010         }
01011         break;
01012     default:
01013         skip_if(1);
01014     }
01015 
01016     error_if (error || istar < 0, CPL_ERROR_DATA_NOT_FOUND, "Star magnitude "
01017               "for filter '%s' not found in catalog(s) at (RA,DEC)=(%g,%g)"
01018               " with tolerance=%g (degrees)", filter, ra, dec,
01019               IRPLIB_STDSTAR_MAXDIST/60.0);
01020 
01021     star_mag  = cpl_table_get_double(stdstars, bandname, istar, NULL);
01022     star_name = cpl_table_get_string(stdstars, IRPLIB_STDSTAR_STAR_COL, istar);
01023     star_type = cpl_table_get_string(stdstars, IRPLIB_STDSTAR_TYPE_COL, istar);
01024     cat_name  = cpl_table_get_string(stdstars, "CAT_NAME",              istar);
01025 
01026 
01027     /* Store results */
01028     bug_if(cpl_propertylist_append_string(qclist, "ESO QC STDNAME",
01029                                           star_name));
01030     bug_if(cpl_propertylist_append_string(qclist, "ESO QC CATNAME",
01031                                           cat_name));
01032     bug_if(cpl_propertylist_append_string(qclist, "ESO QC SPECTYPE",
01033                                           star_type));
01034 
01035     cpl_msg_info(cpl_func, "Found standard star '%s' of type '%s' with "
01036                  "magnitude %g for filter %s from catalog '%s'",
01037                  star_name, star_type, star_mag, bandname, cat_name);
01038 
01039     end_skip;
01040 
01041     cpl_table_delete(stdstars);
01042 
01043     return star_mag;
01044 }
01045 
01046 
01047 /*----------------------------------------------------------------------------*/
01069 /*----------------------------------------------------------------------------*/
01070 static cpl_error_code naco_img_zpoint_find_std_star(cpl_table * self,
01071                                                     const char * cat[],
01072                                                     double ra, double dec,
01073                                                     const char * bandname,
01074                                                     cpl_boolean retry,
01075                                                     int * pistar)
01076 {
01077 
01078     double min_dist = IRPLIB_STDSTAR_MAXDIST / 60.0; /* Maximum */
01079     int nsel;
01080 
01081     bug_if (pistar   == NULL);
01082     *pistar = -1;
01083 
01084     bug_if (self     == NULL);
01085     bug_if (bandname == NULL);
01086 
01087     nsel = (retry 
01088             ? cpl_table_or_selected_double
01089             : cpl_table_and_selected_double)(self, bandname,
01090                                              CPL_LESS_THAN,
01091                                              IRPLIB_STDSTAR_LIMIT);
01092 
01093     if (nsel == 0) {
01094         cpl_msg_info(cpl_func, "None of the standard star catalog(s) have "
01095                      "a star with a known magnitude for band %s within a "
01096                      "distance of tol=%g [degrees] from (RA,DEC)=(%g,%g)",
01097                      bandname, min_dist, ra, dec);
01098     } else {
01099 
01100         /* At this point we are sure to find a star, if not in the specified
01101            list of catalogs, then when searching them all */
01102 
01103         const int nrows = cpl_table_get_nrow(self);
01104         int ibest = -1;
01105         int i;
01106 
01107         int icat;
01108 
01109         bug_if (cat == NULL);
01110 
01111         for (icat = 0; cat[icat] != NULL; icat++) {
01112 
01113             char * regexp = cpl_sprintf("^%s$", cat[icat]);
01114 
01115             cpl_msg_info(cpl_func, "Trying %s", cat[icat]);
01116 
01117             nsel = cpl_table_and_selected_string(self, "CAT_NAME",
01118                                                  CPL_EQUAL_TO, regexp);
01119             cpl_free(regexp);
01120 
01121             if (nsel > 0) break;
01122 
01123             /* No stars found in this catalog, reset to try next */
01124             nsel = cpl_table_or_selected_double(self, bandname,
01125                                                 CPL_LESS_THAN,
01126                                                 IRPLIB_STDSTAR_LIMIT);
01127             bug_if(nsel == 0);
01128         }
01129 
01130         bug_if (nsel == 0);
01131 
01132         /* Compute distances to the selected rows */
01133 
01134         for (i=0; i < nrows; i++) {
01135             if (cpl_table_is_selected(self, i)) {
01136                 /* The row is selected - compute the distance */
01137                 const double distance
01138                     = irplib_wcs_great_circle_dist(ra, dec,
01139                     cpl_table_get_double(self, IRPLIB_STDSTAR_RA_COL, i, NULL),
01140                     cpl_table_get_double(self, IRPLIB_STDSTAR_DEC_COL, i,NULL));
01141 
01142                 if (distance < min_dist) {
01143                     min_dist = distance;
01144                     ibest = i;
01145                 }
01146             }
01147         }
01148 
01149         bug_if (ibest < 0);
01150 
01151         cpl_msg_info(cpl_func, "Found %d star(s) with known magnitude in band "
01152                      "%s - selected one with distance %g", nsel, bandname,
01153                      min_dist);
01154 
01155         *pistar = ibest;
01156     }
01157 
01158     end_skip;
01159 
01160     return cpl_error_get_code();
01161 }
01162 
01163 
01164 /*----------------------------------------------------------------------------*/
01173 /*----------------------------------------------------------------------------*/
01174 static cpl_error_code naco_img_zpoint_qc(cpl_propertylist       * qclist,
01175                                          cpl_propertylist       * paflist,
01176                                          const irplib_framelist * rawframes)
01177 {
01178 
01179     cpl_errorstate cleanstate = cpl_errorstate_get();
01180     const cpl_propertylist * reflist
01181         = irplib_framelist_get_propertylist_const(rawframes, 0);
01182 
01183     const char       * sval;
01184 
01185 
01186     bug_if(0);
01187 
01188     /* Copy the keywords for the paf file */
01189     bug_if(cpl_propertylist_copy_property_regexp(paflist, reflist, "^("
01190                                                  NACO_PFITS_REGEXP_ZPOINT_PAF
01191                                                  ")$", 0));
01192 
01193     /* Add QC parameters */
01194 
01195     sval = naco_pfits_get_opti3_name(reflist);
01196     if (sval == NULL)
01197         naco_error_reset("Could not get FITS key:");
01198     else
01199         bug_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER NDENS",
01200                                                sval));
01201     sval = naco_pfits_get_opti4_name(reflist);
01202     if (sval == NULL)
01203         naco_error_reset("Could not get FITS key:");
01204     else
01205         bug_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER POL",
01206                                                sval));
01207 
01208     skip_if (naco_img_zpoint_qc_all(qclist, paflist, rawframes));
01209 
01210     bug_if(cpl_propertylist_append(paflist, qclist));
01211 
01212     bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
01213                                                  IRPLIB_PFITS_REGEXP_RECAL 
01214                                                  ")$", 0));
01215 
01216     bug_if (irplib_pfits_set_airmass(qclist, rawframes));
01217     bug_if(cpl_propertylist_append_double(qclist, "ESO QC AIRMASS", 
01218                                           irplib_pfits_get_double(qclist,
01219                                                                   "AIRMASS")));
01220 
01221     end_skip;
01222 
01223     return cpl_error_get_code();
01224 }
01225 
01226 /*----------------------------------------------------------------------------*/
01238 /*----------------------------------------------------------------------------*/
01239 static cpl_error_code naco_img_zpoint_save(cpl_frameset            * set,
01240                                            const cpl_parameterlist * parlist,
01241                                            const cpl_propertylist  * qclist,
01242                                            const cpl_propertylist  * paflist,
01243                                            const cpl_table         * tab,
01244                                            const cpl_image         * check_im)
01245 {
01246 
01247     bug_if(0);
01248 
01249     /* Write the zpoint table  */
01250     skip_if (irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
01251                                NACO_IMG_ZPOINT_RES, qclist, NULL, naco_pipe_id,
01252                                RECIPE_STRING CPL_DFS_FITS));
01253 
01254 
01255     /* Write the check image */
01256     if (check_im)
01257         skip_if (irplib_dfs_save_image(set, parlist, set, check_im,
01258                                    CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
01259                                    NACO_IMG_ZPOINT_CHECK, qclist, NULL,
01260                                    naco_pipe_id,
01261                                    RECIPE_STRING "_check" CPL_DFS_FITS));
01262 
01263     skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist,
01264                              RECIPE_STRING CPL_DFS_PAF));
01265 
01266     end_skip;
01267 
01268     return cpl_error_get_code();
01269 }
01270 
01271 /*----------------------------------------------------------------------------*/
01280 /*----------------------------------------------------------------------------*/
01281 static cpl_error_code naco_img_zpoint_qc_all(cpl_propertylist * qclist,
01282                                              cpl_propertylist * paflist,
01283                                              const irplib_framelist * rawframes)
01284 {
01285     const int    nframes  = irplib_framelist_get_size(rawframes);
01286     cpl_vector * ec_vec   = cpl_vector_new(nframes);
01287     cpl_vector * flux_vec = cpl_vector_new(nframes);
01288     cpl_vector * l0_vec   = cpl_vector_new(nframes);
01289     cpl_vector * t0_vec   = cpl_vector_new(nframes);
01290     cpl_vector * r0_vec   = cpl_vector_new(nframes);
01291     cpl_vector * hum_vec  = cpl_vector_new(nframes);
01292     double       ec, flux, l0, t0, r0, hum;
01293     int          i;
01294 
01295 
01296     bug_if(0);
01297 
01298     bug_if(nframes <= 0);
01299 
01300     for (i = 0; i < nframes; i++) {
01301         const cpl_propertylist * plist
01302             = irplib_framelist_get_propertylist_const(rawframes, i);
01303 
01304         bug_if(0);
01305 
01306         skip_if(cpl_vector_set(ec_vec,   i, naco_pfits_get_ecmean(plist)));
01307         skip_if(cpl_vector_set(flux_vec, i, naco_pfits_get_fluxmean(plist)));
01308         skip_if(cpl_vector_set(t0_vec,   i, naco_pfits_get_t0mean(plist)));
01309         skip_if(cpl_vector_set(l0_vec,   i, naco_pfits_get_l0mean(plist)));
01310         skip_if(cpl_vector_set(r0_vec,   i, naco_pfits_get_r0mean(plist)));
01311         skip_if(cpl_vector_set(hum_vec,  i,
01312                                naco_pfits_get_humidity_level(plist)));
01313 
01314     }
01315 
01316     bug_if(0);
01317 
01318     ec   = cpl_vector_get_median(ec_vec);   /* vector-permute */
01319     flux = cpl_vector_get_median(flux_vec); /* vector-permute */
01320     l0   = cpl_vector_get_median(l0_vec);   /* vector-permute */
01321     t0   = cpl_vector_get_median(t0_vec);   /* vector-permute */
01322     r0   = cpl_vector_get_median(r0_vec);   /* vector-permute */
01323     hum  = cpl_vector_get_mean(hum_vec);
01324     bug_if(0);
01325 
01326     cpl_propertylist_append_double(qclist, "ESO QC AMBI RHUM AVG", hum);
01327     cpl_propertylist_append_double(paflist, 
01328                                    "ESO AOS RTC DET DST L0MEAN", l0);
01329     cpl_propertylist_append_double(paflist, 
01330                                    "ESO AOS RTC DET DST T0MEAN", t0);
01331     cpl_propertylist_append_double(paflist, 
01332                                    "ESO AOS RTC DET DST R0MEAN", r0);
01333     cpl_propertylist_append_double(paflist, 
01334                                    "ESO AOS RTC DET DST ECMEAN", ec);
01335     cpl_propertylist_append_double(paflist, 
01336                                    "ESO AOS RTC DET DST FLUXMEAN", flux);
01337     bug_if(0);
01338 
01339 
01340     end_skip;
01341 
01342     cpl_vector_delete(ec_vec);
01343     cpl_vector_delete(flux_vec);
01344     cpl_vector_delete(r0_vec);
01345     cpl_vector_delete(t0_vec);
01346     cpl_vector_delete(l0_vec);
01347     cpl_vector_delete(hum_vec);
01348 
01349     return cpl_error_get_code();
01350 }
01351 
01352 
01353 /*----------------------------------------------------------------------------*/
01360 /*----------------------------------------------------------------------------*/
01361 static naco_band naco_get_bbfilter(const char * filter)
01362 {
01363     naco_band self = BAND_UNKNOWN;
01364 
01365 
01366     bug_if (filter == NULL);
01367 
01368     if (!strcmp(filter, "J"))             self = BAND_J;
01369     else if (!strcmp(filter, "Jc"))       self = BAND_J;
01370     else if (!strcmp(filter, "H"))        self = BAND_H;
01371     else if (!strcmp(filter, "K"))        self = BAND_K;
01372     else if (!strcmp(filter, "Ks"))       self = BAND_KS;
01373     else if (!strcmp(filter, "L"))        self = BAND_L;
01374     else if (!strcmp(filter, "L_prime"))  self = BAND_LP;
01375     else if (!strcmp(filter, "M_prime"))  self = BAND_MP;
01376     else if (!strcmp(filter, "NB_1.04"))  self = BAND_J;
01377     else if (!strcmp(filter, "NB_1.08"))  self = BAND_J;
01378     else if (!strcmp(filter, "NB_1.09"))  self = BAND_J;
01379     else if (!strcmp(filter, "NB_1.24"))  self = BAND_J;
01380     else if (!strcmp(filter, "NB_1.26"))  self = BAND_J;
01381     else if (!strcmp(filter, "NB_1.28"))  self = BAND_J;
01382     else if (!strcmp(filter, "NB_1.64"))  self = BAND_H;
01383     else if (!strcmp(filter, "NB_1.75"))  self = BAND_H;
01384     else if (!strcmp(filter, "NB_3.74"))  self = BAND_L;
01385     else if (!strcmp(filter, "IB_2.00"))  self = BAND_K;
01386     else if (!strcmp(filter, "IB_2.03"))  self = BAND_K;
01387     else if (!strcmp(filter, "IB_2.06"))  self = BAND_K;
01388     else if (!strcmp(filter, "IB_2.09"))  self = BAND_K;
01389     else if (!strcmp(filter, "IB_2.12"))  self = BAND_K;
01390     else if (!strcmp(filter, "NB_2.12"))  self = BAND_K;
01391     else if (!strcmp(filter, "IB_2.15"))  self = BAND_K;
01392     else if (!strcmp(filter, "NB_2.17"))  self = BAND_K;
01393     else if (!strcmp(filter, "IB_2.18"))  self = BAND_K;
01394     else if (!strcmp(filter, "IB_2.21"))  self = BAND_K;
01395     else if (!strcmp(filter, "IB_2.24"))  self = BAND_K;
01396     else if (!strcmp(filter, "IB_2.27"))  self = BAND_K;
01397     else if (!strcmp(filter, "IB_2.30"))  self = BAND_K;
01398     else if (!strcmp(filter, "IB_2.33"))  self = BAND_K;
01399     else if (!strcmp(filter, "IB_2.36"))  self = BAND_K;
01400     else if (!strcmp(filter, "IB_2.39"))  self = BAND_K;
01401     else if (!strcmp(filter, "IB_2.42"))  self = BAND_K;
01402     else if (!strcmp(filter, "IB_2.45"))  self = BAND_K;
01403     else if (!strcmp(filter, "IB_2.48"))  self = BAND_K;
01404     else if (!strcmp(filter, "NB_4.05"))  self = BAND_M;
01405         /* As per email 2009-10-13 from epompei@eso.org */
01406     else if (!strcmp(filter, "IB_4.05"))  self = BAND_M;
01407     else if (!strcmp(filter, "SJ"))       self = BAND_UNKNOWN;
01408     else if (!strcmp(filter, "SH"))       self = BAND_UNKNOWN;
01409     else if (!strcmp(filter, "SK"))       self = BAND_UNKNOWN;
01410 
01411     error_if (self == BAND_UNKNOWN, CPL_ERROR_UNSUPPORTED_MODE, "No broad-"
01412               "band could be associated with the filter: %s", filter);
01413 
01414     end_skip;
01415 
01416     return self;
01417 }
01418 
01419 
01420 /*----------------------------------------------------------------------------*/
01431 /*----------------------------------------------------------------------------*/
01432 static cpl_table * naco_img_zpoint_load_std_star(const char * star_cat,
01433                                                  double ra, double dec,
01434                                                  double tol)
01435 {
01436 
01437     cpl_table    * self = cpl_table_load(star_cat, 1, 0);
01438     const double * pra
01439         = cpl_table_get_data_double_const(self, IRPLIB_STDSTAR_RA_COL);
01440     const double * pdec
01441         = cpl_table_get_data_double_const(self, IRPLIB_STDSTAR_DEC_COL);
01442     const int      nrow = cpl_table_get_nrow(self);
01443     int i;
01444 
01445     skip_if(self == NULL);
01446     bug_if(star_cat == NULL);
01447     skip_if(pra == NULL);
01448     skip_if(pdec == NULL);
01449     skip_if(tol < 0.0);
01450 
01451     bug_if(cpl_table_unselect_all(self));
01452 
01453     for (i = 0; i < nrow; i++) {
01454         const double deci = pdec[i];
01455 
01456         if (deci > dec + tol || 
01457             deci < dec - tol || 
01458             irplib_wcs_great_circle_dist(ra, dec, pra[i], deci) > tol) {
01459 
01460             bug_if(cpl_table_select_row(self, i));
01461 
01462         }
01463     }
01464 
01465     if (cpl_table_count_selected(self) == nrow) {
01466         double mindist = DBL_MAX; /* Avoid uninit warning */
01467         int    idist   = 0;       /* Avoid uninit warning */
01468 
01469         for (i = 0; i < nrow; i++) {
01470             const double dist
01471                 = irplib_wcs_great_circle_dist(ra, dec, pra[i], pdec[i]);
01472 
01473             if (i == 0 || dist < mindist) {
01474                 idist = i;
01475                 mindist = dist;
01476             }
01477         }
01478 
01479         (void)cpl_error_set_message_macro(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
01480                                           __FILE__, __LINE__,  "Catalog '%s' "
01481                                           "star %d of %d at (RA,DEC)=(%g,%g) "
01482                                           "is closest to target at "
01483                                           "(RA,DEC)=(%g,%g) with a too large "
01484                                           "distance of %g [degrees] > %g",
01485                                           star_cat, 1+idist, nrow, pra[idist],
01486                                           pdec[idist], ra, dec, mindist, tol);
01487     }
01488 
01489     bug_if(cpl_table_erase_selected(self));
01490 
01491     cpl_msg_info(cpl_func, "Loaded %d of %d stars from '%s' which are within "
01492                  "%g degrees from (RA,DEC)=(%g,%g)",
01493                  (int)cpl_table_get_nrow(self), nrow, star_cat, tol, ra, dec);
01494     if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
01495         cpl_table_dump(self, 0, cpl_table_get_nrow(self), stdout);
01496     }
01497 
01498     end_skip;
01499 
01500     return self;
01501 
01502 }
01503 
01504 /*-------------------------------------------------------------------------*/
01511 /*--------------------------------------------------------------------------*/
01512 static const char * naco_std_band_name(naco_band band)
01513 {
01514     switch (band) {
01515         case BAND_J:        return "J"; 
01516         case BAND_JS:       return "Js";
01517         case BAND_JBLOCK:   return "J+Block";
01518         case BAND_H:        return "H";
01519         case BAND_K:        return "K";
01520         case BAND_KS:       return "Ks";
01521         case BAND_L:        return "L";
01522         case BAND_M:        return "M";
01523         case BAND_LP:       return "Lp";
01524         case BAND_MP:       return "Mp";
01525         case BAND_Z:        return "Z";
01526         case BAND_SZ:       return "SZ";
01527         case BAND_SH:       return "SH";
01528         case BAND_SK:       return "SK";
01529         case BAND_SL:       return "SL";
01530         default:            return "Unknown";
01531     } 
01532 }
01533 

Generated on Mon Feb 6 14:42:07 2012 for NACO Pipeline Reference Manual by  doxygen 1.5.8