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, 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 if rectify-conserve-flux=TRUE or do_wavemap=TRUE and 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 - PREFIX_ON_arm bias (dark) subtracted sci frame\n\
00131 - PREFIX_SUB_BACK_SLIT_arm sci frame bias, (dark), inter-order bkg subtracted\n\
00132 - PREFIX_NOCRH_arm sci frame corrected from cosmic ray hits\n\
00133 - [OPTIONAL,-compute-map=TRUE] PREFIX_WAVE_MAP_arm, wave map image\n\
00134 - [OPTIONAL,-compute-map=TRUE] PREFIX_SLIT_MAP_arm, slit map image\n\
00135 - PREFIX_DIVFF_arm as PREFIX_SUB_BACK_SLIT_arm, flat fielded\n\
00136 - PREFIX_SUB_SKY_arm, as PREFIX_DIVFF_arm, sky subtracted\n\
00137 - PREFIX_SKY_arm, 2D sky frame\n\
00138 - PREFIX_SKY_ORD1D_arm, 1D sky image (order-by-order)\n\
00139 - PREFIX_SCI_SLIT_STARE_BACK_SLIT_arm, inter-order background image\n\
00140 - OBJECT_SLIT_STARE_arm, sci frame in PRE format\n\
00141 - where PREFIX is SCI, FLUX, TELL if input raw DPR.TYPE contains OBJECT or FLUX or TELLURIC\n\
00142 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUXCAL2D_arm (2 dimension)\n\
00143 - [OPTIONAL, if response and atm ext are provided] PREFIX_FLUXCAL1D_arm (1 dimension)";
00144
00145
00146
00147
00148
00157
00158
00159 int cpl_plugin_get_info(cpl_pluginlist *list) {
00160 cpl_recipe *recipe = NULL;
00161 cpl_plugin *plugin = NULL;
00162
00163 recipe = cpl_calloc(1, sizeof(*recipe));
00164 if ( recipe == NULL ){
00165 return -1;
00166 }
00167
00168 plugin = &recipe->interface ;
00169
00170 cpl_plugin_init(plugin,
00171 CPL_PLUGIN_API,
00172 XSH_BINARY_VERSION,
00173 CPL_PLUGIN_TYPE_RECIPE,
00174 RECIPE_ID,
00175 xsh_scired_slit_stare_description_short,
00176 xsh_scired_slit_stare_description,
00177 RECIPE_AUTHOR,
00178 RECIPE_CONTACT,
00179 xsh_get_license(),
00180 xsh_scired_slit_stare_create,
00181 xsh_scired_slit_stare_exec,
00182 xsh_scired_slit_stare_destroy);
00183
00184 cpl_pluginlist_append(list, plugin);
00185
00186 return (cpl_error_get_code() != CPL_ERROR_NONE);
00187 }
00188
00189
00199
00200
00201 static int xsh_scired_slit_stare_create(cpl_plugin *plugin){
00202 cpl_recipe *recipe = NULL;
00203 cpl_parameter* p=NULL;
00204
00205 xsh_clipping_param crh_clip_param = {5.0, 5, 0.7, 0, 0.3};
00206
00207 xsh_remove_crh_single_param crh_single = { 0.1, 5, 2.0, 4} ;
00208 xsh_rectify_param rectify = { "default",
00209 CPL_KERNEL_DEFAULT,
00210 4,
00211 -1.0,
00212 -1.0,
00213 1,
00214 0, 0. };
00215
00216 xsh_subtract_sky_single_param sky_single = {3000, 3000,7,20, 5., -1, -1,
00217 BSPLINE_METHOD, FINE,7, 1.5,
00218 0.0, 0.0,
00219 0.0, 0.0} ;
00220
00221
00222 xsh_localize_obj_param loc_obj =
00223 {10, 0.1, 0, 0, LOC_MANUAL_METHOD, 0, 2.0,3,3, FALSE};
00224 xsh_extract_param extract_par =
00225 { LOCALIZATION_METHOD };
00226
00227 xsh_opt_extract_param opt_extract_par =
00228 { 5, 10, 10, 0.01, 10.0, 1., 2, 2, GAUSS_METHOD };
00229 xsh_stack_param stack_param = {"median",5.,5.,5};
00230
00231 opt_extract_par.oversample = 5;
00232 opt_extract_par.box_hsize = 10;
00233 opt_extract_par.chunk_size = 50;
00234 opt_extract_par.lambda_step = 0.02;
00235 opt_extract_par.clip_kappa = 3;
00236 opt_extract_par.clip_frac = 0.4;
00237 opt_extract_par.clip_niter = 2;
00238 opt_extract_par.niter = 1;
00239 opt_extract_par.method = GAUSS_METHOD;
00240
00241 xsh_init();
00242
00243
00244 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00245
00246
00247 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00248 CPL_ERROR_TYPE_MISMATCH,
00249 "Plugin is not a recipe");
00250
00251 recipe = (cpl_recipe *)plugin;
00252
00253
00254 recipe->parameters = cpl_parameterlist_new();
00255 assure( recipe->parameters != NULL,
00256 CPL_ERROR_ILLEGAL_OUTPUT,
00257 "Memory allocation failed!");
00258
00259
00260 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00261
00262 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00263 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00264
00265 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00266 crh_clip_param));
00267
00268
00269 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00270
00271
00272 check(p=xsh_parameters_find(recipe->parameters,RECIPE_ID,"background-method"));
00273 cpl_parameter_set_default_string(p,"poly");
00274
00275
00276
00277 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00278 crh_single )) ;
00279
00280
00281 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00282 rectify )) ;
00283
00284
00285 check(xsh_parameters_localize_obj_create(RECIPE_ID,recipe->parameters,
00286 loc_obj )) ;
00287
00288
00289 check(xsh_parameters_subtract_sky_single_create(RECIPE_ID,recipe->parameters,
00290 sky_single )) ;
00291
00292
00293 check(xsh_parameters_extract_create(RECIPE_ID,
00294 recipe->parameters,
00295 extract_par,LOCALIZATION_METHOD )) ;
00296
00297
00298 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00299 "do-optextract", FALSE,
00300 "TRUE if we do the optimal extraction"));
00301
00302 check( xsh_parameters_opt_extract_create( RECIPE_ID, recipe->parameters,
00303 opt_extract_par));
00304
00305 check(xsh_parameters_merge_ord_create(RECIPE_ID,
00306 recipe->parameters,
00307 WEIGHTED_MERGE_METHOD)) ;
00308
00309 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00310 "do-flatfield", TRUE,
00311 "TRUE if we do the flatfielding"));
00312
00313 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00314 "compute-map", TRUE,
00315 "if TRUE recompute (wave and slit) maps from the dispersion solution. Use FALSE only if you are sure to have wave and slit maps better than what this recipe may compute. If sky-subtract is set to TRUE this must be set to TRUE."));
00316
00317 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00318 "trace-obj", FALSE,
00319 "if TRUE compute object position trace via Gaussian fit."));
00320
00321
00322 cleanup:
00323 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00324 xsh_error_dump(CPL_MSG_ERROR);
00325 return 1;
00326 }
00327 else {
00328 return 0;
00329 }
00330 }
00331
00332
00338
00339
00340 static int xsh_scired_slit_stare_exec(cpl_plugin *plugin) {
00341 cpl_recipe *recipe = NULL;
00342
00343
00344
00345 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00346
00347
00348 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00349 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00350
00351 recipe = (cpl_recipe *)plugin;
00352
00353
00354 xsh_scired_slit_stare(recipe->parameters, recipe->frames);
00355
00356 cleanup:
00357 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00358 xsh_error_dump(CPL_MSG_ERROR);
00359 xsh_error_reset();
00360 return 1;
00361 }
00362 else {
00363 return 0;
00364 }
00365 }
00366
00367
00373
00374 static int xsh_scired_slit_stare_destroy(cpl_plugin *plugin)
00375 {
00376 cpl_recipe *recipe = NULL;
00377
00378
00379 xsh_error_reset();
00380
00381 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00382
00383
00384 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00385 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00386
00387 recipe = (cpl_recipe *)plugin;
00388
00389 xsh_free_parameterlist(&recipe->parameters);
00390
00391 cleanup:
00392 if (cpl_error_get_code() != CPL_ERROR_NONE)
00393 {
00394 return 1;
00395 }
00396 else
00397 {
00398 return 0;
00399 }
00400 }
00401
00402 static cpl_error_code
00403 xsh_params_set_defaults(cpl_parameterlist* pars,
00404 const char* rec_id,
00405 xsh_instrument* inst,
00406 xsh_rectify_param * rectify_par,
00407 xsh_background_param* backg)
00408 {
00409 cpl_parameter* p=NULL;
00410 check(p=xsh_parameters_find(pars,RECIPE_ID,"background-radius-x"));
00411 if(cpl_parameter_get_int(p) <= 0) {
00412 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00413 backg->radius_x=1;
00414 } else {
00415 backg->radius_x=2;
00416 }
00417 }
00418 check(xsh_rectify_params_set_defaults(pars,rec_id,inst,rectify_par));
00419 cleanup:
00420
00421 return cpl_error_get_code();
00422 }
00423
00424 static cpl_error_code
00425 xsh_params_monitor(xsh_background_param* backg,
00426 xsh_rectify_param * rectify_par,
00427 xsh_localize_obj_param * loc_obj_par,
00428 xsh_opt_extract_param *opt_extract_par,
00429 int sub_sky_nbkpts1,
00430 int sub_sky_nbkpts2)
00431 {
00432
00433
00434 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00435 backg->sampley,backg->radius_y,backg->smooth_y);
00436
00437 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00438 backg->radius_x,backg->smooth_x);
00439
00440 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00441 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00442 rectify_par->rectif_bin_space);
00443
00444 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
00445 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
00446
00447 xsh_msg_dbg_low("opt extract params: chunk_size=%d lambda_step=%g box_hsize=%d",
00448 opt_extract_par->chunk_size,opt_extract_par->lambda_step,
00449 opt_extract_par->box_hsize);
00450
00451 xsh_msg_dbg_low("sky params: nbkpts1=%d nbkpts2=%d",
00452 sub_sky_nbkpts1,sub_sky_nbkpts2);
00453
00454 return cpl_error_get_code();
00455
00456 }
00457
00458
00466
00467 static cpl_error_code
00468 xsh_scired_slit_stare(cpl_parameterlist* parameters,
00469 cpl_frameset* frameset)
00470 {
00471 const char* recipe_tags[3] = {XSH_OBJECT_SLIT_STARE,
00472 XSH_STD_TELL_SLIT_STARE,
00473 XSH_STD_FLUX_SLIT_STARE};
00474 int recipe_tags_size = 3;
00475
00476
00477 cpl_frameset *raws = NULL;
00478 cpl_frameset *calib = NULL;
00479
00480
00481 cpl_frame *bpmap = NULL;
00482 cpl_frame *master_bias = NULL;
00483 cpl_frame *master_dark = NULL;
00484 cpl_frame *master_flat = NULL;
00485 cpl_frame *order_tab_edges = NULL;
00486 cpl_frame *wave_tab = NULL ;
00487 cpl_frame *model_config_frame = NULL ;
00488 cpl_frame *wavemap_frame = NULL ;
00489 cpl_frame *slitmap_frame = NULL ;
00490 cpl_frame *disp_tab_frame = NULL;
00491 cpl_frame *spectralformat_frame = NULL ;
00492 cpl_frame *tellmask_frame = NULL;
00493
00494 xsh_clipping_param* crh_clipping_par = NULL;
00495 xsh_background_param* backg_par = NULL;
00496 xsh_remove_crh_single_param * crh_single_par = NULL ;
00497 xsh_rectify_param * rectify_par = NULL ;
00498 xsh_localize_obj_param * loc_obj_par = NULL ;
00499 xsh_stack_param* stack_par=NULL;
00500 int sub_sky_nbkpts1 = SUBTRACT_SKY_SINGLE_NBKPTS ;
00503 int sub_sky_nbkpts2 = SUBTRACT_SKY_SINGLE_NBKPTS ;
00506 int do_sub_sky = FALSE;
00507 int recipe_use_model = 0;
00508 int do_optextract = 0;
00509 int do_flatfield = 0;
00510 int do_compute_map = 0;
00511 int do_trace_obj = 0;
00512
00513 xsh_merge_param *merge_par = NULL;
00514 xsh_extract_param *extract_par = NULL ;
00515 xsh_opt_extract_param *opt_extract_par = NULL;
00516 xsh_subtract_sky_single_param *sky_par = NULL;
00517
00518 xsh_instrument* instrument = NULL;
00519 int nb_raw_frames ;
00520 char rec_name[80];
00521
00522
00523 #if 0
00524 cpl_frameset * on = NULL, * off = NULL ;
00525 #endif
00526 cpl_frame * crhm_frame = NULL ;
00527 cpl_frame * rmbias = NULL;
00528 cpl_frame * rmdark = NULL;
00529 cpl_frame * rmbkg = NULL ;
00530 cpl_frame * div_frame = NULL ;
00531 cpl_frame * sub_sky_frame = NULL ;
00532 cpl_frame * sub_sky2_frame = NULL ;
00534 cpl_frame * rect_frame = NULL ;
00535 cpl_frame * loc_table_frame = NULL ;
00536 cpl_frame * clean_frame = NULL ;
00537 cpl_frame * rect2_frame = NULL ;
00538 cpl_frame * rect2_frame_eso = NULL ;
00539 cpl_frame * rect2_frame_tab = NULL ;
00540
00541
00542 cpl_frame * rect2_sky_frame = NULL ;
00543 cpl_frame * rect2_sky_frame_eso = NULL ;
00544 cpl_frame * rect2_sky_frame_tab = NULL ;
00545
00546
00547 cpl_frame * sky_frame = NULL ;
00548 cpl_frame * sky_frame_eso = NULL ;
00549 cpl_frame * res_1D_frame = NULL ;
00550 cpl_frame * res_2D_frame = NULL ;
00551 cpl_frame * res_2D_sky_frame = NULL ;
00552
00553 cpl_frame * fluxcal_rect_1D_frame = NULL ;
00554 cpl_frame * fluxcal_rect_2D_frame = NULL ;
00555 cpl_frame * fluxcal_1D_frame = NULL ;
00556 cpl_frame * fluxcal_2D_frame = NULL ;
00557
00558 cpl_frame * ext_frame = NULL ;
00559 cpl_frame * ext_frame_eso = NULL ;
00560
00561 cpl_frame *orderext1d_frame = NULL;
00562 cpl_frame *orderoxt1d_frame = NULL;
00563 cpl_frame *orderoxt1d_eso_frame = NULL;
00564
00565 cpl_frame *mergeext1d_frame = NULL;
00566 cpl_frame *mergeoxt1d_frame = NULL;
00567 cpl_frame *fluxcal_rect_opt1D_frame = NULL;
00568 cpl_frame *fluxcal_merg_opt1D_frame = NULL;
00569
00570 cpl_frame* grid_backg=NULL;
00571 cpl_frame* frame_backg=NULL;
00572 cpl_frame* sky_frame_ima=NULL;
00573 char prefix[80];
00574 char fname[80];
00575 char tag[80];
00576
00577 const char* ftag=NULL;
00578
00579 cpl_frame* single_frame_sky_sub_tab_frame=NULL;
00580
00581
00582
00583 cpl_frame* clean_obj=NULL;
00584 char *rec_prefix = NULL;
00585 char sky_prefix[80];
00586 cpl_frame* sky_list_frame=NULL;
00587 cpl_frame* qc_sky_frame=NULL;
00588 cpl_propertylist* plist=NULL;
00589 cpl_frame* qc_obj_trace_ord=NULL;
00590 cpl_frame* qc_obj_trace_mer=NULL;
00591 int hsize=0;
00592 int naxis2=0;
00593 const char* name=NULL;
00594 xsh_pre* pre_sci=NULL;
00595
00596 cpl_frame* response_frame=NULL;
00597 cpl_frame* frm_atmext=NULL;
00598 int pre_overscan_corr=0;
00599 cpl_frame *qc_subex_frame = NULL;
00600 cpl_frame *qc_s2ddiv1d_frame = NULL;
00601 cpl_frame *qc_model_frame = NULL;
00602 cpl_frame *qc_weight_frame = NULL;
00603
00604
00605
00606
00607 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00608 recipe_tags, recipe_tags_size,
00609 RECIPE_ID, XSH_BINARY_VERSION,
00610 xsh_scired_slit_stare_description_short ) ) ;
00611
00612
00613
00614
00615
00616 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00617 if(instrument->arm == XSH_ARM_NIR) {
00618 xsh_instrument_nir_corr_if_JH(raws,instrument);
00619 }
00620
00621
00622
00623
00624 check( nb_raw_frames = cpl_frameset_get_size( raws ) ) ;
00625 xsh_msg_dbg_low("nb_raw_frames=%d",nb_raw_frames);
00626
00627
00628
00629
00630 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00631 "pre-overscan-corr"));
00632
00633 check(xsh_slit_stare_get_calibs(calib,instrument, &spectralformat_frame,
00634 &master_bias,&master_dark,&master_flat,
00635 &order_tab_edges,&model_config_frame,
00636 &wave_tab,&sky_list_frame,&qc_sky_frame,
00637 &bpmap,&single_frame_sky_sub_tab_frame,
00638 &wavemap_frame,&slitmap_frame,RECIPE_ID,
00639 &recipe_use_model,pre_overscan_corr));
00640
00641
00642 if((response_frame=xsh_find_frame_with_tag(calib,XSH_MRESPONSE_MERGE1D_SLIT,
00643 instrument)) == NULL ) {
00644 check(response_frame=xsh_find_frame_with_tag(calib,XSH_RESPONSE_MERGE1D_SLIT,
00645 instrument));
00646 }
00647 if(response_frame != NULL) {
00648 frm_atmext=xsh_find_frame_with_tag(calib,XSH_ATMOS_EXT,instrument);
00649 if(frm_atmext==NULL) {
00650 xsh_msg_error("Provide atmospheric extinction frame");
00651 return CPL_ERROR_DATA_NOT_FOUND;
00652 }
00653 }
00654
00655 tellmask_frame = xsh_find_frame_with_tag(calib,XSH_TELL_MASK, instrument);
00656
00657
00658
00659 check(xsh_slit_stare_get_params(parameters,RECIPE_ID, &pre_overscan_corr,
00660 &crh_clipping_par,&backg_par,&loc_obj_par,
00661 &rectify_par,&crh_single_par,&sub_sky_nbkpts1,
00662 &do_flatfield,&sub_sky_nbkpts2,&sky_par,
00663 &extract_par,&do_optextract,&merge_par,
00664 &opt_extract_par,&do_trace_obj));
00665 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00666 check(xsh_params_set_defaults(parameters,RECIPE_ID,instrument,rectify_par,
00667 backg_par));
00668
00669
00670 check( xsh_parameters_dosky_domap_get( RECIPE_ID, parameters,
00671 wavemap_frame,slitmap_frame,
00672 &do_sub_sky,&do_compute_map));
00673
00674
00675 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR) {
00676 check(xsh_stare_params_bin_scale(raws,backg_par,
00677 opt_extract_par,
00678 &sub_sky_nbkpts1,&sub_sky_nbkpts2));
00679 }
00680
00681
00682 if ( rectify_par->conserve_flux){
00683 check_msg(disp_tab_frame = xsh_find_disp_tab( calib, instrument),
00684 "rectify-conserve-flux=TRUE, you must give a DISP_TAB_ARM input");
00685 }
00686 if ( do_compute_map && recipe_use_model==FALSE){
00687 check_msg(disp_tab_frame = xsh_find_disp_tab( calib, instrument),
00688 "compute-map=TRUE, physmodel mode, you must give a DISP_TAB_ARM input");
00689 }
00690 check(xsh_params_monitor(backg_par,rectify_par,loc_obj_par,opt_extract_par,
00691 sub_sky_nbkpts1,sub_sky_nbkpts2));
00692
00693
00694
00695
00696
00697 check(xsh_prepare(raws, bpmap, master_bias, XSH_OBJECT_SLIT_STARE,
00698 instrument,pre_overscan_corr));
00699 check( rec_prefix = xsh_set_recipe_file_prefix( raws,
00700 "xsh_scired_slit_stare"));
00701
00702
00703 ftag = XSH_GET_TAG_FROM_ARM( XSH_SLIT_STARE_REMOVE_CRH, instrument);
00704 check( crhm_frame = xsh_check_remove_crh_multiple( raws, ftag,
00705 stack_par,crh_clipping_par, instrument, NULL, NULL));
00706
00707 check(xsh_slit_stare_get_maps(calib,
00708 do_compute_map,recipe_use_model,rec_prefix,
00709 instrument,model_config_frame,crhm_frame,
00710 disp_tab_frame,order_tab_edges,
00711 &wavemap_frame, &slitmap_frame));
00712
00713 sprintf(prefix,"%s_",rec_prefix);
00714
00715
00716 check( rmbias = xsh_check_subtract_bias( crhm_frame, master_bias,
00717 instrument, prefix,
00718 pre_overscan_corr,0));
00719
00720
00721 check( rmdark = xsh_check_subtract_dark( rmbias, master_dark,
00722 instrument, rec_prefix));
00723
00724 if(strcmp(backg_par->method,"no") != 0) {
00725
00726 xsh_msg("Subtract inter-order background");
00727 check(rmbkg = xsh_subtract_background( rmdark,
00728 order_tab_edges,
00729 backg_par, instrument,rec_prefix,
00730 &grid_backg,&frame_backg,1));
00731
00732 } else {
00733 rmbkg = cpl_frame_duplicate( rmdark );
00734 pre_sci=xsh_pre_load(rmbkg,instrument);
00735 sprintf(tag,"%sNO_SUB_BACK_%s",prefix,
00736 xsh_instrument_arm_tostring(instrument));
00737 sprintf(fname,"%s.fits",tag);
00738 xsh_pre_save(pre_sci,fname,tag,1);
00739 cpl_frame_set_filename(rmbkg,fname);
00740 cpl_frame_set_tag(rmbkg,tag);
00741 }
00742
00743
00744
00745 xsh_slit_stare_correct_crh_and_sky(loc_obj_par,crh_single_par,rectify_par,
00746 do_sub_sky,rec_prefix,rmbkg,
00747 order_tab_edges, slitmap_frame,
00748 wavemap_frame,model_config_frame,
00749 single_frame_sky_sub_tab_frame,instrument,
00750 sub_sky_nbkpts1, sky_par,
00751 &sky_frame,&sky_frame_eso,&sky_frame_ima,
00752 wave_tab,disp_tab_frame,
00753 spectralformat_frame,nb_raw_frames,
00754 &loc_table_frame,&clean_frame,&clean_obj);
00755
00756
00757
00758 check( div_frame = xsh_check_divide_flat( do_flatfield, clean_obj,
00759 master_flat, instrument, rec_prefix));
00760
00761 check( sub_sky2_frame = xsh_check_subtract_sky_single( do_sub_sky, div_frame,
00762 order_tab_edges, slitmap_frame, wavemap_frame, loc_table_frame, NULL,
00763 single_frame_sky_sub_tab_frame, instrument, sub_sky_nbkpts2, sky_par,
00764 &sky_frame, &sky_frame_eso, &sky_frame_ima, rec_prefix));
00765
00766 xsh_msg( "Prepare S2D products" ) ;
00767 xsh_msg( "Rectify") ;
00768 sprintf(rec_name,"%s_%s_%s.fits",rec_prefix,XSH_ORDER2D,
00769 xsh_instrument_arm_tostring( instrument));
00770
00771 check( rect2_frame = xsh_rectify( sub_sky2_frame, order_tab_edges,
00772 wave_tab, model_config_frame, instrument,
00773 rectify_par,spectralformat_frame,
00774 disp_tab_frame,rec_name,
00775 &rect2_frame_eso,&rect2_frame_tab,
00776 rec_prefix));
00777
00778 if(sky_frame_ima) {
00779
00780 sprintf(sky_prefix,xsh_set_recipe_sky_file_prefix(rec_prefix));
00781 sprintf(rec_name,"%s_%s_%s.fits",sky_prefix,XSH_ORDER2D,
00782 xsh_instrument_arm_tostring( instrument));
00783
00784 check( rect2_sky_frame = xsh_rectify( sky_frame_ima, order_tab_edges,
00785 wave_tab, model_config_frame, instrument,
00786 rectify_par,spectralformat_frame,
00787 disp_tab_frame,rec_name,
00788 &rect2_sky_frame_eso,&rect2_sky_frame_tab,
00789 rec_prefix));
00790 }
00791
00792 xsh_msg( "Extract" ) ;
00793 check(ext_frame=xsh_extract(rect2_frame, loc_table_frame,
00794 instrument, extract_par,&ext_frame_eso,
00795 rec_prefix)) ;
00796
00797 xsh_msg( "Merge orders with 1D frame" ) ;
00798 check( res_1D_frame = xsh_merge_ord( ext_frame, instrument,
00799 merge_par,rec_prefix));
00800 check( xsh_mark_tell( res_1D_frame, tellmask_frame));
00801
00802 check(xsh_monitor_spectrum1D_flux(res_1D_frame,instrument));
00803
00804
00805 xsh_msg( "Calling xsh_merge_ord with 2D frame" ) ;
00806 if( rect2_sky_frame ) {
00807 check( res_2D_sky_frame = xsh_merge_ord( rect2_sky_frame, instrument,
00808 merge_par,sky_prefix));
00809 }
00810 check( res_2D_frame = xsh_merge_ord( rect2_frame, instrument,
00811 merge_par,rec_prefix));
00812
00813
00814
00815 xsh_msg("Prepare S1D products" ) ;
00816
00817 if ( do_optextract){
00818 xsh_msg( "Optimal extraction");
00819 check( xsh_opt_extract( sub_sky2_frame, order_tab_edges,
00820 wave_tab, model_config_frame, wavemap_frame,
00821 slitmap_frame, loc_table_frame,
00822 spectralformat_frame, master_flat, instrument,
00823 opt_extract_par, rec_prefix,
00824 &orderext1d_frame, &orderoxt1d_frame,
00825 &orderoxt1d_eso_frame,
00826 &qc_subex_frame,
00827 &qc_s2ddiv1d_frame,
00828 &qc_model_frame,
00829 &qc_weight_frame));
00830
00831 check( mergeext1d_frame = xsh_merge_ord( orderext1d_frame, instrument,
00832 merge_par,rec_prefix));
00833 check( mergeoxt1d_frame = xsh_merge_ord( orderoxt1d_frame, instrument,
00834 merge_par,rec_prefix));
00835
00836 check( xsh_mark_tell( mergeext1d_frame, tellmask_frame));
00837 check( xsh_mark_tell( mergeoxt1d_frame, tellmask_frame));
00838 }
00839
00840 if(response_frame != NULL && frm_atmext != NULL) {
00841
00842 check(xsh_flux_calibrate(rect2_frame_eso,ext_frame_eso,frm_atmext,
00843 response_frame,merge_par,instrument,rec_prefix,
00844 &fluxcal_rect_2D_frame,&fluxcal_rect_1D_frame,
00845 &fluxcal_2D_frame,&fluxcal_1D_frame));
00846
00847 if ( do_optextract){
00848
00849 check(xsh_flux_calibrate1D(orderoxt1d_eso_frame,frm_atmext,
00850 response_frame,merge_par,instrument,rec_prefix,
00851 &fluxcal_rect_opt1D_frame,
00852 &fluxcal_merg_opt1D_frame));
00853
00854 }
00855
00856 }
00857
00858
00859
00860
00861
00862
00863
00864
00865 if( do_trace_obj) {
00866 name=cpl_frame_get_filename(res_2D_frame);
00867 plist=cpl_propertylist_load(name,0);
00868 naxis2=xsh_pfits_get_naxis2(plist);
00869 xsh_free_propertylist(&plist);
00870 hsize=naxis2/4;
00871 check(qc_obj_trace_ord=xsh_frame_image_ext_qc_trace_window(rect2_frame_eso,
00872 instrument,
00873 rec_prefix,
00874 hsize,0));
00875
00876 check(qc_obj_trace_mer=xsh_frame_image_qc_trace_window(res_2D_frame,
00877 instrument,
00878 rec_prefix,
00879 hsize,0));
00880 }
00881 if(model_config_frame) {
00882 xsh_compute_resampling_accuracy(wavemap_frame,slitmap_frame,order_tab_edges,model_config_frame,res_2D_frame,instrument);
00883 }
00884
00885
00886 xsh_msg( "Save products" ) ;
00887
00888
00889
00890
00891
00892
00893 check( xsh_add_product_image(rect2_frame_eso, frameset, parameters,
00894 RECIPE_ID, instrument,NULL));
00895
00896 check( xsh_add_product_image(ext_frame_eso, frameset, parameters,
00897 RECIPE_ID, instrument,NULL));
00898
00899 check( xsh_add_product_spectrum( res_2D_frame, frameset, parameters,
00900 RECIPE_ID, instrument));
00901
00902
00903 check( xsh_add_product_spectrum( res_1D_frame, frameset, parameters,
00904 RECIPE_ID, instrument));
00905
00906 if(res_2D_sky_frame) {
00907 check( xsh_add_product_spectrum( res_2D_sky_frame, frameset, parameters,
00908 RECIPE_ID, instrument));
00909 }
00910 sprintf(tag,"%s_SUB_BIAS",rec_prefix);
00911 check( xsh_add_product_image( rmbias, frameset, parameters,
00912 RECIPE_ID, instrument,tag));
00913
00914 check( xsh_add_product_image( rmbkg, frameset, parameters,
00915 RECIPE_ID, instrument,NULL));
00916
00917 if ( do_sub_sky == 1 && nb_raw_frames == 1 && crh_single_par->nb_iter > 0){
00918
00919 check( xsh_add_product_image( clean_frame, frameset, parameters,
00920 RECIPE_ID, instrument,NULL));
00921
00922 }
00923 if (do_compute_map){
00924
00925
00926
00927 check(xsh_add_product_image( wavemap_frame, frameset,
00928 parameters, RECIPE_ID, instrument, NULL));
00929
00930
00931
00932 check(xsh_add_product_image( slitmap_frame, frameset,
00933 parameters, RECIPE_ID, instrument,NULL));
00934 }
00935 if(do_flatfield) {
00936
00937 check( xsh_add_product_image( div_frame, frameset, parameters,
00938 RECIPE_ID, instrument,NULL));
00939 }
00940
00941 if ( do_optextract){
00942 check( xsh_add_product_table( orderext1d_frame, frameset,parameters,
00943 RECIPE_ID, instrument,NULL));
00944 check( xsh_add_product_table( orderoxt1d_frame, frameset,parameters,
00945 RECIPE_ID, instrument,NULL));
00946 check( xsh_add_product_spectrum( mergeext1d_frame, frameset, parameters,
00947 RECIPE_ID, instrument));
00948 check( xsh_add_product_spectrum( mergeoxt1d_frame, frameset,
00949 parameters, RECIPE_ID, instrument));
00950 check( xsh_add_product_image( qc_subex_frame, frameset, parameters,
00951 RECIPE_ID, instrument,
00952 cpl_frame_get_tag(qc_subex_frame)));
00953 check( xsh_add_product_image( qc_s2ddiv1d_frame, frameset, parameters,
00954 RECIPE_ID, instrument,
00955 cpl_frame_get_tag(qc_s2ddiv1d_frame)));
00956 check( xsh_add_product_image( qc_model_frame, frameset, parameters,
00957 RECIPE_ID, instrument,
00958 cpl_frame_get_tag(qc_model_frame)));
00959 check( xsh_add_product_image( qc_weight_frame, frameset, parameters,
00960 RECIPE_ID, instrument,
00961 cpl_frame_get_tag(qc_weight_frame)));
00962 }
00963 if ( do_sub_sky == 1 ) {
00964 check( xsh_add_product_pre( sub_sky2_frame, frameset, parameters,
00965 RECIPE_ID, instrument));
00966 check( xsh_add_product_image( sky_frame_ima, frameset, parameters,
00967 RECIPE_ID, instrument,NULL));
00968 check( xsh_add_product_image( sky_frame_eso, frameset,
00969 parameters, RECIPE_ID, instrument,NULL));
00970 }
00971 if(frame_backg != NULL) {
00972 check( xsh_add_product_image( frame_backg, frameset, parameters,
00973 RECIPE_ID, instrument,NULL));
00974 }
00975 if(crhm_frame != NULL) {
00976 sprintf(tag,"%s_ON",rec_prefix);
00977 check( xsh_add_product_image( crhm_frame, frameset, parameters,
00978 RECIPE_ID, instrument,tag));
00979
00980 }
00981
00982 if(qc_sky_frame != NULL) {
00983 check( xsh_add_product_table(qc_sky_frame, frameset,parameters,
00984 RECIPE_ID, instrument,NULL));
00985
00986 }
00987
00988 if(do_trace_obj) {
00989 check( xsh_add_product_table(qc_obj_trace_ord, frameset,
00990 parameters, RECIPE_ID, instrument,NULL));
00991
00992 check( xsh_add_product_table(qc_obj_trace_mer, frameset,
00993 parameters, RECIPE_ID, instrument,NULL));
00994 }
00995
00996 if(fluxcal_rect_2D_frame != NULL) {
00997 check( xsh_add_product_image( fluxcal_rect_2D_frame, frameset, parameters,
00998 RECIPE_ID, instrument,NULL));
00999
01000 check( xsh_add_product_image( fluxcal_rect_1D_frame, frameset, parameters,
01001 RECIPE_ID, instrument,NULL));
01002
01003 check( xsh_add_product_spectrum( fluxcal_2D_frame, frameset, parameters,
01004 RECIPE_ID, instrument));
01005 check( xsh_add_product_spectrum( fluxcal_1D_frame, frameset, parameters,
01006 RECIPE_ID, instrument));
01007 if ( do_optextract){
01008
01009 check( xsh_add_product_image( fluxcal_rect_opt1D_frame, frameset,
01010 parameters, RECIPE_ID, instrument,
01011 NULL));
01012
01013 check( xsh_add_product_spectrum( fluxcal_merg_opt1D_frame, frameset,
01014 parameters, RECIPE_ID, instrument));
01015
01016 }
01017 }
01018
01019 cleanup:
01020 xsh_end( RECIPE_ID, frameset, parameters );
01021 XSH_FREE( rec_prefix);
01022 XSH_FREE( crh_clipping_par);
01023 XSH_FREE( backg_par);
01024 XSH_FREE( crh_single_par);
01025 XSH_FREE( rectify_par);
01026 XSH_FREE( stack_par);
01027 XSH_FREE( sky_par);
01028 XSH_FREE( loc_obj_par);
01029 XSH_FREE( merge_par);
01030 XSH_FREE( extract_par);
01031 XSH_FREE( opt_extract_par);
01032 xsh_instrument_free(&instrument);
01033
01034 xsh_free_frameset(&raws);
01035 xsh_free_frameset(&calib);
01036 xsh_free_frame( &crhm_frame);
01037 xsh_free_frame( &qc_sky_frame);
01038
01039
01040 if(do_compute_map) {
01041 xsh_free_frame( &wavemap_frame);
01042 xsh_free_frame( &slitmap_frame);
01043 }
01044 xsh_free_frame(&rmbias);
01045 xsh_free_frame(&rmdark);
01046 xsh_free_frame(&rmbkg);
01047 xsh_free_frame(&div_frame);
01048 xsh_free_frame(&sub_sky_frame);
01049
01050
01051 xsh_free_frame(&sub_sky2_frame);
01052 xsh_free_frame(&sky_frame);
01053 xsh_free_frame(&sky_frame_eso);
01054 xsh_free_frame(&sky_frame_ima);
01055
01056 xsh_free_frame(&rect_frame) ;
01057 xsh_free_frame(&rect2_frame_eso) ;
01058 xsh_free_frame(&rect2_frame_tab) ;
01059
01060 xsh_free_frame(&rect2_sky_frame) ;
01061 xsh_free_frame(&rect2_sky_frame_eso) ;
01062 xsh_free_frame(&rect2_sky_frame_tab) ;
01063
01064
01065
01066 xsh_free_frame( &orderext1d_frame);
01067 xsh_free_frame( &orderoxt1d_frame);
01068 xsh_free_frame( &mergeext1d_frame);
01069 xsh_free_frame( &mergeoxt1d_frame);
01070
01071 xsh_free_frame(&loc_table_frame) ;
01072 xsh_free_frame( &clean_frame);
01073 xsh_free_frame( &clean_obj);
01074 xsh_free_frame( &ext_frame);
01075 xsh_free_frame( &ext_frame_eso);
01076
01077 xsh_free_frame(&res_1D_frame) ;
01078 xsh_free_frame(&res_2D_frame) ;
01079
01080 xsh_free_frame(&res_2D_sky_frame) ;
01081
01082 xsh_free_frame(&fluxcal_rect_1D_frame) ;
01083 xsh_free_frame(&fluxcal_rect_2D_frame) ;
01084 xsh_free_frame(&fluxcal_1D_frame) ;
01085 xsh_free_frame(&fluxcal_2D_frame) ;
01086
01087 xsh_free_frame(&rect2_frame) ;
01088 xsh_free_frame(&grid_backg) ;
01089 xsh_free_frame(&frame_backg) ;
01090 xsh_free_propertylist(&plist);
01091 xsh_free_frame( &qc_subex_frame);
01092 xsh_free_frame(&qc_s2ddiv1d_frame);
01093 xsh_free_frame(&qc_model_frame);
01094 xsh_free_frame(&qc_weight_frame);
01095 return CPL_ERROR_NONE;
01096 }
01097