91#define RECIPE_ID "xsh_cfg_recover"
92#define RECIPE_AUTHOR "A. Modigliani, P. Bristow"
93#define RECIPE_CONTACT "amodigli@eso.org"
95#define XSH_THE_TAB_SUB_VIS "XSH_THE_TAB_VIS"
96#define XSH_THE_TAB_SUB_UVB "XSH_THE_TAB_UVB"
97#define XSH_THE_TAB_SUB_NIR "XSH_THE_TAB_NIR"
99#define XSH_STARTUP_TABLE_COLNAME_X "X"
102#define XSH_ORDPOS_POL_DIM_MAX 4
114 cpl_parameterlist* parameters,
115 cpl_matrix** mat_pat,
116 cpl_matrix** mat_dat,
125 cpl_frame* wave_list,
126 cpl_frame** THE1, cpl_frame** THE9);
141 cpl_frame** model_xy_gue);
146 cpl_parameterlist* parameters,
147 cpl_frame** model_xy_gue,
154 cpl_table** tab_xy_peaks);
160 cpl_table* tab_xy_peaks_sel,
167 cpl_parameterlist* parameters);
171 cpl_table* tab_xy_peaks,
172 const double factor);
180 cpl_frame* order_tab_centr,
181 cpl_parameterlist* parameters,
183 cpl_frame* model_config,
189 cpl_table * lines_tab,
190 struct xs_3* p_xs_3_config,
192 cpl_table ** lines_gue);
197 cpl_frame * wave_list,
198 cpl_frame * config_frame,
204 cpl_frame* order_tab_centr,
207 const double thresh_min);
211 cpl_frame* order_tab_centr,
212 cpl_frame** model_xy_gue);
224 cpl_frameset* frameset,
227 cpl_frameset* calib);
231 cpl_frameset* frameset);
237 "Optimizes a model configuration to match data taken after a major format change";
250 "This recipe creates a wavelength solution and an order table.\n\
252 - [UVB, VIS] A RAW frame (Format = RAW, Tag = FMTCHK_arm)\n\
253 - [NIR] Two RAW frames (Format = RAW, Tag = FMTCHK_arm_ON,FMTCHK_arm_OFF)\n\
254 - The old model cfg table (Format = TABLE, Tag = XSH_MOD_CFG_TAB_arm)\n\
255 - A ref. line list. The model computes corresponding positions \n\
256 (Format = TABLE, Tag = ARC_LINE_LIST_arm)\n\
257 - [UVB,VIS,OPTIONAL] A master bias (Format = PRE, Tag = MASTER_BIAS_arm)\n\
258 - [UVB,VIS,OPTIONAL] A master dark (Format = PRE, Tag = MASTER_DARK_arm)\n\
259 - [OPTIONAL-Required if method=pm,peaks] \n\
260 An order table (Format = TABLE, Tag = ORDER_TAB_CENTR_arm)\n\
261 - [OPTIONAL-Required if first-anneal=TRUE] \n\
262 A table with measured line positions (Format = TABLE, Tag = XSH_MEASCOORD_arm)\n\
264 - if first-anneal=FALSE & last-step=FALSE\n\
266 - if first-anneal=TRUE & last-step=FALSE\n\
267 an optimized model configuration, PRO.CATG=XSH_MOD_CFG_arm\n\
268 - if last-step=TRUE\n\
269 an optimized model configuration, PRO.CATG=XSH_MOD_FAN_arm\n\
270 an optimized model configuration, PRO.CATG=XSH_MOD_CFG_OPT_arm\n\
271 an quality control table, PRO.CATG=MODEL_GUESS_XY_arm\n\
272 the model theoretical map corresponding to the optimized model config,\n\
273 PRO.CATG=THEO_TAB_MULT_arm, THEO_TAB_IFU_arm, and THEO_TAB_SING_arm\n\
295 cpl_recipe *recipe = NULL;
296 cpl_plugin *plugin = NULL;
298 recipe = cpl_calloc(1,
sizeof(*recipe));
299 if ( recipe == NULL ){
303 plugin = &recipe->interface ;
305 cpl_plugin_init(plugin,
308 CPL_PLUGIN_TYPE_RECIPE,
319 cpl_pluginlist_append(list, plugin);
321 return (cpl_error_get_code() != CPL_ERROR_NONE);
349 cpl_recipe * recipe ;
362 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
363 recipe = (cpl_recipe *)plugin ;
367 recipe->parameters = cpl_parameterlist_new() ;
369 assure( recipe->parameters != NULL,
370 CPL_ERROR_ILLEGAL_OUTPUT,
371 "Memory allocation failed!");
379 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.first-anneal",
381 "Run first annealing (TRUE) or not (FALSE)"
382 "See recipe man-page % Input frames",
383 "xsh.xsh_cfg_recover",CPL_FALSE);
385 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"first-anneal");
386 cpl_parameterlist_append(recipe->parameters,p);
391 p = cpl_parameter_new_enum(
"xsh.xsh_model_compute.arm",
394 "xsh.xsh_model_compute",
396 3,
"uvb",
"vis",
"nir");
398 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"arm");
399 cpl_parameterlist_append(recipe->parameters, p);
402 p = cpl_parameter_new_value(
"xsh.xsh_model_compute.name_i",
404 "Filename with wavelength,x,y,order: ",
405 "xsh.xsh_model_compute",
408 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"name_i");
409 cpl_parameterlist_append(recipe->parameters, p);
412 p = cpl_parameter_new_value(
"xsh.xsh_model_compute.niter",
413 CPL_TYPE_INT,
"No of iterations for first anneal",
414 "xsh.xsh_model_compute", 100000);
416 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"niter");
417 cpl_parameterlist_append(recipe->parameters,p);
419 p = cpl_parameter_new_value(
"xsh.xsh_model_compute.coord_frame",
420 CPL_TYPE_INT,
"Co-ordinate frame for centroids (0=raw,1=pre)",
421 "xsh.xsh_model_compute", 1);
423 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"coord_frame");
424 cpl_parameterlist_append(recipe->parameters,p);
448 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.last-step",
450 "Run last step (TRUE) or not (FALSE)"
451 "See recipe man-page % Input frames",
452 "xsh.xsh_cfg_recover",CPL_FALSE);
454 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"last-step");
455 cpl_parameterlist_append(recipe->parameters,p);
458 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.plot",
460 "Display plot (TRUE) or not (FALSE)",
461 "xsh.xsh_cfg_recover",CPL_FALSE);
463 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"plot");
464 cpl_parameterlist_append(recipe->parameters,p);
466 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.ima_thresh_min",
468 "Min thresh raw image.",
469 "xsh.xsh_cfg_recover", 40.);
471 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"ima_tresh_min");
472 cpl_parameterlist_append(recipe->parameters,p);
475 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.cor_prescan",
476 CPL_TYPE_BOOL,
"Correct for prescan",
477 "xsh.xsh_cfg_recover", FALSE);
479 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"cor_prescan");
480 cpl_parameterlist_append(recipe->parameters,p);
483 p = cpl_parameter_new_enum(
"xsh.xsh_cfg_recover.method",
485 "Model predictions correction method. "
487 "gfit: 2D Gaussian line fit"
488 "peaks: line peaks detection"
489 "pm: line peaks detection & pattern match",
490 "xsh.xsh_cfg_recover",
492 "safefit",
"gfit",
"peaks",
"pm");
497 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"method");
498 cpl_parameterlist_append(recipe->parameters,p);
501 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.offx",
502 CPL_TYPE_DOUBLE,
"X offset to model predictions",
503 "xsh.xsh_cfg_recover", 0.);
505 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"offx");
506 cpl_parameterlist_append(recipe->parameters,p);
509 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.offy",
510 CPL_TYPE_DOUBLE,
"Y offset to model predictions",
511 "xsh.xsh_cfg_recover", 0.);
513 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"offy");
514 cpl_parameterlist_append(recipe->parameters,p);
518 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.slit",
519 CPL_TYPE_INT,
"Extraction slit",
520 "xsh.xsh_cfg_recover", 5);
522 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"slit");
523 cpl_parameterlist_append(recipe->parameters,p);
537 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.gfit_box_sy",
538 CPL_TYPE_INT,
"Gauss fit Y search box size "
539 "for lines on actual frame",
540 "xsh.xsh_cfg_recover", 20);
542 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"gfit_box_sy");
543 cpl_parameterlist_append(recipe->parameters,p);
546 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.peak_line_fwhm",
548 "The FWHM used in line convolution, "
550 "xsh.xsh_cfg_recover", 4);
552 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"peak_line_fwhm");
553 cpl_parameterlist_append(recipe->parameters,p);
556 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.peak_kappa",
558 "The kappa value, used to identify line peaks "
559 "if max>kappa*stdev+median, max is a valid peak "
560 "where max, stdev,median are computed on the "
561 "extracted spectrum",
562 "xsh.xsh_cfg_recover", 5.);
564 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"peak_kappa");
565 cpl_parameterlist_append(recipe->parameters,p);
570 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.peak_factor",
572 "Relative Intensity threshold factor "
573 "for line peaks detection",
574 "xsh.xsh_cfg_recover", 10.);
576 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"peak_factor");
577 cpl_parameterlist_append(recipe->parameters,p);
580 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.peak_match_x",
581 CPL_TYPE_INT,
"Radii for line peaks matches",
582 "xsh.xsh_cfg_recover", 10);
584 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"peak_match_x");
585 cpl_parameterlist_append(recipe->parameters,p);
588 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.peak_match_y",
589 CPL_TYPE_INT,
"Radii for line peaks matches",
590 "xsh.xsh_cfg_recover", 20);
592 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"peak_match_y");
593 cpl_parameterlist_append(recipe->parameters,p);
596 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.pm_ord_sel",
598 "From guess line and peaks positions "
599 "are extracted the ones in the range "
600 "[ord_min,ord_min+pm_ord_sel] ",
601 "xsh.xsh_cfg_recover", 1);
603 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"pm_ord_sel");
604 cpl_parameterlist_append(recipe->parameters,p);
607 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.pm_radius",
609 "Search radius applied in final pattern "
610 "matching (data units).",
611 "xsh.xsh_cfg_recover", 20.);
613 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"pm_radius");
614 cpl_parameterlist_append(recipe->parameters,p);
616 p = cpl_parameter_new_range(
"xsh.xsh_cfg_recover.pm_tolerance",
618 "Max relative difference of angles and scales "
619 "from their median value for match acceptance.",
620 "xsh.xsh_cfg_recover", 0.1,0.001,0.5);
622 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"pm_tolerance");
623 cpl_parameterlist_append(recipe->parameters,p);
627 p = cpl_parameter_new_value(
"xsh.xsh_cfg_recover.anneal_niter",
628 CPL_TYPE_INT,
"Simulated annealing iterations",
629 "xsh.xsh_cfg_recover", 1000);
631 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"anneal_niter");
632 cpl_parameterlist_append(recipe->parameters,p);
652 cpl_recipe *recipe = NULL;
654 cpl_errorstate initial_errorstate = cpl_errorstate_get();
657 assure( plugin != NULL, CPL_ERROR_NULL_INPUT,
"Null plugin" );
660 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
661 CPL_ERROR_TYPE_MISMATCH,
"Plugin is not a recipe");
663 recipe = (cpl_recipe *)plugin;
668 if (!cpl_errorstate_is_equal(initial_errorstate)) {
672 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
676 return recipe_status;
689 cpl_recipe *recipe = NULL;
692 assure( plugin != NULL, CPL_ERROR_NULL_INPUT,
"Null plugin" );
695 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
696 CPL_ERROR_TYPE_MISMATCH,
"Plugin is not a recipe");
698 recipe = (cpl_recipe *)plugin;
703 if (cpl_error_get_code() != CPL_ERROR_NONE)
715 cpl_frameset* frameset)
719 int recipe_tags_size = 1;
721 cpl_parameter* p=NULL;
725 cpl_frameset* raws=NULL;
726 cpl_frameset* calib=NULL;
735 check(p = cpl_parameterlist_find(parameters,
736 "xsh.xsh_cfg_recover.first-anneal"));
737 check(first_anneal = cpl_parameter_get_bool(p));
740 check(p = cpl_parameterlist_find(parameters,
741 "xsh.xsh_cfg_recover.last-step"));
742 check(last_step = cpl_parameter_get_bool(p));
751 recipe_tags, recipe_tags_size,
RECIPE_ID,
757 "error_performing first anneal");
770 "error performing startup");
781 return cpl_error_get_code();
797 cpl_frameset* frameset,
811 cpl_frameset* on = NULL;
812 cpl_frameset* off = NULL;
813 cpl_frameset* on_off = NULL;
816 cpl_frame* master_bias = NULL;
817 cpl_frame* master_dark = NULL;
818 cpl_frame* bpmap = NULL;
819 cpl_frame* predict_rmbias = NULL;
820 cpl_frame* predict_rmdark = NULL;
822 cpl_frame* wave_list=NULL ;
823 cpl_frame* xsh_config_first_anneal=NULL ;
824 cpl_frame* xsh_config_input=NULL ;
825 cpl_frame* xsh_config_last_anneal=NULL ;
826 cpl_frame * order_tab_centr = NULL ;
827 cpl_frame * model_xy_gue=NULL;
828 cpl_frame * measure_xy_pos_frm=NULL;
829 cpl_frame * model_THE1_frm=NULL;
830 cpl_frame * model_THE9_frm=NULL;
832 int anneal_niter=10000;
833 cpl_parameter* p=NULL;
837 cpl_frame* fmtchk = NULL;
838 const char* name=NULL;
839 const char* tag=NULL;
842 const char* filename=NULL;
843 cpl_propertylist* plist=NULL ;
844 cpl_table* tab_xsh_config_first_anneal=NULL;
845 cpl_table* tab_xsh_config_input=NULL;
846 cpl_frame* first_anneal_frm=NULL;
848 int first_anneal=
true;
850 int pre_overscan_corr=0;
855 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.cor_prescan"));
856 check(prescan = cpl_parameter_get_bool(p));
858 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.anneal_niter"));
859 check(anneal_niter = cpl_parameter_get_int(p));
861 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.method"));
864 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.first-anneal"));
865 check(first_anneal = cpl_parameter_get_bool(p));
882 check(xsh_config_first_anneal=cpl_frame_duplicate(first_anneal_frm));
887 check(xsh_config_first_anneal=cpl_frame_duplicate(first_anneal_frm));
888 check(name=cpl_frame_get_filename(xsh_config_first_anneal));
889 check(tab_xsh_config_first_anneal=cpl_table_load(name,1,0));
890 check(plist=cpl_propertylist_load(name,0));
892 check(filename=cpl_sprintf(
"%s%s",tag,
".fits"));
893 check(cpl_table_save(tab_xsh_config_first_anneal,plist, NULL,filename,
895 check(cpl_frame_set_filename(xsh_config_first_anneal,filename));
896 check(cpl_frame_set_tag(xsh_config_first_anneal,tag));
897 check(cpl_frame_set_group(xsh_config_first_anneal,CPL_FRAME_GROUP_PRODUCT));
898 check(cpl_frame_set_level(xsh_config_first_anneal,CPL_FRAME_LEVEL_FINAL));
909 if(strcmp(
method,
"safefit")!=0) {
914 cpl_error_set(cpl_func,CPL_ERROR_DATA_NOT_FOUND);
915 order_tab_centr=NULL;
917 if ((strcmp(
method,
"pm") == 0) || (strcmp(
method,
"peaks") == 0) ) {
918 xsh_msg_error(
"If method is 'pm' or 'peaks' you must provide");
948 pre_overscan_corr,CPL_TRUE));
949 check(fmtchk = cpl_frameset_get_frame(raws,0));
951 if(master_bias != NULL) {
955 predict_rmbias = cpl_frame_duplicate(fmtchk);
957 if(master_dark != NULL) {
961 ".fits", (
void*)NULL ) ;
965 predict_rmdark = cpl_frame_duplicate(predict_rmbias);
978 check(predict_rmdark = cpl_frame_duplicate(cpl_frameset_get_frame(on_off,0)));
984 xsh_config_first_anneal,
1004 if (!first_anneal) {
1006 xsh_msg(
"update the prism temperatures before line matching");
1017 xsh_config_first_anneal,
1021 xsh_msg(
" REGDEBUG A generate %s", cpl_frame_get_filename(model_xy_gue));
1031 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
1040 xsh_msg(
"update the prism temperatures before annealing");
1050 check(measure_xy_pos_frm=cpl_frame_duplicate(model_xy_gue));
1055 check(name=cpl_frame_get_filename(measure_xy_pos_frm));
1057 nrows=cpl_table_get_nrow(cpl_table_load(name,1,0));
1062 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)));
1068 xsh_msg(
" REGDEBUG B before annealing %s", cpl_frame_get_filename( measure_xy_pos_frm));
1113 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1139static cpl_error_code
1142 cpl_frame* wave_list,
1143 cpl_frame** THE1_frm,
1144 cpl_frame** THE9_frm)
1146 struct xs_3* p_xs_3_config=NULL;
1147 struct xs_3 xs_3_config;
1148 const char* line_list=NULL;
1149 const char* THE1_filename=
"model_THE1.fits";
1150 const char* THE9_filename=
"model_THE9.fits";
1151 const char* pro_catg=NULL;
1154 p_xs_3_config=&xs_3_config;
1158 cpl_frame_get_filename(config_frame)) ;
1159 return CPL_ERROR_DATA_NOT_FOUND ;
1162 check(line_list=cpl_frame_get_filename(wave_list));
1164 1,-1,THE1_filename));
1167 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
1171 9,-1,THE9_filename));
1175 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
1180 if(cpl_error_get_code() == CPL_ERROR_NONE) {
1181 return cpl_error_get_code();
1184 return cpl_error_get_code();
1200static cpl_error_code
1202 cpl_frame** model_xy_gue)
1204 const char* name=NULL;
1205 cpl_table* tab=NULL;
1208 cpl_parameter* p=NULL;
1210 check(name=cpl_frame_get_filename(*model_xy_gue));
1211 check(tab=cpl_table_load(name,1,0));
1213 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.offx"));
1214 check(
offx = cpl_parameter_get_double(p));
1216 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.offy"));
1217 check(
offy = cpl_parameter_get_double(p));
1219 check(cpl_table_add_scalar(tab,
"XG",
offx));
1220 check(cpl_table_add_scalar(tab,
"YG",
offy));
1222 check(cpl_table_save(tab, NULL, NULL,name, CPL_IO_DEFAULT));
1228 return cpl_error_get_code();
1241static cpl_error_code
1244 const char * name=NULL;
1245 cpl_table* tbl=NULL;
1247 cpl_propertylist* header = NULL;
1251 sprintf(name_o,
"%s%s",tag,
".fits");
1253 check(name=cpl_frame_get_filename(*frm));
1257 check(tbl=cpl_table_load(name,1,0));
1258 check ( header = cpl_propertylist_load( name, 0));
1260 check(nrows=cpl_table_get_nrow(tbl));
1262 xsh_msg_error(
"Table %s has 0 rows. Something wrong! Exit",name);
1267 check(cpl_table_name_column(tbl,
"WAVELENGTH",
1269 check(cpl_table_name_column(tbl,
"ABS_ORD",
1273 check(cpl_table_fill_column_window(tbl,
1281 check(cpl_table_duplicate_column(tbl,
1284 check( cpl_table_duplicate_column(tbl,
1289 check(cpl_table_duplicate_column(tbl,
1292 check( cpl_table_duplicate_column(tbl,
1297 check(cpl_table_duplicate_column(tbl,
1300 check( cpl_table_duplicate_column(tbl,
1334 cpl_table_erase_invalid_rows(tbl);
1336 check(cpl_table_save(tbl, header, NULL,name_o, CPL_IO_DEFAULT));
1338 cpl_frame_set_filename(*frm,name_o);
1339 cpl_frame_set_tag(*frm,tag);
1343 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1346 return cpl_error_get_code() ;
1363static cpl_error_code
1365 cpl_parameterlist* parameters,
1371 cpl_parameter* p=NULL;
1381 cpl_image* ima=NULL;
1382 const char* name=NULL;
1391 const char* name_g=
"tab_xy_corr_gauss.fits";
1392 cpl_table* tab=NULL;
1393 cpl_table* tmp=NULL;
1400 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.gfit_box_sy"));
1401 check(box_sy = cpl_parameter_get_int(p));
1403 check(name=cpl_frame_get_filename(raw_frm));
1404 check(ima=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
1406 check(name=cpl_frame_get_filename(*guess));
1407 check(tab=cpl_table_load(name,1,0));
1409 check(ngue=cpl_table_get_nrow(tab));
1410 check(cpl_table_new_column(tab,
"SELECT",CPL_TYPE_INT));
1411 check(cpl_table_new_column(tab,
"XF",CPL_TYPE_DOUBLE));
1412 check(cpl_table_new_column(tab,
"YF",CPL_TYPE_DOUBLE));
1414 check(cpl_table_fill_column_window(tab,
"SELECT",0,ngue,-1));
1415 check(cpl_table_fill_column_window(tab,
"XF",0,ngue,0));
1416 check(cpl_table_fill_column_window(tab,
"YF",0,ngue,0));
1418 check(pxg=cpl_table_get_data_double(tab,
"XC"));
1419 check(pyg=cpl_table_get_data_double(tab,
"YC"));
1420 check(pxf=cpl_table_get_data_double(tab,
"XF"));
1421 check(pyf=cpl_table_get_data_double(tab,
"YF"));
1422 check(ps=cpl_table_get_data_int(tab,
"SELECT"));
1425 for(i=0;i<ngue;i++) {
1428 if(cpl_image_fit_gaussian(ima,(
int)pxg[i],(
int)pyg[i],box_sy,
1432 &fwhm_x,&fwhm_y) == CPL_ERROR_NONE) {
1442 check(nfit=cpl_table_and_selected_int(tab,
"SELECT",CPL_GREATER_THAN,-1));
1445 check(tmp=cpl_table_extract_selected(tab));
1447 check(cpl_table_erase_column(tmp,
"XC"));
1448 check(cpl_table_erase_column(tmp,
"YC"));
1450 check(cpl_table_duplicate_column(tmp,
"XC",tmp,
"XF"));
1451 check(cpl_table_duplicate_column(tmp,
"YC",tmp,
"YF"));
1454 check(cpl_table_save(tmp, NULL, NULL, name_g, CPL_IO_DEFAULT));
1455 check(cpl_table_save(tmp, NULL, NULL, name, CPL_IO_DEFAULT));
1465 return CPL_ERROR_UNSPECIFIED;
1467 if( cpl_error_get_code() != CPL_ERROR_NONE) {
1470 return cpl_error_get_code() ;
1490 cpl_parameterlist* parameters,
const char*
method, cpl_frame* model_config,
1491 cpl_frame** guess,
int debug_level) {
1492 cpl_image* ima = NULL;
1496 cpl_table* tab_tmp1 = NULL;
1497 cpl_table* tab_tmp2 = NULL;
1498 cpl_table* tab_xy_corr = NULL;
1499 cpl_image* ima_ext = NULL;
1500 cpl_image* ima_raw = NULL;
1503 cpl_parameter* p = NULL;
1504 const char* name = NULL;
1505 const char* name_g = NULL;
1506 const char* name_t =
"tab_xy_peaks.fits";
1509 cpl_table* tab_xy_guess=NULL;
1510 cpl_matrix* mat_gue = NULL;
1511 cpl_matrix* mat_msr = NULL;
1512 double thresh_min = 25;
1514 int use_pattern = 0;
1518 double yg_temp[20000];
1519 double xg_temp[20000];
1520 double* wave = NULL;
1531 cpl_vector* spectrum = NULL;
1532 cpl_vector* filtered = NULL;
1533 cpl_vector* spec_clean = NULL;
1534 cpl_vector* bright_lines = NULL;
1543 cpl_vector** trace = NULL;
1544 cpl_vector** extracted = NULL;
1545 int MAX_NO_ORD = 16;
1546 struct xs_3 xs_3_config;
1547 struct xs_3* p_xs_3 = NULL;
1549 cpl_table* spec_form_tab = NULL;
1555 double* detec = NULL;
1561 cpl_frame* spec_form_frame = NULL;
1564 cpl_table* tab_tmp = NULL;
1565 cpl_table* tab_xy_peaks = NULL;
1566 cpl_table* tab_xy_peaks_sel = NULL;
1567 double wave_use = 0, zero_off = 0, match_lim = 0, startlam = 0, endlam = 0;
1569 p_xs_3 = &xs_3_config;
1573 xsh_msg(
"Calling spectral format create");
1578 "not_spec_form.fits"));
1580 cpl_frame_set_tag(spec_form_frame,
1583 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.method"));
1589 if (strcmp(
method,
"gfit") == 0) {
1594 }
else if (strcmp(
method,
"pbrfit") == 0 || strcmp(
method,
"safefit") == 0) {
1596 if (strcmp(
method,
"safefit") == 0) {
1599 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.offx"));
1600 check(
offx = cpl_parameter_get_double(p));
1601 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.offy"));
1602 check(
offy = cpl_parameter_get_double(p));
1609 spec_form_tab = cpl_table_load(cpl_frame_get_filename(spec_form_frame), 1,
1613 check(name = cpl_frame_get_filename(raw_frm));
1614 check(ima_raw = cpl_image_load(name, CPL_TYPE_DOUBLE, 0, 0));
1615 check(size_y = cpl_image_get_size_y(ima_raw));
1616 check(size_x = cpl_image_get_size_x(ima_raw));
1617 xsh_msg(
"size_x=%d size_y=%d", size_x, size_y);
1618 check(detec = cpl_image_get_data_double(ima_raw));
1621 check(extracted = cpl_malloc(MAX_NO_ORD *
sizeof(cpl_vector *)));
1622 for (ii = 0; ii < MAX_NO_ORD; ii++) {
1623 extracted[ii] = cpl_vector_new(size_y);
1624 cpl_vector_fill(extracted[ii], 0.0);
1630 while (cpl_table_get_int(spec_form_tab,
"ORDER", starti, &status)
1638 absord = cpl_table_get_int(spec_form_tab,
"ORDER", ii, &status);
1639 if (p_xs_3->
arm != 2) {
1640 starty = (int) (cpl_table_get_float(spec_form_tab,
"DISP_MIN", ii,
1642 endy = (int) (cpl_table_get_float(spec_form_tab,
"DISP_MAX", ii,
1645 starty = (int) (cpl_table_get_float(spec_form_tab,
"DISP_MAX", ii,
1647 endy = (int) (cpl_table_get_float(spec_form_tab,
"DISP_MIN", ii,
1659 size_y_use = size_y;
1662 size_y_use = size_y -
offy;
1665 for (jj =
starty; ((jj <
endy) && (jj < size_y_use)); jj++) {
1668 dx = cpl_vector_get(trace[absord - p_xs_3->
morder_min], jj +
offy)
1670 for (kk = -h; kk <= h; kk++) {
1671 if (p_xs_3->
arm == 2) {
1672 pix = size_x * jj + ((int) (dx + 0.5) + kk);
1674 pix = size_x * jj + (int) (dx + 0.5) + kk;
1676 if (
pix < size_x * size_y) {
1681 cpl_vector_set(extracted[absord - p_xs_3->
morder_min], jj, tot));
1688 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.slit"));
1692 p = cpl_parameterlist_find(parameters,
1693 "xsh.xsh_cfg_recover.peak_factor"));
1694 check(fct = cpl_parameter_get_double(p));
1697 p = cpl_parameterlist_find(parameters,
1698 "xsh.xsh_cfg_recover.peak_match_x"));
1699 check(thresh_x = cpl_parameter_get_int(p));
1702 p = cpl_parameterlist_find(parameters,
1703 "xsh.xsh_cfg_recover.peak_match_y"));
1704 check(thresh_y = cpl_parameter_get_int(p));
1707 p = cpl_parameterlist_find(parameters,
1708 "xsh.xsh_cfg_recover.ima_thresh_min"));
1709 check(thresh_min = cpl_parameter_get_double(p));
1719 cpl_table* tab_xy_guess = NULL;
1721 check(name_g = cpl_frame_get_filename(*guess));
1722 check(tab_xy_guess = cpl_table_load(name_g, 1, 0));
1723 xsh_msg(
"guess tablename=%s", name_g);
1726 nrow = cpl_table_get_nrow(tab_xy_guess);
1728 cpl_table_new_column(tab_xy_guess,
"XF", CPL_TYPE_DOUBLE);
1729 cpl_table_new_column(tab_xy_guess,
"YF", CPL_TYPE_DOUBLE);
1730 cpl_table_new_column(tab_xy_guess,
"SELECT", CPL_TYPE_INT);
1731 cpl_table_fill_column_window(tab_xy_guess,
"XF", 0, nrow, -1);
1732 cpl_table_fill_column_window(tab_xy_guess,
"YF", 0, nrow, -1);
1733 cpl_table_fill_column_window(tab_xy_guess,
"SELECT", 0, nrow, -1);
1735 pxc = cpl_table_get_data_double(tab_xy_guess,
"XF");
1736 pyc = cpl_table_get_data_double(tab_xy_guess,
"YF");
1741 for (kk = 0; kk < nrow; kk++) {
1744 for (kk = 0; kk < nrow; kk++) {
1754 absord = cpl_table_get_int(spec_form_tab,
"ORDER", ord + starti,
1756 if (p_xs_3->
arm != 2) {
1758 starty = (
int) (cpl_table_get_float(spec_form_tab,
"DISP_MIN",
1759 ord + starti, &status)));
1761 endy = (
int) (cpl_table_get_float(spec_form_tab,
"DISP_MAX",
1762 ord + starti, &status)));
1765 starty = (
int) (cpl_table_get_float(spec_form_tab,
"DISP_MAX",
1766 ord + starti, &status)));
1768 endy = (
int) (cpl_table_get_float(spec_form_tab,
"DISP_MIN",
1769 ord + starti, &status)));
1772 startlam = cpl_table_get_float(spec_form_tab,
"LFSR", ord + starti,
1775 endlam = cpl_table_get_float(spec_form_tab,
"UFSR", ord + starti,
1778 if (strcmp(
method,
"safefit") == 0) {
1779 spectrum = extracted[ord];
1782 if ((filtered = cpl_vector_filter_median_create(spectrum, 200))
1784 cpl_msg_error(__func__,
"Cannot filter the spectrum");
1785 return CPL_ERROR_ILLEGAL_INPUT;
1787 spec_clean = cpl_vector_duplicate(spectrum);
1788 cpl_vector_subtract(spec_clean, filtered);
1789 cpl_vector_delete(filtered);
1795 check(spectrum = cpl_vector_new_from_image_row(ima_ext, ord + 1));
1796 spec_clean = cpl_vector_duplicate(spectrum);
1801 check(pxg = cpl_table_get_data_double(tab_xy_guess,
"XC"));
1802 check(pyg = cpl_table_get_data_double(tab_xy_guess,
"YC"));
1803 check(wave = cpl_table_get_data_double(tab_xy_guess,
"WAVELENGTH"));
1804 check(ordg = cpl_table_get_data_int(tab_xy_guess,
"ABS_ORD"));
1805 check(psl = cpl_table_get_data_int(tab_xy_guess,
"SELECT"));
1818 double prec = 0, precfac = 0, precmin = 0;
1820 int lmax = 0, rmax = 0;
1821 int nbright_lines = 0;
1824 cpl_table* tab_xy_guess = NULL;
1827 if (bright_lines != NULL) {
1828 check(nbright_lines = cpl_vector_get_size(bright_lines));
1829 double cent = 0, cent2 = 0;
1830 for (ii = 0; ii < nbright_lines; ii++) {
1831 cent = cpl_vector_get(bright_lines, ii) + 0.5 - 1.0;
1840 if (lines_dev == 1) {
1847 }
else if (lines_dev == 2) {
1854 }
else if (lines_dev == 3) {
1861 }
else if (lines_dev == 4) {
1868 }
else if (lines_dev == 5) {
1875 }
else if (lines_dev == 6) {
1894 prec = (float) (w) / precfac;
1895 if (prec < precmin) {
1902 for (jj = (
int) (cent) - wmin; jj <= (int) (cent) + w; jj++) {
1904 if (jj > 0 && jj <
endy) {
1905 ex_cor = cpl_vector_get(spec_clean, jj);
1908 cent2 += ex_cor * ((float) (jj) + 0.5 - cent);
1916 if (fabs(cent2 - cent) < prec) {
1928 prec = (float) (w) / precfac;
1929 if (prec < precmin) {
1936 for (jj = (
int) (cent) - w; jj <= (int) (cent) + wmin; jj++) {
1937 if (jj > 0 && jj <
endy) {
1938 ex_cor = cpl_vector_get(spec_clean, jj);
1941 cent2 += ex_cor * ((float) (jj) + 0.5 - cent);
1948 if (fabs(cent2 - cent) < prec) {
1959 if (lmax >= min_iso && rmax >= min_iso) {
1962 if (strcmp(
method,
"safefit") == 0) {
1964 for (jj = 0; jj < cpl_table_get_nrow(tab_xy_guess); jj++) {
1972 if (lines_dev > 0 && fabs(
y - cent + zero_off) < match_lim) {
1975 wave_use = wave[jj];
1977 if (abs(
y - cent) < (
float) (min_iso) / 2.0) {
1986 for (ll = -h; ll <= h; ll++) {
1988 for (kk = -1; kk <= 1; kk++) {
1993 pix = size_x * ((int) (cent + 0.5) + kk)
1994 + (
int) (
x + 0.5) + ll;
1996 pix = size_x * ((int) (cent + 0.5) + kk)
1997 + (
int) (
x + 0.5) + ll + 15;
2002 cent2 += tot * ((float) (ll) + 0.5);
2004 if (fabs(cent2 / total) < 3.0) {
2005 cent2 =
x + (cent2 / total);
2011 xg_temp[jj] = cent2;
2015 if (nb_match == 1 && lines_dev > 0) {
2016 if (wave_use > startlam && wave_use < endlam) {
2019 printf(
"%lf 100.0 %d tot%d\n", wave_use, min_iso, cnt);
2023 for (jj = 0; jj < cpl_table_get_nrow(tab_xy_guess); jj++) {
2026 if (abs(pyg[jj] - cent) < (float) (min_iso / 2)) {
2044 for (jj = 0; jj < cpl_table_get_nrow(tab_xy_guess); jj++) {
2045 if (yg_temp[jj] > 0.0) {
2046 pyg[jj] = yg_temp[jj];
2047 pxg[jj] = xg_temp[jj];
2054 check(cpl_table_and_selected_int(tab_xy_guess,
"SELECT",
2055 CPL_GREATER_THAN, -1));
2056 check(tab_tmp = cpl_table_extract_selected(tab_xy_guess));
2058 check(cpl_table_erase_column(tab_tmp,
"XC"));
2059 check(cpl_table_erase_column(tab_tmp,
"YC"));
2061 check(cpl_table_duplicate_column(tab_tmp,
"XC", tab_tmp,
"XF"));
2062 check(cpl_table_duplicate_column(tab_tmp,
"YC", tab_tmp,
"YF"));
2064 check(cpl_table_save( tab_tmp, NULL, NULL, name_g, CPL_IO_DEFAULT));
2070 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.slit"));
2074 p = cpl_parameterlist_find(parameters,
2075 "xsh.xsh_cfg_recover.peak_factor"));
2076 check(fct = cpl_parameter_get_double(p));
2079 p = cpl_parameterlist_find(parameters,
2080 "xsh.xsh_cfg_recover.peak_match_x"));
2081 check(thresh_x = cpl_parameter_get_int(p));
2084 p = cpl_parameterlist_find(parameters,
2085 "xsh.xsh_cfg_recover.peak_match_y"));
2086 check(thresh_y = cpl_parameter_get_int(p));
2089 p = cpl_parameterlist_find(parameters,
2090 "xsh.xsh_cfg_recover.ima_thresh_min"));
2091 check(thresh_min = cpl_parameter_get_double(p));
2101 cpl_table* tab_xy_trace=NULL;
2107 cpl_table_duplicate_column(tab_xy_peaks,
"ABS_ORD", tab_xy_peaks,
2110 check(name_g = cpl_frame_get_filename(*guess));
2111 check(tab_xy_guess = cpl_table_load(name_g, 1, 0));
2112 xsh_msg(
"guess table name=%s", name_g);
2114 check(name = cpl_frame_get_filename(order_tab_centr));
2115 check(tab_xy_trace = cpl_table_load(name, 1, 0));
2116 xsh_msg(
"Trace table name=%s", name);
2118 check(ord_min = cpl_table_get_column_min(tab_xy_trace,
"ABSORDER"));
2119 check(cpl_table_add_scalar(tab_xy_peaks,
"ABS_ORD", ord_min));
2122 check(cpl_table_save( tab_xy_peaks, NULL, NULL, name_t, CPL_IO_DEFAULT));
2129 check(cpl_table_save( tab_xy_peaks, NULL, NULL, name_t, CPL_IO_DEFAULT));
2135 xsh_msg(
"Peaks selection fct=%g", fct);
2138 tab_xy_peaks, fct));
2140 xsh_msg(
"No peaks selection");
2141 tab_xy_peaks_sel = cpl_table_duplicate(tab_xy_peaks);
2146 cpl_table_save(tab_xy_peaks_sel, NULL, NULL,
"tab_xy_peaks_sel.fits",
2152 if ((strcmp(
method,
"pm") == 0) || (strcmp(
method,
"manual") == 0)) {
2155 parameters, &mat_gue, &mat_msr, &use_pattern, &use_data));
2159 use_pattern, use_data, debug_level));
2164 xsh_msg(
"Remove blended lines");
2169 ngue = cpl_table_get_nrow(tab_xy_guess);
2170 npck = cpl_table_get_nrow(tab_xy_peaks_sel);
2174 tab_xy_peaks_sel, thresh_x, thresh_y));
2178 cpl_table_save(tab_xy_corr, NULL, NULL,
"tab_xy_corr.fits",
2183 xsh_msg_error(
"Number og guess lines more than found peaks!");
2185 xsh_msg_error(
"or choose a reference line table with less entries");
2186 return CPL_ERROR_ILLEGAL_INPUT;
2191 cleanup:
if (trace != NULL) {
2192 for (ii = 0; ii < MAX_NO_ORD; ii++) {
2199 if (extracted != NULL) {
2200 for (ii = 0; ii < MAX_NO_ORD; ii++) {
2203 cpl_free(extracted);
2218 cpl_vector_delete(spec_clean);
2219 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2222 return cpl_error_get_code();
2241static cpl_error_code
2244 cpl_parameterlist* parameters,
2245 cpl_matrix** mat_pat,
2246 cpl_matrix** mat_dat,
2273 cpl_parameter* p=NULL;
2274 cpl_table* pat=NULL;
2275 cpl_table* dat=NULL;
2276 cpl_table* tmp=NULL;
2285 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.pm_ord_sel"));
2286 check(ord_sel = cpl_parameter_get_int(p));
2288 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.pm_radius"));
2289 check(rad = cpl_parameter_get_double(p));
2292 check(npat=cpl_table_get_nrow(tab_pat));
2293 check(ndat=cpl_table_get_nrow(tab_dat));
2299 check(pat=cpl_table_duplicate(tab_pat));
2300 check(cpl_table_new_column(pat,
"SEL",CPL_TYPE_INT));
2301 check(cpl_table_fill_column_window(pat,
"SEL",0,npat,-1));
2304 check(ps=cpl_table_get_data_int(pat,
"SEL"));
2305 check(pog=cpl_table_get_data_int(pat,
"ABS_ORD"));
2307 check(ord_min=cpl_table_get_column_min(pat,
"ABS_ORD"));
2308 check(ord_max=cpl_table_get_column_max(pat,
"ABS_ORD"));
2311 check(ord_min_dat=cpl_table_get_column_min(tab_dat,
"ABS_ORD"));
2312 check(ord_max_dat=cpl_table_get_column_max(tab_dat,
"ABS_ORD"));
2314 xsh_msg(
"pat min=%d max=%d",ord_min,ord_max);
2315 xsh_msg(
"dat min=%d max=%d",ord_min_dat,ord_max_dat);
2321 for(ord=ord_min;ord<=ord_max;ord++) {
2322 for(i=0;i<npat;i++) {
2323 if((pog[i]==ord) && (j< ord_sel)) {
2330 check(*use_pat=cpl_table_and_selected_int(pat,
"SEL",CPL_GREATER_THAN,0));
2331 check(tmp=cpl_table_extract_selected(pat));
2333 check(cpl_table_save(pat,NULL,NULL,
"pat.fits",CPL_IO_DEFAULT));
2336 check(dat=cpl_table_duplicate(tab_dat));
2337 check(cpl_table_new_column(dat,
"SEL",CPL_TYPE_INT));
2338 check(cpl_table_fill_column_window(dat,
"SEL",0,ndat,-1));
2339 check(ps=cpl_table_get_data_int(dat,
"SEL"));
2341 check(pod=cpl_table_get_data_int(dat,
"ABS_ORD"));
2342 check(pxd=cpl_table_get_data_double(dat,
"XP"));
2343 check(pyd=cpl_table_get_data_double(dat,
"YP"));
2345 check(pog=cpl_table_get_data_int(tmp,
"ABS_ORD"));
2346 check(pxg=cpl_table_get_data_double(tmp,
"XC"));
2347 check(pyg=cpl_table_get_data_double(tmp,
"YC"));
2350 for(i=0;i<*use_pat;i++) {
2355 for(j=0;j<ndat;j++) {
2356 if( pog[i] == pod[j] ) {
2357 if(pxd[j] >= xmin && pxd[j] <= xmax) {
2358 if(pyd[j] >= ymin && pyd[j] <= ymax ) {
2365 check(cpl_table_save(dat,NULL,NULL,
"dat.fits",CPL_IO_DEFAULT));
2366 check(*use_dat=cpl_table_and_selected_int(dat,
"SEL",CPL_GREATER_THAN,0));
2368 xsh_msg(
"use_pattern=%d use_data=%d",*use_pat,*use_dat);
2369 if(*use_pat > *use_dat) {
2370 xsh_msg_error(
"You try to find a matching pattern searching ");
2371 xsh_msg_error(
"more points than available data. Try to increase ");
2373 xsh_msg_error(
"and/or decrease pm_ord_sel parameter values");
2379 check(*mat_pat=cpl_matrix_new(2,npat));
2380 check(*mat_dat=cpl_matrix_new(2,ndat));
2382 check(pxg=cpl_table_get_data_double(pat,
"XC"));
2383 check(pyg=cpl_table_get_data_double(pat,
"YC"));
2384 for(i=0;i<npat;i++) {
2386 check(cpl_matrix_set(*mat_pat,0,i,pxg[i]));
2387 check(cpl_matrix_set(*mat_pat,1,i,pyg[i]));
2391 check(pxd=cpl_table_get_data_double(dat,
"XP"));
2392 check(pyd=cpl_table_get_data_double(dat,
"YP"));
2393 for(i=0;i<ndat;i++) {
2395 check(cpl_matrix_set(*mat_dat,0,i,pxd[i]));
2396 check(cpl_matrix_set(*mat_dat,1,i,pyd[i]));
2407 return cpl_error_get_code() ;
2428static cpl_error_code
2430 cpl_matrix* mat_gue,
2431 cpl_matrix* mat_dat,
2436 cpl_array *matches = NULL;
2439 double err_pattern=0;
2442 cpl_matrix* mdata=NULL;
2443 cpl_matrix* mpattern=NULL;
2444 cpl_parameter* p=NULL;
2445 cpl_table* pm_tbl=NULL;
2446 cpl_table* dat_tbl=NULL;
2447 cpl_table*gue_tbl=NULL;
2457 double lin_scale=1.0;
2458 double lin_angle=0.0;
2461 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.pm_tolerance"));
2464 check(p = cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.pm_radius"));
2465 check(radius = cpl_parameter_get_double(p));
2469 ndat=cpl_matrix_get_ncol(mat_dat);
2470 ngue=cpl_matrix_get_ncol(mat_gue);
2472 check(dat_tbl=cpl_table_new(ndat));
2473 check(gue_tbl=cpl_table_new(ngue));
2475 check(cpl_table_new_column(dat_tbl,
"XD",CPL_TYPE_DOUBLE));
2476 check(cpl_table_new_column(dat_tbl,
"YD",CPL_TYPE_DOUBLE));
2477 check(cpl_table_fill_column_window(dat_tbl,
"XD",0,ndat,0));
2478 check(cpl_table_fill_column_window(dat_tbl,
"YD",0,ndat,0));
2479 check(pcatx=cpl_table_get_data_double(dat_tbl,
"XD"));
2480 check(pcaty=cpl_table_get_data_double(dat_tbl,
"YD"));
2484 for(i=0;i<ndat;i++) {
2486 check(pcatx[i]=cpl_matrix_get(mat_dat,0,i));
2487 check(pcaty[i]=cpl_matrix_get(mat_dat,1,i));
2493 check(cpl_table_save(dat_tbl,NULL,NULL,
"pm_dat.fits",CPL_IO_DEFAULT));
2496 check(cpl_table_new_column(gue_tbl,
"XG",CPL_TYPE_DOUBLE));
2497 check(cpl_table_new_column(gue_tbl,
"YG",CPL_TYPE_DOUBLE));
2498 check(cpl_table_fill_column_window(gue_tbl,
"XG",0,ngue,0));
2499 check(cpl_table_fill_column_window(gue_tbl,
"YG",0,ngue,0));
2500 check(ppatx=cpl_table_get_data_double(gue_tbl,
"XG"));
2501 check(ppaty=cpl_table_get_data_double(gue_tbl,
"YG"));
2504 for(i=0;i<ngue;i++) {
2506 check(ppatx[i]=cpl_matrix_get(mat_gue,0,i));
2507 check(ppaty[i]=cpl_matrix_get(mat_gue,1,i));
2513 check(cpl_table_save(gue_tbl,NULL,NULL,
"pm_gue.fits",CPL_IO_DEFAULT));
2515 check(matches = cpl_ppm_match_points(mat_dat,use_data,err_data,
2516 mat_gue,use_pattern,err_pattern,
2518 &lin_scale,&lin_angle));
2520 check(nmatch=cpl_matrix_get_ncol(mpattern));
2521 xsh_msg(
"%d points matched linear scale factor=%f linear angle factor %f",
2522 nmatch,lin_scale,lin_angle);
2524 check(pm_tbl=cpl_table_new(nmatch));
2525 check(cpl_table_new_column(pm_tbl,
"PM_CAT_X",CPL_TYPE_DOUBLE));
2526 check(cpl_table_new_column(pm_tbl,
"PM_CAT_Y",CPL_TYPE_DOUBLE));
2527 check(cpl_table_new_column(pm_tbl,
"PM_PAT_X",CPL_TYPE_DOUBLE));
2528 check(cpl_table_new_column(pm_tbl,
"PM_PAT_Y",CPL_TYPE_DOUBLE));
2530 check(cpl_table_fill_column_window(pm_tbl,
"PM_CAT_X",0,nmatch,0));
2531 check(cpl_table_fill_column_window(pm_tbl,
"PM_CAT_Y",0,nmatch,0));
2532 check(cpl_table_fill_column_window(pm_tbl,
"PM_PAT_X",0,nmatch,0));
2533 check(cpl_table_fill_column_window(pm_tbl,
"PM_PAT_Y",0,nmatch,0));
2536 check(ppatx=cpl_table_get_data_double(pm_tbl,
"PM_CAT_X"));
2537 check(ppaty=cpl_table_get_data_double(pm_tbl,
"PM_CAT_Y"));
2539 check(pcatx=cpl_table_get_data_double(pm_tbl,
"PM_PAT_X"));
2540 check(pcaty=cpl_table_get_data_double(pm_tbl,
"PM_PAT_Y"));
2542 for(i=0;i<nmatch;i++) {
2544 check(pcatx[i]=cpl_matrix_get(mdata,0,i));
2545 check(pcaty[i]=cpl_matrix_get(mdata,1,i));
2547 check(ppatx[i]=cpl_matrix_get(mpattern,0,i));
2548 check(ppaty[i]=cpl_matrix_get(mpattern,1,i));
2554 check(cpl_table_save(pm_tbl,NULL,NULL,
2555 "pattern_match.fits",CPL_IO_DEFAULT));
2565 if(cpl_error_get_code() != CPL_ERROR_NONE) {
2566 xsh_msg(
"Try to increase pm_radius and/or peak_factor");
2568 return cpl_error_get_code() ;
2584static cpl_error_code
2587 cpl_table** tab_xy_peaks)
2601 check(
size=cpl_table_get_nrow(*tab_xy_peaks));
2602 check(cpl_table_new_column(*tab_xy_peaks,
"XP",CPL_TYPE_DOUBLE));
2603 check(cpl_table_fill_column_window(*tab_xy_peaks,
"XP",0,
size,-1));
2605 check(px=cpl_table_get_data_double(*tab_xy_peaks,
"XP"));
2606 check(py=cpl_table_get_data_double(*tab_xy_peaks,
"YP"));
2607 check(po=cpl_table_get_data_int(*tab_xy_peaks,
"ABS_ORD"));
2608 ord_min=cpl_table_get_column_min(*tab_xy_peaks,
"ABS_ORD");
2613 for(i=0;i<
size;i++) {
2617 ord_rel=po[i]-ord_min;
2621 if(cpl_polynomial_get_dimension(order_list->
list[ord_rel].
cenpoly) <
2624 check( px[i] = cpl_polynomial_eval_1d(
2635 return cpl_error_get_code();
2655 cpl_table* tab_xy_peaks_sel,
2691 cpl_table* tab_tmp=NULL;
2692 cpl_table* tab_tmp1=NULL;
2693 cpl_table* tab_tmp2=NULL;
2695 cpl_table* result=NULL;
2705 ord_min=cpl_table_get_column_min(tab_xy_guess,
"ABS_ORD");
2706 ord_max=cpl_table_get_column_max(tab_xy_guess,
"ABS_ORD");
2708 check(tab_tmp2=cpl_table_duplicate(tab_xy_peaks_sel));
2709 check(nrows=cpl_table_get_nrow(tab_tmp2));
2710 check(cpl_table_fill_column_window(tab_tmp2,
"YP",0,nrows,-1));
2711 check(cpl_table_fill_column_window(tab_tmp2,
"XP",0,nrows,-1));
2712 check(cpl_table_fill_column_window(tab_tmp2,
"REL_ORD",0,nrows,-1));
2713 check(cpl_table_fill_column_window(tab_tmp2,
"ABS_ORD",0,nrows,-1));
2714 check(cpl_table_fill_column_window(tab_tmp2,
"WEIGHT",0,nrows,-1));
2716 check(pxr=cpl_table_get_data_double(tab_tmp2,
"XP"));
2717 check(pyr=cpl_table_get_data_double(tab_tmp2,
"YP"));
2718 check(prr=cpl_table_get_data_int(tab_tmp2,
"REL_ORD"));
2719 check(pwr=cpl_table_get_data_int(tab_tmp2,
"WEIGHT"));
2720 check(par=cpl_table_get_data_int(tab_tmp2,
"ABS_ORD"));
2722 for(ord=ord_min;ord<=ord_max;ord++) {
2723 check(nord=cpl_table_and_selected_int(tab_xy_guess,
"ABS_ORD",
2725 check(npks=cpl_table_and_selected_int(tab_xy_peaks_sel,
"ABS_ORD",
2728 check(tab_tmp=cpl_table_extract_selected(tab_xy_guess));
2730 check(tab_tmp1=cpl_table_extract_selected(tab_xy_peaks_sel));
2732 pxt=cpl_table_get_data_double(tab_tmp,
"XC");
2733 pyt=cpl_table_get_data_double(tab_tmp,
"YC");
2735 pxp=cpl_table_get_data_double(tab_tmp1,
"XP");
2736 pyp=cpl_table_get_data_double(tab_tmp1,
"YP");
2737 prp=cpl_table_get_data_int(tab_tmp1,
"REL_ORD");
2738 pwp=cpl_table_get_data_int(tab_tmp1,
"WEIGHT");
2739 pap=cpl_table_get_data_int(tab_tmp1,
"ABS_ORD");
2742 for(i=0;i<nord;i++) {
2744 for(j=0;j<npks;j++) {
2746 if((fabs(pxp[j]-pxt[i]) < thresh_x) &&
2747 (fabs(pyp[j]-pyt[i]) < thresh_y)) {
2767 check(cpl_table_select_all(tab_xy_peaks_sel));
2768 check(cpl_table_select_all(tab_xy_guess));
2771 check(cpl_table_and_selected_int(tab_tmp2,
"REL_ORD",CPL_GREATER_THAN,-1));
2772 check(result=cpl_table_extract_selected(tab_tmp2));
2773 check(cpl_table_name_column(result,
"XP",
"XC"));
2774 check(cpl_table_name_column(result,
"YP",
"YC"));
2782 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2804 cpl_table* tab_xy_peaks,
2805 const double factor)
2813 int ord_min_peaks=0;
2814 int ord_max_peaks=0;
2815 int ord_min_guess=0;
2816 int ord_max_guess=0;
2819 cpl_table* tab_tmp=NULL;
2820 cpl_table* tab_tmp1=NULL;
2821 cpl_table* result=NULL;
2825 check(ord_min_guess=cpl_table_get_column_min(tab_xy_guess,
"ABS_ORD"));
2826 check(ord_max_guess=cpl_table_get_column_max(tab_xy_guess,
"ABS_ORD"));
2828 check(ord_min_peaks=cpl_table_get_column_min(tab_xy_peaks,
"ABS_ORD"));
2829 check(ord_max_peaks=cpl_table_get_column_max(tab_xy_peaks,
"ABS_ORD"));
2830 ord_min=(ord_min_guess<=ord_min_peaks) ? ord_min_guess : ord_min_peaks;
2831 ord_max=(ord_max_guess>=ord_max_peaks) ? ord_max_guess : ord_max_peaks;
2833 cpl_table_select_all(tab_xy_peaks);
2835 xsh_msg(
"select lines in order range=[%d,%d]",ord_min,ord_max);
2838 for(ord=ord_min;ord<=ord_max;ord++) {
2840 nord=cpl_table_and_selected_int(tab_xy_guess,
"ABS_ORD",CPL_EQUAL_TO,ord);
2841 npks=cpl_table_and_selected_int(tab_xy_peaks,
"ABS_ORD",CPL_EQUAL_TO,ord);
2842 check(tab_tmp=cpl_table_extract_selected(tab_xy_peaks));
2845 weight_min=cpl_table_get_column_min(tab_tmp,
"WEIGHT");
2846 check(npks=cpl_table_and_selected_int(tab_tmp,
"WEIGHT",
2847 CPL_NOT_GREATER_THAN,
2848 (
int)(weight_min+factor*nord)));
2849 tab_tmp1=cpl_table_extract_selected(tab_tmp);
2854 tab_tmp1=cpl_table_duplicate(tab_tmp);
2856 if(ord == ord_min) {
2857 result=cpl_table_duplicate(tab_tmp1);
2859 nrows=cpl_table_get_nrow(tab_xy_peaks);
2860 cpl_table_insert(result,tab_tmp1,nrows);
2865 cpl_table_select_all(tab_xy_peaks);
2866 cpl_table_select_all(tab_xy_guess);
2873 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2895 cpl_parameterlist* parameters)
2898 const char* name_p=
"ima_peaks.fits";
2899 const int size=5000;
2900 cpl_table* result=NULL;
2901 cpl_propertylist* plist=NULL;
2912 cpl_vector* spectrum=NULL;
2913 cpl_vector* peaks=NULL;
2917 cpl_parameter* p=NULL;
2919 cpl_table* tab_tmp=NULL;
2923 check(p=cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.plot"));
2924 check(display = cpl_parameter_get_bool(p));
2926 check(p=cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.peak_line_fwhm"));
2927 check(line_fwhm = cpl_parameter_get_int(p));
2929 check(p=cpl_parameterlist_find(parameters,
"xsh.xsh_cfg_recover.peak_kappa"));
2932 ord_num=cpl_image_get_size_y(ima_ext);
2934 result=cpl_table_new(
size);
2935 cpl_table_new_column(result,
"YP",CPL_TYPE_DOUBLE);
2936 cpl_table_new_column(result,
"REL_ORD",CPL_TYPE_INT);
2937 check(cpl_table_fill_column_window(result,
"YP",0,
size,-1));
2938 check(cpl_table_fill_column_window(result,
"REL_ORD",0,
size,-1));
2940 pp=cpl_table_get_data_double(result,
"YP");
2941 po=cpl_table_get_data_int(result,
"REL_ORD");
2944 for(ord=0;ord<ord_num;ord++) {
2945 check(spectrum=cpl_vector_new_from_image_row(ima_ext,ord+1));
2948 plist=cpl_propertylist_new();
2949 sprintf(ext_val,
"%s%d",
"ext",ord);
2950 cpl_propertylist_append_string(plist,
"EXTNAME",ext_val);
2952 cpl_vector_save(spectrum,name_p,CPL_BPP_IEEE_FLOAT,plist,
2955 cpl_vector_save(spectrum,name_p,CPL_BPP_IEEE_FLOAT,
2956 plist,CPL_IO_EXTEND);
2961 check(sv=cpl_vector_get_size(peaks));
2963 check(pv=cpl_vector_get_data(peaks));
2974 check(cpl_table_and_selected_double(result,
"YP",CPL_GREATER_THAN,-1));
2975 check(tab_tmp=cpl_table_extract_selected(result));
2977 result=cpl_table_duplicate(tab_tmp);
2980 check(cpl_table_erase_invalid_rows(result));
2982 cpl_table_new_column(result,
"WEIGHT",CPL_TYPE_INT);
2983 nrows=cpl_table_get_nrow(result);
2985 check(cpl_table_fill_column_window(result,
"WEIGHT",0,
size,-1));
2986 check(pw=cpl_table_get_data_int(result,
"WEIGHT"));
2988 for(i=0;i<nrows;i++) {
2997 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3021 cpl_frame* order_tab_centr,
3027 const char* name=NULL;
3028 cpl_image* input=NULL;
3030 const char* name_o=
"ima_raw.fits";
3031 const char* name_e=
"ima_ext.fits";
3040 cpl_image* result=NULL;
3050 cpl_table* tab_centr=NULL;
3052 check(name=cpl_frame_get_filename(raw_frm));
3053 check(input=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0));
3054 check(cpl_image_threshold(input,min,FLT_MAX,0,FLT_MAX));
3055 check(cpl_image_save(input,name_o,CPL_BPP_IEEE_FLOAT,NULL,
3058 xsh_msg(
"extracting image %s",name_o);
3060 check(sx=cpl_image_get_size_x(input));
3061 check(sy=cpl_image_get_size_y(input));
3062 check(name=cpl_frame_get_filename( order_tab_centr));
3063 check(tab_centr=cpl_table_load(name,1,0));
3065 ord_min_tra=cpl_table_get_column_min(tab_centr,
"ABSORDER");
3066 ord_max_tra=cpl_table_get_column_max(tab_centr,
"ABSORDER");
3069 ord_num=ord_max_tra-ord_min_tra+1;
3072 check(result=cpl_image_new(sy,ord_num,CPL_TYPE_DOUBLE));
3073 check(pou=cpl_image_get_data_double(result));
3074 check(pin=cpl_image_get_data_double(input));
3082 for(ord=0;ord<ord_num;ord++) {
3085 if(cpl_polynomial_get_dimension(order_list->
list[ord].
cenpoly) <
3093 if ( (
pix>0) && (
pix<sx*sy) ) {
3103 check(cpl_image_save(result,name_e,CPL_BPP_IEEE_FLOAT,NULL,
3111 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3131static cpl_error_code
3133 cpl_frame* order_tab_centr,
3134 cpl_frame** model_xy_gue) {
3136 cpl_table* tab_xy_guess=NULL;
3158 const char* name_c=NULL;
3159 const char* name_g=NULL;
3161 cpl_table * tab_centr=NULL;
3167 check(name_g=cpl_frame_get_filename(*model_xy_gue));
3169 check(tab_xy_guess=cpl_table_load(name_g,1,0));
3171 check(
size=cpl_table_get_nrow(tab_xy_guess));
3172 check(cpl_table_new_column(tab_xy_guess,
"XC",CPL_TYPE_DOUBLE));
3173 check(cpl_table_new_column(tab_xy_guess,
"YC",CPL_TYPE_DOUBLE));
3174 check(cpl_table_fill_column_window(tab_xy_guess,
"XC",0,
size,-1));
3175 check(cpl_table_fill_column_window(tab_xy_guess,
"YC",0,
size,-1));
3177 check(pxc=cpl_table_get_data_double(tab_xy_guess,
"XC"));
3178 check(pyc=cpl_table_get_data_double(tab_xy_guess,
"YC"));
3179 check(px=cpl_table_get_data_double(tab_xy_guess,
"XG"));
3180 check(py=cpl_table_get_data_double(tab_xy_guess,
"YG"));
3181 check(pog=cpl_table_get_data_int(tab_xy_guess,
"ABS_ORD"));
3182 check(ord_min_gue=cpl_table_get_column_min(tab_xy_guess,
"ABS_ORD"));
3183 check(ord_max_gue=cpl_table_get_column_max(tab_xy_guess,
"ABS_ORD"));
3185 if (order_tab_centr!=NULL) {
3186 name_c=cpl_frame_get_filename(order_tab_centr);
3187 xsh_msg(
"tab centre file=%s",name_c);
3188 check(tab_centr=cpl_table_load(name_c,1,0));
3189 check(ord_min_tra=cpl_table_get_column_min(tab_centr,
"ABSORDER"));
3190 check(ord_max_tra=cpl_table_get_column_max(tab_centr,
"ABSORDER"));
3194 xsh_msg(
"Trace tab order min=%d max=%d",ord_min_tra,ord_max_tra);
3196 xsh_msg(
"Guess tab order min=%d max=%d",ord_min_gue,ord_max_gue);
3197 ord_min_off=ord_min_gue-ord_min_tra;
3198 ord_max_off=ord_max_gue-ord_max_tra;
3202 if(ord_min_off!=0) {
3203 xsh_msg_error(
"The guess line table has lines in an order not traced");
3204 xsh_msg_error(
"Remove lines from guess at order %d",ord_min_gue);
3205 xsh_msg_error(
"Or allow xsh_orderpos to trace order %d",ord_min_gue);
3206 xsh_msg_error(
"(For example changing XSH_SPECTRALFORMAT_TABLE_arm");
3207 cpl_error_set(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT);
3212 size,ord_min_gue,ord_max_gue);
3214 for(i=0;i<
size;i++) {
3220 ord_rel=pog[i]-ord_min_gue+ord_min_off;
3225 if(cpl_polynomial_get_dimension(order_list->
list[ord_rel].
cenpoly) <
3227 check( pxc[i] = cpl_polynomial_eval_1d(order_list->
list[ord_rel].
cenpoly,py[i],NULL));
3238 nord=ord_max_tra-ord_min_tra+1;
3240 check(tab_centr=cpl_table_new(nord*sizey));
3241 check(cpl_table_new_column(tab_centr,
"X",CPL_TYPE_DOUBLE));
3242 check(cpl_table_new_column(tab_centr,
"Y",CPL_TYPE_DOUBLE));
3243 check(cpl_table_new_column(tab_centr,
"ORDER",CPL_TYPE_INT));
3244 check(cpl_table_new_column(tab_centr,
"ABSORDER",CPL_TYPE_INT));
3245 check(cpl_table_fill_column_window(tab_centr,
"X",0,
size,-1));
3246 check(cpl_table_fill_column_window(tab_centr,
"Y",0,
size,-1));
3247 check(cpl_table_fill_column_window(tab_centr,
"ORDER",0,
size,-1));
3248 check(cpl_table_fill_column_window(tab_centr,
"ABSORDER",0,
size,-1));
3249 check(px=cpl_table_get_data_double(tab_centr,
"X"));
3250 check(py=cpl_table_get_data_double(tab_centr,
"Y"));
3251 check(por=cpl_table_get_data_int(tab_centr,
"ORDER"));
3252 check(poa=cpl_table_get_data_int(tab_centr,
"ABSORDER"));
3256 for(ord=0;ord<nord;ord++) {
3257 for(j=0;j<sizey;j++) {
3259 poa[i]=ord+ord_min_tra;
3267 check(cpl_table_save(tab_centr,NULL,NULL,
"tab_centr.fits",CPL_IO_DEFAULT));
3270 check(cpl_table_save(tab_xy_guess, NULL, NULL, name_g, CPL_IO_DEFAULT));
3277 if(cpl_error_get_code() != CPL_ERROR_NONE) {
3280 return cpl_error_get_code();
3296 cpl_frame * config_frame,
3300 cpl_table* lines_tab=NULL ;
3301 cpl_frame* result=NULL;
3302 struct xs_3* p_xs_3_config=NULL;
3303 struct xs_3 xs_3_config;
3304 cpl_table* lines_xyg=NULL;
3305 cpl_propertylist* header=NULL;
3309 sprintf(name_o,
"%s%s",tag,
".fits");
3313 p_xs_3_config=&xs_3_config;
3317 cpl_frame_get_filename(config_frame)) ;
3322 check(lines_tab = cpl_table_load(cpl_frame_get_filename(wave_list), 1, 0)) ;
3329 header=cpl_propertylist_new();
3331 cpl_table_save(lines_xyg, header, NULL,name_o, CPL_IO_DEFAULT);
3335 CPL_FRAME_GROUP_PRODUCT,
3336 CPL_FRAME_LEVEL_FINAL);
3345 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3366static cpl_error_code
3368 cpl_table * lines_tab,
3369 struct xs_3* p_xs_3_config,
3371 cpl_table ** lines_xyg)
3379 double lambda_min=0;
3380 double lambda_max=0;
3382 double m_to_mu=1.e6;
3388 lines_tot = cpl_table_get_nrow(lines_tab) ;
3389 xsh_msg(
"lines_tot=%d",lines_tot);
3390 *lines_xyg = cpl_table_new(lines_tot*3);
3391 cpl_table_new_column(*lines_xyg,
"WAVELENGTH",CPL_TYPE_DOUBLE);
3392 cpl_table_new_column(*lines_xyg,
"XG",CPL_TYPE_DOUBLE);
3393 cpl_table_new_column(*lines_xyg,
"YG",CPL_TYPE_DOUBLE);
3394 cpl_table_new_column(*lines_xyg,
"ABS_ORD",CPL_TYPE_INT);
3396 pw=cpl_table_get_data_float(lines_tab,
"WAVELENGTH");
3397 xsh_msg(
"order min:%d max: %d",
3400 for(i=0;i<lines_tot;i++) {
3404 blaze_wav=2*(sin(-p_xs_3_config->
nug))/(morder_cnt*p_xs_3_config->
sg);
3405 lambda_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
3406 lambda_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
3407 lambda_min *=m_to_mu;
3408 lambda_max *=m_to_mu;
3411 if(pw[i]> lambda_min*0.98 && pw[i]<1.02*lambda_max) {
3413 morder_cnt,0.0,&
x,&
y));
3416 cpl_table_set_double(*lines_xyg,
"WAVELENGTH",row,pw[i]);
3417 cpl_table_set_int(*lines_xyg,
"ABS_ORD",row,morder_cnt);
3418 cpl_table_set_double(*lines_xyg,
"XG",row,
x-prescan);
3419 cpl_table_set_double(*lines_xyg,
"YG",row,
y);
3435 cpl_table_erase_invalid(*lines_xyg);
3437 xsh_msg(
"lines inc. overlap: %" CPL_SIZE_FORMAT
"\n",cpl_table_get_nrow(*lines_xyg));
3440 return cpl_error_get_code();
static xsh_instrument * instrument
static cpl_error_code xsh_cfg_recover_gen_xyg(cpl_table *lines_tab, struct xs_3 *p_xs_3_config, xsh_instrument *inst, int pre_scan, cpl_table **lines_gue)
static cpl_error_code xsh_cfg_recover_guess_tab_corr_by_ordpos(xsh_instrument *instr, cpl_frame *order_tab_centr, cpl_frame **model_xy_gue)
static cpl_error_code xsh_cfg_recover_measure_line_xy_fit(cpl_frame *raw_frm, cpl_parameterlist *parameters, cpl_frame **model_xy_gue, int debug_level)
static cpl_error_code xsh_cfg_recover_guess_tab_corr_by_user(cpl_parameterlist *parameters, cpl_frame **model_xy_gue)
static int xsh_cfg_recover_create(cpl_plugin *)
Setup the recipe options.
static cpl_error_code xsh_cfg_recover_extend_xy_pos_frm(cpl_frame **frm, xsh_instrument *instrument)
static cpl_error_code xsh_cfg_recover_measure_line_xy(cpl_frame *frame, xsh_instrument *inst, cpl_frame *order_tab_centr, cpl_parameterlist *parameters, const char *method, cpl_frame *model_config, cpl_frame **guess, int debug_level)
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static cpl_table * xsh_cfg_recover_remove_blends(cpl_table *tab_xy_guess, cpl_table *tab_xy_peaks_sel, const int thresh_x, const int thresh_y)
static cpl_table * xsh_cfg_recover_measure_tab_xy_peaks(cpl_image *ima_ext, cpl_parameterlist *parameters)
static cpl_error_code xsh_cfg_recover_driver(cpl_parameterlist *parameters, cpl_frameset *frameset)
static int xsh_cfg_recover_last_step(cpl_parameterlist *parameters, cpl_frameset *frameset, xsh_instrument *instrument, cpl_frameset *raws, cpl_frameset *calib)
Interpret the command line options and execute the data processing.
static int xsh_cfg_recover_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static cpl_frame * xsh_cfg_recover_gen_xyg_frame(cpl_frame *wave_list, cpl_frame *config_frame, xsh_instrument *instr, int prescan)
static char xsh_cfg_recover_description[]
static cpl_image * xsh_cfg_recover_linear_ext(cpl_frame *raw_frm, cpl_frame *order_tab_centr, xsh_instrument *instr, const int slit, const double thresh_min)
static cpl_error_code xsh_cfg_recover_pattern_match(cpl_parameterlist *parameters, cpl_matrix *mat_gue, cpl_matrix *mat_dat, int use_pattern, int use_data, int debug_level)
static cpl_table * xsh_cfg_recover_select_peaks(cpl_table *tab_xy_guess, cpl_table *tab_xy_peaks, const double factor)
static cpl_error_code xsh_cfg_recover_model_THE_create(cpl_frame *config_frame, xsh_instrument *instrument, cpl_frame *wave_list, cpl_frame **THE1, cpl_frame **THE9)
static char xsh_cfg_recover_description_short[]
static cpl_error_code xsh_cfg_recover_prepare_pm_set(cpl_table *tab_pat, cpl_table *tab_dat, cpl_parameterlist *parameters, cpl_matrix **mat_pat, cpl_matrix **mat_dat, int *use_pat, int *use_dat)
static int xsh_cfg_recover_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
#define XSH_ORDPOS_POL_DIM_MAX
static cpl_error_code xsh_cfg_recover_add_peaks_xpos(cpl_frame *order_tab_centr, xsh_instrument *instr, cpl_table **tab_xy_peaks)
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
int xsh_order_list_get_order(xsh_order_list *list, int absorder)
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
void xsh_prepare(cpl_frameset *frames, cpl_frame *bpmap, cpl_frame *mbias, const char *prefix, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
This function transform RAW frames dataset in PRE frames dataset attaching the default bad pixel map ...
#define XSH_ASSURE_NOT_ILLEGAL(cond)
#define assure(CONDITION, ERROR_CODE,...)
#define check_msg(COMMAND,...)
#define xsh_error_reset()
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
cpl_error_code xsh_model_config_load_best(cpl_frame *config_frame, xsh_xs_3 *p_xs_3)
Load the config model table and fill the struct.
cpl_vector * xsh_model_refining_detect(const cpl_vector *in, int fwhm, double sigma, int display)
Detect the brightest features in a spectrum.
void xsh_model_get_xy(xsh_xs_3 *p_xs_3, xsh_instrument *instr, double lambda_nm, int morder, double ent_slit_pos, double *x, double *y)
Compute the detector location (floating point pixels) of a given wavelength/entrance slit position.
cpl_frame * xsh_model_spectralformat_create(xsh_xs_3 *p_xs_3, const char *tab_filename)
creates the model spectral format table
int xsh_model_first_anneal(cpl_parameterlist *parlist, cpl_frameset *frameset)
Interpret the command line options and execute the data processing.
cpl_frame * xsh_model_THE_create(xsh_xs_3 *p_xs_3, xsh_instrument *instr, const char *line_list, int num_ph, double sep_ph, const char *THE_filename)
Compute the THE table (centroid for each feature in lamp spectrum)
cpl_vector ** xsh_model_locus(struct xs_3 *p_xs_3, xsh_instrument *instr, double ent_slit_pos)
Compute the locus of the spectrum.
cpl_frame * xsh_model_pipe_anneal(cpl_frame *cfg_frame, cpl_frame *resid_frame, int maxit, double ann_fac, int scenario, int rec_id)
Run the annealing (optimisation) algoritm to improve the fit of the model parameter set to a given wa...
#define xsh_msg_warning(...)
Print an warning message.
#define xsh_msg_debug(...)
Print a debug message.
#define xsh_msg_error(...)
Print an error message.
#define xsh_msg(...)
Print a message on info level.
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
void xsh_pfits_set_wavesoltype(cpl_propertylist *plist, const char *value)
Write the WAVESOL TYPE value.
cpl_vector * xsh_spectrum_detect_peaks(const cpl_vector *in, int fwhm, double kappa, int display)
Detect the brightest features in a spectrum.
cpl_frameset * xsh_subtract_nir_on_off(cpl_frameset *on, cpl_frameset *off, xsh_instrument *instr)
(NIR only) subtract the OFF set of files from the On set of files
cpl_frame * xsh_subtract_bias(cpl_frame *frame, cpl_frame *bias, xsh_instrument *instr, const char *type, const int pre_overscan_corr, const int save_tmp)
Subtract the master bias frame from PRE frame.
cpl_frame * xsh_subtract_dark(cpl_frame *frame, cpl_frame *dark, const char *filename, xsh_instrument *instr)
subtract the master dark frame from PRE frame
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
void xsh_free_parameterlist(cpl_parameterlist **p)
Deallocate a parameter list and set the pointer to NULL.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
cpl_error_code xsh_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
Sort a table by one column.
void xsh_free_array(cpl_array **m)
Deallocate an array and set the pointer to NULL.
const char * xsh_get_license(void)
Get the pipeline copyright and license.
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
void xsh_init(void)
Reset library state.
void xsh_free_matrix(cpl_matrix **m)
Deallocate a matrix and set the pointer to NULL.
cpl_error_code xsh_begin(cpl_frameset *frames, const cpl_parameterlist *parameters, xsh_instrument **instrument, cpl_frameset **raws, cpl_frameset **calib, const char *tag_list[], int tag_list_size, const char *recipe_id, unsigned int binary_version, const char *short_descr)
Recipe initialization.
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
cpl_error_code xsh_end(const char *recipe_id, cpl_frameset *frames, cpl_parameterlist *parameters)
Recipe termination.
#define XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION
#define XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL
#define XSH_RESID_TAB_TABLE_COLNAME_ORDER
#define XSH_RESID_TAB_TABLE_COLNAME_YTHPRE
#define XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL
#define XSH_RESID_TAB_TABLE_COLNAME_XGAUSS
#define XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH
#define XSH_RESID_TAB_TABLE_COLNAME_YGAUSS
#define XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS
#define XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL
#define XSH_RESID_TAB_TABLE_COLNAME_XTHCOR
#define XSH_RESID_TAB_TABLE_COLNAME_YTHCOR
#define XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX
#define XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL
#define XSH_RESID_TAB_TABLE_COLNAME_XTHPRE
#define XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
void xsh_add_product_table(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Save Table product (input frame has several extensions, 1 table per extension)
char * xsh_get_tag_from_arm(const char *tag, xsh_instrument *instr)
Find arm specific tag from base and instrument setting.
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
cpl_frame * xsh_find_frame_with_tag(cpl_frameset *frames, const char *tag, xsh_instrument *instr)
Find frame with a given tag.
void xsh_frame_config(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level, cpl_frame **frame)
Define a frame characteristics.
cpl_frameset * xsh_frameset_extract(const cpl_frameset *frames, const char *tag)
Extract frames with given tag from frameset.
cpl_frame * xsh_find_master_bpmap(cpl_frameset *set)
find the master bad pixel map in a set of files
#define XSH_THEO_TAB_SING
#define XSH_MODEL_GUESS_XY
#define XSH_MEASURE_LINE_POS_XY
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_ORDER_TAB_CENTR
#define XSH_ARC_LINE_LIST
#define XSH_THEO_TAB_MULT
cpl_error_code xsh_model_temperature_update_frame(cpl_frame **model_config_frame, cpl_frame *ref_frame, xsh_instrument *instrument, int *found_temp)
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
int xsh_parameters_debug_level_get(const char *recipe_id, const cpl_parameterlist *list)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
#define XSH_WAVESOLTYPE_MODEL