VIRCAM Pipeline  1.3.3
tests/vircam_flatcor.c
1 /* $Id: vircam_flatcor.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 
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_flatcor_create(cpl_plugin *) ;
45 static int vircam_flatcor_exec(cpl_plugin *) ;
46 static int vircam_flatcor_destroy(cpl_plugin *) ;
47 static int vircam_flatcor_test(cpl_parameterlist *, cpl_frameset *) ;
48 static int vircam_flatcor_save(cpl_frameset *framelist,
49  cpl_parameterlist *parlist);
50 static void vircam_flatcor_init(void);
51 static void vircam_flatcor_tidy(void);
52 
53 static struct {
54 
55  /* Input */
56 
57  int extenum;
58 
59 } vircam_flatcor_config;
60 
61 static struct {
62  cpl_size *labels;
63  cpl_frame *flat;
64  cpl_frame *img;
65  vir_fits *flatf;
66  vir_fits *imgf;
67 } ps;
68 
69 static int isfirst;
70 static cpl_frame *product_frame = NULL;
71 
72 static char vircam_flatcor_description[] =
73 "vircam_flatcor -- VIRCAM flat correction test recipe.\n\n"
74 "Flat correct an input frame using a master flat frame\n\n"
75 "The program accepts the following files in the SOF:\n\n"
76 " Tag Description\n"
77 " -----------------------------------------------------------------------\n"
78 " %-21s A input uncorrected image\n"
79 " %-21s A master flat field frame\n"
80 "\n";
81 
127 /* Function code */
128 
129 /*---------------------------------------------------------------------------*/
137 /*---------------------------------------------------------------------------*/
138 
139 int cpl_plugin_get_info(cpl_pluginlist *list) {
140  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
141  cpl_plugin *plugin = &recipe->interface;
142  char alldesc[SZ_ALLDESC];
143  (void)snprintf(alldesc,SZ_ALLDESC,vircam_flatcor_description,
144  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_TWILIGHT_FLAT);
145 
146  cpl_plugin_init(plugin,
147  CPL_PLUGIN_API,
148  VIRCAM_BINARY_VERSION,
149  CPL_PLUGIN_TYPE_RECIPE,
150  "vircam_flatcor",
151  "VIRCAM flat field division test recipe [test]",
152  alldesc,
153  "Jim Lewis",
154  "jrl@ast.cam.ac.uk",
156  vircam_flatcor_create,
157  vircam_flatcor_exec,
158  vircam_flatcor_destroy);
159 
160  cpl_pluginlist_append(list,plugin);
161 
162  return(0);
163 }
164 
165 /*---------------------------------------------------------------------------*/
174 /*---------------------------------------------------------------------------*/
175 
176 static int vircam_flatcor_create(cpl_plugin *plugin) {
177  cpl_recipe *recipe;
178  cpl_parameter *p;
179 
180  /* Get the recipe out of the plugin */
181 
182  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
183  recipe = (cpl_recipe *)plugin;
184  else
185  return(-1);
186 
187  /* Create the parameters list in the cpl_recipe object */
188 
189  recipe->parameters = cpl_parameterlist_new();
190 
191  /* Get the extension number of input frames to use */
192 
193  p = cpl_parameter_new_range("vircam.vircam_flatcor.extenum",
194  CPL_TYPE_INT,
195  "Extension number to be done, 0 == all",
196  "vircam.vircam_flatcor",1,0,16);
197  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
198  cpl_parameterlist_append(recipe->parameters,p);
199 
200  /* Get out of here */
201 
202  return(0);
203 }
204 
205 /*---------------------------------------------------------------------------*/
211 /*---------------------------------------------------------------------------*/
212 
213 static int vircam_flatcor_exec(cpl_plugin *plugin) {
214  cpl_recipe *recipe;
215 
216  /* Get the recipe out of the plugin */
217 
218  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
219  recipe = (cpl_recipe *)plugin;
220  else
221  return(-1);
222 
223  return(vircam_flatcor_test(recipe->parameters,recipe->frames));
224 }
225 
226 
227 /*---------------------------------------------------------------------------*/
233 /*---------------------------------------------------------------------------*/
234 
235 static int vircam_flatcor_destroy(cpl_plugin *plugin) {
236  cpl_recipe *recipe ;
237 
238  /* Get the recipe out of the plugin */
239 
240  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
241  recipe = (cpl_recipe *)plugin;
242  else
243  return(-1);
244 
245  cpl_parameterlist_delete(recipe->parameters);
246  return(0);
247 }
248 
249 /*---------------------------------------------------------------------------*/
256 /*---------------------------------------------------------------------------*/
257 
258 static int vircam_flatcor_test(cpl_parameterlist *parlist,
259  cpl_frameset *framelist) {
260  const char *fctid="vircam_flatcor";
261  cpl_parameter *p;
262  int jst,jfn,status,j;
263  cpl_size nlab;
264 
265  /* Check validity of input frameset */
266 
267  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
268  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
269  return(-1);
270  }
271 
272  /* Initialise some things */
273 
274  vircam_flatcor_init();
275 
276  /* Get the parameters */
277 
278  p = cpl_parameterlist_find(parlist,"vircam.vircam_flatcor.extenum");
279  vircam_flatcor_config.extenum = cpl_parameter_get_int(p);
280 
281  /* Sort out raw from calib frames */
282 
283  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
284  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
285  vircam_flatcor_tidy();
286  return(-1);
287  }
288 
289  /* Get the frames */
290 
291  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
292  &nlab)) == NULL) {
293  cpl_msg_error(fctid,"Cannot labelise the input frames");
294  vircam_flatcor_tidy();
295  return(-1);
296  }
297  if ((ps.flat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
298  VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
299  cpl_msg_info(fctid,"No master flat found -- cannot continue");
300  vircam_flatcor_tidy();
301  return(-1);
302  }
303  if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
304  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
305  cpl_msg_info(fctid,"No raw image found -- cannot continue");
306  vircam_flatcor_tidy();
307  return(-1);
308  }
309 
310  /* Now, how many image extensions do we want to do? If the extension
311  number is zero, then we loop for all possible extensions. If it
312  isn't then we just do the extension specified */
313 
314  vircam_exten_range(vircam_flatcor_config.extenum,(const cpl_frame *)ps.img,
315  &jst,&jfn);
316  if (jst == -1 || jfn == -1) {
317  cpl_msg_error(fctid,"Unable to continue");
318  vircam_flatcor_tidy();
319  return(-1);
320  }
321 
322  /* Now loop for all the extension... */
323 
324  status = VIR_OK;
325  for (j = jst; j <= jfn; j++) {
326  isfirst = (j == jst);
327 
328  /* Load up the images */
329 
330  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
331  ps.flatf = vircam_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
332  if (ps.img == NULL || ps.flatf == NULL) {
333  vircam_flatcor_tidy();
334  return(-1);
335  }
336 
337  /* Now do the correction */
338 
339  cpl_msg_info(fctid,"Doing the flat fielding for extension %" CPL_SIZE_FORMAT,
340  (cpl_size)j);
341  (void)vircam_flatcor(ps.imgf,ps.flatf,&status);
342  if (status != VIR_OK) {
343  vircam_flatcor_tidy();
344  return(-1);
345  }
346 
347  /* Now save the result */
348 
349  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
350  (cpl_size)j);
351  if (vircam_flatcor_save(framelist,parlist) != 0) {
352  vircam_flatcor_tidy();
353  return(-1);
354  }
355 
356  /* Tidy a few things before the next image */
357 
358  freefits(ps.imgf);
359  freefits(ps.flatf);
360  }
361  vircam_flatcor_tidy();
362  return(0);
363 }
364 
365 
366 /*---------------------------------------------------------------------------*/
373 /*---------------------------------------------------------------------------*/
374 
375 static int vircam_flatcor_save(cpl_frameset *framelist,
376  cpl_parameterlist *parlist) {
377  const char *fctid = "vircam_flatcor_save";
378  const char *outfile = "flatcor.fits";
379  const char *recipeid = "vircam_flatcor";
380  cpl_propertylist *plist;
381 
382  /* If we need to make a PHU then do that now based on the first frame
383  in the input frame list */
384 
385  if (isfirst) {
386 
387  /* Create a new product frame object and define some tags */
388 
389  product_frame = cpl_frame_new();
390  cpl_frame_set_filename(product_frame,outfile);
391  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
392  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
393  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
394  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
395 
396  /* Set up the product phu */
397 
398  plist = vircam_fits_get_phu(ps.imgf);
399  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
400  parlist,(char *)recipeid,
401  "?Dictionary?",NULL,0);
402 
403  /* 'Save' the PHU image */
404 
405  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
406  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
407  cpl_msg_error(fctid,"Cannot save product PHU");
408  cpl_frame_delete(product_frame);
409  return(-1);
410  }
411  cpl_frameset_insert(framelist,product_frame);
412  }
413 
414  /* Get the extension property list */
415 
416  plist = vircam_fits_get_ehu(ps.imgf);
417 
418  /* Fiddle with the header now */
419 
420  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
421  (char *)recipeid,"?Dictionary?",NULL);
422 
423  /* Save the image */
424 
425  if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
426  plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
427  cpl_msg_error(fctid,"Cannot save product image extension");
428  return(-1);
429  }
430 
431  return(0);
432 }
433 
434 
435 /*---------------------------------------------------------------------------*/
439 /*---------------------------------------------------------------------------*/
440 
441 static void vircam_flatcor_init(void) {
442  ps.labels = NULL;
443  ps.flat = NULL;
444  ps.flatf = NULL;
445  ps.img = NULL;
446  ps.imgf = NULL;
447 }
448 
449 
450 /*---------------------------------------------------------------------------*/
454 /*---------------------------------------------------------------------------*/
455 
456 static void vircam_flatcor_tidy(void) {
457  freespace(ps.labels);
458  freefits(ps.imgf);
459  freefits(ps.flatf);
460  freeframe(ps.flat);
461  freeframe(ps.img);
462 }
463 
466 /*
467 
468 $Log: not supported by cvs2svn $
469 Revision 1.13 2012/01/15 17:40:09 jim
470 Minor modifications to take into accout the changes in cpl API for v6
471 
472 Revision 1.12 2009/09/09 09:51:13 jim
473 modified to use new saving routines so that headers are right
474 
475 Revision 1.11 2007/10/15 12:53:55 jim
476 Modified for compatibility with cpl_4.0
477 
478 Revision 1.10 2007/07/09 13:22:08 jim
479 Modified to use new version of vircam_exten_range
480 
481 Revision 1.9 2007/04/13 12:27:38 jim
482 Added some extra docs
483 
484 Revision 1.8 2007/04/04 10:36:29 jim
485 Modified to use new dfs tags
486 
487 Revision 1.7 2007/03/01 12:42:59 jim
488 Modified slightly after code checking
489 
490 Revision 1.6 2006/06/15 09:58:59 jim
491 Minor changes to docs
492 
493 Revision 1.5 2006/05/04 11:53:40 jim
494 Fixed _save routine so that it's more consistent with the standard CPL
495 way of doing things
496 
497 Revision 1.4 2006/05/02 11:29:12 jim
498 Fixed problem where propertylist was being deleted incorrectly
499 
500 Revision 1.3 2006/04/27 14:22:04 jim
501 Fixed docs
502 
503 Revision 1.2 2006/04/24 13:46:13 jim
504 fixed problem in docs
505 
506 Revision 1.1 2006/04/24 10:42:44 jim
507 New routine
508 
509 
510 */
511 
512 
513 
514