vircam_mesostep_analyse.c

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

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1