40 #include "irplib_utils.h"
41 #include "hawki_utils.h"
42 #include "hawki_load.h"
43 #include "hawki_save.h"
44 #include "hawki_pfits.h"
45 #include "hawki_dfs.h"
46 #include "hawki_calib.h"
59 int cpl_plugin_get_info(cpl_pluginlist * list);
61 static int hawki_step_subtract_bkg_create(cpl_plugin *) ;
62 static int hawki_step_subtract_bkg_exec(cpl_plugin *) ;
63 static int hawki_step_subtract_bkg_destroy(cpl_plugin *) ;
64 static int hawki_step_subtract_bkg(cpl_parameterlist *, cpl_frameset *) ;
66 static int hawki_step_subtract_bkg_apply_one_to_one_save
67 (cpl_frameset * objframes,
68 cpl_frameset * bkgframes,
69 cpl_parameterlist * parlist,
70 cpl_frameset * recipe_framelist);
72 static int hawki_step_subtract_bkg_apply_one_to_all_save
73 (cpl_frameset * objframes,
74 cpl_frameset * bkgframes,
75 cpl_parameterlist * recipe_parlist,
76 cpl_frameset * recipe_framelist);
78 static int hawki_step_subtract_bkg_save
79 (cpl_imagelist * obj_images,
81 cpl_frameset * used_frameset,
82 cpl_parameterlist * recipe_parlist,
83 cpl_frameset * recipe_framelist);
89 static char hawki_step_subtract_bkg_description[] =
90 "hawki_step_subtract_bkg -- hawki background subtraction utility.\n"
91 "This recipe will subtract the given background to the science images.\n"
92 "The background can be obtained from the sky or object images\n"
93 "using the hawki_util_compute_bkg utility.\n"
94 "There are two modes of operation:\n"
95 "One single background image that it is subtracted to all object images.\n"
96 "As many background images as objects. A one to one relationship is applied.\n"
97 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
98 "obj_basic_cal-file.fits "HAWKI_CALPRO_BASICCALIBRATED
" or\n"
99 "background-file.fits "HAWKI_CALPRO_BKGIMAGE
" \n";
114 int cpl_plugin_get_info(cpl_pluginlist * list)
116 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
117 cpl_plugin * plugin = &recipe->interface ;
119 cpl_plugin_init(plugin,
121 HAWKI_BINARY_VERSION,
122 CPL_PLUGIN_TYPE_RECIPE,
123 "hawki_step_subtract_bkg",
124 "Background subtraction utility",
125 hawki_step_subtract_bkg_description,
126 "Cesar Enrique Garcia Dabo",
129 hawki_step_subtract_bkg_create,
130 hawki_step_subtract_bkg_exec,
131 hawki_step_subtract_bkg_destroy) ;
133 cpl_pluginlist_append(list, plugin) ;
148 static int hawki_step_subtract_bkg_create(cpl_plugin * plugin)
150 cpl_recipe * recipe ;
154 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
155 recipe = (cpl_recipe *)plugin ;
159 recipe->parameters = cpl_parameterlist_new() ;
160 if (recipe->parameters == NULL)
177 static int hawki_step_subtract_bkg_exec(cpl_plugin * plugin)
179 cpl_recipe * recipe ;
182 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
183 recipe = (cpl_recipe *)plugin ;
189 return hawki_step_subtract_bkg(recipe->parameters, recipe->frames) ;
199 static int hawki_step_subtract_bkg_destroy(cpl_plugin * plugin)
201 cpl_recipe * recipe ;
204 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
205 recipe = (cpl_recipe *)plugin ;
208 cpl_parameterlist_delete(recipe->parameters) ;
220 static int hawki_step_subtract_bkg(
221 cpl_parameterlist * parlist,
222 cpl_frameset * framelist)
226 cpl_frameset * objframes;
227 cpl_frameset * bkgframes;
233 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
238 cpl_msg_info(__func__,
"Identifying objects and background data");
240 (framelist, HAWKI_CALPRO_BASICCALIBRATED);
241 if (objframes == NULL)
243 cpl_msg_error(__func__,
"No object frames provided (%s)",
244 HAWKI_CALPRO_BASICCALIBRATED);
249 (framelist, HAWKI_CALPRO_BKGIMAGE);
250 if (bkgframes == NULL)
252 cpl_msg_error(__func__,
"No background frames provided (%s)",
253 HAWKI_CALPRO_BKGIMAGE);
254 cpl_frameset_delete(objframes);
259 nobj = cpl_frameset_get_size(objframes);
260 nbkg = cpl_frameset_get_size(bkgframes);
262 hawki_step_subtract_bkg_apply_one_to_one_save
263 (objframes, bkgframes, parlist, framelist);
265 hawki_step_subtract_bkg_apply_one_to_all_save
266 (objframes, bkgframes, parlist, framelist);
269 cpl_msg_error(__func__,
"Incompatible number of science and background"
271 cpl_msg_error(__func__,
"Supply only 1 bkg frame or as many as objects");
272 cpl_frameset_delete(objframes);
273 cpl_frameset_delete(bkgframes);
278 cpl_frameset_delete(objframes);
279 cpl_frameset_delete(bkgframes);
282 if (cpl_error_get_code())
284 cpl_msg_error(__func__,
285 "HAWK-I pipeline could not recover from previous errors");
301 static int hawki_step_subtract_bkg_apply_one_to_one_save
302 (cpl_frameset * objframes,
303 cpl_frameset * bkgframes,
304 cpl_parameterlist * recipe_parlist,
305 cpl_frameset * recipe_framelist)
311 cpl_msg_info(__func__,
"Using a one to one relation btw objects and bkgs");
312 nobjs = cpl_frameset_get_size(objframes);
313 for(iobj = 0; iobj < nobjs; ++iobj)
315 cpl_frame * obj_frame = NULL;
316 cpl_frame * bkg_frame = NULL;
317 cpl_imagelist * obj_images = NULL;
318 cpl_imagelist * bkg_images = NULL;
319 cpl_frameset * used_frameset;
322 used_frameset = cpl_frameset_new();
325 cpl_msg_indent_more();
326 cpl_msg_info(__func__,
"Applying correction to object %d", iobj+1) ;
327 obj_frame = cpl_frameset_get_frame(objframes, iobj);
328 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
329 if(obj_frame != NULL)
331 if(obj_images == NULL)
333 cpl_msg_indent_less();
334 cpl_msg_error(__func__,
"Error reading obj image") ;
335 cpl_frameset_delete(used_frameset);
340 bkg_frame = cpl_frameset_get_frame(bkgframes, iobj);
341 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
342 if(bkg_frame != NULL)
344 if(bkg_images == NULL)
346 cpl_msg_error(__func__,
"Error reading background image") ;
347 cpl_msg_indent_less();
348 cpl_imagelist_delete(obj_images);
349 cpl_frameset_delete(used_frameset);
357 if(hawki_step_subtract_bkg_save(obj_images,
361 recipe_framelist) != 0)
362 cpl_msg_warning(__func__,
"Some data could not be saved. "
363 "Check permisions or disk space");
366 cpl_msg_indent_less();
367 cpl_imagelist_delete(obj_images);
368 cpl_imagelist_delete(bkg_images);
369 cpl_frameset_delete(used_frameset);
386 static int hawki_step_subtract_bkg_apply_one_to_all_save
387 (cpl_frameset * objframes,
388 cpl_frameset * bkgframes,
389 cpl_parameterlist * recipe_parlist,
390 cpl_frameset * recipe_framelist)
394 cpl_frame * bkg_frame;
395 cpl_imagelist * bkg_images = NULL;
398 cpl_msg_info(__func__,
"Using the same bkg for all the objects");
399 bkg_frame = cpl_frameset_get_first(bkgframes);
400 if(bkg_frame != NULL)
402 if(bkg_images == NULL)
404 cpl_msg_error(__func__,
"Error reading background image");
409 nobjs = cpl_frameset_get_size(objframes);
410 for(iobj = 0; iobj < nobjs; ++iobj)
412 cpl_frame * obj_frame;
413 cpl_imagelist * obj_images = NULL;
414 cpl_frameset * used_frameset;
417 used_frameset = cpl_frameset_new();
420 cpl_msg_indent_more();
421 cpl_msg_info(__func__,
"Applying correction to object %d", iobj+1) ;
422 obj_frame = cpl_frameset_get_frame(objframes, iobj);
423 if(obj_frame != NULL)
425 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
426 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
427 if(obj_images == NULL)
429 cpl_msg_indent_less();
430 cpl_msg_error(__func__,
"Error reading obj image") ;
431 cpl_frameset_delete(used_frameset);
439 hawki_step_subtract_bkg_save(obj_images,
446 cpl_msg_indent_less();
447 cpl_imagelist_delete(obj_images);
448 cpl_frameset_delete(used_frameset);
452 cpl_imagelist_delete(bkg_images);
467 static int hawki_step_subtract_bkg_save
468 (cpl_imagelist * obj_images,
470 cpl_frameset * used_frameset,
471 cpl_parameterlist * recipe_parlist,
472 cpl_frameset * recipe_framelist)
474 const cpl_frame * raw_reference;
475 cpl_propertylist ** extproplists;
477 cpl_propertylist * inputlist ;
479 const char * recipe_name =
"hawki_step_subtract_bkg";
481 cpl_errorstate error_prevstate = cpl_errorstate_get();
484 raw_reference = irplib_frameset_get_first_from_group
485 (used_frameset, CPL_FRAME_GROUP_RAW);
488 cpl_msg_indent_more();
489 cpl_msg_info(__func__,
"Creating the keywords list") ;
490 extproplists = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*));
491 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
495 (cpl_frame_get_filename(raw_reference), idet+1);
498 extproplists[idet] = cpl_propertylist_new();
501 inputlist = cpl_propertylist_load_regexp(
502 cpl_frame_get_filename(raw_reference), ext_nb,
503 HAWKI_HEADER_EXT_FORWARD, 0);
504 cpl_propertylist_append(extproplists[idet], inputlist);
505 cpl_propertylist_delete(inputlist);
506 inputlist = cpl_propertylist_load_regexp(
507 cpl_frame_get_filename(raw_reference), ext_nb,
508 HAWKI_HEADER_WCS, 0);
509 cpl_propertylist_append(extproplists[idet], inputlist);
510 cpl_propertylist_delete(inputlist);
514 snprintf(filename, 256,
"hawki_step_subtract_bkg_%04d.fits", iserie+1);
520 HAWKI_CALPRO_BKG_SUBTRACTED,
521 HAWKI_PROTYPE_BKG_SUBTRACTED,
523 (
const cpl_propertylist**)extproplists,
527 cpl_msg_indent_less();
528 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
530 cpl_propertylist_delete(extproplists[idet]) ;
532 cpl_free(extproplists) ;
533 if(!cpl_errorstate_is_equal(error_prevstate))
535 cpl_errorstate_set(CPL_ERROR_NONE);