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 #include <xsh_error.h>
00050
00051 #include <xsh_utils.h>
00052 #include <xsh_msg.h>
00053
00054 #include <xsh_dfs.h>
00055 #include <xsh_pfits.h>
00056 #include <xsh_data_spectrum1D.h>
00057 #include <xsh_utils_image.h>
00058 #include <xsh_model_io.h>
00059 #include <xsh_model_utils.h>
00060 #include <xsh_utils_ifu.h>
00061 #include <xsh_model_arm_constants.h>
00062
00063 #include <xsh_drl.h>
00064 #include <xsh_drl_check.h>
00065
00066 #include <cpl.h>
00067
00068
00069
00070
00071
00072 #define RECIPE_ID "xsh_scired_ifu_offset"
00073 #define RECIPE_AUTHOR "A.Modigliani, J.Vernet, P. Bristow"
00074 #define RECIPE_CONTACT "Andrea.Modigliani@eso.org"
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 static int xsh_scired_ifu_offset_create( cpl_plugin *);
00085 static int xsh_scired_ifu_offset_exec( cpl_plugin *);
00086 static int xsh_scired_ifu_offset_destroy( cpl_plugin *);
00087
00088
00089 static void xsh_scired_ifu_offset( cpl_parameterlist *, cpl_frameset *);
00090
00091
00092
00093
00094 static char xsh_scired_ifu_offset_description_short[] =
00095 "Reduce science IFU on-off exposures and build a 3D cube";
00096
00097 static char xsh_scired_ifu_offset_description[] =
00098 "This recipe Reduce science IFU on-off exposures and build a 3D cube\n\
00099 Input Frames : \n\
00100 - A set of 2xn Science frames, \
00101 Tag = OBJECT_IFU_OFFSET_arm, SKY_IFU_arm\n\
00102 - A spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00103 - A master flat frame (Tag = MASTER_FLAT_IFU_arm)\n\
00104 - An order table frame(Tag = ORDER_TABLE_EDGES_IFU_arm)\n\
00105 - 3 wave solution frames, one per slitlet (Tag = WAVE_TAB_ARC_IFU_slitlet_arm)\n\
00106 where 'slitlet' is DOWN, CEN or UP\n\
00107 - [OPTIONAL] A dispersion table (Tag = DISP_TAB_IFU_arm)\n\
00108 - [OPTIONAL] A non-linear badpixel map (Tag = BP_MAP_NL_arm)\n\
00109 - [OPTIONAL] A reference badpixel map (Tag = BP_MAP_RP_arm)\n\
00110 Products : \n\
00111 - 3 Spectrum order tables 2D (1 per slitlet), PRO.CATG=ORDER2D_slitlet_IFU_arm\n\
00112 - 3 Spectrum order tables 1D (1 per slitlet), PRO.CATG=ORDER1D_slitlet_IFU_arm\n\
00113 - 3 Spectrum merge tables 2D (1 per slitlet), PRO.CATG=MERGE2D_slitlet_IFU_arm\n\
00114 - 3 Spectrum merge tables 1D (1 per slitlet), PRO.CATG=MERGE1D_slitlet_IFU_arm\n\
00115 - 1 Spectrum merge 3D, PRO.CATG=MERGE3D_IFU_arm\n" ;
00116
00117
00118
00119
00120
00129
00130
00131 int cpl_plugin_get_info(cpl_pluginlist *list) {
00132 cpl_recipe *recipe = NULL;
00133 cpl_plugin *plugin = NULL;
00134
00135 recipe = cpl_calloc(1, sizeof(*recipe));
00136 if ( recipe == NULL ){
00137 return -1;
00138 }
00139
00140 plugin = &recipe->interface ;
00141
00142 cpl_plugin_init(plugin,
00143 CPL_PLUGIN_API,
00144 XSH_BINARY_VERSION,
00145 CPL_PLUGIN_TYPE_RECIPE,
00146 RECIPE_ID,
00147 xsh_scired_ifu_offset_description_short,
00148 xsh_scired_ifu_offset_description,
00149 RECIPE_AUTHOR,
00150 RECIPE_CONTACT,
00151 xsh_get_license(),
00152 xsh_scired_ifu_offset_create,
00153 xsh_scired_ifu_offset_exec,
00154 xsh_scired_ifu_offset_destroy);
00155
00156 cpl_pluginlist_append(list, plugin);
00157
00158 return (cpl_error_get_code() != CPL_ERROR_NONE);
00159 }
00160
00161
00171
00172
00173 static int xsh_scired_ifu_offset_create(cpl_plugin *plugin){
00174 cpl_recipe *recipe = NULL;
00175 cpl_parameter* p=NULL;
00176
00177 xsh_clipping_param crh_clip_param = {5.0, 5, 0.7, 0,0.3};
00178
00179 xsh_remove_crh_single_param crh_single = { 0.1, 5, 2.0, 4} ;
00180 xsh_rectify_param rectify = { "default",
00181 CPL_KERNEL_DEFAULT,
00182 4,
00183 -1.0,
00184 -1.0,
00185 1,0,0.};
00186
00187 xsh_localize_obj_param loc_obj =
00188 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3,FALSE};
00189 xsh_stack_param stack_param = {"median",5.,5.,5};
00190
00191
00192
00193
00194
00195 xsh_init();
00196
00197
00198 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
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 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00214 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00215 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00216
00217 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00218 crh_clip_param));
00219
00220
00221 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00222 check(p=xsh_parameters_find(recipe->parameters,RECIPE_ID,"background-method"));
00223 cpl_parameter_set_default_string(p,"poly");
00224
00225
00226 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00227 crh_single )) ;
00228
00229 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00230 rectify )) ;
00231
00232 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00233 loc_obj )) ;
00234
00235
00236 check(xsh_parameters_optimal_extract_create(RECIPE_ID,
00237 recipe->parameters,-1. )) ;
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00249 "compute-map", FALSE,
00250 "if TRUE recompute (wave and slit) maps from the dispersion solution. If sky-subtract is set to TRUE this must be set to TRUE."));
00251
00252 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00253 "trace-obj", FALSE,
00254 "if TRUE trace object position on each IFU slice. In this case order TAB edges is required"));
00255
00256
00257 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00258 "check-afc", TRUE,
00259 "Input AFC corrected model/wave solution and science frame check. If TRUE the recipe verify that the input mode/wave solution is AFC corrected, its INS.OPTIi.NAME is 'Pin_0.5 ', and its OBS.ID and OBS.TARG.NAME values matches with the corresponding values of the science frame."));
00260
00261
00262 cleanup:
00263 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00264 xsh_error_dump(CPL_MSG_ERROR);
00265 return 1;
00266 }
00267 else {
00268 return 0;
00269 }
00270 }
00271
00272
00278
00279
00280 static int xsh_scired_ifu_offset_exec(cpl_plugin *plugin) {
00281 cpl_recipe *recipe = NULL;
00282
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
00294 xsh_scired_ifu_offset(recipe->parameters, recipe->frames);
00295
00296 cleanup:
00297 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00298 xsh_error_dump(CPL_MSG_ERROR);
00299 xsh_error_reset();
00300 return 1;
00301 }
00302 else {
00303 return 0;
00304 }
00305 }
00306
00315 static cpl_error_code
00316 xsh_params_set_defaults(cpl_parameterlist* pars,
00317 const char* rec_id,
00318 xsh_instrument* inst,
00319 xsh_rectify_param * rectify_par,
00320 xsh_background_param* backg)
00321 {
00322 cpl_parameter* p=NULL;
00323 check(p=xsh_parameters_find(pars,RECIPE_ID,"background-radius-x"));
00324 if(cpl_parameter_get_int(p) <= 0) {
00325 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00326 backg->radius_x=1;
00327 } else {
00328 backg->radius_x=2;
00329 }
00330 }
00331 check(xsh_rectify_params_set_defaults(pars,rec_id,inst,rectify_par));
00332 cleanup:
00333
00334 return cpl_error_get_code();
00335 }
00336
00337
00344 static cpl_error_code
00345 xsh_params_monitor(xsh_background_param* backg,
00346 xsh_rectify_param * rectify_par)
00347 {
00348
00349
00350 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00351 backg->sampley,backg->radius_y,backg->smooth_y);
00352
00353 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00354 backg->radius_x,backg->smooth_x);
00355
00356 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00357 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00358 rectify_par->rectif_bin_space);
00359
00360 return cpl_error_get_code();
00361
00362 }
00363
00371
00372
00373 static cpl_error_code
00374 xsh_params_bin_scale(cpl_frameset* raws,
00375 xsh_background_param* backg)
00376 {
00377
00378 cpl_frame* frame=NULL;
00379 const char* name=NULL;
00380 cpl_propertylist* plist=NULL;
00381 int binx=0;
00382 int biny=0;
00383
00384 check(frame=cpl_frameset_get_first(raws));
00385 check(name=cpl_frame_get_filename(frame));
00386 check(plist=cpl_propertylist_load(name,0));
00387 check(binx=xsh_pfits_get_binx(plist));
00388 check(biny=xsh_pfits_get_biny(plist));
00389 xsh_free_propertylist(&plist);
00390
00391 if(biny>1) {
00392
00393
00394
00395
00396
00397
00398 backg->radius_y=backg->radius_y/biny;
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419 }
00420
00421
00422 if(binx>1) {
00423
00424 backg->radius_x=backg->radius_x/binx;
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 }
00440
00441 cleanup:
00442 xsh_free_propertylist(&plist);
00443 return cpl_error_get_code();
00444
00445 }
00446
00447
00448
00454
00455 static int xsh_scired_ifu_offset_destroy(cpl_plugin *plugin)
00456 {
00457 cpl_recipe *recipe = NULL;
00458
00459
00460 xsh_error_reset();
00461
00462 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00463
00464
00465 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00466 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00467
00468 recipe = (cpl_recipe *)plugin;
00469
00470 xsh_free_parameterlist(&recipe->parameters);
00471
00472 cleanup:
00473 if (cpl_error_get_code() != CPL_ERROR_NONE)
00474 {
00475 return 1;
00476 }
00477 else
00478 {
00479 return 0;
00480 }
00481 }
00482
00483
00484
00492
00493 static void xsh_scired_ifu_offset( cpl_parameterlist* parameters,
00494 cpl_frameset* frameset)
00495 {
00496 const char* recipe_tags[4] = {XSH_OBJECT_IFU_OFFSET, XSH_STD_TELL_IFU_OFFSET, XSH_STD_FLUX_IFU_OFFSET, XSH_SKY_IFU};
00497 int recipe_tags_size = 4;
00498
00499
00500
00501 cpl_frame *spectral_format_frame = NULL;
00502 cpl_frame *ifu_cfg_tab_frame = NULL ;
00503 cpl_frame *ifu_cfg_cor_frame = NULL ;
00504
00505
00506 cpl_frameset* raws = NULL;
00507 cpl_frameset* calib = NULL;
00508
00509
00510 cpl_frame* bpmap = NULL;
00511 cpl_frame * master_bias = NULL ;
00512 cpl_frame * master_dark = NULL ;
00513 cpl_frame* master_flat = NULL;
00514 cpl_frame* order_tab_edges = NULL;
00515 cpl_frame * wavesol_frame = NULL ;
00516 cpl_frame * model_config_frame = NULL ;
00517 cpl_frame * wavemap = NULL ;
00518 cpl_frame * spectral_format = NULL ;
00519 cpl_frame * slice_offset_frame = NULL ;
00520 cpl_frameset * object_frameset = NULL, * sky_frameset = NULL ;
00521 xsh_stack_param* stack_par=NULL;
00522
00523 xsh_clipping_param* crh_clipping_par = NULL;
00524 xsh_background_param* backg_par = NULL;
00525 xsh_remove_crh_single_param * crh_single_par = NULL ;
00526 xsh_rectify_param * rectify_par = NULL ;
00527
00528
00529
00530
00531
00532 xsh_instrument* instrument = NULL;
00533 int nb_sub_frames ;
00534 int nb_raw_frames ;
00535
00536
00537
00538 cpl_frameset * sub_frameset = NULL ;
00539 cpl_frameset * rmbkg_frameset = NULL ;
00541 cpl_frameset * clean_frameset = NULL ;
00543 cpl_frame * comb_obj_frame = NULL ;
00544 cpl_frameset * rect2_frameset = NULL ;
00545
00546
00547
00548 cpl_frameset * res_1D_frameset = NULL ;
00549 cpl_frameset * res_2D_frameset = NULL ;
00550 cpl_frame * data_cube = NULL ;
00551 cpl_frameset * ext_frameset = NULL ;
00552 cpl_frameset * ext_frameset_tables = NULL ;
00553
00554 int i ;
00555 char file_name[80], arm_str[16] ;
00556 char * file_tag = NULL;
00557 cpl_frame* grid_back=NULL;
00558 cpl_frame* frame_backg=NULL;
00559 cpl_frameset * ext_frameset_images = NULL ;
00560 cpl_frameset * rect2_frameset_tables = NULL ;
00561
00562
00563 char rec_prefix[80];
00564 cpl_frame* object_frame=NULL;
00565 int check_afc = TRUE;
00566 int do_compute_map = TRUE;
00567 int recipe_use_model = FALSE;
00568 char wave_map_tag[80];
00569 char slit_map_tag[80];
00570 cpl_frame *wavemap_frame = NULL ;
00571 cpl_frame *slitmap_frame = NULL ;
00572 cpl_frame *disp_tab_frame = NULL;
00573 cpl_frame* comb_sky_frame=NULL;
00574 int pre_overscan_corr=0;
00575 cpl_frame* ifu_sky_map=NULL;
00576
00577
00578
00579 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00580 recipe_tags, recipe_tags_size,
00581 RECIPE_ID, XSH_BINARY_VERSION,
00582 xsh_scired_ifu_offset_description_short));
00583
00584 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
00585 "Instrument NOT in Ifu Mode" ) ;
00586
00587 check(xsh_recipe_params_check(parameters,instrument,RECIPE_ID));
00588
00589 check(xsh_ensure_raws_input_offset_recipe_is_proper(raws,instrument));
00590
00591 check( nb_raw_frames = cpl_frameset_get_size( raws ) ) ;
00592 xsh_msg( "Nb of Raw frames: %d", nb_raw_frames ) ;
00593 XSH_ASSURE_NOT_ILLEGAL( nb_raw_frames > 1 ) ;
00594 {
00595
00596 int even_nb = nb_raw_frames % 2 ;
00597 XSH_ASSURE_NOT_ILLEGAL( even_nb == 0 ) ;
00598
00599 }
00600
00601 check(strcpy(rec_prefix,
00602 xsh_set_recipe_file_prefix(raws,"xsh_scired_ifu_offset")));
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612 check(ifu_cfg_tab_frame=xsh_find_frame_with_tag(calib,XSH_IFU_CFG_TAB,instrument));
00613 check(ifu_cfg_cor_frame=xsh_find_frame_with_tag(calib,XSH_IFU_CFG_COR,instrument));
00614 check(spectral_format_frame=xsh_find_spectral_format( calib, instrument ) ) ;
00615
00616
00617 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00618
00619 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00620
00621 check( master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00622 instrument));
00623 }
00624
00625 else {
00626
00627
00628
00629
00630 }
00631 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
00632
00633
00634 check(model_config_frame=xsh_find_frame_with_tag(calib,XSH_MOD_CFG_OPT_AFC,
00635 instrument));
00636
00637 if(model_config_frame==NULL) {
00638 check(wavesol_frame=xsh_find_frame_with_tag(calib,XSH_WAVE_TAB_AFC,
00639 instrument));
00640 }
00641
00642
00643 check( check_afc = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00644 "check-afc"));
00645
00646 check( do_compute_map = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00647 "compute-map"));
00648
00649
00650
00651
00652 XSH_ASSURE_NOT_ILLEGAL( (model_config_frame != NULL && wavesol_frame == NULL ) ||
00653
00654 (model_config_frame == NULL && wavesol_frame != NULL ) );
00655
00656 check( master_flat = xsh_find_master_flat( calib, instrument ) ) ;
00657
00658 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00659 instrument)) == NULL){
00660 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00661 xsh_error_reset();
00662 }
00663
00664 check( wavemap = xsh_find_wavemap( calib, instrument ) ) ;
00665 check( spectral_format = xsh_find_spectral_format( calib, instrument ) ) ;
00666
00667 if ((slice_offset_frame = xsh_find_frame_with_tag( calib, XSH_SLICE_OFFSET,
00668 instrument) ) == NULL ) {
00669 xsh_msg_warning("Frame %s not provided", XSH_SLICE_OFFSET );
00670 xsh_error_reset() ;
00671 }
00672
00673
00674
00675
00676 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00677 check( crh_clipping_par = xsh_parameters_clipping_crh_get(RECIPE_ID,
00678 parameters));
00679 check( backg_par = xsh_parameters_background_get(RECIPE_ID,
00680 parameters));
00681
00682 check( rectify_par = xsh_parameters_rectify_get(RECIPE_ID,
00683 parameters));
00684 check( crh_single_par = xsh_parameters_remove_crh_single_get(RECIPE_ID,
00685 parameters));
00686 if ( rectify_par->rectify_full_slit == 1 ) {
00687 xsh_msg( "Use Full Slit" ) ;
00688 }
00689 else {
00690 xsh_msg( "Use Max Possible Slit" ) ;
00691 }
00692
00693 check(xsh_params_set_defaults(parameters,RECIPE_ID,instrument,rectify_par,
00694 backg_par));
00695
00696 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00697 check(xsh_params_bin_scale(raws,backg_par));
00698 }
00699 check(xsh_params_monitor(backg_par,rectify_par));
00700
00701
00702
00703
00704
00705
00706
00707 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_IFU_OFFSET,
00708 instrument,pre_overscan_corr));
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721 check( nb_sub_frames = xsh_dfs_split_offset( raws, "IFU_OFFSET",
00722 XSH_SKY_IFU, &object_frameset,
00723 &sky_frameset ) ) ;
00724
00725 check( sub_frameset = xsh_subtract_sky_offset( object_frameset, sky_frameset,
00726 nb_sub_frames, instrument)) ;
00727
00728
00729
00730 check(object_frame=cpl_frameset_get_frame(object_frameset,0));
00731 if ( model_config_frame == NULL){
00732 xsh_msg("RECIPE USE WAVE SOLUTION");
00733 recipe_use_model = FALSE;
00734 }
00735 else{
00736 xsh_msg("RECIPE USE MODEL");
00737 recipe_use_model = TRUE;
00738 }
00739
00740 if ( do_compute_map && recipe_use_model==FALSE){
00741 check( disp_tab_frame = xsh_find_disp_tab( calib, instrument));
00742 }
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 if (recipe_use_model){
00763 int found_temp=true;
00764 xsh_msg("correct for temperature");
00765 check(xsh_model_temperature_update_frame(&model_config_frame,
00766 object_frame,
00767 instrument,&found_temp));
00768 }
00769 if ( do_compute_map){
00770 check(xsh_mode_set(instrument,XSH_MODE_IFU));
00771
00772 if (recipe_use_model){
00773
00774
00775 sprintf(wave_map_tag,"%s_%s",rec_prefix,XSH_WAVE_MAP_MODEL);
00776 sprintf(slit_map_tag,"%s_%s",rec_prefix,XSH_SLIT_MAP_MODEL);
00777 check( xsh_create_model_map( model_config_frame, instrument,
00778 wave_map_tag,slit_map_tag,
00779 &wavemap_frame, &slitmap_frame,1));
00780
00781 }
00782 else{
00783 xsh_msg( "Compute the wave map and the slit map");
00784 check( xsh_create_map( disp_tab_frame, order_tab_edges,
00785 object_frame, instrument,
00786 &wavemap_frame, &slitmap_frame,rec_prefix));
00787 }
00788 cpl_frameset_insert( calib, wavemap_frame);
00789 cpl_frameset_insert( calib, slitmap_frame);
00790 }
00791 else{
00792 check( wavemap_frame = xsh_find_wavemap( calib, instrument));
00793 slitmap_frame = xsh_find_slitmap(calib, instrument);
00794 }
00795
00796
00797
00798 check( rmbkg_frameset = cpl_frameset_new() ) ;
00799 check( clean_frameset = cpl_frameset_new() ) ;
00800
00801 sprintf( arm_str, "%s_", xsh_instrument_arm_tostring(instrument) ) ;
00802
00803
00804 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00805 cpl_frame * a_b = NULL ;
00806 cpl_frame * rm_bkg = NULL ;
00807 char str[16] ;
00808
00809 sprintf( str, "%d", i ) ;
00810 a_b = cpl_frameset_get_frame( sub_frameset, i ) ;
00811
00812 xsh_msg("Subtract inter-order background %d", i );
00813 sprintf(file_name,"%s%s",rec_prefix, str) ;
00814 check(rm_bkg = xsh_subtract_background( a_b,
00815 order_tab_edges,
00816 backg_par, instrument,
00817 file_name,&grid_back,
00818 &frame_backg,0));
00819
00820 check( cpl_frameset_insert( rmbkg_frameset, rm_bkg ) ) ;
00821 }
00822
00823 if ( nb_sub_frames < 3 && crh_single_par->nb_iter > 0 ) {
00824 xsh_msg( "Less than 3 frames AND crh_single_niter > 0" ) ;
00825 for ( i = 0 ; i < nb_sub_frames ; i++ ) {
00826 cpl_frame * divided = NULL ;
00827 cpl_frame * rm_crh = NULL ;
00828 cpl_frame * a_b = NULL ;
00829 char str[16] ;
00830
00831 check( a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ) ;
00832 sprintf( str, "%d", i ) ;
00833 file_tag = xsh_stringcat_any( "NO_CRH_IFU_OFFSET_", arm_str,str, NULL ) ;
00834 xsh_msg( "Remove crh (single frame)" ) ;
00835 check( rm_crh = xsh_remove_crh_single( a_b, wavemap,instrument,
00836 crh_single_par,
00837 file_tag ) ) ;
00838 XSH_FREE( file_tag ) ;
00839 xsh_msg( "Divide by flat" ) ;
00840 file_tag = xsh_stringcat_any( "FF_IFU_OFFSET_", arm_str,
00841 str, NULL ) ;
00842 check( divided = xsh_divide_flat( rm_crh,
00843 master_flat, file_tag,
00844 instrument ) ) ;
00845 XSH_FREE( file_tag ) ;
00846 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00847 }
00848 }
00849 else for( i = 0 ; i < nb_sub_frames ; i++ ) {
00850 cpl_frame * divided = NULL ;
00851 cpl_frame * a_b = NULL ;
00852 char str[16] ;
00853
00854
00855 a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ;
00856 sprintf( str, "%d", i ) ;
00857
00858
00859 xsh_msg( "Divide by flat" ) ;
00860 file_tag = xsh_stringcat_any( "FF_IFU_OFFSET_", arm_str,
00861 str, NULL ) ;
00862 check( divided = xsh_divide_flat( a_b,
00863 master_flat, file_tag,
00864 instrument ) ) ;
00865 XSH_FREE( file_tag ) ;
00866 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00867 }
00868
00869
00870 xsh_msg( "Combining all frames" ) ;
00871 file_tag = xsh_stringcat_any( "COMBINED_IFU_OFFSET_", arm_str,
00872 "ALL", NULL ) ;
00873
00874 check( comb_obj_frame = xsh_combine_offset( clean_frameset,
00875 file_tag, stack_par,crh_clipping_par,
00876 instrument, NULL, NULL,0 ) ) ;
00877
00878 XSH_FREE( file_tag ) ;
00879
00880 check(comb_sky_frame=xsh_util_frameset_collapse_mean(sky_frameset,instrument));
00881
00882
00883
00884 check(xsh_build_ifu_cube(comb_obj_frame,ifu_cfg_tab_frame,ifu_cfg_cor_frame,
00885 spectral_format_frame,
00886 model_config_frame,wavesol_frame,
00887 instrument,
00888 frameset,
00889 parameters,rectify_par,RECIPE_ID,rec_prefix,1));
00890
00891
00892
00893
00894 check(xsh_build_ifu_cube(comb_sky_frame,ifu_cfg_tab_frame,ifu_cfg_cor_frame,
00895 spectral_format_frame,
00896 model_config_frame,wavesol_frame,
00897 instrument,
00898 frameset,
00899 parameters,rectify_par,RECIPE_ID,rec_prefix,0));
00900 if(do_compute_map) {
00901 check(ifu_sky_map=xsh_build_ifu_map(comb_obj_frame,wavemap_frame,slitmap_frame,model_config_frame,instrument));
00902 check( xsh_add_product_image( ifu_sky_map, frameset,parameters, RECIPE_ID, instrument,NULL));
00903 }
00904 cleanup:
00905
00906 xsh_end( RECIPE_ID, frameset, parameters );
00907 XSH_FREE(crh_clipping_par);
00908 XSH_FREE( backg_par ) ;
00909 XSH_FREE( crh_single_par ) ;
00910 XSH_FREE( rectify_par ) ;
00911
00912 xsh_instrument_free(&instrument );
00913 xsh_free_frameset(&ext_frameset_images);
00914 xsh_free_frameset(&rect2_frameset_tables);
00915 xsh_free_frameset(&ext_frameset_tables);
00916
00917 xsh_free_frameset(&raws);
00918 xsh_free_frameset(&calib);
00919 xsh_free_frameset( &sub_frameset ) ;
00920 xsh_free_frameset( &clean_frameset ) ;
00921 xsh_free_frameset( &rmbkg_frameset ) ;
00922 xsh_free_frameset(&res_1D_frameset) ;
00923 xsh_free_frameset(&res_2D_frameset) ;
00924 xsh_free_frameset(&rect2_frameset) ;
00925 xsh_free_frameset(&ext_frameset) ;
00926
00927
00928 xsh_free_frame( &frame_backg);
00929
00930 xsh_free_frame( &data_cube ) ;
00931
00932 return;
00933 }
00934