vircam_jitter_microstep_process.c

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

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