vircam_imcore.c

00001 /* $Id: vircam_imcore.c,v 1.15 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.15 $
00025  * $Name:  $
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     int              *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 nlab,j,jst,jfn,retval,ipix,icrowd,nbsize,mcattype,status;
00351     float thresh,rcore,filtfwhm;
00352 
00353     /* Initialise a few things */
00354 
00355     vircam_imcore_init();
00356 
00357     /* Get the parameters */
00358 
00359     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.ipix");
00360     vircam_imcore_config.ipix = cpl_parameter_get_int(p);
00361     ipix = vircam_imcore_config.ipix;
00362     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.thresh");
00363     vircam_imcore_config.threshold = (float)cpl_parameter_get_double(p);
00364     thresh = vircam_imcore_config.threshold;
00365     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.icrowd");
00366     vircam_imcore_config.icrowd = cpl_parameter_get_bool(p);
00367     icrowd = vircam_imcore_config.icrowd;
00368     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.rcore");
00369     vircam_imcore_config.rcore = (float)cpl_parameter_get_double(p);
00370     rcore = vircam_imcore_config.rcore;
00371     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.filtfwhm");
00372     vircam_imcore_config.filtfwhm = (float)cpl_parameter_get_double(p);
00373     filtfwhm = vircam_imcore_config.filtfwhm;
00374     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.nbsize");
00375     vircam_imcore_config.nbsize = cpl_parameter_get_int(p);
00376     nbsize = vircam_imcore_config.nbsize;
00377     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.cattype");
00378     vircam_imcore_config.cattype = cpl_parameter_get_int(p);
00379     mcattype = vircam_imcore_config.cattype;
00380     p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.extenum");
00381     vircam_imcore_config.extenum = cpl_parameter_get_int(p);
00382 
00383     /* Sort out raw from calib frames */
00384 
00385     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00386         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00387         vircam_imcore_tidy();
00388         return(-1);
00389     }
00390 
00391     /* Get the images and confidence maps */
00392 
00393     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00394                                            &nlab)) == NULL) {
00395         cpl_msg_error(fctid,"Cannot labelise the input frameset");
00396         BUZZ_OFF
00397     }
00398     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00399                                            VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00400         cpl_msg_error(fctid,"Cannot find any image frames in input frameset");
00401         BUZZ_OFF
00402     } 
00403     if ((ps.conf = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00404                                             VIRCAM_CAL_CONF)) == NULL) {
00405         cpl_msg_info(fctid,"No confidence map specified. Proceding without one");
00406         ps.conf = NULL;
00407     }
00408 
00409     /* Loop for each of the image extensions */
00410 
00411     vircam_exten_range(vircam_imcore_config.extenum,(const cpl_frame *)ps.img,
00412                        &jst,&jfn);
00413     if (jst == -1 || jfn == -1) {
00414         cpl_msg_error(fctid,"Unable to continue");
00415         vircam_imcore_tidy();
00416         return(-1);
00417     }
00418     for (j = jst; j <= jfn; j++) {
00419         cpl_msg_info(fctid,"Processing extension %d\n",j);
00420         status = VIR_OK;
00421         isfirst = (j == jst);
00422 
00423         /* Load up the images */
00424 
00425         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00426         if (ps.imgf == NULL) {
00427             vircam_imcore_tidy();
00428             return(-1);
00429         }
00430         if (ps.conf != NULL) 
00431             ps.conff = vircam_fits_load(ps.conf,CPL_TYPE_INT,j);
00432 
00433         /* Call the imcore routine */
00434 
00435         (void)vircam_imcore(ps.imgf,ps.conff,ipix,thresh,icrowd,rcore,
00436                             nbsize,mcattype,filtfwhm,&(ps.outcat),&status);
00437         if (status != VIR_OK) {
00438             cpl_msg_error(fctid,"Error extracting objects in extension %d",j);
00439             cpl_error_reset();
00440             ps.outcat = vircam_tfits_wrap(vircam_dummy_catalogue(mcattype),
00441                                           NULL,
00442                                           vircam_fits_get_phu(ps.imgf),
00443                                           vircam_fits_get_ehu(ps.imgf));
00444         }
00445   
00446         /* Save the products */
00447 
00448         retval = vircam_imcore_save(framelist,parlist);
00449         if (retval != 0) {
00450             cpl_msg_error(fctid,"Error saving products in extension %d",j);
00451             BUZZ_OFF
00452         }
00453 
00454         /* Clean up a bit */
00455 
00456         freetfits(ps.outcat);
00457         freefits(ps.imgf);
00458         freefits(ps.conff);
00459     }
00460     vircam_imcore_tidy();
00461     return(0);
00462 }
00463 
00464 /*---------------------------------------------------------------------------*/
00471 /*---------------------------------------------------------------------------*/
00472 
00473 static int vircam_imcore_save(cpl_frameset *framelist,
00474                               cpl_parameterlist *parlist) {
00475     const char *recipeid = "vircam_imcore";
00476     const char *fctid = "vircam_imcore_save";
00477     const char *outfile = "imcoretab.fits";
00478     cpl_propertylist *elist,*plist;
00479 
00480     /* Create the output table. First see if you need a primary */
00481 
00482     if (isfirst) {
00483  
00484         /* Create a new product frame object and define some tags */
00485 
00486         product_frame = cpl_frame_new();
00487         cpl_frame_set_filename(product_frame,outfile);
00488         cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_TEST);
00489         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00490         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00491         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00492 
00493         /* Fiddle with the primary header now */
00494 
00495         plist = vircam_tfits_get_phu(ps.outcat);
00496         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00497                                               parlist,(char *)recipeid,
00498                                               "?Dictionary?");
00499 
00500         /* Get the extension header and tack the extra header items onto it. */
00501 
00502         elist = vircam_tfits_get_ehu(ps.outcat);
00503         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00504                                             parlist,(char *)recipeid,
00505                                             "?Dictionary?");
00506 
00507         /* 'Save' the PHU and create a table extension */
00508 
00509         if (cpl_table_save(vircam_tfits_get_table(ps.outcat),plist,elist,
00510                            outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00511             cpl_msg_error(fctid,"Cannot save product table");
00512             cpl_frame_delete(product_frame);
00513             return(-1);
00514         }
00515         cpl_frameset_insert(framelist,product_frame);
00516 
00517     /* Otherwise save the next extension */
00518 
00519     } else {
00520 
00521         /* Get the extension header and tack the extra header items onto it. */
00522 
00523         elist = vircam_tfits_get_ehu(ps.outcat);
00524         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00525                                             parlist,(char *)recipeid,
00526                                             "?Dictionary?");
00527 
00528         /* Save the table */
00529 
00530         if (cpl_table_save(vircam_tfits_get_table(ps.outcat),NULL,elist,
00531                            outfile,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00532             cpl_msg_error(fctid,"Cannot save product table");
00533             return(-1);
00534         }
00535     }
00536 
00537     return(0);
00538 }
00539 
00540 /*---------------------------------------------------------------------------*/
00544 /*---------------------------------------------------------------------------*/
00545 
00546 static void vircam_imcore_init(void) {
00547     ps.labels = NULL;
00548     ps.img = NULL;
00549     ps.conf = NULL;
00550     ps.imgf = NULL;
00551     ps.conff = NULL;
00552     ps.outcat = NULL;
00553 }
00554 
00555 /*---------------------------------------------------------------------------*/
00559 /*---------------------------------------------------------------------------*/
00560 
00561 static void vircam_imcore_tidy(void) {
00562     freespace(ps.labels);
00563     freeframe(ps.img);
00564     freeframe(ps.conf);
00565     freefits(ps.imgf);
00566     freefits(ps.conff);
00567     freetfits(ps.outcat);
00568 }
00569 
00573 /*
00574 
00575 $Log: vircam_imcore.c,v $
00576 Revision 1.15  2007/10/25 19:38:22  jim
00577 modified to keep lint happy
00578 
00579 Revision 1.14  2007/10/15 12:53:55  jim
00580 Modified for compatibility with cpl_4.0
00581 
00582 Revision 1.13  2007/07/09 13:22:09  jim
00583 Modified to use new version of vircam_exten_range
00584 
00585 Revision 1.12  2007/05/02 09:16:54  jim
00586 uses new vircam_imcore api
00587 
00588 Revision 1.11  2007/04/13 12:27:38  jim
00589 Added some extra docs
00590 
00591 Revision 1.10  2007/04/04 10:36:29  jim
00592 Modified to use new dfs tags
00593 
00594 Revision 1.9  2007/03/01 12:42:59  jim
00595 Modified slightly after code checking
00596 
00597 Revision 1.8  2006/08/01 11:34:17  jim
00598 uses newer version of the imcore software
00599 
00600 Revision 1.7  2006/06/15 09:58:59  jim
00601 Minor changes to docs
00602 
00603 Revision 1.6  2006/05/18 12:31:50  jim
00604 Fixed bug where raw and calib frames were not being classified
00605 
00606 Revision 1.5  2006/05/18 09:49:13  jim
00607 Fixed error in online doc
00608 
00609 Revision 1.4  2006/05/04 11:53:41  jim
00610 Fixed _save routine so that it's more consistent with the standard CPL
00611 way of doing things
00612 
00613 Revision 1.3  2006/04/28 08:55:26  jim
00614 Modified to allow both types of confidence maps
00615 
00616 Revision 1.2  2006/04/27 14:22:04  jim
00617 Fixed docs
00618 
00619 Revision 1.1  2006/04/24 10:42:44  jim
00620 New routine
00621 
00622 
00623 */

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