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