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 = { "tanh",
00180 CPL_KERNEL_DEFAULT,
00181 2,
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.};
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 xsh_interpolate_bp_param ipol_par = {30 };
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 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00213 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00214 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00215
00216 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00217 crh_clip_param));
00218
00219
00220 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00221
00222
00223 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00224 crh_single )) ;
00225
00226 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00227 rectify )) ;
00228
00229 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00230 loc_obj )) ;
00231
00232 check(xsh_parameters_extract_create(RECIPE_ID,
00233 recipe->parameters,
00234 extract_par,LOCALIZATION_METHOD )) ;
00235
00236 check(xsh_parameters_interpolate_bp_create(RECIPE_ID,
00237 recipe->parameters,ipol_par)) ;
00238
00239 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00240 "shift-offsettab-low", 0.0,
00241 "Global shift of the lower slitlet slit positions, relative to the central one[arcsec]."));
00242 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00243 "shift-offsettab-up", 0.0,
00244 "Global shift of the upper slitlet slit positions, relative to the central one[arcsec]."));
00245
00246 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00247 "compute-map", TRUE,
00248 "if TRUE recompute (wave and slit) maps from the dispersion solution. If sky-subtract is set to TRUE this must be set to TRUE."));
00249
00250 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00251 "check-afc", TRUE,
00252 "Input AFC corrected model/wave solution and science frame check."\
00253 "If TRUE the recipe verify that the input mode/wave solution is AFC corrected,"\
00254 " its INS.OPTIi.NAME is 'Pin_0.5 ', and its OBS.ID and OBS.TARG.NAME values"\
00255 " matches with the corresponding values of the science frame."));
00256
00257 sprintf(recipename,"xsh.%s",RECIPE_ID);
00258 sprintf(paramname,"%s.%s",recipename,"flat-method");
00259 check( p = cpl_parameter_new_enum( paramname,CPL_TYPE_STRING,
00260 "method adopted for flat:",
00261 recipename,"master",
00262 2,"master","blaze"));
00263
00264 check(cpl_parameter_set_alias( p,CPL_PARAMETER_MODE_CLI,
00265 "flat-method"));
00266 check(cpl_parameterlist_append( recipe->parameters, p));
00267
00268 cleanup:
00269 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00270 xsh_error_dump(CPL_MSG_ERROR);
00271 return 1;
00272 }
00273 else {
00274 return 0;
00275 }
00276 }
00277
00278
00284
00285
00286 static int xsh_scired_ifu_offset_exec(cpl_plugin *plugin) {
00287 cpl_recipe *recipe = NULL;
00288
00289
00290
00291 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00292
00293
00294 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00295 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00296
00297 recipe = (cpl_recipe *)plugin;
00298
00299
00300 xsh_scired_ifu_offset(recipe->parameters, recipe->frames);
00301
00302 cleanup:
00303 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00304 xsh_error_dump(CPL_MSG_ERROR);
00305 xsh_error_reset();
00306 return 1;
00307 }
00308 else {
00309 return 0;
00310 }
00311 }
00312
00313
00314 static cpl_error_code
00315 xsh_params_monitor(xsh_background_param* backg,
00316 xsh_rectify_param * rectify_par,
00317 xsh_localize_obj_param * loc_obj_par)
00318 {
00319
00320
00321 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d",
00322 backg->sampley,backg->radius_y);
00323
00324 xsh_msg_dbg_low("bkg params: radius_x=%d",
00325 backg->radius_x);
00326
00327 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00328 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00329 rectify_par->rectif_bin_space);
00330
00331 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00332 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00333
00334 return cpl_error_get_code();
00335
00336 }
00337
00345
00346
00347 static cpl_error_code
00348 xsh_params_bin_scale(cpl_frameset* raws,
00349 xsh_background_param* backg)
00350 {
00351
00352 cpl_frame* frame=NULL;
00353 const char* name=NULL;
00354 cpl_propertylist* plist=NULL;
00355 int binx=0;
00356 int biny=0;
00357
00358 check(frame=cpl_frameset_get_first(raws));
00359 check(name=cpl_frame_get_filename(frame));
00360 check(plist=cpl_propertylist_load(name,0));
00361 check(binx=xsh_pfits_get_binx(plist));
00362 check(biny=xsh_pfits_get_biny(plist));
00363 xsh_free_propertylist(&plist);
00364
00365 if(biny>1) {
00366
00367
00368
00369
00370
00371
00372 backg->radius_y=backg->radius_y/biny;
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 }
00392
00393
00394 if(binx>1) {
00395
00396 backg->radius_x=backg->radius_x/binx;
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 }
00410
00411 cleanup:
00412 xsh_free_propertylist(&plist);
00413 return cpl_error_get_code();
00414
00415 }
00416
00417
00418
00424
00425 static int xsh_scired_ifu_offset_destroy(cpl_plugin *plugin)
00426 {
00427 cpl_recipe *recipe = NULL;
00428
00429
00430 xsh_error_reset();
00431
00432 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00433
00434
00435 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00436 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00437
00438 recipe = (cpl_recipe *)plugin;
00439
00440 xsh_free_parameterlist(&recipe->parameters);
00441
00442 cleanup:
00443 if (cpl_error_get_code() != CPL_ERROR_NONE)
00444 {
00445 return 1;
00446 }
00447 else
00448 {
00449 return 0;
00450 }
00451 }
00452
00453
00461
00462 static void xsh_scired_ifu_offset( cpl_parameterlist* parameters,
00463 cpl_frameset* frameset)
00464 {
00465 const char* recipe_tags[4] = {XSH_OBJECT_IFU_OFFSET, XSH_STD_TELL_IFU_OFFSET, XSH_STD_FLUX_IFU_OFFSET, XSH_SKY_IFU};
00466 int recipe_tags_size = 4;
00467
00468
00469 cpl_frameset* raws = NULL;
00470 cpl_frameset* calib = NULL;
00471
00472
00473 cpl_frame* bpmap = NULL;
00474 cpl_frame * master_bias = NULL ;
00475 cpl_frame * master_dark = NULL ;
00476 cpl_frame* master_flat = NULL;
00477 cpl_frame* order_tab_edges = NULL;
00478 cpl_frameset *wavetab_frameset = NULL ;
00479 cpl_frameset *shiftifu_frameset = NULL;
00480 cpl_frameset *nshiftifu_frameset = NULL;
00481 cpl_frame * model_config_frame = NULL ;
00482 cpl_frame * wavemap_frame = NULL ;
00483 cpl_frame * slitmap_frame = NULL;
00484 cpl_frame * spectral_format = NULL ;
00485 cpl_frame *disp_tab_frame = NULL;
00486 cpl_frameset * object_frameset = NULL, * sky_frameset = NULL ;
00487
00488
00489 xsh_background_param* backg_par = NULL;
00490 xsh_remove_crh_single_param * crh_single_par = NULL ;
00491 xsh_rectify_param * rectify_par = NULL ;
00492 xsh_localize_obj_param * loc_obj_par = NULL ;
00493 xsh_extract_param * extract_par = NULL ;
00494 double offset_low =0.0;
00495 double offset_up =0.0;
00496
00497 int recipe_use_model = FALSE;
00498 int do_computemap = 0;
00499 int check_afc = TRUE;
00500
00501 xsh_instrument* instrument = NULL;
00502 int nb_sub_frames ;
00503 int nb_raw_frames ;
00504
00505
00506
00507 cpl_frameset * sub_frameset = NULL ;
00508 cpl_frameset * rmbkg_frameset = NULL ;
00510 cpl_frameset * nocrh_frameset = NULL;
00511 cpl_frameset * clean_frameset = NULL ;
00513 cpl_frame * first_frame = NULL;
00514 cpl_frame * comb_frame = NULL ;
00515 cpl_frameset * rect2_frameset = NULL ;
00516
00517
00518
00519 cpl_frameset * res_1D_frameset = NULL ;
00520 cpl_frameset * res_2D_frameset = NULL ;
00521 cpl_frame * data_cube = NULL ;
00522 cpl_frameset * ext_frameset = NULL ;
00523 cpl_frameset * ext_frameset_tables = NULL ;
00524
00525 int i ;
00526 char file_name[256], arm_str[16] ;
00527 char file_tag[256];
00528 cpl_frame* grid_back=NULL;
00529 cpl_frame* frame_backg=NULL;
00530 cpl_frameset * ext_frameset_images = NULL ;
00531 cpl_frameset * rect2_frameset_tables = NULL ;
00532 cpl_frameset * rect2_frameset_tab=NULL;
00533 cpl_frameset * rect2_frameset_eso=NULL;
00534 char *rec_prefix = NULL;
00535 int pre_overscan_corr=0;
00536
00537 char* flat_method = NULL;
00538 cpl_frame * blaze_frame = NULL;
00539 cpl_propertylist* plist=NULL;
00540 const char* name=NULL;
00541 const char* tag="";
00542 int naxis2=0;
00543 cpl_frame* qc_trace_frame=NULL;
00544 int save_size=0;
00545 const int peack_search_hsize=5;
00546 int method=0;
00547
00548 xsh_stack_param* stack_par=NULL;
00549 int merge_par=0;
00550
00551
00552
00553 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00554 recipe_tags, recipe_tags_size,
00555 RECIPE_ID, XSH_BINARY_VERSION,
00556 xsh_scired_ifu_offset_description_short));
00557
00558 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
00559 "Instrument NOT in Ifu Mode" ) ;
00560
00561 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00562 xsh_ensure_raws_input_offset_recipe_is_proper(&raws,instrument);
00563
00564 check( nb_raw_frames = cpl_frameset_get_size( raws ) ) ;
00565 xsh_msg_dbg_low( "Nb of Raw frames: %d", nb_raw_frames ) ;
00566 XSH_ASSURE_NOT_ILLEGAL( nb_raw_frames > 1 ) ;
00567 {
00568 int even_nb = nb_raw_frames % 2 ;
00569 XSH_ASSURE_NOT_ILLEGAL( even_nb == 0 ) ;
00570 }
00571
00572 check( rec_prefix = xsh_set_recipe_file_prefix( raws,
00573 RECIPE_ID));
00574
00575
00576
00577
00578
00579
00580 check(bpmap = xsh_find_master_bpmap(calib));
00581
00582
00583 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00584
00585 check( master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00586 instrument));
00587 }
00588
00589 else {
00590
00591
00592
00593
00594 }
00595
00596 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
00597
00598
00599 if((model_config_frame = xsh_find_frame_with_tag(calib,
00600 XSH_MOD_CFG_OPT_AFC,
00601 instrument)) == NULL) {
00602
00603 xsh_error_reset();
00604
00605 if ((model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
00606 instrument)) == NULL) {
00607 xsh_error_reset();
00608 }
00609
00610 }
00611
00612 cpl_error_reset() ;
00613
00614
00615 shiftifu_frameset = xsh_find_offset_tab_ifu( calib,
00616 instrument);
00617 xsh_error_reset();
00618
00619 wavetab_frameset = xsh_find_wave_tab_ifu( calib, instrument);
00620 cpl_error_reset() ;
00621
00622
00623
00624
00625 if ( model_config_frame == NULL){
00626 xsh_msg("RECIPE USE WAVE SOLUTION");
00627 recipe_use_model = FALSE;
00628 }
00629 else{
00630 xsh_msg("RECIPE USE MODEL");
00631 recipe_use_model = TRUE;
00632 }
00633
00634 XSH_ASSURE_NOT_ILLEGAL( (model_config_frame != NULL && wavetab_frameset == NULL ) ||
00635 (model_config_frame == NULL && wavetab_frameset != NULL ) );
00636
00637 check( master_flat = xsh_find_master_flat( calib, instrument ) ) ;
00638
00639 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00640 instrument)) == NULL){
00641 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00642 xsh_error_reset();
00643 }
00644
00645 check( spectral_format = xsh_find_spectral_format( calib, instrument ) ) ;
00646
00647
00648
00649
00650
00651 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00652
00653 check( backg_par = xsh_parameters_background_get(RECIPE_ID,
00654 parameters));
00655
00656 check( loc_obj_par = xsh_parameters_localize_obj_get(RECIPE_ID,
00657 parameters));
00658 check( rectify_par = xsh_parameters_rectify_get(RECIPE_ID,
00659 parameters));
00660 rectify_par->conserve_flux=FALSE;
00661 check( crh_single_par = xsh_parameters_remove_crh_single_get(RECIPE_ID,
00662 parameters));
00663
00664 check(extract_par=xsh_parameters_extract_get(RECIPE_ID, parameters));
00665
00666 check( do_computemap = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00667 "compute-map"));
00668
00669 check( check_afc = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00670 "check-afc"));
00671
00672 if ( do_computemap && recipe_use_model==FALSE){
00673 check( disp_tab_frame = xsh_find_disp_tab( calib, instrument));
00674 }
00675
00676 check(xsh_rectify_params_set_defaults(parameters,RECIPE_ID,instrument,rectify_par));
00677
00678
00679 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00680 check(xsh_params_bin_scale(raws,backg_par));
00681 }
00682 check(xsh_params_monitor(backg_par,rectify_par,loc_obj_par));
00683
00684 check( offset_low = xsh_parameters_get_double( parameters, RECIPE_ID,
00685 "shift-offsettab-low"));
00686
00687 check( offset_up = xsh_parameters_get_double( parameters, RECIPE_ID,
00688 "shift-offsettab-up"));
00689
00690
00691 if ( shiftifu_frameset != NULL){
00692 xsh_msg("offset low %f up %f", offset_low, offset_up);
00693 check( nshiftifu_frameset = xsh_shift_offsettab( shiftifu_frameset,
00694 offset_low, offset_up));
00695 }
00696
00697
00698
00699
00700
00701
00702 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_IFU_OFFSET,
00703 instrument,pre_overscan_corr,CPL_TRUE));
00704 check( first_frame = cpl_frameset_get_frame( raws, 0));
00705
00706
00707 check( xsh_check_get_map( disp_tab_frame, order_tab_edges,
00708 first_frame, model_config_frame, calib, instrument,
00709 do_computemap, recipe_use_model, rec_prefix,
00710 &wavemap_frame, &slitmap_frame));
00711
00712
00713 check( nb_sub_frames = xsh_dfs_split_offset( raws, XSH_OBJECT_IFU_OFFSET,
00714 XSH_SKY_IFU, &object_frameset,
00715 &sky_frameset ));
00716
00717
00718
00719
00720 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00721 cpl_frame *frame = NULL;
00722
00723 frame = cpl_frameset_get_frame( object_frameset, i );
00724 check( xsh_check_afc( check_afc, model_config_frame,
00725 frame, wavetab_frameset, order_tab_edges, disp_tab_frame,
00726 instrument));
00727 }
00728
00729
00730
00731 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00732 cpl_frame *frame = NULL;
00733
00734 frame = cpl_frameset_get_frame( sky_frameset, i );
00735 check( xsh_check_afc( check_afc, model_config_frame,
00736 frame, wavetab_frameset, order_tab_edges, disp_tab_frame,
00737 instrument));
00738 }
00739
00740
00741
00742 check( sub_frameset = xsh_subtract_sky_offset( object_frameset, sky_frameset,
00743 nb_sub_frames, instrument )) ;
00744 check( rmbkg_frameset = cpl_frameset_new() ) ;
00745 check( nocrh_frameset = cpl_frameset_new() ) ;
00746 check( clean_frameset = cpl_frameset_new() ) ;
00747
00748 sprintf( arm_str, "%s_", xsh_instrument_arm_tostring(instrument) ) ;
00749
00750
00751
00752 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00753 cpl_frame * a_b = NULL ;
00754 cpl_frame * rm_bkg = NULL ;
00755 char str[16] ;
00756
00757 sprintf( str, "%d", i ) ;
00758 a_b = cpl_frameset_get_frame( sub_frameset, i ) ;
00759
00760
00761 xsh_msg("Subtract inter-order background %d", i );
00762 sprintf(file_name, "%s%s",rec_prefix, str) ;
00763 xsh_free_frame( &frame_backg);
00764 check(rm_bkg = xsh_subtract_background( a_b,
00765 order_tab_edges,
00766 backg_par, instrument,
00767 file_name,&grid_back,
00768 &frame_backg,0,0,0));
00769
00770 check( cpl_frameset_insert( rmbkg_frameset, rm_bkg ) ) ;
00771 }
00772
00773
00774 check( flat_method = xsh_parameters_get_string( parameters, RECIPE_ID,
00775 "flat-method"));
00776 if ( strcmp( flat_method, "master") != 0){
00777 xsh_msg("---Create blaze image");
00778 check( blaze_frame = xsh_blaze_image( master_flat, order_tab_edges,
00779 instrument));
00780 }
00781
00782 if ( nb_sub_frames < 3 && crh_single_par->nb_iter > 0 ) {
00783 xsh_msg( "Less than 3 frames AND crh_single_niter > 0" ) ;
00784 for ( i = 0 ; i < nb_sub_frames ; i++ ) {
00785 cpl_frame * divided = NULL ;
00786 cpl_frame * rm_crh = NULL ;
00787 cpl_frame * a_b = NULL ;
00788
00789 check( a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ) ;
00790 sprintf( file_tag, "NO_CRH_IFU_OFFSET_%s%d", arm_str,i);
00791 xsh_msg( "Remove crh (single frame)" );
00792 check( rm_crh = xsh_remove_crh_single( a_b,instrument,
00793 crh_single_par,
00794 file_tag ));
00795 check( cpl_frameset_insert( nocrh_frameset, rm_crh));
00796
00797 xsh_msg( "Divide by flat" ) ;
00798 sprintf( file_tag, "FF_IFU_OFFSET_%s%d", arm_str, i);
00799 if ( strcmp( flat_method, "master") == 0){
00800 check( divided = xsh_divide_flat( rm_crh, master_flat,
00801 file_tag, instrument));
00802 }
00803 else{
00804 check( divided = xsh_divide_by_blaze( rm_crh,
00805 blaze_frame, instrument));
00806 }
00807
00808 check( cpl_frameset_insert( clean_frameset, divided));
00809 }
00810 }
00811 else for( i = 0 ; i < nb_sub_frames ; i++ ) {
00812 cpl_frame * divided = NULL ;
00813 cpl_frame * a_b = NULL ;
00814
00815
00816 a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ;
00817
00818
00819 xsh_msg( "Divide by flat" ) ;
00820 sprintf( file_tag, "FF_IFU_OFFSET_%s%d", arm_str, i);
00821
00822 if ( strcmp( flat_method, "master") == 0){
00823 check( divided = xsh_divide_flat( a_b, master_flat,
00824 file_tag, instrument));
00825 }
00826 else{
00827 check( divided = xsh_divide_by_blaze( a_b,
00828 blaze_frame, instrument));
00829 }
00830 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00831 }
00832
00833
00834
00835 xsh_msg( "Combining all frames" );
00836 sprintf( file_tag, "COMBINED_IFU_OFFSET_%s_ALL", arm_str);
00837
00838
00839 check( comb_frame = xsh_combine_offset( clean_frameset,
00840 file_tag, stack_par,
00841 instrument, NULL, NULL,0));
00842
00843
00844
00845 xsh_msg( "Rectify combined frame" ) ;
00846 rect2_frameset_eso=cpl_frameset_new();
00847 rect2_frameset_tab=cpl_frameset_new();
00848 check( rect2_frameset = xsh_rectify_ifu( comb_frame, order_tab_edges,
00849 wavetab_frameset,
00850 nshiftifu_frameset,
00851 model_config_frame,
00852 instrument, rectify_par,
00853 spectral_format,
00854 slitmap_frame, &rect2_frameset_eso,
00855 &rect2_frameset_tab,rec_prefix));
00856
00857 #if 0
00858
00859 if ( extract_par->method == LOCALIZATION_METHOD &&
00860 loc_table_frameset == NULL ) {
00861 xsh_msg( "Localize obj ifu frame" ) ;
00862 check( loc_table_frameset = xsh_localize_obj_ifu( rect2_frameset, NULL,
00863 instrument,
00864 loc_obj_par, NULL) ) ;
00865 }
00866
00867 xsh_msg( "Extract ifu frame" ) ;
00868 check(rect2_frameset_tables=xsh_frameset_ext_table_frames(rect2_frameset));
00869 check(ext_frameset = xsh_extract_ifu(rect2_frameset_tables,
00870 loc_table_frameset,
00871 instrument, extract_par,rec_prefix )) ;
00872 xsh_msg( "Merge orders with 1D frame" ) ;
00873 check(ext_frameset_tables=xsh_frameset_ext_table_frames(ext_frameset));
00874
00875 check( res_1D_frameset = xsh_merge_ord_ifu( ext_frameset_tables, instrument,
00876 merge_par,rec_prefix ));
00877 xsh_free_frameset(&ext_frameset_tables);
00878 #endif
00879
00880
00881
00882 xsh_msg( "Merge orders with 2D frame" ) ;
00883 check( res_2D_frameset = xsh_merge_ord_ifu( rect2_frameset,instrument,
00884 merge_par,rec_prefix ));
00885
00886 check( data_cube = xsh_cube( res_2D_frameset, instrument, rec_prefix));
00887
00888 xsh_msg( "Saving Products for IFU" ) ;
00889 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
00890 RECIPE_ID, instrument));
00891
00892 name=cpl_frame_get_filename(data_cube);
00893 plist=cpl_propertylist_load(name,0);
00894 naxis2=xsh_pfits_get_naxis2(plist);
00895 xsh_free_propertylist(&plist);
00896 check( qc_trace_frame=xsh_cube_qc_trace_window(data_cube,
00897 instrument,tag,rec_prefix,
00898 save_size+1,
00899 naxis2-save_size,
00900 peack_search_hsize,
00901 method,0));
00902 if(qc_trace_frame) {
00903 check( xsh_add_product_table( qc_trace_frame, frameset,parameters,
00904 RECIPE_ID, instrument,NULL));
00905 }
00906 #if 0
00907 check(ext_frameset_tables=xsh_frameset_ext_table_frames(ext_frameset));
00908 check(ext_frameset_images=xsh_frameset_ext_image_frames(ext_frameset));
00909
00910 xsh_msg( "Saving Products for IFU" ) ;
00911
00912 for( i = 0 ; i<3 ; i++ ) {
00913 cpl_frame * rec_frame = NULL ;
00914 cpl_frame * ext_frame = NULL ;
00915 cpl_frame * res1d_frame = NULL ;
00916 cpl_frame * res2d_frame = NULL ;
00917
00918 check( rec_frame = cpl_frameset_get_frame( rect_frameset_eso, i ) ) ;
00919 xsh_msg( "Product from '%s' [%s]",
00920 cpl_frame_get_filename( rec_frame ),
00921 cpl_frame_get_tag( rec_frame ) ) ;
00922 check( xsh_add_product_image( rec_frame, frameset, parameters,
00923 RECIPE_ID, instrument,NULL));
00924
00925 check( ext_frame = cpl_frameset_get_frame( ext_frameset_images, i ) ) ;
00926 xsh_msg( "Product from '%s' [%s]",
00927 cpl_frame_get_filename( ext_frame ),
00928 cpl_frame_get_tag( ext_frame ) ) ;
00929 check( xsh_add_product_image( ext_frame, frameset, parameters,
00930 RECIPE_ID, instrument,NULL));
00931
00932 check( res2d_frame = cpl_frameset_get_frame( res_2D_frameset, i ) ) ;
00933 xsh_msg( "Product from '%s' [%s]",
00934 cpl_frame_get_filename( res2d_frame ),
00935 cpl_frame_get_tag( res2d_frame ) ) ;
00936 check( xsh_add_product_pre( res2d_frame, frameset, parameters,
00937 RECIPE_ID, instrument));
00938
00939
00940 check( res1d_frame = cpl_frameset_get_frame( res_1D_frameset, i ) ) ;
00941 check(xsh_monitor_spectrum1D_flux(res1d_frame,instrument));
00942
00943
00944 xsh_msg( "Product from '%s' [%s]",
00945 cpl_frame_get_filename( res1d_frame ),
00946 cpl_frame_get_tag( res1d_frame ) ) ;
00947 check( xsh_add_product_spectrum( res1d_frame, frameset, parameters,
00948 RECIPE_ID, instrument));
00949
00950 }
00951 xsh_free_frameset(&rect2_frameset_tables);
00952 xsh_free_frameset(&ext_frameset_tables);
00953
00954 xsh_msg( "Product from '%s' [%s]",
00955 cpl_frame_get_filename( data_cube ),
00956 cpl_frame_get_tag( data_cube ) ) ;
00957 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
00958 RECIPE_ID, instrument));
00959
00960 #endif
00961 cleanup:
00962 xsh_end( RECIPE_ID, frameset, parameters );
00963 XSH_FREE( rec_prefix);
00964 XSH_FREE( backg_par);
00965 XSH_FREE( crh_single_par);
00966 XSH_FREE( rectify_par);
00967 XSH_FREE( loc_obj_par);
00968 XSH_FREE( stack_par);
00969
00970 XSH_FREE( extract_par);
00971 xsh_instrument_free(&instrument);
00972
00973 xsh_free_frameset(&ext_frameset_images);
00974 xsh_free_frameset(&ext_frameset_tables);
00975
00976 xsh_free_frameset(&raws);
00977 xsh_free_frameset(&calib);
00978 xsh_free_frameset( &wavetab_frameset);
00979 xsh_free_frameset( &shiftifu_frameset);
00980 xsh_free_frameset( &nshiftifu_frameset);
00981 xsh_free_frameset( &object_frameset);
00982 xsh_free_frameset( &sky_frameset);
00983 xsh_free_frameset( &sub_frameset ) ;
00984 xsh_free_frameset( &nocrh_frameset);
00985 xsh_free_frameset( &clean_frameset ) ;
00986 xsh_free_frameset( &rmbkg_frameset ) ;
00987 xsh_free_frameset(&res_1D_frameset) ;
00988 xsh_free_frameset(&res_2D_frameset) ;
00989
00990 xsh_free_frameset( &rect2_frameset) ;
00991 xsh_free_frameset( &rect2_frameset_eso);
00992 xsh_free_frameset( &rect2_frameset_tab);
00993 xsh_free_frameset(&rect2_frameset_tables);
00994
00995 xsh_free_frameset(&ext_frameset) ;
00996 xsh_free_frame( &slitmap_frame);
00997 xsh_free_frame( &wavemap_frame);
00998 xsh_free_frame( &comb_frame);
00999 xsh_free_frame( &data_cube);
01000 xsh_free_frame( &grid_back);
01001 xsh_free_frame( &frame_backg);
01002 xsh_free_frame( &blaze_frame);
01003 xsh_free_frame( &qc_trace_frame);
01004 return;
01005 }
01006