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 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00038
00041
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_msg.h>
00054
00055 #include <xsh_dfs.h>
00056 #include <xsh_pfits.h>
00057
00058 #include <xsh_utils_image.h>
00059 #include <xsh_utils_scired_slit.h>
00060 #include <xsh_data_instrument.h>
00061 #include <xsh_data_spectrum1D.h>
00062 #include <xsh_drl_check.h>
00063 #include <xsh_drl.h>
00064 #include <xsh_model_utils.h>
00065 #include <xsh_model_arm_constants.h>
00066
00067
00068 #include <cpl.h>
00069
00070
00071
00072
00073
00074
00075 #define RECIPE_ID "xsh_scired_slit_stare"
00076 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer, D. Bramich, A. Modigliani"
00077 #define RECIPE_CONTACT "amodigli@eso.org"
00078
00079
00080
00081
00082
00083
00084
00085 #include <xsh_utils_image.h>
00086
00087
00088 static int xsh_scired_slit_stare_create(cpl_plugin *);
00089 static int xsh_scired_slit_stare_exec(cpl_plugin *);
00090 static int xsh_scired_slit_stare_destroy(cpl_plugin *);
00091
00092
00093 static cpl_error_code xsh_scired_slit_stare(cpl_parameterlist *, cpl_frameset *);
00094
00095
00096
00097
00098 static char xsh_scired_slit_stare_description_short[] =
00099 "Reduce science exposure in SLIT configuration and stare mode";
00100
00101 static char xsh_scired_slit_stare_description[] =
00102 "This recipe reduces science exposure in SLIT configuration and stare mode\n\
00103 Input Frames : \n\
00104 - A set of n Science frames ( n == 1 or >=3, \
00105 Tag = OBJECT_SLIT_STARE_UVB)\n\
00106 - A spectral format table (Tag = SPECTRAL_FORMAT_TAB_arm)\n\
00107 - [UVB,VIS] A master bias frame (Tag = MASTER_BIAS_arm)\n\
00108 - [OPTIONAL]A master dark frame (Tag = MASTER_DARK_arm)\n\
00109 - A master flat frame (Tag = MASTER_FLAT_SLIT_arm)\n\
00110 - An order table frame(Tag = ORDER_TAB_EDGES_SLIT_arm)\n\
00111 - [OPTIONAL] A table with dispersion coefficients (Tag = DISP_TAB_arm,\n\
00112 required in poly mode\n\
00113 - [poly mode] A wave solution frame(Tag = WAVE_TAB_2D_arm)\n\
00114 - [physical model mode] A model cfg table (Format = TABLE, Tag = XSH_MOD_CFG_TAB_arm)\n\
00115 - [OPTIONAL] A table specifying multiplying factor for break points \n\
00116 (Tag = SKY_SUB_BKPTS_arm) to generate\n\
00117 - [OPTIONAL] A non-linear badpixel map (Tag = BP_MAP_NL_arm)\n\
00118 - [OPTIONAL] A reference badpixel map (Tag = BP_MAP_RP_arm)\n\
00119 - [OPTIONAL,physmod mode] A table listing sky line positions (Tag = SKY_LINE_LIST_arm)\n\
00120 this is used to be able to control quality of sky subtraction, for example\n\
00121 projecting guess positions on the product SCI_SLIT_STARE_SUB_SKY_arm\n\
00122 - [OPTIONAL] The instrument response table (Tag = RESPONSE_ORDER1D_SLIT_arm)\n\
00123 - [OPTIONAL] An atmospheric extinction table (Tag = ATMOS_EXT_arm)\n\
00124 - [OPTIONAL] A telluric mask (Tag = TELL_MASK_arm)\n\
00125 Products : \n\
00126 - PREFIX_ORDER2D_arm extracted spectrum, order-by-order, 2D\n\
00127 - PREFIX_ORDER1D_arm extracted spectrum, order-by-order, 1D\n\
00128 - PREFIX_MERGE2D_arm merged spectrum, 2D\n\
00129 - PREFIX_MERGE1D_arm merged spectrum, 1D\n\
00130 - SKY_SLIT_MERGE2D_arm merged spectrum sky, 2D\n\
00131 - PREFIX_SUB_BACK_SLIT_arm sci frame bias, (dark), inter-order bkg subtracted\n\
00132 - PREFIX_WAVE_MAP_arm, wave map image\n\
00133 - PREFIX_SLIT_MAP_arm, slit map image\n\
00134 - PREFIX_DIVFF_arm as PREFIX_SUB_BACK_SLIT_arm, flat fielded\n\
00135 - PREFIX_SUB_SKY_arm, as PREFIX_DIVFF_arm, sky subtracted\n\
00136 - PREFIX_SKY_arm, 2D sky frame\n\
00137 - PREFIX_SKY_ORD1D_arm, 1D sky image (order-by-order)\n\
00138 - PREFIX_BACK_SLIT_arm, inter order background image \n\
00139 - where PREFIX is SCI, FLUX, TELL if input raw DPR.TYPE contains OBJECT or FLUX or TELLURIC\n\
00140 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUX_ORDER2D_arm (2 dimension)\n\
00141 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUX_ORDER1D_arm (1 dimension)\n\
00142 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUX_MERGE2D_arm (2 dimension)\n\
00143 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUX_MERGE1D_arm (1 dimension)\n\
00144 - PREFIX_ON_arm bias (dark) subtracted sci frame";
00145
00146
00147
00148
00149
00158
00159
00160 int cpl_plugin_get_info(cpl_pluginlist *list) {
00161 cpl_recipe *recipe = NULL;
00162 cpl_plugin *plugin = NULL;
00163
00164 recipe = cpl_calloc(1, sizeof(*recipe));
00165 if ( recipe == NULL ){
00166 return -1;
00167 }
00168
00169 plugin = &recipe->interface ;
00170
00171 cpl_plugin_init(plugin,
00172 CPL_PLUGIN_API,
00173 XSH_BINARY_VERSION,
00174 CPL_PLUGIN_TYPE_RECIPE,
00175 RECIPE_ID,
00176 xsh_scired_slit_stare_description_short,
00177 xsh_scired_slit_stare_description,
00178 RECIPE_AUTHOR,
00179 RECIPE_CONTACT,
00180 xsh_get_license(),
00181 xsh_scired_slit_stare_create,
00182 xsh_scired_slit_stare_exec,
00183 xsh_scired_slit_stare_destroy);
00184
00185 cpl_pluginlist_append(list, plugin);
00186
00187 return (cpl_error_get_code() != CPL_ERROR_NONE);
00188 }
00189
00190
00200
00201
00202 static int xsh_scired_slit_stare_create(cpl_plugin *plugin){
00203 cpl_recipe *recipe = NULL;
00204
00205
00206 xsh_remove_crh_single_param crh_single = { 0.1, 20, 2.0, 4} ;
00207 xsh_rectify_param rectify = { "tanh",
00208 CPL_KERNEL_DEFAULT,
00209 2,
00210 -1.0,
00211 -1.0,
00212 1,
00213 0, 0. };
00214
00215 xsh_subtract_sky_single_param sky_single = {3000, 3000,7,20, 5., -1, -1,
00216 MEDIAN_METHOD, FINE,7, 1.5,
00217 0.0, 0.0,
00218 0.0, 0.0} ;
00219
00220
00221 xsh_localize_obj_param loc_obj =
00222 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3, FALSE};
00223
00224 xsh_opt_extract_param opt_extract_par =
00225 { 5, 10, 10, 0.01, 10.0, 1., 2, 2, GAUSS_METHOD };
00226 xsh_stack_param stack_param = {"median",5.,5.};
00227 xsh_interpolate_bp_param ipol_par = {30 };
00228
00229 opt_extract_par.oversample = 5;
00230 opt_extract_par.box_hsize = 10;
00231 opt_extract_par.chunk_size = 50;
00232 opt_extract_par.lambda_step = 0.02;
00233 opt_extract_par.clip_kappa = 3;
00234 opt_extract_par.clip_frac = 0.4;
00235 opt_extract_par.clip_niter = 2;
00236 opt_extract_par.niter = 1;
00237 opt_extract_par.method = GAUSS_METHOD;
00238
00239 xsh_init();
00240
00241
00242 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00243
00244
00245 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00246 CPL_ERROR_TYPE_MISMATCH,
00247 "Plugin is not a recipe");
00248
00249 recipe = (cpl_recipe *)plugin;
00250
00251
00252 recipe->parameters = cpl_parameterlist_new();
00253 assure( recipe->parameters != NULL,
00254 CPL_ERROR_ILLEGAL_OUTPUT,
00255 "Memory allocation failed!");
00256
00257
00258 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00259 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00260 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00261 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00262
00263
00264 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00265
00266
00267 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00268 crh_single )) ;
00269
00270
00271 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00272 rectify )) ;
00273
00274
00275 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00276 loc_obj )) ;
00277
00278
00279 check(xsh_parameters_subtract_sky_single_create(RECIPE_ID,recipe->parameters,
00280 sky_single )) ;
00281
00282
00283
00284
00285
00286
00287
00288 check(xsh_parameters_interpolate_bp_create(RECIPE_ID,
00289 recipe->parameters,ipol_par)) ;
00290
00291
00292 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00293 "do-optextract", FALSE,
00294 "TRUE if we do the optimal extraction"));
00295
00296 check( xsh_parameters_opt_extract_create( RECIPE_ID, recipe->parameters,
00297 opt_extract_par));
00298
00299
00300
00301
00302
00303 cleanup:
00304 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00305 xsh_error_dump(CPL_MSG_ERROR);
00306 return 1;
00307 }
00308 else {
00309 return 0;
00310 }
00311 }
00312
00313
00319
00320
00321 static int xsh_scired_slit_stare_exec(cpl_plugin *plugin) {
00322 cpl_recipe *recipe = NULL;
00323
00324
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
00335 xsh_scired_slit_stare(recipe->parameters, recipe->frames);
00336
00337 cleanup:
00338 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00339 xsh_error_dump(CPL_MSG_ERROR);
00340 xsh_error_reset();
00341 return 1;
00342 }
00343 else {
00344 return 0;
00345 }
00346 }
00347
00348
00354
00355 static int xsh_scired_slit_stare_destroy(cpl_plugin *plugin)
00356 {
00357 cpl_recipe *recipe = NULL;
00358
00359
00360 xsh_error_reset();
00361
00362 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00363
00364
00365 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00366 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00367
00368 recipe = (cpl_recipe *)plugin;
00369
00370 xsh_free_parameterlist(&recipe->parameters);
00371
00372 cleanup:
00373 if (cpl_error_get_code() != CPL_ERROR_NONE)
00374 {
00375 return 1;
00376 }
00377 else
00378 {
00379 return 0;
00380 }
00381 }
00382
00383 static cpl_error_code
00384 xsh_params_monitor(xsh_rectify_param * rectify_par,
00385 xsh_localize_obj_param * loc_obj_par,
00386 xsh_opt_extract_param *opt_extract_par,
00387 int sub_sky_nbkpts1,
00388 int sub_sky_nbkpts2)
00389 {
00390
00391 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00392 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00393 rectify_par->rectif_bin_space);
00394
00395 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00396 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00397
00398 xsh_msg_dbg_low("opt extract params: chunk_size=%d lambda_step=%g box_hsize=%d",
00399 opt_extract_par->chunk_size,opt_extract_par->lambda_step,
00400 opt_extract_par->box_hsize);
00401
00402 xsh_msg_dbg_low("sky params: nbkpts1=%d nbkpts2=%d",
00403 sub_sky_nbkpts1,sub_sky_nbkpts2);
00404
00405 return cpl_error_get_code();
00406
00407 }
00408
00409
00417
00418 static cpl_error_code
00419 xsh_scired_slit_stare(cpl_parameterlist* parameters,
00420 cpl_frameset* frameset)
00421 {
00422 const char* recipe_tags[3] = {XSH_OBJECT_SLIT_STARE,
00423 XSH_STD_TELL_SLIT_STARE,
00424 XSH_STD_FLUX_SLIT_STARE};
00425 int recipe_tags_size = 3;
00426
00427
00428 cpl_frameset *raws = NULL;
00429 cpl_frameset *calib = NULL;
00430
00431
00432 cpl_frame *bpmap = NULL;
00433 cpl_frame *master_bias = NULL;
00434 cpl_frame *master_dark = NULL;
00435 cpl_frame *master_flat = NULL;
00436 cpl_frame *order_tab_edges = NULL;
00437 cpl_frame *wave_tab = NULL ;
00438 cpl_frame *model_config_frame = NULL ;
00439 cpl_frame *wavemap_frame = NULL ;
00440 cpl_frame *slitmap_frame = NULL ;
00441 cpl_frame *disp_tab_frame = NULL;
00442 cpl_frame *spectralformat_frame = NULL ;
00443 cpl_frame *tellmask_frame = NULL;
00444
00445 xsh_clipping_param* crh_clipping_par = NULL;
00446 xsh_background_param* backg_par = NULL;
00447 xsh_remove_crh_single_param * crh_single_par = NULL ;
00448 xsh_rectify_param * rectify_par = NULL ;
00449 xsh_localize_obj_param * loc_obj_par = NULL ;
00450 xsh_stack_param* stack_par=NULL;
00451 int sub_sky_nbkpts1 = SUBTRACT_SKY_SINGLE_NBKPTS ;
00454 int sub_sky_nbkpts2 = SUBTRACT_SKY_SINGLE_NBKPTS ;
00457 int do_sub_sky = FALSE;
00458 int recipe_use_model = 0;
00459 int do_optextract = 0;
00460 int do_flatfield = 1;
00461 int do_compute_map = 0;
00462 xsh_extract_param extract_par =
00463 { LOCALIZATION_METHOD };
00464
00465 xsh_opt_extract_param *opt_extract_par = NULL;
00466 xsh_subtract_sky_single_param *sky_par = NULL;
00467
00468 xsh_instrument* instrument = NULL;
00469 int nb_raw_frames ;
00470 char rec_name[256];
00471
00472
00473 #if 0
00474 cpl_frameset * on = NULL, * off = NULL ;
00475 #endif
00476 cpl_frame * crhm_frame = NULL ;
00477
00478 cpl_frame * rmdark = NULL;
00479 cpl_frame * rmbkg = NULL ;
00480 cpl_frame * div_frame = NULL ;
00481 cpl_frame * sub_sky_frame = NULL ;
00482 cpl_frame * sub_sky2_frame = NULL ;
00484 cpl_frame * rect_frame = NULL ;
00485 cpl_frame * loc_table_frame = NULL ;
00486 cpl_frame * clean_frame = NULL ;
00487 cpl_frame * rect2_frame = NULL ;
00488 cpl_frame * rect2_frame_eso = NULL ;
00489 cpl_frame * rect2_frame_tab = NULL ;
00490
00491
00492 cpl_frame * rect2_sky_frame = NULL ;
00493 cpl_frame * rect2_sky_frame_eso = NULL ;
00494 cpl_frame * rect2_sky_frame_tab = NULL ;
00495
00496
00497 cpl_frame * sky_frame = NULL ;
00498 cpl_frame * sky_frame_eso = NULL ;
00499 cpl_frame * res_1D_frame = NULL ;
00500 cpl_frame * res_2D_frame = NULL ;
00501 cpl_frame * res_2D_sky_frame = NULL ;
00502
00503 cpl_frame * fluxcal_rect_1D_frame = NULL ;
00504 cpl_frame * fluxcal_rect_2D_frame = NULL ;
00505 cpl_frame * fluxcal_1D_frame = NULL ;
00506 cpl_frame * fluxcal_2D_frame = NULL ;
00507
00508 cpl_frame * ext_frame = NULL ;
00509 cpl_frame * ext_frame_eso = NULL ;
00510
00511 cpl_frame *orderext1d_frame = NULL;
00512 cpl_frame *orderoxt1d_frame = NULL;
00513 cpl_frame *orderoxt1d_eso_frame = NULL;
00514
00515 cpl_frame *mergeext1d_frame = NULL;
00516 cpl_frame *mergeoxt1d_frame = NULL;
00517 cpl_frame *fluxcal_rect_opt1D_frame = NULL;
00518 cpl_frame *fluxcal_merg_opt1D_frame = NULL;
00519
00520 cpl_frame* grid_backg=NULL;
00521 cpl_frame* frame_backg=NULL;
00522 cpl_frame* sky_frame_ima=NULL;
00523 char prefix[256];
00524 char fname[256];
00525 char tag[256];
00526
00527 const char* ftag=NULL;
00528
00529 cpl_frame* single_frame_sky_sub_tab_frame=NULL;
00530
00531
00532
00533 cpl_frame* clean_obj=NULL;
00534 char *rec_prefix = NULL;
00535 char sky_prefix[256];
00536 cpl_frame* sky_list_frame=NULL;
00537 cpl_frame* qc_sky_frame=NULL;
00538 cpl_propertylist* plist=NULL;
00539
00540 xsh_pre* pre_sci=NULL;
00541
00542 cpl_frame* response_frame=NULL;
00543 cpl_frame* frm_atmext=NULL;
00544 int pre_overscan_corr=0;
00545 cpl_frame *qc_subex_frame = NULL;
00546 cpl_frame *qc_s2ddiv1d_frame = NULL;
00547 cpl_frame *qc_model_frame = NULL;
00548 cpl_frame *qc_weight_frame = NULL;
00549 int merge_par=0;
00550 xsh_interpolate_bp_param *ipol_bp=NULL;
00551 cpl_frameset* crh_clean_lacosmic = NULL;
00552 cpl_frameset *sub_bias_set = NULL;
00553 cpl_frameset *sub_dark_set = NULL;
00554
00555
00556
00557 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00558 recipe_tags, recipe_tags_size,
00559 RECIPE_ID, XSH_BINARY_VERSION,
00560 xsh_scired_slit_stare_description_short ) ) ;
00561
00562
00563
00564
00565
00566 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00567 if(instrument->arm == XSH_ARM_NIR) {
00568 xsh_instrument_nir_corr_if_JH(raws,instrument);
00569 }
00570
00571
00572
00573
00574 check( nb_raw_frames = cpl_frameset_get_size( raws ) ) ;
00575 xsh_msg_dbg_low("nb_raw_frames=%d",nb_raw_frames);
00576
00577 if(nb_raw_frames>1) {
00578 check(xsh_frameset_check_uniform_exptime(raws,instrument));
00579 }
00580
00581 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00582 "pre-overscan-corr"));
00583
00584
00585 check(xsh_slit_stare_get_calibs(calib,instrument, &spectralformat_frame,
00586 &master_bias,&master_dark,&master_flat,
00587 &order_tab_edges,&model_config_frame,
00588 &wave_tab,&sky_list_frame,&qc_sky_frame,
00589 &bpmap,&single_frame_sky_sub_tab_frame,
00590 &wavemap_frame,&slitmap_frame,RECIPE_ID,
00591 &recipe_use_model,pre_overscan_corr));
00592
00593
00594 if((response_frame=xsh_find_frame_with_tag(calib,XSH_MRESPONSE_MERGE1D_SLIT,
00595 instrument)) == NULL ) {
00596 check(response_frame=xsh_find_frame_with_tag(calib,XSH_RESPONSE_MERGE1D_SLIT,
00597 instrument));
00598 }
00599 if(response_frame != NULL) {
00600 frm_atmext=xsh_find_frame_with_tag(calib,XSH_ATMOS_EXT,instrument);
00601 if(frm_atmext==NULL) {
00602 xsh_msg_error("Provide atmospheric extinction frame");
00603 return CPL_ERROR_DATA_NOT_FOUND;
00604 }
00605 }
00606
00607 tellmask_frame = xsh_find_frame_with_tag(calib,XSH_TELL_MASK, instrument);
00608
00609
00610
00611 check(xsh_slit_stare_get_params(parameters,RECIPE_ID, &pre_overscan_corr,
00612 &backg_par,&loc_obj_par,
00613 &rectify_par,&crh_single_par,&sub_sky_nbkpts1,
00614 &do_flatfield,&sub_sky_nbkpts2,&sky_par,
00615 &do_optextract,
00616 &opt_extract_par));
00617
00618 rectify_par->conserve_flux=FALSE;
00619 check(ipol_bp = xsh_parameters_interpolate_bp_get(RECIPE_ID,parameters));
00620
00621 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00622
00623 check(xsh_rectify_params_set_defaults(parameters,RECIPE_ID,instrument,rectify_par));
00624
00625 check( do_sub_sky = xsh_parameters_subtract_sky_single_get_true( RECIPE_ID,
00626 parameters));
00627 if ( (do_sub_sky && !do_compute_map) &&
00628 (wavemap_frame == NULL || slitmap_frame == NULL) ) {
00629 xsh_msg_warning( "sky-subtract is true but wave,slits maps missing create them");
00630 do_compute_map = TRUE;
00631
00632 }
00633
00634
00635 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR) {
00636 check(xsh_stare_params_bin_scale(raws,backg_par,
00637 opt_extract_par,
00638 &sub_sky_nbkpts1,&sub_sky_nbkpts2));
00639 }
00640
00641
00642 if ( do_compute_map && recipe_use_model==FALSE){
00643 check_msg(disp_tab_frame = xsh_find_disp_tab( calib, instrument),
00644 "compute-map=TRUE, physmodel mode, you must give a DISP_TAB_ARM input");
00645 }
00646
00647 check(xsh_params_monitor(rectify_par,loc_obj_par,opt_extract_par,
00648 sub_sky_nbkpts1,sub_sky_nbkpts2));
00649
00650
00651
00652
00653
00654 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_SLIT_STARE,
00655 instrument,pre_overscan_corr,CPL_TRUE));
00656
00657
00658
00659
00660
00661 cpl_frame* frm = NULL;
00662 cpl_frame* sub = NULL;
00663
00664 int i = 0;
00665 int nraws = 0;
00666 if (master_bias != NULL) {
00667
00668 xsh_msg( "Subtract bias");
00669 sub_bias_set = cpl_frameset_new();
00670 nraws = cpl_frameset_get_size(raws);
00671
00672 for (i = 0; i < nraws; i++) {
00673 sprintf(prefix, "SCI_SUB_%d_", i);
00674 frm = cpl_frameset_get_frame(raws, i);
00675
00676 sub = xsh_check_subtract_bias(frm, master_bias, instrument, prefix,
00677 pre_overscan_corr, 0);
00678 cpl_frameset_insert(sub_bias_set, sub);
00679 }
00680 } else {
00681 sub_bias_set = cpl_frameset_duplicate(raws);
00682 }
00683
00684
00685
00686 if (master_dark != NULL) {
00687 xsh_msg( "Subtract dark");
00688
00689 sub_dark_set = cpl_frameset_new();
00690 nraws = cpl_frameset_get_size(sub_bias_set);
00691
00692 for (i = 0; i < nraws; i++) {
00693 sprintf(prefix, "SCI_%d_SUBTRACT", i);
00694 frm = cpl_frameset_get_frame(sub_bias_set, i);
00695
00696 sub = xsh_check_subtract_dark(frm, master_dark, instrument, prefix);
00697 cpl_frameset_insert(sub_dark_set, sub);
00698 }
00699
00700 } else {
00701 sub_dark_set = cpl_frameset_duplicate(sub_bias_set);
00702 }
00703 xsh_free_frameset(&sub_bias_set);
00704 rmdark=cpl_frameset_get_frame(sub_dark_set,0);
00705
00706 check( rec_prefix = xsh_set_recipe_file_prefix( raws,
00707 "xsh_scired_slit_stare"));
00708 sprintf(prefix,"%s_",rec_prefix);
00709
00710 crh_clean_lacosmic = xsh_frameset_crh_single(sub_dark_set, crh_single_par,instrument,rec_prefix,"OBJ");
00711
00712
00713
00714
00715 ftag = XSH_GET_TAG_FROM_ARM( XSH_SLIT_STARE_REMOVE_CRH, instrument);
00716 check( crhm_frame = xsh_check_remove_crh_multiple( crh_clean_lacosmic, ftag,
00717 stack_par,NULL, instrument, NULL, NULL));
00718
00719 check(xsh_slit_stare_get_maps(calib,
00720 do_compute_map,recipe_use_model,rec_prefix,
00721 instrument,model_config_frame,crhm_frame,
00722 disp_tab_frame,order_tab_edges,
00723 &wavemap_frame, &slitmap_frame));
00724
00725 if( backg_par->method ) {
00726
00727 xsh_msg("Subtract inter-order background");
00728 check(rmbkg = xsh_subtract_background( crhm_frame,
00729 order_tab_edges,
00730 backg_par, instrument,rec_prefix,
00731 &grid_backg,&frame_backg,1,0,1));
00732
00733 } else {
00734 cpl_frame* frm_tmp=NULL;
00735 rmbkg = cpl_frame_duplicate( rmdark );
00736 pre_sci=xsh_pre_load(rmbkg,instrument);
00737 sprintf(tag,"%sNO_SUB_BACK_%s",prefix,
00738 xsh_instrument_arm_tostring(instrument));
00739 sprintf(fname,"%s.fits",tag);
00740 frm_tmp=xsh_pre_save(pre_sci,fname,tag,0);
00741 cpl_frame_set_filename(rmbkg,fname);
00742 cpl_frame_set_tag(rmbkg,tag);
00743 xsh_free_frame(&frm_tmp);
00744 xsh_pre_free(&pre_sci);
00745 }
00746
00747
00748
00749
00750 xsh_slit_stare_correct_crh_and_sky(loc_obj_par,crh_single_par,rectify_par,
00751 do_sub_sky,rec_prefix,rmbkg,
00752 order_tab_edges, slitmap_frame,
00753 wavemap_frame,model_config_frame,
00754 single_frame_sky_sub_tab_frame,instrument,
00755 sub_sky_nbkpts1, sky_par,
00756 &sky_frame,&sky_frame_eso,&sky_frame_ima,
00757 wave_tab,disp_tab_frame,
00758 spectralformat_frame,nb_raw_frames,
00759 &loc_table_frame,&clean_frame,&clean_obj,0);
00760
00761
00762
00763 check( div_frame = xsh_check_divide_flat( do_flatfield, clean_obj,
00764 master_flat, instrument, rec_prefix));
00765
00766 check( sub_sky2_frame = xsh_check_subtract_sky_single( do_sub_sky, div_frame,
00767 order_tab_edges, slitmap_frame, wavemap_frame, loc_table_frame, NULL,
00768 single_frame_sky_sub_tab_frame, instrument, sub_sky_nbkpts2, sky_par,
00769 &sky_frame, &sky_frame_eso, &sky_frame_ima, rec_prefix,0));
00770
00771 xsh_msg( "Prepare S2D products" ) ;
00772 xsh_msg( "Rectify") ;
00773 sprintf(rec_name,"%s_%s_%s.fits",rec_prefix,XSH_ORDER2D,
00774 xsh_instrument_arm_tostring( instrument));
00775
00776 check( rect2_frame = xsh_rectify( sub_sky2_frame, order_tab_edges,
00777 wave_tab, model_config_frame, instrument,
00778 rectify_par,spectralformat_frame,
00779 disp_tab_frame,rec_name,
00780 &rect2_frame_eso,&rect2_frame_tab,
00781 rec_prefix));
00782
00783 if(sky_frame_ima) {
00784
00785 sprintf(sky_prefix,xsh_set_recipe_sky_file_prefix(rec_prefix));
00786 sprintf(rec_name,"%s_%s_%s.fits",sky_prefix,XSH_ORDER2D,
00787 xsh_instrument_arm_tostring( instrument));
00788
00789 check( rect2_sky_frame = xsh_rectify( sky_frame_ima, order_tab_edges,
00790 wave_tab, model_config_frame, instrument,
00791 rectify_par,spectralformat_frame,
00792 disp_tab_frame,rec_name,
00793 &rect2_sky_frame_eso,&rect2_sky_frame_tab,
00794 rec_prefix));
00795 xsh_add_temporary_file(cpl_frame_get_filename(rect2_sky_frame_eso));
00796 }
00797
00798 xsh_msg( "Extract" ) ;
00799 check(ext_frame=xsh_extract_clean(rect2_frame, loc_table_frame,
00800 instrument, &extract_par,ipol_bp,
00801 &ext_frame_eso,rec_prefix)) ;
00802
00803 xsh_msg( "Merge orders with 1D frame" ) ;
00804 check( res_1D_frame = xsh_merge_ord( ext_frame, instrument,
00805 merge_par,rec_prefix));
00806 check( xsh_mark_tell( res_1D_frame, tellmask_frame));
00807
00808 check(xsh_monitor_spectrum1D_flux(res_1D_frame,instrument));
00809
00810
00811 xsh_msg( "Calling xsh_merge_ord with 2D frame" ) ;
00812 if( rect2_sky_frame ) {
00813 check( res_2D_sky_frame = xsh_merge_ord( rect2_sky_frame, instrument,
00814 merge_par,sky_prefix));
00815 }
00816 check( res_2D_frame = xsh_merge_ord( rect2_frame, instrument,
00817 merge_par,rec_prefix));
00818
00819
00820
00821 xsh_msg("Prepare S1D products" ) ;
00822
00823 if ( do_optextract){
00824 xsh_msg( "Optimal extraction");
00825 check( xsh_opt_extract( sub_sky2_frame, order_tab_edges,
00826 wave_tab, model_config_frame, wavemap_frame,
00827 slitmap_frame, loc_table_frame,
00828 spectralformat_frame, master_flat, instrument,
00829 opt_extract_par, rec_prefix,
00830 &orderext1d_frame, &orderoxt1d_frame,
00831 &orderoxt1d_eso_frame,
00832 &qc_subex_frame,
00833 &qc_s2ddiv1d_frame,
00834 &qc_model_frame,
00835 &qc_weight_frame));
00836
00837 check( mergeext1d_frame = xsh_merge_ord( orderext1d_frame, instrument,
00838 merge_par,rec_prefix));
00839 check( mergeoxt1d_frame = xsh_merge_ord( orderoxt1d_frame, instrument,
00840 merge_par,rec_prefix));
00841
00842 check( xsh_mark_tell( mergeext1d_frame, tellmask_frame));
00843 check( xsh_mark_tell( mergeoxt1d_frame, tellmask_frame));
00844 }
00845
00846 if(response_frame != NULL && frm_atmext != NULL) {
00847
00848 check(xsh_flux_calibrate(rect2_frame_eso,ext_frame_eso,frm_atmext,
00849 response_frame,merge_par,instrument,rec_prefix,
00850 &fluxcal_rect_2D_frame,&fluxcal_rect_1D_frame,
00851 &fluxcal_2D_frame,&fluxcal_1D_frame));
00852
00853 if ( do_optextract){
00854
00855 check(xsh_flux_calibrate1D(orderoxt1d_eso_frame,frm_atmext,
00856 response_frame,merge_par,instrument,rec_prefix,
00857 &fluxcal_rect_opt1D_frame,
00858 &fluxcal_merg_opt1D_frame));
00859
00860 }
00861
00862 }
00863
00864
00865
00866
00867
00868
00869
00870
00871 if(model_config_frame!=NULL && wavemap_frame != NULL&& slitmap_frame != NULL) {
00872
00873 check(xsh_compute_resampling_accuracy(wavemap_frame,slitmap_frame,order_tab_edges,model_config_frame,res_2D_frame,instrument));
00874 check(xsh_compute_resampling_accuracy(wavemap_frame,slitmap_frame,order_tab_edges,model_config_frame,rect2_frame_eso,instrument));
00875
00876 check(xsh_compute_wavelength_resampling_accuracy(wavemap_frame,order_tab_edges,model_config_frame,res_1D_frame,instrument));
00877 check(xsh_compute_wavelength_resampling_accuracy(wavemap_frame,order_tab_edges,model_config_frame,ext_frame_eso,instrument));
00878
00879 xsh_add_afc_info(model_config_frame,wavemap_frame);
00880 xsh_add_afc_info(model_config_frame,slitmap_frame);
00881
00882 if(fluxcal_rect_2D_frame != NULL) {
00883
00884 check(xsh_compute_resampling_accuracy(wavemap_frame,slitmap_frame,order_tab_edges,model_config_frame,fluxcal_rect_2D_frame,instrument));
00885 check(xsh_compute_resampling_accuracy(wavemap_frame,slitmap_frame,order_tab_edges,model_config_frame,fluxcal_2D_frame,instrument));
00886
00887 check(xsh_compute_wavelength_resampling_accuracy(wavemap_frame,order_tab_edges,model_config_frame,fluxcal_rect_1D_frame,instrument));
00888 check(xsh_compute_wavelength_resampling_accuracy(wavemap_frame,order_tab_edges,model_config_frame,fluxcal_1D_frame,instrument));
00889
00890 }
00891
00892 if(res_2D_sky_frame) {
00893 check(xsh_compute_resampling_accuracy(wavemap_frame,slitmap_frame,order_tab_edges,model_config_frame,res_2D_sky_frame,instrument));
00894 }
00895
00896 if ( do_sub_sky == 1 ) {
00897 check(xsh_compute_wavelength_resampling_accuracy(wavemap_frame,order_tab_edges,model_config_frame,sky_frame_eso,instrument));
00898 }
00899 }
00900
00901
00902 xsh_msg( "Save products" ) ;
00903
00904 check( xsh_add_product_image(rect2_frame_eso, frameset, parameters,
00905 RECIPE_ID, instrument,NULL));
00906
00907 check( xsh_add_product_image(ext_frame_eso, frameset, parameters,
00908 RECIPE_ID, instrument,NULL));
00909
00910 check( xsh_add_product_spectrum( res_2D_frame, frameset, parameters,
00911 RECIPE_ID, instrument));
00912
00913
00914 check( xsh_add_product_spectrum( res_1D_frame, frameset, parameters,
00915 RECIPE_ID, instrument));
00916
00917 if(res_2D_sky_frame) {
00918 check( xsh_add_product_spectrum( res_2D_sky_frame, frameset, parameters,
00919 RECIPE_ID, instrument));
00920 }
00921
00922 check( xsh_add_product_image( rmbkg, frameset, parameters,
00923 RECIPE_ID, instrument,NULL));
00924
00925 if (do_compute_map){
00926
00927
00928
00929 check(xsh_add_product_image( wavemap_frame, frameset,
00930 parameters, RECIPE_ID, instrument, NULL));
00931
00932
00933
00934 check(xsh_add_product_image( slitmap_frame, frameset,
00935 parameters, RECIPE_ID, instrument,NULL));
00936 }
00937 if(do_flatfield) {
00938
00939 check( xsh_add_product_image( div_frame, frameset, parameters,
00940 RECIPE_ID, instrument,NULL));
00941 }
00942
00943 if ( do_optextract){
00944 check( xsh_add_product_table( orderext1d_frame, frameset,parameters,
00945 RECIPE_ID, instrument,NULL));
00946 check( xsh_add_product_table( orderoxt1d_frame, frameset,parameters,
00947 RECIPE_ID, instrument,NULL));
00948 check( xsh_add_product_spectrum( mergeext1d_frame, frameset, parameters,
00949 RECIPE_ID, instrument));
00950 check( xsh_add_product_spectrum( mergeoxt1d_frame, frameset,
00951 parameters, RECIPE_ID, instrument));
00952 check( xsh_add_product_image( qc_subex_frame, frameset, parameters,
00953 RECIPE_ID, instrument,
00954 cpl_frame_get_tag(qc_subex_frame)));
00955 check( xsh_add_product_image( qc_s2ddiv1d_frame, frameset, parameters,
00956 RECIPE_ID, instrument,
00957 cpl_frame_get_tag(qc_s2ddiv1d_frame)));
00958 check( xsh_add_product_image( qc_model_frame, frameset, parameters,
00959 RECIPE_ID, instrument,
00960 cpl_frame_get_tag(qc_model_frame)));
00961 check( xsh_add_product_image( qc_weight_frame, frameset, parameters,
00962 RECIPE_ID, instrument,
00963 cpl_frame_get_tag(qc_weight_frame)));
00964 }
00965 if ( do_sub_sky == 1 ) {
00966 check( xsh_add_product_pre( sub_sky2_frame, frameset, parameters,
00967 RECIPE_ID, instrument));
00968 check( xsh_add_product_image( sky_frame_ima, frameset, parameters,
00969 RECIPE_ID, instrument,NULL));
00970 check( xsh_add_product_image( sky_frame_eso, frameset,
00971 parameters, RECIPE_ID, instrument,NULL));
00972 }
00973 if(frame_backg != NULL) {
00974 check( xsh_add_product_image( frame_backg, frameset, parameters,
00975 RECIPE_ID, instrument,NULL));
00976 }
00977 if(crhm_frame != NULL) {
00978 sprintf(tag,"%s_ON",rec_prefix);
00979 check( xsh_add_product_image( crhm_frame, frameset, parameters,
00980 RECIPE_ID, instrument,tag));
00981
00982 }
00983
00984 if(qc_sky_frame != NULL) {
00985 check( xsh_add_product_table(qc_sky_frame, frameset,parameters,
00986 RECIPE_ID, instrument,NULL));
00987
00988 }
00989
00990
00991 if(fluxcal_rect_2D_frame != NULL) {
00992 check( xsh_add_product_image( fluxcal_rect_2D_frame, frameset, parameters,
00993 RECIPE_ID, instrument,NULL));
00994
00995 check( xsh_add_product_image( fluxcal_rect_1D_frame, frameset, parameters,
00996 RECIPE_ID, instrument,NULL));
00997
00998 check( xsh_add_product_spectrum( fluxcal_2D_frame, frameset, parameters,
00999 RECIPE_ID, instrument));
01000 check( xsh_add_product_spectrum( fluxcal_1D_frame, frameset, parameters,
01001 RECIPE_ID, instrument));
01002 if ( do_optextract){
01003
01004 check( xsh_add_product_image( fluxcal_rect_opt1D_frame, frameset,
01005 parameters, RECIPE_ID, instrument,
01006 NULL));
01007
01008 check( xsh_add_product_spectrum( fluxcal_merg_opt1D_frame, frameset,
01009 parameters, RECIPE_ID, instrument));
01010
01011 }
01012 }
01013
01014 cleanup:
01015
01016 xsh_end( RECIPE_ID, frameset, parameters );
01017
01018 XSH_FREE( rec_prefix);
01019 XSH_FREE( backg_par);
01020 XSH_FREE(ipol_bp);
01021 XSH_FREE( crh_single_par);
01022 XSH_FREE( rectify_par);
01023 XSH_FREE( stack_par);
01024 XSH_FREE( sky_par);
01025 XSH_FREE( loc_obj_par);
01026 XSH_FREE( opt_extract_par);
01027 xsh_instrument_free(&instrument);
01028
01029 xsh_free_frameset(&raws);
01030 xsh_free_frameset(&calib);
01031 xsh_free_frame( &crhm_frame);
01032 xsh_free_frame( &qc_sky_frame);
01033 xsh_free_frameset(&crh_clean_lacosmic);
01034
01035 if(do_compute_map) {
01036 xsh_free_frame( &wavemap_frame);
01037 xsh_free_frame( &slitmap_frame);
01038 }
01039
01040 xsh_free_frameset(&sub_bias_set);
01041 xsh_free_frameset(&sub_dark_set);
01042
01043
01044
01045 xsh_free_frame(&rmbkg);
01046 xsh_free_frame(&div_frame);
01047 xsh_free_frame(&sub_sky_frame);
01048
01049 xsh_free_frame(&bpmap);
01050 xsh_free_frame(&sub_sky2_frame);
01051 xsh_free_frame(&sky_frame);
01052 xsh_free_frame(&sky_frame_eso);
01053 xsh_free_frame(&sky_frame_ima);
01054
01055 xsh_free_frame(&rect_frame) ;
01056 xsh_free_frame(&rect2_frame_eso) ;
01057 xsh_free_frame(&rect2_frame_tab) ;
01058
01059 xsh_free_frame(&rect2_sky_frame) ;
01060 xsh_free_frame(&rect2_sky_frame_eso) ;
01061 xsh_free_frame(&rect2_sky_frame_tab) ;
01062
01063
01064 xsh_free_frame( &orderext1d_frame);
01065 xsh_free_frame( &orderoxt1d_frame);
01066 xsh_free_frame( &mergeext1d_frame);
01067 xsh_free_frame( &mergeoxt1d_frame);
01068
01069 xsh_free_frame(&loc_table_frame) ;
01070 xsh_free_frame( &clean_frame);
01071 xsh_free_frame( &clean_obj);
01072 xsh_free_frame( &ext_frame);
01073 xsh_free_frame( &ext_frame_eso);
01074
01075 xsh_free_frame(&res_1D_frame) ;
01076 xsh_free_frame(&res_2D_frame) ;
01077
01078 xsh_free_frame(&res_2D_sky_frame) ;
01079
01080 xsh_free_frame(&fluxcal_rect_1D_frame) ;
01081 xsh_free_frame(&fluxcal_rect_2D_frame) ;
01082 xsh_free_frame(&fluxcal_1D_frame) ;
01083 xsh_free_frame(&fluxcal_2D_frame) ;
01084
01085
01086 xsh_free_frame(&rect2_frame) ;
01087 xsh_free_frame(&grid_backg) ;
01088 xsh_free_frame(&frame_backg) ;
01089 xsh_free_propertylist(&plist);
01090 xsh_free_frame( &qc_subex_frame);
01091 xsh_free_frame(&qc_s2ddiv1d_frame);
01092 xsh_free_frame(&qc_model_frame);
01093 xsh_free_frame(&qc_weight_frame);
01094
01095 return CPL_ERROR_NONE;
01096 }
01097