00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039
00040 #include "irplib_utils.h"
00041 #include "hawki_utils.h"
00042 #include "hawki_load.h"
00043 #include "hawki_save.h"
00044 #include "hawki_pfits.h"
00045 #include "hawki_dfs.h"
00046 #include "hawki_calib.h"
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 static int hawki_step_subtract_bkg_create(cpl_plugin *) ;
00057 static int hawki_step_subtract_bkg_exec(cpl_plugin *) ;
00058 static int hawki_step_subtract_bkg_destroy(cpl_plugin *) ;
00059 static int hawki_step_subtract_bkg(cpl_parameterlist *, cpl_frameset *) ;
00060
00061 static int hawki_step_subtract_bkg_apply_one_to_one_save
00062 (cpl_frameset * objframes,
00063 cpl_frameset * bkgframes,
00064 cpl_parameterlist * parlist,
00065 cpl_frameset * recipe_framelist);
00066
00067 static int hawki_step_subtract_bkg_apply_one_to_all_save
00068 (cpl_frameset * objframes,
00069 cpl_frameset * bkgframes,
00070 cpl_parameterlist * recipe_parlist,
00071 cpl_frameset * recipe_framelist);
00072
00073 static int hawki_step_subtract_bkg_save
00074 (cpl_imagelist * obj_images,
00075 int iserie,
00076 cpl_frameset * used_frameset,
00077 cpl_parameterlist * recipe_parlist,
00078 cpl_frameset * recipe_framelist);
00079
00080
00081
00082
00083
00084 static char hawki_step_subtract_bkg_description[] =
00085 "hawki_step_subtract_bkg -- hawki background subtraction utility.\n"
00086 "This recipe will subtract the given background to the science images.\n"
00087 "The background can be obtained from the sky or object images\n"
00088 "using the hawki_util_compute_bkg utility.\n"
00089 "There are two modes of operation:\n"
00090 "One single background image that it is subtracted to all object images.\n"
00091 "As many background images as objects. A one to one relationship is applied.\n"
00092 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00093 "obj_basic_cal-file.fits "HAWKI_CALPRO_BASICCALIBRATED" or\n"
00094 "background-file.fits "HAWKI_CALPRO_BKGIMAGE" \n";
00095
00096
00097
00098
00099
00100
00108
00109 int cpl_plugin_get_info(cpl_pluginlist * list)
00110 {
00111 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
00112 cpl_plugin * plugin = &recipe->interface ;
00113
00114 cpl_plugin_init(plugin,
00115 CPL_PLUGIN_API,
00116 HAWKI_BINARY_VERSION,
00117 CPL_PLUGIN_TYPE_RECIPE,
00118 "hawki_step_subtract_bkg",
00119 "Background subtraction utility",
00120 hawki_step_subtract_bkg_description,
00121 "Cesar Enrique Garcia Dabo",
00122 PACKAGE_BUGREPORT,
00123 hawki_get_license(),
00124 hawki_step_subtract_bkg_create,
00125 hawki_step_subtract_bkg_exec,
00126 hawki_step_subtract_bkg_destroy) ;
00127
00128 cpl_pluginlist_append(list, plugin) ;
00129
00130 return 0;
00131 }
00132
00133
00142
00143 static int hawki_step_subtract_bkg_create(cpl_plugin * plugin)
00144 {
00145 cpl_recipe * recipe ;
00146
00147
00148
00149 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00150 recipe = (cpl_recipe *)plugin ;
00151 else return -1 ;
00152
00153
00154 recipe->parameters = cpl_parameterlist_new() ;
00155
00156
00157
00158
00159
00160 return 0;
00161 }
00162
00163
00169
00170 static int hawki_step_subtract_bkg_exec(cpl_plugin * plugin)
00171 {
00172 cpl_recipe * recipe ;
00173
00174
00175 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00176 recipe = (cpl_recipe *)plugin ;
00177 else return -1 ;
00178
00179
00180 hawki_print_banner();
00181
00182 return hawki_step_subtract_bkg(recipe->parameters, recipe->frames) ;
00183 }
00184
00185
00191
00192 static int hawki_step_subtract_bkg_destroy(cpl_plugin * plugin)
00193 {
00194 cpl_recipe * recipe ;
00195
00196
00197 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00198 recipe = (cpl_recipe *)plugin ;
00199 else return -1 ;
00200
00201 cpl_parameterlist_delete(recipe->parameters) ;
00202 return 0 ;
00203 }
00204
00205
00212
00213 static int hawki_step_subtract_bkg(
00214 cpl_parameterlist * parlist,
00215 cpl_frameset * framelist)
00216 {
00217 int nobj;
00218 int nbkg;
00219 cpl_frameset * objframes;
00220 cpl_frameset * bkgframes;
00221
00222
00223
00224 if (hawki_dfs_set_groups(framelist))
00225 {
00226 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00227 return -1 ;
00228 }
00229
00230
00231 cpl_msg_info(__func__, "Identifying objects and background data");
00232 objframes = hawki_extract_frameset
00233 (framelist, HAWKI_CALPRO_BASICCALIBRATED);
00234 if (objframes == NULL)
00235 {
00236 cpl_msg_error(__func__, "No object frames provided (%s)",
00237 HAWKI_CALPRO_BASICCALIBRATED);
00238 return -1 ;
00239 }
00240
00241 bkgframes = hawki_extract_frameset
00242 (framelist, HAWKI_CALPRO_BKGIMAGE);
00243 if (bkgframes == NULL)
00244 {
00245 cpl_msg_error(__func__, "No background frames provided (%s)",
00246 HAWKI_CALPRO_BKGIMAGE);
00247 cpl_frameset_delete(objframes);
00248 return -1 ;
00249 }
00250
00251
00252 nobj = cpl_frameset_get_size(objframes);
00253 nbkg = cpl_frameset_get_size(bkgframes);
00254 if(nobj == nbkg)
00255 hawki_step_subtract_bkg_apply_one_to_one_save
00256 (objframes, bkgframes, parlist, framelist);
00257 else if(nbkg == 1)
00258 hawki_step_subtract_bkg_apply_one_to_all_save
00259 (objframes, bkgframes, parlist, framelist);
00260 else
00261 {
00262 cpl_msg_error(__func__,"Incompatible number of science and background"
00263 " images.");
00264 cpl_msg_error(__func__,"Supply only 1 bkg frame or as many as objects");
00265 cpl_frameset_delete(objframes);
00266 cpl_frameset_delete(bkgframes);
00267 return -1;
00268 }
00269
00270
00271 cpl_frameset_delete(objframes);
00272 cpl_frameset_delete(bkgframes);
00273
00274
00275 if (cpl_error_get_code())
00276 {
00277 cpl_msg_error(__func__,
00278 "HAWK-I pipeline could not recover from previous errors");
00279 return -1 ;
00280 }
00281 else return 0 ;
00282 }
00283
00284
00293
00294 static int hawki_step_subtract_bkg_apply_one_to_one_save
00295 (cpl_frameset * objframes,
00296 cpl_frameset * bkgframes,
00297 cpl_parameterlist * recipe_parlist,
00298 cpl_frameset * recipe_framelist)
00299 {
00300 int iobj;
00301 int nobjs;
00302
00303
00304 cpl_msg_info(__func__,"Using a one to one relation btw objects and bkgs");
00305 nobjs = cpl_frameset_get_size(objframes);
00306 for(iobj = 0; iobj < nobjs; ++iobj)
00307 {
00308 cpl_frame * obj_frame = NULL;
00309 cpl_frame * bkg_frame = NULL;
00310 cpl_imagelist * obj_images = NULL;
00311 cpl_imagelist * bkg_images = NULL;
00312 cpl_frameset * used_frameset;
00313
00314
00315 used_frameset = cpl_frameset_new();
00316
00317
00318 cpl_msg_indent_more();
00319 cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00320 obj_frame = cpl_frameset_get_frame(objframes, iobj);
00321 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00322 if(obj_frame != NULL)
00323 obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00324 if(obj_images == NULL)
00325 {
00326 cpl_msg_indent_less();
00327 cpl_msg_error(__func__, "Error reading obj image") ;
00328 cpl_frameset_delete(used_frameset);
00329 return -1;
00330 }
00331
00332
00333 bkg_frame = cpl_frameset_get_frame(bkgframes, iobj);
00334 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00335 if(bkg_frame != NULL)
00336 bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00337 if(bkg_images == NULL)
00338 {
00339 cpl_msg_error(__func__, "Error reading background image") ;
00340 cpl_msg_indent_less();
00341 cpl_imagelist_delete(obj_images);
00342 cpl_frameset_delete(used_frameset);
00343 return -1;
00344 }
00345
00346
00347 hawki_bkg_imglist_calib(obj_images, bkg_images);
00348
00349
00350 if(hawki_step_subtract_bkg_save(obj_images,
00351 iobj,
00352 used_frameset,
00353 recipe_parlist,
00354 recipe_framelist) != 0)
00355 cpl_msg_warning(__func__,"Some data could not be saved. "
00356 "Check permisions or disk space");
00357
00358
00359 cpl_msg_indent_less();
00360 cpl_imagelist_delete(obj_images);
00361 cpl_imagelist_delete(bkg_images);
00362 cpl_frameset_delete(used_frameset);
00363 }
00364
00365
00366 return 0;
00367 }
00368
00369
00378
00379 static int hawki_step_subtract_bkg_apply_one_to_all_save
00380 (cpl_frameset * objframes,
00381 cpl_frameset * bkgframes,
00382 cpl_parameterlist * recipe_parlist,
00383 cpl_frameset * recipe_framelist)
00384 {
00385 int iobj;
00386 int nobjs;
00387 cpl_frame * bkg_frame;
00388 cpl_imagelist * bkg_images = NULL;
00389
00390
00391 cpl_msg_info(__func__,"Using the same bkg for all the objects");
00392 bkg_frame = cpl_frameset_get_first(bkgframes);
00393 if(bkg_frame != NULL)
00394 bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00395 if(bkg_images == NULL)
00396 {
00397 cpl_msg_error(__func__, "Error reading background image");
00398 return -1;
00399 }
00400
00401
00402 nobjs = cpl_frameset_get_size(objframes);
00403 for(iobj = 0; iobj < nobjs; ++iobj)
00404 {
00405 cpl_frame * obj_frame;
00406 cpl_imagelist * obj_images = NULL;
00407 cpl_frameset * used_frameset;
00408
00409
00410 used_frameset = cpl_frameset_new();
00411
00412
00413 cpl_msg_indent_more();
00414 cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00415 obj_frame = cpl_frameset_get_frame(objframes, iobj);
00416 if(obj_frame != NULL)
00417 obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00418 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00419 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00420 if(obj_images == NULL)
00421 {
00422 cpl_msg_indent_less();
00423 cpl_msg_error(__func__, "Error reading obj image") ;
00424 cpl_frameset_delete(used_frameset);
00425 return -1;
00426 }
00427
00428
00429 hawki_bkg_imglist_calib(obj_images, bkg_images);
00430
00431
00432 hawki_step_subtract_bkg_save(obj_images,
00433 iobj,
00434 used_frameset,
00435 recipe_parlist,
00436 recipe_framelist);
00437
00438
00439 cpl_msg_indent_less();
00440 cpl_imagelist_delete(obj_images);
00441 cpl_frameset_delete(used_frameset);
00442 }
00443
00444
00445 cpl_imagelist_delete(bkg_images);
00446 return 0;
00447 }
00448
00449
00459
00460 static int hawki_step_subtract_bkg_save
00461 (cpl_imagelist * obj_images,
00462 int iserie,
00463 cpl_frameset * used_frameset,
00464 cpl_parameterlist * recipe_parlist,
00465 cpl_frameset * recipe_framelist)
00466 {
00467 const cpl_frame * raw_reference;
00468 cpl_propertylist ** extproplists;
00469 char filename[256] ;
00470 cpl_propertylist * inputlist ;
00471 int ext_nb ;
00472 const char * recipe_name = "hawki_step_subtract_bkg";
00473 int idet;
00474 cpl_errorstate error_prevstate = cpl_errorstate_get();
00475
00476
00477 raw_reference = irplib_frameset_get_first_from_group
00478 (used_frameset, CPL_FRAME_GROUP_RAW);
00479
00480
00481 cpl_msg_indent_more();
00482 cpl_msg_info(__func__, "Creating the keywords list") ;
00483 extproplists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00484 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00485 {
00486
00487 ext_nb=hawki_get_ext_from_detector
00488 (cpl_frame_get_filename(raw_reference), idet+1);
00489
00490
00491 extproplists[idet] = cpl_propertylist_new();
00492
00493
00494 inputlist = cpl_propertylist_load_regexp(
00495 cpl_frame_get_filename(raw_reference), ext_nb,
00496 HAWKI_HEADER_EXT_FORWARD, 0);
00497 cpl_propertylist_append(extproplists[idet], inputlist);
00498 cpl_propertylist_delete(inputlist);
00499 inputlist = cpl_propertylist_load_regexp(
00500 cpl_frame_get_filename(raw_reference), ext_nb,
00501 HAWKI_HEADER_WCS, 0);
00502 cpl_propertylist_append(extproplists[idet], inputlist);
00503 cpl_propertylist_delete(inputlist);
00504 }
00505
00506
00507 snprintf(filename, 256, "hawki_step_subtract_bkg_%03d.fits", iserie+1);
00508 hawki_imagelist_save(recipe_framelist,
00509 recipe_parlist,
00510 used_frameset,
00511 obj_images,
00512 recipe_name,
00513 HAWKI_CALPRO_BKG_SUBTRACTED,
00514 HAWKI_PROTYPE_BKG_SUBTRACTED,
00515 NULL,
00516 (const cpl_propertylist**)extproplists,
00517 filename);
00518
00519
00520 cpl_msg_indent_less();
00521 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00522 {
00523 cpl_propertylist_delete(extproplists[idet]) ;
00524 }
00525 cpl_free(extproplists) ;
00526 if(!cpl_errorstate_is_equal(error_prevstate))
00527 {
00528 cpl_errorstate_set(CPL_ERROR_NONE);
00529 return -1;
00530 }
00531 return 0;
00532 }