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 #include "omega_recipe.h"
00034 #include "omega_background.h"
00035
00036
00067
00068
00069
00070
00071 static int omega_mdome_create(cpl_plugin *);
00072 static int omega_mdome_exec(cpl_plugin *);
00073 static int omega_mdome_destroy(cpl_plugin *);
00074 static int omega_mdome(cpl_frameset *, cpl_parameterlist *);
00075
00076
00077
00078
00079
00080 int omega_mdome_combine(cpl_parameterlist *pars, int xn);
00081 static void omega_mdome_init(void);
00082 static void omega_mdome_tidy(void);
00083
00084
00085
00086
00087
00088 static struct {
00089
00090 int extnum;
00091 int oc;
00092 int paf;
00093 double sigma;
00094 double lthre;
00095 double hthre;
00096
00097
00098 int CountColdPixels;
00099 double Mean;
00100 double Median;
00101 double Stdev;
00102 double RawMin;
00103 double RawMax;
00104 double RawMean;
00105 double RawMedian;
00106 double RawStdev;
00107
00108 }omega_mdome_config;
00109
00110
00111 static struct {
00112
00113 cpl_size *labels;
00114 const cpl_frame *mbframe;
00115 const cpl_frame *cframe;
00116 const cpl_frame *hframe;
00117 cpl_frameset *domelist;
00118 cpl_stats *stats;
00119 cpl_propertylist *ph;
00120 cpl_propertylist *eh;
00121 omega_fits *firstdome;
00122
00123
00124 cpl_image *mdome;
00125 cpl_image *cpixels;
00126
00127 }ps;
00128
00129
00130
00131
00132
00133
00134 #define RECIPE "omega_mdome"
00135
00136 static int isfirst;
00137
00138
00139
00147
00148 int cpl_plugin_get_info(cpl_pluginlist * list)
00149 {
00150 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
00151 cpl_plugin * plugin = &recipe->interface ;
00152
00153 cpl_plugin_init(plugin,
00154 CPL_PLUGIN_API,
00155 OMEGA_BINARY_VERSION,
00156 CPL_PLUGIN_TYPE_RECIPE,
00157 "omega_mdome",
00158 "OMEGA - Create Master Dome Flat for each chip (Calfile 542).",
00159 "This recipe is used to derive a valid domeflat frame (Calfile 542) \n"
00160 "for one particular chip and filter. The recipe always takes as input \n"
00161 "a list of raw domeflat frames, a master bias and a measurement of the \n"
00162 "gain of the chip under consideration. In addition, a hot pixel map \n"
00163 "(Calfile 522) and a cold pixel map (Calfile 535) can be provided. Optionally, \n"
00164 "an overscan correction mode can be set. The default is to apply no overscan \n"
00165 "correction.\n\n"
00166 "The raw dome flats are trimmed and overscan corrected. The data are then \n"
00167 "normalized, averaged, and the result is normalized again. Image statisics \n"
00168 "are computed for the resulting frame.",
00169 "Sandra Castro",
00170 "scastro@eso.org",
00171 omega_get_license(),
00172 omega_mdome_create,
00173 omega_mdome_exec,
00174 omega_mdome_destroy) ;
00175
00176 cpl_pluginlist_append(list, plugin) ;
00177
00178 return 0;
00179 }
00180
00181
00190
00191 static int omega_mdome_create(cpl_plugin * plugin)
00192 {
00193 cpl_recipe * recipe;
00194 cpl_parameter * p ;
00195 char *path = NULL;
00196
00197
00198 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00199 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00200 cpl_func, __LINE__, cpl_error_get_where());
00201 return (int)cpl_error_get_code();
00202 }
00203
00204 if (plugin == NULL) {
00205 cpl_msg_error(cpl_func, "Null plugin");
00206 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00207 }
00208
00209
00210 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00211 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00212 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00213 }
00214
00215
00216 recipe = (cpl_recipe *)plugin;
00217
00218
00219 recipe->parameters = cpl_parameterlist_new() ;
00220
00221 if (recipe->parameters == NULL) {
00222 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00223 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00224 }
00225
00226
00227
00228
00229 p = cpl_parameter_new_value("omega.omega_mdome.ExtensionNumber",
00230 CPL_TYPE_INT,
00231 "FITS extension number to load (1 to 32). (-1 = all)",
00232 "omega_mdome",
00233 -1) ;
00234
00235 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"ext") ;
00236 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00237 cpl_parameterlist_append(recipe->parameters, p) ;
00238
00239
00240 p = cpl_parameter_new_range("omega.omega_mdome.OverscanMethod",
00241 CPL_TYPE_INT,
00242 "Overscan Correction Method",
00243 "omega_mdome",
00244 0, 0, 6);
00245
00246 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"oc-meth") ;
00247 cpl_parameterlist_append(recipe->parameters, p) ;
00248
00249 p = cpl_parameter_new_value("omega.omega_mdome.PAF",
00250 CPL_TYPE_BOOL,
00251 "Boolean value to create PAF files. 1(Yes), 0(No)",
00252 "omega_mdome",
00253 1) ;
00254
00255 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "paf") ;
00256 cpl_parameterlist_append(recipe->parameters, p) ;
00257
00258 p = cpl_parameter_new_value("omega.omega_mdome.SigmaClip",
00259 CPL_TYPE_DOUBLE,
00260 "Sigma Clipping Threshold",
00261 "omega_mdome",
00262 3.0) ;
00263
00264 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sig-clip") ;
00265 cpl_parameterlist_append(recipe->parameters, p) ;
00266
00267
00268
00269 p = cpl_parameter_new_range("omega.omega_mdome.LowThre",
00270 CPL_TYPE_DOUBLE,
00271 "Low flagging threshold for cold pixels map",
00272 "omega_coldpixels",
00273 0.93, 0.90, 1.00) ;
00274
00275 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "low") ;
00276 cpl_parameterlist_append(recipe->parameters, p) ;
00277
00278 p = cpl_parameter_new_range("omega.omega_mdome.HighThre",
00279 CPL_TYPE_DOUBLE,
00280 "High flagging threshold for cold pixels map",
00281 "omega_coldpixels",
00282 1.07, 1.00, 1.10) ;
00283
00284 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"high") ;
00285 cpl_parameterlist_append(recipe->parameters, p) ;
00286
00287 p = cpl_parameter_new_range("omega.omega_mdome.BackSize",
00288 CPL_TYPE_INT,
00289 "Sextractor background mesh size for cold pixels map",
00290 "omega_coldpixels",
00291 32, 4, 256) ;
00292
00293 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"backsize") ;
00294 cpl_parameterlist_append(recipe->parameters, p) ;
00295
00296
00297 path = cpl_sprintf("%s", OMEGA_BIN_PATH);
00298 p = cpl_parameter_new_value("omega.omega_mdome.BinPath",
00299 CPL_TYPE_STRING,
00300 "Path to any external executable program.",
00301 "omega.BinPath",
00302 path);
00303
00304 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "bin-path");
00305 cpl_parameterlist_append(recipe->parameters, p);
00306 cpl_free(path);
00307
00308
00309 path = cpl_sprintf("%s/omega.sex", OMEGA_CONFIG_PATH);
00310 p = cpl_parameter_new_value("omega.omega_mdome.SexConfig",
00311 CPL_TYPE_STRING,
00312 "Path to Sextractor config file.",
00313 "omega.Sextractor",
00314 path);
00315
00316 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-config");
00317 cpl_parameterlist_append(recipe->parameters, p);
00318 cpl_free(path);
00319
00320 path = cpl_sprintf("%s/omega.conv", OMEGA_CONFIG_PATH);
00321 p = cpl_parameter_new_value("omega.omega_mdome.SexConv",
00322 CPL_TYPE_STRING,
00323 "Path to Sextractor convolution mask file.",
00324 "omega.Sextractor",
00325 path);
00326
00327 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"sex-conv");
00328 cpl_parameterlist_append(recipe->parameters, p);
00329 cpl_free(path);
00330
00331
00332 path = cpl_sprintf("%s/omega.param", OMEGA_CONFIG_PATH);
00333 p = cpl_parameter_new_value("omega.omega_mdome.SexParam",
00334 CPL_TYPE_STRING,
00335 "Path to Sextractor parameters file.",
00336 "omega.Sextractor",
00337 path);
00338
00339 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-param");
00340 cpl_parameterlist_append(recipe->parameters, p);
00341 cpl_free(path);
00342
00343 path = cpl_sprintf("%s/omega.nnw", OMEGA_CONFIG_PATH);
00344 p = cpl_parameter_new_value("omega.omega_mdome.SexNnw",
00345 CPL_TYPE_STRING,
00346 "Path to Sextractor neural network config file.",
00347 "omega.Sextractor",
00348 path);
00349
00350 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"sex-nnw");
00351 cpl_parameterlist_append(recipe->parameters, p);
00352 cpl_free(path);
00353
00354 p = cpl_parameter_new_value("omega.omega_mdome.BackThreshold",
00355 CPL_TYPE_DOUBLE,
00356 "Detection threshold for background in image (Sextractor DETECT_THRESH)",
00357 "omega_Sextractor",
00358 1000.) ;
00359
00360 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "backthre") ;
00361 cpl_parameterlist_append(recipe->parameters, p) ;
00362
00363
00364
00365 return 0;
00366 }
00367
00368
00374
00375 static int omega_mdome_exec(cpl_plugin * plugin)
00376 {
00377 cpl_recipe * recipe;
00378 int recipe_status;
00379
00380
00381
00382 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00383 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00384 cpl_func, __LINE__, cpl_error_get_where());
00385 return (int)cpl_error_get_code();
00386 }
00387
00388 if (plugin == NULL) {
00389 cpl_msg_error(cpl_func, "Null plugin");
00390 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00391 }
00392
00393
00394 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00395 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00396 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00397 }
00398
00399
00400 recipe = (cpl_recipe *)plugin;
00401
00402
00403 if (recipe->parameters == NULL) {
00404 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00405 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00406 }
00407 if (recipe->frames == NULL) {
00408 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00409 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00410 }
00411
00412
00413 recipe_status = omega_mdome(recipe->frames, recipe->parameters);
00414
00415
00416 if (cpl_dfs_update_product_header(recipe->frames)) {
00417 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00418 }
00419
00420
00421
00422
00423
00424
00425
00426 return recipe_status;
00427
00428 }
00429
00430
00436
00437 static int omega_mdome_destroy(cpl_plugin * plugin)
00438 {
00439 cpl_recipe *recipe;
00440
00441 if (plugin == NULL) {
00442 cpl_msg_error(cpl_func, "Null plugin");
00443 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00444 }
00445
00446
00447 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00448 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00449 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00450 }
00451
00452
00453 recipe = (cpl_recipe *)plugin;
00454
00455 cpl_parameterlist_delete(recipe->parameters);
00456
00457 return 0 ;
00458 }
00459
00460
00467
00468 static int omega_mdome(cpl_frameset *set, cpl_parameterlist *pars)
00469 {
00470
00471 int j,jst,jfn;
00472 cpl_size nlab;
00473 int nflats = 0;
00474 int oscan1 = 0;
00475 int status = 1;
00476
00477 float bias = BIAS;
00478
00479 char *outcpm = NULL;
00480 char *outmdome = NULL;
00481 const char *_id = "omega_mdome";
00482 cpl_frame *firstframe = NULL;
00483 cpl_frame *prframe_cpm = NULL;
00484 cpl_frame *prframe_mdome = NULL;
00485 const cpl_frame *refframe = NULL;
00486 cpl_parameter *par = NULL;
00487 cpl_propertylist *qclist = NULL;
00488 cpl_propertylist *alist = NULL;
00489 cpl_stats *diffstats = NULL;
00490
00491
00492
00493
00494 if (!pars) {
00495 cpl_msg_error (_id, "Parameters list not found");
00496 return -1;
00497 }
00498
00499 if (cpl_frameset_is_empty(set) == 1) {
00500 cpl_msg_error (_id, "Frameset not found");
00501 return -1;
00502 }
00503
00504
00505 par = cpl_parameterlist_find(pars, "omega.omega_mdome.ExtensionNumber") ;
00506 omega_mdome_config.extnum = cpl_parameter_get_int(par) ;
00507
00508 par = cpl_parameterlist_find(pars, "omega.omega_mdome.OverscanMethod") ;
00509 omega_mdome_config.oc = cpl_parameter_get_int(par) ;
00510
00511 par = cpl_parameterlist_find(pars, "omega.omega_mdome.SigmaClip") ;
00512 omega_mdome_config.sigma = cpl_parameter_get_double(par) ;
00513
00514 par = cpl_parameterlist_find(pars, "omega.omega_mdome.LowThre") ;
00515 omega_mdome_config.lthre = cpl_parameter_get_double(par) ;
00516
00517 par = cpl_parameterlist_find(pars, "omega.omega_mdome.HighThre") ;
00518 omega_mdome_config.hthre = cpl_parameter_get_double(par) ;
00519
00520 par = cpl_parameterlist_find(pars, "omega.omega_mdome.PAF") ;
00521 omega_mdome_config.paf = cpl_parameter_get_bool(par) ;
00522
00523
00524 if (oc_dfs_set_groups(set)) {
00525 cpl_msg_error(_id, "Cannot identify RAW and CALIB frames") ;
00526 return -1 ;
00527 }
00528
00529
00530 omega_mdome_init();
00531
00532
00533 if ((ps.labels = cpl_frameset_labelise(set,omega_compare_tags,
00534 &nlab)) == NULL) {
00535 cpl_msg_error(_id,"Cannot labelise the input frameset");
00536 omega_mdome_tidy();
00537 return -1;
00538 }
00539 if ((ps.domelist = omega_frameset_subgroup(set,ps.labels,nlab,
00540 MDOME_RAW)) == NULL) {
00541 cpl_msg_error(_id,"Cannot find dome frames in input frameset");
00542 omega_mdome_tidy();
00543 return -1;
00544 }
00545
00546
00547 nflats = cpl_frameset_count_tags(ps.domelist, MDOME_RAW);
00548 if (nflats < 2) {
00549 cpl_msg_error (_id, "Need at least 2 (%s) frames to run "
00550 "this recipe", MDOME_RAW);
00551 omega_mdome_tidy();
00552 return -1;
00553 }
00554
00555 cpl_msg_info (_id,"There are %d %s in frame set",nflats, MDOME_RAW);
00556
00557
00558
00559 ps.mbframe = cpl_frameset_find_const(set, OMEGA_CALIB_BIAS);
00560 if (ps.mbframe == NULL) {
00561 cpl_msg_info(_id,"A Master Bias is not present in frame set. Using default value %f", bias);
00562 }
00563 else{
00564 cpl_msg_info(_id,"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbframe));
00565 }
00566
00567
00568 ps.cframe = cpl_frameset_find_const(set, OMEGA_CALIB_CPM);
00569 if(ps.cframe != NULL){
00570 cpl_msg_info(_id,"Using %s %s",OMEGA_CALIB_CPM, cpl_frame_get_filename(ps.cframe));
00571 }
00572
00573
00574 ps.hframe = cpl_frameset_find_const(set, OMEGA_CALIB_HPM);
00575 if(ps.hframe != NULL){
00576 cpl_msg_info(_id,"Using %s %s",OMEGA_CALIB_HPM, cpl_frame_get_filename(ps.hframe));
00577 }
00578
00579
00580 refframe = cpl_frameset_find_const(set, REFDOME);
00581 if (refframe != NULL)
00582 cpl_msg_info(cpl_func,"Using %s for comparison",cpl_frame_get_filename(refframe));
00583
00584
00585 firstframe = cpl_frameset_get_first(ps.domelist);
00586
00587
00588 omega_exten_range(omega_mdome_config.extnum,&jst,&jfn);
00589 if(omega_mdome_config.extnum == 0){
00590 cpl_msg_error(cpl_func,"Unsupported extension request, %d",omega_mdome_config.extnum);
00591 omega_mdome_tidy();
00592 return -1;
00593 }
00594
00595
00596 if (omega_pfits_check_instrume(firstframe) == 1)
00597 sprintf(INSTRUME,"wfi");
00598
00599
00600 if(omega_pfits_check_instrume(firstframe) == 1 &&
00601 omega_mdome_config.extnum == 0 && jfn == 32)
00602 jfn = 8;
00603
00604 for (j = jst; j <= jfn; j++) {
00605
00606 isfirst = (j == jst);
00607 cpl_msg_info(_id,"Beginning work on extension %d",j);
00608 omega_mdome_config.CountColdPixels = 0;
00609 omega_mdome_config.Mean = 0.0;
00610 omega_mdome_config.Median = 0.0;
00611 omega_mdome_config.Stdev = 0.0;
00612 omega_mdome_config.RawMin = 0.0;
00613 omega_mdome_config.RawMax = 0.0;
00614 omega_mdome_config.RawMean = 0.0;
00615 omega_mdome_config.RawMedian = 0.0;
00616 omega_mdome_config.RawStdev = 0.0;
00617
00618
00619 if(ps.mbframe != NULL){
00620 oscan1 = omega_pfits_get_overscan(ps.mbframe, j);
00621 if(oscan1 != omega_mdome_config.oc) {
00622 cpl_msg_warning (_id, "Overscan correction mode for Master Bias (oc = %d) differs from "
00623 "the one used here (oc = %d)", oscan1, omega_mdome_config.oc);
00624 }
00625 }
00626
00627
00628
00629 ps.firstdome = omega_fits_load(firstframe,CPL_TYPE_FLOAT,j);
00630 ps.eh = omega_fits_get_ehu(ps.firstdome);
00631
00632
00633 status = omega_mdome_combine(pars, j);
00634 if(status == 1){
00635 cpl_msg_warning(_id, "Image detector is not live");
00636
00637 freefits(ps.firstdome);
00638 ps.eh = NULL;
00639 continue;
00640 }
00641 else if(status == -1){
00642 cpl_msg_error(_id,"Cannot combine images");
00643 freespace(outcpm);
00644 freespace(outmdome);
00645 omega_mdome_tidy();
00646 return -1;
00647 }
00648
00649 qclist = cpl_propertylist_new();
00650
00651
00652 if(refframe != NULL){
00653 if(omega_compare_reference(ps.mdome,refframe,j,&diffstats) == -1){
00654 cpl_msg_warning(cpl_func,"Cannot compare with reference frame");
00655 }
00656 else{
00657 cpl_propertylist_append_double(qclist,"ESO QC DIFF REFDOME MEAN",
00658 cpl_stats_get_mean(diffstats));
00659 cpl_propertylist_set_comment(qclist,"ESO QC DIFF REFDOME MEAN",
00660 "Mean of difference with reference");
00661 cpl_propertylist_append_double(qclist,"ESO QC DIFF REFDOME MEDIAN",
00662 cpl_stats_get_median(diffstats));
00663 cpl_propertylist_set_comment(qclist,"ESO QC DIFF REFDOME MEDIAN",
00664 "Median of difference with reference");
00665 cpl_propertylist_append_double(qclist,"ESO QC DIFF REFDOME STDEV",
00666 cpl_stats_get_stdev(diffstats));
00667 cpl_propertylist_set_comment(qclist,"ESO QC DIFF REFDOME STDEV",
00668 "Stdev of difference with reference");
00669
00670 freestats(diffstats);
00671 }
00672 }
00673
00674
00675
00676
00677 cpl_propertylist_append_double(qclist, "ESO QC MASTER DOME MEAN",
00678 omega_mdome_config.Mean) ;
00679 cpl_propertylist_set_comment (qclist, "ESO QC MASTER DOME MEAN",
00680 "Mean of master dome flat");
00681
00682 cpl_propertylist_append_double(qclist, "ESO QC MASTER DOME MEDIAN",
00683 omega_mdome_config.Median) ;
00684 cpl_propertylist_set_comment (qclist, "ESO QC MASTER DOME MEDIAN",
00685 "Median of master dome flat");
00686
00687 cpl_propertylist_append_double(qclist, "ESO QC MASTER DOME STDEV",
00688 omega_mdome_config.Stdev) ;
00689 cpl_propertylist_set_comment (qclist, "ESO QC MASTER DOME STDEV",
00690 "Std Deviation of master dome flat");
00691
00692 cpl_propertylist_append_double(qclist, "ESO QC RAW DOME MIN",
00693 omega_mdome_config.RawMin);
00694 cpl_propertylist_append_double(qclist, "ESO QC RAW DOME MAX",
00695 omega_mdome_config.RawMax);
00696 cpl_propertylist_append_double(qclist, "ESO QC RAW DOME MEAN",
00697 omega_mdome_config.RawMean);
00698 cpl_propertylist_append_double(qclist, "ESO QC RAW DOME MEDIAN",
00699 omega_mdome_config.RawMedian);
00700 cpl_propertylist_append_double(qclist, "ESO QC RAW DOME STDEV",
00701 omega_mdome_config.RawStdev);
00702
00703 cpl_propertylist_set_comment(qclist, "ESO QC RAW DOME MIN",
00704 "median value of the raw dome flat having the lowest flux");
00705 cpl_propertylist_set_comment(qclist, "ESO QC RAW DOME MAX",
00706 "median value of the raw dome flat having the highest flux");
00707 cpl_propertylist_set_comment(qclist, "ESO QC RAW DOME MEAN",
00708 "mean value of all input raw dome flats (ADU)");
00709 cpl_propertylist_set_comment(qclist, "ESO QC RAW DOME MEDIAN",
00710 "median value of all input raw dome flats (ADU)");
00711 cpl_propertylist_set_comment(qclist, "ESO QC RAW DOME STDEV",
00712 "standard deviation of all input raw dome flats (ADU)");
00713
00714 if(isfirst){
00715 outmdome = cpl_sprintf("%s_%s.fits", INSTRUME,MDOME_PROCATG);
00716 prframe_mdome = omega_product_frame(outmdome, MDOME_PROCATG, CPL_FRAME_TYPE_IMAGE);
00717 }
00718
00719 alist = cpl_propertylist_new();
00720 cpl_propertylist_append_string(alist, "EXTNAME",
00721 cpl_propertylist_get_string(ps.eh, "EXTNAME"));
00722 cpl_propertylist_set_comment(alist,"EXTNAME", "Extension name");
00723
00724
00725 cpl_propertylist_update_int(alist, "ESO DRS OVERSCAN METHOD", omega_mdome_config.oc);
00726 cpl_propertylist_set_comment(alist, "ESO DRS OVERSCAN METHOD", "overscan correction method");
00727
00728 if(omega_save_image(ps.mdome,set,pars,alist,qclist,CPL_BPP_IEEE_FLOAT,outmdome,
00729 RECIPE,prframe_mdome,NULL,isfirst) == -1){
00730 cpl_msg_error(_id,"Cannot save product %s", MDOME_PROCATG);
00731 freeplist(qclist);
00732 freeplist(alist);
00733 freespace(outcpm);
00734 freespace(outmdome);
00735 omega_mdome_tidy();
00736 return -1;;
00737 }
00738 freeplist(qclist);
00739 freeimage(ps.mdome);
00740
00741
00742 qclist = cpl_propertylist_new();
00743 cpl_propertylist_append_int(qclist,"ESO QC NUMBER COLD PIXELS",
00744 omega_mdome_config.CountColdPixels);
00745
00746 cpl_propertylist_set_comment(qclist,"ESO QC NUMBER COLD PIXELS",
00747 "Number of cold pixels");
00748 if(isfirst){
00749 outcpm = cpl_sprintf("%s_%s.fits", INSTRUME,CPM_PROCATG);
00750 prframe_cpm = omega_product_frame(outcpm, CPM_PROCATG, CPL_FRAME_TYPE_IMAGE);
00751 }
00752
00753 if(omega_save_image(ps.cpixels,set,pars,alist,qclist,CPL_BPP_16_SIGNED,outcpm,
00754 RECIPE,prframe_cpm,NULL,isfirst) == -1){
00755 cpl_msg_error(_id,"Cannot save product %s", CPM_PROCATG);
00756 freeplist(qclist);
00757 freeplist(alist);
00758 freespace(outcpm);
00759 freespace(outmdome);
00760 omega_mdome_tidy();
00761 return -1;;
00762 }
00763
00764 freeplist(qclist);
00765 freeplist(alist);
00766 freeimage(ps.cpixels);
00767 freefits(ps.firstdome);
00768 ps.firstdome = NULL;
00769 ps.eh = NULL;
00770
00771 }
00772
00773
00774
00775 freespace(outcpm);
00776 freespace(outmdome);
00777 omega_mdome_tidy();
00778
00779 return 0;
00780
00781 }
00782
00783
00794
00795 int omega_mdome_combine(cpl_parameterlist *pars, int xn)
00796 {
00797
00798 int i,nflats,live, naxis1, naxis2;
00799 float bias = BIAS;
00800 double gain = 0.0;
00801 double median = 1.0;
00802 double threshold = 0.0;
00803 double *data_scales = NULL;
00804 const char *_id = "omega_mdome_combine";
00805 const char *mdome_name = "omega_mdome_temp.fits";
00806 const char *backname = "omega_mdome_background.fits";
00807
00808 cpl_error_code code;
00809 const cpl_frame *domefr = NULL;
00810 cpl_vector *scales = NULL;
00811 cpl_vector *median_vector = NULL;
00812 cpl_image *mbias = NULL;
00813 cpl_image *trim_raw = NULL, *back_image = NULL, *dev = NULL;
00814 cpl_image *good_float = NULL, *good_int = NULL, *median_all = NULL;
00815 cpl_image *sum_data = NULL, *sum_good = NULL, *new_image = NULL;
00816 cpl_image *norm_image = NULL;
00817 cpl_imagelist *ilist = NULL;
00818 cpl_mask *good = NULL, *bpm_map = NULL;
00819 cpl_mask *pixelmap = NULL;
00820
00821 nflats = cpl_frameset_get_size(ps.domelist);
00822 domefr = cpl_frameset_get_first_const(ps.domelist);
00823
00824
00825 omega_pfits_get_detlive(ps.eh,&live);
00826 if (! live) {
00827 return 1;
00828 }
00829
00830
00831 if(ps.mbframe != NULL){
00832 mbias = cpl_image_load(cpl_frame_get_filename(ps.mbframe), CPL_TYPE_FLOAT,0,xn);
00833 if (mbias == NULL) {
00834 cpl_msg_warning(_id,"Cannot load image %s", OMEGA_CALIB_BIAS);
00835 }
00836 }
00837
00838
00839 bpm_map = makebpm(ps.hframe, ps.cframe, xn);
00840
00841
00842 scales = cpl_vector_new (nflats);
00843 median_vector = cpl_vector_new (nflats);
00844 cpl_vector_fill(median_vector,0.0);
00845
00846 data_scales = cpl_vector_get_data(scales);
00847
00848 ilist = cpl_imagelist_new();
00849 cpl_msg_info (_id,"Doing trim and overscan correction on images");
00850
00851
00852 for (i=0; i< nflats; i++){
00853
00854 trim_raw = TrimOscanCorrect(domefr, omega_mdome_config.oc, xn);
00855 if(trim_raw == NULL){
00856 freevector(scales);
00857 freevector(median_vector);
00858 freeilist(ilist);
00859 return -1;
00860 }
00861
00862 if (mbias != NULL)
00863 cpl_image_subtract(trim_raw, mbias);
00864 else
00865 cpl_image_subtract_scalar(trim_raw, bias);
00866
00867 if (bpm_map == NULL) {
00868 ps.stats = cpl_stats_new_from_image(trim_raw, CPL_STATS_ALL);
00869 median = cpl_stats_get_median(ps.stats);
00870 }
00871 else {
00872 cpl_image_reject_from_mask(trim_raw, bpm_map);
00873 ps.stats = cpl_stats_new_from_image(trim_raw, CPL_STATS_ALL);
00874 median = cpl_stats_get_median(ps.stats);
00875 }
00876
00877 cpl_vector_set(median_vector, i, median);
00878
00879 data_scales[i] = (double)1.0/median;
00880
00881 cpl_image_divide_scalar(trim_raw, median);
00882
00883 cpl_imagelist_set(ilist, trim_raw, i);
00884
00885
00886 domefr = cpl_frameset_get_next_const(ps.domelist);
00887 if (domefr == NULL)
00888 break;
00889
00890 freestats(ps.stats);
00891
00892 }
00893
00894 omega_mdome_config.RawMin = cpl_vector_get_min(median_vector);
00895 omega_mdome_config.RawMax = cpl_vector_get_max(median_vector);
00896 omega_mdome_config.RawMean = cpl_vector_get_mean(median_vector);
00897 omega_mdome_config.RawMedian = cpl_vector_get_median(median_vector);
00898 omega_mdome_config.RawStdev = cpl_vector_get_stdev(median_vector);
00899
00900 freevector(median_vector);
00901
00902 freestats(ps.stats);
00903 freeimage(mbias);
00904 freemask(bpm_map);
00905
00906 if (ilist == NULL) {
00907 cpl_msg_error(_id,"Error in image list <%s>",cpl_error_get_message());
00908 freevector(scales);
00909 freeimage(trim_raw);
00910 return -1;
00911 }
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923 omega_pfits_get_conad(ps.eh, &gain);
00924
00925 if (gain < 0.1){
00926 omega_pfits_get_gain(ps.eh, &gain);
00927
00928 if(gain < 0.1) {
00929 cpl_msg_info(_id,"Using default value of gain");
00930 gain = 2.0;
00931 }
00932 }
00933 else {
00934 cpl_msg_info(_id,"Gain value from image is %g", gain);
00935 }
00936
00937 cpl_msg_info (_id,"Computing the median of all images...");
00938 median_all = cpl_imagelist_collapse_median_create(ilist);
00939
00940 if (median_all == NULL) {
00941 cpl_msg_error (_id,"Cannot take median of list <%s>",cpl_error_get_message());
00942 freevector(scales);
00943 freeilist(ilist);
00944 return -1;
00945 }
00946
00947 naxis1 = cpl_image_get_size_x(median_all);
00948 naxis2 = cpl_image_get_size_y(median_all);
00949
00950 sum_data = cpl_image_new(naxis1,naxis2,CPL_TYPE_FLOAT);
00951 sum_good = cpl_image_new(naxis1,naxis2,CPL_TYPE_FLOAT);
00952
00953
00954 for (i=0; i< nflats; i++){
00955
00956 trim_raw = cpl_imagelist_get(ilist, i);
00957 new_image = cpl_image_duplicate(trim_raw);
00958
00959
00960
00961
00962
00963 cpl_image_threshold(trim_raw, 0, FLT_MAX, 0, 2e20);
00964
00965 dev = cpl_image_subtract_create(median_all, trim_raw);
00966
00967 code = cpl_image_power(trim_raw, 0.5);
00968 if(code != CPL_ERROR_NONE) {
00969 cpl_msg_error(_id,"Error in SQRT operation <%s>",cpl_error_get_message());
00970 freevector(scales);
00971 freeilist(ilist);
00972 freeimage(median_all);
00973 freeimage(sum_data);
00974 freeimage(sum_good);
00975 freeimage(new_image);
00976 freeimage(dev);
00977 return -1;
00978 }
00979
00980 cpl_image_divide(dev, trim_raw);
00981
00982 threshold = omega_mdome_config.sigma * (sqrt(gain * data_scales[i]));
00983
00984 good = cpl_mask_threshold_image_create(dev, -threshold, threshold);
00985
00986 freeimage(dev);
00987 good_int = cpl_image_new_from_mask(good) ;
00988 freemask(good) ;
00989
00990 good_float = cpl_image_cast(good_int, CPL_TYPE_FLOAT);
00991 freeimage(good_int);
00992
00993 cpl_image_multiply(new_image, good_float);
00994 cpl_image_add(sum_data, new_image);
00995
00996 cpl_image_add(sum_good, good_float);
00997
00998 freeimage(new_image);
00999 freeimage(good_float);
01000
01001 }
01002
01003 freeimage(median_all);
01004 freevector(scales);
01005
01006
01007 ps.mdome = cpl_image_divide_create(sum_data, sum_good);
01008 if (ps.mdome == NULL) {
01009 cpl_msg_error(_id,"Error in division %s <%s>",MDOME_PROCATG, cpl_error_get_message());
01010 freeilist(ilist);
01011 freeimage(sum_data);
01012 freeimage(sum_good);
01013 return -1;
01014 }
01015
01016 freeimage(sum_data);
01017 freeimage(sum_good);
01018 freeilist(ilist);
01019
01020
01021 cpl_image_save(ps.mdome, mdome_name, BITPIX, NULL, CPL_IO_DEFAULT);
01022
01023
01024
01025
01026 if(omega_create_background(pars, mdome_name, backname) != 0){
01027 cpl_msg_warning(_id,"Error in creating background image");
01028
01029 }
01030
01031 back_image = cpl_image_load(backname, CPL_TYPE_FLOAT, 0, 0);
01032 if (back_image != NULL){
01033 norm_image = cpl_image_divide_create(ps.mdome, back_image);
01034 freeimage(back_image);
01035 }
01036 else{
01037 cpl_msg_warning(_id,"Could not create normalised image. Cannot load background image.");
01038 norm_image = NULL;
01039 }
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052 pixelmap = cpl_mask_threshold_image_create(norm_image, omega_mdome_config.lthre,
01053 omega_mdome_config.hthre);
01054 cpl_mask_not(pixelmap);
01055
01056 cpl_msg_info(_id,"Creating pixel map with thresholds: %g,%g",omega_mdome_config.lthre,
01057 omega_mdome_config.hthre);
01058
01059 omega_mdome_config.CountColdPixels = cpl_mask_count(pixelmap);
01060 if (omega_mdome_config.CountColdPixels == -1) {
01061 cpl_msg_warning(_id,"Pixelmap is NULL. <%s>", cpl_error_get_message());
01062 }
01063
01064 cpl_msg_info(_id,"Detected %d cold pixels", omega_mdome_config.CountColdPixels);
01065
01066 freeimage(norm_image);
01067
01068
01069 ps.cpixels = cpl_image_new_from_mask(pixelmap);
01070 freemask(pixelmap);
01071
01072
01073 ps.stats = cpl_stats_new_from_image(ps.mdome, CPL_STATS_ALL);
01074 if(ps.stats == NULL && (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND)){
01075 cpl_msg_warning(_id," There are no good pixels in image for doing statistics");
01076 cpl_error_reset();
01077 }
01078 else{
01079 omega_mdome_config.Mean = cpl_stats_get_mean(ps.stats);
01080 omega_mdome_config.Median = cpl_stats_get_median(ps.stats);
01081 omega_mdome_config.Stdev = cpl_stats_get_stdev(ps.stats);
01082 }
01083
01084 freestats(ps.stats);
01085
01086
01087 return 0;
01088 }
01089
01090
01091
01092 static void omega_mdome_init(void) {
01093 ps.labels = NULL;
01094 ps.mbframe = NULL;
01095 ps.cframe = NULL;
01096 ps.hframe = NULL;
01097 ps.domelist = NULL;
01098 ps.ph = NULL;
01099 ps.eh = NULL;
01100 ps.stats = NULL;
01101 ps.mdome = NULL;
01102 ps.cpixels = NULL;
01103 ps.firstdome = NULL;
01104 }
01105
01106
01107 static void omega_mdome_tidy(void) {
01108 freespace(ps.labels);
01109 freeframeset(ps.domelist);
01110 freestats(ps.stats);
01111 freeimage(ps.mdome);
01112 freeimage(ps.cpixels);
01113 freefits(ps.firstdome);
01114 }