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()) return -1 ;
00276 else return 0 ;
00277 }
00278
00279
00288
00289 static int hawki_step_subtract_bkg_apply_one_to_one_save
00290 (cpl_frameset * objframes,
00291 cpl_frameset * bkgframes,
00292 cpl_parameterlist * recipe_parlist,
00293 cpl_frameset * recipe_framelist)
00294 {
00295 int iobj;
00296 int nobjs;
00297
00298
00299 cpl_msg_info(__func__,"Using a one to one relation btw objects and bkgs");
00300 nobjs = cpl_frameset_get_size(objframes);
00301 for(iobj = 0; iobj < nobjs; ++iobj)
00302 {
00303 cpl_frame * obj_frame = NULL;
00304 cpl_frame * bkg_frame = NULL;
00305 cpl_imagelist * obj_images = NULL;
00306 cpl_imagelist * bkg_images = NULL;
00307 cpl_frameset * used_frameset;
00308
00309
00310 used_frameset = cpl_frameset_new();
00311
00312
00313 cpl_msg_indent_more();
00314 cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00315 obj_frame = cpl_frameset_get_frame(objframes, iobj);
00316 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00317 if(obj_frame != NULL)
00318 obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00319 if(obj_images == NULL)
00320 {
00321 cpl_msg_indent_less();
00322 cpl_msg_error(__func__, "Error reading obj image") ;
00323 cpl_frameset_delete(used_frameset);
00324 return -1;
00325 }
00326
00327
00328 bkg_frame = cpl_frameset_get_frame(bkgframes, iobj);
00329 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00330 if(bkg_frame != NULL)
00331 bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00332 if(bkg_images == NULL)
00333 {
00334 cpl_msg_error(__func__, "Error reading background image") ;
00335 cpl_msg_indent_less();
00336 cpl_imagelist_delete(obj_images);
00337 cpl_frameset_delete(used_frameset);
00338 return -1;
00339 }
00340
00341
00342 hawki_bkg_imglist_calib(obj_images, bkg_images);
00343
00344
00345 if(hawki_step_subtract_bkg_save(obj_images,
00346 iobj,
00347 used_frameset,
00348 recipe_parlist,
00349 recipe_framelist) != 0)
00350 cpl_msg_warning(__func__,"Some data could not be saved. "
00351 "Check permisions or disk space");
00352
00353
00354 cpl_msg_indent_less();
00355 cpl_imagelist_delete(obj_images);
00356 cpl_imagelist_delete(bkg_images);
00357 cpl_frameset_delete(used_frameset);
00358 }
00359
00360
00361 return 0;
00362 }
00363
00364
00373
00374 static int hawki_step_subtract_bkg_apply_one_to_all_save
00375 (cpl_frameset * objframes,
00376 cpl_frameset * bkgframes,
00377 cpl_parameterlist * recipe_parlist,
00378 cpl_frameset * recipe_framelist)
00379 {
00380 int iobj;
00381 int nobjs;
00382 cpl_frame * bkg_frame;
00383 cpl_imagelist * bkg_images = NULL;
00384
00385
00386 cpl_msg_info(__func__,"Using the same bkg for all the objects");
00387 bkg_frame = cpl_frameset_get_first(bkgframes);
00388 if(bkg_frame != NULL)
00389 bkg_images = hawki_load_frame(bkg_frame, CPL_TYPE_FLOAT);
00390 if(bkg_images == NULL)
00391 {
00392 cpl_msg_error(__func__, "Error reading background image");
00393 return -1;
00394 }
00395
00396
00397 nobjs = cpl_frameset_get_size(objframes);
00398 for(iobj = 0; iobj < nobjs; ++iobj)
00399 {
00400 cpl_frame * obj_frame;
00401 cpl_imagelist * obj_images = NULL;
00402 cpl_frameset * used_frameset;
00403
00404
00405 used_frameset = cpl_frameset_new();
00406
00407
00408 cpl_msg_indent_more();
00409 cpl_msg_info(__func__, "Applying correction to object %d", iobj+1) ;
00410 obj_frame = cpl_frameset_get_frame(objframes, iobj);
00411 if(obj_frame != NULL)
00412 obj_images = hawki_load_frame(obj_frame, CPL_TYPE_FLOAT);
00413 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(obj_frame));
00414 cpl_frameset_insert(used_frameset, cpl_frame_duplicate(bkg_frame));
00415 if(obj_images == NULL)
00416 {
00417 cpl_msg_indent_less();
00418 cpl_msg_error(__func__, "Error reading obj image") ;
00419 cpl_frameset_delete(used_frameset);
00420 return -1;
00421 }
00422
00423
00424 hawki_bkg_imglist_calib(obj_images, bkg_images);
00425
00426
00427 hawki_step_subtract_bkg_save(obj_images,
00428 iobj,
00429 used_frameset,
00430 recipe_parlist,
00431 recipe_framelist);
00432
00433
00434 cpl_msg_indent_less();
00435 cpl_imagelist_delete(obj_images);
00436 cpl_frameset_delete(used_frameset);
00437 }
00438
00439
00440 cpl_imagelist_delete(bkg_images);
00441 return 0;
00442 }
00443
00444
00454
00455 static int hawki_step_subtract_bkg_save
00456 (cpl_imagelist * obj_images,
00457 int iserie,
00458 cpl_frameset * used_frameset,
00459 cpl_parameterlist * recipe_parlist,
00460 cpl_frameset * recipe_framelist)
00461 {
00462 const cpl_frame * raw_reference;
00463 cpl_propertylist ** extproplists;
00464 char filename[256] ;
00465 cpl_propertylist * inputlist ;
00466 int ext_nb ;
00467 const char * recipe_name = "hawki_step_subtract_bkg";
00468 int idet;
00469 cpl_errorstate error_prevstate = cpl_errorstate_get();
00470
00471
00472 raw_reference = irplib_frameset_get_first_from_group
00473 (used_frameset, CPL_FRAME_GROUP_RAW);
00474
00475
00476 cpl_msg_indent_more();
00477 cpl_msg_info(__func__, "Creating the keywords list") ;
00478 extproplists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00479 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00480 {
00481
00482 ext_nb=hawki_get_ext_from_detector
00483 (cpl_frame_get_filename(raw_reference), idet+1);
00484
00485
00486 extproplists[idet] = cpl_propertylist_new();
00487
00488
00489 inputlist = cpl_propertylist_load_regexp(
00490 cpl_frame_get_filename(raw_reference), ext_nb,
00491 HAWKI_HEADER_EXT_FORWARD, 0);
00492 cpl_propertylist_append(extproplists[idet], inputlist);
00493 cpl_propertylist_delete(inputlist);
00494 inputlist = cpl_propertylist_load_regexp(
00495 cpl_frame_get_filename(raw_reference), ext_nb,
00496 HAWKI_HEADER_WCS, 0);
00497 cpl_propertylist_append(extproplists[idet], inputlist);
00498 cpl_propertylist_delete(inputlist);
00499 }
00500
00501
00502 snprintf(filename, 256, "hawki_step_subtract_bkg_%03d.fits", iserie+1);
00503 hawki_imagelist_save(recipe_framelist,
00504 recipe_parlist,
00505 used_frameset,
00506 obj_images,
00507 recipe_name,
00508 HAWKI_CALPRO_BKG_SUBTRACTED,
00509 HAWKI_PROTYPE_BKG_SUBTRACTED,
00510 NULL,
00511 (const cpl_propertylist**)extproplists,
00512 filename);
00513
00514
00515 cpl_msg_indent_less();
00516 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00517 {
00518 cpl_propertylist_delete(extproplists[idet]) ;
00519 }
00520 cpl_free(extproplists) ;
00521 if(!cpl_errorstate_is_equal(error_prevstate))
00522 {
00523 cpl_errorstate_set(CPL_ERROR_NONE);
00524 return -1;
00525 }
00526 return 0;
00527 }