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
00042
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include <xsh_error.h>
00054
00055 #include <xsh_utils.h>
00056 #include <xsh_utils_table.h>
00057 #include <xsh_pfits.h>
00058 #include <xsh_msg.h>
00059
00060 #include <xsh_dfs.h>
00061
00062 #include <xsh_data_resid_tab.h>
00063
00064 #include <xsh_data_order.h>
00065 #include <xsh_drl.h>
00066 #include <xsh_drl_check.h>
00067 #include <xsh_model_kernel.h>
00068 #include <xsh_model_arm_constants.h>
00069
00070
00071 #include <cpl.h>
00072
00073
00074
00075
00076
00077 #define RECIPE_ID "xsh_2dmap"
00078 #define RECIPE_AUTHOR "R. Haigron, P. Bristow, D. Bramich, A. Modigliani"
00079 #define RECIPE_CONTACT "amodigli@eso.org"
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 static int xsh_2dmap_create(cpl_plugin *);
00090 static int xsh_2dmap_exec(cpl_plugin *);
00091 static int xsh_2dmap_destroy(cpl_plugin *);
00092
00093
00094 static void xsh_2dmap(cpl_parameterlist *, cpl_frameset *);
00095
00096
00097
00098
00099 static char xsh_2dmap_description_short[] =
00100 "Creates a wavelength and spatial resampling solution, a clean arc line list";
00101
00102 static char xsh_2dmap_description[] =
00103 "This recipe creates a wavelength and spatial resampling solution, a clean arc line list.\n\
00104 a residual map and a wave map.\n\
00105 Input Frames:\n\
00106 Raw Frame (Tag = WAVE_arm)\n\
00107 Arc Line List (Tag = ARC_LINE_LIST_arm)\n\
00108 Order Table (Tag = ORDER_TAB_EDGES_arm)\n\
00109 Master Bias (Tag = MASTER_BIAS_arm)\n\
00110 [poly mode] Wave Solution (Tag = WAVE_TAB_GUESS_arm)\n\
00111 [poly mode] Theoretical Map (Tag = THEO_TAB_MULT_arm)\n\
00112 [physical model mode] model cfg table (Tag = XSH_MOD_CFG_OPT_FMT_arm)\n\
00113 [OPTIONAL] Master Dark (Tag = MASTER_DARK_arm)\n\
00114 [OPTIONAL] Non-linear Bad Pixel Map (Tag = BP_MAP_NL_arm)\n\
00115 [OPTIONAL] Reference Bad Pixel Map (Tag = BP_MAP_RP_arm)\n\
00116 [OPTIONAL] Reference list to monitor line intensity (Tag = ARC_LINE_LIST_INTMON_arm)\n \
00117 Prepare the frames.\n\
00118 For UVB,VIS:\n\
00119 Subtract Master Bias.\n\
00120 Subtract Master Dark.\n\
00121 For NIR:\n\
00122 Subtract ON OFF \n\
00123 Compute Wavelength Solution, clean arc line list, residual table, wave map\n\
00124 Products:\n\
00125 Wavelength table solution, PRO.CATG = WAVE_TAB_2D_arm [poly mode]\n\
00126 A Residual tab, PRO.CATG=WAVE_RESID_TAB_SLIT_arm\n\
00127 A Wavelelength image map, PRO.CATG=WAVE_MAP_arm. [if model-wavemap-compute=TRUE]\n\
00128 A Slit image map, PRO.CATG=SLIT_MAP_NIR [if model-wavemap-compute=TRUE]\n\
00129 A Dispersion solution table, PRO.CATG=DISP_TAB_NIR\n\
00130 An Arc frame in pre format bias subtracted, \n\
00131 PRO.CATG=WAVE_ON_arm\n\
00132 The optimized model cfg frame, PRO.CATG=XSH_MOD_CFG_OPT_2D_arm [if physical model mode].\n";
00133
00134
00135
00136
00137
00138
00139
00140
00149
00150
00151 int cpl_plugin_get_info(cpl_pluginlist *list) {
00152 cpl_recipe *recipe = NULL;
00153 cpl_plugin *plugin = NULL;
00154
00155 recipe = cpl_calloc(1, sizeof(*recipe));
00156 if ( recipe == NULL ){
00157 return -1;
00158 }
00159
00160 plugin = &recipe->interface ;
00161
00162 cpl_plugin_init(plugin,
00163 CPL_PLUGIN_API,
00164 XSH_BINARY_VERSION,
00165 CPL_PLUGIN_TYPE_RECIPE,
00166 RECIPE_ID,
00167 xsh_2dmap_description_short,
00168 xsh_2dmap_description,
00169 RECIPE_AUTHOR,
00170 RECIPE_CONTACT,
00171 xsh_get_license(),
00172 xsh_2dmap_create,
00173 xsh_2dmap_exec,
00174 xsh_2dmap_destroy);
00175
00176 cpl_pluginlist_append(list, plugin);
00177
00178 return (cpl_error_get_code() != CPL_ERROR_NONE);
00179 }
00180
00181
00191
00192
00193 static int xsh_2dmap_create(cpl_plugin *plugin){
00194 cpl_recipe *recipe = NULL;
00195 xsh_clipping_param detarc_clip_param = {2.0, 0, 0.7, 0, 0.3};
00196 xsh_detect_arclines_param detarc_param =
00197 {6, 3, 0, 5, 4, 1, 5, 5.0,
00198 XSH_GAUSSIAN_METHOD, FALSE};
00199 xsh_dispersol_param dispsol_param = { 4, 5 } ;
00200 char paramname[256];
00201 cpl_parameter* p=NULL;
00202 int ival=DECODE_BP_FLAG_DEF;
00203
00204
00205 xsh_init();
00206
00207
00208 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00209
00210
00211 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00212 CPL_ERROR_TYPE_MISMATCH,
00213 "Plugin is not a recipe");
00214
00215 recipe = (cpl_recipe *)plugin;
00216
00217
00218 recipe->parameters = cpl_parameterlist_new();
00219 assure( recipe->parameters != NULL,
00220 CPL_ERROR_ILLEGAL_OUTPUT,
00221 "Memory allocation failed!");
00222
00223
00224 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00225 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,ival);
00226 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
00227
00228
00229
00230
00231 check(xsh_parameters_detect_arclines_create(RECIPE_ID, recipe->parameters,
00232 detarc_param));
00233 check(xsh_parameters_clipping_detect_arclines_create(RECIPE_ID,
00234 recipe->parameters, detarc_clip_param));
00235 check(xsh_parameters_dispersol_create(RECIPE_ID,
00236 recipe->parameters,
00237 dispsol_param ) ) ;
00238
00239
00240 check( xsh_parameters_new_int( recipe->parameters, RECIPE_ID,
00241 "model-maxit",500,
00242 "Number/10 of annealing iterations "
00243 "(physical model mode)."));
00244
00245
00246 check( xsh_parameters_new_double( recipe->parameters, RECIPE_ID,
00247 "model-anneal-factor",1.0,
00248 "Multiplier applied to the automatic "
00249 "parameter ranges (i.e. when scenario!=0). "
00250 "For routine operations should be 1.0. "
00251 "(physical model mode)."));
00252
00253
00254 sprintf(paramname,"xsh.%s.%s",RECIPE_ID,"model-scenario");
00255
00256
00257 check(p=cpl_parameter_new_enum(paramname,CPL_TYPE_INT,
00258 "selects preset flag and range combinations "
00259 "appropriate to common scenarios: \n"
00260
00261
00262 " 0 - No scenario, input cfg flags and limits"
00263 "used.\n"
00264 " 1 - scenario appropriate for the startup"
00265 "recipe (large ranges for parameters "
00266 "affecting single ph exposures, dist "
00267 "coeff fixed)\n"
00268 " 2 - Like 1, but includes parameters "
00269 "affecting all ph positions\n"
00270 " 3 - Scenario for use in fine tuning cfg "
00271 "to match routine single pinhole exposures. "
00272 "All parameters affecting 1ph exposures "
00273 "except dist coeffs are included and "
00274 "parameter ranges are small. (For use by "
00275 "predict in 1ph case).\n"
00276 " 4 - Like 3 but includes parameters "
00277 "affecting all ph positions (Standard for "
00278 "use by predict in 9ph case and 2dmap).\n"
00279
00280
00281 ,RECIPE_ID,4,9,-1,0,1,2,3,4,5,6,8));
00282
00283 check(cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
00284 "model-scenario"));
00285 check(cpl_parameterlist_append(recipe->parameters,p));
00286
00287 cleanup:
00288 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00289 xsh_error_dump(CPL_MSG_ERROR);
00290 return 1;
00291 }
00292 else {
00293 return 0;
00294 }
00295 }
00296
00297
00303
00304
00305 static int xsh_2dmap_exec(cpl_plugin *plugin) {
00306 cpl_recipe *recipe = NULL;
00307
00308
00309 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00310
00311
00312 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00313 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00314
00315 recipe = (cpl_recipe *)plugin;
00316
00317 xsh_2dmap(recipe->parameters, recipe->frames);
00318
00319 cleanup:
00320 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00321 xsh_error_dump(CPL_MSG_ERROR);
00322 cpl_error_reset();
00323 return 1;
00324 }
00325 else {
00326 return 0;
00327 }
00328 }
00329
00330
00336
00337 static int xsh_2dmap_destroy(cpl_plugin *plugin)
00338 {
00339 cpl_recipe *recipe = NULL;
00340
00341
00342 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00343
00344
00345 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00346 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00347
00348 recipe = (cpl_recipe *)plugin;
00349
00350 xsh_free_parameterlist(&recipe->parameters);
00351
00352 cleanup:
00353 if (cpl_error_get_code() != CPL_ERROR_NONE)
00354 {
00355 return 1;
00356 }
00357 else
00358 {
00359 return 0;
00360 }
00361 }
00362
00363
00364
00365 static cpl_error_code
00366 xsh_verify_2dmap_poly_input(cpl_frame* order_tab_edges_frame,
00367 cpl_frame* spectralformat_frame,
00368 cpl_frame* theo_tab_frame)
00369 {
00370
00371 cpl_table* tab_edges=NULL;
00372 cpl_table* tab_spectral_format=NULL;
00373 cpl_table* theo_tab=NULL;
00374 const char* name=NULL;
00375 int ord_min_ref=0;
00376 int ord_max_ref=0;
00377 int ord_min=0;
00378 int ord_max=0;
00379
00380 check(name=cpl_frame_get_filename(order_tab_edges_frame));
00381 check(tab_edges=cpl_table_load(name,1,0));
00382 check(ord_min_ref=cpl_table_get_column_min(tab_edges,"ABSORDER"));
00383 check(ord_max_ref=cpl_table_get_column_max(tab_edges,"ABSORDER"));
00384
00385 check(name=cpl_frame_get_filename(spectralformat_frame));
00386 check(tab_spectral_format=cpl_table_load(name,1,0));
00387 check(ord_min=cpl_table_get_column_min(tab_spectral_format,"ORDER"));
00388 check(ord_max=cpl_table_get_column_max(tab_spectral_format,"ORDER"));
00389
00390 if(ord_min != ord_min_ref) {
00391 xsh_msg_error("Edge order table's ord_min != spectral format table's ord_min");
00392 return CPL_ERROR_INCOMPATIBLE_INPUT;
00393 }
00394
00395
00396 if(ord_max != ord_max_ref) {
00397 xsh_msg_error("Edge order table's ord_max != spectral format table's ord_max");
00398 return CPL_ERROR_INCOMPATIBLE_INPUT;
00399 }
00400
00401
00402 if(theo_tab_frame != NULL) {
00403 check(name=cpl_frame_get_filename(theo_tab_frame));
00404 check(theo_tab=cpl_table_load(name,1,0));
00405 check(ord_min=cpl_table_get_column_min(theo_tab,"Order"));
00406 check(ord_max=cpl_table_get_column_max(theo_tab,"Order"));
00407
00408 if(ord_min != ord_min_ref) {
00409 xsh_msg_error("Theo table's ord_min != spectral format table's ord_min");
00410 return CPL_ERROR_INCOMPATIBLE_INPUT;
00411 }
00412
00413
00414 if(ord_max != ord_max_ref) {
00415 xsh_msg_error("Theo table's ord_max != spectral format table's ord_max");
00416 return CPL_ERROR_INCOMPATIBLE_INPUT;
00417 }
00418
00419
00420 }
00421
00422
00423
00424 cleanup:
00425 xsh_free_table(&tab_edges);
00426 xsh_free_table(&tab_spectral_format);
00427 xsh_free_table(&theo_tab);
00428
00429 return cpl_error_get_code();
00430
00431 }
00432
00433 static cpl_error_code
00434 xsh_params_set_defaults(cpl_parameterlist* pars,
00435 xsh_detect_arclines_param* arclines_p,
00436 xsh_instrument* inst)
00437 {
00438 cpl_parameter* p=NULL;
00439 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-min-sn"));
00440 if(cpl_parameter_get_double(p) <= 0) {
00441 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00442 arclines_p->min_sn=3;
00443 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS) {
00444 arclines_p->min_sn=6;
00445 } else {
00446 arclines_p->min_sn=10;
00447 }
00448 }
00449
00450 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-fit-window-half-size"));
00451 if(cpl_parameter_get_int(p) <= 0) {
00452 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00453 arclines_p->fit_window_hsize=3;
00454 } else {
00455 arclines_p->fit_window_hsize=4;
00456 }
00457 }
00458 cleanup:
00459
00460 return cpl_error_get_code();
00461
00462 }
00463
00464
00465
00473
00474 static void xsh_2dmap(cpl_parameterlist* parameters, cpl_frameset* frameset)
00475 {
00476 const char* recipe_tags[1] = {XSH_WAVE};
00477 int recipe_tags_size = 1;
00478
00479
00480 xsh_clipping_param* detect_arclines_clipping = NULL;
00481 xsh_detect_arclines_param* detect_arclines_p = NULL;
00482 xsh_dispersol_param *dispsol_param = NULL ;
00483 int model_map_compute=CPL_TRUE;
00484
00485
00486 cpl_frameset* raws = NULL;
00487 cpl_frameset* calib = NULL;
00488 cpl_frameset* on = NULL;
00489 cpl_frameset* off = NULL;
00490 cpl_frameset* on_off = NULL;
00491 cpl_frame* dmap_rmbias = NULL;
00492 cpl_frame* dmap_rmdark = NULL;
00493 xsh_instrument* instrument = NULL;
00494
00495 cpl_frame* model_config_frame = NULL;
00496 cpl_frame* opt_model_config_frame = NULL;
00497 cpl_frame* spectralformat_frame = NULL;
00498 cpl_frame* bpmap = NULL;
00499 cpl_frame* master_bias = NULL;
00500 cpl_frame* master_dark = NULL;
00501 cpl_frame* theo_tab_mult = NULL;
00502 cpl_frame* resid_map = NULL;
00503 cpl_frame* resid_dan = NULL;
00504 cpl_frame* arclines = NULL;
00505 cpl_frame* clean_arclines = NULL;
00506 cpl_frame* wave_tab_guess = NULL;
00507 cpl_frame* wave_tab_2d = NULL;
00508 cpl_frame * order_tab_edges = NULL ;
00509 cpl_frame * wave_map = NULL ;
00510 cpl_frame * line_intmon = NULL ;
00511 char wave_map_tag[256];
00512 char slit_map_tag[256];
00513
00514 char *prefix = NULL ;
00515 char filename[256];
00516 int solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
00517 char paramname[256];
00518 cpl_parameter * p =NULL;
00519 cpl_frame *dispersol_frame = NULL;
00520 cpl_frame *slitmap_frame = NULL;
00521 int pre_overscan_corr=0;
00522 cpl_propertylist* plist=NULL;
00523 double exptime=0;
00524 cpl_boolean mode_phys;
00525 char tag[256];
00526 int resid_name_sw=0;
00527
00528
00529
00530 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib, recipe_tags,
00531 recipe_tags_size,
00532 RECIPE_ID, XSH_BINARY_VERSION,
00533 xsh_2dmap_description_short ));
00534
00535 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00536
00537
00538
00539 mode_phys=xsh_mode_is_physmod(calib,instrument);
00540
00541 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00542
00543 cknull_msg(arclines = xsh_find_frame_with_tag(calib,
00544 XSH_ARC_LINE_LIST,
00545 instrument),
00546 "Missing %s frame",XSH_ARC_LINE_LIST);
00547 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
00548
00549 if (!mode_phys) {
00550 if((theo_tab_mult = xsh_find_frame_with_tag( calib,XSH_THEO_TAB_MULT,
00551 instrument)) == NULL) {
00552
00553 }
00554 solution_type=XSH_DETECT_ARCLINES_TYPE_POLY;
00555 } else {
00556 if((model_config_frame = xsh_find_frame_with_tag(calib,
00557 XSH_MOD_CFG_OPT_FMT,
00558 instrument)) == NULL) {
00559
00560 xsh_error_reset();
00561
00562 if ((model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
00563 instrument)) == NULL) {
00564 xsh_error_reset();
00565 }
00566
00567 }
00568 solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
00569 }
00570 if( (model_config_frame!=NULL) && (theo_tab_mult != NULL) ) {
00571
00572 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",
00573 XSH_THEO_TAB_MULT,XSH_MOD_CFG_TAB);
00574 goto cleanup;
00575 }
00576
00577 if((model_config_frame==NULL) && (theo_tab_mult == NULL) ) {
00578 xsh_msg_error("You must provide either a %s or a %s frame",
00579 XSH_THEO_TAB_MULT,XSH_MOD_CFG_TAB);
00580 goto cleanup;
00581
00582 }
00583
00584
00585 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00586 XSH_SPECTRAL_FORMAT, instrument));
00587
00588
00589 if( (spectralformat_frame = xsh_find_frame_with_tag( calib,
00590 XSH_SPECTRAL_FORMAT,
00591 instrument)) == NULL) {
00592 xsh_msg("Frame %s not provided",
00593 XSH_SPECTRAL_FORMAT);
00594 xsh_error_reset();
00595 }
00596
00597
00598 if((line_intmon = xsh_find_frame_with_tag( calib, XSH_ARC_LINE_LIST_INTMON,
00599 instrument)) == NULL) {
00600 xsh_error_reset();
00601 }
00602
00603
00604
00605
00606
00607
00608 if(theo_tab_mult!=NULL) {
00609 check(xsh_verify_2dmap_poly_input(order_tab_edges, spectralformat_frame,
00610 theo_tab_mult));
00611 }
00612
00613
00614
00615 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00616
00617 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) == 1);
00618 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00619 instrument)) == NULL) {
00620
00621 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00622 xsh_error_reset();
00623 }
00624
00625
00626 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00627 instrument)) == NULL){
00628 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00629 xsh_error_reset();
00630 }
00631 }
00632
00633 else {
00634
00635 check(xsh_dfs_split_nir(raws,&on,&off));
00636 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(on) == 1);
00637 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(off) == 1);
00638 }
00639 check( xsh_instrument_update_from_spectralformat( instrument,
00640 spectralformat_frame));
00641
00642
00643
00644
00645 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00646 "pre-overscan-corr"));
00647
00648 check( detect_arclines_clipping =
00649 xsh_parameters_clipping_detect_arclines_get( RECIPE_ID, parameters));
00650 check( detect_arclines_p = xsh_parameters_detect_arclines_get( RECIPE_ID,
00651 parameters));
00652 check( dispsol_param = xsh_parameters_dispersol_get( RECIPE_ID,parameters)) ;
00653
00654 check(xsh_params_set_defaults(parameters,detect_arclines_p,instrument));
00655
00656
00657
00658
00659
00660 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00661 cpl_frame* multi_pin_hole = NULL;
00662
00663
00664 check(xsh_prepare(raws,bpmap, master_bias, XSH_WAVE, instrument,
00665 pre_overscan_corr,CPL_TRUE));
00666 check(multi_pin_hole = cpl_frameset_get_first(raws));
00667 if(master_bias != NULL) {
00668
00669 check(dmap_rmbias = xsh_subtract_bias(multi_pin_hole,master_bias,
00670 instrument,"WAVE_",pre_overscan_corr,0));
00671 } else {
00672 dmap_rmbias=cpl_frame_duplicate(multi_pin_hole);
00673 }
00674 if(master_dark != NULL) {
00675
00676 strcpy(filename,xsh_stringcat_any( "WAVE_DARK_",
00677 xsh_instrument_arm_tostring( instrument ),
00678 ".fits", NULL )) ;
00679
00680 check(dmap_rmdark = xsh_subtract_dark(dmap_rmbias, master_dark,
00681 filename, instrument));
00682 } else {
00683 dmap_rmdark=cpl_frame_duplicate(dmap_rmbias);
00684 }
00685 }
00686
00687 else{
00688
00689 check(xsh_prepare(on,bpmap, NULL, "ON", instrument,pre_overscan_corr,CPL_TRUE));
00690
00691 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr,CPL_TRUE));
00692
00693
00694
00695 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
00696 check(dmap_rmdark = cpl_frame_duplicate(
00697 cpl_frameset_get_first(on_off)));
00698 }
00699
00700 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00701 check(xsh_check_input_is_unbinned(dmap_rmdark));
00702 }
00703
00704
00705
00706
00707 plist=cpl_propertylist_load(cpl_frame_get_filename(dmap_rmdark),0);
00708 exptime=xsh_pfits_get_exptime(plist);
00709 check(xsh_free_propertylist(&plist));
00710
00711
00712 if(model_config_frame!=NULL) {
00713 resid_name_sw=1;
00714 check( xsh_detect_arclines_dan(dmap_rmdark, theo_tab_mult,
00715 arclines, wave_tab_guess, NULL,
00716 model_config_frame, spectralformat_frame, NULL,
00717 &clean_arclines, &wave_tab_2d,
00718 &resid_dan, XSH_SOLUTION_ABSOLUTE,
00719 detect_arclines_p, detect_arclines_clipping,
00720 instrument,RECIPE_ID,0,resid_name_sw));
00721
00722
00723 xsh_free_frame(&wave_tab_2d);
00724
00725
00726 check(resid_map = cpl_frame_duplicate(resid_dan));
00727 cpl_table* resid = NULL;
00728 cpl_table* ext = NULL;
00729 const char* fname = NULL;
00730 char rtag[256];
00731 char fout[256];
00732 cpl_propertylist* phead = NULL;
00733 cpl_propertylist* xhead = NULL;
00734
00735 fname = cpl_frame_get_filename(resid_dan);
00736 resid = cpl_table_load(fname, 1, 0);
00737 phead = cpl_propertylist_load(fname, 0);
00738
00739 sprintf(rtag, "WAVE_RESID_TAB_LINES_GFIT_%s",
00740 xsh_instrument_arm_tostring(instrument));
00741 sprintf(fout, "%s.fits", rtag);
00742
00743 check(cpl_table_and_selected_int(resid, "Flag", CPL_EQUAL_TO, 0));
00744 ext = cpl_table_extract_selected(resid);
00745 cpl_table_save(ext, phead, xhead, fout, CPL_IO_DEFAULT);
00746 xsh_free_table(&ext);
00747 xsh_free_table(&resid);
00748 xsh_free_propertylist(&phead);
00749
00750 cpl_frame_set_filename(resid_map, fout);
00751 check(cpl_frame_set_tag(resid_map, rtag));
00752 xsh_add_temporary_file(fout);
00753
00754 } else {
00755
00756
00757
00758 check( xsh_detect_arclines(dmap_rmdark, theo_tab_mult,
00759 arclines, wave_tab_guess, NULL,
00760 model_config_frame, spectralformat_frame, NULL,
00761 &clean_arclines, &wave_tab_2d,
00762 &resid_map, XSH_SOLUTION_ABSOLUTE,
00763 detect_arclines_p, detect_arclines_clipping,
00764 instrument,RECIPE_ID,0,resid_name_sw));
00765
00766 }
00767
00768
00769 sprintf(slit_map_tag,"SLIT_MAP_%s",
00770 xsh_instrument_arm_tostring( instrument )) ;
00771
00772 sprintf(wave_map_tag,"WAVE_MAP_%s",
00773 xsh_instrument_arm_tostring( instrument )) ;
00774
00775
00776 check( xsh_data_check_spectralformat( spectralformat_frame,
00777 order_tab_edges, wave_tab_2d, model_config_frame, instrument));
00778
00779
00780 int maxit=200;
00781 double ann_fac=1.0;
00782 int scenario=4;
00783 if ( model_config_frame == NULL){
00784
00785 check( wave_map = xsh_create_poly_wavemap( dmap_rmdark, wave_tab_2d,
00786 order_tab_edges,
00787 spectralformat_frame,
00788 dispsol_param, instrument,
00789 wave_map_tag, &dispersol_frame,
00790 &slitmap_frame));
00791 }
00792 else {
00793
00794 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-maxit");
00795 check(p = cpl_parameterlist_find(parameters,paramname));
00796 check(maxit=cpl_parameter_get_int(p));
00797
00798 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-anneal-factor");
00799 check(p = cpl_parameterlist_find(parameters,paramname));
00800 check(ann_fac=cpl_parameter_get_double(p));
00801
00802 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-scenario");
00803 check(p = cpl_parameterlist_find(parameters,paramname));
00804 check(scenario=cpl_parameter_get_int(p));
00805 xsh_msg_dbg_low("maxit=%d ann_fac=%g scenario=%d",maxit,ann_fac,scenario);
00806
00807 check(opt_model_config_frame=xsh_model_pipe_anneal(model_config_frame,
00808 resid_map,
00809 maxit,ann_fac,
00810 scenario,2));
00811
00812
00813 if (model_map_compute){
00814 check( xsh_create_model_map( opt_model_config_frame, instrument,
00815 wave_map_tag,slit_map_tag,
00816 &wave_map, &slitmap_frame,0));
00817
00818
00819
00820 check(dispersol_frame=xsh_create_dispersol_physmod(dmap_rmdark,
00821 order_tab_edges,
00822 opt_model_config_frame,
00823 wave_map,
00824 slitmap_frame,
00825 dispsol_param,
00826 spectralformat_frame,
00827 instrument,1));
00828 }
00829 }
00830 if(wave_map!=NULL) {
00831 check(xsh_wavemap_qc(wave_map,order_tab_edges));
00832 }
00833
00834 xsh_msg_dbg_low("QC wavetab");
00835 if ( model_config_frame == NULL){
00836 check(xsh_wavetab_qc(resid_map,true));
00837 } else {
00838 check(xsh_wavetab_qc(resid_map,false));
00839 }
00840
00841 if(line_intmon) {
00842 check(xsh_wavecal_qclog_intmon(resid_map,line_intmon,exptime,instrument));
00843 }
00844 check(xsh_table_merge_clean_and_resid_tabs(resid_map,clean_arclines));
00845
00846
00847
00848 xsh_msg("Saving products");
00849
00850 if ( wave_tab_2d != NULL){
00851 check(xsh_add_product_table(wave_tab_2d, frameset,
00852 parameters, RECIPE_ID, instrument,NULL));
00853 }
00854
00855 if(model_config_frame!=NULL) {
00856
00857 check(xsh_frame_table_resid_merge(resid_dan,resid_map,solution_type));
00858 check(xsh_add_product_table( resid_dan, frameset,
00859 parameters, RECIPE_ID, instrument,NULL));
00860
00861
00862
00863
00864 } else {
00865 check(xsh_add_product_table( resid_map, frameset,parameters, RECIPE_ID,
00866 instrument,"WAVE_RESID_TAB_LINES"));
00867 }
00868 if ( wave_map != NULL){
00869 check(xsh_add_product_image( wave_map, frameset,
00870 parameters, RECIPE_ID, instrument, XSH_WAVE_MAP ));
00871 }
00872 if (slitmap_frame != NULL){
00873 check(xsh_add_product_image( slitmap_frame, frameset,
00874 parameters, RECIPE_ID, instrument, XSH_SLIT_MAP ));
00875 }
00876 if ( dispersol_frame != NULL){
00877 check(xsh_add_product_table( dispersol_frame, frameset,parameters,
00878 RECIPE_ID, instrument,XSH_DISP_TAB));
00879 }
00880 sprintf(tag,"WAVE_ON");
00881 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00882 check(xsh_add_product_image(dmap_rmbias,frameset,parameters,
00883 RECIPE_ID,instrument,tag));
00884 } else {
00885
00886 check(xsh_add_product_image(dmap_rmdark,frameset,parameters,
00887 RECIPE_ID,instrument,tag));
00888
00889 }
00890
00891 if(opt_model_config_frame!=NULL) {
00892 check(xsh_add_product_table(opt_model_config_frame, frameset,
00893 parameters, RECIPE_ID, instrument,NULL));
00894 }
00895
00896 xsh_msg("xsh_2dmap success!!");
00897
00898 cleanup:
00899 xsh_end( RECIPE_ID, frameset, parameters );
00900
00901 XSH_FREE( detect_arclines_clipping);
00902 XSH_FREE( detect_arclines_p);
00903 XSH_FREE( dispsol_param);
00904
00905 xsh_free_frameset(&raws);
00906 xsh_free_frameset(&calib);
00907 xsh_free_frameset(&on);
00908 xsh_free_frameset(&off);
00909 xsh_free_frameset(&on_off);
00910 xsh_free_frame(&clean_arclines);
00911 xsh_free_frame(&resid_map);
00912 xsh_free_frame(&resid_dan);
00913 xsh_free_frame(&bpmap);
00914 xsh_free_frame(&wave_tab_2d);
00915 xsh_free_frame(&dmap_rmbias);
00916 xsh_free_frame(&dmap_rmdark);
00917 xsh_free_frame(&dispersol_frame);
00918 xsh_free_frame(&slitmap_frame);
00919 xsh_free_frame(&opt_model_config_frame);
00920 xsh_free_propertylist(&plist);
00921
00922 xsh_instrument_free(&instrument);
00923 xsh_free_frame( &wave_map ) ;
00924 XSH_FREE( prefix ) ;
00925 return;
00926 }
00927