vircam_standard_process.c

00001 /* $Id: vircam_standard_process.c,v 1.22 2008/05/06 12:15:21 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:21 $
00024  * $Revision: 1.22 $
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_standard_process_create(cpl_plugin *);
00051 static int vircam_standard_process_exec(cpl_plugin *);
00052 static int vircam_standard_process_destroy(cpl_plugin *);
00053 static int vircam_standard_process(cpl_parameterlist *,cpl_frameset *);
00054 static cpl_propertylist *vircam_standard_process_dummyqc(int type);
00055 
00056 
00057 static char vircam_standard_process_description[] =
00058 "vircam_standard_process -- VIRCAM standard field processing recipe.\n\n"
00059 "Process a complete pawprint for standard fields in VIRCAM data.\n"
00060 "Remove instrumental signature, interleave microstep sequences (if done),\n"
00061 "combine jitters, photometrically and astrometrically calibrate the pawprint\n"
00062 "image. Photometric calibration can be done with 2mass and 1 other source\n\n"
00063 "The program accepts the following files in the SOF:\n\n"
00064 "    Tag                   Description\n"
00065 "    -----------------------------------------------------------------------\n"
00066 "    %-21s A list of raw science images\n"
00067 "    %-21s A master dark frame\n"
00068 "    %-21s A master twilight flat frame\n"
00069 "    %-21s A channel table\n"
00070 "    %-21s A photometric calibration table\n"
00071 "    %-21s A readnoise/gain file\n"
00072 "    %-21s A master confidence map or\n"
00073 "    %-21s A master bad pixel mask\n"
00074 "    %-21s A master 2mass catalogue index\n"
00075 "    %-21s A second reference catalogue index\n"
00076 "All of the above are required\n"
00077 "\n";
00078 
00215 /* Function code */
00216 
00217 /*---------------------------------------------------------------------------*/
00225 /*---------------------------------------------------------------------------*/
00226 
00227 int cpl_plugin_get_info(cpl_pluginlist *list) {
00228     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00229     cpl_plugin  *plugin = &recipe->interface;
00230     char alldesc[SZ_ALLDESC];
00231     (void)snprintf(alldesc,SZ_ALLDESC,vircam_standard_process_description,
00232                    VIRCAM_STD_OBJECT_RAW,VIRCAM_CAL_DARK,
00233                    VIRCAM_CAL_TWILIGHT_FLAT,VIRCAM_CAL_CHANTAB,
00234                    VIRCAM_CAL_PHOTTAB,VIRCAM_CAL_READGAINFILE,VIRCAM_CAL_CONF,
00235                    VIRCAM_CAL_BPM,VIRCAM_CAL_2MASS,VIRCAM_CAL_REFCAT);
00236 
00237     cpl_plugin_init(plugin,
00238                     CPL_PLUGIN_API,
00239                     VIRCAM_BINARY_VERSION,
00240                     CPL_PLUGIN_TYPE_RECIPE,
00241                     "vircam_standard_process",
00242                     "VIRCAM standard field processing recipe",
00243                     alldesc,
00244                     "Jim Lewis",
00245                     "jrl@ast.cam.ac.uk",
00246                     vircam_get_license(),
00247                     vircam_standard_process_create,
00248                     vircam_standard_process_exec,
00249                     vircam_standard_process_destroy);
00250 
00251     cpl_pluginlist_append(list,plugin);
00252 
00253     return(0);
00254 }
00255 
00256 /*---------------------------------------------------------------------------*/
00265 /*---------------------------------------------------------------------------*/
00266 
00267 static int vircam_standard_process_create(cpl_plugin *plugin) {
00268     cpl_recipe      *recipe;
00269     cpl_parameter   *p;
00270 
00271     /* Get the recipe out of the plugin */
00272 
00273     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00274         recipe = (cpl_recipe *)plugin;
00275     else 
00276         return(-1);
00277 
00278     /* Create the parameters list in the cpl_recipe object */
00279 
00280     recipe->parameters = cpl_parameterlist_new();
00281 
00282     /* Fill in the minimum object size */
00283 
00284     p = cpl_parameter_new_value("vircam.vircam_standard_process.ipix",
00285                                 CPL_TYPE_INT,
00286                                 "Minimum pixel area for each detected object",
00287                                 "vircam.vircam_standard_process",5);
00288     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
00289     cpl_parameterlist_append(recipe->parameters,p);
00290 
00291     /* Fill in the detection threshold parameter */
00292 
00293     p = cpl_parameter_new_value("vircam.vircam_standard_process.thresh",
00294                                 CPL_TYPE_DOUBLE,
00295                                 "Detection threshold in sigma above sky",
00296                                 "vircam.vircam_standard_process",2.0);
00297     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
00298     cpl_parameterlist_append(recipe->parameters,p);
00299 
00300     /* Fill in flag to use deblending software or not */
00301 
00302     p = cpl_parameter_new_value("vircam.vircam_standard_process.icrowd",
00303                                 CPL_TYPE_BOOL,"Use deblending?",
00304                                 "vircam.vircam_standard_process",0);
00305     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
00306     cpl_parameterlist_append(recipe->parameters,p);
00307 
00308     /* Fill in core radius */
00309 
00310     p = cpl_parameter_new_value("vircam.vircam_standard_process.rcore",
00311                                 CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
00312                                 "vircam.vircam_standard_process",4.0);
00313     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
00314     cpl_parameterlist_append(recipe->parameters,p);
00315 
00316     /* Fill in background smoothing box size */
00317 
00318     p = cpl_parameter_new_value("vircam.vircam_standard_process.nbsize",
00319                                 CPL_TYPE_INT,"Background smoothing box size",
00320                                 "vircam.vircam_standard_process",64);
00321     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
00322     cpl_parameterlist_append(recipe->parameters,p);
00323 
00324     /* Fill in flag to use save the output catalogue */
00325 
00326     p = cpl_parameter_new_value("vircam.vircam_standard_process.savecat",
00327                                 CPL_TYPE_BOOL,"Save catalogue?",
00328                                 "vircam.vircam_standard_process",1);
00329     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"savecat");
00330     cpl_parameterlist_append(recipe->parameters,p);
00331 
00332 
00333     /* Fill in flag to destripe the images */
00334 
00335     p = cpl_parameter_new_value("vircam.vircam_standard_process.destripe",
00336                                 CPL_TYPE_BOOL,"Destripe images?",
00337                                 "vircam.vircam_standard_process",1);
00338     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"destripe");
00339     cpl_parameterlist_append(recipe->parameters,p);
00340 
00341     /* Fill in flag to correct sky background */
00342 
00343     p = cpl_parameter_new_value("vircam.vircam_standard_process.skycor",
00344                                 CPL_TYPE_BOOL,"Sky correct images?",
00345                                 "vircam.vircam_standard_process",1);
00346     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"skycor");
00347     cpl_parameterlist_append(recipe->parameters,p);
00348 
00349     /* Extension number of input frames to use */
00350 
00351     p = cpl_parameter_new_range("vircam.vircam_standard_process.extenum",
00352                                 CPL_TYPE_INT,
00353                                 "Extension number to be done, 0 == all",
00354                                 "vircam.vircam_standard_process",
00355                                 1,0,16);
00356     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00357     cpl_parameterlist_append(recipe->parameters,p);
00358         
00359     /* Get out of here */
00360 
00361     return(0);
00362 }
00363     
00364     
00365 /*---------------------------------------------------------------------------*/
00371 /*---------------------------------------------------------------------------*/
00372 
00373 static int vircam_standard_process_exec(cpl_plugin *plugin) {
00374     cpl_recipe  *recipe;
00375 
00376     /* Get the recipe out of the plugin */
00377 
00378     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00379         recipe = (cpl_recipe *)plugin;
00380     else 
00381         return(-1);
00382 
00383     return(vircam_standard_process(recipe->parameters,recipe->frames));
00384 }
00385                                 
00386 /*---------------------------------------------------------------------------*/
00392 /*---------------------------------------------------------------------------*/
00393 
00394 static int vircam_standard_process_destroy(cpl_plugin *plugin) {
00395     cpl_recipe *recipe ;
00396 
00397     /* Get the recipe out of the plugin */
00398 
00399     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00400         recipe = (cpl_recipe *)plugin;
00401     else 
00402         return(-1);
00403 
00404     cpl_parameterlist_delete(recipe->parameters);
00405     return(0);
00406 }
00407 
00408 /*---------------------------------------------------------------------------*/
00415 /*---------------------------------------------------------------------------*/
00416 
00417 static int vircam_standard_process(cpl_parameterlist *parlist, 
00418                                    cpl_frameset *framelist) {
00419     const char *fctid="vircam_standard_process";
00420     cpl_parameter *p;
00421     int nlab,jst,jfn,status,j,i,retval,nusteps,isconf,live,ndit;
00422     float readnoise,gain,gaincor_fac;
00423     vir_fits *ff;
00424     cpl_propertylist *ehu,*pp;
00425     cpl_frame *catindex;
00426 
00427     /* Check validity of input frameset */
00428 
00429     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00430         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00431         return(-1);
00432     }
00433 
00434     /* Initialise some things */
00435 
00436     vircam_jmp_init();
00437     (void)strncpy(vircam_recipename,fctid,VIRCAM_PATHSZ);
00438     (void)snprintf(vircam_recipepaf,VIRCAM_PATHSZ,"VIRCAM/%s",fctid);
00439     recflag = RECSTD;
00440 
00441     /* Get the parameters */
00442 
00443     p = cpl_parameterlist_find(parlist,
00444                                "vircam.vircam_standard_process.ipix");
00445     vircam_jmp_config.ipix = cpl_parameter_get_int(p);
00446     p = cpl_parameterlist_find(parlist,
00447                                "vircam.vircam_standard_process.thresh");
00448     vircam_jmp_config.threshold = (float)cpl_parameter_get_double(p);
00449     p = cpl_parameterlist_find(parlist,
00450                                "vircam.vircam_standard_process.icrowd");
00451     vircam_jmp_config.icrowd = cpl_parameter_get_bool(p);
00452     p = cpl_parameterlist_find(parlist,
00453                                "vircam.vircam_standard_process.rcore");
00454     vircam_jmp_config.rcore = (float)cpl_parameter_get_double(p);
00455     p = cpl_parameterlist_find(parlist,
00456                                "vircam.vircam_standard_process.nbsize");
00457     vircam_jmp_config.nbsize = cpl_parameter_get_int(p);
00458     p = cpl_parameterlist_find(parlist,
00459                                "vircam.vircam_standard_process.savecat");
00460     vircam_jmp_config.savecat = cpl_parameter_get_bool(p);
00461     p = cpl_parameterlist_find(parlist,
00462                                "vircam.vircam_standard_process.destripe");
00463     vircam_jmp_config.destripe = cpl_parameter_get_bool(p);
00464     p = cpl_parameterlist_find(parlist,
00465                                "vircam.vircam_standard_process.skycor");
00466     vircam_jmp_config.skycor = cpl_parameter_get_bool(p);
00467     p = cpl_parameterlist_find(parlist,
00468                                "vircam.vircam_standard_process.extenum");
00469     vircam_jmp_config.extenum = cpl_parameter_get_int(p);
00470 
00471     /* Sort out raw from calib frames */
00472 
00473     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00474         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00475         vircam_jmp_tidy(0);
00476         return(-1);
00477     }
00478 
00479     /* Label the input frames */
00480 
00481     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00482                                            &nlab)) == NULL) {
00483         cpl_msg_error(fctid,"Cannot labelise the input frames");
00484         vircam_jmp_tidy(0);
00485         return(-1);
00486     }
00487 
00488     /* Get the input science frames */
00489 
00490     if ((ps.science_frames = 
00491          vircam_frameset_subgroup(framelist,ps.labels,nlab,
00492                                   VIRCAM_STD_OBJECT_RAW)) == NULL) {
00493         cpl_msg_error(fctid,"No science images to process!");
00494         vircam_jmp_tidy(0);
00495         return(-1);
00496     }
00497 
00498     /* Check to see if there is a master dark frame */
00499 
00500     if ((ps.master_dark = 
00501          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00502                                     VIRCAM_CAL_DARK)) == NULL) {
00503         cpl_msg_error(fctid,"No master dark found");
00504         vircam_jmp_tidy(0);
00505         return(-1);
00506     }
00507         
00508     /* Check to see if there is a master twilight flat frame */
00509 
00510     if ((ps.master_twilight_flat = 
00511          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00512                                     VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
00513         cpl_msg_error(fctid,"No master twilight flat found");
00514         vircam_jmp_tidy(0);
00515         return(-1);
00516     }
00517         
00518     /* Get the gain corrections */
00519 
00520     status = VIR_OK;
00521     if (vircam_gaincor_calc(ps.master_twilight_flat,&i,&(ps.gaincors),
00522                             &status) != VIR_OK) {
00523         cpl_msg_error(fctid,"Error calculating gain corrections");
00524         vircam_jmp_tidy(0);
00525         return(-1);
00526     }
00527         
00528     /* Check to see if there is a readgain file */
00529 
00530     if ((ps.readgain_file = 
00531          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00532                                     VIRCAM_CAL_READGAINFILE)) == NULL) {
00533         cpl_msg_error(fctid,"No master readnoise/gain file found");
00534         vircam_jmp_tidy(0);
00535         return(-1);
00536     }
00537         
00538     /* Check to see if there is a master confidence map. If there isn't
00539        then look for a bad pixel mask that can be converted into a 
00540        confidence map (in an emergency) */
00541 
00542     isconf = 1;
00543     if ((ps.master_conf = 
00544          vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00545                                     VIRCAM_CAL_CONF)) == NULL) {
00546         isconf = 0;
00547         if ((ps.master_conf = 
00548              vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00549                                         VIRCAM_CAL_BPM)) == NULL) {
00550             cpl_msg_error(fctid,"No master confidence map found");
00551             vircam_jmp_tidy(0);
00552             return(-1);
00553         }
00554     }
00555     ps.mask = vircam_mask_define(framelist,ps.labels,nlab);
00556         
00557     /* Check to see if there is a channel table */
00558 
00559     if ((ps.chantab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00560                                                  VIRCAM_CAL_CHANTAB)) == NULL) {
00561         cpl_msg_error(fctid,"No channel table found");
00562         vircam_jmp_tidy(0);
00563         return(-1);
00564     }
00565 
00566     /* Check to see if there is a photometric table */
00567 
00568     if ((ps.phottab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00569                                                  VIRCAM_CAL_PHOTTAB)) == NULL) {
00570         cpl_msg_error(fctid,"No photometric table found");
00571         vircam_jmp_tidy(0);
00572         return(-1);
00573     }
00574     if ((ps.tphottab = cpl_table_load(cpl_frame_get_filename(ps.phottab),1,0)) == NULL) {
00575         cpl_msg_error(fctid,"Unable to load photometric table");
00576         vircam_jmp_tidy(0);
00577         return(-1);
00578     }
00579 
00580     /* Is the 2mass index file specified? */
00581 
00582     if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00583                                                VIRCAM_CAL_2MASS)) == NULL) {
00584         cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
00585         vircam_jmp_tidy(0);
00586         return(-1);
00587     }
00588     
00589     /* Get catalogue parameters */
00590 
00591     if (vircam_catpars(catindex,&(ps.catpath),&(ps.catname)) == VIR_FATAL) {
00592         vircam_jmp_tidy(0);
00593         cpl_frame_delete(catindex);
00594         return(-1);
00595     }
00596     cpl_frame_delete(catindex);
00597 
00598     /* Is there a second photometric catalogue index file specified? */
00599 
00600     if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00601                                                VIRCAM_CAL_2MASS)) != NULL) {
00602     
00603         /* Get catalogue parameters */
00604 
00605         if (vircam_catpars(catindex,&(ps.catpath2),&(ps.catname2)) == VIR_FATAL) {
00606             vircam_jmp_tidy(0);
00607             cpl_frame_delete(catindex);
00608             return(-1);
00609         }
00610         cpl_frame_delete(catindex);
00611     }
00612 
00613     /* Get the number of DITs */
00614 
00615     pp = cpl_propertylist_load(cpl_frame_get_filename(cpl_frameset_get_frame(ps.science_frames,0)),0);
00616     if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
00617         cpl_msg_error(fctid,"No value for NDIT available");
00618         freepropertylist(pp);
00619         vircam_jmp_tidy(0);
00620         return(-1);
00621     }
00622     cpl_propertylist_delete(pp);
00623 
00624     /* Now, how many image extensions do we want to do? If the extension
00625        number is zero, then we loop for all possible extensions. If it
00626        isn't then we just do the extension specified */
00627 
00628     vircam_exten_range(vircam_jmp_config.extenum,
00629                        (const cpl_frame *)cpl_frameset_get_frame(ps.science_frames,0),
00630                        &jst,&jfn);
00631     if (jst == -1 || jfn == -1) {
00632         cpl_msg_error(fctid,"Unable to continue");
00633         vircam_jmp_tidy(0);
00634         return(-1);
00635     }
00636 
00637     /* Now loop for all the extensions... */
00638 
00639     status = VIR_OK;
00640     for (j = jst; j <= jfn; j++) {
00641         isfirst = (j == jst);
00642         gaincor_fac = (ps.gaincors)[j-1];
00643 
00644         /* Load up the calibration frames into vir_fits/vir_tfits structures 
00645            It is a fatal error if any one of them can't load properly */
00646         
00647         ps.fdark = vircam_fits_load(ps.master_dark,CPL_TYPE_FLOAT,j);
00648         if (ps.fdark == NULL) {
00649             cpl_msg_error(fctid,"Error loading master dark %s[%d]\n%s",
00650                           cpl_frame_get_filename(ps.master_dark),j,
00651                           cpl_error_get_message());
00652             vircam_jmp_tidy(0);
00653             return(-1);
00654         }
00655         ps.fflat = vircam_fits_load(ps.master_twilight_flat,CPL_TYPE_FLOAT,j);
00656         if (ps.fflat == NULL) {
00657             cpl_msg_error(fctid,"Error loading master flat %s[%d]\n%s",
00658                           cpl_frame_get_filename(ps.master_twilight_flat),j,
00659                           cpl_error_get_message());
00660             vircam_jmp_tidy(0);
00661             return(-1);
00662         }
00663         ps.fconf = vircam_fits_load(ps.master_conf,CPL_TYPE_INT,j);
00664         if (ps.fconf == NULL) {
00665             cpl_msg_error(fctid,"Error loading master conf %s[%d]\n%s",
00666                           cpl_frame_get_filename(ps.master_conf),j,
00667                           cpl_error_get_message());
00668             vircam_jmp_tidy(0);
00669             return(-1);
00670         }
00671         if (! isconf) 
00672             vircam_jmp_bpm2conf();
00673         if (vircam_mask_load(ps.mask,j,
00674                              cpl_image_get_size_x(vircam_fits_get_image(ps.fconf)),
00675                              cpl_image_get_size_y(vircam_fits_get_image(ps.fconf))) != VIR_OK) {
00676             cpl_msg_error(fctid,"Error loading mask from master conf %s[%d]\n%s",
00677                           cpl_frame_get_filename(ps.master_conf),j,
00678                           cpl_error_get_message());
00679             vircam_jmp_tidy(0);
00680             return(-1);
00681         }
00682         ps.fchantab = vircam_tfits_load(ps.chantab,j);
00683         if (ps.fchantab == NULL) {
00684             cpl_msg_error(fctid,"Error loading channel table %s[%d]\n%s",
00685                           cpl_frame_get_filename(ps.chantab),j,
00686                           cpl_error_get_message());
00687             vircam_jmp_tidy(0);
00688             return(-1);
00689         }
00690 
00691         /* Load up the vir_fits structures for the science images */
00692 
00693         ps.nscience = cpl_frameset_get_size(ps.science_frames);
00694         ps.sci_fits = vircam_fits_load_list(ps.science_frames,CPL_TYPE_FLOAT,j);
00695         if (ps.sci_fits == NULL) {
00696             cpl_msg_error(fctid,"Error loading science frames extension %d: %s",
00697                           j,cpl_error_get_message());
00698             vircam_jmp_tidy(0);
00699             return(-1);
00700         }
00701 
00702         /* Loop through and mark the frames where the header says the detector
00703            wasn't live */
00704 
00705         for (i = 0; i < ps.nscience; i++) {
00706             ff = ps.sci_fits[i];
00707             vircam_pfits_get_detlive(vircam_fits_get_ehu(ff),&live);
00708             if (! live) 
00709                 vircam_fits_set_error(ff,VIR_FATAL);
00710         }
00711 
00712         /* Get the readnoise and gain estimate for this extension */
00713 
00714         vircam_jmp_get_readnoise_gain(j,&readnoise,&gain);
00715 
00716         /* Loop for all the science frames and do the 2d corrections */
00717 
00718         cpl_msg_info(fctid,"Doing stage1 corrections on %s",
00719                      vircam_fits_get_extname(ps.sci_fits[0]));
00720         for (i = 0; i < ps.nscience; i++) {
00721             ff = ps.sci_fits[i];
00722             cpl_propertylist_update_float(vircam_fits_get_ehu(ff),"READNOIS",
00723                                           readnoise);
00724             cpl_propertylist_set_comment(vircam_fits_get_ehu(ff),"READNOIS",
00725                                          "[e-] Readnoise used in processing");
00726             cpl_propertylist_update_float(vircam_fits_get_ehu(ff),"GAIN",gain);
00727             cpl_propertylist_set_comment(vircam_fits_get_ehu(ff),"GAIN",
00728                                          "[e-/adu] Gain used in processing");
00729             if (vircam_fits_get_status(ff) == VIR_FATAL) {
00730                 cpl_msg_info(fctid,"Detector is flagged dead in %s",
00731                              vircam_fits_get_fullname(ff));
00732                 continue;
00733             }
00734             status = VIR_OK;
00735             (void)vircam_darkcor(ff,ps.fdark,1.0,&status);
00736             (void)vircam_lincor(ff,ps.fchantab,1,ndit,&status);
00737             (void)vircam_flatcor(ff,ps.fflat,&status);
00738             (void)vircam_gaincor(ff,gaincor_fac,&status);
00739             if (vircam_jmp_config.destripe) 
00740                 (void)vircam_destripe(ff,ps.mask,&status);
00741             vircam_fits_set_error(ff,status);
00742         }
00743 
00744         /* Do a simple sky correction if requested */
00745 
00746         if (vircam_jmp_config.skycor) {
00747             cpl_msg_info(fctid,"Doing sky correction");
00748             vircam_jmp_skycor();
00749         }
00750 
00751         /* Calculate the illumination correction */
00752 
00753         cpl_msg_info(fctid,"Doing illumination correction");
00754         (void)strcpy(current_cat,ps.catname);
00755         (void)strcpy(current_catpath,ps.catpath);
00756         vircam_jmp_illum();     
00757         
00758         /* Save the simple images */ 
00759 
00760         cpl_msg_info(fctid,"Saving simple images");
00761         vircam_jmp_save_simple(framelist,parlist);
00762         vircam_mask_clear(ps.mask);
00763 
00764         /* Save the illumination correction table */
00765 
00766         cpl_msg_info(fctid,"Saving illumination correction table");
00767         dummyqc = vircam_standard_process_dummyqc(3);
00768         vircam_jmp_save_illum(framelist,parlist);
00769         freepropertylist(dummyqc);
00770 
00771         /* Look at the first frame in the list and see if the number of
00772            microsteps is greater than 1. If so, then we'll have to go
00773            through interleaving. */
00774 
00775         retval = vircam_pfits_get_nusteps(vircam_fits_get_phu(ps.sci_fits[0]),
00776                                           &nusteps);
00777         if (retval != VIR_OK) {
00778             cpl_msg_warning(fctid,"Unable to get nusteps from header.\nAssuming no microstepping");
00779             nusteps = 1;
00780         }
00781         
00782         /* If the number of microsteps is 1 then copy over the good frames
00783            into a fits list for dithering. */
00784 
00785         ps.ndith = 0;
00786         if (nusteps == 1) {
00787             cpl_msg_info(fctid,"No interleaving will be done");
00788             ps.dith_input = cpl_malloc(ps.nscience*sizeof(vir_fits *));
00789             ps.dithc_input = cpl_malloc(sizeof(vir_fits *));
00790             for (i = 0; i < ps.nscience; i++) {
00791                 if (vircam_fits_get_status(ps.sci_fits[i]) == VIR_OK) 
00792                     ps.dith_input[ps.ndith++] = ps.sci_fits[i];
00793             }
00794             ps.dithc_input[0] = ps.fconf;
00795             ps.ndithc = 1;
00796             interlv = 0;
00797 
00798         /* If the number of microsteps is more than 1, then we need
00799            to do interleaving. The interleaving routine define
00800            ps.dith_input. */
00801 
00802         } else {
00803             cpl_msg_info(fctid,"Interleaving");
00804             vircam_jmp_interleave();
00805             cpl_msg_info(fctid,"Saving superframe images");
00806             vircam_jmp_save_super(framelist,parlist);
00807             interlv = 1;
00808         }
00809 
00810         /* Work out the jitter offsets and the stack the jitter frame */
00811 
00812         cpl_msg_info(fctid,"Working out jitter offsets");
00813         vircam_jmp_dither_offsets();
00814         cpl_msg_info(fctid,"Stacking jittered frame");
00815         vircam_jmp_dither_images();
00816 
00817         /* Do a catalogue generation */
00818 
00819         cpl_msg_info(fctid,"Doing object extraction");
00820         vircam_jmp_catalogue();
00821 
00822         /* Create a matched standards table */
00823 
00824         cpl_msg_info(fctid,"Matching objects with 2mass standards");
00825         vircam_jmp_matched_stds();
00826 
00827         /* Do a WCS fit for the dithered image */
00828 
00829         cpl_msg_info(fctid,"Fitting a WCS");
00830         vircam_jmp_wcsfit();
00831 
00832         /* Finally do the photometric zeropoint fit */
00833 
00834         cpl_msg_info(fctid,"Doing 2mass photometric zeropoint calculation");
00835         vircam_jmp_photcal();
00836         if (vircam_fits_get_status(ps.stack_frame) == VIR_OK) {
00837             ehu = vircam_fits_get_ehu(ps.stack_frame);
00838             cpl_propertylist_update_float(ehu,"ESO QC ZPT_2MASS",
00839                                           cpl_propertylist_get_float(ehu,"ESO QC MAGZPT"));
00840             cpl_propertylist_set_comment(ehu,"ESO QC ZPT_2MASS",
00841                                          cpl_propertylist_get_comment(ehu,"ESO QC MAGZPT"));
00842         }
00843 
00844         /* Redo the photometric zeropoint with another catalogue if we
00845            have one */
00846 
00847         if (ps.catpath2 != NULL) {
00848             freetable(ps.matchstds);
00849             (void)strcpy(current_cat,ps.catname2);
00850             (void)strcpy(current_catpath,ps.catpath2);
00851             cpl_msg_info(fctid,"Matching objects with %s standards",
00852                          ps.catname2);
00853             vircam_jmp_matched_stds();
00854             cpl_msg_info(fctid,"Doing %s photometric zeropoint calculation",
00855                          ps.catname2);
00856             vircam_jmp_photcal();
00857             if (vircam_fits_get_status(ps.stack_frame) == VIR_OK) {
00858                 ehu = vircam_fits_get_ehu(ps.stack_frame);
00859                 cpl_propertylist_update_float(ehu,"ESO QC ZPT_STDS",
00860                                               cpl_propertylist_get_float(ehu,"ESO QC MAGZPT"));
00861                 cpl_propertylist_set_comment(ehu,"ESO QC ZPT_STDS",
00862                                              cpl_propertylist_get_comment(ehu,"ESO QC MAGZPT"));
00863                 cpl_propertylist_update_string(ehu,"ESO QC ZPT_STDS_CAT",
00864                                                ps.catname2);
00865                 cpl_propertylist_set_comment(ehu,"ESO QC ZPT_STDS_CAT",
00866                                              "Catalogue used in zeropoint calc");
00867             }
00868         }
00869 
00870         /* Save the dithered images */
00871 
00872         cpl_msg_info(fctid,"Saving stacked images");    
00873         dummyqc = vircam_standard_process_dummyqc(1);
00874         vircam_jmp_save_stack(framelist,parlist);
00875         freepropertylist(dummyqc);
00876         if (vircam_jmp_config.savecat) {
00877             cpl_msg_info(fctid,"Saving stacked image catalogues");
00878             dummyqc = vircam_standard_process_dummyqc(2);
00879             vircam_jmp_save_catalogue(framelist,parlist);
00880             freepropertylist(dummyqc);
00881         }
00882 
00883         /* Clean up on aisle 12! */
00884 
00885         vircam_jmp_tidy(1);
00886     }
00887 
00888     /* Final cleanup */
00889 
00890     vircam_jmp_tidy(0);
00891     return(0);
00892 }
00893 
00894 static cpl_propertylist *vircam_standard_process_dummyqc(int type) {
00895     cpl_propertylist *p;
00896 
00897     /* Get an empty property list */
00898 
00899     p = cpl_propertylist_new();
00900 
00901     /* Now switch for the various products */
00902 
00903     switch (type) {
00904 
00905     /* Stack images */
00906 
00907     case 1:
00908         cpl_propertylist_update_double(p,"ESO QC WCS_DCRVAL1",0.0);
00909         cpl_propertylist_set_comment(p,"ESO QC WCS_DCRVAL1",
00910                                      "[deg] change in crval1");
00911         cpl_propertylist_update_double(p,"ESO QC WCS_DCRVAL2",0.0);
00912         cpl_propertylist_set_comment(p,"ESO QC WCS_DCRVAL2",
00913                                      "[deg] change in crval2");
00914         cpl_propertylist_update_double(p,"ESO QC WCS_DTHETA",0.0);
00915         cpl_propertylist_set_comment(p,"ESO QC WCS_DTHETA",
00916                                      "[deg] change in rotation");
00917         cpl_propertylist_update_double(p,"ESO QC WCS_SCALE",0.0);
00918         cpl_propertylist_set_comment(p,"ESO QC WCS_SCALE",
00919                                      "[arcsec] mean plate scale");
00920         cpl_propertylist_update_double(p,"ESO QC WCS_SHEAR",0.0);
00921         cpl_propertylist_set_comment(p,"ESO QC WCS_SHEAR",
00922                                      "[deg] abs(xrot) - abs(yrot)");
00923         cpl_propertylist_update_double(p,"ESO QC WCS_RMS",0.0);
00924         cpl_propertylist_set_comment(p,"ESO QC WCS_RMS",
00925                                      "[arcsec] Average error in WCS fit");
00926         cpl_propertylist_update_float(p,"ESO QC MAGZPT",0.0);
00927         cpl_propertylist_set_comment(p,"ESO QC MAGZPT",
00928                                      "[mag] photometric zeropoint");
00929         cpl_propertylist_update_float(p,"ESO QC MAGZERR",0.0);
00930         cpl_propertylist_set_comment(p,"ESO QC MAGZERR",
00931                                      "[mag] photometric zeropoint error");
00932         cpl_propertylist_update_int(p,"ESO QC MAGNZPT",0);
00933         cpl_propertylist_set_comment(p,"ESO QC MAGNZPT",
00934                                      "number of stars in magzpt calc");
00935         cpl_propertylist_update_float(p,"ESO QC LIMITING_MAG",0.0);
00936         cpl_propertylist_set_comment(p,"ESO QC LIMITING_MAG",
00937                                      "[mag] 5 sigma limiting mag");
00938         cpl_propertylist_update_float(p,"ESO QC ZPT_2MASS",0.0);
00939         cpl_propertylist_set_comment(p,"ESO QC ZPT_2MASS",
00940                                      "[mag] photometric zeropoint");
00941         cpl_propertylist_update_float(p,"ESO QC ZPT_STDS",0.0);
00942         cpl_propertylist_set_comment(p,"ESO QC ZPT_STDS",
00943                                      "[mag] photometric zeropoint");
00944         cpl_propertylist_update_string(p,"ESO QC ZPT_STDS_CAT","");
00945         cpl_propertylist_set_comment(p,"ESO QC ZPT_STDS_CAT",
00946                                      "Catalogue used in zeropoint calc");
00947         break;
00948         
00949     /* Catalogues */
00950 
00951     case 2:
00952         cpl_propertylist_update_float(p,"ESO QC SATURATION",0.0);
00953         cpl_propertylist_set_comment(p,"ESO QC SATURATION",
00954                                      "[adu] Saturation level");
00955         cpl_propertylist_update_float(p,"ESO QC MEAN_SKY",0.0);
00956         cpl_propertylist_set_comment(p,"ESO QC MEAN_SKY",
00957                                      "[adu] Median sky brightness");
00958         cpl_propertylist_update_float(p,"ESO QC SKY_NOISE",0.0);
00959         cpl_propertylist_set_comment(p,"ESO QC SKY_NOISE",
00960                                      "[adu] Pixel noise at sky level");
00961         cpl_propertylist_update_float(p,"ESO QC IMAGE_SIZE",0.0);
00962         cpl_propertylist_set_comment(p,"ESO QC IMAGE_SIZE",
00963                                      "[pixels] Average FWHM of stellar objects");
00964         cpl_propertylist_update_float(p,"ESO QC ELLIPTICITY",0.0);
00965         cpl_propertylist_set_comment(p,"ESO QC ELLIPTICITY",
00966                                      "Average stellar ellipticity (1-b/a)");
00967         cpl_propertylist_update_float(p,"ESO QC APERTURE_CORR",0.0);
00968         cpl_propertylist_set_comment(p,"ESO QC APERTURE_CORR",
00969                                      "Stellar ap-corr 1x core flux");
00970         cpl_propertylist_update_int(p,"ESO QC NOISE_OBJ",0);
00971         cpl_propertylist_set_comment(p,"ESO QC NOISE_OBJ",
00972                                      "Number of noise objects");
00973         break;
00974 
00975     /* Illumination tables */
00976 
00977     case 3:
00978         cpl_propertylist_update_float(p,"ESO QC ILLUMCOR_RMS",0.0);
00979         cpl_propertylist_set_comment(p,"ESO QC ILLUMCOR_RMS",
00980                                      "RMS of illumination correction map");
00981         break;
00982     default:
00983         break;
00984     }
00985 
00986     /* Get out of here */
00987 
00988     return(p);
00989 }
00990 
00993 /*
00994 
00995 $Log: vircam_standard_process.c,v $
00996 Revision 1.22  2008/05/06 12:15:21  jim
00997 Changed to use new version of vircam_catpars
00998 
00999 Revision 1.21  2007/11/26 09:59:06  jim
01000 Recipe now takes ndit into account when doing linearity correction
01001 
01002 Revision 1.20  2007/10/25 18:39:22  jim
01003 Altered to remove some lint messages
01004 
01005 Revision 1.19  2007/10/19 06:55:06  jim
01006 Modifications made to use new method for directing the recipes to the
01007 standard catalogues using the sof
01008 
01009 Revision 1.18  2007/07/09 13:21:56  jim
01010 Modified to use new version of vircam_exten_range
01011 
01012 Revision 1.17  2007/06/13 08:11:27  jim
01013 Modified docs to reflect changes in DFS tags
01014 
01015 Revision 1.16  2007/05/08 21:31:16  jim
01016 fixed typo
01017 
01018 Revision 1.15  2007/05/08 10:42:44  jim
01019 Added gain correction
01020 
01021 Revision 1.14  2007/05/02 12:53:11  jim
01022 typo fixes in docs
01023 
01024 Revision 1.13  2007/04/26 13:09:40  jim
01025 fixed typos
01026 
01027 Revision 1.12  2007/04/04 16:05:59  jim
01028 Modified to make paf information a bit more correct
01029 
01030 Revision 1.11  2007/04/04 10:36:18  jim
01031 Modified to use new dfs tags
01032 
01033 Revision 1.10  2007/03/29 12:19:39  jim
01034 Little changes to improve documentation
01035 
01036 Revision 1.9  2007/03/14 14:49:13  jim
01037 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
01038 fixed problem where extra dummy products were being created
01039 
01040 Revision 1.8  2007/03/06 12:00:48  jim
01041 Fixed stupid typo in header
01042 
01043 Revision 1.7  2007/03/02 12:37:51  jim
01044 Fixed small memory leak
01045 
01046 Revision 1.6  2007/03/01 12:41:49  jim
01047 Modified slightly after code checking
01048 
01049 Revision 1.5  2007/02/07 10:12:40  jim
01050 Removed calls to vircam_ndit_correct as this is now no longer necessary
01051 
01052 Revision 1.4  2006/12/19 13:32:03  jim
01053 Images that are flagged as dead detectors now generate an INFO rather than
01054 an ERROR message
01055 
01056 Revision 1.3  2006/11/29 12:28:45  jim
01057 Modified so that the correct recipe names would appear in the headers of
01058 data products
01059 
01060 Revision 1.2  2006/11/28 20:57:43  jim
01061 Added illumination correction section
01062 
01063 Revision 1.1  2006/11/27 12:15:43  jim
01064 Initial entry
01065 
01066 
01067 */

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