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
00045
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <xsh_error.h>
00057
00058 #include <xsh_utils.h>
00059 #include <xsh_msg.h>
00060
00061
00062
00063 #include <xsh_dfs.h>
00064
00065
00066 #include <xsh_drl.h>
00067 #include <xsh_pfits.h>
00068
00069
00070
00071
00072 #include <cpl.h>
00073 #include <math.h>
00074
00075 #include <xsh_data_resid_tab.h>
00076 #include <xsh_utils_table.h>
00077
00078 #include <xsh_model_kernel.h>
00079 #include <xsh_model_utils.h>
00080 #include <xsh_model_io.h>
00081 #include <xsh_spectrum.h>
00082 #include<xsh_model_utils.h>
00083 #include <xsh_fit.h>
00084
00085 #include <stdlib.h>
00086
00087
00088
00089
00090
00091 #define RECIPE_ID "xsh_cfg_recover"
00092 #define RECIPE_AUTHOR "A. Modigliani, P. Bristow"
00093 #define RECIPE_CONTACT "amodigli@eso.org"
00094
00095 #define XSH_THE_TAB_SUB_VIS "XSH_THE_TAB_VIS"
00096 #define XSH_THE_TAB_SUB_UVB "XSH_THE_TAB_UVB"
00097 #define XSH_THE_TAB_SUB_NIR "XSH_THE_TAB_NIR"
00098
00099 #define XSH_STARTUP_TABLE_COLNAME_X "X"
00100
00101
00102 #define XSH_ORDPOS_POL_DIM_MAX 4
00103
00104
00105
00106
00107
00108
00109
00110
00111 static cpl_error_code
00112 xsh_cfg_recover_prepare_pm_set(cpl_table* tab_pat,
00113 cpl_table* tab_dat,
00114 cpl_parameterlist* parameters,
00115 cpl_matrix** mat_pat,
00116 cpl_matrix** mat_dat,
00117 int* use_pat,
00118 int* use_dat);
00119
00120
00121
00122 static cpl_error_code
00123 xsh_cfg_recover_model_THE_create(cpl_frame* config_frame,
00124 xsh_instrument* instrument,
00125 cpl_frame* wave_list,
00126 cpl_frame** THE1, cpl_frame** THE9);
00127
00128
00129 static cpl_error_code
00130 xsh_cfg_recover_pattern_match(cpl_parameterlist* parameters,
00131 cpl_matrix* mat_gue,
00132 cpl_matrix* mat_dat,
00133 int use_pattern,
00134 int use_data,
00135 int debug_level);
00136
00137
00138
00139 static cpl_error_code
00140 xsh_cfg_recover_guess_tab_corr_by_user(cpl_parameterlist* parameters,
00141 cpl_frame** model_xy_gue);
00142
00143
00144 static cpl_error_code
00145 xsh_cfg_recover_measure_line_xy_fit(cpl_frame* raw_frm,
00146 cpl_parameterlist* parameters,
00147 cpl_frame** model_xy_gue,
00148 int debug_level);
00149
00150
00151 static cpl_error_code
00152 xsh_cfg_recover_add_peaks_xpos(cpl_frame* order_tab_centr,
00153 xsh_instrument* instr,
00154 cpl_table** tab_xy_peaks);
00155
00156
00157
00158 static cpl_table*
00159 xsh_cfg_recover_remove_blends(cpl_table* tab_xy_guess,
00160 cpl_table* tab_xy_peaks_sel,
00161 const int thresh_x,
00162 const int thresh_y);
00163
00164
00165 static cpl_table*
00166 xsh_cfg_recover_measure_tab_xy_peaks(cpl_image* ima_ext,
00167 cpl_parameterlist* parameters);
00168
00169 static cpl_table*
00170 xsh_cfg_recover_select_peaks(cpl_table* tab_xy_guess,
00171 cpl_table* tab_xy_peaks,
00172 const double factor);
00173
00174 static cpl_error_code
00175 xsh_cfg_recover_extend_xy_pos_frm(cpl_frame** frm,xsh_instrument* instrument);
00176
00177 static cpl_error_code
00178 xsh_cfg_recover_measure_line_xy(cpl_frame* frame,
00179 xsh_instrument* inst,
00180 cpl_frame* order_tab_centr,
00181 cpl_parameterlist* parameters,
00182 const char* method,
00183 cpl_frame* model_config,
00184 cpl_frame** guess,
00185 int debug_level);
00186
00187 static cpl_error_code
00188 xsh_cfg_recover_gen_xyg(
00189 cpl_table * lines_tab,
00190 struct xs_3* p_xs_3_config,
00191 xsh_instrument* inst,int pre_scan,
00192 cpl_table ** lines_gue);
00193
00194
00195 static cpl_frame*
00196 xsh_cfg_recover_gen_xyg_frame(
00197 cpl_frame * wave_list,
00198 cpl_frame * config_frame,
00199 xsh_instrument* instr,int prescan);
00200
00201
00202 static cpl_image*
00203 xsh_cfg_recover_linear_ext(cpl_frame* raw_frm,
00204 cpl_frame* order_tab_centr,
00205 xsh_instrument* instr,
00206 const int slit,
00207 const double thresh_min);
00208
00209 static cpl_error_code
00210 xsh_cfg_recover_guess_tab_corr_by_ordpos(xsh_instrument* instr,
00211 cpl_frame* order_tab_centr,
00212 cpl_frame** model_xy_gue);
00213
00214
00215
00216
00217 static int xsh_cfg_recover_create(cpl_plugin *);
00218 static int xsh_cfg_recover_exec(cpl_plugin *);
00219 static int xsh_cfg_recover_destroy(cpl_plugin *);
00220
00221
00222 static int
00223 xsh_cfg_recover_last_step(cpl_parameterlist* parameters,
00224 cpl_frameset* frameset,
00225 xsh_instrument* instrument,
00226 cpl_frameset* raws,
00227 cpl_frameset* calib);
00228
00229 static cpl_error_code
00230 xsh_cfg_recover_driver(cpl_parameterlist* parameters,
00231 cpl_frameset* frameset);
00232
00233
00234
00235
00236 static char xsh_cfg_recover_description_short[] =
00237 "Optimizes a model configuration to match data taken after a major format change";
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 static char xsh_cfg_recover_description[] =
00250 "This recipe creates a wavelength solution and an order table.\n\
00251 Input Frames :\n\
00252 - [UVB, VIS] A RAW frame (Format = RAW, Tag = FMTCHK_arm)\n\
00253 - [NIR] Two RAW frames (Format = RAW, Tag = FMTCHK_arm_ON,FMTCHK_arm_OFF)\n\
00254 - The old model cfg table (Format = TABLE, Tag = XSH_MOD_CFG_TAB_arm)\n\
00255 - A ref. line list. The model computes corresponding positions \n\
00256 (Format = TABLE, Tag = ARC_LINE_LIST_arm)\n\
00257 - [UVB,VIS,OPTIONAL] A master bias (Format = PRE, Tag = MASTER_BIAS_arm)\n\
00258 - [UVB,VIS,OPTIONAL] A master dark (Format = PRE, Tag = MASTER_DARK_arm)\n\
00259 - [OPTIONAL-Required if method=pm,peaks] \n\
00260 An order table (Format = TABLE, Tag = ORDER_TAB_CENTR_arm)\n\
00261 - [OPTIONAL-Required if first-anneal=TRUE] \n\
00262 A table with measured line positions (Format = TABLE, Tag = XSH_MEASCOORD_arm)\n\
00263 Products : \n\
00264 - if first-anneal=FALSE & last-step=FALSE\n\
00265 nothing\n\
00266 - if first-anneal=TRUE & last-step=FALSE\n\
00267 an optimized model configuration, PRO.CATG=XSH_MOD_CFG_arm\n\
00268 - if last-step=TRUE\n\
00269 an optimized model configuration, PRO.CATG=XSH_MOD_FAN_arm\n\
00270 an optimized model configuration, PRO.CATG=XSH_MOD_CFG_OPT_arm\n\
00271 an quality control table, PRO.CATG=MODEL_GUESS_XY_arm\n\
00272 the model theoretical map corresponding to the optimized model config,\n\
00273 PRO.CATG=THEO_TAB_MULT_arm, THEO_TAB_IFU_arm, and THEO_TAB_SING_arm\n\
00274 \n";
00275
00276
00277
00278
00279
00280
00281
00282
00283
00292
00293
00294 int cpl_plugin_get_info(cpl_pluginlist *list) {
00295 cpl_recipe *recipe = NULL;
00296 cpl_plugin *plugin = NULL;
00297
00298 recipe = cpl_calloc(1, sizeof(*recipe));
00299 if ( recipe == NULL ){
00300 return -1;
00301 }
00302
00303 plugin = &recipe->interface ;
00304
00305 cpl_plugin_init(plugin,
00306 CPL_PLUGIN_API,
00307 XSH_BINARY_VERSION,
00308 CPL_PLUGIN_TYPE_RECIPE,
00309 RECIPE_ID,
00310 xsh_cfg_recover_description_short,
00311 xsh_cfg_recover_description,
00312 RECIPE_AUTHOR,
00313 RECIPE_CONTACT,
00314 xsh_get_license(),
00315 xsh_cfg_recover_create,
00316 xsh_cfg_recover_exec,
00317 xsh_cfg_recover_destroy);
00318
00319 cpl_pluginlist_append(list, plugin);
00320
00321 return (cpl_error_get_code() != CPL_ERROR_NONE);
00322 }
00323
00324
00325
00326
00346
00347 static int xsh_cfg_recover_create(cpl_plugin * plugin)
00348 {
00349 cpl_recipe * recipe ;
00350 cpl_parameter * p ;
00351
00352
00353
00354
00355
00356
00357
00358 xsh_init();
00359
00360
00361
00362 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00363 recipe = (cpl_recipe *)plugin ;
00364 else return -1 ;
00365
00366
00367 recipe->parameters = cpl_parameterlist_new() ;
00368
00369 assure( recipe->parameters != NULL,
00370 CPL_ERROR_ILLEGAL_OUTPUT,
00371 "Memory allocation failed!");
00372
00373
00374 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00375
00376
00377
00378
00379 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.first-anneal",
00380 CPL_TYPE_BOOL,
00381 "Run first annealing (TRUE) or not (FALSE)"
00382 "See recipe man-page % Input frames",
00383 "xsh.xsh_cfg_recover",CPL_FALSE);
00384
00385 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"first-anneal");
00386 cpl_parameterlist_append(recipe->parameters,p);
00387
00388
00389
00390
00391 p = cpl_parameter_new_enum("xsh.xsh_model_compute.arm",
00392 CPL_TYPE_STRING,
00393 "Arm setting: ",
00394 "xsh.xsh_model_compute",
00395 "vis",
00396 3,"uvb","vis","nir");
00397
00398 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"arm");
00399 cpl_parameterlist_append(recipe->parameters, p);
00400
00401
00402 p = cpl_parameter_new_value("xsh.xsh_model_compute.name_i",
00403 CPL_TYPE_STRING,
00404 "Filename with wavelength,x,y,order: ",
00405 "xsh.xsh_model_compute",
00406 "line_xy_ord.txt");
00407
00408 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"name_i");
00409 cpl_parameterlist_append(recipe->parameters, p);
00410
00411
00412 p = cpl_parameter_new_value("xsh.xsh_model_compute.niter",
00413 CPL_TYPE_INT,"No of iterations for first anneal",
00414 "xsh.xsh_model_compute", 100000);
00415
00416 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"niter");
00417 cpl_parameterlist_append(recipe->parameters,p);
00418
00419 p = cpl_parameter_new_value("xsh.xsh_model_compute.coord_frame",
00420 CPL_TYPE_INT,"Co-ordinate frame for centroids (0=raw,1=pre)",
00421 "xsh.xsh_model_compute", 1);
00422
00423 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"coord_frame");
00424 cpl_parameterlist_append(recipe->parameters,p);
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.last-step",
00449 CPL_TYPE_BOOL,
00450 "Run last step (TRUE) or not (FALSE)"
00451 "See recipe man-page % Input frames",
00452 "xsh.xsh_cfg_recover",CPL_FALSE);
00453
00454 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"last-step");
00455 cpl_parameterlist_append(recipe->parameters,p);
00456
00457
00458 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.plot",
00459 CPL_TYPE_BOOL,
00460 "Display plot (TRUE) or not (FALSE)",
00461 "xsh.xsh_cfg_recover",CPL_FALSE);
00462
00463 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"plot");
00464 cpl_parameterlist_append(recipe->parameters,p);
00465
00466 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.ima_thresh_min",
00467 CPL_TYPE_DOUBLE,
00468 "Min thresh raw image.",
00469 "xsh.xsh_cfg_recover", 40.);
00470
00471 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ima_tresh_min");
00472 cpl_parameterlist_append(recipe->parameters,p);
00473
00474
00475 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.cor_prescan",
00476 CPL_TYPE_BOOL,"Correct for prescan",
00477 "xsh.xsh_cfg_recover", FALSE);
00478
00479 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"cor_prescan");
00480 cpl_parameterlist_append(recipe->parameters,p);
00481
00482
00483 p = cpl_parameter_new_enum("xsh.xsh_cfg_recover.method",
00484 CPL_TYPE_STRING,
00485 "Model predictions correction method. "
00486 "safefit: safe fit"
00487 "gfit: 2D Gaussian line fit"
00488 "peaks: line peaks detection"
00489 "pm: line peaks detection & pattern match",
00490 "xsh.xsh_cfg_recover",
00491 "safefit", 4,
00492 "safefit","gfit","peaks", "pm");
00493
00494
00495
00496
00497 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"method");
00498 cpl_parameterlist_append(recipe->parameters,p);
00499
00500
00501 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.offx",
00502 CPL_TYPE_DOUBLE,"X offset to model predictions",
00503 "xsh.xsh_cfg_recover", 0.);
00504
00505 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"offx");
00506 cpl_parameterlist_append(recipe->parameters,p);
00507
00508
00509 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.offy",
00510 CPL_TYPE_DOUBLE,"Y offset to model predictions",
00511 "xsh.xsh_cfg_recover", 0.);
00512
00513 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"offy");
00514 cpl_parameterlist_append(recipe->parameters,p);
00515
00516
00517
00518 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.slit",
00519 CPL_TYPE_INT,"Extraction slit",
00520 "xsh.xsh_cfg_recover", 5);
00521
00522 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"slit");
00523 cpl_parameterlist_append(recipe->parameters,p);
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.gfit_box_sy",
00538 CPL_TYPE_INT,"Gauss fit Y search box size "
00539 "for lines on actual frame",
00540 "xsh.xsh_cfg_recover", 20);
00541
00542 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"gfit_box_sy");
00543 cpl_parameterlist_append(recipe->parameters,p);
00544
00545
00546 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.peak_line_fwhm",
00547 CPL_TYPE_INT,
00548 "The FWHM used in line convolution, "
00549 "in pixel units",
00550 "xsh.xsh_cfg_recover", 4);
00551
00552 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"peak_line_fwhm");
00553 cpl_parameterlist_append(recipe->parameters,p);
00554
00555
00556 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.peak_kappa",
00557 CPL_TYPE_DOUBLE,
00558 "The kappa value, used to identify line peaks "
00559 "if max>kappa*stdev+median, max is a valid peak "
00560 "where max, stdev,median are computed on the "
00561 "extracted spectrum",
00562 "xsh.xsh_cfg_recover", 5.);
00563
00564 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"peak_kappa");
00565 cpl_parameterlist_append(recipe->parameters,p);
00566
00567
00568
00569
00570 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.peak_factor",
00571 CPL_TYPE_DOUBLE,
00572 "Relative Intensity threshold factor "
00573 "for line peaks detection",
00574 "xsh.xsh_cfg_recover", 10.);
00575
00576 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"peak_factor");
00577 cpl_parameterlist_append(recipe->parameters,p);
00578
00579
00580 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.peak_match_x",
00581 CPL_TYPE_INT,"Radii for line peaks matches",
00582 "xsh.xsh_cfg_recover", 10);
00583
00584 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"peak_match_x");
00585 cpl_parameterlist_append(recipe->parameters,p);
00586
00587
00588 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.peak_match_y",
00589 CPL_TYPE_INT,"Radii for line peaks matches",
00590 "xsh.xsh_cfg_recover", 20);
00591
00592 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"peak_match_y");
00593 cpl_parameterlist_append(recipe->parameters,p);
00594
00595
00596 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.pm_ord_sel",
00597 CPL_TYPE_INT,
00598 "From guess line and peaks positions "
00599 "are extracted the ones in the range "
00600 "[ord_min,ord_min+pm_ord_sel] ",
00601 "xsh.xsh_cfg_recover", 1);
00602
00603 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"pm_ord_sel");
00604 cpl_parameterlist_append(recipe->parameters,p);
00605
00606
00607 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.pm_radius",
00608 CPL_TYPE_DOUBLE,
00609 "Search radius applied in final pattern "
00610 "matching (data units).",
00611 "xsh.xsh_cfg_recover", 20.);
00612
00613 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"pm_radius");
00614 cpl_parameterlist_append(recipe->parameters,p);
00615
00616 p = cpl_parameter_new_range("xsh.xsh_cfg_recover.pm_tolerance",
00617 CPL_TYPE_DOUBLE,
00618 "Max relative difference of angles and scales "
00619 "from their median value for match acceptance.",
00620 "xsh.xsh_cfg_recover", 0.1,0.001,0.5);
00621
00622 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"pm_tolerance");
00623 cpl_parameterlist_append(recipe->parameters,p);
00624
00625
00626
00627 p = cpl_parameter_new_value("xsh.xsh_cfg_recover.anneal_niter",
00628 CPL_TYPE_INT,"Simulated annealing iterations",
00629 "xsh.xsh_cfg_recover", 1000);
00630
00631 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"anneal_niter");
00632 cpl_parameterlist_append(recipe->parameters,p);
00633
00634
00635
00636
00637 cleanup:
00638 return 0;
00639 }
00640
00641
00642
00643
00649
00650
00651 static int xsh_cfg_recover_exec(cpl_plugin *plugin) {
00652 cpl_recipe *recipe = NULL;
00653 int recipe_status=0;
00654 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00655
00656
00657 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00658
00659
00660 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00661 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00662
00663 recipe = (cpl_recipe *)plugin;
00664
00665
00666 check(xsh_cfg_recover_driver(recipe->parameters, recipe->frames));
00667
00668 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00669
00670
00671 xsh_free_parameterlist(&recipe->parameters);
00672 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00673 }
00674
00675 cleanup:
00676 return recipe_status;
00677
00678 }
00679
00680
00686
00687 static int xsh_cfg_recover_destroy(cpl_plugin *plugin)
00688 {
00689 cpl_recipe *recipe = NULL;
00690
00691
00692 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00693
00694
00695 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00696 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00697
00698 recipe = (cpl_recipe *)plugin;
00699
00700 xsh_free_parameterlist(&recipe->parameters);
00701
00702 cleanup:
00703 if (cpl_error_get_code() != CPL_ERROR_NONE)
00704 {
00705 return 1;
00706 }
00707 else
00708 {
00709 return 0;
00710 }
00711 }
00712
00713 static cpl_error_code
00714 xsh_cfg_recover_driver(cpl_parameterlist* parameters,
00715 cpl_frameset* frameset)
00716 {
00717
00718 const char* recipe_tags[1] = {XSH_FMTCHK};
00719 int recipe_tags_size = 1;
00720
00721 cpl_parameter* p=NULL;
00722 int first_anneal=0;
00723
00724 int last_step=0;
00725 cpl_frameset* raws=NULL;
00726 cpl_frameset* calib=NULL;
00727 xsh_instrument* instrument=NULL;
00728 int recipe_status=0;
00729
00730
00731
00732
00733
00734
00735
00736 check(p = cpl_parameterlist_find(parameters,
00737 "xsh.xsh_cfg_recover.first-anneal"));
00738 check(first_anneal = cpl_parameter_get_bool(p));
00739
00740
00741 check(p = cpl_parameterlist_find(parameters,
00742 "xsh.xsh_cfg_recover.last-step"));
00743 check(last_step = cpl_parameter_get_bool(p));
00744
00745
00746
00747
00748
00749
00750
00751 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00752 recipe_tags, recipe_tags_size, RECIPE_ID,
00753 XSH_BINARY_VERSION,
00754 xsh_cfg_recover_description_short ) ) ;
00755
00756 if(first_anneal) {
00757 check_msg(recipe_status=xsh_model_first_anneal(parameters,frameset),
00758 "error_performing first anneal");
00759 }
00760
00761
00762
00763
00764
00765
00766
00767
00768 if(last_step) {
00769 check_msg(recipe_status = xsh_cfg_recover_last_step(parameters,frameset,
00770 instrument,raws,calib),
00771 "error performing startup");
00772 }
00773
00774 xsh_free_frameset(&raws);
00775 xsh_free_frameset(&calib);
00776
00777 cleanup:
00778 xsh_free_frameset(&raws);
00779 xsh_free_frameset(&calib);
00780 xsh_instrument_free(&instrument);
00781
00782 return cpl_error_get_code();
00783
00784 }
00785
00786
00787
00795
00796 static int
00797 xsh_cfg_recover_last_step(cpl_parameterlist* parameters,
00798 cpl_frameset* frameset,
00799 xsh_instrument* instrument,
00800 cpl_frameset* raws,
00801 cpl_frameset* calib)
00802 {
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812 cpl_frameset* on = NULL;
00813 cpl_frameset* off = NULL;
00814 cpl_frameset* on_off = NULL;
00815
00816
00817 cpl_frame* master_bias = NULL;
00818 cpl_frame* master_dark = NULL;
00819 cpl_frame* bpmap = NULL;
00820 cpl_frame* predict_rmbias = NULL;
00821 cpl_frame* predict_rmdark = NULL;
00822
00823 cpl_frame* wave_list=NULL ;
00824 cpl_frame* xsh_config_first_anneal=NULL ;
00825 cpl_frame* xsh_config_input=NULL ;
00826 cpl_frame* xsh_config_last_anneal=NULL ;
00827 cpl_frame * order_tab_centr = NULL ;
00828 cpl_frame * model_xy_gue=NULL;
00829 cpl_frame * measure_xy_pos_frm=NULL;
00830 cpl_frame * model_THE1_frm=NULL;
00831 cpl_frame * model_THE9_frm=NULL;
00832
00833 int anneal_niter=10000;
00834 cpl_parameter* p=NULL;
00835 const char* method=NULL;
00836 int prescan=false;
00837 int debug_level=0;
00838 cpl_frame* fmtchk = NULL;
00839 const char* name=NULL;
00840 const char* tag=NULL;
00841
00842
00843 const char* filename=NULL;
00844 cpl_propertylist* plist=NULL ;
00845 cpl_table* tab_xsh_config_first_anneal=NULL;
00846 cpl_table* tab_xsh_config_input=NULL;
00847 cpl_frame* first_anneal_frm=NULL;
00848 int found_temp=true;
00849 int first_anneal=true;
00850
00851 int pre_overscan_corr=0;
00852
00853
00854 int nrows;
00855
00856 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.cor_prescan"));
00857 check(prescan = cpl_parameter_get_bool(p));
00858
00859 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.anneal_niter"));
00860 check(anneal_niter = cpl_parameter_get_int(p));
00861
00862 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.method"));
00863 check(method = cpl_parameter_get_string(p));
00864
00865 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.first-anneal"));
00866 check(first_anneal = cpl_parameter_get_bool(p));
00867
00868 check(debug_level=xsh_parameters_debug_level_get("xsh_cfg_recover",parameters));
00869
00870
00871
00872
00873
00874
00875
00876 check( xsh_instrument_set_mode( instrument, XSH_MODE_SLIT));
00877
00878
00879 if(first_anneal) {
00880 check(first_anneal_frm=xsh_find_frame_with_tag(frameset,
00881 XSH_MOD_CFG_FAN,
00882 instrument));
00883 check(xsh_config_first_anneal=cpl_frame_duplicate(first_anneal_frm));
00884 } else {
00885 check(first_anneal_frm=xsh_find_frame_with_tag(frameset,
00886 XSH_MOD_CFG,
00887 instrument));
00888 check(xsh_config_first_anneal=cpl_frame_duplicate(first_anneal_frm));
00889 check(name=cpl_frame_get_filename(xsh_config_first_anneal));
00890 check(tab_xsh_config_first_anneal=cpl_table_load(name,1,0));
00891 check(plist=cpl_propertylist_load(name,0));
00892 check(tag=xsh_get_tag_from_arm(XSH_MOD_CFG_FAN,instrument));
00893 check(filename=cpl_sprintf("%s%s",tag,".fits"));
00894 check(cpl_table_save(tab_xsh_config_first_anneal,plist, NULL,filename,
00895 CPL_IO_DEFAULT));
00896 check(cpl_frame_set_filename(xsh_config_first_anneal,filename));
00897 check(cpl_frame_set_tag(xsh_config_first_anneal,tag));
00898 check(cpl_frame_set_group(xsh_config_first_anneal,CPL_FRAME_GROUP_PRODUCT));
00899 check(cpl_frame_set_level(xsh_config_first_anneal,CPL_FRAME_LEVEL_FINAL));
00900
00901 }
00902
00903 check(wave_list = xsh_find_frame_with_tag( calib,XSH_ARC_LINE_LIST,
00904 instrument));
00905
00906
00907 check(bpmap = xsh_find_master_bpmap( calib));
00908
00909
00910 if(strcmp(method,"safefit")!=0) {
00911 if((order_tab_centr = xsh_find_frame_with_tag(calib,XSH_ORDER_TAB_CENTR,
00912 instrument)) == NULL ) {
00913 xsh_msg_warning("Frame %s not provided",
00914 xsh_get_tag_from_arm(XSH_ORDER_TAB_CENTR,instrument));
00915 cpl_error_set(cpl_func,CPL_ERROR_DATA_NOT_FOUND);
00916 order_tab_centr=NULL;
00917 xsh_error_reset();
00918 if ((strcmp(method,"pm") == 0) || (strcmp(method,"peaks") == 0) ) {
00919 xsh_msg_error("If method is 'pm' or 'peaks' you must provide");
00920 xsh_msg_error("A valid frame taged as %s ",
00921 xsh_get_tag_from_arm(XSH_ORDER_TAB_CENTR,instrument));
00922 goto cleanup;
00923 }
00924 }
00925 }
00926
00927 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00928
00929 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) == 1);
00930 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00931 instrument)) == NULL) {
00932 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00933 xsh_error_reset();
00934 }
00935 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00936 instrument)) == NULL) {
00937 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00938 xsh_error_reset();
00939 }
00940 }
00941
00942
00943
00944
00945
00946 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00947
00948 check(xsh_prepare( raws, bpmap, master_bias, XSH_FMTCHK, instrument,
00949 pre_overscan_corr));
00950 check(fmtchk = cpl_frameset_get_first(raws));
00951
00952 if(master_bias != NULL) {
00953 check(predict_rmbias = xsh_subtract_bias(fmtchk,master_bias,
00954 instrument,"FMTCHK_",0,0));
00955 } else {
00956 predict_rmbias = cpl_frame_duplicate(fmtchk);
00957 }
00958 if(master_dark != NULL) {
00959
00960 filename = xsh_stringcat_any( "FMTCHK_DARK_",
00961 xsh_instrument_arm_tostring( instrument ),
00962 ".fits", NULL ) ;
00963 check(predict_rmdark = xsh_subtract_dark(predict_rmbias, master_dark,
00964 filename, instrument));
00965 } else {
00966 predict_rmdark = cpl_frame_duplicate(predict_rmbias);
00967 }
00968 }
00969
00970 else{
00971 check( on = xsh_frameset_extract( raws,XSH_FMTCHK_NIR ) ) ;
00972 check( off = xsh_frameset_extract ( raws,XSH_FMTCHK_OFF ) ) ;
00973
00974 check(xsh_prepare(on,NULL, NULL, "ON", instrument,pre_overscan_corr));
00975
00976 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr));
00977
00978 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
00979 check(predict_rmdark = cpl_frame_duplicate(cpl_frameset_get_first(on_off)));
00980 }
00981
00982
00983
00984 check(model_xy_gue=xsh_cfg_recover_gen_xyg_frame(wave_list,
00985 xsh_config_first_anneal,
00986 instrument,prescan));
00987
00988
00989
00990 check(xsh_cfg_recover_guess_tab_corr_by_user(parameters,&model_xy_gue));
00991
00992
00993
00994
00995 check(xsh_cfg_recover_guess_tab_corr_by_ordpos(instrument,order_tab_centr,
00996 &model_xy_gue));
00997
00998
00999
01000
01001
01002
01003
01004
01005 if (!first_anneal) {
01006 if(found_temp) {
01007 xsh_msg("update the prism temperatures before line matching");
01008 check(xsh_model_temperature_update_frame(&xsh_config_first_anneal,predict_rmdark,
01009 instrument,&found_temp));
01010 }
01011 }
01012
01013 check(xsh_cfg_recover_measure_line_xy(predict_rmdark,
01014 instrument,
01015 order_tab_centr,
01016 parameters,
01017 method,
01018 xsh_config_first_anneal,
01019 &model_xy_gue,
01020 debug_level));
01021
01022 xsh_msg(" REGDEBUG A generate %s", cpl_frame_get_filename(model_xy_gue));
01023
01024
01025
01026 if((xsh_config_input = xsh_find_frame_with_tag(frameset,XSH_MOD_CFG,
01027 instrument)) == NULL) {
01028 xsh_msg_error("Frame %s not found",
01029 xsh_stringcat_any( XSH_MOD_CFG,
01030 xsh_instrument_arm_tostring(instrument),
01031 NULL ) ) ;
01032 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
01033 goto cleanup;
01034 }
01035
01036
01037
01038
01039
01040 if(found_temp) {
01041 xsh_msg("update the prism temperatures before annealing");
01042
01043
01044 check(xsh_model_temperature_update_frame(&xsh_config_input,predict_rmdark,
01045 instrument,&found_temp));
01046 }
01047
01048
01049
01050
01051 check(measure_xy_pos_frm=cpl_frame_duplicate(model_xy_gue));
01052
01053
01054 check(xsh_cfg_recover_extend_xy_pos_frm(&measure_xy_pos_frm,instrument));
01055
01056 check(name=cpl_frame_get_filename(measure_xy_pos_frm));
01057
01058 nrows=cpl_table_get_nrow(cpl_table_load(name,1,0));
01059
01060 if ((xsh_instrument_get_arm(instrument)==XSH_ARM_UVB && nrows<70) ||
01061 (xsh_instrument_get_arm(instrument) == XSH_ARM_VIS && nrows<120) ||
01062 (xsh_instrument_get_arm(instrument) == XSH_ARM_NIR && nrows<80)) {
01063 xsh_msg_error("Not enough matches (%" CPL_SIZE_FORMAT "). Try running with more iterations for first_anneal (higher niter)",cpl_table_get_nrow(cpl_table_load(name,1,0)));
01064 return -1;
01065 }
01066
01067
01068
01069 xsh_msg(" REGDEBUG B before annealing %s", cpl_frame_get_filename( measure_xy_pos_frm));
01070 check(xsh_config_last_anneal=xsh_model_pipe_anneal(xsh_config_input,
01071 measure_xy_pos_frm,
01072 anneal_niter,
01073 1.0,
01074 1,
01075 0));
01076
01077 check(xsh_cfg_recover_model_THE_create(xsh_config_last_anneal,
01078 instrument,
01079 wave_list,
01080 &model_THE1_frm,
01081 &model_THE9_frm));
01082
01083
01084
01085 check(xsh_add_product_table(xsh_config_last_anneal,frameset,parameters,
01086 RECIPE_ID,instrument,NULL));
01087 check(xsh_add_product_table(model_xy_gue,frameset,parameters, RECIPE_ID,
01088 instrument,NULL));
01089 check(xsh_add_product_table(model_THE1_frm,frameset,parameters, RECIPE_ID,
01090 instrument,NULL));
01091 check(xsh_add_product_table(model_THE9_frm,frameset,parameters, RECIPE_ID,
01092 instrument,NULL));
01093
01094 cleanup:
01095 xsh_free_table(&tab_xsh_config_first_anneal);
01096 xsh_free_table(&tab_xsh_config_input);
01097 xsh_end( RECIPE_ID, frameset, parameters );
01098
01099
01100 xsh_free_frame(&predict_rmbias );
01101 xsh_free_frame(&predict_rmdark );
01102
01103 xsh_free_frame(&model_xy_gue);
01104 xsh_free_frame(&measure_xy_pos_frm);
01105 xsh_free_frame(&xsh_config_last_anneal);
01106 xsh_free_frame(&model_THE1_frm);
01107 xsh_free_frame(&model_THE9_frm);
01108 xsh_free_frameset(&on);
01109 xsh_free_frameset(&off);
01110 xsh_free_frameset(&on_off);
01111
01112
01113
01114 if (cpl_error_get_code() != CPL_ERROR_NONE) {
01115 xsh_print_rec_status(0);
01116 return -1 ;
01117 } else {
01118 return 0 ;
01119 }
01120
01121 }
01122
01123
01138
01139
01140 static cpl_error_code
01141 xsh_cfg_recover_model_THE_create(cpl_frame* config_frame,
01142 xsh_instrument* instrument,
01143 cpl_frame* wave_list,
01144 cpl_frame** THE1_frm,
01145 cpl_frame** THE9_frm)
01146 {
01147 struct xs_3* p_xs_3_config=NULL;
01148 struct xs_3 xs_3_config;
01149 const char* line_list=NULL;
01150 const char* THE1_filename="model_THE1.fits";
01151 const char* THE9_filename="model_THE9.fits";
01152 const char* pro_catg=NULL;
01153
01154
01155 p_xs_3_config=&xs_3_config;
01156 if (xsh_model_config_load_best(config_frame, p_xs_3_config) !=
01157 CPL_ERROR_NONE) {
01158 xsh_msg_error("Cannot load %s as a config",
01159 cpl_frame_get_filename(config_frame)) ;
01160 return CPL_ERROR_DATA_NOT_FOUND ;
01161 }
01162
01163 check(line_list=cpl_frame_get_filename(wave_list));
01164 check(*THE1_frm=xsh_model_THE_create(p_xs_3_config,instrument,line_list,
01165 1,-1,THE1_filename));
01166 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_THEO_TAB_SING,instrument));
01167 check(xsh_frame_config(THE1_filename,pro_catg,CPL_FRAME_TYPE_TABLE,
01168 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
01169 THE1_frm));
01170
01171 check(*THE9_frm=xsh_model_THE_create(p_xs_3_config,instrument,line_list,
01172 9,-1,THE9_filename));
01173
01174 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_THEO_TAB_MULT,instrument));
01175 check(xsh_frame_config(THE9_filename,pro_catg,CPL_FRAME_TYPE_TABLE,
01176 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
01177 THE9_frm));
01178
01179
01180 cleanup:
01181 if(cpl_error_get_code() == CPL_ERROR_NONE) {
01182 return cpl_error_get_code();
01183 } else {
01184 xsh_print_rec_status(0);
01185 return cpl_error_get_code();
01186 }
01187 }
01188
01189
01199
01200
01201 static cpl_error_code
01202 xsh_cfg_recover_guess_tab_corr_by_user(cpl_parameterlist* parameters,
01203 cpl_frame** model_xy_gue)
01204 {
01205 const char* name=NULL;
01206 cpl_table* tab=NULL;
01207 double offx=0;
01208 double offy=0;
01209 cpl_parameter* p=NULL;
01210
01211 check(name=cpl_frame_get_filename(*model_xy_gue));
01212 check(tab=cpl_table_load(name,1,0));
01213
01214 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offx"));
01215 check(offx = cpl_parameter_get_double(p));
01216
01217 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offy"));
01218 check(offy = cpl_parameter_get_double(p));
01219
01220 check(cpl_table_add_scalar(tab,"XG",offx));
01221 check(cpl_table_add_scalar(tab,"YG",offy));
01222
01223 check(cpl_table_save(tab, NULL, NULL,name, CPL_IO_DEFAULT));
01224
01225 cleanup:
01226 xsh_free_table(&tab);
01227
01228
01229 return cpl_error_get_code();
01230 }
01231
01232
01233
01240
01241
01242 static cpl_error_code
01243 xsh_cfg_recover_extend_xy_pos_frm(cpl_frame** frm,xsh_instrument* inst)
01244 {
01245 const char * name=NULL;
01246 cpl_table* tbl=NULL;
01247 int nrows=0;
01248 cpl_propertylist* header = NULL;
01249 char name_o[80];
01250 const char* tag=XSH_GET_TAG_FROM_ARM(XSH_MEASURE_LINE_POS_XY,inst);
01251
01252 sprintf(name_o,"%s%s",tag,".fits");
01253
01254 check(name=cpl_frame_get_filename(*frm));
01255
01256
01257
01258 check(tbl=cpl_table_load(name,1,0));
01259 check ( header = cpl_propertylist_load( name, 0));
01260
01261 check(nrows=cpl_table_get_nrow(tbl));
01262 if(nrows==0) {
01263 xsh_msg_error("Table %s has 0 rows. Something wrong! Exit",name);
01264 goto cleanup;
01265 }
01266
01267
01268 check(cpl_table_name_column(tbl,"WAVELENGTH",
01269 XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH));
01270 check(cpl_table_name_column(tbl,"ABS_ORD",
01271 XSH_RESID_TAB_TABLE_COLNAME_ORDER));
01272 check(cpl_table_new_column(tbl,XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
01273 CPL_TYPE_DOUBLE));
01274 check(cpl_table_fill_column_window(tbl,
01275 XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
01276 0,nrows,0.));
01277 cpl_table_new_column(tbl,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,CPL_TYPE_INT);
01278 cpl_table_fill_column_window(tbl,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,0,
01279 nrows,4);
01280
01281
01282 check(cpl_table_duplicate_column(tbl,
01283 XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,
01284 tbl,"XC"));
01285 check( cpl_table_duplicate_column(tbl,
01286 XSH_RESID_TAB_TABLE_COLNAME_YTHPRE,
01287 tbl, "YC"));
01288
01289
01290 check(cpl_table_duplicate_column(tbl,
01291 XSH_RESID_TAB_TABLE_COLNAME_XTHCOR,
01292 tbl,"XC"));
01293 check( cpl_table_duplicate_column(tbl,
01294 XSH_RESID_TAB_TABLE_COLNAME_YTHCOR,
01295 tbl, "YC"));
01296
01297
01298 check(cpl_table_duplicate_column(tbl,
01299 XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,
01300 tbl,"XC"));
01301 check( cpl_table_duplicate_column(tbl,
01302 XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,
01303 tbl, "YC"));
01304
01305 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
01306 CPL_TYPE_DOUBLE);
01307 xsh_msg("create column %s", XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS);
01308
01309 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
01310 0, nrows, 0.);
01311 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
01312 CPL_TYPE_DOUBLE);
01313 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
01314 0, nrows, 0.);
01315 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,
01316 CPL_TYPE_DOUBLE);
01317 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,
01318 0, nrows, 0.);
01319 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL,
01320 CPL_TYPE_DOUBLE);
01321 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL,
01322 0, nrows, 0.);
01323
01324 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
01325 CPL_TYPE_DOUBLE);
01326 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
01327 0, nrows, 0.);
01328 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
01329 CPL_TYPE_DOUBLE);
01330 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
01331 0, nrows, 0.);
01332
01333 check( xsh_pfits_set_wavesoltype( header, XSH_WAVESOLTYPE_MODEL));
01334
01335 cpl_table_erase_invalid_rows(tbl);
01336 check( xsh_pfits_set_pcatg(header,tag));
01337 check(cpl_table_save(tbl, header, NULL,name_o, CPL_IO_DEFAULT));
01338
01339 cpl_frame_set_filename(*frm,name_o);
01340 cpl_frame_set_tag(*frm,tag);
01341
01342 cleanup:
01343 xsh_free_table(&tbl);
01344 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01345 xsh_print_rec_status(0);
01346 }
01347 return cpl_error_get_code() ;
01348 }
01349
01362
01363
01364 static cpl_error_code
01365 xsh_cfg_recover_measure_line_xy_fit(cpl_frame* raw_frm,
01366 cpl_parameterlist* parameters,
01367 cpl_frame** guess,
01368 int debug_level)
01369 {
01370
01371 int box_sy=0;
01372 cpl_parameter* p=NULL;
01373 int ngue=0;
01374 int i=0;
01375
01376 double* pxg=NULL;
01377 double* pyg=NULL;
01378 double* pxf=NULL;
01379 double* pyf=NULL;
01380
01381 int* ps=NULL;
01382 cpl_image* ima=NULL;
01383 const char* name=NULL;
01384
01385 double norm=0;
01386 double xcen=0;
01387 double ycen=0;
01388 double sig_x=0;
01389 double sig_y=0;
01390 double fwhm_x=0;
01391 double fwhm_y=0;
01392 const char* name_g="tab_xy_corr_gauss.fits";
01393 cpl_table* tab=NULL;
01394 cpl_table* tmp=NULL;
01395 int nfit=0;
01396
01397
01398
01399
01400
01401 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.gfit_box_sy"));
01402 check(box_sy = cpl_parameter_get_int(p));
01403
01404 check(name=cpl_frame_get_filename(raw_frm));
01405 check(ima=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
01406
01407 check(name=cpl_frame_get_filename(*guess));
01408 check(tab=cpl_table_load(name,1,0));
01409
01410 check(ngue=cpl_table_get_nrow(tab));
01411 check(cpl_table_new_column(tab,"SELECT",CPL_TYPE_INT));
01412 check(cpl_table_new_column(tab,"XF",CPL_TYPE_DOUBLE));
01413 check(cpl_table_new_column(tab,"YF",CPL_TYPE_DOUBLE));
01414
01415 check(cpl_table_fill_column_window(tab,"SELECT",0,ngue,-1));
01416 check(cpl_table_fill_column_window(tab,"XF",0,ngue,0));
01417 check(cpl_table_fill_column_window(tab,"YF",0,ngue,0));
01418
01419 check(pxg=cpl_table_get_data_double(tab,"XC"));
01420 check(pyg=cpl_table_get_data_double(tab,"YC"));
01421 check(pxf=cpl_table_get_data_double(tab,"XF"));
01422 check(pyf=cpl_table_get_data_double(tab,"YF"));
01423 check(ps=cpl_table_get_data_int(tab,"SELECT"));
01424
01425 xsh_msg("ngue=%d",ngue);
01426 for(i=0;i<ngue;i++) {
01427
01428
01429 if(cpl_image_fit_gaussian(ima,(int)pxg[i],(int)pyg[i],box_sy,
01430 &norm,
01431 &xcen,&ycen,
01432 &sig_x,&sig_y,
01433 &fwhm_x,&fwhm_y) == CPL_ERROR_NONE) {
01434 pxf[i]=xcen;
01435 pyf[i]=ycen;
01436 ps[i]=1;
01437
01438 } else {
01439
01440 cpl_error_reset();
01441 }
01442 }
01443 check(nfit=cpl_table_and_selected_int(tab,"SELECT",CPL_GREATER_THAN,-1));
01444
01445 xsh_msg("nfit=%d",nfit);
01446 check(tmp=cpl_table_extract_selected(tab));
01447
01448 check(cpl_table_erase_column(tmp,"XC"));
01449 check(cpl_table_erase_column(tmp,"YC"));
01450
01451 check(cpl_table_duplicate_column(tmp,"XC",tmp,"XF"));
01452 check(cpl_table_duplicate_column(tmp,"YC",tmp,"YF"));
01453
01454 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
01455 check(cpl_table_save(tmp, NULL, NULL, name_g, CPL_IO_DEFAULT));
01456 check(cpl_table_save(tmp, NULL, NULL, name, CPL_IO_DEFAULT));
01457 }
01458
01459 cleanup:
01460 xsh_free_table(&tab);
01461 xsh_free_table(&tmp);
01462 xsh_free_image(&ima);
01463
01464 if(nfit ==0 ) {
01465 xsh_msg_error("nfit = 0, something wrong in %s",cpl_func);
01466 return CPL_ERROR_UNSPECIFIED;
01467 } else {
01468 if( cpl_error_get_code() != CPL_ERROR_NONE) {
01469 xsh_print_rec_status(0);
01470 }
01471 return cpl_error_get_code() ;
01472 }
01473 }
01474
01486
01487
01488
01489 static cpl_error_code
01490 xsh_cfg_recover_measure_line_xy(cpl_frame* raw_frm,
01491 xsh_instrument* instr,
01492 cpl_frame* order_tab_centr,
01493 cpl_parameterlist* parameters,
01494 const char* method,
01495 cpl_frame* model_config,
01496 cpl_frame** guess,
01497 int debug_level)
01498 {
01499 cpl_image* ima=NULL;
01500 cpl_table* tab_xy_trace=NULL;
01501 cpl_table* tab_xy_guess=NULL;
01502 cpl_table* tab_xy_peaks=NULL;
01503 cpl_table* tab_xy_peaks_sel=NULL;
01504 cpl_table* tab_tmp=NULL;
01505 cpl_table* tab_tmp1=NULL;
01506 cpl_table* tab_tmp2=NULL;
01507 cpl_table* tab_xy_corr=NULL;
01508 cpl_image* ima_ext=NULL;
01509 cpl_image* ima_raw=NULL;
01510 double fct=2.;
01511 int slit=10;
01512 cpl_parameter* p=NULL;
01513 const char* name=NULL;
01514 const char* name_g=NULL;
01515 const char* name_t="tab_xy_peaks.fits";
01516 int thresh_x=0;
01517 int thresh_y=0;
01518
01519 cpl_matrix* mat_gue=NULL;
01520 cpl_matrix* mat_msr=NULL;
01521 double thresh_min=25;
01522 int use_data=0;
01523 int use_pattern=0;
01524 int ord_min=0;
01525 int ngue=0;
01526 int npck=0;
01527
01528 double* pxg=NULL;
01529 double* pyg=NULL;
01530 double yg_temp[20000];
01531 double xg_temp[20000];
01532 double* wave=NULL;
01533 int* ordg=NULL;
01534 int* psl=NULL;
01535
01536
01537 double cent, cent2, flux, cent2_cor, flux_cor, ex_cor;
01538 double prec, precfac,precmin;
01539 int lmax, rmax;
01540 int starty;
01541 int endy;
01542 int ii, jj, ll, min_iso, ord;
01543 int cnt=0;
01544
01545 int wmin;
01546 int morder_min;
01547 int w;
01548 int lines_dev;
01549
01550 cpl_vector* spectrum=NULL;
01551 cpl_vector* filtered=NULL;
01552 cpl_vector* spec_clean=NULL;
01553 cpl_vector* bright_lines=NULL ;
01554 int nbright_lines=0 ;
01555
01556 int kk=0;
01557 double* pxc=NULL;
01558 double* pyc=NULL;
01559 double* pwl=NULL;
01560 int nrow=0;
01561 int nfit=0;
01562 cpl_vector** trace=NULL;
01563 cpl_vector** extracted=NULL;
01564 int MAX_NO_ORD=16;
01565 struct xs_3 xs_3_config;
01566 struct xs_3* p_xs_3=NULL;
01567 int h=4;
01568 cpl_table* spec_form_tab=NULL;
01569 int starti=0;
01570 int absord;
01571 double tot=0.0;
01572 double total=0.0;
01573 double dx;
01574 double* detec=NULL;
01575 int size_x=0;
01576 int size_y=0;
01577 int size_y_use=0;
01578 int status=0;
01579 double x=0,y=0, offx=0, offy=0;
01580 cpl_frame* spec_form_frame=NULL ;
01581 int pix=0;
01582 int nb_match=0;
01583 double wave_use=0, zero_off=0,match_lim=0,startlam=0,endlam=0,order_median=0;
01584 p_xs_3=&xs_3_config;
01585
01586
01587 check(xsh_model_config_load_best(model_config, p_xs_3));
01588 xsh_msg( "Calling spectral format create" ) ;
01589
01590
01591 check(spec_form_frame=xsh_model_spectralformat_create(p_xs_3,"not_spec_form.fits"));
01592 check(cpl_frame_set_tag(spec_form_frame, xsh_get_tag_from_arm(XSH_ORDER_TAB_CENTR
01593 ,instr)));
01594
01595 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.method"));
01596 check(method = cpl_parameter_get_string(p));
01597
01598 if (strcmp(method,"gfit")==0) {
01599 check(xsh_cfg_recover_measure_line_xy_fit(raw_frm,parameters,guess,debug_level));
01600
01601 } else if (strcmp(method,"pbrfit")==0 || strcmp(method,"safefit")==0) {
01602
01603 if (strcmp(method,"safefit") == 0) {
01604
01605
01606 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offx"));
01607 check(offx = cpl_parameter_get_double(p));
01608 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offy"));
01609 check(offy = cpl_parameter_get_double(p));
01610
01611
01612
01613 trace=xsh_model_locus(p_xs_3,instr,0.0);
01614
01615
01616 spec_form_tab=cpl_table_load(cpl_frame_get_filename(spec_form_frame),1,0);
01617
01618
01619 check(name=cpl_frame_get_filename(raw_frm));
01620 check(ima_raw=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
01621 check(size_y=cpl_image_get_size_y(ima_raw));
01622 check(size_x=cpl_image_get_size_x(ima_raw));
01623 xsh_msg("size_x=%d size_y=%d",size_x,size_y);
01624 check(detec=cpl_image_get_data_double(ima_raw));
01625
01626
01627 check(extracted=cpl_malloc(MAX_NO_ORD*sizeof(cpl_vector *)));
01628 for (ii=0; ii<MAX_NO_ORD; ii++) {
01629 extracted[ii] = cpl_vector_new(size_y) ;
01630 cpl_vector_fill(extracted[ii], 0.0) ;
01631 }
01632
01633
01634
01635 starti=0;
01636 while (cpl_table_get_int(spec_form_tab,"ORDER",starti,&status)<p_xs_3->morder_min) {
01637 starti++;
01638 }
01639 ii=starti;
01640
01641 for (ii=starti; ii<=starti+p_xs_3->morder_max-p_xs_3->morder_min;ii++) {
01642 absord=cpl_table_get_int(spec_form_tab,"ORDER",ii,&status);
01643 if (p_xs_3->arm!=2) {
01644 starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ii,&status));
01645 endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ii,&status));
01646 }
01647 else {
01648 starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ii,&status));
01649 endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ii,&status));
01650 }
01651
01652
01653
01654
01655
01656
01657
01658 if (offy<0.0) {
01659 starty-=(int)(offy);
01660 size_y_use=size_y;
01661 }
01662 else {
01663 endy-=(int)(offy);
01664 size_y_use=size_y-offy;
01665 }
01666 for (jj=starty;((jj<endy) && (jj<size_y_use));jj++) {
01667 tot=0.0;
01668
01669 dx=cpl_vector_get(trace[absord-p_xs_3->morder_min],jj+offy)-offx;
01670 for (kk=-h; kk<=h; kk++) {
01671 if (p_xs_3->arm==2) {
01672 pix=size_x*jj+((int)(dx+0.5)+kk);
01673 } else {
01674 pix=size_x*jj+(int)(dx+0.5)+kk;
01675 }
01676 if(pix<size_x*size_y) {
01677 tot+=detec[pix];
01678 }
01679 }
01680 check(cpl_vector_set(extracted[absord-p_xs_3->morder_min],jj,tot));
01681 }
01682 }
01683 }
01684
01685 else {
01686
01687 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.slit"));
01688 check(slit = cpl_parameter_get_int(p));
01689
01690 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_factor"));
01691 check(fct = cpl_parameter_get_double(p));
01692
01693 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_x"));
01694 check(thresh_x = cpl_parameter_get_int(p));
01695
01696 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_y"));
01697 check(thresh_y = cpl_parameter_get_int(p));
01698
01699 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.ima_thresh_min"));
01700 check(thresh_min = cpl_parameter_get_double(p));
01701
01702
01703
01704
01705 cknull(ima_ext=xsh_cfg_recover_linear_ext(raw_frm,order_tab_centr,instr,
01706 slit,thresh_min));
01707
01708 }
01709
01710
01711 check(name_g=cpl_frame_get_filename(*guess));
01712 check(tab_xy_guess=cpl_table_load(name_g,1,0));
01713 xsh_msg("guess tablename=%s",name_g);
01714
01715 morder_min=p_xs_3->morder_min;
01716 nrow=cpl_table_get_nrow(tab_xy_guess);
01717
01718 cpl_table_new_column(tab_xy_guess,"XF",CPL_TYPE_DOUBLE);
01719 cpl_table_new_column(tab_xy_guess,"YF",CPL_TYPE_DOUBLE);
01720 cpl_table_new_column(tab_xy_guess,"SELECT",CPL_TYPE_INT);
01721 cpl_table_fill_column_window(tab_xy_guess,"XF",0,nrow,-1);
01722 cpl_table_fill_column_window(tab_xy_guess,"YF",0,nrow,-1);
01723 cpl_table_fill_column_window(tab_xy_guess,"SELECT",0,nrow,-1);
01724
01725 pxc=cpl_table_get_data_double(tab_xy_guess,"XF");
01726 pyc=cpl_table_get_data_double(tab_xy_guess,"YF");
01727 pwl=cpl_table_get_data_double(tab_xy_guess,"WAVELENGTH");
01728
01729
01730
01731 for (kk=0;kk<nrow;kk++) {
01732 yg_temp[kk]=-1.0;
01733 }
01734 for (kk=0;kk<nrow;kk++) {
01735 xg_temp[kk]=-1.0;
01736 }
01737
01738 kk=0;
01739
01740 for(ord=0;ord<p_xs_3->morder_max-morder_min+1;ord++) {
01741 check(absord=cpl_table_get_int(spec_form_tab,"ORDER",ord+starti,&status));
01742 if (p_xs_3->arm!=2) {
01743 check(starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ord+starti,&status)));
01744 check(endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ord+starti,&status)));
01745 }
01746 else {
01747 check(starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ord+starti,&status)));
01748 check(endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ord+starti,&status)));
01749 }
01750 check(startlam=cpl_table_get_float(spec_form_tab,"LFSR",ord+starti,&status));
01751 check(endlam=cpl_table_get_float(spec_form_tab,"UFSR",ord+starti,&status));
01752
01753 if (strcmp(method,"safefit") == 0) {
01754 spectrum=extracted[ord];
01755
01756
01757 if ((filtered=cpl_vector_filter_median_create(spectrum, 200))==NULL){
01758 cpl_msg_error(__func__, "Cannot filter the spectrum") ;
01759 return CPL_ERROR_ILLEGAL_INPUT ;
01760 }
01761 spec_clean = cpl_vector_duplicate(spectrum);
01762 cpl_vector_subtract(spec_clean, filtered) ;
01763 cpl_vector_delete(filtered) ;
01764
01765 order_median=cpl_vector_get_median_const(spec_clean) ;
01766 }
01767 else {
01768
01769
01770 check(spectrum=cpl_vector_new_from_image_row(ima_ext,ord+1));
01771 spec_clean = cpl_vector_duplicate(spectrum);
01772 }
01773 check(bright_lines=xsh_model_refining_detect(spec_clean,3,5.0,0));
01774
01775
01776 check(pxg=cpl_table_get_data_double(tab_xy_guess,"XC"));
01777 check(pyg=cpl_table_get_data_double(tab_xy_guess,"YC"));
01778 check(wave=cpl_table_get_data_double(tab_xy_guess,"WAVELENGTH"));
01779 check(ordg=cpl_table_get_data_int(tab_xy_guess,"ABS_ORD"));
01780 check(psl=cpl_table_get_data_int(tab_xy_guess,"SELECT"));
01781
01782
01783
01784
01785
01786
01787
01788
01789 if(bright_lines != NULL) {
01790 check(nbright_lines=cpl_vector_get_size(bright_lines));
01791 for (ii=0; ii<nbright_lines;ii++) {
01792 cent=cpl_vector_get(bright_lines,ii)+0.5-1.0;
01793
01794
01795
01796
01797
01798
01799 lines_dev=0;
01800
01801 if (lines_dev==1) {
01802 precmin=0.25;
01803 precfac=25.0;
01804 wmin=3;
01805 min_iso=5;
01806 zero_off=-0.5;
01807 match_lim=0.2;
01808 }
01809 else if (lines_dev==2) {
01810 precmin=0.5;
01811 precfac=25.0;
01812 wmin=3;
01813 min_iso=5;
01814 zero_off=-0.5;
01815 match_lim=1.0;
01816 }
01817 else if (lines_dev==3) {
01818 precmin=0.25;
01819 precfac=25.0;
01820 wmin=3;
01821 min_iso=5;
01822 zero_off=-0.5;
01823 match_lim=0.25;
01824 }
01825 else if (lines_dev==4) {
01826 precmin=0.5;
01827 precfac=25.0;
01828 wmin=3;
01829 min_iso=5;
01830 zero_off=-0.5;
01831 match_lim=1.0;
01832 }
01833 else if (lines_dev==5) {
01834 precmin=0.4;
01835 precfac=25.0;
01836 wmin=3;
01837 min_iso=5;
01838 zero_off=-0.5;
01839 match_lim=0.5;
01840 }
01841 else if (lines_dev==6) {
01842 precmin=0.5;
01843 precfac=15.0;
01844 wmin=3;
01845 min_iso=80;
01846 zero_off=-0.5;
01847 match_lim=1.0;
01848 }
01849 else {
01850 precmin=0.5;
01851 precfac=20.0;
01852 wmin=4;
01853 min_iso=4;
01854 match_lim=2.5;
01855 zero_off=0.0;
01856 }
01857 rmax=0;
01858 w=wmin;
01859 while (w<200) {
01860
01861 prec=(float)(w)/precfac;
01862 if (prec<precmin) {
01863 prec=precmin;
01864 }
01865 flux=0.0;
01866 cent2=0.0;
01867 flux_cor=0.0;
01868 cent2_cor=0.0;
01869 for (jj=(int)(cent)-wmin;jj<=(int)(cent)+w;jj++) {
01870
01871 if (jj>0 && jj<endy) {
01872 ex_cor=cpl_vector_get(spec_clean,jj);
01873 if (ex_cor>0.0) {
01874 flux+=ex_cor;
01875 cent2+=ex_cor*((float)(jj)+0.5-cent);
01876 }
01877
01878 }
01879 }
01880 cent2/=flux;
01881
01882 cent2+=cent;
01883 if (fabs(cent2-cent)<prec) {
01884 rmax=w;
01885
01886 }
01887 else {
01888 w=1000;
01889 }
01890 w+=1;
01891 }
01892 lmax=0;
01893 w=wmin;
01894 while (w<200) {
01895
01896 prec=(float)(w)/precfac;
01897 if (prec<precmin) {
01898 prec=precmin;
01899 }
01900 flux=0.0;
01901 cent2=0.0;
01902 flux_cor=0.0;
01903 cent2_cor=0.0;
01904 for (jj=(int)(cent)-w;jj<=(int)(cent)+wmin;jj++) {
01905 if (jj>0 && jj<endy) {
01906 ex_cor=cpl_vector_get(spec_clean,jj);
01907 if (ex_cor>0.0) {
01908 flux+=ex_cor;
01909 cent2+=ex_cor*((float)(jj)+0.5-cent);
01910 }
01911 }
01912 }
01913 cent2/=flux;
01914
01915 cent2+=cent;
01916 if (fabs(cent2-cent)<prec) {
01917 lmax=w;
01918
01919 }
01920 else {
01921 w=1000;
01922 }
01923 w+=1;
01924 }
01925
01926
01927 if (lmax>=min_iso && rmax>=min_iso) {
01928
01929
01930 if (strcmp(method,"safefit") == 0) {
01931 nb_match=0;
01932 for (jj=0; jj< cpl_table_get_nrow(tab_xy_guess);jj++) {
01933 if (ord==ordg[jj]-morder_min) {
01934
01935 check(xsh_model_get_xy(p_xs_3,instr,wave[jj],absord,0.0,&x,&y));
01936 x-=offx;
01937 y-=offy;
01938 if (lines_dev>0 && fabs(y-cent+zero_off)<match_lim) {
01939 nb_match++;
01940
01941 wave_use=wave[jj];
01942 }
01943 if (abs(y-cent)<(float)(min_iso)/2.0) {
01944
01945
01946 yg_temp[jj]=cent;
01947 psl[jj]=1;
01948 total=0.0;
01949 cent2=0.0;
01950
01951
01952 for (ll=-h;ll<=h;ll++) {
01953 tot=0.0;
01954 for (kk=-1;kk<=1;kk++) {
01955
01956
01957
01958
01959 pix=size_x*((int)(cent+0.5)+kk)+(int)(x+0.5)+ll;
01960 tot+=detec[pix];
01961 pix=size_x*((int)(cent+0.5)+kk)+(int)(x+0.5)+ll+15;
01962 tot-=detec[pix];
01963
01964 }
01965 total+=tot;
01966 cent2+=tot*((float)(ll)+0.5);
01967 }
01968 if (fabs(cent2/total)<3.0) {
01969 cent2=x+(cent2/total);
01970 }
01971 else {
01972 cent2=x;
01973 }
01974
01975
01976 xg_temp[jj]=cent2;
01977 }
01978 }
01979 }
01980 if (nb_match==1 && lines_dev>0) {
01981 if (wave_use>startlam && wave_use<endlam) {
01982 cnt++;
01983
01984 printf("%lf 100.0 %d tot%d\n",wave_use, min_iso, cnt);
01985 }
01986 }
01987 }
01988 else{
01989 for (jj=0; jj< cpl_table_get_nrow(tab_xy_guess);jj++) {
01990 if (ord==ordg[jj]-morder_min) {
01991
01992 if (abs(pyg[jj]-cent)<(float)(min_iso/2)) {
01993
01994
01995
01996 yg_temp[jj]=cent;
01997 psl[jj]=1;
01998 }
01999 }
02000 }
02001 }
02002 }
02003 }
02004 }
02005 else {
02006 xsh_msg_warning("bright_lines is NULL, order %d\n",ord);
02007 }
02008 }
02009
02010 for (jj=0; jj< cpl_table_get_nrow(tab_xy_guess);jj++) {
02011 if (yg_temp[jj]>0.0) {
02012 pyg[jj]=yg_temp[jj];
02013 pxg[jj]=xg_temp[jj];
02014 }
02015 pyc[jj]=pyg[jj];
02016 pxc[jj]=pxg[jj];
02017 }
02018
02019
02020 check(nfit=cpl_table_and_selected_int(tab_xy_guess,"SELECT",CPL_GREATER_THAN,-1));
02021 check(tab_tmp=cpl_table_extract_selected(tab_xy_guess));
02022 xsh_free_table(&tab_xy_guess);
02023 check(cpl_table_erase_column(tab_tmp,"XC"));
02024 check(cpl_table_erase_column(tab_tmp,"YC"));
02025
02026 check(cpl_table_duplicate_column(tab_tmp,"XC",tab_tmp,"XF"));
02027 check(cpl_table_duplicate_column(tab_tmp,"YC",tab_tmp,"YF"));
02028
02029 check(cpl_table_save(tab_tmp,NULL,NULL,name_g,CPL_IO_DEFAULT));
02030 xsh_free_table(&tab_tmp);
02031
02032 } else {
02033
02034
02035 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.slit"));
02036 check(slit = cpl_parameter_get_int(p));
02037
02038 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_factor"));
02039 check(fct = cpl_parameter_get_double(p));
02040
02041 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_x"));
02042 check(thresh_x = cpl_parameter_get_int(p));
02043
02044 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_y"));
02045 check(thresh_y = cpl_parameter_get_int(p));
02046
02047 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.ima_thresh_min"));
02048 check(thresh_min = cpl_parameter_get_double(p));
02049
02050
02051
02052
02053 cknull(ima_ext=xsh_cfg_recover_linear_ext(raw_frm,order_tab_centr,instr,
02054 slit,thresh_min));
02055
02056
02057
02058
02059 cknull(tab_xy_peaks=xsh_cfg_recover_measure_tab_xy_peaks(ima_ext,parameters));
02060 check(cpl_table_duplicate_column(tab_xy_peaks,"ABS_ORD",
02061 tab_xy_peaks,"REL_ORD"));
02062
02063
02064
02065 check(name_g=cpl_frame_get_filename(*guess));
02066 check(tab_xy_guess=cpl_table_load(name_g,1,0));
02067 xsh_msg("guess table name=%s",name_g);
02068
02069 check(name=cpl_frame_get_filename(order_tab_centr));
02070 check(tab_xy_trace=cpl_table_load(name,1,0));
02071 xsh_msg("Trace table name=%s",name);
02072
02073 check(ord_min=cpl_table_get_column_min(tab_xy_trace,"ABSORDER"));
02074 check(cpl_table_add_scalar(tab_xy_peaks,"ABS_ORD",ord_min));
02075
02076 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02077 check(cpl_table_save(tab_xy_peaks,NULL,NULL,name_t,CPL_IO_DEFAULT));
02078 }
02079
02080 check(xsh_cfg_recover_add_peaks_xpos(order_tab_centr,instr,&tab_xy_peaks));
02081
02082 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02083 check(cpl_table_save(tab_xy_peaks,NULL,NULL,name_t,CPL_IO_DEFAULT));
02084 }
02085
02086 if(fct>0) {
02087
02088 xsh_msg("Peaks selection fct=%g",fct);
02089 cknull(tab_xy_peaks_sel=xsh_cfg_recover_select_peaks(tab_xy_guess,
02090 tab_xy_peaks,fct));
02091 } else {
02092 xsh_msg("No peaks selection");
02093 tab_xy_peaks_sel=cpl_table_duplicate(tab_xy_peaks);
02094 }
02095
02096 if(debug_level> XSH_DEBUG_LEVEL_NONE) {
02097 check(cpl_table_save(tab_xy_peaks_sel, NULL, NULL,
02098 "tab_xy_peaks_sel.fits",
02099 CPL_IO_DEFAULT));
02100 }
02101
02102 if ((strcmp(method,"pm") == 0) ||
02103 (strcmp(method,"manual") ==0)) {
02104 check(xsh_cfg_recover_prepare_pm_set(tab_xy_guess,tab_xy_peaks_sel,
02105 parameters,
02106 &mat_gue,&mat_msr,&use_pattern,
02107 &use_data));
02108
02109
02110 check(xsh_cfg_recover_pattern_match(parameters,mat_gue,mat_msr,
02111 use_pattern,use_data,debug_level));
02112
02113
02114 } else {
02115
02116
02117 xsh_msg("Remove blended lines");
02118
02119
02120
02121
02122 ngue=cpl_table_get_nrow(tab_xy_guess);
02123 npck=cpl_table_get_nrow(tab_xy_peaks_sel);
02124 if(ngue<=npck) {
02125 cknull(tab_xy_corr=xsh_cfg_recover_remove_blends(tab_xy_guess,
02126 tab_xy_peaks_sel,
02127 thresh_x,
02128 thresh_y));
02129
02130 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02131 check(cpl_table_save(tab_xy_corr, NULL, NULL, "tab_xy_corr.fits",
02132 CPL_IO_DEFAULT));
02133
02134 }
02135 } else {
02136 xsh_msg_error("Number og guess lines more than found peaks!");
02137 xsh_msg_error("Either increase factor ");
02138 xsh_msg_error("or choose a reference line table with less entries");
02139 return CPL_ERROR_ILLEGAL_INPUT ;
02140 }
02141 }
02142 }
02143
02144
02145 cleanup:
02146 if(trace!=NULL) {
02147 for (ii=0 ; ii<MAX_NO_ORD; ii++) {
02148 xsh_free_vector(&(trace[ii]));
02149 }
02150 cpl_free(trace) ;
02151 trace=NULL;
02152 }
02153
02154 if (extracted!=NULL) {
02155 for (ii=0 ; ii<MAX_NO_ORD; ii++) {
02156 xsh_free_vector(&(extracted[ii]));
02157 }
02158 cpl_free(extracted);
02159 extracted=NULL;
02160 }
02161
02162 xsh_free_frame(&spec_form_frame);
02163 xsh_free_table(&spec_form_tab);
02164 xsh_free_image(&ima);
02165 xsh_free_image(&ima_ext);
02166 xsh_free_table(&tab_xy_guess);
02167 xsh_free_table(&tab_xy_peaks);
02168 xsh_free_table(&tab_xy_peaks_sel);
02169 xsh_free_table(&tab_tmp);
02170 xsh_free_table(&tab_tmp1);
02171 xsh_free_table(&tab_tmp2);
02172 xsh_free_table(&tab_xy_corr);
02173 cpl_vector_delete(spec_clean) ;
02174 if(cpl_error_get_code() != CPL_ERROR_NONE) {
02175 xsh_print_rec_status(0);
02176 }
02177 return cpl_error_get_code() ;
02178
02179 }
02180
02181
02182
02194
02195
02196 static cpl_error_code
02197 xsh_cfg_recover_prepare_pm_set(cpl_table* tab_pat,
02198 cpl_table* tab_dat,
02199 cpl_parameterlist* parameters,
02200 cpl_matrix** mat_pat,
02201 cpl_matrix** mat_dat,
02202 int* use_pat,
02203 int* use_dat)
02204
02205 {
02206
02207 int npat=0;
02208 int ndat=0;
02209 int i=0;
02210
02211 double* pxd=NULL;
02212 double* pyd=NULL;
02213 double* pxg=NULL;
02214 double* pyg=NULL;
02215
02216 int* pog=NULL;
02217 int* pod=NULL;
02218 int* ps=NULL;
02219
02220 int ord_min=0;
02221 int ord_max=0;
02222 int ord_min_dat=0;
02223 int ord_max_dat=0;
02224
02225 int ord=0;
02226
02227 int ord_sel=5;
02228 cpl_parameter* p=NULL;
02229 cpl_table* pat=NULL;
02230 cpl_table* dat=NULL;
02231 cpl_table* tmp=NULL;
02232
02233 int j=0;
02234 double rad=0;
02235 double xmin=0;
02236 double xmax=0;
02237 double ymin=0;
02238 double ymax=0;
02239
02240 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_ord_sel"));
02241 check(ord_sel = cpl_parameter_get_int(p));
02242
02243 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_radius"));
02244 check(rad = cpl_parameter_get_double(p));
02245
02246
02247 check(npat=cpl_table_get_nrow(tab_pat));
02248 check(ndat=cpl_table_get_nrow(tab_dat));
02249
02250
02251 check(xsh_sort_table_1(tab_pat,"ABS_ORD",CPL_TRUE));
02252 check(xsh_sort_table_1(tab_dat,"ABS_ORD",CPL_TRUE));
02253
02254 check(pat=cpl_table_duplicate(tab_pat));
02255 check(cpl_table_new_column(pat,"SEL",CPL_TYPE_INT));
02256 check(cpl_table_fill_column_window(pat,"SEL",0,npat,-1));
02257
02258
02259 check(ps=cpl_table_get_data_int(pat,"SEL"));
02260 check(pog=cpl_table_get_data_int(pat,"ABS_ORD"));
02261
02262 check(ord_min=cpl_table_get_column_min(pat,"ABS_ORD"));
02263 check(ord_max=cpl_table_get_column_max(pat,"ABS_ORD"));
02264
02265
02266 check(ord_min_dat=cpl_table_get_column_min(tab_dat,"ABS_ORD"));
02267 check(ord_max_dat=cpl_table_get_column_max(tab_dat,"ABS_ORD"));
02268
02269 xsh_msg("pat min=%d max=%d",ord_min,ord_max);
02270 xsh_msg("dat min=%d max=%d",ord_min_dat,ord_max_dat);
02271
02272
02273 *use_pat=0;
02274
02275
02276 for(ord=ord_min;ord<=ord_max;ord++) {
02277 for(i=0;i<npat;i++) {
02278 if((pog[i]==ord) && (j< ord_sel)) {
02279 ps[i]=1;
02280 j++;
02281 }
02282 }
02283 j=0;
02284 }
02285 check(*use_pat=cpl_table_and_selected_int(pat,"SEL",CPL_GREATER_THAN,0));
02286 check(tmp=cpl_table_extract_selected(pat));
02287
02288 check(cpl_table_save(pat,NULL,NULL,"pat.fits",CPL_IO_DEFAULT));
02289
02290
02291 check(dat=cpl_table_duplicate(tab_dat));
02292 check(cpl_table_new_column(dat,"SEL",CPL_TYPE_INT));
02293 check(cpl_table_fill_column_window(dat,"SEL",0,ndat,-1));
02294 check(ps=cpl_table_get_data_int(dat,"SEL"));
02295
02296 check(pod=cpl_table_get_data_int(dat,"ABS_ORD"));
02297 check(pxd=cpl_table_get_data_double(dat,"XP"));
02298 check(pyd=cpl_table_get_data_double(dat,"YP"));
02299
02300 check(pog=cpl_table_get_data_int(tmp,"ABS_ORD"));
02301 check(pxg=cpl_table_get_data_double(tmp,"XC"));
02302 check(pyg=cpl_table_get_data_double(tmp,"YC"));
02303
02304
02305 for(i=0;i<*use_pat;i++) {
02306 xmin=pxg[i]-rad;
02307 xmax=pxg[i]+rad;
02308 ymin=pyg[i]-rad;
02309 ymax=pyg[i]+rad;
02310 for(j=0;j<ndat;j++) {
02311 if( pog[i] == pod[j] ) {
02312 if(pxd[j] >= xmin && pxd[j] <= xmax) {
02313 if(pyd[j] >= ymin && pyd[j] <= ymax ) {
02314 ps[j]=1;
02315 }
02316 }
02317 }
02318 }
02319 }
02320 check(cpl_table_save(dat,NULL,NULL,"dat.fits",CPL_IO_DEFAULT));
02321 check(*use_dat=cpl_table_and_selected_int(dat,"SEL",CPL_GREATER_THAN,0));
02322
02323 xsh_msg("use_pattern=%d use_data=%d",*use_pat,*use_dat);
02324 if(*use_pat > *use_dat) {
02325 xsh_msg_error("You try to find a matching pattern searching ");
02326 xsh_msg_error("more points than available data. Try to increase ");
02327 xsh_msg_error("pm_radius and or peaks_factor ");
02328 xsh_msg_error("and/or decrease pm_ord_sel parameter values");
02329 goto cleanup;
02330 }
02331
02332 check(xsh_sort_table_1(dat,"SEL",CPL_TRUE));
02333 check(xsh_sort_table_1(pat,"SEL",CPL_TRUE));
02334 check(*mat_pat=cpl_matrix_new(2,npat));
02335 check(*mat_dat=cpl_matrix_new(2,ndat));
02336
02337 check(pxg=cpl_table_get_data_double(pat,"XC"));
02338 check(pyg=cpl_table_get_data_double(pat,"YC"));
02339 for(i=0;i<npat;i++) {
02340
02341 check(cpl_matrix_set(*mat_pat,0,i,pxg[i]));
02342 check(cpl_matrix_set(*mat_pat,1,i,pyg[i]));
02343
02344 }
02345
02346 check(pxd=cpl_table_get_data_double(dat,"XP"));
02347 check(pyd=cpl_table_get_data_double(dat,"YP"));
02348 for(i=0;i<ndat;i++) {
02349
02350 check(cpl_matrix_set(*mat_dat,0,i,pxd[i]));
02351 check(cpl_matrix_set(*mat_dat,1,i,pyd[i]));
02352
02353 }
02354
02355
02356 cleanup:
02357 xsh_free_table(&pat);
02358 xsh_free_table(&dat);
02359 xsh_free_table(&tmp);
02360
02361
02362 return cpl_error_get_code() ;
02363
02364 }
02365
02366
02367
02380
02381
02382
02383 static cpl_error_code
02384 xsh_cfg_recover_pattern_match(cpl_parameterlist* parameters,
02385 cpl_matrix* mat_gue,
02386 cpl_matrix* mat_dat,
02387 int use_pattern,
02388 int use_data,
02389 int debug_level)
02390 {
02391 cpl_array *matches = NULL;
02392
02393 double err_data=1;
02394 double err_pattern=0;
02395 double tolerance=0.1;
02396 double radius=1;
02397 cpl_matrix* mdata=NULL;
02398 cpl_matrix* mpattern=NULL;
02399 cpl_parameter* p=NULL;
02400 cpl_table* pm_tbl=NULL;
02401 cpl_table* dat_tbl=NULL;
02402 cpl_table*gue_tbl=NULL;
02403
02404 int nmatch=0;
02405 double* pcatx=NULL;
02406 double* pcaty=NULL;
02407 double* ppatx=NULL;
02408 double* ppaty=NULL;
02409 int i=0;
02410 int ndat=0;
02411 int ngue=0;
02412 double lin_scale=1.0;
02413 double lin_angle=0.0;
02414
02415
02416 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_tolerance"));
02417 check(tolerance = cpl_parameter_get_double(p));
02418
02419 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_radius"));
02420 check(radius = cpl_parameter_get_double(p));
02421
02422
02423
02424 ndat=cpl_matrix_get_ncol(mat_dat);
02425 ngue=cpl_matrix_get_ncol(mat_gue);
02426
02427 check(dat_tbl=cpl_table_new(ndat));
02428 check(gue_tbl=cpl_table_new(ngue));
02429
02430 check(cpl_table_new_column(dat_tbl,"XD",CPL_TYPE_DOUBLE));
02431 check(cpl_table_new_column(dat_tbl,"YD",CPL_TYPE_DOUBLE));
02432 check(cpl_table_fill_column_window(dat_tbl,"XD",0,ndat,0));
02433 check(cpl_table_fill_column_window(dat_tbl,"YD",0,ndat,0));
02434 check(pcatx=cpl_table_get_data_double(dat_tbl,"XD"));
02435 check(pcaty=cpl_table_get_data_double(dat_tbl,"YD"));
02436
02437
02438
02439 for(i=0;i<ndat;i++) {
02440
02441 check(pcatx[i]=cpl_matrix_get(mat_dat,0,i));
02442 check(pcaty[i]=cpl_matrix_get(mat_dat,1,i));
02443
02444
02445 }
02446
02447 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02448 check(cpl_table_save(dat_tbl,NULL,NULL,"pm_dat.fits",CPL_IO_DEFAULT));
02449 }
02450
02451 check(cpl_table_new_column(gue_tbl,"XG",CPL_TYPE_DOUBLE));
02452 check(cpl_table_new_column(gue_tbl,"YG",CPL_TYPE_DOUBLE));
02453 check(cpl_table_fill_column_window(gue_tbl,"XG",0,ngue,0));
02454 check(cpl_table_fill_column_window(gue_tbl,"YG",0,ngue,0));
02455 check(ppatx=cpl_table_get_data_double(gue_tbl,"XG"));
02456 check(ppaty=cpl_table_get_data_double(gue_tbl,"YG"));
02457
02458
02459 for(i=0;i<ngue;i++) {
02460
02461 check(ppatx[i]=cpl_matrix_get(mat_gue,0,i));
02462 check(ppaty[i]=cpl_matrix_get(mat_gue,1,i));
02463
02464
02465 }
02466
02467 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02468 check(cpl_table_save(gue_tbl,NULL,NULL,"pm_gue.fits",CPL_IO_DEFAULT));
02469 }
02470 check(matches = cpl_ppm_match_points(mat_dat,use_data,err_data,
02471 mat_gue,use_pattern,err_pattern,
02472 tolerance,radius,&mdata, &mpattern,
02473 &lin_scale,&lin_angle));
02474
02475 check(nmatch=cpl_matrix_get_ncol(mpattern));
02476 xsh_msg("%d points matched linear scale factor=%f linear angle factor %f",
02477 nmatch,lin_scale,lin_angle);
02478
02479 check(pm_tbl=cpl_table_new(nmatch));
02480 check(cpl_table_new_column(pm_tbl,"PM_CAT_X",CPL_TYPE_DOUBLE));
02481 check(cpl_table_new_column(pm_tbl,"PM_CAT_Y",CPL_TYPE_DOUBLE));
02482 check(cpl_table_new_column(pm_tbl,"PM_PAT_X",CPL_TYPE_DOUBLE));
02483 check(cpl_table_new_column(pm_tbl,"PM_PAT_Y",CPL_TYPE_DOUBLE));
02484
02485 check(cpl_table_fill_column_window(pm_tbl,"PM_CAT_X",0,nmatch,0));
02486 check(cpl_table_fill_column_window(pm_tbl,"PM_CAT_Y",0,nmatch,0));
02487 check(cpl_table_fill_column_window(pm_tbl,"PM_PAT_X",0,nmatch,0));
02488 check(cpl_table_fill_column_window(pm_tbl,"PM_PAT_Y",0,nmatch,0));
02489
02490
02491 check(ppatx=cpl_table_get_data_double(pm_tbl,"PM_CAT_X"));
02492 check(ppaty=cpl_table_get_data_double(pm_tbl,"PM_CAT_Y"));
02493
02494 check(pcatx=cpl_table_get_data_double(pm_tbl,"PM_PAT_X"));
02495 check(pcaty=cpl_table_get_data_double(pm_tbl,"PM_PAT_Y"));
02496
02497 for(i=0;i<nmatch;i++) {
02498
02499 check(pcatx[i]=cpl_matrix_get(mdata,0,i));
02500 check(pcaty[i]=cpl_matrix_get(mdata,1,i));
02501
02502 check(ppatx[i]=cpl_matrix_get(mpattern,0,i));
02503 check(ppaty[i]=cpl_matrix_get(mpattern,1,i));
02504
02505
02506 }
02507
02508 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02509 check(cpl_table_save(pm_tbl,NULL,NULL,
02510 "pattern_match.fits",CPL_IO_DEFAULT));
02511 }
02512 xsh_free_table(&gue_tbl);
02513 xsh_free_table(&dat_tbl);
02514 xsh_free_table(&pm_tbl);
02515 xsh_free_array(&matches);
02516 xsh_free_matrix(&mpattern);
02517 xsh_free_matrix(&mdata);
02518
02519 cleanup:
02520 if(cpl_error_get_code() != CPL_ERROR_NONE) {
02521 xsh_msg("Try to increase pm_radius and/or peak_factor");
02522 }
02523 return cpl_error_get_code() ;
02524
02525 }
02526
02527
02528
02529
02538
02539 static cpl_error_code
02540 xsh_cfg_recover_add_peaks_xpos(cpl_frame* order_tab_centr,
02541 xsh_instrument* instr,
02542 cpl_table** tab_xy_peaks)
02543 {
02544
02545 xsh_order_list* order_list = NULL;
02546 int size=0;
02547 double* px=NULL;
02548 double* py=NULL;
02549 int* po=NULL;
02550 int ord_min=0;
02551 int ord_rel=0;
02552
02553
02554 int i=0;
02555
02556 check(size=cpl_table_get_nrow(*tab_xy_peaks));
02557 check(cpl_table_new_column(*tab_xy_peaks,"XP",CPL_TYPE_DOUBLE));
02558 check(cpl_table_fill_column_window(*tab_xy_peaks,"XP",0,size,-1));
02559
02560 check(px=cpl_table_get_data_double(*tab_xy_peaks,"XP"));
02561 check(py=cpl_table_get_data_double(*tab_xy_peaks,"YP"));
02562 check(po=cpl_table_get_data_int(*tab_xy_peaks,"ABS_ORD"));
02563 ord_min=cpl_table_get_column_min(*tab_xy_peaks,"ABS_ORD");
02564
02565 check(order_list = xsh_order_list_load(order_tab_centr,instr));
02566
02567
02568 for(i=0;i<size;i++) {
02569
02570 check( ord_rel = xsh_order_list_get_order( order_list, po[i] ) ) ;
02571
02572 ord_rel=po[i]-ord_min;
02573
02574
02575
02576 if(cpl_polynomial_get_dimension(order_list->list[ord_rel].cenpoly) <
02577 XSH_ORDPOS_POL_DIM_MAX) {
02578
02579 check( px[i] = cpl_polynomial_eval_1d(
02580 order_list->list[ord_rel].cenpoly,py[i],NULL));
02581 }
02582
02583
02584
02585 }
02586
02587 cleanup:
02588 xsh_order_list_free(&order_list);
02589
02590 return cpl_error_get_code();
02591
02592
02593 }
02594
02595
02606
02607
02608 static cpl_table*
02609 xsh_cfg_recover_remove_blends(cpl_table* tab_xy_guess,
02610 cpl_table* tab_xy_peaks_sel,
02611 const int thresh_x,
02612 const int thresh_y)
02613 {
02614
02615 double* pxg=NULL;
02616 double* pyg=NULL;
02617 double* pyt=NULL;
02618 double* pyp=NULL;
02619
02620 double* pxt=NULL;
02621 double* pxp=NULL;
02622 int* pwp=NULL;
02623 int* pap=NULL;
02624 int* prp=NULL;
02625
02626
02627
02628 double* pxr=NULL;
02629 double* pyr=NULL;
02630 int* pwr=NULL;
02631 int* par=NULL;
02632 int* prr=NULL;
02633
02634 double x=0;
02635 double y=0;
02636 int r=0;
02637 int a=0;
02638 int w=0;
02639
02640
02641 int ord=0;
02642 int ord_min=0;
02643 int ord_max=0;
02644 int nord=0;
02645 int npks=0;
02646 cpl_table* tab_tmp=NULL;
02647 cpl_table* tab_tmp1=NULL;
02648 cpl_table* tab_tmp2=NULL;
02649
02650 cpl_table* result=NULL;
02651 int found=0;
02652 int i=0;
02653 int j=0;
02654
02655 int k=0;
02656 int nrows=0;
02657
02658 pxg=cpl_table_get_data_double(tab_xy_guess,"XC");
02659 pyg=cpl_table_get_data_double(tab_xy_guess,"YC");
02660 ord_min=cpl_table_get_column_min(tab_xy_guess,"ABS_ORD");
02661 ord_max=cpl_table_get_column_max(tab_xy_guess,"ABS_ORD");
02662
02663 check(tab_tmp2=cpl_table_duplicate(tab_xy_peaks_sel));
02664 check(nrows=cpl_table_get_nrow(tab_tmp2));
02665 check(cpl_table_fill_column_window(tab_tmp2,"YP",0,nrows,-1));
02666 check(cpl_table_fill_column_window(tab_tmp2,"XP",0,nrows,-1));
02667 check(cpl_table_fill_column_window(tab_tmp2,"REL_ORD",0,nrows,-1));
02668 check(cpl_table_fill_column_window(tab_tmp2,"ABS_ORD",0,nrows,-1));
02669 check(cpl_table_fill_column_window(tab_tmp2,"WEIGHT",0,nrows,-1));
02670
02671 check(pxr=cpl_table_get_data_double(tab_tmp2,"XP"));
02672 check(pyr=cpl_table_get_data_double(tab_tmp2,"YP"));
02673 check(prr=cpl_table_get_data_int(tab_tmp2,"REL_ORD"));
02674 check(pwr=cpl_table_get_data_int(tab_tmp2,"WEIGHT"));
02675 check(par=cpl_table_get_data_int(tab_tmp2,"ABS_ORD"));
02676
02677 for(ord=ord_min;ord<=ord_max;ord++) {
02678 check(nord=cpl_table_and_selected_int(tab_xy_guess,"ABS_ORD",
02679 CPL_EQUAL_TO,ord));
02680 check(npks=cpl_table_and_selected_int(tab_xy_peaks_sel,"ABS_ORD",
02681 CPL_EQUAL_TO,ord));
02682 xsh_free_table(&tab_tmp);
02683 check(tab_tmp=cpl_table_extract_selected(tab_xy_guess));
02684 xsh_free_table(&tab_tmp1);
02685 check(tab_tmp1=cpl_table_extract_selected(tab_xy_peaks_sel));
02686
02687 pxt=cpl_table_get_data_double(tab_tmp,"XC");
02688 pyt=cpl_table_get_data_double(tab_tmp,"YC");
02689
02690 pxp=cpl_table_get_data_double(tab_tmp1,"XP");
02691 pyp=cpl_table_get_data_double(tab_tmp1,"YP");
02692 prp=cpl_table_get_data_int(tab_tmp1,"REL_ORD");
02693 pwp=cpl_table_get_data_int(tab_tmp1,"WEIGHT");
02694 pap=cpl_table_get_data_int(tab_tmp1,"ABS_ORD");
02695
02696
02697 for(i=0;i<nord;i++) {
02698 found=0;
02699 for(j=0;j<npks;j++) {
02700
02701 if((fabs(pxp[j]-pxt[i]) < thresh_x) &&
02702 (fabs(pyp[j]-pyt[i]) < thresh_y)) {
02703 found++;
02704 r=prp[j];
02705 w=pwp[j];
02706 a=pap[j];
02707 x=pxp[j];
02708 y=pyp[j];
02709
02710 }
02711 }
02712 if(found==1) {
02713 pxr[k]=x;
02714 pyr[k]=y;
02715 prr[k]=r;
02716 pwr[k]=w;
02717 par[k]=a;
02718 k++;
02719
02720 }
02721 }
02722 check(cpl_table_select_all(tab_xy_peaks_sel));
02723 check(cpl_table_select_all(tab_xy_guess));
02724 }
02725
02726 check(cpl_table_and_selected_int(tab_tmp2,"REL_ORD",CPL_GREATER_THAN,-1));
02727 check(result=cpl_table_extract_selected(tab_tmp2));
02728 check(cpl_table_name_column(result,"XP","XC"));
02729 check(cpl_table_name_column(result,"YP","YC"));
02730
02731 cleanup:
02732 xsh_free_table(&tab_tmp);
02733 xsh_free_table(&tab_tmp1);
02734 xsh_free_table(&tab_tmp2);
02735
02736
02737 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02738 return NULL;
02739 } else {
02740 return result;
02741 }
02742
02743 }
02744
02745
02755
02756
02757 static cpl_table*
02758 xsh_cfg_recover_select_peaks(cpl_table* tab_xy_guess,
02759 cpl_table* tab_xy_peaks,
02760 const double factor)
02761 {
02762
02763
02764 int ord=0;
02765 int nord=0;
02766 int ord_min=0;
02767 int ord_max=0;
02768 int ord_min_peaks=0;
02769 int ord_max_peaks=0;
02770 int ord_min_guess=0;
02771 int ord_max_guess=0;
02772
02773 int npks=0;
02774 cpl_table* tab_tmp=NULL;
02775 cpl_table* tab_tmp1=NULL;
02776 cpl_table* result=NULL;
02777 int weight_min=0;
02778 int nrows=0;
02779
02780 check(ord_min_guess=cpl_table_get_column_min(tab_xy_guess,"ABS_ORD"));
02781 check(ord_max_guess=cpl_table_get_column_max(tab_xy_guess,"ABS_ORD"));
02782
02783 check(ord_min_peaks=cpl_table_get_column_min(tab_xy_peaks,"ABS_ORD"));
02784 check(ord_max_peaks=cpl_table_get_column_max(tab_xy_peaks,"ABS_ORD"));
02785 ord_min=(ord_min_guess<=ord_min_peaks) ? ord_min_guess : ord_min_peaks;
02786 ord_max=(ord_max_guess>=ord_max_peaks) ? ord_max_guess : ord_max_peaks;
02787
02788 cpl_table_select_all(tab_xy_peaks);
02789
02790 xsh_msg("select lines in order range=[%d,%d]",ord_min,ord_max);
02791
02792
02793 for(ord=ord_min;ord<=ord_max;ord++) {
02794
02795 nord=cpl_table_and_selected_int(tab_xy_guess,"ABS_ORD",CPL_EQUAL_TO,ord);
02796 npks=cpl_table_and_selected_int(tab_xy_peaks,"ABS_ORD",CPL_EQUAL_TO,ord);
02797 check(tab_tmp=cpl_table_extract_selected(tab_xy_peaks));
02798
02799 if(npks>nord) {
02800 weight_min=cpl_table_get_column_min(tab_tmp,"WEIGHT");
02801 check(npks=cpl_table_and_selected_int(tab_tmp,"WEIGHT",
02802 CPL_NOT_GREATER_THAN,
02803 (int)(weight_min+factor*nord)));
02804 tab_tmp1=cpl_table_extract_selected(tab_tmp);
02805
02806
02807
02808 } else {
02809 tab_tmp1=cpl_table_duplicate(tab_tmp);
02810 }
02811 if(ord == ord_min) {
02812 result=cpl_table_duplicate(tab_tmp1);
02813 } else {
02814 nrows=cpl_table_get_nrow(tab_xy_peaks);
02815 cpl_table_insert(result,tab_tmp1,nrows);
02816 }
02817
02818 xsh_free_table(&tab_tmp1);
02819 xsh_free_table(&tab_tmp);
02820 cpl_table_select_all(tab_xy_peaks);
02821 cpl_table_select_all(tab_xy_guess);
02822 }
02823
02824 cleanup:
02825 xsh_free_table(&tab_tmp1);
02826 xsh_free_table(&tab_tmp);
02827
02828 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02829 return NULL;
02830 } else {
02831 return result;
02832 }
02833
02834
02835 }
02836
02837
02838
02847
02848 static cpl_table*
02849 xsh_cfg_recover_measure_tab_xy_peaks(cpl_image* ima_ext,
02850 cpl_parameterlist* parameters)
02851 {
02852
02853 const char* name_p="ima_peaks.fits";
02854 const int size=5000;
02855 cpl_table* result=NULL;
02856 cpl_propertylist* plist=NULL;
02857 double* pp=NULL;
02858 int* po=NULL;
02859 int row=0;
02860 int ord=0;
02861 int ord_num=0;
02862
02863 double* pv=NULL;
02864 int sv=0;
02865 int* pw=NULL;
02866 int nrows=0;
02867 cpl_vector* spectrum=NULL;
02868 cpl_vector* peaks=NULL;
02869 int display=0;
02870 int line_fwhm=4;
02871 double kappa=5;
02872 cpl_parameter* p=NULL;
02873 int i=0;
02874 cpl_table* tab_tmp=NULL;
02875 int debug_level=0;
02876 char ext_val[10];
02877
02878 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.plot"));
02879 check(display = cpl_parameter_get_bool(p));
02880
02881 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_line_fwhm"));
02882 check(line_fwhm = cpl_parameter_get_int(p));
02883
02884 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_kappa"));
02885 check(kappa = cpl_parameter_get_double(p));
02886 check(debug_level=xsh_parameters_debug_level_get("xsh_cfg_recover",parameters));
02887 ord_num=cpl_image_get_size_y(ima_ext);
02888
02889 result=cpl_table_new(size);
02890 cpl_table_new_column(result,"YP",CPL_TYPE_DOUBLE);
02891 cpl_table_new_column(result,"REL_ORD",CPL_TYPE_INT);
02892 check(cpl_table_fill_column_window(result,"YP",0,size,-1));
02893 check(cpl_table_fill_column_window(result,"REL_ORD",0,size,-1));
02894
02895 pp=cpl_table_get_data_double(result,"YP");
02896 po=cpl_table_get_data_int(result,"REL_ORD");
02897
02898 row=0;
02899 for(ord=0;ord<ord_num;ord++) {
02900 check(spectrum=cpl_vector_new_from_image_row(ima_ext,ord+1));
02901 check(peaks=xsh_spectrum_detect_peaks(spectrum,line_fwhm,kappa,display));
02902 if(debug_level>XSH_DEBUG_LEVEL_NONE) {
02903 plist=cpl_propertylist_new();
02904 sprintf(ext_val,"%s%d","ext",ord);
02905 cpl_propertylist_append_string(plist,"EXTNAME",ext_val);
02906 if(ord==0) {
02907 cpl_vector_save(spectrum,name_p,CPL_BPP_IEEE_FLOAT,plist,
02908 CPL_IO_DEFAULT);
02909 } else {
02910 cpl_vector_save(spectrum,name_p,CPL_BPP_IEEE_FLOAT,
02911 plist,CPL_IO_EXTEND);
02912 }
02913 xsh_free_propertylist(&plist);
02914 }
02915 if(peaks!=NULL) {
02916 check(sv=cpl_vector_get_size(peaks));
02917 if (sv>0) {
02918 check(pv=cpl_vector_get_data(peaks));
02919 for(i=0;i<sv;i++) {
02920 po[row]=ord;
02921 pp[row]=pv[i];
02922 row++;
02923 }
02924 }
02925 }
02926 xsh_free_vector(&spectrum);
02927 xsh_free_vector(&peaks);
02928 }
02929 check(cpl_table_and_selected_double(result,"YP",CPL_GREATER_THAN,-1));
02930 check(tab_tmp=cpl_table_extract_selected(result));
02931 xsh_free_table(&result);
02932 result=cpl_table_duplicate(tab_tmp);
02933 xsh_free_table(&tab_tmp);
02934
02935 check(cpl_table_erase_invalid_rows(result));
02936
02937 cpl_table_new_column(result,"WEIGHT",CPL_TYPE_INT);
02938 nrows=cpl_table_get_nrow(result);
02939 xsh_msg("size=%d",size);
02940 check(cpl_table_fill_column_window(result,"WEIGHT",0,size,-1));
02941 check(pw=cpl_table_get_data_int(result,"WEIGHT"));
02942
02943 for(i=0;i<nrows;i++) {
02944 pw[i]=i;
02945 }
02946
02947 cleanup:
02948 xsh_free_table(&tab_tmp);
02949 xsh_free_vector(&spectrum);
02950 xsh_free_vector(&peaks);
02951
02952 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02953 return NULL;
02954 } else {
02955 return result;
02956 }
02957
02958 }
02959
02960
02961
02973
02974 static cpl_image*
02975 xsh_cfg_recover_linear_ext(cpl_frame* raw_frm,
02976 cpl_frame* order_tab_centr,
02977 xsh_instrument* instr,
02978 const int slit,
02979 const double min)
02980
02981 {
02982 const char* name=NULL;
02983 cpl_image* input=NULL;
02984
02985 const char* name_o="ima_raw.fits";
02986 const char* name_e="ima_ext.fits";
02987 int sx=0;
02988 int sy=0;
02989 int ord_num=0;
02990 int ord_min_tra=0;
02991 int ord_max_tra=0;
02992
02993 int ord=0;
02994
02995 cpl_image* result=NULL;
02996 double* pou=NULL;
02997 double* pin=NULL;
02998 int i=0;
02999 int j=0;
03000 double x=0;
03001 int s=0;
03002 double flux=0;
03003 int pix=0;
03004 xsh_order_list* order_list = NULL;
03005 cpl_table* tab_centr=NULL;
03006
03007 check(name=cpl_frame_get_filename(raw_frm));
03008 check(input=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
03009 check(cpl_image_threshold(input,min,FLT_MAX,0,FLT_MAX));
03010 check(cpl_image_save(input,name_o,CPL_BPP_IEEE_FLOAT,NULL,
03011 CPL_IO_DEFAULT));
03012
03013 xsh_msg("extracting image %s",name_o);
03014
03015 check(sx=cpl_image_get_size_x(input));
03016 check(sy=cpl_image_get_size_y(input));
03017 check(name=cpl_frame_get_filename( order_tab_centr));
03018 check(tab_centr=cpl_table_load(name,1,0));
03019
03020 ord_min_tra=cpl_table_get_column_min(tab_centr,"ABSORDER");
03021 ord_max_tra=cpl_table_get_column_max(tab_centr,"ABSORDER");
03022 xsh_free_table(&tab_centr);
03023
03024 ord_num=ord_max_tra-ord_min_tra+1;
03025
03026
03027 check(result=cpl_image_new(sy,ord_num,CPL_TYPE_DOUBLE));
03028 check(pou=cpl_image_get_data_double(result));
03029 check(pin=cpl_image_get_data_double(input));
03030
03031
03032 check(order_list = xsh_order_list_load(order_tab_centr,instr));
03033
03034
03035
03036
03037 for(ord=0;ord<ord_num;ord++) {
03038 for(j=0;j<sy;j++) {
03039 flux=0;
03040 if(cpl_polynomial_get_dimension(order_list->list[ord].cenpoly) <
03041 XSH_ORDPOS_POL_DIM_MAX) {
03042 check(x=cpl_polynomial_eval_1d(order_list->list[ord].cenpoly,j,NULL));
03043
03044 }
03045 i=(int)(x+0.5);
03046 for(s=-slit;s<=slit;s++) {
03047 pix=j*sx+i+s;
03048 if ( (pix>0) && (pix<sx*sy) ) {
03049 flux+=pin[pix];
03050
03051 }
03052 }
03053 pou[ord*sy+j]=flux;
03054 }
03055 }
03056
03057
03058 check(cpl_image_save(result,name_e,CPL_BPP_IEEE_FLOAT,NULL,
03059 CPL_IO_DEFAULT));
03060
03061 cleanup:
03062 xsh_free_image(&input);
03063 xsh_free_table(&tab_centr);
03064 xsh_order_list_free( &order_list ) ;
03065
03066 if (cpl_error_get_code() != CPL_ERROR_NONE) {
03067 return NULL;
03068 } else {
03069 return result;
03070 }
03071
03072 }
03073
03074
03085
03086 static cpl_error_code
03087 xsh_cfg_recover_guess_tab_corr_by_ordpos(xsh_instrument* instr,
03088 cpl_frame* order_tab_centr,
03089 cpl_frame** model_xy_gue) {
03090
03091 cpl_table* tab_xy_guess=NULL;
03092 xsh_order_list* order_list = NULL;
03093 int size=0;
03094 double* pxc=NULL;
03095 double* pyc=NULL;
03096 double* px=NULL;
03097 double* py=NULL;
03098 int* pog=NULL;
03099 int* por=NULL;
03100 int* poa=NULL;
03101 int ord_min_tra=0;
03102 int ord_max_tra=0;
03103
03104 int ord_min_gue=0;
03105 int ord_max_gue=0;
03106 int ord_min_off=0;
03107 int ord_max_off=0;
03108
03109 int ord_rel=0;
03110
03111
03112
03113 const char* name_c=NULL;
03114 const char* name_g=NULL;
03115 int i=0;
03116 cpl_table * tab_centr=NULL;
03117 int sizey=4096;
03118 int j=0;
03119 int ord=0;
03120 int nord=0;
03121
03122 check(name_g=cpl_frame_get_filename(*model_xy_gue));
03123
03124 check(tab_xy_guess=cpl_table_load(name_g,1,0));
03125
03126 check(size=cpl_table_get_nrow(tab_xy_guess));
03127 check(cpl_table_new_column(tab_xy_guess,"XC",CPL_TYPE_DOUBLE));
03128 check(cpl_table_new_column(tab_xy_guess,"YC",CPL_TYPE_DOUBLE));
03129 check(cpl_table_fill_column_window(tab_xy_guess,"XC",0,size,-1));
03130 check(cpl_table_fill_column_window(tab_xy_guess,"YC",0,size,-1));
03131
03132 check(pxc=cpl_table_get_data_double(tab_xy_guess,"XC"));
03133 check(pyc=cpl_table_get_data_double(tab_xy_guess,"YC"));
03134 check(px=cpl_table_get_data_double(tab_xy_guess,"XG"));
03135 check(py=cpl_table_get_data_double(tab_xy_guess,"YG"));
03136 check(pog=cpl_table_get_data_int(tab_xy_guess,"ABS_ORD"));
03137 check(ord_min_gue=cpl_table_get_column_min(tab_xy_guess,"ABS_ORD"));
03138 check(ord_max_gue=cpl_table_get_column_max(tab_xy_guess,"ABS_ORD"));
03139
03140 if (order_tab_centr!=NULL) {
03141 name_c=cpl_frame_get_filename(order_tab_centr);
03142 xsh_msg("tab centre file=%s",name_c);
03143 check(tab_centr=cpl_table_load(name_c,1,0));
03144 check(ord_min_tra=cpl_table_get_column_min(tab_centr,"ABSORDER"));
03145 check(ord_max_tra=cpl_table_get_column_max(tab_centr,"ABSORDER"));
03146 xsh_free_table(&tab_centr);
03147
03148
03149 xsh_msg("Trace tab order min=%d max=%d",ord_min_tra,ord_max_tra);
03150 check(order_list = xsh_order_list_load(order_tab_centr,instr));
03151 xsh_msg("Guess tab order min=%d max=%d",ord_min_gue,ord_max_gue);
03152 ord_min_off=ord_min_gue-ord_min_tra;
03153 ord_max_off=ord_max_gue-ord_max_tra;
03154 xsh_msg_debug("offset ord min=%d",ord_min_off);
03155 xsh_msg_debug("offset ord max=%d",ord_max_off);
03156
03157 if(ord_min_off!=0) {
03158 xsh_msg_error("The guess line table has lines in an order not traced");
03159 xsh_msg_error("Remove lines from guess at order %d",ord_min_gue);
03160 xsh_msg_error("Or allow xsh_orderpos to trace order %d",ord_min_gue);
03161 xsh_msg_error("(For example changing XSH_SPECTRALFORMAT_TABLE_arm");
03162 cpl_error_set(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT);
03163 goto cleanup;
03164 }
03165
03166 xsh_msg_debug("size=%d ord_min_gue=%d ord_max_gue=%d",
03167 size,ord_min_gue,ord_max_gue);
03168
03169 for(i=0;i<size;i++) {
03170 pyc[i]=py[i];
03171
03172
03173 check( ord_rel = xsh_order_list_get_order( order_list, pog[i] ) ) ;
03174
03175 ord_rel=pog[i]-ord_min_gue+ord_min_off;
03176
03177
03178
03179 if(ord_rel>-1) {
03180 if(cpl_polynomial_get_dimension(order_list->list[ord_rel].cenpoly) <
03181 XSH_ORDPOS_POL_DIM_MAX) {
03182 check( pxc[i] = cpl_polynomial_eval_1d(order_list->list[ord_rel].cenpoly,py[i],NULL));
03183 }
03184 }
03185
03186
03187
03188 }
03189
03190
03191
03192
03193 nord=ord_max_tra-ord_min_tra+1;
03194 size=nord*sizey;
03195 check(tab_centr=cpl_table_new(nord*sizey));
03196 check(cpl_table_new_column(tab_centr,"X",CPL_TYPE_DOUBLE));
03197 check(cpl_table_new_column(tab_centr,"Y",CPL_TYPE_DOUBLE));
03198 check(cpl_table_new_column(tab_centr,"ORDER",CPL_TYPE_INT));
03199 check(cpl_table_new_column(tab_centr,"ABSORDER",CPL_TYPE_INT));
03200 check(cpl_table_fill_column_window(tab_centr,"X",0,size,-1));
03201 check(cpl_table_fill_column_window(tab_centr,"Y",0,size,-1));
03202 check(cpl_table_fill_column_window(tab_centr,"ORDER",0,size,-1));
03203 check(cpl_table_fill_column_window(tab_centr,"ABSORDER",0,size,-1));
03204 check(px=cpl_table_get_data_double(tab_centr,"X"));
03205 check(py=cpl_table_get_data_double(tab_centr,"Y"));
03206 check(por=cpl_table_get_data_int(tab_centr,"ORDER"));
03207 check(poa=cpl_table_get_data_int(tab_centr,"ABSORDER"));
03208
03209
03210 i=0;
03211 for(ord=0;ord<nord;ord++) {
03212 for(j=0;j<sizey;j++) {
03213
03214 poa[i]=ord+ord_min_tra;
03215 por[i]=ord+1;
03216 py[i]=(double)j;
03217 check(px[i]=cpl_polynomial_eval_1d(order_list->list[ord].cenpoly,py[i],
03218 NULL));
03219 i++;
03220 }
03221 }
03222 check(cpl_table_save(tab_centr,NULL,NULL,"tab_centr.fits",CPL_IO_DEFAULT));
03223
03224 }
03225 check(cpl_table_save(tab_xy_guess, NULL, NULL, name_g, CPL_IO_DEFAULT));
03226
03227 cleanup:
03228
03229 xsh_free_table(&tab_centr);
03230 xsh_free_table(&tab_xy_guess);
03231 xsh_order_list_free( &order_list);
03232 if(cpl_error_get_code() != CPL_ERROR_NONE) {
03233 xsh_print_rec_status(0);
03234 }
03235 return cpl_error_get_code();
03236
03237 }
03238
03239
03248
03249 static cpl_frame*
03250 xsh_cfg_recover_gen_xyg_frame(cpl_frame * wave_list,
03251 cpl_frame * config_frame,
03252 xsh_instrument* inst, int prescan)
03253 {
03254
03255 cpl_table* lines_tab=NULL ;
03256 cpl_frame* result=NULL;
03257 struct xs_3* p_xs_3_config=NULL;
03258 struct xs_3 xs_3_config;
03259 cpl_table* lines_xyg=NULL;
03260 cpl_propertylist* header=NULL;
03261 char name_o[80];
03262 const char* tag=XSH_GET_TAG_FROM_ARM(XSH_MODEL_GUESS_XY,inst);
03263
03264 sprintf(name_o,"%s%s",tag,".fits");
03265
03266
03267
03268 p_xs_3_config=&xs_3_config;
03269 if (xsh_model_config_load_best(config_frame, p_xs_3_config) !=
03270 CPL_ERROR_NONE) {
03271 xsh_msg_error("Cannot load %s as a config",
03272 cpl_frame_get_filename(config_frame)) ;
03273 return NULL ;
03274 }
03275
03276
03277 check(lines_tab = cpl_table_load(cpl_frame_get_filename(wave_list), 1, 0)) ;
03278
03279
03280 check(xsh_cfg_recover_gen_xyg(lines_tab,p_xs_3_config,inst,prescan,&lines_xyg));
03281
03282
03283
03284 header=cpl_propertylist_new();
03285 check( xsh_pfits_set_pcatg(header,tag));
03286 cpl_table_save(lines_xyg, header, NULL,name_o, CPL_IO_DEFAULT);
03287
03288
03289 result=xsh_frame_product(name_o,tag,CPL_FRAME_TYPE_TABLE,
03290 CPL_FRAME_GROUP_PRODUCT,
03291 CPL_FRAME_LEVEL_FINAL);
03292 cpl_error_reset();
03293
03294 cleanup:
03295 xsh_free_propertylist(&header);
03296 xsh_free_table(&lines_tab);
03297 xsh_free_table(&lines_xyg);
03298
03299
03300 if (cpl_error_get_code() != CPL_ERROR_NONE) {
03301 return NULL;
03302 } else {
03303 return result;
03304 }
03305
03306 }
03307
03308
03319
03320
03321 static cpl_error_code
03322 xsh_cfg_recover_gen_xyg(
03323 cpl_table * lines_tab,
03324 struct xs_3* p_xs_3_config,
03325 xsh_instrument* inst,int pre_scan,
03326 cpl_table ** lines_xyg)
03327 {
03328 double x,y;
03329 float* pw=NULL;
03330 int row=0;
03331
03332 int i=0;
03333 double blaze_wav=0;
03334 double lambda_min=0;
03335 double lambda_max=0;
03336 int morder_cnt=0;
03337 double m_to_mu=1.e6;
03338 int lines_tot;
03339 int prescan=0;
03340 if(pre_scan) {
03341 prescan=-50;
03342 }
03343 lines_tot = cpl_table_get_nrow(lines_tab) ;
03344 xsh_msg("lines_tot=%d",lines_tot);
03345 *lines_xyg = cpl_table_new(lines_tot*3);
03346 cpl_table_new_column(*lines_xyg,"WAVELENGTH",CPL_TYPE_DOUBLE);
03347 cpl_table_new_column(*lines_xyg,"XG",CPL_TYPE_DOUBLE);
03348 cpl_table_new_column(*lines_xyg,"YG",CPL_TYPE_DOUBLE);
03349 cpl_table_new_column(*lines_xyg,"ABS_ORD",CPL_TYPE_INT);
03350
03351 pw=cpl_table_get_data_float(lines_tab,"WAVELENGTH");
03352 xsh_msg("order min:%d max: %d",
03353 p_xs_3_config->morder_min,p_xs_3_config->morder_max);
03354
03355 for(i=0;i<lines_tot;i++) {
03356 for (morder_cnt= p_xs_3_config->morder_min;
03357 morder_cnt<=p_xs_3_config->morder_max;
03358 morder_cnt++) {
03359 blaze_wav=2*(sin(-p_xs_3_config->nug))/(morder_cnt*p_xs_3_config->sg);
03360 lambda_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
03361 lambda_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
03362 lambda_min *=m_to_mu;
03363 lambda_max *=m_to_mu;
03364
03365
03366 if(pw[i]> lambda_min*0.98 && pw[i]<1.02*lambda_max) {
03367 check(xsh_model_get_xy(p_xs_3_config,inst,pw[i],
03368 morder_cnt,0.0,&x,&y));
03369
03370
03371 cpl_table_set_double(*lines_xyg,"WAVELENGTH",row,pw[i]);
03372 cpl_table_set_int(*lines_xyg,"ABS_ORD",row,morder_cnt);
03373 cpl_table_set_double(*lines_xyg,"XG",row,x-prescan);
03374 cpl_table_set_double(*lines_xyg,"YG",row,y);
03375 row++;
03376 }
03377 }
03378 }
03379
03380
03381
03382
03383
03384
03385
03386
03387
03388
03389
03390 cpl_table_erase_invalid(*lines_xyg);
03391
03392 xsh_msg("lines inc. overlap: %" CPL_SIZE_FORMAT "\n",cpl_table_get_nrow(*lines_xyg));
03393
03394 cleanup:
03395 return cpl_error_get_code();
03396
03397 }
03398
03399
03400