VIRCAM Pipeline  1.3.4
tests/vircam_destripe.c
1 /* $Id: vircam_destripe.c,v 1.9 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.9 $
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 
37 #include "vircam_utils.h"
38 #include "vircam_dfs.h"
39 #include "vircam_fits.h"
40 #include "vircam_mods.h"
41 
42 /* Function prototypes */
43 
44 static int vircam_destripe_create(cpl_plugin *) ;
45 static int vircam_destripe_exec(cpl_plugin *) ;
46 static int vircam_destripe_destroy(cpl_plugin *) ;
47 static int vircam_destripe_test(cpl_parameterlist *, cpl_frameset *) ;
48 static int vircam_destripe_save(cpl_frameset *framelist,
49  cpl_parameterlist *parlist);
50 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
51  cpl_parameterlist *parlist);
52 static void vircam_destripe_init(void);
53 static void vircam_destripe_tidy(int level);
54 
55 static struct {
56 
57  /* Input */
58 
59  int extenum;
60 
61 } vircam_destripe_config;
62 
63 static struct {
64  cpl_size *labels;
65  cpl_frame *img;
66  vir_fits *imgf;
67  vir_mask *bpm;
68 } ps;
69 
70 static int isfirst;
71 static int dummy;
72 static cpl_frame *product_frame = NULL;
73 
74 
75 static char vircam_destripe_description[] =
76 "vircam_destripe -- VIRCAM destripe correction test recipe.\n\n"
77 "Destripe an input frame.\n\n"
78 "The program accepts the following files in the SOF:\n\n"
79 " Tag Description\n"
80 " -----------------------------------------------------------------------\n"
81 " %-21s A input uncorrected image\n"
82 " %-21s Optional master bad pixel map or\n"
83 " %-21s Optional master confidence map\n"
84 "\n";
85 
131 /* Function code */
132 
133 
134 /*---------------------------------------------------------------------------*/
142 /*---------------------------------------------------------------------------*/
143 
144 int cpl_plugin_get_info(cpl_pluginlist *list) {
145  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
146  cpl_plugin *plugin = &recipe->interface;
147  char alldesc[SZ_ALLDESC];
148  (void)snprintf(alldesc,SZ_ALLDESC,vircam_destripe_description,
149  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_BPM,VIRCAM_CAL_CONF);
150 
151  cpl_plugin_init(plugin,
152  CPL_PLUGIN_API,
153  VIRCAM_BINARY_VERSION,
154  CPL_PLUGIN_TYPE_RECIPE,
155  "vircam_destripe",
156  "VIRCAM destripe correction test recipe [test]",
157  alldesc,
158  "Jim Lewis",
159  "jrl@ast.cam.ac.uk",
161  vircam_destripe_create,
162  vircam_destripe_exec,
163  vircam_destripe_destroy);
164 
165  cpl_pluginlist_append(list,plugin);
166 
167  return(0);
168 }
169 
170 /*---------------------------------------------------------------------------*/
179 /*---------------------------------------------------------------------------*/
180 
181 static int vircam_destripe_create(cpl_plugin *plugin) {
182  cpl_recipe *recipe;
183  cpl_parameter *p;
184 
185  /* Get the recipe out of the plugin */
186 
187  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
188  recipe = (cpl_recipe *)plugin;
189  else
190  return(-1);
191 
192  /* Create the parameters list in the cpl_recipe object */
193 
194  recipe->parameters = cpl_parameterlist_new();
195 
196  /* Extension number of input frames to use */
197 
198  p = cpl_parameter_new_range("vircam.vircam_destripe.extenum",
199  CPL_TYPE_INT,
200  "Extension number to be done, 0 == all",
201  "vircam.vircam_destripe",1,0,16);
202  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
203  cpl_parameterlist_append(recipe->parameters,p);
204 
205  /* Get out of here */
206 
207  return(0);
208 }
209 
210 /*---------------------------------------------------------------------------*/
216 /*---------------------------------------------------------------------------*/
217 
218 static int vircam_destripe_exec(cpl_plugin *plugin) {
219  cpl_recipe *recipe;
220 
221  /* Get the recipe out of the plugin */
222 
223  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
224  recipe = (cpl_recipe *)plugin;
225  else
226  return(-1);
227 
228  return(vircam_destripe_test(recipe->parameters,recipe->frames));
229 }
230 
231 /*---------------------------------------------------------------------------*/
237 /*---------------------------------------------------------------------------*/
238 
239 static int vircam_destripe_destroy(cpl_plugin *plugin) {
240  cpl_recipe *recipe ;
241 
242  /* Get the recipe out of the plugin */
243 
244  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
245  recipe = (cpl_recipe *)plugin;
246  else
247  return(-1);
248 
249  cpl_parameterlist_delete(recipe->parameters);
250  return(0);
251 }
252 
253 /*---------------------------------------------------------------------------*/
260 /*---------------------------------------------------------------------------*/
261 
262 static int vircam_destripe_test(cpl_parameterlist *parlist,
263  cpl_frameset *framelist) {
264  const char *fctid="vircam_destripe";
265  cpl_parameter *p;
266  int jst,jfn,status,j,nx,ny,retval;
267  cpl_size nlab;
268 
269  /* Check validity of input frameset */
270 
271  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
272  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
273  return(-1);
274  }
275 
276  /* Initialise some things */
277 
278  vircam_destripe_init();
279 
280  /* Get the parameters */
281 
282  p = cpl_parameterlist_find(parlist,"vircam.vircam_destripe.extenum");
283  vircam_destripe_config.extenum = cpl_parameter_get_int(p);
284 
285  /* Sort out raw from calib frames */
286 
287  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
288  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
289  vircam_destripe_tidy(2);
290  return(-1);
291  }
292 
293  /* Get the frames */
294 
295  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
296  &nlab)) == NULL) {
297  cpl_msg_error(fctid,"Cannot labelise the input frames");
298  vircam_destripe_tidy(2);
299  return(-1);
300  }
301  if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
302  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
303  cpl_msg_info(fctid,"No raw image found -- cannot continue");
304  vircam_destripe_tidy(2);
305  return(-1);
306  }
307 
308  /* Get the master mask */
309 
310  ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
311 
312  /* Now, how many image extensions do we want to do? If the extension
313  number is zero, then we loop for all possible extensions. If it
314  isn't then we just do the extension specified */
315 
316  vircam_exten_range(vircam_destripe_config.extenum,ps.img,&jst,&jfn);
317  if (jst == -1 || jfn == -1) {
318  cpl_msg_error(fctid,"Unable to continue");
319  vircam_destripe_tidy(2);
320  return(-1);
321  }
322 
323  /* Now loop for all the extension... */
324 
325  for (j = jst; j <= jfn; j++) {
326  status = VIR_OK;
327  isfirst = (j == jst);
328  dummy = 0;
329 
330  /* Load up the images */
331 
332  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
333  if (ps.img == NULL) {
334  cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
335  (cpl_size)j);
336  dummy = 1;
337  retval = vircam_destripe_lastbit(j,framelist,parlist);
338  if (retval != 0)
339  return(-1);
340  }
341 
342  /* Get the size of the flat image */
343 
344  nx = (int)cpl_image_get_size_x(vircam_fits_get_image(ps.imgf));
345  ny = (int)cpl_image_get_size_y(vircam_fits_get_image(ps.imgf));
346 
347  /* Load the data for the bpm */
348 
349  (void)vircam_mask_load(ps.bpm,j,nx,ny);
350 
351  /* Now do the correction */
352 
353  cpl_msg_info(fctid,"Doing the stripe correction for extension %" CPL_SIZE_FORMAT,
354  (cpl_size)j);
355  (void)vircam_destripe(ps.imgf,ps.bpm,&status);
356  if (status != VIR_OK) {
357  cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " destripe failed",
358  (cpl_size)j);
359  dummy = 1;
360  retval = vircam_destripe_lastbit(j,framelist,parlist);
361  if (retval != 0)
362  return(-1);
363  }
364 
365  /* Now save the result */
366 
367  retval = vircam_destripe_lastbit(j,framelist,parlist);
368  if (retval != 0)
369  return(-1);
370  }
371  vircam_destripe_tidy(2);
372  return(0);
373 }
374 
375 /*---------------------------------------------------------------------------*/
382 /*---------------------------------------------------------------------------*/
383 
384 static int vircam_destripe_save(cpl_frameset *framelist,
385  cpl_parameterlist *parlist) {
386  const char *fctid = "vircam_destripe_save";
387  const char *outfile = "destripe.fits";
388  const char *recipeid = "vircam_destripe";
389  cpl_propertylist *plist;
390 
391  /* If we need to make a PHU then do that now based on the first frame
392  in the input frame list */
393 
394  if (isfirst) {
395 
396  /* Create a new product frame object and define some tags */
397 
398  product_frame = cpl_frame_new();
399  cpl_frame_set_filename(product_frame,outfile);
400  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
401  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
402  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
403  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
404 
405  /* Set up the header */
406 
407  plist = vircam_fits_get_phu(ps.imgf);
408  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
409  parlist,(char *)recipeid,
410  "?Dictionary?",NULL,0);
411 
412  /* 'Save' the PHU image */
413 
414  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
415  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
416  cpl_msg_error(fctid,"Cannot save product PHU");
417  cpl_frame_delete(product_frame);
418  return(-1);
419  }
420  cpl_frameset_insert(framelist,product_frame);
421  }
422 
423  /* Get the extension property list */
424 
425  plist = vircam_fits_get_ehu(ps.imgf);
426 
427  /* Fiddle with the header now */
428 
429  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
430  (char *)recipeid,"?Dictionary?",NULL);
431  if (dummy)
432  vircam_dummy_property(plist);
433 
434  /* Save the image */
435 
436  if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
437  plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
438  cpl_msg_error(fctid,"Cannot save product image extension");
439  return(-1);
440  }
441 
442  return(0);
443 }
444 
445 /*---------------------------------------------------------------------------*/
453 /*---------------------------------------------------------------------------*/
454 
455 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
456  cpl_parameterlist *parlist) {
457  int retval;
458  const char *fctid="vircam_destripe_lastbit";
459 
460  /* Save everything */
461 
462  cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
463  (cpl_size)jext);
464  retval = vircam_destripe_save(framelist,parlist);
465  if (retval != 0) {
466  vircam_destripe_tidy(2);
467  return(-1);
468  }
469 
470  /* Free some stuff up */
471 
472  vircam_destripe_tidy(1);
473  return(0);
474 }
475 
476 /*---------------------------------------------------------------------------*/
480 /*---------------------------------------------------------------------------*/
481 
482 static void vircam_destripe_init(void) {
483  ps.labels = NULL;
484  ps.img = NULL;
485  ps.imgf = NULL;
486  ps.bpm = NULL;
487 }
488 
489 
490 /*---------------------------------------------------------------------------*/
494 /*---------------------------------------------------------------------------*/
495 
496 static void vircam_destripe_tidy(int level) {
497  if (level == 1) {
498  freefits(ps.imgf);
499  vircam_mask_clear(ps.bpm);
500  } else {
501  freefits(ps.imgf);
502  freemask(ps.bpm);
503  freespace(ps.labels);
504  freeframe(ps.img);
505  }
506 }
507 
510 /*
511 
512 $Log: not supported by cvs2svn $
513 Revision 1.8 2012/01/15 17:40:09 jim
514 Minor modifications to take into accout the changes in cpl API for v6
515 
516 Revision 1.7 2009/09/09 09:51:13 jim
517 modified to use new saving routines so that headers are right
518 
519 Revision 1.6 2007/10/15 12:53:55 jim
520 Modified for compatibility with cpl_4.0
521 
522 Revision 1.5 2007/07/09 13:22:08 jim
523 Modified to use new version of vircam_exten_range
524 
525 Revision 1.4 2007/04/13 12:27:38 jim
526 Added some extra docs
527 
528 Revision 1.3 2007/04/04 10:36:29 jim
529 Modified to use new dfs tags
530 
531 Revision 1.2 2007/03/01 12:42:59 jim
532 Modified slightly after code checking
533 
534 Revision 1.1 2006/10/02 13:44:23 jim
535 new file
536 
537 
538 */
539 
540 
541 
542 
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
cpl_frame * vircam_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Definition: vircam_utils.c:247
void vircam_mask_clear(vir_mask *m)
Definition: vircam_mask.c:349
int vircam_mask_load(vir_mask *m, int nexten, int nx, int ny)
Definition: vircam_mask.c:210
int vircam_destripe(vir_fits *in, vir_mask *inbpm, int *status)
Remove stripes from the background of an image.
cpl_image * vircam_fits_get_image(vir_fits *p)
Definition: vircam_fits.c:349
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
void vircam_dummy_property(cpl_propertylist *p)
cpl_propertylist * vircam_fits_get_phu(vir_fits *p)
Definition: vircam_fits.c:416
vir_fits * vircam_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: vircam_fits.c:80
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
Definition: vircam_fits.c:457
vir_mask * vircam_mask_define(cpl_frameset *framelist, cpl_size *labels, cpl_size nlab)
Definition: vircam_mask.c:86
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