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