VIRCAM Pipeline  1.3.4
tests/vircam_gaincor.c
1 /* $Id: vircam_gaincor.c,v 1.8 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.8 $
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_gaincor_create(cpl_plugin *) ;
45 static int vircam_gaincor_exec(cpl_plugin *) ;
46 static int vircam_gaincor_destroy(cpl_plugin *) ;
47 static int vircam_gaincor_test(cpl_parameterlist *, cpl_frameset *) ;
48 static int vircam_gaincor_save(cpl_frameset *framelist,
49  cpl_parameterlist *parlist);
50 static void vircam_gaincor_init(void);
51 static void vircam_gaincor_tidy(void);
52 
53 static struct {
54 
55  /* Input */
56 
57  int extenum;
58 
59 } vircam_gaincor_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  float *gaincors;
68 } ps;
69 
70 static int isfirst;
71 static cpl_frame *product_frame = NULL;
72 
73 static char vircam_gaincor_description[] =
74 "vircam_gaincor -- VIRCAM gain correction test recipe.\n\n"
75 "Gain correct an input frame using a master flat frame\n\n"
76 "The program accepts the following files in the SOF:\n\n"
77 " Tag Description\n"
78 " -----------------------------------------------------------------------\n"
79 " %-21s A input uncorrected image\n"
80 " %-21s A master flat field frame\n"
81 "\n";
82 
128 /* Function code */
129 
130 /*---------------------------------------------------------------------------*/
138 /*---------------------------------------------------------------------------*/
139 
140 int cpl_plugin_get_info(cpl_pluginlist *list) {
141  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
142  cpl_plugin *plugin = &recipe->interface;
143  char alldesc[SZ_ALLDESC];
144  (void)snprintf(alldesc,SZ_ALLDESC,vircam_gaincor_description,
145  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_TWILIGHT_FLAT);
146 
147  cpl_plugin_init(plugin,
148  CPL_PLUGIN_API,
149  VIRCAM_BINARY_VERSION,
150  CPL_PLUGIN_TYPE_RECIPE,
151  "vircam_gaincor",
152  "VIRCAM gain correction test recipe [test]",
153  alldesc,
154  "Jim Lewis",
155  "jrl@ast.cam.ac.uk",
157  vircam_gaincor_create,
158  vircam_gaincor_exec,
159  vircam_gaincor_destroy);
160 
161  cpl_pluginlist_append(list,plugin);
162 
163  return(0);
164 }
165 
166 /*---------------------------------------------------------------------------*/
175 /*---------------------------------------------------------------------------*/
176 
177 static int vircam_gaincor_create(cpl_plugin *plugin) {
178  cpl_recipe *recipe;
179  cpl_parameter *p;
180 
181  /* Get the recipe out of the plugin */
182 
183  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
184  recipe = (cpl_recipe *)plugin;
185  else
186  return(-1);
187 
188  /* Create the parameters list in the cpl_recipe object */
189 
190  recipe->parameters = cpl_parameterlist_new();
191 
192  /* Get the extension number of input frames to use */
193 
194  p = cpl_parameter_new_range("vircam.vircam_gaincor.extenum",
195  CPL_TYPE_INT,
196  "Extension number to be done, 0 == all",
197  "vircam.vircam_gaincor",1,0,16);
198  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
199  cpl_parameterlist_append(recipe->parameters,p);
200 
201  /* Get out of here */
202 
203  return(0);
204 }
205 
206 /*---------------------------------------------------------------------------*/
212 /*---------------------------------------------------------------------------*/
213 
214 static int vircam_gaincor_exec(cpl_plugin *plugin) {
215  cpl_recipe *recipe;
216 
217  /* Get the recipe out of the plugin */
218 
219  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
220  recipe = (cpl_recipe *)plugin;
221  else
222  return(-1);
223 
224  return(vircam_gaincor_test(recipe->parameters,recipe->frames));
225 }
226 
227 
228 /*---------------------------------------------------------------------------*/
234 /*---------------------------------------------------------------------------*/
235 
236 static int vircam_gaincor_destroy(cpl_plugin *plugin) {
237  cpl_recipe *recipe ;
238 
239  /* Get the recipe out of the plugin */
240 
241  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
242  recipe = (cpl_recipe *)plugin;
243  else
244  return(-1);
245 
246  cpl_parameterlist_delete(recipe->parameters);
247  return(0);
248 }
249 
250 /*---------------------------------------------------------------------------*/
257 /*---------------------------------------------------------------------------*/
258 
259 static int vircam_gaincor_test(cpl_parameterlist *parlist,
260  cpl_frameset *framelist) {
261  const char *fctid="vircam_gaincor";
262  cpl_parameter *p;
263  int jst,jfn,status,j;
264  cpl_size nlab;
265  float gaincor_fac;
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_gaincor_init();
277 
278  /* Get the parameters */
279 
280  p = cpl_parameterlist_find(parlist,"vircam.vircam_gaincor.extenum");
281  vircam_gaincor_config.extenum = cpl_parameter_get_int(p);
282 
283  /* Sort out raw from calib frames */
284 
285  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
286  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
287  vircam_gaincor_tidy();
288  return(-1);
289  }
290 
291  /* Get the frames */
292 
293  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
294  &nlab)) == NULL) {
295  cpl_msg_error(fctid,"Cannot labelise the input frames");
296  vircam_gaincor_tidy();
297  return(-1);
298  }
299  if ((ps.flat = vircam_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_gaincor_tidy();
303  return(-1);
304  }
305  if ((ps.img = vircam_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_gaincor_tidy();
309  return(-1);
310  }
311 
312  /* Get the gain corrections */
313 
314  status = VIR_OK;
315  if (vircam_gaincor_calc(ps.flat,&j,&(ps.gaincors),&status) != VIR_OK) {
316  cpl_msg_error(fctid,"Error calculating gain corrections");
317  vircam_gaincor_tidy();
318  return(-1);
319  }
320 
321  /* Now, how many image extensions do we want to do? If the extension
322  number is zero, then we loop for all possible extensions. If it
323  isn't then we just do the extension specified */
324 
325  vircam_exten_range(vircam_gaincor_config.extenum,(const cpl_frame *)ps.img,
326  &jst,&jfn);
327  if (jst == -1 || jfn == -1) {
328  cpl_msg_error(fctid,"Unable to continue");
329  vircam_gaincor_tidy();
330  return(-1);
331  }
332 
333  /* Now loop for all the extension... */
334 
335  status = VIR_OK;
336  for (j = jst; j <= jfn; j++) {
337  isfirst = (j == jst);
338  gaincor_fac = (ps.gaincors)[j-1];
339 
340  /* Load up the images */
341 
342  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
343  ps.flatf = vircam_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
344  if (ps.img == NULL || ps.flatf == NULL) {
345  vircam_gaincor_tidy();
346  return(-1);
347  }
348 
349  /* Now do the correction */
350 
351  cpl_msg_info(fctid,"Doing the flat fielding for extension %" CPL_SIZE_FORMAT,
352  (cpl_size)j);
353  (void)vircam_gaincor(ps.imgf,gaincor_fac,&status);
354  if (status != VIR_OK) {
355  vircam_gaincor_tidy();
356  return(-1);
357  }
358 
359  /* Now save the result */
360 
361  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
362  (cpl_size)j);
363  if (vircam_gaincor_save(framelist,parlist) != 0) {
364  vircam_gaincor_tidy();
365  return(-1);
366  }
367 
368  /* Tidy a few things before the next image */
369 
370  freefits(ps.imgf);
371  freefits(ps.flatf);
372  }
373  vircam_gaincor_tidy();
374  return(0);
375 }
376 
377 
378 /*---------------------------------------------------------------------------*/
385 /*---------------------------------------------------------------------------*/
386 
387 static int vircam_gaincor_save(cpl_frameset *framelist,
388  cpl_parameterlist *parlist) {
389  const char *fctid = "vircam_gaincor_save";
390  const char *outfile = "gaincor.fits";
391  const char *recipeid = "vircam_gaincor";
392  cpl_propertylist *plist;
393 
394  /* If we need to make a PHU then do that now based on the first frame
395  in the input frame list */
396 
397  if (isfirst) {
398 
399  /* Create a new product frame object and define some tags */
400 
401  product_frame = cpl_frame_new();
402  cpl_frame_set_filename(product_frame,outfile);
403  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
404  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
405  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
406  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
407 
408  /* Set up the product phu */
409 
410  plist = vircam_fits_get_phu(ps.imgf);
411  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
412  parlist,(char *)recipeid,
413  "?Dictionary?",NULL,0);
414 
415  /* 'Save' the PHU image */
416 
417  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
418  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
419  cpl_msg_error(fctid,"Cannot save product PHU");
420  cpl_frame_delete(product_frame);
421  return(-1);
422  }
423  cpl_frameset_insert(framelist,product_frame);
424  }
425 
426  /* Get the extension property list */
427 
428  plist = vircam_fits_get_ehu(ps.imgf);
429 
430  /* Fiddle with the header now */
431 
432  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
433  (char *)recipeid,"?Dictionary?",NULL);
434 
435  /* Save the image */
436 
437  if (cpl_image_save(vircam_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 
447 /*---------------------------------------------------------------------------*/
451 /*---------------------------------------------------------------------------*/
452 
453 static void vircam_gaincor_init(void) {
454  ps.labels = NULL;
455  ps.flat = NULL;
456  ps.flatf = NULL;
457  ps.img = NULL;
458  ps.imgf = NULL;
459  ps.gaincors = NULL;
460 }
461 
462 
463 /*---------------------------------------------------------------------------*/
467 /*---------------------------------------------------------------------------*/
468 
469 static void vircam_gaincor_tidy(void) {
470  freespace(ps.labels);
471  freefits(ps.imgf);
472  freefits(ps.flatf);
473  freeframe(ps.flat);
474  freeframe(ps.img);
475  freespace(ps.gaincors);
476 }
477 
480 /*
481 
482 $Log: not supported by cvs2svn $
483 Revision 1.7 2012/01/15 17:40:09 jim
484 Minor modifications to take into accout the changes in cpl API for v6
485 
486 Revision 1.6 2009/09/09 09:51:13 jim
487 modified to use new saving routines so that headers are right
488 
489 Revision 1.5 2007/10/19 10:33:20 jim
490 and again
491 
492 Revision 1.4 2007/10/19 10:31:47 jim
493 typo in call to tidy routine
494 
495 Revision 1.3 2007/10/15 12:53:55 jim
496 Modified for compatibility with cpl_4.0
497 
498 Revision 1.2 2007/07/09 13:22:09 jim
499 Modified to use new version of vircam_exten_range
500 
501 Revision 1.1 2007/05/08 21:31:41 jim
502 initial entry
503 
504 
505 
506 */
507 
508 
509 
510 
const char * vircam_get_license(void)
Definition: vircam_utils.c:92
int vircam_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Definition: vircam_utils.c:141
int vircam_gaincor(vir_fits *infile, float gainscl, int *status)
Gain correct input data frame.
cpl_frame * vircam_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Definition: vircam_utils.c:247
int vircam_gaincor_calc(cpl_frame *frame, int *n, float **cors, int *status)
cpl_image * vircam_fits_get_image(vir_fits *p)
Definition: vircam_fits.c:349
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:273
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:203
cpl_propertylist * vircam_fits_get_phu(vir_fits *p)
Definition: vircam_fits.c:416
vir_fits * vircam_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: vircam_fits.c:80
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
Definition: vircam_fits.c:457
void vircam_exten_range(int inexten, const cpl_frame *fr, int *out1, int *out2)
Definition: vircam_utils.c:353
int vircam_dfs_set_groups(cpl_frameset *set)
Definition: vircam_dfs.c:87