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 cpl_propertylist_copy_property_regexp(alist, ps.eh, WCS_KEYS, 0);
00729
00730 if(omega_save_image(ps.mdome,set,pars,alist,qclist,CPL_BPP_IEEE_FLOAT,outmdome,
00731 RECIPE,prframe_mdome,NULL,isfirst) == -1){
00732 cpl_msg_error(_id,"Cannot save product %s", MDOME_PROCATG);
00733 freeplist(qclist);
00734 freeplist(alist);
00735 freespace(outcpm);
00736 freespace(outmdome);
00737 omega_mdome_tidy();
00738 return -1;;
00739 }
00740 freeplist(qclist);
00741 freeimage(ps.mdome);
00742
00743
00744 qclist = cpl_propertylist_new();
00745 cpl_propertylist_append_int(qclist,"ESO QC NUMBER COLD PIXELS",
00746 omega_mdome_config.CountColdPixels);
00747
00748 cpl_propertylist_set_comment(qclist,"ESO QC NUMBER COLD PIXELS",
00749 "Number of cold pixels");
00750 if(isfirst){
00751 outcpm = cpl_sprintf("%s_%s.fits", INSTRUME,CPM_PROCATG);
00752 prframe_cpm = omega_product_frame(outcpm, CPM_PROCATG, CPL_FRAME_TYPE_IMAGE);
00753 }
00754
00755 if(omega_save_image(ps.cpixels,set,pars,alist,qclist,CPL_BPP_16_SIGNED,outcpm,
00756 RECIPE,prframe_cpm,NULL,isfirst) == -1){
00757 cpl_msg_error(_id,"Cannot save product %s", CPM_PROCATG);
00758 freeplist(qclist);
00759 freeplist(alist);
00760 freespace(outcpm);
00761 freespace(outmdome);
00762 omega_mdome_tidy();
00763 return -1;;
00764 }
00765
00766 freeplist(qclist);
00767 freeplist(alist);
00768 freeimage(ps.cpixels);
00769 freefits(ps.firstdome);
00770 ps.firstdome = NULL;
00771 ps.eh = NULL;
00772
00773 }
00774
00775
00776
00777 freespace(outcpm);
00778 freespace(outmdome);
00779 omega_mdome_tidy();
00780
00781 return 0;
00782
00783 }
00784
00785
00796
00797 int omega_mdome_combine(cpl_parameterlist *pars, int xn)
00798 {
00799
00800 int i,nflats,live, naxis1, naxis2;
00801 float bias = BIAS;
00802 double gain = 0.0;
00803 double median = 1.0;
00804 double threshold = 0.0;
00805 double *data_scales = NULL;
00806 const char *_id = "omega_mdome_combine";
00807 const char *mdome_name = "omega_mdome_temp.fits";
00808 const char *backname = "omega_mdome_background.fits";
00809
00810 cpl_error_code code;
00811 const cpl_frame *domefr = NULL;
00812 cpl_vector *scales = NULL;
00813 cpl_vector *median_vector = NULL;
00814 cpl_image *mbias = NULL;
00815 cpl_image *trim_raw = NULL, *back_image = NULL, *dev = NULL;
00816 cpl_image *good_float = NULL, *good_int = NULL, *median_all = NULL;
00817 cpl_image *sum_data = NULL, *sum_good = NULL, *new_image = NULL;
00818 cpl_image *norm_image = NULL;
00819 cpl_imagelist *ilist = NULL;
00820 cpl_mask *good = NULL, *bpm_map = NULL;
00821 cpl_mask *pixelmap = NULL;
00822
00823 nflats = cpl_frameset_get_size(ps.domelist);
00824 domefr = cpl_frameset_get_first_const(ps.domelist);
00825
00826
00827 omega_pfits_get_detlive(ps.eh,&live);
00828 if (! live) {
00829 return 1;
00830 }
00831
00832
00833 if(ps.mbframe != NULL){
00834 mbias = cpl_image_load(cpl_frame_get_filename(ps.mbframe), CPL_TYPE_FLOAT,0,xn);
00835 if (mbias == NULL) {
00836 cpl_msg_warning(_id,"Cannot load image %s", OMEGA_CALIB_BIAS);
00837 }
00838 }
00839
00840
00841 bpm_map = makebpm(ps.hframe, ps.cframe, xn);
00842
00843
00844 scales = cpl_vector_new (nflats);
00845 median_vector = cpl_vector_new (nflats);
00846 cpl_vector_fill(median_vector,0.0);
00847
00848 data_scales = cpl_vector_get_data(scales);
00849
00850 ilist = cpl_imagelist_new();
00851 cpl_msg_info (_id,"Doing trim and overscan correction on images");
00852
00853
00854 for (i=0; i< nflats; i++){
00855
00856 trim_raw = TrimOscanCorrect(domefr, omega_mdome_config.oc, xn);
00857 if(trim_raw == NULL){
00858 freevector(scales);
00859 freevector(median_vector);
00860 freeilist(ilist);
00861 return -1;
00862 }
00863
00864 if (mbias != NULL)
00865 cpl_image_subtract(trim_raw, mbias);
00866 else
00867 cpl_image_subtract_scalar(trim_raw, bias);
00868
00869 if (bpm_map == NULL) {
00870 ps.stats = cpl_stats_new_from_image(trim_raw, CPL_STATS_ALL);
00871 median = cpl_stats_get_median(ps.stats);
00872 }
00873 else {
00874 cpl_image_reject_from_mask(trim_raw, bpm_map);
00875 ps.stats = cpl_stats_new_from_image(trim_raw, CPL_STATS_ALL);
00876 median = cpl_stats_get_median(ps.stats);
00877 }
00878
00879 cpl_vector_set(median_vector, i, median);
00880
00881 data_scales[i] = (double)1.0/median;
00882
00883 cpl_image_divide_scalar(trim_raw, median);
00884
00885 cpl_imagelist_set(ilist, trim_raw, i);
00886
00887
00888 domefr = cpl_frameset_get_next_const(ps.domelist);
00889 if (domefr == NULL)
00890 break;
00891
00892 freestats(ps.stats);
00893
00894 }
00895
00896 omega_mdome_config.RawMin = cpl_vector_get_min(median_vector);
00897 omega_mdome_config.RawMax = cpl_vector_get_max(median_vector);
00898 omega_mdome_config.RawMean = cpl_vector_get_mean(median_vector);
00899 omega_mdome_config.RawMedian = cpl_vector_get_median(median_vector);
00900 omega_mdome_config.RawStdev = cpl_vector_get_stdev(median_vector);
00901
00902 freevector(median_vector);
00903
00904 freestats(ps.stats);
00905 freeimage(mbias);
00906 freemask(bpm_map);
00907
00908 if (ilist == NULL) {
00909 cpl_msg_error(_id,"Error in image list <%s>",cpl_error_get_message());
00910 freevector(scales);
00911 freeimage(trim_raw);
00912 return -1;
00913 }
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925 omega_pfits_get_conad(ps.eh, &gain);
00926
00927 if (gain < 0.1){
00928 omega_pfits_get_gain(ps.eh, &gain);
00929
00930 if(gain < 0.1) {
00931 cpl_msg_info(_id,"Using default value of gain");
00932 gain = 2.0;
00933 }
00934 }
00935 else {
00936 cpl_msg_info(_id,"Gain value from image is %g", gain);
00937 }
00938
00939 cpl_msg_info (_id,"Computing the median of all images...");
00940 median_all = cpl_imagelist_collapse_median_create(ilist);
00941
00942 if (median_all == NULL) {
00943 cpl_msg_error (_id,"Cannot take median of list <%s>",cpl_error_get_message());
00944 freevector(scales);
00945 freeilist(ilist);
00946 return -1;
00947 }
00948
00949 naxis1 = cpl_image_get_size_x(median_all);
00950 naxis2 = cpl_image_get_size_y(median_all);
00951
00952 sum_data = cpl_image_new(naxis1,naxis2,CPL_TYPE_FLOAT);
00953 sum_good = cpl_image_new(naxis1,naxis2,CPL_TYPE_FLOAT);
00954
00955
00956 for (i=0; i< nflats; i++){
00957
00958 trim_raw = cpl_imagelist_get(ilist, i);
00959 new_image = cpl_image_duplicate(trim_raw);
00960
00961
00962
00963
00964
00965 cpl_image_threshold(trim_raw, 0, FLT_MAX, 0, 2e20);
00966
00967 dev = cpl_image_subtract_create(median_all, trim_raw);
00968
00969 code = cpl_image_power(trim_raw, 0.5);
00970 if(code != CPL_ERROR_NONE) {
00971 cpl_msg_error(_id,"Error in SQRT operation <%s>",cpl_error_get_message());
00972 freevector(scales);
00973 freeilist(ilist);
00974 freeimage(median_all);
00975 freeimage(sum_data);
00976 freeimage(sum_good);
00977 freeimage(new_image);
00978 freeimage(dev);
00979 return -1;
00980 }
00981
00982 cpl_image_divide(dev, trim_raw);
00983
00984 threshold = omega_mdome_config.sigma * (sqrt(gain * data_scales[i]));
00985
00986 good = cpl_mask_threshold_image_create(dev, -threshold, threshold);
00987
00988 freeimage(dev);
00989 good_int = cpl_image_new_from_mask(good) ;
00990 freemask(good) ;
00991
00992 good_float = cpl_image_cast(good_int, CPL_TYPE_FLOAT);
00993 freeimage(good_int);
00994
00995 cpl_image_multiply(new_image, good_float);
00996 cpl_image_add(sum_data, new_image);
00997
00998 cpl_image_add(sum_good, good_float);
00999
01000 freeimage(new_image);
01001 freeimage(good_float);
01002
01003 }
01004
01005 freeimage(median_all);
01006 freevector(scales);
01007
01008
01009 ps.mdome = cpl_image_divide_create(sum_data, sum_good);
01010 if (ps.mdome == NULL) {
01011 cpl_msg_error(_id,"Error in division %s <%s>",MDOME_PROCATG, cpl_error_get_message());
01012 freeilist(ilist);
01013 freeimage(sum_data);
01014 freeimage(sum_good);
01015 return -1;
01016 }
01017
01018 freeimage(sum_data);
01019 freeimage(sum_good);
01020 freeilist(ilist);
01021
01022
01023 cpl_image_save(ps.mdome, mdome_name, BITPIX, NULL, CPL_IO_DEFAULT);
01024
01025
01026
01027
01028 if(omega_create_background(pars, mdome_name, backname) != 0){
01029 cpl_msg_warning(_id,"Error in creating background image");
01030
01031 }
01032
01033 back_image = cpl_image_load(backname, CPL_TYPE_FLOAT, 0, 0);
01034 if (back_image != NULL){
01035 norm_image = cpl_image_divide_create(ps.mdome, back_image);
01036 freeimage(back_image);
01037 }
01038 else{
01039 cpl_msg_warning(_id,"Could not create normalised image. Cannot load background image.");
01040 norm_image = NULL;
01041 }
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054 pixelmap = cpl_mask_threshold_image_create(norm_image, omega_mdome_config.lthre,
01055 omega_mdome_config.hthre);
01056 cpl_mask_not(pixelmap);
01057
01058 cpl_msg_info(_id,"Creating pixel map with thresholds: %g,%g",omega_mdome_config.lthre,
01059 omega_mdome_config.hthre);
01060
01061 omega_mdome_config.CountColdPixels = cpl_mask_count(pixelmap);
01062 if (omega_mdome_config.CountColdPixels == -1) {
01063 cpl_msg_warning(_id,"Pixelmap is NULL. <%s>", cpl_error_get_message());
01064 }
01065
01066 cpl_msg_info(_id,"Detected %d cold pixels", omega_mdome_config.CountColdPixels);
01067
01068 freeimage(norm_image);
01069
01070
01071 ps.cpixels = cpl_image_new_from_mask(pixelmap);
01072 freemask(pixelmap);
01073
01074
01075 ps.stats = cpl_stats_new_from_image(ps.mdome, CPL_STATS_ALL);
01076 if(ps.stats == NULL && (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND)){
01077 cpl_msg_warning(_id," There are no good pixels in image for doing statistics");
01078 cpl_error_reset();
01079 }
01080 else{
01081 omega_mdome_config.Mean = cpl_stats_get_mean(ps.stats);
01082 omega_mdome_config.Median = cpl_stats_get_median(ps.stats);
01083 omega_mdome_config.Stdev = cpl_stats_get_stdev(ps.stats);
01084 }
01085
01086 freestats(ps.stats);
01087
01088
01089 return 0;
01090 }
01091
01092
01093
01094 static void omega_mdome_init(void) {
01095 ps.labels = NULL;
01096 ps.mbframe = NULL;
01097 ps.cframe = NULL;
01098 ps.hframe = NULL;
01099 ps.domelist = NULL;
01100 ps.ph = NULL;
01101 ps.eh = NULL;
01102 ps.stats = NULL;
01103 ps.mdome = NULL;
01104 ps.cpixels = NULL;
01105 ps.firstdome = NULL;
01106 }
01107
01108
01109 static void omega_mdome_tidy(void) {
01110 freespace(ps.labels);
01111 freeframeset(ps.domelist);
01112 freestats(ps.stats);
01113 freeimage(ps.mdome);
01114 freeimage(ps.cpixels);
01115 freefits(ps.firstdome);
01116 }