VIRCAM Pipeline  1.3.3
tests/vircam_mkconf.c
1 /* $Id: vircam_mkconf.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_mkconf_create(cpl_plugin *) ;
45 static int vircam_mkconf_exec(cpl_plugin *) ;
46 static int vircam_mkconf_destroy(cpl_plugin *) ;
47 static int vircam_mkconf_test(cpl_parameterlist *, cpl_frameset *) ;
48 static int vircam_mkconf_save(cpl_frameset *framelist,
49  cpl_parameterlist *parlist);
50 static void vircam_mkconf_init(void);
51 static void vircam_mkconf_tidy(void);
52 
53 static struct {
54 
55  /* Input */
56 
57  int extenum;
58 
59 } vircam_mkconf_config;
60 
61 static struct {
62  cpl_size *labels;
63  cpl_frame *flat;
64  vir_mask *bpm;
65  vir_fits *flatf;
66  cpl_image *outimg;
67  cpl_propertylist *drs;
68 } ps;
69 
70 static int isfirst;
71 static cpl_frame *product_frame = NULL;
72 
73 static char vircam_mkconf_description[] =
74 "vircam_mkconf -- VIRCAM confidence map generation test recipe.\n\n"
75 "Create a confidence map using an input flat field and an input mask\n"
76 "The program accepts the following files in the SOF:\n\n"
77 " Tag Description\n"
78 " -----------------------------------------------------------------------\n"
79 " %-21s A input master flat field\n"
80 " %-21s A master bad pixel map or\n"
81 " %-21s A master confidence map\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_mkconf_description,
147  VIRCAM_CAL_TWILIGHT_FLAT,VIRCAM_CAL_BPM,VIRCAM_CAL_CONF);
148 
149  cpl_plugin_init(plugin,
150  CPL_PLUGIN_API,
151  VIRCAM_BINARY_VERSION,
152  CPL_PLUGIN_TYPE_RECIPE,
153  "vircam_mkconf",
154  "VIRCAM confidence map test recipe [test]",
155  alldesc,
156  "Jim Lewis",
157  "jrl@ast.cam.ac.uk",
159  vircam_mkconf_create,
160  vircam_mkconf_exec,
161  vircam_mkconf_destroy);
162 
163  cpl_pluginlist_append(list,plugin);
164 
165  return(0);
166 }
167 
168 /*---------------------------------------------------------------------------*/
177 /*---------------------------------------------------------------------------*/
178 
179 static int vircam_mkconf_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  /* Extension number of input frames to use */
195 
196  p = cpl_parameter_new_range("vircam.vircam_mkconf.extenum",
197  CPL_TYPE_INT,
198  "Extension number to be done, 0 == all",
199  "vircam.vircam_mkconf",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_mkconf_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_mkconf_test(recipe->parameters,recipe->frames));
227 }
228 
229 /*---------------------------------------------------------------------------*/
235 /*---------------------------------------------------------------------------*/
236 
237 static int vircam_mkconf_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_mkconf_test(cpl_parameterlist *parlist,
261  cpl_frameset *framelist) {
262  const char *fctid="vircam_mkconf";
263  cpl_parameter *p;
264  int jst,jfn,status,j,nx,ny;
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_mkconf_init();
277 
278  /* Get the parameters */
279 
280  p = cpl_parameterlist_find(parlist,"vircam.vircam_mkconf.extenum");
281  vircam_mkconf_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_mkconf_tidy();
288  return(-1);
289  }
290 
291  /* Get the flat */
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_mkconf_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_mkconf_tidy();
303  return(-1);
304  }
305 
306  /* Get the master mask */
307 
308  ps.bpm = vircam_mask_define(framelist,ps.labels,nlab);
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_mkconf_config.extenum,(const cpl_frame *)ps.flat,
315  &jst,&jfn);
316  if (jst == -1 || jfn == -1) {
317  cpl_msg_error(fctid,"Unable to continue");
318  vircam_mkconf_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.flatf = vircam_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
331  if (ps.flatf == NULL) {
332  vircam_mkconf_tidy();
333  return(-1);
334  }
335 
336  /* Get the size of the flat image */
337 
338  nx = (int)cpl_image_get_size_x(vircam_fits_get_image(ps.flatf));
339  ny = (int)cpl_image_get_size_y(vircam_fits_get_image(ps.flatf));
340 
341  /* Load the data for the bpm */
342 
343  (void)vircam_mask_load(ps.bpm,j,nx,ny);
344 
345  /* Now do the correction */
346 
347  cpl_msg_info(fctid,"Making confidence map for extension %" CPL_SIZE_FORMAT,
348  (cpl_size)j);
349  (void)vircam_mkconf(vircam_fits_get_image(ps.flatf),
350  vircam_fits_get_fullname(ps.flatf),ps.bpm,
351  &(ps.outimg),&(ps.drs),&status);
352  if (status != VIR_OK) {
353  vircam_mkconf_tidy();
354  return(-1);
355  }
356 
357  /* Now save the result */
358 
359  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
360  (cpl_size)j);
361  if (vircam_mkconf_save(framelist,parlist) != 0) {
362  vircam_mkconf_tidy();
363  return(-1);
364  }
365 
366  /* Tidy a few things before the next image */
367 
368  freefits(ps.flatf);
369  vircam_mask_clear(ps.bpm);
370  freeimage(ps.outimg);
371  freepropertylist(ps.drs);
372  }
373  vircam_mkconf_tidy();
374  return(0);
375 }
376 
377 /*---------------------------------------------------------------------------*/
384 /*---------------------------------------------------------------------------*/
385 
386 static int vircam_mkconf_save(cpl_frameset *framelist,
387  cpl_parameterlist *parlist) {
388  const char *recipeid = "vircam_mkconf";
389  const char *fctid = "vircam_mkconf_save";
390  const char *outfile = "mkconf.fits";
391  cpl_propertylist *plist;
392 
393  /* If we need to make a PHU then do that now based on the first frame
394  in the input frame list */
395 
396  if (isfirst) {
397 
398  /* Create a new product frame object and define some tags */
399 
400  product_frame = cpl_frame_new();
401  cpl_frame_set_filename(product_frame,outfile);
402  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_TEST);
403  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
404  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
405  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
406 
407  /* Create product frame phu header */
408 
409  plist = vircam_fits_get_phu(ps.flatf);
410  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
411  parlist,(char *)recipeid,
412  "?Dictionary?",NULL,0);
413 
414  /* 'Save' the PHU image */
415 
416  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
417  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
418  cpl_msg_error(fctid,"Cannot save product PHU");
419  cpl_frame_delete(product_frame);
420  return(-1);
421  }
422  cpl_frameset_insert(framelist,product_frame);
423  }
424 
425  /* Get the extension property list */
426 
427  plist = vircam_fits_get_ehu(ps.flatf);
428 
429  /* Fiddle with the header now */
430 
431  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
432  (char *)recipeid,"?Dictionary?",NULL);
433 
434  /* Save the image */
435 
436  if (cpl_image_save(ps.outimg,outfile,CPL_TYPE_FLOAT,plist,
437  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
438  cpl_msg_error(fctid,"Cannot save product image extension");
439  return(-1);
440  }
441 
442  return(0);
443 }
444 
445 
446 /*---------------------------------------------------------------------------*/
450 /*---------------------------------------------------------------------------*/
451 
452 static void vircam_mkconf_init(void) {
453  ps.labels = NULL;
454  ps.flat = NULL;
455  ps.bpm = NULL;
456  ps.flatf = NULL;
457  ps.outimg = NULL;
458  ps.drs = NULL;
459 }
460 
461 
462 /*---------------------------------------------------------------------------*/
466 /*---------------------------------------------------------------------------*/
467 
468 static void vircam_mkconf_tidy(void) {
469  freespace(ps.labels);
470  freeframe(ps.flat);
471  freemask(ps.bpm);
472  freefits(ps.flatf);
473  freeimage(ps.outimg);
474  freepropertylist(ps.drs);
475 }
476 
479 /*
480 
481 $Log: not supported by cvs2svn $
482 Revision 1.13 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.12 2009/09/09 09:51:13 jim
486 modified to use new saving routines so that headers are right
487 
488 Revision 1.11 2007/10/15 12:53:55 jim
489 Modified for compatibility with cpl_4.0
490 
491 Revision 1.10 2007/07/09 13:22:09 jim
492 Modified to use new version of vircam_exten_range
493 
494 Revision 1.9 2007/04/23 12:49:34 jim
495 Fixed bug where the wrong image was being saved
496 
497 Revision 1.8 2007/04/13 12:27:39 jim
498 Added some extra docs
499 
500 Revision 1.7 2007/04/04 10:36:29 jim
501 Modified to use new dfs tags
502 
503 Revision 1.6 2007/03/01 12:42:59 jim
504 Modified slightly after code checking
505 
506 Revision 1.5 2006/06/15 09:59:00 jim
507 Minor changes to docs
508 
509 Revision 1.4 2006/05/04 11:53:44 jim
510 Fixed _save routine so that it's more consistent with the standard CPL
511 way of doing things
512 
513 Revision 1.3 2006/05/02 11:29:16 jim
514 Fixed problem where propertylist was being deleted incorrectly
515 
516 Revision 1.2 2006/04/27 14:22:06 jim
517 Fixed docs
518 
519 Revision 1.1 2006/04/24 10:42:45 jim
520 New routine
521 
522 
523 */
524 
525 
526 
527