vircam_destripe.c

00001 /* $Id: vircam_destripe.c,v 1.9 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.9 $
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_destripe_create(cpl_plugin *) ;
00045 static int vircam_destripe_exec(cpl_plugin *) ;
00046 static int vircam_destripe_destroy(cpl_plugin *) ;
00047 static int vircam_destripe_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_destripe_save(cpl_frameset *framelist,
00049                                 cpl_parameterlist *parlist);
00050 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
00051                                    cpl_parameterlist *parlist);
00052 static void vircam_destripe_init(void);
00053 static void vircam_destripe_tidy(int level);
00054 
00055 static struct {
00056 
00057     /* Input */
00058 
00059     int         extenum;
00060 
00061 } vircam_destripe_config;
00062 
00063 static struct {
00064     cpl_size    *labels;
00065     cpl_frame   *img;
00066     vir_fits    *imgf;
00067     vir_mask    *bpm;
00068 } ps;
00069 
00070 static int isfirst;
00071 static int dummy;
00072 static cpl_frame *product_frame = NULL;
00073 
00074 
00075 static char vircam_destripe_description[] =
00076 "vircam_destripe -- VIRCAM destripe correction test recipe.\n\n"
00077 "Destripe an input frame.\n\n"
00078 "The program accepts the following files in the SOF:\n\n"
00079 "    Tag                   Description\n"
00080 "    -----------------------------------------------------------------------\n"
00081 "    %-21s A input uncorrected image\n"
00082 "    %-21s Optional master bad pixel map or\n"
00083 "    %-21s Optional master confidence map\n"
00084 "\n";
00085 
00131 /* Function code */
00132 
00133 
00134 /*---------------------------------------------------------------------------*/
00142 /*---------------------------------------------------------------------------*/
00143 
00144 int cpl_plugin_get_info(cpl_pluginlist *list) {
00145     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00146     cpl_plugin  *plugin = &recipe->interface;
00147     char alldesc[SZ_ALLDESC];
00148     (void)snprintf(alldesc,SZ_ALLDESC,vircam_destripe_description,
00149                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_BPM,VIRCAM_CAL_CONF);
00150 
00151     cpl_plugin_init(plugin,
00152                     CPL_PLUGIN_API,
00153                     VIRCAM_BINARY_VERSION,
00154                     CPL_PLUGIN_TYPE_RECIPE,
00155                     "vircam_destripe",
00156                     "VIRCAM destripe correction test recipe [test]",
00157                     alldesc,
00158                     "Jim Lewis",
00159                     "jrl@ast.cam.ac.uk",
00160                     vircam_get_license(),
00161                     vircam_destripe_create,
00162                     vircam_destripe_exec,
00163                     vircam_destripe_destroy);
00164 
00165     cpl_pluginlist_append(list,plugin);
00166 
00167     return(0);
00168 }
00169 
00170 /*---------------------------------------------------------------------------*/
00179 /*---------------------------------------------------------------------------*/
00180 
00181 static int vircam_destripe_create(cpl_plugin *plugin) {
00182     cpl_recipe      *recipe;
00183     cpl_parameter   *p;
00184 
00185     /* Get the recipe out of the plugin */
00186 
00187     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00188         recipe = (cpl_recipe *)plugin;
00189     else
00190         return(-1);
00191 
00192     /* Create the parameters list in the cpl_recipe object */
00193 
00194     recipe->parameters = cpl_parameterlist_new();
00195 
00196     /* Extension number of input frames to use */
00197 
00198     p = cpl_parameter_new_range("vircam.vircam_destripe.extenum",
00199                                 CPL_TYPE_INT,
00200                                 "Extension number to be done, 0 == all",
00201                                 "vircam.vircam_destripe",1,0,16);
00202     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00203     cpl_parameterlist_append(recipe->parameters,p);
00204 
00205     /* Get out of here */
00206 
00207     return(0);
00208 }
00209 
00210 /*---------------------------------------------------------------------------*/
00216 /*---------------------------------------------------------------------------*/
00217 
00218 static int vircam_destripe_exec(cpl_plugin *plugin) {
00219     cpl_recipe  *recipe;
00220 
00221     /* Get the recipe out of the plugin */
00222 
00223     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00224         recipe = (cpl_recipe *)plugin;
00225     else
00226         return(-1);
00227 
00228     return(vircam_destripe_test(recipe->parameters,recipe->frames));
00229 }
00230 
00231 /*---------------------------------------------------------------------------*/
00237 /*---------------------------------------------------------------------------*/
00238 
00239 static int vircam_destripe_destroy(cpl_plugin *plugin) {
00240     cpl_recipe *recipe ;
00241 
00242     /* Get the recipe out of the plugin */
00243 
00244     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00245         recipe = (cpl_recipe *)plugin;
00246     else
00247         return(-1);
00248 
00249     cpl_parameterlist_delete(recipe->parameters);
00250     return(0);
00251 }
00252 
00253 /*---------------------------------------------------------------------------*/
00260 /*---------------------------------------------------------------------------*/
00261 
00262 static int vircam_destripe_test(cpl_parameterlist *parlist, 
00263                                 cpl_frameset *framelist) {
00264     const char *fctid="vircam_destripe";
00265     cpl_parameter *p;
00266     int jst,jfn,status,j,nx,ny,retval;
00267     cpl_size nlab;
00268 
00269     /* Check validity of input frameset */
00270 
00271     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00272         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00273         return(-1);
00274     }
00275 
00276     /* Initialise some things */
00277 
00278     vircam_destripe_init();
00279 
00280     /* Get the parameters */
00281 
00282     p = cpl_parameterlist_find(parlist,"vircam.vircam_destripe.extenum");
00283     vircam_destripe_config.extenum = cpl_parameter_get_int(p);
00284 
00285     /* Sort out raw from calib frames */
00286 
00287     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00288         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00289         vircam_destripe_tidy(2);
00290         return(-1);
00291     }
00292 
00293     /* Get the frames */
00294 
00295     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00296                                            &nlab)) == NULL) {
00297         cpl_msg_error(fctid,"Cannot labelise the input frames");
00298         vircam_destripe_tidy(2);
00299         return(-1);
00300     }
00301     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00302                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00303         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00304         vircam_destripe_tidy(2);
00305         return(-1);
00306     }
00307 
00308     /* Get the master mask */
00309 
00310     ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00311 
00312     /* Now, how many image extensions do we want to do? If the extension
00313        number is zero, then we loop for all possible extensions. If it
00314        isn't then we just do the extension specified */
00315 
00316     vircam_exten_range(vircam_destripe_config.extenum,ps.img,&jst,&jfn);
00317     if (jst == -1 || jfn == -1) {
00318         cpl_msg_error(fctid,"Unable to continue");
00319         vircam_destripe_tidy(2);
00320         return(-1);
00321     }
00322 
00323     /* Now loop for all the extension... */
00324 
00325     for (j = jst; j <= jfn; j++) {
00326         status = VIR_OK;
00327         isfirst = (j == jst);
00328         dummy = 0;
00329 
00330         /* Load up the images */
00331 
00332         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00333         if (ps.img == NULL) {
00334             cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
00335                          (cpl_size)j);
00336             dummy = 1;
00337             retval = vircam_destripe_lastbit(j,framelist,parlist);
00338             if (retval != 0)
00339                 return(-1);
00340         }
00341 
00342         /* Get the size of the flat image */
00343         
00344         nx = (int)cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00345         ny = (int)cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00346 
00347         /* Load the data for the bpm */
00348 
00349         (void)vircam_mask_load(ps.bpm,j,nx,ny);
00350 
00351         /* Now do the correction */
00352 
00353         cpl_msg_info(fctid,"Doing the stripe correction for extension %" CPL_SIZE_FORMAT,
00354                      (cpl_size)j);
00355         (void)vircam_destripe(ps.imgf,ps.bpm,&status);
00356         if (status != VIR_OK) {
00357             cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " destripe failed",
00358                          (cpl_size)j);
00359             dummy = 1;
00360             retval = vircam_destripe_lastbit(j,framelist,parlist);
00361             if (retval != 0)
00362                 return(-1);
00363         }
00364 
00365         /* Now save the result */
00366 
00367         retval = vircam_destripe_lastbit(j,framelist,parlist);
00368         if (retval != 0)
00369             return(-1);
00370     }
00371     vircam_destripe_tidy(2);
00372     return(0);
00373 }
00374 
00375 /*---------------------------------------------------------------------------*/
00382 /*---------------------------------------------------------------------------*/
00383 
00384 static int vircam_destripe_save(cpl_frameset *framelist, 
00385                                 cpl_parameterlist *parlist) {
00386     const char *fctid = "vircam_destripe_save";
00387     const char *outfile = "destripe.fits";
00388     const char *recipeid = "vircam_destripe";
00389     cpl_propertylist *plist;
00390 
00391     /* If we need to make a PHU then do that now based on the first frame
00392        in the input frame list */
00393 
00394     if (isfirst) {
00395 
00396         /* Create a new product frame object and define some tags */
00397 
00398         product_frame = cpl_frame_new();
00399         cpl_frame_set_filename(product_frame,outfile);
00400         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00401         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00402         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00403         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00404 
00405         /* Set up the header */
00406 
00407         plist = vircam_fits_get_phu(ps.imgf);
00408         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00409                                               parlist,(char *)recipeid,
00410                                               "?Dictionary?",NULL,0);
00411 
00412         /* 'Save' the PHU image */
00413 
00414         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00415                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00416             cpl_msg_error(fctid,"Cannot save product PHU");
00417             cpl_frame_delete(product_frame);
00418             return(-1);
00419         }
00420         cpl_frameset_insert(framelist,product_frame);
00421     }
00422 
00423     /* Get the extension property list */
00424 
00425     plist = vircam_fits_get_ehu(ps.imgf);
00426 
00427     /* Fiddle with the header now */
00428 
00429     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00430                                         (char *)recipeid,"?Dictionary?",NULL);
00431     if (dummy)
00432         vircam_dummy_property(plist);
00433 
00434     /* Save the image */
00435 
00436     if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
00437                        plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00438         cpl_msg_error(fctid,"Cannot save product image extension");
00439         return(-1);
00440     }
00441 
00442     return(0);
00443 }
00444 
00445 /*---------------------------------------------------------------------------*/
00453 /*---------------------------------------------------------------------------*/
00454 
00455 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
00456                                    cpl_parameterlist *parlist) {
00457     int retval;
00458     const char *fctid="vircam_destripe_lastbit";
00459 
00460     /* Save everything */
00461 
00462     cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
00463                  (cpl_size)jext);
00464     retval = vircam_destripe_save(framelist,parlist);
00465     if (retval != 0) {
00466         vircam_destripe_tidy(2);
00467         return(-1);
00468     }
00469 
00470     /* Free some stuff up */
00471 
00472     vircam_destripe_tidy(1);
00473     return(0);
00474 }
00475 
00476 /*---------------------------------------------------------------------------*/
00480 /*---------------------------------------------------------------------------*/
00481 
00482 static void vircam_destripe_init(void) {
00483     ps.labels = NULL;
00484     ps.img = NULL;
00485     ps.imgf = NULL;
00486     ps.bpm = NULL;
00487 }
00488 
00489 
00490 /*---------------------------------------------------------------------------*/
00494 /*---------------------------------------------------------------------------*/
00495 
00496 static void vircam_destripe_tidy(int level) {
00497     if (level == 1) {
00498         freefits(ps.imgf);
00499         vircam_mask_clear(ps.bpm);
00500     } else {
00501         freefits(ps.imgf);
00502         freemask(ps.bpm);
00503         freespace(ps.labels);
00504         freeframe(ps.img);
00505     }
00506 }
00507 
00510 /*
00511 
00512 $Log: vircam_destripe.c,v $
00513 Revision 1.9  2012/01/16 14:44:02  jim
00514 Fixed test recipes for cpl6 compliance
00515 
00516 Revision 1.8  2012/01/15 17:40:09  jim
00517 Minor modifications to take into accout the changes in cpl API for v6
00518 
00519 Revision 1.7  2009/09/09 09:51:13  jim
00520 modified to use new saving routines so that headers are right
00521 
00522 Revision 1.6  2007/10/15 12:53:55  jim
00523 Modified for compatibility with cpl_4.0
00524 
00525 Revision 1.5  2007/07/09 13:22:08  jim
00526 Modified to use new version of vircam_exten_range
00527 
00528 Revision 1.4  2007/04/13 12:27:38  jim
00529 Added some extra docs
00530 
00531 Revision 1.3  2007/04/04 10:36:29  jim
00532 Modified to use new dfs tags
00533 
00534 Revision 1.2  2007/03/01 12:42:59  jim
00535 Modified slightly after code checking
00536 
00537 Revision 1.1  2006/10/02 13:44:23  jim
00538 new file
00539 
00540 
00541 */
00542 
00543 
00544 
00545 

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1