Main Page   File List  

visir_img_phot.c

00001 /* $Id: visir_img_phot.c,v 1.99 2006/01/22 12:11:47 llundin Exp $
00002  *
00003  * This file is part of the VISIR 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: 2006/01/22 12:11:47 $
00024  * $Revision: 1.99 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_utils.h"
00040 #include "irplib_strehl.h"
00041 
00042 #include "visir_utils.h"
00043 #include "visir_pfits.h"
00044 #include "visir_dfs.h"
00045 #include "visir_inputs.h"
00046 
00047 /*-----------------------------------------------------------------------------
00048                                 Define   
00049  -----------------------------------------------------------------------------*/
00050 
00051 #define VISIR_IMG_PHOT_POS_UNCERTAINTY  10.0
00052 #define STREHL_M1                       8.0
00053 #define STREHL_M2                       1.1
00054 #define STREHL_BOX_SIZE                 64
00055 #define STREHL_STAR_RADIUS              2.0
00056 #define STREHL_BG_R1                    2.0
00057 #define STREHL_BG_R2                    3.0
00058 
00059 /*-----------------------------------------------------------------------------
00060                             Functions prototypes
00061  -----------------------------------------------------------------------------*/
00062 
00063 static int visir_img_phot_create(cpl_plugin *);
00064 static int visir_img_phot_exec(cpl_plugin *);
00065 static int visir_img_phot_destroy(cpl_plugin *);
00066 static int visir_img_phot(cpl_parameterlist *, cpl_frameset *);
00067 static double visir_img_phot_jy_from_cat(const char *, const char *, double,
00068                                          double, const char *);
00069 static int visir_img_phot_sensit(const cpl_image *, const cpl_frameset *,
00070                                  const char *);
00071 static int visir_img_phot_flux_three(const cpl_image *);
00072 static int visir_img_phot_flux_four(const cpl_image *);
00073 static int visir_img_phot_flux(const cpl_image *, double, double, int, int, int,
00074                                double *, double *, double *, double *, double*);
00075 static int visir_img_phot_save(const cpl_image *, const cpl_image *,
00076                                const cpl_parameterlist *, cpl_frameset *);
00077 
00078 /*-----------------------------------------------------------------------------
00079                             Static variables
00080  -----------------------------------------------------------------------------*/
00081 
00082 static const char * recipename = "visir_img_phot";
00083 
00084 static const int fits_strlen = FITS_STRLEN;
00085 
00086 static struct {
00087     /* Inputs */
00088     char        offsets[512];
00089     char        objects[512];
00090     char        nodding[512];
00091     int         rem_glitch;
00092     int         auto_bpm;
00093     int         purge_bad;
00094     int         refine;
00095     int         sx;
00096     int         sy;
00097     int         mx;
00098     int         my;
00099     int         union_flag;
00100     int         rej_low;
00101     int         rej_high;
00102     double      jy_val;
00103     int         r0_max;
00104     int         r1;
00105     int         r2;
00106     int         plot;
00107 
00108     /* Outputs */
00109     double      exptime;
00110     double      pscale;
00111     char        star_name[FITS_STRLEN];
00112     char        filter[FITS_STRLEN];
00113     double      bg_sigma;
00114     double      bg_mean;
00115     double      flux_snr;
00116     double      flux_snr_noise;
00117     double      flux_tot;
00118     double      fwhm_x;
00119     double      fwhm_y;
00120     double      fwhm_x_pos1;
00121     double      fwhm_y_pos1;
00122     double      fwhm_x_pos2;
00123     double      fwhm_y_pos2;
00124     double      fwhm_x_neg1;
00125     double      fwhm_y_neg1;
00126     double      fwhm_x_neg2;
00127     double      fwhm_y_neg2;
00128     double      sensitivity;
00129     double      conversion;
00130     double      strehl;
00131     double      strehl_err;
00132 } visir_img_phot_config;
00133 
00134 static char visir_img_phot_description[] =
00135 "This recipe computes the conversion factor and the sentitivity of a\n"
00136 "standard star. It uses a standard star catalog.\n" 
00137 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00138 "VISIR-std-star-observation.fits " VISIR_IMG_PHOT_RAW "\n"
00139 "VISIR-Imaging-Standard-Star-Catalog.fits " VISIR_IMA_STD_CAT_PROCATG "\n";
00140 
00141 /*-----------------------------------------------------------------------------
00142                                 Functions code
00143  -----------------------------------------------------------------------------*/
00144 
00145 /*----------------------------------------------------------------------------*/
00154 /*----------------------------------------------------------------------------*/
00155 int cpl_plugin_get_info(cpl_pluginlist * list)
00156 {
00157     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00158     cpl_plugin  *   plugin = &recipe->interface;
00159 
00160 
00161     if (cpl_plugin_init(plugin,
00162                     CPL_PLUGIN_API,
00163                     VISIR_BINARY_VERSION,
00164                     CPL_PLUGIN_TYPE_RECIPE,
00165                     recipename,
00166                     "Sensitivity computation",
00167                     visir_img_phot_description,
00168                     "Lars Lundin",
00169                     PACKAGE_BUGREPORT,
00170                     visir_get_license(),
00171                     visir_img_phot_create,
00172                     visir_img_phot_exec,
00173                     visir_img_phot_destroy)) return 1;
00174 
00175     if (cpl_pluginlist_append(list, plugin)) return 1;
00176     
00177     return 0;
00178 }
00179 
00180 /*----------------------------------------------------------------------------*/
00189 /*----------------------------------------------------------------------------*/
00190 static int visir_img_phot_create(cpl_plugin * plugin)
00191 {
00192     cpl_recipe * recipe = (cpl_recipe *)plugin;
00193 
00194     /* Verify plugin type */
00195     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00196 
00197     /* Create the parameters list in the cpl_recipe object */
00198     recipe->parameters = cpl_parameterlist_new();
00199 
00200     /* Fill the parameters list */
00201 
00202     visir_parameter_set_string(recipe->parameters, recipename,
00203                                VISIR_PARAM_XCORR, "10 10 15 15");
00204    
00205     return visir_parameter_set(recipe->parameters, recipename,
00206                                VISIR_PARAM_RADII | VISIR_PARAM_JYVAL |
00207                                VISIR_PARAM_REFINE |
00208                                VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
00209                                VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00210                                VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
00211                                VISIR_PARAM_UNION  | VISIR_PARAM_REJECT |
00212                                VISIR_PARAM_PLOT );
00213 
00214 }
00215 
00216 /*----------------------------------------------------------------------------*/
00222 /*----------------------------------------------------------------------------*/
00223 static int visir_img_phot_exec(cpl_plugin * plugin)
00224 {
00225     cpl_recipe * recipe = (cpl_recipe *)plugin;
00226 
00227     /* Verify plugin type */
00228     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00229 
00230     return visir_img_phot(recipe->parameters, recipe->frames);
00231 }
00232 
00233 /*----------------------------------------------------------------------------*/
00239 /*----------------------------------------------------------------------------*/
00240 static int visir_img_phot_destroy(cpl_plugin * plugin)
00241 {
00242     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00243 
00244     /* Verify plugin type */
00245     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00246     cpl_parameterlist_delete(recipe->parameters);
00247     return  0;
00248 }
00249 
00250 /*----------------------------------------------------------------------------*/
00257 /*----------------------------------------------------------------------------*/
00258 static int visir_img_phot(cpl_parameterlist   *   parlist, 
00259                           cpl_frameset        *   framelist)
00260 {
00261     cpl_parameter   *   par;
00262     const char      *   badpix;
00263     const char      *   star_cat;
00264     const char      *   flat;
00265     cpl_frameset    *   rawframes = NULL;
00266     cpl_image       **  combined = NULL;
00267     const char      *   sval;
00268 
00269 
00270     if (cpl_error_get_code()) return cpl_error_get_code();
00271    
00272     visir_img_phot_config.exptime = -1.0;
00273     visir_img_phot_config.pscale = -1.0;
00274     visir_img_phot_config.star_name[0] = (char)0;
00275     visir_img_phot_config.filter[0] = (char)0;
00276     visir_img_phot_config.bg_sigma = -1.0;
00277     visir_img_phot_config.bg_mean = -1.0;
00278     visir_img_phot_config.flux_snr = -1.0;
00279     visir_img_phot_config.flux_snr_noise = -1.0;
00280     visir_img_phot_config.flux_tot = -1.0;
00281     visir_img_phot_config.fwhm_x = -1.0;
00282     visir_img_phot_config.fwhm_y = -1.0;
00283     visir_img_phot_config.fwhm_x_pos1 = -1.0;
00284     visir_img_phot_config.fwhm_y_pos1 = -1.0;
00285     visir_img_phot_config.fwhm_x_pos2 = -1.0;
00286     visir_img_phot_config.fwhm_y_pos2 = -1.0;
00287     visir_img_phot_config.fwhm_x_neg1 = -1.0;
00288     visir_img_phot_config.fwhm_y_neg1 = -1.0;
00289     visir_img_phot_config.fwhm_x_neg2 = -1.0;
00290     visir_img_phot_config.fwhm_y_neg2 = -1.0;
00291     visir_img_phot_config.sensitivity = -1.0;
00292     visir_img_phot_config.conversion = -1.0;
00293     visir_img_phot_config.strehl = -1.0;
00294     visir_img_phot_config.strehl_err = -1.0;
00295 
00296     /* Retrieve input parameters */
00297     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.offsets");
00298     strcpy(visir_img_phot_config.offsets, cpl_parameter_get_string(par));
00299     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.objects");
00300     strcpy(visir_img_phot_config.objects, cpl_parameter_get_string(par));
00301     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.nodding");
00302     strcpy(visir_img_phot_config.nodding, cpl_parameter_get_string(par));
00303     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.auto_bpm");
00304     visir_img_phot_config.auto_bpm = cpl_parameter_get_bool(par);
00305     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.rem_glitch");
00306     visir_img_phot_config.rem_glitch = cpl_parameter_get_bool(par);
00307     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.purge_bad");
00308     visir_img_phot_config.purge_bad = cpl_parameter_get_bool(par);
00309     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.refine");
00310     visir_img_phot_config.refine = cpl_parameter_get_bool(par);
00311     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.xcorr");
00312     sval = cpl_parameter_get_string(par);
00313     skip_if (cpl_error_get_code());
00314 
00315     skip_if (sscanf(sval, "%d %d %d %d",
00316                     &visir_img_phot_config.sx,
00317                     &visir_img_phot_config.sy,
00318                     &visir_img_phot_config.mx,
00319                     &visir_img_phot_config.my) != 4);
00320 
00321     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.union");
00322     visir_img_phot_config.union_flag = cpl_parameter_get_bool(par);
00323     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.rej");
00324     sval = cpl_parameter_get_string(par);
00325     skip_if(sscanf(sval, "%d %d",
00326                     &visir_img_phot_config.rej_low,
00327                     &visir_img_phot_config.rej_high) != 2);
00328 
00329     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.jy_val");
00330     visir_img_phot_config.jy_val = cpl_parameter_get_double(par);
00331     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.radii");
00332     sval = cpl_parameter_get_string(par);
00333 
00334     skip_if (sscanf(sval, "%d %d %d",
00335                     &visir_img_phot_config.r0_max,
00336                     &visir_img_phot_config.r1,
00337                     &visir_img_phot_config.r2) != 3);
00338 
00339     par = cpl_parameterlist_find(parlist, "visir.visir_img_phot.plot");
00340     visir_img_phot_config.plot = cpl_parameter_get_int(par);
00341    
00342     /* Identify the RAW and CALIB frames in the input frameset */
00343     skip_if (visir_dfs_set_groups(framelist));
00344 
00345     /* Objects observation */
00346     rawframes = irplib_frameset_extract(framelist, VISIR_IMG_PHOT_RAW);
00347     skip_if (rawframes == NULL);
00348 
00349     skip_if(visir_dfs_check_frameset_tag(rawframes));
00350     
00351     /* Standard star catalog */
00352     star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_IMG);
00353     if (star_cat == NULL) {
00354          cpl_msg_error(__func__, "The input files has no star catalog tagged "
00355                        "%s", VISIR_CALIB_STDSTAR_IMG);
00356          visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
00357          skip_if(1);
00358     }
00359 
00360     /* Bad pixels calibration file */
00361     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00362 
00363     /* Flatfield calibration file */
00364     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00365 
00366   
00367     /* Combine the frames */
00368     cpl_msg_info(__func__, "Combining the images");
00369     combined = visir_img_recombine(rawframes, badpix, flat,
00370                                    visir_img_phot_config.offsets,
00371                                    visir_img_phot_config.objects,
00372                                    visir_img_phot_config.nodding,
00373                                    visir_img_phot_config.auto_bpm,
00374                                    visir_img_phot_config.rem_glitch,
00375                                    visir_img_phot_config.purge_bad,
00376                                    visir_img_phot_config.refine,
00377                                    visir_img_phot_config.sx,
00378                                    visir_img_phot_config.sy,
00379                                    visir_img_phot_config.mx,
00380                                    visir_img_phot_config.my,
00381                                    visir_img_phot_config.rej_low,
00382                                    visir_img_phot_config.rej_high,
00383                                    visir_img_phot_config.union_flag,
00384                                    CPL_FALSE, 0,0, 0,0,0,0,
00385                                    visir_img_phot_config.plot);
00386 
00387     if (combined == NULL)
00388         cpl_msg_error(__func__, "Could not combine the input frames");
00389   
00390     /* Compute the background values of the HCYCLE frames */
00391     visir_img_phot_config.bg_mean = visir_hcycle_background(rawframes, 0, 0);
00392 
00393     skip_if (cpl_error_get_code() );
00394 
00395     /* Compute here the sensitivity */
00396     cpl_msg_info(__func__, "Computing the sensitivity");
00397     if (visir_img_phot_sensit(combined[0], rawframes, star_cat)) {
00398         cpl_msg_error(__func__, "Could not compute sensitivity: '%s' in %s",
00399                         cpl_error_get_message(), cpl_error_get_where());
00400         skip_if(1);
00401     }
00402 
00403     /* Save the combined image and contribution map */
00404     cpl_msg_info(__func__, "Saving the combined image and contribution map");
00405     if (visir_img_phot_save(combined[0], combined[1], parlist, framelist)) {
00406         cpl_msg_error(__func__, "Could not save products");
00407         skip_if(1);
00408     }
00409 
00410     end_skip;
00411 
00412     cpl_frameset_delete(rawframes);
00413 
00414     if (combined) {
00415         cpl_image_delete(combined[0]);
00416         cpl_image_delete(combined[1]);
00417         cpl_free(combined);
00418     }
00419 
00420     return cpl_error_get_code();
00421 
00422 }
00423  
00424 /*----------------------------------------------------------------------------*/
00445 /*----------------------------------------------------------------------------*/
00446 static int visir_img_phot_sensit(
00447         const cpl_image *   combined,
00448         const cpl_frameset    *   rawframes,
00449         const char      *   star_cat) 
00450 {
00451     /* The filename of the reference frame */
00452     const char       * ref_file =
00453         cpl_frame_get_filename(cpl_frameset_get_first(rawframes));
00454     cpl_propertylist * plist = NULL;
00455     double             exptime;
00456     double             dit;
00457     int                ndit;
00458     int                ncycles, nnod;
00459     double             ra, dec;
00460     const char       * sval;
00461     double             f2;
00462 
00463 
00464     skip_if (cpl_error_get_code());
00465  
00466     plist = cpl_propertylist_load(ref_file, 0);
00467 
00468     skip_if (cpl_error_get_code());
00469 
00470     /* Get the total exposure time */
00471     /* DIT */
00472     dit = visir_pfits_get_dit(plist);
00473     /* NDIT */
00474     ndit = visir_pfits_get_ndit(plist);
00475     /* NNOD */
00476     nnod = cpl_frameset_get_size(rawframes);
00477     /* Number of chopping cycles */
00478     ncycles = visir_pfits_get_chop_ncycles(plist);
00479 
00480     /* Exptime * 2 because of chopping */
00481     exptime = 2 * dit * ndit * nnod * ncycles;
00482     visir_img_phot_config.exptime = exptime;
00483 
00484     if (exptime <= 0 || cpl_error_get_code()) {
00485         cpl_msg_error(__func__, "Illegal exposure time "
00486                       "(dit=%g:ndit=%d:ncycles=%d:nnod=%d): %g",
00487                       dit, ndit, ncycles, nnod, exptime);
00488         skip_if(1);
00489     }
00490 
00491     /* Copy the standard star name */
00492     skip_if ((sval = visir_pfits_get_starname(plist)) == NULL);
00493     (void) strncpy(visir_img_phot_config.star_name, sval, fits_strlen);
00494 
00495     /* Copy the filter name */
00496     skip_if ((sval = visir_pfits_get_filter(plist)) == NULL);
00497     (void)strncpy(visir_img_phot_config.filter, sval, fits_strlen);
00498    
00499     /* Get RA / DEC */
00500     ra  = visir_pfits_get_ra(plist);
00501     skip_if (cpl_error_get_code());
00502     dec = visir_pfits_get_dec(plist);
00503     skip_if (cpl_error_get_code());
00504 
00505     /* Get the pixel scale */
00506     sval = visir_pfits_get_pixscale(plist);
00507     skip_if (cpl_error_get_code());
00508 
00509     visir_img_phot_config.pscale = atof(sval);
00510     
00511     /* Get the JY value from the catalog if not user provided */
00512     if (visir_img_phot_config.jy_val < -998) {
00513         visir_img_phot_config.jy_val =
00514             visir_img_phot_jy_from_cat(star_cat, visir_img_phot_config.filter,
00515                                        ra,dec, visir_img_phot_config.star_name);
00516         skip_if (visir_img_phot_config.jy_val < -998);
00517     }
00518     
00519     /* Display the result */
00520     cpl_msg_info(__func__, "Star %s with filter %s : %g Jy",
00521             visir_img_phot_config.star_name,
00522             visir_img_phot_config.filter,
00523             visir_img_phot_config.jy_val);
00524    
00525     /* Compute the background signa */
00526     visir_img_phot_config.bg_sigma = visir_img_phot_sigma_clip(combined);
00527     skip_if (cpl_error_get_code());
00528     
00529     /* Get the flux and flux noise and fwhm */
00530     cpl_msg_info(__func__, "Compute the star flux");
00531     sval = visir_pfits_get_chopnod_dir(plist);
00532     if (sval == NULL) {
00533         cpl_msg_warning(__func__, "Could not get chopping direction: '%s' in %s",
00534                         cpl_error_get_message(), cpl_error_get_where());
00535         cpl_error_reset();
00536         if (visir_img_phot_flux_three(combined)) {
00537             cpl_msg_error(__func__, "Could not compute the flux");
00538             skip_if(1);
00539         }
00540     } else if (!strcmp(sval, "PARALLEL")) {
00541         /* 3 sources */
00542         if (visir_img_phot_flux_three(combined)) {
00543             cpl_msg_error(__func__, "Could not compute the flux");
00544             skip_if(1);
00545         }
00546     } else {
00547         /* Default is 4 sources */
00548         if (strcmp(sval, "PERPENDICULAR"))
00549             cpl_msg_warning(__func__, "Unknown chopping direction: %s", sval);
00550         if (visir_img_phot_flux_four(combined)) {
00551             cpl_msg_error(__func__, "Could not compute the flux");
00552             skip_if(1);
00553         }
00554     }
00555 
00556     /* Compute the sensitivity and the conversion factor */
00557 
00558     skip_if ( visir_img_phot_config.flux_snr == 0 );
00559     skip_if ( visir_img_phot_config.flux_snr_noise == 0 );
00560     skip_if ( visir_img_phot_config.jy_val == 0 );
00561 
00562     f2  = visir_img_phot_config.flux_snr / visir_img_phot_config.flux_snr_noise;
00563     f2 *= sqrt(3600/visir_img_phot_config.exptime);
00564     visir_img_phot_config.sensitivity = visir_img_phot_config.jy_val*1000*10/f2;
00565     visir_img_phot_config.conversion = visir_img_phot_config.flux_tot /
00566         visir_img_phot_config.jy_val;
00567 
00568     end_skip;
00569 
00570     cpl_msg_info(__func__, "Sensitivity : %g", visir_img_phot_config.sensitivity);
00571     cpl_msg_info(__func__, "Conversion  : %g", visir_img_phot_config.conversion);
00572     cpl_msg_info(__func__, "Strehl      : %g", visir_img_phot_config.strehl);
00573     cpl_msg_info(__func__, "Strehl error: %g", visir_img_phot_config.strehl_err);
00574 
00575     cpl_propertylist_delete(plist);
00576 
00577     return cpl_error_get_code();
00578 }
00579  
00580 /*----------------------------------------------------------------------------*/
00590 /*----------------------------------------------------------------------------*/
00591 static double visir_img_phot_jy_from_cat(
00592         const char * star_cat,
00593         const char * filter,
00594         double       ra,
00595         double       dec,
00596         const char * star_name) 
00597 {
00598     cpl_table    * tab = NULL;
00599     cpl_vector   * v_ra = NULL;
00600     cpl_vector   * v_dec = NULL;
00601     const char   * stdstar; 
00602     int            nb_stars;
00603     const double   max_radius = VISIR_STAR_MAX_RADIUS;
00604     double         value = -999;
00605     double         jy;
00606     double         dist;
00607     int            min_dist_ind;
00608 
00609 
00610     skip_if( cpl_error_get_code() );
00611     skip_if( star_cat  == NULL );
00612     skip_if( star_name == NULL );
00613     skip_if( filter    == NULL );
00614   
00615     /* Open the star catalog */
00616     if ((tab = cpl_table_load(star_cat, 1, 1)) == NULL) {
00617         cpl_msg_error(__func__, "Could not load the star catalog: %s",
00618                       star_cat);
00619         skip_if(1);
00620     }
00621 
00622     /* Check that the filter is in the table */
00623     if (!cpl_table_has_column(tab, filter)) {
00624         cpl_msg_error(__func__, "Catalog %s has no column for filter: %s",
00625                       star_cat, filter);
00626         visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
00627         skip_if(1);
00628     }
00629 
00630     if (!cpl_table_has_column(tab, "STARS")) {
00631         cpl_msg_error(__func__, "Catalog %s does not have a column labeled %s",
00632                       star_cat, "STARS");
00633         visir_error_set(CPL_ERROR_BAD_FILE_FORMAT);
00634         skip_if(1);
00635     }
00636 
00637     nb_stars = cpl_table_get_nrow(tab);
00638     skip_if (nb_stars < 1);
00639 
00640     /* Get the RA and DEC columns */
00641     v_ra = cpl_vector_wrap(nb_stars, cpl_table_get_data_double(tab, "RA"));
00642     skip_if( v_ra == NULL);
00643 
00644     v_dec = cpl_vector_wrap(nb_stars, cpl_table_get_data_double(tab, "DEC"));
00645     skip_if( v_dec == NULL);
00646 
00647     min_dist_ind = visir_star_find(v_ra, v_dec, ra, dec, max_radius, &dist);
00648 
00649     if (min_dist_ind < 0) {
00650         cpl_msg_error(__func__, "Observation target '%s' was not found among "
00651                       "the %d entries in the standard star catalog %s",
00652                       star_name, nb_stars, star_cat);
00653         skip_if(1);
00654     }
00655 
00656     stdstar = cpl_table_get_string(tab, "STARS", min_dist_ind);
00657     skip_if ( stdstar == NULL );
00658 
00659     jy = cpl_table_get(tab, filter, min_dist_ind, NULL);
00660     skip_if( cpl_error_get_code() );
00661 
00662     value = jy;
00663 
00664     if (strcmp(stdstar, star_name) != 0 && dist > 0.0) {
00665         /* The names do not match, nor does the location (exactly) */
00666         cpl_msg_warning(__func__, "The standard star '%s' at (RA,DEC)=(%g,%g) "
00667                         "in the FITS header is closest to the catalog star "
00668                         "'%s' at (RA,DEC)=(%g,%g) at distance %g degrees",
00669                         star_name, ra, dec, stdstar,
00670                         cpl_vector_get(v_ra,  min_dist_ind),
00671                         cpl_vector_get(v_dec, min_dist_ind), dist);
00672     } else if (dist > 0.0) {
00673         /* The names match, but the location does not (exactly) */
00674         cpl_msg_warning(__func__, "The location of the standard star '%s' in "
00675                         "the FITS header (RA,DEC)=(%g,%g) and the catalog "
00676                         "(RA,DEC)=(%g,%g) differ by %g degrees", stdstar, ra,
00677                         dec, cpl_vector_get(v_ra,  min_dist_ind),
00678                         cpl_vector_get(v_dec, min_dist_ind), dist);
00679     } else if (strcmp(stdstar, star_name) != 0) {
00680         /* The names do not match, but the location does */
00681         cpl_msg_warning(__func__, "The name of the standard star at (RA,DEC)="
00682                         "(%g,%g) in the FITS header '%s' and the catalog '%s' "
00683                         "are different", ra, dec, star_name, stdstar);
00684     } else {
00685         cpl_msg_info(__func__, "Standard star is '%s' at (RA, DEC)=(%g,%g)",
00686                      stdstar, ra, dec);
00687  
00688         if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
00689             cpl_table_dump(tab, min_dist_ind, 1, stdout);
00690     }
00691 
00692     cpl_msg_info(__func__, "The standard star '%s' has %g Jy through filter %s",
00693                  stdstar, value, filter);
00694  
00695     end_skip;
00696 
00697     cpl_table_delete(tab);
00698     cpl_vector_unwrap(v_ra);
00699     cpl_vector_unwrap(v_dec);
00700 
00701     return value;
00702 }
00703 
00704 /*----------------------------------------------------------------------------*/
00713 /*----------------------------------------------------------------------------*/
00714 static int visir_img_phot_flux_three(const cpl_image * combined)
00715 {
00716     cpl_image       *   min_combined = NULL;
00717     cpl_apertures   *   apert = NULL;
00718     cpl_vector      *   sigmas = NULL;
00719     double              psigmas[] = {5, 2, 1, 0.5};
00720     double              x[3];
00721     double              y[3];
00722     double              flux_snr[3];
00723     double              flux_snr_noise[3];
00724     double              flux_tot[3];
00725     double              fwhm_x[3];
00726     double              fwhm_y[3];
00727     double              dist1, dist2;
00728     int                 ngood_fwhm;
00729     double              lam, dlam;
00730     double              star_bg,star_peak,star_flux,psf_peak,psf_flux,bg_noise;
00731     int                 iflux2[2];
00732     int                 i;
00733 
00734 
00735     if (cpl_error_get_code()) return cpl_error_get_code();
00736 
00737     visir_assure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
00738 
00739     /* Detect where the POSITIVE star is */
00740     sigmas = cpl_vector_wrap(4, psigmas);
00741     apert = cpl_apertures_extract(combined, sigmas, NULL);
00742     if (apert == NULL || irplib_apertures_find_max_flux(apert, iflux2, 1)) {
00743         cpl_msg_error(__func__, "Could not detect the positive star");
00744         skip_if(1);
00745     }
00746 
00747     x[0] = cpl_apertures_get_centroid_x(apert, iflux2[0]);
00748     y[0] = cpl_apertures_get_centroid_y(apert, iflux2[0]);
00749     cpl_apertures_delete(apert);
00750     apert = NULL;
00751 
00752     if ((int)x[0]-IRPLIB_STREHL_BORDER <= 0 ||
00753         (int)y[0]-IRPLIB_STREHL_BORDER <= 0 ||
00754         (int)x[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_x(combined) || 
00755         (int)y[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_y(combined)) {
00756         cpl_msg_error(__func__, "Positive star at (%g,%g) is less than %d "
00757                       "pixels from the image border", x[0], y[0],
00758                       1+IRPLIB_STREHL_BORDER);
00759         skip_if(1);
00760     } else {
00761         cpl_msg_info(__func__, "Positive star at position %g %g", x[0], y[0]);
00762     }
00763 
00764     /* Detect where the NEGATIVE stars are */
00765     min_combined = cpl_image_multiply_scalar_create(combined, -1.0);
00766     apert = cpl_apertures_extract(min_combined, sigmas, NULL);
00767     if (apert == NULL) {
00768         cpl_msg_error(__func__, "Could not detect any negative stars");
00769         skip_if(1);
00770     }
00771     if (cpl_apertures_get_size(apert) < 2) {
00772         cpl_msg_error(__func__, "Could not detect 2 negative stars");
00773         skip_if(1);
00774     }
00775     skip_if (irplib_apertures_find_max_flux(apert, iflux2, 2));
00776 
00777     x[1] = cpl_apertures_get_centroid_x(apert, iflux2[0]);
00778     y[1] = cpl_apertures_get_centroid_y(apert, iflux2[0]);
00779     x[2] = cpl_apertures_get_centroid_x(apert, iflux2[1]);
00780     y[2] = cpl_apertures_get_centroid_y(apert, iflux2[1]);
00781     cpl_apertures_delete(apert);
00782     apert = NULL;
00783     cpl_msg_info(__func__, "Negative star 1 at position %g %g", x[1], y[1]);
00784     cpl_msg_info(__func__, "Negative star 2 at position %g %g", x[2], y[2]);
00785 
00786     /* Verify the stars positions */
00787     dist1 = sqrt((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]));
00788     dist2 = sqrt((x[2]-x[0])*(x[2]-x[0])+(y[2]-y[0])*(y[2]-y[0]));
00789     cpl_msg_info(__func__, "Star 1 Pos/Neg Distance: %g", dist1);
00790     cpl_msg_info(__func__, "Star 2 Pos/Neg Distance: %g", dist2);
00791     if (fabs(dist1-dist2) > VISIR_IMG_PHOT_POS_UNCERTAINTY) {
00792         cpl_msg_error(__func__, "Too large Pos/Neg Distance between the two "
00793                       "stars: %g > %g", fabs(dist1-dist2),
00794                       VISIR_IMG_PHOT_POS_UNCERTAINTY);
00795         skip_if(1);
00796     }
00797 
00798     /* Photometry on positive stars */
00799     skip_if (visir_img_phot_flux(combined,
00800             x[0], y[0],
00801             visir_img_phot_config.r0_max,
00802             visir_img_phot_config.r1,
00803             visir_img_phot_config.r2,
00804             &(flux_snr[0]),
00805             &(flux_snr_noise[0]),
00806             &(flux_tot[0]),
00807             &(fwhm_x[0]),
00808             &(fwhm_y[0])));
00809 
00810     /* Photometry on negative stars */
00811     for (i=1 ; i<3 ; i++)
00812         skip_if (visir_img_phot_flux(min_combined,
00813                 x[i], y[i],
00814                 visir_img_phot_config.r0_max,
00815                 visir_img_phot_config.r1,
00816                 visir_img_phot_config.r2,
00817                 &(flux_snr[i]),
00818                 &(flux_snr_noise[i]),
00819                 &(flux_tot[i]),
00820                 &(fwhm_x[i]),
00821                 &(fwhm_y[i])));
00822 
00823     cpl_image_delete(min_combined);
00824     min_combined = NULL;
00825     
00826     /* Compute the results */
00827     /* Flux */
00828     visir_img_phot_config.flux_snr = 0.0;
00829     for (i=0 ; i<3 ; i++) visir_img_phot_config.flux_snr += flux_snr[i];
00830 
00831     /* Flux noise */
00832     visir_img_phot_config.flux_snr_noise = 0.0;
00833     for (i=0 ; i<3 ; i++) visir_img_phot_config.flux_snr_noise +=
00834         flux_snr_noise[i]*flux_snr_noise[i];
00835     visir_img_phot_config.flux_snr_noise = 
00836         sqrt(visir_img_phot_config.flux_snr_noise);
00837 
00838     /* Total flux */
00839     visir_img_phot_config.flux_tot = 0.0;
00840     for (i=0 ; i<3 ; i++) visir_img_phot_config.flux_tot += flux_tot[i];
00841     
00842     /* FWHM */
00843     ngood_fwhm = 0;
00844     visir_img_phot_config.fwhm_x = 0.0;
00845     for (i=0 ; i<3 ; i++) {
00846         if (fwhm_x[i] > 0.0) {
00847             visir_img_phot_config.fwhm_x += fwhm_x[i];
00848             ngood_fwhm ++;
00849         }
00850     }
00851     if (ngood_fwhm > 0) visir_img_phot_config.fwhm_x /= ngood_fwhm;
00852     else visir_img_phot_config.fwhm_x = -1.0;
00853     ngood_fwhm = 0;
00854     visir_img_phot_config.fwhm_y = 0.0;
00855     for (i=0 ; i<3 ; i++) {
00856         if (fwhm_y[i] > 0.0) {
00857             visir_img_phot_config.fwhm_y += fwhm_y[i];
00858             ngood_fwhm ++;
00859         }
00860     }
00861     if (ngood_fwhm > 0) visir_img_phot_config.fwhm_y /= ngood_fwhm;
00862     else visir_img_phot_config.fwhm_y = -1.0;
00863     visir_img_phot_config.fwhm_x_pos1 = fwhm_x[0];
00864     visir_img_phot_config.fwhm_y_pos1 = fwhm_y[0];
00865     visir_img_phot_config.fwhm_x_neg1 = fwhm_x[1];
00866     visir_img_phot_config.fwhm_y_neg1 = fwhm_y[1];
00867     visir_img_phot_config.fwhm_x_neg2 = fwhm_x[2];
00868     visir_img_phot_config.fwhm_y_neg2 = fwhm_y[2];
00869 
00870     /* Get lam and dlam from the filter name for the Strehl computation */
00871     if (visir_get_filter_infos(visir_img_phot_config.filter, &lam, &dlam)) {
00872         cpl_msg_error(__func__, "Could not get info for filter: %s",
00873                 visir_img_phot_config.filter);
00874         skip_if(1);
00875     }
00876     
00877     /* Strehl computation */
00878     if (irplib_strehl_compute(combined, STREHL_M1, STREHL_M2, lam, dlam, 
00879                 visir_img_phot_config.pscale,
00880                 STREHL_BOX_SIZE, (int)x[0], (int)y[0], STREHL_STAR_RADIUS,
00881                 STREHL_BG_R1, STREHL_BG_R2, -1, -1, 
00882                 &(visir_img_phot_config.strehl), 
00883                 &(visir_img_phot_config.strehl_err),
00884                 &star_bg, &star_peak, &star_flux, &psf_peak, &psf_flux,
00885                 &bg_noise)) {
00886         cpl_msg_error(__func__, "Could not compute the strehl: '%s' in %s",
00887                         cpl_error_get_message(), cpl_error_get_where());
00888         skip_if(1);
00889     }
00890 
00891 
00892     end_skip;
00893 
00894     cpl_apertures_delete(apert);
00895     cpl_vector_unwrap(sigmas);
00896     cpl_image_delete(min_combined);
00897     
00898     return cpl_error_get_code();
00899 }
00900 
00901 /*----------------------------------------------------------------------------*/
00910 /*----------------------------------------------------------------------------*/
00911 static int visir_img_phot_flux_four(const cpl_image * combined)
00912 {
00913     cpl_image       *   min_combined = NULL;
00914     cpl_apertures   *   apert = NULL;
00915     cpl_vector      *   sigmas = NULL;
00916     double              psigmas[] = {5, 2, 1, 0.5};
00917     double              x[4];
00918     double              y[4];
00919     double              flux_snr[4];
00920     double              flux_snr_noise[4];
00921     double              flux_tot[4];
00922     double              fwhm_x[4];
00923     double              fwhm_y[4];
00924     double              dist1, dist2;
00925     int                 ngood_fwhm;
00926     double              lam, dlam;
00927     double              star_bg,star_peak,star_flux,psf_peak,psf_flux,bg_noise;
00928     int                 i;
00929     int                 iflux2[2];
00930 
00931 
00932     if (cpl_error_get_code()) return cpl_error_get_code();
00933 
00934     visir_assure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
00935 
00936     /* Detect where the POSITIVE stars are */
00937     sigmas = cpl_vector_wrap(4, psigmas);
00938     apert = cpl_apertures_extract(combined, sigmas, NULL);
00939     if (apert == NULL) {
00940         cpl_msg_error(__func__, "Could not detect any object");
00941         skip_if(1);
00942     }
00943     if (cpl_apertures_get_size(apert) < 2) {
00944         cpl_msg_error(__func__, "Could not detect 2 positive stars");
00945         skip_if(1);
00946     }
00947     skip_if (irplib_apertures_find_max_flux(apert, iflux2, 2));
00948 
00949     x[0] = cpl_apertures_get_centroid_x(apert, iflux2[0]);
00950     y[0] = cpl_apertures_get_centroid_y(apert, iflux2[0]);
00951     x[1] = cpl_apertures_get_centroid_x(apert, iflux2[1]);
00952     y[1] = cpl_apertures_get_centroid_y(apert, iflux2[1]);
00953     cpl_apertures_delete(apert);
00954     apert = NULL;
00955 
00956     if ((int)x[0]-IRPLIB_STREHL_BORDER <= 0 ||
00957         (int)y[0]-IRPLIB_STREHL_BORDER <= 0 ||
00958         (int)x[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_x(combined) || 
00959         (int)y[0]+IRPLIB_STREHL_BORDER > cpl_image_get_size_y(combined)) {
00960         cpl_msg_error(__func__, "Positive star 1 at (%g,%g) is less than %d "
00961                       "pixels from the image border", x[0], y[0],
00962                       1+IRPLIB_STREHL_BORDER);
00963         skip_if(1);
00964     } else {
00965         cpl_msg_info(__func__, "Positive star 1 at position %g %g", x[0], y[0]);
00966     }
00967     cpl_msg_info(__func__, "Positive star 2 at position %g %g", x[1], y[1]);
00968 
00969     /* Detect where the NEGATIVE stars are */
00970     min_combined = cpl_image_multiply_scalar_create(combined, -1.0);
00971     apert = cpl_apertures_extract(min_combined, sigmas, NULL);
00972     if (apert == NULL) {
00973         cpl_msg_error(__func__, "Could not detect any object");
00974         skip_if(1);
00975     }
00976     if (cpl_apertures_get_size(apert) < 2) {
00977         cpl_msg_error(__func__, "Could not detect 2 negative stars");
00978         skip_if(1);
00979     }
00980     skip_if (irplib_apertures_find_max_flux(apert, iflux2, 2));
00981     x[2] = cpl_apertures_get_centroid_x(apert, iflux2[0]);
00982     y[2] = cpl_apertures_get_centroid_y(apert, iflux2[0]);
00983     x[3] = cpl_apertures_get_centroid_x(apert, iflux2[1]);
00984     y[3] = cpl_apertures_get_centroid_y(apert, iflux2[1]);
00985     cpl_apertures_delete(apert);
00986     apert = NULL;
00987     cpl_msg_info(__func__, "Negative star 1 at position %g %g", x[2], y[2]);
00988     cpl_msg_info(__func__, "Negative star 2 at position %g %g", x[3], y[3]);
00989 
00990     /* Verify the stars positions */
00991     dist1 = sqrt((x[2]-x[0])*(x[2]-x[0])+(y[2]-y[0])*(y[2]-y[0]));
00992     dist2 = sqrt((x[3]-x[1])*(x[3]-x[1])+(y[3]-y[1])*(y[3]-y[1]));
00993     cpl_msg_info(__func__, "Star 1 Pos/Neg Distance: %g", dist1);
00994     cpl_msg_info(__func__, "Star 2 Pos/Neg Distance: %g", dist2);
00995     if (fabs(dist1-dist2) > VISIR_IMG_PHOT_POS_UNCERTAINTY) {
00996         cpl_msg_error(__func__, "Too large Pos/Neg Distance between the two "
00997                       "stars: %g > %g", fabs(dist1-dist2),
00998                       VISIR_IMG_PHOT_POS_UNCERTAINTY);
00999         skip_if(1);
01000     }
01001 
01002     /* Photometry on positive stars */
01003     for (i=0 ; i<2 ; i++)
01004         skip_if (visir_img_phot_flux(combined,
01005                 x[i], y[i],
01006                 visir_img_phot_config.r0_max,
01007                 visir_img_phot_config.r1,
01008                 visir_img_phot_config.r2,
01009                 &(flux_snr[i]),
01010                 &(flux_snr_noise[i]),
01011                 &(flux_tot[i]),
01012                 &(fwhm_x[i]),
01013                 &(fwhm_y[i])));
01014 
01015     /* Photometry on negative stars */
01016     for (i=2 ; i<4 ; i++)
01017         skip_if (visir_img_phot_flux(min_combined,
01018                 x[i], y[i],
01019                 visir_img_phot_config.r0_max,
01020                 visir_img_phot_config.r1,
01021                 visir_img_phot_config.r2,
01022                 &(flux_snr[i]),
01023                 &(flux_snr_noise[i]),
01024                 &(flux_tot[i]),
01025                 &(fwhm_x[i]),
01026                 &(fwhm_y[i])));
01027 
01028     cpl_image_delete(min_combined);
01029     min_combined = NULL;
01030     
01031     /* Compute the results */
01032     /* Flux */
01033     visir_img_phot_config.flux_snr = 0.0;
01034     for (i=0 ; i<4 ; i++) visir_img_phot_config.flux_snr += flux_snr[i];
01035 
01036     /* Flux noise */
01037     visir_img_phot_config.flux_snr_noise = 0.0;
01038     for (i=0 ; i<4 ; i++) visir_img_phot_config.flux_snr_noise +=
01039         flux_snr_noise[i]*flux_snr_noise[i];
01040     visir_img_phot_config.flux_snr_noise = 
01041         sqrt(visir_img_phot_config.flux_snr_noise);
01042 
01043     /* Total flux */
01044     visir_img_phot_config.flux_tot = 0.0;
01045     for (i=0 ; i<4 ; i++) visir_img_phot_config.flux_tot += flux_tot[i];
01046     
01047     /* FWHM */
01048     ngood_fwhm = 0;
01049     visir_img_phot_config.fwhm_x = 0.0;
01050     for (i=0 ; i<4 ; i++) {
01051         if (fwhm_x[i] > 0.0) {
01052             visir_img_phot_config.fwhm_x += fwhm_x[i];
01053             ngood_fwhm ++;
01054         }
01055     }
01056     if (ngood_fwhm > 0) visir_img_phot_config.fwhm_x /= ngood_fwhm;
01057     else visir_img_phot_config.fwhm_x = -1.0;
01058     ngood_fwhm = 0;
01059     visir_img_phot_config.fwhm_y = 0.0;
01060     for (i=0 ; i<4 ; i++) {
01061         if (fwhm_y[i] > 0.0) {
01062             visir_img_phot_config.fwhm_y += fwhm_y[i];
01063             ngood_fwhm ++;
01064         }
01065     }
01066     if (ngood_fwhm > 0) visir_img_phot_config.fwhm_y /= ngood_fwhm;
01067     else visir_img_phot_config.fwhm_y = -1.0;
01068     visir_img_phot_config.fwhm_x_pos1 = fwhm_x[0];
01069     visir_img_phot_config.fwhm_y_pos1 = fwhm_y[0];
01070     visir_img_phot_config.fwhm_x_pos2 = fwhm_x[1];
01071     visir_img_phot_config.fwhm_y_pos2 = fwhm_y[1];
01072     visir_img_phot_config.fwhm_x_neg1 = fwhm_x[2];
01073     visir_img_phot_config.fwhm_y_neg1 = fwhm_y[2];
01074     visir_img_phot_config.fwhm_x_neg2 = fwhm_x[3];
01075     visir_img_phot_config.fwhm_y_neg2 = fwhm_y[3];
01076 
01077     /* Get lam and dlam from the filter name for the Strehl computation */
01078     if (visir_get_filter_infos(visir_img_phot_config.filter, &lam, &dlam)) {
01079         cpl_msg_error(__func__, "Could not get info for filter: %s",
01080                 visir_img_phot_config.filter);
01081         skip_if(1);
01082     }
01083     
01084     /* Strehl computation */
01085     if (irplib_strehl_compute(combined, STREHL_M1, STREHL_M2, lam, dlam, 
01086                 visir_img_phot_config.pscale,
01087                 STREHL_BOX_SIZE, (int)x[0], (int)y[0], STREHL_STAR_RADIUS,
01088                 STREHL_BG_R1, STREHL_BG_R2, -1, -1, 
01089                 &(visir_img_phot_config.strehl), 
01090                 &(visir_img_phot_config.strehl_err),
01091                 &star_bg, &star_peak, &star_flux, &psf_peak, &psf_flux,
01092                 &bg_noise)) {
01093         cpl_msg_error(__func__, "Could not compute the strehl: '%s' in %s",
01094                         cpl_error_get_message(), cpl_error_get_where());
01095         skip_if(1);
01096     }
01097     
01098 
01099     end_skip;
01100 
01101     cpl_apertures_delete(apert);
01102     cpl_vector_unwrap(sigmas);
01103     cpl_image_delete(min_combined);
01104     
01105     return cpl_error_get_code();
01106 
01107 }
01108  
01109 /*----------------------------------------------------------------------------*/
01128 /*----------------------------------------------------------------------------*/
01129 static int visir_img_phot_flux(
01130         const cpl_image * combined,
01131         double          x_pos,
01132         double          y_pos,
01133         int             r0_max,
01134         int             r1,
01135         int             r2,
01136         double      *   flux_snr,
01137         double      *   flux_snr_noise,
01138         double      *   flux_tot,
01139         double      *   fwhm_x,
01140         double      *   fwhm_y)
01141 {
01142     cpl_apertures   *   apert = NULL;
01143     cpl_image       *   labels;
01144     cpl_vector      *   r0 = NULL;
01145     cpl_vector      *   fl = NULL;
01146     cpl_vector      *   fl_noise = NULL;
01147     cpl_vector      *   snr = NULL;
01148     double              bg, fl_val, max_val;
01149     int                 max_ind;
01150     int                 i;
01151     int                 nx, ny;
01152 
01153 
01154     if (cpl_error_get_code()) return cpl_error_get_code();
01155 
01156     visir_assure_code(combined != NULL, CPL_ERROR_NULL_INPUT);
01157     visir_assure_code(r0_max > 0,       CPL_ERROR_ILLEGAL_INPUT);
01158 
01159     nx = cpl_image_get_size_x(combined);
01160     ny = cpl_image_get_size_y(combined);
01161 
01162     /* Create the label image defining the background ring */
01163     if ((labels = visir_create_ring_intimage(nx, ny, 
01164             (int)x_pos, (int)y_pos, r1, r2)) == NULL) {
01165         cpl_msg_error(__func__, "Could not create a ring image");
01166         skip_if(1);
01167     }
01168     /* Compute the background */
01169     apert = cpl_apertures_new_from_image(combined, labels);
01170     cpl_image_delete(labels);
01171     labels = NULL;
01172     bg = cpl_apertures_get_median(apert, 1);
01173     cpl_apertures_delete(apert);
01174     apert = NULL;
01175     cpl_msg_info(__func__, "Background : %g", bg);
01176    
01177     /* Create the label image defining the total star disk */
01178     if ((labels = visir_create_disk_intimage(nx, ny, 
01179             (int)x_pos, (int)y_pos, r0_max)) == NULL) {
01180         cpl_msg_error(__func__, "Could not create a disk image");
01181         skip_if(1);
01182     }
01183     /* Compute the total flux and the associated error */
01184     apert = cpl_apertures_new_from_image(combined, labels);
01185     cpl_image_delete(labels);
01186     labels = NULL;
01187     *flux_tot = cpl_apertures_get_flux(apert, 1);
01188     *flux_tot -= cpl_apertures_get_npix(apert, 1) * bg;
01189     cpl_apertures_delete(apert);
01190     apert = NULL;
01191     cpl_msg_info(__func__, "Star total flux (error): %g", *flux_tot);
01192     
01193     /* Create and fill r0 */
01194     r0 = cpl_vector_new(r0_max);
01195     for (i=0 ; i<r0_max ; i++) cpl_vector_set(r0, i, i+1);
01196 
01197     /* Create fl, fl_noise */
01198     fl = cpl_vector_new(r0_max);
01199     fl_noise = cpl_vector_new(r0_max);
01200     
01201     /* For each radius, compute fl and fl_noise */
01202     for (i=0 ; i<r0_max ; i++) {
01203         /* Create the label image defining the current star disk */
01204         if ((labels = visir_create_disk_intimage(nx, ny, 
01205                 (int)x_pos, (int)y_pos, (int)cpl_vector_get(r0, i))) == NULL) {
01206             cpl_msg_error(__func__, "Could not create a disk image: %d", i);
01207             break;
01208         }
01209         /* Compute the statistics on the zone defined by the labels */
01210         apert = cpl_apertures_new_from_image(combined, labels);
01211         /* FIXME: apert == NULL ? */
01212         cpl_image_delete(labels);
01213         labels = NULL;
01214         fl_val = cpl_apertures_get_flux(apert, 1);
01215         fl_val -= cpl_apertures_get_npix(apert, 1) * bg;
01216         cpl_vector_set(fl, i, fl_val);
01217         cpl_vector_set(fl_noise, i, visir_img_phot_config.bg_sigma * 
01218                 sqrt((double)cpl_apertures_get_npix(apert, 1)));
01219         cpl_apertures_delete(apert);
01220         apert = NULL;
01221     }
01222     skip_if( cpl_error_get_code() );
01223 
01224     /* Compute the flux (and error) for the best signal to noise */
01225     snr = cpl_vector_duplicate(fl);
01226     cpl_vector_divide(snr, fl_noise);
01227     for (i=0 ; i<r0_max ; i++) {
01228         if (i == 0 || max_val < cpl_vector_get(snr, i)) {
01229             max_val = cpl_vector_get(snr, i);
01230             max_ind = i;
01231         }
01232     }
01233     *flux_snr = cpl_vector_get(fl, max_ind);
01234     *flux_snr_noise = cpl_vector_get(fl_noise, max_ind);
01235     cpl_msg_info(__func__, "Best SNR star flux : %g for radius %g",
01236             *flux_snr, cpl_vector_get(r0, max_ind));
01237     
01238     /* Compute the FWHM */
01239     skip_if (cpl_image_get_fwhm(combined, (int)x_pos, (int)y_pos, fwhm_x,
01240                                 fwhm_y));
01241 
01242     cpl_msg_info(__func__, "FWHM : %g in x ; %g in y ", *fwhm_x, *fwhm_y);
01243 
01244     end_skip;
01245 
01246     cpl_apertures_delete(apert);
01247     cpl_image_delete(labels);
01248 
01249     cpl_vector_delete(r0);
01250     cpl_vector_delete(fl);
01251     cpl_vector_delete(snr);
01252     cpl_vector_delete(fl_noise);
01253 
01254     return cpl_error_get_code();
01255 }
01256  
01257 /*----------------------------------------------------------------------------*/
01266 /*----------------------------------------------------------------------------*/
01267 static int visir_img_phot_save(
01268         const cpl_image         * combined,
01269         const cpl_image         * contrib,
01270         const cpl_parameterlist * parlist,
01271         cpl_frameset            * set)
01272 {
01273     /* The filename of the reference frame */
01274     const char       * ref_file =
01275         cpl_frame_get_filename(irplib_frameset_get_first_from_group(set,
01276                                                          CPL_FRAME_GROUP_RAW));
01277     cpl_propertylist * plist = cpl_propertylist_load(ref_file, 0);
01278     cpl_propertylist * qclist = cpl_propertylist_new();
01279     const char       * capa = visir_get_capa(plist);
01280     const char       * pafcopy = "^(DATE-OBS|ARCFILE|ESO TPL ID|ESO DET DIT"
01281                                   "|MJD-OBS|ESO INS PFOV|ESO TEL AIRM START)$";
01282     FILE             * paf = NULL;
01283 
01284 
01285     /* This will catch ref_file == NULL or plist == NULL */
01286     skip_if (cpl_error_get_code());
01287 
01288     skip_if (cpl_propertylist_copy_property_regexp(qclist, plist, pafcopy));
01289 
01290     /* THE PAF FILE FOR QC PARAMETERS */
01291     skip_if (!(paf = visir_paf_init(recipename)));
01292 
01293     skip_if (irplib_propertylist_dump_paf(qclist, paf));
01294 
01295     /* Reuse the propertylist for QC parameters */
01296     cpl_propertylist_empty(qclist);
01297 
01298     cpl_propertylist_empty(plist); /* Just to reduce number of pointers */
01299 
01300     /* QC.EXPTIME */
01301     skip_if (cpl_propertylist_append_double(qclist, "ESO QC EXPTIME", 
01302             visir_img_phot_config.exptime));
01303     /* QC.JYVAL */
01304     skip_if (cpl_propertylist_append_double(qclist, "ESO QC JYVAL", 
01305             visir_img_phot_config.jy_val));
01306     /* QC.STARNAME */
01307     skip_if (cpl_propertylist_append_string(qclist, "ESO QC STARNAME",
01308             visir_img_phot_config.star_name));
01309     /* QC.FILTER */
01310     skip_if (cpl_propertylist_append_string(qclist, "ESO QC FILTER",
01311             visir_img_phot_config.filter));
01312     /* QC.BACKGD.MEAN */
01313     skip_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN",
01314             visir_img_phot_config.bg_mean));
01315     /* QC.BACKGD.SIGMA */
01316     skip_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD SIGMA",
01317             visir_img_phot_config.bg_sigma));
01318     /* QC.FLUXTOT */
01319     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FLUXTOT",
01320             visir_img_phot_config.flux_tot));
01321     /* QC.FLUXSNR */
01322     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FLUXSNR",
01323             visir_img_phot_config.flux_snr));
01324     /* QC.FLUXSNR.NOISE */
01325     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FLUXSNR NOISE",
01326             visir_img_phot_config.flux_snr_noise));
01327     /* QC.FWHMX */
01328     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX",
01329             visir_img_phot_config.fwhm_x));
01330     /* QC.FWHMY */
01331     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY",
01332             visir_img_phot_config.fwhm_y));
01333     /* QC.FWHMX.POS1 */
01334     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX POS1",
01335             visir_img_phot_config.fwhm_x_pos1));
01336     /* QC.FWHMY.POS1 */
01337     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY POS1",
01338             visir_img_phot_config.fwhm_y_pos1));
01339     /* QC.FWHMX.POS2 */
01340     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX POS2",
01341             visir_img_phot_config.fwhm_x_pos2));
01342     /* QC.FWHMY.POS2 */
01343     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY POS2",
01344             visir_img_phot_config.fwhm_y_pos2));
01345     /* QC.FWHMX.NEG1 */
01346     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX NEG1",
01347             visir_img_phot_config.fwhm_x_neg1));
01348     /* QC.FWHMY.NEG1 */
01349     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY NEG1",
01350             visir_img_phot_config.fwhm_y_neg1));
01351     /* QC.FWHMX.NEG2 */
01352     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMX NEG2",
01353             visir_img_phot_config.fwhm_x_neg2));
01354     /* QC.FWHMY.NEG2 */
01355     skip_if (cpl_propertylist_append_double(qclist, "ESO QC FWHMY NEG2",
01356             visir_img_phot_config.fwhm_y_neg2));
01357     /* QC.SENSIT */
01358     skip_if (cpl_propertylist_append_double(qclist, "ESO QC SENSIT",
01359             visir_img_phot_config.sensitivity));
01360     /* QC.CONVER */
01361     skip_if (cpl_propertylist_append_double(qclist, "ESO QC CONVER",
01362             visir_img_phot_config.conversion));
01363     /* QC.STREHL */
01364     skip_if (cpl_propertylist_append_double(qclist, "ESO QC STREHL",
01365             visir_img_phot_config.strehl));
01366     /* QC.STREHL.ERROR */
01367     skip_if (cpl_propertylist_append_double(qclist, "ESO QC STREHL ERROR",
01368             visir_img_phot_config.strehl_err));
01369 
01370     /* QC.CAPA */
01371     skip_if (capa != NULL && 
01372              cpl_propertylist_append_string(qclist, "ESO QC CAPA", capa));
01373 
01374     /* SAVE THE COMBINED IMAGE */
01375     skip_if (visir_image_save(parlist, set, combined, recipename,
01376                               VISIR_IMG_PHOT_COMBINED_PROCATG,
01377                               qclist, NULL));
01378 
01379     /* THE CONTRIBUTION MAP */
01380     skip_if (visir_image_save(parlist, set, contrib,
01381                               recipename,
01382                               VISIR_IMG_PHOT_CONTRIB_PROCATG,
01383                               NULL, "_contrib"));
01384 
01385     skip_if (irplib_propertylist_dump_paf(qclist, paf));
01386 
01387     end_skip;
01388 
01389     visir_paf_end(paf);
01390 
01391     cpl_propertylist_delete(qclist);
01392     cpl_propertylist_delete(plist);
01393 
01394     return cpl_error_get_code();
01395 }

Generated on Mon Jan 23 12:16:36 2006 for VISIR Pipeline Reference Manual by doxygen1.2.18