vircam_imcore.c

00001 /* $Id: vircam_imcore.c,v 1.18 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.18 $
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 <cpl.h>
00035 #include <math.h>
00036 
00037 #include "vircam_utils.h"
00038 #include "vircam_dfs.h"
00039 #include "vircam_mods.h"
00040 #include "vircam_fits.h"
00041 #include "../vircam/catalogue/imcore.h"
00042 
00043 /* Function prototypes */
00044 
00045 static int vircam_imcore_create(cpl_plugin *) ;
00046 static int vircam_imcore_exec(cpl_plugin *) ;
00047 static int vircam_imcore_destroy(cpl_plugin *) ;
00048 static int vircam_imcore_test(cpl_parameterlist *, cpl_frameset *) ;
00049 static int vircam_imcore_save(cpl_frameset *framelist,
00050                               cpl_parameterlist *parlist);
00051 static void vircam_imcore_init(void);
00052 static void vircam_imcore_tidy(void);
00053 
00054 /* Static global variables */
00055 
00056 static struct {
00057 
00058     /* Input */
00059 
00060     int         ipix;
00061     float       threshold;
00062     int         icrowd;
00063     float       rcore;
00064     float       filtfwhm;
00065     int         nbsize;
00066     int         cattype;
00067     int         extenum;
00068 
00069     /* Output */
00070 
00071     float       skylevel;
00072     float       skynoise;
00073 
00074 } vircam_imcore_config ;
00075 
00076 static struct {
00077     cpl_size         *labels;
00078     cpl_frame        *img;
00079     cpl_frame        *conf;
00080     vir_fits         *imgf;
00081     vir_fits         *conff;
00082     vir_tfits        *outcat;
00083 } ps;
00084 
00085 static int isfirst;
00086 static cpl_frame *product_frame = NULL;
00087 
00088 #define BUZZ_OFF {vircam_imcore_tidy(); return(-1);}
00089 
00090 static char vircam_imcore_description[] =
00091 "vircam_imcore -- VIRCAM catalogue generation recipe.\n\n"
00092 "Extract objects from an image and write the catalogue to a FITS table.\n\n"
00093 "If more than one file of each type is specified in the SOF only the \n"
00094 "first will be used and the rest will be ingored\n"
00095 "The program requires the following files in the SOF:\n\n"
00096 "    Tag                   Description\n"
00097 "    -----------------------------------------------------------------------\n"
00098 "    %-21s An input image\n"
00099 "    %-21s A master confidence maps\n"
00100 "\n";
00101 
00153 /* Function code */
00154 
00155 
00156 /*---------------------------------------------------------------------------*/
00164 /*---------------------------------------------------------------------------*/
00165 
00166 int cpl_plugin_get_info(cpl_pluginlist *list) {
00167     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00168     cpl_plugin  *plugin = &recipe->interface;
00169     char alldesc[SZ_ALLDESC];
00170     (void)snprintf(alldesc,SZ_ALLDESC,vircam_imcore_description,
00171                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
00172 
00173 
00174     cpl_plugin_init(plugin,
00175                     CPL_PLUGIN_API,
00176                     VIRCAM_BINARY_VERSION,
00177                     CPL_PLUGIN_TYPE_RECIPE,
00178                     "vircam_imcore",
00179                     "VIRCAM recipe to extract objects from a frame [test]",
00180                     alldesc,
00181                     "Jim Lewis",
00182                     "jrl@ast.cam.ac.uk",
00183                     vircam_get_license(),
00184                     vircam_imcore_create,
00185                     vircam_imcore_exec,
00186                     vircam_imcore_destroy);
00187 
00188     cpl_pluginlist_append(list,plugin);
00189 
00190     return(0);
00191 }
00192 
00193 
00194 /*---------------------------------------------------------------------------*/
00203 /*---------------------------------------------------------------------------*/
00204 
00205 static int vircam_imcore_create(cpl_plugin *plugin) {
00206     cpl_recipe      *recipe;
00207     cpl_parameter   *p;
00208 
00209     /* Get the recipe out of the plugin */
00210 
00211     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00212         recipe = (cpl_recipe *)plugin;
00213     else
00214         return(-1);
00215 
00216     /* Create the parameters list in the cpl_recipe object */
00217 
00218     recipe->parameters = cpl_parameterlist_new();
00219 
00220     /* Fill in the minimum object size */
00221 
00222     p = cpl_parameter_new_value("vircam.vircam_imcore.ipix",
00223                                 CPL_TYPE_INT,
00224                                 "Minimum pixel area for each detected object",
00225                                 "vircam.vircam_imcore",5);
00226     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
00227     cpl_parameterlist_append(recipe->parameters,p);    
00228 
00229     /* Fill in the detection threshold parameter */
00230 
00231     p = cpl_parameter_new_value("vircam.vircam_imcore.thresh",
00232                                 CPL_TYPE_DOUBLE,
00233                                 "Detection threshold in sigma above sky",
00234                                 "vircam.vircam_imcore",2.0);
00235     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
00236     cpl_parameterlist_append(recipe->parameters,p);
00237 
00238     /* Fill in flag to use deblending software or not */
00239 
00240     p = cpl_parameter_new_value("vircam.vircam_imcore.icrowd",
00241                                 CPL_TYPE_BOOL,"Use deblending?",
00242                                 "vircam.vircam_imcore",0);
00243     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
00244     cpl_parameterlist_append(recipe->parameters,p);
00245 
00246     /* Fill in core radius */
00247 
00248     p = cpl_parameter_new_value("vircam.vircam_imcore.rcore",
00249                                 CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
00250                                 "vircam.vircam_imcore",4.0);
00251     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
00252     cpl_parameterlist_append(recipe->parameters,p);
00253 
00254     /* Fill in background smoothing box size */
00255 
00256     p = cpl_parameter_new_value("vircam.vircam_imcore.nbsize",
00257                                 CPL_TYPE_INT,"Background smoothing box size",
00258                                 "vircam.vircam_imcore",64);
00259     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
00260     cpl_parameterlist_append(recipe->parameters,p);
00261 
00262     /* Fill in detection algorithm smoothing kernel FWHM */
00263 
00264     p = cpl_parameter_new_value("vircam.vircam_imcore.filtfwhm",
00265                                 CPL_TYPE_DOUBLE,
00266                                 "FWHM of smoothing kernel in pixels",
00267                                 "vircam.vircam_imcore",2.0);
00268     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"filtfwhm");
00269     cpl_parameterlist_append(recipe->parameters,p);
00270 
00271     /* Fill in catalogue type */
00272 
00273     p = cpl_parameter_new_enum("vircam.vircam_imcore.cattype",
00274                                CPL_TYPE_INT,"Catalogue type",
00275                                "vircam.vircam_imcore",2,4,1,2,3,4);
00276     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"cattype");
00277     cpl_parameterlist_append(recipe->parameters,p);
00278 
00279     /* Extension number of input frames to use */
00280 
00281     p = cpl_parameter_new_range("vircam.vircam_imcore.extenum",
00282                                 CPL_TYPE_INT,
00283                                 "Extension number to be done, 0 == all",
00284                                 "vircam.vircam_imcore",
00285                                 1,0,16);
00286     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00287     cpl_parameterlist_append(recipe->parameters,p);
00288 
00289     /* Get out of here */
00290 
00291     return(0);
00292 }
00293 
00294 /*---------------------------------------------------------------------------*/
00300 /*---------------------------------------------------------------------------*/
00301 
00302 static int vircam_imcore_destroy(cpl_plugin *plugin) {
00303     cpl_recipe *recipe ;
00304 
00305     /* Get the recipe out of the plugin */
00306 
00307     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00308         recipe = (cpl_recipe *)plugin;
00309     else
00310         return(-1);
00311 
00312     cpl_parameterlist_delete(recipe->parameters);
00313     return(0);
00314 }
00315 
00316 /*---------------------------------------------------------------------------*/
00322 /*---------------------------------------------------------------------------*/
00323 
00324 static int vircam_imcore_exec(cpl_plugin *plugin) {
00325     cpl_recipe  *recipe;
00326 
00327     /* Get the recipe out of the plugin */
00328 
00329     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00330         recipe = (cpl_recipe *)plugin;
00331     else
00332         return(-1);
00333 
00334     return(vircam_imcore_test(recipe->parameters,recipe->frames));
00335 }
00336 
00337 /*---------------------------------------------------------------------------*/
00344 /*---------------------------------------------------------------------------*/
00345 
00346 static int vircam_imcore_test(cpl_parameterlist *parlist, 
00347                               cpl_frameset *framelist) {
00348     const char *fctid="vircam_imcore";
00349     cpl_parameter *p;
00350     int j,jst,jfn,retval,ipix,icrowd,nbsize,mcattype,status;
00351     cpl_size nlab;
00352     float thresh,rcore,filtfwhm;
00353 
00354     /* Initialise a few things */
00355 
00356     vircam_imcore_init();
00357 
00358     /* Get the parameters */
00359 
00360     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.ipix");
00361     vircam_imcore_config.ipix = cpl_parameter_get_int(p);
00362     ipix = vircam_imcore_config.ipix;
00363     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.thresh");
00364     vircam_imcore_config.threshold = (float)cpl_parameter_get_double(p);
00365     thresh = vircam_imcore_config.threshold;
00366     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.icrowd");
00367     vircam_imcore_config.icrowd = cpl_parameter_get_bool(p);
00368     icrowd = vircam_imcore_config.icrowd;
00369     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.rcore");
00370     vircam_imcore_config.rcore = (float)cpl_parameter_get_double(p);
00371     rcore = vircam_imcore_config.rcore;
00372     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.filtfwhm");
00373     vircam_imcore_config.filtfwhm = (float)cpl_parameter_get_double(p);
00374     filtfwhm = vircam_imcore_config.filtfwhm;
00375     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.nbsize");
00376     vircam_imcore_config.nbsize = cpl_parameter_get_int(p);
00377     nbsize = vircam_imcore_config.nbsize;
00378     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.cattype");
00379     vircam_imcore_config.cattype = cpl_parameter_get_int(p);
00380     mcattype = vircam_imcore_config.cattype;
00381     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.extenum");
00382     vircam_imcore_config.extenum = cpl_parameter_get_int(p);
00383 
00384     /* Sort out raw from calib frames */
00385 
00386     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00387         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00388         vircam_imcore_tidy();
00389         return(-1);
00390     }
00391 
00392     /* Get the images and confidence maps */
00393 
00394     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00395                                            &nlab)) == NULL) {
00396         cpl_msg_error(fctid,"Cannot labelise the input frameset");
00397         BUZZ_OFF
00398     }
00399     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00400                                            VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00401         cpl_msg_error(fctid,"Cannot find any image frames in input frameset");
00402         BUZZ_OFF
00403     } 
00404     if ((ps.conf = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00405                                             VIRCAM_CAL_CONF)) == NULL) {
00406         cpl_msg_info(fctid,"No confidence map specified. Proceding without one");
00407         ps.conf = NULL;
00408     }
00409 
00410     /* Loop for each of the image extensions */
00411 
00412     vircam_exten_range(vircam_imcore_config.extenum,(const cpl_frame *)ps.img,
00413                        &jst,&jfn);
00414     if (jst == -1 || jfn == -1) {
00415         cpl_msg_error(fctid,"Unable to continue");
00416         vircam_imcore_tidy();
00417         return(-1);
00418     }
00419     for (j = jst; j <= jfn; j++) {
00420         cpl_msg_info(fctid,"Processing extension %" CPL_SIZE_FORMAT,
00421                      (cpl_size)j);
00422         status = VIR_OK;
00423         isfirst = (j == jst);
00424 
00425         /* Load up the images */
00426 
00427         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00428         if (ps.imgf == NULL) {
00429             vircam_imcore_tidy();
00430             return(-1);
00431         }
00432         if (ps.conf != NULL) 
00433             ps.conff = vircam_fits_load(ps.conf,CPL_TYPE_INT,j);
00434 
00435         /* Call the imcore routine */
00436 
00437         (void)vircam_imcore(ps.imgf,ps.conff,ipix,thresh,icrowd,rcore,
00438                             nbsize,mcattype,filtfwhm,&(ps.outcat),&status);
00439         if (status != VIR_OK) {
00440             cpl_msg_error(fctid,"Error extracting objects in extension %" CPL_SIZE_FORMAT,
00441                           (cpl_size)j);
00442             cpl_error_reset();
00443             ps.outcat = vircam_tfits_wrap(vircam_dummy_catalogue(mcattype),
00444                                           NULL,
00445                                           vircam_fits_get_phu(ps.imgf),
00446                                           vircam_fits_get_ehu(ps.imgf));
00447         }
00448   
00449         /* Save the products */
00450 
00451         retval = vircam_imcore_save(framelist,parlist);
00452         if (retval != 0) {
00453             cpl_msg_error(fctid,"Error saving products in extension %" CPL_SIZE_FORMAT,
00454                           (cpl_size)j);
00455             BUZZ_OFF
00456         }
00457 
00458         /* Clean up a bit */
00459 
00460         freetfits(ps.outcat);
00461         freefits(ps.imgf);
00462         freefits(ps.conff);
00463     }
00464     vircam_imcore_tidy();
00465     return(0);
00466 }
00467 
00468 /*---------------------------------------------------------------------------*/
00475 /*---------------------------------------------------------------------------*/
00476 
00477 static int vircam_imcore_save(cpl_frameset *framelist,
00478                               cpl_parameterlist *parlist) {
00479     const char *recipeid = "vircam_imcore";
00480     const char *fctid = "vircam_imcore_save";
00481     const char *outfile = "imcoretab.fits";
00482     cpl_propertylist *elist,*plist;
00483 
00484     /* Create the output table. First see if you need a primary */
00485 
00486     if (isfirst) {
00487  
00488         /* Create a new product frame object and define some tags */
00489 
00490         product_frame = cpl_frame_new();
00491         cpl_frame_set_filename(product_frame,outfile);
00492         cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_TEST);
00493         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00494         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00495         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00496 
00497         /* Fiddle with the primary header now */
00498 
00499         plist = vircam_tfits_get_phu(ps.outcat);
00500         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00501                                               parlist,(char *)recipeid,
00502                                               "?Dictionary?",NULL,0);
00503 
00504         /* Get the extension header and tack the extra header items onto it. */
00505 
00506         elist = vircam_tfits_get_ehu(ps.outcat);
00507         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00508                                             parlist,(char *)recipeid,
00509                                             "?Dictionary?",NULL);
00510 
00511         /* 'Save' the PHU and create a table extension */
00512 
00513         if (cpl_table_save(vircam_tfits_get_table(ps.outcat),plist,elist,
00514                            outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00515             cpl_msg_error(fctid,"Cannot save product table");
00516             cpl_frame_delete(product_frame);
00517             return(-1);
00518         }
00519         cpl_frameset_insert(framelist,product_frame);
00520 
00521     /* Otherwise save the next extension */
00522 
00523     } else {
00524 
00525         /* Get the extension header and tack the extra header items onto it. */
00526 
00527         elist = vircam_tfits_get_ehu(ps.outcat);
00528         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00529                                             parlist,(char *)recipeid,
00530                                             "?Dictionary?",NULL);
00531 
00532         /* Save the table */
00533 
00534         if (cpl_table_save(vircam_tfits_get_table(ps.outcat),NULL,elist,
00535                            outfile,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00536             cpl_msg_error(fctid,"Cannot save product table");
00537             return(-1);
00538         }
00539     }
00540 
00541     return(0);
00542 }
00543 
00544 /*---------------------------------------------------------------------------*/
00548 /*---------------------------------------------------------------------------*/
00549 
00550 static void vircam_imcore_init(void) {
00551     ps.labels = NULL;
00552     ps.img = NULL;
00553     ps.conf = NULL;
00554     ps.imgf = NULL;
00555     ps.conff = NULL;
00556     ps.outcat = NULL;
00557 }
00558 
00559 /*---------------------------------------------------------------------------*/
00563 /*---------------------------------------------------------------------------*/
00564 
00565 static void vircam_imcore_tidy(void) {
00566     freespace(ps.labels);
00567     freeframe(ps.img);
00568     freeframe(ps.conf);
00569     freefits(ps.imgf);
00570     freefits(ps.conff);
00571     freetfits(ps.outcat);
00572 }
00573 
00577 /*
00578 
00579 $Log: vircam_imcore.c,v $
00580 Revision 1.18  2012/01/16 14:44:02  jim
00581 Fixed test recipes for cpl6 compliance
00582 
00583 Revision 1.17  2012/01/15 17:40:09  jim
00584 Minor modifications to take into accout the changes in cpl API for v6
00585 
00586 Revision 1.16  2009/09/09 09:51:13  jim
00587 modified to use new saving routines so that headers are right
00588 
00589 Revision 1.15  2007/10/25 19:38:22  jim
00590 modified to keep lint happy
00591 
00592 Revision 1.14  2007/10/15 12:53:55  jim
00593 Modified for compatibility with cpl_4.0
00594 
00595 Revision 1.13  2007/07/09 13:22:09  jim
00596 Modified to use new version of vircam_exten_range
00597 
00598 Revision 1.12  2007/05/02 09:16:54  jim
00599 uses new vircam_imcore api
00600 
00601 Revision 1.11  2007/04/13 12:27:38  jim
00602 Added some extra docs
00603 
00604 Revision 1.10  2007/04/04 10:36:29  jim
00605 Modified to use new dfs tags
00606 
00607 Revision 1.9  2007/03/01 12:42:59  jim
00608 Modified slightly after code checking
00609 
00610 Revision 1.8  2006/08/01 11:34:17  jim
00611 uses newer version of the imcore software
00612 
00613 Revision 1.7  2006/06/15 09:58:59  jim
00614 Minor changes to docs
00615 
00616 Revision 1.6  2006/05/18 12:31:50  jim
00617 Fixed bug where raw and calib frames were not being classified
00618 
00619 Revision 1.5  2006/05/18 09:49:13  jim
00620 Fixed error in online doc
00621 
00622 Revision 1.4  2006/05/04 11:53:41  jim
00623 Fixed _save routine so that it's more consistent with the standard CPL
00624 way of doing things
00625 
00626 Revision 1.3  2006/04/28 08:55:26  jim
00627 Modified to allow both types of confidence maps
00628 
00629 Revision 1.2  2006/04/27 14:22:04  jim
00630 Fixed docs
00631 
00632 Revision 1.1  2006/04/24 10:42:44  jim
00633 New routine
00634 
00635 
00636 */

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1