naco_img_zpoint.c

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

Generated on Fri Jul 3 11:23:58 2009 for NACO Pipeline Reference Manual by  doxygen 1.5.8