SINFONI Pipeline Reference Manual  2.6.0
sinfo_utl_ima_line_corr.c
1 /* $Id: sinfo_utl_ima_line_corr.c,v 1.3 2009-06-05 08:18:55 amodigli Exp $
2  *
3  * This file is part of the SINFONI Pipeline
4  * Copyright (C) 2002,2003 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: amodigli $
23  * $Date: 2009-06-05 08:18:55 $
24  * $Revision: 1.3 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  ----------------------------------------------------------------------------*/
35 #include <string.h>
36 
37 /* cpl */
38 #include <cpl.h>
39 
40 /* irplib */
41 #include <irplib_utils.h>
42 
43 #include <sinfo_tpl_utils.h>
44 #include <sinfo_pfits.h>
45 #include <sinfo_tpl_dfs.h>
46 #include <sinfo_key_names.h>
47 #include <sinfo_pro_types.h>
48 #include <sinfo_functions.h>
49 #include <sinfo_image_ops.h>
50 #include <sinfo_msg.h>
51 #include <sinfo_error.h>
52 #include <sinfo_utils_wrappers.h>
53 
54 /*-----------------------------------------------------------------------------
55  Functions prototypes
56  ----------------------------------------------------------------------------*/
57 
58 static int sinfo_utl_ima_line_corr_create(cpl_plugin *) ;
59 static int sinfo_utl_ima_line_corr_exec(cpl_plugin *) ;
60 static int sinfo_utl_ima_line_corr_destroy(cpl_plugin *) ;
61 static int sinfo_utl_ima_line_corr(cpl_parameterlist *, cpl_frameset *) ;
62 
63 /*-----------------------------------------------------------------------------
64  Static variables
65  ----------------------------------------------------------------------------*/
66 
67 static char sinfo_utl_ima_line_corr_description[] =
68  "This recipe performs image computation.\n"
69  "The input files are images\n"
70  "their associated tags should be IMA.\n"
71  "The output are the images cleaned by the defect introduced by SINFONI sw\n"
72  "\n";
73 
74 /*-----------------------------------------------------------------------------
75  Functions code
76  ----------------------------------------------------------------------------*/
77 /*---------------------------------------------------------------------------*/
81 /*---------------------------------------------------------------------------*/
82 
84 /*---------------------------------------------------------------------------*/
92 /*---------------------------------------------------------------------------*/
93 int cpl_plugin_get_info(cpl_pluginlist * list)
94 {
95  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
96  cpl_plugin * plugin = &recipe->interface ;
97 
98  cpl_plugin_init(plugin,
99  CPL_PLUGIN_API,
100  SINFONI_BINARY_VERSION,
101  CPL_PLUGIN_TYPE_RECIPE,
102  "sinfo_utl_ima_line_corr",
103  "Computes result of ima1 op ima2",
104  sinfo_utl_ima_line_corr_description,
105  "Andrea Modigliani",
106  "Andrea.Modigliani@eso.org",
107  sinfo_get_license(),
108  sinfo_utl_ima_line_corr_create,
109  sinfo_utl_ima_line_corr_exec,
110  sinfo_utl_ima_line_corr_destroy) ;
111 
112  cpl_pluginlist_append(list, plugin) ;
113 
114  return 0;
115 }
116 
117 /*---------------------------------------------------------------------------*/
126 /*---------------------------------------------------------------------------*/
127 static int sinfo_utl_ima_line_corr_create(cpl_plugin * plugin)
128 {
129  cpl_recipe * recipe ;
130  cpl_parameter * p ;
131 
132  /* Get the recipe out of the plugin */
133  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
134  recipe = (cpl_recipe *)plugin ;
135  else return -1 ;
136  cpl_error_reset();
137  irplib_reset();
138 
139  /* Create the parameters list in the cpl_recipe object */
140  recipe->parameters = cpl_parameterlist_new() ;
141 
142  /* Fill the parameters list */
143  p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_line_corr.kappa",
144  CPL_TYPE_INT,
145  "Kappa sigma value",
146  "sinfoni.sinfo_utl_ima_line_corr",18);
147  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "kappa") ;
148  cpl_parameterlist_append(recipe->parameters, p) ;
149 
150  p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_line_corr.filt_rad",
151  CPL_TYPE_INT,
152  "Filtering radii applied during median filter."
153  " Should be small",
154  "sinfoni.sinfo_utl_ima_line_corr",3) ;;
155  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "filt_rad") ;
156  cpl_parameterlist_append(recipe->parameters, p) ;
157 
158  /* Return */
159  return 0;
160 }
161 
162 /*---------------------------------------------------------------------------*/
168 /*---------------------------------------------------------------------------*/
169 static int sinfo_utl_ima_line_corr_exec(cpl_plugin * plugin)
170 {
171  cpl_recipe * recipe ;
172  int code=0;
173  cpl_errorstate initial_errorstate = cpl_errorstate_get();
174 
175  /* Get the recipe out of the plugin */
176  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
177  recipe = (cpl_recipe *)plugin ;
178  else return -1 ;
179  cpl_error_reset();
180  irplib_reset();
181  code = sinfo_utl_ima_line_corr(recipe->parameters, recipe->frames) ;
182 
183 
184  if (!cpl_errorstate_is_equal(initial_errorstate)) {
185  /* Dump the error history since recipe execution start.
186  At this point the recipe cannot recover from the error */
187  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
188  }
189 
190  return code ;
191 }
192 
193 /*---------------------------------------------------------------------------*/
199 /*---------------------------------------------------------------------------*/
200 static int sinfo_utl_ima_line_corr_destroy(cpl_plugin * plugin)
201 {
202  cpl_recipe * recipe ;
203 
204  /* Get the recipe out of the plugin */
205  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
206  recipe = (cpl_recipe *)plugin ;
207  else return -1 ;
208 
209  cpl_parameterlist_delete(recipe->parameters) ;
210  return 0 ;
211 }
212 
213 /*---------------------------------------------------------------------------*/
220 /*---------------------------------------------------------------------------*/
221 static int
222 sinfo_utl_ima_line_corr( cpl_parameterlist * parlist,
223  cpl_frameset * framelist)
224 {
225  cpl_parameter * p= NULL ;
226  int kappa=18;
227  int filt_rad=3;
228  int width=4;
229 
230  cpl_image * ima=NULL ;
231  cpl_image * ima_out=NULL ;
232  cpl_propertylist * plist=NULL ;
233  cpl_frame * product_frame=NULL;
234  cpl_frameset * raw_set=NULL;
235  int i=0;
236  cpl_frame * frm=NULL;
237  char name_o[MAX_NAME_SIZE];
238  const char* name=NULL;
239 
240  int n=0;
241  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
242  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
243 
244  /* HOW TO RETRIEVE INPUT PARAMETERS */
245  /* --stropt */
246  check_nomsg(p=cpl_parameterlist_find(parlist,
247  "sinfoni.sinfo_utl_ima_line_corr.kappa"));
248  check_nomsg(kappa=cpl_parameter_get_int(p));
249 
250  /* --boolopt */
251  check_nomsg(p=cpl_parameterlist_find(parlist,
252  "sinfoni.sinfo_utl_ima_line_corr.filt_rad"));
253  check_nomsg(filt_rad = cpl_parameter_get_int(p)) ;
254 
255  /* Identify the RAW and CALIB frames in the input frameset */
256  check(sinfo_dfs_set_groups(framelist),
257  "Cannot identify RAW and CALIB frames") ;
258 
259  /* HOW TO ACCESS INPUT DATA */
260  n=cpl_frameset_get_size(framelist);
261  if(n<1) {
262  sinfo_msg_error("Empty input frame list!");
263  goto cleanup ;
264  }
265  raw_set=cpl_frameset_new();
266  sinfo_extract_frames_group_type(framelist,&raw_set,CPL_FRAME_GROUP_RAW);
267  n=cpl_frameset_get_size(raw_set);
268  if(n<1) {
269  sinfo_msg_error("No raw data found in frame list!");
270  goto cleanup ;
271  }
272 
273 
274  for(i=0;i<n;i++) {
275  check_nomsg(frm=cpl_frameset_get_frame(raw_set,0));
276  check_nomsg(name=cpl_frame_get_filename(frm));
277  check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
278 
279  check_nomsg(sinfo_image_line_corr(width,filt_rad,kappa,ima,&ima_out));
280 
281  sprintf(name_o,"%s%d%s","ima_cor",i,".fits") ;
282 
283  /* Create product frame */
284  check_nomsg(product_frame = cpl_frame_new());
285  check_nomsg(cpl_frame_set_filename(product_frame, name_o));
286  check_nomsg(cpl_frame_set_tag(product_frame, "IMA_COR"));
287  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE));
288  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT));
289  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
290  "Error while initialising the product frame") ;
291 
292  /* Add DataFlow keywords */
293  /*
294  check(cpl_dfs_setup_product_header(plist,
295  product_frame,
296  framelist,
297  parlist,
298  "sinfo_utl_ima_line_corr",
299  "SINFONI",
300  KEY_VALUE_HPRO_DID),
301  "Problem in the product DFS-compliance") ;
302  */
303 
304  /* Save the file */
305  check(cpl_image_save(ima_out,
306  name_o,
307  CPL_BPP_IEEE_FLOAT,
308  plist,
309  CPL_IO_DEFAULT),
310  "Could not save product");
311  sinfo_free_propertylist(&plist) ;
312  sinfo_free_image(&ima_out);
313  sinfo_free_image(&ima);
314 
315  /* Log the saved file in the input frameset */
316  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
317 
318  }
319 
320  cleanup:
321  sinfo_free_image(&ima);
322  sinfo_free_image(&ima_out);
323  //sinfo_free_frameset(&raw_set);
324 
325 
326  /* This is usually freed by esorex: but what about if errors occurs?
327  sinfo_free_frame(&product_frame) ;
328  */
329 
330 
331  if (cpl_error_get_code()) {
332  return -1 ;
333  } else {
334  return 0 ;
335  }
336 
337 }
void irplib_reset(void)
Reset IRPLIB state.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69