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