vircam_imdither.c

00001 /* $Id: vircam_imdither.c,v 1.14 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.14 $
00025  * $Name: vcam-1_3_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_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     cpl_size         *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 j,jst,jfn,retval,status;
00274     cpl_size nlab;
00275     cpl_parameter *p;
00276     
00277 
00278     /* Check validity of input frameset */
00279 
00280     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00281         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00282         return(-1);
00283     }
00284 
00285     /* Initialise some things */
00286 
00287     vircam_imdither_init();
00288 
00289     /* Get the parameters */
00290 
00291     p = cpl_parameterlist_find(parlist,"vircam.vircam_imdither.extenum");
00292     vircam_imdither_config.extenum = cpl_parameter_get_int(p);
00293 
00294     /* Sort out raw from calib frames */
00295 
00296     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00297         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00298         vircam_imdither_tidy();
00299         return(-1);
00300     }
00301 
00302     /* Get the frames frames */
00303 
00304     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00305                                            &nlab)) == NULL) {
00306         cpl_msg_error(fctid,"Cannot labelise the input frames");
00307         vircam_imdither_tidy();
00308         return(-1);
00309     }
00310     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00311                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00312         cpl_msg_error(fctid,"Cannot get images in input frameset");
00313         vircam_imdither_tidy();
00314         return(-1);
00315     }
00316     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00317     if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00318                                                 VIRCAM_CAL_CONF)) == NULL) {
00319         cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00320         vircam_imdither_tidy();
00321         return(-1);
00322     }
00323     ps.nconfs = cpl_frameset_get_size(ps.conflist);
00324 
00325     /* Now, how many image extensions do we want to do? If the extension
00326        number is zero, then we loop for all possible extensions. If it
00327        isn't then we just do the extension specified */
00328 
00329     vircam_exten_range(vircam_imdither_config.extenum,
00330                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00331                        &jst,&jfn);
00332     if (jst == -1 || jfn == -1) {
00333         cpl_msg_error(fctid,"Unable to continue");
00334         vircam_imdither_tidy();
00335         return(-1);
00336     }
00337 
00338     /* Now loop for all the extension... */
00339 
00340     status = VIR_OK;
00341     for (j = jst; j <= jfn; j++) {
00342         isfirst = (j == jst);
00343 
00344         /* Load the images */
00345 
00346         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00347         ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00348 
00349         /* Call the dithering module */
00350 
00351         cpl_msg_info(fctid,"Doing jittering for extension %" CPL_SIZE_FORMAT,
00352                      (cpl_size)j);
00353         (void)vircam_imdither(ps.images,ps.confs,ps.nimages,ps.nconfs,
00354                               5.0,5.0,&(ps.plist),&(ps.outimage),
00355                               &(ps.outconf),&status);
00356         if (status != VIR_OK) {
00357             vircam_imdither_tidy();
00358             return(-1);
00359         }
00360 
00361         /* Save everything */
00362 
00363         cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
00364                      (cpl_size)j);
00365         retval = vircam_imdither_save(framelist,parlist);
00366         if (retval != 0) {
00367             vircam_imdither_tidy();
00368             return(-1);
00369         }
00370         freefitslist(ps.images,ps.nimages);
00371         freefitslist(ps.confs,ps.nconfs);
00372         freeimage(ps.outimage);
00373         freeimage(ps.outconf);
00374         freepropertylist(ps.plist);
00375     }
00376     vircam_imdither_tidy();
00377     return(0);
00378 }
00379 
00380 
00381 /*---------------------------------------------------------------------------*/
00388 /*---------------------------------------------------------------------------*/
00389 
00390 static int vircam_imdither_save(cpl_frameset *framelist, 
00391                                 cpl_parameterlist *parlist) {
00392     cpl_propertylist *plist;
00393     const char *recipeid = "vircam_imdither";
00394     const char *fctid = "vircam_imdither_save";
00395     const char *outfile = "comb.fits";
00396     const char *outconf = "combconf.fits";
00397 
00398     /* If we need to make a PHU then do that now based on the first frame
00399        in the input frame list */
00400 
00401     if (isfirst) {
00402 
00403         /* Create a new product frame object and define some tags */
00404 
00405         product_frame = cpl_frame_new();
00406         cpl_frame_set_filename(product_frame,outfile);
00407         cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
00408         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00409         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00410         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00411 
00412         /* Set up header for phu */
00413 
00414         plist = vircam_fits_get_phu(ps.images[0]);
00415         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00416                                               parlist,(char *)recipeid,
00417                                               "?Dictionary?",NULL,0);
00418 
00419         /* 'Save' the PHU dithered image */                      
00420 
00421         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00422                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00423             cpl_msg_error(fctid,"Cannot save product PHU");
00424             cpl_frame_delete(product_frame);
00425             return(-1);
00426         }
00427         cpl_frameset_insert(framelist,product_frame);
00428 
00429         /* Create a new product frame object and define some tags */
00430 
00431         product_conf = cpl_frame_new();
00432         cpl_frame_set_filename(product_conf,outconf);
00433         cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00434         cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00435         cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00436         cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00437 
00438         /* 'Save' the PHU confidence map */                      
00439 
00440         if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
00441                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00442             cpl_msg_error(fctid,"Cannot save product PHU");
00443             cpl_frame_delete(product_conf);
00444             return(-1);
00445         }
00446         cpl_frameset_insert(framelist,product_conf);
00447     }
00448 
00449     /* Get the extension property list */
00450 
00451     plist = ps.plist;
00452 
00453     /* Fiddle with the header now */
00454 
00455     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00456                                         parlist,(char *)recipeid,
00457                                         "?Dictionary?",NULL);
00458                 
00459     /* Now save the dithered image extension */
00460 
00461     if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
00462                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00463         cpl_msg_error(fctid,"Cannot save dithered image extension");
00464         return(-1);
00465     }
00466 
00467     /* And the confidence map */
00468 
00469     if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
00470                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00471         cpl_msg_error(fctid,"Cannot save confidence map image extension");
00472         return(-1);
00473     }
00474 
00475     /* Get out of here */
00476 
00477     return(0);
00478 }
00479 
00480 /*---------------------------------------------------------------------------*/
00484 /*---------------------------------------------------------------------------*/
00485 
00486 static void vircam_imdither_init(void) {
00487     ps.labels = NULL;
00488     ps.imagelist = NULL;
00489     ps.images = NULL;
00490     ps.conflist = NULL;
00491     ps.confs = NULL;
00492     ps.outimage = NULL;
00493     ps.outconf = NULL;
00494     ps.plist = NULL;
00495 }
00496 
00497 /*---------------------------------------------------------------------------*/
00501 /*---------------------------------------------------------------------------*/
00502 
00503 static void vircam_imdither_tidy(void) {
00504     freespace(ps.labels);
00505     freeframeset(ps.imagelist);
00506     freefitslist(ps.images,ps.nimages);
00507     freeframeset(ps.conflist);
00508     freefitslist(ps.confs,ps.nconfs);
00509     freeimage(ps.outimage);
00510     freeimage(ps.outconf);
00511     freepropertylist(ps.plist);
00512 }
00513 
00516 /*
00517 
00518 $Log: vircam_imdither.c,v $
00519 Revision 1.14  2012/01/16 14:44:02  jim
00520 Fixed test recipes for cpl6 compliance
00521 
00522 Revision 1.13  2012/01/15 17:40:09  jim
00523 Minor modifications to take into accout the changes in cpl API for v6
00524 
00525 Revision 1.12  2009/09/09 09:51:13  jim
00526 modified to use new saving routines so that headers are right
00527 
00528 Revision 1.11  2007/10/25 19:38:22  jim
00529 modified to keep lint happy
00530 
00531 Revision 1.10  2007/10/15 12:53:55  jim
00532 Modified for compatibility with cpl_4.0
00533 
00534 Revision 1.9  2007/07/09 13:22:09  jim
00535 Modified to use new version of vircam_exten_range
00536 
00537 Revision 1.8  2007/05/02 12:53:11  jim
00538 typo fixes in docs
00539 
00540 Revision 1.7  2007/04/13 12:27:38  jim
00541 Added some extra docs
00542 
00543 Revision 1.6  2007/04/04 10:36:29  jim
00544 Modified to use new dfs tags
00545 
00546 Revision 1.5  2007/03/02 12:38:32  jim
00547 Fixed small memory leak
00548 
00549 Revision 1.4  2007/03/01 12:42:59  jim
00550 Modified slightly after code checking
00551 
00552 Revision 1.3  2006/06/15 09:58:59  jim
00553 Minor changes to docs
00554 
00555 Revision 1.2  2006/06/06 13:08:25  jim
00556 Fixed minor doc problem
00557 
00558 Revision 1.1  2006/06/01 13:57:29  jim
00559 First entry
00560 
00561 
00562 */

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1