vircam_imcombine.c

00001 /* $Id: vircam_imcombine.c,v 1.17 2012/01/16 15:46:08 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 15:46:08 $
00024  * $Revision: 1.17 $
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 #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     cpl_size         *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 j,jst,jfn,retval,status;
00321     cpl_size nlab;
00322     cpl_parameter *p;
00323     unsigned char *rejmask,*rejplus;
00324     cpl_propertylist *drs;
00325 
00326     /* Check validity of input frameset */
00327 
00328     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00329         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00330         return(-1);
00331     }
00332 
00333     /* Initialise some things */
00334 
00335     vircam_imcombine_init();
00336 
00337     /* Get the parameters */
00338 
00339     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.combtype");
00340     vircam_imcombine_config.combtype = cpl_parameter_get_int(p);
00341     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.scaletype");
00342     vircam_imcombine_config.scaletype = cpl_parameter_get_int(p);
00343     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.xrej");
00344     vircam_imcombine_config.xrej = cpl_parameter_get_bool(p);
00345     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.thresh");
00346     vircam_imcombine_config.thresh = (float)cpl_parameter_get_double(p);
00347     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcombine.extenum");
00348     vircam_imcombine_config.extenum = cpl_parameter_get_int(p);
00349 
00350     /* Sort out raw from calib frames */
00351 
00352     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00353         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00354         vircam_imcombine_tidy();
00355         return(-1);
00356     }
00357 
00358     /* Get the frames frames */
00359 
00360     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00361                                            &nlab)) == NULL) {
00362         cpl_msg_error(fctid,"Cannot labelise the input frames");
00363         vircam_imcombine_tidy();
00364         return(-1);
00365     }
00366     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00367                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00368         cpl_msg_error(fctid,"Cannot find images in input frameset");
00369         vircam_imcombine_tidy();
00370         return(-1);
00371     }
00372     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00373 
00374     /* Now, how many image extensions do we want to do? If the extension
00375        number is zero, then we loop for all possible extensions. If it
00376        isn't then we just do the extension specified */
00377 
00378     vircam_exten_range(vircam_imcombine_config.extenum,
00379                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00380                        &jst,&jfn);
00381     if (jst == -1 || jfn == -1) {
00382         cpl_msg_error(fctid,"Unable to continue");
00383         vircam_imcombine_tidy();
00384         return(-1);
00385     }
00386 
00387     /* Now loop for all the extension... */
00388 
00389     status = VIR_OK;
00390     for (j = jst; j <= jfn; j++) {
00391         isfirst = (j == jst);
00392 
00393         /* Load the images */
00394 
00395         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00396 
00397         /* Call the combine module */
00398 
00399         cpl_msg_info(fctid,"Doing combination for extension %" CPL_SIZE_FORMAT,
00400                      (cpl_size)j);
00401         (void)vircam_imcombine(ps.images,ps.nimages,
00402                                vircam_imcombine_config.combtype,
00403                                vircam_imcombine_config.scaletype,
00404                                vircam_imcombine_config.xrej,
00405                                vircam_imcombine_config.thresh,
00406                                &(ps.outimage),&rejmask,&rejplus,&drs,&status);
00407         freespace(rejmask);
00408         freespace(rejplus);
00409         freepropertylist(drs);
00410         if (status != VIR_OK) {
00411             vircam_imcombine_tidy();
00412             return(-1);
00413         }
00414 
00415         /* Save everything */
00416 
00417         cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
00418                      (cpl_size)j);
00419         retval = vircam_imcombine_save(framelist,parlist);
00420         if (retval != 0) {
00421             vircam_imcombine_tidy();
00422             return(-1);
00423         }
00424         freefitslist(ps.images,ps.nimages);
00425         freeimage(ps.outimage);
00426     }
00427     vircam_imcombine_tidy();
00428     return(0);
00429 }
00430 
00431 
00432 /*---------------------------------------------------------------------------*/
00439 /*---------------------------------------------------------------------------*/
00440 
00441 static int vircam_imcombine_save(cpl_frameset *framelist, 
00442                                  cpl_parameterlist *parlist) {
00443     cpl_propertylist *plist;
00444     const char *recipeid = "vircam_imcombine";
00445     const char *fctid = "vircam_imcombine_save";
00446     const char *outfile = "comb.fits";
00447 
00448     /* If we need to make a PHU then do that now based on the first frame
00449        in the input frame list */
00450 
00451     if (isfirst) {
00452 
00453         /* Create a new product frame object and define some tags */
00454 
00455         product_frame = cpl_frame_new();
00456         cpl_frame_set_filename(product_frame,outfile);
00457         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00458         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00459         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00460         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00461 
00462         /* Set up the phu header */
00463 
00464         plist = vircam_fits_get_phu(ps.images[0]);
00465         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00466                                               parlist,(char *)recipeid,
00467                                               "?Dictionary?",NULL,0);
00468         /* 'Save' the PHU image */                       
00469 
00470         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00471                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00472             cpl_msg_error(fctid,"Cannot save product PHU");
00473             cpl_frame_delete(product_frame);
00474             return(-1);
00475         }
00476         cpl_frameset_insert(framelist,product_frame);
00477     }
00478 
00479     /* Get the extension property list */
00480 
00481     plist = vircam_fits_get_ehu(ps.images[0]);
00482 
00483     /* Fiddle with the header now */
00484 
00485     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00486                                         parlist,(char *)recipeid,
00487                                         "?Dictionary?",NULL);
00488                 
00489     /* Now save the mean dome flat image extension */
00490 
00491     if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
00492                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00493         cpl_msg_error(fctid,"Cannot save product image extension");
00494         return(-1);
00495     }
00496 
00497     /* Get out of here */
00498 
00499     return(0);
00500 }
00501 
00502 /*---------------------------------------------------------------------------*/
00506 /*---------------------------------------------------------------------------*/
00507 
00508 static void vircam_imcombine_init(void) {
00509     ps.labels = NULL;
00510     ps.imagelist = NULL;
00511     ps.images = NULL;
00512     ps.outimage = NULL;
00513 }
00514 
00515 /*---------------------------------------------------------------------------*/
00519 /*---------------------------------------------------------------------------*/
00520 
00521 static void vircam_imcombine_tidy(void) {
00522     freespace(ps.labels);
00523     freeframeset(ps.imagelist);
00524     freefitslist(ps.images,ps.nimages);
00525     freeimage(ps.outimage);
00526 }
00527 
00530 /*
00531 
00532 $Log: vircam_imcombine.c,v $
00533 Revision 1.17  2012/01/16 15:46:08  jim
00534 fixed missing declaration
00535 
00536 Revision 1.16  2012/01/16 14:44:02  jim
00537 Fixed test recipes for cpl6 compliance
00538 
00539 Revision 1.15  2012/01/15 17:40:09  jim
00540 Minor modifications to take into accout the changes in cpl API for v6
00541 
00542 Revision 1.14  2009/09/09 09:51:13  jim
00543 modified to use new saving routines so that headers are right
00544 
00545 Revision 1.13  2007/10/25 19:38:22  jim
00546 modified to keep lint happy
00547 
00548 Revision 1.12  2007/10/15 12:53:55  jim
00549 Modified for compatibility with cpl_4.0
00550 
00551 Revision 1.11  2007/07/09 13:22:09  jim
00552 Modified to use new version of vircam_exten_range
00553 
00554 Revision 1.10  2007/05/02 12:53:11  jim
00555 typo fixes in docs
00556 
00557 Revision 1.9  2007/04/13 12:27:38  jim
00558 Added some extra docs
00559 
00560 Revision 1.8  2007/04/04 10:36:29  jim
00561 Modified to use new dfs tags
00562 
00563 Revision 1.7  2007/03/01 12:42:59  jim
00564 Modified slightly after code checking
00565 
00566 Revision 1.6  2006/06/15 09:58:59  jim
00567 Minor changes to docs
00568 
00569 Revision 1.5  2006/05/04 11:53:40  jim
00570 Fixed _save routine so that it's more consistent with the standard CPL
00571 way of doing things
00572 
00573 Revision 1.4  2006/05/02 11:29:14  jim
00574 Fixed problem where propertylist was being deleted incorrectly
00575 
00576 Revision 1.3  2006/04/28 08:51:00  jim
00577 Removed redundant parameter ncells
00578 
00579 Revision 1.2  2006/04/27 14:22:04  jim
00580 Fixed docs
00581 
00582 Revision 1.1  2006/04/24 10:42:44  jim
00583 New routine
00584 
00585 
00586 */

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1