00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00039
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <xsh_error.h>
00051
00052 #include <xsh_utils.h>
00053 #include <xsh_msg.h>
00054
00055 #include <xsh_dfs.h>
00056 #include <xsh_pfits.h>
00057
00058 #include <xsh_drl.h>
00059 #include <xsh_drl_check.h>
00060 #include <xsh_utils_table.h>
00061 #include <xsh_model_utils.h>
00062 #include <xsh_utils_image.h>
00063 #include <xsh_utils_ifu.h>
00064 #include <xsh_blaze.h>
00065 #include <xsh_parameters.h>
00066 #include <xsh_model_arm_constants.h>
00067
00068 #include <cpl.h>
00069
00070
00071
00072
00073
00074
00075 #define RECIPE_ID "xsh_geom_ifu"
00076 #define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer"
00077 #define RECIPE_CONTACT "regis.haigron@obspm.fr"
00078
00079 #define XSH_GEOM_SLIT_BIN_SIZE_PIPE_UVB 0.16
00080 #define XSH_GEOM_SLIT_BIN_SIZE_PIPE_VIS 0.16
00081 #define XSH_GEOM_SLIT_BIN_SIZE_PIPE_NIR 0.2
00082 #define XSH_GEOM_WAVE_BIN_SIZE_PIPE_UVB 0.04
00083 #define XSH_GEOM_WAVE_BIN_SIZE_PIPE_VIS 0.04
00084 #define XSH_GEOM_WAVE_BIN_SIZE_PIPE_NIR 0.1
00085
00086
00087
00088
00089
00090
00091
00092 static cpl_error_code
00093 xsh_rectify_params_set_defaults_drl(cpl_parameterlist* pars,
00094 const char* rec_id,
00095 xsh_instrument* inst,
00096 xsh_rectify_param * rectify_par);
00097
00098 static int xsh_geom_ifu_create(cpl_plugin *);
00099 static int xsh_geom_ifu_exec(cpl_plugin *);
00100 static int xsh_geom_ifu_destroy(cpl_plugin *);
00101
00102
00103 static void xsh_geom_ifu(cpl_parameterlist *, cpl_frameset *);
00104
00105
00106
00107
00108 static char xsh_geom_ifu_description_short[] =
00109 "Produces the spatial geometry of the IFU pattern on the sky";
00110
00111 static char xsh_geom_ifu_description[] =
00112 "This recipe Produces the spatial geometry of the IFU pattern on the sky\n\
00113 Input Frames : \n\
00114 - A set of n science frames ( n == 1 or >=3, Tag = TRACE_arm)\n\
00115 - [UVB,VIS] A master bias frame (Tag = MASTER_BIAS_arm)\n\
00116 - [OPTIONAL] A master dark frame (Tag = MASTER_DARK_arm)\n\
00117 - A master flat frame (Tag = MASTER_FLAT_IFU_arm)\n\
00118 - An AFC corrected model order edges table for IFU frame (Tag = ORDER_TAB_AFC_IFU_arm)\n\
00119 - [PHYSMOD] An AFC corrected model cfg frame (Tag = XSH_MOD_CFG_OPT_AFC_arm)\n\
00120 - [POLY] An AFC corrected model wavesol frame (Tag = WAVE_TAB_AFC_arm)\n\
00121 - An AFC corrected dispersion solution frame (Tag = DISP_TAB_AFC_arm)\n\
00122 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
00123 Products : \n\
00124 - A set of shift offset tables (Tag = OFFSET_TAB_slitlet_IFU_arm)\n" ;
00125
00126
00127
00128
00129
00138
00139
00140 int cpl_plugin_get_info(cpl_pluginlist *list) {
00141 cpl_recipe *recipe = NULL;
00142 cpl_plugin *plugin = NULL;
00143
00144 recipe = cpl_calloc(1, sizeof(*recipe));
00145 if ( recipe == NULL ){
00146 return -1;
00147 }
00148
00149 plugin = &recipe->interface ;
00150
00151 cpl_plugin_init(plugin,
00152 CPL_PLUGIN_API,
00153 XSH_BINARY_VERSION,
00154 CPL_PLUGIN_TYPE_RECIPE,
00155 RECIPE_ID,
00156 xsh_geom_ifu_description_short,
00157 xsh_geom_ifu_description,
00158 RECIPE_AUTHOR,
00159 RECIPE_CONTACT,
00160 xsh_get_license(),
00161 xsh_geom_ifu_create,
00162 xsh_geom_ifu_exec,
00163 xsh_geom_ifu_destroy);
00164
00165 cpl_pluginlist_append(list, plugin);
00166
00167 return (cpl_error_get_code() != CPL_ERROR_NONE);
00168 }
00169
00170
00180
00181
00182 static int xsh_geom_ifu_create(cpl_plugin *plugin){
00183 cpl_recipe *recipe = NULL;
00184 cpl_parameter* p =NULL;
00185 char paramname[256];
00186 char recipename[256];
00187 xsh_clipping_param crh_clip_param = {5.0, 5, 0.7, 0, 0.3};
00188
00189 xsh_remove_crh_single_param crh_single = { 0.1, 5, 2.0, 4} ;
00190 xsh_rectify_param rectify = { "default",
00191 CPL_KERNEL_DEFAULT, 4,
00192 -1,
00193 -1,
00194 1, 0, 0};
00195 xsh_localize_ifu_param loc_ifu =
00196 { 3, 5, 2, 0.05, 0.95, 0.05, 0.95, 0.0, 0.0, 0, FALSE, 50};
00197
00198 xsh_stack_param stack_param = {"median",5.,5.,5};
00199
00200
00201 xsh_init();
00202
00203
00204 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00205
00206
00207 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00208 CPL_ERROR_TYPE_MISMATCH,
00209 "Plugin is not a recipe");
00210
00211 recipe = (cpl_recipe *)plugin;
00212
00213
00214 recipe->parameters = cpl_parameterlist_new();
00215 assure( recipe->parameters != NULL,
00216 CPL_ERROR_ILLEGAL_OUTPUT,
00217 "Memory allocation failed!");
00218
00219
00220 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00221 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00222 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00223
00224 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00225 crh_clip_param));
00226
00227
00228 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00229
00230
00231 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00232 crh_single )) ;
00233
00234
00235 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00236 rectify )) ;
00237
00238 check(xsh_parameters_merge_ord_create(RECIPE_ID,
00239 recipe->parameters,
00240 WEIGHTED_MERGE_METHOD)) ;
00241
00242
00243 check(xsh_parameters_localize_ifu_create(RECIPE_ID,recipe->parameters,
00244 loc_ifu)) ;
00245
00246 check( xsh_parameters_new_int( recipe->parameters, RECIPE_ID,
00247 "correctifu-niter", 3,
00248 "Number of iterations in computing the localization shifts of the IFU slitlets"));
00249 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00250 "correctifu-lambdaref", -1,
00251 "Reference wavelength where the reference localization on the slit is chosen"));
00252 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00253 "correctifu-lambdaref-hsize", 2.5,
00254 "HAlf size in nm to estimate reference wavelength"));
00255
00256 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00257 "do-cube", FALSE,
00258 "if TRUE creates a data cube"));
00259
00260 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00261 "compute-map", TRUE,
00262 "if TRUE recompute (wave and slit) maps from the dispersion solution."));
00263
00264 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00265 "check-afc", TRUE,
00266 "Input AFC corrected model/wave solution and science frame check."\
00267 "If TRUE the recipe verify that the input mode/wave solution is AFC corrected,"\
00268 " its INS.OPTIi.NAME is 'Pin_0.5 ', and its OBS.ID and OBS.TARG.NAME values"\
00269 " matches with the corresponding values of the science frame."));
00270
00271 sprintf(recipename,"xsh.%s",RECIPE_ID);
00272 sprintf(paramname,"%s.%s",recipename,"flat-method");
00273 check( p = cpl_parameter_new_enum( paramname,CPL_TYPE_STRING,
00274 "method adopted for flat:",
00275 recipename,"blaze",
00276 2,"master","blaze"));
00277
00278 check(cpl_parameter_set_alias( p,CPL_PARAMETER_MODE_CLI,
00279 "flat-method"));
00280 check(cpl_parameterlist_append( recipe->parameters, p));
00281
00282 cleanup:
00283 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00284 xsh_error_dump(CPL_MSG_ERROR);
00285 return 1;
00286 }
00287 else {
00288 return 0;
00289 }
00290 }
00291
00299
00300
00301 static cpl_error_code
00302 xsh_params_bin_scale(cpl_frameset* raws,
00303 xsh_background_param* backg)
00304 {
00305
00306 cpl_frame* frame=NULL;
00307 const char* name=NULL;
00308 cpl_propertylist* plist=NULL;
00309 int binx=0;
00310 int biny=0;
00311
00312 check(frame=cpl_frameset_get_first(raws));
00313 check(name=cpl_frame_get_filename(frame));
00314 check(plist=cpl_propertylist_load(name,0));
00315 check(binx=xsh_pfits_get_binx(plist));
00316 check(biny=xsh_pfits_get_biny(plist));
00317 xsh_free_propertylist(&plist);
00318
00319 if(biny>1) {
00320
00321
00322
00323
00324
00325
00326 backg->radius_y=backg->radius_y/biny;
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 }
00353
00354
00355 if(binx>1) {
00356
00357 backg->radius_x=backg->radius_x/binx;
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 }
00373
00374 cleanup:
00375 xsh_free_propertylist(&plist);
00376 return cpl_error_get_code();
00377
00378 }
00379
00380
00386
00387
00388 static int xsh_geom_ifu_exec(cpl_plugin *plugin) {
00389 cpl_recipe *recipe = NULL;
00390
00391
00392
00393 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00394
00395
00396 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00397 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00398
00399 recipe = (cpl_recipe *)plugin;
00400
00401
00402 xsh_geom_ifu(recipe->parameters, recipe->frames);
00403
00404 cleanup:
00405 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00406 xsh_error_dump(CPL_MSG_ERROR);
00407 xsh_error_reset();
00408 return 1;
00409 }
00410 else {
00411 return 0;
00412 }
00413 }
00414
00415
00421
00422 static int xsh_geom_ifu_destroy(cpl_plugin *plugin)
00423 {
00424 cpl_recipe *recipe = NULL;
00425
00426
00427 xsh_error_reset();
00428
00429 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00430
00431
00432 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00433 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00434
00435 recipe = (cpl_recipe *)plugin;
00436
00437 xsh_free_parameterlist(&recipe->parameters);
00438
00439 cleanup:
00440 if (cpl_error_get_code() != CPL_ERROR_NONE)
00441 {
00442 return 1;
00443 }
00444 else
00445 {
00446 return 0;
00447 }
00448 }
00449
00450 static cpl_error_code
00451 xsh_params_monitor(xsh_background_param* backg,
00452 xsh_rectify_param * rectify_par)
00453 {
00454
00455
00456 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d smooth_y=%d",
00457 backg->sampley,backg->radius_y,backg->smooth_y);
00458
00459 xsh_msg_dbg_low("bkg params: radius_x=%d smooth_x=%d",
00460 backg->radius_x,backg->smooth_x);
00461
00462 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00463 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00464 rectify_par->rectif_bin_space);
00465
00466 return cpl_error_get_code();
00467
00468 }
00469
00470 static cpl_error_code
00471 xsh_params_set_defaults(cpl_parameterlist* pars,
00472 xsh_instrument* inst,
00473 xsh_rectify_param * rectify_par,
00474 double* lambda_ref,
00475 xsh_background_param* backg)
00476 {
00477 cpl_parameter *p=NULL;
00478
00479 check(p=xsh_parameters_find(pars,RECIPE_ID,"correctifu-lambdaref"));
00480 if(cpl_parameter_get_double(p) <= 0) {
00481 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00482 *lambda_ref= XSH_CORRECTIFU_LAMBDAREF_PIPE_NIR;
00483 }
00484 else {
00485 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00486 *lambda_ref= XSH_CORRECTIFU_LAMBDAREF_PIPE_UVB;
00487 }
00488 else{
00489 *lambda_ref= XSH_CORRECTIFU_LAMBDAREF_PIPE_VIS;
00490 }
00491 }
00492 cpl_parameter_set_double(p, *lambda_ref);
00493 }
00494 check(p=xsh_parameters_find(pars,RECIPE_ID,"background-radius-x"));
00495 if(cpl_parameter_get_int(p) <= 0) {
00496 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00497 backg->radius_x=1;
00498 } else {
00499 backg->radius_x=2;
00500 }
00501 cpl_parameter_set_int(p,backg->radius_x);
00502 }
00503
00504 check(xsh_rectify_params_set_defaults_drl(pars,RECIPE_ID,inst,rectify_par));
00505
00506 cleanup:
00507 return cpl_error_get_code();
00508
00509 }
00510
00511
00512 static cpl_error_code
00513 xsh_rectify_params_set_defaults_drl(cpl_parameterlist* pars,
00514 const char* rec_id,
00515 xsh_instrument* inst,
00516 xsh_rectify_param * rectify_par)
00517 {
00518 cpl_parameter* p=NULL;
00519
00520 check(p=xsh_parameters_find(pars,rec_id,"rectify-bin-slit"));
00521 if(cpl_parameter_get_double(p) <= 0) {
00522 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00523 rectify_par->rectif_bin_space=XSH_GEOM_SLIT_BIN_SIZE_PIPE_UVB;
00524 cpl_parameter_set_double(p,XSH_GEOM_SLIT_BIN_SIZE_PIPE_UVB);
00525 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS){
00526 rectify_par->rectif_bin_space=XSH_GEOM_SLIT_BIN_SIZE_PIPE_VIS;
00527 cpl_parameter_set_double(p,XSH_GEOM_SLIT_BIN_SIZE_PIPE_VIS);
00528 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00529 rectify_par->rectif_bin_space=XSH_GEOM_SLIT_BIN_SIZE_PIPE_NIR;
00530 cpl_parameter_set_double(p,XSH_GEOM_SLIT_BIN_SIZE_PIPE_NIR);
00531 }
00532 }
00533 check(p=xsh_parameters_find(pars,rec_id,"rectify-bin-lambda"));
00534 if(cpl_parameter_get_double(p) <= 0) {
00535 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00536 rectify_par->rectif_bin_lambda=XSH_GEOM_WAVE_BIN_SIZE_PIPE_UVB;
00537 cpl_parameter_set_double(p,XSH_GEOM_WAVE_BIN_SIZE_PIPE_UVB);
00538 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS){
00539 rectify_par->rectif_bin_lambda=XSH_GEOM_WAVE_BIN_SIZE_PIPE_VIS;
00540 cpl_parameter_set_double(p,XSH_GEOM_WAVE_BIN_SIZE_PIPE_VIS);
00541 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00542 rectify_par->rectif_bin_lambda=XSH_GEOM_WAVE_BIN_SIZE_PIPE_NIR;
00543 cpl_parameter_set_double(p,XSH_GEOM_WAVE_BIN_SIZE_PIPE_NIR);
00544 }
00545 }
00546
00547 cleanup:
00548
00549 return cpl_error_get_code();
00550
00551 }
00552
00553
00561
00562 static void xsh_geom_ifu(cpl_parameterlist* parameters,
00563 cpl_frameset* frameset)
00564 {
00565 const char* recipe_tags[1] = {XSH_TRACE};
00566 int recipe_tags_size = 1;
00567
00568
00569 cpl_frameset *raws = NULL;
00570 cpl_frameset *calib = NULL;
00571
00572
00573
00574 cpl_frame *bpmap = NULL;
00575 cpl_frame *master_bias = NULL;
00576 cpl_frame *master_dark = NULL;
00577 cpl_frame *masterflat_frame = NULL;
00578 cpl_frame *order_tab_edges = NULL;
00579 cpl_frameset *wave_tabs_ifu = NULL ;
00580 cpl_frame *model_config_frame = NULL ;
00581 cpl_frame *wavemap_frame = NULL ;
00582 cpl_frame *spectralformat_frame = NULL ;
00583 cpl_frame *slitmap_frame = NULL ;
00584 cpl_frame *disp_tab_frame = NULL;
00585 cpl_frame *skymask_frame = NULL;
00586
00587
00588 xsh_clipping_param *crh_clipping_par = NULL;
00589 xsh_background_param *backg_par = NULL;
00590 xsh_remove_crh_single_param *crh_single_par = NULL ;
00591 xsh_rectify_param *rectify_par = NULL ;
00592 xsh_merge_param* merge_par = NULL;
00593 xsh_localize_ifu_param *loc_ifu_par = NULL ;
00594 xsh_stack_param* stack_par=NULL;
00595 char* flat_method = NULL;
00596 int do_computemap = 0, do_cube=0;
00597 int check_afc = TRUE;
00598 int recipe_use_model = FALSE;
00599 xsh_instrument* instrument = NULL;
00600 int nb_raw_frames;
00601
00602
00603 #if 0
00604 cpl_frameset * on = NULL, * off = NULL ;
00605 #endif
00606 cpl_frame * crhm_frame = NULL ;
00607 cpl_frame * rmbias = NULL;
00608 cpl_frame * rmdark = NULL;
00609 cpl_frame * rmbkg = NULL ;
00610 cpl_frame * blaze_frame = NULL;
00611 cpl_frame *div_frame = NULL ;
00612 cpl_frameset *rect_frameset = NULL ;
00613 cpl_frame * clean_frame = NULL ;
00615
00616 cpl_frame* grid_backg=NULL;
00617 cpl_frame* frame_backg=NULL;
00618 cpl_frameset* rect_frameset_eso=NULL;
00619 cpl_frameset* rect_frameset_tab=NULL;
00620 cpl_frameset *shiftifu_frameset=NULL;
00621 cpl_frameset *shiftifu_old_frameset = NULL;
00622 cpl_frameset *merge_frameset=NULL;
00623 cpl_frameset *locifu_frameset=NULL;
00624 cpl_frame *data_cube = NULL ;
00625
00626 const char* ftag=NULL;
00627 char tag[256];
00628 char *rec_prefix = NULL;
00629 char geom_prefix[256];
00630
00631 int pre_overscan_corr=0;
00632 int i, geom_niter = 1;
00633 double lambda_ref;
00634 double lambdaref_hsize;
00635
00636
00637
00638
00639 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00640 recipe_tags, recipe_tags_size,RECIPE_ID, XSH_BINARY_VERSION,
00641 xsh_geom_ifu_description_short));
00642
00643 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
00644 "Instrument NOT in IFU Mode");
00645
00646 check(xsh_recipe_params_check(parameters,instrument,RECIPE_ID));
00647
00648
00649 check( nb_raw_frames = cpl_frameset_get_size( raws));
00650 XSH_ASSURE_NOT_ILLEGAL( nb_raw_frames == 1 || nb_raw_frames >= 3 ) ;
00651
00652 check( rec_prefix = xsh_set_recipe_file_prefix(raws,"xsh_geom_ifu"));
00653
00654
00655
00656
00657 bpmap = xsh_find_master_bpmap(calib);
00658
00659
00660 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00661
00662 check( master_bias = xsh_find_master_bias(calib,instrument));
00663 }
00664 #if 0
00665
00666
00667 else {
00668
00669 xsh_msg( "Calling xsh_dfs_split_nir" ) ;
00670 check(xsh_dfs_split_nir(raws, &on, &off));
00671 xsh_msg( " Nb ON: %d, OFF: %d", cpl_frameset_get_size(on),
00672 cpl_frameset_get_size(off) ) ;
00673 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(on) >= 3);
00674 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(off) >= 3);
00675 }
00676 #endif
00677
00678 check( order_tab_edges = xsh_find_order_tab_edges( calib, instrument));
00679
00680
00681 if(NULL==(model_config_frame = xsh_find_frame_with_tag( calib,
00682 XSH_MOD_CFG_OPT_AFC,
00683 instrument))) {
00684
00685 wave_tabs_ifu = xsh_find_wave_tab_ifu( calib, instrument);
00686
00687 }
00688
00689
00690 if( (model_config_frame!=NULL) && (wave_tabs_ifu != NULL) ) {
00691
00692 xsh_msg_error("You cannot provide both a %s and a %s frame. Decide if you are in poly or physical model mode. We exit",
00693 XSH_WAVE_TAB_2D , XSH_MOD_CFG_TAB);
00694 goto cleanup;
00695 }
00696
00697 if((model_config_frame==NULL) && ( wave_tabs_ifu == NULL) ) {
00698 xsh_msg_error("You must provide either a %s or a %s frame",
00699 XSH_WAVE_TAB_2D, XSH_MOD_CFG_TAB);
00700 goto cleanup;
00701 }
00702
00703 if ( (master_dark = xsh_find_master_dark( calib, instrument ) ) == NULL ){
00704 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00705 xsh_error_reset();
00706 }
00707
00708 check( masterflat_frame = xsh_find_master_flat( calib, instrument));
00709 check(spectralformat_frame = xsh_find_spectral_format( calib, instrument ) ) ;
00710
00711
00712
00713
00714
00715 recipe_use_model = ( model_config_frame != NULL);
00716 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00717 check( crh_clipping_par = xsh_parameters_clipping_crh_get(RECIPE_ID,
00718 parameters));
00719 check( backg_par = xsh_parameters_background_get(RECIPE_ID,
00720 parameters));
00721 check( crh_single_par = xsh_parameters_remove_crh_single_get(RECIPE_ID,
00722 parameters));
00723
00724 check( rectify_par = xsh_parameters_rectify_get(RECIPE_ID,
00725 parameters));
00726 check( merge_par = xsh_parameters_merge_ord_get(RECIPE_ID,
00727 parameters));
00728 check( loc_ifu_par = xsh_parameters_localize_ifu_get(RECIPE_ID,
00729 parameters));
00730 check( geom_niter = xsh_parameters_get_int( parameters, RECIPE_ID,
00731 "correctifu-niter"));
00732 check( lambda_ref = xsh_parameters_get_double( parameters, RECIPE_ID,
00733 "correctifu-lambdaref"));
00734 check( lambdaref_hsize = xsh_parameters_get_double( parameters, RECIPE_ID,
00735 "correctifu-lambdaref-hsize"));
00736 check( do_cube = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00737 "do-cube"));
00738
00739 check( do_computemap = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00740 "compute-map"));
00741
00742 check( check_afc = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00743 "check-afc"));
00744
00745 if ( do_computemap && recipe_use_model==FALSE){
00746 check( disp_tab_frame = xsh_find_frame_with_tag( calib,XSH_DISP_TAB_AFC,
00747 instrument));
00748 }
00749
00750 check(xsh_params_set_defaults(parameters,instrument,rectify_par, &lambda_ref,backg_par));
00751
00752 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00753 check(xsh_params_bin_scale(raws,backg_par));
00754 }
00755 check(xsh_params_monitor(backg_par,rectify_par));
00756
00757
00758 if ( loc_ifu_par->use_skymask == TRUE){
00759 xsh_msg("Using sky mask");
00760 check( skymask_frame = xsh_find_frame_with_tag( calib, XSH_SKY_LINE_LIST,
00761 instrument));
00762 }
00763
00764
00765
00766
00767
00768
00769 xsh_msg( "---Prepare raws frames");
00770 check( xsh_prepare( raws, bpmap, master_bias, XSH_OBJECT_IFU_STARE,
00771 instrument,pre_overscan_corr));
00772
00773
00774
00775 if ( nb_raw_frames >= 3 ) {
00776 xsh_msg( "---Remove cosmics");
00777
00778 ftag=XSH_GET_TAG_FROM_ARM(XSH_SLIT_STARE_REMOVE_CRH,instrument);
00779 check_msg( crhm_frame = xsh_check_remove_crh_multiple( raws,
00780 ftag,stack_par,
00781 crh_clipping_par,
00782 instrument, NULL,NULL ),
00783 "Error in xsh_remove_crh" ) ;
00784 }
00785 else {
00786
00787 check( crhm_frame = cpl_frame_duplicate(cpl_frameset_get_first( raws )));
00788 }
00789 xsh_msg( "created %s", cpl_frame_get_filename( crhm_frame));
00790
00791 if ( do_computemap){
00792 if (recipe_use_model){
00793 char wave_map_tag[80];
00794 char slit_map_tag[80];
00795 int found_temp=true;
00796
00797 check(xsh_model_temperature_update_frame(&model_config_frame,crhm_frame,
00798 instrument,&found_temp));
00799
00800 sprintf(wave_map_tag,"%s_%s",rec_prefix,XSH_WAVE_MAP_MODEL);
00801 sprintf(slit_map_tag,"%s_%s",rec_prefix,XSH_SLIT_MAP_MODEL);
00802 check( xsh_create_model_map( model_config_frame, instrument,
00803 wave_map_tag,slit_map_tag,
00804 &wavemap_frame, &slitmap_frame,1));
00805 }
00806 else{
00807 xsh_msg( "Compute the wave map and the slit map");
00808 check( xsh_create_map( disp_tab_frame, order_tab_edges,
00809 crhm_frame, instrument,
00810 &wavemap_frame, &slitmap_frame,rec_prefix));
00811 }
00812 cpl_frameset_insert( calib, wavemap_frame);
00813 cpl_frameset_insert( calib, slitmap_frame);
00814 }
00815 else{
00816 check( wavemap_frame = xsh_find_wavemap( calib, instrument));
00817 slitmap_frame = xsh_find_slitmap(calib, instrument);
00818 }
00819
00820 check( rmbias = xsh_check_subtract_bias( crhm_frame, master_bias,
00821 instrument, rec_prefix, pre_overscan_corr,0));
00822
00823
00824
00825
00826 check( xsh_check_afc( check_afc, model_config_frame,
00827 rmbias, wave_tabs_ifu, order_tab_edges, disp_tab_frame,
00828 instrument));
00829
00830
00831 check( rmdark = xsh_check_subtract_dark( rmbias, master_dark,
00832 instrument, rec_prefix));
00833
00834
00835 xsh_msg("---Subtract_background");
00836 check( rmbkg = xsh_subtract_background( rmdark,
00837 order_tab_edges, backg_par, instrument, "GEOM_IFU",&grid_backg,
00838 &frame_backg,0 ));
00839
00840
00841 xsh_msg("---Do flatfielding");
00842 sprintf( tag, "GEOM_IFU_FF_%s",
00843 xsh_instrument_arm_tostring(instrument));
00844
00845 check( flat_method = xsh_parameters_get_string( parameters, RECIPE_ID,
00846 "flat-method"));
00847
00848 xsh_msg("method %s", flat_method);
00849
00850 if ( strcmp( flat_method, "master") == 0){
00851 check( div_frame = xsh_divide_flat( rmbkg, masterflat_frame,
00852 tag, instrument));
00853 }
00854 else{
00855 xsh_msg("---Create blaze image");
00856 check( blaze_frame = xsh_blaze_image( masterflat_frame, order_tab_edges,
00857 instrument));
00858 check( div_frame = xsh_divide_by_blaze( rmbkg, blaze_frame, instrument));
00859 }
00860
00861 sprintf( geom_prefix, "orig_%s", rec_prefix);
00862
00863 for( i=1; i<= geom_niter; i++){
00864 xsh_free_frameset( &rect_frameset);
00865 xsh_free_frameset( &rect_frameset_eso);
00866 xsh_free_frameset( &rect_frameset_tab) ;
00867 xsh_free_frameset( &merge_frameset);
00868 xsh_free_frameset( &locifu_frameset);
00869
00870 xsh_msg( "---Do rectify for ifu: iteration %d/%d", i, geom_niter);
00871
00872 rect_frameset_eso=cpl_frameset_new();
00873 rect_frameset_tab=cpl_frameset_new();
00874
00875 check( rect_frameset = xsh_rectify_ifu( div_frame, order_tab_edges,
00876 wave_tabs_ifu, shiftifu_old_frameset, model_config_frame,
00877 instrument, rectify_par,
00878 spectralformat_frame, slitmap_frame,
00879 &rect_frameset_eso, &rect_frameset_tab, geom_prefix));
00880
00881
00882 check( merge_frameset = xsh_merge_ord_ifu( rect_frameset,
00883 instrument, merge_par, geom_prefix));
00884
00885
00886 check( locifu_frameset = xsh_localize_ifu( merge_frameset,
00887 skymask_frame, loc_ifu_par, instrument, geom_prefix));
00888
00889
00890 check( shiftifu_frameset = xsh_compute_shift_ifu( lambda_ref,
00891 lambdaref_hsize,
00892 locifu_frameset, shiftifu_old_frameset, instrument, rec_prefix));
00893
00894 xsh_free_frameset( &shiftifu_old_frameset);
00895 shiftifu_old_frameset = shiftifu_frameset;
00896 sprintf( geom_prefix, "%s", rec_prefix);
00897 }
00898
00899
00900 if ( do_cube){
00901 xsh_free_frameset( &rect_frameset);
00902 xsh_free_frameset( &rect_frameset_eso);
00903 xsh_free_frameset( &rect_frameset_tab) ;
00904 xsh_free_frameset( &merge_frameset);
00905 xsh_free_frameset( &locifu_frameset);
00906
00907 rect_frameset_eso=cpl_frameset_new();
00908 rect_frameset_tab=cpl_frameset_new();
00909
00910 check( rect_frameset = xsh_rectify_ifu( div_frame, order_tab_edges,
00911 wave_tabs_ifu, shiftifu_frameset, model_config_frame,
00912 instrument, rectify_par,
00913 spectralformat_frame, slitmap_frame,
00914 &rect_frameset_eso, &rect_frameset_tab, rec_prefix));
00915
00916
00917 check( merge_frameset = xsh_merge_ord_ifu( rect_frameset,
00918 instrument, merge_par, rec_prefix));
00919
00920 check( data_cube = xsh_cube( merge_frameset, instrument, rec_prefix));
00921 }
00922
00923
00924 xsh_msg( "---Save products");
00925
00926 for( i = 0 ; i<3 ; i++ ) {
00927 cpl_frame *shiftifu_frame = NULL;
00928
00929 check( shiftifu_frame = cpl_frameset_get_frame( shiftifu_frameset, i));
00930 check( xsh_add_product_table( shiftifu_frame, frameset,parameters,
00931 RECIPE_ID, instrument,NULL));
00932 }
00933
00934
00935 if ( do_cube){
00936 const char* name=NULL;
00937 cpl_propertylist* plist=NULL;
00938 int naxis2=0;
00939 int save_size=0;
00940 const int peack_search_hsize=5;
00941 int method=0;
00942 cpl_frame* qc_trace_frame=NULL;
00943
00944 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
00945 RECIPE_ID, instrument));
00946
00947 name=cpl_frame_get_filename(data_cube);
00948 plist=cpl_propertylist_load(name,0);
00949 naxis2=xsh_pfits_get_naxis2(plist);
00950 xsh_free_propertylist(&plist);
00951 check( qc_trace_frame=xsh_cube_qc_trace_window(data_cube,
00952 instrument,tag, rec_prefix,
00953 save_size+1,
00954 naxis2-save_size,
00955 peack_search_hsize,
00956 method,0));
00957
00958
00959 if(qc_trace_frame) {
00960 check(xsh_add_product_table(qc_trace_frame,frameset,parameters,
00961 RECIPE_ID,instrument,NULL));
00962 }
00963 xsh_free_frame(&qc_trace_frame);
00964 }
00965
00966 cleanup:
00967 xsh_end( RECIPE_ID, frameset, parameters);
00968 XSH_FREE( rec_prefix);
00969
00970 XSH_FREE( crh_clipping_par);
00971 XSH_FREE( backg_par);
00972 XSH_FREE( crh_single_par);
00973 XSH_FREE( rectify_par);
00974 XSH_FREE( merge_par);
00975 XSH_FREE( loc_ifu_par);
00976 xsh_instrument_free( &instrument);
00977
00978 xsh_free_frameset( &raws);
00979 xsh_free_frameset( &calib);
00980 xsh_free_frameset( &wave_tabs_ifu);
00981
00982 xsh_free_frame( &crhm_frame ) ;
00983 xsh_free_frame( &rmbias);
00984 xsh_free_frame( &rmdark);
00985 xsh_free_frame( &rmbkg);
00986 xsh_free_frame( &blaze_frame);
00987 xsh_free_frame( &div_frame);
00988
00989 xsh_free_frameset( &rect_frameset);
00990 xsh_free_frameset( &rect_frameset_eso);
00991 xsh_free_frameset( &rect_frameset_tab) ;
00992 xsh_free_frameset( &merge_frameset);
00993 xsh_free_frameset( &locifu_frameset);
00994 xsh_free_frameset( &shiftifu_frameset);
00995 xsh_free_frame( &clean_frame);
00996 xsh_free_frame( &grid_backg);
00997 xsh_free_frame( &frame_backg);
00998 xsh_free_frame( &data_cube);
00999 return;
01000 }
01001