vircam_interleave.c

00001 /* $Id: vircam_interleave.c,v 1.23 2012/01/16 15:47:24 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:47:24 $
00024  * $Revision: 1.23 $
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_stats.h"
00043 #include "vircam_fits.h"
00044 #include "vircam_wcsutils.h"
00045 
00046 /* Function prototypes */
00047 
00048 static int vircam_interleave_create(cpl_plugin *) ;
00049 static int vircam_interleave_exec(cpl_plugin *) ;
00050 static int vircam_interleave_destroy(cpl_plugin *) ;
00051 static int vircam_interleave_test(cpl_parameterlist *, cpl_frameset *) ;
00052 static int vircam_interleave_save(cpl_frameset *framelist, 
00053                                   cpl_parameterlist *parlist);
00054 static void vircam_interleave_init(void);
00055 static void vircam_interleave_tidy(void);
00056 
00057 /* Static global variables */
00058 
00059 static struct {
00060 
00061     /* Input */
00062 
00063     int         extenum;
00064 
00065 } vircam_interleave_config;
00066 
00067 
00068 static struct {
00069     cpl_size         *labels;
00070     cpl_frameset     *imagelist;
00071     vir_fits         **images;
00072     cpl_frameset     *conflist;
00073     vir_fits         **confs;
00074     int              nimages;
00075     int              nconfs;
00076     cpl_image        *outimage;
00077     cpl_image        *outconf;
00078     cpl_propertylist *plist;
00079 } ps;
00080 
00081 static int isfirst;
00082 static cpl_frame *product_frame = NULL;
00083 static cpl_frame *product_conf = NULL;
00084 
00085 static char vircam_interleave_description[] =
00086 "vircam_interleave -- VIRCAM test interleave recipe.\n\n"
00087 "Interleave a list of frames into an output frame.\n\n"
00088 "The program accepts the following files in the SOF:\n\n"
00089 "    Tag                   Description\n"
00090 "    -----------------------------------------------------------------------\n"
00091 "    %-21s A list of images\n"
00092 "    %-21s A list of confidence maps\n"
00093 "\n";
00094 
00137 /* Function code */
00138 
00139 /*---------------------------------------------------------------------------*/
00147 /*---------------------------------------------------------------------------*/
00148 
00149 int cpl_plugin_get_info(cpl_pluginlist *list) {
00150     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00151     cpl_plugin  *plugin = &recipe->interface;
00152     char alldesc[SZ_ALLDESC];
00153     (void)snprintf(alldesc,SZ_ALLDESC,vircam_interleave_description,
00154                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
00155 
00156     cpl_plugin_init(plugin,
00157                     CPL_PLUGIN_API,
00158                     VIRCAM_BINARY_VERSION,
00159                     CPL_PLUGIN_TYPE_RECIPE,
00160                     "vircam_interleave",
00161                     "VIRCAM interleaf test recipe [test]",
00162                     alldesc,
00163                     "Jim Lewis",
00164                     "jrl@ast.cam.ac.uk",
00165                     vircam_get_license(),
00166                     vircam_interleave_create,
00167                     vircam_interleave_exec,
00168                     vircam_interleave_destroy);
00169 
00170     cpl_pluginlist_append(list,plugin);
00171 
00172     return(0);
00173 }
00174 
00175 /*---------------------------------------------------------------------------*/
00184 /*---------------------------------------------------------------------------*/
00185 
00186 static int vircam_interleave_create(cpl_plugin *plugin) {
00187     cpl_recipe      *recipe;
00188     cpl_parameter   *p;
00189 
00190     /* Get the recipe out of the plugin */
00191 
00192     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00193         recipe = (cpl_recipe *)plugin;
00194     else 
00195         return(-1);
00196 
00197     /* Create the parameters list in the cpl_recipe object */
00198 
00199     recipe->parameters = cpl_parameterlist_new();
00200 
00201     /* Extension number of input frames to use */
00202 
00203     p = cpl_parameter_new_range("vircam.vircam_interleave.extenum",
00204                                 CPL_TYPE_INT,
00205                                 "Extension number to be done, 0 == all",
00206                                 "vircam.vircam_interleave",
00207                                 1,0,16);
00208     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00209     cpl_parameterlist_append(recipe->parameters,p);
00210         
00211     /* Get out of here */
00212 
00213     return(0);
00214 }
00215     
00216     
00217 /*---------------------------------------------------------------------------*/
00223 /*---------------------------------------------------------------------------*/
00224 
00225 static int vircam_interleave_exec(cpl_plugin *plugin) {
00226     cpl_recipe  *recipe;
00227 
00228     /* Get the recipe out of the plugin */
00229 
00230     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00231         recipe = (cpl_recipe *)plugin;
00232     else 
00233         return(-1);
00234 
00235     return(vircam_interleave_test(recipe->parameters,recipe->frames));
00236 }
00237                                 
00238 /*---------------------------------------------------------------------------*/
00244 /*---------------------------------------------------------------------------*/
00245 
00246 static int vircam_interleave_destroy(cpl_plugin *plugin) {
00247     cpl_recipe *recipe ;
00248 
00249     /* Get the recipe out of the plugin */
00250 
00251     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00252         recipe = (cpl_recipe *)plugin;
00253     else 
00254         return(-1);
00255 
00256     cpl_parameterlist_delete(recipe->parameters);
00257     return(0);
00258 }
00259 
00260 /*---------------------------------------------------------------------------*/
00267 /*---------------------------------------------------------------------------*/
00268 
00269 static int vircam_interleave_test(cpl_parameterlist *parlist, 
00270                                   cpl_frameset *framelist) {
00271     const char *fctid="vircam_interleave";
00272     int j,jst,jfn,retval,status,i,nstep;
00273     cpl_size nlab;
00274     const int *dims;
00275     long npts;
00276     float val;
00277     double refx,refy,refra,refdec,x,y;
00278     cpl_parameter *p;
00279     cpl_image *image;
00280     const cpl_array *a;
00281     cpl_wcs *wcs;
00282     
00283 
00284     /* Check validity of input frameset */
00285 
00286     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00287         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00288         return(-1);
00289     }
00290 
00291     /* Initialise some things */
00292 
00293     vircam_interleave_init();
00294 
00295     /* Get the parameters */
00296 
00297     p = cpl_parameterlist_find(parlist,"vircam.vircam_interleave.extenum");
00298     vircam_interleave_config.extenum = cpl_parameter_get_int(p);
00299 
00300     /* Sort out raw from calib frames */
00301 
00302     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00303         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00304         vircam_interleave_tidy();
00305         return(-1);
00306     }
00307 
00308     /* Get the frames frames */
00309 
00310     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00311                                            &nlab)) == NULL) {
00312         cpl_msg_error(fctid,"Cannot labelise the input frames");
00313         vircam_interleave_tidy();
00314         return(-1);
00315     }
00316     if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00317                                                 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00318         cpl_msg_error(fctid,"Cannot get images in input frameset");
00319         vircam_interleave_tidy();
00320         return(-1);
00321     }
00322     ps.nimages = cpl_frameset_get_size(ps.imagelist);
00323     nstep = (int)sqrt((float)ps.nimages);
00324     if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00325                                                 VIRCAM_CAL_CONF)) == NULL) {
00326         cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
00327         vircam_interleave_tidy();
00328         return(-1);
00329     }
00330     ps.nconfs = cpl_frameset_get_size(ps.conflist);
00331 
00332     /* Now, how many image extensions do we want to do? If the extension
00333        number is zero, then we loop for all possible extensions. If it
00334        isn't then we just do the extension specified */
00335 
00336     vircam_exten_range(vircam_interleave_config.extenum,
00337                        (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
00338                        &jst,&jfn);
00339     if (jst == -1 || jfn == -1) {
00340         cpl_msg_error(fctid,"Unable to continue");
00341         vircam_interleave_tidy();
00342         return(-1);
00343     }
00344 
00345     /* Now loop for all the extension... */
00346 
00347     status = VIR_OK;
00348     for (j = jst; j <= jfn; j++) {
00349         isfirst = (j == jst);
00350 
00351         /* Load the images */
00352 
00353         ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
00354         ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
00355 
00356         /* Get some information that you need for the interleaving. Start
00357            by getting the background level add it to the extension property
00358            list */
00359 
00360         refx = 0.0;
00361         refy = 0.0;
00362         for (i = 0; i < ps.nimages; i++) {
00363             image = vircam_fits_get_image(ps.images[i]);
00364             npts = vircam_getnpts(image);
00365             val = vircam_med(cpl_image_get_data_float(image),NULL,npts);
00366             cpl_propertylist_update_float(vircam_fits_get_ehu(ps.images[i]),
00367                                           "ESO DRS BACKMED",val);
00368             wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ps.images[i]));
00369             if (i == 0) {
00370                 a = cpl_wcs_get_image_dims(wcs);
00371                 dims = cpl_array_get_data_int_const(a);
00372                 refx = 0.5*(double)dims[0];
00373                 refy = 0.5*(double)dims[1];
00374                 vircam_xytoradec(wcs,refx,refy,&refra,&refdec);
00375             }
00376             vircam_radectoxy(wcs,refra,refdec,&x,&y);
00377             x = refx - x;
00378             y = refy - y;
00379             cpl_propertylist_update_double(vircam_fits_get_ehu(ps.images[i]),
00380                                            "ESO DRS XOFFMICRO",x);
00381             cpl_propertylist_update_double(vircam_fits_get_ehu(ps.images[i]),
00382                                            "ESO DRS YOFFMICRO",y);
00383             cpl_wcs_delete(wcs);
00384         }
00385 
00386         /* Call the interleaf module */
00387 
00388         cpl_msg_info(fctid,"Doing interleaving for extension %" CPL_SIZE_FORMAT,
00389                      (cpl_size)j);
00390         (void)vircam_interleave(ps.images,ps.nimages,ps.confs,ps.nconfs,
00391                                 nstep,&(ps.plist),&(ps.outimage),&(ps.outconf),
00392                                 &status);
00393         if (status != VIR_OK) {
00394             vircam_interleave_tidy();
00395             return(-1);
00396         }
00397 
00398         /* Save everything */
00399 
00400         cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
00401                      (cpl_size)j);
00402         retval = vircam_interleave_save(framelist,parlist);
00403         if (retval != 0) {
00404             vircam_interleave_tidy();
00405             return(-1);
00406         }
00407         freefitslist(ps.images,ps.nimages);
00408         freefitslist(ps.confs,ps.nconfs);
00409         freeimage(ps.outimage);
00410         freeimage(ps.outconf);
00411         freepropertylist(ps.plist);
00412     }
00413     vircam_interleave_tidy();
00414     return(0);
00415 }
00416 
00417 
00418 /*---------------------------------------------------------------------------*/
00426 /*---------------------------------------------------------------------------*/
00427 
00428 static int vircam_interleave_save(cpl_frameset *framelist, 
00429                                   cpl_parameterlist *parlist) {
00430     cpl_propertylist *plist;
00431     const char *recipeid = "vircam_interleave";
00432     const char *fctid = "vircam_interleave_save";
00433     const char *outfile = "comb.fits";
00434     const char *outconf = "combconf.fits";
00435 
00436     /* If we need to make a PHU then do that now based on the first frame
00437        in the input frame list */
00438 
00439     if (isfirst) {
00440 
00441         /* Create a new product frame object and define some tags */
00442 
00443         product_frame = cpl_frame_new();
00444         cpl_frame_set_filename(product_frame,outfile);
00445         cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_TEST);
00446         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00447         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00448         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00449 
00450         /* Set up header for phu */
00451 
00452         plist = vircam_fits_get_phu(ps.images[0]);
00453         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00454                                               parlist,(char *)recipeid,
00455                                               "?Dictionary?",NULL,0);
00456 
00457         /* 'Save' the PHU interleaved image */                   
00458 
00459         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00460                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00461             cpl_msg_error(fctid,"Cannot save product PHU");
00462             cpl_frame_delete(product_frame);
00463             return(-1);
00464         }
00465         cpl_frameset_insert(framelist,product_frame);
00466 
00467         /* Create a new product frame object and define some tags */
00468 
00469         product_conf = cpl_frame_new();
00470         cpl_frame_set_filename(product_conf,outconf);
00471         cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
00472         cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
00473         cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
00474         cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
00475 
00476         /* 'Save' the PHU confidence map image */                        
00477 
00478         if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
00479                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00480             cpl_msg_error(fctid,"Cannot save product PHU");
00481             cpl_frame_delete(product_conf);
00482             return(-1);
00483         }
00484         cpl_frameset_insert(framelist,product_conf);
00485     }
00486 
00487     /* Get the extension property list */
00488 
00489     plist = ps.plist;
00490 
00491     /* Fiddle with the header now */
00492 
00493     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00494                                         parlist,(char *)recipeid,
00495                                         "?Dictionary?",NULL);
00496                 
00497     /* Now save the interleaved image extension */
00498 
00499     if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
00500                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00501         cpl_msg_error(fctid,"Cannot save interleaved image extension");
00502         return(-1);
00503     }
00504 
00505     /* And the confidence map */
00506 
00507     if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
00508                        CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00509         cpl_msg_error(fctid,"Cannot save confidence map image extension");
00510         return(-1);
00511     }
00512 
00513     /* Get out of here */
00514 
00515     return(0);
00516 }
00517 
00518 /*---------------------------------------------------------------------------*/
00522 /*---------------------------------------------------------------------------*/
00523 
00524 static void vircam_interleave_init(void) {
00525     ps.labels = NULL;
00526     ps.imagelist = NULL;
00527     ps.images = NULL;
00528     ps.conflist = NULL;
00529     ps.confs = NULL;
00530     ps.outimage = NULL;
00531     ps.outconf = NULL;
00532     ps.plist = NULL;
00533 }
00534 
00535 /*---------------------------------------------------------------------------*/
00539 /*---------------------------------------------------------------------------*/
00540 
00541 static void vircam_interleave_tidy(void) {
00542     freespace(ps.labels);
00543     freeframeset(ps.imagelist);
00544     freefitslist(ps.images,ps.nimages);
00545     freeframeset(ps.conflist);
00546     freefitslist(ps.confs,ps.nconfs);
00547     freeimage(ps.outimage);
00548     freeimage(ps.outconf);
00549     freepropertylist(ps.plist);
00550 }
00551 
00555 /*
00556 
00557 $Log: vircam_interleave.c,v $
00558 Revision 1.23  2012/01/16 15:47:24  jim
00559 fixed typo
00560 
00561 Revision 1.22  2012/01/16 14:44:02  jim
00562 Fixed test recipes for cpl6 compliance
00563 
00564 Revision 1.21  2012/01/15 17:40:09  jim
00565 Minor modifications to take into accout the changes in cpl API for v6
00566 
00567 Revision 1.20  2010/07/13 11:16:50  jim
00568 A few changes to deal with compiler whinges
00569 
00570 Revision 1.19  2010/06/30 12:42:00  jim
00571 A few fixes to stop compiler compaints
00572 
00573 Revision 1.18  2009/09/09 09:51:13  jim
00574 modified to use new saving routines so that headers are right
00575 
00576 Revision 1.17  2008/07/10 13:01:35  jim
00577 Modified to use v4.2 version of cpl_wcs
00578 
00579 Revision 1.16  2008/06/20 11:13:02  jim
00580 Fixed dodgy call to cpl_wcs_get_image_dims
00581 
00582 Revision 1.15  2008/05/06 08:40:43  jim
00583 Modified to use cpl_wcs interface
00584 
00585 Revision 1.14  2007/10/25 19:38:22  jim
00586 modified to keep lint happy
00587 
00588 Revision 1.13  2007/10/15 12:53:55  jim
00589 Modified for compatibility with cpl_4.0
00590 
00591 Revision 1.12  2007/07/09 13:22:09  jim
00592 Modified to use new version of vircam_exten_range
00593 
00594 Revision 1.11  2007/05/02 12:53:11  jim
00595 typo fixes in docs
00596 
00597 Revision 1.10  2007/04/13 12:27:39  jim
00598 Added some extra docs
00599 
00600 Revision 1.9  2007/04/04 10:36:29  jim
00601 Modified to use new dfs tags
00602 
00603 Revision 1.8  2007/03/02 12:38:33  jim
00604 Fixed small memory leak
00605 
00606 Revision 1.7  2007/03/01 12:42:59  jim
00607 Modified slightly after code checking
00608 
00609 Revision 1.6  2006/07/11 14:59:09  jim
00610 Fixed offsets
00611 
00612 Revision 1.5  2006/06/15 09:58:59  jim
00613 Minor changes to docs
00614 
00615 Revision 1.4  2006/05/15 12:55:42  jim
00616 Fixed a few typos
00617 
00618 Revision 1.3  2006/05/04 11:53:42  jim
00619 Fixed _save routine so that it's more consistent with the standard CPL
00620 way of doing things
00621 
00622 Revision 1.2  2006/04/27 14:22:05  jim
00623 Fixed docs
00624 
00625 Revision 1.1  2006/04/24 10:42:45  jim
00626 New routine
00627 
00628 
00629 */

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1