vircam_defringe.c

00001 /* $Id: vircam_defringe.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_2 $
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     cpl_size    *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 jst,jfn,status,j,retval,nx,ny;
00281     cpl_size nlab;
00282 
00283     /* Check validity of input frameset */
00284 
00285     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00286         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00287         return(-1);
00288     }
00289 
00290     /* Initialise some things */
00291 
00292     vircam_defringe_init();
00293 
00294     /* Get the parameters */
00295 
00296     p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.nbsize");
00297     vircam_defringe_config.nbsize = cpl_parameter_get_int(p);
00298     p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.extenum");
00299     vircam_defringe_config.extenum = cpl_parameter_get_int(p);
00300 
00301     /* Sort out raw from calib frames */
00302 
00303     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00304         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00305         vircam_defringe_tidy(2);
00306         return(-1);
00307     }
00308 
00309     /* Get the frames */
00310 
00311     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00312                                            &nlab)) == NULL) {
00313         cpl_msg_error(fctid,"Cannot labelise the input frames");
00314         vircam_defringe_tidy(2);
00315         return(-1);
00316     }
00317     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00318                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00319         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00320         vircam_defringe_tidy(2);
00321         return(-1);
00322     }
00323     if ((ps.fringe = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00324                                                 VIRCAM_CAL_FRINGE)) == NULL) {
00325         cpl_msg_info(fctid,"No fringe image found -- cannot continue");
00326         vircam_defringe_tidy(2);
00327         return(-1);
00328     }
00329 
00330     /* Get the master mask */
00331 
00332     ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
00333 
00334     /* Now, how many image extensions do we want to do? If the extension
00335        number is zero, then we loop for all possible extensions. If it
00336        isn't then we just do the extension specified */
00337 
00338     vircam_exten_range(vircam_defringe_config.extenum,(const cpl_frame *)ps.img,
00339                        &jst,&jfn);
00340     if (jst == -1 || jfn == -1) {
00341         cpl_msg_error(fctid,"Unable to continue");
00342         vircam_defringe_tidy(2);
00343         return(-1);
00344     }
00345 
00346     /* Now loop for all the extension... */
00347 
00348     status = VIR_OK;
00349     for (j = jst; j <= jfn; j++) {
00350         isfirst = (j == jst);
00351 
00352         /* Load up the images */
00353 
00354         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00355         ps.fringef = vircam_fits_load(ps.fringe,CPL_TYPE_FLOAT,j);
00356         if (ps.img == NULL || ps.fringef == NULL) {
00357             cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
00358                          (cpl_size)j);
00359             dummy = 1;
00360             retval = vircam_defringe_lastbit(j,framelist,parlist);
00361             if (retval != 0)
00362                 return(-1);
00363         }
00364 
00365         /* Get the size of the flat image */
00366         
00367         nx = (int)cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
00368         ny = (int)cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
00369 
00370         /* Load the data for the bpm */
00371 
00372         (void)vircam_mask_load(ps.bpm,j,nx,ny);
00373 
00374         /* Now do the correction */
00375 
00376         cpl_msg_info(fctid,"Doing the fringe correction for extension %" CPL_SIZE_FORMAT,
00377                      (cpl_size)j);
00378         (void)vircam_defringe(&(ps.imgf),1,&(ps.fringef),1,ps.bpm,
00379                               vircam_defringe_config.nbsize,&status);
00380         if (status != VIR_OK) {
00381             cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " defringe failed",
00382                          (cpl_size)j);
00383             dummy = 1;
00384             retval = vircam_defringe_lastbit(j,framelist,parlist);
00385             if (retval != 0) 
00386                 return(-1);
00387         }
00388 
00389         /* Now save the result */
00390 
00391         retval = vircam_defringe_lastbit(j,framelist,parlist);
00392         if (retval != 0)
00393             return(-1);
00394     }
00395     vircam_defringe_tidy(2);
00396     return(0);
00397 }
00398 
00399 /*---------------------------------------------------------------------------*/
00406 /*---------------------------------------------------------------------------*/
00407 
00408 static int vircam_defringe_save(cpl_frameset *framelist, 
00409                                 cpl_parameterlist *parlist) {
00410     const char *fctid = "vircam_defringe_save";
00411     const char *outfile = "defringe.fits";
00412     const char *recipeid = "vircam_defringe";
00413     cpl_propertylist *plist;
00414 
00415     /* If we need to make a PHU then do that now based on the first frame
00416        in the input frame list */
00417 
00418     if (isfirst) {
00419 
00420         /* Create a new product frame object and define some tags */
00421 
00422         product_frame = cpl_frame_new();
00423         cpl_frame_set_filename(product_frame,outfile);
00424         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00425         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00426         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00427         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00428 
00429         /* Set up the header */
00430 
00431         plist = vircam_fits_get_phu(ps.imgf);
00432         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00433                                               parlist,(char *)recipeid,
00434                                               "?Dictionary?",NULL,0);
00435 
00436         /* 'Save' the PHU image */
00437 
00438         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00439                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00440             cpl_msg_error(fctid,"Cannot save product PHU");
00441             cpl_frame_delete(product_frame);
00442             return(-1);
00443         }
00444         cpl_frameset_insert(framelist,product_frame);
00445     }
00446 
00447     /* Get the extension property list */
00448 
00449     plist = vircam_fits_get_ehu(ps.imgf);
00450 
00451     /* Fiddle with the header now */
00452 
00453     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00454                                         (char *)recipeid,"?Dictionary?",NULL);
00455     if (dummy)
00456         vircam_dummy_property(plist);
00457 
00458     /* Save the image */
00459 
00460     if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
00461                        plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00462         cpl_msg_error(fctid,"Cannot save product image extension");
00463         return(-1);
00464     }
00465 
00466     return(0);
00467 }
00468 
00469 /*---------------------------------------------------------------------------*/
00477 /*---------------------------------------------------------------------------*/
00478 
00479 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
00480                                    cpl_parameterlist *parlist) {
00481     int retval;
00482     const char *fctid="vircam_defringe_lastbit";
00483 
00484     /* Save everything */
00485 
00486     cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
00487                  (cpl_size)jext);
00488     retval = vircam_defringe_save(framelist,parlist);
00489     if (retval != 0) {
00490         vircam_defringe_tidy(2);
00491         return(-1);
00492     }
00493 
00494     /* Free some stuff up */
00495 
00496     vircam_defringe_tidy(1);
00497     return(0);
00498 }
00499 
00500 
00501 /*---------------------------------------------------------------------------*/
00505 /*---------------------------------------------------------------------------*/
00506 
00507 static void vircam_defringe_init(void) {
00508     ps.labels = NULL;
00509     ps.fringe = NULL;
00510     ps.fringef = NULL;
00511     ps.img = NULL;
00512     ps.imgf = NULL;
00513 }
00514 
00515 
00516 /*---------------------------------------------------------------------------*/
00520 /*---------------------------------------------------------------------------*/
00521 
00522 static void vircam_defringe_tidy(int level) {
00523     freefits(ps.imgf);
00524     freefits(ps.fringef);
00525     vircam_mask_clear(ps.bpm);
00526     if (level == 1)
00527         return;
00528     freemask(ps.bpm);
00529     freeframe(ps.img);
00530     freeframe(ps.fringe);
00531     freespace(ps.labels);
00532 }
00533 
00536 /*
00537 
00538 $Log: vircam_defringe.c,v $
00539 Revision 1.9  2012/01/16 14:44:02  jim
00540 Fixed test recipes for cpl6 compliance
00541 
00542 Revision 1.8  2012/01/15 17:40:09  jim
00543 Minor modifications to take into accout the changes in cpl API for v6
00544 
00545 Revision 1.7  2009/09/09 09:51:13  jim
00546 modified to use new saving routines so that headers are right
00547 
00548 Revision 1.6  2007/10/15 12:53:55  jim
00549 Modified for compatibility with cpl_4.0
00550 
00551 Revision 1.5  2007/07/09 13:22:08  jim
00552 Modified to use new version of vircam_exten_range
00553 
00554 Revision 1.4  2007/04/13 12:27:38  jim
00555 Added some extra docs
00556 
00557 Revision 1.3  2007/04/04 10:36:29  jim
00558 Modified to use new dfs tags
00559 
00560 Revision 1.2  2007/03/01 12:42:58  jim
00561 Modified slightly after code checking
00562 
00563 Revision 1.1  2006/12/06 13:00:04  jim
00564 Initial entry
00565 
00566 
00567 */
00568 
00569 
00570 
00571 

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1