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