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"
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_TAB_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 A clean arc list PRO.CATG=ARC_LINE_LIST_2DMAP_arm\n\
00126 Wavelength table solution, PRO.CATG = WAVE_TAB_2D_arm [poly mode]\n\
00127 Wavelength image map solution, PRO.CATG = WAVE_RESID_TAB_2D_arm\n\
00128 A Residual tab, PRO.CATG=WAVE_RESID_TAB_SLIT_arm\n\
00129 A Wavelelength map, PRO.CATG=WAVE_MAP_arm. [if model-wavemap-compute=TRUE]\n\
00130 A Slit map image, PRO.CATG=SLIT_MAP_NIR [if model-wavemap-compute=TRUE]\n\
00131 A Dispersion solution table, PRO.CATG=DISP_TAB_NIR\n\
00132 An Arc frame in pre format bias subtracted, \n\
00133 PRO.CATG=WAVE_BIAS_SUBTRACT_arm if arm=UVB|VIS and PRO.CATG=WAVE_NIR_ON if NIR\n\
00134 The optimized model cfg frame, PRO.CATG=MODEL_CONFIG_OPT_arm [if physical model mode].\n";
00135
00136
00137
00138
00139
00140
00141
00142
00151
00152
00153 int cpl_plugin_get_info(cpl_pluginlist *list) {
00154 cpl_recipe *recipe = NULL;
00155 cpl_plugin *plugin = NULL;
00156
00157 recipe = cpl_calloc(1, sizeof(*recipe));
00158 if ( recipe == NULL ){
00159 return -1;
00160 }
00161
00162 plugin = &recipe->interface ;
00163
00164 cpl_plugin_init(plugin,
00165 CPL_PLUGIN_API,
00166 XSH_BINARY_VERSION,
00167 CPL_PLUGIN_TYPE_RECIPE,
00168 RECIPE_ID,
00169 xsh_2dmap_description_short,
00170 xsh_2dmap_description,
00171 RECIPE_AUTHOR,
00172 RECIPE_CONTACT,
00173 xsh_get_license(),
00174 xsh_2dmap_create,
00175 xsh_2dmap_exec,
00176 xsh_2dmap_destroy);
00177
00178 cpl_pluginlist_append(list, plugin);
00179
00180 return (cpl_error_get_code() != CPL_ERROR_NONE);
00181 }
00182
00183
00193
00194
00195 static int xsh_2dmap_create(cpl_plugin *plugin){
00196 cpl_recipe *recipe = NULL;
00197 xsh_clipping_param detarc_clip_param = {2.0, 0, 0.7, 0, 0.3};
00198 xsh_detect_arclines_param detarc_param =
00199 {-1, 2, 0, 5, 4, 1, 5, -1.0,
00200 XSH_GAUSSIAN_METHOD, FALSE};
00201 xsh_dispersol_param dispsol_param = { 5, 5 } ;
00202 char paramname[256];
00203 cpl_parameter* p=NULL;
00204
00205
00206 xsh_init();
00207
00208
00209 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00210
00211
00212 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00213 CPL_ERROR_TYPE_MISMATCH,
00214 "Plugin is not a recipe");
00215
00216 recipe = (cpl_recipe *)plugin;
00217
00218
00219 recipe->parameters = cpl_parameterlist_new();
00220 assure( recipe->parameters != NULL,
00221 CPL_ERROR_ILLEGAL_OUTPUT,
00222 "Memory allocation failed!");
00223
00224
00225 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
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",200,
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 check( xsh_parameters_new_boolean( recipe->parameters, RECIPE_ID,
00288 "model-compute-map",
00289 TRUE,
00290 "Compute the wavemap and the slitmap for the physical model."));
00291
00292
00293
00294
00295 cleanup:
00296 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00297 xsh_error_dump(CPL_MSG_ERROR);
00298 return 1;
00299 }
00300 else {
00301 return 0;
00302 }
00303 }
00304
00305
00311
00312
00313 static int xsh_2dmap_exec(cpl_plugin *plugin) {
00314 cpl_recipe *recipe = NULL;
00315
00316
00317 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00318
00319
00320 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00321 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00322
00323 recipe = (cpl_recipe *)plugin;
00324
00325 xsh_2dmap(recipe->parameters, recipe->frames);
00326
00327 cleanup:
00328 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00329 xsh_error_dump(CPL_MSG_ERROR);
00330 cpl_error_reset();
00331 return 1;
00332 }
00333 else {
00334 return 0;
00335 }
00336 }
00337
00338
00344
00345 static int xsh_2dmap_destroy(cpl_plugin *plugin)
00346 {
00347 cpl_recipe *recipe = NULL;
00348
00349
00350 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00351
00352
00353 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00354 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00355
00356 recipe = (cpl_recipe *)plugin;
00357
00358 xsh_free_parameterlist(&recipe->parameters);
00359
00360 cleanup:
00361 if (cpl_error_get_code() != CPL_ERROR_NONE)
00362 {
00363 return 1;
00364 }
00365 else
00366 {
00367 return 0;
00368 }
00369 }
00370
00371
00372
00373 static cpl_error_code
00374 xsh_verify_2dmap_poly_input(cpl_frame* order_tab_edges_frame,
00375 cpl_frame* spectralformat_frame,
00376 cpl_frame* theo_tab_frame)
00377 {
00378
00379 cpl_table* tab_edges=NULL;
00380 cpl_table* tab_spectral_format=NULL;
00381 cpl_table* theo_tab=NULL;
00382 const char* name=NULL;
00383 int ord_min_ref=0;
00384 int ord_max_ref=0;
00385 int ord_min=0;
00386 int ord_max=0;
00387
00388 check(name=cpl_frame_get_filename(order_tab_edges_frame));
00389 check(tab_edges=cpl_table_load(name,1,0));
00390 check(ord_min_ref=cpl_table_get_column_min(tab_edges,"ABSORDER"));
00391 check(ord_max_ref=cpl_table_get_column_max(tab_edges,"ABSORDER"));
00392
00393 check(name=cpl_frame_get_filename(spectralformat_frame));
00394 check(tab_spectral_format=cpl_table_load(name,1,0));
00395 check(ord_min=cpl_table_get_column_min(tab_spectral_format,"ORDER"));
00396 check(ord_max=cpl_table_get_column_max(tab_spectral_format,"ORDER"));
00397
00398 if(ord_min != ord_min_ref) {
00399 xsh_msg_error("Edge order table's ord_min != spectral format table's ord_min");
00400 return CPL_ERROR_INCOMPATIBLE_INPUT;
00401 }
00402
00403
00404 if(ord_max != ord_max_ref) {
00405 xsh_msg_error("Edge order table's ord_max != spectral format table's ord_max");
00406 return CPL_ERROR_INCOMPATIBLE_INPUT;
00407 }
00408
00409
00410 if(theo_tab_frame != NULL) {
00411 check(name=cpl_frame_get_filename(theo_tab_frame));
00412 check(theo_tab=cpl_table_load(name,1,0));
00413 check(ord_min=cpl_table_get_column_min(theo_tab,"Order"));
00414 check(ord_max=cpl_table_get_column_max(theo_tab,"Order"));
00415
00416 if(ord_min != ord_min_ref) {
00417 xsh_msg_error("Theo table's ord_min != spectral format table's ord_min");
00418 return CPL_ERROR_INCOMPATIBLE_INPUT;
00419 }
00420
00421
00422 if(ord_max != ord_max_ref) {
00423 xsh_msg_error("Theo table's ord_max != spectral format table's ord_max");
00424 return CPL_ERROR_INCOMPATIBLE_INPUT;
00425 }
00426
00427
00428 }
00429
00430
00431
00432 cleanup:
00433 xsh_free_table(&tab_edges);
00434 xsh_free_table(&tab_spectral_format);
00435 xsh_free_table(&theo_tab);
00436
00437 return cpl_error_get_code();
00438
00439 }
00440
00441 static cpl_error_code
00442 xsh_params_set_defaults(cpl_parameterlist* pars,
00443 xsh_detect_arclines_param* arclines_p,
00444 xsh_instrument* inst)
00445 {
00446 cpl_parameter* p=NULL;
00447 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-min-sn"));
00448 if(cpl_parameter_get_double(p) <= 0) {
00449 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00450 arclines_p->min_sn=3;
00451 } else if (xsh_instrument_get_arm(inst) == XSH_ARM_VIS) {
00452 arclines_p->min_sn=6;
00453 } else {
00454 arclines_p->min_sn=10;
00455 }
00456 }
00457
00458 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectarclines-fit-window-half-size"));
00459 if(cpl_parameter_get_int(p) <= 0) {
00460 if (xsh_instrument_get_arm(inst) == XSH_ARM_NIR){
00461 arclines_p->fit_window_hsize=3;
00462 } else {
00463 arclines_p->fit_window_hsize=4;
00464 }
00465 }
00466 cleanup:
00467
00468 return cpl_error_get_code();
00469
00470 }
00471
00472
00473
00481
00482 static void xsh_2dmap(cpl_parameterlist* parameters, cpl_frameset* frameset)
00483 {
00484 const char* recipe_tags[1] = {XSH_WAVE};
00485 int recipe_tags_size = 1;
00486
00487
00488 xsh_clipping_param* detect_arclines_clipping = NULL;
00489 xsh_detect_arclines_param* detect_arclines_p = NULL;
00490 xsh_dispersol_param *dispsol_param = NULL ;
00491 int model_map_compute= FALSE;
00492
00493
00494 cpl_frameset* raws = NULL;
00495 cpl_frameset* calib = NULL;
00496 cpl_frameset* on = NULL;
00497 cpl_frameset* off = NULL;
00498 cpl_frameset* on_off = NULL;
00499 cpl_frame* dmap_rmbias = NULL;
00500 cpl_frame* dmap_rmdark = NULL;
00501 xsh_instrument* instrument = NULL;
00502
00503 cpl_frame* model_config_frame = NULL;
00504 cpl_frame* opt_model_config_frame = NULL;
00505 cpl_frame* spectralformat_frame = NULL;
00506 cpl_frame* bpmap = NULL;
00507 cpl_frame* master_bias = NULL;
00508 cpl_frame* master_dark = NULL;
00509 cpl_frame* theo_tab_mult = NULL;
00510 cpl_frame* resid_map = NULL;
00511 cpl_frame* resid_dan = NULL;
00512 cpl_frame* arclines = NULL;
00513 cpl_frame* clean_arclines = NULL;
00514 cpl_frame* wave_tab_guess = NULL;
00515 cpl_frame* wave_tab_2d = NULL;
00516 cpl_frame * order_tab_edges = NULL ;
00517 cpl_frame * wave_map = NULL ;
00518 cpl_frame * line_intmon = NULL ;
00519 char wave_map_tag[80];
00520 char slit_map_tag[80];
00521
00522 char *prefix = NULL ;
00523 char filename[256];
00524 int maxit=200;
00525 double ann_fac=1.0;
00526 int scenario=4;
00527 int solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
00528 char paramname[256];
00529 cpl_parameter * p =NULL;
00530 cpl_frame *dispersol_frame = NULL;
00531 cpl_frame *slitmap_frame = NULL;
00532 int pre_overscan_corr=0;
00533 cpl_propertylist* plist=NULL;
00534 double exptime=0;
00535 cpl_boolean mode_phys;
00536 char tag[80];
00537 int resid_name_sw=0;
00538
00539
00540
00541 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib, recipe_tags,
00542 recipe_tags_size,
00543 RECIPE_ID, XSH_BINARY_VERSION,
00544 xsh_2dmap_description_short ));
00545
00546 xsh_recipe_params_check(parameters,instrument,RECIPE_ID);
00547
00548
00549
00550 mode_phys=xsh_mode_is_physmod(calib,instrument);
00551
00552 check(bpmap=xsh_check_load_master_bpmap(calib,instrument,RECIPE_ID));
00553
00554 cknull_msg(arclines = xsh_find_frame_with_tag(calib,
00555 XSH_ARC_LINE_LIST,
00556 instrument),
00557 "Missing %s frame",XSH_ARC_LINE_LIST);
00558 check(order_tab_edges = xsh_find_order_tab_edges(calib,instrument));
00559
00560 if (!mode_phys) {
00561 if((theo_tab_mult = xsh_find_frame_with_tag( calib,XSH_THEO_TAB_MULT,
00562 instrument)) == NULL) {
00563
00564 }
00565 solution_type=XSH_DETECT_ARCLINES_TYPE_POLY;
00566 } else {
00567 if((model_config_frame = xsh_find_frame_with_tag(calib,
00568 XSH_MOD_CFG_OPT_FMT,
00569 instrument)) == NULL) {
00570
00571 xsh_error_reset();
00572
00573 if ((model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
00574 instrument)) == NULL) {
00575 xsh_error_reset();
00576 }
00577
00578 }
00579 solution_type=XSH_DETECT_ARCLINES_TYPE_MODEL;
00580 }
00581 if( (model_config_frame!=NULL) && (theo_tab_mult != NULL) ) {
00582
00583 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",
00584 XSH_THEO_TAB_MULT,XSH_MOD_CFG_TAB);
00585 goto cleanup;
00586 }
00587
00588 if((model_config_frame==NULL) && (theo_tab_mult == NULL) ) {
00589 xsh_msg_error("You must provide either a %s or a %s frame",
00590 XSH_THEO_TAB_MULT,XSH_MOD_CFG_TAB);
00591 goto cleanup;
00592
00593 }
00594
00595
00596 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
00597 XSH_SPECTRAL_FORMAT, instrument));
00598
00599
00600 if( (spectralformat_frame = xsh_find_frame_with_tag( calib,
00601 XSH_SPECTRAL_FORMAT,
00602 instrument)) == NULL) {
00603 xsh_msg("Frame %s not provided",
00604 XSH_SPECTRAL_FORMAT);
00605 xsh_error_reset();
00606 }
00607
00608
00609 if((line_intmon = xsh_find_frame_with_tag( calib, XSH_ARC_LINE_LIST_INTMON,
00610 instrument)) == NULL) {
00611 xsh_error_reset();
00612 }
00613
00614
00615
00616
00617
00618
00619 if(theo_tab_mult!=NULL) {
00620 check(xsh_verify_2dmap_poly_input(order_tab_edges, spectralformat_frame,
00621 theo_tab_mult));
00622 }
00623
00624
00625
00626 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00627
00628 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) == 1);
00629 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00630 instrument)) == NULL) {
00631
00632 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00633 xsh_error_reset();
00634 }
00635
00636
00637 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00638 instrument)) == NULL){
00639 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00640 xsh_error_reset();
00641 }
00642 }
00643
00644 else {
00645
00646 check(xsh_dfs_split_nir(raws,&on,&off));
00647 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(on) == 1);
00648 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(off) == 1);
00649 }
00650 check( xsh_instrument_update_from_spectralformat( instrument,
00651 spectralformat_frame));
00652
00653
00654
00655
00656 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
00657 "pre-overscan-corr"));
00658
00659 check( detect_arclines_clipping =
00660 xsh_parameters_clipping_detect_arclines_get( RECIPE_ID, parameters));
00661 check( detect_arclines_p = xsh_parameters_detect_arclines_get( RECIPE_ID,
00662 parameters));
00663 check( dispsol_param = xsh_parameters_dispersol_get( RECIPE_ID,parameters)) ;
00664 check( model_map_compute = xsh_parameters_get_boolean( parameters,
00665 RECIPE_ID,
00666 "model-compute-map"));
00667
00668
00669 check(xsh_params_set_defaults(parameters,detect_arclines_p,instrument));
00670
00671
00672
00673
00674
00675 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00676 cpl_frame* multi_pin_hole = NULL;
00677
00678
00679 check(xsh_prepare(raws,bpmap, master_bias, XSH_WAVE, instrument,
00680 pre_overscan_corr));
00681 check(multi_pin_hole = cpl_frameset_get_first(raws));
00682 if(master_bias != NULL) {
00683
00684 check(dmap_rmbias = xsh_subtract_bias(multi_pin_hole,master_bias,
00685 instrument,"WAVE_",pre_overscan_corr,0));
00686 } else {
00687 dmap_rmbias=cpl_frame_duplicate(multi_pin_hole);
00688 }
00689 if(master_dark != NULL) {
00690
00691 strcpy(filename,xsh_stringcat_any( "WAVE_DARK_",
00692 xsh_instrument_arm_tostring( instrument ),
00693 ".fits", NULL )) ;
00694
00695 check(dmap_rmdark = xsh_subtract_dark(dmap_rmbias, master_dark,
00696 filename, instrument));
00697 } else {
00698 dmap_rmdark=cpl_frame_duplicate(dmap_rmbias);
00699 }
00700 }
00701
00702 else{
00703
00704 check(xsh_prepare(on,bpmap, NULL, "ON", instrument,pre_overscan_corr));
00705
00706 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr));
00707
00708
00709
00710 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
00711 check(dmap_rmdark = cpl_frame_duplicate(
00712 cpl_frameset_get_first(on_off)));
00713 }
00714
00715 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00716 check(xsh_check_input_is_unbinned(dmap_rmdark));
00717 }
00718
00719 plist=cpl_propertylist_load(cpl_frame_get_filename(dmap_rmdark),0);
00720 exptime=xsh_pfits_get_exptime(plist);
00721 xsh_free_propertylist(&plist);
00722
00723
00724 if(model_config_frame!=NULL) {
00725 resid_name_sw=1;
00726 check( xsh_detect_arclines_dan(dmap_rmdark, theo_tab_mult,
00727 arclines, wave_tab_guess, NULL,
00728 model_config_frame, spectralformat_frame, NULL,
00729 &clean_arclines, &wave_tab_2d,
00730 &resid_dan, XSH_SOLUTION_ABSOLUTE,
00731 detect_arclines_p, detect_arclines_clipping,
00732 instrument,RECIPE_ID,0,resid_name_sw));
00733
00734 xsh_free_frame(&clean_arclines);
00735 xsh_free_frame(&wave_tab_2d);
00736 }
00737
00738 check( xsh_detect_arclines(dmap_rmdark, theo_tab_mult,
00739 arclines, wave_tab_guess, NULL,
00740 model_config_frame, spectralformat_frame, NULL,
00741 &clean_arclines, &wave_tab_2d,
00742 &resid_map, XSH_SOLUTION_ABSOLUTE,
00743 detect_arclines_p, detect_arclines_clipping,
00744 instrument,RECIPE_ID,0,resid_name_sw));
00745
00746
00747
00748 sprintf(slit_map_tag,"SLIT_MAP_%s",
00749 xsh_instrument_arm_tostring( instrument )) ;
00750
00751 sprintf(wave_map_tag,"WAVE_MAP_%s",
00752 xsh_instrument_arm_tostring( instrument )) ;
00753
00754
00755 check( xsh_data_check_spectralformat( spectralformat_frame,
00756 order_tab_edges, wave_tab_2d, model_config_frame, instrument));
00757
00758 if ( model_config_frame == NULL){
00759
00760 check( wave_map = xsh_create_poly_wavemap( dmap_rmdark, wave_tab_2d,
00761 order_tab_edges,
00762 spectralformat_frame,
00763 dispsol_param, instrument,
00764 wave_map_tag, &dispersol_frame,
00765 &slitmap_frame));
00766 }
00767 else {
00768
00769 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-maxit");
00770 check(p = cpl_parameterlist_find(parameters,paramname));
00771 check(maxit=cpl_parameter_get_int(p));
00772
00773 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-anneal-factor");
00774 check(p = cpl_parameterlist_find(parameters,paramname));
00775 check(ann_fac=cpl_parameter_get_double(p));
00776
00777 sprintf(paramname,"xsh.%s.%s",RECIPE_ID, "model-scenario");
00778 check(p = cpl_parameterlist_find(parameters,paramname));
00779 check(scenario=cpl_parameter_get_int(p));
00780 xsh_msg_dbg_low("maxit=%d ann_fac=%g scenario=%d",maxit,ann_fac,scenario);
00781
00782 check(opt_model_config_frame=xsh_model_pipe_anneal(model_config_frame,
00783 resid_map,
00784 maxit,ann_fac,
00785 scenario,2));
00786
00787
00788 if (model_map_compute){
00789 check( xsh_create_model_map( opt_model_config_frame, instrument,
00790 wave_map_tag,slit_map_tag,
00791 &wave_map, &slitmap_frame,0));
00792
00793
00794
00795 check(dispersol_frame=xsh_create_dispersol_physmod(dmap_rmdark,
00796 order_tab_edges,
00797 opt_model_config_frame,
00798 wave_map,
00799 slitmap_frame,
00800 dispsol_param,
00801 spectralformat_frame,
00802 instrument,1));
00803 }
00804 }
00805 if(wave_map!=NULL) {
00806 check(xsh_wavemap_qc(wave_map,order_tab_edges));
00807 }
00808
00809 xsh_msg_dbg_low("QC wavetab");
00810 if ( model_config_frame == NULL){
00811 check(xsh_wavetab_qc(resid_map,true));
00812 } else {
00813 check(xsh_wavetab_qc(resid_map,false));
00814 }
00815
00816 if(line_intmon) {
00817 check(xsh_wavecal_qclog_intmon(resid_map,line_intmon,exptime,instrument));
00818 }
00819 check(xsh_table_merge_clean_and_resid_tabs(resid_map,clean_arclines));
00820
00821
00822
00823 xsh_msg("Saving products");
00824
00825 check(xsh_add_product_table(clean_arclines, frameset,
00826 parameters, RECIPE_ID, instrument,NULL));
00827
00828 if ( wave_tab_2d != NULL){
00829 check(xsh_add_product_table(wave_tab_2d, frameset,
00830 parameters, RECIPE_ID, instrument,NULL));
00831 }
00832
00833 if(model_config_frame!=NULL) {
00834
00835 check(xsh_frame_table_resid_merge(resid_dan,resid_map,solution_type));
00836 check(xsh_add_product_table( resid_dan, frameset,
00837 parameters, RECIPE_ID, instrument,NULL));
00838
00839
00840
00841
00842 } else {
00843 check(xsh_add_product_table( resid_map, frameset,parameters, RECIPE_ID,
00844 instrument,"WAVE_RESID_TAB_LINES"));
00845 }
00846 if ( wave_map != NULL){
00847 check(xsh_add_product_image( wave_map, frameset,
00848 parameters, RECIPE_ID, instrument, XSH_WAVE_MAP ));
00849 }
00850 if (slitmap_frame != NULL){
00851 check(xsh_add_product_image( slitmap_frame, frameset,
00852 parameters, RECIPE_ID, instrument, XSH_SLIT_MAP ));
00853 }
00854 if ( dispersol_frame != NULL){
00855 check(xsh_add_product_table( dispersol_frame, frameset,parameters,
00856 RECIPE_ID, instrument,XSH_DISP_TAB));
00857 }
00858
00859 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00860 check(xsh_add_product_image(dmap_rmbias,frameset,parameters,
00861 RECIPE_ID,instrument,NULL));
00862 } else {
00863 sprintf(tag,"WAVE_ON");
00864 check(xsh_add_product_image(dmap_rmdark,frameset,parameters,
00865 RECIPE_ID,instrument,tag));
00866
00867 }
00868
00869 if(opt_model_config_frame!=NULL) {
00870 check(xsh_add_product_table(opt_model_config_frame, frameset,
00871 parameters, RECIPE_ID, instrument,NULL));
00872 }
00873
00874 xsh_msg("xsh_2dmap success!!");
00875
00876 cleanup:
00877 xsh_end( RECIPE_ID, frameset, parameters );
00878
00879 XSH_FREE( detect_arclines_clipping);
00880 XSH_FREE( detect_arclines_p);
00881 XSH_FREE( dispsol_param);
00882
00883 xsh_free_frameset(&raws);
00884 xsh_free_frameset(&calib);
00885 xsh_free_frameset(&on);
00886 xsh_free_frameset(&off);
00887 xsh_free_frameset(&on_off);
00888 xsh_free_frame(&clean_arclines);
00889 xsh_free_frame(&resid_map);
00890 xsh_free_frame(&resid_dan);
00891 xsh_free_frame(&bpmap);
00892 xsh_free_frame(&wave_tab_2d);
00893 xsh_free_frame(&dmap_rmbias);
00894 xsh_free_frame(&dmap_rmdark);
00895 xsh_free_frame(&dispersol_frame);
00896 xsh_free_frame(&slitmap_frame);
00897 xsh_free_frame(&opt_model_config_frame);
00898 xsh_free_propertylist(&plist);
00899
00900 xsh_instrument_free(&instrument);
00901 xsh_free_frame( &wave_map ) ;
00902 XSH_FREE( prefix ) ;
00903 return;
00904 }
00905