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_drl_check.h>
00058 #include <xsh_pfits.h>
00059 #include <xsh_data_spectrum1D.h>
00060 #include <xsh_model_arm_constants.h>
00061
00062
00063 #include <xsh_drl.h>
00064
00065 #include <cpl.h>
00066
00067
00068
00069
00070
00071 #define RECIPE_ID "xsh_respon_slit_offset"
00072 #define RECIPE_AUTHOR "D. Bramich, A.Modigliani"
00073 #define RECIPE_CONTACT "amodigli@eso.org"
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 static int xsh_respon_slit_offset_create( cpl_plugin *);
00084 static int xsh_respon_slit_offset_exec( cpl_plugin *);
00085 static int xsh_respon_slit_offset_destroy( cpl_plugin *);
00086
00087
00088 static cpl_error_code xsh_respon_slit_offset( cpl_parameterlist *, cpl_frameset *);
00089
00090
00091
00092
00093 static char xsh_respon_slit_offset_description_short[] =
00094 "Compute the response function in SLIT and on/off mode";
00095
00096 static char xsh_respon_slit_offset_description[] =
00097 "This recipe reduces science exposure in SLIT configuration and on/off mode\n\
00098 Input Frames : \n\
00099 - A set of n Science frames ( n even ), \
00100 Tag = STD_FLUX_SLIT_OFFSET_arm, SKY_SLIT_arm\n\
00101 - Spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00102 - A master flat frame (Tag = MASTER_FLAT_SLIT_arm)\n\
00103 - An order table frame(Tag = ORDER_TABLE_EDGES_SLIT_arm)\n\
00104 - A wavelength calibration solution frame(Tag = WAVE_TAB_2D_arm)\n\
00105 - [OPTIONAL] Table with dispersion coefficients (Tag = DISP_TAB_arm)\n\
00106 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
00107 - A standard star fluxes catalog (Tag = STD_STAR_CATALOG_arm Type = FLX)\n\
00108 - An atmospheric extinction table (Tag = ATMOS_EXT_arm)\n\
00109 - [OPTIONAL] An master response curve table (Tag = MRESPONSE_MERGE1D_SLIT_arm)\n\
00110 if provided this is the one used to flux calibrate the spectra\n\
00111 Products : \n\
00112 Products : \n\
00113 - [If STD is in catal] The response ord-by-ord function (Tag = RESPONSE_ORDER1D_SLIT_arm)\n\
00114 - [If STD is in catal] The response merged function (Tag = RESPONSE_MERGE1D_SLIT_arm)\n\
00115 - PREFIX_ORDER2D_arm extracted spectrum, order-by-order, 2D\n\
00116 - PREFIX_ORDER1D_arm extracted spectrum, order-by-order, 1D\n\
00117 - PREFIX_MERGE2D_arm merged spectrum, 2D\n\
00118 - PREFIX_MERGE1D_arm merged spectrum, 1D\n\
00119 - [If STD is in catal] Flux calibrated order-by-order 2D spectrum (Tag = PREFIX_FLUX_ORDER2D_arm)\n\
00120 - [If STD is in catal] Flux calibrated order-by-order 1D spectrum (Tag = PREFIX_FLUX_ORDER1D_arm)\n\
00121 - [If STD is in catal] Flux calibrated merged 2D spectrum (Tag = PREFIX_FLUX_MERGE2D_arm)\n\
00122 - [If STD is in catal] Flux calibrated merged 1D spectrum (Tag = PREFIX_FLUX_MERGE1D_arm)\n\
00123 - PREFIX_SKY_arm, 2D sky frame\n\
00124 - SKY_SLIT_ORDER2D_arm, 2D sky image (order-by-order)\n\
00125 - SKY_SLIT_MERGE2D_arm, 2D sky image (merged)\n\
00126 - [If STD is in catal] The efficiency (Tag = EFFICIENCY_arm)\n\
00127 - PREFIX_WAVE_MAP_arm, wave map image\n\
00128 - PREFIX_SLIT_MAP_arm, slit map image\n\
00129 - where PREFIX is SCI, FLUX, TELL if input raw DPR.TYPE contains OBJECT or FLUX or TELLURIC";
00130
00131
00132
00133
00134
00143
00144
00145 int cpl_plugin_get_info(cpl_pluginlist *list) {
00146 cpl_recipe *recipe = NULL;
00147 cpl_plugin *plugin = NULL;
00148
00149 recipe = cpl_calloc(1, sizeof(*recipe));
00150 if ( recipe == NULL ){
00151 return -1;
00152 }
00153
00154 plugin = &recipe->interface ;
00155
00156 cpl_plugin_init(plugin,
00157 CPL_PLUGIN_API,
00158 XSH_BINARY_VERSION,
00159 CPL_PLUGIN_TYPE_RECIPE,
00160 RECIPE_ID,
00161 xsh_respon_slit_offset_description_short,
00162 xsh_respon_slit_offset_description,
00163 RECIPE_AUTHOR,
00164 RECIPE_CONTACT,
00165 xsh_get_license(),
00166 xsh_respon_slit_offset_create,
00167 xsh_respon_slit_offset_exec,
00168 xsh_respon_slit_offset_destroy);
00169
00170 cpl_pluginlist_append(list, plugin);
00171
00172 return (cpl_error_get_code() != CPL_ERROR_NONE);
00173 }
00174
00175
00185
00186
00187 static int xsh_respon_slit_offset_create(cpl_plugin *plugin){
00188 cpl_recipe *recipe = NULL;
00189
00190
00191 xsh_remove_crh_single_param crh_single = { 0.1, 20, 2.0, 4} ;
00192 xsh_rectify_param rectify = { "tanh",
00193 CPL_KERNEL_DEFAULT,
00194 2,
00195 -1.0,
00196 -1.0,
00197 1,0,0.};
00198
00199 xsh_localize_obj_param loc_obj =
00200 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3,FALSE};
00201
00202 xsh_interpolate_bp_param ipol_par = { 30};
00203 xsh_combine_nod_param nod_param = { 5, TRUE, 5, 2, 0.1, "throwlist.asc",TRUE } ;
00204
00205
00206 xsh_init();
00207
00208
00209 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00210
00211 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00212 CPL_ERROR_TYPE_MISMATCH,
00213 "Plugin is not a recipe");
00214
00215 recipe = (cpl_recipe *)plugin;
00216
00217
00218 recipe->parameters = cpl_parameterlist_new();
00219 assure( recipe->parameters != NULL,
00220 CPL_ERROR_ILLEGAL_OUTPUT,
00221 "Memory allocation failed!");
00222
00223
00224 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00225 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00226 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00227
00228
00229 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00230 crh_single )) ;
00231
00232 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00233 rectify )) ;
00234
00235 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00236 loc_obj )) ;
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 check(xsh_parameters_interpolate_bp_create(RECIPE_ID,
00247 recipe->parameters,ipol_par)) ;
00248
00249 check(xsh_parameters_combine_nod_create(RECIPE_ID,
00250 recipe->parameters,
00251 nod_param )) ;
00252
00253
00254
00255
00256
00257
00258 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00259 "gen-sky", TRUE,
00260 "if TRUE a 2D sky frame, a 2D rectified, a 2D merged sky are generated"));
00261
00262 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00263 "correct-tellurics", TRUE,
00264 "TRUE if during response computation we apply telluric correction"));
00265
00266
00267 cleanup:
00268 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00269 xsh_error_dump(CPL_MSG_ERROR);
00270 return 1;
00271 }
00272 else {
00273 return 0;
00274 }
00275 }
00276
00277
00283
00284
00285 static int xsh_respon_slit_offset_exec(cpl_plugin *plugin) {
00286 cpl_recipe *recipe = NULL;
00287
00288
00289
00290 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00291
00292
00293 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00294 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00295
00296 recipe = (cpl_recipe *)plugin;
00297
00298
00299 xsh_respon_slit_offset(recipe->parameters, recipe->frames);
00300
00301 cleanup:
00302 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00303 xsh_error_dump(CPL_MSG_ERROR);
00304 xsh_error_reset();
00305 return 1;
00306 }
00307 else {
00308 return 0;
00309 }
00310 }
00311
00312
00318
00319 static int xsh_respon_slit_offset_destroy(cpl_plugin *plugin)
00320 {
00321 cpl_recipe *recipe = NULL;
00322
00323
00324 xsh_error_reset();
00325
00326 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00327
00328
00329 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00330 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00331
00332 recipe = (cpl_recipe *)plugin;
00333
00334 xsh_free_parameterlist(&recipe->parameters);
00335
00336 cleanup:
00337 if (cpl_error_get_code() != CPL_ERROR_NONE)
00338 {
00339 return 1;
00340 }
00341 else
00342 {
00343 return 0;
00344 }
00345 }
00346
00347
00348 static cpl_error_code
00349 xsh_params_monitor(xsh_rectify_param * rectify_par,
00350 xsh_localize_obj_param * loc_obj_par)
00351 {
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
00372
00373 static cpl_error_code
00374 xsh_respon_slit_offset( cpl_parameterlist* parameters,
00375 cpl_frameset* frameset)
00376 {
00377 const char* recipe_tags[2] = {XSH_STD_FLUX_SLIT_OFFSET, XSH_SKY_SLIT};
00378 int recipe_tags_size = 2;
00379
00380
00381 cpl_frameset* raws = NULL;
00382
00383 cpl_frameset * raw_object = NULL;
00384 cpl_frameset * raw_sky = NULL;
00385 cpl_frameset * calib = NULL;
00386 int nb_sub_frames = 0;
00387
00388
00389 cpl_frame* bpmap = NULL;
00390 cpl_frame* master_bias = NULL;
00391 cpl_frame* master_dark = NULL;
00392 cpl_frame* master_flat = NULL;
00393 cpl_frame* order_tab_edges = NULL;
00394 cpl_frame * wave_tab = NULL ;
00395 cpl_frame * model_config_frame = NULL ;
00396 cpl_frame * slitmap = NULL ;
00397 cpl_frame *disp_tab_frame = NULL;
00398 cpl_frame * spectral_format = NULL ;
00399
00400
00401 xsh_remove_crh_single_param * crh_single_par = NULL ;
00402 xsh_rectify_param * rectify_par = NULL ;
00403 xsh_localize_obj_param * loc_obj_par = NULL ;
00404
00405 xsh_extract_param * extract_par = NULL ;
00406 xsh_combine_nod_param * combine_nod_param = NULL ;
00407
00408 xsh_instrument* instrument = NULL;
00409
00410
00411
00412 cpl_frame * loc_table_frame = NULL ;
00413 cpl_frame * clean_frame = NULL ;
00414 cpl_frameset * sub_frameset = NULL ;
00415 cpl_frameset * clean_frameset = NULL ;
00416 cpl_frameset * rect_frameset = NULL ;
00417 cpl_frameset * rect2_frameset_tables = NULL ;
00419
00420 cpl_frame * res_1D_frame = NULL ;
00421 cpl_frame * res_2D_frame = NULL ;
00422 cpl_frame * ext_frame = NULL ;
00423 cpl_frame * ext_frame_eso = NULL ;
00424 cpl_frame * comb_frame = NULL ;
00425 cpl_frame * comb_frame_eso = NULL ;
00426
00427 char file_name[256];
00428 char arm_str[16] ;
00429 char file_tag[40];
00430
00431 int i ;
00432
00433 int do_flatfield=1;
00434 int gen_sky=0;
00435 char * prefix=NULL;
00436 char rec_prefix[256];
00437 char sky_prefix[256];
00438 char sky_tag[256];
00439 cpl_frame* avg_sky=NULL;
00440 cpl_frame* rec_sky=NULL;
00441 cpl_frame* rec_sky_eso=NULL;
00442 cpl_frame* mer_sky=NULL;
00443 cpl_frame * rectif_tab = NULL ;
00444 cpl_frame * sky_divided = NULL ;
00445 cpl_frameset* sky_bias=NULL;
00446 cpl_frameset* sky_dark=NULL;
00447 cpl_frame * wavemap = NULL ;
00448
00449
00450 cpl_frame* std_flux_frame=NULL;
00451 double exptime=1.;
00452 cpl_frame* response_frame=NULL;
00453 cpl_frame* mresponse_frame=NULL;
00454 cpl_frame* response_ord_frame=NULL;
00455
00456
00457 cpl_frame* frm_atmext=NULL;
00458 cpl_frame* frm_std_cat=NULL;
00459 cpl_frame* eff_frame=NULL;
00460 cpl_frame* rect_eff_frame=NULL;
00461 cpl_frame* ext_eff_frame=NULL;
00462 cpl_frame* ext_eff_frame2=NULL;
00463 cpl_frame* frm_eff=NULL;
00464
00465 cpl_frame * rect_eff_frame_eso = NULL ;
00466 cpl_frame * rect_eff_frame_tab = NULL ;
00467 cpl_frame * div_clean_frame = NULL ;
00468 cpl_frame * fluxcal_rect_1D_frame = NULL ;
00469 cpl_frame * fluxcal_rect_2D_frame = NULL ;
00470 cpl_frame * fluxcal_1D_frame = NULL ;
00471 cpl_frame * fluxcal_2D_frame = NULL ;
00472 cpl_frame * nrm_1D_frame = NULL ;
00473 cpl_frame * nrm_2D_frame = NULL ;
00474 cpl_frame* high_abs_win=NULL;
00475 int pre_overscan_corr=0;
00476 int merge_par=0;
00477 xsh_interpolate_bp_param *ipol_bp=NULL;
00478 cpl_frame* tell_mod_cat=NULL;
00479 cpl_frameset* crh_clean_obj = NULL;
00480 cpl_frameset* crh_clean_sky = NULL;
00481
00482 int do_computemap=1;
00483 int use_model=0;
00484 int corr_tell=0;
00485
00486
00487
00488
00489 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00490 recipe_tags, recipe_tags_size,
00491 RECIPE_ID, XSH_BINARY_VERSION,
00492 xsh_respon_slit_offset_description_short));
00493
00494 check(xsh_ensure_raws_input_offset_recipe_is_proper(&raws,instrument));
00495 assure( instrument->mode == XSH_MODE_SLIT, CPL_ERROR_ILLEGAL_INPUT,
00496 "Instrument NOT in Slit Mode" ) ;
00497
00498 check(xsh_recipe_params_check(parameters,instrument,RECIPE_ID));
00499 if(instrument->arm == XSH_ARM_NIR) {
00500 xsh_calib_nir_corr_if_JH(calib,instrument,RECIPE_ID);
00501 xsh_instrument_nir_corr_if_JH(raws,instrument);
00502 xsh_calib_nir_respon_corr_if_JH(calib,instrument);
00503 }
00504
00505
00506
00507
00508
00509
00510 check(xsh_prepare(raws, bpmap, master_bias, XSH_STD_FLUX_SLIT_OFFSET,
00511 instrument,pre_overscan_corr,CPL_TRUE));
00512
00513
00514
00515
00516
00517
00518
00519
00520 check(xsh_frameset_uniform_bin(&raws, &calib,instrument));
00521
00522
00523
00524
00525
00526 check(xsh_slit_offset_get_calibs(calib,instrument,&bpmap,&master_bias,
00527 &master_dark,&order_tab_edges,
00528 &model_config_frame,&wave_tab,&master_flat,
00529 &wavemap,&slitmap,&spectral_format,RECIPE_ID));
00530
00531
00532
00533 if((mresponse_frame=xsh_find_frame_with_tag(calib,XSH_MRESPONSE_MERGE1D_SLIT,
00534 instrument)) != NULL ) {
00535 xsh_msg("Using the master response curve to flux calibrate the spectrum");
00536 }
00537
00538
00539 frm_atmext=xsh_find_frame_with_tag(calib,XSH_ATMOS_EXT,instrument);
00540 if(frm_atmext==NULL) {
00541 xsh_msg_error("Provide atmospheric extinction frame");
00542 return CPL_ERROR_DATA_NOT_FOUND;
00543 }
00544
00545 frm_std_cat=xsh_find_frame_with_tag(calib,XSH_FLUX_STD_CAT,instrument);
00546 if(frm_std_cat==NULL) {
00547 xsh_msg_error("Provide std star catalog frame");
00548 return CPL_ERROR_DATA_NOT_FOUND;
00549 }
00550 high_abs_win=xsh_find_frame_with_tag(calib,XSH_HIGH_ABS_WIN,instrument);
00551 tell_mod_cat=xsh_find_frame_with_tag(calib,XSH_TELL_MOD_CAT,instrument);
00552
00553
00554 if(NULL == (disp_tab_frame = xsh_find_disp_tab( calib, instrument))) {
00555 xsh_msg("To compute efficiency, you must give a DISP_TAB_ARM input");
00556 }
00557
00558
00559
00560
00561
00562
00563 check(xsh_slit_offset_get_params(parameters,RECIPE_ID,
00564 &loc_obj_par,&rectify_par,&crh_single_par,
00565 &extract_par,
00566 &combine_nod_param,&do_flatfield,&gen_sky));
00567
00568
00569
00570
00571 check(ipol_bp = xsh_parameters_interpolate_bp_get(RECIPE_ID,parameters));
00572 check(xsh_rectify_params_set_defaults(parameters,RECIPE_ID,instrument,rectify_par));
00573 rectify_par->conserve_flux=FALSE;
00574 check(xsh_params_monitor(rectify_par,loc_obj_par));
00575 check( corr_tell = xsh_parameters_get_boolean( parameters,RECIPE_ID,
00576 "correct-tellurics"));
00577
00578 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00579 "pre-overscan-corr"));
00580
00581
00582
00583
00584
00585
00586 check(xsh_frameset_check_uniform_exptime(raws,instrument));
00587
00588
00589
00590 check( nb_sub_frames = xsh_dfs_split_offset( raws, XSH_STD_FLUX_SLIT_OFFSET,
00591 XSH_SKY_SLIT, &raw_object,
00592 &raw_sky ) ) ;
00593
00594 prefix=xsh_set_recipe_file_prefix(raw_object,"xsh_respon_slit_offset");
00595 check(strcpy(rec_prefix,prefix));
00596 XSH_FREE(prefix);
00597
00598 xsh_msg("cmap=%d wavemap=%p slitmap=%p",do_computemap,wavemap,slitmap);
00599 if( (do_computemap == 1) && (wavemap ==NULL || slitmap ==NULL ) ) {
00600 if (model_config_frame != NULL) {
00601 use_model=1;
00602 }
00603 check( xsh_check_get_map( disp_tab_frame, order_tab_edges,
00604 master_flat, model_config_frame, calib, instrument,
00605 do_computemap, use_model, rec_prefix,
00606 &wavemap, &slitmap));
00607 }
00608
00609
00610
00611 xsh_msg("rec_prefix=%s",rec_prefix);
00612 crh_clean_obj = xsh_frameset_crh_single(raw_object, crh_single_par,instrument,rec_prefix,"OBJ");
00613 crh_clean_sky = xsh_frameset_crh_single(raw_sky, crh_single_par, instrument,rec_prefix,"SKY");
00614
00615
00616 check(
00617 sub_frameset = xsh_subtract_sky_offset( crh_clean_obj, crh_clean_sky, nb_sub_frames,instrument));
00618 xsh_free_frameset(&crh_clean_obj);
00619 xsh_free_frameset(&crh_clean_sky);
00620
00621 xsh_msg_dbg_low(
00622 "Nb of Subtracted Frames: %" CPL_SIZE_FORMAT "", cpl_frameset_get_size( sub_frameset ));
00623
00624 sprintf(arm_str, "%s", xsh_instrument_arm_tostring(instrument));
00625
00626
00627 if(gen_sky) {
00628 sprintf(sky_prefix,xsh_set_recipe_sky_file_prefix(rec_prefix));
00629 }
00630 if(nb_sub_frames==0) {
00631 xsh_msg_error("nb_sub_frames=%d something wrong check your input raws",
00632 nb_sub_frames);
00633 goto cleanup;
00634
00635 }
00636
00637
00638
00639 xsh_msg_dbg_low( "Nb of Subtracted Frames: %" CPL_SIZE_FORMAT "",
00640 cpl_frameset_get_size( sub_frameset ) ) ;
00641
00642 check( clean_frameset = cpl_frameset_new() ) ;
00643
00644 sprintf( arm_str, "%s", xsh_instrument_arm_tostring(instrument) ) ;
00645
00646 if(gen_sky) {
00647
00648 if(master_bias!= NULL && pre_overscan_corr ==0 ) {
00649 check(sky_bias=xsh_pre_frameset_subtract_frame(raw_sky,master_bias,"MBIAS",instrument));
00650 } else {
00651 sky_bias=cpl_frameset_duplicate(raw_sky);
00652 }
00653
00654 if(master_dark!= NULL) {
00655 check(sky_dark=xsh_pre_frameset_subtract_frame(sky_bias,master_dark,"MDARK",instrument));
00656 } else {
00657 sky_dark=cpl_frameset_duplicate(sky_bias);
00658 }
00659
00660 sprintf(sky_tag,"%s%s%s",rec_prefix,"_SKY_",arm_str);
00661 check(avg_sky=xsh_frameset_average_pre(sky_dark,instrument,sky_tag));
00662 if(do_flatfield==1) {
00663 sprintf(sky_tag,"%s%s%s",rec_prefix,"_FF_SKY_",arm_str);
00664 sprintf(file_name,"%s.fits",sky_tag);
00665
00666 check( sky_divided = xsh_divide_flat( avg_sky,master_flat, sky_tag,
00667 instrument ) ) ;
00668 xsh_add_temporary_file(file_name);
00669
00670 } else {
00671 sky_divided=cpl_frame_duplicate(avg_sky);
00672 }
00673 }
00674
00675
00676
00677
00678 {
00679 xsh_pre * xpre = NULL ;
00680 std_flux_frame= cpl_frameset_get_frame( sub_frameset, 0 ) ;
00681 check( xpre = xsh_pre_load( std_flux_frame, instrument ) ) ;
00682 exptime = xpre->exptime ;
00683 xsh_msg_dbg_medium( "EXPTIME: %lf", exptime ) ;
00684
00685 xsh_pre_free( &xpre ) ;
00686 }
00687
00688 if (do_flatfield == 1) {
00689 clean_frameset=xsh_frameset_mflat_divide(sub_frameset,master_flat,instrument);
00690 } else {
00691 clean_frameset = cpl_frameset_duplicate(sub_frameset);
00692 }
00693
00694 check(div_clean_frame = cpl_frameset_get_frame(clean_frameset,0));
00695
00696 check( rect_frameset = cpl_frameset_new() ) ;
00697 for( i = 0 ; i < nb_sub_frames ; i++ ) {
00698 cpl_frame * rectif = NULL ;
00699 cpl_frame * rectif_eso = NULL ;
00700 cpl_frame * clean = NULL ;
00701 char str[16] ;
00702
00703 sprintf( str, "%d", i ) ;
00704 check( clean = cpl_frameset_get_frame( clean_frameset, i ) ) ;
00705 xsh_msg( "Rectifying Frame '%s'", cpl_frame_get_filename( clean ) ) ;
00706 sprintf(file_name,"RECTIFIED_SLIT_OFFSET_%s_%s.fits",arm_str,str) ;
00707 check( rectif = xsh_rectify( clean, order_tab_edges, wave_tab,
00708 model_config_frame, instrument,
00709 rectify_par, spectral_format,
00710 disp_tab_frame,
00711 file_name,&rectif_eso,&rectif_tab,
00712 rec_prefix) ) ;
00713 xsh_add_temporary_file(file_name);
00714
00715 check( cpl_frameset_insert( rect_frameset, rectif ) ) ;
00716 check( cpl_frameset_insert( rect_frameset, rectif_eso ) ) ;
00717 xsh_free_frame(&rectif_tab);
00718 }
00719
00720
00721
00722 if(gen_sky) {
00723 xsh_msg("rectify sky frame");
00724 sprintf(file_name,"%s_RECTIFIED_SKY_%s.fits",sky_prefix,arm_str) ;
00725 check( rec_sky = xsh_rectify(sky_divided, order_tab_edges, wave_tab,
00726 model_config_frame, instrument,
00727 rectify_par, spectral_format,
00728 disp_tab_frame,
00729 file_name,&rec_sky_eso,&rectif_tab,
00730 sky_prefix) ) ;
00731
00732 xsh_add_temporary_file(file_name);
00733
00734 xsh_msg("Merge 2D sky frame");
00735 check( mer_sky = xsh_merge_ord( rec_sky, instrument,
00736 merge_par,sky_prefix));
00737
00738
00739 }
00740
00741
00742 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_ORDER2D, arm_str) ;
00743 check(rect2_frameset_tables=xsh_frameset_ext_table_frames(rect_frameset));
00744
00745 check( comb_frame = xsh_combine_nod(rect2_frameset_tables, combine_nod_param,
00746 file_tag,instrument,&comb_frame_eso));
00747 xsh_free_frameset(&rect2_frameset_tables);
00748
00749 if ( extract_par->method == LOCALIZATION_METHOD ||
00750 extract_par->method == CLEAN_METHOD ) {
00751 xsh_msg( "Localize before extraction" ) ;
00752 sprintf(file_name,"LOCALIZE_%s_ALL.fits",arm_str) ;
00753 check( loc_table_frame = xsh_localize_obj( comb_frame, NULL,instrument,
00754 loc_obj_par, NULL,
00755 file_name) ) ;
00756 xsh_add_temporary_file(file_name);
00757 }
00758
00759 xsh_msg( "Calling xsh_extract" ) ;
00760
00761 check(ext_frame = xsh_extract_clean(comb_frame, loc_table_frame,
00762 instrument, extract_par,ipol_bp,&ext_frame_eso,
00763 rec_prefix )) ;
00764
00765 xsh_msg( "Calling xsh_merge_ord with 1D frame" ) ;
00766 check( res_1D_frame = xsh_merge_ord( ext_frame, instrument,
00767 merge_par,rec_prefix));
00768
00769
00770
00771
00772
00773 xsh_msg( "Calling xsh_merge_ord with 2D frame" ) ;
00774 check( res_2D_frame = xsh_merge_ord( comb_frame, instrument, merge_par,
00775 rec_prefix) ) ;
00776
00777
00778
00779 if(frm_std_cat!=NULL && frm_atmext!=NULL) {
00780 xsh_msg( "Calling xsh_compute_response" ) ;
00781 if( (response_ord_frame = xsh_compute_response_ord( ext_frame_eso,
00782 frm_std_cat,
00783 frm_atmext,
00784 high_abs_win,
00785 instrument,
00786 exptime )) == NULL) {
00787 xsh_msg_warning("Some error occurred during response computation");
00788 xsh_print_rec_status(0);
00789 cpl_error_reset();
00790 }
00791
00792 if( ( response_frame = xsh_compute_response2( res_1D_frame,
00793 frm_std_cat,
00794 frm_atmext,
00795 high_abs_win,
00796 tell_mod_cat,
00797 instrument,
00798 exptime,
00799 corr_tell )) == NULL) {
00800 xsh_msg_warning("Some error occurred during response computation");
00801 xsh_print_rec_status(0);
00802 cpl_error_reset();
00803 } else {
00804
00805 check(xsh_frame_table_monitor_flux_qc(response_frame,"LAMBDA",
00806 "RESPONSE","RESP",instrument));
00807 }
00808
00809 }
00810
00811 if(mresponse_frame != NULL) {
00812
00813 check(xsh_flux_calibrate(comb_frame_eso,ext_frame_eso,frm_atmext,
00814 mresponse_frame,merge_par,instrument,rec_prefix,
00815 &fluxcal_rect_2D_frame,&fluxcal_rect_1D_frame,
00816 &fluxcal_2D_frame,&fluxcal_1D_frame));
00817
00818 }
00819 else if(mresponse_frame == NULL && response_frame != NULL) {
00820
00821 check(xsh_flux_calibrate(comb_frame_eso,ext_frame_eso,frm_atmext,
00822 response_frame,merge_par,instrument,rec_prefix,
00823 &fluxcal_rect_2D_frame,&fluxcal_rect_1D_frame,
00824 &fluxcal_2D_frame,&fluxcal_1D_frame));
00825
00826 }
00827
00828
00829 if(response_ord_frame != NULL && disp_tab_frame != NULL) {
00830 int conserve_flux=rectify_par->conserve_flux;
00831 xsh_msg( "Calling xsh_multiply_flat" ) ;
00832
00833 sprintf(file_tag,"SLIT_STARE_NOCRH_NOT_FF_%s",arm_str) ;
00834 if(do_flatfield) {
00835 check( eff_frame = xsh_multiply_flat( div_clean_frame, master_flat,
00836 file_tag, instrument ) ) ;
00837 } else {
00838 check(eff_frame=cpl_frame_duplicate(div_clean_frame));
00839 }
00840 sprintf(file_name,"%s_EFF_%s_%s.fits",rec_prefix,XSH_ORDER2D,
00841 xsh_instrument_arm_tostring(instrument));
00842 xsh_add_temporary_file(file_name);
00843
00844
00845 rectify_par->conserve_flux=1;
00846 check( rect_eff_frame = xsh_rectify( eff_frame,
00847 order_tab_edges,
00848 wave_tab,
00849 model_config_frame,
00850 instrument,
00851 rectify_par,
00852 spectral_format,
00853 disp_tab_frame,
00854 file_name,
00855 &rect_eff_frame_eso,
00856 &rect_eff_frame_tab,
00857 rec_prefix));
00858
00859
00860
00861
00862
00863
00864
00865 xsh_msg( "Calling xsh_extract" ) ;
00866 check( ext_eff_frame = xsh_extract_clean(rect_eff_frame,loc_table_frame,
00867 instrument,extract_par, ipol_bp,
00868 &ext_eff_frame2,rec_prefix)) ;
00869
00870
00871 frm_eff=xsh_compute_efficiency(ext_eff_frame2,frm_std_cat,
00872 frm_atmext,high_abs_win,instrument);
00873
00874 rectify_par->conserve_flux=conserve_flux;
00875
00876 }
00877
00878
00879 if(model_config_frame!=NULL && wavemap != NULL&& slitmap != NULL) {
00880
00881 check(xsh_compute_resampling_accuracy(wavemap,slitmap,order_tab_edges,model_config_frame,comb_frame_eso,instrument));
00882 check(xsh_compute_resampling_accuracy(wavemap,slitmap,order_tab_edges,model_config_frame,res_2D_frame,instrument));
00883
00884 check(xsh_compute_wavelength_resampling_accuracy(wavemap,order_tab_edges,model_config_frame,res_1D_frame,instrument));
00885 check(xsh_compute_wavelength_resampling_accuracy(wavemap,order_tab_edges,model_config_frame,ext_frame_eso,instrument));
00886
00887 xsh_add_afc_info(model_config_frame,wavemap);
00888 xsh_add_afc_info(model_config_frame,slitmap);
00889
00890 if(fluxcal_rect_2D_frame != NULL) {
00891
00892 check(xsh_compute_resampling_accuracy(wavemap,slitmap,order_tab_edges,model_config_frame,fluxcal_rect_2D_frame,instrument));
00893 check(xsh_compute_resampling_accuracy(wavemap,slitmap,order_tab_edges,model_config_frame,fluxcal_2D_frame,instrument));
00894
00895 check(xsh_compute_wavelength_resampling_accuracy(wavemap,order_tab_edges,model_config_frame,fluxcal_rect_1D_frame,instrument));
00896 check(xsh_compute_wavelength_resampling_accuracy(wavemap,order_tab_edges,model_config_frame,fluxcal_1D_frame,instrument));
00897
00898 }
00899
00900 if(gen_sky) {
00901 check(xsh_compute_resampling_accuracy(wavemap,slitmap,order_tab_edges,model_config_frame,rec_sky_eso,instrument));
00902 check(xsh_compute_resampling_accuracy(wavemap,slitmap,order_tab_edges,model_config_frame,mer_sky,instrument));
00903 }
00904
00905 }
00906
00907
00908 xsh_msg( "Saving products") ;
00909 if(response_ord_frame!=NULL) {
00910 check( xsh_add_product_table(response_ord_frame, frameset, parameters,
00911 RECIPE_ID, instrument,NULL));
00912 }
00913 if(response_frame!=NULL) {
00914 check( xsh_add_product_table( response_frame, frameset, parameters,
00915 RECIPE_ID, instrument,NULL));
00916 }
00917
00918
00919
00920
00921
00922
00923
00924
00925 check( xsh_add_product_image( comb_frame_eso, frameset,
00926 parameters, RECIPE_ID, instrument,NULL));
00927
00928
00929
00930
00931
00932
00933 check( xsh_add_product_image( ext_frame_eso, frameset,
00934 parameters, RECIPE_ID, instrument,NULL));
00935
00936
00937 check( xsh_add_product_spectrum( res_2D_frame, frameset, parameters,
00938 RECIPE_ID, instrument));
00939
00940 check(xsh_monitor_spectrum1D_flux(res_1D_frame,instrument));
00941 check( xsh_add_product_spectrum( res_1D_frame, frameset, parameters,
00942 RECIPE_ID, instrument));
00943
00944
00945 if(fluxcal_2D_frame != NULL) {
00946 check( xsh_add_product_image(fluxcal_rect_2D_frame,frameset,parameters,
00947 RECIPE_ID, instrument,NULL));
00948 check( xsh_add_product_image(fluxcal_rect_1D_frame,frameset,parameters,
00949 RECIPE_ID, instrument,NULL));
00950 check( xsh_add_product_spectrum( fluxcal_2D_frame, frameset, parameters,
00951 RECIPE_ID, instrument));
00952 check( xsh_add_product_spectrum( fluxcal_1D_frame, frameset, parameters,
00953 RECIPE_ID, instrument));
00954 }
00955
00956
00957 if(gen_sky) {
00958 check( xsh_add_product_image( avg_sky, frameset, parameters,
00959 RECIPE_ID, instrument,NULL));
00960
00961
00962
00963 check( xsh_add_product_image( rec_sky_eso, frameset, parameters,
00964 RECIPE_ID, instrument,NULL));
00965
00966
00967 check( xsh_add_product_image( mer_sky, frameset, parameters,
00968 RECIPE_ID, instrument,NULL));
00969
00970 }
00971
00972
00973
00974
00975 if(frm_eff!=NULL) {
00976
00977 check(xsh_add_product_table(frm_eff, frameset,parameters,
00978 RECIPE_ID, instrument,NULL));
00979 }
00980
00981
00982 if (do_computemap){
00983
00984
00985
00986 check(xsh_add_product_image( wavemap, frameset,
00987 parameters, RECIPE_ID, instrument, NULL));
00988
00989
00990
00991 check(xsh_add_product_image( slitmap, frameset,
00992 parameters, RECIPE_ID, instrument,NULL));
00993 }
00994
00995 cleanup:
00996
00997 xsh_end( RECIPE_ID, frameset, parameters );
00998 XSH_FREE( crh_single_par ) ;
00999 XSH_FREE( rectify_par ) ;
01000 XSH_FREE( loc_obj_par ) ;
01001 XSH_FREE(extract_par);
01002
01003
01004 XSH_FREE(combine_nod_param);
01005 XSH_FREE(ipol_bp);
01006 xsh_instrument_free(&instrument );
01007 xsh_free_frameset(&rect2_frameset_tables);
01008 xsh_free_frameset(&raws);
01009
01010
01011 xsh_free_frameset( &raw_object);
01012 xsh_free_frameset( &raw_sky);
01013
01014 xsh_free_frameset(&calib);
01015 xsh_free_frameset(&sub_frameset);
01016 xsh_free_frameset(&clean_frameset);
01017
01018
01019 xsh_free_frameset(&sky_bias);
01020 xsh_free_frameset(&sky_dark);
01021
01022 xsh_free_frame(&loc_table_frame) ;
01023 xsh_free_frame(&clean_frame) ;
01024 xsh_free_frame(&res_1D_frame) ;
01025 xsh_free_frame(&res_2D_frame) ;
01026 xsh_free_frame(&fluxcal_rect_1D_frame) ;
01027 xsh_free_frame(&fluxcal_rect_2D_frame) ;
01028 xsh_free_frame(&fluxcal_1D_frame) ;
01029 xsh_free_frame(&fluxcal_2D_frame) ;
01030 xsh_free_frame(&nrm_1D_frame) ;
01031 xsh_free_frame(&nrm_2D_frame) ;
01032 xsh_free_frame( &comb_frame ) ;
01033 xsh_free_frame( &comb_frame_eso ) ;
01034 xsh_free_frame( &ext_frame ) ;
01035 xsh_free_frame( &ext_frame_eso ) ;
01036 xsh_free_frame( &eff_frame ) ;
01037 xsh_free_frame( &rect_eff_frame ) ;
01038 xsh_free_frame( &rect_eff_frame_eso ) ;
01039 xsh_free_frame( &rect_eff_frame_tab ) ;
01040
01041
01042 xsh_free_frame( &avg_sky ) ;
01043 xsh_free_frame( &wavemap ) ;
01044 xsh_free_frame( &slitmap ) ;
01045 xsh_free_frame( &avg_sky ) ;
01046 xsh_free_frame( &sky_divided ) ;
01047 xsh_free_frame( &rec_sky ) ;
01048 xsh_free_frame( &rec_sky_eso ) ;
01049 xsh_free_frame( &mer_sky ) ;
01050 xsh_free_frame( &ext_eff_frame ) ;
01051 xsh_free_frame( &ext_eff_frame2 ) ;
01052
01053 xsh_free_frame( &frm_eff ) ;
01054 xsh_free_frame( &response_frame ) ;
01055 xsh_free_frame( &response_ord_frame ) ;
01056
01057 return CPL_ERROR_NONE;
01058 }
01059