naco_img_jitter.c

00001 /* $Id: naco_img_jitter.c,v 1.67 2009/03/10 10:14:44 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/03/10 10:14:44 $
00024  * $Revision: 1.67 $
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 "naco_recipe.h"
00037 #include "irplib_calib.h"
00038 
00039 /*-----------------------------------------------------------------------------
00040                             Defines
00041  -----------------------------------------------------------------------------*/
00042 
00043 #define RECIPE_STRING "naco_img_jitter"
00044 
00045 /*-----------------------------------------------------------------------------
00046                             Private Functions prototypes
00047  -----------------------------------------------------------------------------*/
00048 
00049 static cpl_image ** naco_img_jitter_reduce(cpl_propertylist *,
00050                                            const irplib_framelist *,
00051                                            const irplib_framelist *,
00052                                            const cpl_parameterlist *,
00053                                            const char *,
00054                                            const char *,
00055                                            const char *);
00056 
00057 static cpl_error_code naco_img_jitter_calib(cpl_imagelist *, cpl_imagelist *,
00058                                  const char *, const char *, const char *);
00059 static cpl_error_code naco_img_jitter_sky(cpl_propertylist *, cpl_imagelist *,
00060                                           const cpl_imagelist *);
00061 static cpl_image ** naco_img_jitter_saa(cpl_imagelist *,
00062                                         const irplib_framelist *,
00063                                         const cpl_parameterlist *);
00064 static cpl_error_code naco_img_jitter_qc(cpl_propertylist *, cpl_propertylist *,
00065                                          const irplib_framelist *,
00066                                          const cpl_image *);
00067 static cpl_error_code naco_img_jitter_qc_apertures(cpl_propertylist *,
00068                                                    const irplib_framelist *,
00069                                                    const cpl_image *);
00070 static cpl_error_code naco_img_jitter_save(cpl_frameset *,
00071                                            const cpl_parameterlist *,
00072                                            const cpl_propertylist *,
00073                                            const cpl_propertylist *,
00074                                            const cpl_image *,
00075                                            const cpl_image *);
00076 
00077 NACO_RECIPE_DEFINE(naco_img_jitter, 
00078                    NACO_PARAM_OFFSETS |
00079                    NACO_PARAM_OBJECTS |
00080                    NACO_PARAM_ODDEVEN |
00081                    NACO_PARAM_XCORR   |
00082                    NACO_PARAM_UNION   |
00083                    NACO_PARAM_REJ_HILO,
00084                    "Jitter recipe",
00085                   RECIPE_STRING " -- NACO imaging jitter recipe.\n"         
00086                   "The files listed in the Set Of Frames (sof-file) "
00087                   "must be tagged:\n" 
00088                   "NACO-raw-file.fits "NACO_IMG_JITTER_OBJ" or\n"            
00089                   "NACO-raw-file.fits "NACO_IMG_JITTER_SKY" or\n"            
00090                   "NACO-raw-file.fits "NACO_IMG_JITTER_OBJ_POL" or\n"        
00091                   "NACO-raw-file.fits "NACO_IMG_JITTER_SKY_POL" or\n"        
00092                   "NACO-flat-file.fits "NACO_CALIB_FLAT" or\n"               
00093                   "NACO-bpm-file.fits "NACO_CALIB_BPM" or\n"               
00094                   "NACO-dark-file.fits "NACO_CALIB_DARK"\n");
00095 
00096 /*----------------------------------------------------------------------------*/
00100 /*----------------------------------------------------------------------------*/
00101 
00102 /*-----------------------------------------------------------------------------
00103                                 Functions code
00104  -----------------------------------------------------------------------------*/
00105 
00106 /*----------------------------------------------------------------------------*/
00113 /*----------------------------------------------------------------------------*/
00114 static int naco_img_jitter(cpl_frameset            * framelist,
00115                            const cpl_parameterlist * parlist)
00116 {
00117     cpl_errorstate cleanstate = cpl_errorstate_get();
00118     irplib_framelist * allframes = NULL;
00119     irplib_framelist * objframes = NULL;
00120     irplib_framelist * skyframes = NULL;
00121     cpl_propertylist * qclist    = cpl_propertylist_new();
00122     cpl_propertylist * paflist   = cpl_propertylist_new();
00123     cpl_image       ** combined  = NULL;
00124     const char       * badpix;
00125     const char       * dark;
00126     const char       * flat;
00127 
00128 
00129     /* Identify the RAW and CALIB frames in the input frameset */
00130     skip_if (naco_dfs_set_groups(framelist));
00131 
00132     allframes = irplib_framelist_cast(framelist);
00133     skip_if(allframes == NULL);
00134 
00135     objframes = irplib_framelist_extract_regexp(allframes,
00136                                                 "^(" NACO_IMG_JITTER_OBJ "|"
00137                                                 NACO_IMG_JITTER_OBJ_POL ")$",
00138                                                 CPL_FALSE);
00139     skip_if(objframes == NULL);
00140 
00141     skyframes = irplib_framelist_extract_regexp(allframes,
00142                                                 "^(" NACO_IMG_JITTER_SKY "|"
00143                                                 NACO_IMG_JITTER_SKY_POL ")$",
00144                                                 CPL_FALSE);
00145     irplib_framelist_empty(allframes);
00146     if (skyframes == NULL) {
00147         naco_error_reset("The set of frames has no sky frames:");
00148     }
00149 
00150     flat = irplib_frameset_find_file(framelist, NACO_CALIB_FLAT);
00151     bug_if(0);
00152 
00153     badpix = irplib_frameset_find_file(framelist, NACO_CALIB_BPM);
00154     bug_if(0);
00155 
00156     dark = irplib_frameset_find_file(framelist, NACO_CALIB_DARK);
00157     bug_if(0);
00158 
00159     skip_if(irplib_framelist_load_propertylist(objframes, 0, 0, "^("
00160                                                NACO_PFITS_REGEXP_JITTER_FIRST
00161                                                ")$", CPL_FALSE));
00162 
00163     skip_if(irplib_framelist_load_propertylist_all(objframes, 0, "^("
00164                                                    NACO_PFITS_REGEXP_JITTER_ALL
00165                                                    ")$", CPL_FALSE));
00166 
00167     /* Apply the reduction */
00168     cpl_msg_info(cpl_func, "Apply the data recombination");
00169     combined = naco_img_jitter_reduce(qclist, objframes, skyframes, parlist,
00170                                       dark, flat, badpix);
00171 
00172     skip_if (combined == NULL);
00173 
00174     irplib_framelist_empty(skyframes);
00175 
00176     /* Compute QC parameters from the combined image */
00177     cpl_msg_info(cpl_func, "Compute QC parameters from the combined image");
00178     skip_if (naco_img_jitter_qc(qclist, paflist, objframes, combined[0]));
00179 
00180     irplib_framelist_empty(objframes);
00181     
00182     /* Save the products */
00183     cpl_msg_info(cpl_func, "Save the products");
00184 
00185     /* PRO.CATG */
00186     bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00187                                            NACO_IMG_JITTER_COMB));
00188 
00189     skip_if (naco_img_jitter_save(framelist, parlist, qclist, paflist,
00190                                   combined[0],
00191                                   combined[1]));
00192 
00193     end_skip;
00194     
00195     if (combined != NULL) {
00196         cpl_image_delete(combined[0]);
00197         cpl_image_delete(combined[1]);
00198         cpl_free(combined);
00199     }
00200     irplib_framelist_delete(allframes);
00201     irplib_framelist_delete(objframes);
00202     irplib_framelist_delete(skyframes);
00203     cpl_propertylist_delete(qclist);
00204     cpl_propertylist_delete(paflist);
00205 
00206     return cpl_error_get_code();
00207 }
00208 
00209 /*----------------------------------------------------------------------------*/
00221 /*----------------------------------------------------------------------------*/
00222 static cpl_image ** naco_img_jitter_reduce(cpl_propertylist       * qclist,
00223                                            const irplib_framelist * obj,
00224                                            const irplib_framelist * sky,
00225                                            const cpl_parameterlist * parlist,
00226                                            const char             * dark,
00227                                            const char             * flat,
00228                                            const char             * bpm)
00229 {
00230     cpl_imagelist * objimages = NULL;
00231     cpl_imagelist * skyimages = NULL;
00232     cpl_image     ** combined = NULL;
00233 
00234 
00235     skip_if (irplib_framelist_contains(obj, NACO_PFITS_DOUBLE_CUMOFFSETX,
00236                                        CPL_TYPE_DOUBLE, CPL_FALSE, 0.0));
00237 
00238     skip_if (irplib_framelist_contains(obj, NACO_PFITS_DOUBLE_CUMOFFSETY,
00239                                        CPL_TYPE_DOUBLE, CPL_FALSE, 0.0));
00240 
00241     /* Load the input data */
00242     cpl_msg_info(cpl_func, "Loading the %d object and %d sky images",
00243                  irplib_framelist_get_size(obj),
00244                  sky == NULL ? 0 : irplib_framelist_get_size(sky));
00245     objimages = irplib_imagelist_load_framelist(obj, CPL_TYPE_FLOAT, 0, 0);
00246     skip_if(0);
00247 
00248     if (sky != NULL) {
00249         skyimages = irplib_imagelist_load_framelist(sky, CPL_TYPE_FLOAT, 0, 0);
00250         skip_if(0);
00251     }
00252 
00253     /* Apply the calibrations */
00254     cpl_msg_info(cpl_func, "Calibrations");
00255     skip_if (naco_img_jitter_calib(objimages, skyimages, dark, flat, bpm));
00256 
00257     /* Apply the sky correction */
00258     cpl_msg_info(cpl_func, "Sky estimation and correction");
00259     skip_if (naco_img_jitter_sky(qclist, objimages, skyimages));
00260 
00261     cpl_imagelist_delete(skyimages);
00262     skyimages = NULL;
00263      
00264     /* Apply the shift and add */
00265     cpl_msg_info(cpl_func, "Shift and add");
00266     combined = naco_img_jitter_saa(objimages, obj, parlist);
00267     skip_if (combined == NULL);
00268 
00269     end_skip;
00270 
00271     cpl_imagelist_delete(objimages);
00272     cpl_imagelist_delete(skyimages);
00273 
00274     return combined;
00275 }
00276 
00277 /*----------------------------------------------------------------------------*/
00286 /*----------------------------------------------------------------------------*/
00287 static cpl_error_code naco_img_jitter_calib(cpl_imagelist   *   objs,
00288                                             cpl_imagelist   *   skys,
00289                                             const char      *   dark,
00290                                             const char      *   flat,
00291                                             const char      *   bpm)
00292 {
00293 
00294     /* Test entries */
00295     bug_if (objs == NULL);
00296 
00297 
00298     /* Apply calibrations to the object images */
00299     cpl_msg_info(cpl_func, "Apply calibrations to the object images");
00300     if (irplib_flat_dark_bpm_calib(objs, flat, dark, bpm) == -1) {
00301         cpl_msg_error(cpl_func, "Cannot apply calibrations to object frames");
00302         skip_if(1);
00303     }
00304 
00305     if (skys) {
00306         /* Apply calibrations to the sky images */
00307         cpl_msg_info(cpl_func, "Apply calibrations to the sky images");
00308         if (irplib_flat_dark_bpm_calib(skys, flat, dark, bpm) == -1) {
00309             cpl_msg_error(cpl_func, "Cannot apply calibrations to sky frames");
00310             skip_if(1);
00311         }
00312     }
00313 
00314     end_skip;
00315 
00316     return cpl_error_get_code();
00317 }
00318 
00319 /*----------------------------------------------------------------------------*/
00327 /*----------------------------------------------------------------------------*/
00328 static cpl_error_code naco_img_jitter_sky(cpl_propertylist    * qclist,
00329                                           cpl_imagelist       * objs,
00330                                           const cpl_imagelist * skys)
00331 {
00332     cpl_image * sky = NULL;
00333 
00334     /* Compute the sky frame */
00335     if (skys != NULL) {
00336         /* Use sky images */
00337         sky = cpl_imagelist_collapse_median_create(skys);
00338         if (sky == NULL) {
00339             cpl_msg_error(cpl_func, "Cannot compute the median of sky images");
00340             skip_if(1);
00341         }
00342     } else {
00343         /* Use objects images */
00344         sky = cpl_imagelist_collapse_median_create(objs);
00345         if (sky == NULL) {
00346             cpl_msg_error(cpl_func, "Cannot compute the median of object "
00347                           "images");
00348             skip_if(1);
00349         }
00350     }
00351 
00352     /* Correct the objects images  */
00353     bug_if (cpl_imagelist_subtract_image(objs, sky));
00354 
00355     /* Get the background value */
00356     bug_if(cpl_propertylist_append_double(qclist, "ESO QC BACKGD",
00357                                           cpl_image_get_median(sky)));
00358 
00359     end_skip;
00360     
00361     cpl_image_delete(sky);
00362 
00363     return cpl_error_get_code();
00364 }
00365 
00366 /*----------------------------------------------------------------------------*/
00374 /*----------------------------------------------------------------------------*/
00375 static cpl_image ** naco_img_jitter_saa(cpl_imagelist    * imlist,
00376                                         const irplib_framelist * objframes,
00377                                         const cpl_parameterlist * parlist)
00378 {
00379     const char   * sval;
00380     cpl_bivector * offsets_est = NULL;
00381     cpl_bivector * objs = NULL;
00382     cpl_image   ** combined = NULL;
00383     cpl_vector   * sigmas = NULL;
00384     double         psigmas[] = {5.0, 2.0, 1.0, 0.5};
00385     const char   * offsets;
00386     const char   * objects;
00387     cpl_boolean    oddeven_flag;
00388     const int      nsigmas = (int)(sizeof(psigmas)/sizeof(double));
00389     const int      nfiles = cpl_imagelist_get_size(imlist);
00390     int            sx, sy, mx, my;
00391     int            rej_low, rej_high;
00392     cpl_geom_combine combine_type;
00393 
00394 
00395     bug_if(0);
00396     bug_if (irplib_framelist_get_size(objframes) != nfiles);
00397 
00398     /* Retrieve input parameters */
00399 
00400     /* Offsets */
00401     offsets = naco_parameterlist_get_string(parlist, RECIPE_STRING,
00402                                             NACO_PARAM_OFFSETS);
00403     /* Objects */
00404     objects = naco_parameterlist_get_string(parlist, RECIPE_STRING,
00405                                             NACO_PARAM_OBJECTS);
00406     /* Oddeven flag */
00407     oddeven_flag = naco_parameterlist_get_bool(parlist, RECIPE_STRING,
00408                                                NACO_PARAM_ODDEVEN);
00409 
00410     /* Cross correlation windows parameters */
00411     sval = naco_parameterlist_get_string(parlist, RECIPE_STRING,
00412                                       NACO_PARAM_XCORR);
00413     bug_if (sval == NULL);
00414 
00415     skip_if (sscanf(sval, "%d %d %d %d", &sx, &sy, &mx, &my) != 4);
00416 
00417     /* Union flag */
00418     combine_type = naco_parameterlist_get_bool(parlist, RECIPE_STRING,
00419                                                NACO_PARAM_UNION)
00420         ? CPL_GEOM_UNION : CPL_GEOM_INTERSECT;
00421 
00422     /* Number of rejected values in stacking */
00423     sval = naco_parameterlist_get_string(parlist, RECIPE_STRING,
00424                                       NACO_PARAM_REJ_HILO);
00425     bug_if (sval == NULL);
00426 
00427     skip_if (sscanf(sval, "%d %d", &rej_low, &rej_high) != 2);
00428 
00429     
00430     /* Get the offsets estimation of each input file pair */
00431     cpl_msg_info(cpl_func, "Get the offsets estimation");
00432     offsets_est = NULL;
00433     if (offsets &&
00434             offsets[0] != (char)0) {
00435         /* A file has been provided on the command line */
00436         offsets_est = cpl_bivector_read(offsets);
00437         if (offsets_est == NULL ||
00438             cpl_bivector_get_size(offsets_est) != nfiles) {
00439             cpl_msg_error(cpl_func, "Cannot get offsets from %s", 
00440                     offsets);
00441             skip_if(1);
00442         }
00443     } else {
00444         double * offsets_est_x;
00445         double * offsets_est_y;
00446         double offx0 = DBL_MAX; /* Avoid (false) uninit warning */
00447         double offy0 = DBL_MAX; /* Avoid (false) uninit warning */
00448         int i;
00449 
00450         /* Get the offsets from the header */
00451         offsets_est = cpl_bivector_new(nfiles);
00452         offsets_est_x = cpl_bivector_get_x_data(offsets_est);
00453         offsets_est_y = cpl_bivector_get_y_data(offsets_est);
00454         for (i=0 ; i < nfiles ; i++) {
00455             const cpl_propertylist * plist
00456                 = irplib_framelist_get_propertylist_const(objframes, i);
00457 
00458             /* X and Y offsets */
00459             if (i == 0) {
00460                 offx0 = naco_pfits_get_cumoffsetx(plist);
00461                 offy0 = naco_pfits_get_cumoffsety(plist);
00462             }
00463 
00464             /* Subtract the first offset to all offsets */
00465             offsets_est_x[i] = offx0 - naco_pfits_get_cumoffsetx(plist);
00466             offsets_est_y[i] = offy0 - naco_pfits_get_cumoffsety(plist);
00467 
00468             bug_if(0);
00469         }
00470     }
00471 
00472     /* Read the provided objects file if provided */
00473     if (objects &&
00474             objects[0] != (char)0) {
00475         cpl_msg_info(cpl_func, "Get the user provided correlation objects");
00476         /* A file has been provided on the command line */
00477         objs = cpl_bivector_read(objects);
00478         if (objs==NULL) {
00479             cpl_msg_error(cpl_func, "Cannot get objects from %s",
00480                     objects);
00481             skip_if (1);
00482         }
00483     }
00484 
00485     /* Create the vector for the detection thresholds */
00486     sigmas = cpl_vector_wrap(nsigmas, psigmas);
00487     
00488     /* Recombine the images */
00489     cpl_msg_info(cpl_func, "Recombine the images set");
00490     combined = cpl_geom_img_offset_combine(imlist, offsets_est, 1, objs,
00491                                            sigmas, NULL, sx, sy, mx, my,
00492                                            rej_low, rej_high, combine_type);
00493 
00494     skip_if (combined == NULL);
00495 
00496     end_skip;
00497 
00498     cpl_bivector_delete(offsets_est);
00499     cpl_bivector_delete(objs);
00500     cpl_vector_unwrap(sigmas);
00501 
00502     return combined;
00503 }
00504 
00505 /*----------------------------------------------------------------------------*/
00514 /*----------------------------------------------------------------------------*/
00515 static cpl_error_code naco_img_jitter_qc(cpl_propertylist       * qclist,
00516                                          cpl_propertylist       * paflist,
00517                                          const irplib_framelist * objframes,
00518                                          const cpl_image        * combined)
00519 {
00520     cpl_errorstate cleanstate = cpl_errorstate_get();
00521     const cpl_propertylist * reflist
00522         = irplib_framelist_get_propertylist_const(objframes, 0);
00523     const char    * sval;
00524 
00525 
00526     bug_if(combined == NULL);
00527 
00528     bug_if(cpl_propertylist_copy_property_regexp(paflist, reflist, "^("
00529                                                  NACO_PFITS_REGEXP_JITTER_PAF
00530                                                  ")$", 0));
00531 
00532     if (naco_img_jitter_qc_apertures(qclist, objframes, combined)) {
00533         naco_error_reset("Could not compute all QC parameters");
00534     }
00535 
00536     sval = naco_pfits_get_filter(reflist);
00537     if (cpl_error_get_code()) {
00538         naco_error_reset("Could not get FITS key:");
00539     } else {
00540         cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
00541     }
00542     sval = naco_pfits_get_opti3_name(reflist);
00543     if (cpl_error_get_code()) {
00544         naco_error_reset("Could not get FITS key:");
00545     } else {
00546         cpl_propertylist_append_string(qclist, "ESO QC FILTER NDENS", sval);
00547     }
00548     sval = naco_pfits_get_opti4_name(reflist);
00549     if (cpl_error_get_code()) {
00550         naco_error_reset("Could not get FITS key:");
00551     } else {
00552         cpl_propertylist_append_string(qclist, "ESO QC FILTER POL", sval);
00553     }
00554 
00555     bug_if(0);
00556 
00557     bug_if (cpl_propertylist_append(paflist, qclist));
00558 
00559     bug_if (cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
00560                                                   NACO_PFITS_REGEXP_JITTER_COPY
00561                                                   ")$", 0));
00562 
00563 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4,0,0)
00564 #define cpl_propertylist_contains cpl_propertylist_has
00565 #endif
00566 
00567     if (cpl_propertylist_contains(qclist, "AIRMASS"))
00568         bug_if (irplib_pfits_set_airmass(qclist, objframes));
00569 
00570     end_skip;
00571 
00572     return cpl_error_get_code();
00573 }
00574 
00575 /*----------------------------------------------------------------------------*/
00583 /*----------------------------------------------------------------------------*/
00584 static cpl_error_code naco_img_jitter_qc_apertures(cpl_propertylist * qclist,
00585                                                    const irplib_framelist *
00586                                                    objframes,
00587                                                    const cpl_image  * combined)
00588 {
00589     const cpl_propertylist * reflist
00590         = irplib_framelist_get_propertylist_const(objframes, 0);
00591 
00592     cpl_apertures * aperts = NULL;
00593     cpl_bivector  * fwhms  = NULL;
00594     cpl_vector    * fwhms_sum = NULL;
00595     cpl_vector    * sigmas = NULL;
00596     double        * fwhms_x;
00597     double        * fwhms_y;
00598     double        * fwhms_sum_data;
00599     int             nb_val, nb_good;
00600     double          f_min, f_max;
00601     double          psigmas[] = {5.0, 2.0, 1.0, 0.5}; /* Not modified */
00602     const int       nsigmas = (int)(sizeof(psigmas)/sizeof(double));
00603     int             isigma;
00604     int             i;
00605 
00606     const double    pixscale = naco_pfits_get_pixscale(reflist);
00607     /* FIXME: Some hard-coded constants */
00608     const double    seeing_min_arcsec = 0.1;
00609     const double    seeing_max_arcsec = 5.0;
00610     const double    seeing_fwhm_var   = 0.2;
00611 
00612     double iq, fwhm_pix, fwhm_arcsec;
00613 
00614 
00615     /* Detect apertures */
00616     cpl_msg_info(cpl_func, "Detecting apertures using %d sigma-levels "
00617                  "ranging from %g down to %g", nsigmas, psigmas[0],
00618                  psigmas[nsigmas-1]);
00619 
00620     /* pixscale could be mising */
00621     skip_if( pixscale <= 0.0 );
00622     bug_if( seeing_min_arcsec < 0.0);
00623 
00624     /* Create the vector for the detection thresholds. (Not modified) */
00625     sigmas = cpl_vector_wrap(nsigmas, psigmas);
00626 
00627     aperts = cpl_apertures_extract(combined, sigmas, &isigma);
00628     if (aperts == NULL) {
00629         cpl_msg_warning(cpl_func, "Could not detect any apertures in combined "
00630                         "image");
00631         skip_if(1);
00632     }
00633     bug_if(0);
00634 
00635     /* Compute the FHWM of the detected apertures */
00636     fwhms = cpl_apertures_get_fwhm(combined, aperts);
00637     if (fwhms == NULL) {
00638         cpl_msg_warning(cpl_func, "Could not compute any FWHMs of the "
00639                           "apertures in the combined image");
00640         skip_if(1);
00641     }
00642     bug_if(0);
00643     cpl_apertures_delete(aperts);
00644     aperts = NULL;
00645 
00646     /* Access the data */
00647     nb_val  = cpl_bivector_get_size(fwhms);
00648     fwhms_x = cpl_bivector_get_x_data(fwhms);
00649     fwhms_y = cpl_bivector_get_y_data(fwhms);
00650 
00651     /* Get the number of good values */
00652     nb_good = 0;
00653     for (i=0 ; i < nb_val ; i++) {
00654         if (fwhms_x[i] <= 0.0 || fwhms_y[i] <= 0.0) continue;
00655         fwhms_x[nb_good] = fwhms_x[i];
00656         fwhms_y[nb_good] = fwhms_y[i];
00657         nb_good++;
00658     }
00659 
00660     cpl_msg_info(cpl_func, "Detected %d (%d) apertures at sigma=%g",
00661                  nb_good, nb_val, psigmas[isigma]);
00662 
00663     skip_if_lt (nb_good, 1, "aperture with a FWHM");
00664 
00665     nb_val = nb_good;
00666     /* This resizing is not really needed */
00667     bug_if(cpl_vector_set_size(cpl_bivector_get_x(fwhms), nb_val));
00668     bug_if(cpl_vector_set_size(cpl_bivector_get_y(fwhms), nb_val));
00669     fwhms_x = cpl_bivector_get_x_data(fwhms);
00670     fwhms_y = cpl_bivector_get_y_data(fwhms);
00671 
00672     /* Get the good values */
00673     fwhms_sum = cpl_vector_new(nb_good);
00674     fwhms_sum_data = cpl_vector_get_data(fwhms_sum);
00675     for (i=0; i < nb_good; i++) {
00676         fwhms_sum_data[i] = fwhms_x[i] + fwhms_y[i];
00677     }
00678     /* Compute the fwhm as the median of the average of the FHWMs in x and y */
00679     fwhm_pix = 0.5 * cpl_vector_get_median_const(fwhms_sum);
00680 
00681     bug_if(cpl_propertylist_append_double(qclist, "ESO QC FWHM PIX", fwhm_pix));
00682 
00683     fwhm_arcsec = fwhm_pix * pixscale;
00684 
00685     bug_if(cpl_propertylist_append_double(qclist, "ESO QC FWHM ARCSEC",
00686                                           fwhm_arcsec));
00687 
00688     /* iq is the median of the (fwhm_x+fwhm_y)/2 */
00689     /* of the good stars */
00690 
00691     /* Compute f_min and f_max */
00692     f_min = seeing_min_arcsec / pixscale;
00693     f_max = seeing_max_arcsec / pixscale;
00694 
00695     /* Sum the the good values */
00696     nb_good = 0;
00697     for (i=0 ; i < nb_val ; i++) {
00698         const double fx = fwhms_x[i];
00699         const double fy = fwhms_y[i];
00700 
00701         if (fx <= f_min || f_max <= fx || fy <= f_min || f_max <= fy) continue;
00702         if (fabs(fx-fy) >= 0.5 * (fx + fy) * seeing_fwhm_var) continue;
00703 
00704         fwhms_sum_data[nb_good] = fx + fy;
00705         nb_good++;
00706     }
00707 
00708     cpl_msg_info(cpl_func, "%d of the apertures have FWHMs within the range "
00709                  "%g to %g and eccentricity within %g", nb_good, f_min, f_max,
00710                  seeing_fwhm_var);
00711 
00712     skip_if_lt (nb_good, 1, "aperture with a good FWHM");
00713 
00714     bug_if(cpl_vector_set_size(fwhms_sum, nb_good));
00715 
00716     /* Compute the fwhm */
00717     iq = pixscale * 0.5 * cpl_vector_get_median(fwhms_sum); /* vector-permute */
00718 
00719     bug_if(cpl_propertylist_append_double(qclist, "ESO QC IQ", iq));
00720 
00721     end_skip;
00722 
00723     cpl_vector_delete(fwhms_sum);
00724     cpl_bivector_delete(fwhms);
00725     cpl_vector_unwrap(sigmas);
00726     cpl_apertures_delete(aperts);
00727 
00728     return cpl_error_get_code();
00729 }
00730 
00731 /*----------------------------------------------------------------------------*/
00742 /*----------------------------------------------------------------------------*/
00743 static cpl_error_code naco_img_jitter_save(cpl_frameset            * set,
00744                                            const cpl_parameterlist * parlist,
00745                                            const cpl_propertylist  * qclist,
00746                                            const cpl_propertylist  * paflist,
00747                                            const cpl_image         * combined,
00748                                            const cpl_image         * contrib)
00749 {
00750 
00751     bug_if (0);
00752     bug_if (contrib == NULL);
00753 
00754     /* Write the FITS file */
00755     skip_if (irplib_dfs_save_image(set, parlist, set, combined, CPL_BPP_IEEE_FLOAT,
00756                                RECIPE_STRING, NACO_IMG_JITTER_COMB, qclist, NULL,
00757                                naco_pipe_id, RECIPE_STRING CPL_DFS_FITS));
00758 
00759     /* Append the contribution map */
00760     skip_if (cpl_image_save(contrib, RECIPE_STRING CPL_DFS_FITS,
00761                             CPL_BPP_16_SIGNED, NULL, CPL_IO_EXTEND));
00762 
00763     skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist,
00764                              RECIPE_STRING CPL_DFS_PAF));
00765 
00766     end_skip;
00767 
00768     return cpl_error_get_code();
00769 
00770 }

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