VIRCAM Pipeline  1.3.4
tests/vircam_imcore.c
1 /* $Id: vircam_imcore.c,v 1.19 2013-10-15 17:06:55 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2006 Cambridge Astronomy Survey Unit
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2013-10-15 17:06:55 $
24  * $Revision: 1.19 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <cpl.h>
35 #include <math.h>
36 
37 #include "vircam_utils.h"
38 #include "vircam_dfs.h"
39 #include "vircam_mods.h"
40 #include "vircam_fits.h"
41 #include "../vircam/catalogue/imcore.h"
42 
43 /* Function prototypes */
44 
45 static int vircam_imcore_create(cpl_plugin *) ;
46 static int vircam_imcore_exec(cpl_plugin *) ;
47 static int vircam_imcore_destroy(cpl_plugin *) ;
48 static int vircam_imcore_test(cpl_parameterlist *, cpl_frameset *) ;
49 static int vircam_imcore_save(cpl_frameset *framelist,
50  cpl_parameterlist *parlist);
51 static void vircam_imcore_init(void);
52 static void vircam_imcore_tidy(void);
53 
54 /* Static global variables */
55 
56 static struct {
57 
58  /* Input */
59 
60  int ipix;
61  float threshold;
62  int icrowd;
63  float rcore;
64  float filtfwhm;
65  int nbsize;
66  int cattype;
67  int extenum;
68 
69  /* Output */
70 
71  float skylevel;
72  float skynoise;
73 
74 } vircam_imcore_config ;
75 
76 static struct {
77  cpl_size *labels;
78  cpl_frame *img;
79  cpl_frame *conf;
80  vir_fits *imgf;
81  vir_fits *conff;
82  vir_tfits *outcat;
83 } ps;
84 
85 static int isfirst;
86 static cpl_frame *product_frame = NULL;
87 
88 #define BUZZ_OFF {vircam_imcore_tidy(); return(-1);}
89 
90 static char vircam_imcore_description[] =
91 "vircam_imcore -- VIRCAM catalogue generation recipe.\n\n"
92 "Extract objects from an image and write the catalogue to a FITS table.\n\n"
93 "If more than one file of each type is specified in the SOF only the \n"
94 "first will be used and the rest will be ingored\n"
95 "The program requires the following files in the SOF:\n\n"
96 " Tag Description\n"
97 " -----------------------------------------------------------------------\n"
98 " %-21s An input image\n"
99 " %-21s A master confidence maps\n"
100 "\n";
101 
153 /* Function code */
154 
155 
156 /*---------------------------------------------------------------------------*/
164 /*---------------------------------------------------------------------------*/
165 
166 int cpl_plugin_get_info(cpl_pluginlist *list) {
167  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
168  cpl_plugin *plugin = &recipe->interface;
169  char alldesc[SZ_ALLDESC];
170  (void)snprintf(alldesc,SZ_ALLDESC,vircam_imcore_description,
171  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
172 
173 
174  cpl_plugin_init(plugin,
175  CPL_PLUGIN_API,
176  VIRCAM_BINARY_VERSION,
177  CPL_PLUGIN_TYPE_RECIPE,
178  "vircam_imcore",
179  "VIRCAM recipe to extract objects from a frame [test]",
180  alldesc,
181  "Jim Lewis",
182  "jrl@ast.cam.ac.uk",
184  vircam_imcore_create,
185  vircam_imcore_exec,
186  vircam_imcore_destroy);
187 
188  cpl_pluginlist_append(list,plugin);
189 
190  return(0);
191 }
192 
193 
194 /*---------------------------------------------------------------------------*/
203 /*---------------------------------------------------------------------------*/
204 
205 static int vircam_imcore_create(cpl_plugin *plugin) {
206  cpl_recipe *recipe;
207  cpl_parameter *p;
208 
209  /* Get the recipe out of the plugin */
210 
211  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
212  recipe = (cpl_recipe *)plugin;
213  else
214  return(-1);
215 
216  /* Create the parameters list in the cpl_recipe object */
217 
218  recipe->parameters = cpl_parameterlist_new();
219 
220  /* Fill in the minimum object size */
221 
222  p = cpl_parameter_new_value("vircam.vircam_imcore.ipix",
223  CPL_TYPE_INT,
224  "Minimum pixel area for each detected object",
225  "vircam.vircam_imcore",5);
226  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
227  cpl_parameterlist_append(recipe->parameters,p);
228 
229  /* Fill in the detection threshold parameter */
230 
231  p = cpl_parameter_new_value("vircam.vircam_imcore.thresh",
232  CPL_TYPE_DOUBLE,
233  "Detection threshold in sigma above sky",
234  "vircam.vircam_imcore",2.0);
235  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
236  cpl_parameterlist_append(recipe->parameters,p);
237 
238  /* Fill in flag to use deblending software or not */
239 
240  p = cpl_parameter_new_value("vircam.vircam_imcore.icrowd",
241  CPL_TYPE_BOOL,"Use deblending?",
242  "vircam.vircam_imcore",0);
243  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
244  cpl_parameterlist_append(recipe->parameters,p);
245 
246  /* Fill in core radius */
247 
248  p = cpl_parameter_new_value("vircam.vircam_imcore.rcore",
249  CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
250  "vircam.vircam_imcore",4.0);
251  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
252  cpl_parameterlist_append(recipe->parameters,p);
253 
254  /* Fill in background smoothing box size */
255 
256  p = cpl_parameter_new_value("vircam.vircam_imcore.nbsize",
257  CPL_TYPE_INT,"Background smoothing box size",
258  "vircam.vircam_imcore",64);
259  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
260  cpl_parameterlist_append(recipe->parameters,p);
261 
262  /* Fill in detection algorithm smoothing kernel FWHM */
263 
264  p = cpl_parameter_new_value("vircam.vircam_imcore.filtfwhm",
265  CPL_TYPE_DOUBLE,
266  "FWHM of smoothing kernel in pixels",
267  "vircam.vircam_imcore",2.0);
268  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"filtfwhm");
269  cpl_parameterlist_append(recipe->parameters,p);
270 
271  /* Fill in catalogue type */
272 
273  p = cpl_parameter_new_enum("vircam.vircam_imcore.cattype",
274  CPL_TYPE_INT,"Catalogue type",
275  "vircam.vircam_imcore",2,5,1,2,3,4,6);
276  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"cattype");
277  cpl_parameterlist_append(recipe->parameters,p);
278 
279  /* Extension number of input frames to use */
280 
281  p = cpl_parameter_new_range("vircam.vircam_imcore.extenum",
282  CPL_TYPE_INT,
283  "Extension number to be done, 0 == all",
284  "vircam.vircam_imcore",
285  1,0,16);
286  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
287  cpl_parameterlist_append(recipe->parameters,p);
288 
289  /* Get out of here */
290 
291  return(0);
292 }
293 
294 /*---------------------------------------------------------------------------*/
300 /*---------------------------------------------------------------------------*/
301 
302 static int vircam_imcore_destroy(cpl_plugin *plugin) {
303  cpl_recipe *recipe ;
304 
305  /* Get the recipe out of the plugin */
306 
307  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
308  recipe = (cpl_recipe *)plugin;
309  else
310  return(-1);
311 
312  cpl_parameterlist_delete(recipe->parameters);
313  return(0);
314 }
315 
316 /*---------------------------------------------------------------------------*/
322 /*---------------------------------------------------------------------------*/
323 
324 static int vircam_imcore_exec(cpl_plugin *plugin) {
325  cpl_recipe *recipe;
326 
327  /* Get the recipe out of the plugin */
328 
329  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
330  recipe = (cpl_recipe *)plugin;
331  else
332  return(-1);
333 
334  return(vircam_imcore_test(recipe->parameters,recipe->frames));
335 }
336 
337 /*---------------------------------------------------------------------------*/
344 /*---------------------------------------------------------------------------*/
345 
346 static int vircam_imcore_test(cpl_parameterlist *parlist,
347  cpl_frameset *framelist) {
348  const char *fctid="vircam_imcore";
349  cpl_parameter *p;
350  int j,jst,jfn,retval,ipix,icrowd,nbsize,mcattype,status;
351  cpl_size nlab;
352  float thresh,rcore,filtfwhm;
353 
354  /* Initialise a few things */
355 
356  vircam_imcore_init();
357 
358  /* Get the parameters */
359 
360  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.ipix");
361  vircam_imcore_config.ipix = cpl_parameter_get_int(p);
362  ipix = vircam_imcore_config.ipix;
363  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.thresh");
364  vircam_imcore_config.threshold = (float)cpl_parameter_get_double(p);
365  thresh = vircam_imcore_config.threshold;
366  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.icrowd");
367  vircam_imcore_config.icrowd = cpl_parameter_get_bool(p);
368  icrowd = vircam_imcore_config.icrowd;
369  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.rcore");
370  vircam_imcore_config.rcore = (float)cpl_parameter_get_double(p);
371  rcore = vircam_imcore_config.rcore;
372  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.filtfwhm");
373  vircam_imcore_config.filtfwhm = (float)cpl_parameter_get_double(p);
374  filtfwhm = vircam_imcore_config.filtfwhm;
375  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.nbsize");
376  vircam_imcore_config.nbsize = cpl_parameter_get_int(p);
377  nbsize = vircam_imcore_config.nbsize;
378  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.cattype");
379  vircam_imcore_config.cattype = cpl_parameter_get_int(p);
380  mcattype = vircam_imcore_config.cattype;
381  p = cpl_parameterlist_find(parlist,"vircam.vircam_imcore.extenum");
382  vircam_imcore_config.extenum = cpl_parameter_get_int(p);
383 
384  /* Sort out raw from calib frames */
385 
386  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
387  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
388  vircam_imcore_tidy();
389  return(-1);
390  }
391 
392  /* Get the images and confidence maps */
393 
394  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
395  &nlab)) == NULL) {
396  cpl_msg_error(fctid,"Cannot labelise the input frameset");
397  BUZZ_OFF
398  }
399  if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
400  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
401  cpl_msg_error(fctid,"Cannot find any image frames in input frameset");
402  BUZZ_OFF
403  }
404  if ((ps.conf = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
405  VIRCAM_CAL_CONF)) == NULL) {
406  cpl_msg_info(fctid,"No confidence map specified. Proceding without one");
407  ps.conf = NULL;
408  }
409 
410  /* Loop for each of the image extensions */
411 
412  vircam_exten_range(vircam_imcore_config.extenum,(const cpl_frame *)ps.img,
413  &jst,&jfn);
414  if (jst == -1 || jfn == -1) {
415  cpl_msg_error(fctid,"Unable to continue");
416  vircam_imcore_tidy();
417  return(-1);
418  }
419  for (j = jst; j <= jfn; j++) {
420  cpl_msg_info(fctid,"Processing extension %" CPL_SIZE_FORMAT,
421  (cpl_size)j);
422  status = VIR_OK;
423  isfirst = (j == jst);
424 
425  /* Load up the images */
426 
427  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
428  if (ps.imgf == NULL) {
429  vircam_imcore_tidy();
430  return(-1);
431  }
432  if (ps.conf != NULL)
433  ps.conff = vircam_fits_load(ps.conf,CPL_TYPE_INT,j);
434 
435  /* Call the imcore routine */
436 
437  (void)vircam_imcore(ps.imgf,ps.conff,ipix,thresh,icrowd,rcore,
438  nbsize,mcattype,filtfwhm,&(ps.outcat),&status);
439  if (status != VIR_OK) {
440  cpl_msg_error(fctid,"Error extracting objects in extension %" CPL_SIZE_FORMAT,
441  (cpl_size)j);
442  cpl_error_reset();
443  ps.outcat = vircam_tfits_wrap(vircam_dummy_catalogue(mcattype),
444  NULL,
445  vircam_fits_get_phu(ps.imgf),
446  vircam_fits_get_ehu(ps.imgf));
447  }
448 
449  /* Save the products */
450 
451  retval = vircam_imcore_save(framelist,parlist);
452  if (retval != 0) {
453  cpl_msg_error(fctid,"Error saving products in extension %" CPL_SIZE_FORMAT,
454  (cpl_size)j);
455  BUZZ_OFF
456  }
457 
458  /* Clean up a bit */
459 
460  freetfits(ps.outcat);
461  freefits(ps.imgf);
462  freefits(ps.conff);
463  }
464  vircam_imcore_tidy();
465  return(0);
466 }
467 
468 /*---------------------------------------------------------------------------*/
475 /*---------------------------------------------------------------------------*/
476 
477 static int vircam_imcore_save(cpl_frameset *framelist,
478  cpl_parameterlist *parlist) {
479  const char *recipeid = "vircam_imcore";
480  const char *fctid = "vircam_imcore_save";
481  const char *outfile = "imcoretab.fits";
482  cpl_propertylist *elist,*plist;
483 
484  /* Create the output table. First see if you need a primary */
485 
486  if (isfirst) {
487 
488  /* Create a new product frame object and define some tags */
489 
490  product_frame = cpl_frame_new();
491  cpl_frame_set_filename(product_frame,outfile);
492  cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_TEST);
493  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
494  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
495  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
496 
497  /* Fiddle with the primary header now */
498 
499  plist = vircam_tfits_get_phu(ps.outcat);
500  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
501  parlist,(char *)recipeid,
502  "?Dictionary?",NULL,0);
503 
504  /* Get the extension header and tack the extra header items onto it. */
505 
506  elist = vircam_tfits_get_ehu(ps.outcat);
507  vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
508  parlist,(char *)recipeid,
509  "?Dictionary?",NULL);
510 
511  /* 'Save' the PHU and create a table extension */
512 
513  if (cpl_table_save(vircam_tfits_get_table(ps.outcat),plist,elist,
514  outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
515  cpl_msg_error(fctid,"Cannot save product table");
516  cpl_frame_delete(product_frame);
517  return(-1);
518  }
519  cpl_frameset_insert(framelist,product_frame);
520 
521  /* Otherwise save the next extension */
522 
523  } else {
524 
525  /* Get the extension header and tack the extra header items onto it. */
526 
527  elist = vircam_tfits_get_ehu(ps.outcat);
528  vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
529  parlist,(char *)recipeid,
530  "?Dictionary?",NULL);
531 
532  /* Save the table */
533 
534  if (cpl_table_save(vircam_tfits_get_table(ps.outcat),NULL,elist,
535  outfile,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
536  cpl_msg_error(fctid,"Cannot save product table");
537  return(-1);
538  }
539  }
540 
541  return(0);
542 }
543 
544 /*---------------------------------------------------------------------------*/
548 /*---------------------------------------------------------------------------*/
549 
550 static void vircam_imcore_init(void) {
551  ps.labels = NULL;
552  ps.img = NULL;
553  ps.conf = NULL;
554  ps.imgf = NULL;
555  ps.conff = NULL;
556  ps.outcat = NULL;
557 }
558 
559 /*---------------------------------------------------------------------------*/
563 /*---------------------------------------------------------------------------*/
564 
565 static void vircam_imcore_tidy(void) {
566  freespace(ps.labels);
567  freeframe(ps.img);
568  freeframe(ps.conf);
569  freefits(ps.imgf);
570  freefits(ps.conff);
571  freetfits(ps.outcat);
572 }
573 
577 /*
578 
579 $Log: not supported by cvs2svn $
580 Revision 1.18 2012/01/16 14:44:02 jim
581 Fixed test recipes for cpl6 compliance
582 
583 Revision 1.17 2012/01/15 17:40:09 jim
584 Minor modifications to take into accout the changes in cpl API for v6
585 
586 Revision 1.16 2009/09/09 09:51:13 jim
587 modified to use new saving routines so that headers are right
588 
589 Revision 1.15 2007/10/25 19:38:22 jim
590 modified to keep lint happy
591 
592 Revision 1.14 2007/10/15 12:53:55 jim
593 Modified for compatibility with cpl_4.0
594 
595 Revision 1.13 2007/07/09 13:22:09 jim
596 Modified to use new version of vircam_exten_range
597 
598 Revision 1.12 2007/05/02 09:16:54 jim
599 uses new vircam_imcore api
600 
601 Revision 1.11 2007/04/13 12:27:38 jim
602 Added some extra docs
603 
604 Revision 1.10 2007/04/04 10:36:29 jim
605 Modified to use new dfs tags
606 
607 Revision 1.9 2007/03/01 12:42:59 jim
608 Modified slightly after code checking
609 
610 Revision 1.8 2006/08/01 11:34:17 jim
611 uses newer version of the imcore software
612 
613 Revision 1.7 2006/06/15 09:58:59 jim
614 Minor changes to docs
615 
616 Revision 1.6 2006/05/18 12:31:50 jim
617 Fixed bug where raw and calib frames were not being classified
618 
619 Revision 1.5 2006/05/18 09:49:13 jim
620 Fixed error in online doc
621 
622 Revision 1.4 2006/05/04 11:53:41 jim
623 Fixed _save routine so that it's more consistent with the standard CPL
624 way of doing things
625 
626 Revision 1.3 2006/04/28 08:55:26 jim
627 Modified to allow both types of confidence maps
628 
629 Revision 1.2 2006/04/27 14:22:04 jim
630 Fixed docs
631 
632 Revision 1.1 2006/04/24 10:42:44 jim
633 New routine
634 
635 
636 */
const char * vircam_get_license(void)
Definition: vircam_utils.c:92
cpl_table * vircam_tfits_get_table(vir_tfits *p)
Definition: vircam_tfits.c:364
int vircam_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Definition: vircam_utils.c:141
cpl_frame * vircam_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Definition: vircam_utils.c:247
cpl_propertylist * vircam_tfits_get_ehu(vir_tfits *p)
Definition: vircam_tfits.c:473
vir_tfits * vircam_tfits_wrap(cpl_table *tab, vir_tfits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: vircam_tfits.c:703
void vircam_dfs_set_product_exten_header(cpl_propertylist *plist, cpl_frame *frame, cpl_frameset *frameset, cpl_parameterlist *parlist, char *recipeid, const char *dict, cpl_frame *inherit)
Definition: vircam_dfs.c:273
void vircam_dfs_set_product_primary_header(cpl_propertylist *plist, cpl_frame *frame, cpl_frameset *frameset, cpl_parameterlist *parlist, char *recipeid, const char *dict, cpl_frame *inherit, int synch)
Definition: vircam_dfs.c:203
cpl_table * vircam_dummy_catalogue(int type)
cpl_propertylist * vircam_fits_get_phu(vir_fits *p)
Definition: vircam_fits.c:416
vir_fits * vircam_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: vircam_fits.c:80
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
Definition: vircam_fits.c:457
cpl_propertylist * vircam_tfits_get_phu(vir_tfits *p)
Definition: vircam_tfits.c:432
int vircam_imcore(vir_fits *infile, vir_fits *conf, int ipix, float threshold, int icrowd, float rcore, int nbsize, int cattyp, float filtfwhm, vir_tfits **outtab, int *status)
Generate object catalogues from input images.
void vircam_exten_range(int inexten, const cpl_frame *fr, int *out1, int *out2)
Definition: vircam_utils.c:353
int vircam_dfs_set_groups(cpl_frameset *set)
Definition: vircam_dfs.c:87