vircam_mesostep_analyse.c

00001 /* $Id: vircam_mesostep_analyse.c,v 1.18 2008/05/06 12:15:20 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2008/05/06 12:15:20 $
00024  * $Revision: 1.18 $
00025  * $Name:  $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <stdio.h>
00035 #include <math.h>
00036 #include <string.h>
00037 #include <cpl.h>
00038 
00039 #include "vircam_utils.h"
00040 #include "vircam_mask.h"
00041 #include "vircam_pfits.h"
00042 #include "vircam_dfs.h"
00043 #include "vircam_mods.h"
00044 #include "vircam_fits.h"
00045 #include "vircam_tfits.h"
00046 #include "vircam_jmp_utils.h"
00047 
00048 /* Function prototypes */
00049 
00050 static int vircam_mesostep_analyse_create(cpl_plugin *);
00051 static int vircam_mesostep_analyse_exec(cpl_plugin *);
00052 static int vircam_mesostep_analyse_destroy(cpl_plugin *);
00053 static int vircam_mesostep_analyse(cpl_parameterlist *, cpl_frameset *);
00054 static cpl_propertylist *vircam_mesostep_analyse_dummyqc(int type);
00055 
00056 
00057 static char vircam_mesostep_analyse_description[] =
00058 "vircam_mesostep_analyse -- VIRCAM mesostep processing recipe.\n\n"
00059 "Process a complete mesostep sequence of vircam data. Remove instrumental\n"
00060 "signature and sky subtract if desired. Work out the illumination correction\n"
00061 "for all of the input frames and then smooth the result by fitting a 2d\n"
00062 "polynomial. Evaluate the polynomial at the grid points to form the final\n"
00063 "illumination correction data product\n"
00064 "The program accepts the following files in the SOF:\n\n"
00065 "    Tag                   Description\n"
00066 "    -----------------------------------------------------------------------\n"
00067 "    %-21s A list of raw science images\n"
00068 "    %-21s A master dark frame\n"
00069 "    %-21s A master twilight flat frame\n"
00070 "    %-21s A channel table\n"
00071 "    %-21s A photometric calibration table\n"
00072 "    %-21s A master confidence map or\n"
00073 "    %-21s A master bad pixel mask\n"
00074 "    %-21s A master standard star index\n"
00075 "All of the above are required\n"
00076 "\n";
00077 
00155 /* Function code */
00156 
00157 /*---------------------------------------------------------------------------*/
00165 /*---------------------------------------------------------------------------*/
00166 
00167 int cpl_plugin_get_info(cpl_pluginlist *list) {
00168     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00169     cpl_plugin  *plugin = &recipe->interface;
00170     char alldesc[SZ_ALLDESC];
00171     (void)snprintf(alldesc,SZ_ALLDESC,vircam_mesostep_analyse_description,
00172                    VIRCAM_ILLUM_RAW,VIRCAM_CAL_DARK,VIRCAM_CAL_TWILIGHT_FLAT,
00173                    VIRCAM_CAL_CHANTAB,VIRCAM_CAL_PHOTTAB,VIRCAM_CAL_CONF,
00174                    VIRCAM_CAL_BPM,VIRCAM_CAL_2MASS);
00175 
00176     cpl_plugin_init(plugin,
00177                     CPL_PLUGIN_API,
00178                     VIRCAM_BINARY_VERSION,
00179                     CPL_PLUGIN_TYPE_RECIPE,
00180                     "vircam_mesostep_analyse",
00181                     "VIRCAM mesostep analysis recipe",
00182                     alldesc,
00183                     "Jim Lewis",
00184                     "jrl@ast.cam.ac.uk",
00185                     vircam_get_license(),
00186                     vircam_mesostep_analyse_create,
00187                     vircam_mesostep_analyse_exec,
00188                     vircam_mesostep_analyse_destroy);
00189 
00190     cpl_pluginlist_append(list,plugin);
00191 
00192     return(0);
00193 }
00194 
00195 /*---------------------------------------------------------------------------*/
00204 /*---------------------------------------------------------------------------*/
00205 
00206 static int vircam_mesostep_analyse_create(cpl_plugin *plugin) {
00207     cpl_recipe      *recipe;
00208     cpl_parameter   *p;
00209 
00210     /* Get the recipe out of the plugin */
00211 
00212     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00213         recipe = (cpl_recipe *)plugin;
00214     else 
00215         return(-1);
00216 
00217     /* Create the parameters list in the cpl_recipe object */
00218 
00219     recipe->parameters = cpl_parameterlist_new();
00220 
00221     /* Fill in the minimum object size */
00222 
00223     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.ipix",
00224                                 CPL_TYPE_INT,
00225                                 "Minimum pixel area for each detected object",
00226                                 "vircam.vircam_mesostep_analyse",5);
00227     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
00228     cpl_parameterlist_append(recipe->parameters,p);
00229 
00230     /* Fill in the detection threshold parameter */
00231 
00232     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.thresh",
00233                                 CPL_TYPE_DOUBLE,
00234                                 "Detection threshold in sigma above sky",
00235                                 "vircam.vircam_mesostep_analyse",2.0);
00236     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
00237     cpl_parameterlist_append(recipe->parameters,p);
00238 
00239     /* Fill in flag to use deblending software or not */
00240 
00241     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.icrowd",
00242                                 CPL_TYPE_BOOL,"Use deblending?",
00243                                 "vircam.vircam_mesostep_analyse",0);
00244     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
00245     cpl_parameterlist_append(recipe->parameters,p);
00246 
00247     /* Fill in core radius */
00248 
00249     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.rcore",
00250                                 CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
00251                                 "vircam.vircam_mesostep_analyse",4.0);
00252     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
00253     cpl_parameterlist_append(recipe->parameters,p);
00254 
00255     /* Fill in background smoothing box size */
00256 
00257     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.nbsize",
00258                                 CPL_TYPE_INT,"Background smoothing box size",
00259                                 "vircam.vircam_mesostep_analyse",64);
00260     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
00261     cpl_parameterlist_append(recipe->parameters,p);
00262 
00263     /* Fill in flag to destripe the images */
00264 
00265     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.destripe",
00266                                 CPL_TYPE_BOOL,"Destripe images?",
00267                                 "vircam.vircam_mesostep_analyse",1);
00268     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"destripe");
00269     cpl_parameterlist_append(recipe->parameters,p);
00270 
00271     /* Fill in flag to correct sky background */
00272 
00273     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.skycor",
00274                                 CPL_TYPE_BOOL,"Sky correct images?",
00275                                 "vircam.vircam_mesostep_analyse",1);
00276     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"skycor");
00277     cpl_parameterlist_append(recipe->parameters,p);
00278 
00279     /* Order for surface fit */
00280 
00281     p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.nord",
00282                                 CPL_TYPE_INT,
00283                                 "Polynomial order for surface fit",
00284                                 "vircam.vircam_mesostep_analyse",3);
00285     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nord");
00286     cpl_parameterlist_append(recipe->parameters,p);
00287 
00288     /* Extension number of input frames to use */
00289 
00290     p = cpl_parameter_new_range("vircam.vircam_mesostep_analyse.extenum",
00291                                 CPL_TYPE_INT,
00292                                 "Extension number to be done, 0 == all",
00293                                 "vircam.vircam_mesostep_analyse",
00294                                 1,0,16);
00295     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00296     cpl_parameterlist_append(recipe->parameters,p);
00297         
00298     /* Get out of here */
00299 
00300     return(0);
00301 }
00302         
00303 /*---------------------------------------------------------------------------*/
00309 /*---------------------------------------------------------------------------*/
00310 
00311 static int vircam_mesostep_analyse_exec(cpl_plugin *plugin) {
00312     cpl_recipe  *recipe;
00313 
00314     /* Get the recipe out of the plugin */
00315 
00316     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00317         recipe = (cpl_recipe *)plugin;
00318     else 
00319         return(-1);
00320 
00321     return(vircam_mesostep_analyse(recipe->parameters,recipe->frames));
00322 }
00323                                 
00324 /*---------------------------------------------------------------------------*/
00330 /*---------------------------------------------------------------------------*/
00331 
00332 static int vircam_mesostep_analyse_destroy(cpl_plugin *plugin) {
00333     cpl_recipe *recipe ;
00334 
00335     /* Get the recipe out of the plugin */
00336 
00337     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00338         recipe = (cpl_recipe *)plugin;
00339     else 
00340         return(-1);
00341 
00342     cpl_parameterlist_delete(recipe->parameters);
00343     return(0);
00344 }
00345 
00346 /*---------------------------------------------------------------------------*/
00353 /*---------------------------------------------------------------------------*/
00354 
00355 static int vircam_mesostep_analyse(cpl_parameterlist *parlist, 
00356                                    cpl_frameset *framelist) {
00357     const char *fctid="vircam_mesostep_analyse";
00358     cpl_parameter *p;
00359     cpl_polynomial *poly;
00360     int nlab,jst,jfn,status,isconf,j,i,live,nrows,n,ndit;
00361     float *x1,*x2,*y1,*y2,*ill,gaincor_fac;
00362     double *bv_x,*bv_y,*vdata,val;
00363     cpl_bivector *bv;
00364     cpl_vector *v;
00365     vir_fits *ff;
00366     cpl_table *ic;
00367     cpl_frame *catindex;
00368     cpl_propertylist *pp;
00369 
00370     /* Check validity of input frameset */
00371 
00372     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00373         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00374         return(-1);
00375     }
00376 
00377     /* Initialise some things */
00378 
00379     vircam_jmp_init();
00380     (void)strncpy(vircam_recipename,fctid,VIRCAM_PATHSZ);
00381     (void)snprintf(vircam_recipepaf,VIRCAM_PATHSZ,"VIRCAM/%s",fctid);
00382     recflag = RECMES;
00383 
00384     /* Get the parameters */
00385 
00386     p = cpl_parameterlist_find(parlist,
00387                                "vircam.vircam_mesostep_analyse.ipix");
00388     vircam_jmp_config.ipix = cpl_parameter_get_int(p);
00389     p = cpl_parameterlist_find(parlist,
00390                                "vircam.vircam_mesostep_analyse.thresh");
00391     vircam_jmp_config.threshold = (float)cpl_parameter_get_double(p);
00392     p = cpl_parameterlist_find(parlist,
00393                                "vircam.vircam_mesostep_analyse.icrowd");
00394     vircam_jmp_config.icrowd = cpl_parameter_get_bool(p);
00395     p = cpl_parameterlist_find(parlist,
00396                                "vircam.vircam_mesostep_analyse.rcore");
00397     vircam_jmp_config.rcore = (float)cpl_parameter_get_double(p);
00398     p = cpl_parameterlist_find(parlist,
00399                                "vircam.vircam_mesostep_analyse.nbsize");
00400     vircam_jmp_config.nbsize = cpl_parameter_get_int(p);
00401     p = cpl_parameterlist_find(parlist,
00402                                "vircam.vircam_mesostep_analyse.destripe");
00403     vircam_jmp_config.destripe = cpl_parameter_get_bool(p);
00404     p = cpl_parameterlist_find(parlist,
00405                                "vircam.vircam_mesostep_analyse.skycor");
00406     vircam_jmp_config.skycor = cpl_parameter_get_bool(p);
00407     p = cpl_parameterlist_find(parlist,
00408                                "vircam.vircam_mesostep_analyse.nord");
00409     vircam_jmp_config.nord = cpl_parameter_get_int(p);
00410     p = cpl_parameterlist_find(parlist,
00411                                "vircam.vircam_mesostep_analyse.extenum");
00412     vircam_jmp_config.extenum = cpl_parameter_get_int(p);
00413 
00414     /* Sort out raw from calib frames */
00415 
00416     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00417         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00418         vircam_jmp_tidy(0);
00419         return(-1);
00420     }
00421 
00422     /* Label the input frames */
00423 
00424     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00425                                            &nlab)) == NULL) {
00426         cpl_msg_error(fctid,"Cannot labelise the input frames");
00427         vircam_jmp_tidy(0);
00428         return(-1);
00429     }
00430 
00431     /* Get the input science frames */
00432 
00433     if ((ps.science_frames = 
00434          vircam_frameset_subgroup(framelist,ps.labels,nlab,
00435                                   VIRCAM_ILLUM_RAW)) == NULL) {
00436         cpl_msg_error(fctid,"No science images to process!");
00437         vircam_jmp_tidy(0);
00438         return(-1);
00439     }
00440 
00441     /* Check to see if there is a master dark frame */
00442 
00443     if ((ps.master_dark = 
00444          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00445                                     VIRCAM_CAL_DARK)) == NULL) {
00446         cpl_msg_error(fctid,"No master dark found");
00447         vircam_jmp_tidy(0);
00448         return(-1);
00449     }
00450         
00451     /* Check to see if there is a master twilight flat frame */
00452 
00453     if ((ps.master_twilight_flat = 
00454          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00455                                     VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
00456         cpl_msg_error(fctid,"No master twilight flat found");
00457         vircam_jmp_tidy(0);
00458         return(-1);
00459     }
00460         
00461     /* Get the gain corrections */
00462 
00463     status = VIR_OK;
00464     if (vircam_gaincor_calc(ps.master_twilight_flat,&i,&(ps.gaincors),
00465                             &status) != VIR_OK) {
00466         cpl_msg_error(fctid,"Error calculating gain corrections");
00467         vircam_jmp_tidy(0);
00468         return(-1);
00469     }
00470         
00471     /* Check to see if there is a master confidence map. If there isn't
00472        then look for a bad pixel mask that can be converted into a 
00473        confidence map (in an emergency) */
00474 
00475     isconf = 1;
00476     if ((ps.master_conf = 
00477          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00478                                     VIRCAM_CAL_CONF)) == NULL) {
00479         isconf = 0;
00480         if ((ps.master_conf = 
00481              vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00482                                         VIRCAM_CAL_BPM)) == NULL) {
00483             cpl_msg_error(fctid,"No master confidence map found");
00484             vircam_jmp_tidy(0);
00485             return(-1);
00486         }
00487     }
00488     ps.mask = vircam_mask_define(framelist,ps.labels,nlab);
00489         
00490     /* Check to see if there is a channel table */
00491 
00492     if ((ps.chantab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00493                                                  VIRCAM_CAL_CHANTAB)) == NULL) {
00494         cpl_msg_error(fctid,"No channel table found");
00495         vircam_jmp_tidy(0);
00496         return(-1);
00497     }
00498 
00499     /* Check to see if there is a photometric table */
00500 
00501     if ((ps.phottab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00502                                                  VIRCAM_CAL_PHOTTAB)) == NULL) {
00503         cpl_msg_error(fctid,"No photometric table found");
00504         vircam_jmp_tidy(0);
00505         return(-1);
00506     }
00507     if ((ps.tphottab = cpl_table_load(cpl_frame_get_filename(ps.phottab),1,0)) == NULL) {
00508         cpl_msg_error(fctid,"Unable to load photometric table");
00509         vircam_jmp_tidy(0);
00510         return(-1);
00511     }
00512 
00513     /* Is the 2mass index file specified? */
00514 
00515     if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00516                                                VIRCAM_CAL_2MASS)) == NULL) {
00517         cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
00518         vircam_jmp_tidy(0);
00519         return(-1);
00520     }
00521     
00522     /* Get catalogue parameters */
00523 
00524     if (vircam_catpars(catindex,&(ps.catpath),&(ps.catname)) == VIR_FATAL) {
00525         vircam_jmp_tidy(0);
00526         cpl_frame_delete(catindex);
00527         return(-1);
00528     }
00529     cpl_frame_delete(catindex);
00530 
00531     /* Get the number of DITs */
00532 
00533     pp = cpl_propertylist_load(cpl_frame_get_filename(cpl_frameset_get_frame(ps.science_frames,0)),0);
00534     if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
00535         cpl_msg_error(fctid,"No value for NDIT available");
00536         freepropertylist(pp);
00537         vircam_jmp_tidy(0);
00538         return(-1);
00539     }
00540     cpl_propertylist_delete(pp);
00541 
00542     /* Now, how many image extensions do we want to do? If the extension
00543        number is zero, then we loop for all possible extensions. If it
00544        isn't then we just do the extension specified */
00545 
00546     vircam_exten_range(vircam_jmp_config.extenum,
00547                        (const cpl_frame *)cpl_frameset_get_frame(ps.science_frames,0),
00548                        &jst,&jfn);
00549     if (jst == -1 || jfn == -1) {
00550         cpl_msg_error(fctid,"Unable to continue");
00551         vircam_jmp_tidy(0);
00552         return(-1);
00553     }
00554 
00555     /* Now loop for all the extensions... */
00556 
00557     status = VIR_OK;
00558     for (j = jst; j <= jfn; j++) {
00559         isfirst = (j == jst);
00560         gaincor_fac = (ps.gaincors)[j-1];
00561 
00562         /* Load up the calibration frames into vir_fits/vir_tfits structures 
00563            It is a fatal error if any one of them can't load properly */
00564         
00565         ps.fdark = vircam_fits_load(ps.master_dark,CPL_TYPE_FLOAT,j);
00566         if (ps.fdark == NULL) {
00567             cpl_msg_error(fctid,"Error loading master dark %s[%d]\n%s",
00568                           cpl_frame_get_filename(ps.master_dark),j,
00569                           cpl_error_get_message());
00570             vircam_jmp_tidy(0);
00571             return(-1);
00572         }
00573         ps.fflat = vircam_fits_load(ps.master_twilight_flat,CPL_TYPE_FLOAT,j);
00574         if (ps.fflat == NULL) {
00575             cpl_msg_error(fctid,"Error loading master flat %s[%d]\n%s",
00576                           cpl_frame_get_filename(ps.master_twilight_flat),j,
00577                           cpl_error_get_message());
00578             vircam_jmp_tidy(0);
00579             return(-1);
00580         }
00581         ps.fconf = vircam_fits_load(ps.master_conf,CPL_TYPE_INT,j);
00582         if (ps.fconf == NULL) {
00583             cpl_msg_error(fctid,"Error loading master conf %s[%d]\n%s",
00584                           cpl_frame_get_filename(ps.master_conf),j,
00585                           cpl_error_get_message());
00586             vircam_jmp_tidy(0);
00587             return(-1);
00588         }
00589         if (! isconf) 
00590             vircam_jmp_bpm2conf();
00591         if (vircam_mask_load(ps.mask,j,
00592                              cpl_image_get_size_x(vircam_fits_get_image(ps.fconf)),
00593                              cpl_image_get_size_y(vircam_fits_get_image(ps.fconf))) != VIR_OK) {
00594             cpl_msg_error(fctid,"Error loading mask from master conf %s[%d]\n%s",
00595                           cpl_frame_get_filename(ps.master_conf),j,
00596                           cpl_error_get_message());
00597             vircam_jmp_tidy(0);
00598             return(-1);
00599         }
00600         ps.fchantab = vircam_tfits_load(ps.chantab,j);
00601         if (ps.fchantab == NULL) {
00602             cpl_msg_error(fctid,"Error loading channel table %s[%d]\n%s",
00603                           cpl_frame_get_filename(ps.chantab),j,
00604                           cpl_error_get_message());
00605             vircam_jmp_tidy(0);
00606             return(-1);
00607         }
00608 
00609         /* Load up the vir_fits structures for the science images */
00610 
00611         ps.nscience = cpl_frameset_get_size(ps.science_frames);
00612         ps.sci_fits = vircam_fits_load_list(ps.science_frames,CPL_TYPE_FLOAT,j);
00613         if (ps.sci_fits == NULL) {
00614             cpl_msg_error(fctid,"Error loading science frames extension %d: %s",
00615                           j,cpl_error_get_message());
00616             vircam_jmp_tidy(0);
00617             return(-1);
00618         }
00619 
00620         /* Loop through and mark the frames where the header says the detector
00621            wasn't live */
00622 
00623         for (i = 0; i < ps.nscience; i++) {
00624             ff = ps.sci_fits[i];
00625             vircam_pfits_get_detlive(vircam_fits_get_ehu(ff),&live);
00626             if (! live) 
00627                 vircam_fits_set_error(ff,VIR_FATAL);
00628         }
00629 
00630         /* Loop for all the science frames and do the 2d corrections */
00631 
00632         cpl_msg_info(fctid,"Doing stage1 corrections on %s",
00633                      vircam_fits_get_extname(ps.sci_fits[0]));
00634         for (i = 0; i < ps.nscience; i++) {
00635             ff = ps.sci_fits[i];
00636             if (vircam_fits_get_status(ff) == VIR_FATAL) {
00637                 cpl_msg_info(fctid,"Detector is flagged dead in %s",
00638                              vircam_fits_get_fullname(ff));
00639                 continue;
00640             }
00641             status = VIR_OK;
00642             (void)vircam_darkcor(ff,ps.fdark,1.0,&status);
00643             (void)vircam_lincor(ff,ps.fchantab,1,ndit,&status);
00644             (void)vircam_flatcor(ff,ps.fflat,&status);
00645             (void)vircam_gaincor(ff,gaincor_fac,&status);
00646             if (vircam_jmp_config.destripe) 
00647                 (void)vircam_destripe(ff,ps.mask,&status);
00648             vircam_fits_set_error(ff,status);
00649         }
00650 
00651         /* Do a simple sky correction if requested */
00652 
00653         if (vircam_jmp_config.skycor) {
00654             cpl_msg_info(fctid,"Doing sky correction");
00655             vircam_jmp_skycor();
00656         }
00657 
00658         /* Calculate the illumination correction */
00659 
00660         cpl_msg_info(fctid,"Doing illumination correction");
00661         (void)strcpy(current_cat,ps.catname);
00662         (void)strcpy(current_catpath,ps.catpath);
00663         vircam_jmp_illum();     
00664 
00665         /* Get the data from the illumination correction table */
00666 
00667         if (ps.illcor != NULL) {
00668             ic = vircam_tfits_get_table(ps.illcor);
00669             nrows = cpl_table_get_nrow(ic);
00670             x1 = cpl_table_get_data_float(ic,"xmin");
00671             x2 = cpl_table_get_data_float(ic,"xmax");
00672             y1 = cpl_table_get_data_float(ic,"ymin");
00673             y2 = cpl_table_get_data_float(ic,"ymax");
00674             ill = cpl_table_get_data_float(ic,"illcor");
00675             n = 0;
00676             for (i = 0; i < nrows; i++) 
00677                 if (ill[i] != -99.0)
00678                     n++;
00679 
00680             /* If there is nothing to fit, then don't bother with the rest
00681                of this */
00682 
00683             if (n == 0) {
00684                 cpl_msg_warning(fctid,"Illum correction table is all NULLs");
00685 
00686             } else {
00687                 
00688                 /* Create bivector and vector objects with the xy positions 
00689                    and the illumination correction */
00690 
00691                 bv = cpl_bivector_new(n);
00692                 bv_x = cpl_bivector_get_x_data(bv);
00693                 bv_y = cpl_bivector_get_y_data(bv);
00694                 v = cpl_vector_new(n);
00695                 vdata = cpl_vector_get_data(v);
00696                 n = 0;
00697                 for (i = 0; i < nrows; i++) {
00698                     if (ill[i] == -99.0)
00699                         continue;
00700                     bv_x[n] = 0.5*(double)(x2[i] + x1[i]);
00701                     bv_y[n] = 0.5*(double)(y2[i] + y1[i]);
00702                     vdata[n++] = (double)ill[i];
00703                 }
00704 
00705                 /* Now fit a surface to these results */
00706 
00707                 poly = cpl_polynomial_fit_2d_create(bv,v,vircam_jmp_config.nord,
00708                                                     NULL);
00709                 cpl_vector_delete(v);
00710 
00711                 /* Evaluate the polynomial at the input points and replace 
00712                    the values in the illumination correction with the new 
00713                    value. */
00714 
00715                 v = cpl_vector_new(2);
00716                 vdata = cpl_vector_get_data(v);
00717                 for (i = 0; i < nrows; i++) {
00718                     vdata[0] = 0.5*(double)(x2[i] + x1[i]);
00719                     vdata[1] = 0.5*(double)(y2[i] + y1[i]);
00720                     val = cpl_polynomial_eval(poly,v);
00721                     ill[i] = val;
00722                 }
00723                 cpl_vector_delete(v);
00724                 cpl_bivector_delete(bv);
00725                 cpl_polynomial_delete(poly);
00726             }
00727         }
00728 
00729         /* Save the table */
00730 
00731         cpl_msg_info(fctid,"Saving illumination correction table");
00732         dummyqc = vircam_mesostep_analyse_dummyqc(3);
00733         vircam_jmp_save_illum(framelist,parlist);
00734         freepropertylist(dummyqc);
00735         
00736         /* Clean up on aisle 12! */
00737 
00738         vircam_jmp_tidy(1);
00739     }
00740 
00741     /* Final cleanup */
00742 
00743     vircam_jmp_tidy(0);
00744     return(0);
00745 }
00746 
00747 static cpl_propertylist *vircam_mesostep_analyse_dummyqc(int type) {
00748     cpl_propertylist *p;
00749 
00750     /* Get an empty property list */
00751 
00752     p = cpl_propertylist_new();
00753 
00754     /* Now switch for the various products */
00755 
00756     switch (type) {
00757 
00758     /* Illumination tables */
00759 
00760     case 3:
00761         cpl_propertylist_update_float(p,"ESO QC ILLUMCOR_RMS",0.0);
00762         cpl_propertylist_set_comment(p,"ESO QC ILLUMCOR_RMS",
00763                                      "RMS of illumination correction map");
00764         break;
00765     default:
00766         break;
00767     }
00768 
00769     /* Get out of here */
00770 
00771     return(p);
00772 } 
00773 
00776 /*
00777 
00778 $Log: vircam_mesostep_analyse.c,v $
00779 Revision 1.18  2008/05/06 12:15:20  jim
00780 Changed to use new version of vircam_catpars
00781 
00782 Revision 1.17  2007/11/26 09:59:06  jim
00783 Recipe now takes ndit into account when doing linearity correction
00784 
00785 Revision 1.16  2007/10/25 18:39:22  jim
00786 Altered to remove some lint messages
00787 
00788 Revision 1.15  2007/10/19 06:55:06  jim
00789 Modifications made to use new method for directing the recipes to the
00790 standard catalogues using the sof
00791 
00792 Revision 1.14  2007/07/09 13:21:56  jim
00793 Modified to use new version of vircam_exten_range
00794 
00795 Revision 1.13  2007/06/13 08:11:27  jim
00796 Modified docs to reflect changes in DFS tags
00797 
00798 Revision 1.12  2007/05/08 21:31:16  jim
00799 fixed typo
00800 
00801 Revision 1.11  2007/05/08 10:42:44  jim
00802 Added gain correction
00803 
00804 Revision 1.10  2007/05/02 12:53:11  jim
00805 typo fixes in docs
00806 
00807 Revision 1.9  2007/04/04 16:05:59  jim
00808 Modified to make paf information a bit more correct
00809 
00810 Revision 1.8  2007/04/04 10:36:18  jim
00811 Modified to use new dfs tags
00812 
00813 Revision 1.7  2007/03/29 12:19:38  jim
00814 Little changes to improve documentation
00815 
00816 Revision 1.6  2007/03/14 14:49:13  jim
00817 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
00818 fixed problem where extra dummy products were being created
00819 
00820 Revision 1.5  2007/02/07 10:12:40  jim
00821 Removed calls to vircam_ndit_correct as this is now no longer necessary
00822 
00823 Revision 1.4  2006/12/19 13:33:02  jim
00824 Blocked off bivariate fit so that in the event that the original illumnation
00825 correction fails, it doesn't then try to fit the surface.
00826 
00827 Revision 1.3  2006/12/18 16:43:15  jim
00828 fixed bug with nord parameter doc
00829 
00830 Revision 1.2  2006/12/18 16:42:27  jim
00831 Blocked off the fitting bit so that it doesn't get done if the initial
00832 illumination correction fails.
00833 
00834 Revision 1.1  2006/12/04 21:10:14  jim
00835 Initial entry
00836 
00837 
00838 */

Generated on Wed Apr 10 04:01:56 2013 for VIRCAM Pipeline by  doxygen 1.5.1