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
00058 #include <xsh_drl.h>
00059
00060 #include <cpl.h>
00061
00062
00063
00064
00065
00066 #define RECIPE_ID "xsh_scired_ifu_offset"
00067 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer"
00068 #define RECIPE_CONTACT "amodigli@eso.org"
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 static int xsh_scired_ifu_offset_create( cpl_plugin *);
00079 static int xsh_scired_ifu_offset_exec( cpl_plugin *);
00080 static int xsh_scired_ifu_offset_destroy( cpl_plugin *);
00081
00082
00083 static void xsh_scired_ifu_offset( cpl_parameterlist *, cpl_frameset *);
00084
00085
00086
00087
00088 static char xsh_scired_ifu_offset_description_short[] =
00089 "Reduce science exposure in IFU configuration and on/off mode";
00090
00091 static char xsh_scired_ifu_offset_description[] =
00092 "This recipe reduces science exposure in IFU configuration and on/off mode\n\
00093 Input Frames : \n\
00094 - A set of 2xn Science frames, \
00095 Tag = OBJECT_IFU_OFFSET_arm, SKY_IFU_arm\n\
00096 - A spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00097 - A master flat frame (Tag = MASTER_FLAT_IFU_arm)\n\
00098 - An order table frame(Tag = ORDER_TABLE_EDGES_IFU_arm)\n\
00099 - 3 wave solution frames, one per slitlet (Tag = WAVE_TAB_ARC_IFU_slitlet_arm)\n\
00100 where 'slitlet' is DOWN, CEN or UP\n\
00101 - [OPTIONAL] A dispersion table (Tag = DISP_TAB_IFU_arm)\n\
00102 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
00103 Products : \n\
00104 - 3 Spectrum order tables 2D (1 per slitlet), PRO.CATG=ORDER2D_slitlet_IFU_arm\n\
00105 - 3 Spectrum order tables 1D (1 per slitlet), PRO.CATG=ORDER1D_slitlet_IFU_arm\n\
00106 - 3 Spectrum merge tables 2D (1 per slitlet), PRO.CATG=MERGE2D_slitlet_IFU_arm\n\
00107 - 3 Spectrum merge tables 1D (1 per slitlet), PRO.CATG=MERGE1D_slitlet_IFU_arm\n\
00108 - 1 Spectrum merge 3D, PRO.CATG=MERGE3D_IFU_arm\n" ;
00109
00110
00111
00112
00113
00122
00123
00124 int cpl_plugin_get_info(cpl_pluginlist *list) {
00125 cpl_recipe *recipe = NULL;
00126 cpl_plugin *plugin = NULL;
00127
00128 recipe = cpl_calloc(1, sizeof(*recipe));
00129 if ( recipe == NULL ){
00130 return -1;
00131 }
00132
00133 plugin = &recipe->interface ;
00134
00135 cpl_plugin_init(plugin,
00136 CPL_PLUGIN_API,
00137 XSH_BINARY_VERSION,
00138 CPL_PLUGIN_TYPE_RECIPE,
00139 RECIPE_ID,
00140 xsh_scired_ifu_offset_description_short,
00141 xsh_scired_ifu_offset_description,
00142 RECIPE_AUTHOR,
00143 RECIPE_CONTACT,
00144 xsh_get_license(),
00145 xsh_scired_ifu_offset_create,
00146 xsh_scired_ifu_offset_exec,
00147 xsh_scired_ifu_offset_destroy);
00148
00149 cpl_pluginlist_append(list, plugin);
00150
00151 return (cpl_error_get_code() != CPL_ERROR_NONE);
00152 }
00153
00154
00164
00165
00166 static int xsh_scired_ifu_offset_create(cpl_plugin *plugin){
00167 cpl_recipe *recipe = NULL;
00168 cpl_parameter* p=NULL;
00169
00170 xsh_clipping_param crh_clip_param = {0.3, 5.0, 5, 0.7, 0};
00171
00172 xsh_remove_crh_single_param crh_single = { 0.1, 5, 2.0, 4} ;
00173 xsh_rectify_param rectify = { "default",
00174 CPL_KERNEL_DEFAULT,
00175 4,
00176 XSH_WAVE_BIN_SIZE_PIPE_NIR,
00177 XSH_SLIT_BIN_SIZE_PIPE_NIR,
00178 1,0,0.};
00179
00180 xsh_localize_obj_param loc_obj =
00181 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3};
00182 xsh_extract_param extract_par =
00183 { LOCALIZATION_METHOD};
00184
00185
00186 xsh_init();
00187
00188
00189 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00190
00191 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00192 CPL_ERROR_TYPE_MISMATCH,
00193 "Plugin is not a recipe");
00194
00195 recipe = (cpl_recipe *)plugin;
00196
00197
00198 recipe->parameters = cpl_parameterlist_new();
00199 assure( recipe->parameters != NULL,
00200 CPL_ERROR_ILLEGAL_OUTPUT,
00201 "Memory allocation failed!");
00202
00203
00204 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00205 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00206
00207
00208 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00209 crh_clip_param));
00210
00211
00212 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00213 check(p=xsh_parameters_find(recipe->parameters,RECIPE_ID,"background-method"));
00214 cpl_parameter_set_default_string(p,"poly");
00215
00216
00217 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00218 crh_single )) ;
00219
00220 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00221 rectify )) ;
00222
00223 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00224 loc_obj )) ;
00225
00226
00227 check(xsh_parameters_optimal_extract_create(RECIPE_ID,
00228 recipe->parameters,-1. )) ;
00229
00230 check(xsh_parameters_extract_create(RECIPE_ID,
00231 recipe->parameters,
00232 extract_par,LOCALIZATION_METHOD )) ;
00233
00234 check(xsh_parameters_merge_ord_create(RECIPE_ID,
00235 recipe->parameters,
00236 WEIGHTED_MERGE_METHOD)) ;
00237
00238 cleanup:
00239 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00240 xsh_error_dump(CPL_MSG_ERROR);
00241 return 1;
00242 }
00243 else {
00244 return 0;
00245 }
00246 }
00247
00248
00254
00255
00256 static int xsh_scired_ifu_offset_exec(cpl_plugin *plugin) {
00257 cpl_recipe *recipe = NULL;
00258
00259
00260
00261 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00262
00263
00264 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00265 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00266
00267 recipe = (cpl_recipe *)plugin;
00268
00269
00270 xsh_scired_ifu_offset(recipe->parameters, recipe->frames);
00271
00272 cleanup:
00273 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00274 xsh_error_dump(CPL_MSG_ERROR);
00275 xsh_error_reset();
00276 return 1;
00277 }
00278 else {
00279 return 0;
00280 }
00281 }
00282
00283 static cpl_error_code
00284 xsh_params_set_defaults(cpl_parameterlist* pars,
00285 xsh_instrument* inst,
00286 xsh_rectify_param * rectify_par)
00287 {
00288 cpl_parameter* p=NULL;
00289
00290 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00291
00292 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-slit"));
00293 if(xsh_parameter_get_default_flag(p) == 0) {
00294
00295 rectify_par->rectif_bin_space=XSH_SLIT_BIN_SIZE_PIPE_NIR;
00296 cpl_parameter_set_double(p,XSH_SLIT_BIN_SIZE_PIPE_NIR);
00297 }
00298
00299 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-lambda"));
00300 if(xsh_parameter_get_default_flag(p) == 0) {
00301
00302 rectify_par->rectif_bin_lambda=XSH_WAVE_BIN_SIZE_PIPE_NIR;
00303 cpl_parameter_set_double(p,XSH_WAVE_BIN_SIZE_PIPE_NIR);
00304 }
00305
00306
00307
00308 } else {
00309
00310
00311 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-slit"));
00312 if(xsh_parameter_get_default_flag(p) == 0) {
00313
00314 rectify_par->rectif_bin_space=XSH_SLIT_BIN_SIZE_PIPE_UVB;
00315 cpl_parameter_set_double(p,XSH_SLIT_BIN_SIZE_PIPE_UVB);
00316 }
00317
00318 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-lambda"));
00319 if(xsh_parameter_get_default_flag(p) == 0) {
00320
00321 rectify_par->rectif_bin_lambda=XSH_WAVE_BIN_SIZE_PIPE_UVB;
00322 cpl_parameter_set_double(p,XSH_WAVE_BIN_SIZE_PIPE_UVB);
00323 }
00324
00325
00326 }
00327
00328 cleanup:
00329
00330 return cpl_error_get_code();
00331
00332 }
00333
00334
00335
00336 static cpl_error_code
00337 xsh_params_monitor(xsh_background_param* backg,
00338 xsh_rectify_param * rectify_par,
00339 xsh_localize_obj_param * loc_obj_par)
00340 {
00341
00342
00343 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00344 backg->sampley,backg->radius_y,backg->smooth_y);
00345
00346 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00347 backg->radius_x,backg->smooth_x);
00348
00349 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00350 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00351 rectify_par->rectif_bin_space);
00352
00353 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00354 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00355
00356 return cpl_error_get_code();
00357
00358 }
00359
00367
00368
00369 static cpl_error_code
00370 xsh_params_bin_scale(cpl_frameset* raws,
00371 xsh_background_param* backg)
00372 {
00373
00374 cpl_frame* frame=NULL;
00375 const char* name=NULL;
00376 cpl_propertylist* plist=NULL;
00377 int binx=0;
00378 int biny=0;
00379
00380 check(frame=cpl_frameset_get_first(raws));
00381 check(name=cpl_frame_get_filename(frame));
00382 check(plist=cpl_propertylist_load(name,0));
00383 check(binx=xsh_pfits_get_binx(plist));
00384 check(biny=xsh_pfits_get_biny(plist));
00385 xsh_free_propertylist(&plist);
00386
00387 if(biny>1) {
00388
00389
00390
00391
00392
00393
00394 backg->radius_y=backg->radius_y/biny;
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 }
00421
00422
00423 if(binx>1) {
00424
00425 backg->radius_x=backg->radius_x/binx;
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 }
00446
00447 cleanup:
00448 xsh_free_propertylist(&plist);
00449 return cpl_error_get_code();
00450
00451 }
00452
00453
00454
00460
00461 static int xsh_scired_ifu_offset_destroy(cpl_plugin *plugin)
00462 {
00463 cpl_recipe *recipe = NULL;
00464
00465
00466 xsh_error_reset();
00467
00468 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00469
00470
00471 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00472 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00473
00474 recipe = (cpl_recipe *)plugin;
00475
00476 xsh_free_parameterlist(&recipe->parameters);
00477
00478 cleanup:
00479 if (cpl_error_get_code() != CPL_ERROR_NONE)
00480 {
00481 return 1;
00482 }
00483 else
00484 {
00485 return 0;
00486 }
00487 }
00488
00489
00497
00498 static void xsh_scired_ifu_offset( cpl_parameterlist* parameters,
00499 cpl_frameset* frameset)
00500 {
00501 const char* recipe_tags[4] = {XSH_OBJECT_IFU_OFFSET, XSH_STD_TELL_IFU_OFFSET, XSH_STD_FLUX_IFU_OFFSET, XSH_SKY_IFU};
00502 int recipe_tags_size = 4;
00503
00504
00505 cpl_frameset* raws = NULL;
00506 cpl_frameset* calib = NULL;
00507
00508
00509 cpl_frame* bpmap = NULL;
00510 cpl_frame * master_bias = NULL ;
00511 cpl_frame * master_dark = NULL ;
00512 cpl_frame* master_flat = NULL;
00513 cpl_frame* order_tab_edges = NULL;
00514 cpl_frameset * wave_tab_frameset = NULL ;
00515 cpl_frame * model_config_frame = NULL ;
00516 cpl_frame * wavemap = NULL ;
00517 cpl_frame * spectral_format = NULL ;
00518 cpl_frame * slice_offset_frame = NULL ;
00519 cpl_frameset * object_frameset = NULL, * sky_frameset = NULL ;
00520
00521
00522 xsh_clipping_param* crh_clipping_par = NULL;
00523 xsh_background_param* backg_par = NULL;
00524 xsh_remove_crh_single_param * crh_single_par = NULL ;
00525 xsh_rectify_param * rectify_par = NULL ;
00526 xsh_localize_obj_param * loc_obj_par = NULL ;
00527
00528 double opt_kappa = -1. ;
00529 xsh_merge_param* merge_par = NULL;
00530 xsh_extract_param * extract_par = NULL ;
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_frame = NULL ;
00544 cpl_frameset * rect2_frameset = NULL ;
00545 cpl_frameset * loc_table_frameset = NULL ;
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 cpl_frameset * rect_frameset_tab=NULL;
00562 cpl_frameset * rect_frameset_eso=NULL;
00563 char rec_prefix[80];
00564 int pre_overscan_corr=0;
00565
00566
00567
00568
00569 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00570 recipe_tags, recipe_tags_size,
00571 RECIPE_ID, XSH_BINARY_VERSION,
00572 xsh_scired_ifu_offset_description_short));
00573
00574 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
00575 "Instrument NOT in Ifu Mode" ) ;
00576
00577
00578 xsh_ensure_raws_input_offset_recipe_is_proper(raws,instrument);
00579
00580 check( nb_raw_frames = cpl_frameset_get_size( raws ) ) ;
00581 xsh_msg_dbg_low( "Nb of Raw frames: %d", nb_raw_frames ) ;
00582 XSH_ASSURE_NOT_ILLEGAL( nb_raw_frames > 1 ) ;
00583 {
00584 int even_nb = nb_raw_frames % 2 ;
00585 XSH_ASSURE_NOT_ILLEGAL( even_nb == 0 ) ;
00586 }
00587
00588 check(strcpy(rec_prefix,
00589 xsh_set_recipe_file_prefix(raws,"xsh_scired_ifu_offset")));
00590
00591
00592
00593
00594
00595
00596
00597 check(bpmap = xsh_find_master_bpmap(calib));
00598
00599
00600 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00601
00602 check( master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00603 instrument));
00604 }
00605
00606 else {
00607
00608
00609
00610
00611 }
00612
00613 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
00614
00615
00616 if((model_config_frame = xsh_find_frame_with_tag(calib,
00617 XSH_MOD_CFG_OPT_2D,
00618 instrument)) == NULL) {
00619
00620 xsh_error_reset();
00621
00622 if ((model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
00623 instrument)) == NULL) {
00624 xsh_error_reset();
00625 }
00626
00627 }
00628
00629 cpl_error_reset() ;
00630
00631 wave_tab_frameset = xsh_find_wave_tab_ifu( calib, instrument ) ;
00632 cpl_error_reset() ;
00633
00634
00635
00636 XSH_ASSURE_NOT_ILLEGAL( (model_config_frame != NULL && wave_tab_frameset == NULL ) ||
00637 (model_config_frame == NULL && wave_tab_frameset != NULL ) );
00638
00639 check( master_flat = xsh_find_master_flat( calib, instrument ) ) ;
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 check( wavemap = xsh_find_wavemap( calib, instrument ) ) ;
00648 check( spectral_format = xsh_find_spectral_format( calib, instrument ) ) ;
00649
00650 if ((slice_offset_frame = xsh_find_frame_with_tag( calib, XSH_SLICE_OFFSET,
00651 instrument) ) == NULL ) {
00652 xsh_msg_warning("Frame %s not provided", XSH_SLICE_OFFSET );
00653 xsh_error_reset() ;
00654 }
00655
00656
00657
00658
00659 check( crh_clipping_par = xsh_parameters_clipping_crh_get(RECIPE_ID,
00660 parameters));
00661 check( backg_par = xsh_parameters_background_get(RECIPE_ID,
00662 parameters));
00663
00664 check( loc_obj_par = xsh_parameters_localize_obj_get(RECIPE_ID,
00665 parameters));
00666 check( rectify_par = xsh_parameters_rectify_get(RECIPE_ID,
00667 parameters));
00668 check( crh_single_par = xsh_parameters_remove_crh_single_get(RECIPE_ID,
00669 parameters));
00670 if ( rectify_par->rectify_full_slit == 1 ) {
00671 xsh_msg( "Use Full Slit" ) ;
00672 }
00673 else {
00674 xsh_msg( "Use Max Possible Slit" ) ;
00675 }
00676 check( opt_kappa = xsh_parameters_optimal_extract_get_kappa(RECIPE_ID,
00677 parameters));
00678 check( merge_par = xsh_parameters_merge_ord_get(RECIPE_ID,
00679 parameters));
00680 check(extract_par=xsh_parameters_extract_get(RECIPE_ID, parameters )) ;
00681
00682
00683
00684 check(xsh_params_set_defaults(parameters,instrument,rectify_par));
00685
00686 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00687 check(xsh_params_bin_scale(raws,backg_par));
00688 }
00689 check(xsh_params_monitor(backg_par,rectify_par,loc_obj_par));
00690
00691
00692
00693
00694
00695
00696
00697 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_IFU_OFFSET,
00698 instrument,pre_overscan_corr));
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711 check( nb_sub_frames = xsh_dfs_split_offset( raws, XSH_OBJECT_IFU_OFFSET,
00712 XSH_SKY_IFU, &object_frameset,
00713 &sky_frameset ) ) ;
00714
00715 check( sub_frameset = xsh_subtract_sky_offset( object_frameset, sky_frameset,
00716 nb_sub_frames, instrument )) ;
00717 check( rmbkg_frameset = cpl_frameset_new() ) ;
00718 check( clean_frameset = cpl_frameset_new() ) ;
00719
00720 sprintf( arm_str, "%s_", xsh_instrument_arm_tostring(instrument) ) ;
00721
00722
00723 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00724 cpl_frame * a_b = NULL ;
00725 cpl_frame * rm_bkg = NULL ;
00726 char str[16] ;
00727
00728 sprintf( str, "%d", i ) ;
00729 a_b = cpl_frameset_get_frame( sub_frameset, i ) ;
00730 xsh_msg( "***** Frame %s", cpl_frame_get_filename( a_b ) ) ;
00731
00732 xsh_msg("Subtract inter-order background %d", i );
00733 sprintf(file_name, "%s%s",rec_prefix, str) ;
00734 check(rm_bkg = xsh_subtract_background( a_b,
00735 order_tab_edges,
00736 backg_par, instrument,
00737 file_name,&grid_back,
00738 &frame_backg));
00739
00740 check( cpl_frameset_insert( rmbkg_frameset, rm_bkg ) ) ;
00741 }
00742
00743 if ( nb_sub_frames < 3 && crh_single_par->nb_iter > 0 ) {
00744 xsh_msg( "Less than 3 frames AND crh_single_niter > 0" ) ;
00745 for ( i = 0 ; i < nb_sub_frames ; i++ ) {
00746 cpl_frame * divided = NULL ;
00747 cpl_frame * rm_crh = NULL ;
00748 cpl_frame * a_b = NULL ;
00749 char str[16] ;
00750
00751 check( a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ) ;
00752 sprintf( str, "%d", i ) ;
00753 file_tag = xsh_stringcat_any( "NO_CRH_IFU_OFFSET_", arm_str,str, NULL ) ;
00754 xsh_msg( "Remove crh (single frame)" ) ;
00755 check( rm_crh = xsh_remove_crh_single( a_b, wavemap,instrument,
00756 crh_single_par,
00757 file_tag ) ) ;
00758 XSH_FREE( file_tag ) ;
00759 xsh_msg( "Divide by flat" ) ;
00760 file_tag = xsh_stringcat_any( "FF_IFU_OFFSET_", arm_str,
00761 str, NULL ) ;
00762 check( divided = xsh_divide_flat( rm_crh,
00763 master_flat, file_tag,
00764 instrument ) ) ;
00765 XSH_FREE( file_tag ) ;
00766 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00767 }
00768 }
00769 else for( i = 0 ; i < nb_sub_frames ; i++ ) {
00770 cpl_frame * divided = NULL ;
00771 cpl_frame * a_b = NULL ;
00772 char str[16] ;
00773
00774
00775 a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ;
00776 sprintf( str, "%d", i ) ;
00777
00778
00779 xsh_msg( "Divide by flat" ) ;
00780 file_tag = xsh_stringcat_any( "FF_IFU_OFFSET_", arm_str,
00781 str, NULL ) ;
00782 check( divided = xsh_divide_flat( a_b,
00783 master_flat, file_tag,
00784 instrument ) ) ;
00785 XSH_FREE( file_tag ) ;
00786 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00787 }
00788
00789
00790 xsh_msg( "Combining all frames" ) ;
00791 file_tag = xsh_stringcat_any( "COMBINED_IFU_OFFSET_", arm_str,
00792 "ALL", NULL ) ;
00793
00794 check( comb_frame = xsh_combine_offset( clean_frameset,
00795 file_tag, crh_clipping_par,
00796 instrument, NULL, NULL ) ) ;
00797 XSH_FREE( file_tag ) ;
00798
00799
00800 xsh_msg( "Rectify combined frame" ) ;
00801 rect_frameset_eso=cpl_frameset_new();
00802 rect_frameset_tab=cpl_frameset_new();
00803 check( rect2_frameset = xsh_rectify_ifu( comb_frame, order_tab_edges,
00804 wave_tab_frameset,
00805 model_config_frame,
00806 instrument, rectify_par,
00807 spectral_format,
00808 slice_offset_frame,
00809 NULL, &rect_frameset_eso,
00810 &rect_frameset_tab,rec_prefix ) );
00811
00812
00813 if ( extract_par->method == LOCALIZATION_METHOD &&
00814 loc_table_frameset == NULL ) {
00815 xsh_msg( "Localize obj ifu frame" ) ;
00816 check( loc_table_frameset = xsh_localize_obj_ifu( rect2_frameset, NULL,
00817 instrument,
00818 loc_obj_par, NULL) ) ;
00819 }
00820
00821 xsh_msg( "Extract ifu frame" ) ;
00822 check(rect2_frameset_tables=xsh_frameset_ext_table_frames(rect2_frameset));
00823 check(ext_frameset = xsh_extract_ifu(rect2_frameset_tables,
00824 loc_table_frameset,
00825 instrument, extract_par,rec_prefix )) ;
00826 xsh_msg( "Merge orders with 1D frame" ) ;
00827 check(ext_frameset_tables=xsh_frameset_ext_table_frames(ext_frameset));
00828
00829 check( res_1D_frameset = xsh_merge_ord_ifu( ext_frameset_tables, instrument,
00830 merge_par,rec_prefix ));
00831 xsh_free_frameset(&ext_frameset_tables);
00832
00833 xsh_msg( "Merge orders with 2D frame" ) ;
00834 check( res_2D_frameset = xsh_merge_ord_ifu( rect2_frameset_tables,instrument,
00835 merge_par,rec_prefix ));
00836 xsh_free_frameset(&rect2_frameset_tables);
00837
00838 sprintf(file_name,"MERGE3D_IFU_OFFSET_%s.fits",
00839 xsh_instrument_arm_tostring(instrument));
00840
00841 check( data_cube = xsh_format(res_2D_frameset,file_name,instrument,
00842 rec_prefix )) ;
00843
00844
00845 check(ext_frameset_tables=xsh_frameset_ext_table_frames(ext_frameset));
00846 check(ext_frameset_images=xsh_frameset_ext_image_frames(ext_frameset));
00847
00848 xsh_msg( "Saving Products for IFU" ) ;
00849
00850 for( i = 0 ; i<3 ; i++ ) {
00851 cpl_frame * rec_frame = NULL ;
00852 cpl_frame * ext_frame = NULL ;
00853 cpl_frame * res1d_frame = NULL ;
00854 cpl_frame * res2d_frame = NULL ;
00855
00856 check( rec_frame = cpl_frameset_get_frame( rect_frameset_eso, i ) ) ;
00857 xsh_msg( "Product from '%s' [%s]",
00858 cpl_frame_get_filename( rec_frame ),
00859 cpl_frame_get_tag( rec_frame ) ) ;
00860 check( xsh_add_product_image( rec_frame, frameset, parameters,
00861 RECIPE_ID, instrument,
00862 cpl_frame_get_tag(rec_frame)));
00863
00864 check( ext_frame = cpl_frameset_get_frame( ext_frameset_images, i ) ) ;
00865 xsh_msg( "Product from '%s' [%s]",
00866 cpl_frame_get_filename( ext_frame ),
00867 cpl_frame_get_tag( ext_frame ) ) ;
00868 check( xsh_add_product_image( ext_frame, frameset, parameters,
00869 RECIPE_ID, instrument,
00870 cpl_frame_get_tag(ext_frame)));
00871
00872 check( res2d_frame = cpl_frameset_get_frame( res_2D_frameset, i ) ) ;
00873 xsh_msg( "Product from '%s' [%s]",
00874 cpl_frame_get_filename( res2d_frame ),
00875 cpl_frame_get_tag( res2d_frame ) ) ;
00876 check( xsh_add_product_pre( res2d_frame, frameset, parameters,
00877 RECIPE_ID, instrument));
00878
00879
00880 check( res1d_frame = cpl_frameset_get_frame( res_1D_frameset, i ) ) ;
00881 check(xsh_monitor_spectrum1D_flux(res1d_frame,instrument));
00882
00883
00884 xsh_msg( "Product from '%s' [%s]",
00885 cpl_frame_get_filename( res1d_frame ),
00886 cpl_frame_get_tag( res1d_frame ) ) ;
00887 check( xsh_add_product_spectrum( res1d_frame, frameset, parameters,
00888 RECIPE_ID, instrument));
00889
00890 }
00891 xsh_free_frameset(&rect2_frameset_tables);
00892 xsh_free_frameset(&ext_frameset_tables);
00893
00894 xsh_msg( "Product from '%s' [%s]",
00895 cpl_frame_get_filename( data_cube ),
00896 cpl_frame_get_tag( data_cube ) ) ;
00897 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
00898 RECIPE_ID, instrument));
00899
00900 cleanup:
00901 xsh_end( RECIPE_ID, frameset, parameters );
00902 XSH_FREE(crh_clipping_par);
00903 XSH_FREE( backg_par ) ;
00904 XSH_FREE( crh_single_par ) ;
00905 XSH_FREE( rectify_par ) ;
00906 XSH_FREE( loc_obj_par ) ;
00907 xsh_instrument_free(&instrument );
00908 xsh_free_frameset(&ext_frameset_images);
00909 xsh_free_frameset(&rect2_frameset_tables);
00910 xsh_free_frameset(&ext_frameset_tables);
00911
00912 xsh_free_frameset(&raws);
00913 xsh_free_frameset(&calib);
00914 xsh_free_frameset( &sub_frameset ) ;
00915 xsh_free_frameset( &clean_frameset ) ;
00916 xsh_free_frameset( &rmbkg_frameset ) ;
00917 xsh_free_frameset(&res_1D_frameset) ;
00918 xsh_free_frameset(&res_2D_frameset) ;
00919 xsh_free_frameset(&rect2_frameset) ;
00920 xsh_free_frameset(&ext_frameset) ;
00921 xsh_free_frame( &data_cube ) ;
00922 xsh_free_frame( &grid_back ) ;
00923 xsh_free_frame( &frame_backg ) ;
00924
00925 return;
00926 }
00927