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 = { "tanh",
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.};
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 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00222 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00223 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
00224
00225 check(xsh_parameters_clipping_crh_create(RECIPE_ID,recipe->parameters,
00226 crh_clip_param));
00227
00228
00229 check(xsh_parameters_background_create(RECIPE_ID,recipe->parameters));
00230
00231
00232 check(xsh_parameters_remove_crh_single_create(RECIPE_ID,recipe->parameters,
00233 crh_single )) ;
00234
00235
00236 check(xsh_parameters_rectify_create(RECIPE_ID,recipe->parameters,
00237 rectify )) ;
00238
00239
00240 check(xsh_parameters_localize_ifu_create(RECIPE_ID,recipe->parameters,
00241 loc_ifu)) ;
00242
00243 check( xsh_parameters_new_int( recipe->parameters, RECIPE_ID,
00244 "correctifu-niter", 3,
00245 "Number of iterations in computing the localization shifts of the IFU slitlets"));
00246 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00247 "correctifu-lambdaref", -1,
00248 "Reference wavelength where the reference localization on the slit is chosen"));
00249 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00250 "correctifu-lambdaref-hsize", 2.5,
00251 "HAlf size in nm to estimate reference wavelength"));
00252
00253 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00254 "do-cube", FALSE,
00255 "if TRUE creates a data cube"));
00256
00257 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00258 "compute-map", TRUE,
00259 "if TRUE recompute (wave and slit) maps from the dispersion solution."));
00260
00261 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00262 "check-afc", TRUE,
00263 "Input AFC corrected model/wave solution and science frame check."\
00264 "If TRUE the recipe verify that the input mode/wave solution is AFC corrected,"\
00265 " its INS.OPTIi.NAME is 'Pin_0.5 ', and its OBS.ID and OBS.TARG.NAME values"\
00266 " matches with the corresponding values of the science frame."));
00267
00268 sprintf(recipename,"xsh.%s",RECIPE_ID);
00269 sprintf(paramname,"%s.%s",recipename,"flat-method");
00270 check( p = cpl_parameter_new_enum( paramname,CPL_TYPE_STRING,
00271 "method adopted for flat:",
00272 recipename,"blaze",
00273 2,"master","blaze"));
00274
00275 check(cpl_parameter_set_alias( p,CPL_PARAMETER_MODE_CLI,
00276 "flat-method"));
00277 check(cpl_parameterlist_append( recipe->parameters, p));
00278
00279 cleanup:
00280 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00281 xsh_error_dump(CPL_MSG_ERROR);
00282 return 1;
00283 }
00284 else {
00285 return 0;
00286 }
00287 }
00288
00296
00297
00298 static cpl_error_code
00299 xsh_params_bin_scale(cpl_frameset* raws,
00300 xsh_background_param* backg)
00301 {
00302
00303 cpl_frame* frame=NULL;
00304 const char* name=NULL;
00305 cpl_propertylist* plist=NULL;
00306 int binx=0;
00307 int biny=0;
00308
00309 check(frame=cpl_frameset_get_first(raws));
00310 check(name=cpl_frame_get_filename(frame));
00311 check(plist=cpl_propertylist_load(name,0));
00312 check(binx=xsh_pfits_get_binx(plist));
00313 check(biny=xsh_pfits_get_biny(plist));
00314 xsh_free_propertylist(&plist);
00315
00316 if(biny>1) {
00317
00318
00319
00320
00321
00322
00323 backg->radius_y=backg->radius_y/biny;
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 }
00344
00345
00346 if(binx>1) {
00347
00348 backg->radius_x=backg->radius_x/binx;
00349
00350
00351
00352
00353
00354
00355
00356
00357 }
00358
00359 cleanup:
00360 xsh_free_propertylist(&plist);
00361 return cpl_error_get_code();
00362
00363 }
00364
00365
00371
00372
00373 static int xsh_geom_ifu_exec(cpl_plugin *plugin) {
00374 cpl_recipe *recipe = NULL;
00375
00376
00377
00378 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00379
00380
00381 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00382 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00383
00384 recipe = (cpl_recipe *)plugin;
00385
00386
00387 xsh_geom_ifu(recipe->parameters, recipe->frames);
00388
00389 cleanup:
00390 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00391 xsh_error_dump(CPL_MSG_ERROR);
00392 xsh_error_reset();
00393 return 1;
00394 }
00395 else {
00396 return 0;
00397 }
00398 }
00399
00400
00406
00407 static int xsh_geom_ifu_destroy(cpl_plugin *plugin)
00408 {
00409 cpl_recipe *recipe = NULL;
00410
00411
00412 xsh_error_reset();
00413
00414 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00415
00416
00417 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00418 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00419
00420 recipe = (cpl_recipe *)plugin;
00421
00422 xsh_free_parameterlist(&recipe->parameters);
00423
00424 cleanup:
00425 if (cpl_error_get_code() != CPL_ERROR_NONE)
00426 {
00427 return 1;
00428 }
00429 else
00430 {
00431 return 0;
00432 }
00433 }
00434
00435 static cpl_error_code
00436 xsh_params_monitor(xsh_background_param* backg,
00437 xsh_rectify_param * rectify_par)
00438 {
00439
00440
00441 xsh_msg_dbg_low("bkg params: sampley=%d radius_y=%d",
00442 backg->sampley,backg->radius_y);
00443
00444 xsh_msg_dbg_low("bkg params: radius_x=%d",
00445 backg->radius_x);
00446
00447 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
00448 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
00449 rectify_par->rectif_bin_space);
00450
00451 return cpl_error_get_code();
00452
00453 }
00454
00455 static cpl_error_code
00456 xsh_params_set_defaults(cpl_parameterlist* pars,
00457 xsh_instrument* inst,
00458 xsh_rectify_param * rectify_par,
00459 double* lambda_ref)
00460 {
00461 cpl_parameter *p=NULL;
00462
00463 check(p=xsh_parameters_find(pars,RECIPE_ID,"correctifu-lambdaref"));
00464 if(cpl_parameter_get_double(p) <= 0) {
00465 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00466 *lambda_ref= XSH_CORRECTIFU_LAMBDAREF_PIPE_NIR;
00467 }
00468 else {
00469 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00470 *lambda_ref= XSH_CORRECTIFU_LAMBDAREF_PIPE_UVB;
00471 }
00472 else{
00473 *lambda_ref= XSH_CORRECTIFU_LAMBDAREF_PIPE_VIS;
00474 }
00475 }
00476 cpl_parameter_set_double(p, *lambda_ref);
00477 }
00478
00479 check(xsh_rectify_params_set_defaults_drl(pars,RECIPE_ID,inst,rectify_par));
00480
00481 cleanup:
00482 return cpl_error_get_code();
00483
00484 }
00485
00486
00487 static cpl_error_code
00488 xsh_rectify_params_set_defaults_drl(cpl_parameterlist* pars,
00489 const char* rec_id,
00490 xsh_instrument* inst,
00491 xsh_rectify_param * rectify_par)
00492 {
00493 cpl_parameter* p=NULL;
00494
00495 check(p=xsh_parameters_find(pars,rec_id,"rectify-bin-slit"));
00496 if(cpl_parameter_get_double(p) <= 0) {
00497 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00498 rectify_par->rectif_bin_space=XSH_GEOM_SLIT_BIN_SIZE_PIPE_UVB;
00499 cpl_parameter_set_double(p,XSH_GEOM_SLIT_BIN_SIZE_PIPE_UVB);
00500 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS){
00501 rectify_par->rectif_bin_space=XSH_GEOM_SLIT_BIN_SIZE_PIPE_VIS;
00502 cpl_parameter_set_double(p,XSH_GEOM_SLIT_BIN_SIZE_PIPE_VIS);
00503 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00504 rectify_par->rectif_bin_space=XSH_GEOM_SLIT_BIN_SIZE_PIPE_NIR;
00505 cpl_parameter_set_double(p,XSH_GEOM_SLIT_BIN_SIZE_PIPE_NIR);
00506 }
00507 }
00508 check(p=xsh_parameters_find(pars,rec_id,"rectify-bin-lambda"));
00509 if(cpl_parameter_get_double(p) <= 0) {
00510 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00511 rectify_par->rectif_bin_lambda=XSH_GEOM_WAVE_BIN_SIZE_PIPE_UVB;
00512 cpl_parameter_set_double(p,XSH_GEOM_WAVE_BIN_SIZE_PIPE_UVB);
00513 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS){
00514 rectify_par->rectif_bin_lambda=XSH_GEOM_WAVE_BIN_SIZE_PIPE_VIS;
00515 cpl_parameter_set_double(p,XSH_GEOM_WAVE_BIN_SIZE_PIPE_VIS);
00516 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00517 rectify_par->rectif_bin_lambda=XSH_GEOM_WAVE_BIN_SIZE_PIPE_NIR;
00518 cpl_parameter_set_double(p,XSH_GEOM_WAVE_BIN_SIZE_PIPE_NIR);
00519 }
00520 }
00521
00522 cleanup:
00523
00524 return cpl_error_get_code();
00525
00526 }
00527
00528
00536
00537 static void xsh_geom_ifu(cpl_parameterlist* parameters,
00538 cpl_frameset* frameset)
00539 {
00540 const char* recipe_tags[1] = {XSH_TRACE};
00541 int recipe_tags_size = 1;
00542
00543
00544 cpl_frameset *raws = NULL;
00545 cpl_frameset *calib = NULL;
00546
00547
00548
00549 cpl_frame *bpmap = NULL;
00550 cpl_frame *master_bias = NULL;
00551 cpl_frame *master_dark = NULL;
00552 cpl_frame *masterflat_frame = NULL;
00553 cpl_frame *order_tab_edges = NULL;
00554 cpl_frameset *wave_tabs_ifu = NULL ;
00555 cpl_frame *model_config_frame = NULL ;
00556 cpl_frame *wavemap_frame = NULL ;
00557 cpl_frame *spectralformat_frame = NULL ;
00558 cpl_frame *slitmap_frame = NULL ;
00559 cpl_frame *disp_tab_frame = NULL;
00560 cpl_frame *skymask_frame = NULL;
00561
00562
00563 xsh_clipping_param *crh_clipping_par = NULL;
00564 xsh_background_param *backg_par = NULL;
00565 xsh_remove_crh_single_param *crh_single_par = NULL ;
00566 xsh_rectify_param *rectify_par = NULL ;
00567 int merge_par = 0;
00568 xsh_localize_ifu_param *loc_ifu_par = NULL ;
00569 xsh_stack_param* stack_par=NULL;
00570 char* flat_method = NULL;
00571 int do_computemap = 0, do_cube=0;
00572 int check_afc = TRUE;
00573 int recipe_use_model = FALSE;
00574 xsh_instrument* instrument = NULL;
00575 int nb_raw_frames;
00576
00577
00578 #if 0
00579 cpl_frameset * on = NULL, * off = NULL ;
00580 #endif
00581 cpl_frame * crhm_frame = NULL ;
00582 cpl_frame * rmbias = NULL;
00583 cpl_frame * rmdark = NULL;
00584 cpl_frame * rmbkg = NULL ;
00585 cpl_frame * blaze_frame = NULL;
00586 cpl_frame *div_frame = NULL ;
00587 cpl_frameset *rect_frameset = NULL ;
00588 cpl_frame * clean_frame = NULL ;
00590
00591 cpl_frame* grid_backg=NULL;
00592 cpl_frame* frame_backg=NULL;
00593 cpl_frameset* rect_frameset_eso=NULL;
00594 cpl_frameset* rect_frameset_tab=NULL;
00595 cpl_frameset *shiftifu_frameset=NULL;
00596 cpl_frameset *shiftifu_old_frameset = NULL;
00597 cpl_frameset *merge_frameset=NULL;
00598 cpl_frameset *locifu_frameset=NULL;
00599 cpl_frame *data_cube = NULL ;
00600
00601 const char* ftag=NULL;
00602 char tag[256];
00603 char *rec_prefix = NULL;
00604 char geom_prefix[256];
00605
00606 int pre_overscan_corr=0;
00607 int i, geom_niter = 1;
00608 double lambda_ref;
00609 double lambdaref_hsize;
00610
00611
00612
00613
00614 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00615 recipe_tags, recipe_tags_size,RECIPE_ID, XSH_BINARY_VERSION,
00616 xsh_geom_ifu_description_short));
00617
00618 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
00619 "Instrument NOT in IFU Mode");
00620
00621 check(xsh_recipe_params_check(parameters,instrument,RECIPE_ID));
00622
00623
00624 check( nb_raw_frames = cpl_frameset_get_size( raws));
00625 XSH_ASSURE_NOT_ILLEGAL( nb_raw_frames == 1 || nb_raw_frames >= 3 ) ;
00626
00627 check( rec_prefix = xsh_set_recipe_file_prefix(raws,"xsh_geom_ifu"));
00628
00629
00630
00631
00632 bpmap = xsh_find_master_bpmap(calib);
00633
00634
00635 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00636
00637 check( master_bias = xsh_find_master_bias(calib,instrument));
00638 }
00639 #if 0
00640
00641
00642 else {
00643
00644 xsh_msg( "Calling xsh_dfs_split_nir" ) ;
00645 check(xsh_dfs_split_nir(raws, &on, &off));
00646 xsh_msg( " Nb ON: %d, OFF: %d", cpl_frameset_get_size(on),
00647 cpl_frameset_get_size(off) ) ;
00648 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(on) >= 3);
00649 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(off) >= 3);
00650 }
00651 #endif
00652
00653 check( order_tab_edges = xsh_find_order_tab_edges( calib, instrument));
00654
00655
00656 if(NULL==(model_config_frame = xsh_find_frame_with_tag( calib,
00657 XSH_MOD_CFG_OPT_AFC,
00658 instrument))) {
00659
00660 wave_tabs_ifu = xsh_find_wave_tab_ifu( calib, instrument);
00661
00662 }
00663
00664
00665 if( (model_config_frame!=NULL) && (wave_tabs_ifu != NULL) ) {
00666
00667 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",
00668 XSH_WAVE_TAB_2D , XSH_MOD_CFG_TAB);
00669 goto cleanup;
00670 }
00671
00672 if((model_config_frame==NULL) && ( wave_tabs_ifu == NULL) ) {
00673 xsh_msg_error("You must provide either a %s or a %s frame",
00674 XSH_WAVE_TAB_2D, XSH_MOD_CFG_TAB);
00675 goto cleanup;
00676 }
00677
00678 if ( (master_dark = xsh_find_master_dark( calib, instrument ) ) == NULL ){
00679 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00680 xsh_error_reset();
00681 }
00682
00683 check( masterflat_frame = xsh_find_master_flat( calib, instrument));
00684 check(spectralformat_frame = xsh_find_spectral_format( calib, instrument ) ) ;
00685
00686
00687
00688
00689
00690 recipe_use_model = ( model_config_frame != NULL);
00691 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
00692 check( crh_clipping_par = xsh_parameters_clipping_crh_get(RECIPE_ID,
00693 parameters));
00694 check( backg_par = xsh_parameters_background_get(RECIPE_ID,
00695 parameters));
00696 check( crh_single_par = xsh_parameters_remove_crh_single_get(RECIPE_ID,
00697 parameters));
00698
00699 check( rectify_par = xsh_parameters_rectify_get(RECIPE_ID,
00700 parameters));
00701
00702 check( loc_ifu_par = xsh_parameters_localize_ifu_get(RECIPE_ID,
00703 parameters));
00704 check( geom_niter = xsh_parameters_get_int( parameters, RECIPE_ID,
00705 "correctifu-niter"));
00706 check( lambda_ref = xsh_parameters_get_double( parameters, RECIPE_ID,
00707 "correctifu-lambdaref"));
00708 check( lambdaref_hsize = xsh_parameters_get_double( parameters, RECIPE_ID,
00709 "correctifu-lambdaref-hsize"));
00710 check( do_cube = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00711 "do-cube"));
00712
00713 check( do_computemap = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00714 "compute-map"));
00715
00716 check( check_afc = xsh_parameters_get_boolean( parameters, RECIPE_ID,
00717 "check-afc"));
00718 rectify_par->conserve_flux=FALSE;
00719 if ( do_computemap && recipe_use_model==FALSE){
00720 check( disp_tab_frame = xsh_find_frame_with_tag( calib,XSH_DISP_TAB_AFC,
00721 instrument));
00722 }
00723
00724 check(xsh_params_set_defaults(parameters,instrument,rectify_par, &lambda_ref));
00725
00726 if ( xsh_instrument_get_arm( instrument ) != XSH_ARM_NIR ) {
00727 check(xsh_params_bin_scale(raws,backg_par));
00728 }
00729 check(xsh_params_monitor(backg_par,rectify_par));
00730
00731
00732 if ( loc_ifu_par->use_skymask == TRUE){
00733 xsh_msg("Using sky mask");
00734 check( skymask_frame = xsh_find_frame_with_tag( calib, XSH_SKY_LINE_LIST,
00735 instrument));
00736 }
00737
00738
00739
00740
00741
00742
00743 xsh_msg( "---Prepare raws frames");
00744 check( xsh_prepare( raws, bpmap, master_bias, XSH_OBJECT_IFU_STARE,
00745 instrument,pre_overscan_corr,CPL_TRUE));
00746
00747
00748
00749 if ( nb_raw_frames >= 3 ) {
00750 xsh_msg( "---Remove cosmics");
00751
00752 ftag=XSH_GET_TAG_FROM_ARM(XSH_SLIT_STARE_REMOVE_CRH,instrument);
00753 check_msg( crhm_frame = xsh_check_remove_crh_multiple( raws,
00754 ftag,stack_par,
00755 NULL,
00756 instrument, NULL,NULL ),
00757 "Error in xsh_remove_crh" ) ;
00758 }
00759 else {
00760
00761 check( crhm_frame = cpl_frame_duplicate(cpl_frameset_get_first( raws )));
00762 }
00763 xsh_msg( "created %s", cpl_frame_get_filename( crhm_frame));
00764
00765 if ( do_computemap){
00766 if (recipe_use_model){
00767 char wave_map_tag[256];
00768 char slit_map_tag[256];
00769 int found_temp=true;
00770
00771 check(xsh_model_temperature_update_frame(&model_config_frame,crhm_frame,
00772 instrument,&found_temp));
00773
00774 sprintf(wave_map_tag,"%s_%s",rec_prefix,XSH_WAVE_MAP_MODEL);
00775 sprintf(slit_map_tag,"%s_%s",rec_prefix,XSH_SLIT_MAP_MODEL);
00776 check( xsh_create_model_map( model_config_frame, instrument,
00777 wave_map_tag,slit_map_tag,
00778 &wavemap_frame, &slitmap_frame,1));
00779 }
00780 else{
00781 xsh_msg( "Compute the wave map and the slit map");
00782 check( xsh_create_map( disp_tab_frame, order_tab_edges,
00783 crhm_frame, instrument,
00784 &wavemap_frame, &slitmap_frame,rec_prefix));
00785 }
00786 cpl_frameset_insert( calib, wavemap_frame);
00787 cpl_frameset_insert( calib, slitmap_frame);
00788 }
00789 else{
00790 check( wavemap_frame = xsh_find_wavemap( calib, instrument));
00791 slitmap_frame = xsh_find_slitmap(calib, instrument);
00792 }
00793
00794 check( rmbias = xsh_check_subtract_bias( crhm_frame, master_bias,
00795 instrument, rec_prefix, pre_overscan_corr,0));
00796
00797
00798
00799
00800 check( xsh_check_afc( check_afc, model_config_frame,
00801 rmbias, wave_tabs_ifu, order_tab_edges, disp_tab_frame,
00802 instrument));
00803
00804
00805 check( rmdark = xsh_check_subtract_dark( rmbias, master_dark,
00806 instrument, rec_prefix));
00807
00808
00809 xsh_msg("---Subtract_background");
00810 check( rmbkg = xsh_subtract_background( rmdark,
00811 order_tab_edges, backg_par, instrument, "GEOM_IFU",&grid_backg,
00812 &frame_backg,0,0,0 ));
00813
00814
00815 xsh_msg("---Do flatfielding");
00816 sprintf( tag, "GEOM_IFU_FF_%s",
00817 xsh_instrument_arm_tostring(instrument));
00818
00819 check( flat_method = xsh_parameters_get_string( parameters, RECIPE_ID,
00820 "flat-method"));
00821
00822 xsh_msg("method %s", flat_method);
00823
00824 if ( strcmp( flat_method, "master") == 0){
00825 check( div_frame = xsh_divide_flat( rmbkg, masterflat_frame,
00826 tag, instrument));
00827 }
00828 else{
00829 xsh_msg("---Create blaze image");
00830 check( blaze_frame = xsh_blaze_image( masterflat_frame, order_tab_edges,
00831 instrument));
00832 check( div_frame = xsh_divide_by_blaze( rmbkg, blaze_frame, instrument));
00833 }
00834
00835 sprintf( geom_prefix, "orig_%s", rec_prefix);
00836
00837 for( i=1; i<= geom_niter; i++){
00838 xsh_free_frameset( &rect_frameset);
00839 xsh_free_frameset( &rect_frameset_eso);
00840 xsh_free_frameset( &rect_frameset_tab) ;
00841 xsh_free_frameset( &merge_frameset);
00842 xsh_free_frameset( &locifu_frameset);
00843
00844 xsh_msg( "---Do rectify for ifu: iteration %d/%d", i, geom_niter);
00845
00846 rect_frameset_eso=cpl_frameset_new();
00847 rect_frameset_tab=cpl_frameset_new();
00848
00849 check( rect_frameset = xsh_rectify_ifu( div_frame, order_tab_edges,
00850 wave_tabs_ifu, shiftifu_old_frameset, model_config_frame,
00851 instrument, rectify_par,
00852 spectralformat_frame, slitmap_frame,
00853 &rect_frameset_eso, &rect_frameset_tab, geom_prefix));
00854
00855
00856 check( merge_frameset = xsh_merge_ord_ifu( rect_frameset,
00857 instrument, merge_par, geom_prefix));
00858
00859
00860 check( locifu_frameset = xsh_localize_ifu( merge_frameset,
00861 skymask_frame, loc_ifu_par, instrument, geom_prefix));
00862
00863
00864 check( shiftifu_frameset = xsh_compute_shift_ifu( lambda_ref,
00865 lambdaref_hsize,
00866 locifu_frameset, shiftifu_old_frameset, instrument, rec_prefix));
00867
00868 xsh_free_frameset( &shiftifu_old_frameset);
00869 shiftifu_old_frameset = shiftifu_frameset;
00870 sprintf( geom_prefix, "%s", rec_prefix);
00871 }
00872
00873
00874 if ( do_cube){
00875 xsh_free_frameset( &rect_frameset);
00876 xsh_free_frameset( &rect_frameset_eso);
00877 xsh_free_frameset( &rect_frameset_tab) ;
00878 xsh_free_frameset( &merge_frameset);
00879 xsh_free_frameset( &locifu_frameset);
00880
00881 rect_frameset_eso=cpl_frameset_new();
00882 rect_frameset_tab=cpl_frameset_new();
00883
00884 check( rect_frameset = xsh_rectify_ifu( div_frame, order_tab_edges,
00885 wave_tabs_ifu, shiftifu_frameset, model_config_frame,
00886 instrument, rectify_par,
00887 spectralformat_frame, slitmap_frame,
00888 &rect_frameset_eso, &rect_frameset_tab, rec_prefix));
00889
00890
00891 check( merge_frameset = xsh_merge_ord_ifu( rect_frameset,
00892 instrument, merge_par, rec_prefix));
00893
00894 check( data_cube = xsh_cube( merge_frameset, instrument, rec_prefix));
00895 }
00896
00897
00898 xsh_msg( "---Save products");
00899
00900 for( i = 0 ; i<3 ; i++ ) {
00901 cpl_frame *shiftifu_frame = NULL;
00902
00903 check( shiftifu_frame = cpl_frameset_get_frame( shiftifu_frameset, i));
00904 check( xsh_add_product_table( shiftifu_frame, frameset,parameters,
00905 RECIPE_ID, instrument,NULL));
00906 }
00907
00908
00909 if ( do_cube){
00910 const char* name=NULL;
00911 cpl_propertylist* plist=NULL;
00912 int naxis2=0;
00913 int save_size=0;
00914 const int peack_search_hsize=5;
00915 int method=0;
00916 cpl_frame* qc_trace_frame=NULL;
00917
00918 check( xsh_add_product_pre_3d( data_cube, frameset, parameters,
00919 RECIPE_ID, instrument));
00920
00921 name=cpl_frame_get_filename(data_cube);
00922 plist=cpl_propertylist_load(name,0);
00923 naxis2=xsh_pfits_get_naxis2(plist);
00924 xsh_free_propertylist(&plist);
00925 check( qc_trace_frame=xsh_cube_qc_trace_window(data_cube,
00926 instrument,tag, rec_prefix,
00927 save_size+1,
00928 naxis2-save_size,
00929 peack_search_hsize,
00930 method,0));
00931
00932
00933 if(qc_trace_frame) {
00934 check(xsh_add_product_table(qc_trace_frame,frameset,parameters,
00935 RECIPE_ID,instrument,NULL));
00936 }
00937 xsh_free_frame(&qc_trace_frame);
00938 }
00939
00940 cleanup:
00941 xsh_end( RECIPE_ID, frameset, parameters);
00942 XSH_FREE( rec_prefix);
00943
00944 XSH_FREE( crh_clipping_par);
00945 XSH_FREE( backg_par);
00946 XSH_FREE( crh_single_par);
00947 XSH_FREE( rectify_par);
00948 XSH_FREE( stack_par);
00949
00950 XSH_FREE( loc_ifu_par);
00951 xsh_instrument_free( &instrument);
00952
00953 xsh_free_frameset( &raws);
00954 xsh_free_frameset( &calib);
00955 xsh_free_frameset( &wave_tabs_ifu);
00956
00957 xsh_free_frame( &crhm_frame ) ;
00958 xsh_free_frame( &rmbias);
00959 xsh_free_frame( &rmdark);
00960 xsh_free_frame( &rmbkg);
00961 xsh_free_frame( &blaze_frame);
00962 xsh_free_frame( &div_frame);
00963
00964 xsh_free_frameset( &rect_frameset);
00965 xsh_free_frameset( &rect_frameset_eso);
00966 xsh_free_frameset( &rect_frameset_tab) ;
00967 xsh_free_frameset( &merge_frameset);
00968 xsh_free_frameset( &locifu_frameset);
00969 xsh_free_frameset( &shiftifu_frameset);
00970 xsh_free_frame( &clean_frame);
00971 xsh_free_frame( &grid_backg);
00972 xsh_free_frame( &frame_backg);
00973 xsh_free_frame( &data_cube);
00974 return;
00975 }
00976