vircam_flatcor.c

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

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1