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_pfits.h>
00053 #include <xsh_utils.h>
00054 #include <xsh_utils_image.h>
00055 #include <xsh_msg.h>
00056
00057 #include <xsh_dfs.h>
00058
00059 #include <xsh_data_instrument.h>
00060 #include <xsh_data_order.h>
00061 #include <xsh_drl.h>
00062 #include <xsh_drl_check.h>
00063 #include <xsh_model_arm_constants.h>
00064
00065
00066 #include <cpl.h>
00067 #include <xsh_drl_check.h>
00068
00069
00070
00071
00072
00073 #define RECIPE_ID "xsh_mflat"
00074 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer, D. Bramich, A. Modigliani"
00075 #define RECIPE_CONTACT "amodigli@eso.org"
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 static int xsh_mflat_create(cpl_plugin *);
00086 static int xsh_mflat_exec(cpl_plugin *);
00087 static int xsh_mflat_destroy(cpl_plugin *);
00088
00089
00090 static void xsh_mflat(cpl_parameterlist *, cpl_frameset *);
00091
00092
00093
00094
00095 static char xsh_mflat_description_short[] =
00096 "Create the master flat and the orders edges traces table frames";
00097
00098 static char xsh_mflat_description[] =
00099 "This recipe creates the master flat and the orders edges traces table frames.\n\
00100 Input Frames :\n\
00101 - [UVB] A set of n RAW frames (Format = RAW, n>=3,\
00102 Tag = FLAT_D2_mode_UVB, mode=SLIT/IFU)\n\
00103 - [UVB] A set of n RAW frames (Format = RAW, n>=3,\
00104 Tag = FLAT_QTH_mode_UVB)\n\
00105 - [VIS] A set of n RAW frames (Format = RAW, n>=3,\
00106 Tag = FLAT_mode_VIS)\n\
00107 - [NIR] A set of n x n RAW frames ((Format = RAW, n>=3,\
00108 Tag = FLAT_mode_NIR_ON, FLAT_mode_NIR_OFF)\n\
00109 - A spectral format table (Format = PRE, Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00110 - An order table (Format = TABLE, Tag = ORDER_TAB_CENTR_arm)\n\
00111 - [UVB,VIS] A master bias (Format = PRE, Tag = MASTER_BIAS_arm)\n\
00112 - [OPTIONAL] A map of reference bad pixel (Format = QUP,RAW, Tag = BP_MAP_RP_arm)\n\
00113 - [OPTIONAL] A map of non linear pixel (Format = QUP,RAW, Tag = BP_MAP_NL_arm)\n\
00114 - [OPTIONAL,UVB,VIS] A master dark (Format = PRE, Tag = MASTER_DARK_arm)\n\
00115 Products : \n\
00116 - An updated order table with edge UP and edge LOW\
00117 (Format = TABLE, TAG = ORDER_TAB_EDGES_mode_arm)\n\
00118 - A master flat (Format = PRE, PRO.CATG = MASTER_FLAT_mode_arm)\n\
00119 - The inter-order background frame (Format = PRE, PRO.CATG = MFLAT_BACK_mode_arm)\n\
00120 - The inter-order background sampling points grid table\n\
00121 (Format = PRE, PRO.CATG = MFLAT_GRID_BACK_mode_arm)\n\
00122 Prepare the flat frames.\n\
00123 Stack and sigma clip all the flat frames.\n\
00124 Subtract master bias.\n\
00125 Subtract master dark.\n\
00126 Detect order edge.\n\
00127 Subtract background.\n\
00128 Create the Master Flat.\n";
00129
00130
00131
00132
00133
00142
00143
00144 int cpl_plugin_get_info(cpl_pluginlist *list) {
00145 cpl_recipe *recipe = NULL;
00146 cpl_plugin *plugin = NULL;
00147
00148 recipe = cpl_calloc(1, sizeof(*recipe));
00149 if ( recipe == NULL ){
00150 return -1;
00151 }
00152
00153 plugin = &recipe->interface ;
00154
00155 cpl_plugin_init(plugin,
00156 CPL_PLUGIN_API,
00157 XSH_BINARY_VERSION,
00158 CPL_PLUGIN_TYPE_RECIPE,
00159 RECIPE_ID,
00160 xsh_mflat_description_short,
00161 xsh_mflat_description,
00162 RECIPE_AUTHOR,
00163 RECIPE_CONTACT,
00164 xsh_get_license(),
00165 xsh_mflat_create,
00166 xsh_mflat_exec,
00167 xsh_mflat_destroy);
00168
00169 cpl_pluginlist_append(list, plugin);
00170
00171 return (cpl_error_get_code() != CPL_ERROR_NONE);
00172 }
00173
00174
00184
00185
00186 static int xsh_mflat_create(cpl_plugin *plugin){
00187 cpl_recipe *recipe = NULL;
00188 xsh_stack_param stack_param = {"median",5.,5.};
00189
00190
00191 xsh_init();
00192
00193
00194 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00195
00196
00197 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00198 CPL_ERROR_TYPE_MISMATCH,
00199 "Plugin is not a recipe");
00200
00201 recipe = (cpl_recipe *)plugin;
00202
00203
00204 recipe->parameters = cpl_parameterlist_new();
00205 assure( recipe->parameters != NULL,
00206 CPL_ERROR_ILLEGAL_OUTPUT,
00207 "Memory allocation failed!");
00208
00209
00210
00211 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00212 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00213 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00214 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00215
00216
00217 check( xsh_parameters_detect_order_create( RECIPE_ID,
00218 recipe->parameters));
00219
00220
00221 check( xsh_parameters_d2_detect_order_create( RECIPE_ID,
00222 recipe->parameters));
00223
00224
00225 check( xsh_parameters_background_create( RECIPE_ID,
00226 recipe->parameters));
00227
00228 cleanup:
00229 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00230 xsh_error_dump(CPL_MSG_ERROR);
00231 return 1;
00232 }
00233 else {
00234 return 0;
00235 }
00236 }
00237
00238
00244
00245
00246 static int xsh_mflat_exec(cpl_plugin *plugin) {
00247 cpl_recipe *recipe = NULL;
00248
00249
00250 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00251
00252
00253 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00254 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00255
00256 recipe = (cpl_recipe *)plugin;
00257
00258
00259 xsh_mflat(recipe->parameters, recipe->frames);
00260
00261 cleanup:
00262 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00263 xsh_error_dump(CPL_MSG_ERROR);
00264
00265 cpl_error_reset();
00266 return 1;
00267 }
00268 else {
00269 return 0;
00270 }
00271 }
00272
00273
00279
00280 static int xsh_mflat_destroy(cpl_plugin *plugin)
00281 {
00282 cpl_recipe *recipe = NULL;
00283
00284
00285 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00286
00287
00288 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00289 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00290
00291 recipe = (cpl_recipe *)plugin;
00292
00293 xsh_free_parameterlist(&recipe->parameters);
00294
00295 cleanup:
00296 if (cpl_error_get_code() != CPL_ERROR_NONE)
00297 {
00298 return 1;
00299 }
00300 else
00301 {
00302 return 0;
00303 }
00304 }
00305
00306 static cpl_frame*
00307 xsh_mflat_combine_flats(cpl_frameset *raw_set, cpl_frameset *on_set,
00308 cpl_frameset *off_set, cpl_frame *bpmap_frame, cpl_frame *master_bias_frame,
00309 cpl_frame *master_dark_frame, cpl_frame *cen_order_tab_frame,
00310 xsh_instrument* instrument, xsh_stack_param* stack_par,
00311 const int pre_overscan_corr) {
00312
00313 cpl_frame *rmdark = NULL;
00314 cpl_frameset *onoff_set = NULL;
00315 if (xsh_instrument_get_arm(instrument) != XSH_ARM_NIR) {
00316
00317 check(
00318 xsh_prepare( raw_set, bpmap_frame, master_bias_frame, XSH_FLAT, instrument,pre_overscan_corr,CPL_TRUE));
00319
00320 cpl_frameset *sub_bias_set = NULL;
00321 cpl_frameset *sub_dark_set = NULL;
00322 cpl_frame* frm = NULL;
00323 cpl_frame* sub = NULL;
00324 char prefix[256];
00325 int i = 0;
00326 int nraws = 0;
00327 if (master_bias_frame != NULL) {
00328
00329 xsh_msg( "Subtract bias");
00330 sub_bias_set = cpl_frameset_new();
00331 nraws = cpl_frameset_get_size(raw_set);
00332
00333 for (i = 0; i < nraws; i++) {
00334 sprintf(prefix, "FLAT_SUB_%d_", i);
00335 frm = cpl_frameset_get_frame(raw_set, i);
00336
00337 sub = xsh_subtract_bias(frm, master_bias_frame, instrument, prefix,
00338 pre_overscan_corr, 1);
00339 cpl_frameset_insert(sub_bias_set, sub);
00340 }
00341 } else {
00342 sub_bias_set = cpl_frameset_duplicate(raw_set);
00343 }
00344
00345 if (master_dark_frame != NULL) {
00346 xsh_msg( "Subtract dark");
00347
00348 sub_dark_set = cpl_frameset_new();
00349 nraws = cpl_frameset_get_size(sub_bias_set);
00350
00351 for (i = 0; i < nraws; i++) {
00352 sprintf(prefix, "FLAT_SUBTRACT_DARK_%d.fits", i);
00353 frm = cpl_frameset_get_frame(sub_bias_set, i);
00354
00355 sub = xsh_subtract_dark(frm, master_dark_frame, prefix, instrument);
00356 cpl_frameset_insert(sub_dark_set, sub);
00357 }
00358
00359 } else {
00360 sub_dark_set = cpl_frameset_duplicate(sub_bias_set);
00361 }
00362 xsh_free_frameset(&sub_bias_set);
00363
00364 check(
00365 rmdark=xsh_create_master_flat2(sub_dark_set,cen_order_tab_frame,stack_par,instrument));
00366 xsh_free_frameset(&sub_dark_set);
00367
00368 }
00369
00370 else {
00371 check(
00372 xsh_prepare( on_set, bpmap_frame, NULL, "ON", instrument,pre_overscan_corr,CPL_TRUE));
00373 check(
00374 xsh_prepare( off_set, bpmap_frame, NULL, "OFF", instrument,pre_overscan_corr,CPL_TRUE));
00375
00376 check( onoff_set = xsh_subtract_nir_on_off( on_set, off_set, instrument));
00377 check(
00378 rmdark=xsh_create_master_flat2(onoff_set,cen_order_tab_frame,stack_par,instrument));
00379
00380 }
00381
00382
00383 cleanup:
00384 xsh_free_frameset(&onoff_set);
00385 return rmdark;
00386 }
00387
00388
00389 static void
00390 xsh_combine_flats_and_detect_edges( cpl_frameset *raw_set,
00391 cpl_frameset *on_set,
00392 cpl_frameset *off_set,
00393 cpl_frame *bpmap_frame,
00394 cpl_frame *master_bias_frame,
00395 cpl_frame *master_dark_frame,
00396 cpl_frame *cen_order_tab_frame,
00397 cpl_frame *qc_cen_order_tab_frame,
00398 xsh_stack_param* stack_par,
00399 xsh_detect_order_param *detectorder_par,
00400 const int pre_overscan_corr,
00401 cpl_frame **edges_order_tab_frame,
00402 cpl_frame **rmdark,
00403 xsh_instrument* instrument)
00404 {
00405
00406
00407 check(*rmdark=xsh_mflat_combine_flats(raw_set,on_set,off_set,bpmap_frame,master_bias_frame,
00408 master_dark_frame,cen_order_tab_frame,instrument,stack_par,pre_overscan_corr));
00409
00410
00411 check(xsh_monitor_flux(*rmdark,qc_cen_order_tab_frame, instrument));
00412
00413
00414 xsh_msg("Detect order edges");
00415 check( *edges_order_tab_frame = xsh_detect_order_edge( *rmdark,
00416 cen_order_tab_frame, detectorder_par, instrument));
00417
00418 cleanup:
00419
00420 return;
00421 }
00422
00423
00431
00432
00433 static cpl_error_code
00434 xsh_params_bin_scale(cpl_frameset* raws,
00435 xsh_background_param* backg,
00436 xsh_detect_order_param* det_order)
00437 {
00438
00439 cpl_frame* frame=NULL;
00440 const char* name=NULL;
00441 cpl_propertylist* plist=NULL;
00442 int binx=0;
00443 int biny=0;
00444
00445 check(frame=cpl_frameset_get_first(raws));
00446 check(name=cpl_frame_get_filename(frame));
00447 check(plist=cpl_propertylist_load(name,0));
00448 check(binx=xsh_pfits_get_binx(plist));
00449 check(biny=xsh_pfits_get_biny(plist));
00450 xsh_free_propertylist(&plist);
00451
00452 if(biny>1) {
00453
00454
00455
00456 backg->radius_y=backg->radius_y/biny;
00457
00458 }
00459
00460
00461 if(binx>1) {
00462
00463 backg->radius_x=backg->radius_x/binx;
00464 det_order->search_window_hsize=det_order->search_window_hsize/biny;
00465 det_order->min_order_size_x=det_order->min_order_size_x/binx;
00466
00467 }
00468
00469 cleanup:
00470 xsh_free_propertylist(&plist);
00471 return cpl_error_get_code();
00472
00473 }
00474
00475 static cpl_error_code
00476 xsh_params_set_defaults(cpl_parameterlist* pars,
00477 xsh_instrument* inst,
00478 xsh_detect_order_param* det_order,
00479 xsh_background_param* backg)
00480 {
00481 cpl_parameter* p=NULL;
00482
00483 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-min-order-size-x"));
00484 if(cpl_parameter_get_int(p) <= 0) {
00485 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00486 det_order->min_order_size_x=40;
00487 } else {
00488 det_order->min_order_size_x=60;
00489 }
00490 }
00491
00492 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-min-sn"));
00493 if(cpl_parameter_get_double(p) <= 0) {
00494 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00495 if(xsh_instrument_get_mode(inst) == XSH_MODE_IFU) {
00496 det_order->min_sn=4;
00497 } else {
00498 det_order->min_sn=60;
00499 }
00500 } else {
00501 if(xsh_instrument_get_mode(inst) == XSH_MODE_IFU) {
00502 det_order->min_sn=20;
00503 } else {
00504 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00505 det_order->min_sn=20;
00506 } else {
00507 det_order->min_sn=40;
00508 }
00509 }
00510 }
00511 }
00512
00513 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-slice-trace-method"));
00514 if(strcmp(cpl_parameter_get_string(p),"auto") == 0) {
00515 if (xsh_instrument_get_mode(inst) == XSH_MODE_IFU){
00516 cpl_parameter_set_default_string(p,"sobel");
00517 } else {
00518 cpl_parameter_set_default_string(p,"fixed");
00519 }
00520 }
00521
00522 cleanup:
00523
00524 return cpl_error_get_code();
00525
00526 }
00527
00528
00529
00537
00538 static void xsh_mflat(cpl_parameterlist* parameters, cpl_frameset* frameset)
00539 {
00540 const char* recipe_tags[1] = {XSH_FLAT};
00541 int recipe_tags_size = 1;
00542
00543
00544 cpl_frameset* raws = NULL;
00545 cpl_frameset* calib = NULL;
00546 cpl_frameset* on = NULL;
00547 cpl_frameset* off = NULL;
00548 cpl_frameset* qth = NULL;
00549 cpl_frameset* d2 = NULL;
00550 xsh_instrument* instrument = NULL;
00551
00552 cpl_frame* spectralformat_frame = NULL;
00553 cpl_frame* bpmap = NULL;
00554 cpl_frame* master_bias = NULL;
00555 cpl_frame* master_dark = NULL;
00556 cpl_frame* cen_order_tab_frame = NULL;
00557
00558 cpl_frame* edges_order_tab_frame = NULL;
00559 cpl_frame* master_flat_frame = NULL;
00560 cpl_frame* master_bkg_frame = NULL;
00561
00562 cpl_frame *qth_edges_order_tab_frame = NULL;
00563 cpl_frame *qth_master_flat_frame = NULL;
00564 cpl_frame *d2_edges_order_tab_frame = NULL;
00565 cpl_frame* d2_master_flat_frame = NULL;
00566
00567 xsh_detect_order_param* det_order = NULL;
00568 xsh_d2_detect_order_param * d2_det_order = NULL;
00569 xsh_background_param* backg = NULL;
00570
00571 cpl_frame *qth_cen_order_tab_frame = NULL;
00572 cpl_frame *d2_cen_order_tab_frame = NULL;
00573
00574 char fname_d2[256];
00575 char fname_qth[256];
00576
00577 cpl_table* grid_tab1=NULL;
00578 cpl_table* grid_tab2=NULL;
00579 cpl_propertylist* plist=NULL;
00580 cpl_frame* rmbackground=NULL;
00581 cpl_frame* qth_rmbackground=NULL;
00582 cpl_frame* d2_rmbackground=NULL;
00583 cpl_frame* grid_backg=NULL;
00584 cpl_frame* d2_grid_backg=NULL;
00585 cpl_frame* qth_grid_backg=NULL;
00586 cpl_frame* frame_backg=NULL;
00587 cpl_frame* d2_frame_backg=NULL;
00588 cpl_frame* qth_frame_backg=NULL;
00589 int pre_overscan_corr=0;
00590 xsh_stack_param* stack_par=NULL;
00591
00592 cpl_frame *rmdark = NULL;
00593 cpl_frame *qth_rmdark = NULL;
00594 cpl_frame *d2_rmdark = NULL;
00595
00596
00597
00598
00599 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00600 recipe_tags, recipe_tags_size,
00601 RECIPE_ID, XSH_BINARY_VERSION,
00602 xsh_mflat_description_short ) ) ;
00603
00604 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00605 if(instrument->arm == XSH_ARM_NIR) {
00606 xsh_instrument_nir_corr_if_JH(raws,instrument);
00607 }
00608
00609
00610
00611
00612
00613 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00614 check( cen_order_tab_frame = xsh_find_order_tab_centr( calib, instrument));
00615
00616
00617
00618
00619 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00620 XSH_SPECTRAL_FORMAT, instrument));
00621
00622
00623 if (xsh_instrument_get_arm(instrument) == XSH_ARM_UVB){
00624 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(raws) >= 2,"UVB arm requires D2 and QTH input");
00625 XSH_ASSURE_NOT_ILLEGAL_MSG( instrument->lamp == XSH_LAMP_QTH_D2,"Something wrong in instrument setting");
00626 }
00627 else{
00628 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(raws) >= 1,"Provide at least one input raw flat frame");
00629 }
00630
00631
00632
00633 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00634 if ((master_bias = xsh_find_frame_with_tag(calib, XSH_MASTER_BIAS,
00635 instrument)) == NULL) {
00636
00637 xsh_msg_warning("Frame %s not provided", XSH_MASTER_BIAS);
00638 xsh_error_reset();
00639 }
00640
00641 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00642 instrument)) == NULL){
00643 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00644 xsh_error_reset();
00645 }
00646
00647 if ( xsh_instrument_get_arm( instrument) == XSH_ARM_UVB){
00648 check( xsh_dfs_split_qth_d2( raws, &qth, &d2));
00649 }
00650 }
00651
00652 else {
00653
00654 check(xsh_dfs_split_nir(raws,&on,&off));
00655 }
00656
00657
00658 check( xsh_instrument_update_from_spectralformat( instrument,
00659 spectralformat_frame));
00660
00661
00662
00663
00664 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00665 "pre-overscan-corr"));
00666 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00667 check( det_order = xsh_parameters_detect_order_get( RECIPE_ID,
00668 parameters,parameters));
00669 check( d2_det_order = xsh_parameters_d2_detect_order_get( RECIPE_ID,
00670 parameters));
00671 check( backg = xsh_parameters_background_get( RECIPE_ID,
00672 parameters));
00673
00674 xsh_params_set_defaults(parameters,instrument,det_order,backg);
00675
00676 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00677 check(xsh_params_bin_scale(raws,backg,det_order));
00678 }
00679
00680
00681
00682
00683
00684 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_UVB){
00685
00686 check( xsh_combine_flats_and_detect_edges(raws, on, off,bpmap,master_bias,
00687 master_dark, cen_order_tab_frame,
00688 cen_order_tab_frame,stack_par,
00689 det_order,
00690 pre_overscan_corr,
00691 &edges_order_tab_frame, &rmdark,
00692 instrument));
00693
00694
00695 xsh_msg("Subtract inter-order background");
00696 check( rmbackground = xsh_subtract_background(rmdark,
00697 edges_order_tab_frame,
00698 backg,
00699 instrument, "MFLAT",
00700 &grid_backg,
00701 &frame_backg,1,1,0));
00702
00703
00704 check(master_flat_frame = xsh_create_master_flat_with_mask(rmbackground,edges_order_tab_frame,instrument));
00705
00706
00707 xsh_msg("Detect dead-saturated pixels");
00708 check(xsh_image_mflat_detect_blemishes(master_flat_frame, instrument));
00709
00710
00711
00712
00713
00714 xsh_msg("Saving products");
00715 check( xsh_add_product_table( edges_order_tab_frame, frameset,
00716 parameters,RECIPE_ID,instrument,NULL));
00717
00718 check( xsh_add_product_pre( master_flat_frame, frameset,
00719 parameters, RECIPE_ID, instrument));
00720
00721
00722
00723
00724 check( xsh_add_product_image( frame_backg, frameset, parameters, RECIPE_ID,
00725 instrument,NULL));
00726
00727 check(xsh_add_product_table( grid_backg, frameset,parameters,
00728 RECIPE_ID, instrument,NULL));
00729
00730
00731 } else {
00732
00733
00734 check( xsh_order_split_qth_d2( cen_order_tab_frame, spectralformat_frame,
00735 &qth_cen_order_tab_frame, &d2_cen_order_tab_frame, instrument));
00736
00737 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_QTH));
00738 check( xsh_combine_flats_and_detect_edges( qth, on, off,bpmap, master_bias,
00739 master_dark, qth_cen_order_tab_frame,
00740 cen_order_tab_frame,stack_par,
00741 det_order,
00742 pre_overscan_corr,
00743 &qth_edges_order_tab_frame,
00744 &qth_rmdark,
00745 instrument));
00746
00747
00748
00749 det_order->min_sn = d2_det_order->min_sn;
00750 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_D2));
00751 check( xsh_combine_flats_and_detect_edges( d2, on, off,bpmap, master_bias,
00752 master_dark, d2_cen_order_tab_frame,
00753 cen_order_tab_frame,stack_par,
00754 det_order,
00755 pre_overscan_corr,
00756 &d2_edges_order_tab_frame,
00757 &d2_rmdark,
00758 instrument));
00759
00760
00761 check(edges_order_tab_frame=xsh_flat_merge_qth_d2_tabs(qth_edges_order_tab_frame,
00762 d2_edges_order_tab_frame,instrument));
00763
00764
00765 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00766
00767 xsh_msg("Subtract inter-order background");
00768 check( qth_rmbackground = xsh_subtract_background( qth_rmdark,
00769 edges_order_tab_frame,
00770 backg,
00771 instrument, "MFLAT_QTH",
00772 &qth_grid_backg,
00773 &qth_frame_backg,1,1,0));
00774
00775
00776 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_QTH));
00777 check(qth_master_flat_frame = xsh_create_master_flat_with_mask(qth_rmbackground,edges_order_tab_frame,instrument));
00778
00779
00780 check( xsh_add_product_table( qth_edges_order_tab_frame, frameset,
00781 parameters,RECIPE_ID,instrument,NULL));
00782 check( xsh_add_product_pre( qth_master_flat_frame, frameset,
00783 parameters, RECIPE_ID, instrument));
00784
00785
00786
00787
00788 check( xsh_add_product_image( qth_frame_backg, frameset,
00789 parameters, RECIPE_ID, instrument,NULL));
00790
00791 check(xsh_add_product_table(qth_grid_backg,frameset,parameters,
00792 RECIPE_ID, instrument,NULL));
00793 xsh_msg("name=%s tag=%s",cpl_frame_get_filename(qth_grid_backg),cpl_frame_get_tag(qth_grid_backg));
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00811 xsh_msg("Subtract inter-order background");
00812 check( d2_rmbackground = xsh_subtract_background( d2_rmdark,
00813 edges_order_tab_frame,
00814 backg,
00815 instrument, "MFLAT_D2",
00816 &d2_grid_backg,
00817 &d2_frame_backg,1,1,0));
00818
00819
00820 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_D2));
00821 check(d2_master_flat_frame = xsh_create_master_flat_with_mask(d2_rmbackground,d2_edges_order_tab_frame,instrument));
00822
00823
00824 sprintf(fname_d2,"MFLAT_D2_GRID_BACK_%s_%s.fits",
00825 xsh_instrument_mode_tostring( instrument),
00826 xsh_instrument_arm_tostring( instrument));
00827
00828 sprintf(fname_qth,"MFLAT_QTH_GRID_BACK_%s_%s.fits",
00829 xsh_instrument_mode_tostring( instrument),
00830 xsh_instrument_arm_tostring( instrument));
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861 check( xsh_add_product_table( d2_edges_order_tab_frame, frameset,
00862 parameters,RECIPE_ID,instrument,NULL));
00863 check( xsh_add_product_pre( d2_master_flat_frame, frameset,
00864 parameters, RECIPE_ID, instrument));
00865
00866
00867
00868
00869 check( xsh_add_product_image( d2_frame_backg, frameset,
00870 parameters, RECIPE_ID, instrument,NULL));
00871
00872 check(xsh_add_product_table(d2_grid_backg,frameset,parameters,
00873 RECIPE_ID, instrument,NULL));
00874 xsh_msg("name=%s tag=%s",cpl_frame_get_filename(d2_grid_backg),cpl_frame_get_tag(d2_grid_backg));
00875
00876
00877
00878
00879
00880
00881
00882 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00883
00884 xsh_free_frame(&edges_order_tab_frame);
00885 check( xsh_flat_merge_qth_d2( qth_master_flat_frame,
00886 qth_edges_order_tab_frame,
00887 d2_master_flat_frame,
00888 d2_edges_order_tab_frame,
00889 qth_frame_backg,d2_frame_backg,
00890 &master_flat_frame, &master_bkg_frame,
00891 &edges_order_tab_frame, instrument));
00892
00893
00894 check( xsh_add_product_table( edges_order_tab_frame, frameset,
00895 parameters,RECIPE_ID,instrument,NULL));
00896
00897
00898 xsh_msg("Detect dead-saturated pixels");
00899 check(xsh_image_mflat_detect_blemishes(master_flat_frame,instrument));
00900
00901 check( xsh_add_product_pre( master_flat_frame, frameset,
00902 parameters, RECIPE_ID, instrument));
00903
00904
00905 check( xsh_add_product_image( master_bkg_frame, frameset,
00906 parameters, RECIPE_ID, instrument,NULL));
00907
00908
00909 }
00910 xsh_msg("xsh_mflat success!!");
00911
00912 cleanup:
00913 xsh_end( RECIPE_ID, frameset, parameters);
00914 XSH_FREE( det_order);
00915 XSH_FREE( d2_det_order);
00916 XSH_FREE( backg);
00917 XSH_FREE( stack_par);
00918
00919 xsh_free_frameset( &raws);
00920 xsh_free_frameset( &calib);
00921 xsh_free_frameset( &on);
00922 xsh_free_frameset( &off);
00923 xsh_free_frameset( &qth);
00924 xsh_free_frameset( &d2);
00925
00926 xsh_free_frame( &qth_cen_order_tab_frame);
00927 xsh_free_frame( &qth_edges_order_tab_frame);
00928 xsh_free_frame( &qth_master_flat_frame);
00929
00930
00931 xsh_free_frame( &d2_cen_order_tab_frame);
00932 xsh_free_frame( &d2_edges_order_tab_frame);
00933 xsh_free_frame( &d2_master_flat_frame);
00934
00935
00936 xsh_free_frame( &edges_order_tab_frame);
00937 xsh_free_frame( &master_flat_frame);
00938 xsh_free_frame( &master_bkg_frame);
00939
00940 xsh_free_frame( &rmbackground);
00941 xsh_free_frame( &d2_rmbackground);
00942 xsh_free_frame( &qth_rmbackground);
00943 xsh_free_frame( &grid_backg);
00944 xsh_free_frame( &d2_grid_backg);
00945 xsh_free_frame( &qth_grid_backg);
00946
00947 xsh_free_frame( &frame_backg);
00948 xsh_free_frame( &bpmap);
00949 xsh_free_frame( &rmdark);
00950 xsh_free_frame( &d2_rmdark);
00951 xsh_free_frame( &qth_rmdark);
00952
00953 xsh_free_frame( &d2_frame_backg);
00954 xsh_free_frame( &qth_frame_backg);
00955
00956 xsh_instrument_free( &instrument);
00957
00958 xsh_free_propertylist(&plist);
00959
00960 xsh_free_table(&grid_tab1);
00961 xsh_free_table(&grid_tab2);
00962
00963
00964 return;
00965 }
00966
00967