VIRCAM Pipeline  1.3.4
tests/vircam_imstack.c
1 /* $Id: vircam_imstack.c,v 1.6 2013-10-15 17:08:04 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:08:04 $
24  * $Revision: 1.6 $
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 <stdio.h>
35 #include <cpl.h>
36 #include <math.h>
37 
38 #include "vircam_utils.h"
39 #include "vircam_mask.h"
40 #include "vircam_dfs.h"
41 #include "vircam_mods.h"
42 #include "vircam_fits.h"
43 
44 /* Function prototypes */
45 
46 static int vircam_imstack_create(cpl_plugin *) ;
47 static int vircam_imstack_exec(cpl_plugin *) ;
48 static int vircam_imstack_destroy(cpl_plugin *) ;
49 static int vircam_imstack_test(cpl_parameterlist *, cpl_frameset *) ;
50 static int vircam_imstack_save(cpl_frameset *framelist,
51  cpl_parameterlist *parlist);
52 static void vircam_imstack_init(void);
53 static void vircam_imstack_tidy(void);
54 
55 /* Static global variables */
56 
57 static struct {
58 
59  /* Input */
60 
61  int method;
62  int extenum;
63 
64 } vircam_imstack_config;
65 
66 
67 static struct {
68  cpl_size *labels;
69  cpl_frameset *imagelist;
70  vir_fits **images;
71  cpl_frameset *conflist;
72  vir_fits **confs;
73  cpl_frameset *catlist;
74  vir_tfits **cats;
75  int nimages;
76  int nconfs;
77  vir_fits *outfits;
78  vir_fits *outfitsc;
79 } ps;
80 
81 static int isfirst;
82 static cpl_frame *product_frame = NULL;
83 static cpl_frame *product_conf = NULL;
84 
85 
86 static char vircam_imstack_description[] =
87 "vircam_imstack -- VIRCAM test jitter recipe.\n\n"
88 "Dither a list of frames into an output frame.\n\n"
89 "The program accepts the following files in the SOF:\n\n"
90 " Tag Description\n"
91 " -----------------------------------------------------------------------\n"
92 " %-21s A list of images\n"
93 " %-21s A list of confidence maps\n"
94 " %-21s An optional list of object catalogues\n"
95 "\n";
96 
141 /* Function code */
142 
143 /*---------------------------------------------------------------------------*/
151 /*---------------------------------------------------------------------------*/
152 
153 int cpl_plugin_get_info(cpl_pluginlist *list) {
154  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
155  cpl_plugin *plugin = &recipe->interface;
156  char alldesc[SZ_ALLDESC];
157  (void)snprintf(alldesc,SZ_ALLDESC,vircam_imstack_description,
158  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF,VIRCAM_CAL_OBJCAT);
159 
160  cpl_plugin_init(plugin,
161  CPL_PLUGIN_API,
162  VIRCAM_BINARY_VERSION,
163  CPL_PLUGIN_TYPE_RECIPE,
164  "vircam_imstack",
165  "VIRCAM jitter test recipe [test]",
166  alldesc,
167  "Jim Lewis",
168  "jrl@ast.cam.ac.uk",
170  vircam_imstack_create,
171  vircam_imstack_exec,
172  vircam_imstack_destroy);
173 
174  cpl_pluginlist_append(list,plugin);
175 
176  return(0);
177 }
178 
179 /*---------------------------------------------------------------------------*/
188 /*---------------------------------------------------------------------------*/
189 
190 static int vircam_imstack_create(cpl_plugin *plugin) {
191  cpl_recipe *recipe;
192  cpl_parameter *p;
193 
194  /* Get the recipe out of the plugin */
195 
196  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
197  recipe = (cpl_recipe *)plugin;
198  else
199  return(-1);
200 
201  /* Create the parameters list in the cpl_recipe object */
202 
203  recipe->parameters = cpl_parameterlist_new();
204 
205  /* Combination method */
206 
207  p = cpl_parameter_new_enum("vircam.vircam_imstack.method",
208  CPL_TYPE_INT,"Combination method",
209  "vircam.vircam_imstack",0,2,0,1);
210  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"method");
211  cpl_parameterlist_append(recipe->parameters,p);
212 
213 
214  /* Extension number of input frames to use */
215 
216  p = cpl_parameter_new_range("vircam.vircam_imstack.extenum",
217  CPL_TYPE_INT,
218  "Extension number to be done, 0 == all",
219  "vircam.vircam_imstack",
220  1,0,16);
221  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
222  cpl_parameterlist_append(recipe->parameters,p);
223 
224  /* Get out of here */
225 
226  return(0);
227 }
228 
229 
230 /*---------------------------------------------------------------------------*/
236 /*---------------------------------------------------------------------------*/
237 
238 static int vircam_imstack_exec(cpl_plugin *plugin) {
239  cpl_recipe *recipe;
240 
241  /* Get the recipe out of the plugin */
242 
243  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
244  recipe = (cpl_recipe *)plugin;
245  else
246  return(-1);
247 
248  return(vircam_imstack_test(recipe->parameters,recipe->frames));
249 }
250 
251 /*---------------------------------------------------------------------------*/
257 /*---------------------------------------------------------------------------*/
258 
259 static int vircam_imstack_destroy(cpl_plugin *plugin) {
260  cpl_recipe *recipe ;
261 
262  /* Get the recipe out of the plugin */
263 
264  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
265  recipe = (cpl_recipe *)plugin;
266  else
267  return(-1);
268 
269  cpl_parameterlist_delete(recipe->parameters);
270  return(0);
271 }
272 
273 /*---------------------------------------------------------------------------*/
280 /*---------------------------------------------------------------------------*/
281 
282 static int vircam_imstack_test(cpl_parameterlist *parlist,
283  cpl_frameset *framelist) {
284  const char *fctid="vircam_imstack";
285  int j,jst,jfn,retval,status;
286  cpl_size nlab;
287  cpl_parameter *p;
288 
289 
290  /* Check validity of input frameset */
291 
292  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
293  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
294  return(-1);
295  }
296 
297  /* Initialise some things */
298 
299  vircam_imstack_init();
300 
301  /* Get the parameters */
302 
303  p = cpl_parameterlist_find(parlist,"vircam.vircam_imstack.extenum");
304  vircam_imstack_config.extenum = cpl_parameter_get_int(p);
305  p = cpl_parameterlist_find(parlist,"vircam.vircam_imstack.method");
306  vircam_imstack_config.method = cpl_parameter_get_int(p);
307 
308  /* Sort out raw from calib frames */
309 
310  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
311  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
312  vircam_imstack_tidy();
313  return(-1);
314  }
315 
316  /* Get the frames frames */
317 
318  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
319  &nlab)) == NULL) {
320  cpl_msg_error(fctid,"Cannot labelise the input frames");
321  vircam_imstack_tidy();
322  return(-1);
323  }
324  if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
325  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
326  cpl_msg_error(fctid,"Cannot get images in input frameset");
327  vircam_imstack_tidy();
328  return(-1);
329  }
330  ps.nimages = cpl_frameset_get_size(ps.imagelist);
331  if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
332  VIRCAM_CAL_CONF)) == NULL) {
333  cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
334  vircam_imstack_tidy();
335  return(-1);
336  }
337  ps.nconfs = cpl_frameset_get_size(ps.conflist);
338  if ((ps.catlist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
339  VIRCAM_CAL_OBJCAT)) == NULL) {
340  cpl_msg_info(fctid,"No object catalogues found -- no matching done");
341  }
342 
343  /* Now, how many image extensions do we want to do? If the extension
344  number is zero, then we loop for all possible extensions. If it
345  isn't then we just do the extension specified */
346 
347  vircam_exten_range(vircam_imstack_config.extenum,
348  (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
349  &jst,&jfn);
350  if (jst == -1 || jfn == -1) {
351  cpl_msg_error(fctid,"Unable to continue");
352  vircam_imstack_tidy();
353  return(-1);
354  }
355 
356  /* Now loop for all the extension... */
357 
358  status = VIR_OK;
359  for (j = jst; j <= jfn; j++) {
360  isfirst = (j == jst);
361 
362  /* Load the images */
363 
364  ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
365  ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
366  if (ps.catlist != NULL)
367  ps.cats = vircam_tfits_load_list(ps.catlist,j);
368 
369  /* Call the dithering module */
370 
371  cpl_msg_info(fctid,"Doing jittering for extension %" CPL_SIZE_FORMAT,
372  (cpl_size)j);
373  (void)vircam_imstack(ps.images,ps.confs,ps.cats,ps.nimages,ps.nconfs,
374  5.0,5.0,vircam_imstack_config.method,0,
375  &(ps.outfits),&(ps.outfitsc),&status);
376  if (status != VIR_OK) {
377  vircam_imstack_tidy();
378  return(-1);
379  }
380 
381  /* Save everything */
382 
383  cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
384  (cpl_size)j);
385  retval = vircam_imstack_save(framelist,parlist);
386  if (retval != 0) {
387  vircam_imstack_tidy();
388  return(-1);
389  }
390  freefitslist(ps.images,ps.nimages);
391  freefitslist(ps.confs,ps.nconfs);
392  freefits(ps.outfits);
393  freefits(ps.outfitsc);
394  }
395  vircam_imstack_tidy();
396  return(0);
397 }
398 
399 /*---------------------------------------------------------------------------*/
406 /*---------------------------------------------------------------------------*/
407 
408 static int vircam_imstack_save(cpl_frameset *framelist,
409  cpl_parameterlist *parlist) {
410  cpl_propertylist *plist;
411  const char *recipeid = "vircam_imstack";
412  const char *fctid = "vircam_imstack_save";
413  const char *outfile = "comb.fits";
414  const char *outconf = "combconf.fits";
415 
416  /* If we need to make a PHU then do that now based on the first frame
417  in the input frame list */
418 
419  if (isfirst) {
420 
421  /* Create a new product frame object and define some tags */
422 
423  product_frame = cpl_frame_new();
424  cpl_frame_set_filename(product_frame,outfile);
425  cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
426  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
427  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
428  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
429 
430  /* Set up header for phu */
431 
432  plist = vircam_fits_get_phu(ps.outfits);
433  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
434  parlist,(char *)recipeid,
435  "?Dictionary?",NULL,0);
436 
437  /* 'Save' the PHU dithered image */
438 
439  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
440  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
441  cpl_msg_error(fctid,"Cannot save product PHU");
442  cpl_frame_delete(product_frame);
443  return(-1);
444  }
445  cpl_frameset_insert(framelist,product_frame);
446 
447  /* Create a new product frame object and define some tags */
448 
449  product_conf = cpl_frame_new();
450  cpl_frame_set_filename(product_conf,outconf);
451  cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
452  cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
453  cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
454  cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
455 
456  /* 'Save' the PHU confidence map */
457 
458  if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
459  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
460  cpl_msg_error(fctid,"Cannot save product PHU");
461  cpl_frame_delete(product_conf);
462  return(-1);
463  }
464  cpl_frameset_insert(framelist,product_conf);
465  }
466 
467  /* Get the extension property list */
468 
469  plist = vircam_fits_get_ehu(ps.outfits);
470 
471  /* Fiddle with the header now */
472 
473  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
474  parlist,(char *)recipeid,
475  "?Dictionary?",NULL);
476 
477  /* Now save the dithered image extension */
478 
479  if (cpl_image_save(vircam_fits_get_image(ps.outfits),outfile,
480  CPL_TYPE_FLOAT,plist,
481  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
482  cpl_msg_error(fctid,"Cannot save dithered image extension");
483  return(-1);
484  }
485 
486  /* And the confidence map */
487 
488  if (cpl_image_save(vircam_fits_get_image(ps.outfitsc),outconf,
489  CPL_TYPE_SHORT,plist,
490  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
491  cpl_msg_error(fctid,"Cannot save confidence map image extension");
492  return(-1);
493  }
494 
495  /* Get out of here */
496 
497  return(0);
498 }
499 
500 /*---------------------------------------------------------------------------*/
504 /*---------------------------------------------------------------------------*/
505 
506 static void vircam_imstack_init(void) {
507  ps.labels = NULL;
508  ps.imagelist = NULL;
509  ps.conflist = NULL;
510  ps.catlist = NULL;
511  ps.images = NULL;
512  ps.confs = NULL;
513  ps.cats = NULL;
514  ps.outfits = NULL;
515  ps.outfitsc = NULL;
516 }
517 
518 /*---------------------------------------------------------------------------*/
522 /*---------------------------------------------------------------------------*/
523 
524 static void vircam_imstack_tidy(void) {
525  freespace(ps.labels);
526  freeframeset(ps.imagelist);
527  freeframeset(ps.conflist);
528  freeframeset(ps.catlist);
529  freefitslist(ps.images,ps.nimages);
530  freefitslist(ps.confs,ps.nconfs);
531  freetfitslist(ps.cats,ps.nimages);
532  freefits(ps.outfits);
533  freefits(ps.outfitsc);
534 }
535 
538 /*
539 
540 $Log: not supported by cvs2svn $
541 Revision 1.5 2012/01/16 14:44:02 jim
542 Fixed test recipes for cpl6 compliance
543 
544 Revision 1.4 2012/01/15 17:40:09 jim
545 Minor modifications to take into accout the changes in cpl API for v6
546 
547 Revision 1.3 2010/06/30 12:42:00 jim
548 A few fixes to stop compiler compaints
549 
550 Revision 1.2 2009/09/09 09:51:13 jim
551 modified to use new saving routines so that headers are right
552 
553 Revision 1.1 2008/11/21 10:17:43 jim
554 New routine
555 
556 
557 */
const char * vircam_get_license(void)
Definition: vircam_utils.c:92
int vircam_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Definition: vircam_utils.c:141
vir_fits ** vircam_fits_load_list(cpl_frameset *f, cpl_type type, int exten)
Definition: vircam_fits.c:231
int vircam_imstack(vir_fits **inf, vir_fits **inconf, vir_tfits **cats, int nimages, int nconfs, float lthr, float hthr, int method, int seeing, vir_fits **out, vir_fits **outc, int *status)
Stack images into a mean image using WCS info.
vir_tfits ** vircam_tfits_load_list(cpl_frameset *f, int exten)
Definition: vircam_tfits.c:247
cpl_image * vircam_fits_get_image(vir_fits *p)
Definition: vircam_fits.c:349
cpl_frameset * vircam_frameset_subgroup(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Definition: vircam_utils.c:193
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_propertylist * vircam_fits_get_phu(vir_fits *p)
Definition: vircam_fits.c:416
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
Definition: vircam_fits.c:457
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