41 #include <irplib_utils.h>
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>
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 *) ;
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"
96 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe ) ;
97 cpl_plugin * plugin = &recipe->interface ;
99 cpl_plugin_init(plugin,
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,
107 "Andrea.Modigliani@eso.org",
109 sinfo_utl_remove_crh_single_create,
110 sinfo_utl_remove_crh_single_exec,
111 sinfo_utl_remove_crh_single_destroy) ;
113 cpl_pluginlist_append(list, plugin) ;
128 static int sinfo_utl_remove_crh_single_create(cpl_plugin * plugin)
130 cpl_recipe * recipe ;
134 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
135 recipe = (cpl_recipe *)plugin ;
141 recipe->parameters = cpl_parameterlist_new() ;
145 p = cpl_parameter_new_value(
"sinfoni.sinfo_utl_remove_crh_single.crh_frac_max",
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) ;
153 p = cpl_parameter_new_value(
"sinfoni.sinfo_utl_remove_crh_single.sigma_lim",
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) ;
162 p = cpl_parameter_new_value(
"sinfoni.sinfo_utl_remove_crh_single.f_lim",
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) ;
170 p = cpl_parameter_new_value(
"sinfoni.sinfo_utl_remove_crh_single.max_iter",
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) ;
179 p = cpl_parameter_new_value(
"sinfoni.sinfo_utl_remove_crh_single.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) ;
187 p = cpl_parameter_new_value(
"sinfoni.sinfo_utl_remove_crh_single.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) ;
208 static int sinfo_utl_remove_crh_single_exec(cpl_plugin * plugin)
210 cpl_recipe * recipe ;
212 cpl_errorstate initial_errorstate = cpl_errorstate_get();
215 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
216 recipe = (cpl_recipe *)plugin ;
220 code = sinfo_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
223 if (!cpl_errorstate_is_equal(initial_errorstate)) {
226 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
239 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin * plugin)
241 cpl_recipe * recipe ;
244 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
245 recipe = (cpl_recipe *)plugin ;
248 cpl_parameterlist_delete(recipe->parameters) ;
261 sinfo_utl_remove_crh_single( cpl_parameterlist * parlist,
262 cpl_frameset * framelist)
264 cpl_parameter * param= NULL ;
265 cpl_frameset * raw_on=NULL;
266 cpl_frameset * raw_off=NULL;
267 double crh_frac_max=0;
275 cpl_image* ima_res=NULL;
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;
286 sinfo_msg(
"Welcome to SINFONI Pipeline release %d.%d.%d",
287 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
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));
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)) ;
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)) ;
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)) ;
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)) ;
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)) ;
317 check(sinfo_dfs_set_groups(framelist),
318 "Cannot identify RAW and CALIB frames") ;
322 n=cpl_frameset_get_size(framelist);
329 check_nomsg(raw_on=cpl_frameset_new());
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));
340 check_nomsg(raw_off=cpl_frameset_new());
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));
351 check_nomsg(frame_on=cpl_frameset_get_first(raw_on));
352 check_nomsg(strcpy(name,cpl_frame_get_filename(frame_on)));
353 check_nomsg(sinfo_free_frameset(&raw_on));
354 sinfo_msg(
"name=%s",name);
355 check_nomsg(img_on=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
356 check(plist=cpl_propertylist_load(name,0),
357 "Cannot read the FITS header") ;
360 frame_off=cpl_frameset_get_first(raw_off);
361 strcpy(name,cpl_frame_get_filename(frame_off));
362 sinfo_free_frameset(&raw_off);
364 img_off=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
366 image=cpl_image_duplicate(img_on);
367 sinfo_free_image(&img_on);
369 cpl_image_subtract(image,img_off);
370 cpl_image_save(image,
"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,NULL,
373 check(ima_res=sinfo_remove_crh_single(image,crh_frac_max,sigma_lim,f_lim,
375 "fail to remove CRHs");
377 sinfo_free_image(&image);
383 name_o =
"ima_res.fits" ;
386 check_nomsg(product_frame = cpl_frame_new());
387 check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
388 check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
389 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
390 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
391 check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
392 "Error while initialising the product frame") ;
395 check_nomsg(cpl_propertylist_erase_regexp(plist,
"^ESO PRO CATG",0));
396 check(cpl_dfs_setup_product_header(plist,
400 "sinfo_utl_remove_crh_single",
402 KEY_VALUE_HPRO_DID,NULL),
403 "Problem in the product DFS-compliance") ;
406 check(cpl_image_save(ima_res,
411 "Could not save product");
412 sinfo_free_propertylist(&plist) ;
416 check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
421 sinfo_free_frameset(&raw_on);
422 sinfo_free_frameset(&raw_off);
423 sinfo_free_image(&img_on);
424 sinfo_free_image(&img_off);
425 sinfo_free_propertylist(&plist) ;
429 sinfo_free_image(&image) ;
431 if (cpl_error_get_code()) {