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_model_arm_constants.h>
00058
00059 #include <xsh_drl.h>
00060 #include <xsh_drl_check.h>
00061
00062 #include <cpl.h>
00063 #include <xsh_blaze.h>
00064 #include <xsh_utils_ifu.h>
00065 #include <xsh_utils_image.h>
00066
00067
00068
00069
00070 #define RECIPE_ID "xsh_scired_ifu_offset_drl"
00071 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer"
00072 #define RECIPE_CONTACT "amodigli@eso.org"
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 static int xsh_scired_ifu_offset_create( cpl_plugin *);
00083 static int xsh_scired_ifu_offset_exec( cpl_plugin *);
00084 static int xsh_scired_ifu_offset_destroy( cpl_plugin *);
00085
00086
00087 static void xsh_scired_ifu_offset( cpl_parameterlist *, cpl_frameset *);
00088
00089
00090
00091
00092 static char xsh_scired_ifu_offset_description_short[] =
00093 "Reduce science exposure in IFU configuration and on/off mode";
00094
00095 static char xsh_scired_ifu_offset_description[] =
00096 "This recipe reduces science exposure in IFU configuration and on/off mode\n\
00097 Input Frames : \n\
00098 - A set of 2xn Science frames, \
00099 Tag = OBJECT_IFU_OFFSET_arm, SKY_IFU_arm\n\
00100 - A spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00101 - A master flat frame (Tag = MASTER_FLAT_IFU_arm)\n\
00102 - An order table frame(Tag = ORDER_TABLE_EDGES_IFU_arm)\n\
00103 - 3 wave solution frames, one per slitlet (Tag = WAVE_TAB_ARC_IFU_slitlet_arm)\n\
00104 where 'slitlet' is DOWN, CEN or UP\n\
00105 - [OPTIONAL] A dispersion table (Tag = DISP_TAB_IFU_arm)\n\
00106 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
00107 Products : \n\
00108 - 3 Spectrum order tables 2D (1 per slitlet), PRO.CATG=ORDER2D_slitlet_IFU_arm\n\
00109 - 3 Spectrum order tables 1D (1 per slitlet), PRO.CATG=ORDER1D_slitlet_IFU_arm\n\
00110 - 3 Spectrum merge tables 2D (1 per slitlet), PRO.CATG=MERGE2D_slitlet_IFU_arm\n\
00111 - 3 Spectrum merge tables 1D (1 per slitlet), PRO.CATG=MERGE1D_slitlet_IFU_arm\n\
00112 - 1 Spectrum merge 3D, PRO.CATG=MERGE3D_IFU_arm\n" ;
00113
00114
00115
00116
00117
00126
00127
00128 int cpl_plugin_get_info(cpl_pluginlist *list) {
00129 cpl_recipe *recipe = NULL;
00130 cpl_plugin *plugin = NULL;
00131
00132 recipe = cpl_calloc(1, sizeof(*recipe));
00133 if ( recipe == NULL ){
00134 return -1;
00135 }
00136
00137 plugin = &recipe->interface ;
00138
00139 cpl_plugin_init(plugin,
00140 CPL_PLUGIN_API,
00141 XSH_BINARY_VERSION,
00142 CPL_PLUGIN_TYPE_RECIPE,
00143 RECIPE_ID,
00144 xsh_scired_ifu_offset_description_short,
00145 xsh_scired_ifu_offset_description,
00146 RECIPE_AUTHOR,
00147 RECIPE_CONTACT,
00148 xsh_get_license(),
00149 xsh_scired_ifu_offset_create,
00150 xsh_scired_ifu_offset_exec,
00151 xsh_scired_ifu_offset_destroy);
00152
00153 cpl_pluginlist_append(list, plugin);
00154
00155 return (cpl_error_get_code() != CPL_ERROR_NONE);
00156 }
00157
00158
00168
00169
00170 static int xsh_scired_ifu_offset_create(cpl_plugin *plugin){
00171 cpl_recipe *recipe = NULL;
00172 cpl_parameter* p=NULL;
00173 char paramname[256];
00174 char recipename[256];
00175
00176 xsh_clipping_param crh_clip_param = {5.0, 5, 0.7, 0,0.3};
00177
00178 xsh_remove_crh_single_param crh_single = { 0.1, 5, 2.0, 4} ;
00179 xsh_rectify_param rectify = { "default",
00180 CPL_KERNEL_DEFAULT,
00181 4,
00182 XSH_WAVE_BIN_SIZE_PIPE_NIR,
00183 XSH_SLIT_BIN_SIZE_PIPE_NIR,
00184 1,0,0.};
00185 xsh_stack_param stack_param = {"median",5.,5.,5};
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_extract_param extract_par =
00190 { LOCALIZATION_METHOD};
00191
00192
00193 xsh_init();
00194
00195
00196 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00197
00198 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00199 CPL_ERROR_TYPE_MISMATCH,
00200 "Plugin is not a recipe");
00201
00202 recipe = (cpl_recipe *)plugin;
00203
00204
00205 recipe->parameters = cpl_parameterlist_new();
00206 assure( recipe->parameters != NULL,
00207 CPL_ERROR_ILLEGAL_OUTPUT,
00208 "Memory allocation failed!");
00209
00210
00211 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00212 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00213 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00214
00215 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00216 crh_clip_param));
00217
00218
00219 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00220 check(p=xsh_parameters_find(recipe->parameters,RECIPE_ID,"background-method"));
00221 cpl_parameter_set_default_string(p,"poly");
00222
00223
00224 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00225 crh_single )) ;
00226
00227 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00228 rectify )) ;
00229
00230 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00231 loc_obj )) ;
00232
00233 check(xsh_parameters_extract_create(RECIPE_ID,
00234 recipe->parameters,
00235 extract_par,LOCALIZATION_METHOD )) ;
00236
00237 check(xsh_parameters_merge_ord_create(RECIPE_ID,
00238 recipe->parameters,
00239 WEIGHTED_MERGE_METHOD)) ;
00240
00241 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00242 "shift-offsettab-low", 0.0,
00243 "Global shift of the lower slitlet slit positions, relative to the central one[arcsec]."));
00244 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00245 "shift-offsettab-up", 0.0,
00246 "Global shift of the upper slitlet slit positions, relative to the central one[arcsec]."));
00247
00248 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00249 "compute-map", TRUE,
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 "check-afc", TRUE,
00254 "Input AFC corrected model/wave solution and science frame check."\
00255 "If TRUE the recipe verify that the input mode/wave solution is AFC corrected,"\
00256 " its INS.OPTIi.NAME is 'Pin_0.5 ', and its OBS.ID and OBS.TARG.NAME values"\
00257 " matches with the corresponding values of the science frame."));
00258
00259 sprintf(recipename,"xsh.%s",RECIPE_ID);
00260 sprintf(paramname,"%s.%s",recipename,"flat-method");
00261 check( p = cpl_parameter_new_enum( paramname,CPL_TYPE_STRING,
00262 "method adopted for flat:",
00263 recipename,"master",
00264 2,"master","blaze"));
00265
00266 check(cpl_parameter_set_alias( p,CPL_PARAMETER_MODE_CLI,
00267 "flat-method"));
00268 check(cpl_parameterlist_append( recipe->parameters, p));
00269
00270 cleanup:
00271 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00272 xsh_error_dump(CPL_MSG_ERROR);
00273 return 1;
00274 }
00275 else {
00276 return 0;
00277 }
00278 }
00279
00280
00286
00287
00288 static int xsh_scired_ifu_offset_exec(cpl_plugin *plugin) {
00289 cpl_recipe *recipe = NULL;
00290
00291
00292
00293 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00294
00295
00296 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00297 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00298
00299 recipe = (cpl_recipe *)plugin;
00300
00301
00302 xsh_scired_ifu_offset(recipe->parameters, recipe->frames);
00303
00304 cleanup:
00305 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00306 xsh_error_dump(CPL_MSG_ERROR);
00307 xsh_error_reset();
00308 return 1;
00309 }
00310 else {
00311 return 0;
00312 }
00313 }
00314
00315
00316 static cpl_error_code
00317 xsh_params_set_defaults(cpl_parameterlist* pars,
00318 xsh_instrument* inst,
00319 xsh_rectify_param * rectify_par,
00320 xsh_background_param* backg)
00321 {
00322 cpl_parameter* p=NULL;
00323
00324 check(p=xsh_parameters_find(pars,RECIPE_ID,"background-radius-x"));
00325 if(cpl_parameter_get_int(p) <= 0) {
00326 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00327 backg->radius_x=1;
00328 } else {
00329 backg->radius_x=2;
00330 }
00331 cpl_parameter_set_int(p,backg->radius_x);
00332 }
00333 check(xsh_rectify_params_set_defaults(pars,RECIPE_ID,inst,rectify_par));
00334
00335 cleanup:
00336
00337 return cpl_error_get_code();
00338
00339 }
00340
00341
00342 static cpl_error_code
00343 xsh_params_monitor(xsh_background_param* backg,
00344 xsh_rectify_param * rectify_par,
00345 xsh_localize_obj_param * loc_obj_par)
00346 {
00347
00348
00349 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00350 backg->sampley,backg->radius_y,backg->smooth_y);
00351
00352 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00353 backg->radius_x,backg->smooth_x);
00354
00355 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00356 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00357 rectify_par->rectif_bin_space);
00358
00359 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00360 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00361
00362 return cpl_error_get_code();
00363
00364 }
00365
00373
00374
00375 static cpl_error_code
00376 xsh_params_bin_scale(cpl_frameset* raws,
00377 xsh_background_param* backg)
00378 {
00379
00380 cpl_frame* frame=NULL;
00381 const char* name=NULL;
00382 cpl_propertylist* plist=NULL;
00383 int binx=0;
00384 int biny=0;
00385
00386 check(frame=cpl_frameset_get_first(raws));
00387 check(name=cpl_frame_get_filename(frame));
00388 check(plist=cpl_propertylist_load(name,0));
00389 check(binx=xsh_pfits_get_binx(plist));
00390 check(biny=xsh_pfits_get_biny(plist));
00391 xsh_free_propertylist(&plist);
00392
00393 if(biny>1) {
00394
00395
00396
00397
00398
00399
00400 backg->radius_y=backg->radius_y/biny;
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 }
00427
00428
00429 if(binx>1) {
00430
00431 backg->radius_x=backg->radius_x/binx;
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 }
00452
00453 cleanup:
00454 xsh_free_propertylist(&plist);
00455 return cpl_error_get_code();
00456
00457 }
00458
00459
00460
00466
00467 static int xsh_scired_ifu_offset_destroy(cpl_plugin *plugin)
00468 {
00469 cpl_recipe *recipe = NULL;
00470
00471
00472 xsh_error_reset();
00473
00474 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00475
00476
00477 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00478 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00479
00480 recipe = (cpl_recipe *)plugin;
00481
00482 xsh_free_parameterlist(&recipe->parameters);
00483
00484 cleanup:
00485 if (cpl_error_get_code() != CPL_ERROR_NONE)
00486 {
00487 return 1;
00488 }
00489 else
00490 {
00491 return 0;
00492 }
00493 }
00494
00495
00503
00504 static void xsh_scired_ifu_offset( cpl_parameterlist* parameters,
00505 cpl_frameset* frameset)
00506 {
00507 const char* recipe_tags[4] = {XSH_OBJECT_IFU_OFFSET, XSH_STD_TELL_IFU_OFFSET, XSH_STD_FLUX_IFU_OFFSET, XSH_SKY_IFU};
00508 int recipe_tags_size = 4;
00509
00510
00511 cpl_frameset* raws = NULL;
00512 cpl_frameset* calib = NULL;
00513
00514
00515 cpl_frame* bpmap = NULL;
00516 cpl_frame * master_bias = NULL ;
00517 cpl_frame * master_dark = NULL ;
00518 cpl_frame* master_flat = NULL;
00519 cpl_frame* order_tab_edges = NULL;
00520 cpl_frameset *wavetab_frameset = NULL ;
00521 cpl_frameset *shiftifu_frameset = NULL;
00522 cpl_frameset *nshiftifu_frameset = NULL;
00523 cpl_frame * model_config_frame = NULL ;
00524 cpl_frame * wavemap_frame = NULL ;
00525 cpl_frame * slitmap_frame = NULL;
00526 cpl_frame * spectral_format = NULL ;
00527 cpl_frame *disp_tab_frame = NULL;
00528 cpl_frameset * object_frameset = NULL, * sky_frameset = NULL ;
00529
00530
00531 xsh_clipping_param* crh_clipping_par = NULL;
00532 xsh_background_param* backg_par = NULL;
00533 xsh_remove_crh_single_param * crh_single_par = NULL ;
00534 xsh_rectify_param * rectify_par = NULL ;
00535 xsh_localize_obj_param * loc_obj_par = NULL ;
00536 xsh_merge_param* merge_par = NULL;
00537 xsh_extract_param * extract_par = NULL ;
00538 double offset_low =0.0;
00539 double offset_up =0.0;
00540
00541 int recipe_use_model = FALSE;
00542 int do_computemap = 0;
00543 int check_afc = TRUE;
00544
00545 xsh_instrument* instrument = NULL;
00546 int nb_sub_frames ;
00547 int nb_raw_frames ;
00548
00549
00550
00551 cpl_frameset * sub_frameset = NULL ;
00552 cpl_frameset * rmbkg_frameset = NULL ;
00554 cpl_frameset * nocrh_frameset = NULL;
00555 cpl_frameset * clean_frameset = NULL ;
00557 cpl_frame * first_frame = NULL;
00558 cpl_frame * comb_frame = NULL ;
00559 cpl_frameset * rect2_frameset = NULL ;
00560
00561
00562
00563 cpl_frameset * res_1D_frameset = NULL ;
00564 cpl_frameset * res_2D_frameset = NULL ;
00565 cpl_frame * data_cube = NULL ;
00566 cpl_frameset * ext_frameset = NULL ;
00567 cpl_frameset * ext_frameset_tables = NULL ;
00568
00569 int i ;
00570 char file_name[80], arm_str[16] ;
00571 char file_tag[256];
00572 cpl_frame* grid_back=NULL;
00573 cpl_frame* frame_backg=NULL;
00574 cpl_frameset * ext_frameset_images = NULL ;
00575 cpl_frameset * rect2_frameset_tables = NULL ;
00576 cpl_frameset * rect2_frameset_tab=NULL;
00577 cpl_frameset * rect2_frameset_eso=NULL;
00578 char *rec_prefix = NULL;
00579 int pre_overscan_corr=0;
00580
00581 char* flat_method = NULL;
00582 cpl_frame * blaze_frame = NULL;
00583 cpl_propertylist* plist=NULL;
00584 const char* name=NULL;
00585 const char* tag="";
00586 int naxis2=0;
00587 cpl_frame* qc_trace_frame=NULL;
00588 int save_size=0;
00589 const int peack_search_hsize=5;
00590 int method=0;
00591
00592 xsh_stack_param* stack_par=NULL;
00593
00594
00595
00596
00597 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00598 recipe_tags, recipe_tags_size,
00599 RECIPE_ID, XSH_BINARY_VERSION,
00600 xsh_scired_ifu_offset_description_short));
00601
00602 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
00603 "Instrument NOT in Ifu Mode" ) ;
00604
00605 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00606 xsh_ensure_raws_input_offset_recipe_is_proper(raws,instrument);
00607
00608 check( nb_raw_frames = cpl_frameset_get_size( raws ) ) ;
00609 xsh_msg_dbg_low( "Nb of Raw frames: %d", nb_raw_frames ) ;
00610 XSH_ASSURE_NOT_ILLEGAL( nb_raw_frames > 1 ) ;
00611 {
00612 int even_nb = nb_raw_frames % 2 ;
00613 XSH_ASSURE_NOT_ILLEGAL( even_nb == 0 ) ;
00614 }
00615
00616 check( rec_prefix = xsh_set_recipe_file_prefix( raws,
00617 RECIPE_ID));
00618
00619
00620
00621
00622
00623
00624 check(bpmap = xsh_find_master_bpmap(calib));
00625
00626
00627 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00628
00629 check( master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00630 instrument));
00631 }
00632
00633 else {
00634
00635
00636
00637
00638 }
00639
00640 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
00641
00642
00643 if((model_config_frame = xsh_find_frame_with_tag(calib,
00644 XSH_MOD_CFG_OPT_AFC,
00645 instrument)) == NULL) {
00646
00647 xsh_error_reset();
00648
00649 if ((model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
00650 instrument)) == NULL) {
00651 xsh_error_reset();
00652 }
00653
00654 }
00655
00656 cpl_error_reset() ;
00657
00658
00659 shiftifu_frameset = xsh_find_offset_tab_ifu( calib,
00660 instrument);
00661 xsh_error_reset();
00662
00663 wavetab_frameset = xsh_find_wave_tab_ifu( calib, instrument);
00664 cpl_error_reset() ;
00665
00666
00667
00668
00669 if ( model_config_frame == NULL){
00670 xsh_msg("RECIPE USE WAVE SOLUTION");
00671 recipe_use_model = FALSE;
00672 }
00673 else{
00674 xsh_msg("RECIPE USE MODEL");
00675 recipe_use_model = TRUE;
00676 }
00677
00678 XSH_ASSURE_NOT_ILLEGAL( (model_config_frame != NULL && wavetab_frameset == NULL ) ||
00679 (model_config_frame == NULL && wavetab_frameset != NULL ) );
00680
00681 check( master_flat = xsh_find_master_flat( calib, instrument ) ) ;
00682
00683 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00684 instrument)) == NULL){
00685 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00686 xsh_error_reset();
00687 }
00688
00689 check( spectral_format = xsh_find_spectral_format( calib, instrument ) ) ;
00690
00691
00692
00693
00694
00695 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00696 check( crh_clipping_par = xsh_parameters_clipping_crh_get(RECIPE_ID,
00697 parameters));
00698 check( backg_par = xsh_parameters_background_get(RECIPE_ID,
00699 parameters));
00700
00701 check( loc_obj_par = xsh_parameters_localize_obj_get(RECIPE_ID,
00702 parameters));
00703 check( rectify_par = xsh_parameters_rectify_get(RECIPE_ID,
00704 parameters));
00705 check( crh_single_par = xsh_parameters_remove_crh_single_get(RECIPE_ID,
00706 parameters));
00707 check( merge_par = xsh_parameters_merge_ord_get(RECIPE_ID,
00708 parameters));
00709 check(extract_par=xsh_parameters_extract_get(RECIPE_ID, parameters));
00710
00711 check( do_computemap = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00712 "compute-map"));
00713
00714 check( check_afc = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00715 "check-afc"));
00716
00717 if ( do_computemap && recipe_use_model==FALSE){
00718 check( disp_tab_frame = xsh_find_disp_tab( calib, instrument));
00719 }
00720
00721 check(xsh_params_set_defaults(parameters,instrument,rectify_par,backg_par));
00722
00723
00724 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00725 check(xsh_params_bin_scale(raws,backg_par));
00726 }
00727 check(xsh_params_monitor(backg_par,rectify_par,loc_obj_par));
00728
00729 check( offset_low = xsh_parameters_get_double( parameters, RECIPE_ID,
00730 "shift-offsettab-low"));
00731
00732 check( offset_up = xsh_parameters_get_double( parameters, RECIPE_ID,
00733 "shift-offsettab-up"));
00734
00735
00736 if ( shiftifu_frameset != NULL){
00737 xsh_msg("offset low %f up %f", offset_low, offset_up);
00738 check( nshiftifu_frameset = xsh_shift_offsettab( shiftifu_frameset,
00739 offset_low, offset_up));
00740 }
00741
00742
00743
00744
00745
00746
00747 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_IFU_OFFSET,
00748 instrument,pre_overscan_corr));
00749 check( first_frame = cpl_frameset_get_frame( raws, 0));
00750
00751
00752 check( xsh_check_get_map( disp_tab_frame, order_tab_edges,
00753 first_frame, model_config_frame, calib, instrument,
00754 do_computemap, recipe_use_model, rec_prefix,
00755 &wavemap_frame, &slitmap_frame));
00756
00757
00758 check( nb_sub_frames = xsh_dfs_split_offset( raws, XSH_OBJECT_IFU_OFFSET,
00759 XSH_SKY_IFU, &object_frameset,
00760 &sky_frameset ));
00761
00762
00763
00764
00765 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00766 cpl_frame *frame = NULL;
00767
00768 frame = cpl_frameset_get_frame( object_frameset, i );
00769 check( xsh_check_afc( check_afc, model_config_frame,
00770 frame, wavetab_frameset, order_tab_edges, disp_tab_frame,
00771 instrument));
00772 }
00773
00774
00775
00776 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00777 cpl_frame *frame = NULL;
00778
00779 frame = cpl_frameset_get_frame( sky_frameset, i );
00780 check( xsh_check_afc( check_afc, model_config_frame,
00781 frame, wavetab_frameset, order_tab_edges, disp_tab_frame,
00782 instrument));
00783 }
00784
00785
00786
00787 check( sub_frameset = xsh_subtract_sky_offset( object_frameset, sky_frameset,
00788 nb_sub_frames, instrument )) ;
00789 check( rmbkg_frameset = cpl_frameset_new() ) ;
00790 check( nocrh_frameset = cpl_frameset_new() ) ;
00791 check( clean_frameset = cpl_frameset_new() ) ;
00792
00793 sprintf( arm_str, "%s_", xsh_instrument_arm_tostring(instrument) ) ;
00794
00795
00796
00797 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00798 cpl_frame * a_b = NULL ;
00799 cpl_frame * rm_bkg = NULL ;
00800 char str[16] ;
00801
00802 sprintf( str, "%d", i ) ;
00803 a_b = cpl_frameset_get_frame( sub_frameset, i ) ;
00804
00805
00806 xsh_msg("Subtract inter-order background %d", i );
00807 sprintf(file_name, "%s%s",rec_prefix, str) ;
00808 check(rm_bkg = xsh_subtract_background( a_b,
00809 order_tab_edges,
00810 backg_par, instrument,
00811 file_name,&grid_back,
00812 &frame_backg,0));
00813
00814 check( cpl_frameset_insert( rmbkg_frameset, rm_bkg ) ) ;
00815 }
00816
00817
00818 check( flat_method = xsh_parameters_get_string( parameters, RECIPE_ID,
00819 "flat-method"));
00820 if ( strcmp( flat_method, "master") != 0){
00821 xsh_msg("---Create blaze image");
00822 check( blaze_frame = xsh_blaze_image( master_flat, order_tab_edges,
00823 instrument));
00824 }
00825
00826 if ( nb_sub_frames < 3 && crh_single_par->nb_iter > 0 ) {
00827 xsh_msg( "Less than 3 frames AND crh_single_niter > 0" ) ;
00828 for ( i = 0 ; i < nb_sub_frames ; i++ ) {
00829 cpl_frame * divided = NULL ;
00830 cpl_frame * rm_crh = NULL ;
00831 cpl_frame * a_b = NULL ;
00832
00833 check( a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ) ;
00834 sprintf( file_tag, "NO_CRH_IFU_OFFSET_%s%d", arm_str,i);
00835 xsh_msg( "Remove crh (single frame)" );
00836 check( rm_crh = xsh_remove_crh_single( a_b, wavemap_frame,instrument,
00837 crh_single_par,
00838 file_tag ));
00839 check( cpl_frameset_insert( nocrh_frameset, rm_crh));
00840
00841 xsh_msg( "Divide by flat" ) ;
00842 sprintf( file_tag, "FF_IFU_OFFSET_%s%d", arm_str, i);
00843 if ( strcmp( flat_method, "master") == 0){
00844 check( divided = xsh_divide_flat( rm_crh, master_flat,
00845 file_tag, instrument));
00846 }
00847 else{
00848 check( divided = xsh_divide_by_blaze( rm_crh,
00849 blaze_frame, instrument));
00850 }
00851
00852 check( cpl_frameset_insert( clean_frameset, divided));
00853 }
00854 }
00855 else for( i = 0 ; i < nb_sub_frames ; i++ ) {
00856 cpl_frame * divided = NULL ;
00857 cpl_frame * a_b = NULL ;
00858
00859
00860 a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ;
00861
00862
00863 xsh_msg( "Divide by flat" ) ;
00864 sprintf( file_tag, "FF_IFU_OFFSET_%s%d", arm_str, i);
00865
00866 if ( strcmp( flat_method, "master") == 0){
00867 check( divided = xsh_divide_flat( a_b, master_flat,
00868 file_tag, instrument));
00869 }
00870 else{
00871 check( divided = xsh_divide_by_blaze( a_b,
00872 blaze_frame, instrument));
00873 }
00874 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00875 }
00876
00877
00878
00879 xsh_msg( "Combining all frames" );
00880 sprintf( file_tag, "COMBINED_IFU_OFFSET_%s_ALL", arm_str);
00881
00882
00883 check( comb_frame = xsh_combine_offset( clean_frameset,
00884 file_tag, stack_par,crh_clipping_par,
00885 instrument, NULL, NULL,0));
00886
00887
00888
00889 xsh_msg( "Rectify combined frame" ) ;
00890 rect2_frameset_eso=cpl_frameset_new();
00891 rect2_frameset_tab=cpl_frameset_new();
00892 check( rect2_frameset = xsh_rectify_ifu( comb_frame, order_tab_edges,
00893 wavetab_frameset,
00894 nshiftifu_frameset,
00895 model_config_frame,
00896 instrument, rectify_par,
00897 spectral_format,
00898 slitmap_frame, &rect2_frameset_eso,
00899 &rect2_frameset_tab,rec_prefix));
00900
00901 #if 0
00902
00903 if ( extract_par->method == LOCALIZATION_METHOD &&
00904 loc_table_frameset == NULL ) {
00905 xsh_msg( "Localize obj ifu frame" ) ;
00906 check( loc_table_frameset = xsh_localize_obj_ifu( rect2_frameset, NULL,
00907 instrument,
00908 loc_obj_par, NULL) ) ;
00909 }
00910
00911 xsh_msg( "Extract ifu frame" ) ;
00912 check(rect2_frameset_tables=xsh_frameset_ext_table_frames(rect2_frameset));
00913 check(ext_frameset = xsh_extract_ifu(rect2_frameset_tables,
00914 loc_table_frameset,
00915 instrument, extract_par,rec_prefix )) ;
00916 xsh_msg( "Merge orders with 1D frame" ) ;
00917 check(ext_frameset_tables=xsh_frameset_ext_table_frames(ext_frameset));
00918
00919 check( res_1D_frameset = xsh_merge_ord_ifu( ext_frameset_tables, instrument,
00920 merge_par,rec_prefix ));
00921 xsh_free_frameset(&ext_frameset_tables);
00922 #endif
00923
00924
00925
00926 xsh_msg( "Merge orders with 2D frame" ) ;
00927 check( res_2D_frameset = xsh_merge_ord_ifu( rect2_frameset,instrument,
00928 merge_par,rec_prefix ));
00929
00930 check( data_cube = xsh_cube( res_2D_frameset, instrument, rec_prefix));
00931
00932 xsh_msg( "Saving Products for IFU" ) ;
00933 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
00934 RECIPE_ID, instrument));
00935
00936 name=cpl_frame_get_filename(data_cube);
00937 plist=cpl_propertylist_load(name,0);
00938 naxis2=xsh_pfits_get_naxis2(plist);
00939 xsh_free_propertylist(&plist);
00940 check( qc_trace_frame=xsh_cube_qc_trace_window(data_cube,
00941 instrument,tag,rec_prefix,
00942 save_size+1,
00943 naxis2-save_size,
00944 peack_search_hsize,
00945 method,0));
00946 if(qc_trace_frame) {
00947 check( xsh_add_product_table( qc_trace_frame, frameset,parameters,
00948 RECIPE_ID, instrument,NULL));
00949 }
00950 #if 0
00951 check(ext_frameset_tables=xsh_frameset_ext_table_frames(ext_frameset));
00952 check(ext_frameset_images=xsh_frameset_ext_image_frames(ext_frameset));
00953
00954 xsh_msg( "Saving Products for IFU" ) ;
00955
00956 for( i = 0 ; i<3 ; i++ ) {
00957 cpl_frame * rec_frame = NULL ;
00958 cpl_frame * ext_frame = NULL ;
00959 cpl_frame * res1d_frame = NULL ;
00960 cpl_frame * res2d_frame = NULL ;
00961
00962 check( rec_frame = cpl_frameset_get_frame( rect_frameset_eso, i ) ) ;
00963 xsh_msg( "Product from '%s' [%s]",
00964 cpl_frame_get_filename( rec_frame ),
00965 cpl_frame_get_tag( rec_frame ) ) ;
00966 check( xsh_add_product_image( rec_frame, frameset, parameters,
00967 RECIPE_ID, instrument,NULL));
00968
00969 check( ext_frame = cpl_frameset_get_frame( ext_frameset_images, i ) ) ;
00970 xsh_msg( "Product from '%s' [%s]",
00971 cpl_frame_get_filename( ext_frame ),
00972 cpl_frame_get_tag( ext_frame ) ) ;
00973 check( xsh_add_product_image( ext_frame, frameset, parameters,
00974 RECIPE_ID, instrument,NULL));
00975
00976 check( res2d_frame = cpl_frameset_get_frame( res_2D_frameset, i ) ) ;
00977 xsh_msg( "Product from '%s' [%s]",
00978 cpl_frame_get_filename( res2d_frame ),
00979 cpl_frame_get_tag( res2d_frame ) ) ;
00980 check( xsh_add_product_pre( res2d_frame, frameset, parameters,
00981 RECIPE_ID, instrument));
00982
00983
00984 check( res1d_frame = cpl_frameset_get_frame( res_1D_frameset, i ) ) ;
00985 check(xsh_monitor_spectrum1D_flux(res1d_frame,instrument));
00986
00987
00988 xsh_msg( "Product from '%s' [%s]",
00989 cpl_frame_get_filename( res1d_frame ),
00990 cpl_frame_get_tag( res1d_frame ) ) ;
00991 check( xsh_add_product_spectrum( res1d_frame, frameset, parameters,
00992 RECIPE_ID, instrument));
00993
00994 }
00995 xsh_free_frameset(&rect2_frameset_tables);
00996 xsh_free_frameset(&ext_frameset_tables);
00997
00998 xsh_msg( "Product from '%s' [%s]",
00999 cpl_frame_get_filename( data_cube ),
01000 cpl_frame_get_tag( data_cube ) ) ;
01001 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
01002 RECIPE_ID, instrument));
01003
01004 #endif
01005 cleanup:
01006 xsh_end( RECIPE_ID, frameset, parameters );
01007 XSH_FREE( rec_prefix);
01008 XSH_FREE(crh_clipping_par);
01009 XSH_FREE( backg_par);
01010 XSH_FREE( crh_single_par);
01011 XSH_FREE( rectify_par);
01012 XSH_FREE( loc_obj_par);
01013 XSH_FREE( merge_par);
01014 XSH_FREE( extract_par);
01015 xsh_instrument_free(&instrument);
01016
01017 xsh_free_frameset(&ext_frameset_images);
01018 xsh_free_frameset(&ext_frameset_tables);
01019
01020 xsh_free_frameset(&raws);
01021 xsh_free_frameset(&calib);
01022 xsh_free_frameset( &wavetab_frameset);
01023 xsh_free_frameset( &shiftifu_frameset);
01024 xsh_free_frameset( &nshiftifu_frameset);
01025 xsh_free_frameset( &object_frameset);
01026 xsh_free_frameset( &sky_frameset);
01027 xsh_free_frameset( &sub_frameset ) ;
01028 xsh_free_frameset( &nocrh_frameset);
01029 xsh_free_frameset( &clean_frameset ) ;
01030 xsh_free_frameset( &rmbkg_frameset ) ;
01031 xsh_free_frameset(&res_1D_frameset) ;
01032 xsh_free_frameset(&res_2D_frameset) ;
01033
01034 xsh_free_frameset( &rect2_frameset) ;
01035 xsh_free_frameset( &rect2_frameset_eso);
01036 xsh_free_frameset( &rect2_frameset_tab);
01037 xsh_free_frameset(&rect2_frameset_tables);
01038
01039 xsh_free_frameset(&ext_frameset) ;
01040 xsh_free_frame( &slitmap_frame);
01041 xsh_free_frame( &wavemap_frame);
01042 xsh_free_frame( &comb_frame);
01043 xsh_free_frame( &data_cube);
01044 xsh_free_frame( &grid_back);
01045 xsh_free_frame( &frame_backg);
01046 xsh_free_frame( &blaze_frame);
01047 xsh_free_frame( &qc_trace_frame);
01048 return;
01049 }
01050