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 #include <xsh_utils_table.h>
00054 #include <xsh_msg.h>
00055
00056 #include <xsh_dfs.h>
00057 #include <xsh_pfits.h>
00058 #include <xsh_data_spectrum1D.h>
00059 #include <xsh_utils_efficiency.h>
00060
00061 #include <xsh_drl.h>
00062
00063 #include <cpl.h>
00064
00065
00066
00067
00068
00069 #define RECIPE_ID "xsh_respon_slit_offset"
00070 #define RECIPE_AUTHOR "A.Modigliani"
00071 #define RECIPE_CONTACT "amodigli@eso.org"
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 static int xsh_respon_slit_offset_create( cpl_plugin *);
00082 static int xsh_respon_slit_offset_exec( cpl_plugin *);
00083 static int xsh_respon_slit_offset_destroy( cpl_plugin *);
00084
00085
00086 static cpl_error_code xsh_respon_slit_offset( cpl_parameterlist *, cpl_frameset *);
00087
00088
00089
00090
00091 static char xsh_respon_slit_offset_description_short[] =
00092 "Compute the response function in SLIT and on/off mode";
00093
00094 static char xsh_respon_slit_offset_description[] =
00095 "This recipe reduces science exposure in SLIT configuration and on/off mode\n\
00096 Input Frames : \n\
00097 - A set of n Science frames ( n even ), \
00098 Tag = STD_FLUX_SLIT_OFFSET_arm, SKY_SLIT_arm\n\
00099 - Spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00100 - A master flat frame (Tag = MASTER_FLAT_SLIT_arm)\n\
00101 - An order table frame(Tag = ORDER_TABLE_EDGES_SLIT_arm)\n\
00102 - A wavelength calibration solution frame(Tag = WAVE_TAB_2D_arm)\n\
00103 - [OPTIONAL] Table with dispersion coefficients (Tag = DISP_TAB_arm)\n\
00104 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
00105 - A standard star fluxes catalog (Tag = STD_STAR_CATALOG_arm Type = FLX)\n\
00106 - An atmospheric extinction table (Tag = ATMOS_EXT_arm)\n\
00107 Products : \n\
00108 - [If STD is in catal] The response ord-by-ord function (Tag = RESPONSE_ORDER1D_SLIT_arm)\n\
00109 - [If STD is in catal] The response merged function (Tag = RESPONSE_MERGE1D_SLIT_arm)\n\
00110 - PREFIX_ORDER2D_arm extracted spectrum, order-by-order, 2D\n\
00111 - PREFIX_ORDER1D_arm extracted spectrum, order-by-order, 1D\n\
00112 - PREFIX_MERGE2D_arm merged spectrum, 2D\n\
00113 - PREFIX_MERGE1D_arm merged spectrum, 1D\n\
00114 - [If STD is in catal] Flux calibrated merged 2D spectrum (Tag = PREFIX_FLUXCAL2D_arm)\n\
00115 - [If STD is in catal] Flux calibrated merged 1D spectrum (Tag = PREFIX_FLUXCAL1D_arm)\n\
00116 - [If STD is in catal] The efficiency (Tag = EFFICIENCY_arm)\n\
00117 - where PREFIX is SCI, FLUX, TELL if input raw DPR.TYPE contains OBJECT or FLUX or TELLURIC";
00118
00119
00120
00121
00122
00131
00132
00133 int cpl_plugin_get_info(cpl_pluginlist *list) {
00134 cpl_recipe *recipe = NULL;
00135 cpl_plugin *plugin = NULL;
00136
00137 recipe = cpl_calloc(1, sizeof(*recipe));
00138 if ( recipe == NULL ){
00139 return -1;
00140 }
00141
00142 plugin = &recipe->interface ;
00143
00144 cpl_plugin_init(plugin,
00145 CPL_PLUGIN_API,
00146 XSH_BINARY_VERSION,
00147 CPL_PLUGIN_TYPE_RECIPE,
00148 RECIPE_ID,
00149 xsh_respon_slit_offset_description_short,
00150 xsh_respon_slit_offset_description,
00151 RECIPE_AUTHOR,
00152 RECIPE_CONTACT,
00153 xsh_get_license(),
00154 xsh_respon_slit_offset_create,
00155 xsh_respon_slit_offset_exec,
00156 xsh_respon_slit_offset_destroy);
00157
00158 cpl_pluginlist_append(list, plugin);
00159
00160 return (cpl_error_get_code() != CPL_ERROR_NONE);
00161 }
00162
00163
00173
00174
00175 static int xsh_respon_slit_offset_create(cpl_plugin *plugin){
00176 cpl_recipe *recipe = NULL;
00177 cpl_parameter* p=NULL;
00178
00179 xsh_clipping_param crh_clip_param = {0.3, 5.0, 5, 0.7, 0};
00180
00181 xsh_remove_crh_single_param crh_single = { 0.1, 5, 2.0, 4} ;
00182 xsh_rectify_param rectify = { "default",
00183 CPL_KERNEL_DEFAULT,
00184 4,
00185 XSH_WAVE_BIN_SIZE_PIPE_NIR,
00186 XSH_SLIT_BIN_SIZE_PIPE_NIR,
00187 1,0,0.};
00188
00189 xsh_localize_obj_param loc_obj =
00190 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3,FALSE};
00191 xsh_extract_param extract_par =
00192 { LOCALIZATION_METHOD};
00193 xsh_combine_nod_param nod_param = { 5, TRUE, 5, 2, 0.1, NULL,FALSE } ;
00194
00195
00196 xsh_init();
00197
00198
00199 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00200
00201 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00202 CPL_ERROR_TYPE_MISMATCH,
00203 "Plugin is not a recipe");
00204
00205 recipe = (cpl_recipe *)plugin;
00206
00207
00208 recipe->parameters = cpl_parameterlist_new();
00209 assure( recipe->parameters != NULL,
00210 CPL_ERROR_ILLEGAL_OUTPUT,
00211 "Memory allocation failed!");
00212
00213
00214 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00215 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00216
00217
00218 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00219 crh_clip_param));
00220
00221
00222 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00223 check(p=xsh_parameters_find(recipe->parameters,RECIPE_ID,"background-method"));
00224 cpl_parameter_set_default_string(p,"poly");
00225
00226
00227 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00228 crh_single )) ;
00229
00230 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00231 rectify )) ;
00232
00233 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00234 loc_obj )) ;
00235
00236 check(xsh_parameters_optimal_extract_create(RECIPE_ID,
00237 recipe->parameters,-1. )) ;
00238
00239 check(xsh_parameters_extract_create(RECIPE_ID,
00240 recipe->parameters,
00241 extract_par,LOCALIZATION_METHOD )) ;
00242
00243 check(xsh_parameters_merge_ord_create(RECIPE_ID,
00244 recipe->parameters,
00245 WEIGHTED_MERGE_METHOD)) ;
00246
00247 check(xsh_parameters_combine_nod_create(RECIPE_ID,
00248 recipe->parameters,
00249 nod_param )) ;
00250
00251 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00252 "do-flatfield", TRUE,
00253 "TRUE if we do the flatfielding"));
00254
00255 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00256 "gen-sky", FALSE,
00257 "if TRUE a 2D sky frame, a 2D rectified, a 2D merged sky are generated"));
00258
00259 cleanup:
00260 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00261 xsh_error_dump(CPL_MSG_ERROR);
00262 return 1;
00263 }
00264 else {
00265 return 0;
00266 }
00267 }
00268
00269
00275
00276
00277 static int xsh_respon_slit_offset_exec(cpl_plugin *plugin) {
00278 cpl_recipe *recipe = NULL;
00279
00280
00281
00282 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00283
00284
00285 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00286 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00287
00288 recipe = (cpl_recipe *)plugin;
00289
00290
00291 xsh_respon_slit_offset(recipe->parameters, recipe->frames);
00292
00293 cleanup:
00294 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00295 xsh_error_dump(CPL_MSG_ERROR);
00296 xsh_error_reset();
00297 return 1;
00298 }
00299 else {
00300 return 0;
00301 }
00302 }
00303
00304
00310
00311 static int xsh_respon_slit_offset_destroy(cpl_plugin *plugin)
00312 {
00313 cpl_recipe *recipe = NULL;
00314
00315
00316 xsh_error_reset();
00317
00318 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00319
00320
00321 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00322 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00323
00324 recipe = (cpl_recipe *)plugin;
00325
00326 xsh_free_parameterlist(&recipe->parameters);
00327
00328 cleanup:
00329 if (cpl_error_get_code() != CPL_ERROR_NONE)
00330 {
00331 return 1;
00332 }
00333 else
00334 {
00335 return 0;
00336 }
00337 }
00338
00339
00340 static cpl_error_code
00341 xsh_params_monitor(xsh_background_param* backg,
00342 xsh_rectify_param * rectify_par,
00343 xsh_localize_obj_param * loc_obj_par)
00344 {
00345
00346
00347 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00348 backg->sampley,backg->radius_y,backg->smooth_y);
00349
00350 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00351 backg->radius_x,backg->smooth_x);
00352
00353 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00354 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00355 rectify_par->rectif_bin_space);
00356
00357 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00358 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00359
00360 return cpl_error_get_code();
00361
00362 }
00363
00364
00365
00366 static cpl_error_code
00367 xsh_params_set_defaults(cpl_parameterlist* pars,
00368 xsh_instrument* inst,
00369 xsh_rectify_param * rectify_par)
00370 {
00371 cpl_parameter* p=NULL;
00372
00373 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00374
00375 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-slit"));
00376 if(xsh_parameter_get_default_flag(p) == 0) {
00377
00378 rectify_par->rectif_bin_space=XSH_SLIT_BIN_SIZE_PIPE_NIR;
00379 cpl_parameter_set_double(p,XSH_SLIT_BIN_SIZE_PIPE_NIR);
00380 }
00381
00382 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-lambda"));
00383 if(xsh_parameter_get_default_flag(p) == 0) {
00384
00385 rectify_par->rectif_bin_lambda=XSH_WAVE_BIN_SIZE_PIPE_NIR;
00386 cpl_parameter_set_double(p,XSH_WAVE_BIN_SIZE_PIPE_NIR);
00387 }
00388
00389
00390
00391 } else {
00392
00393
00394 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-slit"));
00395 if(xsh_parameter_get_default_flag(p) == 0) {
00396
00397 rectify_par->rectif_bin_space=XSH_SLIT_BIN_SIZE_PIPE_UVB;
00398 cpl_parameter_set_double(p,XSH_SLIT_BIN_SIZE_PIPE_UVB);
00399 }
00400
00401 check(p=xsh_parameters_find(pars,RECIPE_ID,"rectify-bin-lambda"));
00402 if(xsh_parameter_get_default_flag(p) == 0) {
00403
00404 rectify_par->rectif_bin_lambda=XSH_WAVE_BIN_SIZE_PIPE_UVB;
00405 cpl_parameter_set_double(p,XSH_WAVE_BIN_SIZE_PIPE_UVB);
00406 }
00407
00408
00409 }
00410
00411 cleanup:
00412
00413 return cpl_error_get_code();
00414
00415 }
00416
00417
00418
00419
00427
00428
00429 static cpl_error_code
00430 xsh_params_bin_scale(cpl_frameset* raws,
00431 xsh_background_param* backg)
00432 {
00433
00434 cpl_frame* frame=NULL;
00435 const char* name=NULL;
00436 cpl_propertylist* plist=NULL;
00437 int binx=0;
00438 int biny=0;
00439 xsh_msg("Rescale relevant parameters to bin size");
00440 check(frame=cpl_frameset_get_first(raws));
00441 check(name=cpl_frame_get_filename(frame));
00442
00443 check(plist=cpl_propertylist_load(name,0));
00444 check(binx=xsh_pfits_get_binx(plist));
00445 check(biny=xsh_pfits_get_biny(plist));
00446 xsh_free_propertylist(&plist);
00447
00448 if(biny>1) {
00449
00450
00451
00452
00453
00454
00455 backg->radius_y=backg->radius_y/biny;
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
00483
00484 if(binx>1) {
00485
00486 backg->radius_x=backg->radius_x/binx;
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 }
00507
00508 cleanup:
00509 xsh_free_propertylist(&plist);
00510 return cpl_error_get_code();
00511
00512 }
00513
00514
00515
00516
00524
00525 static cpl_error_code
00526 xsh_respon_slit_offset( cpl_parameterlist* parameters,
00527 cpl_frameset* frameset)
00528 {
00529 const char* recipe_tags[2] = {XSH_STD_FLUX_SLIT_OFFSET, XSH_SKY_SLIT};
00530 int recipe_tags_size = 2;
00531
00532
00533 cpl_frameset* raws = NULL;
00534
00535 cpl_frameset * raw_object = NULL;
00536 cpl_frameset * raw_sky = NULL;
00537 cpl_frameset * calib = NULL;
00538 int nb_sub_frames = 0;
00539
00540
00541 cpl_frame* bpmap = NULL;
00542 cpl_frame* master_bias = NULL;
00543 cpl_frame* master_dark = NULL;
00544 cpl_frame* master_flat = NULL;
00545 cpl_frame* order_tab_edges = NULL;
00546 cpl_frame * wave_tab = NULL ;
00547 cpl_frame * model_config_frame = NULL ;
00548 cpl_frame * wavemap = NULL ;
00549 cpl_frame *disp_tab_frame = NULL;
00550 cpl_frame * spectral_format = NULL ;
00551
00552
00553 xsh_clipping_param* crh_clipping_par = NULL;
00554 xsh_background_param* backg_par = NULL;
00555 xsh_remove_crh_single_param * crh_single_par = NULL ;
00556 xsh_rectify_param * rectify_par = NULL ;
00557 xsh_localize_obj_param * loc_obj_par = NULL ;
00558
00559 double opt_kappa = -1. ;
00560 xsh_merge_param* merge_par = NULL;
00561 xsh_extract_param * extract_par = NULL ;
00562 xsh_combine_nod_param * combine_nod_param = NULL ;
00563
00564 xsh_instrument* instrument = NULL;
00565
00566
00567
00568 cpl_frame * loc_table_frame = NULL ;
00569 cpl_frame * clean_frame = NULL ;
00570 cpl_frameset * sub_frameset = NULL ;
00571 cpl_frameset * rmbkg_frameset = NULL ;
00572 cpl_frameset * clean_frameset = NULL ;
00573 cpl_frameset * rect_frameset = NULL ;
00574 cpl_frameset * rect2_frameset_tables = NULL ;
00576
00577 cpl_frame * res_1D_frame = NULL ;
00578 cpl_frame * res_2D_frame = NULL ;
00579 cpl_frame * ext_frame = NULL ;
00580 cpl_frame * ext_frame_eso = NULL ;
00581 cpl_frame * comb_frame = NULL ;
00582 cpl_frame * comb_frame_eso = NULL ;
00583
00584 char file_name[80];
00585 char arm_str[16] ;
00586 char file_tag[40];
00587
00588 int i ;
00589 cpl_frame* grid_back=NULL;
00590 cpl_frame* frame_backg=NULL;
00591 int do_flatfield=0;
00592 int gen_sky=0;
00593 char * prefix=NULL;
00594 char rec_prefix[80];
00595 char sky_prefix[80];
00596 char sky_tag[80];
00597 cpl_frame* avg_sky=NULL;
00598 cpl_frame* rec_sky=NULL;
00599 cpl_frame* rec_sky_eso=NULL;
00600 cpl_frame* mer_sky=NULL;
00601 cpl_frame * rectif_tab = NULL ;
00602 cpl_frame * sky_divided = NULL ;
00603 cpl_frameset* sky_bias=NULL;
00604 cpl_frameset* sky_dark=NULL;
00605
00606
00607 cpl_frame* std_flux_frame=NULL;
00608 double exptime=1.;
00609 cpl_frame* response_frame=NULL;
00610 cpl_frame* response_ord_frame=NULL;
00611
00612
00613 cpl_frame* frm_atmext=NULL;
00614 cpl_frame* frm_std_cat=NULL;
00615 cpl_frame* eff_frame=NULL;
00616 cpl_frame* rect_eff_frame=NULL;
00617 cpl_frame* ext_eff_frame=NULL;
00618 cpl_frame* ext_eff_frame2=NULL;
00619 cpl_frame* frm_eff=NULL;
00620
00621 cpl_frame * rect_eff_frame_eso = NULL ;
00622 cpl_frame * rect_eff_frame_tab = NULL ;
00623 cpl_frame * div_clean_frame = NULL ;
00624 cpl_frame * fluxcal_rect_1D_frame = NULL ;
00625 cpl_frame * fluxcal_rect_2D_frame = NULL ;
00626 cpl_frame * fluxcal_1D_frame = NULL ;
00627 cpl_frame * fluxcal_2D_frame = NULL ;
00628 cpl_frame * nrm_1D_frame = NULL ;
00629 cpl_frame * nrm_2D_frame = NULL ;
00630 cpl_frame* high_abs_win=NULL;
00631 int pre_overscan_corr=0;
00632
00633
00634
00635 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00636 recipe_tags, recipe_tags_size,
00637 RECIPE_ID, XSH_BINARY_VERSION,
00638 xsh_respon_slit_offset_description_short));
00639
00640 check(xsh_ensure_raws_input_offset_recipe_is_proper(raws,instrument));
00641 assure( instrument->mode == XSH_MODE_SLIT, CPL_ERROR_ILLEGAL_INPUT,
00642 "Instrument NOT in Slit Mode" ) ;
00643
00644
00645
00646
00647 check(xsh_slit_offset_get_calibs(calib,instrument,&bpmap,&master_bias,
00648 &master_dark,&order_tab_edges,
00649 &model_config_frame,&wave_tab,&master_flat,
00650 &wavemap,&spectral_format));
00651
00652
00653 frm_atmext=xsh_find_frame_with_tag(calib,XSH_ATMOS_EXT,instrument);
00654 if(frm_atmext==NULL) {
00655 xsh_msg_error("Provide atmospheric extinction frame");
00656 return CPL_ERROR_DATA_NOT_FOUND;
00657 }
00658
00659 frm_std_cat=xsh_find_frame_with_tag(calib,XSH_FLUX_STD_CAT,instrument);
00660 if(frm_std_cat==NULL) {
00661 xsh_msg_error("Provide std star catalog frame");
00662 return CPL_ERROR_DATA_NOT_FOUND;
00663 }
00664 high_abs_win=xsh_find_frame_with_tag(calib,XSH_HIGH_ABS_WIN,instrument);
00665
00666
00667 if(NULL == (disp_tab_frame = xsh_find_disp_tab( calib, instrument))) {
00668 xsh_msg("To compute efficiency, you must give a DISP_TAB_ARM input");
00669 }
00670
00671
00672
00673
00674
00675 check(xsh_slit_offset_get_params(parameters,RECIPE_ID,
00676 &crh_clipping_par,&backg_par,
00677 &loc_obj_par,&rectify_par,&crh_single_par,
00678 &opt_kappa,&merge_par,&extract_par,
00679 &combine_nod_param,&do_flatfield,&gen_sky));
00680
00681 check(xsh_params_set_defaults(parameters,instrument,rectify_par));
00682
00683 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00684 check(xsh_params_bin_scale(raws,backg_par));
00685 }
00686 check(xsh_params_monitor(backg_par,rectify_par,loc_obj_par));
00687
00688
00689
00690
00691
00692 check(xsh_prepare(raws, bpmap, master_bias, XSH_STD_FLUX_SLIT_OFFSET,
00693 instrument,pre_overscan_corr));
00694
00695
00696 check( nb_sub_frames = xsh_dfs_split_offset( raws, XSH_STD_FLUX_SLIT_OFFSET,
00697 XSH_SKY_SLIT, &raw_object,
00698 &raw_sky ) ) ;
00699
00700 prefix=xsh_set_recipe_file_prefix(raw_object,"xsh_respon_slit_offset");
00701 check(strcpy(rec_prefix,prefix));
00702 XSH_FREE(prefix);
00703
00704 if(gen_sky) {
00705 if(strstr(rec_prefix,"SCI") != NULL) {
00706 strcpy(sky_prefix,"SKY_SLIT");
00707 } else if(strstr(rec_prefix,"TELL") != NULL) {
00708 strcpy(sky_prefix,"TELL_SLIT_SKY");
00709 } else if(strstr(rec_prefix,"FLUX") != NULL) {
00710 strcpy(sky_prefix,"FLUX_SLIT_SKY");
00711 } else {
00712 strcpy(sky_prefix,"CAL_SLIT_SKY");
00713 }
00714 }
00715 if(nb_sub_frames==0) {
00716 xsh_msg_error("nb_sub_frames=%d something wrong check your input raws",
00717 nb_sub_frames);
00718 goto cleanup;
00719
00720 }
00721
00722 check( sub_frameset = xsh_subtract_sky_offset( raw_object, raw_sky,
00723 nb_sub_frames,
00724 instrument )) ;
00725
00726
00727 xsh_msg_dbg_low( "Nb of Subtracted Frames: %d",
00728 cpl_frameset_get_size( sub_frameset ) ) ;
00729
00730 check( clean_frameset = cpl_frameset_new() ) ;
00731 check( rmbkg_frameset = cpl_frameset_new() ) ;
00732
00733 sprintf( arm_str, "%s", xsh_instrument_arm_tostring(instrument) ) ;
00734
00735 if(gen_sky) {
00736
00737 if(master_bias!= NULL && pre_overscan_corr ==0 ) {
00738 check(sky_bias=xsh_pre_frameset_subtract_frame(raw_sky,master_bias,"MBIAS_",instrument));
00739 } else {
00740 sky_bias=cpl_frameset_duplicate(raw_sky);
00741 }
00742 if(master_dark!= NULL) {
00743 check(sky_dark=xsh_pre_frameset_subtract_frame(sky_bias,master_dark,"MDARK",instrument));
00744 } else {
00745 sky_dark=cpl_frameset_duplicate(sky_bias);
00746 }
00747
00748 sprintf(sky_tag,"%s%s%s",rec_prefix,"_SKY_",arm_str);
00749 check(avg_sky=xsh_frameset_average_pre(sky_dark,instrument,sky_tag));
00750 if(do_flatfield==1) {
00751 sprintf(sky_tag,"%s%s%s",rec_prefix,"_FF_SKY_",arm_str);
00752 check( sky_divided = xsh_divide_flat( avg_sky,master_flat, sky_tag,
00753 instrument ) ) ;
00754 } else {
00755 sky_divided=cpl_frame_duplicate(avg_sky);
00756 }
00757 }
00758
00759
00760
00761
00762 {
00763 xsh_pre * xpre = NULL ;
00764 std_flux_frame= cpl_frameset_get_frame( sub_frameset, 0 ) ;
00765 check( xpre = xsh_pre_load( std_flux_frame, instrument ) ) ;
00766 exptime = xpre->exptime ;
00767 xsh_msg_dbg_medium( "EXPTIME: %lf", exptime ) ;
00768
00769 xsh_pre_free( &xpre ) ;
00770 }
00771
00772 for( i = 0 ; i<nb_sub_frames ; i++ ) {
00773 cpl_frame * a_b = NULL ;
00774 cpl_frame * rm_bkg = NULL ;
00775 char str[16] ;
00776
00777 sprintf( str, "_%d", i ) ;
00778 a_b = cpl_frameset_get_frame( sub_frameset, i ) ;
00779
00780
00781 xsh_msg("Subtract inter-order background [%d]", i );
00782 sprintf(file_name,"%s%s", rec_prefix, str) ;
00783 check(rm_bkg = xsh_subtract_background( a_b,
00784 order_tab_edges,
00785 backg_par, instrument,
00786 file_name,&grid_back,
00787 &frame_backg));
00788 check( cpl_frameset_insert( rmbkg_frameset, rm_bkg ) ) ;
00789 }
00790
00791 xsh_msg_dbg_low( "Nb of Frames Bkg Subtracted: %d",
00792 cpl_frameset_get_size( rmbkg_frameset ) ) ;
00793
00794 if ( nb_sub_frames < 3 && crh_single_par->nb_iter > 0 ) {
00795 xsh_msg( "Less than 3 frames AND removecrhsingle_niter > 0" ) ;
00796 for ( i = 0 ; i < nb_sub_frames ; i++ ) {
00797 cpl_frame * divided = NULL ;
00798 cpl_frame * rm_crh = NULL ;
00799 cpl_frame * a_b = NULL ;
00800 char str[16] ;
00801
00802 check( a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ) ;
00803 sprintf( str, "%d", i ) ;
00804 sprintf(file_tag,"NO_CRH_SLIT_OFFSET_%s%s",arm_str,str) ;
00805 xsh_msg( "Remove crh (single frame)" ) ;
00806 check( rm_crh = xsh_remove_crh_single( a_b, wavemap,instrument,
00807 crh_single_par,
00808 file_tag ) ) ;
00809 if(do_flatfield==1) {
00810 xsh_msg( "Calling xsh_divide_flat" ) ;
00811 sprintf(file_tag,"FF_SLIT_OFFSET_%s%s", arm_str,str) ;
00812 check( divided = xsh_divide_flat( rm_crh,master_flat, file_tag,
00813 instrument ) ) ;
00814 } else {
00815 divided=cpl_frame_duplicate(rm_crh);
00816 }
00817 xsh_free_frame(&rm_crh);
00818 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00819 }
00820 }
00821 else for( i = 0 ; i < nb_sub_frames ; i++ ) {
00822 cpl_frame * divided = NULL ;
00823 cpl_frame * a_b = NULL ;
00824 char str[16] ;
00825
00826
00827 a_b = cpl_frameset_get_frame( rmbkg_frameset, i ) ;
00828 sprintf( str, "%d", i ) ;
00829
00830 if(do_flatfield==1) {
00831
00832 xsh_msg( "Calling xsh_divide_flat" ) ;
00833
00834 sprintf(file_tag,"FF_SLIT_OFFSET_%s%s", arm_str,str) ;
00835 check( divided = xsh_divide_flat( a_b,master_flat, file_tag,
00836 instrument ) ) ;
00837 } else {
00838 divided=cpl_frame_duplicate(a_b);
00839 }
00840 check( cpl_frameset_insert( clean_frameset, divided ) ) ;
00841 }
00842
00843 {
00844 int clean_size ;
00845
00846 check( clean_size = cpl_frameset_get_size( clean_frameset ) ) ;
00847 xsh_msg( "Nb of clean Frames: %d", clean_size ) ;
00848 }
00849
00850 div_clean_frame = cpl_frameset_get_frame(clean_frameset,0);
00851
00852 check( rect_frameset = cpl_frameset_new() ) ;
00853 for( i = 0 ; i < nb_sub_frames ; i++ ) {
00854 cpl_frame * rectif = NULL ;
00855 cpl_frame * rectif_eso = NULL ;
00856 cpl_frame * clean = NULL ;
00857 char str[16] ;
00858
00859 sprintf( str, "%d", i ) ;
00860 check( clean = cpl_frameset_get_frame( clean_frameset, i ) ) ;
00861 xsh_msg( "Rectifying Frame '%s'", cpl_frame_get_filename( clean ) ) ;
00862 sprintf(file_name,"RECTIFIED_SLIT_OFFSET_%s_%s.fits",arm_str,str) ;
00863 check( rectif = xsh_rectify( clean, order_tab_edges, wave_tab,
00864 model_config_frame, instrument,
00865 rectify_par, spectral_format,
00866 disp_tab_frame,
00867 file_name,&rectif_eso,&rectif_tab,
00868 rec_prefix ) ) ;
00869 check( cpl_frameset_insert( rect_frameset, rectif ) ) ;
00870 check( cpl_frameset_insert( rect_frameset, rectif_eso ) ) ;
00871 xsh_free_frame(&rectif_tab);
00872 }
00873
00874
00875
00876 if(gen_sky) {
00877 xsh_msg("rectify sky frame");
00878 sprintf(file_name,"%s_RECTIFIED_SKY_%s.fits",sky_prefix,arm_str) ;
00879 check( rec_sky = xsh_rectify(sky_divided, order_tab_edges, wave_tab,
00880 model_config_frame, instrument,
00881 rectify_par, spectral_format,
00882 disp_tab_frame,
00883 file_name,&rec_sky_eso,&rectif_tab,
00884 sky_prefix ) ) ;
00885
00886
00887 xsh_msg("Merge 2D sky frame");
00888 check( mer_sky = xsh_merge_ord( rec_sky, instrument,
00889 merge_par,sky_prefix ));
00890
00891
00892 }
00893
00894
00895 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_ORDER2D, arm_str) ;
00896 check(rect2_frameset_tables=xsh_frameset_ext_table_frames(rect_frameset));
00897 check( comb_frame = xsh_combine_nod(rect2_frameset_tables, combine_nod_param,
00898 file_tag,instrument,&comb_frame_eso));
00899 xsh_free_frameset(&rect2_frameset_tables);
00900
00901 if ( extract_par->method == LOCALIZATION_METHOD ) {
00902 xsh_msg( "Localize before extraction" ) ;
00903 sprintf(file_name,"LOCALIZE_%s_ALL.fits",arm_str) ;
00904 check( loc_table_frame = xsh_localize_obj( comb_frame, NULL,instrument,
00905 loc_obj_par, NULL,
00906 file_name) ) ;
00907 }
00908
00909 xsh_msg( "Calling xsh_extract" ) ;
00910 check(ext_frame = xsh_extract(comb_frame, loc_table_frame,
00911 instrument, extract_par,&ext_frame_eso,
00912 rec_prefix )) ;
00913 xsh_msg( "Calling xsh_merge_ord with 1D frame" ) ;
00914 check( res_1D_frame = xsh_merge_ord( ext_frame, instrument,
00915 merge_par,rec_prefix ));
00916
00917
00918
00919
00920
00921 xsh_msg( "Calling xsh_merge_ord with 2D frame" ) ;
00922 check( res_2D_frame = xsh_merge_ord( comb_frame, instrument, merge_par,
00923 rec_prefix ) ) ;
00924
00925
00926
00927 if(frm_std_cat!=NULL && frm_atmext!=NULL) {
00928 xsh_msg( "Calling xsh_compute_response" ) ;
00929 if( (response_ord_frame = xsh_compute_response_ord( ext_frame_eso,
00930 frm_std_cat,
00931 frm_atmext,
00932 high_abs_win,
00933 instrument,
00934 exptime )) == NULL) {
00935 cpl_error_reset();
00936 } else {
00937 response_frame = xsh_compute_response( res_1D_frame,
00938 frm_std_cat,
00939 frm_atmext,
00940 high_abs_win,
00941 instrument,
00942 exptime );
00943
00944 check(xsh_frame_table_monitor_flux_qc(response_frame,"LAMBDA","FLUX",
00945 "RESP",instrument));
00946 }
00947
00948 }
00949
00950
00951 if(response_frame != NULL) {
00952
00953 check(xsh_flux_calibrate(comb_frame_eso,ext_frame_eso,frm_atmext,
00954 response_frame,merge_par,instrument,rec_prefix,
00955 &fluxcal_rect_2D_frame,&fluxcal_rect_1D_frame,
00956 &fluxcal_2D_frame,&fluxcal_1D_frame));
00957
00958 }
00959
00960
00961 if(response_ord_frame != NULL && disp_tab_frame != NULL) {
00962 int conserve_flux=rectify_par->conserve_flux;
00963 xsh_msg( "Calling xsh_multiply_flat" ) ;
00964
00965 sprintf(file_tag,"SLIT_STARE_NOCRH_NOT_FF_%s",arm_str) ;
00966 if(do_flatfield) {
00967 check( eff_frame = xsh_multiply_flat( div_clean_frame, master_flat,
00968 file_tag, instrument ) ) ;
00969 } else {
00970 check(eff_frame=cpl_frame_duplicate(div_clean_frame));
00971 }
00972 sprintf(file_name,"%s_EFF_%s_%s.fits",rec_prefix,XSH_ORDER2D,
00973 xsh_instrument_arm_tostring(instrument));
00974
00975
00976 rectify_par->conserve_flux=1;
00977 check( rect_eff_frame = xsh_rectify( eff_frame,
00978 order_tab_edges,
00979 wave_tab,
00980 model_config_frame,
00981 instrument,
00982 rectify_par,
00983 spectral_format,
00984 disp_tab_frame,
00985 file_name,
00986 &rect_eff_frame_eso,
00987 &rect_eff_frame_tab,
00988 rec_prefix ));
00989
00990
00991
00992
00993
00994
00995
00996 xsh_msg( "Calling xsh_extract" ) ;
00997 check( ext_eff_frame = xsh_extract(rect_eff_frame,loc_table_frame,
00998 instrument,extract_par,
00999 &ext_eff_frame2,rec_prefix)) ;
01000
01001
01002
01003 check(frm_eff=xsh_efficiency_compute(ext_eff_frame2,frm_std_cat,
01004 frm_atmext,instrument));
01005 check(xsh_frame_table_monitor_flux_qc(frm_eff,"WAVELENGTH","EFF","EFF",
01006 instrument));
01007
01008 rectify_par->conserve_flux=conserve_flux;
01009
01010 }
01011
01012
01013
01014 xsh_msg( "Saving products" ) ;
01015 if(response_frame!=NULL) {
01016 check( xsh_add_product_table_multi(response_ord_frame, frameset, parameters,
01017 RECIPE_ID, instrument));
01018
01019 check( xsh_add_product_table( response_frame, frameset, parameters,
01020 RECIPE_ID, instrument));
01021 }
01022
01023
01024
01025
01026
01027
01028
01029
01030 check( xsh_add_product_image( comb_frame_eso, frameset,
01031 parameters, RECIPE_ID, instrument,
01032 cpl_frame_get_tag(comb_frame_eso)));
01033
01034
01035
01036
01037
01038
01039 check( xsh_add_product_image( ext_frame_eso, frameset,
01040 parameters, RECIPE_ID, instrument,
01041 cpl_frame_get_tag(ext_frame_eso)));
01042
01043
01044 check( xsh_add_product_spectrum( res_2D_frame, frameset, parameters,
01045 RECIPE_ID, instrument));
01046
01047 check(xsh_monitor_spectrum1D_flux(res_1D_frame,instrument));
01048 check( xsh_add_product_spectrum( res_1D_frame, frameset, parameters,
01049 RECIPE_ID, instrument));
01050
01051
01052 if(response_frame != NULL) {
01053 check( xsh_add_product_image(fluxcal_rect_2D_frame,frameset,parameters,
01054 RECIPE_ID, instrument,
01055 cpl_frame_get_filename(fluxcal_rect_2D_frame)));
01056 check( xsh_add_product_image(fluxcal_rect_1D_frame,frameset,parameters,
01057 RECIPE_ID, instrument,
01058 cpl_frame_get_filename(fluxcal_rect_1D_frame)));
01059 check( xsh_add_product_spectrum( fluxcal_2D_frame, frameset, parameters,
01060 RECIPE_ID, instrument));
01061 check( xsh_add_product_spectrum( fluxcal_1D_frame, frameset, parameters,
01062 RECIPE_ID, instrument));
01063 }
01064
01065
01066 if(gen_sky) {
01067 check( xsh_add_product_image( avg_sky, frameset, parameters,
01068 RECIPE_ID, instrument,
01069 cpl_frame_get_tag(avg_sky)));
01070
01071
01072
01073 check( xsh_add_product_image( rec_sky_eso, frameset, parameters,
01074 RECIPE_ID, instrument,
01075 cpl_frame_get_tag(rec_sky_eso)));
01076
01077
01078 check( xsh_add_product_image( mer_sky, frameset, parameters,
01079 RECIPE_ID, instrument,
01080 cpl_frame_get_tag(mer_sky)));
01081
01082 }
01083
01084
01085
01086
01087 if(frm_eff!=NULL) {
01088
01089 check(xsh_add_product_table_multi(frm_eff, frameset,parameters,
01090 RECIPE_ID, instrument));
01091 }
01092 cleanup:
01093
01094 xsh_end( RECIPE_ID, frameset, parameters );
01095 XSH_FREE(crh_clipping_par);
01096 XSH_FREE( backg_par ) ;
01097 XSH_FREE( crh_single_par ) ;
01098 XSH_FREE( rectify_par ) ;
01099 XSH_FREE( loc_obj_par ) ;
01100 XSH_FREE(merge_par);
01101 XSH_FREE(extract_par);
01102
01103
01104 XSH_FREE(combine_nod_param);
01105 xsh_instrument_free(&instrument );
01106 xsh_free_frameset(&rect2_frameset_tables);
01107 xsh_free_frameset(&raws);
01108
01109
01110 xsh_free_frameset( &raw_object);
01111 xsh_free_frameset( &raw_sky);
01112
01113 xsh_free_frameset(&calib);
01114 xsh_free_frameset(&sub_frameset);
01115 xsh_free_frameset(&clean_frameset);
01116 xsh_free_frameset(&rmbkg_frameset);
01117 xsh_free_frameset(&rect_frameset);
01118
01119 xsh_free_frameset(&sky_bias);
01120 xsh_free_frameset(&sky_dark);
01121
01122 xsh_free_frame(&loc_table_frame) ;
01123 xsh_free_frame(&clean_frame) ;
01124 xsh_free_frame(&res_1D_frame) ;
01125 xsh_free_frame(&res_2D_frame) ;
01126 xsh_free_frame(&fluxcal_rect_1D_frame) ;
01127 xsh_free_frame(&fluxcal_rect_2D_frame) ;
01128 xsh_free_frame(&fluxcal_1D_frame) ;
01129 xsh_free_frame(&fluxcal_2D_frame) ;
01130 xsh_free_frame(&nrm_1D_frame) ;
01131 xsh_free_frame(&nrm_2D_frame) ;
01132 xsh_free_frame( &comb_frame ) ;
01133 xsh_free_frame( &comb_frame_eso ) ;
01134 xsh_free_frame( &ext_frame ) ;
01135 xsh_free_frame( &ext_frame_eso ) ;
01136 xsh_free_frame( &eff_frame ) ;
01137 xsh_free_frame( &rect_eff_frame ) ;
01138 xsh_free_frame( &rect_eff_frame_eso ) ;
01139 xsh_free_frame( &rect_eff_frame_tab ) ;
01140
01141
01142 xsh_free_frame( &avg_sky ) ;
01143 xsh_free_frame( &sky_divided ) ;
01144 xsh_free_frame( &rec_sky ) ;
01145 xsh_free_frame( &rec_sky_eso ) ;
01146 xsh_free_frame( &mer_sky ) ;
01147 xsh_free_frame( &ext_eff_frame ) ;
01148 xsh_free_frame( &ext_eff_frame2 ) ;
01149
01150 xsh_free_frame( &frame_backg ) ;
01151 xsh_free_frame( &grid_back ) ;
01152 xsh_free_frame( &frm_eff ) ;
01153 xsh_free_frame( &response_frame ) ;
01154 xsh_free_frame( &response_ord_frame ) ;
01155
01156 return CPL_ERROR_NONE;
01157 }
01158