vircam_imdither.c

00001 /* $Id: vircam_imdither.c,v 1.11 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.11 $
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_imdither_create(cpl_plugin *) ;
00047 static int vircam_imdither_exec(cpl_plugin *) ;
00048 static int vircam_imdither_destroy(cpl_plugin *) ;
00049 static int vircam_imdither_test(cpl_parameterlist *, cpl_frameset *) ;
00050 static int vircam_imdither_save(cpl_frameset *framelist, 
00051                                 cpl_parameterlist *parlist);
00052 static void vircam_imdither_init(void);
00053 static void vircam_imdither_tidy(void);
00054 
00055 /* Static global variables */
00056 
00057 static struct {
00058 
00059     /* Input */
00060 
00061     int         extenum;
00062 
00063 } vircam_imdither_config;
00064 
00065 
00066 static struct {
00067     int              *labels;
00068     cpl_frameset     *imagelist;
00069     vir_fits         **images;
00070     cpl_frameset     *conflist;
00071     vir_fits         **confs;
00072     int              nimages;
00073     int              nconfs;
00074     cpl_image        *outimage;
00075     cpl_image        *outconf;
00076     cpl_propertylist *plist;
00077 } ps;
00078 
00079 static int isfirst;
00080 static cpl_frame *product_frame = NULL;
00081 static cpl_frame *product_conf = NULL;
00082 
00083 
00084 static char vircam_imdither_description[] =
00085 "vircam_imdither -- VIRCAM test jitter recipe.\n\n"
00086 "Dither a list of frames into an output frame.\n\n"
00087 "The program accepts the following files in the SOF:\n\n"
00088 "    Tag                   Description\n"
00089 "    -----------------------------------------------------------------------\n"
00090 "    %-21s A list of images\n"
00091 "    %-21s A list of confidence maps\n"
00092 "\n";
00093 
00138 /* Function code */
00139 
00140 /*---------------------------------------------------------------------------*/
00148 /*---------------------------------------------------------------------------*/
00149 
00150 int cpl_plugin_get_info(cpl_pluginlist *list) {
00151     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00152     cpl_plugin  *plugin = &recipe->interface;
00153     char alldesc[SZ_ALLDESC];
00154     (void)snprintf(alldesc,SZ_ALLDESC,vircam_imdither_description,
00155                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
00156 
00157     cpl_plugin_init(plugin,
00158                     CPL_PLUGIN_API,
00159                     VIRCAM_BINARY_VERSION,
00160                     CPL_PLUGIN_TYPE_RECIPE,
00161                     "vircam_imdither",
00162                     "VIRCAM jitter test recipe [test]",
00163                     alldesc,
00164                     "Jim Lewis",
00165                     "jrl@ast.cam.ac.uk",
00166                     vircam_get_license(),
00167                     vircam_imdither_create,
00168                     vircam_imdither_exec,
00169                     vircam_imdither_destroy);
00170 
00171     cpl_pluginlist_append(list,plugin);
00172 
00173     return(0);
00174 }
00175 
00176 /*---------------------------------------------------------------------------*/
00185 /*---------------------------------------------------------------------------*/
00186 
00187 static int vircam_imdither_create(cpl_plugin *plugin) {
00188     cpl_recipe      *recipe;
00189     cpl_parameter   *p;
00190 
00191     /* Get the recipe out of the plugin */
00192 
00193     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00194         recipe = (cpl_recipe *)plugin;
00195     else 
00196         return(-1);
00197 
00198     /* Create the parameters list in the cpl_recipe object */
00199 
00200     recipe->parameters = cpl_parameterlist_new();
00201 
00202     /* Extension number of input frames to use */
00203 
00204     p = cpl_parameter_new_range("vircam.vircam_imdither.extenum",
00205                                 CPL_TYPE_INT,
00206                                 "Extension number to be done, 0 == all",
00207                                 "vircam.vircam_imdither",
00208                                 1,0,16);
00209     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00210     cpl_parameterlist_append(recipe->parameters,p);
00211         
00212     /* Get out of here */
00213 
00214     return(0);
00215 }
00216     
00217     
00218 /*---------------------------------------------------------------------------*/
00224 /*---------------------------------------------------------------------------*/
00225 
00226 static int vircam_imdither_exec(cpl_plugin *plugin) {
00227     cpl_recipe  *recipe;
00228 
00229     /* Get the recipe out of the plugin */
00230 
00231     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00232         recipe = (cpl_recipe *)plugin;
00233     else 
00234         return(-1);
00235 
00236     return(vircam_imdither_test(recipe->parameters,recipe->frames));
00237 }
00238                                 
00239 /*---------------------------------------------------------------------------*/
00245 /*---------------------------------------------------------------------------*/
00246 
00247 static int vircam_imdither_destroy(cpl_plugin *plugin) {
00248     cpl_recipe *recipe ;
00249 
00250     /* Get the recipe out of the plugin */
00251 
00252     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00253         recipe = (cpl_recipe *)plugin;
00254     else 
00255         return(-1);
00256 
00257     cpl_parameterlist_delete(recipe->parameters);
00258     return(0);
00259 }
00260 
00261 /*---------------------------------------------------------------------------*/
00268 /*---------------------------------------------------------------------------*/
00269 
00270 static int vircam_imdither_test(cpl_parameterlist *parlist, 
00271                                 cpl_frameset *framelist) {
00272     const char *fctid="vircam_imdither";
00273     int nlab,j,jst,jfn,retval,status;
00274     cpl_parameter *p;
00275     
00276 
00277     /* Check validity of input frameset */
00278 
00279     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00280         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00281         return(-1);
00282     }
00283 
00284     /* Initialise some things */
00285 
00286     vircam_imdither_init();
00287 
00288     /* Get the parameters */
00289 
00290     p = cpl_parameterlist_find(parlist,"vircam.vircam_imdither.extenum");
00291     vircam_imdither_config.extenum = cpl_parameter_get_int(p);
00292 
00293     /* Sort out raw from calib frames */
00294 
00295     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00296         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00297         vircam_imdither_tidy();
00298         return(-1);
00299     }
00300 
00301     /* Get the frames frames */
00302 
00303     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00304                                            &nlab)) == NULL) {
00305         cpl_msg_error(fctid,"Cannot labelise the input frames");
00306         vircam_imdither_tidy();
00307         return(-1);
00308     }
00309     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00310                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00311         cpl_msg_error(fctid,"Cannot get images in input frameset");
00312         vircam_imdither_tidy();
00313         return(-1);
00314     }
00315     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00316     if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00317                                                 VIRCAM_CAL_CONF)) == NULL) {
00318         cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00319         vircam_imdither_tidy();
00320         return(-1);
00321     }
00322     ps.nconfs = cpl_frameset_get_size(ps.conflist);
00323 
00324     /* Now, how many image extensions do we want to do? If the extension
00325        number is zero, then we loop for all possible extensions. If it
00326        isn't then we just do the extension specified */
00327 
00328     vircam_exten_range(vircam_imdither_config.extenum,
00329                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00330                        &jst,&jfn);
00331     if (jst == -1 || jfn == -1) {
00332         cpl_msg_error(fctid,"Unable to continue");
00333         vircam_imdither_tidy();
00334         return(-1);
00335     }
00336 
00337     /* Now loop for all the extension... */
00338 
00339     status = VIR_OK;
00340     for (j = jst; j <= jfn; j++) {
00341         isfirst = (j == jst);
00342 
00343         /* Load the images */
00344 
00345         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00346         ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00347 
00348         /* Call the dithering module */
00349 
00350         cpl_msg_info(fctid,"Doing jittering for extension %d\n",j);
00351         (void)vircam_imdither(ps.images,ps.confs,ps.nimages,ps.nconfs,
00352                               5.0,5.0,&(ps.plist),&(ps.outimage),
00353                               &(ps.outconf),&status);
00354         if (status != VIR_OK) {
00355             vircam_imdither_tidy();
00356             return(-1);
00357         }
00358 
00359         /* Save everything */
00360 
00361         cpl_msg_info(fctid,"Saving combined image extension %d\n",j);
00362         retval = vircam_imdither_save(framelist,parlist);
00363         if (retval != 0) {
00364             vircam_imdither_tidy();
00365             return(-1);
00366         }
00367         freefitslist(ps.images,ps.nimages);
00368         freefitslist(ps.confs,ps.nconfs);
00369         freeimage(ps.outimage);
00370         freeimage(ps.outconf);
00371         freepropertylist(ps.plist);
00372     }
00373     vircam_imdither_tidy();
00374     return(0);
00375 }
00376 
00377 
00378 /*---------------------------------------------------------------------------*/
00385 /*---------------------------------------------------------------------------*/
00386 
00387 static int vircam_imdither_save(cpl_frameset *framelist, 
00388                                 cpl_parameterlist *parlist) {
00389     cpl_propertylist *plist;
00390     const char *recipeid = "vircam_imdither";
00391     const char *fctid = "vircam_imdither_save";
00392     const char *outfile = "comb.fits";
00393     const char *outconf = "combconf.fits";
00394 
00395     /* If we need to make a PHU then do that now based on the first frame
00396        in the input frame list */
00397 
00398     if (isfirst) {
00399 
00400         /* Create a new product frame object and define some tags */
00401 
00402         product_frame = cpl_frame_new();
00403         cpl_frame_set_filename(product_frame,outfile);
00404         cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
00405         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00406         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00407         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00408 
00409         /* Set up header for phu */
00410 
00411         plist = vircam_fits_get_phu(ps.images[0]);
00412         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00413                                               parlist,(char *)recipeid,
00414                                               "?Dictionary?");
00415 
00416         /* 'Save' the PHU dithered image */                      
00417 
00418         if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00419                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00420             cpl_msg_error(fctid,"Cannot save product PHU");
00421             cpl_frame_delete(product_frame);
00422             return(-1);
00423         }
00424         cpl_frameset_insert(framelist,product_frame);
00425 
00426         /* Create a new product frame object and define some tags */
00427 
00428         product_conf = cpl_frame_new();
00429         cpl_frame_set_filename(product_conf,outconf);
00430         cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00431         cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00432         cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00433         cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00434 
00435         /* 'Save' the PHU confidence map */                      
00436 
00437         if (cpl_image_save(NULL,outconf,CPL_BPP_8_UNSIGNED,plist,
00438                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00439             cpl_msg_error(fctid,"Cannot save product PHU");
00440             cpl_frame_delete(product_conf);
00441             return(-1);
00442         }
00443         cpl_frameset_insert(framelist,product_conf);
00444     }
00445 
00446     /* Get the extension property list */
00447 
00448     plist = ps.plist;
00449 
00450     /* Fiddle with the header now */
00451 
00452     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00453                                         parlist,(char *)recipeid,
00454                                         "?Dictionary?");
00455                 
00456     /* Now save the dithered image extension */
00457 
00458     if (cpl_image_save(ps.outimage,outfile,CPL_BPP_IEEE_FLOAT,plist,
00459                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00460         cpl_msg_error(fctid,"Cannot save dithered image extension");
00461         return(-1);
00462     }
00463 
00464     /* And the confidence map */
00465 
00466     if (cpl_image_save(ps.outconf,outconf,CPL_BPP_16_SIGNED,plist,
00467                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00468         cpl_msg_error(fctid,"Cannot save confidence map image extension");
00469         return(-1);
00470     }
00471 
00472     /* Get out of here */
00473 
00474     return(0);
00475 }
00476 
00477 /*---------------------------------------------------------------------------*/
00481 /*---------------------------------------------------------------------------*/
00482 
00483 static void vircam_imdither_init(void) {
00484     ps.labels = NULL;
00485     ps.imagelist = NULL;
00486     ps.images = NULL;
00487     ps.conflist = NULL;
00488     ps.confs = NULL;
00489     ps.outimage = NULL;
00490     ps.outconf = NULL;
00491     ps.plist = NULL;
00492 }
00493 
00494 /*---------------------------------------------------------------------------*/
00498 /*---------------------------------------------------------------------------*/
00499 
00500 static void vircam_imdither_tidy(void) {
00501     freespace(ps.labels);
00502     freeframeset(ps.imagelist);
00503     freefitslist(ps.images,ps.nimages);
00504     freeframeset(ps.conflist);
00505     freefitslist(ps.confs,ps.nconfs);
00506     freeimage(ps.outimage);
00507     freeimage(ps.outconf);
00508     freepropertylist(ps.plist);
00509 }
00510 
00513 /*
00514 
00515 $Log: vircam_imdither.c,v $
00516 Revision 1.11  2007/10/25 19:38:22  jim
00517 modified to keep lint happy
00518 
00519 Revision 1.10  2007/10/15 12:53:55  jim
00520 Modified for compatibility with cpl_4.0
00521 
00522 Revision 1.9  2007/07/09 13:22:09  jim
00523 Modified to use new version of vircam_exten_range
00524 
00525 Revision 1.8  2007/05/02 12:53:11  jim
00526 typo fixes in docs
00527 
00528 Revision 1.7  2007/04/13 12:27:38  jim
00529 Added some extra docs
00530 
00531 Revision 1.6  2007/04/04 10:36:29  jim
00532 Modified to use new dfs tags
00533 
00534 Revision 1.5  2007/03/02 12:38:32  jim
00535 Fixed small memory leak
00536 
00537 Revision 1.4  2007/03/01 12:42:59  jim
00538 Modified slightly after code checking
00539 
00540 Revision 1.3  2006/06/15 09:58:59  jim
00541 Minor changes to docs
00542 
00543 Revision 1.2  2006/06/06 13:08:25  jim
00544 Fixed minor doc problem
00545 
00546 Revision 1.1  2006/06/01 13:57:29  jim
00547 First entry
00548 
00549 
00550 */

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