VIRCAM Pipeline  1.3.3
tests/vircam_interleave.c
1 /* $Id: vircam_interleave.c,v 1.23 2012-01-16 15:47:24 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: 2012-01-16 15:47:24 $
24  * $Revision: 1.23 $
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_stats.h"
43 #include "vircam_fits.h"
44 #include "vircam_wcsutils.h"
45 
46 /* Function prototypes */
47 
48 static int vircam_interleave_create(cpl_plugin *) ;
49 static int vircam_interleave_exec(cpl_plugin *) ;
50 static int vircam_interleave_destroy(cpl_plugin *) ;
51 static int vircam_interleave_test(cpl_parameterlist *, cpl_frameset *) ;
52 static int vircam_interleave_save(cpl_frameset *framelist,
53  cpl_parameterlist *parlist);
54 static void vircam_interleave_init(void);
55 static void vircam_interleave_tidy(void);
56 
57 /* Static global variables */
58 
59 static struct {
60 
61  /* Input */
62 
63  int extenum;
64 
65 } vircam_interleave_config;
66 
67 
68 static struct {
69  cpl_size *labels;
70  cpl_frameset *imagelist;
71  vir_fits **images;
72  cpl_frameset *conflist;
73  vir_fits **confs;
74  int nimages;
75  int nconfs;
76  cpl_image *outimage;
77  cpl_image *outconf;
78  cpl_propertylist *plist;
79 } ps;
80 
81 static int isfirst;
82 static cpl_frame *product_frame = NULL;
83 static cpl_frame *product_conf = NULL;
84 
85 static char vircam_interleave_description[] =
86 "vircam_interleave -- VIRCAM test interleave recipe.\n\n"
87 "Interleave a list of frames into an output frame.\n\n"
88 "The program accepts the following files in the SOF:\n\n"
89 " Tag Description\n"
90 " -----------------------------------------------------------------------\n"
91 " %-21s A list of images\n"
92 " %-21s A list of confidence maps\n"
93 "\n";
94 
137 /* Function code */
138 
139 /*---------------------------------------------------------------------------*/
147 /*---------------------------------------------------------------------------*/
148 
149 int cpl_plugin_get_info(cpl_pluginlist *list) {
150  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
151  cpl_plugin *plugin = &recipe->interface;
152  char alldesc[SZ_ALLDESC];
153  (void)snprintf(alldesc,SZ_ALLDESC,vircam_interleave_description,
154  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
155 
156  cpl_plugin_init(plugin,
157  CPL_PLUGIN_API,
158  VIRCAM_BINARY_VERSION,
159  CPL_PLUGIN_TYPE_RECIPE,
160  "vircam_interleave",
161  "VIRCAM interleaf test recipe [test]",
162  alldesc,
163  "Jim Lewis",
164  "jrl@ast.cam.ac.uk",
166  vircam_interleave_create,
167  vircam_interleave_exec,
168  vircam_interleave_destroy);
169 
170  cpl_pluginlist_append(list,plugin);
171 
172  return(0);
173 }
174 
175 /*---------------------------------------------------------------------------*/
184 /*---------------------------------------------------------------------------*/
185 
186 static int vircam_interleave_create(cpl_plugin *plugin) {
187  cpl_recipe *recipe;
188  cpl_parameter *p;
189 
190  /* Get the recipe out of the plugin */
191 
192  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
193  recipe = (cpl_recipe *)plugin;
194  else
195  return(-1);
196 
197  /* Create the parameters list in the cpl_recipe object */
198 
199  recipe->parameters = cpl_parameterlist_new();
200 
201  /* Extension number of input frames to use */
202 
203  p = cpl_parameter_new_range("vircam.vircam_interleave.extenum",
204  CPL_TYPE_INT,
205  "Extension number to be done, 0 == all",
206  "vircam.vircam_interleave",
207  1,0,16);
208  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
209  cpl_parameterlist_append(recipe->parameters,p);
210 
211  /* Get out of here */
212 
213  return(0);
214 }
215 
216 
217 /*---------------------------------------------------------------------------*/
223 /*---------------------------------------------------------------------------*/
224 
225 static int vircam_interleave_exec(cpl_plugin *plugin) {
226  cpl_recipe *recipe;
227 
228  /* Get the recipe out of the plugin */
229 
230  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
231  recipe = (cpl_recipe *)plugin;
232  else
233  return(-1);
234 
235  return(vircam_interleave_test(recipe->parameters,recipe->frames));
236 }
237 
238 /*---------------------------------------------------------------------------*/
244 /*---------------------------------------------------------------------------*/
245 
246 static int vircam_interleave_destroy(cpl_plugin *plugin) {
247  cpl_recipe *recipe ;
248 
249  /* Get the recipe out of the plugin */
250 
251  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
252  recipe = (cpl_recipe *)plugin;
253  else
254  return(-1);
255 
256  cpl_parameterlist_delete(recipe->parameters);
257  return(0);
258 }
259 
260 /*---------------------------------------------------------------------------*/
267 /*---------------------------------------------------------------------------*/
268 
269 static int vircam_interleave_test(cpl_parameterlist *parlist,
270  cpl_frameset *framelist) {
271  const char *fctid="vircam_interleave";
272  int j,jst,jfn,retval,status,i,nstep;
273  cpl_size nlab;
274  const int *dims;
275  long npts;
276  float val;
277  double refx,refy,refra,refdec,x,y;
278  cpl_parameter *p;
279  cpl_image *image;
280  const cpl_array *a;
281  cpl_wcs *wcs;
282 
283 
284  /* Check validity of input frameset */
285 
286  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
287  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
288  return(-1);
289  }
290 
291  /* Initialise some things */
292 
293  vircam_interleave_init();
294 
295  /* Get the parameters */
296 
297  p = cpl_parameterlist_find(parlist,"vircam.vircam_interleave.extenum");
298  vircam_interleave_config.extenum = cpl_parameter_get_int(p);
299 
300  /* Sort out raw from calib frames */
301 
302  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
303  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
304  vircam_interleave_tidy();
305  return(-1);
306  }
307 
308  /* Get the frames frames */
309 
310  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
311  &nlab)) == NULL) {
312  cpl_msg_error(fctid,"Cannot labelise the input frames");
313  vircam_interleave_tidy();
314  return(-1);
315  }
316  if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
317  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
318  cpl_msg_error(fctid,"Cannot get images in input frameset");
319  vircam_interleave_tidy();
320  return(-1);
321  }
322  ps.nimages = cpl_frameset_get_size(ps.imagelist);
323  nstep = (int)sqrt((float)ps.nimages);
324  if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
325  VIRCAM_CAL_CONF)) == NULL) {
326  cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
327  vircam_interleave_tidy();
328  return(-1);
329  }
330  ps.nconfs = cpl_frameset_get_size(ps.conflist);
331 
332  /* Now, how many image extensions do we want to do? If the extension
333  number is zero, then we loop for all possible extensions. If it
334  isn't then we just do the extension specified */
335 
336  vircam_exten_range(vircam_interleave_config.extenum,
337  (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
338  &jst,&jfn);
339  if (jst == -1 || jfn == -1) {
340  cpl_msg_error(fctid,"Unable to continue");
341  vircam_interleave_tidy();
342  return(-1);
343  }
344 
345  /* Now loop for all the extension... */
346 
347  status = VIR_OK;
348  for (j = jst; j <= jfn; j++) {
349  isfirst = (j == jst);
350 
351  /* Load the images */
352 
353  ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
354  ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
355 
356  /* Get some information that you need for the interleaving. Start
357  by getting the background level add it to the extension property
358  list */
359 
360  refx = 0.0;
361  refy = 0.0;
362  for (i = 0; i < ps.nimages; i++) {
363  image = vircam_fits_get_image(ps.images[i]);
364  npts = vircam_getnpts(image);
365  val = vircam_med(cpl_image_get_data_float(image),NULL,npts);
366  cpl_propertylist_update_float(vircam_fits_get_ehu(ps.images[i]),
367  "ESO DRS BACKMED",val);
368  wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ps.images[i]));
369  if (i == 0) {
370  a = cpl_wcs_get_image_dims(wcs);
371  dims = cpl_array_get_data_int_const(a);
372  refx = 0.5*(double)dims[0];
373  refy = 0.5*(double)dims[1];
374  vircam_xytoradec(wcs,refx,refy,&refra,&refdec);
375  }
376  vircam_radectoxy(wcs,refra,refdec,&x,&y);
377  x = refx - x;
378  y = refy - y;
379  cpl_propertylist_update_double(vircam_fits_get_ehu(ps.images[i]),
380  "ESO DRS XOFFMICRO",x);
381  cpl_propertylist_update_double(vircam_fits_get_ehu(ps.images[i]),
382  "ESO DRS YOFFMICRO",y);
383  cpl_wcs_delete(wcs);
384  }
385 
386  /* Call the interleaf module */
387 
388  cpl_msg_info(fctid,"Doing interleaving for extension %" CPL_SIZE_FORMAT,
389  (cpl_size)j);
390  (void)vircam_interleave(ps.images,ps.nimages,ps.confs,ps.nconfs,
391  nstep,&(ps.plist),&(ps.outimage),&(ps.outconf),
392  &status);
393  if (status != VIR_OK) {
394  vircam_interleave_tidy();
395  return(-1);
396  }
397 
398  /* Save everything */
399 
400  cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
401  (cpl_size)j);
402  retval = vircam_interleave_save(framelist,parlist);
403  if (retval != 0) {
404  vircam_interleave_tidy();
405  return(-1);
406  }
407  freefitslist(ps.images,ps.nimages);
408  freefitslist(ps.confs,ps.nconfs);
409  freeimage(ps.outimage);
410  freeimage(ps.outconf);
411  freepropertylist(ps.plist);
412  }
413  vircam_interleave_tidy();
414  return(0);
415 }
416 
417 
418 /*---------------------------------------------------------------------------*/
426 /*---------------------------------------------------------------------------*/
427 
428 static int vircam_interleave_save(cpl_frameset *framelist,
429  cpl_parameterlist *parlist) {
430  cpl_propertylist *plist;
431  const char *recipeid = "vircam_interleave";
432  const char *fctid = "vircam_interleave_save";
433  const char *outfile = "comb.fits";
434  const char *outconf = "combconf.fits";
435 
436  /* If we need to make a PHU then do that now based on the first frame
437  in the input frame list */
438 
439  if (isfirst) {
440 
441  /* Create a new product frame object and define some tags */
442 
443  product_frame = cpl_frame_new();
444  cpl_frame_set_filename(product_frame,outfile);
445  cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_TEST);
446  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
447  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
448  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
449 
450  /* Set up header for phu */
451 
452  plist = vircam_fits_get_phu(ps.images[0]);
453  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
454  parlist,(char *)recipeid,
455  "?Dictionary?",NULL,0);
456 
457  /* 'Save' the PHU interleaved image */
458 
459  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
460  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
461  cpl_msg_error(fctid,"Cannot save product PHU");
462  cpl_frame_delete(product_frame);
463  return(-1);
464  }
465  cpl_frameset_insert(framelist,product_frame);
466 
467  /* Create a new product frame object and define some tags */
468 
469  product_conf = cpl_frame_new();
470  cpl_frame_set_filename(product_conf,outconf);
471  cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
472  cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
473  cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
474  cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
475 
476  /* 'Save' the PHU confidence map image */
477 
478  if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
479  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
480  cpl_msg_error(fctid,"Cannot save product PHU");
481  cpl_frame_delete(product_conf);
482  return(-1);
483  }
484  cpl_frameset_insert(framelist,product_conf);
485  }
486 
487  /* Get the extension property list */
488 
489  plist = ps.plist;
490 
491  /* Fiddle with the header now */
492 
493  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
494  parlist,(char *)recipeid,
495  "?Dictionary?",NULL);
496 
497  /* Now save the interleaved image extension */
498 
499  if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
500  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
501  cpl_msg_error(fctid,"Cannot save interleaved image extension");
502  return(-1);
503  }
504 
505  /* And the confidence map */
506 
507  if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
508  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
509  cpl_msg_error(fctid,"Cannot save confidence map image extension");
510  return(-1);
511  }
512 
513  /* Get out of here */
514 
515  return(0);
516 }
517 
518 /*---------------------------------------------------------------------------*/
522 /*---------------------------------------------------------------------------*/
523 
524 static void vircam_interleave_init(void) {
525  ps.labels = NULL;
526  ps.imagelist = NULL;
527  ps.images = NULL;
528  ps.conflist = NULL;
529  ps.confs = NULL;
530  ps.outimage = NULL;
531  ps.outconf = NULL;
532  ps.plist = NULL;
533 }
534 
535 /*---------------------------------------------------------------------------*/
539 /*---------------------------------------------------------------------------*/
540 
541 static void vircam_interleave_tidy(void) {
542  freespace(ps.labels);
543  freeframeset(ps.imagelist);
544  freefitslist(ps.images,ps.nimages);
545  freeframeset(ps.conflist);
546  freefitslist(ps.confs,ps.nconfs);
547  freeimage(ps.outimage);
548  freeimage(ps.outconf);
549  freepropertylist(ps.plist);
550 }
551 
555 /*
556 
557 $Log: not supported by cvs2svn $
558 Revision 1.22 2012/01/16 14:44:02 jim
559 Fixed test recipes for cpl6 compliance
560 
561 Revision 1.21 2012/01/15 17:40:09 jim
562 Minor modifications to take into accout the changes in cpl API for v6
563 
564 Revision 1.20 2010/07/13 11:16:50 jim
565 A few changes to deal with compiler whinges
566 
567 Revision 1.19 2010/06/30 12:42:00 jim
568 A few fixes to stop compiler compaints
569 
570 Revision 1.18 2009/09/09 09:51:13 jim
571 modified to use new saving routines so that headers are right
572 
573 Revision 1.17 2008/07/10 13:01:35 jim
574 Modified to use v4.2 version of cpl_wcs
575 
576 Revision 1.16 2008/06/20 11:13:02 jim
577 Fixed dodgy call to cpl_wcs_get_image_dims
578 
579 Revision 1.15 2008/05/06 08:40:43 jim
580 Modified to use cpl_wcs interface
581 
582 Revision 1.14 2007/10/25 19:38:22 jim
583 modified to keep lint happy
584 
585 Revision 1.13 2007/10/15 12:53:55 jim
586 Modified for compatibility with cpl_4.0
587 
588 Revision 1.12 2007/07/09 13:22:09 jim
589 Modified to use new version of vircam_exten_range
590 
591 Revision 1.11 2007/05/02 12:53:11 jim
592 typo fixes in docs
593 
594 Revision 1.10 2007/04/13 12:27:39 jim
595 Added some extra docs
596 
597 Revision 1.9 2007/04/04 10:36:29 jim
598 Modified to use new dfs tags
599 
600 Revision 1.8 2007/03/02 12:38:33 jim
601 Fixed small memory leak
602 
603 Revision 1.7 2007/03/01 12:42:59 jim
604 Modified slightly after code checking
605 
606 Revision 1.6 2006/07/11 14:59:09 jim
607 Fixed offsets
608 
609 Revision 1.5 2006/06/15 09:58:59 jim
610 Minor changes to docs
611 
612 Revision 1.4 2006/05/15 12:55:42 jim
613 Fixed a few typos
614 
615 Revision 1.3 2006/05/04 11:53:42 jim
616 Fixed _save routine so that it's more consistent with the standard CPL
617 way of doing things
618 
619 Revision 1.2 2006/04/27 14:22:05 jim
620 Fixed docs
621 
622 Revision 1.1 2006/04/24 10:42:45 jim
623 New routine
624 
625 
626 */