VIRCAM Pipeline  1.3.3
tests/vircam_darkcor.c
1 /* $Id: vircam_darkcor.c,v 1.13 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.13 $
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_darkcor_create(cpl_plugin *) ;
45 static int vircam_darkcor_exec(cpl_plugin *) ;
46 static int vircam_darkcor_destroy(cpl_plugin *) ;
47 static int vircam_darkcor_test(cpl_parameterlist *, cpl_frameset *) ;
48 static int vircam_darkcor_save(cpl_frameset *framelist,
49  cpl_parameterlist *parlist);
50 static void vircam_darkcor_init(void);
51 static void vircam_darkcor_tidy(void);
52 
53 static struct {
54 
55  /* Input */
56 
57  float darkscl;
58  int extenum;
59 
60 } vircam_darkcor_config;
61 
62 static struct {
63  cpl_size *labels;
64  cpl_frame *dark;
65  cpl_frame *img;
66  vir_fits *darkf;
67  vir_fits *imgf;
68 } ps;
69 
70 static int isfirst;
71 static cpl_frame *product_frame = NULL;
72 
73 
74 static char vircam_darkcor_description[] =
75 "vircam_darkcor -- VIRCAM dark correction test recipe.\n\n"
76 "Dark correct an input frame using a master dark 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 dark frame\n"
82 "\n";
83 
129 /* Function code */
130 
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_darkcor_description,
147  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_DARK);
148 
149  cpl_plugin_init(plugin,
150  CPL_PLUGIN_API,
151  VIRCAM_BINARY_VERSION,
152  CPL_PLUGIN_TYPE_RECIPE,
153  "vircam_darkcor",
154  "VIRCAM dark correction test recipe [test]",
155  alldesc,
156  "Jim Lewis",
157  "jrl@ast.cam.ac.uk",
159  vircam_darkcor_create,
160  vircam_darkcor_exec,
161  vircam_darkcor_destroy);
162 
163  cpl_pluginlist_append(list,plugin);
164 
165  return(0);
166 }
167 
168 /*---------------------------------------------------------------------------*/
177 /*---------------------------------------------------------------------------*/
178 
179 static int vircam_darkcor_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  /* Fill in the parameters. First the dark scaling factor */
195 
196  p = cpl_parameter_new_value("vircam.vircam_darkcor.darkscl",
197  CPL_TYPE_DOUBLE,
198  "Dark correction scale factor",
199  "vircam.vircam_darkcor",1.0);
200  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"darkscl");
201  cpl_parameterlist_append(recipe->parameters,p);
202 
203  /* Extension number of input frames to use */
204 
205  p = cpl_parameter_new_range("vircam.vircam_darkcor.extenum",
206  CPL_TYPE_INT,
207  "Extension number to be done, 0 == all",
208  "vircam.vircam_darkcor",1,0,16);
209  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
210  cpl_parameterlist_append(recipe->parameters,p);
211 
212  /* Get out of here */
213 
214  return(0);
215 }
216 
217 /*---------------------------------------------------------------------------*/
223 /*---------------------------------------------------------------------------*/
224 
225 static int vircam_darkcor_exec(cpl_plugin *plugin) {
226  cpl_recipe *recipe;
227 
228  /* Get the recipe out of the plugin */
229 
230  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
231  recipe = (cpl_recipe *)plugin;
232  else
233  return(-1);
234 
235  return(vircam_darkcor_test(recipe->parameters,recipe->frames));
236 }
237 
238 /*---------------------------------------------------------------------------*/
244 /*---------------------------------------------------------------------------*/
245 
246 static int vircam_darkcor_destroy(cpl_plugin *plugin) {
247  cpl_recipe *recipe ;
248 
249  /* Get the recipe out of the plugin */
250 
251  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
252  recipe = (cpl_recipe *)plugin;
253  else
254  return(-1);
255 
256  cpl_parameterlist_delete(recipe->parameters);
257  return(0);
258 }
259 
260 /*---------------------------------------------------------------------------*/
267 /*---------------------------------------------------------------------------*/
268 
269 static int vircam_darkcor_test(cpl_parameterlist *parlist,
270  cpl_frameset *framelist) {
271  const char *fctid="vircam_darkcor";
272  cpl_parameter *p;
273  int jst,jfn,status,j;
274  cpl_size nlab;
275 
276  /* Check validity of input frameset */
277 
278  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
279  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
280  return(-1);
281  }
282 
283  /* Initialise some things */
284 
285  vircam_darkcor_init();
286 
287  /* Get the parameters */
288 
289  p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.darkscl");
290  vircam_darkcor_config.darkscl = cpl_parameter_get_double(p);
291  p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.extenum");
292  vircam_darkcor_config.extenum = cpl_parameter_get_int(p);
293 
294  /* Sort out raw from calib frames */
295 
296  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
297  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
298  vircam_darkcor_tidy();
299  return(-1);
300  }
301 
302  /* Get the frames */
303 
304  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
305  &nlab)) == NULL) {
306  cpl_msg_error(fctid,"Cannot labelise the input frames");
307  vircam_darkcor_tidy();
308  return(-1);
309  }
310  if ((ps.dark = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
311  VIRCAM_CAL_DARK)) == NULL) {
312  cpl_msg_info(fctid,"No master dark found -- cannot continue");
313  vircam_darkcor_tidy();
314  return(-1);
315  }
316  if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
317  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
318  cpl_msg_info(fctid,"No raw image found -- cannot continue");
319  vircam_darkcor_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_darkcor_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_darkcor_tidy();
332  return(-1);
333  }
334 
335  /* Now loop for all the extension... */
336 
337  status = VIR_OK;
338  for (j = jst; j <= jfn; j++) {
339  isfirst = (j == jst);
340 
341  /* Load up the images */
342 
343  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
344  ps.darkf = vircam_fits_load(ps.dark,CPL_TYPE_FLOAT,j);
345  if (ps.img == NULL || ps.darkf == NULL) {
346  vircam_darkcor_tidy();
347  return(-1);
348  }
349 
350  /* Now do the correction */
351 
352  cpl_msg_info(fctid,"Doing the dark correction for extension %" CPL_SIZE_FORMAT,
353  (cpl_size)j);
354  (void)vircam_darkcor(ps.imgf,ps.darkf,vircam_darkcor_config.darkscl,
355  &status);
356  if (status != VIR_OK) {
357  vircam_darkcor_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_darkcor_save(framelist,parlist) != 0) {
366  vircam_darkcor_tidy();
367  return(-1);
368  }
369 
370  /* Tidy a few things before the next image */
371 
372  freefits(ps.imgf);
373  freefits(ps.darkf);
374  }
375  vircam_darkcor_tidy();
376  return(0);
377 }
378 
379 /*---------------------------------------------------------------------------*/
386 /*---------------------------------------------------------------------------*/
387 
388 static int vircam_darkcor_save(cpl_frameset *framelist,
389  cpl_parameterlist *parlist) {
390  const char *fctid = "vircam_darkcor_save";
391  const char *outfile = "darkcor.fits";
392  const char *recipeid = "vircam_darkcor";
393  cpl_propertylist *plist;
394 
395  /* If we need to make a PHU then do that now based on the first frame
396  in the input frame list */
397 
398  if (isfirst) {
399 
400  /* Create a new product frame object and define some tags */
401 
402  product_frame = cpl_frame_new();
403  cpl_frame_set_filename(product_frame,outfile);
404  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
405  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
406  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
407  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
408 
409  /* Set up the header */
410 
411  plist = vircam_fits_get_phu(ps.imgf);
412  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
413  parlist,(char *)recipeid,
414  "?Dictionary?",NULL,0);
415 
416  /* 'Save' the PHU image */
417 
418  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
419  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
420  cpl_msg_error(fctid,"Cannot save product PHU");
421  cpl_frame_delete(product_frame);
422  return(-1);
423  }
424  cpl_frameset_insert(framelist,product_frame);
425  }
426 
427  /* Get the extension property list */
428 
429  plist = vircam_fits_get_ehu(ps.imgf);
430 
431  /* Fiddle with the header now */
432 
433  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
434  (char *)recipeid,"?Dictionary?",NULL);
435 
436  /* Save the image */
437 
438  if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
439  plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
440  cpl_msg_error(fctid,"Cannot save product image extension");
441  return(-1);
442  }
443 
444  return(0);
445 }
446 
447 
448 /*---------------------------------------------------------------------------*/
452 /*---------------------------------------------------------------------------*/
453 
454 static void vircam_darkcor_init(void) {
455  ps.labels = NULL;
456  ps.dark = NULL;
457  ps.darkf = NULL;
458  ps.img = NULL;
459  ps.imgf = NULL;
460 }
461 
462 
463 /*---------------------------------------------------------------------------*/
467 /*---------------------------------------------------------------------------*/
468 
469 static void vircam_darkcor_tidy(void) {
470  freespace(ps.labels);
471  freefits(ps.imgf);
472  freefits(ps.darkf);
473  freeframe(ps.dark);
474  freeframe(ps.img);
475 }
476 
479 /*
480 
481 $Log: not supported by cvs2svn $
482 Revision 1.12 2012/01/15 17:40:09 jim
483 Minor modifications to take into accout the changes in cpl API for v6
484 
485 Revision 1.11 2009/09/09 09:51:13 jim
486 modified to use new saving routines so that headers are right
487 
488 Revision 1.10 2007/10/15 12:53:55 jim
489 Modified for compatibility with cpl_4.0
490 
491 Revision 1.9 2007/07/09 13:22:08 jim
492 Modified to use new version of vircam_exten_range
493 
494 Revision 1.8 2007/04/13 12:27:38 jim
495 Added some extra docs
496 
497 Revision 1.7 2007/04/04 10:36:29 jim
498 Modified to use new dfs tags
499 
500 Revision 1.6 2007/03/01 12:42:58 jim
501 Modified slightly after code checking
502 
503 Revision 1.5 2006/06/15 09:58:59 jim
504 Minor changes to docs
505 
506 Revision 1.4 2006/05/04 11:53:40 jim
507 Fixed _save routine so that it's more consistent with the standard CPL
508 way of doing things
509 
510 Revision 1.3 2006/05/02 11:29:11 jim
511 Fixed problem where propertylist was being deleted incorrectly
512 
513 Revision 1.2 2006/04/27 14:22:04 jim
514 Fixed docs
515 
516 Revision 1.1 2006/04/24 10:42:44 jim
517 New routine
518 
519 
520 */
521 
522 
523 
524