vircam_darkcor.c

00001 /* $Id: vircam_darkcor.c,v 1.13 2012/01/16 14:44:02 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2006 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 14:44:02 $
00024  * $Revision: 1.13 $
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 <cpl.h>
00036 
00037 #include "vircam_utils.h"
00038 #include "vircam_dfs.h"
00039 #include "vircam_fits.h"
00040 #include "vircam_mods.h"
00041 
00042 /* Function prototypes */
00043 
00044 static int vircam_darkcor_create(cpl_plugin *) ;
00045 static int vircam_darkcor_exec(cpl_plugin *) ;
00046 static int vircam_darkcor_destroy(cpl_plugin *) ;
00047 static int vircam_darkcor_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_darkcor_save(cpl_frameset *framelist,
00049                                cpl_parameterlist *parlist);
00050 static void vircam_darkcor_init(void);
00051 static void vircam_darkcor_tidy(void);
00052 
00053 static struct {
00054 
00055     /* Input */
00056 
00057     float       darkscl;
00058     int         extenum;
00059 
00060 } vircam_darkcor_config;
00061 
00062 static struct {
00063     cpl_size    *labels;
00064     cpl_frame   *dark;
00065     cpl_frame   *img;
00066     vir_fits    *darkf;
00067     vir_fits    *imgf;
00068 } ps;
00069 
00070 static int isfirst;
00071 static cpl_frame *product_frame = NULL;
00072 
00073 
00074 static char vircam_darkcor_description[] =
00075 "vircam_darkcor -- VIRCAM dark correction test recipe.\n\n"
00076 "Dark correct an input frame using a master dark frame\n\n"
00077 "The program accepts the following files in the SOF:\n\n"
00078 "    Tag                   Description\n"
00079 "    -----------------------------------------------------------------------\n"
00080 "    %-21s A input uncorrected image\n"
00081 "    %-21s A master dark frame\n"
00082 "\n";
00083 
00129 /* Function code */
00130 
00131 
00132 /*---------------------------------------------------------------------------*/
00140 /*---------------------------------------------------------------------------*/
00141 
00142 int cpl_plugin_get_info(cpl_pluginlist *list) {
00143     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00144     cpl_plugin  *plugin = &recipe->interface;
00145     char alldesc[SZ_ALLDESC];
00146     (void)snprintf(alldesc,SZ_ALLDESC,vircam_darkcor_description,
00147                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_DARK);
00148 
00149     cpl_plugin_init(plugin,
00150                     CPL_PLUGIN_API,
00151                     VIRCAM_BINARY_VERSION,
00152                     CPL_PLUGIN_TYPE_RECIPE,
00153                     "vircam_darkcor",
00154                     "VIRCAM dark correction test recipe [test]",
00155                     alldesc,
00156                     "Jim Lewis",
00157                     "jrl@ast.cam.ac.uk",
00158                     vircam_get_license(),
00159                     vircam_darkcor_create,
00160                     vircam_darkcor_exec,
00161                     vircam_darkcor_destroy);
00162 
00163     cpl_pluginlist_append(list,plugin);
00164 
00165     return(0);
00166 }
00167 
00168 /*---------------------------------------------------------------------------*/
00177 /*---------------------------------------------------------------------------*/
00178 
00179 static int vircam_darkcor_create(cpl_plugin *plugin) {
00180     cpl_recipe      *recipe;
00181     cpl_parameter   *p;
00182 
00183     /* Get the recipe out of the plugin */
00184 
00185     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00186         recipe = (cpl_recipe *)plugin;
00187     else
00188         return(-1);
00189 
00190     /* Create the parameters list in the cpl_recipe object */
00191 
00192     recipe->parameters = cpl_parameterlist_new();
00193 
00194     /* Fill in the parameters. First the dark scaling factor */
00195 
00196     p = cpl_parameter_new_value("vircam.vircam_darkcor.darkscl",
00197                                 CPL_TYPE_DOUBLE,
00198                                 "Dark correction scale factor",
00199                                 "vircam.vircam_darkcor",1.0);
00200     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"darkscl");
00201     cpl_parameterlist_append(recipe->parameters,p);
00202 
00203     /* Extension number of input frames to use */
00204 
00205     p = cpl_parameter_new_range("vircam.vircam_darkcor.extenum",
00206                                 CPL_TYPE_INT,
00207                                 "Extension number to be done, 0 == all",
00208                                 "vircam.vircam_darkcor",1,0,16);
00209     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00210     cpl_parameterlist_append(recipe->parameters,p);
00211 
00212     /* Get out of here */
00213 
00214     return(0);
00215 }
00216 
00217 /*---------------------------------------------------------------------------*/
00223 /*---------------------------------------------------------------------------*/
00224 
00225 static int vircam_darkcor_exec(cpl_plugin *plugin) {
00226     cpl_recipe  *recipe;
00227 
00228     /* Get the recipe out of the plugin */
00229 
00230     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00231         recipe = (cpl_recipe *)plugin;
00232     else
00233         return(-1);
00234 
00235     return(vircam_darkcor_test(recipe->parameters,recipe->frames));
00236 }
00237 
00238 /*---------------------------------------------------------------------------*/
00244 /*---------------------------------------------------------------------------*/
00245 
00246 static int vircam_darkcor_destroy(cpl_plugin *plugin) {
00247     cpl_recipe *recipe ;
00248 
00249     /* Get the recipe out of the plugin */
00250 
00251     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00252         recipe = (cpl_recipe *)plugin;
00253     else
00254         return(-1);
00255 
00256     cpl_parameterlist_delete(recipe->parameters);
00257     return(0);
00258 }
00259 
00260 /*---------------------------------------------------------------------------*/
00267 /*---------------------------------------------------------------------------*/
00268 
00269 static int vircam_darkcor_test(cpl_parameterlist *parlist, 
00270                                cpl_frameset *framelist) {
00271     const char *fctid="vircam_darkcor";
00272     cpl_parameter *p;
00273     int jst,jfn,status,j;
00274     cpl_size nlab;
00275 
00276     /* Check validity of input frameset */
00277 
00278     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00279         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00280         return(-1);
00281     }
00282 
00283     /* Initialise some things */
00284 
00285     vircam_darkcor_init();
00286 
00287     /* Get the parameters */
00288 
00289     p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.darkscl");
00290     vircam_darkcor_config.darkscl = cpl_parameter_get_double(p);
00291     p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.extenum");
00292     vircam_darkcor_config.extenum = cpl_parameter_get_int(p);
00293 
00294     /* Sort out raw from calib frames */
00295 
00296     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00297         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00298         vircam_darkcor_tidy();
00299         return(-1);
00300     }
00301 
00302     /* Get the frames */
00303 
00304     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00305                                            &nlab)) == NULL) {
00306         cpl_msg_error(fctid,"Cannot labelise the input frames");
00307         vircam_darkcor_tidy();
00308         return(-1);
00309     }
00310     if ((ps.dark = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00311                                               VIRCAM_CAL_DARK)) == NULL) {
00312         cpl_msg_info(fctid,"No master dark found -- cannot continue");
00313         vircam_darkcor_tidy();
00314         return(-1);
00315     }
00316     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00317                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00318         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00319         vircam_darkcor_tidy();
00320         return(-1);
00321     }
00322 
00323     /* Now, how many image extensions do we want to do? If the extension
00324        number is zero, then we loop for all possible extensions. If it
00325        isn't then we just do the extension specified */
00326 
00327     vircam_exten_range(vircam_darkcor_config.extenum,(const cpl_frame *)ps.img,
00328                        &jst,&jfn);
00329     if (jst == -1 || jfn == -1) {
00330         cpl_msg_error(fctid,"Unable to continue");
00331         vircam_darkcor_tidy();
00332         return(-1);
00333     }
00334 
00335     /* Now loop for all the extension... */
00336 
00337     status = VIR_OK;
00338     for (j = jst; j <= jfn; j++) {
00339         isfirst = (j == jst);
00340 
00341         /* Load up the images */
00342 
00343         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00344         ps.darkf = vircam_fits_load(ps.dark,CPL_TYPE_FLOAT,j);
00345         if (ps.img == NULL || ps.darkf == NULL) {
00346             vircam_darkcor_tidy();
00347             return(-1);
00348         }
00349 
00350         /* Now do the correction */
00351 
00352         cpl_msg_info(fctid,"Doing the dark correction for extension %" CPL_SIZE_FORMAT,
00353                      (cpl_size)j);
00354         (void)vircam_darkcor(ps.imgf,ps.darkf,vircam_darkcor_config.darkscl,
00355                              &status);
00356         if (status != VIR_OK) {
00357             vircam_darkcor_tidy();
00358             return(-1);
00359         }
00360 
00361         /* Now save the result */
00362 
00363         cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00364                      (cpl_size)j);
00365         if (vircam_darkcor_save(framelist,parlist) != 0) {
00366             vircam_darkcor_tidy();
00367             return(-1);
00368         }
00369 
00370         /* Tidy a few things before the next image */
00371 
00372         freefits(ps.imgf);
00373         freefits(ps.darkf);
00374     }
00375     vircam_darkcor_tidy();
00376     return(0);
00377 }
00378 
00379 /*---------------------------------------------------------------------------*/
00386 /*---------------------------------------------------------------------------*/
00387 
00388 static int vircam_darkcor_save(cpl_frameset *framelist, 
00389                                cpl_parameterlist *parlist) {
00390     const char *fctid = "vircam_darkcor_save";
00391     const char *outfile = "darkcor.fits";
00392     const char *recipeid = "vircam_darkcor";
00393     cpl_propertylist *plist;
00394 
00395     /* If we need to make a PHU then do that now based on the first frame
00396        in the input frame list */
00397 
00398     if (isfirst) {
00399 
00400         /* Create a new product frame object and define some tags */
00401 
00402         product_frame = cpl_frame_new();
00403         cpl_frame_set_filename(product_frame,outfile);
00404         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00405         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00406         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00407         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00408 
00409         /* Set up the header */
00410 
00411         plist = vircam_fits_get_phu(ps.imgf);
00412         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00413                                               parlist,(char *)recipeid,
00414                                               "?Dictionary?",NULL,0);
00415 
00416         /* 'Save' the PHU image */
00417 
00418         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00419                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00420             cpl_msg_error(fctid,"Cannot save product PHU");
00421             cpl_frame_delete(product_frame);
00422             return(-1);
00423         }
00424         cpl_frameset_insert(framelist,product_frame);
00425     }
00426 
00427     /* Get the extension property list */
00428 
00429     plist = vircam_fits_get_ehu(ps.imgf);
00430 
00431     /* Fiddle with the header now */
00432 
00433     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00434                                         (char *)recipeid,"?Dictionary?",NULL);
00435 
00436     /* Save the image */
00437 
00438     if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
00439                        plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00440         cpl_msg_error(fctid,"Cannot save product image extension");
00441         return(-1);
00442     }
00443 
00444     return(0);
00445 }
00446 
00447 
00448 /*---------------------------------------------------------------------------*/
00452 /*---------------------------------------------------------------------------*/
00453 
00454 static void vircam_darkcor_init(void) {
00455     ps.labels = NULL;
00456     ps.dark = NULL;
00457     ps.darkf = NULL;
00458     ps.img = NULL;
00459     ps.imgf = NULL;
00460 }
00461 
00462 
00463 /*---------------------------------------------------------------------------*/
00467 /*---------------------------------------------------------------------------*/
00468 
00469 static void vircam_darkcor_tidy(void) {
00470     freespace(ps.labels);
00471     freefits(ps.imgf);
00472     freefits(ps.darkf);
00473     freeframe(ps.dark);
00474     freeframe(ps.img);
00475 }
00476 
00479 /*
00480 
00481 $Log: vircam_darkcor.c,v $
00482 Revision 1.13  2012/01/16 14:44:02  jim
00483 Fixed test recipes for cpl6 compliance
00484 
00485 Revision 1.12  2012/01/15 17:40:09  jim
00486 Minor modifications to take into accout the changes in cpl API for v6
00487 
00488 Revision 1.11  2009/09/09 09:51:13  jim
00489 modified to use new saving routines so that headers are right
00490 
00491 Revision 1.10  2007/10/15 12:53:55  jim
00492 Modified for compatibility with cpl_4.0
00493 
00494 Revision 1.9  2007/07/09 13:22:08  jim
00495 Modified to use new version of vircam_exten_range
00496 
00497 Revision 1.8  2007/04/13 12:27:38  jim
00498 Added some extra docs
00499 
00500 Revision 1.7  2007/04/04 10:36:29  jim
00501 Modified to use new dfs tags
00502 
00503 Revision 1.6  2007/03/01 12:42:58  jim
00504 Modified slightly after code checking
00505 
00506 Revision 1.5  2006/06/15 09:58:59  jim
00507 Minor changes to docs
00508 
00509 Revision 1.4  2006/05/04 11:53:40  jim
00510 Fixed _save routine so that it's more consistent with the standard CPL
00511 way of doing things
00512 
00513 Revision 1.3  2006/05/02 11:29:11  jim
00514 Fixed problem where propertylist was being deleted incorrectly
00515 
00516 Revision 1.2  2006/04/27 14:22:04  jim
00517 Fixed docs
00518 
00519 Revision 1.1  2006/04/24 10:42:44  jim
00520 New routine
00521 
00522 
00523 */
00524 
00525 
00526 
00527 

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1