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