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 if(omega_save_image(ps.mdark,set,pars,alist,qclist,CPL_BPP_IEEE_FLOAT,outmdark,
00593 RECIPE,prframe_mdark,NULL,isfirst) == -1){
00594 cpl_msg_error(_id,"Cannot save product %s", DARK_PROCATG);
00595 freeplist(qclist);
00596 freeplist(alist);
00597 freespace(outmdark);
00598 omega_darkcurrent_tidy();
00599 return -1;;
00600 }
00601
00602 freeplist(qclist);
00603 freeplist(alist);
00604 freeimage(ps.mdark);
00605 freefits(ps.firstdark);
00606 ps.firstdark = NULL;
00607 ps.eh = NULL;
00608
00609 }
00610
00611
00612 freespace(outmdark);
00613 omega_darkcurrent_tidy();
00614
00615
00616 return 0;
00617 }
00618
00619
00628
00629 int omega_darkcurrent_combine(cpl_parameterlist *pars, int xn)
00630 {
00631
00632 int count = 0;
00633 int i = 0;
00634 int maxiter = 0;
00635 int ndark = 0;
00636 int live = 0;
00637 float bias = BIAS;
00638 double dethres = 3.0;
00639 double exptime = 0.0;
00640 double rate = 0.0;
00641 double rejt = 5.0;
00642 double detector_size_x=1.;
00643 double detector_size_y=1.;
00644 double detector_size=1.;
00645
00646 const char *cmd = NULL;
00647 const char *cosmiccat = "OMEGA_cosmicrays.fits";
00648 const char *cosmic_conf = NULL;
00649 const char *_id = "";
00650 const char *path = NULL;
00651 const char *sex_conv = NULL;
00652 const char *sex_nnw = NULL;
00653 const char *sex_par = NULL;
00654 const char *temp1739 = "OMEGA_temp001739.fits";
00655
00656 const cpl_frame *darkfr = NULL;
00657 cpl_image *mbias = NULL, *trim_raw = NULL;
00658 cpl_imagelist *ilist = NULL;
00659 cpl_stats *stats = NULL;
00660 cpl_parameter *par = NULL;
00661 cpl_propertylist *mplist = NULL, *xplist = NULL;
00662
00663
00664 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.MaxIter") ;
00665 maxiter = cpl_parameter_get_int(par) ;
00666
00667 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.RejThre") ;
00668 rejt = cpl_parameter_get_double(par) ;
00669
00670 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.DetThre") ;
00671 dethres = cpl_parameter_get_double(par) ;
00672
00673 par = cpl_parameterlist_find(pars, "omega.omega_darkcurrent.BinPath");
00674 path = cpl_parameter_get_string(par) ;
00675
00676 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexCosmic" );
00677 cosmic_conf = cpl_parameter_get_string(par) ;
00678
00679 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexConv" );
00680 sex_conv = cpl_parameter_get_string(par) ;
00681
00682 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexParam" );
00683 sex_par = cpl_parameter_get_string(par) ;
00684
00685 par = cpl_parameterlist_find(pars,"omega.omega_darkcurrent.SexNnw" );
00686 sex_nnw = cpl_parameter_get_string(par) ;
00687
00688 ndark = cpl_frameset_get_size(ps.darklist);
00689 darkfr = cpl_frameset_get_first_const(ps.darklist);
00690
00691
00692 omega_pfits_get_detlive(ps.eh,&live);
00693 if (! live) {
00694 return 1;
00695 }
00696
00697
00698 if(ps.mbframe != NULL){
00699 mbias = cpl_image_load(cpl_frame_get_filename(ps.mbframe), CPL_TYPE_FLOAT,0,xn);
00700 if (mbias == NULL) {
00701 cpl_msg_warning(_id,"Cannot load image %s", OMEGA_CALIB_BIAS);
00702 }
00703 }
00704
00705 ilist = cpl_imagelist_new();
00706 cpl_msg_info (_id,"Doing trim and overscan correction on images");
00707
00708
00709
00710
00711 for (i=0; i< ndark; i++)
00712 {
00713
00714 trim_raw = TrimOscanCorrect(darkfr, omega_darkcurrent_config.oc, xn);
00715 if(trim_raw == NULL){
00716 cpl_msg_error(_id,"Cannot trim image");
00717 freeilist(ilist);
00718 return -1;
00719 }
00720
00721 if(omega_darkcurrent_config.oc==0){
00722 if (mbias != NULL){
00723 cpl_msg_info(cpl_func,"Subtracting bias frame");
00724 cpl_image_subtract(trim_raw, mbias);
00725 }
00726 else
00727 cpl_image_subtract_scalar(trim_raw, bias);
00728 }
00729
00730 cpl_imagelist_set(ilist, trim_raw, i);
00731 darkfr = cpl_frameset_get_next_const(ps.darklist);
00732 if (darkfr == NULL)
00733 break;
00734
00735 }
00736
00737 freeimage(mbias);
00738 if (ilist == NULL) {
00739 cpl_msg_error(_id,"Error in image list <%s>",cpl_error_get_message());
00740 freeimage(trim_raw);
00741 return -1;
00742 }
00743
00744 cpl_msg_info (_id,"Computing the median of all images...");
00745 ps.mdark = cpl_imagelist_collapse_median_create(ilist);
00746
00747 if (ps.mdark == NULL) {
00748 cpl_msg_error (_id,"Cannot take median of list <%s>",cpl_error_get_message());
00749 freeilist(ilist);
00750 return -1;
00751 }
00752
00753 cpl_msg_info(_id,"Estimating the RMS using %d maximum iterations",maxiter);
00754 cpl_msg_info(_id,"Rejection threshold is %g (sigma)",rejt);
00755
00756
00757
00758 stats = omega_iter_stat(ps.mdark,rejt,maxiter);
00759 if(stats == NULL){
00760 cpl_msg_warning(_id,"Cannot calculate statistics iteratively");
00761 }
00762
00763 darkfr = cpl_frameset_get_first(ps.darklist);
00764 mplist = cpl_propertylist_load_regexp(cpl_frame_get_filename(darkfr), 0, "EXPTIME", 0);
00765 xplist = cpl_propertylist_load_regexp(cpl_frame_get_filename(darkfr), xn, "EXPTIME", 0);
00766 exptime = omega_pfits_get_exptime(xplist);
00767 if (exptime == 0.0){
00768 exptime = omega_pfits_get_exptime(mplist);
00769 }
00770
00771 omega_darkcurrent_config.dark_current = cpl_stats_get_mean(stats) / exptime*3600.0;
00772 freeplist(mplist);
00773 freeplist(xplist);
00774
00775 freestats(stats);
00776
00777
00778 rate = 0.0;
00779 for(i=0; i < ndark; i++){
00780
00781 cpl_image_save(cpl_imagelist_get(ilist, i),temp1739,BITPIX,NULL,CPL_IO_DEFAULT);
00782
00783
00784
00785
00786
00787 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00788 "-DETECT_THRESH %g "
00789 "-CHECKIMAGE_NAME chkimage.fits "
00790 "-CATALOG_TYPE FITS_1.0 "
00791 "-CATALOG_NAME %s",
00792 path,
00793 temp1739,
00794 cosmic_conf,
00795 sex_par,
00796 sex_conv,
00797 sex_nnw,
00798 dethres,
00799 cosmiccat);
00800
00801 if (system(cmd) != 0) {
00802 cpl_free((char *)cmd);
00803 freeilist(ilist);
00804 cpl_msg_error("","%s Failed to run Sextractor", _id);
00805 return -1;
00806 }
00807
00808 cpl_free((char *)cmd);
00809
00810 mplist = cpl_propertylist_load_regexp(cosmiccat, 1, "NAXIS2", 0);
00811 if(mplist == NULL){
00812 cpl_msg_error(_id,"Cannot load header for %s",cosmiccat);
00813 freeilist(ilist);
00814 return -1;
00815 }
00816
00817 count = cpl_propertylist_get_int(mplist,"NAXIS2");
00818
00819
00820 rate += count;
00821 freeplist(mplist);
00822 }
00823
00824
00825 detector_size_x=cpl_image_get_size_x(cpl_imagelist_get(ilist,0));
00826 detector_size_y=cpl_image_get_size_y(cpl_imagelist_get(ilist,0));
00827 detector_size=detector_size_x * detector_size_y * 0.0015 * 0.0015;
00828
00829 rate = rate/ndark;
00830 rate = rate/exptime*3600.0;
00831 if(detector_size>0){
00832 rate = rate/detector_size;
00833 }
00834 omega_darkcurrent_config.rate = rate;
00835
00836 freeilist(ilist);
00837
00838
00839 return 0;
00840 }
00841
00855
00870 int omega_darkcurrent_save(cpl_image *image, cpl_frameset *set, cpl_parameterlist *parlist,
00871 cpl_propertylist *qclist, cpl_type_bpp bitpix,
00872 const char *outfile, cpl_frame *product_frame)
00873
00874 {
00875
00876 const char *fctid = "omega_darkcurrent_save";
00877 cpl_propertylist *plist = NULL;
00878
00879
00880 if (isfirst) {
00881
00882
00883 plist = cpl_propertylist_new();
00884
00885
00886 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00887 RECIPE,PIPEID,DICID,NULL) != CPL_ERROR_NONE) {
00888
00889 cpl_msg_warning(fctid, "Problem in the main header of product DFS-compliance") ;
00890 }
00891
00892
00893
00894
00895 cpl_propertylist_erase_regexp(plist,REM_PRIM_KEYS,0);
00896
00897
00898 if (cpl_propertylist_save(plist,outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE){
00899 cpl_msg_error(fctid,"Cannot save product PHU");
00900 freeplist(plist);
00901 return 1;
00902 }
00903
00904 freeplist(plist);
00905 cpl_frameset_insert(set,product_frame);
00906 }
00907
00908
00909 plist = cpl_propertylist_duplicate(ps.eh);
00910
00911
00912
00913 cpl_propertylist_append_int(plist, "ESO DRS OVERSCAN METHOD", omega_darkcurrent_config.oc);
00914 cpl_propertylist_set_comment(plist, "ESO DRS OVERSCAN METHOD", "overscan correction method");
00915
00916
00917 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
00918 RECIPE,PIPEID,DICID,NULL) != CPL_ERROR_NONE) {
00919
00920 cpl_msg_warning(fctid, "Problem in the extension header of product DFS-compliance") ;
00921 }
00922
00923
00924 cpl_propertylist_erase_regexp(plist, REM_EXT_KEYS, 0);
00925
00926
00927
00928 cpl_propertylist_append(plist, qclist);
00929
00930
00931 if (cpl_image_save(image,outfile,bitpix,plist,
00932 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00933 cpl_msg_error(fctid,"Cannot save product. %s", cpl_error_get_message());
00934 freeplist(plist);
00935 return -1;
00936 }
00937
00938
00939
00940 if(omega_darkcurrent_config.paf){
00941 int xtnum = omega_fits_get_extnum(ps.firstdark);
00942 char *prefix = omega_get_root_name(outfile);
00943 char *outpaf = cpl_sprintf("%s_%d.paf",prefix,xtnum);
00944 const char pafcopy[] = "^(DATE-OBS|ARCFILE|ESO TPL ID|ESO DET WIN1 DIT1|MJD-OBS|EXTNAME|ESO PRO CATG)$";
00945 cpl_frame *fframe = cpl_frameset_get_first(set);
00946 cpl_propertylist *mlist = cpl_propertylist_load_regexp(cpl_frame_get_filename(fframe),0,pafcopy, 0);
00947 cpl_propertylist_copy_property_regexp(qclist, mlist, pafcopy, 0);
00948 if (cpl_propertylist_copy_property_regexp(qclist, plist,
00949 pafcopy, 0) != CPL_ERROR_NONE){
00950
00951 cpl_msg_warning(fctid,"Some mandatory keywords are missing in PAF file");
00952 }
00953
00954 cpl_dfs_save_paf(INSTRUME, RECIPE, qclist, outpaf);
00955 cpl_free(outpaf);
00956 freeplist(mlist);
00957 }
00958
00959 freeplist(plist);
00960
00961 return 0;
00962 }
00963
00964
00965 static void omega_darkcurrent_init(void) {
00966 ps.labels = NULL;
00967 ps.darklist = NULL;
00968 ps.mdark = NULL;
00969 ps.mbframe = NULL;
00970 ps.eh = NULL;
00971 ps.firstdark = NULL;
00972 }
00973
00974
00975 static void omega_darkcurrent_tidy(void) {
00976 freespace(ps.labels);
00977 freeframeset(ps.darklist);
00978 freeimage(ps.mdark);
00979 freefits(ps.firstdark);
00980 }
00981