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_utils_scired_slit.h>
00053
00054 #include <xsh_msg.h>
00055
00056 #include <xsh_dfs.h>
00057 #include <xsh_pfits.h>
00058 #include <xsh_data_spectrum1D.h>
00059
00060 #include <xsh_drl.h>
00061
00062 #include <cpl.h>
00063
00064
00065
00066
00067
00068 #define RECIPE_ID "xsh_scired_slit_offset"
00069 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer"
00070 #define RECIPE_CONTACT "amodigli@eso.org"
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 static int xsh_scired_slit_offset_create( cpl_plugin *);
00081 static int xsh_scired_slit_offset_exec( cpl_plugin *);
00082 static int xsh_scired_slit_offset_destroy( cpl_plugin *);
00083
00084
00085 static void xsh_scired_slit_offset( cpl_parameterlist *, cpl_frameset *);
00086
00087
00088
00089
00090 static char xsh_scired_slit_offset_description_short[] =
00091 "Reduce science exposure in SLIT configuration and on/off mode";
00092
00093 static char xsh_scired_slit_offset_description[] =
00094 "This recipe reduces science exposure in SLIT configuration and on/off mode\n\
00095 Input Frames : \n\
00096 - A set of n Science frames ( n even ), \
00097 Tag = STD_FLUX_SLIT_OFFSET_arm, SKY_SLIT_arm\n\
00098 - Spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00099 - A master flat frame (Tag = MASTER_FLAT_SLIT_arm)\n\
00100 - An order table frame(Tag = ORDER_TABLE_EDGES_SLIT_arm)\n\
00101 - A wavelength calibration solution frame(Tag = WAVE_TAB_2D_arm)\n\
00102 - [OPTIONAL] Table with dispersion coefficients (Tag = DISP_TAB_arm)\n\
00103 It is required if -rectify-conserve-flux=TRUE\n\
00104 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
00105 - [OPTIONAL] The instrument response table (Tag = RESPONSE_MERGE1D_SLIT_arm)\n\
00106 - [OPTIONAL] An atmospheric extinction table (Tag = ATMOS_EXT_arm)\n\
00107 Products : \n\
00108 - PREFIX_ORDER2D_arm extracted spectrum, order-by-order, 2D\n\
00109 - PREFIX_ORDER1D_arm extracted spectrum, order-by-order, 1D\n\
00110 - PREFIX_MERGE2D_arm merged spectrum, 2D\n\
00111 - PREFIX_MERGE1D_arm merged spectrum, 1D\n\
00112 - where PREFIX is SCI, FLUX, TELL if input raw DPR.TYPE contains OBJECT or FLUX or TELLURIC\n\
00113 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUXCAL2D_MERGE2D_arm (2 dimension)\n\
00114 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUXCAL1D_MERGE1D_arm (1 dimension)\n";
00115
00116
00117
00118
00119
00128
00129
00130 int cpl_plugin_get_info(cpl_pluginlist *list) {
00131 cpl_recipe *recipe = NULL;
00132 cpl_plugin *plugin = NULL;
00133
00134 recipe = cpl_calloc(1, sizeof(*recipe));
00135 if ( recipe == NULL ){
00136 return -1;
00137 }
00138
00139 plugin = &recipe->interface ;
00140
00141 cpl_plugin_init(plugin,
00142 CPL_PLUGIN_API,
00143 XSH_BINARY_VERSION,
00144 CPL_PLUGIN_TYPE_RECIPE,
00145 RECIPE_ID,
00146 xsh_scired_slit_offset_description_short,
00147 xsh_scired_slit_offset_description,
00148 RECIPE_AUTHOR,
00149 RECIPE_CONTACT,
00150 xsh_get_license(),
00151 xsh_scired_slit_offset_create,
00152 xsh_scired_slit_offset_exec,
00153 xsh_scired_slit_offset_destroy);
00154
00155 cpl_pluginlist_append(list, plugin);
00156
00157 return (cpl_error_get_code() != CPL_ERROR_NONE);
00158 }
00159
00160
00170
00171
00172 static int xsh_scired_slit_offset_create(cpl_plugin *plugin){
00173 cpl_recipe *recipe = NULL;
00174 cpl_parameter* p=NULL;
00175
00176 xsh_clipping_param crh_clip_param = {0.3, 5.0, 5, 0.7, 0};
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
00186 xsh_localize_obj_param loc_obj =
00187 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3,FALSE};
00188 xsh_extract_param extract_par =
00189 { LOCALIZATION_METHOD};
00190 xsh_combine_nod_param nod_param = { 5, TRUE, 5, 2, 0.1, NULL, COMBINE_MEAN_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
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_optimal_extract_create(RECIPE_ID,
00234 recipe->parameters,-1. )) ;
00235
00236 check(xsh_parameters_extract_create(RECIPE_ID,
00237 recipe->parameters,
00238 extract_par,LOCALIZATION_METHOD )) ;
00239
00240 check(xsh_parameters_merge_ord_create(RECIPE_ID,
00241 recipe->parameters,
00242 WEIGHTED_MERGE_METHOD)) ;
00243
00244 check(xsh_parameters_combine_nod_create(RECIPE_ID,
00245 recipe->parameters,
00246 nod_param )) ;
00247
00248 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00249 "do-flatfield", TRUE,
00250 "TRUE if we do the flatfielding"));
00251
00252 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00253 "gen-sky", FALSE,
00254 "if TRUE a 2D sky frame, a 2D rectified, a 2D merged sky are generated"));
00255
00256 cleanup:
00257 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00258 xsh_error_dump(CPL_MSG_ERROR);
00259 return 1;
00260 }
00261 else {
00262 return 0;
00263 }
00264 }
00265
00266
00272
00273
00274 static int xsh_scired_slit_offset_exec(cpl_plugin *plugin) {
00275 cpl_recipe *recipe = NULL;
00276
00277
00278
00279 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00280
00281
00282 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00283 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00284
00285 recipe = (cpl_recipe *)plugin;
00286
00287
00288 xsh_scired_slit_offset(recipe->parameters, recipe->frames);
00289
00290 cleanup:
00291 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00292 xsh_error_dump(CPL_MSG_ERROR);
00293 xsh_error_reset();
00294 return 1;
00295 }
00296 else {
00297 return 0;
00298 }
00299 }
00300
00301
00307
00308 static int xsh_scired_slit_offset_destroy(cpl_plugin *plugin)
00309 {
00310 cpl_recipe *recipe = NULL;
00311
00312
00313 xsh_error_reset();
00314
00315 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00316
00317
00318 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00319 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00320
00321 recipe = (cpl_recipe *)plugin;
00322
00323 xsh_free_parameterlist(&recipe->parameters);
00324
00325 cleanup:
00326 if (cpl_error_get_code() != CPL_ERROR_NONE)
00327 {
00328 return 1;
00329 }
00330 else
00331 {
00332 return 0;
00333 }
00334 }
00335
00336
00337 static cpl_error_code
00338 xsh_params_monitor(xsh_background_param* backg,
00339 xsh_rectify_param * rectify_par,
00340 xsh_localize_obj_param * loc_obj_par)
00341 {
00342
00343
00344 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00345 backg->sampley,backg->radius_y,backg->smooth_y);
00346
00347 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00348 backg->radius_x,backg->smooth_x);
00349
00350 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00351 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00352 rectify_par->rectif_bin_space);
00353
00354 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00355 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00356
00357 return cpl_error_get_code();
00358
00359 }
00360
00361
00362
00363 static cpl_error_code
00364 xsh_params_set_defaults(cpl_parameterlist* pars,
00365 xsh_instrument* inst,
00366 xsh_rectify_param * rectify_par)
00367 {
00368 cpl_parameter* p=NULL;
00369
00370 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00371
00372 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-slit"));
00373 if(xsh_parameter_get_default_flag(p) == 0) {
00374
00375 rectify_par->rectif_bin_space=XSH_SLIT_BIN_SIZE_PIPE_NIR;
00376 cpl_parameter_set_double(p,XSH_SLIT_BIN_SIZE_PIPE_NIR);
00377 }
00378
00379 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-lambda"));
00380 if(xsh_parameter_get_default_flag(p) == 0) {
00381
00382 rectify_par->rectif_bin_lambda=XSH_WAVE_BIN_SIZE_PIPE_NIR;
00383 cpl_parameter_set_double(p,XSH_WAVE_BIN_SIZE_PIPE_NIR);
00384 }
00385
00386
00387
00388 } else {
00389
00390
00391 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-slit"));
00392 if(xsh_parameter_get_default_flag(p) == 0) {
00393
00394 rectify_par->rectif_bin_space=XSH_SLIT_BIN_SIZE_PIPE_UVB;
00395 cpl_parameter_set_double(p,XSH_SLIT_BIN_SIZE_PIPE_UVB);
00396 }
00397
00398 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-lambda"));
00399 if(xsh_parameter_get_default_flag(p) == 0) {
00400
00401 rectify_par->rectif_bin_lambda=XSH_WAVE_BIN_SIZE_PIPE_UVB;
00402 cpl_parameter_set_double(p,XSH_WAVE_BIN_SIZE_PIPE_UVB);
00403
00404 }
00405
00406
00407 }
00408
00409 cleanup:
00410
00411 return cpl_error_get_code();
00412
00413 }
00414
00415
00416
00417
00425
00426
00427 static cpl_error_code
00428 xsh_params_bin_scale(cpl_frameset* raws,
00429 xsh_background_param* backg)
00430 {
00431
00432 cpl_frame* frame=NULL;
00433 const char* name=NULL;
00434 cpl_propertylist* plist=NULL;
00435 int binx=0;
00436 int biny=0;
00437 xsh_msg("Rescale relevant parameters to bin size");
00438 check(frame=cpl_frameset_get_first(raws));
00439 check(name=cpl_frame_get_filename(frame));
00440
00441 check(plist=cpl_propertylist_load(name,0));
00442 check(binx=xsh_pfits_get_binx(plist));
00443 check(biny=xsh_pfits_get_biny(plist));
00444 xsh_free_propertylist(&plist);
00445
00446 if(biny>1) {
00447
00448
00449
00450
00451
00452
00453 backg->radius_y=backg->radius_y/biny;
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 }
00480
00481
00482 if(binx>1) {
00483
00484 backg->radius_x=backg->radius_x/binx;
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 }
00505
00506 cleanup:
00507 xsh_free_propertylist(&plist);
00508 return cpl_error_get_code();
00509
00510 }
00511
00512
00513
00521
00522 static void xsh_scired_slit_offset( cpl_parameterlist* parameters,
00523 cpl_frameset* frameset)
00524 {
00525 const char* recipe_tags[3] = {XSH_OBJECT_SLIT_OFFSET, XSH_STD_FLUX_SLIT_OFFSET, XSH_SKY_SLIT};
00526 int recipe_tags_size = 3;
00527
00528
00529 cpl_frameset* raws = NULL;
00530
00531 cpl_frameset * raw_object = NULL;
00532 cpl_frameset * raw_sky = NULL;
00533 cpl_frameset * calib = NULL;
00534 int nb_sub_frames = 0;
00535
00536
00537 cpl_frame* bpmap = NULL;
00538 cpl_frame* master_bias = NULL;
00539 cpl_frame* master_dark = NULL;
00540 cpl_frame* master_flat = NULL;
00541 cpl_frame* order_tab_edges = NULL;
00542 cpl_frame * wave_tab = NULL ;
00543 cpl_frame * model_config_frame = NULL ;
00544 cpl_frame * wavemap = NULL ;
00545 cpl_frame *disp_tab_frame = NULL;
00546 cpl_frame * spectral_format = NULL ;
00547
00548
00549 xsh_clipping_param* crh_clipping_par = NULL;
00550 xsh_background_param* backg_par = NULL;
00551 xsh_remove_crh_single_param * crh_single_par = NULL ;
00552 xsh_rectify_param * rectify_par = NULL ;
00553 xsh_localize_obj_param * loc_obj_par = NULL ;
00554
00555 double opt_kappa = -1. ;
00556 xsh_merge_param* merge_par = NULL;
00557 xsh_extract_param * extract_par = NULL ;
00558 xsh_combine_nod_param * combine_nod_param = NULL ;
00559
00560 xsh_instrument* instrument = NULL;
00561
00562
00563
00564 cpl_frame * loc_table_frame = NULL ;
00565 cpl_frame * clean_frame = NULL ;
00566 cpl_frameset * sub_frameset = NULL ;
00567 cpl_frameset * rmbkg_frameset = NULL ;
00568 cpl_frameset * clean_frameset = NULL ;
00569 cpl_frameset * rect_frameset = NULL ;
00570 cpl_frameset * rect2_frameset_tables = NULL ;
00572
00573 cpl_frame * res1D_frame = NULL ;
00574 cpl_frame * res2D_frame = NULL ;
00575 cpl_frame * ext_frame = NULL ;
00576 cpl_frame * ext_frame_eso = NULL ;
00577 cpl_frame * comb_frame = NULL ;
00578 cpl_frame * comb_frame_eso = NULL ;
00579 cpl_frame* response_ord_frame=NULL;
00580
00581 char file_name[80];
00582 char arm_str[16] ;
00583 char file_tag[40];
00584
00585 int i ;
00586 cpl_frame* grid_back=NULL;
00587 cpl_frame* frame_backg=NULL;
00588 int do_flatfield=0;
00589 int gen_sky=0;
00590 char rec_prefix[80];
00591 char sky_prefix[80];
00592 char sky_tag[80];
00593 cpl_frame* avg_sky=NULL;
00594 cpl_frame* rec_sky=NULL;
00595 cpl_frame* rec_sky_eso=NULL;
00596 cpl_frame* mer_sky=NULL;
00597 cpl_frame * rectif_tab = NULL ;
00598 cpl_frame * sky_divided = NULL ;
00599 cpl_frameset* sky_bias=NULL;
00600 cpl_frameset* sky_dark=NULL;
00601 cpl_frame * fluxcal_rect_1D_frame = NULL ;
00602 cpl_frame * fluxcal_rect_2D_frame = NULL ;
00603 cpl_frame * fluxcal_1D_frame = NULL ;
00604 cpl_frame * fluxcal_2D_frame = NULL ;
00605 cpl_frame* frm_atmext=NULL;
00606 int pre_overscan_corr=0;
00607
00608
00609
00610
00611 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00612 recipe_tags, recipe_tags_size,
00613 RECIPE_ID, XSH_BINARY_VERSION,
00614 xsh_scired_slit_offset_description_short));
00615
00616 check(xsh_ensure_raws_input_offset_recipe_is_proper(raws,instrument));
00617 assure( instrument->mode == XSH_MODE_SLIT, CPL_ERROR_ILLEGAL_INPUT,
00618 "Instrument NOT in Slit Mode" ) ;
00619
00620
00621
00622
00623 check(xsh_slit_offset_get_calibs(calib,instrument,&bpmap,&master_bias,
00624 &master_dark,&order_tab_edges,
00625 &model_config_frame,&wave_tab,&master_flat,
00626 &wavemap,&spectral_format));
00627
00628 check( response_ord_frame = xsh_find_frame_with_tag(calib,
00629 XSH_RESPONSE_MERGE1D_SLIT,
00630 instrument));
00631
00632 if(response_ord_frame != NULL) {
00633 frm_atmext=xsh_find_frame_with_tag(calib,XSH_ATMOS_EXT,instrument);
00634 if(frm_atmext==NULL) {
00635 xsh_msg_error("Provide atmospheric extinction frame");
00636 }
00637 }
00638
00639
00640
00641
00642 check(xsh_slit_offset_get_params(parameters,RECIPE_ID,
00643 &crh_clipping_par,&backg_par,
00644 &loc_obj_par,&rectify_par,&crh_single_par,
00645 &opt_kappa,&merge_par,&extract_par,
00646 &combine_nod_param,&do_flatfield,&gen_sky));
00647
00648 check(xsh_params_set_defaults(parameters,instrument,rectify_par));
00649
00650 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00651 check(xsh_params_bin_scale(raws,backg_par));
00652 }
00653 check(xsh_params_monitor(backg_par,rectify_par,loc_obj_par));
00654
00655 if ( rectify_par->conserve_flux){
00656 check_msg( disp_tab_frame = xsh_find_disp_tab( calib, instrument),
00657 "if rectify-conserve-flux=TRUE DISP_TAB_ARM is required input");
00658 }
00659
00660
00661
00662
00663 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_SLIT_OFFSET,
00664 instrument,pre_overscan_corr));
00665
00666
00667 check( nb_sub_frames = xsh_dfs_split_offset( raws, XSH_OBJECT_SLIT_OFFSET,
00668 XSH_SKY_SLIT, &raw_object,
00669 &raw_sky ) ) ;
00670
00671 check(strcpy(rec_prefix,
00672 xsh_set_recipe_file_prefix(raw_object,"xsh_scired_slit_offset")));
00673
00674 if(gen_sky) {
00675 if(strstr(rec_prefix,"SCI") != NULL) {
00676 strcpy(sky_prefix,"SKY_SLIT");
00677 } else if(strstr(rec_prefix,"TELL") != NULL) {
00678 strcpy(sky_prefix,"TELL_SLIT_SKY");
00679 } else if(strstr(rec_prefix,"FLUX") != NULL) {
00680 strcpy(sky_prefix,"FLUX_SLIT_SKY");
00681 } else {
00682 strcpy(sky_prefix,"CAL_SLIT_SKY");
00683 }
00684 }
00685 if(nb_sub_frames==0) {
00686 xsh_msg_error("nb_sub_frames=%d something wrong check your input raws",
00687 nb_sub_frames);
00688 goto cleanup;
00689
00690 }
00691
00692 check( sub_frameset = xsh_subtract_sky_offset( raw_object, raw_sky,
00693 nb_sub_frames,
00694 instrument )) ;
00695
00696 xsh_msg_dbg_low( "Nb of Subtracted Frames: %d",
00697 cpl_frameset_get_size( sub_frameset ) ) ;
00698
00699 check( clean_frameset = cpl_frameset_new() ) ;
00700 check( rmbkg_frameset = cpl_frameset_new() ) ;
00701
00702 sprintf( arm_str, "%s", xsh_instrument_arm_tostring(instrument) ) ;
00703
00704 if(gen_sky) {
00705
00706 if(master_bias!= NULL && pre_overscan_corr ==0 ) {
00707 check(sky_bias=xsh_pre_frameset_subtract_frame(raw_sky,master_bias,"MBIAS_",instrument));
00708 } else {
00709 sky_bias=cpl_frameset_duplicate(raw_sky);
00710 }
00711 if(master_dark!= NULL) {
00712 check(sky_dark=xsh_pre_frameset_subtract_frame(sky_bias,master_dark,"MDARK",instrument));
00713 } else {
00714 sky_dark=cpl_frameset_duplicate(sky_bias);
00715 }
00716 sprintf(sky_tag,"%s_SKY_%s",rec_prefix,arm_str);
00717 check(avg_sky=xsh_frameset_average_pre(sky_dark,instrument,sky_tag));
00718 if(do_flatfield==1) {
00719 sprintf(sky_tag,"%s_FF_SKY_%s",rec_prefix,arm_str);
00720 check( sky_divided = xsh_divide_flat( avg_sky,master_flat, sky_tag,
00721 instrument ) ) ;
00722 } else {
00723 sky_divided=cpl_frame_duplicate(avg_sky);
00724 }
00725 }
00726
00727
00728 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00729 cpl_frame * a_b = NULL ;
00730 cpl_frame * rm_bkg = NULL ;
00731 char str[16] ;
00732
00733 sprintf( str, "_%d", i ) ;
00734 a_b = cpl_frameset_get_frame( sub_frameset, i ) ;
00735
00736
00737 xsh_msg("Subtract inter-order background [%d]", i );
00738 sprintf(file_name,"%s%s", rec_prefix, str) ;
00739 check(rm_bkg = xsh_subtract_background( a_b,
00740 order_tab_edges,
00741 backg_par, instrument,
00742 file_name,&grid_back,
00743 &frame_backg));
00744 check( cpl_frameset_insert( rmbkg_frameset, rm_bkg ) ) ;
00745 }
00746
00747 xsh_msg_dbg_low( "Nb of Frames Bkg Subtracted: %d",
00748 cpl_frameset_get_size( rmbkg_frameset ) ) ;
00749
00750 if ( nb_sub_frames < 3 && crh_single_par->nb_iter > 0 ) {
00751 xsh_msg( "Less than 3 frames AND removecrhsingle_niter > 0" ) ;
00752 for ( i = 0 ; i < nb_sub_frames ; i++ ) {
00753 cpl_frame * divided = NULL ;
00754 cpl_frame * rm_crh = NULL ;
00755 cpl_frame * a_b = NULL ;
00756 char str[16] ;
00757
00758 check( a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ) ;
00759 sprintf( str, "%d", i ) ;
00760 sprintf(file_tag,"NO_CRH_SLIT_OFFSET_%s%s",arm_str,str) ;
00761 xsh_msg( "Remove crh (single frame)" ) ;
00762 check( rm_crh = xsh_remove_crh_single( a_b, wavemap,instrument,
00763 crh_single_par,
00764 file_tag ) ) ;
00765 if(do_flatfield==1) {
00766 xsh_msg( "Calling xsh_divide_flat" ) ;
00767 sprintf(file_tag,"FF_SLIT_OFFSET_%s%s", arm_str,str) ;
00768 check( divided = xsh_divide_flat( rm_crh,master_flat, file_tag,
00769 instrument ) ) ;
00770 } else {
00771 divided=cpl_frame_duplicate(rm_crh);
00772 }
00773 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00774 }
00775 }
00776 else for( i = 0 ; i < nb_sub_frames ; i++ ) {
00777 cpl_frame * divided = NULL ;
00778 cpl_frame * a_b = NULL ;
00779 char str[16] ;
00780
00781
00782 a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ;
00783 sprintf( str, "%d", i ) ;
00784
00785 if(do_flatfield==1) {
00786
00787 xsh_msg( "Calling xsh_divide_flat" ) ;
00788
00789 sprintf(file_tag,"FF_SLIT_OFFSET_%s%s", arm_str,str) ;
00790 check( divided = xsh_divide_flat( a_b,master_flat, file_tag,
00791 instrument ) ) ;
00792 } else {
00793 divided=cpl_frame_duplicate(a_b);
00794 }
00795 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00796 }
00797
00798 {
00799 int clean_size ;
00800
00801 check( clean_size = cpl_frameset_get_size( clean_frameset ) ) ;
00802 xsh_msg( "Nb of clean Frames: %d", clean_size ) ;
00803 }
00804
00805
00806 check( rect_frameset = cpl_frameset_new() ) ;
00807 for( i = 0 ; i < nb_sub_frames ; i++ ) {
00808 cpl_frame * rectif = NULL ;
00809 cpl_frame * rectif_eso = NULL ;
00810 cpl_frame * clean = NULL ;
00811 char str[16] ;
00812
00813 sprintf( str, "%d", i ) ;
00814 check( clean = cpl_frameset_get_frame( clean_frameset, i ) ) ;
00815 xsh_msg( "Rectifying Frame '%s'", cpl_frame_get_filename( clean ) ) ;
00816 sprintf(file_name,"RECTIFIED_SLIT_OFFSET_%s_%s.fits",arm_str,str) ;
00817 check( rectif = xsh_rectify( clean, order_tab_edges, wave_tab,
00818 model_config_frame, instrument,
00819 rectify_par, spectral_format,
00820 disp_tab_frame,
00821 file_name,&rectif_eso,&rectif_tab,
00822 rec_prefix ) ) ;
00823 check( cpl_frameset_insert( rect_frameset, rectif ) ) ;
00824 check( cpl_frameset_insert( rect_frameset, rectif_eso ) ) ;
00825 xsh_free_frame(&rectif_tab);
00826 }
00827
00828
00829
00830 if(gen_sky) {
00831 xsh_msg("rectify sky frame");
00832 sprintf(file_name,"%s_RECTIFIED_SKY_%s.fits",sky_prefix,arm_str) ;
00833 check( rec_sky = xsh_rectify(sky_divided, order_tab_edges, wave_tab,
00834 model_config_frame, instrument,
00835 rectify_par, spectral_format,
00836 disp_tab_frame,
00837 file_name,&rec_sky_eso,&rectif_tab,
00838 sky_prefix ) ) ;
00839
00840
00841 xsh_msg("Merge 2D sky frame");
00842 check( mer_sky = xsh_merge_ord( rec_sky, instrument,
00843 merge_par,sky_prefix ));
00844
00845
00846 }
00847
00848
00849 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_ORDER2D, arm_str) ;
00850 check(rect2_frameset_tables=xsh_frameset_ext_table_frames(rect_frameset));
00851 check( comb_frame = xsh_combine_nod(rect2_frameset_tables, combine_nod_param,
00852 file_tag,instrument,&comb_frame_eso));
00853 xsh_free_frameset(&rect2_frameset_tables);
00854
00855 if ( extract_par->method == LOCALIZATION_METHOD ) {
00856 xsh_msg( "Localize before extraction" ) ;
00857 sprintf(file_name,"LOCALIZE_%s_ALL.fits",arm_str) ;
00858 check( loc_table_frame = xsh_localize_obj( comb_frame, NULL,instrument,
00859 loc_obj_par, NULL,
00860 file_name) ) ;
00861 }
00862
00863 xsh_msg( "Calling xsh_extract" ) ;
00864 check(ext_frame = xsh_extract(comb_frame, loc_table_frame,
00865 instrument, extract_par,&ext_frame_eso,
00866 rec_prefix )) ;
00867 xsh_msg( "Calling xsh_merge_ord with 1D frame" ) ;
00868 check( res1D_frame = xsh_merge_ord( ext_frame, instrument,
00869 merge_par,rec_prefix ));
00870
00871
00872
00873
00874
00875 xsh_msg( "Calling xsh_merge_ord with 2D frame" ) ;
00876 check( res2D_frame = xsh_merge_ord( comb_frame, instrument, merge_par,
00877 rec_prefix ) ) ;
00878
00879 if(response_ord_frame != NULL && frm_atmext != NULL) {
00880 check(xsh_flux_calibrate(comb_frame_eso,ext_frame_eso,frm_atmext,
00881 response_ord_frame,merge_par,instrument,rec_prefix,
00882 &fluxcal_rect_2D_frame,&fluxcal_rect_1D_frame,
00883 &fluxcal_2D_frame,&fluxcal_1D_frame));
00884
00885 }
00886
00887
00888 xsh_msg( "Saving products" ) ;
00889
00890
00891
00892
00893
00894
00895 check( xsh_add_product_image( comb_frame_eso, frameset,
00896 parameters, RECIPE_ID, instrument,
00897 cpl_frame_get_tag(comb_frame_eso)));
00898
00899
00900
00901
00902
00903
00904 check( xsh_add_product_image( ext_frame_eso, frameset,
00905 parameters, RECIPE_ID, instrument,
00906 cpl_frame_get_tag(ext_frame_eso)));
00907
00908
00909 check( xsh_add_product_spectrum( res2D_frame, frameset, parameters,
00910 RECIPE_ID, instrument));
00911
00912 check(xsh_monitor_spectrum1D_flux(res1D_frame,instrument));
00913 check( xsh_add_product_spectrum( res1D_frame, frameset, parameters,
00914 RECIPE_ID, instrument));
00915
00916
00917 if(gen_sky) {
00918 check( xsh_add_product_image( avg_sky, frameset, parameters,
00919 RECIPE_ID, instrument,
00920 cpl_frame_get_tag(avg_sky)));
00921
00922
00923 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_ORDER2D, arm_str) ;
00924 check( xsh_add_product_image( rec_sky_eso, frameset, parameters,
00925 RECIPE_ID, instrument,
00926 cpl_frame_get_tag(rec_sky_eso)));
00927
00928
00929 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_MERGE2D, arm_str) ;
00930 check( xsh_add_product_image( mer_sky, frameset, parameters,
00931 RECIPE_ID, instrument,
00932 cpl_frame_get_tag(mer_sky)));
00933
00934 }
00935
00936 if(response_ord_frame != NULL) {
00937 check( xsh_add_product_image(fluxcal_rect_2D_frame,frameset,parameters,
00938 RECIPE_ID, instrument,
00939 cpl_frame_get_filename(fluxcal_rect_2D_frame)));
00940 check( xsh_add_product_image(fluxcal_rect_1D_frame,frameset,parameters,
00941 RECIPE_ID, instrument,
00942 cpl_frame_get_filename(fluxcal_rect_1D_frame)));
00943 check( xsh_add_product_spectrum( fluxcal_2D_frame, frameset, parameters,
00944 RECIPE_ID, instrument));
00945 check( xsh_add_product_spectrum( fluxcal_1D_frame, frameset, parameters,
00946 RECIPE_ID, instrument));
00947 }
00948
00949
00950 cleanup:
00951
00952 xsh_end( RECIPE_ID, frameset, parameters );
00953 XSH_FREE(crh_clipping_par);
00954 XSH_FREE( backg_par ) ;
00955 XSH_FREE( crh_single_par ) ;
00956 XSH_FREE( rectify_par ) ;
00957 XSH_FREE( loc_obj_par ) ;
00958 XSH_FREE(merge_par);
00959 XSH_FREE(extract_par);
00960 XSH_FREE(combine_nod_param);
00961 xsh_instrument_free(&instrument );
00962 xsh_free_frameset(&rect2_frameset_tables);
00963 xsh_free_frameset(&raws);
00964
00965
00966
00967
00968 xsh_free_frameset(&calib);
00969 xsh_free_frameset(&sub_frameset);
00970 xsh_free_frameset(&clean_frameset);
00971 xsh_free_frameset(&rmbkg_frameset);
00972 xsh_free_frameset(&rect_frameset);
00973
00974 xsh_free_frameset(&sky_bias);
00975 xsh_free_frameset(&sky_dark);
00976
00977
00978 xsh_free_frame(&loc_table_frame) ;
00979 xsh_free_frame(&clean_frame) ;
00980 xsh_free_frame(&res1D_frame) ;
00981 xsh_free_frame(&res2D_frame) ;
00982 xsh_free_frame( &comb_frame ) ;
00983 xsh_free_frame( &comb_frame_eso ) ;
00984 xsh_free_frame( &ext_frame ) ;
00985
00986 xsh_free_frame( &avg_sky ) ;
00987 xsh_free_frame( &sky_divided ) ;
00988 xsh_free_frame( &rec_sky ) ;
00989 xsh_free_frame( &rec_sky_eso ) ;
00990 xsh_free_frame( &mer_sky ) ;
00991
00992 xsh_free_frame( &frame_backg ) ;
00993 xsh_free_frame( &grid_back ) ;
00994 xsh_free_frame(&fluxcal_rect_1D_frame) ;
00995 xsh_free_frame(&fluxcal_rect_2D_frame) ;
00996 xsh_free_frame(&fluxcal_1D_frame) ;
00997 xsh_free_frame(&fluxcal_2D_frame) ;
00998
00999 return;
01000 }
01001