VIRCAM Pipeline  2.3.10
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) 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.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 <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 #include "vircam_pfits.h"
44 
45 /* Function prototypes */
46 
47 static int vircam_lincor_create(cpl_plugin *) ;
48 static int vircam_lincor_exec(cpl_plugin *) ;
49 static int vircam_lincor_destroy(cpl_plugin *) ;
50 static int vircam_lincor_test(cpl_parameterlist *, cpl_frameset *) ;
51 static int vircam_lincor_save(cpl_frameset *framelist,
52  cpl_parameterlist *parlist);
53 static void vircam_lincor_init(void);
54 static void vircam_lincor_tidy(void);
55 
56 static struct {
57 
58  /* Input */
59 
60  int extenum;
61 
62 } vircam_lincor_config;
63 
64 static struct {
65  cpl_size *labels;
66  cpl_frame *chan;
67  cpl_frame *img;
68  casu_tfits *chanf;
69  casu_fits *imgf;
70 } ps;
71 
72 static int isfirst;
73 static cpl_frame *product_frame = NULL;
74 
75 static char vircam_lincor_description[] =
76 "vircam_lincor -- VIRCAM linearity correction test recipe.\n\n"
77 "Linearity correct an input frame using a pre-existing channel table\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 channel table\n"
83 "\n";
84 
131 /* Function code */
132 
133 /*---------------------------------------------------------------------------*/
141 /*---------------------------------------------------------------------------*/
142 
143 int cpl_plugin_get_info(cpl_pluginlist *list) {
144  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
145  cpl_plugin *plugin = &recipe->interface;
146  char alldesc[SZ_ALLDESC];
147  (void)snprintf(alldesc,SZ_ALLDESC,vircam_lincor_description,
148  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CHANTAB);
149 
150  cpl_plugin_init(plugin,
151  CPL_PLUGIN_API,
152  VIRCAM_BINARY_VERSION,
153  CPL_PLUGIN_TYPE_RECIPE,
154  "vircam_lincor",
155  "VIRCAM linearisation test recipe [test]",
156  alldesc,
157  "Jim Lewis",
158  "jrl@ast.cam.ac.uk",
160  vircam_lincor_create,
161  vircam_lincor_exec,
162  vircam_lincor_destroy);
163 
164  cpl_pluginlist_append(list,plugin);
165 
166  return(0);
167 }
168 
169 /*---------------------------------------------------------------------------*/
178 /*---------------------------------------------------------------------------*/
179 
180 static int vircam_lincor_create(cpl_plugin *plugin) {
181  cpl_recipe *recipe;
182  cpl_parameter *p;
183 
184  /* Get the recipe out of the plugin */
185 
186  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
187  recipe = (cpl_recipe *)plugin;
188  else
189  return(-1);
190 
191  /* Create the parameters list in the cpl_recipe object */
192 
193  recipe->parameters = cpl_parameterlist_new();
194 
195  /* Get the extension number of input frames to use */
196 
197  p = cpl_parameter_new_range("vircam.vircam_lincor.extenum",
198  CPL_TYPE_INT,
199  "Extension number to be done, 0 == all",
200  "vircam.vircam_lincor",1,0,16);
201  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
202  cpl_parameterlist_append(recipe->parameters,p);
203 
204  /* Get out of here */
205 
206  return(0);
207 }
208 
209 /*---------------------------------------------------------------------------*/
215 /*---------------------------------------------------------------------------*/
216 
217 static int vircam_lincor_exec(cpl_plugin *plugin) {
218  cpl_recipe *recipe;
219 
220  /* Get the recipe out of the plugin */
221 
222  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
223  recipe = (cpl_recipe *)plugin;
224  else
225  return(-1);
226 
227  return(vircam_lincor_test(recipe->parameters,recipe->frames));
228 }
229 
230 
231 /*---------------------------------------------------------------------------*/
237 /*---------------------------------------------------------------------------*/
238 
239 static int vircam_lincor_destroy(cpl_plugin *plugin) {
240  cpl_recipe *recipe ;
241 
242  /* Get the recipe out of the plugin */
243 
244  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
245  recipe = (cpl_recipe *)plugin;
246  else
247  return(-1);
248 
249  cpl_parameterlist_delete(recipe->parameters);
250  return(0);
251 }
252 
253 /*---------------------------------------------------------------------------*/
260 /*---------------------------------------------------------------------------*/
261 
262 static int vircam_lincor_test(cpl_parameterlist *parlist,
263  cpl_frameset *framelist) {
264  const char *fctid="vircam_lincor";
265  cpl_parameter *p;
266  int jst,jfn,status,j,ndit;
267  cpl_size nlab;
268  cpl_propertylist *pp;
269 
270  /* Check validity of input frameset */
271 
272  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
273  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
274  return(-1);
275  }
276 
277  /* Initialise some things */
278 
279  vircam_lincor_init();
280 
281  /* Get parameters */
282 
283  p = cpl_parameterlist_find(parlist,"vircam.vircam_lincor.extenum");
284  vircam_lincor_config.extenum = cpl_parameter_get_int(p);
285 
286  /* Sort out raw from calib frames */
287 
288  if (vircam_dfs_set_groups(framelist) != CASU_OK) {
289  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
290  vircam_lincor_tidy();
291  return(-1);
292  }
293 
294  /* Get the frames */
295 
296  if ((ps.labels = cpl_frameset_labelise(framelist,casu_compare_tags,
297  &nlab)) == NULL) {
298  cpl_msg_error(fctid,"Cannot labelise the input frames");
299  vircam_lincor_tidy();
300  return(-1);
301  }
302  if ((ps.chan = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
303  VIRCAM_CAL_CHANTAB)) == NULL) {
304  cpl_msg_info(fctid,"No channel table found -- cannot continue");
305  vircam_lincor_tidy();
306  return(-1);
307  }
308  if ((ps.img = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
309  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
310  cpl_msg_info(fctid,"No raw image found -- cannot continue");
311  vircam_lincor_tidy();
312  return(-1);
313  }
314 
315  /* Get the number of DITs */
316 
317  pp = cpl_propertylist_load(cpl_frame_get_filename(ps.img),0);
318  if (vircam_pfits_get_ndit(pp,&ndit) != CASU_OK) {
319  cpl_msg_error(fctid,"No value for NDIT available");
320  freepropertylist(pp);
321  vircam_lincor_tidy();
322  return(-1);
323  }
324  cpl_propertylist_delete(pp);
325 
326  /* Now, how many image extensions do we want to do? If the extension
327  number is zero, then we loop for all possible extensions. If it
328  isn't then we just do the extension specified */
329 
330  vircam_exten_range(vircam_lincor_config.extenum,(const cpl_frame *)ps.img,
331  &jst,&jfn);
332  if (jst == -1 || jfn == -1) {
333  cpl_msg_error(fctid,"Unable to continue");
334  vircam_lincor_tidy();
335  return(-1);
336  }
337 
338  /* Now loop for all the extension... */
339 
340  status = CASU_OK;
341  for (j = jst; j <= jfn; j++) {
342  isfirst = (j == jst);
343 
344  /* Load up the images */
345 
346  ps.imgf = casu_fits_load(ps.img,CPL_TYPE_FLOAT,j);
347  ps.chanf = casu_tfits_load(ps.chan,j);
348  if (ps.img == NULL || ps.chanf == NULL) {
349  vircam_lincor_tidy();
350  return(-1);
351  }
352 
353  /* Now do the correction */
354 
355  cpl_msg_info(fctid,"Doing the linearisation for extension %" CPL_SIZE_FORMAT,
356  (cpl_size)j);
357  (void)vircam_lincor(ps.imgf,ps.chanf,1,ndit,&status);
358  if (status != CASU_OK) {
359  vircam_lincor_tidy();
360  return(-1);
361  }
362 
363  /* Now save the result */
364 
365  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
366  (cpl_size)j);
367  if (vircam_lincor_save(framelist,parlist) != 0) {
368  vircam_lincor_tidy();
369  return(-1);
370  }
371 
372  /* Tidy a few things before the next image */
373 
374  freefits(ps.imgf);
375  freetfits(ps.chanf);
376  }
377  vircam_lincor_tidy();
378  return(0);
379 }
380 
381 
382 /*---------------------------------------------------------------------------*/
389 /*---------------------------------------------------------------------------*/
390 
391 static int vircam_lincor_save(cpl_frameset *framelist,
392  cpl_parameterlist *parlist) {
393  const char *recipeid = "vircam_lincor";
394  const char *fctid = "vircam_lincor_save";
395  const char *outfile = "lincor.fits";
396  cpl_propertylist *plist;
397 
398  /* If we need to make a PHU then do that now based on the first frame
399  in the input frame list */
400 
401  if (isfirst) {
402 
403  /* Create a new product frame object and define some tags */
404 
405  product_frame = cpl_frame_new();
406  cpl_frame_set_filename(product_frame,outfile);
407  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
408  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
409  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
410  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
411 
412  /* Set up phu header */
413 
414  plist = casu_fits_get_phu(ps.imgf);
415  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
416  parlist,(char *)recipeid,
417  "?Dictionary?",NULL,0);
418 
419  /* 'Save' the PHU image */
420 
421  if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
422  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
423  cpl_msg_error(fctid,"Cannot save product PHU");
424  cpl_frame_delete(product_frame);
425  return(-1);
426  }
427  cpl_frameset_insert(framelist,product_frame);
428  }
429 
430  /* Get the extension property list */
431 
432  plist = casu_fits_get_ehu(ps.imgf);
433 
434  /* Fiddle with the header now */
435 
436  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
437  (char *)recipeid,"?Dictionary?",NULL);
438 
439  /* Save the image */
440 
441  if (cpl_image_save(casu_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
442  plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
443  cpl_msg_error(fctid,"Cannot save product image extension");
444  return(-1);
445  }
446 
447  return(0);
448 }
449 
450 
451 /*---------------------------------------------------------------------------*/
455 /*---------------------------------------------------------------------------*/
456 
457 static void vircam_lincor_init(void) {
458  ps.labels = NULL;
459  ps.chan = NULL;
460  ps.chanf = NULL;
461  ps.img = NULL;
462  ps.imgf = NULL;
463 }
464 
465 
466 /*---------------------------------------------------------------------------*/
470 /*---------------------------------------------------------------------------*/
471 
472 static void vircam_lincor_tidy(void) {
473  freespace(ps.labels);
474  freefits(ps.imgf);
475  freetfits(ps.chanf);
476  freeframe(ps.chan);
477  freeframe(ps.img);
478 }
479 
482 /*
483 
484 $Log: not supported by cvs2svn $
485 Revision 1.14 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.13 2010/03/12 10:44:07 lbilbao
489 Added missing header inclusion.
490 
491 Revision 1.12 2009/09/09 09:51:13 jim
492 modified to use new saving routines so that headers are right
493 
494 Revision 1.11 2007/11/26 09:56:04 jim
495 Fixed call to vircam_lincor to include ndit
496 
497 Revision 1.10 2007/10/15 12:53:55 jim
498 Modified for compatibility with cpl_4.0
499 
500 Revision 1.9 2007/07/09 13:22:09 jim
501 Modified to use new version of vircam_exten_range
502 
503 Revision 1.8 2007/04/13 12:27:39 jim
504 Added some extra docs
505 
506 Revision 1.7 2007/04/04 10:36:29 jim
507 Modified to use new dfs tags
508 
509 Revision 1.6 2007/03/01 12:42:59 jim
510 Modified slightly after code checking
511 
512 Revision 1.5 2006/06/15 09:58:59 jim
513 Minor changes to docs
514 
515 Revision 1.4 2006/05/04 11:53:43 jim
516 Fixed _save routine so that it's more consistent with the standard CPL
517 way of doing things
518 
519 Revision 1.3 2006/05/02 11:29:15 jim
520 Fixed problem where propertylist was being deleted incorrectly
521 
522 Revision 1.2 2006/04/27 14:22:05 jim
523 Fixed docs
524 
525 Revision 1.1 2006/04/24 10:42:45 jim
526 New routine
527 
528 
529 */
530 
531 
532 
533 
534 
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
casu_tfits * casu_tfits_load(cpl_frame *table, int nexten)
Definition: casu_tfits.c:78
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 vircam_lincor(casu_fits *infile, casu_tfits *lchantab, int kconst, int ndit, int *status)
Apply linearity curves to data.
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
int vircam_pfits_get_ndit(const cpl_propertylist *plist, int *ndit)
Get the value of NDIT.
Definition: vircam_pfits.c:583
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