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