vircam_imstack.c

00001 /* $Id: vircam_imstack.c,v 1.3 2010/06/30 12:42:00 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: 2010/06/30 12:42:00 $
00024  * $Revision: 1.3 $
00025  * $Name: v1-1-0 $
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     int              *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 nlab,j,jst,jfn,retval,status;
00286     cpl_parameter *p;
00287     
00288 
00289     /* Check validity of input frameset */
00290 
00291     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00292         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00293         return(-1);
00294     }
00295 
00296     /* Initialise some things */
00297 
00298     vircam_imstack_init();
00299 
00300     /* Get the parameters */
00301 
00302     p = cpl_parameterlist_find(parlist,"vircam.vircam_imstack.extenum");
00303     vircam_imstack_config.extenum = cpl_parameter_get_int(p);
00304     p = cpl_parameterlist_find(parlist,"vircam.vircam_imstack.method");
00305     vircam_imstack_config.method = cpl_parameter_get_int(p);
00306 
00307     /* Sort out raw from calib frames */
00308 
00309     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00310         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00311         vircam_imstack_tidy();
00312         return(-1);
00313     }
00314 
00315     /* Get the frames frames */
00316 
00317     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00318                                            &nlab)) == NULL) {
00319         cpl_msg_error(fctid,"Cannot labelise the input frames");
00320         vircam_imstack_tidy();
00321         return(-1);
00322     }
00323     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00324                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00325         cpl_msg_error(fctid,"Cannot get images in input frameset");
00326         vircam_imstack_tidy();
00327         return(-1);
00328     }
00329     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00330     if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00331                                                 VIRCAM_CAL_CONF)) == NULL) {
00332         cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00333         vircam_imstack_tidy();
00334         return(-1);
00335     }
00336     ps.nconfs = cpl_frameset_get_size(ps.conflist);
00337     if ((ps.catlist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00338                                                VIRCAM_CAL_OBJCAT)) == NULL) {
00339         cpl_msg_info(fctid,"No object catalogues found -- no matching done");
00340     }
00341 
00342     /* Now, how many image extensions do we want to do? If the extension
00343        number is zero, then we loop for all possible extensions. If it
00344        isn't then we just do the extension specified */
00345 
00346     vircam_exten_range(vircam_imstack_config.extenum,
00347                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00348                        &jst,&jfn);
00349     if (jst == -1 || jfn == -1) {
00350         cpl_msg_error(fctid,"Unable to continue");
00351         vircam_imstack_tidy();
00352         return(-1);
00353     }
00354 
00355     /* Now loop for all the extension... */
00356 
00357     status = VIR_OK;
00358     for (j = jst; j <= jfn; j++) {
00359         isfirst = (j == jst);
00360 
00361         /* Load the images */
00362 
00363         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00364         ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00365         if (ps.catlist != NULL) 
00366             ps.cats = vircam_tfits_load_list(ps.catlist,j);
00367 
00368         /* Call the dithering module */
00369 
00370         cpl_msg_info(fctid,"Doing jittering for extension %d\n",j);
00371         (void)vircam_imstack(ps.images,ps.confs,ps.cats,ps.nimages,ps.nconfs,
00372                              5.0,5.0,vircam_imstack_config.method,
00373                              &(ps.outfits),&(ps.outfitsc),&status);
00374         if (status != VIR_OK) {
00375             vircam_imstack_tidy();
00376             return(-1);
00377         }
00378 
00379         /* Save everything */
00380 
00381         cpl_msg_info(fctid,"Saving combined image extension %d\n",j);
00382         retval = vircam_imstack_save(framelist,parlist);
00383         if (retval != 0) {
00384             vircam_imstack_tidy();
00385             return(-1);
00386         }
00387         freefitslist(ps.images,ps.nimages);
00388         freefitslist(ps.confs,ps.nconfs);
00389         freefits(ps.outfits);
00390         freefits(ps.outfitsc);
00391     }
00392     vircam_imstack_tidy();
00393     return(0);
00394 }
00395 
00396 /*---------------------------------------------------------------------------*/
00403 /*---------------------------------------------------------------------------*/
00404 
00405 static int vircam_imstack_save(cpl_frameset *framelist, 
00406                                 cpl_parameterlist *parlist) {
00407     cpl_propertylist *plist;
00408     const char *recipeid = "vircam_imstack";
00409     const char *fctid = "vircam_imstack_save";
00410     const char *outfile = "comb.fits";
00411     const char *outconf = "combconf.fits";
00412 
00413     /* If we need to make a PHU then do that now based on the first frame
00414        in the input frame list */
00415 
00416     if (isfirst) {
00417 
00418         /* Create a new product frame object and define some tags */
00419 
00420         product_frame = cpl_frame_new();
00421         cpl_frame_set_filename(product_frame,outfile);
00422         cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
00423         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00424         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00425         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00426 
00427         /* Set up header for phu */
00428 
00429         plist = vircam_fits_get_phu(ps.outfits);
00430         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00431                                               parlist,(char *)recipeid,
00432                                               "?Dictionary?",NULL,0);
00433 
00434         /* 'Save' the PHU dithered image */                      
00435 
00436         if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00437                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00438             cpl_msg_error(fctid,"Cannot save product PHU");
00439             cpl_frame_delete(product_frame);
00440             return(-1);
00441         }
00442         cpl_frameset_insert(framelist,product_frame);
00443 
00444         /* Create a new product frame object and define some tags */
00445 
00446         product_conf = cpl_frame_new();
00447         cpl_frame_set_filename(product_conf,outconf);
00448         cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00449         cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00450         cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00451         cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00452 
00453         /* 'Save' the PHU confidence map */                      
00454 
00455         if (cpl_image_save(NULL,outconf,CPL_BPP_8_UNSIGNED,plist,
00456                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00457             cpl_msg_error(fctid,"Cannot save product PHU");
00458             cpl_frame_delete(product_conf);
00459             return(-1);
00460         }
00461         cpl_frameset_insert(framelist,product_conf);
00462     }
00463 
00464     /* Get the extension property list */
00465 
00466     plist = vircam_fits_get_ehu(ps.outfits);
00467 
00468     /* Fiddle with the header now */
00469 
00470     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00471                                         parlist,(char *)recipeid,
00472                                         "?Dictionary?",NULL);
00473                 
00474     /* Now save the dithered image extension */
00475 
00476     if (cpl_image_save(vircam_fits_get_image(ps.outfits),outfile,
00477                        CPL_BPP_IEEE_FLOAT,plist,
00478                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00479         cpl_msg_error(fctid,"Cannot save dithered image extension");
00480         return(-1);
00481     }
00482 
00483     /* And the confidence map */
00484 
00485     if (cpl_image_save(vircam_fits_get_image(ps.outfitsc),outconf,
00486                        CPL_BPP_16_SIGNED,plist,
00487                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00488         cpl_msg_error(fctid,"Cannot save confidence map image extension");
00489         return(-1);
00490     }
00491 
00492     /* Get out of here */
00493 
00494     return(0);
00495 }
00496 
00497 /*---------------------------------------------------------------------------*/
00501 /*---------------------------------------------------------------------------*/
00502 
00503 static void vircam_imstack_init(void) {
00504     ps.labels = NULL;
00505     ps.imagelist = NULL;
00506     ps.conflist = NULL;
00507     ps.catlist = NULL;
00508     ps.images = NULL;
00509     ps.confs = NULL;
00510     ps.cats = NULL;
00511     ps.outfits = NULL;
00512     ps.outfitsc = NULL;
00513 }
00514 
00515 /*---------------------------------------------------------------------------*/
00519 /*---------------------------------------------------------------------------*/
00520 
00521 static void vircam_imstack_tidy(void) {
00522     freespace(ps.labels); 
00523     freeframeset(ps.imagelist);
00524     freeframeset(ps.conflist);
00525     freeframeset(ps.catlist);
00526     freefitslist(ps.images,ps.nimages);
00527     freefitslist(ps.confs,ps.nconfs);
00528     freetfitslist(ps.cats,ps.nimages);
00529     freefits(ps.outfits);
00530     freefits(ps.outfitsc);
00531 }
00532 
00535 /*
00536 
00537 $Log: vircam_imstack.c,v $
00538 Revision 1.3  2010/06/30 12:42:00  jim
00539 A few fixes to stop compiler compaints
00540 
00541 Revision 1.2  2009/09/09 09:51:13  jim
00542 modified to use new saving routines so that headers are right
00543 
00544 Revision 1.1  2008/11/21 10:17:43  jim
00545 New routine
00546 
00547 
00548 */

Generated on 7 Feb 2011 for VIRCAM Pipeline by  doxygen 1.6.1