74#define RECIPE_ID "xsh_model_compute"
75#define RECIPE_AUTHOR "P. Bristow"
76#define RECIPE_CONTACT "bristowp@eso.org"
91static int xsh_model(cpl_parameterlist *, cpl_frameset *);
99 "This recipe is for development use only.\n";
122 cpl_recipe *recipe = NULL;
123 cpl_plugin *plugin = NULL;
125 recipe = cpl_calloc(1,
sizeof(*recipe));
126 if (recipe == NULL) {
130 plugin = &recipe->interface;
132 cpl_plugin_init(plugin, CPL_PLUGIN_API,
134 CPL_PLUGIN_TYPE_RECIPE,
145 cpl_pluginlist_append(list, plugin);
147 return (cpl_error_get_code() != CPL_ERROR_NONE);
162 cpl_recipe * recipe ;
169 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
170 recipe = (cpl_recipe *)plugin ;
174 recipe->parameters = cpl_parameterlist_new() ;
179 p = cpl_parameter_new_enum(
"xsh.xsh_model_compute.arm",
182 "xsh.xsh_model_compute",
184 3,
"uvb",
"vis",
"nir");
186 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"arm");
187 cpl_parameterlist_append(recipe->parameters, p);
204 cpl_recipe * recipe ;
207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
208 recipe = (cpl_recipe *)plugin ;
211 return xsh_model(recipe->parameters, recipe->frames) ;
224 cpl_recipe * recipe ;
227 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
228 recipe = (cpl_recipe *)plugin ;
231 cpl_parameterlist_delete(recipe->parameters) ;
246 cpl_parameterlist * parlist,
247 cpl_frameset * frameset)
250 const char * fctid =
"xsh_model" ;
251 cpl_parameter * param ;
252 cpl_frame * wave_list ;
253 cpl_frame * test_par ;
254 const char* arm=NULL;
259 param = cpl_parameterlist_find(parlist,
"xsh.xsh_model_compute.arm");
260 arm = cpl_parameter_get_string(param);
267 if (cpl_error_get_code()) {
268 cpl_msg_error(fctid,
"Failed to retrieve the input parameters") ;
276 if (cpl_error_get_code()) {
277 cpl_msg_error(fctid,
"Some input file(s) is(are) missing") ;
283 cpl_frame_get_filename(test_par)),
284 "Failed computing the model") ;
292 if (cpl_error_get_code())
310 const char * wave_list_file,
311 const char * test_par_file)
313 FILE* rec_out = NULL;
314 FILE* ex1d_out = NULL;
315 int ii,morder, morder_cnt;
316 int morder_it,morder_min,morder_max, n_order;
318 int fitsrow, tab_size;
319 int test_p,temp_fs=0, spos_int;
320 char test_string[20];
323 DOUBLE lam_range, blaze_wav, blaze_pad, lam_min, lam_max;
324 DOUBLE pixwavlast, pixylast, pixxlast;
325 int chipdist_current = 0;
326 DOUBLE inten, blaze, p_dif, sinc_arg;
328 DOUBLE prob, prob2, prob3, prob4;
329 DOUBLE gauss1, gauss2, gauss3;
330 DOUBLE es_x_init, es_y_init, spos;
331 DOUBLE x1, xdet2xslit, xdet2yslit, ydet2xslit, ydet2yslit;
333 DOUBLE y_1, xslit2xdet, xslit2ydet;
334 DOUBLE yslit2xdet, yslit2ydet;
336 DOUBLE flux_scale, bg_sig=0.0, ccd_sig;
338 double* trace_lam=NULL;
339 double* trace_xdisp=NULL;
340 double* trace_flux=NULL;
341 double* trace_flux1=NULL;
342 double* trace_flux2=NULL;
343 double* trace_flux3=NULL;
345 int half_prof_wid=20;
346 int flag, bgflag, BackMapFlag, THEtabflag, readout_flag;
347 int THEtxtflag, atmosflag, testfuncflag, det_slitflag;
353 double profile[6001],profile_w[301];
354 DOUBLE prof_scale_s, prof_scale_w;
357 int line_no, total_lines;
358 int line_cnt, lines_tot;
359 cpl_table* lines_tab ;
369 struct xs_3 xs_3_model;
377 int arm_ef_ord0,arm_lam_min,arm_lam_fac,jj,
SIZE;
378 const char* cfg_file=NULL;
379 const char* tag=NULL;
394 FILE* fin,* efin,* profin,* qe_in,* dichro_in,* tran_in;
396 double ef_corr,ef_corr_down,ef_corr_up,substep,pfac,pfac2;
397 double phot_res,tot_phot,nphot;
400 int qe_wv_min, qe_wv_step;
401 double dummy,qe_corr_down,qe_corr_up,qe_corr;
403 double dichro_corr_down,dichro_corr_up,dichro_corr;
407 double tran_corr_down,tran_corr_up;
413 cpl_table* THE_tab = NULL;
414 cpl_propertylist* THEplist = NULL;
415 cpl_frame* THEproduct_frame = NULL;
419 double** ccd_wav=NULL;
428 cpl_frame* config_frame = NULL;
429 struct xs_3* p_xs_3_config;
432 p_all_par=&all_par[0];
433 tran_in=fopen(
"tran.dat",
"r");
434 dichro_in=fopen(
"dichroics.dat",
"r");
437 for (ii=0;ii<154321;ii++) {
456 for (ii=0;ii<6001;ii++) profile[ii]=0.0;
457 for (ii=0;ii<301;ii++) profile_w[ii]=0.0;
458 for (ii=0;ii<10000;ii++) {
466 for (ii=0;ii<102;ii++) qe[ii]=0.0;
467 for (ii=0;ii<2400;ii++) dichro[ii]=0.0;
468 for (ii=0;ii<50000;ii++) tran[ii]=0.0;
475 fin=fopen(test_par_file,
"r");
477 printf(
"couldn't find test parameter settings file %s\n",
479 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
488 while (fscanf(fin,
"%19s %64d\n",test_string, &test_p) !=EOF) {
490 if (strncmp(test_string,
"arm",3)==0) {
493 if (strncmp(test_string,
"proftype",14)==0) {
496 if (strncmp(test_string,
"trace_out_flag",14)==0) {
497 trace_out_flag=test_p;
499 if (strncmp(test_string,
"BackMapFlag",11)==0) {
502 if (strncmp(test_string,
"bgflag",6)==0) {
505 if (strncmp(test_string,
"flux_scale1000",14)==0) {
507 flux_scale=(double)(test_p)/1000.0;
510 if (strncmp(test_string,
"THEtab",6)==0) {
513 if (strncmp(test_string,
"Atmos",5)==0) {
516 if (strncmp(test_string,
"bin_X",5)==0) {
519 if (strncmp(test_string,
"bin_Y",5)==0) {
522 if (strncmp(test_string,
"THEtxt",6)==0) {
525 if (strncmp(test_string,
"det_slit",6)==0) {
528 if (strncmp(test_string,
"testfunc",8)==0) {
531 if (strncmp(test_string,
"bg_sig",8)==0) {
532 bg_sig=(double)(test_p);
537 flux_scale=(double)(temp_fs)/1000.0;
544 if (p_xs_3->
arm==0) {
570 cfg_file=
"/home/bristowp/allscripts/x-s/cfg_edge.fits";
579 efin=fopen(
"effic_uvb.dat",
"r");
581 while (fscanf(efin,
"%64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf \n",
582 &ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],
583 &ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],
584 &ef[10][ii],&ef[11][ii]) !=EOF) {
600 qe_in=fopen(
"uvb_qe.dat",
"r");
605 else if (p_xs_3->
arm==1) {
628 cfg_file=
"xs_vis_def_aug10.fits";
635 efin=fopen(
"effic_vis.dat",
"r");
637 while (fscanf(efin,
"%64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf \n",
638 &ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],
639 &ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],
640 &ef[10][ii],&ef[11][ii],&ef[12][ii],&ef[13][ii],&ef[14][ii]) !=EOF) {
650 qe_in=fopen(
"vis_qe.dat",
"r");
669 cfg_file=
"/scratch/bristowp/IFU_analysis/IFU_DATA/2dmap/PROD_XSH_MOD_CFG_OPT_2D_NIR.fits";
676 efin=fopen(
"effic_nir.dat",
"r");
678 while (fscanf(efin,
"%64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf \n",
679 &ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],
680 &ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],
681 &ef[10][ii],&ef[11][ii],&ef[12][ii],&ef[13][ii],&ef[14][ii],
682 &ef[15][ii],&ef[16][ii],&ef[17][ii],&ef[18][ii],&ef[19][ii],
683 &ef[20][ii]) !=EOF) {
693 qe_in=fopen(
"nir_qe.dat",
"r");
702 if (testfuncflag==11) {
704 struct xs_3 xs_3_config;
707 cpl_frame* wav_frame = NULL;
708 cpl_frame* slit_frame = NULL;
710 cpl_error_code error_maps;
714 p_xs_3_config=&xs_3_config;
715 config_frame = cpl_frame_new();
716 cpl_frame_set_filename( config_frame, cfg_file);
717 cpl_frame_set_tag( config_frame,tag);
719 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
724 if (p_xs_3_config->
arm==0) {
727 else if (p_xs_3_config->
arm==1) {
730 else if (p_xs_3_config->
arm==2) {
734 printf(
"ARM NOT SET\n");
743 wtag=
"chips_wav_test";
744 stag=
"chips_slit_test";
756 if (testfuncflag==2) {
758 struct xs_3 xs_3_config;
763 p_xs_3_config=&xs_3_config;
765 config_frame = cpl_frame_new();
766 cpl_frame_set_filename( config_frame, cfg_file);
767 cpl_frame_set_tag( config_frame, tag);
770 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
780 if (p_xs_3_config->
arm==0) {
784 printf(
"404.656494nm in order 18 on arm %d : x=%lf y=%lf \n",
788 printf(
"385.11790nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
791 printf(
"385.11790nm at s=-5.49536 in order 20 on arm %d : x=%lf y=%lf \n",
794 printf(
"385.11790nm at s=-5.51854 in order 20 on arm %d : x=%lf y=%lf \n",
797 printf(
"385.11940nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
800 printf(
"385.11590nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
804 printf(
"369.4774nm at s=-2.50531 in order 20 on arm %d : x=%lf y=%lf \n",
807 else if (p_xs_3_config->
arm==1) {
810 printf(
"649.06520nm in order 25 on arm %d : x=%lf y=%lf \n",
813 printf(
"676.0587nm in order 24 on arm %d : x=%lf y=%lf \n",
816 printf(
"573.0815nm in order 29 on arm %d : x=%lf y=%lf \n",
819 printf(
"871.2175nm in order 18 on arm %d : x=%lf y=%lf \n",
822 printf(
"676.0599nm in order 24 on arm %d : x=%lf y=%lf \n",
825 printf(
"573.0818nm in order 29 on arm %d : x=%lf y=%lf \n",
828 printf(
"871.2192nm in order 18 on arm %d : x=%lf y=%lf \n",
870 else if (p_xs_3_config->
arm==2) {
873 printf(
"1382.36nm in order 19 on arm %d : x=%lf y=%lf \n",
876 printf(
"1382.37nm in order 19 on arm %d : x=%lf y=%lf \n",
879 printf(
"1382.38nm in order 19 on arm %d : x=%lf y=%lf \n",
882 printf(
"1382.108nm at s=5.55761 in order 19 on arm %d : x=%lf y=%lf \n",
886 printf(
"ARM NOT SET\n");
893 cpl_frame_delete(config_frame) ;
902 if (testfuncflag==3) {
904 struct xs_3 xs_3_config;
906 int tempsize=p_xs_3->
BSIZE;
912 p_xs_3_config=&xs_3_config;
914 config_frame = cpl_frame_new();
915 cpl_frame_set_filename( config_frame, cfg_file);
916 cpl_frame_set_tag( config_frame, tag);
919 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
925 if (p_xs_3_config->
arm==0) {
928 else if (p_xs_3_config->
arm==1) {
931 else if (p_xs_3_config->
arm==2) {
935 printf(
"ARM NOT SET\n");
939 for (ii=0;ii<tempsize;ii+=1) {
940 printf(
"order %d dispersion pixel no: %d, x-dispersion: %lf \n",
944 for (ii=0 ; ii<16 ; ii++) cpl_vector_delete(trace[ii]);
947 cpl_frame_delete(config_frame) ;
956 if (testfuncflag==5) {
957 struct xs_3 xs_3_config;
960 cpl_frame* temp_sf_frame=NULL ;
962 p_xs_3_config=&xs_3_config;
964 config_frame = cpl_frame_new();
965 cpl_frame_set_filename( config_frame, cfg_file);
966 cpl_frame_set_tag( config_frame, tag);
969 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
977 xsh_msg(
"SF2 table %s %s", cpl_frame_get_filename(temp_sf_frame),
978 cpl_frame_get_tag(temp_sf_frame));
980 cpl_frame_delete(config_frame) ;
992 if (testfuncflag==4) {
993 struct xs_3 xs_3_config;
996 cpl_frame * temp_THEproduct_frame ;
1000 p_xs_3_config=&xs_3_config;
1002 config_frame = cpl_frame_new();
1003 cpl_frame_set_filename( config_frame, cfg_file);
1004 cpl_frame_set_tag( config_frame,tag);
1007 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
1014 if (p_xs_3_config->
arm==0) {
1017 else if (p_xs_3_config->
arm==1) {
1020 else if (p_xs_3_config->
arm==2) {
1024 printf(
"ARM NOT SET\n");
1030 wave_list_file,1,0.0,
1035 wave_list_file,9,1.4,
1040 cpl_frame_get_filename(temp_THEproduct_frame),
1041 cpl_frame_get_tag(temp_THEproduct_frame));
1043 cpl_frame_delete(config_frame) ;
1054 if (testfuncflag==7) {
1055 cpl_image * im = NULL;
1056 cpl_image * wavmap = NULL;
1058 double* wavmap_pix=NULL;
1061 cpl_vector * extracted;
1063 struct xs_3 xs_3_config;
1065 char order_tab_name[256];
1066 cpl_propertylist *header = NULL;
1067 cpl_frame* order_tab_frame = NULL;
1068 cpl_table* table = NULL;
1072 int order_tab_size = 0;
1073 double wvlen, dx, tot;
1074 cpl_vector * ext_med_fil;
1079 p_xs_3_config=&xs_3_config;
1080 config_frame = cpl_frame_new();
1081 cpl_frame_set_filename( config_frame, cfg_file);
1082 cpl_frame_set_tag( config_frame,tag);
1084 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
1091 if (p_xs_3_config->
arm==0) {
1094 else if (p_xs_3_config->
arm==1) {
1097 else if (p_xs_3_config->
arm==2) {
1101 printf(
"ARM NOT SET\n");
1105 if (order_tab_flag==0) {
1106 strcpy(order_tab_name,
"none");
1112 strcpy(order_tab_name,
"PROD_ORDER_TAB_CENTR_UVB.fits");
1117 order_tab_frame = cpl_frame_new();
1118 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
1119 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
1120 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
1122 check( nbcol = cpl_table_get_nrow(table));
1139 xsh_msg(
"invalid nbcol %d", nbcol);
1144 order_tab_size = order_tab->
size;
1145 check ( header = cpl_propertylist_load( order_tab_name, 0));
1147 check( nbcol = cpl_table_get_nrow(table));
1150 lines_tab = cpl_table_load(wave_list_file, 1, 0);
1152 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
1155 check(wavmap=cpl_image_load(
"xs_vis_Mar08_wav_map.fits", CPL_TYPE_DOUBLE, 0, 0));
1157 check(wavmap_pix=cpl_image_get_data_double(wavmap));
1159 check(extracted = cpl_vector_new(p_xs_3_config->
SIZE)) ;
1160 check(im=cpl_image_load(
"9ph_test/FMTCHK_VIS_multi.fits", CPL_TYPE_DOUBLE, 0, 0));
1164 check(detec=cpl_image_get_data_double(im));
1167 for (ii=0;ii<nbcol;ii++) {
1169 if (order_tab_flag!=0){
1178 xsh_msg(
"Warning starty and endy equal zero, put endy to %d",
1179 p_xs_3_config->
SIZE-1);
1184 for (jj=0;jj<p_xs_3_config->
SIZE;jj++) {
1185 cpl_vector_set(extracted,jj,0.0);
1193 if (order_tab_flag!=0){
1197 dx=cpl_vector_get(trace[ii],jj);
1200 for (kk=-h; kk<=h; kk++) {
1201 if (p_xs_3_config->
arm==0) {
1202 tot+=detec[2044*jj+(int)(dx+0.5)+kk];
1204 else if (p_xs_3_config->
arm==1) {
1205 tot+=detec[2044*jj+(int)(dx+0.5)+kk];
1208 tot+=detec[1056*jj+((int)(dx+0.5)+kk)];
1212 cpl_vector_set(extracted,jj,tot);
1214 ext_med_fil=cpl_vector_filter_median_create(extracted,med_wid);
1218 for (jj=0;jj<med_wid;jj++) {
1219 cpl_vector_set(ext_med_fil,jj,cpl_vector_get(ext_med_fil,med_wid+1));
1221 for (jj=p_xs_3_config->
SIZE-med_wid-2;jj<p_xs_3_config->
SIZE;jj++) {
1222 cpl_vector_set(ext_med_fil,jj,cpl_vector_get(ext_med_fil,p_xs_3_config->
BSIZE-med_wid-3));
1224 cpl_vector_subtract(extracted,ext_med_fil);
1226 if (order_tab_flag!=0){
1230 dx=cpl_vector_get(trace[ii],jj);
1232 wvlen=wavmap_pix[(jj)*p_xs_3_config->
ASIZE+(
int)(dx)];
1234 printf(
"%d %d %lf %lf %lf \n",real_ord,jj,wvlen,dx,cpl_vector_get(extracted,jj));
1238 cpl_vector_delete(ext_med_fil);
1240 if (strcmp(order_tab_name,
"none") == 0){
1241 for (ii=0 ; ii<16 ; ii++) cpl_vector_delete(trace[ii]);
1244 cpl_vector_delete(extracted);
1255 if (testfuncflag==8) {
1256 cpl_image * im = NULL;
1257 cpl_image * wavmap = NULL;
1258 double* ex_detec=NULL;
1259 double* wavmap_pix=NULL;
1263 cpl_vector * extracted;
1264 cpl_vector * bright_lines[n_ord] ;
1265 struct xs_3 xs_3_config;
1270 char order_tab_name[256];
1271 cpl_propertylist *header = NULL;
1272 cpl_frame* order_tab_frame = NULL;
1273 cpl_table* table = NULL;
1278 int order_tab_size = 0;
1282 p_xs_3_config=&xs_3_config;
1283 config_frame = cpl_frame_new();
1284 cpl_frame_set_filename( config_frame, cfg_file);
1285 cpl_frame_set_tag( config_frame,tag);
1287 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
1293 if (p_xs_3_config->
arm==0) {
1296 else if (p_xs_3_config->
arm==1) {
1299 else if (p_xs_3_config->
arm==2) {
1303 printf(
"ARM NOT SET\n");
1306 strcpy(order_tab_name,
"PROD_ORDER_TAB_CENTR_VIS_Mar08.fits");
1312 order_tab_frame = cpl_frame_new();
1313 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
1314 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
1315 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
1317 check( nbcol = cpl_table_get_nrow(table));
1334 xsh_msg(
"invalid nbcol %d", nbcol);
1337 order_tab_size = order_tab->
size;
1339 check ( header = cpl_propertylist_load( order_tab_name, 0));
1341 lines_tab = cpl_table_load(wave_list_file, 1, 0);
1343 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
1346 check( nbcol = cpl_table_get_nrow(table));
1348 check(wavmap=cpl_image_load(
"chips_wav_test.fits", CPL_TYPE_DOUBLE, 0, 0));
1349 check(wavmap_pix=cpl_image_get_data_double(wavmap));
1354 check(extracted = cpl_vector_new(p_xs_3->
BSIZE)) ;
1355 check(im=cpl_image_load(
"/home/fkerber/Xshooter/AIT/ThAr_currrent/080131_VIS_RM4_ThAr2+ND1_20mA_1s_wcal.fits", CPL_TYPE_DOUBLE, 0, 0));
1356 check(ex_detec=cpl_image_get_data_double(im));
1357 for (ii=0;ii<n_ord-1;ii++) {
1359 for (jj=0;jj<p_xs_3->
BSIZE;jj++) {
1360 cpl_vector_set(extracted,jj,ex_detec[ii*p_xs_3->
BSIZE+jj]);
1364 for (kk=0; kk<cpl_vector_get_size(bright_lines[ii]);kk++) {
1365 yint=(int)(cpl_vector_get(bright_lines[ii],kk));
1366 check(dx=cpl_polynomial_eval_1d(order_tab->
list[real_ord-ord_min].
cenpoly,yint, NULL));
1367 wvlen=wavmap_pix[yint*p_xs_3->
ASIZE+(int)(dx)];
1368 printf(
"%d %d %lf %lf \n",
1369 real_ord,kk,cpl_vector_get(bright_lines[ii],kk),wvlen);
1407 if (testfuncflag==10) {
1408 struct xs_3 xs_3_config;
1409 cpl_frame* temp_oe_frame=NULL ;
1413 p_xs_3_config=&xs_3_config;
1415 check(config_frame = cpl_frame_new());
1416 check(cpl_frame_set_filename(config_frame, cfg_file));
1417 cpl_frame_set_tag( config_frame, tag);
1420 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
1426 xsh_msg(
"oet table %s %s", cpl_frame_get_filename(temp_oe_frame),
1427 cpl_frame_get_tag(temp_oe_frame));
1429 cpl_frame_delete(config_frame) ;
1440 if (testfuncflag==9) {
1442 struct xs_3 xs_3_config;
1448 char order_tab_name[256];
1449 cpl_frame* order_tab_frame = NULL;
1450 cpl_table* table = NULL;
1451 cpl_table* line_table = NULL;
1454 int order_tab_size = 0;
1455 int iy,iyuse, iorder;
1459 int h, w, wmin, min_iso;
1461 cpl_propertylist *header = NULL;
1462 const char* pro_catg = NULL;
1463 cpl_vector * extracted;
1464 cpl_vector * ext_med_fil;
1467 cpl_vector * bright_lines[15] ;
1469 cpl_image * im = NULL;
1473 double x,
y, cent, cent2, flux, cent2_cor, flux_cor;
1474 double flux_keep=0.0;
1475 double last_cent=0.0;
1476 double prec, ex_cor, med, stdev_ord;
1481 rec_out=fopen(
"rec_out",
"w");
1482 ex1d_out=fopen(
"ex1d_out",
"w");
1486 cfg_file=
"/home/bristowp/allscripts/x-s/jun08sol/xs_uvb_def_jun08.fits";
1492 p_xs_3_config=&xs_3_config;
1493 check(config_frame = cpl_frame_new());
1494 check(cpl_frame_set_filename( config_frame, cfg_file));
1495 cpl_frame_set_tag( config_frame, tag);
1500 cpl_msg_error(__func__,
"Cannot load %s as a config", cfg_file) ;
1515 check(im=cpl_image_load(
"/scratch/pen_rays/pre_allpen_mar08.fits", CPL_TYPE_DOUBLE, 0, 0));
1519 check(detec=cpl_image_get_data_double(im));
1525 check(cpl_msg_set_level(CPL_MSG_DEBUG));
1530 strcpy(order_tab_name,
"PROD_ORDER_TAB_CENTR_UVB.fits");
1535 order_tab_frame = cpl_frame_new();
1536 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
1537 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
1538 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
1541 check( nbcol = cpl_table_get_nrow(table));
1562 xsh_msg(
"invalid nbcol %d", nbcol);
1571 order_tab_size = order_tab->
size;
1573 check ( header = cpl_propertylist_load( order_tab_name, 0));
1576 xsh_msg(
"Order Table of type %s",pro_catg);
1579 line_table = cpl_table_load(wave_list_file, 1, 0);
1581 check(lines_tot = cpl_table_get_nrow(line_table)) ;
1587 for( iorder=0; iorder< order_tab_size; iorder++){
1589 check(extracted = cpl_vector_new(p_xs_3->
BSIZE)) ;
1594 xsh_msg(
"Warning starty and endy equal zero, put endy to %d",
ny-1);
1602 for( iy=300; iy<=p_xs_3->
BSIZE-100; iy=iy+1){
1613 for (ii=-h; ii<=h; ii++) {
1614 tot+=detec[p_xs_3->
ASIZE*iyuse+(int)(dx+0.5)+ii];
1617 cpl_vector_set(extracted,iyuse,tot);
1625 med=cpl_vector_get_median_const(extracted);
1626 stdev_ord=cpl_vector_get_stdev(extracted);
1627 ext_med_fil=cpl_vector_filter_median_create(extracted,50);
1628 cpl_vector_subtract(extracted,ext_med_fil);
1631 fprintf(ex1d_out,
"%d %d %lf \n",
1632 iorder, iy, cpl_vector_get(extracted,iy));
1644 cpl_vector_delete(ext_med_fil);
1655 for (ii=0; ii<cpl_vector_get_size(bright_lines[iorder]);ii++) {
1656 cent=cpl_vector_get(bright_lines[iorder],ii)+0.5-1.0;
1663 prec=(float)(w)/20.0;
1671 for (jj=(
int)(cent)-wmin;jj<=(int)(cent)+w;jj++) {
1673 if (jj>0 && jj<p_xs_3->
BSIZE) {
1674 ex_cor=cpl_vector_get(extracted,jj);
1676 cent2+=ex_cor*((float)(jj)+0.5-cent);
1686 if (fabs(cent2-cent)<prec) {
1698 prec=(float)(w)/20.0;
1706 for (jj=(
int)(cent)-w;jj<=(int)(cent)+wmin;jj++) {
1707 if (jj>0 && jj<p_xs_3->
BSIZE) {
1708 ex_cor=cpl_vector_get(extracted,jj);
1710 cent2+=ex_cor*((float)(jj)+0.5-cent);
1716 if (fabs(cent2-cent)<prec) {
1729 blaze_wav=2*(sin(-p_xs_3_config->
nug))/((
double)(iorder+
morder_min)*p_xs_3_config->
sg);
1733 if (lmax>=min_iso && rmax>=min_iso) {
1735 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
1736 check(lambda_nm=cpl_table_get_float(line_table,
"WAVELENGTH",
1738 check(inten = cpl_table_get_int(line_table,
"FLUX", line_cnt, NULL));
1740 blaze_wav=2*(sin(-p_xs_3_config->
nug))/((
double)(iorder+
morder_min)*p_xs_3_config->
sg);
1745 if (lambda_nm<lam_max && lambda_nm>lam_min) {
1747 check(dx=cpl_polynomial_eval_1d(order_tab->
list[iorder].
cenpoly,(
int)(
y), NULL));
1749 printf(
"%lf %lf %lf %lf \n",dx,
y, cent,
y-cent);
1751 if (fabs(
y-cent)<1.5 && rmax!=0 && lmax!=0) {
1752 printf(
"%d %lf %lf %lf %d %d\n",iorder+
morder_min, lambda_nm, dx,cent,lmax,rmax);
1754 if (cent!=last_cent) {
1755 if (fabs(
y-cent)<1.5 && rmax!=0 && lmax!=0) {
1756 fprintf(rec_out,
"\n %d %lf %lf %lf %lf %d %lf ",iorder+
morder_min, lambda_nm, dx,cent,flux_keep,min_iso,
y-cent);
1760 fprintf(rec_out,
"warning");
1768 cpl_vector_delete(extracted) ;
1796 cpl_image_delete(im);
1799 cpl_frame_delete(config_frame) ;
1802 for (ii=0 ; ii<15; ii++) {
1803 if (bright_lines[ii] != NULL) cpl_vector_delete(bright_lines[ii]) ;
1805 cpl_table_delete(line_table) ;
1815 if (testfuncflag==0) {
1819 if (THEtabflag==1) {
1820 if (trace_out_flag==1 || BackMapFlag==1) {
1821 printf(
"Warning: THE table generation has been chosen, backgroud map and\nspectral profile generation will be ignored. \nRun again with:\n THEtab 0\nto get these products.\n\n");
1823 THE_tab = cpl_table_new(tab_size);
1824 cpl_table_new_column(THE_tab,
"Wavelength", CPL_TYPE_FLOAT) ;
1825 cpl_table_new_column(THE_tab,
"Order", CPL_TYPE_INT) ;
1826 cpl_table_new_column(THE_tab,
"Slit position", CPL_TYPE_FLOAT) ;
1827 cpl_table_new_column(THE_tab,
"detector x", CPL_TYPE_DOUBLE) ;
1828 cpl_table_new_column(THE_tab,
"detector y", CPL_TYPE_DOUBLE) ;
1832 while (fscanf(qe_in,
"%64lf %64lf \n",&dummy,&qe[ii]) !=EOF) {
1838 while (fscanf(tran_in,
"%64lf %64lf \n",&dummy,&tran[ii]) !=EOF) {
1844 while (fscanf(dichro_in,
"%64lf %64lf %64lf %64lf \n",
1845 &dummy,&temp[0],&temp[1],&temp[2]) !=EOF) {
1846 dichro[ii]=temp[dichro_col];
1851 config_frame = cpl_frame_new();
1852 cpl_frame_set_filename( config_frame, cfg_file);
1853 cpl_frame_set_tag( config_frame, tag);
1856 "Cannot load %s as a config",
1874 es_x_init=p_xs_3->
es_x;
1875 es_y_init=p_xs_3->
es_y;
1878 if (p_xs_3->
arm!=2) {
1897 p_xs_3->
pix_X=p_xs_3->
pix*(float)(bin_X);
1898 p_xs_3->
pix_Y=p_xs_3->
pix*(float)(bin_Y);
1902 profin=fopen(
"single_0.5ph_pro.dat",
"r");
1904 profin=fopen(
"full_slit_pro.dat",
"r");
1907 else if (proftype==2) {
1908 profin=fopen(
"single_0.5ph_pro.dat",
"r");
1912 else if (proftype==3) {
1913 profin=fopen(
"multi_ph_pro.dat",
"r");
1917 else if (proftype==4) {
1918 profin=fopen(
"multi7_ph_pro.dat",
"r");
1922 else if (proftype==5) {
1923 profin=fopen(
"multi9_ph_HR_pro.dat",
"r");
1927 else if (proftype==6) {
1928 profin=fopen(
"ifu.dat",
"r");
1932 else if (proftype==7) {
1933 profin=fopen(
"custom_profile.dat",
"r");
1939 while (fscanf(profin,
"%64lf %64lf\n",&spos,&profile[ii]) !=EOF) {
1944 profin=fopen(
"single_0.5ph_pro.dat",
"r");
1946 while (fscanf(profin,
"%64lf %64lf\n",&spos,&profile_w[ii]) !=EOF) {
1954 check(lines_tab = cpl_table_load(wave_list_file, 1, 0)) ;
1955 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
1957 trace_out=fopen(
"trace_r.dat",
"w");
1958 trace_out1=fopen(
"trace1.dat",
"w");
1959 trace_out2=fopen(
"trace2.dat",
"w");
1960 trace_out3=fopen(
"trace3.dat",
"w");
1966 if (THEtabflag==1) {
1967 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
1968 lambda_nm=cpl_table_get_float( lines_tab,
1975 blaze_wav=2*(sin(-p_xs_3->
nug))/(morder_cnt*p_xs_3->
sg);
1976 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
1977 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
1979 lambda=lambda_nm*1e-6;
1982 for (spos=-sep_ph*(num_ph-1.0)/2.0; spos<=0.01+sep_ph*(num_ph-1.0)/2.0; spos+=sep_ph) {
1984 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
1989 cpl_table_set_float(THE_tab,
"Wavelength", fitsrow, lambda_nm) ;
1990 cpl_table_set_int(THE_tab,
"Order", fitsrow, morder_cnt) ;
1991 cpl_table_set_float(THE_tab,
"Slit position",
1993 cpl_table_set_double(THE_tab,
"detector x",
1995 cpl_table_set_double(THE_tab,
"detector y",
1997 if (THEtxtflag==1) {
1998 printf(
"%lf %d %lf %lf %lf %lf \n",
2013 else if (det_slitflag==1) {
2015 blaze_wav=2*(sin(-p_xs_3->
nug))/(morder_cnt*p_xs_3->
sg);
2016 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
2017 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
2019 lambda+=(lam_max-lam_min)/100.0) {
2020 lambda_nm=lambda*1000000.0;
2023 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2037 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2043 x1!=0.0 && y_1!=0.0) {
2053 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2067 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2073 x1!=0.0 && y_1!=0.0) {
2083 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2085 if (xslit2xdet!=9999.0 && xslit2ydet!=9999.0 &&
2086 yslit2xdet!=9999.0 && yslit2ydet!=9999.0) {
2087 ydet2xslit=yslit2xdet/(xslit2ydet*yslit2xdet-xslit2xdet*yslit2ydet);
2088 ydet2yslit=-xslit2xdet/(xslit2ydet*yslit2xdet-xslit2xdet*yslit2ydet);
2089 xdet2xslit=yslit2ydet/(xslit2xdet*yslit2ydet-xslit2ydet*yslit2xdet);
2090 xdet2yslit=-xslit2ydet/(xslit2xdet*yslit2ydet-xslit2ydet*yslit2xdet);
2091 printf(
"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",
2093 xslit2ydet, yslit2xdet, xslit2xdet, yslit2ydet, ydet2xslit,
2094 ydet2yslit,xdet2xslit,xdet2yslit);
2102 for (ii=0;ii<p_xs_3->
BSIZE;ii++) {
2103 for (jj=0;jj<p_xs_3->
ASIZE;jj++) {
2107 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
2108 lambda_nm=cpl_table_get_float(lines_tab,
2111 inten = cpl_table_get_int(lines_tab,
2116 blaze_wav=2*(sin(-p_xs_3->
nug))/(morder_cnt*p_xs_3->
sg);
2117 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
2118 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
2120 lambda=lambda_nm*1e-6;
2124 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2129 sinc_arg=(
M_PI/(lambda*p_xs_3->
sg))*
2132 blaze=((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
2135 sinc_arg=(
M_PI/(lambda*p_xs_3->
sg))*
2140 ((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
2144 if (p_xs_3->
arm==0) {
2145 if (morder_cnt-arm_ef_ord0+1>0 && ((
int)(lambda_nm)-arm_lam_min)/arm_lam_fac>=0) {
2146 ef_corr_down=ef[morder_cnt-arm_ef_ord0+1][((int)(lambda_nm)-arm_lam_min)/arm_lam_fac];
2147 ef_corr_up=ef[morder_cnt-arm_ef_ord0+1][(((int)(lambda_nm)-arm_lam_min)/arm_lam_fac)+1];
2148 substep=(lambda_nm/(double)(arm_lam_fac)-(double)((
int)(lambda_nm/(double)(arm_lam_fac))));
2149 ef_corr=ef_corr_down+substep*(ef_corr_up-ef_corr_down);
2156 if ((
int)(lambda_nm)-qe_wv_min>=0) {
2157 qe_corr_down=qe[((int)(lambda_nm)-qe_wv_min)/qe_wv_step];
2158 qe_corr_up=qe[(((int)(lambda_nm)-qe_wv_min)/qe_wv_step)+1];
2159 substep=(lambda_nm/(double)(qe_wv_step))-(
double)((int)(lambda_nm/(
double)(qe_wv_step)));
2160 qe_corr=(qe_corr_down+substep*(qe_corr_up-qe_corr_down))/100.0;
2163 qe_corr=qe[0]/100.0;
2165 dichro_corr_down=dichro[(int)(lambda_nm)-200];
2166 dichro_corr_up=dichro[((int)(lambda_nm)-200)+1];
2167 substep=(lambda_nm)-(
double)((int)(lambda_nm));
2168 dichro_corr=(dichro_corr_down+substep*(dichro_corr_up-dichro_corr_down))/100.0;
2171 if (lambda_nm>tran_wv_min && lambda_nm<tran_wv_max) {
2173 tran_corr_down=tran[(int)((lambda_nm-tran_wv_min)/tran_wv_step)];
2174 tran_corr_up=tran[(int)((lambda_nm-tran_wv_min)/tran_wv_step)+1];
2175 substep=((lambda_nm-tran_wv_min)/tran_wv_step)-(double)((
int)((lambda_nm-tran_wv_min)/tran_wv_step));
2176 at_tran=tran_corr_down+substep*(tran_corr_up-tran_corr_down);
2187 if (flux_scale*at_tran*ef_corr*qe_corr*dichro_corr<1.0) {
2188 phot_in=(int)(inten);
2189 pfac=flux_scale*at_tran*ef_corr*qe_corr*dichro_corr;
2192 phot_in=(int)(inten*flux_scale);
2193 pfac=at_tran*ef_corr*qe_corr*dichro_corr;
2195 pfac2=at_tran*ef_corr*qe_corr*dichro_corr;
2198 if (prob/2147483648.0<at_tran*ef_corr*qe_corr*dichro_corr*inten*flux_scale) {
2205 else if (phot_in<10000){
2207 for (ii=0;ii<phot_in;ii++) {
2209 if (prob/2147483648.0<pfac) {
2215 tot_phot=(double)(phot_in)*pfac;
2219 printf(
"m %d lam %lf tot %g inten %lf \n", morder_cnt,lambda_nm,tot_phot, inten);
2223 if (inten*pfac>phot_res/flux_scale) {
2224 nphot=(inten/phot_res)*flux_scale*pfac;
2230 while (photons<=(
int)(tot_phot)) {
2244 prob=prob/2147483648.0;
2246 prob2=prob2/2147483648.0;
2248 prob3=prob3/2147483648.0;
2250 prob4=prob4/2147483648.0;
2251 gauss1=sqrt(-2.0*log(1-prob))*cos(2*
M_PI*prob2);
2253 gauss2=sqrt(-2.0*log(1-prob))*sin(2*
M_PI*prob2);
2256 if (proftype==0 || proftype==6) {
2257 p_xs_3->
es_x=(p_xs_3->
es_w)*(prob3-0.5)+(p_xs_3->
es_w/100.0)*gauss1;
2262 if (prob2>profile_w[(
int)(prob3*301.0)] || p_xs_3->
es_x<-p_xs_3->
es_w/2.0 || p_xs_3->
es_x>p_xs_3->
es_w/2.0) {
2268 prof_data_size=6001;
2274 if (prob>profile[(
int)(prob4*prof_data_size)]) {
2279 else if (proftype==1) {
2281 p_xs_3->
es_x=gauss1*0.255/2.35;
2282 p_xs_3->
es_y_tot=gauss2*0.255/2.35;
2295 p_xs_3->
es_x=es_x_init+p_xs_3->
es_x;
2296 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2302 ccd[(int)(p_xs_3->
ypospix-0.5)][(int)(p_xs_3->
xpospix-0.5)]+=nphot;
2312 if (bgflag==0 && continuum==0) line_no+=1;
2315 cpl_table_delete(lines_tab) ;
2316 total_lines=line_no;
2322 cpl_msg_error(__func__,
"Cannot free 2D array ef");
2328 p_xs_3->
es_x=es_x_init;
2331 if (BackMapFlag==1 && THEtabflag!=1) {
2332 if (p_xs_3->
arm==2) {
2339 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2343 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2347 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2351 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2355 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2359 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2363 cpl_msg_error(__func__,
"Cannot allocate 2D array");
2367 for (ii=0;ii<p_xs_3->
BSIZE;ii++) {
2368 for (jj=0;jj<p_xs_3->
ASIZE;jj++) {
2369 ccd_wav[ii][jj]=0.0;
2373 for (ii=0;ii<
SIZE;ii+=1) {
2376 trace_xdisp[ii]=0.0;
2378 trace_flux1[ii]=0.0;
2379 trace_flux2[ii]=0.0;
2380 trace_flux3[ii]=0.0;
2382 blaze_wav=2*(sin(-p_xs_3->
nug))/(morder_cnt*p_xs_3->
sg);
2383 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
2384 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
2385 printf(
"blaze wav=%lf lam_max=%lf lam_min=%lf \n",blaze_wav, lam_max, lam_min);
2390 if (p_xs_3->
arm==2) {
2391 chipdist_current=2047;
2396 for (lambda=lam_min-
blaze_pad; lambda<=lam_max+
blaze_pad; lambda+=(lam_max-lam_min)/10000.0) {
2397 lambda_nm=lambda*1000000.0;
2398 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2405 if (p_xs_3->
arm==2) {
2406 if (p_xs_3->
xpospix<(
double)(chipdist_current)-0.5) {
2408 (pixwavlast+(lambda_nm-pixwavlast)*
2409 ((
double)(chipdist_current)-0.5-pixxlast)/
2411 chipdist_current=p_xs_3->
chippix[1]-1;
2416 if (p_xs_3->
ypospix>(
double)(chipdist_current)-0.5) {
2418 pixwavlast+(lambda_nm-pixwavlast)*
2419 ((
double)(chipdist_current)-0.5-pixylast)/
2421 chipdist_current=p_xs_3->
chippix[2]+1;
2422 if (p_xs_3->
chippix[2]-1==0) {
2431 pixwavlast=lambda_nm;
2434 if (p_xs_3->
arm==2) {
2435 chipdist_current=3000;
2445 lambda+=(lam_max-lam_min)/10000.0) {
2446 lambda_nm=1000000.0*lambda;
2448 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2455 if (p_xs_3->
arm==2){
2456 if (p_xs_3->
xpospix<(
double)(chipdist_current)-0.5) {
2457 trace_lam[p_xs_3->
chippix[1]-1]=pixwavlast+(lambda_nm-pixwavlast)*
2458 ((
double)(chipdist_current)-0.5-pixxlast)/(p_xs_3->
xpospix-pixxlast);
2459 trace_xdisp[p_xs_3->
chippix[1]-1]=pixylast+(p_xs_3->
ypospix-pixylast)*
2460 ((
double)(chipdist_current)-0.5-pixxlast)/(p_xs_3->
xpospix-pixxlast);
2461 trace_flux[p_xs_3->
chippix[1]-1]=0.0;
2462 trace_flux1[p_xs_3->
chippix[1]-1]=0.0;
2463 trace_flux2[p_xs_3->
chippix[1]-1]=0.0;
2464 trace_flux3[p_xs_3->
chippix[1]-1]=0.0;
2465 for (ii=-half_prof_wid;ii<half_prof_wid+1;ii+=1) {
2466 if (p_xs_3->
chippix[2]-1+ii>=0 &&
2469 if (ccd_sig>65535 && ccd_sig<=131072) {
2472 else if (ccd_sig>131072) {
2475 trace_flux[p_xs_3->
chippix[1]-1]+=ccd_sig;
2477 prob=prob/2147483648.0;
2479 prob2=prob2/2147483648.0;
2481 prob3=prob/2147483648.0;
2483 prob4=prob2/2147483648.0;
2484 gauss1=bg_sig*sqrt(-2.0*log(1-prob))*sin(2*
M_PI*prob2);
2485 gauss2=bg_sig*sqrt(-2.0*log(1-prob))*cos(2*
M_PI*prob2);
2486 gauss3=bg_sig*sqrt(-2.0*log(1-prob3))*sin(2*
M_PI*prob4);
2487 trace_flux1[p_xs_3->
chippix[1]-1]+=ccd_sig+gauss1;
2488 trace_flux2[p_xs_3->
chippix[1]-1]+=ccd_sig+gauss2;
2489 trace_flux3[p_xs_3->
chippix[1]-1]+=ccd_sig+gauss3;
2492 trace_mm[p_xs_3->
chippix[1]-1]=morder_cnt;
2494 chipdist_current=p_xs_3->
chippix[1]+1;
2498 if (p_xs_3->
ypospix>(
double)(chipdist_current)-0.5) {
2499 trace_lam[p_xs_3->
chippix[2]-1]=
2500 pixwavlast+(lambda_nm-pixwavlast)*
2501 ((
double)(chipdist_current)-0.5-pixylast)/
2503 trace_xdisp[p_xs_3->
chippix[2]-1]=
2504 pixxlast+(p_xs_3->
xpospix-pixxlast)*
2505 ((
double)(chipdist_current)-0.5-pixylast)/
2507 trace_flux[p_xs_3->
chippix[2]-1]=0.0;
2508 trace_flux1[p_xs_3->
chippix[1]-1]=0.0;
2509 trace_flux2[p_xs_3->
chippix[1]-1]=0.0;
2510 trace_flux3[p_xs_3->
chippix[1]-1]=0.0;
2511 for (ii=-half_prof_wid;ii<half_prof_wid+1;ii+=1) {
2512 if (p_xs_3->
chippix[1]-1+ii>=0 &&
2515 trace_flux[p_xs_3->
chippix[2]-1]+=ccd_sig;
2516 if (ccd_sig>65535 && ccd_sig<=131072) {
2519 else if (ccd_sig>131072) {
2523 prob=prob/2147483648.0;
2525 prob2=prob2/2147483648.0;
2527 prob3=prob3/2147483648.0;
2529 prob4=prob4/2147483648.0;
2530 gauss1=bg_sig*sqrt(-2.0*log(1-prob))*sin(2*
M_PI*prob2);
2531 gauss2=bg_sig*sqrt(-2.0*log(1-prob))*cos(2*
M_PI*prob2);
2532 gauss3=bg_sig*sqrt(-2.0*log(1-prob3))*sin(2*
M_PI*prob4);
2533 trace_flux1[p_xs_3->
chippix[2]-1]+=ccd_sig+gauss1;
2534 trace_flux2[p_xs_3->
chippix[2]-1]+=ccd_sig+gauss2;
2535 trace_flux3[p_xs_3->
chippix[2]-1]+=ccd_sig+gauss3;
2538 trace_mm[p_xs_3->
chippix[2]-1]=morder_cnt;
2539 chipdist_current=p_xs_3->
chippix[2]+1;
2546 pixwavlast=lambda_nm;
2548 for (ii=1;ii<
SIZE;ii+=1) {
2549 fprintf(trace_out,
"%d 0 %lf %lf %d %lf %lf\n", trace_mm[ii], trace_lam[ii], trace_xdisp[ii], ii, trace_flux[ii], trace_flux[ii]/(trace_lam[ii]-trace_lam[ii-1]));
2550 fprintf(trace_out1,
"%d %lf o%d\n", ii, trace_flux1[ii], trace_mm[ii]);
2551 fprintf(trace_out2,
"%d %lf o%d\n", ii, trace_flux2[ii], trace_mm[ii]);
2552 fprintf(trace_out3,
"%d %lf o%d\n", ii, trace_flux3[ii], trace_mm[ii]);
2556 cpl_free(trace_lam);
2557 cpl_free(trace_xdisp);
2558 cpl_free(trace_flux);
2559 cpl_free(trace_flux1);
2560 cpl_free(trace_flux2);
2561 cpl_free(trace_flux3);
2565 cpl_msg_error(__func__,
"Cannot free 2D array ref_ind");
2573 if (THEtabflag==1) {
2575 sprintf(name_o,
"THE.fits") ;
2576 cpl_msg_info(__func__,
"Writing %s" , name_o) ;
2579 THEplist = cpl_propertylist_new();
2580 cpl_propertylist_append_string(THEplist,
"INSTRUME",
"XSHOOTER") ;
2583 THEproduct_frame = cpl_frame_new() ;
2584 cpl_frame_set_filename(THEproduct_frame, name_o) ;
2585 cpl_frame_set_tag(THEproduct_frame,
"XSHOOTER_THE") ;
2586 cpl_frame_set_type(THEproduct_frame, CPL_FRAME_TYPE_TABLE);
2587 cpl_frame_set_group(THEproduct_frame, CPL_FRAME_GROUP_PRODUCT);
2588 cpl_frame_set_level(THEproduct_frame, CPL_FRAME_LEVEL_FINAL);
2589 cpl_table_set_size(THE_tab,fitsrow);
2590 cpl_msg_info(__func__,
"Save the table") ;
2591 if (cpl_table_save(THE_tab, NULL, THEplist, name_o,
2592 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
2593 cpl_msg_error(__func__,
"Cannot write the table") ;
2596 cpl_frame_delete(THEproduct_frame) ;
2597 cpl_table_delete(THE_tab) ;
2598 cpl_propertylist_delete(THEplist) ;
2601 naxes[0]=p_xs_3->
ASIZE;
2602 naxes[1]=p_xs_3->
BSIZE;
2605 if (readout_flag==1) {
2607 for (ii=0;ii<p_xs_3->
BSIZE;ii++) {
2608 for (jj=0;jj<p_xs_3->
ASIZE;jj++) {
2610 prob=prob/2147483648.0;
2612 prob2=prob2/2147483648.0;
2613 gauss1=rn*sqrt(-2.0*log(1-prob))*sin(2*
M_PI*prob2);
2614 gauss2=rn*sqrt(-2.0*log(1-prob))*cos(2*
M_PI*prob2);
2615 ccd[ii][jj]+=gauss1*rn;
2623 ima = cpl_image_wrap_double(naxes[0], naxes[1], oneD_AB) ;
2624 cpl_image_save(ima,
"chips_out.fits", CPL_BPP_IEEE_DOUBLE, NULL, CPL_IO_DEFAULT) ;
2625 cpl_image_unwrap(ima) ;
2628 if (BackMapFlag==1) {
2630 ima2 = cpl_image_wrap_double(naxes[0], naxes[1], oneD_AB) ;
2631 cpl_image_save(ima2,
"chips_wav.fits", CPL_BPP_IEEE_DOUBLE, NULL, CPL_IO_DEFAULT) ;
2632 cpl_image_unwrap(ima2) ;
2635 cpl_msg_error(__func__,
"Cannot free 2D array ccd_wav");
2640 cpl_msg_error(__func__,
"Cannot free 2D array ccd");
2648 if (rec_out != NULL) fclose(rec_out);
2649 if (ex1d_out != NULL) fclose(ex1d_out);
2650 if (cpl_error_get_code() != CPL_ERROR_NONE) {
static xsh_instrument * instrument
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
#define xsh_error_dump(level)
#define check_msg(COMMAND,...)
#define XSH_ASSURE_NOT_NULL(pointer)
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
void xsh_instrument_set_biny(xsh_instrument *instrument, const int biny)
void xsh_instrument_set_binx(xsh_instrument *instrument, const int binx)
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
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
xsh_instrument * xsh_instrument_new(void)
create new 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.
double * xsh_copy2D_to_1D(double **ccdtemp, int asize, int bsize)
copy 2D to 1D array
void xsh_ref_ind_read(int arm, DOUBLE **ref_ind_T, DOUBLE temper)
Load the refractive index coeffs for a given arm and temperature.
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.
void xsh_3_detpix(struct xs_3 *p_xs_3)
Takes the physical x,y position at the detector array and converts this to a pixel position.
cpl_frame * xsh_model_order_edges_tab_create(xsh_xs_3 *p_xs_3, const char *tab_filename)
Create an order edges table using the phys mod.
void xsh_model_binxy(xsh_xs_3 *p_xs_3, int bin_X, int bin_Y)
corrects model for detector's binning
void xsh_3_eval(DOUBLE lambda, int morder, DOUBLE **ref_ind, struct xs_3 *p_xs_3)
Compute the physical x,y position at the detector array for a given wavelength, order and parameter c...
cpl_frame * xsh_model_spectralformat_create(xsh_xs_3 *p_xs_3, const char *tab_filename)
creates the model spectral format table
DOUBLE * xsh_alloc1Darray(int asize)
allocates memory for an array of doubles
int xsh_free2Darray(double **ccdtemp, int asize)
free memory for a 2D array of doubles
int * xsh_alloc1Darray_INT(int asize)
allocates memory for an array of integers
void xsh_3_init(struct xs_3 *p_xs_3)
Pre-compute a number of non-wavelength dependent secondary parameters required by the model.
cpl_error_code xsh_model_maps_create(xsh_xs_3 *p_xs_3, xsh_instrument *instr, const char *wtag, const char *stag, cpl_frame **wmap_frame, cpl_frame **smap_frame, const int save_tmp)
Compute the wavelength and slit maps.
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)
double ** xsh_alloc2Darray(int asize, int bsize)
allocates memory for a 2D array of doubles
cpl_vector ** xsh_model_locus(struct xs_3 *p_xs_3, xsh_instrument *instr, double ent_slit_pos)
Compute the locus of the spectrum.
static int xsh_model_create(cpl_plugin *)
Setup the recipe options.
static int xsh_model(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
static int xsh_model_reduce(const char *, const char *)
The actual model computation takes place here.
static char xsh_model_compute_description[]
static const int vectordim
static int xsh_model_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
static char xsh_model_compute_description_short[]
static int xsh_model_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
#define xsh_msg(...)
Print a message on info level.
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
int xsh_debug_level_set(int level)
set debug level
const char * xsh_get_license(void)
Get the pipeline copyright and license.
void xsh_init(void)
Reset library state.
#define XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH
#define XSH_ARCLIST_TABLE_COLNAME_FLUX
cpl_frame * xsh_find_model_wavelist(cpl_frameset *frames, xsh_instrument *instr)
Find model wavelength list frame.
cpl_frame * xsh_find_model_testpar(cpl_frameset *frames, xsh_instrument *instr)
Find model test parameter list.
void xsh_model_io_output_cfg_txt(struct xs_3 *p_xs_3)
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)