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