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