VIRCAM Pipeline  2.3.12
vircam_defringe.c
1 /* $Id: vircam_defringe.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 
37 #include <casu_utils.h>
38 #include <casu_mods.h>
39 
40 #include "vircam_utils.h"
41 #include "vircam_dfs.h"
42 #include "vircam_mods.h"
43 
44 /* Function prototypes */
45 
46 static int vircam_defringe_create(cpl_plugin *) ;
47 static int vircam_defringe_exec(cpl_plugin *) ;
48 static int vircam_defringe_destroy(cpl_plugin *) ;
49 static int vircam_defringe_test(cpl_parameterlist *, cpl_frameset *) ;
50 static int vircam_defringe_save(cpl_frameset *framelist,
51  cpl_parameterlist *parlist);
52 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
53  cpl_parameterlist *parlist);
54 static void vircam_defringe_init(void);
55 static void vircam_defringe_tidy(int level);
56 
57 static struct {
58 
59  /* Input */
60 
61  int nbsize;
62  int extenum;
63 
64 } vircam_defringe_config;
65 
66 static struct {
67  cpl_size *labels;
68  cpl_frame *fringe;
69  cpl_frame *img;
70  casu_fits *fringef;
71  casu_fits *imgf;
72  casu_mask *bpm;
73 } ps;
74 
75 static int isfirst;
76 static int dummy;
77 static cpl_frame *product_frame = NULL;
78 
79 static char vircam_defringe_description[] =
80 "vircam_defringe -- VIRCAM defringe test recipe.\n\n"
81 "Defringe an input frame using a master fringe frame\n\n"
82 "The program accepts the following files in the SOF:\n\n"
83 " Tag Description\n"
84 " -----------------------------------------------------------------------\n"
85 " %-21s A input uncorrected image\n"
86 " %-21s A master fringe frame\n"
87 " %-21s Optional master bad pixel map or\n"
88 " %-21s Optional master confidence map\n"
89 "\n";
90 
137 /* Function code */
138 
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_defringe_description,
155  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_FRINGE,VIRCAM_CAL_BPM,
156  VIRCAM_CAL_CONF);
157 
158  cpl_plugin_init(plugin,
159  CPL_PLUGIN_API,
160  VIRCAM_BINARY_VERSION,
161  CPL_PLUGIN_TYPE_RECIPE,
162  "vircam_defringe",
163  "VIRCAM fringe correction test recipe [test]",
164  alldesc,
165  "Jim Lewis",
166  "jrl@ast.cam.ac.uk",
168  vircam_defringe_create,
169  vircam_defringe_exec,
170  vircam_defringe_destroy);
171 
172  cpl_pluginlist_append(list,plugin);
173 
174  return(0);
175 }
176 
177 /*---------------------------------------------------------------------------*/
186 /*---------------------------------------------------------------------------*/
187 
188 static int vircam_defringe_create(cpl_plugin *plugin) {
189  cpl_recipe *recipe;
190  cpl_parameter *p;
191 
192  /* Get the recipe out of the plugin */
193 
194  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
195  recipe = (cpl_recipe *)plugin;
196  else
197  return(-1);
198 
199  /* Create the parameters list in the cpl_recipe object */
200 
201  recipe->parameters = cpl_parameterlist_new();
202 
203  /* Fill in the parameters. First the background cell size */
204 
205  p = cpl_parameter_new_value("vircam.vircam_defringe.nbsize",
206  CPL_TYPE_INT,
207  "Cell size in pixels",
208  "vircam.vircam_defringe",128);
209  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nbsize");
210  cpl_parameterlist_append(recipe->parameters,p);
211 
212  /* Extension number of input frames to use */
213 
214  p = cpl_parameter_new_range("vircam.vircam_defringe.extenum",
215  CPL_TYPE_INT,
216  "Extension number to be done, 0 == all",
217  "vircam.vircam_defringe",1,0,16);
218  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
219  cpl_parameterlist_append(recipe->parameters,p);
220 
221  /* Get out of here */
222 
223  return(0);
224 }
225 
226 /*---------------------------------------------------------------------------*/
232 /*---------------------------------------------------------------------------*/
233 
234 static int vircam_defringe_exec(cpl_plugin *plugin) {
235  cpl_recipe *recipe;
236 
237  /* Get the recipe out of the plugin */
238 
239  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
240  recipe = (cpl_recipe *)plugin;
241  else
242  return(-1);
243 
244  return(vircam_defringe_test(recipe->parameters,recipe->frames));
245 }
246 
247 /*---------------------------------------------------------------------------*/
253 /*---------------------------------------------------------------------------*/
254 
255 static int vircam_defringe_destroy(cpl_plugin *plugin) {
256  cpl_recipe *recipe ;
257 
258  /* Get the recipe out of the plugin */
259 
260  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
261  recipe = (cpl_recipe *)plugin;
262  else
263  return(-1);
264 
265  cpl_parameterlist_delete(recipe->parameters);
266  return(0);
267 }
268 
269 /*---------------------------------------------------------------------------*/
276 /*---------------------------------------------------------------------------*/
277 
278 static int vircam_defringe_test(cpl_parameterlist *parlist,
279  cpl_frameset *framelist) {
280  const char *fctid="vircam_defringe";
281  cpl_parameter *p;
282  int jst,jfn,status,j,retval,nx,ny;
283  cpl_size nlab;
284 
285  /* Check validity of input frameset */
286 
287  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
288  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
289  return(-1);
290  }
291 
292  /* Initialise some things */
293 
294  vircam_defringe_init();
295 
296  /* Get the parameters */
297 
298  p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.nbsize");
299  vircam_defringe_config.nbsize = cpl_parameter_get_int(p);
300  p = cpl_parameterlist_find(parlist,"vircam.vircam_defringe.extenum");
301  vircam_defringe_config.extenum = cpl_parameter_get_int(p);
302 
303  /* Sort out raw from calib frames */
304 
305  if (vircam_dfs_set_groups(framelist) != CASU_OK) {
306  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
307  vircam_defringe_tidy(2);
308  return(-1);
309  }
310 
311  /* Get the frames */
312 
313  if ((ps.labels = cpl_frameset_labelise(framelist,casu_compare_tags,
314  &nlab)) == NULL) {
315  cpl_msg_error(fctid,"Cannot labelise the input frames");
316  vircam_defringe_tidy(2);
317  return(-1);
318  }
319  if ((ps.img = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
320  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
321  cpl_msg_info(fctid,"No raw image found -- cannot continue");
322  vircam_defringe_tidy(2);
323  return(-1);
324  }
325  if ((ps.fringe = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
326  VIRCAM_CAL_FRINGE)) == NULL) {
327  cpl_msg_info(fctid,"No fringe image found -- cannot continue");
328  vircam_defringe_tidy(2);
329  return(-1);
330  }
331 
332  /* Get the master mask */
333 
334  ps.bpm = casu_mask_define(framelist,ps.labels,nlab,VIRCAM_CAL_CONF,
335  VIRCAM_CAL_BPM);
336 
337  /* Now, how many image extensions do we want to do? If the extension
338  number is zero, then we loop for all possible extensions. If it
339  isn't then we just do the extension specified */
340 
341  vircam_exten_range(vircam_defringe_config.extenum,(const cpl_frame *)ps.img,
342  &jst,&jfn);
343  if (jst == -1 || jfn == -1) {
344  cpl_msg_error(fctid,"Unable to continue");
345  vircam_defringe_tidy(2);
346  return(-1);
347  }
348 
349  /* Now loop for all the extension... */
350 
351  status = CASU_OK;
352  for (j = jst; j <= jfn; j++) {
353  isfirst = (j == jst);
354 
355  /* Load up the images */
356 
357  ps.imgf = casu_fits_load(ps.img,CPL_TYPE_FLOAT,j);
358  ps.fringef = casu_fits_load(ps.fringe,CPL_TYPE_FLOAT,j);
359  if (ps.img == NULL || ps.fringef == NULL) {
360  cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " frame wouldn't load",
361  (cpl_size)j);
362  dummy = 1;
363  retval = vircam_defringe_lastbit(j,framelist,parlist);
364  if (retval != 0)
365  return(-1);
366  }
367 
368  /* Get the size of the flat image */
369 
370  nx = (int)cpl_image_get_size_x(casu_fits_get_image(ps.imgf));
371  ny = (int)cpl_image_get_size_y(casu_fits_get_image(ps.imgf));
372 
373  /* Load the data for the bpm */
374 
375  (void)casu_mask_load(ps.bpm,j,nx,ny);
376 
377  /* Now do the correction */
378 
379  cpl_msg_info(fctid,"Doing the fringe correction for extension %" CPL_SIZE_FORMAT,
380  (cpl_size)j);
381  (void)casu_defringe(&(ps.imgf),1,&(ps.fringef),1,ps.bpm,
382  vircam_defringe_config.nbsize,&status);
383  if (status != CASU_OK) {
384  cpl_msg_info(fctid,"Extension %" CPL_SIZE_FORMAT " defringe failed",
385  (cpl_size)j);
386  dummy = 1;
387  retval = vircam_defringe_lastbit(j,framelist,parlist);
388  if (retval != 0)
389  return(-1);
390  }
391 
392  /* Now save the result */
393 
394  retval = vircam_defringe_lastbit(j,framelist,parlist);
395  if (retval != 0)
396  return(-1);
397  }
398  vircam_defringe_tidy(2);
399  return(0);
400 }
401 
402 /*---------------------------------------------------------------------------*/
409 /*---------------------------------------------------------------------------*/
410 
411 static int vircam_defringe_save(cpl_frameset *framelist,
412  cpl_parameterlist *parlist) {
413  const char *fctid = "vircam_defringe_save";
414  const char *outfile = "defringe.fits";
415  const char *recipeid = "vircam_defringe";
416  cpl_propertylist *plist;
417 
418  /* If we need to make a PHU then do that now based on the first frame
419  in the input frame list */
420 
421  if (isfirst) {
422 
423  /* Create a new product frame object and define some tags */
424 
425  product_frame = cpl_frame_new();
426  cpl_frame_set_filename(product_frame,outfile);
427  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
428  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
429  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
430  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
431 
432  /* Set up the header */
433 
434  plist = casu_fits_get_phu(ps.imgf);
435  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
436  parlist,(char *)recipeid,
437  "?Dictionary?",NULL,0);
438 
439  /* 'Save' the PHU image */
440 
441  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
442  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
443  cpl_msg_error(fctid,"Cannot save product PHU");
444  cpl_frame_delete(product_frame);
445  return(-1);
446  }
447  cpl_frameset_insert(framelist,product_frame);
448  }
449 
450  /* Get the extension property list */
451 
452  plist = casu_fits_get_ehu(ps.imgf);
453 
454  /* Fiddle with the header now */
455 
456  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
457  (char *)recipeid,"?Dictionary?",NULL);
458  if (dummy)
459  casu_dummy_property(plist);
460 
461  /* Save the image */
462 
463  if (cpl_image_save(casu_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
464  plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
465  cpl_msg_error(fctid,"Cannot save product image extension");
466  return(-1);
467  }
468 
469  return(0);
470 }
471 
472 /*---------------------------------------------------------------------------*/
480 /*---------------------------------------------------------------------------*/
481 
482 static int vircam_defringe_lastbit(int jext, cpl_frameset *framelist,
483  cpl_parameterlist *parlist) {
484  int retval;
485  const char *fctid="vircam_defringe_lastbit";
486 
487  /* Save everything */
488 
489  cpl_msg_info(fctid,"Saving products for extension %" CPL_SIZE_FORMAT,
490  (cpl_size)jext);
491  retval = vircam_defringe_save(framelist,parlist);
492  if (retval != 0) {
493  vircam_defringe_tidy(2);
494  return(-1);
495  }
496 
497  /* Free some stuff up */
498 
499  vircam_defringe_tidy(1);
500  return(0);
501 }
502 
503 
504 /*---------------------------------------------------------------------------*/
508 /*---------------------------------------------------------------------------*/
509 
510 static void vircam_defringe_init(void) {
511  ps.labels = NULL;
512  ps.fringe = NULL;
513  ps.fringef = NULL;
514  ps.img = NULL;
515  ps.imgf = NULL;
516 }
517 
518 
519 /*---------------------------------------------------------------------------*/
523 /*---------------------------------------------------------------------------*/
524 
525 static void vircam_defringe_tidy(int level) {
526  freefits(ps.imgf);
527  freefits(ps.fringef);
528  casu_mask_clear(ps.bpm);
529  if (level == 1)
530  return;
531  freemask(ps.bpm);
532  freeframe(ps.img);
533  freeframe(ps.fringe);
534  freespace(ps.labels);
535 }
536 
539 /*
540 
541 $Log: not supported by cvs2svn $
542 Revision 1.8 2012/01/15 17:40:09 jim
543 Minor modifications to take into accout the changes in cpl API for v6
544 
545 Revision 1.7 2009/09/09 09:51:13 jim
546 modified to use new saving routines so that headers are right
547 
548 Revision 1.6 2007/10/15 12:53:55 jim
549 Modified for compatibility with cpl_4.0
550 
551 Revision 1.5 2007/07/09 13:22:08 jim
552 Modified to use new version of vircam_exten_range
553 
554 Revision 1.4 2007/04/13 12:27:38 jim
555 Added some extra docs
556 
557 Revision 1.3 2007/04/04 10:36:29 jim
558 Modified to use new dfs tags
559 
560 Revision 1.2 2007/03/01 12:42:58 jim
561 Modified slightly after code checking
562 
563 Revision 1.1 2006/12/06 13:00:04 jim
564 Initial entry
565 
566 
567 */
568 
569 
570 
571 
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
int casu_defringe(casu_fits **infiles, int nimages, casu_fits **fringes, int nfringes, casu_mask *mask, int nbsize, int *status)
Correct input data to remove fringes.
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_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