vircam_imstack.c

00001 /* $Id: vircam_imstack.c,v 1.5 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.5 $
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_imstack_create(cpl_plugin *) ;
00047 static int vircam_imstack_exec(cpl_plugin *) ;
00048 static int vircam_imstack_destroy(cpl_plugin *) ;
00049 static int vircam_imstack_test(cpl_parameterlist *, cpl_frameset *) ;
00050 static int vircam_imstack_save(cpl_frameset *framelist, 
00051                                 cpl_parameterlist *parlist);
00052 static void vircam_imstack_init(void);
00053 static void vircam_imstack_tidy(void);
00054 
00055 /* Static global variables */
00056 
00057 static struct {
00058 
00059     /* Input */
00060 
00061     int         method;
00062     int         extenum;
00063 
00064 } vircam_imstack_config;
00065 
00066 
00067 static struct {
00068     cpl_size         *labels;
00069     cpl_frameset     *imagelist;
00070     vir_fits         **images;
00071     cpl_frameset     *conflist;
00072     vir_fits         **confs;
00073     cpl_frameset     *catlist;
00074     vir_tfits        **cats;
00075     int              nimages;
00076     int              nconfs;
00077     vir_fits         *outfits;
00078     vir_fits         *outfitsc;
00079 } ps;
00080 
00081 static int isfirst;
00082 static cpl_frame *product_frame = NULL;
00083 static cpl_frame *product_conf = NULL;
00084 
00085 
00086 static char vircam_imstack_description[] =
00087 "vircam_imstack -- VIRCAM test jitter recipe.\n\n"
00088 "Dither a list of frames into an output frame.\n\n"
00089 "The program accepts the following files in the SOF:\n\n"
00090 "    Tag                   Description\n"
00091 "    -----------------------------------------------------------------------\n"
00092 "    %-21s A list of images\n"
00093 "    %-21s A list of confidence maps\n"
00094 "    %-21s An optional list of object catalogues\n"
00095 "\n";
00096 
00141 /* Function code */
00142 
00143 /*---------------------------------------------------------------------------*/
00151 /*---------------------------------------------------------------------------*/
00152 
00153 int cpl_plugin_get_info(cpl_pluginlist *list) {
00154     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00155     cpl_plugin  *plugin = &recipe->interface;
00156     char alldesc[SZ_ALLDESC];
00157     (void)snprintf(alldesc,SZ_ALLDESC,vircam_imstack_description,
00158                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF,VIRCAM_CAL_OBJCAT);
00159 
00160     cpl_plugin_init(plugin,
00161                     CPL_PLUGIN_API,
00162                     VIRCAM_BINARY_VERSION,
00163                     CPL_PLUGIN_TYPE_RECIPE,
00164                     "vircam_imstack",
00165                     "VIRCAM jitter test recipe [test]",
00166                     alldesc,
00167                     "Jim Lewis",
00168                     "jrl@ast.cam.ac.uk",
00169                     vircam_get_license(),
00170                     vircam_imstack_create,
00171                     vircam_imstack_exec,
00172                     vircam_imstack_destroy);
00173 
00174     cpl_pluginlist_append(list,plugin);
00175 
00176     return(0);
00177 }
00178 
00179 /*---------------------------------------------------------------------------*/
00188 /*---------------------------------------------------------------------------*/
00189 
00190 static int vircam_imstack_create(cpl_plugin *plugin) {
00191     cpl_recipe      *recipe;
00192     cpl_parameter   *p;
00193 
00194     /* Get the recipe out of the plugin */
00195 
00196     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00197         recipe = (cpl_recipe *)plugin;
00198     else 
00199         return(-1);
00200 
00201     /* Create the parameters list in the cpl_recipe object */
00202 
00203     recipe->parameters = cpl_parameterlist_new();
00204 
00205     /* Combination method */
00206 
00207     p = cpl_parameter_new_enum("vircam.vircam_imstack.method",
00208                                CPL_TYPE_INT,"Combination method",
00209                                "vircam.vircam_imstack",0,2,0,1);
00210     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"method");
00211     cpl_parameterlist_append(recipe->parameters,p);
00212     
00213 
00214     /* Extension number of input frames to use */
00215 
00216     p = cpl_parameter_new_range("vircam.vircam_imstack.extenum",
00217                                 CPL_TYPE_INT,
00218                                 "Extension number to be done, 0 == all",
00219                                 "vircam.vircam_imstack",
00220                                 1,0,16);
00221     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00222     cpl_parameterlist_append(recipe->parameters,p);
00223         
00224     /* Get out of here */
00225 
00226     return(0);
00227 }
00228     
00229     
00230 /*---------------------------------------------------------------------------*/
00236 /*---------------------------------------------------------------------------*/
00237 
00238 static int vircam_imstack_exec(cpl_plugin *plugin) {
00239     cpl_recipe  *recipe;
00240 
00241     /* Get the recipe out of the plugin */
00242 
00243     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00244         recipe = (cpl_recipe *)plugin;
00245     else 
00246         return(-1);
00247 
00248     return(vircam_imstack_test(recipe->parameters,recipe->frames));
00249 }
00250                                 
00251 /*---------------------------------------------------------------------------*/
00257 /*---------------------------------------------------------------------------*/
00258 
00259 static int vircam_imstack_destroy(cpl_plugin *plugin) {
00260     cpl_recipe *recipe ;
00261 
00262     /* Get the recipe out of the plugin */
00263 
00264     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00265         recipe = (cpl_recipe *)plugin;
00266     else 
00267         return(-1);
00268 
00269     cpl_parameterlist_delete(recipe->parameters);
00270     return(0);
00271 }
00272 
00273 /*---------------------------------------------------------------------------*/
00280 /*---------------------------------------------------------------------------*/
00281 
00282 static int vircam_imstack_test(cpl_parameterlist *parlist, 
00283                                 cpl_frameset *framelist) {
00284     const char *fctid="vircam_imstack";
00285     int j,jst,jfn,retval,status;
00286     cpl_size nlab;
00287     cpl_parameter *p;
00288     
00289 
00290     /* Check validity of input frameset */
00291 
00292     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00293         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00294         return(-1);
00295     }
00296 
00297     /* Initialise some things */
00298 
00299     vircam_imstack_init();
00300 
00301     /* Get the parameters */
00302 
00303     p = cpl_parameterlist_find(parlist,"vircam.vircam_imstack.extenum");
00304     vircam_imstack_config.extenum = cpl_parameter_get_int(p);
00305     p = cpl_parameterlist_find(parlist,"vircam.vircam_imstack.method");
00306     vircam_imstack_config.method = cpl_parameter_get_int(p);
00307 
00308     /* Sort out raw from calib frames */
00309 
00310     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00311         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00312         vircam_imstack_tidy();
00313         return(-1);
00314     }
00315 
00316     /* Get the frames frames */
00317 
00318     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00319                                            &nlab)) == NULL) {
00320         cpl_msg_error(fctid,"Cannot labelise the input frames");
00321         vircam_imstack_tidy();
00322         return(-1);
00323     }
00324     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00325                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00326         cpl_msg_error(fctid,"Cannot get images in input frameset");
00327         vircam_imstack_tidy();
00328         return(-1);
00329     }
00330     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00331     if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00332                                                 VIRCAM_CAL_CONF)) == NULL) {
00333         cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00334         vircam_imstack_tidy();
00335         return(-1);
00336     }
00337     ps.nconfs = cpl_frameset_get_size(ps.conflist);
00338     if ((ps.catlist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00339                                                VIRCAM_CAL_OBJCAT)) == NULL) {
00340         cpl_msg_info(fctid,"No object catalogues found -- no matching done");
00341     }
00342 
00343     /* Now, how many image extensions do we want to do? If the extension
00344        number is zero, then we loop for all possible extensions. If it
00345        isn't then we just do the extension specified */
00346 
00347     vircam_exten_range(vircam_imstack_config.extenum,
00348                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00349                        &jst,&jfn);
00350     if (jst == -1 || jfn == -1) {
00351         cpl_msg_error(fctid,"Unable to continue");
00352         vircam_imstack_tidy();
00353         return(-1);
00354     }
00355 
00356     /* Now loop for all the extension... */
00357 
00358     status = VIR_OK;
00359     for (j = jst; j <= jfn; j++) {
00360         isfirst = (j == jst);
00361 
00362         /* Load the images */
00363 
00364         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00365         ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00366         if (ps.catlist != NULL) 
00367             ps.cats = vircam_tfits_load_list(ps.catlist,j);
00368 
00369         /* Call the dithering module */
00370 
00371         cpl_msg_info(fctid,"Doing jittering for extension %" CPL_SIZE_FORMAT,
00372                      (cpl_size)j);
00373         (void)vircam_imstack(ps.images,ps.confs,ps.cats,ps.nimages,ps.nconfs,
00374                              5.0,5.0,vircam_imstack_config.method,
00375                              &(ps.outfits),&(ps.outfitsc),&status);
00376         if (status != VIR_OK) {
00377             vircam_imstack_tidy();
00378             return(-1);
00379         }
00380 
00381         /* Save everything */
00382 
00383         cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
00384                      (cpl_size)j);
00385         retval = vircam_imstack_save(framelist,parlist);
00386         if (retval != 0) {
00387             vircam_imstack_tidy();
00388             return(-1);
00389         }
00390         freefitslist(ps.images,ps.nimages);
00391         freefitslist(ps.confs,ps.nconfs);
00392         freefits(ps.outfits);
00393         freefits(ps.outfitsc);
00394     }
00395     vircam_imstack_tidy();
00396     return(0);
00397 }
00398 
00399 /*---------------------------------------------------------------------------*/
00406 /*---------------------------------------------------------------------------*/
00407 
00408 static int vircam_imstack_save(cpl_frameset *framelist, 
00409                                 cpl_parameterlist *parlist) {
00410     cpl_propertylist *plist;
00411     const char *recipeid = "vircam_imstack";
00412     const char *fctid = "vircam_imstack_save";
00413     const char *outfile = "comb.fits";
00414     const char *outconf = "combconf.fits";
00415 
00416     /* If we need to make a PHU then do that now based on the first frame
00417        in the input frame list */
00418 
00419     if (isfirst) {
00420 
00421         /* Create a new product frame object and define some tags */
00422 
00423         product_frame = cpl_frame_new();
00424         cpl_frame_set_filename(product_frame,outfile);
00425         cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
00426         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00427         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00428         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00429 
00430         /* Set up header for phu */
00431 
00432         plist = vircam_fits_get_phu(ps.outfits);
00433         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00434                                               parlist,(char *)recipeid,
00435                                               "?Dictionary?",NULL,0);
00436 
00437         /* 'Save' the PHU dithered image */                      
00438 
00439         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00440                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00441             cpl_msg_error(fctid,"Cannot save product PHU");
00442             cpl_frame_delete(product_frame);
00443             return(-1);
00444         }
00445         cpl_frameset_insert(framelist,product_frame);
00446 
00447         /* Create a new product frame object and define some tags */
00448 
00449         product_conf = cpl_frame_new();
00450         cpl_frame_set_filename(product_conf,outconf);
00451         cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00452         cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00453         cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00454         cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00455 
00456         /* 'Save' the PHU confidence map */                      
00457 
00458         if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
00459                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00460             cpl_msg_error(fctid,"Cannot save product PHU");
00461             cpl_frame_delete(product_conf);
00462             return(-1);
00463         }
00464         cpl_frameset_insert(framelist,product_conf);
00465     }
00466 
00467     /* Get the extension property list */
00468 
00469     plist = vircam_fits_get_ehu(ps.outfits);
00470 
00471     /* Fiddle with the header now */
00472 
00473     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00474                                         parlist,(char *)recipeid,
00475                                         "?Dictionary?",NULL);
00476                 
00477     /* Now save the dithered image extension */
00478 
00479     if (cpl_image_save(vircam_fits_get_image(ps.outfits),outfile,
00480                        CPL_TYPE_FLOAT,plist,
00481                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00482         cpl_msg_error(fctid,"Cannot save dithered image extension");
00483         return(-1);
00484     }
00485 
00486     /* And the confidence map */
00487 
00488     if (cpl_image_save(vircam_fits_get_image(ps.outfitsc),outconf,
00489                        CPL_TYPE_SHORT,plist,
00490                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00491         cpl_msg_error(fctid,"Cannot save confidence map image extension");
00492         return(-1);
00493     }
00494 
00495     /* Get out of here */
00496 
00497     return(0);
00498 }
00499 
00500 /*---------------------------------------------------------------------------*/
00504 /*---------------------------------------------------------------------------*/
00505 
00506 static void vircam_imstack_init(void) {
00507     ps.labels = NULL;
00508     ps.imagelist = NULL;
00509     ps.conflist = NULL;
00510     ps.catlist = NULL;
00511     ps.images = NULL;
00512     ps.confs = NULL;
00513     ps.cats = NULL;
00514     ps.outfits = NULL;
00515     ps.outfitsc = NULL;
00516 }
00517 
00518 /*---------------------------------------------------------------------------*/
00522 /*---------------------------------------------------------------------------*/
00523 
00524 static void vircam_imstack_tidy(void) {
00525     freespace(ps.labels); 
00526     freeframeset(ps.imagelist);
00527     freeframeset(ps.conflist);
00528     freeframeset(ps.catlist);
00529     freefitslist(ps.images,ps.nimages);
00530     freefitslist(ps.confs,ps.nconfs);
00531     freetfitslist(ps.cats,ps.nimages);
00532     freefits(ps.outfits);
00533     freefits(ps.outfitsc);
00534 }
00535 
00538 /*
00539 
00540 $Log: vircam_imstack.c,v $
00541 Revision 1.5  2012/01/16 14:44:02  jim
00542 Fixed test recipes for cpl6 compliance
00543 
00544 Revision 1.4  2012/01/15 17:40:09  jim
00545 Minor modifications to take into accout the changes in cpl API for v6
00546 
00547 Revision 1.3  2010/06/30 12:42:00  jim
00548 A few fixes to stop compiler compaints
00549 
00550 Revision 1.2  2009/09/09 09:51:13  jim
00551 modified to use new saving routines so that headers are right
00552 
00553 Revision 1.1  2008/11/21 10:17:43  jim
00554 New routine
00555 
00556 
00557 */

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1