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, 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,UVB,VIS] A map of non linear pixel (Format = QUP,RAW, Tag = BP_MAP_RP_arm)\n\
00113 - [OPTIONAL,NIR] 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 - [OPTIONAL] A DRS setup table to specify the value of parameter detectorder-qc-mode\n\
00116 Products : \n\
00117 - An updated order table with edge UP and edge LOW\
00118 (Format = TABLE, TAG = ORDER_TAB_EDGES_mode_arm)\n\
00119 - A master flat (Format = PRE, PRO.CATG = MASTER_FLAT_mode_arm)\n\
00120 - A background subtracted flat (Format = PRE, PRO.CATG = MFLAT_SUB_BACK_mode_arm)\n\
00121 - The inter-order background frame (Format = PRE, PRO.CATG = MFLAT_BACK_mode_arm)\n\
00122 - The inter-order background sampling points grid table\n\
00123 (Format = PRE, PRO.CATG = MFLAT_GRID_BACK_mode_arm)\n\
00124 Prepare the flat frames.\n\
00125 Stack and sigma clip all the flat frames.\n\
00126 Subtract master bias.\n\
00127 Subtract master dark.\n\
00128 Detect order edge.\n\
00129 Subtract background.\n\
00130 Create the Master Flat.\n";
00131
00132
00133
00134
00135
00144
00145
00146 int cpl_plugin_get_info(cpl_pluginlist *list) {
00147 cpl_recipe *recipe = NULL;
00148 cpl_plugin *plugin = NULL;
00149
00150 recipe = cpl_calloc(1, sizeof(*recipe));
00151 if ( recipe == NULL ){
00152 return -1;
00153 }
00154
00155 plugin = &recipe->interface ;
00156
00157 cpl_plugin_init(plugin,
00158 CPL_PLUGIN_API,
00159 XSH_BINARY_VERSION,
00160 CPL_PLUGIN_TYPE_RECIPE,
00161 RECIPE_ID,
00162 xsh_mflat_description_short,
00163 xsh_mflat_description,
00164 RECIPE_AUTHOR,
00165 RECIPE_CONTACT,
00166 xsh_get_license(),
00167 xsh_mflat_create,
00168 xsh_mflat_exec,
00169 xsh_mflat_destroy);
00170
00171 cpl_pluginlist_append(list, plugin);
00172
00173 return (cpl_error_get_code() != CPL_ERROR_NONE);
00174 }
00175
00176
00186
00187
00188 static int xsh_mflat_create(cpl_plugin *plugin){
00189 cpl_recipe *recipe = NULL;
00190 xsh_clipping_param crh_clip_param = {10.0, 2, 0.7, 0,0.3};
00191 xsh_stack_param stack_param = {"median",5.,5.,5};
00192
00193
00194 xsh_init();
00195
00196
00197 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00198
00199
00200 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00201 CPL_ERROR_TYPE_MISMATCH,
00202 "Plugin is not a recipe");
00203
00204 recipe = (cpl_recipe *)plugin;
00205
00206
00207 recipe->parameters = cpl_parameterlist_new();
00208 assure( recipe->parameters != NULL,
00209 CPL_ERROR_ILLEGAL_OUTPUT,
00210 "Memory allocation failed!");
00211
00212
00213
00214 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00215 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00216 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00217
00218 check( xsh_parameters_clipping_crh_create( RECIPE_ID, recipe->parameters,
00219 crh_clip_param));
00220
00221
00222 check( xsh_parameters_detect_order_create( RECIPE_ID,
00223 recipe->parameters));
00224
00225
00226 check( xsh_parameters_d2_detect_order_create( RECIPE_ID,
00227 recipe->parameters));
00228
00229
00230 check( xsh_parameters_background_create( RECIPE_ID,
00231 recipe->parameters));
00232
00233 cleanup:
00234 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00235 xsh_error_dump(CPL_MSG_ERROR);
00236 return 1;
00237 }
00238 else {
00239 return 0;
00240 }
00241 }
00242
00243
00249
00250
00251 static int xsh_mflat_exec(cpl_plugin *plugin) {
00252 cpl_recipe *recipe = NULL;
00253
00254
00255 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00256
00257
00258 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00259 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00260
00261 recipe = (cpl_recipe *)plugin;
00262
00263
00264 xsh_mflat(recipe->parameters, recipe->frames);
00265
00266 cleanup:
00267 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00268 xsh_error_dump(CPL_MSG_ERROR);
00269
00270 cpl_error_reset();
00271 return 1;
00272 }
00273 else {
00274 return 0;
00275 }
00276 }
00277
00278
00284
00285 static int xsh_mflat_destroy(cpl_plugin *plugin)
00286 {
00287 cpl_recipe *recipe = NULL;
00288
00289
00290 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00291
00292
00293 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00294 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00295
00296 recipe = (cpl_recipe *)plugin;
00297
00298 xsh_free_parameterlist(&recipe->parameters);
00299
00300 cleanup:
00301 if (cpl_error_get_code() != CPL_ERROR_NONE)
00302 {
00303 return 1;
00304 }
00305 else
00306 {
00307 return 0;
00308 }
00309 }
00310
00311
00312
00313
00314 static void
00315 xsh_mflat_recipe_main( cpl_frameset *raw_set,
00316 cpl_frameset *on_set,
00317 cpl_frameset *off_set,
00318 cpl_frame *bpmap_frame,
00319 cpl_frame *master_bias_frame,
00320 cpl_frame *master_dark_frame,
00321 cpl_frame *cen_order_tab_frame,
00322 cpl_frame *qc_cen_order_tab_frame,
00323 xsh_stack_param* stack_par,
00324 xsh_clipping_param *crhclip_par,
00325 xsh_detect_order_param *detectorder_par,
00326 xsh_background_param* background_par,
00327 const int pre_overscan_corr,
00328 cpl_frame **edges_order_tab_frame,
00329 cpl_frame **master_flat_frame,
00330 cpl_frame **rmbackground,
00331 cpl_frame **grid_background,
00332 cpl_frame** frame_backg,
00333 xsh_instrument* instrument)
00334 {
00335 cpl_frameset *onoff_set = NULL;
00336 cpl_frame *rmcrh = NULL;
00337 cpl_frame *rmbias = NULL;
00338 cpl_frame *rmdark = NULL;
00339 char* fname = NULL;
00340 const char* ftag = NULL;
00341
00342 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00343
00344 check(xsh_prepare( raw_set, bpmap_frame, master_bias_frame,
00345 XSH_FLAT, instrument,pre_overscan_corr));
00346
00347 ftag=XSH_GET_TAG_FROM_ARM(XSH_FLAT_REMOVE_CRH,instrument);
00348
00349 check( rmcrh = xsh_remove_crh_multiple( raw_set, ftag, stack_par, crhclip_par,
00350 instrument, NULL,NULL,1));
00351
00352 xsh_msg("Subtract bias");
00353
00354
00355
00356 check( rmbias = xsh_subtract_bias( rmcrh, master_bias_frame,
00357 instrument,"FLAT_",pre_overscan_corr,1));
00358
00359 check(xsh_monitor_flux(rmbias,qc_cen_order_tab_frame, instrument));
00360 if( master_dark_frame != NULL) {
00361 xsh_msg( "Subtract dark");
00362 XSH_NAME_LAMP_MODE_ARM( fname, "FLAT_SUBTRACT_DARK", ".fits",
00363 instrument);
00364 check( rmdark = xsh_subtract_dark( rmbias, master_dark_frame,
00365 fname, instrument));
00366 }
00367 else {
00368 rmdark = cpl_frame_duplicate( rmbias);
00369 }
00370 }
00371
00372 else{
00373 check( xsh_prepare( on_set, bpmap_frame, NULL, "ON", instrument,pre_overscan_corr));
00374 check( xsh_prepare( off_set, bpmap_frame, NULL, "OFF", instrument,pre_overscan_corr));
00375
00376 check( onoff_set = xsh_subtract_nir_on_off( on_set, off_set, instrument));
00377 ftag=XSH_GET_TAG_FROM_ARM(XSH_ONOFF_REMOVE_CRH,instrument);
00378
00379 check( rmdark = xsh_remove_crh_multiple( onoff_set, ftag, stack_par,
00380 crhclip_par, instrument, NULL,NULL,1));
00381 check(xsh_monitor_flux(rmdark,qc_cen_order_tab_frame, instrument));
00382 }
00383
00384
00385 xsh_msg("Detect order edges");
00386 check( *edges_order_tab_frame = xsh_detect_order_edge( rmdark,
00387 cen_order_tab_frame, detectorder_par, instrument));
00388
00389
00390 xsh_msg("Subtract inter-order background");
00391 check( *rmbackground = xsh_subtract_background( rmdark,
00392 *edges_order_tab_frame,
00393 background_par,
00394 instrument, "MFLAT",
00395 grid_background,
00396 frame_backg,1));
00397
00398
00399 check( *master_flat_frame = xsh_create_master_flat( *rmbackground,
00400 instrument));
00401
00402
00403
00404 cleanup:
00405 XSH_FREE( fname);
00406 xsh_free_frameset( &onoff_set);
00407 xsh_free_frame( &rmcrh);
00408 xsh_free_frame( &rmbias);
00409 xsh_free_frame( &rmdark);
00410 return;
00411 }
00412
00413
00414 static cpl_error_code
00415 xsh_params_monitor(xsh_background_param* backg,
00416 xsh_detect_order_param* det_order)
00417 {
00418
00419
00420 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00421 backg->sampley,backg->radius_y,backg->smooth_y);
00422
00423 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00424 backg->radius_x,backg->smooth_x);
00425
00426 xsh_msg_dbg_low("detect order params: min_order_size_x=%d",
00427 det_order->min_order_size_x);
00428
00429
00430
00431 return cpl_error_get_code();
00432
00433 }
00434
00442
00443
00444 static cpl_error_code
00445 xsh_params_bin_scale(cpl_frameset* raws,
00446 xsh_background_param* backg,
00447 xsh_detect_order_param* det_order)
00448 {
00449
00450 cpl_frame* frame=NULL;
00451 const char* name=NULL;
00452 cpl_propertylist* plist=NULL;
00453 int binx=0;
00454 int biny=0;
00455
00456 check(frame=cpl_frameset_get_first(raws));
00457 check(name=cpl_frame_get_filename(frame));
00458 check(plist=cpl_propertylist_load(name,0));
00459 check(binx=xsh_pfits_get_binx(plist));
00460 check(biny=xsh_pfits_get_biny(plist));
00461 xsh_free_propertylist(&plist);
00462
00463 if(biny>1) {
00464
00465
00466
00467 backg->radius_y=backg->radius_y/biny;
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 }
00479
00480
00481 if(binx>1) {
00482
00483 backg->radius_x=backg->radius_x/binx;
00484
00485
00486
00487
00488
00489
00490
00491
00492 det_order->search_window_hsize=det_order->search_window_hsize/biny;
00493 det_order->min_order_size_x=det_order->min_order_size_x/binx;
00494
00495 }
00496
00497 cleanup:
00498 xsh_free_propertylist(&plist);
00499 return cpl_error_get_code();
00500
00501 }
00502
00503 static cpl_error_code
00504 xsh_params_set_defaults(cpl_parameterlist* pars,
00505 xsh_instrument* inst,
00506 xsh_detect_order_param* det_order,
00507 xsh_background_param* backg)
00508 {
00509 cpl_parameter* p=NULL;
00510
00511 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-min-order-size-x"));
00512 if(cpl_parameter_get_int(p) <= 0) {
00513 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00514 det_order->min_order_size_x=40;
00515 } else {
00516 det_order->min_order_size_x=60;
00517 }
00518 }
00519 check(p=xsh_parameters_find(pars,RECIPE_ID,"background-radius-x"));
00520 if(cpl_parameter_get_int(p) <= 0) {
00521 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00522 backg->radius_x=1;
00523 } else {
00524 backg->radius_x=2;
00525 }
00526 }
00527 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-min-sn"));
00528 if(cpl_parameter_get_double(p) <= 0) {
00529 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00530 if(xsh_instrument_get_mode(inst) == XSH_MODE_IFU) {
00531 det_order->min_sn=4;
00532 } else {
00533 det_order->min_sn=25;
00534 }
00535 } else {
00536 if(xsh_instrument_get_mode(inst) == XSH_MODE_IFU) {
00537 det_order->min_sn=20;
00538 } else {
00539 det_order->min_sn=35;
00540 }
00541 }
00542 }
00543
00544 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-slice-trace-method"));
00545 if(strcmp(cpl_parameter_get_string(p),"auto") == 0) {
00546 if (xsh_instrument_get_mode(inst) == XSH_MODE_IFU){
00547 cpl_parameter_set_default_string(p,"sobel");
00548 } else {
00549 cpl_parameter_set_default_string(p,"fixed");
00550 }
00551 }
00552
00553 cleanup:
00554
00555 return cpl_error_get_code();
00556
00557 }
00558
00559
00560
00568
00569 static void xsh_mflat(cpl_parameterlist* parameters, cpl_frameset* frameset)
00570 {
00571 const char* recipe_tags[1] = {XSH_FLAT};
00572 int recipe_tags_size = 1;
00573
00574
00575 cpl_frameset* raws = NULL;
00576 cpl_frameset* calib = NULL;
00577 cpl_frameset* on = NULL;
00578 cpl_frameset* off = NULL;
00579 cpl_frameset* qth = NULL;
00580 cpl_frameset* d2 = NULL;
00581 xsh_instrument* instrument = NULL;
00582
00583 cpl_frame* spectralformat_frame = NULL;
00584 cpl_frame* bpmap = NULL;
00585 cpl_frame* master_bias = NULL;
00586 cpl_frame* master_dark = NULL;
00587 cpl_frame* cen_order_tab_frame = NULL;
00588
00589 cpl_frame* edges_order_tab_frame = NULL;
00590 cpl_frame* master_flat_frame = NULL;
00591 cpl_frame* master_bkg_frame = NULL;
00592
00593 cpl_frame *qth_edges_order_tab_frame = NULL;
00594 cpl_frame *qth_master_flat_frame = NULL;
00595 cpl_frame *d2_edges_order_tab_frame = NULL;
00596 cpl_frame* d2_master_flat_frame = NULL;
00597
00598 xsh_clipping_param* crh_clipping = NULL;
00599 xsh_detect_order_param* det_order = NULL;
00600 xsh_d2_detect_order_param * d2_det_order = NULL;
00601 xsh_background_param* backg = NULL;
00602
00603 cpl_frame *qth_cen_order_tab_frame = NULL;
00604 cpl_frame *d2_cen_order_tab_frame = NULL;
00605 char fname[80];
00606 char fname_d2[80];
00607 char fname_qth[80];
00608 char pcatg[80];
00609 cpl_table* grid_tab1=NULL;
00610 cpl_table* grid_tab2=NULL;
00611 cpl_propertylist* plist=NULL;
00612 cpl_frame* rmbackground=NULL;
00613 cpl_frame* qth_rmbackground=NULL;
00614 cpl_frame* d2_rmbackground=NULL;
00615 cpl_frame* grid_backg=NULL;
00616 cpl_frame* d2_grid_backg=NULL;
00617 cpl_frame* qth_grid_backg=NULL;
00618 cpl_frame* frame_backg=NULL;
00619 cpl_frame* d2_frame_backg=NULL;
00620 cpl_frame* qth_frame_backg=NULL;
00621 cpl_frame* cpix_frm=NULL;
00622 cpl_frame* hpix_frm=NULL;
00623 int pre_overscan_corr=0;
00624 xsh_stack_param* stack_par=NULL;
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00635 recipe_tags, recipe_tags_size,
00636 RECIPE_ID, XSH_BINARY_VERSION,
00637 xsh_mflat_description_short ) ) ;
00638
00639 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00640 if(instrument->arm == XSH_ARM_NIR) {
00641 xsh_instrument_nir_corr_if_JH(raws,instrument);
00642 }
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00661 check( cen_order_tab_frame = xsh_find_order_tab_centr( calib, instrument));
00662
00663 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00664 XSH_SPECTRAL_FORMAT, instrument));
00665
00666
00667
00668 if (xsh_instrument_get_arm(instrument) == XSH_ARM_UVB){
00669 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) >= 2);
00670 XSH_ASSURE_NOT_ILLEGAL( instrument->lamp == XSH_LAMP_QTH_D2);
00671 }
00672 else{
00673 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) >= 1);
00674 }
00675
00676
00677
00678 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00679
00680 check(master_bias = xsh_find_master_bias( calib, instrument));
00681
00682
00683
00684 if((master_dark = xsh_find_master_dark( calib, instrument))==NULL) {
00685
00686
00687 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00688 xsh_error_reset();
00689 }
00690 if ( xsh_instrument_get_arm( instrument) == XSH_ARM_UVB){
00691 check( xsh_dfs_split_qth_d2( raws, &qth, &d2));
00692 }
00693 }
00694
00695 else {
00696
00697 check(xsh_dfs_split_nir(raws,&on,&off));
00698 }
00699
00700
00701 check( xsh_instrument_update_from_spectralformat( instrument,
00702 spectralformat_frame));
00703
00704
00705
00706
00707 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00708 "pre-overscan-corr"));
00709 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00710 check( crh_clipping = xsh_parameters_clipping_crh_get( RECIPE_ID,
00711 parameters));
00712 check( det_order = xsh_parameters_detect_order_get( RECIPE_ID,
00713 parameters,parameters));
00714 check( d2_det_order = xsh_parameters_d2_detect_order_get( RECIPE_ID,
00715 parameters));
00716 check( backg = xsh_parameters_background_get( RECIPE_ID,
00717 parameters));
00718
00719 xsh_params_set_defaults(parameters,instrument,det_order,backg);
00720
00721 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00722 check(xsh_params_bin_scale(raws,backg,det_order));
00723 }
00724 check(xsh_params_monitor(backg,det_order));
00725
00726
00727
00728
00729
00730 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_UVB){
00731
00732 check( xsh_mflat_recipe_main(raws, on, off,bpmap,master_bias,
00733 master_dark, cen_order_tab_frame,
00734 cen_order_tab_frame,stack_par,
00735 crh_clipping, det_order, backg,
00736 pre_overscan_corr,
00737 &edges_order_tab_frame, &master_flat_frame,
00738 &rmbackground, &grid_backg,&frame_backg,
00739 instrument));
00740
00741
00742 xsh_msg("Detect dead-saturated pixels");
00743 hpix_frm = xsh_image_mflat_detect_hotcold(master_flat_frame, instrument, 1);
00744 cpix_frm = xsh_image_mflat_detect_hotcold(master_flat_frame, instrument, 0);
00745 if (bpmap != NULL) {
00746 check(xsh_badpixelmap_coadd(bpmap,hpix_frm));
00747 check(xsh_badpixelmap_coadd(bpmap,cpix_frm));
00748 } else {
00749 bpmap = xsh_badpixelmap_crea_master_from_bpmap(hpix_frm, instrument);
00750 check(xsh_badpixelmap_coadd(bpmap,cpix_frm));
00751 }
00752
00753 sprintf(pcatg, "%s_%s", XSH_MASTER_BP_MAP_FLAT,
00754 xsh_instrument_arm_tostring(instrument));
00755 cpl_frame_set_tag(bpmap, pcatg);
00756 if(bpmap!=NULL) {
00757 xsh_frame_qual_update(master_flat_frame,bpmap,instrument);
00758 }
00759
00760
00761
00762 xsh_msg("Saving products");
00763 check( xsh_add_product_table( edges_order_tab_frame, frameset,
00764 parameters,RECIPE_ID,instrument,NULL));
00765
00766 check( xsh_add_product_pre( master_flat_frame, frameset,
00767 parameters, RECIPE_ID, instrument));
00768
00769 check( xsh_add_product_image( rmbackground, frameset,
00770 parameters, RECIPE_ID, instrument,NULL));
00771
00772 check( xsh_add_product_image( frame_backg, frameset, parameters, RECIPE_ID,
00773 instrument,NULL));
00774
00775 check(xsh_add_product_table( grid_backg, frameset,parameters,
00776 RECIPE_ID, instrument,NULL));
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 } else {
00788
00789
00790 check( xsh_order_split_qth_d2( cen_order_tab_frame, spectralformat_frame,
00791 &qth_cen_order_tab_frame, &d2_cen_order_tab_frame, instrument));
00792
00793 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_QTH));
00794 check( xsh_mflat_recipe_main( qth, on, off,bpmap, master_bias,
00795 master_dark, qth_cen_order_tab_frame,
00796 cen_order_tab_frame,stack_par,
00797 crh_clipping, det_order, backg,
00798 pre_overscan_corr,
00799 &qth_edges_order_tab_frame,
00800 &qth_master_flat_frame,
00801 &qth_rmbackground, &qth_grid_backg,
00802 &qth_frame_backg,instrument));
00803
00804 check( xsh_add_product_table( qth_edges_order_tab_frame, frameset,
00805 parameters,RECIPE_ID,instrument,NULL));
00806 check( xsh_add_product_pre( qth_master_flat_frame, frameset,
00807 parameters, RECIPE_ID, instrument));
00808 check( xsh_add_product_image( qth_rmbackground, frameset,
00809 parameters, RECIPE_ID, instrument,NULL));
00810 check( xsh_add_product_image( qth_frame_backg, frameset,
00811 parameters, RECIPE_ID, instrument,NULL));
00812
00813
00814 det_order->min_sn = d2_det_order->min_sn;
00815 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_D2));
00816 check( xsh_mflat_recipe_main( d2, on, off,bpmap, master_bias,
00817 master_dark, d2_cen_order_tab_frame,
00818 cen_order_tab_frame,stack_par,
00819 crh_clipping, det_order, backg,
00820 pre_overscan_corr,
00821 &d2_edges_order_tab_frame,
00822 &d2_master_flat_frame,
00823 &d2_rmbackground,&d2_grid_backg,
00824 &d2_frame_backg,instrument));
00825
00826
00827
00828 sprintf(fname_d2,"MFLAT_GRID_BACK_D2_%s_%s.fits",
00829 xsh_instrument_mode_tostring( instrument),
00830 xsh_instrument_arm_tostring( instrument));
00831
00832 sprintf(fname_qth,"MFLAT_GRID_BACK_QTH_%s_%s.fits",
00833 xsh_instrument_mode_tostring( instrument),
00834 xsh_instrument_arm_tostring( instrument));
00835
00836 check(grid_tab1=cpl_table_load(fname_d2,1,0));
00837 check(grid_tab2=cpl_table_load(fname_qth,1,0));
00838 check(cpl_table_insert(grid_tab1,grid_tab2,cpl_table_get_nrow(grid_tab1)));
00839 check(plist=cpl_propertylist_load(fname_d2,0));
00840 sprintf(pcatg,"%s_%s_%s","MFLAT_GRID_BACK",
00841 xsh_instrument_mode_tostring( instrument),
00842 xsh_instrument_arm_tostring( instrument));
00843 check( xsh_pfits_set_pcatg(plist,pcatg));
00844
00845
00846 sprintf(fname,"%s.fits",pcatg);
00847 check(cpl_table_save( grid_tab1, plist, NULL, fname, CPL_IO_DEFAULT));
00848
00849
00850 grid_backg=xsh_frame_product(fname,pcatg,CPL_FRAME_TYPE_TABLE,
00851 CPL_FRAME_GROUP_CALIB,CPL_FRAME_LEVEL_FINAL);
00852
00853 xsh_free_propertylist(&plist);
00854 xsh_free_table(&grid_tab1);
00855 xsh_free_table(&grid_tab2);
00856
00857
00858 check( xsh_add_product_table( d2_edges_order_tab_frame, frameset,
00859 parameters,RECIPE_ID,instrument,NULL));
00860 check( xsh_add_product_pre( d2_master_flat_frame, frameset,
00861 parameters, RECIPE_ID, instrument));
00862
00863 check( xsh_add_product_image( d2_rmbackground, frameset,
00864 parameters, RECIPE_ID, instrument,NULL));
00865
00866 check( xsh_add_product_image( d2_frame_backg, frameset,
00867 parameters, RECIPE_ID, instrument,NULL));
00868
00869 check(xsh_add_product_table(grid_backg,frameset,parameters,
00870 RECIPE_ID, instrument,NULL));
00871
00872
00873
00874
00875 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00876
00877
00878 check( xsh_flat_merge_qth_d2( qth_master_flat_frame,
00879 qth_edges_order_tab_frame,
00880 d2_master_flat_frame,
00881 d2_edges_order_tab_frame,
00882 qth_frame_backg,d2_frame_backg,
00883 &master_flat_frame, &master_bkg_frame,
00884 &edges_order_tab_frame, instrument));
00885
00886
00887 check( xsh_add_product_table( edges_order_tab_frame, frameset,
00888 parameters,RECIPE_ID,instrument,NULL));
00889
00890
00891 xsh_msg("Detect dead-saturated pixels");
00892 hpix_frm=xsh_image_mflat_detect_hotcold(master_flat_frame,instrument,1);
00893 cpix_frm=xsh_image_mflat_detect_hotcold(master_flat_frame,instrument,0);
00894 if(bpmap!=NULL) {
00895
00896 check(xsh_badpixelmap_coadd(bpmap,hpix_frm));
00897 check(xsh_badpixelmap_coadd(bpmap,cpix_frm));
00898 } else {
00899 bpmap=xsh_badpixelmap_crea_master_from_bpmap(hpix_frm,instrument);
00900 check(xsh_badpixelmap_coadd(bpmap,cpix_frm));
00901 }
00902
00903 if(bpmap!=NULL) {
00904 xsh_frame_qual_update(master_flat_frame,bpmap,instrument);
00905 }
00906
00907 check( xsh_add_product_pre( master_flat_frame, frameset,
00908 parameters, RECIPE_ID, instrument));
00909
00910
00911 check( xsh_add_product_image( master_bkg_frame, frameset,
00912 parameters, RECIPE_ID, instrument,NULL));
00913
00914
00915
00916
00917
00918
00919
00920 }
00921 xsh_msg("xsh_mflat success!!");
00922
00923 cleanup:
00924 xsh_end( RECIPE_ID, frameset, parameters);
00925 XSH_FREE( crh_clipping);
00926 XSH_FREE( det_order);
00927 XSH_FREE( d2_det_order);
00928 XSH_FREE( backg);
00929 XSH_FREE( stack_par);
00930
00931 xsh_free_frameset( &raws);
00932 xsh_free_frameset( &calib);
00933 xsh_free_frameset( &on);
00934 xsh_free_frameset( &off);
00935 xsh_free_frameset( &qth);
00936 xsh_free_frameset( &d2);
00937
00938 xsh_free_frame( &qth_cen_order_tab_frame);
00939 xsh_free_frame( &qth_edges_order_tab_frame);
00940 xsh_free_frame( &qth_master_flat_frame);
00941 xsh_free_frame( &cpix_frm);
00942 xsh_free_frame( &hpix_frm);
00943
00944
00945 xsh_free_frame( &d2_cen_order_tab_frame);
00946 xsh_free_frame( &d2_edges_order_tab_frame);
00947 xsh_free_frame( &d2_master_flat_frame);
00948
00949
00950 xsh_free_frame( &edges_order_tab_frame);
00951 xsh_free_frame( &master_flat_frame);
00952 xsh_free_frame( &master_bkg_frame);
00953
00954 xsh_free_frame( &rmbackground);
00955 xsh_free_frame( &d2_rmbackground);
00956 xsh_free_frame( &qth_rmbackground);
00957 xsh_free_frame( &grid_backg);
00958 xsh_free_frame( &d2_grid_backg);
00959 xsh_free_frame( &qth_grid_backg);
00960
00961 xsh_free_frame( &frame_backg);
00962 xsh_free_frame( &bpmap);
00963
00964
00965 xsh_free_frame( &qth_frame_backg);
00966
00967 xsh_instrument_free( &instrument);
00968
00969 xsh_free_propertylist(&plist);
00970
00971 xsh_free_table(&grid_tab1);
00972 xsh_free_table(&grid_tab2);
00973
00974
00975
00976 return;
00977 }
00978
00979