VIRCAM Pipeline  2.3.10
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) 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: 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 #include <casu_utils.h>
37 
38 #include "vircam_utils.h"
39 #include "vircam_dfs.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  casu_fits *imgf;
67  casu_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) != CASU_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,casu_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 = casu_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 = casu_mask_define(framelist,ps.labels,nlab,VIRCAM_CAL_CONF,
311  VIRCAM_CAL_BPM);
312 
313  /* Now, how many image extensions do we want to do? If the extension
314  number is zero, then we loop for all possible extensions. If it
315  isn't then we just do the extension specified */
316 
317  vircam_exten_range(vircam_destripe_config.extenum,ps.img,&jst,&jfn);
318  if (jst == -1 || jfn == -1) {
319  cpl_msg_error(fctid,"Unable to continue");
320  vircam_destripe_tidy(2);
321  return(-1);
322  }
323 
324  /* Now loop for all the extension... */
325 
326  for (j = jst; j <= jfn; j++) {
327  status = CASU_OK;
328  isfirst = (j == jst);
329  dummy = 0;
330 
331  /* Load up the images */
332 
333  ps.imgf = casu_fits_load(ps.img,CPL_TYPE_FLOAT,j);
334  if (ps.img == NULL) {
335  cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
336  (cpl_size)j);
337  dummy = 1;
338  retval = vircam_destripe_lastbit(j,framelist,parlist);
339  if (retval != 0)
340  return(-1);
341  }
342 
343  /* Get the size of the flat image */
344 
345  nx = (int)cpl_image_get_size_x(casu_fits_get_image(ps.imgf));
346  ny = (int)cpl_image_get_size_y(casu_fits_get_image(ps.imgf));
347 
348  /* Load the data for the bpm */
349 
350  (void)casu_mask_load(ps.bpm,j,nx,ny);
351 
352  /* Now do the correction */
353 
354  cpl_msg_info(fctid,"Doing the stripe correction for extension %" CPL_SIZE_FORMAT,
355  (cpl_size)j);
356  (void)vircam_destripe(ps.imgf,ps.bpm,&status);
357  if (status != CASU_OK) {
358  cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " destripe failed",
359  (cpl_size)j);
360  dummy = 1;
361  retval = vircam_destripe_lastbit(j,framelist,parlist);
362  if (retval != 0)
363  return(-1);
364  }
365 
366  /* Now save the result */
367 
368  retval = vircam_destripe_lastbit(j,framelist,parlist);
369  if (retval != 0)
370  return(-1);
371  }
372  vircam_destripe_tidy(2);
373  return(0);
374 }
375 
376 /*---------------------------------------------------------------------------*/
383 /*---------------------------------------------------------------------------*/
384 
385 static int vircam_destripe_save(cpl_frameset *framelist,
386  cpl_parameterlist *parlist) {
387  const char *fctid = "vircam_destripe_save";
388  const char *outfile = "destripe.fits";
389  const char *recipeid = "vircam_destripe";
390  cpl_propertylist *plist;
391 
392  /* If we need to make a PHU then do that now based on the first frame
393  in the input frame list */
394 
395  if (isfirst) {
396 
397  /* Create a new product frame object and define some tags */
398 
399  product_frame = cpl_frame_new();
400  cpl_frame_set_filename(product_frame,outfile);
401  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
402  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
403  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
404  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
405 
406  /* Set up the header */
407 
408  plist = casu_fits_get_phu(ps.imgf);
409  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
410  parlist,(char *)recipeid,
411  "?Dictionary?",NULL,0);
412 
413  /* 'Save' the PHU image */
414 
415  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
416  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
417  cpl_msg_error(fctid,"Cannot save product PHU");
418  cpl_frame_delete(product_frame);
419  return(-1);
420  }
421  cpl_frameset_insert(framelist,product_frame);
422  }
423 
424  /* Get the extension property list */
425 
426  plist = casu_fits_get_ehu(ps.imgf);
427 
428  /* Fiddle with the header now */
429 
430  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
431  (char *)recipeid,"?Dictionary?",NULL);
432  if (dummy)
433  casu_dummy_property(plist);
434 
435  /* Save the image */
436 
437  if (cpl_image_save(casu_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
438  plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
439  cpl_msg_error(fctid,"Cannot save product image extension");
440  return(-1);
441  }
442 
443  return(0);
444 }
445 
446 /*---------------------------------------------------------------------------*/
454 /*---------------------------------------------------------------------------*/
455 
456 static int vircam_destripe_lastbit(int jext, cpl_frameset *framelist,
457  cpl_parameterlist *parlist) {
458  int retval;
459  const char *fctid="vircam_destripe_lastbit";
460 
461  /* Save everything */
462 
463  cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
464  (cpl_size)jext);
465  retval = vircam_destripe_save(framelist,parlist);
466  if (retval != 0) {
467  vircam_destripe_tidy(2);
468  return(-1);
469  }
470 
471  /* Free some stuff up */
472 
473  vircam_destripe_tidy(1);
474  return(0);
475 }
476 
477 /*---------------------------------------------------------------------------*/
481 /*---------------------------------------------------------------------------*/
482 
483 static void vircam_destripe_init(void) {
484  ps.labels = NULL;
485  ps.img = NULL;
486  ps.imgf = NULL;
487  ps.bpm = NULL;
488 }
489 
490 
491 /*---------------------------------------------------------------------------*/
495 /*---------------------------------------------------------------------------*/
496 
497 static void vircam_destripe_tidy(int level) {
498  if (level == 1) {
499  freefits(ps.imgf);
500  casu_mask_clear(ps.bpm);
501  } else {
502  freefits(ps.imgf);
503  freemask(ps.bpm);
504  freespace(ps.labels);
505  freeframe(ps.img);
506  }
507 }
508 
511 /*
512 
513 $Log: not supported by cvs2svn $
514 Revision 1.8 2012/01/15 17:40:09 jim
515 Minor modifications to take into accout the changes in cpl API for v6
516 
517 Revision 1.7 2009/09/09 09:51:13 jim
518 modified to use new saving routines so that headers are right
519 
520 Revision 1.6 2007/10/15 12:53:55 jim
521 Modified for compatibility with cpl_4.0
522 
523 Revision 1.5 2007/07/09 13:22:08 jim
524 Modified to use new version of vircam_exten_range
525 
526 Revision 1.4 2007/04/13 12:27:38 jim
527 Added some extra docs
528 
529 Revision 1.3 2007/04/04 10:36:29 jim
530 Modified to use new dfs tags
531 
532 Revision 1.2 2007/03/01 12:42:59 jim
533 Modified slightly after code checking
534 
535 Revision 1.1 2006/10/02 13:44:23 jim
536 new file
537 
538 
539 */
540 
541 
542 
543 
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
casu_fits * casu_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: casu_fits.c:80
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_mask_load(casu_mask *m, int nexten, int nx, int ny)
Definition: casu_mask.c:214
casu_mask * casu_mask_define(cpl_frameset *framelist, cpl_size *labels, cpl_size nlab, const char *conftag, const char *bpmtag)
Definition: casu_mask.c:89
void casu_mask_clear(casu_mask *m)
Definition: casu_mask.c:357
cpl_frame * casu_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frame of a given label from a frameset.
Definition: casu_utils.c:206
int casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
Definition: casu_utils.c:96
void casu_dummy_property(cpl_propertylist *p)
Set dummy property keyword.
Definition: casu_utils.c:445
int vircam_destripe(casu_fits *in, casu_mask *inbpm, int *status)
Remove stripes from the background of an image.
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