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
00038
00059
00062
00063
00064
00065
00066 static int omega_darkcurrent_create(cpl_plugin *) ;
00067 static int omega_darkcurrent_exec(cpl_plugin *) ;
00068 static int omega_darkcurrent_destroy(cpl_plugin *) ;
00069 static int omega_darkcurrent(cpl_frameset *set,cpl_parameterlist *pars) ;
00070
00071
00072
00073
00074 int omega_darkcurrent_combine(cpl_parameterlist *pars, int xn);
00075 int omega_darkcurrent_save(cpl_image *image, cpl_frameset *set, cpl_parameterlist *parlist,
00076 cpl_propertylist *qclist, cpl_type_bpp bitpix,
00077 const char *outfile, cpl_frame *product_frame);
00078 static void omega_darkcurrent_init(void);
00079 static void omega_darkcurrent_tidy(void);
00080
00081
00082
00083
00084
00085 static struct {
00086
00087
00088 int extnum;
00089 int oc;
00090 int paf;
00091
00092
00093 double dark_current;
00094 double rate;
00095
00096 }omega_darkcurrent_config;
00097
00098
00099 static struct {
00100 cpl_size *labels;
00101 const cpl_frame *mbframe;
00102 cpl_frameset *darklist;
00103 omega_fits *firstdark;
00104 cpl_propertylist *eh;
00105
00106
00107 cpl_image *mdark;
00108
00109 } ps;
00110
00111
00112
00113 #define RECIPE "omega_darkcurrent"
00114
00115 static int isfirst;
00116 static int dummy;
00117
00118
00119
00127
00128 int cpl_plugin_get_info(cpl_pluginlist * list)
00129 {
00130 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
00131 cpl_plugin * plugin = &recipe->interface ;
00132
00133 cpl_plugin_init(plugin,
00134 CPL_PLUGIN_API,
00135 OMEGA_BINARY_VERSION,
00136 CPL_PLUGIN_TYPE_RECIPE,
00137 "omega_darkcurrent",
00138 "OMEGA - Measure the dark current and the particle event rate.(Calfile 531).",
00139 "The raw dark frames are trimmed, overscan-corrected and"
00140 "debiased. The dark current is calculated by median averaging"
00141 "three reduced dark frames and iteratively rejecting outliers"
00142 "from the result, and computing the mean of the remaining pixels.",
00143 "Sandra Castro",
00144 "scastro@eso.org",
00145 omega_get_license(),
00146 omega_darkcurrent_create,
00147 omega_darkcurrent_exec,
00148 omega_darkcurrent_destroy) ;
00149
00150 cpl_pluginlist_append(list, plugin) ;
00151
00152 return 0;
00153 }
00154
00155
00156
00165
00166 static int omega_darkcurrent_create(cpl_plugin * plugin)
00167 {
00168 cpl_recipe * recipe;
00169 cpl_parameter * p ;
00170 char *path = NULL;
00171
00172
00173 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00174 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00175 cpl_func, __LINE__, cpl_error_get_where());
00176 return (int)cpl_error_get_code();
00177 }
00178
00179 if (plugin == NULL) {
00180 cpl_msg_error(cpl_func, "Null plugin");
00181 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00182 }
00183
00184
00185 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00186 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00187 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00188 }
00189
00190
00191 recipe = (cpl_recipe *)plugin;
00192
00193
00194 recipe->parameters = cpl_parameterlist_new() ;
00195 if (recipe->parameters == NULL) {
00196 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00197 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00198 }
00199
00200
00201 p = cpl_parameter_new_value("omega.omega_darkcurrent.ExtensionNumber",
00202 CPL_TYPE_INT,
00203 "FITS extension number to load (1 to 32). (-1 = all)",
00204 "omega_darkcurrent",
00205 -1) ;
00206
00207 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"ext") ;
00208 cpl_parameterlist_append(recipe->parameters, p) ;
00209
00210
00211 p = cpl_parameter_new_range("omega.omega_darkcurrent.OverscanMethod",
00212 CPL_TYPE_INT,
00213 "Overscan Correction Method",
00214 "omega_darkcurrent",
00215 6, 0, 6);
00216 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"oc-meth") ;
00217 cpl_parameterlist_append(recipe->parameters, p) ;
00218
00219 p = cpl_parameter_new_value("omega.omega_darkcurrent.PAF",
00220 CPL_TYPE_BOOL,
00221 "Boolean value to create PAF files. 1(Yes), 0(No)",
00222 "omega_darkcurrent",
00223 1) ;
00224
00225 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "paf") ;
00226 cpl_parameterlist_append(recipe->parameters, p) ;
00227
00228 p = cpl_parameter_new_value("omega.omega_darkcurrent.RejThre",
00229 CPL_TYPE_DOUBLE,
00230 "The threshold rejecting outlying pixels",
00231 "omega_darkcurrent",
00232 5.0) ;
00233
00234 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"rej-thre") ;
00235 cpl_parameterlist_append(recipe->parameters, p) ;
00236
00237 p = cpl_parameter_new_value("omega.omega_darkcurrent.DetThre",
00238 CPL_TYPE_DOUBLE,
00239 "The detection threshold for cosmic ray event ",
00240 "omega_darkcurrent",
00241 6.0) ;
00242
00243 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"det-thre") ;
00244 cpl_parameterlist_append(recipe->parameters, p) ;
00245
00246
00247 p = cpl_parameter_new_value("omega.omega_darkcurrent.MaxIter",
00248 CPL_TYPE_INT,
00249 "The maximum number of iterations",
00250 "omega_darkcurrent",
00251 3) ;
00252
00253 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"niter") ;
00254 cpl_parameterlist_append(recipe->parameters, p) ;
00255
00256 p = cpl_parameter_new_value("omega.omega_darkcurrent.MaxCurrent",
00257 CPL_TYPE_DOUBLE,
00258 "The maximum dark current in ADU/pixel/hour",
00259 "omega_darkcurrent",
00260 5.0) ;
00261
00262 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "maxcur") ;
00263 cpl_parameterlist_append(recipe->parameters, p) ;
00264
00265
00266 p = cpl_parameter_new_value("omega.omega_darkcurrent.MaxDiffCurrent",
00267 CPL_TYPE_DOUBLE,
00268 "The maximum difference of dark current between previous ??",
00269 "omega_darkcurrent",
00270 0.2) ;
00271
00272 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "maxdifcur") ;
00273 cpl_parameterlist_append(recipe->parameters, p) ;
00274
00275
00276 path = cpl_sprintf("%s", OMEGA_BIN_PATH);
00277 p = cpl_parameter_new_value("omega.omega_darkcurrent.BinPath",
00278 CPL_TYPE_STRING,
00279 "Path to any external executable program.",
00280 "omega.BinPath",
00281 path);
00282
00283 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "bin-path");
00284 cpl_parameterlist_append(recipe->parameters, p);
00285 cpl_free(path);
00286
00287 path = cpl_sprintf("%s/omega.cosmic.sex", OMEGA_CONFIG_PATH);
00288 p = cpl_parameter_new_value("omega.omega_darkcurrent.SexCosmic",
00289 CPL_TYPE_STRING,
00290 "Path to Sextractor cosmic detection mode config file.",
00291 "omega.Sextractor",
00292 path);
00293
00294 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-cosmic");
00295 cpl_parameterlist_append(recipe->parameters, p);
00296 cpl_free(path);
00297
00298 path = cpl_sprintf("%s/omega.conv", OMEGA_CONFIG_PATH);
00299 p = cpl_parameter_new_value("omega.omega_darkcurrent.SexConv",
00300 CPL_TYPE_STRING,
00301 "Path to Sextractor convolution mask file.",
00302 "omega.Sextractor",
00303 path);
00304
00305 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-conv");
00306 cpl_parameterlist_append(recipe->parameters, p);
00307 cpl_free(path);
00308
00309 path = cpl_sprintf("%s/omega.param", OMEGA_CONFIG_PATH);
00310 p = cpl_parameter_new_value("omega.omega_darkcurrent.SexParam",
00311 CPL_TYPE_STRING,
00312 "Path to Sextractor parameters file.",
00313 "omega.Sextractor",
00314 path);
00315
00316 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"sex-param");
00317 cpl_parameterlist_append(recipe->parameters, p);
00318 cpl_free(path);
00319
00320 path = cpl_sprintf("%s/omega.nnw", OMEGA_CONFIG_PATH);
00321 p = cpl_parameter_new_value("omega.omega_darkcurrent.SexNnw",
00322 CPL_TYPE_STRING,
00323 "Path to Sextractor neural network config file.",
00324 "omega.Sextractor",
00325 path);
00326
00327 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-nnw");
00328 cpl_parameterlist_append(recipe->parameters, p);
00329 cpl_free(path);
00330
00331
00332
00333 return 0;
00334 }
00335
00336
00342
00343 static int omega_darkcurrent_exec(cpl_plugin * plugin)
00344 {
00345
00346 cpl_recipe * recipe;
00347 int recipe_status;
00348
00349
00350 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00351 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00352 cpl_func, __LINE__, cpl_error_get_where());
00353 return (int)cpl_error_get_code();
00354 }
00355
00356 if (plugin == NULL) {
00357 cpl_msg_error(cpl_func, "Null plugin");
00358 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00359 }
00360
00361
00362 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00363 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00364 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00365 }
00366
00367
00368 recipe = (cpl_recipe *)plugin;
00369
00370
00371 if (recipe->parameters == NULL) {
00372 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00373 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00374 }
00375 if (recipe->frames == NULL) {
00376 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00377 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00378 }
00379
00380
00381 recipe_status = omega_darkcurrent(recipe->frames, recipe->parameters);
00382
00383
00384 if (cpl_dfs_update_product_header(recipe->frames)) {
00385 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00386 }
00387
00388
00389 return recipe_status;
00390 }
00391
00392
00398
00399 static int omega_darkcurrent_destroy(cpl_plugin * plugin)
00400 {
00401 cpl_recipe * recipe;
00402
00403 if (plugin == NULL) {
00404 cpl_msg_error(cpl_func, "Null plugin");
00405 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00406 }
00407
00408
00409 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00410 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00411 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00412 }
00413
00414
00415 recipe = (cpl_recipe *)plugin;
00416
00417 cpl_parameterlist_delete(recipe->parameters);
00418
00419 return 0 ;
00420 }
00421
00422
00423
00430
00431 static int omega_darkcurrent(cpl_frameset *set, cpl_parameterlist *pars)
00432 {
00433
00434 int j,jst,jfn;
00435 cpl_size nlab;
00436 int ndark = 0;
00437 int oscan1 = 0;
00438 int status = 0;
00439 float bias = BIAS;
00440 char *outmdark = NULL;
00441 const char *_id = "omega_darkcurrent";
00442
00443 cpl_parameter *par = NULL;
00444 cpl_frame *firstframe = NULL;
00445 cpl_frame *prframe_mdark = NULL;
00446 cpl_propertylist *qclist = NULL;
00447 cpl_propertylist *alist = NULL;
00448
00449
00450
00451
00452
00453 if (pars == NULL) {
00454 cpl_msg_error (_id, "Parameters list not found");
00455 return -1;
00456 }
00457
00458 if (cpl_frameset_is_empty(set) == 1) {
00459 cpl_msg_error (_id, "Frameset not found");
00460 return -1;
00461 }
00462
00463
00464 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.ExtensionNumber") ;
00465 omega_darkcurrent_config.extnum = cpl_parameter_get_int(par) ;
00466
00467 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.OverscanMethod") ;
00468 omega_darkcurrent_config.oc = cpl_parameter_get_int(par) ;
00469
00470 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.PAF");
00471 omega_darkcurrent_config.paf = cpl_parameter_get_bool(par);
00472
00473
00474
00475
00476 if (oc_dfs_set_groups(set)) {
00477 cpl_msg_error(_id, "Cannot identify RAW and CALIB frames") ;
00478 return -1 ;
00479 }
00480
00481
00482 omega_darkcurrent_init();
00483
00484
00485 if ((ps.labels = cpl_frameset_labelise(set,omega_compare_tags,
00486 &nlab)) == NULL) {
00487 cpl_msg_error(_id,"Cannot labelise the input frameset");
00488 omega_darkcurrent_tidy();
00489 return -1;
00490 }
00491 if ((ps.darklist = omega_frameset_subgroup(set,ps.labels,nlab,
00492 DARK_RAW)) == NULL) {
00493 cpl_msg_error(_id,"Cannot find bias frames in input frameset");
00494 omega_darkcurrent_tidy();
00495 return -1;
00496 }
00497
00498 ndark = cpl_frameset_count_tags (set, DARK_RAW);
00499 if (ndark < 3) {
00500 cpl_msg_error (_id, "Need at least 3 (%s) frames to run this recipe", DARK_RAW);
00501 omega_darkcurrent_tidy();
00502 return -1;
00503 }
00504
00505 cpl_msg_info (_id,"There are %d %s frames in frame set",ndark, DARK_RAW);
00506
00507
00508 firstframe = cpl_frameset_get_first(ps.darklist);
00509
00510
00511
00512
00513 ps.mbframe = cpl_frameset_find_const(set, OMEGA_CALIB_BIAS);
00514 if (ps.mbframe == NULL) {
00515 cpl_msg_info(_id,"A Master Bias is not present in frame set. Using default value %f", bias);
00516 }
00517 else{
00518 cpl_msg_info(_id,"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbframe));
00519 }
00520
00521
00522 omega_exten_range(omega_darkcurrent_config.extnum,&jst,&jfn);
00523 if(omega_darkcurrent_config.extnum == 0){
00524 cpl_msg_error(cpl_func,"Unsupported extension request, %d",omega_darkcurrent_config.extnum);
00525 omega_darkcurrent_tidy();
00526 return -1;
00527 }
00528
00529
00530 for (j = jst; j <= jfn; j++) {
00531
00532 isfirst = (j == jst);
00533 cpl_msg_info(_id,"Beginning work on extension %d",j);
00534 omega_darkcurrent_config.dark_current = 0.0;
00535 omega_darkcurrent_config.rate = 0;
00536
00537
00538
00539 if(omega_darkcurrent_config.oc==0){
00540 if(ps.mbframe != NULL){
00541 oscan1 = omega_pfits_get_overscan(ps.mbframe, j);
00542 if(oscan1 != omega_darkcurrent_config.oc) {
00543 cpl_msg_warning (_id, "Overscan correction mode for Master Bias (oc = %d) differs from "
00544 "the one used here (oc = %d)", oscan1, omega_darkcurrent_config.oc);
00545 }
00546 }
00547 }
00548
00549 ps.firstdark = omega_fits_load(firstframe,CPL_TYPE_FLOAT,j);
00550 ps.eh = omega_fits_get_ehu(ps.firstdark);
00551
00552
00553 status = omega_darkcurrent_combine(pars, j);
00554 if(status == 1){
00555 cpl_msg_warning(_id, "Image detector is not live");
00556
00557 freefits(ps.firstdark);
00558 ps.eh = NULL;
00559 continue;
00560 }
00561 else if(status == -1){
00562 cpl_msg_error(_id,"Cannot combine images");
00563 freespace(outmdark);
00564 omega_darkcurrent_tidy();
00565 return -1;
00566 }
00567
00568 status = 0;
00569
00570
00571
00572 qclist = cpl_propertylist_new();
00573 cpl_propertylist_append_double(qclist, "ESO QC DARK CURRENT",
00574 omega_darkcurrent_config.dark_current) ;
00575 cpl_propertylist_set_comment(qclist, "ESO QC DARK CURRENT",
00576 "Dark current") ;
00577 cpl_propertylist_append_double(qclist, "ESO QC PARTICLE RATE",
00578 omega_darkcurrent_config.rate) ;
00579 cpl_propertylist_set_comment(qclist, "ESO QC PARTICLE RATE",
00580 "Value of particle rate" ) ;
00581
00582 if(isfirst){
00583 outmdark = cpl_sprintf("%s_%s.fits", INSTRUME,DARK_PROCATG);
00584 prframe_mdark = omega_product_frame(outmdark, DARK_PROCATG, CPL_FRAME_TYPE_IMAGE);
00585 }
00586
00587 alist = cpl_propertylist_new();
00588 cpl_propertylist_append_string(alist, "EXTNAME",
00589 cpl_propertylist_get_string(ps.eh, "EXTNAME"));
00590 cpl_propertylist_set_comment(alist,"EXTNAME", "Extension name");
00591
00592 cpl_propertylist_copy_property_regexp(alist, ps.eh, WCS_KEYS, 0);
00593
00594 if(omega_save_image(ps.mdark,set,pars,alist,qclist,CPL_BPP_IEEE_FLOAT,outmdark,
00595 RECIPE,prframe_mdark,NULL,isfirst) == -1){
00596 cpl_msg_error(_id,"Cannot save product %s", DARK_PROCATG);
00597 freeplist(qclist);
00598 freeplist(alist);
00599 freespace(outmdark);
00600 omega_darkcurrent_tidy();
00601 return -1;;
00602 }
00603
00604 freeplist(qclist);
00605 freeplist(alist);
00606 freeimage(ps.mdark);
00607 freefits(ps.firstdark);
00608 ps.firstdark = NULL;
00609 ps.eh = NULL;
00610
00611 }
00612
00613
00614 freespace(outmdark);
00615 omega_darkcurrent_tidy();
00616
00617
00618 return 0;
00619 }
00620
00621
00630
00631 int omega_darkcurrent_combine(cpl_parameterlist *pars, int xn)
00632 {
00633
00634 int count = 0;
00635 int i = 0;
00636 int maxiter = 0;
00637 int ndark = 0;
00638 int live = 0;
00639 float bias = BIAS;
00640 double dethres = 3.0;
00641 double exptime = 0.0;
00642 double rate = 0.0;
00643 double rejt = 5.0;
00644 double detector_size_x=1.;
00645 double detector_size_y=1.;
00646 double detector_size=1.;
00647
00648 const char *cmd = NULL;
00649 const char *cosmiccat = "OMEGA_cosmicrays.fits";
00650 const char *cosmic_conf = NULL;
00651 const char *_id = "";
00652 const char *path = NULL;
00653 const char *sex_conv = NULL;
00654 const char *sex_nnw = NULL;
00655 const char *sex_par = NULL;
00656 const char *temp1739 = "OMEGA_temp001739.fits";
00657
00658 const cpl_frame *darkfr = NULL;
00659 cpl_image *mbias = NULL, *trim_raw = NULL;
00660 cpl_imagelist *ilist = NULL;
00661 cpl_stats *stats = NULL;
00662 cpl_parameter *par = NULL;
00663 cpl_propertylist *mplist = NULL, *xplist = NULL;
00664
00665
00666 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.MaxIter") ;
00667 maxiter = cpl_parameter_get_int(par) ;
00668
00669 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.RejThre") ;
00670 rejt = cpl_parameter_get_double(par) ;
00671
00672 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.DetThre") ;
00673 dethres = cpl_parameter_get_double(par) ;
00674
00675 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.BinPath");
00676 path = cpl_parameter_get_string(par) ;
00677
00678 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexCosmic" );
00679 cosmic_conf = cpl_parameter_get_string(par) ;
00680
00681 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexConv" );
00682 sex_conv = cpl_parameter_get_string(par) ;
00683
00684 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexParam" );
00685 sex_par = cpl_parameter_get_string(par) ;
00686
00687 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexNnw" );
00688 sex_nnw = cpl_parameter_get_string(par) ;
00689
00690 ndark = cpl_frameset_get_size(ps.darklist);
00691 darkfr = cpl_frameset_get_first_const(ps.darklist);
00692
00693
00694 omega_pfits_get_detlive(ps.eh,&live);
00695 if (! live) {
00696 return 1;
00697 }
00698
00699
00700 if(ps.mbframe != NULL){
00701 mbias = cpl_image_load(cpl_frame_get_filename(ps.mbframe), CPL_TYPE_FLOAT,0,xn);
00702 if (mbias == NULL) {
00703 cpl_msg_warning(_id,"Cannot load image %s", OMEGA_CALIB_BIAS);
00704 }
00705 }
00706
00707 ilist = cpl_imagelist_new();
00708 cpl_msg_info (_id,"Doing trim and overscan correction on images");
00709
00710
00711
00712
00713 for (i=0; i< ndark; i++)
00714 {
00715
00716 trim_raw = TrimOscanCorrect(darkfr, omega_darkcurrent_config.oc, xn);
00717 if(trim_raw == NULL){
00718 cpl_msg_error(_id,"Cannot trim image");
00719 freeilist(ilist);
00720 return -1;
00721 }
00722
00723 if(omega_darkcurrent_config.oc==0){
00724 if (mbias != NULL){
00725 cpl_msg_info(cpl_func,"Subtracting bias frame");
00726 cpl_image_subtract(trim_raw, mbias);
00727 }
00728 else
00729 cpl_image_subtract_scalar(trim_raw, bias);
00730 }
00731
00732 cpl_imagelist_set(ilist, trim_raw, i);
00733 darkfr = cpl_frameset_get_next_const(ps.darklist);
00734 if (darkfr == NULL)
00735 break;
00736
00737 }
00738
00739 freeimage(mbias);
00740 if (ilist == NULL) {
00741 cpl_msg_error(_id,"Error in image list <%s>",cpl_error_get_message());
00742 freeimage(trim_raw);
00743 return -1;
00744 }
00745
00746 cpl_msg_info (_id,"Computing the median of all images...");
00747 ps.mdark = cpl_imagelist_collapse_median_create(ilist);
00748
00749 if (ps.mdark == NULL) {
00750 cpl_msg_error (_id,"Cannot take median of list <%s>",cpl_error_get_message());
00751 freeilist(ilist);
00752 return -1;
00753 }
00754
00755 cpl_msg_info(_id,"Estimating the RMS using %d maximum iterations",maxiter);
00756 cpl_msg_info(_id,"Rejection threshold is %g (sigma)",rejt);
00757
00758
00759
00760 stats = omega_iter_stat(ps.mdark,rejt,maxiter);
00761 if(stats == NULL){
00762 cpl_msg_warning(_id,"Cannot calculate statistics iteratively");
00763 }
00764
00765 darkfr = cpl_frameset_get_first(ps.darklist);
00766 mplist = cpl_propertylist_load_regexp(cpl_frame_get_filename(darkfr), 0, "EXPTIME", 0);
00767 xplist = cpl_propertylist_load_regexp(cpl_frame_get_filename(darkfr), xn, "EXPTIME", 0);
00768 exptime = omega_pfits_get_exptime(xplist);
00769 if (exptime == 0.0){
00770 exptime = omega_pfits_get_exptime(mplist);
00771 }
00772
00773 omega_darkcurrent_config.dark_current = cpl_stats_get_mean(stats) / exptime*3600.0;
00774 freeplist(mplist);
00775 freeplist(xplist);
00776
00777 freestats(stats);
00778
00779
00780 rate = 0.0;
00781 for(i=0; i < ndark; i++){
00782
00783 cpl_image_save(cpl_imagelist_get(ilist, i),temp1739,BITPIX,NULL,CPL_IO_DEFAULT);
00784
00785
00786
00787
00788
00789 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00790 "-DETECT_THRESH %g "
00791 "-CHECKIMAGE_NAME chkimage.fits "
00792 "-CATALOG_TYPE FITS_1.0 "
00793 "-CATALOG_NAME %s",
00794 path,
00795 temp1739,
00796 cosmic_conf,
00797 sex_par,
00798 sex_conv,
00799 sex_nnw,
00800 dethres,
00801 cosmiccat);
00802
00803 if (system(cmd) != 0) {
00804 cpl_free((char *)cmd);
00805 freeilist(ilist);
00806 cpl_msg_error("","%s Failed to run Sextractor", _id);
00807 return -1;
00808 }
00809
00810 cpl_free((char *)cmd);
00811
00812 mplist = cpl_propertylist_load_regexp(cosmiccat, 1, "NAXIS2", 0);
00813 if(mplist == NULL){
00814 cpl_msg_error(_id,"Cannot load header for %s",cosmiccat);
00815 freeilist(ilist);
00816 return -1;
00817 }
00818
00819 count = cpl_propertylist_get_int(mplist,"NAXIS2");
00820
00821
00822 rate += count;
00823 freeplist(mplist);
00824 }
00825
00826
00827 detector_size_x=cpl_image_get_size_x(cpl_imagelist_get(ilist,0));
00828 detector_size_y=cpl_image_get_size_y(cpl_imagelist_get(ilist,0));
00829 detector_size=detector_size_x * detector_size_y * 0.0015 * 0.0015;
00830
00831 rate = rate/ndark;
00832 rate = rate/exptime*3600.0;
00833 if(detector_size>0){
00834 rate = rate/detector_size;
00835 }
00836 omega_darkcurrent_config.rate = rate;
00837
00838 freeilist(ilist);
00839
00840
00841 return 0;
00842 }
00843
00857
00872 int omega_darkcurrent_save(cpl_image *image, cpl_frameset *set, cpl_parameterlist *parlist,
00873 cpl_propertylist *qclist, cpl_type_bpp bitpix,
00874 const char *outfile, cpl_frame *product_frame)
00875
00876 {
00877
00878 const char *fctid = "omega_darkcurrent_save";
00879 cpl_propertylist *plist = NULL;
00880
00881
00882 if (isfirst) {
00883
00884
00885 plist = cpl_propertylist_new();
00886
00887
00888 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00889 RECIPE,PIPEID,DICID,NULL) != CPL_ERROR_NONE) {
00890
00891 cpl_msg_warning(fctid, "Problem in the main header of product DFS-compliance") ;
00892 }
00893
00894
00895
00896
00897 cpl_propertylist_erase_regexp(plist,REM_PRIM_KEYS,0);
00898
00899
00900 if (cpl_propertylist_save(plist,outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE){
00901 cpl_msg_error(fctid,"Cannot save product PHU");
00902 freeplist(plist);
00903 return 1;
00904 }
00905
00906 freeplist(plist);
00907 cpl_frameset_insert(set,product_frame);
00908 }
00909
00910
00911 plist = cpl_propertylist_duplicate(ps.eh);
00912
00913
00914
00915 cpl_propertylist_append_int(plist, "ESO DRS OVERSCAN METHOD", omega_darkcurrent_config.oc);
00916 cpl_propertylist_set_comment(plist, "ESO DRS OVERSCAN METHOD", "overscan correction method");
00917
00918
00919 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00920 RECIPE,PIPEID,DICID,NULL) != CPL_ERROR_NONE) {
00921
00922 cpl_msg_warning(fctid, "Problem in the extension header of product DFS-compliance") ;
00923 }
00924
00925
00926 cpl_propertylist_erase_regexp(plist, REM_EXT_KEYS, 0);
00927
00928
00929
00930 cpl_propertylist_append(plist, qclist);
00931
00932
00933 if (cpl_image_save(image,outfile,bitpix,plist,
00934 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00935 cpl_msg_error(fctid,"Cannot save product. %s", cpl_error_get_message());
00936 freeplist(plist);
00937 return -1;
00938 }
00939
00940
00941
00942 if(omega_darkcurrent_config.paf){
00943 int xtnum = omega_fits_get_extnum(ps.firstdark);
00944 char *prefix = omega_get_root_name(outfile);
00945 char *outpaf = cpl_sprintf("%s_%d.paf",prefix,xtnum);
00946 const char pafcopy[] = "^(DATE-OBS|ARCFILE|ESO TPL ID|ESO DET WIN1 DIT1|MJD-OBS|EXTNAME|ESO PRO CATG)$";
00947 cpl_frame *fframe = cpl_frameset_get_first(set);
00948 cpl_propertylist *mlist = cpl_propertylist_load_regexp(cpl_frame_get_filename(fframe),0,pafcopy, 0);
00949 cpl_propertylist_copy_property_regexp(qclist, mlist, pafcopy, 0);
00950 if (cpl_propertylist_copy_property_regexp(qclist, plist,
00951 pafcopy, 0) != CPL_ERROR_NONE){
00952
00953 cpl_msg_warning(fctid,"Some mandatory keywords are missing in PAF file");
00954 }
00955
00956 cpl_dfs_save_paf(INSTRUME, RECIPE, qclist, outpaf);
00957 cpl_free(outpaf);
00958 freeplist(mlist);
00959 }
00960
00961 freeplist(plist);
00962
00963 return 0;
00964 }
00965
00966
00967 static void omega_darkcurrent_init(void) {
00968 ps.labels = NULL;
00969 ps.darklist = NULL;
00970 ps.mdark = NULL;
00971 ps.mbframe = NULL;
00972 ps.eh = NULL;
00973 ps.firstdark = NULL;
00974 }
00975
00976
00977 static void omega_darkcurrent_tidy(void) {
00978 freespace(ps.labels);
00979 freeframeset(ps.darklist);
00980 freeimage(ps.mdark);
00981 freefits(ps.firstdark);
00982 }
00983