VIRCAM Pipeline  1.3.3
tests/vircam_imdither.c
1 /* $Id: vircam_imdither.c,v 1.14 2012-01-16 14:44:02 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 14:44:02 $
24  * $Revision: 1.14 $
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_imdither_create(cpl_plugin *) ;
47 static int vircam_imdither_exec(cpl_plugin *) ;
48 static int vircam_imdither_destroy(cpl_plugin *) ;
49 static int vircam_imdither_test(cpl_parameterlist *, cpl_frameset *) ;
50 static int vircam_imdither_save(cpl_frameset *framelist,
51  cpl_parameterlist *parlist);
52 static void vircam_imdither_init(void);
53 static void vircam_imdither_tidy(void);
54 
55 /* Static global variables */
56 
57 static struct {
58 
59  /* Input */
60 
61  int extenum;
62 
63 } vircam_imdither_config;
64 
65 
66 static struct {
67  cpl_size *labels;
68  cpl_frameset *imagelist;
69  vir_fits **images;
70  cpl_frameset *conflist;
71  vir_fits **confs;
72  int nimages;
73  int nconfs;
74  cpl_image *outimage;
75  cpl_image *outconf;
76  cpl_propertylist *plist;
77 } ps;
78 
79 static int isfirst;
80 static cpl_frame *product_frame = NULL;
81 static cpl_frame *product_conf = NULL;
82 
83 
84 static char vircam_imdither_description[] =
85 "vircam_imdither -- VIRCAM test jitter recipe.\n\n"
86 "Dither a list of frames into an output frame.\n\n"
87 "The program accepts the following files in the SOF:\n\n"
88 " Tag Description\n"
89 " -----------------------------------------------------------------------\n"
90 " %-21s A list of images\n"
91 " %-21s A list of confidence maps\n"
92 "\n";
93 
138 /* Function code */
139 
140 /*---------------------------------------------------------------------------*/
148 /*---------------------------------------------------------------------------*/
149 
150 int cpl_plugin_get_info(cpl_pluginlist *list) {
151  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
152  cpl_plugin *plugin = &recipe->interface;
153  char alldesc[SZ_ALLDESC];
154  (void)snprintf(alldesc,SZ_ALLDESC,vircam_imdither_description,
155  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
156 
157  cpl_plugin_init(plugin,
158  CPL_PLUGIN_API,
159  VIRCAM_BINARY_VERSION,
160  CPL_PLUGIN_TYPE_RECIPE,
161  "vircam_imdither",
162  "VIRCAM jitter test recipe [test]",
163  alldesc,
164  "Jim Lewis",
165  "jrl@ast.cam.ac.uk",
167  vircam_imdither_create,
168  vircam_imdither_exec,
169  vircam_imdither_destroy);
170 
171  cpl_pluginlist_append(list,plugin);
172 
173  return(0);
174 }
175 
176 /*---------------------------------------------------------------------------*/
185 /*---------------------------------------------------------------------------*/
186 
187 static int vircam_imdither_create(cpl_plugin *plugin) {
188  cpl_recipe *recipe;
189  cpl_parameter *p;
190 
191  /* Get the recipe out of the plugin */
192 
193  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
194  recipe = (cpl_recipe *)plugin;
195  else
196  return(-1);
197 
198  /* Create the parameters list in the cpl_recipe object */
199 
200  recipe->parameters = cpl_parameterlist_new();
201 
202  /* Extension number of input frames to use */
203 
204  p = cpl_parameter_new_range("vircam.vircam_imdither.extenum",
205  CPL_TYPE_INT,
206  "Extension number to be done, 0 == all",
207  "vircam.vircam_imdither",
208  1,0,16);
209  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
210  cpl_parameterlist_append(recipe->parameters,p);
211 
212  /* Get out of here */
213 
214  return(0);
215 }
216 
217 
218 /*---------------------------------------------------------------------------*/
224 /*---------------------------------------------------------------------------*/
225 
226 static int vircam_imdither_exec(cpl_plugin *plugin) {
227  cpl_recipe *recipe;
228 
229  /* Get the recipe out of the plugin */
230 
231  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
232  recipe = (cpl_recipe *)plugin;
233  else
234  return(-1);
235 
236  return(vircam_imdither_test(recipe->parameters,recipe->frames));
237 }
238 
239 /*---------------------------------------------------------------------------*/
245 /*---------------------------------------------------------------------------*/
246 
247 static int vircam_imdither_destroy(cpl_plugin *plugin) {
248  cpl_recipe *recipe ;
249 
250  /* Get the recipe out of the plugin */
251 
252  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
253  recipe = (cpl_recipe *)plugin;
254  else
255  return(-1);
256 
257  cpl_parameterlist_delete(recipe->parameters);
258  return(0);
259 }
260 
261 /*---------------------------------------------------------------------------*/
268 /*---------------------------------------------------------------------------*/
269 
270 static int vircam_imdither_test(cpl_parameterlist *parlist,
271  cpl_frameset *framelist) {
272  const char *fctid="vircam_imdither";
273  int j,jst,jfn,retval,status;
274  cpl_size nlab;
275  cpl_parameter *p;
276 
277 
278  /* Check validity of input frameset */
279 
280  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
281  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
282  return(-1);
283  }
284 
285  /* Initialise some things */
286 
287  vircam_imdither_init();
288 
289  /* Get the parameters */
290 
291  p = cpl_parameterlist_find(parlist,"vircam.vircam_imdither.extenum");
292  vircam_imdither_config.extenum = cpl_parameter_get_int(p);
293 
294  /* Sort out raw from calib frames */
295 
296  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
297  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
298  vircam_imdither_tidy();
299  return(-1);
300  }
301 
302  /* Get the frames frames */
303 
304  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
305  &nlab)) == NULL) {
306  cpl_msg_error(fctid,"Cannot labelise the input frames");
307  vircam_imdither_tidy();
308  return(-1);
309  }
310  if ((ps.imagelist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
311  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
312  cpl_msg_error(fctid,"Cannot get images in input frameset");
313  vircam_imdither_tidy();
314  return(-1);
315  }
316  ps.nimages = cpl_frameset_get_size(ps.imagelist);
317  if ((ps.conflist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
318  VIRCAM_CAL_CONF)) == NULL) {
319  cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
320  vircam_imdither_tidy();
321  return(-1);
322  }
323  ps.nconfs = cpl_frameset_get_size(ps.conflist);
324 
325  /* Now, how many image extensions do we want to do? If the extension
326  number is zero, then we loop for all possible extensions. If it
327  isn't then we just do the extension specified */
328 
329  vircam_exten_range(vircam_imdither_config.extenum,
330  (const cpl_frame *)cpl_frameset_get_frame(ps.imagelist,0),
331  &jst,&jfn);
332  if (jst == -1 || jfn == -1) {
333  cpl_msg_error(fctid,"Unable to continue");
334  vircam_imdither_tidy();
335  return(-1);
336  }
337 
338  /* Now loop for all the extension... */
339 
340  status = VIR_OK;
341  for (j = jst; j <= jfn; j++) {
342  isfirst = (j == jst);
343 
344  /* Load the images */
345 
346  ps.images = vircam_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
347  ps.confs = vircam_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
348 
349  /* Call the dithering module */
350 
351  cpl_msg_info(fctid,"Doing jittering for extension %" CPL_SIZE_FORMAT,
352  (cpl_size)j);
353  (void)vircam_imdither(ps.images,ps.confs,ps.nimages,ps.nconfs,
354  5.0,5.0,&(ps.plist),&(ps.outimage),
355  &(ps.outconf),&status);
356  if (status != VIR_OK) {
357  vircam_imdither_tidy();
358  return(-1);
359  }
360 
361  /* Save everything */
362 
363  cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
364  (cpl_size)j);
365  retval = vircam_imdither_save(framelist,parlist);
366  if (retval != 0) {
367  vircam_imdither_tidy();
368  return(-1);
369  }
370  freefitslist(ps.images,ps.nimages);
371  freefitslist(ps.confs,ps.nconfs);
372  freeimage(ps.outimage);
373  freeimage(ps.outconf);
374  freepropertylist(ps.plist);
375  }
376  vircam_imdither_tidy();
377  return(0);
378 }
379 
380 
381 /*---------------------------------------------------------------------------*/
388 /*---------------------------------------------------------------------------*/
389 
390 static int vircam_imdither_save(cpl_frameset *framelist,
391  cpl_parameterlist *parlist) {
392  cpl_propertylist *plist;
393  const char *recipeid = "vircam_imdither";
394  const char *fctid = "vircam_imdither_save";
395  const char *outfile = "comb.fits";
396  const char *outconf = "combconf.fits";
397 
398  /* If we need to make a PHU then do that now based on the first frame
399  in the input frame list */
400 
401  if (isfirst) {
402 
403  /* Create a new product frame object and define some tags */
404 
405  product_frame = cpl_frame_new();
406  cpl_frame_set_filename(product_frame,outfile);
407  cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
408  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
409  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
410  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
411 
412  /* Set up header for phu */
413 
414  plist = vircam_fits_get_phu(ps.images[0]);
415  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
416  parlist,(char *)recipeid,
417  "?Dictionary?",NULL,0);
418 
419  /* 'Save' the PHU dithered image */
420 
421  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
422  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
423  cpl_msg_error(fctid,"Cannot save product PHU");
424  cpl_frame_delete(product_frame);
425  return(-1);
426  }
427  cpl_frameset_insert(framelist,product_frame);
428 
429  /* Create a new product frame object and define some tags */
430 
431  product_conf = cpl_frame_new();
432  cpl_frame_set_filename(product_conf,outconf);
433  cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
434  cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
435  cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
436  cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
437 
438  /* 'Save' the PHU confidence map */
439 
440  if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
441  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
442  cpl_msg_error(fctid,"Cannot save product PHU");
443  cpl_frame_delete(product_conf);
444  return(-1);
445  }
446  cpl_frameset_insert(framelist,product_conf);
447  }
448 
449  /* Get the extension property list */
450 
451  plist = ps.plist;
452 
453  /* Fiddle with the header now */
454 
455  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
456  parlist,(char *)recipeid,
457  "?Dictionary?",NULL);
458 
459  /* Now save the dithered image extension */
460 
461  if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
462  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
463  cpl_msg_error(fctid,"Cannot save dithered image extension");
464  return(-1);
465  }
466 
467  /* And the confidence map */
468 
469  if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
470  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
471  cpl_msg_error(fctid,"Cannot save confidence map image extension");
472  return(-1);
473  }
474 
475  /* Get out of here */
476 
477  return(0);
478 }
479 
480 /*---------------------------------------------------------------------------*/
484 /*---------------------------------------------------------------------------*/
485 
486 static void vircam_imdither_init(void) {
487  ps.labels = NULL;
488  ps.imagelist = NULL;
489  ps.images = NULL;
490  ps.conflist = NULL;
491  ps.confs = NULL;
492  ps.outimage = NULL;
493  ps.outconf = NULL;
494  ps.plist = NULL;
495 }
496 
497 /*---------------------------------------------------------------------------*/
501 /*---------------------------------------------------------------------------*/
502 
503 static void vircam_imdither_tidy(void) {
504  freespace(ps.labels);
505  freeframeset(ps.imagelist);
506  freefitslist(ps.images,ps.nimages);
507  freeframeset(ps.conflist);
508  freefitslist(ps.confs,ps.nconfs);
509  freeimage(ps.outimage);
510  freeimage(ps.outconf);
511  freepropertylist(ps.plist);
512 }
513 
516 /*
517 
518 $Log: not supported by cvs2svn $
519 Revision 1.13 2012/01/15 17:40:09 jim
520 Minor modifications to take into accout the changes in cpl API for v6
521 
522 Revision 1.12 2009/09/09 09:51:13 jim
523 modified to use new saving routines so that headers are right
524 
525 Revision 1.11 2007/10/25 19:38:22 jim
526 modified to keep lint happy
527 
528 Revision 1.10 2007/10/15 12:53:55 jim
529 Modified for compatibility with cpl_4.0
530 
531 Revision 1.9 2007/07/09 13:22:09 jim
532 Modified to use new version of vircam_exten_range
533 
534 Revision 1.8 2007/05/02 12:53:11 jim
535 typo fixes in docs
536 
537 Revision 1.7 2007/04/13 12:27:38 jim
538 Added some extra docs
539 
540 Revision 1.6 2007/04/04 10:36:29 jim
541 Modified to use new dfs tags
542 
543 Revision 1.5 2007/03/02 12:38:32 jim
544 Fixed small memory leak
545 
546 Revision 1.4 2007/03/01 12:42:59 jim
547 Modified slightly after code checking
548 
549 Revision 1.3 2006/06/15 09:58:59 jim
550 Minor changes to docs
551 
552 Revision 1.2 2006/06/06 13:08:25 jim
553 Fixed minor doc problem
554 
555 Revision 1.1 2006/06/01 13:57:29 jim
556 First entry
557 
558 
559 */