SINFONI Pipeline Reference Manual  2.6.0
sinfo_utl_remove_crh_single.c
1 /* $Id: sinfo_utl_remove_crh_single.c,v 1.2 2009-01-30 14:56:12 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-01-30 14:56:12 $
24  * $Revision: 1.2 $
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_raw_types.h>
48 #include <sinfo_pro_types.h>
49 #include <sinfo_functions.h>
50 #include <sinfo_msg.h>
51 #include <sinfo_error.h>
52 #include <sinfo_utils_wrappers.h>
53 #include <sinfo_remove_crh_single.h>
54 
55 /*-----------------------------------------------------------------------------
56  Functions prototypes
57  ----------------------------------------------------------------------------*/
58 
59 static int sinfo_utl_remove_crh_single_create(cpl_plugin *) ;
60 static int sinfo_utl_remove_crh_single_exec(cpl_plugin *) ;
61 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin *) ;
62 static int sinfo_utl_remove_crh_single(cpl_parameterlist *, cpl_frameset *) ;
63 
64 /*-----------------------------------------------------------------------------
65  Static variables
66  ----------------------------------------------------------------------------*/
67 
68 static char sinfo_utl_remove_crh_single_description[] =
69  "This recipe performs image computation.\n"
70  "The input files is one image\n"
71  "their associated tags should be IMA.\n"
72  "The output is the image cleaned from CRHs\n"
73  "\n";
74 
75 /*-----------------------------------------------------------------------------
76  Functions code
77  ----------------------------------------------------------------------------*/
78 /*---------------------------------------------------------------------------*/
82 /*---------------------------------------------------------------------------*/
83 
85 /*---------------------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
94 int cpl_plugin_get_info(cpl_pluginlist * list)
95 {
96  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
97  cpl_plugin * plugin = &recipe->interface ;
98 
99  cpl_plugin_init(plugin,
100  CPL_PLUGIN_API,
101  SINFONI_BINARY_VERSION,
102  CPL_PLUGIN_TYPE_RECIPE,
103  "sinfo_utl_remove_crh_single",
104  "Remove CRHs from an image",
105  sinfo_utl_remove_crh_single_description,
106  "Andrea Modigliani",
107  "Andrea.Modigliani@eso.org",
108  sinfo_get_license(),
109  sinfo_utl_remove_crh_single_create,
110  sinfo_utl_remove_crh_single_exec,
111  sinfo_utl_remove_crh_single_destroy) ;
112 
113  cpl_pluginlist_append(list, plugin) ;
114 
115  return 0;
116 }
117 
118 /*---------------------------------------------------------------------------*/
127 /*---------------------------------------------------------------------------*/
128 static int sinfo_utl_remove_crh_single_create(cpl_plugin * plugin)
129 {
130  cpl_recipe * recipe ;
131  cpl_parameter * p ;
132 
133  /* Get the recipe out of the plugin */
134  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
135  recipe = (cpl_recipe *)plugin ;
136  else return -1 ;
137  cpl_error_reset();
138  irplib_reset();
139 
140  /* Create the parameters list in the cpl_recipe object */
141  recipe->parameters = cpl_parameterlist_new() ;
142 
143  /* Fill the parameters list */
144  /* --stropt */
145  p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.crh_frac_max",
146  CPL_TYPE_DOUBLE,
147  "Maximum fraction of allowed CRHs",
148  "sinfoni.sinfo_utl_remove_crh_single",0.7);
149  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "crh_frac_max") ;
150  cpl_parameterlist_append(recipe->parameters, p) ;
151 
152  /* --doubleopt */
153  p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.sigma_lim",
154  CPL_TYPE_DOUBLE,
155  "Maximum sigma in kappa-sigma clip",
156  "sinfoni.sinfo_utl_remove_crh_single", 25.) ;
157  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_lim") ;
158  cpl_parameterlist_append(recipe->parameters, p) ;
159 
160 
161 
162  p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.f_lim",
163  CPL_TYPE_DOUBLE,
164  "Max fraction of bad pixels allowed",
165  "sinfoni.sinfo_utl_remove_crh_single", 0.7) ;
166  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "f_lim") ;
167  cpl_parameterlist_append(recipe->parameters, p) ;
168 
169 
170  p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.max_iter",
171  CPL_TYPE_INT,
172  "Max fraction of bad pixels allowed",
173  "sinfoni.sinfo_utl_remove_crh_single",5) ;
174  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "max_iter") ;
175  cpl_parameterlist_append(recipe->parameters, p) ;
176 
177 
178 
179  p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.gain",
180  CPL_TYPE_DOUBLE,
181  "Detector's gain",
182  "sinfoni.sinfo_utl_remove_crh_single",2.42) ;
183  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
184  cpl_parameterlist_append(recipe->parameters, p) ;
185 
186 
187  p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.ron",
188  CPL_TYPE_DOUBLE,
189  "Detector's ron",
190  "sinfoni.sinfo_utl_remove_crh_single",1.) ;
191  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
192  cpl_parameterlist_append(recipe->parameters, p) ;
193 
194 
195 
196 
197  /* Return */
198  return 0;
199 }
200 
201 /*---------------------------------------------------------------------------*/
207 /*---------------------------------------------------------------------------*/
208 static int sinfo_utl_remove_crh_single_exec(cpl_plugin * plugin)
209 {
210  cpl_recipe * recipe ;
211  int code=0;
212  cpl_errorstate initial_errorstate = cpl_errorstate_get();
213 
214  /* Get the recipe out of the plugin */
215  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
216  recipe = (cpl_recipe *)plugin ;
217  else return -1 ;
218  cpl_error_reset();
219  irplib_reset();
220  code = sinfo_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
221 
222 
223  if (!cpl_errorstate_is_equal(initial_errorstate)) {
224  /* Dump the error history since recipe execution start.
225  At this point the recipe cannot recover from the error */
226  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
227  }
228 
229  return code ;
230 }
231 
232 /*---------------------------------------------------------------------------*/
238 /*---------------------------------------------------------------------------*/
239 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin * plugin)
240 {
241  cpl_recipe * recipe ;
242 
243  /* Get the recipe out of the plugin */
244  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
245  recipe = (cpl_recipe *)plugin ;
246  else return -1 ;
247 
248  cpl_parameterlist_delete(recipe->parameters) ;
249  return 0 ;
250 }
251 
252 /*---------------------------------------------------------------------------*/
259 /*---------------------------------------------------------------------------*/
260 static int
261 sinfo_utl_remove_crh_single( cpl_parameterlist * parlist,
262  cpl_frameset * framelist)
263 {
264  cpl_parameter * param= NULL ;
265  cpl_frameset * raw_on=NULL;
266  cpl_frameset * raw_off=NULL;
267  double crh_frac_max=0;
268  double sigma_lim=0;
269  double f_lim=0;
270  int max_iter=0;
271  double gain=0;
272  double ron=0;
273  int nraw=0;
274  int n=0;
275  cpl_image* ima_res=NULL;
276  char name[256];
277  cpl_image* image=NULL;
278  cpl_image* img_on=NULL;
279  cpl_image* img_off=NULL;
280  cpl_propertylist* plist=NULL;
281  const char* name_o=NULL;
282  cpl_frame* product_frame=NULL;
283  //cpl_frame* frame_on=NULL;
284  //cpl_frame* frame_off=NULL;
285 
286  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
287  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
288 
289  /* HOW TO RETRIEVE INPUT PARAMETERS */
290  check_nomsg(param=cpl_parameterlist_find(parlist,
291  "sinfoni.sinfo_utl_remove_crh_single.crh_frac_max"));
292  check_nomsg(crh_frac_max=cpl_parameter_get_double(param));
293 
294  check_nomsg(param=cpl_parameterlist_find(parlist,
295  "sinfoni.sinfo_utl_remove_crh_single.sigma_lim"));
296  check_nomsg(sigma_lim = cpl_parameter_get_double(param)) ;
297 
298 
299  check_nomsg(param=cpl_parameterlist_find(parlist,
300  "sinfoni.sinfo_utl_remove_crh_single.f_lim"));
301  check_nomsg(f_lim = cpl_parameter_get_double(param)) ;
302 
303  check_nomsg(param=cpl_parameterlist_find(parlist,
304  "sinfoni.sinfo_utl_remove_crh_single.max_iter"));
305  check_nomsg(max_iter = cpl_parameter_get_int(param)) ;
306 
307  check_nomsg(param=cpl_parameterlist_find(parlist,
308  "sinfoni.sinfo_utl_remove_crh_single.gain"));
309  check_nomsg(gain = cpl_parameter_get_double(param)) ;
310 
311 
312  check_nomsg(param=cpl_parameterlist_find(parlist,
313  "sinfoni.sinfo_utl_remove_crh_single.ron"));
314  check_nomsg(ron = cpl_parameter_get_double(param)) ;
315 
316  /* Identify the RAW and CALIB frames in the input frameset */
317  check(sinfo_dfs_set_groups(framelist),
318  "Cannot identify RAW and CALIB frames") ;
319  //cpl_frameset_dump(framelist,stdout);
320 
321  /* HOW TO ACCESS INPUT DATA */
322  n=cpl_frameset_get_size(framelist);
323  if(n<1) {
324  sinfo_msg_error("Empty input frame list!");
325  goto cleanup ;
326  }
327 
328  /* HOW TO ACCESS INPUT DATA */
329  check_nomsg(raw_on=cpl_frameset_new());
330 
331  check(sinfo_contains_frames_kind(framelist,raw_on,RAW_ON),
332  "Found no input frames with tag %s",RAW_ON);
333  check_nomsg(nraw=cpl_frameset_get_size(raw_on));
334  if (nraw<1) {
335  sinfo_msg_error("Found no input frames with tag %s",RAW_ON);
336  goto cleanup;
337  }
338 
339 
340  check_nomsg(raw_off=cpl_frameset_new());
341 
342  check(sinfo_contains_frames_kind(framelist,raw_off,RAW_OFF),
343  "Found no input frames with tag %s",RAW_OFF);
344  check_nomsg(nraw=cpl_frameset_get_size(raw_off));
345  if (nraw<1) {
346  sinfo_msg_error("Found no input frames with tag %s",RAW_OFF);
347  goto cleanup;
348  }
349 
350  cpl_frameset_iterator* it = cpl_frameset_iterator_new(raw_on);
351  cpl_frame *frame_on = cpl_frameset_iterator_get(it);
352  cpl_frameset_iterator_delete(it);
353 
354  check_nomsg(strcpy(name,cpl_frame_get_filename(frame_on)));
355  check_nomsg(sinfo_free_frameset(&raw_on));
356  sinfo_msg("name=%s",name);
357  check_nomsg(img_on=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
358  check(plist=cpl_propertylist_load(name,0),
359  "Cannot read the FITS header") ;
360 
361  cpl_frameset_iterator* it = cpl_frameset_iterator_new(raw_off);
362  cpl_frame *frame_off = cpl_frameset_iterator_get(it);
363  cpl_frameset_iterator_delete(it);
364 
365  strcpy(name,cpl_frame_get_filename(frame_off));
366  sinfo_free_frameset(&raw_off);
367 
368  img_off=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
369 
370  image=cpl_image_duplicate(img_on);
371  sinfo_free_image(&img_on);
372 
373  cpl_image_subtract(image,img_off);
374  cpl_image_save(image,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,NULL,
375  CPL_IO_DEFAULT);
376 
377  check(ima_res=sinfo_remove_crh_single(image,crh_frac_max,sigma_lim,f_lim,
378  max_iter,gain,ron),
379  "fail to remove CRHs");
380 
381  sinfo_free_image(&image);
382 
383 
384 
385  /* HOW TO SAVE A PRODUCT ON DISK */
386  /* Set the file name */
387  name_o = "ima_res.fits" ;
388 
389  /* Create product frame */
390  check_nomsg(product_frame = cpl_frame_new());
391  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
392  check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
393  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
394  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
395  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
396  "Error while initialising the product frame") ;
397 
398  /* Add DataFlow keywords */
399  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
400  check(cpl_dfs_setup_product_header(plist,
401  product_frame,
402  framelist,
403  parlist,
404  "sinfo_utl_remove_crh_single",
405  "SINFONI",
406  KEY_VALUE_HPRO_DID,NULL),
407  "Problem in the product DFS-compliance") ;
408 
409  /* Save the file */
410  check(cpl_image_save(ima_res,
411  name_o,
412  CPL_BPP_IEEE_FLOAT,
413  plist,
414  CPL_IO_DEFAULT),
415  "Could not save product");
416  sinfo_free_propertylist(&plist) ;
417 
418 
419  /* Log the saved file in the input frameset */
420  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
421 
422 
423  cleanup:
424 
425  sinfo_free_frameset(&raw_on);
426  sinfo_free_frameset(&raw_off);
427  sinfo_free_image(&img_on);
428  sinfo_free_image(&img_off);
429  sinfo_free_propertylist(&plist) ;
430  /* This is usually freed by esorex: but what about if errors occurs?
431  sinfo_free_frame(&product_frame) ;
432  */
433  sinfo_free_image(&image) ;
434 
435  if (cpl_error_get_code()) {
436  return -1 ;
437  } else {
438  return 0 ;
439  }
440 
441 }
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