vircam_imcombine.c

00001 /* $Id: vircam_imcombine.c,v 1.13 2007/10/25 19:38:22 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/25 19:38:22 $
00024  * $Revision: 1.13 $
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 #include <math.h>
00037 
00038 #include "vircam_utils.h"
00039 #include "vircam_mask.h"
00040 #include "vircam_dfs.h"
00041 #include "vircam_mods.h"
00042 #include "vircam_fits.h"
00043 
00044 /* Function prototypes */
00045 
00046 static int vircam_imcombine_create(cpl_plugin *) ;
00047 static int vircam_imcombine_exec(cpl_plugin *) ;
00048 static int vircam_imcombine_destroy(cpl_plugin *) ;
00049 static int vircam_imcombine_test(cpl_parameterlist *, cpl_frameset *) ;
00050 static int vircam_imcombine_save(cpl_frameset *framelist, 
00051                                  cpl_parameterlist *parlist);
00052 static void vircam_imcombine_init(void);
00053 static void vircam_imcombine_tidy(void);
00054 
00055 /* Static global variables */
00056 
00057 static struct {
00058 
00059     /* Input */
00060 
00061     int         combtype;
00062     int         scaletype;
00063     int         xrej;
00064     float       thresh;
00065     int         extenum;
00066 
00067 } vircam_imcombine_config;
00068 
00069 
00070 static struct {
00071     int              *labels;
00072     cpl_frameset     *imagelist;
00073     vir_fits         **images;
00074     int              nimages;
00075     cpl_image        *outimage;
00076 } ps;
00077 
00078 static int isfirst;
00079 static cpl_frame *product_frame = NULL;
00080 
00081 static char vircam_imcombine_description[] =
00082 "vircam_imcombine -- VIRCAM test imcombine recipe.\n\n"
00083 "Combine a list of frames into a mean frame.\n\n"
00084 "The program accepts the following files in the SOF:\n\n"
00085 "    Tag                   Description\n"
00086 "    -----------------------------------------------------------------------\n"
00087 "    %-21s A list of images\n"
00088 "\n";
00089 
00146 /* Function code */
00147 
00148 /*---------------------------------------------------------------------------*/
00156 /*---------------------------------------------------------------------------*/
00157 
00158 int cpl_plugin_get_info(cpl_pluginlist *list) {
00159     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00160     cpl_plugin  *plugin = &recipe->interface;
00161     char alldesc[SZ_ALLDESC];
00162     (void)snprintf(alldesc,SZ_ALLDESC,vircam_imcombine_description,
00163                    VIRCAM_TEST_SCIENCE_RAW);
00164 
00165     cpl_plugin_init(plugin,
00166                     CPL_PLUGIN_API,
00167                     VIRCAM_BINARY_VERSION,
00168                     CPL_PLUGIN_TYPE_RECIPE,
00169                     "vircam_imcombine",
00170                     "VIRCAM test image combination recipe [test]",
00171                     alldesc,
00172                     "Jim Lewis",
00173                     "jrl@ast.cam.ac.uk",
00174                     vircam_get_license(),
00175                     vircam_imcombine_create,
00176                     vircam_imcombine_exec,
00177                     vircam_imcombine_destroy);
00178 
00179     cpl_pluginlist_append(list,plugin);
00180 
00181     return(0);
00182 }
00183 
00184 /*---------------------------------------------------------------------------*/
00193 /*---------------------------------------------------------------------------*/
00194 
00195 static int vircam_imcombine_create(cpl_plugin *plugin) {
00196     cpl_recipe      *recipe;
00197     cpl_parameter   *p;
00198 
00199     /* Get the recipe out of the plugin */
00200 
00201     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00202         recipe = (cpl_recipe *)plugin;
00203     else 
00204         return(-1);
00205 
00206     /* Create the parameters list in the cpl_recipe object */
00207 
00208     recipe->parameters = cpl_parameterlist_new();
00209 
00210     /* Fill in the parameters. First the combination type */
00211 
00212     p = cpl_parameter_new_range("vircam.vircam_imcombine.combtype",
00213                                 CPL_TYPE_INT,
00214                                 "1 == Median,\n 2 == Mean",
00215                                 "vircam.vircam_imcombine",
00216                                 2,1,2);
00217     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"comb");
00218     cpl_parameterlist_append(recipe->parameters,p);
00219 
00220     /* The requested scaling */
00221 
00222     p = cpl_parameter_new_range("vircam.vircam_imcombine.scaletype",
00223                                 CPL_TYPE_INT,
00224                                 "0 == none,\n 1 == additive offset,\n 2 == multiplicative offset,\n 3 == exposure time scaling + additive offset",
00225                                 "vircam.vircam_imcombine",
00226                                 1,0,3);
00227     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"scale");
00228     cpl_parameterlist_append(recipe->parameters,p);
00229     
00230     /* Extra rejection cycle */
00231 
00232     p = cpl_parameter_new_value("vircam.vircam_imcombine.xrej",
00233                                 CPL_TYPE_BOOL,
00234                                 "True if using extra rejection cycle",
00235                                 "vircam.vircam_imcombine",
00236                                 TRUE);
00237     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"xrej");
00238     cpl_parameterlist_append(recipe->parameters,p);
00239 
00240     /* Rejection threshold */
00241 
00242     p = cpl_parameter_new_value("vircam.vircam_imcombine.thresh",
00243                                 CPL_TYPE_DOUBLE,
00244                                 "Rejection threshold in sigma above background",
00245                                 "vircam.vircam_imcombine",5.0);
00246     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
00247     cpl_parameterlist_append(recipe->parameters,p);
00248 
00249     /* Extension number of input frames to use */
00250 
00251     p = cpl_parameter_new_range("vircam.vircam_imcombine.extenum",
00252                                 CPL_TYPE_INT,
00253                                 "Extension number to be done, 0 == all",
00254                                 "vircam.vircam_imcombine",
00255                                 1,0,16);
00256     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00257     cpl_parameterlist_append(recipe->parameters,p);
00258         
00259     /* Get out of here */
00260 
00261     return(0);
00262 }
00263     
00264     
00265 /*---------------------------------------------------------------------------*/
00271 /*---------------------------------------------------------------------------*/
00272 
00273 static int vircam_imcombine_exec(cpl_plugin *plugin) {
00274     cpl_recipe  *recipe;
00275 
00276     /* Get the recipe out of the plugin */
00277 
00278     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00279         recipe = (cpl_recipe *)plugin;
00280     else 
00281         return(-1);
00282 
00283     return(vircam_imcombine_test(recipe->parameters,recipe->frames));
00284 }
00285                                 
00286 /*---------------------------------------------------------------------------*/
00292 /*---------------------------------------------------------------------------*/
00293 
00294 static int vircam_imcombine_destroy(cpl_plugin *plugin) {
00295     cpl_recipe *recipe ;
00296 
00297     /* Get the recipe out of the plugin */
00298 
00299     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00300         recipe = (cpl_recipe *)plugin;
00301     else 
00302         return(-1);
00303 
00304     cpl_parameterlist_delete(recipe->parameters);
00305     return(0);
00306 }
00307 
00308 /*---------------------------------------------------------------------------*/
00315 /*---------------------------------------------------------------------------*/
00316 
00317 static int vircam_imcombine_test(cpl_parameterlist *parlist, 
00318                                  cpl_frameset *framelist) {
00319     const char *fctid="vircam_imcombine";
00320     int nlab,j,jst,jfn,retval,status;
00321     cpl_parameter *p;
00322     unsigned char *rejmask,*rejplus;
00323     cpl_propertylist *drs;
00324 
00325     /* Check validity of input frameset */
00326 
00327     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00328         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00329         return(-1);
00330     }
00331 
00332     /* Initialise some things */
00333 
00334     vircam_imcombine_init();
00335 
00336     /* Get the parameters */
00337 
00338     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.combtype");
00339     vircam_imcombine_config.combtype = cpl_parameter_get_int(p);
00340     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.scaletype");
00341     vircam_imcombine_config.scaletype = cpl_parameter_get_int(p);
00342     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.xrej");
00343     vircam_imcombine_config.xrej = cpl_parameter_get_bool(p);
00344     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.thresh");
00345     vircam_imcombine_config.thresh = (float)cpl_parameter_get_double(p);
00346     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.extenum");
00347     vircam_imcombine_config.extenum = cpl_parameter_get_int(p);
00348 
00349     /* Sort out raw from calib frames */
00350 
00351     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00352         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00353         vircam_imcombine_tidy();
00354         return(-1);
00355     }
00356 
00357     /* Get the frames frames */
00358 
00359     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00360                                            &nlab)) == NULL) {
00361         cpl_msg_error(fctid,"Cannot labelise the input frames");
00362         vircam_imcombine_tidy();
00363         return(-1);
00364     }
00365     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00366                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00367         cpl_msg_error(fctid,"Cannot find images in input frameset");
00368         vircam_imcombine_tidy();
00369         return(-1);
00370     }
00371     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00372 
00373     /* Now, how many image extensions do we want to do? If the extension
00374        number is zero, then we loop for all possible extensions. If it
00375        isn't then we just do the extension specified */
00376 
00377     vircam_exten_range(vircam_imcombine_config.extenum,
00378                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00379                        &jst,&jfn);
00380     if (jst == -1 || jfn == -1) {
00381         cpl_msg_error(fctid,"Unable to continue");
00382         vircam_imcombine_tidy();
00383         return(-1);
00384     }
00385 
00386     /* Now loop for all the extension... */
00387 
00388     status = VIR_OK;
00389     for (j = jst; j <= jfn; j++) {
00390         isfirst = (j == jst);
00391 
00392         /* Load the images */
00393 
00394         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00395 
00396         /* Call the combine module */
00397 
00398         cpl_msg_info(fctid,"Doing combination for extension %d\n",j);
00399         (void)vircam_imcombine(ps.images,ps.nimages,
00400                                vircam_imcombine_config.combtype,
00401                                vircam_imcombine_config.scaletype,
00402                                vircam_imcombine_config.xrej,
00403                                vircam_imcombine_config.thresh,
00404                                &(ps.outimage),&rejmask,&rejplus,&drs,&status);
00405         freespace(rejmask);
00406         freespace(rejplus);
00407         freepropertylist(drs);
00408         if (status != VIR_OK) {
00409             vircam_imcombine_tidy();
00410             return(-1);
00411         }
00412 
00413         /* Save everything */
00414 
00415         cpl_msg_info(fctid,"Saving combined image extension %d\n",j);
00416         retval = vircam_imcombine_save(framelist,parlist);
00417         if (retval != 0) {
00418             vircam_imcombine_tidy();
00419             return(-1);
00420         }
00421         freefitslist(ps.images,ps.nimages);
00422         freeimage(ps.outimage);
00423     }
00424     vircam_imcombine_tidy();
00425     return(0);
00426 }
00427 
00428 
00429 /*---------------------------------------------------------------------------*/
00436 /*---------------------------------------------------------------------------*/
00437 
00438 static int vircam_imcombine_save(cpl_frameset *framelist, 
00439                                  cpl_parameterlist *parlist) {
00440     cpl_propertylist *plist;
00441     const char *recipeid = "vircam_imcombine";
00442     const char *fctid = "vircam_imcombine_save";
00443     const char *outfile = "comb.fits";
00444 
00445     /* If we need to make a PHU then do that now based on the first frame
00446        in the input frame list */
00447 
00448     if (isfirst) {
00449 
00450         /* Create a new product frame object and define some tags */
00451 
00452         product_frame = cpl_frame_new();
00453         cpl_frame_set_filename(product_frame,outfile);
00454         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00455         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00456         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00457         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00458 
00459         /* Set up the phu header */
00460 
00461         plist = vircam_fits_get_phu(ps.images[0]);
00462         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00463                                               parlist,(char *)recipeid,
00464                                               "?Dictionary?");
00465         /* 'Save' the PHU image */                       
00466 
00467         if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00468                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00469             cpl_msg_error(fctid,"Cannot save product PHU");
00470             cpl_frame_delete(product_frame);
00471             return(-1);
00472         }
00473         cpl_frameset_insert(framelist,product_frame);
00474     }
00475 
00476     /* Get the extension property list */
00477 
00478     plist = vircam_fits_get_ehu(ps.images[0]);
00479 
00480     /* Fiddle with the header now */
00481 
00482     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00483                                         parlist,(char *)recipeid,
00484                                         "?Dictionary?");
00485                 
00486     /* Now save the mean dome flat image extension */
00487 
00488     if (cpl_image_save(ps.outimage,outfile,CPL_BPP_IEEE_FLOAT,plist,
00489                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00490         cpl_msg_error(fctid,"Cannot save product image extension");
00491         return(-1);
00492     }
00493 
00494     /* Get out of here */
00495 
00496     return(0);
00497 }
00498 
00499 /*---------------------------------------------------------------------------*/
00503 /*---------------------------------------------------------------------------*/
00504 
00505 static void vircam_imcombine_init(void) {
00506     ps.labels = NULL;
00507     ps.imagelist = NULL;
00508     ps.images = NULL;
00509     ps.outimage = NULL;
00510 }
00511 
00512 /*---------------------------------------------------------------------------*/
00516 /*---------------------------------------------------------------------------*/
00517 
00518 static void vircam_imcombine_tidy(void) {
00519     freespace(ps.labels);
00520     freeframeset(ps.imagelist);
00521     freefitslist(ps.images,ps.nimages);
00522     freeimage(ps.outimage);
00523 }
00524 
00527 /*
00528 
00529 $Log: vircam_imcombine.c,v $
00530 Revision 1.13  2007/10/25 19:38:22  jim
00531 modified to keep lint happy
00532 
00533 Revision 1.12  2007/10/15 12:53:55  jim
00534 Modified for compatibility with cpl_4.0
00535 
00536 Revision 1.11  2007/07/09 13:22:09  jim
00537 Modified to use new version of vircam_exten_range
00538 
00539 Revision 1.10  2007/05/02 12:53:11  jim
00540 typo fixes in docs
00541 
00542 Revision 1.9  2007/04/13 12:27:38  jim
00543 Added some extra docs
00544 
00545 Revision 1.8  2007/04/04 10:36:29  jim
00546 Modified to use new dfs tags
00547 
00548 Revision 1.7  2007/03/01 12:42:59  jim
00549 Modified slightly after code checking
00550 
00551 Revision 1.6  2006/06/15 09:58:59  jim
00552 Minor changes to docs
00553 
00554 Revision 1.5  2006/05/04 11:53:40  jim
00555 Fixed _save routine so that it's more consistent with the standard CPL
00556 way of doing things
00557 
00558 Revision 1.4  2006/05/02 11:29:14  jim
00559 Fixed problem where propertylist was being deleted incorrectly
00560 
00561 Revision 1.3  2006/04/28 08:51:00  jim
00562 Removed redundant parameter ncells
00563 
00564 Revision 1.2  2006/04/27 14:22:04  jim
00565 Fixed docs
00566 
00567 Revision 1.1  2006/04/24 10:42:44  jim
00568 New routine
00569 
00570 
00571 */

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