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
00857 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.cor_prescan"));
00858 check(prescan = cpl_parameter_get_bool(p));
00859
00860 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.anneal_niter"));
00861 check(anneal_niter = cpl_parameter_get_int(p));
00862
00863 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.method"));
00864 check(method = cpl_parameter_get_string(p));
00865
00866 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.first-anneal"));
00867 check(first_anneal = cpl_parameter_get_bool(p));
00868
00869 check(debug_level=xsh_parameters_debug_level_get("xsh_cfg_recover",parameters));
00870
00871
00872
00873
00874
00875
00876
00877 check( xsh_instrument_set_mode( instrument, XSH_MODE_SLIT));
00878
00879
00880 if(first_anneal) {
00881 check(first_anneal_frm=xsh_find_frame_with_tag(frameset,
00882 XSH_MOD_CFG_FAN,
00883 instrument));
00884 check(xsh_config_first_anneal=cpl_frame_duplicate(first_anneal_frm));
00885 } else {
00886 check(first_anneal_frm=xsh_find_frame_with_tag(frameset,
00887 XSH_MOD_CFG,
00888 instrument));
00889 check(xsh_config_first_anneal=cpl_frame_duplicate(first_anneal_frm));
00890 check(name=cpl_frame_get_filename(xsh_config_first_anneal));
00891 check(tab_xsh_config_first_anneal=cpl_table_load(name,1,0));
00892 check(plist=cpl_propertylist_load(name,0));
00893 check(tag=xsh_get_tag_from_arm(XSH_MOD_CFG_FAN,instrument));
00894 check(filename=cpl_sprintf("%s%s",tag,".fits"));
00895 check(cpl_table_save(tab_xsh_config_first_anneal,plist, NULL,filename,
00896 CPL_IO_DEFAULT));
00897 check(cpl_frame_set_filename(xsh_config_first_anneal,filename));
00898 check(cpl_frame_set_tag(xsh_config_first_anneal,tag));
00899 check(cpl_frame_set_group(xsh_config_first_anneal,CPL_FRAME_GROUP_PRODUCT));
00900 check(cpl_frame_set_level(xsh_config_first_anneal,CPL_FRAME_LEVEL_FINAL));
00901
00902 }
00903
00904 check(wave_list = xsh_find_frame_with_tag( calib,XSH_ARC_LINE_LIST,
00905 instrument));
00906
00907
00908 check(bpmap = xsh_find_master_bpmap( calib));
00909
00910
00911 if(strcmp(method,"safefit")!=0) {
00912 if((order_tab_centr = xsh_find_frame_with_tag(calib,XSH_ORDER_TAB_CENTR,
00913 instrument)) == NULL ) {
00914 xsh_msg_warning("Frame %s not provided",
00915 xsh_get_tag_from_arm(XSH_ORDER_TAB_CENTR,instrument));
00916 cpl_error_set(cpl_func,CPL_ERROR_DATA_NOT_FOUND);
00917 order_tab_centr=NULL;
00918 xsh_error_reset();
00919 if ((strcmp(method,"pm") == 0) || (strcmp(method,"peaks") == 0) ) {
00920 xsh_msg_error("If method is 'pm' or 'peaks' you must provide");
00921 xsh_msg_error("A valid frame taged as %s ",
00922 xsh_get_tag_from_arm(XSH_ORDER_TAB_CENTR,instrument));
00923 goto cleanup;
00924 }
00925 }
00926 }
00927
00928 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00929
00930 XSH_ASSURE_NOT_ILLEGAL(cpl_frameset_get_size(raws) == 1);
00931 if((master_bias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,
00932 instrument)) == NULL) {
00933 xsh_msg_warning("Frame %s not provided",XSH_MASTER_BIAS);
00934 xsh_error_reset();
00935 }
00936 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
00937 instrument)) == NULL) {
00938 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
00939 xsh_error_reset();
00940 }
00941 }
00942
00943
00944
00945
00946
00947 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
00948
00949 check(xsh_prepare( raws, bpmap, master_bias, XSH_FMTCHK, instrument,
00950 pre_overscan_corr));
00951 check(fmtchk = cpl_frameset_get_first(raws));
00952
00953 if(master_bias != NULL) {
00954 check(predict_rmbias = xsh_subtract_bias(fmtchk,master_bias,
00955 instrument,"FMTCHK_",0));
00956 } else {
00957 predict_rmbias = cpl_frame_duplicate(fmtchk);
00958 }
00959 if(master_dark != NULL) {
00960
00961 filename = xsh_stringcat_any( "FMTCHK_DARK_",
00962 xsh_instrument_arm_tostring( instrument ),
00963 ".fits", NULL ) ;
00964 check(predict_rmdark = xsh_subtract_dark(predict_rmbias, master_dark,
00965 filename, instrument));
00966 } else {
00967 predict_rmdark = cpl_frame_duplicate(predict_rmbias);
00968 }
00969 }
00970
00971 else{
00972 check( on = xsh_frameset_extract( raws,XSH_FMTCHK_NIR ) ) ;
00973 check( off = xsh_frameset_extract ( raws,XSH_FMTCHK_OFF ) ) ;
00974
00975 check(xsh_prepare(on,NULL, NULL, "ON", instrument,pre_overscan_corr));
00976
00977 check(xsh_prepare(off,bpmap, NULL, "OFF", instrument,pre_overscan_corr));
00978
00979 check(on_off = xsh_subtract_nir_on_off(on, off, instrument));
00980 check(predict_rmdark = cpl_frame_duplicate(cpl_frameset_get_first(on_off)));
00981 }
00982
00983
00984
00985 check(model_xy_gue=xsh_cfg_recover_gen_xyg_frame(wave_list,
00986 xsh_config_first_anneal,
00987 instrument,prescan));
00988
00989
00990
00991 check(xsh_cfg_recover_guess_tab_corr_by_user(parameters,&model_xy_gue));
00992
00993
00994
00995
00996 check(xsh_cfg_recover_guess_tab_corr_by_ordpos(instrument,order_tab_centr,
00997 &model_xy_gue));
00998
00999
01000
01001
01002
01003
01004
01005
01006 if (!first_anneal) {
01007 if(found_temp) {
01008 xsh_msg("update the prism temperatures before line matching");
01009 check(xsh_model_temperature_update_frame(&xsh_config_first_anneal,predict_rmdark,
01010 instrument,&found_temp));
01011 }
01012 }
01013
01014 check(xsh_cfg_recover_measure_line_xy(predict_rmdark,
01015 instrument,
01016 order_tab_centr,
01017 parameters,
01018 method,
01019 xsh_config_first_anneal,
01020 &model_xy_gue,
01021 debug_level));
01022
01023 xsh_msg(" REGDEBUG A generate %s", cpl_frame_get_filename(model_xy_gue));
01024
01025
01026
01027 if((xsh_config_input = xsh_find_frame_with_tag(frameset,XSH_MOD_CFG,
01028 instrument)) == NULL) {
01029 xsh_msg_error("Frame %s not found",
01030 xsh_stringcat_any( XSH_MOD_CFG,
01031 xsh_instrument_arm_tostring(instrument),
01032 NULL ) ) ;
01033 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
01034 goto cleanup;
01035 }
01036
01037
01038
01039
01040
01041 if(found_temp) {
01042 xsh_msg("update the prism temperatures before annealing");
01043
01044
01045 check(xsh_model_temperature_update_frame(&xsh_config_input,predict_rmdark,
01046 instrument,&found_temp));
01047 }
01048
01049
01050
01051
01052 check(measure_xy_pos_frm=cpl_frame_duplicate(model_xy_gue));
01053
01054
01055 check(xsh_cfg_recover_extend_xy_pos_frm(&measure_xy_pos_frm,instrument));
01056
01057 check(name=cpl_frame_get_filename(measure_xy_pos_frm));
01058
01059 nrows=cpl_table_get_nrow(cpl_table_load(name,1,0));
01060
01061 if ((xsh_instrument_get_arm(instrument)==XSH_ARM_UVB && nrows<70) ||
01062 (xsh_instrument_get_arm(instrument) == XSH_ARM_VIS && nrows<120) ||
01063 (xsh_instrument_get_arm(instrument) == XSH_ARM_NIR && nrows<80)) {
01064 xsh_msg_error("Not enough matches (%d). Try running with more iterations for first_anneal (higher niter)",cpl_table_get_nrow(cpl_table_load(name,1,0)));
01065 return -1;
01066 }
01067
01068
01069
01070 xsh_msg(" REGDEBUG B before annealing %s", cpl_frame_get_filename( measure_xy_pos_frm));
01071 check(xsh_config_last_anneal=xsh_model_pipe_anneal(xsh_config_input,
01072 measure_xy_pos_frm,
01073 anneal_niter,
01074 1.0,
01075 1,
01076 0));
01077
01078 check(xsh_cfg_recover_model_THE_create(xsh_config_last_anneal,
01079 instrument,
01080 wave_list,
01081 &model_THE1_frm,
01082 &model_THE9_frm));
01083
01084
01085
01086 check(xsh_add_product_table(xsh_config_last_anneal,frameset,parameters,
01087 RECIPE_ID,instrument));
01088 check(xsh_add_product_table(model_xy_gue,frameset,parameters, RECIPE_ID,
01089 instrument));
01090 check(xsh_add_product_table(model_THE1_frm,frameset,parameters, RECIPE_ID,
01091 instrument));
01092 check(xsh_add_product_table(model_THE9_frm,frameset,parameters, RECIPE_ID,
01093 instrument));
01094
01095 cleanup:
01096 xsh_free_table(&tab_xsh_config_first_anneal);
01097 xsh_free_table(&tab_xsh_config_input);
01098 xsh_end( RECIPE_ID, frameset, parameters );
01099
01100
01101 xsh_free_frame(&predict_rmbias );
01102 xsh_free_frame(&predict_rmdark );
01103
01104 xsh_free_frame(&model_xy_gue);
01105 xsh_free_frame(&measure_xy_pos_frm);
01106 xsh_free_frame(&xsh_config_last_anneal);
01107 xsh_free_frame(&model_THE1_frm);
01108 xsh_free_frame(&model_THE9_frm);
01109 xsh_free_frameset(&on);
01110 xsh_free_frameset(&off);
01111 xsh_free_frameset(&on_off);
01112
01113
01114
01115 if (cpl_error_get_code() != CPL_ERROR_NONE) {
01116 xsh_print_rec_status(0);
01117 return -1 ;
01118 } else {
01119 return 0 ;
01120 }
01121
01122 }
01123
01124
01139
01140
01141 static cpl_error_code
01142 xsh_cfg_recover_model_THE_create(cpl_frame* config_frame,
01143 xsh_instrument* instrument,
01144 cpl_frame* wave_list,
01145 cpl_frame** THE1_frm,
01146 cpl_frame** THE9_frm)
01147 {
01148 struct xs_3* p_xs_3_config=NULL;
01149 struct xs_3 xs_3_config;
01150 const char* line_list=NULL;
01151 const char* THE1_filename="model_THE1.fits";
01152 const char* THE9_filename="model_THE9.fits";
01153 const char* pro_catg=NULL;
01154
01155
01156 p_xs_3_config=&xs_3_config;
01157 if (xsh_model_config_load_best(config_frame, p_xs_3_config) !=
01158 CPL_ERROR_NONE) {
01159 xsh_msg_error("Cannot load %s as a config",
01160 cpl_frame_get_filename(config_frame)) ;
01161 return CPL_ERROR_DATA_NOT_FOUND ;
01162 }
01163
01164 check(line_list=cpl_frame_get_filename(wave_list));
01165 check(*THE1_frm=xsh_model_THE_create(p_xs_3_config,instrument,line_list,
01166 1,-1,THE1_filename));
01167 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_THEO_TAB_SING,instrument));
01168 check(xsh_frame_config(THE1_filename,pro_catg,CPL_FRAME_TYPE_TABLE,
01169 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
01170 THE1_frm));
01171
01172 check(*THE9_frm=xsh_model_THE_create(p_xs_3_config,instrument,line_list,
01173 9,-1,THE9_filename));
01174
01175 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_THEO_TAB_MULT,instrument));
01176 check(xsh_frame_config(THE9_filename,pro_catg,CPL_FRAME_TYPE_TABLE,
01177 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
01178 THE9_frm));
01179
01180
01181 cleanup:
01182 if(cpl_error_get_code() == CPL_ERROR_NONE) {
01183 return cpl_error_get_code();
01184 } else {
01185 xsh_print_rec_status(0);
01186 return cpl_error_get_code();
01187 }
01188 }
01189
01190
01200
01201
01202 static cpl_error_code
01203 xsh_cfg_recover_guess_tab_corr_by_user(cpl_parameterlist* parameters,
01204 cpl_frame** model_xy_gue)
01205 {
01206 const char* name=NULL;
01207 cpl_table* tab=NULL;
01208 double offx=0;
01209 double offy=0;
01210 cpl_parameter* p=NULL;
01211
01212 check(name=cpl_frame_get_filename(*model_xy_gue));
01213 check(tab=cpl_table_load(name,1,0));
01214
01215 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offx"));
01216 check(offx = cpl_parameter_get_double(p));
01217
01218 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offy"));
01219 check(offy = cpl_parameter_get_double(p));
01220
01221 check(cpl_table_add_scalar(tab,"XG",offx));
01222 check(cpl_table_add_scalar(tab,"YG",offy));
01223
01224 check(cpl_table_save(tab, NULL, NULL,name, CPL_IO_DEFAULT));
01225
01226 cleanup:
01227 xsh_free_table(&tab);
01228
01229
01230 return cpl_error_get_code();
01231 }
01232
01233
01234
01241
01242
01243 static cpl_error_code
01244 xsh_cfg_recover_extend_xy_pos_frm(cpl_frame** frm,xsh_instrument* inst)
01245 {
01246 const char * name=NULL;
01247 cpl_table* tbl=NULL;
01248 int nrows=0;
01249 cpl_propertylist* header = NULL;
01250 char name_o[80];
01251 const char* tag=XSH_GET_TAG_FROM_ARM(XSH_MEASURE_LINE_POS_XY,inst);
01252
01253 sprintf(name_o,"%s%s",tag,".fits");
01254
01255 check(name=cpl_frame_get_filename(*frm));
01256
01257
01258
01259 check(tbl=cpl_table_load(name,1,0));
01260 check ( header = cpl_propertylist_load( name, 0));
01261
01262 check(nrows=cpl_table_get_nrow(tbl));
01263 if(nrows==0) {
01264 xsh_msg_error("Table %s has 0 rows. Something wrong! Exit",name);
01265 goto cleanup;
01266 }
01267
01268
01269 check(cpl_table_name_column(tbl,"WAVELENGTH",
01270 XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH));
01271 check(cpl_table_name_column(tbl,"ABS_ORD",
01272 XSH_RESID_TAB_TABLE_COLNAME_ORDER));
01273 check(cpl_table_new_column(tbl,XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
01274 CPL_TYPE_DOUBLE));
01275 check(cpl_table_fill_column_window(tbl,
01276 XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
01277 0,nrows,0.));
01278 cpl_table_new_column(tbl,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,CPL_TYPE_INT);
01279 cpl_table_fill_column_window(tbl,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,0,
01280 nrows,4);
01281
01282
01283 check(cpl_table_duplicate_column(tbl,
01284 XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,
01285 tbl,"XC"));
01286 check( cpl_table_duplicate_column(tbl,
01287 XSH_RESID_TAB_TABLE_COLNAME_YTHPRE,
01288 tbl, "YC"));
01289
01290
01291 check(cpl_table_duplicate_column(tbl,
01292 XSH_RESID_TAB_TABLE_COLNAME_XTHCOR,
01293 tbl,"XC"));
01294 check( cpl_table_duplicate_column(tbl,
01295 XSH_RESID_TAB_TABLE_COLNAME_YTHCOR,
01296 tbl, "YC"));
01297
01298
01299 check(cpl_table_duplicate_column(tbl,
01300 XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,
01301 tbl,"XC"));
01302 check( cpl_table_duplicate_column(tbl,
01303 XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,
01304 tbl, "YC"));
01305
01306 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
01307 CPL_TYPE_DOUBLE);
01308 xsh_msg("create column %s", XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS);
01309
01310 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
01311 0, nrows, 0.);
01312 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
01313 CPL_TYPE_DOUBLE);
01314 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
01315 0, nrows, 0.);
01316 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,
01317 CPL_TYPE_DOUBLE);
01318 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,
01319 0, nrows, 0.);
01320 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL,
01321 CPL_TYPE_DOUBLE);
01322 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL,
01323 0, nrows, 0.);
01324
01325 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
01326 CPL_TYPE_DOUBLE);
01327 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
01328 0, nrows, 0.);
01329 cpl_table_new_column(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
01330 CPL_TYPE_DOUBLE);
01331 cpl_table_fill_column_window(tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
01332 0, nrows, 0.);
01333
01334 check( xsh_pfits_set_wavesoltype( header, XSH_WAVESOLTYPE_MODEL));
01335
01336 cpl_table_erase_invalid_rows(tbl);
01337 check( xsh_pfits_set_pcatg(header,tag));
01338 check(cpl_table_save(tbl, header, NULL,name_o, CPL_IO_DEFAULT));
01339
01340 cpl_frame_set_filename(*frm,name_o);
01341 cpl_frame_set_tag(*frm,tag);
01342
01343 cleanup:
01344 xsh_free_table(&tbl);
01345 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01346 xsh_print_rec_status(0);
01347 }
01348 return cpl_error_get_code() ;
01349 }
01350
01363
01364
01365 static cpl_error_code
01366 xsh_cfg_recover_measure_line_xy_fit(cpl_frame* raw_frm,
01367 cpl_parameterlist* parameters,
01368 cpl_frame** guess,
01369 int debug_level)
01370 {
01371
01372 int box_sy=0;
01373 cpl_parameter* p=NULL;
01374 int ngue=0;
01375 int i=0;
01376
01377 double* pxg=NULL;
01378 double* pyg=NULL;
01379 double* pxf=NULL;
01380 double* pyf=NULL;
01381
01382 int* ps=NULL;
01383 cpl_image* ima=NULL;
01384 const char* name=NULL;
01385
01386 double norm=0;
01387 double xcen=0;
01388 double ycen=0;
01389 double sig_x=0;
01390 double sig_y=0;
01391 double fwhm_x=0;
01392 double fwhm_y=0;
01393 const char* name_g="tab_xy_corr_gauss.fits";
01394 cpl_table* tab=NULL;
01395 cpl_table* tmp=NULL;
01396 int nfit=0;
01397
01398
01399
01400
01401
01402 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.gfit_box_sy"));
01403 check(box_sy = cpl_parameter_get_int(p));
01404
01405 check(name=cpl_frame_get_filename(raw_frm));
01406 check(ima=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
01407
01408 check(name=cpl_frame_get_filename(*guess));
01409 check(tab=cpl_table_load(name,1,0));
01410
01411 check(ngue=cpl_table_get_nrow(tab));
01412 check(cpl_table_new_column(tab,"SELECT",CPL_TYPE_INT));
01413 check(cpl_table_new_column(tab,"XF",CPL_TYPE_DOUBLE));
01414 check(cpl_table_new_column(tab,"YF",CPL_TYPE_DOUBLE));
01415
01416 check(cpl_table_fill_column_window(tab,"SELECT",0,ngue,-1));
01417 check(cpl_table_fill_column_window(tab,"XF",0,ngue,0));
01418 check(cpl_table_fill_column_window(tab,"YF",0,ngue,0));
01419
01420 check(pxg=cpl_table_get_data_double(tab,"XC"));
01421 check(pyg=cpl_table_get_data_double(tab,"YC"));
01422 check(pxf=cpl_table_get_data_double(tab,"XF"));
01423 check(pyf=cpl_table_get_data_double(tab,"YF"));
01424 check(ps=cpl_table_get_data_int(tab,"SELECT"));
01425
01426 xsh_msg("ngue=%d",ngue);
01427 for(i=0;i<ngue;i++) {
01428
01429
01430 if(cpl_image_fit_gaussian(ima,(int)pxg[i],(int)pyg[i],box_sy,
01431 &norm,
01432 &xcen,&ycen,
01433 &sig_x,&sig_y,
01434 &fwhm_x,&fwhm_y) == CPL_ERROR_NONE) {
01435 pxf[i]=xcen;
01436 pyf[i]=ycen;
01437 ps[i]=1;
01438
01439 } else {
01440
01441 cpl_error_reset();
01442 }
01443 }
01444 check(nfit=cpl_table_and_selected_int(tab,"SELECT",CPL_GREATER_THAN,-1));
01445
01446 xsh_msg("nfit=%d",nfit);
01447 check(tmp=cpl_table_extract_selected(tab));
01448
01449 check(cpl_table_erase_column(tmp,"XC"));
01450 check(cpl_table_erase_column(tmp,"YC"));
01451
01452 check(cpl_table_duplicate_column(tmp,"XC",tmp,"XF"));
01453 check(cpl_table_duplicate_column(tmp,"YC",tmp,"YF"));
01454
01455 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
01456 check(cpl_table_save(tmp, NULL, NULL, name_g, CPL_IO_DEFAULT));
01457 check(cpl_table_save(tmp, NULL, NULL, name, CPL_IO_DEFAULT));
01458 }
01459
01460 cleanup:
01461 xsh_free_table(&tab);
01462 xsh_free_table(&tmp);
01463 xsh_free_image(&ima);
01464
01465 if(nfit ==0 ) {
01466 xsh_msg_error("nfit = 0, something wrong in %s",cpl_func);
01467 return CPL_ERROR_UNSPECIFIED;
01468 } else {
01469 if( cpl_error_get_code() != CPL_ERROR_NONE) {
01470 xsh_print_rec_status(0);
01471 }
01472 return cpl_error_get_code() ;
01473 }
01474 }
01475
01487
01488
01489
01490 static cpl_error_code
01491 xsh_cfg_recover_measure_line_xy(cpl_frame* raw_frm,
01492 xsh_instrument* instr,
01493 cpl_frame* order_tab_centr,
01494 cpl_parameterlist* parameters,
01495 const char* method,
01496 cpl_frame* model_config,
01497 cpl_frame** guess,
01498 int debug_level)
01499 {
01500 cpl_image* ima=NULL;
01501 cpl_table* tab_xy_trace=NULL;
01502 cpl_table* tab_xy_guess=NULL;
01503 cpl_table* tab_xy_peaks=NULL;
01504 cpl_table* tab_xy_peaks_sel=NULL;
01505 cpl_table* tab_tmp=NULL;
01506 cpl_table* tab_tmp1=NULL;
01507 cpl_table* tab_tmp2=NULL;
01508 cpl_table* tab_xy_corr=NULL;
01509 cpl_image* ima_ext=NULL;
01510 cpl_image* ima_raw=NULL;
01511 double fct=2.;
01512 int slit=10;
01513 cpl_parameter* p=NULL;
01514 const char* name=NULL;
01515 const char* name_g=NULL;
01516 const char* name_t="tab_xy_peaks.fits";
01517 int thresh_x=0;
01518 int thresh_y=0;
01519
01520 cpl_matrix* mat_gue=NULL;
01521 cpl_matrix* mat_msr=NULL;
01522 double thresh_min=25;
01523 int use_data=0;
01524 int use_pattern=0;
01525 int ord_min=0;
01526 int ngue=0;
01527 int npck=0;
01528
01529 double* pxg=NULL;
01530 double* pyg=NULL;
01531 double yg_temp[20000];
01532 double xg_temp[20000];
01533 double* wave=NULL;
01534 int* ordg=NULL;
01535 int* psl=NULL;
01536
01537
01538 double cent, cent2, flux, cent2_cor, flux_cor, ex_cor;
01539 double prec, precfac,precmin;
01540 int lmax, rmax;
01541 int starty;
01542 int endy;
01543 int ii, jj, ll, min_iso, ord;
01544 int cnt=0;
01545
01546 int wmin;
01547 int morder_min;
01548 int w;
01549 int lines_dev;
01550
01551 cpl_vector* spectrum=NULL;
01552 cpl_vector* filtered=NULL;
01553 cpl_vector* spec_clean=NULL;
01554 cpl_vector* bright_lines=NULL ;
01555 int nbright_lines=0 ;
01556
01557 int kk=0;
01558 double* pxc=NULL;
01559 double* pyc=NULL;
01560 double* pwl=NULL;
01561 int nrow=0;
01562 int nfit=0;
01563 cpl_vector** trace=NULL;
01564 cpl_vector** extracted=NULL;
01565 int MAX_NO_ORD=16;
01566 struct xs_3 xs_3_config;
01567 struct xs_3* p_xs_3=NULL;
01568 int h=4;
01569 cpl_table* spec_form_tab=NULL;
01570 int starti=0;
01571 int absord;
01572 double tot=0.0;
01573 double total=0.0;
01574 double dx;
01575 double* detec=NULL;
01576 int size_x=0;
01577 int size_y=0;
01578 int size_y_use=0;
01579 int status=0;
01580 double x=0,y=0, offx=0, offy=0;
01581 cpl_frame* spec_form_frame=NULL ;
01582 int pix=0;
01583 int nb_match=0;
01584 double wave_use=0, zero_off=0,match_lim=0,startlam=0,endlam=0,order_median=0;
01585 p_xs_3=&xs_3_config;
01586
01587
01588 check(xsh_model_config_load_best(model_config, p_xs_3));
01589 xsh_msg( "Calling spectral format create" ) ;
01590
01591
01592 check(spec_form_frame=xsh_model_spectralformat_create(p_xs_3,"not_spec_form.fits"));
01593 check(cpl_frame_set_tag(spec_form_frame, xsh_get_tag_from_arm(XSH_ORDER_TAB_CENTR
01594 ,instr)));
01595
01596 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.method"));
01597 check(method = cpl_parameter_get_string(p));
01598
01599 if (strcmp(method,"gfit")==0) {
01600 check(xsh_cfg_recover_measure_line_xy_fit(raw_frm,parameters,guess,debug_level));
01601
01602 } else if (strcmp(method,"pbrfit")==0 || strcmp(method,"safefit")==0) {
01603
01604 if (strcmp(method,"safefit") == 0) {
01605
01606
01607 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offx"));
01608 check(offx = cpl_parameter_get_double(p));
01609 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.offy"));
01610 check(offy = cpl_parameter_get_double(p));
01611
01612
01613
01614 trace=xsh_model_locus(p_xs_3,instr,0.0);
01615
01616
01617 spec_form_tab=cpl_table_load(cpl_frame_get_filename(spec_form_frame),1,0);
01618
01619
01620 check(name=cpl_frame_get_filename(raw_frm));
01621 check(ima_raw=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
01622 check(size_y=cpl_image_get_size_y(ima_raw));
01623 check(size_x=cpl_image_get_size_x(ima_raw));
01624 xsh_msg("size_x=%d size_y=%d",size_x,size_y);
01625 check(detec=cpl_image_get_data_double(ima_raw));
01626
01627
01628 check(extracted=cpl_malloc(MAX_NO_ORD*sizeof(cpl_vector *)));
01629 for (ii=0; ii<MAX_NO_ORD; ii++) {
01630 extracted[ii] = cpl_vector_new(size_y) ;
01631 cpl_vector_fill(extracted[ii], 0.0) ;
01632 }
01633
01634
01635
01636 starti=0;
01637 while (cpl_table_get_int(spec_form_tab,"ORDER",starti,&status)<p_xs_3->morder_min) {
01638 starti++;
01639 }
01640 ii=starti;
01641
01642 for (ii=starti; ii<=starti+p_xs_3->morder_max-p_xs_3->morder_min;ii++) {
01643 absord=cpl_table_get_int(spec_form_tab,"ORDER",ii,&status);
01644 if (p_xs_3->arm!=2) {
01645 starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ii,&status));
01646 endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ii,&status));
01647 }
01648 else {
01649 starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ii,&status));
01650 endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ii,&status));
01651 }
01652
01653
01654
01655
01656
01657
01658
01659 if (offy<0.0) {
01660 starty-=(int)(offy);
01661 size_y_use=size_y;
01662 }
01663 else {
01664 endy-=(int)(offy);
01665 size_y_use=size_y-offy;
01666 }
01667 for (jj=starty;((jj<endy) && (jj<size_y_use));jj++) {
01668 tot=0.0;
01669
01670 dx=cpl_vector_get(trace[absord-p_xs_3->morder_min],jj+offy)-offx;
01671 for (kk=-h; kk<=h; kk++) {
01672 if (p_xs_3->arm==2) {
01673 pix=size_x*jj+((int)(dx+0.5)+kk);
01674 } else {
01675 pix=size_x*jj+(int)(dx+0.5)+kk;
01676 }
01677 if(pix<size_x*size_y) {
01678 tot+=detec[pix];
01679 }
01680 }
01681 check(cpl_vector_set(extracted[absord-p_xs_3->morder_min],jj,tot));
01682 }
01683 }
01684 }
01685
01686 else {
01687
01688 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.slit"));
01689 check(slit = cpl_parameter_get_int(p));
01690
01691 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_factor"));
01692 check(fct = cpl_parameter_get_double(p));
01693
01694 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_x"));
01695 check(thresh_x = cpl_parameter_get_int(p));
01696
01697 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_y"));
01698 check(thresh_y = cpl_parameter_get_int(p));
01699
01700 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.ima_thresh_min"));
01701 check(thresh_min = cpl_parameter_get_double(p));
01702
01703
01704
01705
01706 cknull(ima_ext=xsh_cfg_recover_linear_ext(raw_frm,order_tab_centr,instr,
01707 slit,thresh_min));
01708
01709 }
01710
01711
01712 check(name_g=cpl_frame_get_filename(*guess));
01713 check(tab_xy_guess=cpl_table_load(name_g,1,0));
01714 xsh_msg("guess tablename=%s",name_g);
01715
01716 morder_min=p_xs_3->morder_min;
01717 nrow=cpl_table_get_nrow(tab_xy_guess);
01718
01719 cpl_table_new_column(tab_xy_guess,"XF",CPL_TYPE_DOUBLE);
01720 cpl_table_new_column(tab_xy_guess,"YF",CPL_TYPE_DOUBLE);
01721 cpl_table_new_column(tab_xy_guess,"SELECT",CPL_TYPE_INT);
01722 cpl_table_fill_column_window(tab_xy_guess,"XF",0,nrow,-1);
01723 cpl_table_fill_column_window(tab_xy_guess,"YF",0,nrow,-1);
01724 cpl_table_fill_column_window(tab_xy_guess,"SELECT",0,nrow,-1);
01725
01726 pxc=cpl_table_get_data_double(tab_xy_guess,"XF");
01727 pyc=cpl_table_get_data_double(tab_xy_guess,"YF");
01728 pwl=cpl_table_get_data_double(tab_xy_guess,"WAVELENGTH");
01729
01730
01731
01732 for (kk=0;kk<nrow;kk++) {
01733 yg_temp[kk]=-1.0;
01734 }
01735 for (kk=0;kk<nrow;kk++) {
01736 xg_temp[kk]=-1.0;
01737 }
01738
01739 kk=0;
01740
01741 for(ord=0;ord<p_xs_3->morder_max-morder_min+1;ord++) {
01742 check(absord=cpl_table_get_int(spec_form_tab,"ORDER",ord+starti,&status));
01743 if (p_xs_3->arm!=2) {
01744 check(starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ord+starti,&status)));
01745 check(endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ord+starti,&status)));
01746 }
01747 else {
01748 check(starty=(int)(cpl_table_get_float(spec_form_tab,"DISP_MAX",ord+starti,&status)));
01749 check(endy=(int)(cpl_table_get_float(spec_form_tab,"DISP_MIN",ord+starti,&status)));
01750 }
01751 check(startlam=cpl_table_get_float(spec_form_tab,"LFSR",ord+starti,&status));
01752 check(endlam=cpl_table_get_float(spec_form_tab,"UFSR",ord+starti,&status));
01753
01754 if (strcmp(method,"safefit") == 0) {
01755 spectrum=extracted[ord];
01756
01757
01758 if ((filtered=cpl_vector_filter_median_create(spectrum, 200))==NULL){
01759 cpl_msg_error(__func__, "Cannot filter the spectrum") ;
01760 return CPL_ERROR_ILLEGAL_INPUT ;
01761 }
01762 spec_clean = cpl_vector_duplicate(spectrum);
01763 cpl_vector_subtract(spec_clean, filtered) ;
01764 cpl_vector_delete(filtered) ;
01765
01766 order_median=cpl_vector_get_median_const(spec_clean) ;
01767 }
01768 else {
01769
01770
01771 check(spectrum=cpl_vector_new_from_image_row(ima_ext,ord+1));
01772 spec_clean = cpl_vector_duplicate(spectrum);
01773 }
01774 check(bright_lines=xsh_model_refining_detect(spec_clean,3,5.0,0));
01775
01776
01777 check(pxg=cpl_table_get_data_double(tab_xy_guess,"XC"));
01778 check(pyg=cpl_table_get_data_double(tab_xy_guess,"YC"));
01779 check(wave=cpl_table_get_data_double(tab_xy_guess,"WAVELENGTH"));
01780 check(ordg=cpl_table_get_data_int(tab_xy_guess,"ABS_ORD"));
01781 check(psl=cpl_table_get_data_int(tab_xy_guess,"SELECT"));
01782
01783
01784
01785
01786
01787
01788
01789
01790 if(bright_lines != NULL) {
01791 check(nbright_lines=cpl_vector_get_size(bright_lines));
01792 for (ii=0; ii<nbright_lines;ii++) {
01793 cent=cpl_vector_get(bright_lines,ii)+0.5-1.0;
01794
01795
01796
01797
01798
01799
01800 lines_dev=0;
01801
01802 if (lines_dev==1) {
01803 precmin=0.25;
01804 precfac=25.0;
01805 wmin=3;
01806 min_iso=5;
01807 zero_off=-0.5;
01808 match_lim=0.2;
01809 }
01810 else if (lines_dev==2) {
01811 precmin=0.5;
01812 precfac=25.0;
01813 wmin=3;
01814 min_iso=5;
01815 zero_off=-0.5;
01816 match_lim=1.0;
01817 }
01818 else if (lines_dev==3) {
01819 precmin=0.25;
01820 precfac=25.0;
01821 wmin=3;
01822 min_iso=5;
01823 zero_off=-0.5;
01824 match_lim=0.25;
01825 }
01826 else if (lines_dev==4) {
01827 precmin=0.5;
01828 precfac=25.0;
01829 wmin=3;
01830 min_iso=5;
01831 zero_off=-0.5;
01832 match_lim=1.0;
01833 }
01834 else if (lines_dev==5) {
01835 precmin=0.4;
01836 precfac=25.0;
01837 wmin=3;
01838 min_iso=5;
01839 zero_off=-0.5;
01840 match_lim=0.5;
01841 }
01842 else if (lines_dev==6) {
01843 precmin=0.5;
01844 precfac=15.0;
01845 wmin=3;
01846 min_iso=80;
01847 zero_off=-0.5;
01848 match_lim=1.0;
01849 }
01850 else {
01851 precmin=0.5;
01852 precfac=20.0;
01853 wmin=4;
01854 min_iso=4;
01855 match_lim=2.5;
01856 zero_off=0.0;
01857 }
01858 rmax=0;
01859 w=wmin;
01860 while (w<200) {
01861
01862 prec=(float)(w)/precfac;
01863 if (prec<precmin) {
01864 prec=precmin;
01865 }
01866 flux=0.0;
01867 cent2=0.0;
01868 flux_cor=0.0;
01869 cent2_cor=0.0;
01870 for (jj=(int)(cent)-wmin;jj<=(int)(cent)+w;jj++) {
01871
01872 if (jj>0 && jj<endy) {
01873 ex_cor=cpl_vector_get(spec_clean,jj);
01874 if (ex_cor>0.0) {
01875 flux+=ex_cor;
01876 cent2+=ex_cor*((float)(jj)+0.5-cent);
01877 }
01878
01879 }
01880 }
01881 cent2/=flux;
01882
01883 cent2+=cent;
01884 if (fabs(cent2-cent)<prec) {
01885 rmax=w;
01886
01887 }
01888 else {
01889 w=1000;
01890 }
01891 w+=1;
01892 }
01893 lmax=0;
01894 w=wmin;
01895 while (w<200) {
01896
01897 prec=(float)(w)/precfac;
01898 if (prec<precmin) {
01899 prec=precmin;
01900 }
01901 flux=0.0;
01902 cent2=0.0;
01903 flux_cor=0.0;
01904 cent2_cor=0.0;
01905 for (jj=(int)(cent)-w;jj<=(int)(cent)+wmin;jj++) {
01906 if (jj>0 && jj<endy) {
01907 ex_cor=cpl_vector_get(spec_clean,jj);
01908 if (ex_cor>0.0) {
01909 flux+=ex_cor;
01910 cent2+=ex_cor*((float)(jj)+0.5-cent);
01911 }
01912 }
01913 }
01914 cent2/=flux;
01915
01916 cent2+=cent;
01917 if (fabs(cent2-cent)<prec) {
01918 lmax=w;
01919
01920 }
01921 else {
01922 w=1000;
01923 }
01924 w+=1;
01925 }
01926
01927
01928 if (lmax>=min_iso && rmax>=min_iso) {
01929
01930
01931 if (strcmp(method,"safefit") == 0) {
01932 nb_match=0;
01933 for (jj=0; jj< cpl_table_get_nrow(tab_xy_guess);jj++) {
01934 if (ord==ordg[jj]-morder_min) {
01935
01936 check(xsh_model_get_xy(p_xs_3,instr,wave[jj],absord,0.0,&x,&y));
01937 x-=offx;
01938 y-=offy;
01939 if (lines_dev>0 && fabs(y-cent+zero_off)<match_lim) {
01940 nb_match++;
01941
01942 wave_use=wave[jj];
01943 }
01944 if (abs(y-cent)<(float)(min_iso)/2.0) {
01945
01946
01947 yg_temp[jj]=cent;
01948 psl[jj]=1;
01949 total=0.0;
01950 cent2=0.0;
01951
01952
01953 for (ll=-h;ll<=h;ll++) {
01954 tot=0.0;
01955 for (kk=-1;kk<=1;kk++) {
01956
01957
01958
01959
01960 pix=size_x*((int)(cent+0.5)+kk)+(int)(x+0.5)+ll;
01961 tot+=detec[pix];
01962 pix=size_x*((int)(cent+0.5)+kk)+(int)(x+0.5)+ll+15;
01963 tot-=detec[pix];
01964
01965 }
01966 total+=tot;
01967 cent2+=tot*((float)(ll)+0.5);
01968 }
01969 if (fabs(cent2/total)<3.0) {
01970 cent2=x+(cent2/total);
01971 }
01972 else {
01973 cent2=x;
01974 }
01975
01976
01977 xg_temp[jj]=cent2;
01978 }
01979 }
01980 }
01981 if (nb_match==1 && lines_dev>0) {
01982 if (wave_use>startlam && wave_use<endlam) {
01983 cnt++;
01984
01985 printf("%lf 100.0 %d tot%d\n",wave_use, min_iso, cnt);
01986 }
01987 }
01988 }
01989 else{
01990 for (jj=0; jj< cpl_table_get_nrow(tab_xy_guess);jj++) {
01991 if (ord==ordg[jj]-morder_min) {
01992
01993 if (abs(pyg[jj]-cent)<(float)(min_iso/2)) {
01994
01995
01996
01997 yg_temp[jj]=cent;
01998 psl[jj]=1;
01999 }
02000 }
02001 }
02002 }
02003 }
02004 }
02005 }
02006 else {
02007 xsh_msg_warning("bright_lines is NULL, order %d\n",ord);
02008 }
02009 }
02010
02011 for (jj=0; jj< cpl_table_get_nrow(tab_xy_guess);jj++) {
02012 if (yg_temp[jj]>0.0) {
02013 pyg[jj]=yg_temp[jj];
02014 pxg[jj]=xg_temp[jj];
02015 }
02016 pyc[jj]=pyg[jj];
02017 pxc[jj]=pxg[jj];
02018 }
02019
02020
02021 check(nfit=cpl_table_and_selected_int(tab_xy_guess,"SELECT",CPL_GREATER_THAN,-1));
02022 check(tab_tmp=cpl_table_extract_selected(tab_xy_guess));
02023 xsh_free_table(&tab_xy_guess);
02024 check(cpl_table_erase_column(tab_tmp,"XC"));
02025 check(cpl_table_erase_column(tab_tmp,"YC"));
02026
02027 check(cpl_table_duplicate_column(tab_tmp,"XC",tab_tmp,"XF"));
02028 check(cpl_table_duplicate_column(tab_tmp,"YC",tab_tmp,"YF"));
02029
02030 check(cpl_table_save(tab_tmp,NULL,NULL,name_g,CPL_IO_DEFAULT));
02031 xsh_free_table(&tab_tmp);
02032
02033 } else {
02034
02035
02036 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.slit"));
02037 check(slit = cpl_parameter_get_int(p));
02038
02039 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_factor"));
02040 check(fct = cpl_parameter_get_double(p));
02041
02042 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_x"));
02043 check(thresh_x = cpl_parameter_get_int(p));
02044
02045 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_match_y"));
02046 check(thresh_y = cpl_parameter_get_int(p));
02047
02048 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.ima_thresh_min"));
02049 check(thresh_min = cpl_parameter_get_double(p));
02050
02051
02052
02053
02054 cknull(ima_ext=xsh_cfg_recover_linear_ext(raw_frm,order_tab_centr,instr,
02055 slit,thresh_min));
02056
02057
02058
02059
02060 cknull(tab_xy_peaks=xsh_cfg_recover_measure_tab_xy_peaks(ima_ext,parameters));
02061 check(cpl_table_duplicate_column(tab_xy_peaks,"ABS_ORD",
02062 tab_xy_peaks,"REL_ORD"));
02063
02064
02065
02066 check(name_g=cpl_frame_get_filename(*guess));
02067 check(tab_xy_guess=cpl_table_load(name_g,1,0));
02068 xsh_msg("guess table name=%s",name_g);
02069
02070 check(name=cpl_frame_get_filename(order_tab_centr));
02071 check(tab_xy_trace=cpl_table_load(name,1,0));
02072 xsh_msg("Trace table name=%s",name);
02073
02074 check(ord_min=cpl_table_get_column_min(tab_xy_trace,"ABSORDER"));
02075 check(cpl_table_add_scalar(tab_xy_peaks,"ABS_ORD",ord_min));
02076
02077 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02078 check(cpl_table_save(tab_xy_peaks,NULL,NULL,name_t,CPL_IO_DEFAULT));
02079 }
02080
02081 check(xsh_cfg_recover_add_peaks_xpos(order_tab_centr,instr,&tab_xy_peaks));
02082
02083 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02084 check(cpl_table_save(tab_xy_peaks,NULL,NULL,name_t,CPL_IO_DEFAULT));
02085 }
02086
02087 if(fct>0) {
02088
02089 xsh_msg("Peaks selection fct=%g",fct);
02090 cknull(tab_xy_peaks_sel=xsh_cfg_recover_select_peaks(tab_xy_guess,
02091 tab_xy_peaks,fct));
02092 } else {
02093 xsh_msg("No peaks selection");
02094 tab_xy_peaks_sel=cpl_table_duplicate(tab_xy_peaks);
02095 }
02096
02097 if(debug_level> XSH_DEBUG_LEVEL_NONE) {
02098 check(cpl_table_save(tab_xy_peaks_sel, NULL, NULL,
02099 "tab_xy_peaks_sel.fits",
02100 CPL_IO_DEFAULT));
02101 }
02102
02103 if ((strcmp(method,"pm") == 0) ||
02104 (strcmp(method,"manual") ==0)) {
02105 check(xsh_cfg_recover_prepare_pm_set(tab_xy_guess,tab_xy_peaks_sel,
02106 parameters,
02107 &mat_gue,&mat_msr,&use_pattern,
02108 &use_data));
02109
02110
02111 check(xsh_cfg_recover_pattern_match(parameters,mat_gue,mat_msr,
02112 use_pattern,use_data,debug_level));
02113
02114
02115 } else {
02116
02117
02118 xsh_msg("Remove blended lines");
02119
02120
02121
02122
02123 ngue=cpl_table_get_nrow(tab_xy_guess);
02124 npck=cpl_table_get_nrow(tab_xy_peaks_sel);
02125 if(ngue<=npck) {
02126 cknull(tab_xy_corr=xsh_cfg_recover_remove_blends(tab_xy_guess,
02127 tab_xy_peaks_sel,
02128 thresh_x,
02129 thresh_y));
02130
02131 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02132 check(cpl_table_save(tab_xy_corr, NULL, NULL, "tab_xy_corr.fits",
02133 CPL_IO_DEFAULT));
02134
02135 }
02136 } else {
02137 xsh_msg_error("Number og guess lines more than found peaks!");
02138 xsh_msg_error("Either increase factor ");
02139 xsh_msg_error("or choose a reference line table with less entries");
02140 return CPL_ERROR_ILLEGAL_INPUT ;
02141 }
02142 }
02143 }
02144
02145
02146 cleanup:
02147 if(trace!=NULL) {
02148 for (ii=0 ; ii<MAX_NO_ORD; ii++) {
02149 xsh_free_vector(&(trace[ii]));
02150 }
02151 cpl_free(trace) ;
02152 trace=NULL;
02153 }
02154
02155 if (extracted!=NULL) {
02156 for (ii=0 ; ii<MAX_NO_ORD; ii++) {
02157 xsh_free_vector(&(extracted[ii]));
02158 }
02159 cpl_free(extracted);
02160 extracted=NULL;
02161 }
02162
02163 xsh_free_frame(&spec_form_frame);
02164 xsh_free_table(&spec_form_tab);
02165 xsh_free_image(&ima);
02166 xsh_free_image(&ima_ext);
02167 xsh_free_table(&tab_xy_guess);
02168 xsh_free_table(&tab_xy_peaks);
02169 xsh_free_table(&tab_xy_peaks_sel);
02170 xsh_free_table(&tab_tmp);
02171 xsh_free_table(&tab_tmp1);
02172 xsh_free_table(&tab_tmp2);
02173 xsh_free_table(&tab_xy_corr);
02174 cpl_vector_delete(spec_clean) ;
02175 if(cpl_error_get_code() != CPL_ERROR_NONE) {
02176 xsh_print_rec_status(0);
02177 }
02178 return cpl_error_get_code() ;
02179
02180 }
02181
02182
02183
02195
02196
02197 static cpl_error_code
02198 xsh_cfg_recover_prepare_pm_set(cpl_table* tab_pat,
02199 cpl_table* tab_dat,
02200 cpl_parameterlist* parameters,
02201 cpl_matrix** mat_pat,
02202 cpl_matrix** mat_dat,
02203 int* use_pat,
02204 int* use_dat)
02205
02206 {
02207
02208 int npat=0;
02209 int ndat=0;
02210 int i=0;
02211
02212 double* pxd=NULL;
02213 double* pyd=NULL;
02214 double* pxg=NULL;
02215 double* pyg=NULL;
02216
02217 int* pog=NULL;
02218 int* pod=NULL;
02219 int* ps=NULL;
02220
02221 int ord_min=0;
02222 int ord_max=0;
02223 int ord_min_dat=0;
02224 int ord_max_dat=0;
02225
02226 int ord=0;
02227
02228 int ord_sel=5;
02229 cpl_parameter* p=NULL;
02230 cpl_table* pat=NULL;
02231 cpl_table* dat=NULL;
02232 cpl_table* tmp=NULL;
02233
02234 int j=0;
02235 double rad=0;
02236 double xmin=0;
02237 double xmax=0;
02238 double ymin=0;
02239 double ymax=0;
02240
02241 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_ord_sel"));
02242 check(ord_sel = cpl_parameter_get_int(p));
02243
02244 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_radius"));
02245 check(rad = cpl_parameter_get_double(p));
02246
02247
02248 check(npat=cpl_table_get_nrow(tab_pat));
02249 check(ndat=cpl_table_get_nrow(tab_dat));
02250
02251
02252 check(xsh_sort_table_1(tab_pat,"ABS_ORD",CPL_TRUE));
02253 check(xsh_sort_table_1(tab_dat,"ABS_ORD",CPL_TRUE));
02254
02255 check(pat=cpl_table_duplicate(tab_pat));
02256 check(cpl_table_new_column(pat,"SEL",CPL_TYPE_INT));
02257 check(cpl_table_fill_column_window(pat,"SEL",0,npat,-1));
02258
02259
02260 check(ps=cpl_table_get_data_int(pat,"SEL"));
02261 check(pog=cpl_table_get_data_int(pat,"ABS_ORD"));
02262
02263 check(ord_min=cpl_table_get_column_min(pat,"ABS_ORD"));
02264 check(ord_max=cpl_table_get_column_max(pat,"ABS_ORD"));
02265
02266
02267 check(ord_min_dat=cpl_table_get_column_min(tab_dat,"ABS_ORD"));
02268 check(ord_max_dat=cpl_table_get_column_max(tab_dat,"ABS_ORD"));
02269
02270 xsh_msg("pat min=%d max=%d",ord_min,ord_max);
02271 xsh_msg("dat min=%d max=%d",ord_min_dat,ord_max_dat);
02272
02273
02274 *use_pat=0;
02275
02276
02277 for(ord=ord_min;ord<=ord_max;ord++) {
02278 for(i=0;i<npat;i++) {
02279 if((pog[i]==ord) && (j< ord_sel)) {
02280 ps[i]=1;
02281 j++;
02282 }
02283 }
02284 j=0;
02285 }
02286 check(*use_pat=cpl_table_and_selected_int(pat,"SEL",CPL_GREATER_THAN,0));
02287 check(tmp=cpl_table_extract_selected(pat));
02288
02289 check(cpl_table_save(pat,NULL,NULL,"pat.fits",CPL_IO_DEFAULT));
02290
02291
02292 check(dat=cpl_table_duplicate(tab_dat));
02293 check(cpl_table_new_column(dat,"SEL",CPL_TYPE_INT));
02294 check(cpl_table_fill_column_window(dat,"SEL",0,ndat,-1));
02295 check(ps=cpl_table_get_data_int(dat,"SEL"));
02296
02297 check(pod=cpl_table_get_data_int(dat,"ABS_ORD"));
02298 check(pxd=cpl_table_get_data_double(dat,"XP"));
02299 check(pyd=cpl_table_get_data_double(dat,"YP"));
02300
02301 check(pog=cpl_table_get_data_int(tmp,"ABS_ORD"));
02302 check(pxg=cpl_table_get_data_double(tmp,"XC"));
02303 check(pyg=cpl_table_get_data_double(tmp,"YC"));
02304
02305
02306 for(i=0;i<*use_pat;i++) {
02307 xmin=pxg[i]-rad;
02308 xmax=pxg[i]+rad;
02309 ymin=pyg[i]-rad;
02310 ymax=pyg[i]+rad;
02311 for(j=0;j<ndat;j++) {
02312 if( pog[i] == pod[j] ) {
02313 if(pxd[j] >= xmin && pxd[j] <= xmax) {
02314 if(pyd[j] >= ymin && pyd[j] <= ymax ) {
02315 ps[j]=1;
02316 }
02317 }
02318 }
02319 }
02320 }
02321 check(cpl_table_save(dat,NULL,NULL,"dat.fits",CPL_IO_DEFAULT));
02322 check(*use_dat=cpl_table_and_selected_int(dat,"SEL",CPL_GREATER_THAN,0));
02323
02324 xsh_msg("use_pattern=%d use_data=%d",*use_pat,*use_dat);
02325 if(*use_pat > *use_dat) {
02326 xsh_msg_error("You try to find a matching pattern searching ");
02327 xsh_msg_error("more points than available data. Try to increase ");
02328 xsh_msg_error("pm_radius and or peaks_factor ");
02329 xsh_msg_error("and/or decrease pm_ord_sel parameter values");
02330 goto cleanup;
02331 }
02332
02333 check(xsh_sort_table_1(dat,"SEL",CPL_TRUE));
02334 check(xsh_sort_table_1(pat,"SEL",CPL_TRUE));
02335 check(*mat_pat=cpl_matrix_new(2,npat));
02336 check(*mat_dat=cpl_matrix_new(2,ndat));
02337
02338 check(pxg=cpl_table_get_data_double(pat,"XC"));
02339 check(pyg=cpl_table_get_data_double(pat,"YC"));
02340 for(i=0;i<npat;i++) {
02341
02342 check(cpl_matrix_set(*mat_pat,0,i,pxg[i]));
02343 check(cpl_matrix_set(*mat_pat,1,i,pyg[i]));
02344
02345 }
02346
02347 check(pxd=cpl_table_get_data_double(dat,"XP"));
02348 check(pyd=cpl_table_get_data_double(dat,"YP"));
02349 for(i=0;i<ndat;i++) {
02350
02351 check(cpl_matrix_set(*mat_dat,0,i,pxd[i]));
02352 check(cpl_matrix_set(*mat_dat,1,i,pyd[i]));
02353
02354 }
02355
02356
02357 cleanup:
02358 xsh_free_table(&pat);
02359 xsh_free_table(&dat);
02360 xsh_free_table(&tmp);
02361
02362
02363 return cpl_error_get_code() ;
02364
02365 }
02366
02367
02368
02381
02382
02383
02384 static cpl_error_code
02385 xsh_cfg_recover_pattern_match(cpl_parameterlist* parameters,
02386 cpl_matrix* mat_gue,
02387 cpl_matrix* mat_dat,
02388 int use_pattern,
02389 int use_data,
02390 int debug_level)
02391 {
02392 cpl_array *matches = NULL;
02393
02394 double err_data=1;
02395 double err_pattern=0;
02396 double tolerance=0.1;
02397 double radius=1;
02398 cpl_matrix* mdata=NULL;
02399 cpl_matrix* mpattern=NULL;
02400 cpl_parameter* p=NULL;
02401 cpl_table* pm_tbl=NULL;
02402 cpl_table* dat_tbl=NULL;
02403 cpl_table*gue_tbl=NULL;
02404
02405 int nmatch=0;
02406 double* pcatx=NULL;
02407 double* pcaty=NULL;
02408 double* ppatx=NULL;
02409 double* ppaty=NULL;
02410 int i=0;
02411 int ndat=0;
02412 int ngue=0;
02413 double lin_scale=1.0;
02414 double lin_angle=0.0;
02415
02416
02417 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_tolerance"));
02418 check(tolerance = cpl_parameter_get_double(p));
02419
02420 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.pm_radius"));
02421 check(radius = cpl_parameter_get_double(p));
02422
02423
02424
02425 ndat=cpl_matrix_get_ncol(mat_dat);
02426 ngue=cpl_matrix_get_ncol(mat_gue);
02427
02428 check(dat_tbl=cpl_table_new(ndat));
02429 check(gue_tbl=cpl_table_new(ngue));
02430
02431 check(cpl_table_new_column(dat_tbl,"XD",CPL_TYPE_DOUBLE));
02432 check(cpl_table_new_column(dat_tbl,"YD",CPL_TYPE_DOUBLE));
02433 check(cpl_table_fill_column_window(dat_tbl,"XD",0,ndat,0));
02434 check(cpl_table_fill_column_window(dat_tbl,"YD",0,ndat,0));
02435 check(pcatx=cpl_table_get_data_double(dat_tbl,"XD"));
02436 check(pcaty=cpl_table_get_data_double(dat_tbl,"YD"));
02437
02438
02439
02440 for(i=0;i<ndat;i++) {
02441
02442 check(pcatx[i]=cpl_matrix_get(mat_dat,0,i));
02443 check(pcaty[i]=cpl_matrix_get(mat_dat,1,i));
02444
02445
02446 }
02447
02448 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02449 check(cpl_table_save(dat_tbl,NULL,NULL,"pm_dat.fits",CPL_IO_DEFAULT));
02450 }
02451
02452 check(cpl_table_new_column(gue_tbl,"XG",CPL_TYPE_DOUBLE));
02453 check(cpl_table_new_column(gue_tbl,"YG",CPL_TYPE_DOUBLE));
02454 check(cpl_table_fill_column_window(gue_tbl,"XG",0,ngue,0));
02455 check(cpl_table_fill_column_window(gue_tbl,"YG",0,ngue,0));
02456 check(ppatx=cpl_table_get_data_double(gue_tbl,"XG"));
02457 check(ppaty=cpl_table_get_data_double(gue_tbl,"YG"));
02458
02459
02460 for(i=0;i<ngue;i++) {
02461
02462 check(ppatx[i]=cpl_matrix_get(mat_gue,0,i));
02463 check(ppaty[i]=cpl_matrix_get(mat_gue,1,i));
02464
02465
02466 }
02467
02468 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02469 check(cpl_table_save(gue_tbl,NULL,NULL,"pm_gue.fits",CPL_IO_DEFAULT));
02470 }
02471 check(matches = cpl_ppm_match_points(mat_dat,use_data,err_data,
02472 mat_gue,use_pattern,err_pattern,
02473 tolerance,radius,&mdata, &mpattern,
02474 &lin_scale,&lin_angle));
02475
02476 check(nmatch=cpl_matrix_get_ncol(mpattern));
02477 xsh_msg("%d points matched linear scale factor=%f linear angle factor %f",
02478 nmatch,lin_scale,lin_angle);
02479
02480 check(pm_tbl=cpl_table_new(nmatch));
02481 check(cpl_table_new_column(pm_tbl,"PM_CAT_X",CPL_TYPE_DOUBLE));
02482 check(cpl_table_new_column(pm_tbl,"PM_CAT_Y",CPL_TYPE_DOUBLE));
02483 check(cpl_table_new_column(pm_tbl,"PM_PAT_X",CPL_TYPE_DOUBLE));
02484 check(cpl_table_new_column(pm_tbl,"PM_PAT_Y",CPL_TYPE_DOUBLE));
02485
02486 check(cpl_table_fill_column_window(pm_tbl,"PM_CAT_X",0,nmatch,0));
02487 check(cpl_table_fill_column_window(pm_tbl,"PM_CAT_Y",0,nmatch,0));
02488 check(cpl_table_fill_column_window(pm_tbl,"PM_PAT_X",0,nmatch,0));
02489 check(cpl_table_fill_column_window(pm_tbl,"PM_PAT_Y",0,nmatch,0));
02490
02491
02492 check(ppatx=cpl_table_get_data_double(pm_tbl,"PM_CAT_X"));
02493 check(ppaty=cpl_table_get_data_double(pm_tbl,"PM_CAT_Y"));
02494
02495 check(pcatx=cpl_table_get_data_double(pm_tbl,"PM_PAT_X"));
02496 check(pcaty=cpl_table_get_data_double(pm_tbl,"PM_PAT_Y"));
02497
02498 for(i=0;i<nmatch;i++) {
02499
02500 check(pcatx[i]=cpl_matrix_get(mdata,0,i));
02501 check(pcaty[i]=cpl_matrix_get(mdata,1,i));
02502
02503 check(ppatx[i]=cpl_matrix_get(mpattern,0,i));
02504 check(ppaty[i]=cpl_matrix_get(mpattern,1,i));
02505
02506
02507 }
02508
02509 if(debug_level > XSH_DEBUG_LEVEL_NONE) {
02510 check(cpl_table_save(pm_tbl,NULL,NULL,
02511 "pattern_match.fits",CPL_IO_DEFAULT));
02512 }
02513 xsh_free_table(&gue_tbl);
02514 xsh_free_table(&dat_tbl);
02515 xsh_free_table(&pm_tbl);
02516 xsh_free_array(&matches);
02517 xsh_free_matrix(&mpattern);
02518 xsh_free_matrix(&mdata);
02519
02520 cleanup:
02521 if(cpl_error_get_code() != CPL_ERROR_NONE) {
02522 xsh_msg("Try to increase pm_radius and/or peak_factor");
02523 }
02524 return cpl_error_get_code() ;
02525
02526 }
02527
02528
02529
02530
02539
02540 static cpl_error_code
02541 xsh_cfg_recover_add_peaks_xpos(cpl_frame* order_tab_centr,
02542 xsh_instrument* instr,
02543 cpl_table** tab_xy_peaks)
02544 {
02545
02546 xsh_order_list* order_list = NULL;
02547 int size=0;
02548 double* px=NULL;
02549 double* py=NULL;
02550 int* po=NULL;
02551 int ord_min=0;
02552 int ord_rel=0;
02553
02554
02555 int i=0;
02556
02557 check(size=cpl_table_get_nrow(*tab_xy_peaks));
02558 check(cpl_table_new_column(*tab_xy_peaks,"XP",CPL_TYPE_DOUBLE));
02559 check(cpl_table_fill_column_window(*tab_xy_peaks,"XP",0,size,-1));
02560
02561 check(px=cpl_table_get_data_double(*tab_xy_peaks,"XP"));
02562 check(py=cpl_table_get_data_double(*tab_xy_peaks,"YP"));
02563 check(po=cpl_table_get_data_int(*tab_xy_peaks,"ABS_ORD"));
02564 ord_min=cpl_table_get_column_min(*tab_xy_peaks,"ABS_ORD");
02565
02566 check(order_list = xsh_order_list_load(order_tab_centr,instr));
02567
02568
02569 for(i=0;i<size;i++) {
02570
02571 check( ord_rel = xsh_order_list_get_order( order_list, po[i] ) ) ;
02572
02573 ord_rel=po[i]-ord_min;
02574
02575
02576
02577 if(cpl_polynomial_get_dimension(order_list->list[ord_rel].cenpoly) <
02578 XSH_ORDPOS_POL_DIM_MAX) {
02579
02580 check( px[i] = cpl_polynomial_eval_1d(
02581 order_list->list[ord_rel].cenpoly,py[i],NULL));
02582 }
02583
02584
02585
02586 }
02587
02588 cleanup:
02589 xsh_order_list_free(&order_list);
02590
02591 return cpl_error_get_code();
02592
02593
02594 }
02595
02596
02607
02608
02609 static cpl_table*
02610 xsh_cfg_recover_remove_blends(cpl_table* tab_xy_guess,
02611 cpl_table* tab_xy_peaks_sel,
02612 const int thresh_x,
02613 const int thresh_y)
02614 {
02615
02616 double* pxg=NULL;
02617 double* pyg=NULL;
02618 double* pyt=NULL;
02619 double* pyp=NULL;
02620
02621 double* pxt=NULL;
02622 double* pxp=NULL;
02623 int* pwp=NULL;
02624 int* pap=NULL;
02625 int* prp=NULL;
02626
02627
02628
02629 double* pxr=NULL;
02630 double* pyr=NULL;
02631 int* pwr=NULL;
02632 int* par=NULL;
02633 int* prr=NULL;
02634
02635 double x=0;
02636 double y=0;
02637 int r=0;
02638 int a=0;
02639 int w=0;
02640
02641
02642 int ord=0;
02643 int ord_min=0;
02644 int ord_max=0;
02645 int nord=0;
02646 int npks=0;
02647 cpl_table* tab_tmp=NULL;
02648 cpl_table* tab_tmp1=NULL;
02649 cpl_table* tab_tmp2=NULL;
02650
02651 cpl_table* result=NULL;
02652 int found=0;
02653 int i=0;
02654 int j=0;
02655
02656 int k=0;
02657 int nrows=0;
02658
02659 pxg=cpl_table_get_data_double(tab_xy_guess,"XC");
02660 pyg=cpl_table_get_data_double(tab_xy_guess,"YC");
02661 ord_min=cpl_table_get_column_min(tab_xy_guess,"ABS_ORD");
02662 ord_max=cpl_table_get_column_max(tab_xy_guess,"ABS_ORD");
02663
02664 check(tab_tmp2=cpl_table_duplicate(tab_xy_peaks_sel));
02665 check(nrows=cpl_table_get_nrow(tab_tmp2));
02666 check(cpl_table_fill_column_window(tab_tmp2,"YP",0,nrows,-1));
02667 check(cpl_table_fill_column_window(tab_tmp2,"XP",0,nrows,-1));
02668 check(cpl_table_fill_column_window(tab_tmp2,"REL_ORD",0,nrows,-1));
02669 check(cpl_table_fill_column_window(tab_tmp2,"ABS_ORD",0,nrows,-1));
02670 check(cpl_table_fill_column_window(tab_tmp2,"WEIGHT",0,nrows,-1));
02671
02672 check(pxr=cpl_table_get_data_double(tab_tmp2,"XP"));
02673 check(pyr=cpl_table_get_data_double(tab_tmp2,"YP"));
02674 check(prr=cpl_table_get_data_int(tab_tmp2,"REL_ORD"));
02675 check(pwr=cpl_table_get_data_int(tab_tmp2,"WEIGHT"));
02676 check(par=cpl_table_get_data_int(tab_tmp2,"ABS_ORD"));
02677
02678 for(ord=ord_min;ord<=ord_max;ord++) {
02679 check(nord=cpl_table_and_selected_int(tab_xy_guess,"ABS_ORD",
02680 CPL_EQUAL_TO,ord));
02681 check(npks=cpl_table_and_selected_int(tab_xy_peaks_sel,"ABS_ORD",
02682 CPL_EQUAL_TO,ord));
02683 xsh_free_table(&tab_tmp);
02684 check(tab_tmp=cpl_table_extract_selected(tab_xy_guess));
02685 xsh_free_table(&tab_tmp1);
02686 check(tab_tmp1=cpl_table_extract_selected(tab_xy_peaks_sel));
02687
02688 pxt=cpl_table_get_data_double(tab_tmp,"XC");
02689 pyt=cpl_table_get_data_double(tab_tmp,"YC");
02690
02691 pxp=cpl_table_get_data_double(tab_tmp1,"XP");
02692 pyp=cpl_table_get_data_double(tab_tmp1,"YP");
02693 prp=cpl_table_get_data_int(tab_tmp1,"REL_ORD");
02694 pwp=cpl_table_get_data_int(tab_tmp1,"WEIGHT");
02695 pap=cpl_table_get_data_int(tab_tmp1,"ABS_ORD");
02696
02697
02698 for(i=0;i<nord;i++) {
02699 found=0;
02700 for(j=0;j<npks;j++) {
02701
02702 if((fabs(pxp[j]-pxt[i]) < thresh_x) &&
02703 (fabs(pyp[j]-pyt[i]) < thresh_y)) {
02704 found++;
02705 r=prp[j];
02706 w=pwp[j];
02707 a=pap[j];
02708 x=pxp[j];
02709 y=pyp[j];
02710
02711 }
02712 }
02713 if(found==1) {
02714 pxr[k]=x;
02715 pyr[k]=y;
02716 prr[k]=r;
02717 pwr[k]=w;
02718 par[k]=a;
02719 k++;
02720
02721 }
02722 }
02723 check(cpl_table_select_all(tab_xy_peaks_sel));
02724 check(cpl_table_select_all(tab_xy_guess));
02725 }
02726
02727 check(cpl_table_and_selected_int(tab_tmp2,"REL_ORD",CPL_GREATER_THAN,-1));
02728 check(result=cpl_table_extract_selected(tab_tmp2));
02729 check(cpl_table_name_column(result,"XP","XC"));
02730 check(cpl_table_name_column(result,"YP","YC"));
02731
02732 cleanup:
02733 xsh_free_table(&tab_tmp);
02734 xsh_free_table(&tab_tmp1);
02735 xsh_free_table(&tab_tmp2);
02736
02737
02738 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02739 return NULL;
02740 } else {
02741 return result;
02742 }
02743
02744 }
02745
02746
02756
02757
02758 static cpl_table*
02759 xsh_cfg_recover_select_peaks(cpl_table* tab_xy_guess,
02760 cpl_table* tab_xy_peaks,
02761 const double factor)
02762 {
02763
02764
02765 int ord=0;
02766 int nord=0;
02767 int ord_min=0;
02768 int ord_max=0;
02769 int ord_min_peaks=0;
02770 int ord_max_peaks=0;
02771 int ord_min_guess=0;
02772 int ord_max_guess=0;
02773
02774 int npks=0;
02775 cpl_table* tab_tmp=NULL;
02776 cpl_table* tab_tmp1=NULL;
02777 cpl_table* result=NULL;
02778 int weight_min=0;
02779 int nrows=0;
02780
02781 check(ord_min_guess=cpl_table_get_column_min(tab_xy_guess,"ABS_ORD"));
02782 check(ord_max_guess=cpl_table_get_column_max(tab_xy_guess,"ABS_ORD"));
02783
02784 check(ord_min_peaks=cpl_table_get_column_min(tab_xy_peaks,"ABS_ORD"));
02785 check(ord_max_peaks=cpl_table_get_column_max(tab_xy_peaks,"ABS_ORD"));
02786 ord_min=(ord_min_guess<=ord_min_peaks) ? ord_min_guess : ord_min_peaks;
02787 ord_max=(ord_max_guess>=ord_max_peaks) ? ord_max_guess : ord_max_peaks;
02788
02789 cpl_table_select_all(tab_xy_peaks);
02790
02791 xsh_msg("select lines in order range=[%d,%d]",ord_min,ord_max);
02792
02793
02794 for(ord=ord_min;ord<=ord_max;ord++) {
02795
02796 nord=cpl_table_and_selected_int(tab_xy_guess,"ABS_ORD",CPL_EQUAL_TO,ord);
02797 npks=cpl_table_and_selected_int(tab_xy_peaks,"ABS_ORD",CPL_EQUAL_TO,ord);
02798 check(tab_tmp=cpl_table_extract_selected(tab_xy_peaks));
02799
02800 if(npks>nord) {
02801 weight_min=cpl_table_get_column_min(tab_tmp,"WEIGHT");
02802 check(npks=cpl_table_and_selected_int(tab_tmp,"WEIGHT",
02803 CPL_NOT_GREATER_THAN,
02804 (int)(weight_min+factor*nord)));
02805 tab_tmp1=cpl_table_extract_selected(tab_tmp);
02806
02807
02808
02809 } else {
02810 tab_tmp1=cpl_table_duplicate(tab_tmp);
02811 }
02812 if(ord == ord_min) {
02813 result=cpl_table_duplicate(tab_tmp1);
02814 } else {
02815 nrows=cpl_table_get_nrow(tab_xy_peaks);
02816 cpl_table_insert(result,tab_tmp1,nrows);
02817 }
02818
02819 xsh_free_table(&tab_tmp1);
02820 xsh_free_table(&tab_tmp);
02821 cpl_table_select_all(tab_xy_peaks);
02822 cpl_table_select_all(tab_xy_guess);
02823 }
02824
02825 cleanup:
02826 xsh_free_table(&tab_tmp1);
02827 xsh_free_table(&tab_tmp);
02828
02829 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02830 return NULL;
02831 } else {
02832 return result;
02833 }
02834
02835
02836 }
02837
02838
02839
02848
02849 static cpl_table*
02850 xsh_cfg_recover_measure_tab_xy_peaks(cpl_image* ima_ext,
02851 cpl_parameterlist* parameters)
02852 {
02853
02854 const char* name_p="ima_peaks.fits";
02855 const int size=5000;
02856 cpl_table* result=NULL;
02857 cpl_propertylist* plist=NULL;
02858 double* pp=NULL;
02859 int* po=NULL;
02860 int row=0;
02861 int ord=0;
02862 int ord_num=0;
02863
02864 double* pv=NULL;
02865 int sv=0;
02866 int* pw=NULL;
02867 int nrows=0;
02868 cpl_vector* spectrum=NULL;
02869 cpl_vector* peaks=NULL;
02870 int display=0;
02871 int line_fwhm=4;
02872 double kappa=5;
02873 cpl_parameter* p=NULL;
02874 int i=0;
02875 cpl_table* tab_tmp=NULL;
02876 int debug_level=0;
02877 char ext_val[10];
02878
02879 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.plot"));
02880 check(display = cpl_parameter_get_bool(p));
02881
02882 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_line_fwhm"));
02883 check(line_fwhm = cpl_parameter_get_int(p));
02884
02885 check(p=cpl_parameterlist_find(parameters,"xsh.xsh_cfg_recover.peak_kappa"));
02886 check(kappa = cpl_parameter_get_double(p));
02887 check(debug_level=xsh_parameters_debug_level_get("xsh_cfg_recover",parameters));
02888 ord_num=cpl_image_get_size_y(ima_ext);
02889
02890 result=cpl_table_new(size);
02891 cpl_table_new_column(result,"YP",CPL_TYPE_DOUBLE);
02892 cpl_table_new_column(result,"REL_ORD",CPL_TYPE_INT);
02893 check(cpl_table_fill_column_window(result,"YP",0,size,-1));
02894 check(cpl_table_fill_column_window(result,"REL_ORD",0,size,-1));
02895
02896 pp=cpl_table_get_data_double(result,"YP");
02897 po=cpl_table_get_data_int(result,"REL_ORD");
02898
02899 row=0;
02900 for(ord=0;ord<ord_num;ord++) {
02901 check(spectrum=cpl_vector_new_from_image_row(ima_ext,ord+1));
02902 check(peaks=xsh_spectrum_detect_peaks(spectrum,line_fwhm,kappa,display));
02903 if(debug_level>XSH_DEBUG_LEVEL_NONE) {
02904 plist=cpl_propertylist_new();
02905 sprintf(ext_val,"%s%d","ext",ord);
02906 cpl_propertylist_append_string(plist,"EXTNAME",ext_val);
02907 if(ord==0) {
02908 cpl_vector_save(spectrum,name_p,CPL_BPP_IEEE_FLOAT,plist,
02909 CPL_IO_DEFAULT);
02910 } else {
02911 cpl_vector_save(spectrum,name_p,CPL_BPP_IEEE_FLOAT,
02912 plist,CPL_IO_EXTEND);
02913 }
02914 xsh_free_propertylist(&plist);
02915 }
02916 if(peaks!=NULL) {
02917 check(sv=cpl_vector_get_size(peaks));
02918 if (sv>0) {
02919 check(pv=cpl_vector_get_data(peaks));
02920 for(i=0;i<sv;i++) {
02921 po[row]=ord;
02922 pp[row]=pv[i];
02923 row++;
02924 }
02925 }
02926 }
02927 xsh_free_vector(&spectrum);
02928 xsh_free_vector(&peaks);
02929 }
02930 check(cpl_table_and_selected_double(result,"YP",CPL_GREATER_THAN,-1));
02931 check(tab_tmp=cpl_table_extract_selected(result));
02932 xsh_free_table(&result);
02933 result=cpl_table_duplicate(tab_tmp);
02934 xsh_free_table(&tab_tmp);
02935
02936 check(cpl_table_erase_invalid_rows(result));
02937
02938 cpl_table_new_column(result,"WEIGHT",CPL_TYPE_INT);
02939 nrows=cpl_table_get_nrow(result);
02940 xsh_msg("size=%d",size);
02941 check(cpl_table_fill_column_window(result,"WEIGHT",0,size,-1));
02942 check(pw=cpl_table_get_data_int(result,"WEIGHT"));
02943
02944 for(i=0;i<nrows;i++) {
02945 pw[i]=i;
02946 }
02947
02948 cleanup:
02949 xsh_free_table(&tab_tmp);
02950 xsh_free_vector(&spectrum);
02951 xsh_free_vector(&peaks);
02952
02953 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02954 return NULL;
02955 } else {
02956 return result;
02957 }
02958
02959 }
02960
02961
02962
02974
02975 static cpl_image*
02976 xsh_cfg_recover_linear_ext(cpl_frame* raw_frm,
02977 cpl_frame* order_tab_centr,
02978 xsh_instrument* instr,
02979 const int slit,
02980 const double min)
02981
02982 {
02983 const char* name=NULL;
02984 cpl_image* input=NULL;
02985
02986 const char* name_o="ima_raw.fits";
02987 const char* name_e="ima_ext.fits";
02988 int sx=0;
02989 int sy=0;
02990 int ord_num=0;
02991 int ord_min_tra=0;
02992 int ord_max_tra=0;
02993
02994 int ord=0;
02995
02996 cpl_image* result=NULL;
02997 double* pou=NULL;
02998 double* pin=NULL;
02999 int i=0;
03000 int j=0;
03001 double x=0;
03002 int s=0;
03003 double flux=0;
03004 int pix=0;
03005 xsh_order_list* order_list = NULL;
03006 cpl_table* tab_centr=NULL;
03007
03008 check(name=cpl_frame_get_filename(raw_frm));
03009 check(input=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
03010 check(cpl_image_threshold(input,min,FLT_MAX,0,FLT_MAX));
03011 check(cpl_image_save(input,name_o,CPL_BPP_IEEE_FLOAT,NULL,
03012 CPL_IO_DEFAULT));
03013
03014 xsh_msg("extracting image %s",name_o);
03015
03016 check(sx=cpl_image_get_size_x(input));
03017 check(sy=cpl_image_get_size_y(input));
03018 check(name=cpl_frame_get_filename( order_tab_centr));
03019 check(tab_centr=cpl_table_load(name,1,0));
03020
03021 ord_min_tra=cpl_table_get_column_min(tab_centr,"ABSORDER");
03022 ord_max_tra=cpl_table_get_column_max(tab_centr,"ABSORDER");
03023 xsh_free_table(&tab_centr);
03024
03025 ord_num=ord_max_tra-ord_min_tra+1;
03026
03027
03028 check(result=cpl_image_new(sy,ord_num,CPL_TYPE_DOUBLE));
03029 check(pou=cpl_image_get_data_double(result));
03030 check(pin=cpl_image_get_data_double(input));
03031
03032
03033 check(order_list = xsh_order_list_load(order_tab_centr,instr));
03034
03035
03036
03037
03038 for(ord=0;ord<ord_num;ord++) {
03039 for(j=0;j<sy;j++) {
03040 flux=0;
03041 if(cpl_polynomial_get_dimension(order_list->list[ord].cenpoly) <
03042 XSH_ORDPOS_POL_DIM_MAX) {
03043 check(x=cpl_polynomial_eval_1d(order_list->list[ord].cenpoly,j,NULL));
03044
03045 }
03046 i=(int)(x+0.5);
03047 for(s=-slit;s<=slit;s++) {
03048 pix=j*sx+i+s;
03049 if ( (pix>0) && (pix<sx*sy) ) {
03050 flux+=pin[pix];
03051
03052 }
03053 }
03054 pou[ord*sy+j]=flux;
03055 }
03056 }
03057
03058
03059 check(cpl_image_save(result,name_e,CPL_BPP_IEEE_FLOAT,NULL,
03060 CPL_IO_DEFAULT));
03061
03062 cleanup:
03063 xsh_free_image(&input);
03064 xsh_free_table(&tab_centr);
03065 xsh_order_list_free( &order_list ) ;
03066
03067 if (cpl_error_get_code() != CPL_ERROR_NONE) {
03068 return NULL;
03069 } else {
03070 return result;
03071 }
03072
03073 }
03074
03075
03086
03087 static cpl_error_code
03088 xsh_cfg_recover_guess_tab_corr_by_ordpos(xsh_instrument* instr,
03089 cpl_frame* order_tab_centr,
03090 cpl_frame** model_xy_gue) {
03091
03092 cpl_table* tab_xy_guess=NULL;
03093 xsh_order_list* order_list = NULL;
03094 int size=0;
03095 double* pxc=NULL;
03096 double* pyc=NULL;
03097 double* px=NULL;
03098 double* py=NULL;
03099 int* pog=NULL;
03100 int* por=NULL;
03101 int* poa=NULL;
03102 int ord_min_tra=0;
03103 int ord_max_tra=0;
03104
03105 int ord_min_gue=0;
03106 int ord_max_gue=0;
03107 int ord_min_off=0;
03108 int ord_max_off=0;
03109
03110 int ord_rel=0;
03111
03112
03113
03114 const char* name_c=NULL;
03115 const char* name_g=NULL;
03116 int i=0;
03117 cpl_table * tab_centr=NULL;
03118 int sizey=4096;
03119 int j=0;
03120 int ord=0;
03121 int nord=0;
03122
03123 check(name_g=cpl_frame_get_filename(*model_xy_gue));
03124
03125 check(tab_xy_guess=cpl_table_load(name_g,1,0));
03126
03127 check(size=cpl_table_get_nrow(tab_xy_guess));
03128 check(cpl_table_new_column(tab_xy_guess,"XC",CPL_TYPE_DOUBLE));
03129 check(cpl_table_new_column(tab_xy_guess,"YC",CPL_TYPE_DOUBLE));
03130 check(cpl_table_fill_column_window(tab_xy_guess,"XC",0,size,-1));
03131 check(cpl_table_fill_column_window(tab_xy_guess,"YC",0,size,-1));
03132
03133 check(pxc=cpl_table_get_data_double(tab_xy_guess,"XC"));
03134 check(pyc=cpl_table_get_data_double(tab_xy_guess,"YC"));
03135 check(px=cpl_table_get_data_double(tab_xy_guess,"XG"));
03136 check(py=cpl_table_get_data_double(tab_xy_guess,"YG"));
03137 check(pog=cpl_table_get_data_int(tab_xy_guess,"ABS_ORD"));
03138 check(ord_min_gue=cpl_table_get_column_min(tab_xy_guess,"ABS_ORD"));
03139 check(ord_max_gue=cpl_table_get_column_max(tab_xy_guess,"ABS_ORD"));
03140
03141 if (order_tab_centr!=NULL) {
03142 name_c=cpl_frame_get_filename(order_tab_centr);
03143 xsh_msg("tab centre file=%s",name_c);
03144 check(tab_centr=cpl_table_load(name_c,1,0));
03145 check(ord_min_tra=cpl_table_get_column_min(tab_centr,"ABSORDER"));
03146 check(ord_max_tra=cpl_table_get_column_max(tab_centr,"ABSORDER"));
03147 xsh_free_table(&tab_centr);
03148
03149
03150 xsh_msg("Trace tab order min=%d max=%d",ord_min_tra,ord_max_tra);
03151 check(order_list = xsh_order_list_load(order_tab_centr,instr));
03152 xsh_msg("Guess tab order min=%d max=%d",ord_min_gue,ord_max_gue);
03153 ord_min_off=ord_min_gue-ord_min_tra;
03154 ord_max_off=ord_max_gue-ord_max_tra;
03155 xsh_msg_debug("offset ord min=%d",ord_min_off);
03156 xsh_msg_debug("offset ord max=%d",ord_max_off);
03157
03158 if(ord_min_off!=0) {
03159 xsh_msg_error("The guess line table has lines in an order not traced");
03160 xsh_msg_error("Remove lines from guess at order %d",ord_min_gue);
03161 xsh_msg_error("Or allow xsh_orderpos to trace order %d",ord_min_gue);
03162 xsh_msg_error("(For example changing XSH_SPECTRALFORMAT_TABLE_arm");
03163 cpl_error_set(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT);
03164 goto cleanup;
03165 }
03166
03167 xsh_msg_debug("size=%d ord_min_gue=%d ord_max_gue=%d",
03168 size,ord_min_gue,ord_max_gue);
03169
03170 for(i=0;i<size;i++) {
03171 pyc[i]=py[i];
03172
03173
03174 check( ord_rel = xsh_order_list_get_order( order_list, pog[i] ) ) ;
03175
03176 ord_rel=pog[i]-ord_min_gue+ord_min_off;
03177
03178
03179
03180 if(ord_rel>-1) {
03181 if(cpl_polynomial_get_dimension(order_list->list[ord_rel].cenpoly) <
03182 XSH_ORDPOS_POL_DIM_MAX) {
03183 check( pxc[i] = cpl_polynomial_eval_1d(order_list->list[ord_rel].cenpoly,py[i],NULL));
03184 }
03185 }
03186
03187
03188
03189 }
03190
03191
03192
03193
03194 nord=ord_max_tra-ord_min_tra+1;
03195 size=nord*sizey;
03196 check(tab_centr=cpl_table_new(nord*sizey));
03197 check(cpl_table_new_column(tab_centr,"X",CPL_TYPE_DOUBLE));
03198 check(cpl_table_new_column(tab_centr,"Y",CPL_TYPE_DOUBLE));
03199 check(cpl_table_new_column(tab_centr,"ORDER",CPL_TYPE_INT));
03200 check(cpl_table_new_column(tab_centr,"ABSORDER",CPL_TYPE_INT));
03201 check(cpl_table_fill_column_window(tab_centr,"X",0,size,-1));
03202 check(cpl_table_fill_column_window(tab_centr,"Y",0,size,-1));
03203 check(cpl_table_fill_column_window(tab_centr,"ORDER",0,size,-1));
03204 check(cpl_table_fill_column_window(tab_centr,"ABSORDER",0,size,-1));
03205 check(px=cpl_table_get_data_double(tab_centr,"X"));
03206 check(py=cpl_table_get_data_double(tab_centr,"Y"));
03207 check(por=cpl_table_get_data_int(tab_centr,"ORDER"));
03208 check(poa=cpl_table_get_data_int(tab_centr,"ABSORDER"));
03209
03210
03211 i=0;
03212 for(ord=0;ord<nord;ord++) {
03213 for(j=0;j<sizey;j++) {
03214
03215 poa[i]=ord+ord_min_tra;
03216 por[i]=ord+1;
03217 py[i]=(double)j;
03218 check(px[i]=cpl_polynomial_eval_1d(order_list->list[ord].cenpoly,py[i],
03219 NULL));
03220 i++;
03221 }
03222 }
03223 check(cpl_table_save(tab_centr,NULL,NULL,"tab_centr.fits",CPL_IO_DEFAULT));
03224
03225 }
03226 check(cpl_table_save(tab_xy_guess, NULL, NULL, name_g, CPL_IO_DEFAULT));
03227
03228 cleanup:
03229
03230 xsh_free_table(&tab_centr);
03231 xsh_free_table(&tab_xy_guess);
03232 xsh_order_list_free( &order_list);
03233 if(cpl_error_get_code() != CPL_ERROR_NONE) {
03234 xsh_print_rec_status(0);
03235 }
03236 return cpl_error_get_code();
03237
03238 }
03239
03240
03249
03250 static cpl_frame*
03251 xsh_cfg_recover_gen_xyg_frame(cpl_frame * wave_list,
03252 cpl_frame * config_frame,
03253 xsh_instrument* inst, int prescan)
03254 {
03255
03256 cpl_table* lines_tab=NULL ;
03257 cpl_frame* result=NULL;
03258 struct xs_3* p_xs_3_config=NULL;
03259 struct xs_3 xs_3_config;
03260 cpl_table* lines_xyg=NULL;
03261 cpl_propertylist* header=NULL;
03262 char name_o[80];
03263 const char* tag=XSH_GET_TAG_FROM_ARM(XSH_MODEL_GUESS_XY,inst);
03264
03265 sprintf(name_o,"%s%s",tag,".fits");
03266
03267
03268
03269 p_xs_3_config=&xs_3_config;
03270 if (xsh_model_config_load_best(config_frame, p_xs_3_config) !=
03271 CPL_ERROR_NONE) {
03272 xsh_msg_error("Cannot load %s as a config",
03273 cpl_frame_get_filename(config_frame)) ;
03274 return NULL ;
03275 }
03276
03277
03278 check(lines_tab = cpl_table_load(cpl_frame_get_filename(wave_list), 1, 0)) ;
03279
03280
03281 check(xsh_cfg_recover_gen_xyg(lines_tab,p_xs_3_config,inst,prescan,&lines_xyg));
03282
03283
03284
03285 header=cpl_propertylist_new();
03286 check( xsh_pfits_set_pcatg(header,tag));
03287 cpl_table_save(lines_xyg, header, NULL,name_o, CPL_IO_DEFAULT);
03288
03289
03290 result=xsh_frame_product(name_o,tag,CPL_FRAME_TYPE_TABLE,
03291 CPL_FRAME_GROUP_PRODUCT,
03292 CPL_FRAME_LEVEL_FINAL);
03293 cpl_error_reset();
03294
03295 cleanup:
03296 xsh_free_propertylist(&header);
03297 xsh_free_table(&lines_tab);
03298 xsh_free_table(&lines_xyg);
03299
03300
03301 if (cpl_error_get_code() != CPL_ERROR_NONE) {
03302 return NULL;
03303 } else {
03304 return result;
03305 }
03306
03307 }
03308
03309
03320
03321
03322 static cpl_error_code
03323 xsh_cfg_recover_gen_xyg(
03324 cpl_table * lines_tab,
03325 struct xs_3* p_xs_3_config,
03326 xsh_instrument* inst,int pre_scan,
03327 cpl_table ** lines_xyg)
03328 {
03329 double x,y;
03330 float* pw=NULL;
03331 int row=0;
03332
03333 int i=0;
03334 double blaze_wav=0;
03335 double lambda_min=0;
03336 double lambda_max=0;
03337 int morder_cnt=0;
03338 double m_to_mu=1.e6;
03339 int lines_tot;
03340 int prescan=0;
03341 if(pre_scan) {
03342 prescan=-50;
03343 }
03344 lines_tot = cpl_table_get_nrow(lines_tab) ;
03345 xsh_msg("lines_tot=%d",lines_tot);
03346 *lines_xyg = cpl_table_new(lines_tot*3);
03347 cpl_table_new_column(*lines_xyg,"WAVELENGTH",CPL_TYPE_DOUBLE);
03348 cpl_table_new_column(*lines_xyg,"XG",CPL_TYPE_DOUBLE);
03349 cpl_table_new_column(*lines_xyg,"YG",CPL_TYPE_DOUBLE);
03350 cpl_table_new_column(*lines_xyg,"ABS_ORD",CPL_TYPE_INT);
03351
03352 pw=cpl_table_get_data_float(lines_tab,"WAVELENGTH");
03353 xsh_msg("order min:%d max: %d",
03354 p_xs_3_config->morder_min,p_xs_3_config->morder_max);
03355
03356 for(i=0;i<lines_tot;i++) {
03357 for (morder_cnt= p_xs_3_config->morder_min;
03358 morder_cnt<=p_xs_3_config->morder_max;
03359 morder_cnt++) {
03360 blaze_wav=2*(sin(-p_xs_3_config->nug))/(morder_cnt*p_xs_3_config->sg);
03361 lambda_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
03362 lambda_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
03363 lambda_min *=m_to_mu;
03364 lambda_max *=m_to_mu;
03365
03366
03367 if(pw[i]> lambda_min*0.98 && pw[i]<1.02*lambda_max) {
03368 check(xsh_model_get_xy(p_xs_3_config,inst,pw[i],
03369 morder_cnt,0.0,&x,&y));
03370
03371
03372 cpl_table_set_double(*lines_xyg,"WAVELENGTH",row,pw[i]);
03373 cpl_table_set_int(*lines_xyg,"ABS_ORD",row,morder_cnt);
03374 cpl_table_set_double(*lines_xyg,"XG",row,x-prescan);
03375 cpl_table_set_double(*lines_xyg,"YG",row,y);
03376 row++;
03377 }
03378 }
03379 }
03380
03381
03382
03383
03384
03385
03386
03387
03388
03389
03390
03391 cpl_table_erase_invalid(*lines_xyg);
03392
03393 xsh_msg("lines inc. overlap: %d\n",cpl_table_get_nrow(*lines_xyg));
03394
03395 cleanup:
03396 return cpl_error_get_code();
03397
03398 }
03399
03400
03401