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 "omega_recipe.h"
00037 #include "omega_wcscor.h"
00038
00063
00064
00065
00066
00067 static int omega_fringes_flat_create(cpl_plugin *) ;
00068 static int omega_fringes_flat_exec(cpl_plugin *) ;
00069 static int omega_fringes_flat_destroy(cpl_plugin *) ;
00070 static int omega_fringes_flat(cpl_frameset *, cpl_parameterlist *) ;
00071 cpl_image *omega_fringes_flat_process(omega_fits *scifits, cpl_image *bias, cpl_image *flat,
00072 cpl_image *bpm, cpl_parameterlist *pars, int ext);
00073
00074 static int omega_fringes_load_calib(int ext);
00075 static void omega_fringes_init(void);
00076 static void omega_fringes_tidy(int level);
00077
00078
00079
00080 static struct {
00081
00082 int extnum;
00083 int oc;
00084
00085 }omega_fringes_config;
00086
00087 static struct {
00088
00089
00090 cpl_size *labels;
00091 const cpl_frame *mbiasfr;
00092 const cpl_frame *mflatfr;
00093 const cpl_frame *bpmfr;
00094 cpl_frameset *sciset;
00095 omega_fits *scifits;
00096
00097
00098 cpl_image *bpm;
00099 cpl_image *mbias;
00100 cpl_image *mflat;
00101 cpl_image *fringes;
00102
00103 }ps;
00104
00105 #define RECIPE "omega_fringes_flat"
00106
00107
00116
00117 int cpl_plugin_get_info(cpl_pluginlist * list)
00118 {
00119 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
00120 cpl_plugin * plugin = &recipe->interface ;
00121
00122 cpl_plugin_init(plugin,
00123 CPL_PLUGIN_API,
00124 OMEGA_BINARY_VERSION,
00125 CPL_PLUGIN_TYPE_RECIPE,
00126 "omega_fringes_flat",
00127 "OMEGA - Create Fringes Flat.",
00128 "This recipe is used to create a Fringes Flat if the filter \n"
00129 "requires it. The recipe always takes as input a list of at least 3 \n"
00130 "science frames, a master bias and a master flat. In addition to these, \n"
00131 "an optional Bad Pixels Map may be provided.",
00132 "Sandra Castro",
00133 "scastro@eso.org",
00134 omega_get_license(),
00135 omega_fringes_flat_create,
00136 omega_fringes_flat_exec,
00137 omega_fringes_flat_destroy) ;
00138
00139 cpl_pluginlist_append(list, plugin) ;
00140
00141 return 0;
00142 }
00143
00144
00153
00154 static int omega_fringes_flat_create(cpl_plugin * plugin)
00155 {
00156 cpl_recipe * recipe;
00157 cpl_parameter * p ;
00158
00159
00160 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00161 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00162 cpl_func, __LINE__, cpl_error_get_where());
00163 return (int)cpl_error_get_code();
00164 }
00165
00166 if (plugin == NULL) {
00167 cpl_msg_error(cpl_func, "Null plugin");
00168 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00169 }
00170
00171
00172 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00173 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00174 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00175 }
00176
00177
00178 recipe = (cpl_recipe *)plugin;
00179
00180
00181 recipe->parameters = cpl_parameterlist_new() ;
00182 if (recipe->parameters == NULL) {
00183 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00184 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00185 }
00186
00187
00188 p = cpl_parameter_new_value("omega.omega_fringes_flat.ExtensionNumber",
00189 CPL_TYPE_INT,
00190 "FITS extension number to load (1 to 32). (-1 == all)",
00191 "omega_fringes_flat",
00192 -1) ;
00193
00194 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"ext") ;
00195 cpl_parameterlist_append(recipe->parameters, p) ;
00196
00197 p = cpl_parameter_new_range("omega.omega_fringes_flat.OverscanMethod",
00198 CPL_TYPE_INT,
00199 "Overscan Correction Method",
00200 "omega_fringes_flat",
00201 0, 0, 6);
00202 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "oc-meth") ;
00203 cpl_parameterlist_append(recipe->parameters, p) ;
00204
00205
00206 return 0;
00207 }
00208
00209
00215
00216 static int omega_fringes_flat_exec(cpl_plugin * plugin)
00217 {
00218 cpl_recipe * recipe;
00219 int recipe_status;
00220
00221
00222 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00223 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00224 cpl_func, __LINE__, cpl_error_get_where());
00225 return (int)cpl_error_get_code();
00226 }
00227
00228 if (plugin == NULL) {
00229 cpl_msg_error(cpl_func, "Null plugin");
00230 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00231 }
00232
00233
00234 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00235 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00236 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00237 }
00238
00239
00240 recipe = (cpl_recipe *)plugin;
00241
00242
00243 if (recipe->parameters == NULL) {
00244 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00245 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00246 }
00247 if (recipe->frames == NULL) {
00248 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00249 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00250 }
00251
00252
00253 recipe_status = omega_fringes_flat(recipe->frames, recipe->parameters);
00254
00255
00256 if (cpl_dfs_update_product_header(recipe->frames)) {
00257 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00258 }
00259
00260 return recipe_status;
00261 }
00262
00263
00269
00270 static int omega_fringes_flat_destroy(cpl_plugin * plugin)
00271 {
00272 cpl_recipe * recipe;
00273
00274 if (plugin == NULL) {
00275 cpl_msg_error(cpl_func, "Null plugin");
00276 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00277 }
00278
00279
00280 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00281 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00282 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00283 }
00284
00285
00286 recipe = (cpl_recipe *)plugin;
00287
00288 cpl_parameterlist_delete(recipe->parameters) ;
00289
00290 return 0 ;
00291 }
00292
00293
00300
00301 static int omega_fringes_flat(cpl_frameset *set, cpl_parameterlist *pars)
00302 {
00303
00304 int i,j,jst,jfn,isfirst;
00305 cpl_size nlab;
00306 int nraw = 0;
00307 char *outfile = NULL;
00308
00309 cpl_frame *sciframe, *product_frame;
00310 cpl_parameter *par;
00311 cpl_image *image;
00312 cpl_imagelist *ilist;
00313 cpl_propertylist *qclist;
00314 cpl_stats *stats;
00315
00316
00317
00318 if (!pars) {
00319 cpl_msg_error (cpl_func, "Parameters list not found");
00320 return -1;
00321 }
00322
00323 if (cpl_frameset_is_empty(set) == 1) {
00324 cpl_msg_error (cpl_func, "Frameset not found");
00325 return -1;
00326 }
00327
00328
00329 par = cpl_parameterlist_find(pars, "omega.omega_fringes_flat.ExtensionNumber") ;
00330 omega_fringes_config.extnum = cpl_parameter_get_int(par) ;
00331
00332
00333 if (oc_dfs_set_groups(set)) {
00334 cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames") ;
00335 return -1 ;
00336 }
00337
00338
00339 omega_fringes_init();
00340
00341
00342 if ((ps.labels = cpl_frameset_labelise(set,omega_compare_tags,
00343 &nlab)) == NULL) {
00344 cpl_msg_error(cpl_func,"Cannot labelise the input frameset");
00345 omega_fringes_tidy(0);
00346 return -1;
00347 }
00348 if ((ps.sciset = omega_frameset_subgroup(set,ps.labels,nlab,
00349 FRINGES_RAW)) == NULL) {
00350 cpl_msg_error(cpl_func,"Cannot find fringes frames in input frameset");
00351 omega_fringes_tidy(0);
00352 return -1;
00353 }
00354
00355
00356
00357 nraw = cpl_frameset_count_tags(ps.sciset, FRINGES_RAW);
00358 if (nraw < 3) {
00359 cpl_msg_error (cpl_func, "Need at least 3 (%s) frames to run "
00360 "this recipe", FRINGES_RAW);
00361 omega_fringes_tidy(0);
00362 return -1;
00363 }
00364
00365 cpl_msg_info (cpl_func,"There are %d %s frames in frame set",nraw, FRINGES_RAW);
00366
00367
00368
00369 ps.mbiasfr = cpl_frameset_find_const(set, OMEGA_CALIB_BIAS);
00370 if (ps.mbiasfr == NULL) {
00371 cpl_msg_error(cpl_func,"Cannot find %s frame in frame set", OMEGA_CALIB_BIAS);
00372 omega_fringes_tidy(0);
00373 }
00374 cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbiasfr));
00375
00376
00377 ps.mflatfr = cpl_frameset_find_const(set, OMEGA_CALIB_FLAT);
00378 if(ps.mflatfr == NULL) {
00379 cpl_msg_error(cpl_func,"Cannot find %s frame in frame set", OMEGA_CALIB_FLAT);
00380 omega_fringes_tidy(0);
00381 return -1;
00382 }
00383 cpl_msg_info(cpl_func,"Using %s %s", OMEGA_CALIB_FLAT, cpl_frame_get_filename(ps.mflatfr));
00384
00385
00386 ps.bpmfr = cpl_frameset_find (set,OMEGA_CALIB_BPM);
00387 if(ps.bpmfr != NULL)
00388 cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_BPM,cpl_frame_get_filename(ps.bpmfr));
00389
00390
00391 sciframe = cpl_frameset_get_first(ps.sciset);
00392
00393
00394 omega_extensions(sciframe, omega_fringes_config.extnum,&jst,&jfn);
00395 if(omega_fringes_config.extnum == 0){
00396 cpl_msg_error(cpl_func,"Unsupported extension request, %d",omega_fringes_config.extnum);
00397 omega_fringes_tidy(0);
00398 return -1;
00399 }
00400
00401 for (j = jst; j <= jfn; j++) {
00402 isfirst = (j == jst);
00403 cpl_msg_indent_more();
00404 cpl_msg_info(cpl_func,".....Working on extension %d.....",j);
00405 cpl_msg_indent_less();
00406
00407
00408 if(omega_fringes_load_calib(j) != 0){
00409 cpl_msg_error(cpl_func,"Cannot load calibration frame(s)");
00410 freespace(outfile);
00411 omega_fringes_tidy(0);
00412 return -1;
00413 }
00414
00415 ilist = cpl_imagelist_new();
00416
00417 sciframe = cpl_frameset_get_first(ps.sciset);
00418 for(i = 0; i < nraw; i++) {
00419
00420 ps.scifits = omega_fits_load(sciframe, CPL_TYPE_FLOAT, j);
00421
00422
00423 image = omega_fringes_flat_process(ps.scifits,ps.mbias,ps.mflat,ps.bpm, pars, j);
00424 if(image == NULL){
00425 cpl_msg_error(cpl_func,"Cannot reduce image %d of %d",i,nraw);
00426 freespace(outfile);
00427 freeilist(ilist);
00428 omega_fringes_tidy(0);
00429 return -1;
00430 }
00431
00432 cpl_imagelist_set(ilist, image,i);
00433 sciframe = cpl_frameset_get_next(ps.sciset);
00434 freefits(ps.scifits);
00435 ps.scifits = NULL;
00436
00437 }
00438 ps.fringes = omega_fringes_create(ilist,ps.bpm,nraw);
00439 freeilist(ilist);
00440
00441 if(isfirst){
00442 outfile = cpl_sprintf("%s_%s.fits", INSTRUME,FRINGES_PROCATG);
00443 product_frame = omega_product_frame(outfile, FRINGES_PROCATG, CPL_FRAME_TYPE_IMAGE);
00444 }
00445
00446 stats = cpl_stats_new_from_image(ps.fringes, CPL_STATS_ALL);
00447 qclist = cpl_propertylist_new();
00448 cpl_propertylist_append_double(qclist, "HIERARCH ESO QC FRINGESMIN", cpl_stats_get_min(stats));
00449 cpl_propertylist_append_double(qclist, "HIERARCH ESO QC FRINGESMAX", cpl_stats_get_max(stats));
00450 cpl_propertylist_append_double(qclist, "HIERARCH ESO QC FRINGESMEAN", cpl_stats_get_mean(stats));
00451 cpl_propertylist_append_double(qclist, "HIERARCH ESO QC FRINGESDEV", cpl_stats_get_stdev(stats));
00452 cpl_propertylist_append_double(qclist, "HIERARCH ESO QC FRINGESMED", cpl_stats_get_median(stats));
00453 freestats(stats);
00454
00455 cpl_msg_info(cpl_func,"Saving Fringes Flat");
00456
00457 if(omega_save_image(ps.fringes,set,pars,NULL,qclist,CPL_BPP_IEEE_FLOAT,outfile,
00458 RECIPE,product_frame,NULL,isfirst) == -1){
00459 cpl_msg_error(cpl_func,"Cannot save product %s",FRINGES_PROCATG);
00460 freeplist(qclist);
00461 freespace(outfile);
00462 omega_fringes_tidy(0);
00463 return -1;
00464 }
00465
00466 freeplist(qclist);
00467 omega_fringes_tidy(1);
00468 }
00469
00470 freespace(outfile);
00471 omega_fringes_tidy(0);
00472
00473 return 0;
00474 }
00475
00488 cpl_image *omega_fringes_flat_process(omega_fits *scifits, cpl_image *mbias, cpl_image *mflat,
00489 cpl_image *bpm, cpl_parameterlist *pars, int ext)
00490 {
00491 int oc = 0;
00492
00493 cpl_image *fringes;
00494 cpl_parameter *p;
00495 cpl_propertylist *xlist;
00496
00497 if((scifits == NULL) || (mbias == NULL) || (mflat == NULL) ||
00498 (bpm == NULL))
00499 return NULL;
00500
00501
00502 p = cpl_parameterlist_find(pars, "omega.omega_fringes_flat.OverscanMethod") ;
00503 omega_fringes_config.oc = cpl_parameter_get_int(p);
00504
00505
00506 oc = omega_pfits_get_overscan(ps.mbiasfr, ext);
00507 if(oc != omega_fringes_config.oc) {
00508 cpl_msg_warning (cpl_func, "Overscan correction mode for Master Bias (oc = %d) differs from "
00509 "the one used here (oc = %d)", oc, omega_fringes_config.oc);
00510 }
00511
00512
00513 if((fringes = omega_trim_oscan_correct(scifits, omega_fringes_config.oc)) == NULL){
00514 cpl_msg_error(cpl_func,"Unable to trim image");
00515 return NULL;
00516 }
00517
00518
00519 xlist = omega_fits_get_ehu(scifits);
00520 omega_shift_refpix(omega_fits_get_frame(scifits), ext, xlist);
00521
00522
00523 omega_biascor(fringes, mbias);
00524
00525
00526 if(omega_flatcor(fringes, mflat, NULL) != 0){
00527 cpl_msg_error(cpl_func,"Error in flat correction");
00528 freeimage(fringes);
00529 xlist = NULL;
00530 return NULL;
00531 }
00532
00533 xlist = NULL;
00534
00535 return fringes;
00536 }
00537
00538 static int omega_fringes_load_calib(int ext)
00539 {
00540
00541
00542
00543 ps.mbias = cpl_image_load(cpl_frame_get_filename(ps.mbiasfr), CPL_TYPE_FLOAT, 0, ext);
00544 if(ps.mbias == NULL){
00545 cpl_msg_error(cpl_func,"Cannot load Master Bias. %s", cpl_error_get_message());
00546 return -1;
00547 }
00548
00549 ps.mflat = cpl_image_load(cpl_frame_get_filename(ps.mflatfr),CPL_TYPE_FLOAT,0,ext);
00550 if (ps.mflat == NULL){
00551 cpl_msg_error(cpl_func,"Cannot load Master Flat. %s",cpl_error_get_message());
00552 return -1;
00553 }
00554
00555
00556
00557 if(ps.bpmfr != NULL){
00558 ps.bpm = cpl_image_load(cpl_frame_get_filename(ps.bpmfr), CPL_TYPE_INT, 0, ext);
00559 if(ps.bpm == NULL)
00560 cpl_msg_warning(cpl_func, "Cannot load BPM. %s", cpl_error_get_message());
00561
00562 }
00563
00564 return 0;
00565 }
00566
00567
00568 static void omega_fringes_init(void)
00569 {
00570 ps.sciset = NULL;
00571 ps.bpmfr = NULL;
00572 ps.mbiasfr = NULL;
00573 ps.mflatfr = NULL;
00574 ps.scifits = NULL;
00575 ps.labels = NULL;
00576 ps.mbias = NULL;
00577 ps.mflat = NULL;
00578 ps.bpm = NULL;
00579 ps.fringes = NULL;
00580 }
00581
00582
00583
00584
00585 static void omega_fringes_tidy(int level)
00586 {
00587 freeimage(ps.mbias);
00588 freeimage(ps.mflat);
00589 freeimage(ps.bpm);
00590 freeimage(ps.fringes);
00591 if(level == 1)
00592 return;
00593
00594 freeframeset(ps.sciset);
00595 freespace(ps.labels);
00596 freefits(ps.scifits);
00597 }
00598