vircam_defringe.c

00001 /* $Id: vircam_defringe.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_defringe_create(cpl_plugin *) ;
00045 static int vircam_defringe_exec(cpl_plugin *) ;
00046 static int vircam_defringe_destroy(cpl_plugin *) ;
00047 static int vircam_defringe_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_defringe_save(cpl_frameset *framelist,
00049                                 cpl_parameterlist *parlist);
00050 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
00051                                    cpl_parameterlist *parlist);
00052 static void vircam_defringe_init(void);
00053 static void vircam_defringe_tidy(int level);
00054 
00055 static struct {
00056 
00057     /* Input */
00058 
00059     int         nbsize;
00060     int         extenum;
00061 
00062 } vircam_defringe_config;
00063 
00064 static struct {
00065     int         *labels;
00066     cpl_frame   *fringe;
00067     cpl_frame   *img;
00068     vir_fits    *fringef;
00069     vir_fits    *imgf;
00070     vir_mask    *bpm;
00071 } ps;
00072 
00073 static int isfirst;
00074 static int dummy;
00075 static cpl_frame *product_frame = NULL;
00076 
00077 static char vircam_defringe_description[] =
00078 "vircam_defringe -- VIRCAM defringe test recipe.\n\n"
00079 "Defringe an input frame using a master fringe frame\n\n"
00080 "The program accepts the following files in the SOF:\n\n"
00081 "    Tag                   Description\n"
00082 "    -----------------------------------------------------------------------\n"
00083 "    %-21s A input uncorrected image\n"
00084 "    %-21s A master fringe frame\n"
00085 "    %-21s Optional master bad pixel map or\n"
00086 "    %-21s Optional master confidence map\n"
00087 "\n";
00088 
00135 /* Function code */
00136 
00137 
00138 /*---------------------------------------------------------------------------*/
00146 /*---------------------------------------------------------------------------*/
00147 
00148 int cpl_plugin_get_info(cpl_pluginlist *list) {
00149     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00150     cpl_plugin  *plugin = &recipe->interface;
00151     char alldesc[SZ_ALLDESC];
00152     (void)snprintf(alldesc,SZ_ALLDESC,vircam_defringe_description,
00153                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_FRINGE,VIRCAM_CAL_BPM,
00154                    VIRCAM_CAL_CONF);
00155 
00156     cpl_plugin_init(plugin,
00157                     CPL_PLUGIN_API,
00158                     VIRCAM_BINARY_VERSION,
00159                     CPL_PLUGIN_TYPE_RECIPE,
00160                     "vircam_defringe",
00161                     "VIRCAM fringe correction test recipe [test]",
00162                     alldesc,
00163                     "Jim Lewis",
00164                     "jrl@ast.cam.ac.uk",
00165                     vircam_get_license(),
00166                     vircam_defringe_create,
00167                     vircam_defringe_exec,
00168                     vircam_defringe_destroy);
00169 
00170     cpl_pluginlist_append(list,plugin);
00171 
00172     return(0);
00173 }
00174 
00175 /*---------------------------------------------------------------------------*/
00184 /*---------------------------------------------------------------------------*/
00185 
00186 static int vircam_defringe_create(cpl_plugin *plugin) {
00187     cpl_recipe      *recipe;
00188     cpl_parameter   *p;
00189 
00190     /* Get the recipe out of the plugin */
00191 
00192     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00193         recipe = (cpl_recipe *)plugin;
00194     else
00195         return(-1);
00196 
00197     /* Create the parameters list in the cpl_recipe object */
00198 
00199     recipe->parameters = cpl_parameterlist_new();
00200 
00201     /* Fill in the parameters. First the background cell size */
00202 
00203     p = cpl_parameter_new_value("vircam.vircam_defringe.nbsize",
00204                                 CPL_TYPE_INT,
00205                                 "Cell size in pixels",
00206                                 "vircam.vircam_defringe",128);
00207     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nbsize");
00208     cpl_parameterlist_append(recipe->parameters,p);
00209 
00210     /* Extension number of input frames to use */
00211 
00212     p = cpl_parameter_new_range("vircam.vircam_defringe.extenum",
00213                                 CPL_TYPE_INT,
00214                                 "Extension number to be done, 0 == all",
00215                                 "vircam.vircam_defringe",1,0,16);
00216     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00217     cpl_parameterlist_append(recipe->parameters,p);
00218 
00219     /* Get out of here */
00220 
00221     return(0);
00222 }
00223 
00224 /*---------------------------------------------------------------------------*/
00230 /*---------------------------------------------------------------------------*/
00231 
00232 static int vircam_defringe_exec(cpl_plugin *plugin) {
00233     cpl_recipe  *recipe;
00234 
00235     /* Get the recipe out of the plugin */
00236 
00237     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00238         recipe = (cpl_recipe *)plugin;
00239     else
00240         return(-1);
00241 
00242     return(vircam_defringe_test(recipe->parameters,recipe->frames));
00243 }
00244 
00245 /*---------------------------------------------------------------------------*/
00251 /*---------------------------------------------------------------------------*/
00252 
00253 static int vircam_defringe_destroy(cpl_plugin *plugin) {
00254     cpl_recipe *recipe ;
00255 
00256     /* Get the recipe out of the plugin */
00257 
00258     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00259         recipe = (cpl_recipe *)plugin;
00260     else
00261         return(-1);
00262 
00263     cpl_parameterlist_delete(recipe->parameters);
00264     return(0);
00265 }
00266 
00267 /*---------------------------------------------------------------------------*/
00274 /*---------------------------------------------------------------------------*/
00275 
00276 static int vircam_defringe_test(cpl_parameterlist *parlist, 
00277                                 cpl_frameset *framelist) {
00278     const char *fctid="vircam_defringe";
00279     cpl_parameter *p;
00280     int nlab,jst,jfn,status,j,retval,nx,ny;
00281 
00282     /* Check validity of input frameset */
00283 
00284     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00285         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00286         return(-1);
00287     }
00288 
00289     /* Initialise some things */
00290 
00291     vircam_defringe_init();
00292 
00293     /* Get the parameters */
00294 
00295     p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.nbsize");
00296     vircam_defringe_config.nbsize = cpl_parameter_get_int(p);
00297     p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.extenum");
00298     vircam_defringe_config.extenum = cpl_parameter_get_int(p);
00299 
00300     /* Sort out raw from calib frames */
00301 
00302     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00303         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00304         vircam_defringe_tidy(2);
00305         return(-1);
00306     }
00307 
00308     /* Get the frames */
00309 
00310     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00311                                            &nlab)) == NULL) {
00312         cpl_msg_error(fctid,"Cannot labelise the input frames");
00313         vircam_defringe_tidy(2);
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_defringe_tidy(2);
00320         return(-1);
00321     }
00322     if ((ps.fringe = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00323                                                 VIRCAM_CAL_FRINGE)) == NULL) {
00324         cpl_msg_info(fctid,"No fringe image found -- cannot continue");
00325         vircam_defringe_tidy(2);
00326         return(-1);
00327     }
00328 
00329     /* Get the master mask */
00330 
00331     ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00332 
00333     /* Now, how many image extensions do we want to do? If the extension
00334        number is zero, then we loop for all possible extensions. If it
00335        isn't then we just do the extension specified */
00336 
00337     vircam_exten_range(vircam_defringe_config.extenum,(const cpl_frame *)ps.img,
00338                        &jst,&jfn);
00339     if (jst == -1 || jfn == -1) {
00340         cpl_msg_error(fctid,"Unable to continue");
00341         vircam_defringe_tidy(2);
00342         return(-1);
00343     }
00344 
00345     /* Now loop for all the extension... */
00346 
00347     status = VIR_OK;
00348     for (j = jst; j <= jfn; j++) {
00349         isfirst = (j == jst);
00350 
00351         /* Load up the images */
00352 
00353         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00354         ps.fringef = vircam_fits_load(ps.fringe,CPL_TYPE_FLOAT,j);
00355         if (ps.img == NULL || ps.fringef == NULL) {
00356             cpl_msg_info(fctid,"Extension %d frame wouldn't load",j);
00357             dummy = 1;
00358             retval = vircam_defringe_lastbit(j,framelist,parlist);
00359             if (retval != 0)
00360                 return(-1);
00361         }
00362 
00363         /* Get the size of the flat image */
00364         
00365         nx = cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00366         ny = cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00367 
00368         /* Load the data for the bpm */
00369 
00370         (void)vircam_mask_load(ps.bpm,j,nx,ny);
00371 
00372         /* Now do the correction */
00373 
00374         cpl_msg_info(fctid,"Doing the fringe correction for extension %d",j);
00375         (void)vircam_defringe(&(ps.imgf),1,&(ps.fringef),1,ps.bpm,
00376                               vircam_defringe_config.nbsize,&status);
00377         if (status != VIR_OK) {
00378             cpl_msg_info(fctid,"Extension %d defringe failed",j);
00379             dummy = 1;
00380             retval = vircam_defringe_lastbit(j,framelist,parlist);
00381             if (retval != 0) 
00382                 return(-1);
00383         }
00384 
00385         /* Now save the result */
00386 
00387         retval = vircam_defringe_lastbit(j,framelist,parlist);
00388         if (retval != 0)
00389             return(-1);
00390     }
00391     vircam_defringe_tidy(2);
00392     return(0);
00393 }
00394 
00395 /*---------------------------------------------------------------------------*/
00402 /*---------------------------------------------------------------------------*/
00403 
00404 static int vircam_defringe_save(cpl_frameset *framelist, 
00405                                 cpl_parameterlist *parlist) {
00406     const char *fctid = "vircam_defringe_save";
00407     const char *outfile = "defringe.fits";
00408     const char *recipeid = "vircam_defringe";
00409     cpl_propertylist *plist;
00410 
00411     /* If we need to make a PHU then do that now based on the first frame
00412        in the input frame list */
00413 
00414     if (isfirst) {
00415 
00416         /* Create a new product frame object and define some tags */
00417 
00418         product_frame = cpl_frame_new();
00419         cpl_frame_set_filename(product_frame,outfile);
00420         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00421         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00422         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00423         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00424 
00425         /* Set up the header */
00426 
00427         plist = vircam_fits_get_phu(ps.imgf);
00428         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00429                                               parlist,(char *)recipeid,
00430                                               "?Dictionary?");
00431 
00432         /* 'Save' the PHU image */
00433 
00434         if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00435                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00436             cpl_msg_error(fctid,"Cannot save product PHU");
00437             cpl_frame_delete(product_frame);
00438             return(-1);
00439         }
00440         cpl_frameset_insert(framelist,product_frame);
00441     }
00442 
00443     /* Get the extension property list */
00444 
00445     plist = vircam_fits_get_ehu(ps.imgf);
00446 
00447     /* Fiddle with the header now */
00448 
00449     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00450                                         (char *)recipeid,"?Dictionary?");
00451     if (dummy)
00452         vircam_dummy_property(plist);
00453 
00454     /* Save the image */
00455 
00456     if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_BPP_IEEE_FLOAT,
00457                        plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00458         cpl_msg_error(fctid,"Cannot save product image extension");
00459         return(-1);
00460     }
00461 
00462     return(0);
00463 }
00464 
00465 /*---------------------------------------------------------------------------*/
00473 /*---------------------------------------------------------------------------*/
00474 
00475 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
00476                                    cpl_parameterlist *parlist) {
00477     int retval;
00478     const char *fctid="vircam_defringe_lastbit";
00479 
00480     /* Save everything */
00481 
00482     cpl_msg_info(fctid,"Saving products for extension %d",jext);
00483     retval = vircam_defringe_save(framelist,parlist);
00484     if (retval != 0) {
00485         vircam_defringe_tidy(2);
00486         return(-1);
00487     }
00488 
00489     /* Free some stuff up */
00490 
00491     vircam_defringe_tidy(1);
00492     return(0);
00493 }
00494 
00495 
00496 /*---------------------------------------------------------------------------*/
00500 /*---------------------------------------------------------------------------*/
00501 
00502 static void vircam_defringe_init(void) {
00503     ps.labels = NULL;
00504     ps.fringe = NULL;
00505     ps.fringef = NULL;
00506     ps.img = NULL;
00507     ps.imgf = NULL;
00508 }
00509 
00510 
00511 /*---------------------------------------------------------------------------*/
00515 /*---------------------------------------------------------------------------*/
00516 
00517 static void vircam_defringe_tidy(int level) {
00518     freefits(ps.imgf);
00519     freefits(ps.fringef);
00520     vircam_mask_clear(ps.bpm);
00521     if (level == 1)
00522         return;
00523     freemask(ps.bpm);
00524     freeframe(ps.img);
00525     freeframe(ps.fringe);
00526     freespace(ps.labels);
00527 }
00528 
00531 /*
00532 
00533 $Log: vircam_defringe.c,v $
00534 Revision 1.6  2007/10/15 12:53:55  jim
00535 Modified for compatibility with cpl_4.0
00536 
00537 Revision 1.5  2007/07/09 13:22:08  jim
00538 Modified to use new version of vircam_exten_range
00539 
00540 Revision 1.4  2007/04/13 12:27:38  jim
00541 Added some extra docs
00542 
00543 Revision 1.3  2007/04/04 10:36:29  jim
00544 Modified to use new dfs tags
00545 
00546 Revision 1.2  2007/03/01 12:42:58  jim
00547 Modified slightly after code checking
00548 
00549 Revision 1.1  2006/12/06 13:00:04  jim
00550 Initial entry
00551 
00552 
00553 */
00554 
00555 
00556 
00557 

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