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