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