VIRCAM Pipeline  1.3.4
vircam_lincor.c
1 /* $Id: vircam_lincor.c,v 1.15 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.15 $
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 #include "vircam_pfits.h"
42 
43 /* Function prototypes */
44 
45 static int vircam_lincor_create(cpl_plugin *) ;
46 static int vircam_lincor_exec(cpl_plugin *) ;
47 static int vircam_lincor_destroy(cpl_plugin *) ;
48 static int vircam_lincor_test(cpl_parameterlist *, cpl_frameset *) ;
49 static int vircam_lincor_save(cpl_frameset *framelist,
50  cpl_parameterlist *parlist);
51 static void vircam_lincor_init(void);
52 static void vircam_lincor_tidy(void);
53 
54 static struct {
55 
56  /* Input */
57 
58  int extenum;
59 
60 } vircam_lincor_config;
61 
62 static struct {
63  cpl_size *labels;
64  cpl_frame *chan;
65  cpl_frame *img;
66  vir_tfits *chanf;
67  vir_fits *imgf;
68 } ps;
69 
70 static int isfirst;
71 static cpl_frame *product_frame = NULL;
72 
73 static char vircam_lincor_description[] =
74 "vircam_lincor -- VIRCAM linearity correction test recipe.\n\n"
75 "Linearity correct an input frame using a pre-existing channel table\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 channel table\n"
81 "\n";
82 
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_lincor_description,
146  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CHANTAB);
147 
148  cpl_plugin_init(plugin,
149  CPL_PLUGIN_API,
150  VIRCAM_BINARY_VERSION,
151  CPL_PLUGIN_TYPE_RECIPE,
152  "vircam_lincor",
153  "VIRCAM linearisation test recipe [test]",
154  alldesc,
155  "Jim Lewis",
156  "jrl@ast.cam.ac.uk",
158  vircam_lincor_create,
159  vircam_lincor_exec,
160  vircam_lincor_destroy);
161 
162  cpl_pluginlist_append(list,plugin);
163 
164  return(0);
165 }
166 
167 /*---------------------------------------------------------------------------*/
176 /*---------------------------------------------------------------------------*/
177 
178 static int vircam_lincor_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_lincor.extenum",
196  CPL_TYPE_INT,
197  "Extension number to be done, 0 == all",
198  "vircam.vircam_lincor",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_lincor_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_lincor_test(recipe->parameters,recipe->frames));
226 }
227 
228 
229 /*---------------------------------------------------------------------------*/
235 /*---------------------------------------------------------------------------*/
236 
237 static int vircam_lincor_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_lincor_test(cpl_parameterlist *parlist,
261  cpl_frameset *framelist) {
262  const char *fctid="vircam_lincor";
263  cpl_parameter *p;
264  int jst,jfn,status,j,ndit;
265  cpl_size nlab;
266  cpl_propertylist *pp;
267 
268  /* Check validity of input frameset */
269 
270  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
271  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
272  return(-1);
273  }
274 
275  /* Initialise some things */
276 
277  vircam_lincor_init();
278 
279  /* Get parameters */
280 
281  p = cpl_parameterlist_find(parlist,"vircam.vircam_lincor.extenum");
282  vircam_lincor_config.extenum = cpl_parameter_get_int(p);
283 
284  /* Sort out raw from calib frames */
285 
286  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
287  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
288  vircam_lincor_tidy();
289  return(-1);
290  }
291 
292  /* Get the frames */
293 
294  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
295  &nlab)) == NULL) {
296  cpl_msg_error(fctid,"Cannot labelise the input frames");
297  vircam_lincor_tidy();
298  return(-1);
299  }
300  if ((ps.chan = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
301  VIRCAM_CAL_CHANTAB)) == NULL) {
302  cpl_msg_info(fctid,"No channel table found -- cannot continue");
303  vircam_lincor_tidy();
304  return(-1);
305  }
306  if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
307  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
308  cpl_msg_info(fctid,"No raw image found -- cannot continue");
309  vircam_lincor_tidy();
310  return(-1);
311  }
312 
313  /* Get the number of DITs */
314 
315  pp = cpl_propertylist_load(cpl_frame_get_filename(ps.img),0);
316  if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
317  cpl_msg_error(fctid,"No value for NDIT available");
318  freepropertylist(pp);
319  vircam_lincor_tidy();
320  return(-1);
321  }
322  cpl_propertylist_delete(pp);
323 
324  /* Now, how many image extensions do we want to do? If the extension
325  number is zero, then we loop for all possible extensions. If it
326  isn't then we just do the extension specified */
327 
328  vircam_exten_range(vircam_lincor_config.extenum,(const cpl_frame *)ps.img,
329  &jst,&jfn);
330  if (jst == -1 || jfn == -1) {
331  cpl_msg_error(fctid,"Unable to continue");
332  vircam_lincor_tidy();
333  return(-1);
334  }
335 
336  /* Now loop for all the extension... */
337 
338  status = VIR_OK;
339  for (j = jst; j <= jfn; j++) {
340  isfirst = (j == jst);
341 
342  /* Load up the images */
343 
344  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
345  ps.chanf = vircam_tfits_load(ps.chan,j);
346  if (ps.img == NULL || ps.chanf == NULL) {
347  vircam_lincor_tidy();
348  return(-1);
349  }
350 
351  /* Now do the correction */
352 
353  cpl_msg_info(fctid,"Doing the linearisation for extension %" CPL_SIZE_FORMAT,
354  (cpl_size)j);
355  (void)vircam_lincor(ps.imgf,ps.chanf,1,ndit,&status);
356  if (status != VIR_OK) {
357  vircam_lincor_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_lincor_save(framelist,parlist) != 0) {
366  vircam_lincor_tidy();
367  return(-1);
368  }
369 
370  /* Tidy a few things before the next image */
371 
372  freefits(ps.imgf);
373  freetfits(ps.chanf);
374  }
375  vircam_lincor_tidy();
376  return(0);
377 }
378 
379 
380 /*---------------------------------------------------------------------------*/
387 /*---------------------------------------------------------------------------*/
388 
389 static int vircam_lincor_save(cpl_frameset *framelist,
390  cpl_parameterlist *parlist) {
391  const char *recipeid = "vircam_lincor";
392  const char *fctid = "vircam_lincor_save";
393  const char *outfile = "lincor.fits";
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 phu header */
411 
412  plist = vircam_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 = vircam_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(vircam_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_lincor_init(void) {
456  ps.labels = NULL;
457  ps.chan = NULL;
458  ps.chanf = NULL;
459  ps.img = NULL;
460  ps.imgf = NULL;
461 }
462 
463 
464 /*---------------------------------------------------------------------------*/
468 /*---------------------------------------------------------------------------*/
469 
470 static void vircam_lincor_tidy(void) {
471  freespace(ps.labels);
472  freefits(ps.imgf);
473  freetfits(ps.chanf);
474  freeframe(ps.chan);
475  freeframe(ps.img);
476 }
477 
480 /*
481 
482 $Log: not supported by cvs2svn $
483 Revision 1.14 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.13 2010/03/12 10:44:07 lbilbao
487 Added missing header inclusion.
488 
489 Revision 1.12 2009/09/09 09:51:13 jim
490 modified to use new saving routines so that headers are right
491 
492 Revision 1.11 2007/11/26 09:56:04 jim
493 Fixed call to vircam_lincor to include ndit
494 
495 Revision 1.10 2007/10/15 12:53:55 jim
496 Modified for compatibility with cpl_4.0
497 
498 Revision 1.9 2007/07/09 13:22:09 jim
499 Modified to use new version of vircam_exten_range
500 
501 Revision 1.8 2007/04/13 12:27:39 jim
502 Added some extra docs
503 
504 Revision 1.7 2007/04/04 10:36:29 jim
505 Modified to use new dfs tags
506 
507 Revision 1.6 2007/03/01 12:42:59 jim
508 Modified slightly after code checking
509 
510 Revision 1.5 2006/06/15 09:58:59 jim
511 Minor changes to docs
512 
513 Revision 1.4 2006/05/04 11:53:43 jim
514 Fixed _save routine so that it's more consistent with the standard CPL
515 way of doing things
516 
517 Revision 1.3 2006/05/02 11:29:15 jim
518 Fixed problem where propertylist was being deleted incorrectly
519 
520 Revision 1.2 2006/04/27 14:22:05 jim
521 Fixed docs
522 
523 Revision 1.1 2006/04/24 10:42:45 jim
524 New routine
525 
526 
527 */
528 
529 
530 
531 
532 
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
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_lincor(vir_fits *infile, vir_tfits *lchantab, int kconst, int ndit, int *status)
Apply linearity curves to data.
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
int vircam_pfits_get_ndit(const cpl_propertylist *plist, int *ndit)
Get the value of NDIT.
Definition: vircam_pfits.c:583
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
vir_tfits * vircam_tfits_load(cpl_frame *table, int nexten)
Definition: vircam_tfits.c:78
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