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 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00039
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <xsh_error.h>
00051
00052 #include <xsh_utils.h>
00053 #include <xsh_msg.h>
00054 #include <xsh_data_instrument.h>
00055 #include <math.h>
00056
00057 #include <xsh_dfs.h>
00058
00059 #include <xsh_drl.h>
00060 #include <xsh_pfits.h>
00061 #include <xsh_detmon.h>
00062 #include <xsh_irplib_utils.h>
00063 #include <xsh_paf_save.h>
00064 #include <xsh_utils_image.h>
00065 #include <xsh_parameters.h>
00066 #include <xsh_drl_check.h>
00067
00068
00069 #include <cpl.h>
00070 #include <assert.h>
00071
00072
00073
00074
00075 #define RECIPE_ID "xsh_mbias"
00076 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer, A. Modigliani"
00077 #define RECIPE_CONTACT "amodigli@eso.org"
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 static int xsh_mbias_create(cpl_plugin *);
00088 static int xsh_mbias_exec(cpl_plugin *);
00089 static int xsh_mbias_destroy(cpl_plugin *);
00090
00091
00092 static void xsh_mbias(cpl_parameterlist *, cpl_frameset *);
00093
00094
00095
00096
00097 static char xsh_mbias_description_short[] = "Create the master bias frame";
00098
00099 static char xsh_mbias_description[] =
00100 "This recipe creates a master bias frame by computing the median of all \
00101 input bias frames.\n\
00102 Input Frames : \n\
00103 - A set of n RAW frames (Format=RAW, n >=3, Tag = BIAS_arm)\n\
00104 - [OPTIONAL] A badpixel map (Format=QUP, Tag = MASTER_BP_MAP_arm)\n\
00105 - [OPTIONAL] A map on non linear pixels (Format=QUP,RAW, Tag = BP_MAP_NL_arm)\n\
00106 Products : \n\
00107 - A master bias frame (Format=PRE, PRO.CATG = MASTER_BIAS_arm)\n\
00108 - A cold pixel map frame (Format=PRE, PRO.CATG = BP_MAP_CP_arm)\n\
00109 - A hot pixel map frame (Format=PRE, PRO.CATG = BP_MAP_HP_arm)\n\
00110 - [if given in input] the master bp map is updated frame (Format=PRE, PRO.CATG = MASTER_BP_MAP_arm)\n";
00111
00112
00113
00114
00115
00124
00125
00126 int cpl_plugin_get_info(cpl_pluginlist * list)
00127 {
00128 cpl_recipe *recipe = NULL;
00129 cpl_plugin *plugin = NULL;
00130
00131 recipe = cpl_calloc(1, sizeof(*recipe));
00132 if (recipe == NULL) {
00133 return -1;
00134 }
00135
00136 plugin = &recipe->interface;
00137
00138 cpl_plugin_init(plugin, CPL_PLUGIN_API,
00139 XSH_BINARY_VERSION,
00140 CPL_PLUGIN_TYPE_RECIPE,
00141 RECIPE_ID,
00142 xsh_mbias_description_short,
00143 xsh_mbias_description,
00144 RECIPE_AUTHOR,
00145 RECIPE_CONTACT,
00146 xsh_get_license(),
00147 xsh_mbias_create,
00148 xsh_mbias_exec,
00149 xsh_mbias_destroy);
00150
00151 cpl_pluginlist_append(list, plugin);
00152
00153 return (cpl_error_get_code() != CPL_ERROR_NONE);
00154 }
00155
00156
00166
00167
00168 static int xsh_mbias_create(cpl_plugin * plugin)
00169 {
00170 cpl_recipe *recipe = NULL;
00171 cpl_parameter * p =NULL;
00172 xsh_clipping_param crh_clip_param = {8.0, 2, 0.7, 0, 0.3};
00173 xsh_hot_cold_pix_param hp_clip_param = {1, 5.0, 3, 5.0 , 3};
00174 xsh_fpn_param fpn_param = {10,10,1024,1024,10,100};
00175 xsh_ron_param ron_param = {"ALL",
00176 10,100,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,3,25};
00177
00178 xsh_struct_param struct_param = {-1,-1};
00179
00180
00181 xsh_init();
00182
00183
00184 assure(plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00185
00186
00187 assure(cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00188 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00189
00190 recipe = (cpl_recipe *) plugin;
00191
00192
00193 recipe->parameters = cpl_parameterlist_new();
00194 assure(recipe->parameters != NULL,
00195 CPL_ERROR_ILLEGAL_OUTPUT, "Memory allocation failed!");
00196
00197
00198 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00199
00200 p = cpl_parameter_new_enum("xsh.xsh_mbias.method",
00201 CPL_TYPE_STRING,
00202 "Method for master bias generation: "
00203 "[median] (+CRH clip) or (clean) mean",
00204 "xsh.xsh_mbias",
00205 "median", 2, "median", "mean");
00206
00207 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"method");
00208 cpl_parameterlist_append(recipe->parameters,p);
00209
00210
00211 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00212 crh_clip_param));
00213
00214 check(xsh_parameters_hot_cold_pix_create(RECIPE_ID,recipe->parameters,
00215 hp_clip_param));
00216
00217
00218 check(xsh_parameters_fpn_create(RECIPE_ID,recipe->parameters,fpn_param));
00219 check(xsh_parameters_ron_create(RECIPE_ID,recipe->parameters,ron_param));
00220 check(xsh_parameters_struct_create(RECIPE_ID,recipe->parameters,struct_param));
00221
00222
00223 cleanup:
00224 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00225 xsh_error_dump(CPL_MSG_ERROR);
00226 return 1;
00227 } else {
00228 return 0;
00229 }
00230 }
00231
00232
00238
00239
00240 static int xsh_mbias_exec(cpl_plugin * plugin)
00241 {
00242 cpl_recipe *recipe = NULL;
00243
00244
00245 assure(plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00246
00247
00248 assure(cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00249 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00250
00251 recipe = (cpl_recipe *) plugin;
00252
00253
00254 xsh_mbias(recipe->parameters, recipe->frames);
00255
00256 cleanup:
00257 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00258 xsh_error_dump(CPL_MSG_ERROR);
00259 cpl_error_reset();
00260 return 1;
00261 } else {
00262 return 0;
00263 }
00264 }
00265
00266
00272
00273 static int xsh_mbias_destroy(cpl_plugin * plugin)
00274 {
00275 cpl_recipe *recipe = NULL;
00276
00277 xsh_error_reset();
00278
00279 assure(plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00280
00281
00282 assure(cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00283 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00284
00285 recipe = (cpl_recipe *) plugin;
00286
00287 xsh_free_parameterlist(&recipe->parameters);
00288
00289 cleanup:
00290 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00291 return 1;
00292 } else {
00293 return 0;
00294 }
00295 }
00296
00297
00298
00306
00307 static void xsh_mbias(cpl_parameterlist * parameters, cpl_frameset * frameset)
00308 {
00309 const char* recipe_tags[1] = {XSH_BIAS};
00310 int recipe_tags_size = 1;
00311
00312 cpl_frameset *raws = NULL;
00313 cpl_frameset *calib = NULL;
00314 cpl_frame *bpmap = NULL;
00315 cpl_frame *master_bias = NULL;
00316 cpl_frame *product = NULL;
00317 xsh_instrument* instrument = NULL;
00318 xsh_clipping_param* crh_clipping = NULL;
00319 cpl_imagelist* crh_list=NULL;
00320 cpl_frame* crh_frm=NULL;
00321
00322
00323
00324
00325
00326
00327
00328 char* crh_name=NULL;
00329
00330 char prefix[80];
00331
00332 double ks_low=0;
00333 double ks_high=0;
00334
00335
00336 cpl_parameter* p=NULL;
00337 int binx=0;
00338 int biny=0;
00339 cpl_frame* bias_frm=NULL;
00340 cpl_propertylist* plist=NULL;
00341 const char* method=NULL;
00342 char name[256];
00343 const char* ftag;
00344 int find_hot_cold_pix=0;
00345
00346 int cold_niter=3;
00347 int hot_niter=3;
00348 cpl_frame* cpix_frm=NULL;
00349 cpl_frame* hpix_frm=NULL;
00350 cpl_image* ima=NULL;
00351 cpl_propertylist* head=NULL;
00352 cpl_frame* coadd_bp_map=NULL;
00353 const char* pro_catg=NULL;
00354 int pre_overscan_corr=0;
00355
00356
00357 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.cold-pix-kappa"));
00358 check(ks_low = cpl_parameter_get_double(p));
00359
00360 if(ks_low<2.0) {
00361 xsh_msg_error("You set %s too low: %f! ",
00362 cpl_parameter_get_name(p),cpl_parameter_get_double(p));
00363 xsh_msg("Should be at least 2 for proper kappa-sigma clipping");
00364 goto cleanup;
00365 }
00366
00367 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.hot-pix-kappa"));
00368 check(ks_high = cpl_parameter_get_double(p));
00369
00370 if(ks_high<2.0) {
00371 xsh_msg_error("You set %s too low: %f! ",
00372 cpl_parameter_get_name(p),cpl_parameter_get_double(p));
00373 xsh_msg("Should be at least 2 for proper kappa-sigma clipping");
00374 goto cleanup;
00375 }
00376
00377 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.hot-cold-pix-search"));
00378 check(find_hot_cold_pix = cpl_parameter_get_bool(p));
00379
00380
00381
00382
00383 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00384 recipe_tags, recipe_tags_size,
00385 RECIPE_ID, XSH_BINARY_VERSION,
00386 xsh_mbias_description_short ) ) ;
00387
00388
00389
00390
00391
00392 check(bpmap=xsh_check_load_master_bpmap(calib,instrument));
00393
00394 check(bias_frm=cpl_frameset_get_first(raws));
00395 check(plist=cpl_propertylist_load(cpl_frame_get_filename(bias_frm),0));
00396 check(binx=xsh_pfits_get_binx(plist));
00397 check(biny=xsh_pfits_get_biny(plist));
00398 xsh_free_propertylist(&plist);
00399
00400
00401
00402
00403 sprintf(name,"%s.fits",XSH_BIAS_REMOVE_CRH);
00404 ftag=XSH_GET_TAG_FROM_ARM(XSH_BIAS_REMOVE_CRH,instrument);
00405
00406
00407
00408
00409
00410 check(crh_clipping = xsh_parameters_clipping_crh_get(RECIPE_ID,
00411 parameters));
00412
00413
00414
00415
00416
00417
00418 check(xsh_prepare(raws, bpmap, NULL, XSH_BIAS, instrument,pre_overscan_corr));
00419
00420
00421
00422 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.method"));
00423 check(method = cpl_parameter_get_string(p));
00424
00425 if(strcmp(method,"mean") == 0) {
00426 check(master_bias = xsh_remove_crh_multiple(raws,
00427 ftag,
00428 crh_clipping,
00429 instrument,
00430 &crh_list,
00431 NULL));
00432 } else {
00433 check(master_bias = xsh_create_master_bias2(raws,instrument,ftag));
00434 }
00435
00436
00437 check(product=xsh_create_master_bias(raws,master_bias,instrument,parameters));
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 xsh_msg("Save products");
00465
00466 check( xsh_add_product_pre(product, frameset, parameters, RECIPE_ID,
00467 instrument));
00468
00469 if(find_hot_cold_pix) {
00470 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_mbias.cold-pix-niter"));
00471 check(cold_niter = cpl_parameter_get_int(p));
00472
00473 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_mbias.hot-pix-niter"));
00474 check(hot_niter = cpl_parameter_get_int(p));
00475
00476
00477
00478
00479
00480 check(xsh_image_get_hot_cold_pixs(master_bias,instrument,
00481 ks_low,cold_niter,ks_high,hot_niter,
00482 &cpix_frm,&hpix_frm));
00483
00484
00485
00486 sprintf(prefix, "%s_%s",XSH_BP_MAP_CP,
00487 xsh_instrument_arm_tostring(instrument) ) ;
00488
00489
00490 check(xsh_add_product_image(cpix_frm, frameset,parameters,
00491 RECIPE_ID, instrument, prefix ));
00492
00493
00494 sprintf(prefix, "%s_%s", XSH_BP_MAP_HP,
00495 xsh_instrument_arm_tostring(instrument) ) ;
00496
00497
00498 check(xsh_add_product_image(hpix_frm, frameset,parameters,
00499 RECIPE_ID, instrument, prefix ));
00500
00501
00502 }
00503
00504 if(bpmap!=NULL) {
00505
00506
00507 xsh_msg( "Coadd non-lin and static bp map frames" ) ;
00508 check(coadd_bp_map=cpl_frame_duplicate(bpmap));
00509 sprintf(name,cpl_frame_get_filename(bpmap));
00510 check(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00511 check(head=cpl_propertylist_load(name,0));
00512 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_MASTER_BP_MAP,instrument));
00513 sprintf(name,"%s.fits",pro_catg);
00514 check(cpl_image_save(ima,name,CPL_BPP_IEEE_FLOAT,head,CPL_IO_DEFAULT));
00515 check(cpl_frame_set_filename(coadd_bp_map,name));
00516 check(cpl_frame_set_tag(coadd_bp_map,pro_catg));
00517 check(xsh_badpixelmap_coadd(coadd_bp_map,hpix_frm));
00518 check( xsh_add_product_bpmap(coadd_bp_map, frameset, parameters,
00519 RECIPE_ID, instrument, pro_catg ));
00520 xsh_free_frame(&coadd_bp_map);
00521 xsh_free_image(&ima);
00522 xsh_free_propertylist(&head);
00523
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555 xsh_msg("xsh_mbias success!!");
00556
00557 cleanup:
00558 xsh_end( RECIPE_ID, frameset, parameters );
00559 XSH_FREE(crh_clipping);
00560 xsh_free_propertylist(&plist);
00561 xsh_instrument_free(&instrument);
00562 xsh_free_frame(&product);
00563 xsh_free_frameset(&raws);
00564 xsh_free_frameset(&calib);
00565 xsh_free_frame(&master_bias);
00566 xsh_free_imagelist(&crh_list);
00567 XSH_FREE(crh_name);
00568
00569 xsh_free_frame(&crh_frm);
00570 xsh_free_frame(&cpix_frm);
00571 xsh_free_frame(&hpix_frm);
00572 return;
00573 }
00574