UVES Pipeline Reference Manual  5.4.0
uves_utl_remove_crh_single.c
1 /* $Id: uves_utl_remove_crh_single.c,v 1.4 2012-05-09 12:50:33 amodigli Exp $
2  *
3  * This file is part of the UVES 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: 2012-05-09 12:50:33 $
24  * $Revision: 1.4 $
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 /*
44 #include <uves_tpl_utils.h>
45 #include <uves_pfits.h>
46 #include <uves_key_names.h>
47 #include <uves_raw_types.h>
48 #include <uves_pro_types.h>
49 #include <uves_functions.h>
50 */
51 
52 #include <uves_dfs.h>
53 #include <uves_msg.h>
54 #include <uves_error.h>
55 #include <uves_utils_wrappers.h>
56 #include <uves_remove_crh_single.h>
57 #include <uves_cpl_size.h>
58 
59 /*-----------------------------------------------------------------------------
60  Functions prototypes
61  ----------------------------------------------------------------------------*/
62 
63 static int uves_utl_remove_crh_single_create(cpl_plugin *) ;
64 static int uves_utl_remove_crh_single_exec(cpl_plugin *) ;
65 static int uves_utl_remove_crh_single_destroy(cpl_plugin *) ;
66 static int uves_utl_remove_crh_single(cpl_parameterlist *, cpl_frameset *) ;
67 
68 /*-----------------------------------------------------------------------------
69  Static variables
70  ----------------------------------------------------------------------------*/
71 
72 static char uves_utl_remove_crh_single_description[] =
73 "This recipe performs image computation.\n"
74 "The input files is one image\n"
75 "their associated tags should be IMA.\n"
76 "The output is the image cleaned from CRHs\n"
77 "Information on relevant parameters can be found with\n"
78 "esorex --params uves_utl_remove_crh_single\n"
79 "esorex --help uves_utl_remove_crh_single\n"
80 "\n";
81 
82 /*-----------------------------------------------------------------------------
83  Functions code
84  ----------------------------------------------------------------------------*/
85 /*---------------------------------------------------------------------------*/
89 /*---------------------------------------------------------------------------*/
90 
92 /*---------------------------------------------------------------------------*/
100 /*---------------------------------------------------------------------------*/
101 int cpl_plugin_get_info(cpl_pluginlist * list)
102 {
103  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
104  cpl_plugin * plugin = &recipe->interface ;
105 
106  cpl_plugin_init(plugin,
107  CPL_PLUGIN_API,
108  UVES_BINARY_VERSION,
109  CPL_PLUGIN_TYPE_RECIPE,
110  "uves_utl_remove_crh_single",
111  "Remove CRHs from an image",
112  uves_utl_remove_crh_single_description,
113  "Andrea Modigliani",
114  "Andrea.Modigliani@eso.org",
119 
120  cpl_pluginlist_append(list, plugin) ;
121 
122  return 0;
123 }
124 
125 /*---------------------------------------------------------------------------*/
134 /*---------------------------------------------------------------------------*/
135 static int uves_utl_remove_crh_single_create(cpl_plugin * plugin)
136 {
137  cpl_recipe * recipe ;
138  cpl_parameter * p ;
139 
140  /* Get the recipe out of the plugin */
141  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
142  recipe = (cpl_recipe *)plugin ;
143  else return -1 ;
144  cpl_error_reset();
145  irplib_reset();
146 
147  /* Create the parameters list in the cpl_recipe object */
148  recipe->parameters = cpl_parameterlist_new() ;
149 
150  /* Fill the parameters list */
151  /* --stropt */
152  p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.crh_frac_max",
153  CPL_TYPE_DOUBLE,
154  "Maximum fraction of allowed CRHs",
155  "uves.uves_utl_remove_crh_single",0.7);
156  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "crh_frac_max") ;
157  cpl_parameterlist_append(recipe->parameters, p) ;
158 
159  /* --doubleopt */
160  p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.sigma_lim",
161  CPL_TYPE_DOUBLE,
162  "Maximum sigma in kappa-sigma clip",
163  "uves.uves_utl_remove_crh_single", 25.) ;
164  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_lim") ;
165  cpl_parameterlist_append(recipe->parameters, p) ;
166 
167 
168 
169  p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.f_lim",
170  CPL_TYPE_DOUBLE,
171  "Max fraction of bad pixels allowed",
172  "uves.uves_utl_remove_crh_single", 0.7) ;
173  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "f_lim") ;
174  cpl_parameterlist_append(recipe->parameters, p) ;
175 
176 
177  p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.max_iter",
178  CPL_TYPE_INT,
179  "Max fraction of bad pixels allowed",
180  "uves.uves_utl_remove_crh_single",5) ;
181  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "max_iter") ;
182  cpl_parameterlist_append(recipe->parameters, p) ;
183 
184 
185 
186  p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.gain",
187  CPL_TYPE_DOUBLE,
188  "Detector's gain",
189  "uves.uves_utl_remove_crh_single",2.42) ;
190  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
191  cpl_parameterlist_append(recipe->parameters, p) ;
192 
193 
194  p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.ron",
195  CPL_TYPE_DOUBLE,
196  "Detector's ron",
197  "uves.uves_utl_remove_crh_single",1.) ;
198  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
199  cpl_parameterlist_append(recipe->parameters, p) ;
200 
201 
202 
203 
204  /* Return */
205  return 0;
206 }
207 
208 /*---------------------------------------------------------------------------*/
214 /*---------------------------------------------------------------------------*/
215 static int uves_utl_remove_crh_single_exec(cpl_plugin * plugin)
216 {
217  cpl_recipe * recipe ;
218  int code=0;
219  cpl_errorstate initial_errorstate = cpl_errorstate_get();
220 
221  /* Get the recipe out of the plugin */
222  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
223  recipe = (cpl_recipe *)plugin ;
224  else return -1 ;
225  cpl_error_reset();
226  irplib_reset();
227  code = uves_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
228 
229 
230  if (!cpl_errorstate_is_equal(initial_errorstate)) {
231  /* Dump the error history since recipe execution start.
232  At this point the recipe cannot recover from the error */
233  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
234  }
235 
236  return code ;
237 }
238 
239 /*---------------------------------------------------------------------------*/
245 /*---------------------------------------------------------------------------*/
246 static int uves_utl_remove_crh_single_destroy(cpl_plugin * plugin)
247 {
248  cpl_recipe * recipe ;
249 
250  /* Get the recipe out of the plugin */
251  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
252  recipe = (cpl_recipe *)plugin ;
253  else return -1 ;
254 
255  cpl_parameterlist_delete(recipe->parameters) ;
256  return 0 ;
257 }
258 
259 /*---------------------------------------------------------------------------*/
266 /*---------------------------------------------------------------------------*/
267 static int
268 uves_utl_remove_crh_single( cpl_parameterlist * parlist,
269  cpl_frameset * framelist)
270 {
271  cpl_parameter * param= NULL ;
272  cpl_frameset * raw_on=NULL;
273  cpl_frameset * raw_off=NULL;
274  double crh_frac_max=0;
275  double sigma_lim=0;
276  double f_lim=0;
277  int max_iter=0;
278  double gain=0;
279  double ron=0;
280  int nraw=0;
281 
282  cpl_image* ima_res=NULL;
283  cpl_image* ima_on=NULL;
284  cpl_image* ima_off=NULL;
285 
286 
287  cpl_propertylist* plist1=NULL;
288  cpl_propertylist* plist2=NULL;
289  cpl_propertylist* pliste=NULL;
290 
291  const char* name_r="ima_res.fits";
292 
293  cpl_frame* product_frame=NULL;
294  cpl_frame* frame_on=NULL;
295  cpl_frame* frame_off=NULL;
296  cpl_size next1=0;
297  const char* name1=NULL;
298 
299  cpl_size next2=0;
300  const char* name2=NULL;
301 
302  int noff=0;
303  int chips=0;
304  int nfrm=0;
305  int i=0;
306 
307 
308  uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
309  UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
310 
311  /* HOW TO RETRIEVE INPUT PARAMETERS */
312  check_nomsg(param=cpl_parameterlist_find(parlist,
313  "uves.uves_utl_remove_crh_single.crh_frac_max"));
314  check_nomsg(crh_frac_max=cpl_parameter_get_double(param));
315 
316  check_nomsg(param=cpl_parameterlist_find(parlist,
317  "uves.uves_utl_remove_crh_single.sigma_lim"));
318  check_nomsg(sigma_lim = cpl_parameter_get_double(param)) ;
319 
320 
321  check_nomsg(param=cpl_parameterlist_find(parlist,
322  "uves.uves_utl_remove_crh_single.f_lim"));
323  check_nomsg(f_lim = cpl_parameter_get_double(param)) ;
324 
325  check_nomsg(param=cpl_parameterlist_find(parlist,
326  "uves.uves_utl_remove_crh_single.max_iter"));
327  check_nomsg(max_iter = cpl_parameter_get_int(param)) ;
328 
329  check_nomsg(param=cpl_parameterlist_find(parlist,
330  "uves.uves_utl_remove_crh_single.gain"));
331  check_nomsg(gain = cpl_parameter_get_double(param)) ;
332 
333 
334  check_nomsg(param=cpl_parameterlist_find(parlist,
335  "uves.uves_utl_remove_crh_single.ron"));
336  check_nomsg(ron = cpl_parameter_get_double(param)) ;
337 
338 
339 
340 
341  /* Identify the RAW and CALIB frames in the input frameset */
342  check(uves_dfs_set_groups(framelist),
343  "Cannot identify RAW and CALIB frames") ;
344  //cpl_frameset_dump(framelist,stdout);
345 
346  /* HOW TO ACCESS INPUT DATA */
347  nfrm=cpl_frameset_get_size(framelist);
348  if(nfrm<1) {
349  uves_msg_error("Empty input frame list!");
350  goto cleanup ;
351  }
352 
353  /* HOW TO ACCESS INPUT DATA */
354  check_nomsg(raw_on=cpl_frameset_new());
355 
356  check(uves_contains_frames_kind(framelist,raw_on,"RAW_IMA"),
357  "Found no input frames with tag %s","RAW_IMA");
358  check_nomsg(nraw=cpl_frameset_get_size(raw_on));
359  if (nraw<1) {
360  uves_msg_error("Found no input frames with tag %s","RAW_IMA");
361  goto cleanup;
362  }
363 
364  uves_msg("nraw=%d",nraw);
365 
366  check_nomsg(frame_on=cpl_frameset_get_first(raw_on));
367  check_nomsg(next1=cpl_frame_get_nextensions(frame_on));
368  check_nomsg(name1=cpl_frame_get_filename(frame_on));
369  check_nomsg(plist1=cpl_propertylist_load(name1,0));
370  uves_msg("CRH affected file name =%s",name1);
371 
372 
373 
374  if (nfrm>1) {
375  /* if input has more than a frame, search for bias */
376  check_nomsg(raw_off=cpl_frameset_new());
377 
378  chips=cpl_propertylist_get_int(plist1,"ESO DET CHIPS");
379 
380  /* deal with BLUE/RED arms and search for proper bias*/
381  if(chips==2) {
382  check(uves_contains_frames_kind(framelist,raw_off,"BIAS_RED"),
383  "Found no input frames with tag %s","BIAS_RED");
384  } else {
385  check(uves_contains_frames_kind(framelist,raw_off,"BIAS_BLUE"),
386  "Found no input frames with tag %s","BIAS_BLUE");
387  }
388 
389  check_nomsg(noff=cpl_frameset_get_size(raw_off));
390  if (noff<1) {
391  uves_msg_error("Found no input bias frames");
392 
393  } else {
394 
395  frame_off=cpl_frameset_get_first(raw_off);
396  next2=cpl_frame_get_nextensions(frame_off);
397  /* check that raw frame and bias are coherent else exit */
398  if(next2 != next1) {
399  uves_msg_error("Raw frames with different number of extensions");
400  uves_msg_error("Something wrong! Exit");
401  goto cleanup;
402  }
403  name2=cpl_frame_get_filename(frame_off);
404  uves_msg("Bias file name =%s",name2);
405  check_nomsg(cpl_image_save(NULL, name_r,CPL_BPP_IEEE_FLOAT,
406  plist1,CPL_IO_DEFAULT));
407 
408  /* subtract bias on each extension */
409  if(next1==0) {
410  /*
411  subtract bias,
412  correct for cosmics,
413  save result
414  */
415 
416  check_nomsg(ima_on=cpl_image_load(name1,CPL_TYPE_FLOAT,0,0));
417  check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,0));
418  check_nomsg(cpl_image_subtract(ima_on,ima_off));
419 
420 
421  cpl_image_save(ima_on,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,
422  NULL,CPL_IO_DEFAULT);
423 
424 
425  check(ima_res=uves_remove_crh_single(ima_on,crh_frac_max,
426  sigma_lim,f_lim,
427  max_iter,gain,ron),
428  "fail to remove CRHs");
429 
430  check_nomsg(cpl_image_add(ima_res,ima_off));
431 
432  check_nomsg(cpl_image_save(ima_res, name_r,CPL_BPP_IEEE_FLOAT,
433  plist1,CPL_IO_DEFAULT));
434  } else {
435  uves_msg("next=%" CPL_SIZE_FORMAT "",next1);
436  /* loop over extensions,
437  subtract bias,
438  correct for cosmics,
439  save result
440  */
441 
442 
443 
444  for(i=1;i<=next1;i++) {
445  uves_msg("name1=%s",name1);
446  uves_msg("name2=%s",name2);
447  check_nomsg(ima_on=cpl_image_load(name1,CPL_TYPE_FLOAT,0,i));
448 
449 
450  check_nomsg(pliste=cpl_propertylist_load(name1,i));
451 
452  if(next2==0) {
453  check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,0));
454 
455  } else {
456  check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,i));
457 
458  }
459  uves_msg("ima_on=%p ima_off=%p",ima_on,ima_off);
460  check_nomsg(cpl_image_subtract(ima_on,ima_off));
461 
462  cpl_image_save(ima_on,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,
463  NULL,CPL_IO_DEFAULT);
464 
465  check(ima_res=uves_remove_crh_single(ima_on,crh_frac_max,
466  sigma_lim,f_lim,
467  max_iter,gain,ron),
468  "fail to remove CRHs");
469 
470  check_nomsg(cpl_image_add(ima_res,ima_off));
471 
472 
473  if(i>0) {
474  check_nomsg(cpl_image_save(ima_res, name_r,CPL_BPP_IEEE_FLOAT,
475  pliste,CPL_IO_EXTEND));
476  }
477  uves_free_image(&ima_on);
478  uves_free_image(&ima_off);
479  uves_free_image(&ima_res);
480  cpl_propertylist_delete(pliste); pliste=NULL;
481 
482 
483  }
484 
485 
486  }
487  }
488 
489  uves_free_frameset(&raw_off);
490  uves_free_frameset(&raw_on);
491 
492 
493  } else {
494  uves_msg("Please, provide a bias frame. Exit.");
495  goto cleanup;
496  }
497 
498 
499 
500 
501  /* HOW TO SAVE A PRODUCT ON DISK */
502  /* Set the file name */
503 
504  /* Create product frame */
505  check_nomsg(product_frame = cpl_frame_new());
506  check_nomsg(cpl_frame_set_filename(product_frame, name_r)) ;
507  check_nomsg(cpl_frame_set_tag(product_frame, "PRODUCT")) ;
508  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
509  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
510  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
511  "Error while initialising the product frame") ;
512 
513  /* Add DataFlow keywords */
514  check_nomsg(cpl_propertylist_erase_regexp(plist1, "^ESO PRO CATG",0));
515  /*
516  check(cpl_dfs_setup_product_header(plist,
517  product_frame,
518  framelist,
519  parlist,
520  "uves_utl_remove_crh_single",
521  "UVES",
522  DICTIONARY),
523  "Problem in the product DFS-compliance") ;
524  */
525  /* Save the file
526  check(cpl_image_save(ima_res,
527  name_r,
528  CPL_BPP_IEEE_FLOAT,
529  plist1,
530  CPL_IO_DEFAULT),
531  "Could not save product");
532  */
533  cpl_propertylist_delete(plist1) ; plist1=NULL;
534 
535  /* Log the saved file in the input frameset */
536  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
537 
538 
539  cleanup:
540  uves_free_frameset(&raw_off);
541  uves_free_image(&ima_on);
542  uves_free_image(&ima_off);
543  uves_free_image(&ima_res);
544 
545  if(pliste!=NULL) cpl_propertylist_delete(pliste); pliste=NULL;
546 
547  if (plist1!=NULL) cpl_propertylist_delete(plist1);plist1=NULL;
548  if (plist2!=NULL) cpl_propertylist_delete(plist2);plist2=NULL;
549 
550 
551  if (cpl_error_get_code()) {
552  return -1 ;
553  } else {
554  return 0 ;
555  }
556 
557 }