vircam_destripe.c

00001 /* $Id: vircam_destripe.c,v 1.6 2007/10/15 12:53:55 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: 2007/10/15 12:53:55 $
00024  * $Revision: 1.6 $
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 <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     int         *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 nlab,jst,jfn,status,j,nx,ny,retval;
00267 
00268     /* Check validity of input frameset */
00269 
00270     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00271         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00272         return(-1);
00273     }
00274 
00275     /* Initialise some things */
00276 
00277     vircam_destripe_init();
00278 
00279     /* Get the parameters */
00280 
00281     p = cpl_parameterlist_find(parlist,"vircam.vircam_destripe.extenum");
00282     vircam_destripe_config.extenum = cpl_parameter_get_int(p);
00283 
00284     /* Sort out raw from calib frames */
00285 
00286     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00287         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00288         vircam_destripe_tidy(2);
00289         return(-1);
00290     }
00291 
00292     /* Get the frames */
00293 
00294     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00295                                            &nlab)) == NULL) {
00296         cpl_msg_error(fctid,"Cannot labelise the input frames");
00297         vircam_destripe_tidy(2);
00298         return(-1);
00299     }
00300     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00301                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00302         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00303         vircam_destripe_tidy(2);
00304         return(-1);
00305     }
00306 
00307     /* Get the master mask */
00308 
00309     ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00310 
00311     /* Now, how many image extensions do we want to do? If the extension
00312        number is zero, then we loop for all possible extensions. If it
00313        isn't then we just do the extension specified */
00314 
00315     vircam_exten_range(vircam_destripe_config.extenum,ps.img,&jst,&jfn);
00316     if (jst == -1 || jfn == -1) {
00317         cpl_msg_error(fctid,"Unable to continue");
00318         vircam_destripe_tidy(2);
00319         return(-1);
00320     }
00321 
00322     /* Now loop for all the extension... */
00323 
00324     for (j = jst; j <= jfn; j++) {
00325         status = VIR_OK;
00326         isfirst = (j == jst);
00327         dummy = 0;
00328 
00329         /* Load up the images */
00330 
00331         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00332         if (ps.img == NULL) {
00333             cpl_msg_info(fctid,"Extension %d frame wouldn't load",j);
00334             dummy = 1;
00335             retval = vircam_destripe_lastbit(j,framelist,parlist);
00336             if (retval != 0)
00337                 return(-1);
00338         }
00339 
00340         /* Get the size of the flat image */
00341         
00342         nx = cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00343         ny = cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00344 
00345         /* Load the data for the bpm */
00346 
00347         (void)vircam_mask_load(ps.bpm,j,nx,ny);
00348 
00349         /* Now do the correction */
00350 
00351         cpl_msg_info(fctid,"Doing the stripe correction for extension %d",j);
00352         (void)vircam_destripe(ps.imgf,ps.bpm,&status);
00353         if (status != VIR_OK) {
00354             cpl_msg_info(fctid,"Extension %d destripe failed",j);
00355             dummy = 1;
00356             retval = vircam_destripe_lastbit(j,framelist,parlist);
00357             if (retval != 0)
00358                 return(-1);
00359         }
00360 
00361         /* Now save the result */
00362 
00363         retval = vircam_destripe_lastbit(j,framelist,parlist);
00364         if (retval != 0)
00365             return(-1);
00366     }
00367     vircam_destripe_tidy(2);
00368     return(0);
00369 }
00370 
00371 /*---------------------------------------------------------------------------*/
00378 /*---------------------------------------------------------------------------*/
00379 
00380 static int vircam_destripe_save(cpl_frameset *framelist, 
00381                                 cpl_parameterlist *parlist) {
00382     const char *fctid = "vircam_destripe_save";
00383     const char *outfile = "destripe.fits";
00384     const char *recipeid = "vircam_destripe";
00385     cpl_propertylist *plist;
00386 
00387     /* If we need to make a PHU then do that now based on the first frame
00388        in the input frame list */
00389 
00390     if (isfirst) {
00391 
00392         /* Create a new product frame object and define some tags */
00393 
00394         product_frame = cpl_frame_new();
00395         cpl_frame_set_filename(product_frame,outfile);
00396         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00397         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00398         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00399         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00400 
00401         /* Set up the header */
00402 
00403         plist = vircam_fits_get_phu(ps.imgf);
00404         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00405                                               parlist,(char *)recipeid,
00406                                               "?Dictionary?");
00407 
00408         /* 'Save' the PHU image */
00409 
00410         if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00411                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00412             cpl_msg_error(fctid,"Cannot save product PHU");
00413             cpl_frame_delete(product_frame);
00414             return(-1);
00415         }
00416         cpl_frameset_insert(framelist,product_frame);
00417     }
00418 
00419     /* Get the extension property list */
00420 
00421     plist = vircam_fits_get_ehu(ps.imgf);
00422 
00423     /* Fiddle with the header now */
00424 
00425     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00426                                         (char *)recipeid,"?Dictionary?");
00427     if (dummy)
00428         vircam_dummy_property(plist);
00429 
00430     /* Save the image */
00431 
00432     if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00433                        plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00434         cpl_msg_error(fctid,"Cannot save product image extension");
00435         return(-1);
00436     }
00437 
00438     return(0);
00439 }
00440 
00441 /*---------------------------------------------------------------------------*/
00449 /*---------------------------------------------------------------------------*/
00450 
00451 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
00452                                    cpl_parameterlist *parlist) {
00453     int retval;
00454     const char *fctid="vircam_destripe_lastbit";
00455 
00456     /* Save everything */
00457 
00458     cpl_msg_info(fctid,"Saving products for extension %d",jext);
00459     retval = vircam_destripe_save(framelist,parlist);
00460     if (retval != 0) {
00461         vircam_destripe_tidy(2);
00462         return(-1);
00463     }
00464 
00465     /* Free some stuff up */
00466 
00467     vircam_destripe_tidy(1);
00468     return(0);
00469 }
00470 
00471 /*---------------------------------------------------------------------------*/
00475 /*---------------------------------------------------------------------------*/
00476 
00477 static void vircam_destripe_init(void) {
00478     ps.labels = NULL;
00479     ps.img = NULL;
00480     ps.imgf = NULL;
00481     ps.bpm = NULL;
00482 }
00483 
00484 
00485 /*---------------------------------------------------------------------------*/
00489 /*---------------------------------------------------------------------------*/
00490 
00491 static void vircam_destripe_tidy(int level) {
00492     if (level == 1) {
00493         freefits(ps.imgf);
00494         vircam_mask_clear(ps.bpm);
00495     } else {
00496         freefits(ps.imgf);
00497         freemask(ps.bpm);
00498         freespace(ps.labels);
00499         freeframe(ps.img);
00500     }
00501 }
00502 
00505 /*
00506 
00507 $Log: vircam_destripe.c,v $
00508 Revision 1.6  2007/10/15 12:53:55  jim
00509 Modified for compatibility with cpl_4.0
00510 
00511 Revision 1.5  2007/07/09 13:22:08  jim
00512 Modified to use new version of vircam_exten_range
00513 
00514 Revision 1.4  2007/04/13 12:27:38  jim
00515 Added some extra docs
00516 
00517 Revision 1.3  2007/04/04 10:36:29  jim
00518 Modified to use new dfs tags
00519 
00520 Revision 1.2  2007/03/01 12:42:59  jim
00521 Modified slightly after code checking
00522 
00523 Revision 1.1  2006/10/02 13:44:23  jim
00524 new file
00525 
00526 
00527 */
00528 
00529 
00530 
00531 

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