00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00037
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <xsh_error.h>
00049
00050 #include <xsh_utils.h>
00051 #include <xsh_msg.h>
00052 #include <xsh_data_instrument.h>
00053
00054 #include <xsh_dfs.h>
00055
00056 #include <xsh_drl.h>
00057
00058 #include <cpl.h>
00059
00060 #include <math.h>
00061 #include "xsh_model_kernel.h"
00062 #include "xsh_model_arm_constants.h"
00063 #include "xsh_model_io.h"
00064 #include "xsh_pfits.h"
00065 #include <stdlib.h>
00066 #include <xsh_data_order.h>
00067 #include <xsh_data_arclist.h>
00068 #include <xsh_utils_table.h>
00069
00070
00071
00072
00073
00074 #define RECIPE_ID "xsh_model_compute"
00075 #define RECIPE_AUTHOR "P. Bristow"
00076 #define RECIPE_CONTACT "bristowp@eso.org"
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 static int xsh_model_create(cpl_plugin *);
00087 static int xsh_model_exec(cpl_plugin *);
00088 static int xsh_model_destroy(cpl_plugin *);
00089
00090
00091 static int xsh_model(cpl_parameterlist *, cpl_frameset *);
00092 static int xsh_model_reduce(const char *, const char *) ;
00093
00094
00095
00096 static char xsh_model_compute_description_short[] = "Development Only";
00097
00098 static char xsh_model_compute_description[] =
00099 "This recipe is for development use only.\n";
00100
00101
00102
00103 static const int vectordim=4;
00104
00105
00106
00107
00108
00109
00118
00119
00120 int cpl_plugin_get_info(cpl_pluginlist * list)
00121 {
00122 cpl_recipe *recipe = NULL;
00123 cpl_plugin *plugin = NULL;
00124
00125 recipe = cpl_calloc(1, sizeof(*recipe));
00126 if (recipe == NULL) {
00127 return -1;
00128 }
00129
00130 plugin = &recipe->interface;
00131
00132 cpl_plugin_init(plugin, CPL_PLUGIN_API,
00133 XSH_BINARY_VERSION,
00134 CPL_PLUGIN_TYPE_RECIPE,
00135 RECIPE_ID,
00136 xsh_model_compute_description_short,
00137 xsh_model_compute_description,
00138 RECIPE_AUTHOR,
00139 RECIPE_CONTACT,
00140 xsh_get_license(),
00141 xsh_model_create,
00142 xsh_model_exec,
00143 xsh_model_destroy);
00144
00145 cpl_pluginlist_append(list, plugin);
00146
00147 return (cpl_error_get_code() != CPL_ERROR_NONE);
00148 }
00149
00159
00160 static int xsh_model_create(cpl_plugin * plugin)
00161 {
00162 cpl_recipe * recipe ;
00163 cpl_parameter * p ;
00164
00165
00166 xsh_init();
00167
00168
00169 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00170 recipe = (cpl_recipe *)plugin ;
00171 else return -1 ;
00172
00173
00174 recipe->parameters = cpl_parameterlist_new() ;
00175
00176
00177
00178
00179 p = cpl_parameter_new_enum("xsh.xsh_model_compute.arm",
00180 CPL_TYPE_STRING,
00181 "Arm setting: ",
00182 "xsh.xsh_model_compute",
00183 "vis",
00184 3,"uvb","vis","nir");
00185
00186 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"arm");
00187 cpl_parameterlist_append(recipe->parameters, p);
00188
00189
00190 return 0;
00191 }
00192
00193
00194
00200
00201
00202 static int xsh_model_exec(cpl_plugin * plugin)
00203 {
00204 cpl_recipe * recipe ;
00205
00206
00207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00208 recipe = (cpl_recipe *)plugin ;
00209 else return -1 ;
00210
00211 return xsh_model(recipe->parameters, recipe->frames) ;
00212 }
00213
00214
00215
00221
00222 static int xsh_model_destroy(cpl_plugin * plugin)
00223 {
00224 cpl_recipe * recipe ;
00225
00226
00227 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00228 recipe = (cpl_recipe *)plugin ;
00229 else return -1 ;
00230
00231 cpl_parameterlist_delete(recipe->parameters) ;
00232 return 0 ;
00233 }
00234
00235
00236
00244
00245 static int xsh_model(
00246 cpl_parameterlist * parlist,
00247 cpl_frameset * frameset)
00248 {
00249
00250 const char * fctid = "xsh_model" ;
00251 cpl_parameter * param ;
00252 cpl_frame * wave_list ;
00253 cpl_frame * test_par ;
00254 const char* arm=NULL;
00255 xsh_instrument* instrument = NULL;
00256
00257
00258
00259 param = cpl_parameterlist_find(parlist,"xsh.xsh_model_compute.arm");
00260 arm = cpl_parameter_get_string(param);
00261
00262 instrument=xsh_instrument_new();
00263 if(strcmp(arm,"uvb") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_UVB);
00264 if(strcmp(arm,"vis") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_VIS);
00265 if(strcmp(arm,"nir") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_NIR);
00266
00267 if (cpl_error_get_code()) {
00268 cpl_msg_error(fctid, "Failed to retrieve the input parameters") ;
00269 return -1 ;
00270 }
00271
00272
00273 check(wave_list = xsh_find_model_wavelist(frameset,instrument)) ;
00274 check(test_par = xsh_find_model_testpar(frameset,instrument)) ;
00275
00276 if (cpl_error_get_code()) {
00277 cpl_msg_error(fctid, "Some input file(s) is(are) missing") ;
00278 return -1 ;
00279 }
00280
00281
00282 check_msg(xsh_model_reduce(cpl_frame_get_filename(wave_list),
00283 cpl_frame_get_filename(test_par)),
00284 "Failed computing the model") ;
00285
00286
00287 cleanup:
00288
00289 xsh_instrument_free(&instrument);
00290
00291
00292 if (cpl_error_get_code())
00293 return -1 ;
00294 else
00295 return 0 ;
00296 }
00297
00298
00305
00306
00307
00308 static int
00309 xsh_model_reduce(
00310 const char * wave_list_file,
00311 const char * test_par_file)
00312 {
00313 int ii,morder, morder_cnt;
00314 int morder_it,morder_min,morder_max, n_order;
00315 int photons;
00316 int fitsrow, tab_size;
00317 int test_p,temp_fs=0, spos_int;
00318 char test_string[20];
00319 DOUBLE lambda;
00320 DOUBLE lambda_nm;
00321 DOUBLE lam_range, blaze_wav, blaze_pad, lam_min, lam_max;
00322 DOUBLE pixwavlast, pixylast, pixxlast;
00323 int chipdist_current = 0;
00324 DOUBLE inten, blaze, p_dif, sinc_arg;
00325
00326 DOUBLE prob, prob2, prob3, prob4;
00327 DOUBLE gauss1, gauss2, gauss3;
00328 DOUBLE es_x_init, es_y_init, spos;
00329 DOUBLE x1, xdet2xslit, xdet2yslit, ydet2xslit, ydet2yslit;
00330
00331 DOUBLE y_1, xslit2xdet, xslit2ydet;
00332 DOUBLE yslit2xdet, yslit2ydet;
00333
00334 DOUBLE flux_scale, bg_sig=0.0, ccd_sig;
00335 int trace_out_flag;
00336 double* trace_lam=NULL;
00337 double* trace_xdisp=NULL;
00338 double* trace_flux=NULL;
00339 double* trace_flux1=NULL;
00340 double* trace_flux2=NULL;
00341 double* trace_flux3=NULL;
00342 int* trace_mm=NULL;
00343 int half_prof_wid=20;
00344 int flag, bgflag, BackMapFlag, THEtabflag, readout_flag;
00345 int THEtxtflag, atmosflag, testfuncflag, det_slitflag;
00346 int continuum;
00347
00348 double num_ph;
00349 double fwhm_ph=1.0;
00350 double sep_ph;
00351 double profile[6001],profile_w[301];
00352 DOUBLE prof_scale_s, prof_scale_w;
00353
00354 DOUBLE lines[3][10000];
00355 int line_no, total_lines;
00356 int line_cnt, lines_tot;
00357 cpl_table* lines_tab ;
00358 long naxes[2];
00359 double rn;
00360
00361 int bin_X=1;
00362 int bin_Y=1;
00363
00364
00365
00366
00367 struct xs_3 xs_3_model;
00368 struct xs_3* p_xs_3;
00369
00370
00371
00372
00373 ann_all_par all_par[100],* p_all_par;
00374
00375 int arm_ef_ord0,arm_lam_min,arm_lam_fac,jj,SIZE;
00376 const char* cfg_file=NULL;
00377 const char* tag=NULL;
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 int proftype;
00390 int prof_data_size;
00391 double at_tran;
00392 FILE* fin,* efin,* profin,* qe_in,* dichro_in,* tran_in;
00393 double** ef=NULL;
00394 double ef_corr,ef_corr_down,ef_corr_up,substep,pfac,pfac2;
00395 double phot_res,tot_phot,nphot;
00396 int phot_in;
00397 double qe[102];
00398 int qe_wv_min, qe_wv_step;
00399 double dummy,qe_corr_down,qe_corr_up,qe_corr;
00400 double dichro[2400];
00401 double dichro_corr_down,dichro_corr_up,dichro_corr;
00402 double temp[3];
00403 int dichro_col;
00404 double tran[50000];
00405 double tran_corr_down,tran_corr_up;
00406 double tran_wv_max;
00407 double tran_wv_min;
00408 double tran_wv_step;
00409 DOUBLE fsc;
00410
00411 cpl_table* THE_tab = NULL;
00412 cpl_propertylist* THEplist = NULL;
00413 cpl_frame* THEproduct_frame = NULL;
00414 char name_o[512] ;
00415
00416 double** ccd=NULL;
00417 double** ccd_wav=NULL;
00418 double** ref_ind;
00419 FILE* trace_out;
00420 FILE* trace_out1;
00421 FILE* trace_out2;
00422 FILE* trace_out3;
00423 cpl_image* ima ;
00424 cpl_image* ima2 ;
00425 double* oneD_AB;
00426 cpl_frame* config_frame = NULL;
00427 struct xs_3* p_xs_3_config;
00428
00429 p_xs_3=&xs_3_model;
00430 p_all_par=&all_par[0];
00431 tran_in=fopen("tran.dat","r");
00432 dichro_in=fopen("dichroics.dat","r");
00433
00434
00435 for (ii=0;ii<154321;ii++) {
00436 prob=rand();
00437
00438 }
00439 flux_scale=100.0;
00440 trace_out_flag=0;
00441 readout_flag=0;
00442 flag=0;
00443 bgflag=1;
00444 BackMapFlag=0;
00445 THEtabflag=0;
00446 THEtxtflag=0;
00447 det_slitflag=0;
00448 testfuncflag=0;
00449 atmosflag=0;
00450 continuum=0;
00451 num_ph=1.0;
00452 fwhm_ph=1.0;
00453
00454 for (ii=0;ii<6001;ii++) profile[ii]=0.0;
00455 for (ii=0;ii<301;ii++) profile_w[ii]=0.0;
00456 for (ii=0;ii<10000;ii++) {
00457 lines[0][ii]=0.0;
00458 lines[1][ii]=0.0;
00459 lines[2][ii]=0.0;
00460 }
00461 naxes[0]=0;
00462 naxes[1]=0;
00463 at_tran=1.0;
00464 for (ii=0;ii<102;ii++) qe[ii]=0.0;
00465 for (ii=0;ii<2400;ii++) dichro[ii]=0.0;
00466 for (ii=0;ii<50000;ii++) tran[ii]=0.0;
00467 tran_wv_max=2500.0;
00468 tran_wv_min=903.0;
00469 tran_wv_step=0.5;
00470
00471
00472
00473 fin=fopen(test_par_file,"r");
00474 if(fin==NULL) {
00475 printf("couldn't find test parameter settings file %s\n",
00476 test_par_file);
00477 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00478 return -1;
00479 }
00480
00481 proftype=2;
00482 while (fscanf(fin,"%s %d\n",&test_string, &test_p) !=EOF) {
00483
00484 if (strncmp(test_string,"arm",3)==0) {
00485 p_xs_3->arm=test_p;
00486 }
00487 if (strncmp(test_string,"proftype",14)==0) {
00488 proftype=test_p;
00489 }
00490 if (strncmp(test_string,"trace_out_flag",14)==0) {
00491 trace_out_flag=test_p;
00492 }
00493 if (strncmp(test_string,"BackMapFlag",11)==0) {
00494 BackMapFlag=test_p;
00495 }
00496 if (strncmp(test_string,"bgflag",6)==0) {
00497 bgflag=test_p;
00498 }
00499 if (strncmp(test_string,"flux_scale1000",14)==0) {
00500 temp_fs=test_p;
00501 flux_scale=(double)(test_p)/1000.0;
00502
00503 }
00504 if (strncmp(test_string,"THEtab",6)==0) {
00505 THEtabflag=test_p;
00506 }
00507 if (strncmp(test_string,"Atmos",5)==0) {
00508 atmosflag=test_p;
00509 }
00510 if (strncmp(test_string,"bin_X",5)==0) {
00511 bin_X=test_p;
00512 }
00513 if (strncmp(test_string,"bin_Y",5)==0) {
00514 bin_Y=test_p;
00515 }
00516 if (strncmp(test_string,"THEtxt",6)==0) {
00517 THEtxtflag=test_p;
00518 }
00519 if (strncmp(test_string,"det_slit",6)==0) {
00520 det_slitflag=test_p;
00521 }
00522 if (strncmp(test_string,"testfunc",8)==0) {
00523 testfuncflag=test_p;
00524 }
00525 if (strncmp(test_string,"bg_sig",8)==0) {
00526 bg_sig=(double)(test_p);
00527 }
00528 }
00529 fclose(fin);
00530
00531 flux_scale=(double)(temp_fs)/1000.0;
00532 fsc=200.0;
00533
00534
00535
00536
00537
00538 if (p_xs_3->arm==0) {
00539 p_xs_3->BSIZE=UVB_BSIZE;
00540 p_xs_3->ASIZE=UVB_ASIZE;
00541 p_xs_3->SIZE=UVB_SIZE;
00542 p_xs_3->chipxpix=UVB_chipxpix;
00543 p_xs_3->chipypix=UVB_chipypix;
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564 cfg_file="/home/bristowp/allscripts/x-s/cfg_edge.fits";
00565
00566
00567 tag=XSH_MOD_CFG_UVB;
00568 arm_ef_ord0=14;
00569 arm_lam_min=200;
00570 arm_lam_fac=1;
00571 n_order=13;
00572 ef=xsh_alloc2Darray(n_order,501);
00573 efin=fopen("effic_uvb.dat","r");
00574 ii=0;
00575 while (fscanf(efin,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",&ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],&ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],&ef[10][ii],&ef[11][ii]) !=EOF) {
00576
00577
00578
00579
00580
00581
00582 ii++;
00583 }
00584 fclose(efin);
00585 morder=UVB_morder;
00586 morder_it=5;
00587 morder_min=UVB_morder_min;
00588 morder_max=UVB_morder_max;
00589 blaze_pad=0.000015;
00590 dichro_col=0;
00591 qe_in=fopen("uvb_qe.dat","r");
00592 qe_wv_min=300;
00593 qe_wv_step=10;
00594 sep_ph=0.357;
00595 }
00596 else if (p_xs_3->arm==1) {
00597 p_xs_3->BSIZE=VIS_BSIZE;
00598 p_xs_3->ASIZE=VIS_ASIZE;
00599 p_xs_3->SIZE=VIS_SIZE;
00600 p_xs_3->chipypix=VIS_chipypix;
00601 p_xs_3->chipxpix=VIS_chipxpix;
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619 cfg_file="xs_vis_def_aug10.fits";
00620 tag=XSH_MOD_CFG_VIS;
00621 arm_ef_ord0=12;
00622 arm_lam_min=400;
00623 arm_lam_fac=1;
00624 n_order=15;
00625 ef=xsh_alloc2Darray(n_order,701);
00626 efin=fopen("effic_vis.dat","r");
00627 ii=0;
00628 while (fscanf(efin,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",&ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],&ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],&ef[10][ii],&ef[11][ii],&ef[12][ii],&ef[13][ii],&ef[14][ii]) !=EOF) {
00629 ii++;
00630 }
00631 fclose(efin);
00632 morder=VIS_morder;
00633 morder_it=7;
00634 morder_min=VIS_morder_min;
00635 morder_max=VIS_morder_max;
00636 blaze_pad=0.00003;
00637 dichro_col=1;
00638 qe_in=fopen("vis_qe.dat","r");
00639 qe_wv_min=320;
00640 qe_wv_step=10;
00641 sep_ph=0.357;
00642 }
00643 else {
00644 p_xs_3->ASIZE=NIR_ASIZE;
00645 p_xs_3->BSIZE=NIR_BSIZE;
00646 p_xs_3->SIZE=NIR_SIZE;
00647 p_xs_3->chipxpix=NIR_chipxpix;
00648 p_xs_3->chipypix=NIR_chipypix;
00649
00650
00651
00652
00653
00654
00655
00656
00657 cfg_file="/scratch/bristowp/IFU_analysis/IFU_DATA/2dmap/PROD_XSH_MOD_CFG_OPT_2D_NIR.fits";
00658 tag=XSH_MOD_CFG_NIR;
00659 arm_ef_ord0=13;
00660 arm_lam_min=900;
00661 arm_lam_fac=1;
00662 n_order=21;
00663 ef=xsh_alloc2Darray(n_order,851);
00664 efin=fopen("effic_nir.dat","r");
00665 ii=0;
00666 while (fscanf(efin,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",&ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],&ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],&ef[10][ii],&ef[11][ii],&ef[12][ii],&ef[13][ii],&ef[14][ii],&ef[15][ii],&ef[16][ii],&ef[17][ii],&ef[18][ii],&ef[19][ii],&ef[20][ii]) !=EOF) {
00667 ii++;
00668 }
00669 fclose(efin);
00670 morder=NIR_morder;
00671 morder_it=8;
00672 morder_min=NIR_morder_min;
00673 morder_max=NIR_morder_max;
00674 blaze_pad=0.00005;
00675 dichro_col=2;
00676 qe_in=fopen("nir_qe.dat","r");
00677 qe_wv_min=800;
00678 qe_wv_step=20;
00679 sep_ph=0.743162;
00680 }
00681
00682
00683
00684
00685 if (testfuncflag==11) {
00686
00687 struct xs_3 xs_3_config;
00688
00689
00690 cpl_frame* wav_frame = NULL;
00691 cpl_frame* slit_frame = NULL;
00692 xsh_instrument* inst=NULL;
00693 cpl_error_code error_maps;
00694 const char * wtag;
00695 const char * stag;
00696 xsh_free_frame(&config_frame);
00697 p_xs_3_config=&xs_3_config;
00698 config_frame = cpl_frame_new();
00699 cpl_frame_set_filename( config_frame, cfg_file);
00700 cpl_frame_set_tag( config_frame,tag);
00701 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
00702 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
00703 return -1;
00704 }
00705
00706 inst=xsh_instrument_new();
00707 if (p_xs_3_config->arm==0) {
00708 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
00709 }
00710 else if (p_xs_3_config->arm==1) {
00711 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
00712 }
00713 else if (p_xs_3_config->arm==2) {
00714 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
00715 }
00716 else {
00717 printf("ARM NOT SET\n");
00718 }
00719
00720 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
00721
00722 xsh_instrument_set_binx(inst,bin_X);
00723 xsh_instrument_set_biny(inst,bin_Y);
00724
00725 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
00726 wtag="chips_wav_test";
00727 stag="chips_slit_test";
00728 error_maps=xsh_model_maps_create(p_xs_3_config,inst,wtag,stag, &wav_frame, &slit_frame);
00729
00730 xsh_instrument_free(&inst);
00731 }
00732
00733
00734
00735
00736
00737
00738
00739 if (testfuncflag==2) {
00740 double x,y;
00741 struct xs_3 xs_3_config;
00742
00743 xsh_instrument* inst=NULL;
00744 xsh_free_frame(&config_frame);
00745
00746 p_xs_3_config=&xs_3_config;
00747
00748 config_frame = cpl_frame_new();
00749 cpl_frame_set_filename( config_frame, cfg_file);
00750 cpl_frame_set_tag( config_frame, tag);
00751
00752 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
00753 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
00754 return -1 ;
00755 }
00756
00757 inst=xsh_instrument_new();
00758
00759 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
00760 xsh_instrument_set_binx(inst,bin_X);
00761 xsh_instrument_set_biny(inst,bin_Y);
00762
00763 if (p_xs_3_config->arm==0) {
00764 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
00765
00766 xsh_model_get_xy(p_xs_3_config,inst,404.656494,18,0.0, &x, &y);
00767 printf("404.656494nm in order 18 on arm %d : x=%lf y=%lf \n",
00768 xsh_instrument_get_arm(inst),x,y);
00769
00770 xsh_model_get_xy(p_xs_3_config,inst,385.11790,20,-5.50696, &x, &y);
00771 printf("385.11790nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
00772 xsh_instrument_get_arm(inst),x,y);
00773 xsh_model_get_xy(p_xs_3_config,inst,385.11790,20,-5.49536, &x, &y);
00774 printf("385.11790nm at s=-5.49536 in order 20 on arm %d : x=%lf y=%lf \n",
00775 xsh_instrument_get_arm(inst),x,y);
00776 xsh_model_get_xy(p_xs_3_config,inst,385.11790,20,-5.51854, &x, &y);
00777 printf("385.11790nm at s=-5.51854 in order 20 on arm %d : x=%lf y=%lf \n",
00778 xsh_instrument_get_arm(inst),x,y);
00779 xsh_model_get_xy(p_xs_3_config,inst,385.11940,20,-5.50696, &x, &y);
00780 printf("385.11940nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
00781 xsh_instrument_get_arm(inst),x,y);
00782 xsh_model_get_xy(p_xs_3_config,inst,385.11590,20,-5.50696, &x, &y);
00783 printf("385.11590nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
00784 xsh_instrument_get_arm(inst),x,y);
00785
00786 xsh_model_get_xy(p_xs_3_config,inst,369.4774,20,-2.50531, &x, &y);
00787 printf("369.4774nm at s=-2.50531 in order 20 on arm %d : x=%lf y=%lf \n",
00788 xsh_instrument_get_arm(inst),x,y);
00789 }
00790 else if (p_xs_3_config->arm==1) {
00791 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
00792 xsh_model_get_xy(p_xs_3_config,inst,649.06520,25,-3.85736,&x,&y);
00793 printf("649.06520nm in order 25 on arm %d : x=%lf y=%lf \n",
00794 xsh_instrument_get_arm(inst),x,y);
00795 xsh_model_get_xy(p_xs_3_config,inst,676.0587,24,-1.9593574,&x,&y);
00796 printf("676.0587nm in order 24 on arm %d : x=%lf y=%lf \n",
00797 xsh_instrument_get_arm(inst),x,y);
00798 xsh_model_get_xy(p_xs_3_config,inst,573.0815,29,-0.6741213,&x,&y);
00799 printf("573.0815nm in order 29 on arm %d : x=%lf y=%lf \n",
00800 xsh_instrument_get_arm(inst),x,y);
00801 xsh_model_get_xy(p_xs_3_config,inst,871.2175,18,2.8843977,&x,&y);
00802 printf("871.2175nm in order 18 on arm %d : x=%lf y=%lf \n",
00803 xsh_instrument_get_arm(inst),x,y);
00804 xsh_model_get_xy(p_xs_3_config,inst,676.0599,24,-1.9593425,&x,&y);
00805 printf("676.0599nm in order 24 on arm %d : x=%lf y=%lf \n",
00806 xsh_instrument_get_arm(inst),x,y);
00807 xsh_model_get_xy(p_xs_3_config,inst,573.0818,29,-0.6741213,&x,&y);
00808 printf("573.0818nm in order 29 on arm %d : x=%lf y=%lf \n",
00809 xsh_instrument_get_arm(inst),x,y);
00810 xsh_model_get_xy(p_xs_3_config,inst,871.2192,18,2.9050856,&x,&y);
00811 printf("871.2192nm in order 18 on arm %d : x=%lf y=%lf \n",
00812 xsh_instrument_get_arm(inst),x,y);
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852 }
00853 else if (p_xs_3_config->arm==2) {
00854 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
00855 xsh_model_get_xy(p_xs_3_config,inst,1382.36,19,-0.7106,&x,&y);
00856 printf("1382.36nm in order 19 on arm %d : x=%lf y=%lf \n",
00857 xsh_instrument_get_arm(inst),x,y);
00858 xsh_model_get_xy(p_xs_3_config,inst,1382.37,19,-0.7106,&x,&y);
00859 printf("1382.37nm in order 19 on arm %d : x=%lf y=%lf \n",
00860 xsh_instrument_get_arm(inst),x,y);
00861 xsh_model_get_xy(p_xs_3_config,inst,1382.38,19,-0.7106,&x,&y);
00862 printf("1382.38nm in order 19 on arm %d : x=%lf y=%lf \n",
00863 xsh_instrument_get_arm(inst),x,y);
00864 xsh_model_get_xy(p_xs_3_config,inst,1382.108,19,5.55761,&x,&y);
00865 printf("1382.108nm at s=5.55761 in order 19 on arm %d : x=%lf y=%lf \n",
00866 xsh_instrument_get_arm(inst),x,y);
00867 }
00868 else {
00869 printf("ARM NOT SET\n");
00870 }
00871
00872
00873
00874
00875 xsh_instrument_free(&inst);
00876 cpl_frame_delete(config_frame) ;
00877 }
00878
00879
00880
00881
00882
00883
00884
00885 if (testfuncflag==3) {
00886 cpl_vector** trace;
00887 struct xs_3 xs_3_config;
00888
00889 int tempsize=p_xs_3->BSIZE;
00890 xsh_instrument* inst=NULL;
00891
00892 xsh_free_frame(&config_frame);
00893
00894
00895 p_xs_3_config=&xs_3_config;
00896
00897 config_frame = cpl_frame_new();
00898 cpl_frame_set_filename( config_frame, cfg_file);
00899 cpl_frame_set_tag( config_frame, tag);
00900
00901 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
00902 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
00903 return -1 ;
00904 }
00905 inst=xsh_instrument_new();
00906 xsh_instrument_set_binx(inst,bin_X);
00907 xsh_instrument_set_biny(inst,bin_Y);
00908 if (p_xs_3_config->arm==0) {
00909 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
00910 }
00911 else if (p_xs_3_config->arm==1) {
00912 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
00913 }
00914 else if (p_xs_3_config->arm==2) {
00915 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
00916 }
00917 else {
00918 printf("ARM NOT SET\n");
00919 }
00920 trace=xsh_model_locus(p_xs_3_config,inst,0.0);
00921 for (morder=morder_min;morder<=morder_max;morder+=1) {
00922 for (ii=0;ii<tempsize;ii+=1) {
00923 printf("order %d dispersion pixel no: %d, x-dispersion: %lf \n",
00924 morder, ii, cpl_vector_get(trace[morder-morder_min],ii));
00925 }
00926 }
00927 for (ii=0 ; ii<16 ; ii++) cpl_vector_delete(trace[ii]);
00928 cpl_free(trace) ;
00929 xsh_instrument_free(&inst);
00930 cpl_frame_delete(config_frame) ;
00931 }
00932
00933
00934
00935
00936
00937
00938
00939 if (testfuncflag==5) {
00940 struct xs_3 xs_3_config;
00941
00942
00943 cpl_frame* temp_sf_frame=NULL ;
00944
00945 p_xs_3_config=&xs_3_config;
00946
00947 config_frame = cpl_frame_new();
00948 cpl_frame_set_filename( config_frame, cfg_file);
00949 cpl_frame_set_tag( config_frame, tag);
00950
00951 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
00952 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
00953 return -1 ;
00954 }
00955
00956 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
00957 check(temp_sf_frame=xsh_model_spectralformat_create(p_xs_3_config,
00958 "SFtestTAB.fits"));
00959
00960 xsh_msg("SF2 table %s %s", cpl_frame_get_filename(temp_sf_frame),
00961 cpl_frame_get_tag(temp_sf_frame));
00962
00963 cpl_frame_delete(config_frame) ;
00964 xsh_free_frame(&temp_sf_frame);
00965
00966 }
00967
00968
00969
00970
00971
00972
00973
00974
00975 if (testfuncflag==4) {
00976 struct xs_3 xs_3_config;
00977
00978 xsh_instrument* inst=NULL;
00979 cpl_frame * temp_THEproduct_frame ;
00980
00981
00982 xsh_free_frame(&config_frame);
00983 p_xs_3_config=&xs_3_config;
00984
00985 config_frame = cpl_frame_new();
00986 cpl_frame_set_filename( config_frame, cfg_file);
00987 cpl_frame_set_tag( config_frame,tag);
00988
00989 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
00990 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
00991 return -1 ;
00992 }
00993 inst=xsh_instrument_new();
00994 xsh_instrument_set_binx(inst,bin_X);
00995 xsh_instrument_set_biny(inst,bin_Y);
00996
00997 if (p_xs_3_config->arm==0) {
00998 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
00999 }
01000 else if (p_xs_3_config->arm==1) {
01001 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
01002 }
01003 else if (p_xs_3_config->arm==2) {
01004 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
01005 }
01006 else {
01007 printf("ARM NOT SET\n");
01008 }
01009 xsh_model_binxy(p_xs_3_config,1,1);
01010 fitsrow=0;
01011 if (xsh_instrument_get_arm(inst)<2) {
01012 temp_THEproduct_frame=xsh_model_THE_create(p_xs_3_config,inst,
01013 wave_list_file,1,0.0,
01014 "THEtestTAB.fits");
01015 }
01016 else {
01017 temp_THEproduct_frame=xsh_model_THE_create(p_xs_3_config,inst,
01018 wave_list_file,9,1.4,
01019 "THEtestTAB.fits");
01020 }
01021
01022 xsh_msg(" THE table %s %s",
01023 cpl_frame_get_filename(temp_THEproduct_frame),
01024 cpl_frame_get_tag(temp_THEproduct_frame));
01025
01026 cpl_frame_delete(config_frame) ;
01027 xsh_free_frame(&temp_THEproduct_frame);
01028 xsh_instrument_free(&inst);
01029 }
01030
01031
01032
01033
01034
01035
01036
01037 if (testfuncflag==7) {
01038 cpl_image * im = NULL;
01039 cpl_image * wavmap = NULL;
01040 double* detec=NULL;
01041 double* wavmap_pix=NULL;
01042
01043 int h,kk, real_ord;
01044 cpl_vector * extracted;
01045 cpl_vector** trace;
01046 struct xs_3 xs_3_config;
01047 xsh_instrument* inst=NULL;
01048 char order_tab_name[256];
01049 cpl_propertylist *header = NULL;
01050 cpl_frame* order_tab_frame = NULL;
01051 cpl_table* table = NULL;
01052 int nbcol, med_wid;
01053 int starty, endy, order_tab_flag;
01054 xsh_order_list* order_tab = NULL;
01055 int order_tab_size = 0;
01056 double wvlen, dx, tot;
01057 cpl_vector * ext_med_fil;
01058
01059
01060
01061 xsh_free_frame(&config_frame);
01062 p_xs_3_config=&xs_3_config;
01063 config_frame = cpl_frame_new();
01064 cpl_frame_set_filename( config_frame, cfg_file);
01065 cpl_frame_set_tag( config_frame,tag);
01066 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
01067 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
01068 return -1;
01069 }
01070 inst=xsh_instrument_new();
01071 xsh_instrument_set_binx(inst,bin_X);
01072 xsh_instrument_set_biny(inst,bin_Y);
01073
01074 if (p_xs_3_config->arm==0) {
01075 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
01076 }
01077 else if (p_xs_3_config->arm==1) {
01078 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
01079 }
01080 else if (p_xs_3_config->arm==2) {
01081 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
01082 }
01083 else {
01084 printf("ARM NOT SET\n");
01085 }
01086
01087 order_tab_flag=0;
01088 if (order_tab_flag==0) {
01089 strcat(order_tab_name,"none");
01090 nbcol=morder_max-morder_min+1;
01091 trace=xsh_model_locus(p_xs_3_config,inst,0.0);
01092 }
01093 else {
01094
01095 strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_UVB.fits");
01096
01097
01098
01099 cknull( order_tab_name);
01100 order_tab_frame = cpl_frame_new();
01101 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
01102 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
01103 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
01104 XSH_TABLE_LOAD( table, order_tab_name);
01105 check( nbcol = cpl_table_get_nrow(table));
01106 if (nbcol == XSH_ORDERS_UVB){
01107 inst = xsh_instrument_new() ;
01108 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
01109 xsh_instrument_set_arm( inst, XSH_ARM_UVB);
01110 }
01111 else if (nbcol == XSH_ORDERS_VIS){
01112 inst = xsh_instrument_new() ;
01113 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
01114 xsh_instrument_set_arm( inst, XSH_ARM_VIS);
01115 }
01116 else if (nbcol == XSH_ORDERS_NIR){
01117 inst = xsh_instrument_new() ;
01118 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
01119 xsh_instrument_set_arm( inst, XSH_ARM_NIR);
01120 }
01121 else{
01122 xsh_msg("invalid nbcol %d", nbcol);
01123 }
01124 xsh_instrument_set_binx(inst,bin_X);
01125 xsh_instrument_set_biny(inst,bin_Y);
01126 check( order_tab = xsh_order_list_load( order_tab_frame, inst));
01127 order_tab_size = order_tab->size;
01128 check ( header = cpl_propertylist_load( order_tab_name, 0));
01129 XSH_TABLE_LOAD( table, order_tab_name);
01130 check( nbcol = cpl_table_get_nrow(table));
01131 }
01132
01133 lines_tab = cpl_table_load(wave_list_file, 1, 0);
01134 cpl_error_reset() ;
01135 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
01136
01137
01138 check(wavmap=cpl_image_load("xs_vis_Mar08_wav_map.fits", CPL_TYPE_DOUBLE, 0, 0));
01139
01140 check(wavmap_pix=cpl_image_get_data_double(wavmap));
01141
01142 check(extracted = cpl_vector_new(p_xs_3_config->SIZE)) ;
01143 check(im=cpl_image_load("9ph_test/FMTCHK_VIS_multi.fits", CPL_TYPE_DOUBLE, 0, 0));
01144
01145
01146
01147 check(detec=cpl_image_get_data_double(im));
01148 h=3;
01149 med_wid=200;
01150 for (ii=0;ii<nbcol;ii++) {
01151 real_ord=ii+morder_min;
01152 if (order_tab_flag!=0){
01153 starty = order_tab->list[ii].starty;
01154 endy = order_tab->list[ii].endy;
01155 }
01156 else {
01157 starty = 1;
01158 endy = p_xs_3_config->SIZE-201 ;
01159 }
01160 if (starty == 0 && endy == 0){
01161 xsh_msg("Warning starty and endy equal zero, put endy to %d",
01162 p_xs_3_config->SIZE-1);
01163 starty =0;
01164 endy = p_xs_3_config->SIZE-1;
01165 }
01166
01167 for (jj=0;jj<p_xs_3_config->SIZE;jj++) {
01168 cpl_vector_set(extracted,jj,0.0);
01169 }
01170
01171
01172
01173
01174 for (jj=starty;jj<endy;jj++) {
01175 tot=0;
01176 if (order_tab_flag!=0){
01177 check(dx=cpl_polynomial_eval_1d(order_tab->list[ii].cenpoly,jj, NULL));
01178 }
01179 else {
01180 dx=cpl_vector_get(trace[ii],jj);
01181 }
01182
01183 for (kk=-h; kk<=h; kk++) {
01184 if (p_xs_3_config->arm==0) {
01185 tot+=detec[2044*jj+(int)(dx+0.5)+kk];
01186 }
01187 else if (p_xs_3_config->arm==1) {
01188 tot+=detec[2044*jj+(int)(dx+0.5)+kk];
01189 }
01190 else {
01191 tot+=detec[1056*jj+((int)(dx+0.5)+kk)];
01192
01193 }
01194 }
01195 cpl_vector_set(extracted,jj,tot);
01196 }
01197 ext_med_fil=cpl_vector_filter_median_create(extracted,med_wid);
01198
01199
01200
01201 for (jj=0;jj<med_wid;jj++) {
01202 cpl_vector_set(ext_med_fil,jj,cpl_vector_get(ext_med_fil,med_wid+1));
01203 }
01204 for (jj=p_xs_3_config->SIZE-med_wid-2;jj<p_xs_3_config->SIZE;jj++) {
01205 cpl_vector_set(ext_med_fil,jj,cpl_vector_get(ext_med_fil,p_xs_3_config->BSIZE-med_wid-3));
01206 }
01207 cpl_vector_subtract(extracted,ext_med_fil);
01208 for (jj=starty;jj<endy;jj++) {
01209 if (order_tab_flag!=0){
01210 check(dx=cpl_polynomial_eval_1d(order_tab->list[ii].cenpoly,jj, NULL));
01211 }
01212 else {
01213 dx=cpl_vector_get(trace[ii],jj);
01214 }
01215 wvlen=wavmap_pix[(jj)*p_xs_3_config->ASIZE+(int)(dx)];
01216 if (wvlen>0.0) {
01217 printf("%d %d %lf %lf %lf \n",real_ord,jj,wvlen,dx,cpl_vector_get(extracted,jj));
01218 }
01219
01220 }
01221 cpl_vector_delete(ext_med_fil);
01222 }
01223 if (strcmp(order_tab_name,"none") == 0){
01224 for (ii=0 ; ii<16 ; ii++) cpl_vector_delete(trace[ii]);
01225 cpl_free(trace) ;
01226 }
01227 cpl_vector_delete(extracted);
01228 xsh_instrument_free(&inst);
01229 }
01230
01231
01232
01233
01234
01235
01236
01237
01238 if (testfuncflag==8) {
01239 cpl_image * im = NULL;
01240 cpl_image * wavmap = NULL;
01241 double* ex_detec=NULL;
01242 double* wavmap_pix=NULL;
01243 int n_ord=15;
01244 int ord_min=17;
01245 int kk, real_ord;
01246 cpl_vector * extracted;
01247 cpl_vector * bright_lines[n_ord] ;
01248 struct xs_3 xs_3_config;
01249
01250
01251 xsh_instrument* inst=NULL;
01252
01253 char order_tab_name[256];
01254 cpl_propertylist *header = NULL;
01255 cpl_frame* order_tab_frame = NULL;
01256 cpl_table* table = NULL;
01257 int nbcol;
01258
01259 int yint;
01260 xsh_order_list* order_tab = NULL;
01261 int order_tab_size = 0;
01262 double wvlen, dx;
01263
01264 xsh_free_frame(&config_frame);
01265 p_xs_3_config=&xs_3_config;
01266 config_frame = cpl_frame_new();
01267 cpl_frame_set_filename( config_frame, cfg_file);
01268 cpl_frame_set_tag( config_frame,tag);
01269 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
01270 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
01271 return -1;
01272 }
01273 inst=xsh_instrument_new();
01274 xsh_instrument_set_binx(inst,bin_X);
01275 xsh_instrument_set_biny(inst,bin_Y);
01276 if (p_xs_3_config->arm==0) {
01277 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
01278 }
01279 else if (p_xs_3_config->arm==1) {
01280 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
01281 }
01282 else if (p_xs_3_config->arm==2) {
01283 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
01284 }
01285 else {
01286 printf("ARM NOT SET\n");
01287 }
01288
01289 strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Mar08.fits");
01290
01291
01292
01293
01294 cknull( order_tab_name);
01295 order_tab_frame = cpl_frame_new();
01296 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
01297 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
01298 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
01299 XSH_TABLE_LOAD( table, order_tab_name);
01300 check( nbcol = cpl_table_get_nrow(table));
01301 if (nbcol == XSH_ORDERS_UVB){
01302 inst = xsh_instrument_new() ;
01303 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
01304 xsh_instrument_set_arm( inst, XSH_ARM_UVB);
01305 }
01306 else if (nbcol == XSH_ORDERS_VIS){
01307 inst = xsh_instrument_new() ;
01308 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
01309 xsh_instrument_set_arm( inst, XSH_ARM_VIS);
01310 }
01311 else if (nbcol == XSH_ORDERS_NIR){
01312 inst = xsh_instrument_new() ;
01313 xsh_instrument_set_mode( inst, XSH_MODE_IFU );
01314 xsh_instrument_set_arm( inst, XSH_ARM_NIR);
01315 }
01316 else{
01317 xsh_msg("invalid nbcol %d", nbcol);
01318 }
01319 check( order_tab = xsh_order_list_load( order_tab_frame, inst));
01320 order_tab_size = order_tab->size;
01321
01322 check ( header = cpl_propertylist_load( order_tab_name, 0));
01323
01324 lines_tab = cpl_table_load(wave_list_file, 1, 0);
01325 cpl_error_reset() ;
01326 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
01327
01328 XSH_TABLE_LOAD( table, order_tab_name);
01329 check( nbcol = cpl_table_get_nrow(table));
01330
01331 check(wavmap=cpl_image_load("chips_wav_test.fits", CPL_TYPE_DOUBLE, 0, 0));
01332 check(wavmap_pix=cpl_image_get_data_double(wavmap));
01333
01334
01335
01336
01337 check(extracted = cpl_vector_new(p_xs_3->BSIZE)) ;
01338 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));
01339 check(ex_detec=cpl_image_get_data_double(im));
01340 for (ii=0;ii<n_ord-1;ii++) {
01341 real_ord=30-ii;
01342 for (jj=0;jj<p_xs_3->BSIZE;jj++) {
01343 cpl_vector_set(extracted,jj,ex_detec[ii*p_xs_3->BSIZE+jj]);
01344
01345 }
01346 bright_lines[ii]=xsh_model_refining_detect(extracted,3,5.0, 0);
01347 for (kk=0; kk<cpl_vector_get_size(bright_lines[ii]);kk++) {
01348 yint=(int)(cpl_vector_get(bright_lines[ii],kk));
01349 check(dx=cpl_polynomial_eval_1d(order_tab->list[real_ord-ord_min].cenpoly,yint, NULL));
01350 wvlen=wavmap_pix[yint*p_xs_3->ASIZE+(int)(dx)];
01351 printf("%d %d %lf %lf \n",
01352 real_ord,kk,cpl_vector_get(bright_lines[ii],kk),wvlen);
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378 }
01379 }
01380 xsh_instrument_free(&inst);
01381 }
01382
01383
01384
01385
01386
01387
01388
01389
01390 if (testfuncflag==10) {
01391 struct xs_3 xs_3_config;
01392 cpl_frame* temp_oe_frame=NULL ;
01393
01394
01395
01396 p_xs_3_config=&xs_3_config;
01397
01398 check(config_frame = cpl_frame_new());
01399 check(cpl_frame_set_filename(config_frame, cfg_file));
01400 cpl_frame_set_tag( config_frame, tag);
01401
01402 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
01403 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
01404 return -1 ;
01405 }
01406 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
01407 check(temp_oe_frame=xsh_model_order_edges_tab_create(p_xs_3_config,
01408 "OEtestTAB.fits"));
01409 xsh_msg("oet table %s %s", cpl_frame_get_filename(temp_oe_frame),
01410 cpl_frame_get_tag(temp_oe_frame));
01411
01412 cpl_frame_delete(config_frame) ;
01413 xsh_free_frame(&temp_oe_frame);
01414
01415 exit(1);
01416 }
01417
01418
01419
01420
01421
01422
01423 if (testfuncflag==9) {
01424 float dx;
01425 struct xs_3 xs_3_config;
01426
01427 xsh_instrument* instrument=NULL;
01428 FILE* rec_out;
01429 FILE* ex1d_out;
01430
01431 XSH_INSTRCONFIG* iconfig = NULL;
01432
01433 char order_tab_name[256];
01434 cpl_frame* order_tab_frame = NULL;
01435 cpl_table* table = NULL;
01436 cpl_table* line_table = NULL;
01437 int nbcol;
01438 xsh_order_list* order_tab = NULL;
01439 int order_tab_size = 0;
01440 int iy,iyuse, iorder;
01441
01442 int ny;
01443
01444 int h, w, wmin, min_iso;
01445
01446 cpl_propertylist *header = NULL;
01447 const char* pro_catg = NULL;
01448 cpl_vector * extracted;
01449 cpl_vector * ext_med_fil;
01450
01451
01452 cpl_vector * bright_lines[15] ;
01453
01454 cpl_image * im = NULL;
01455 double* detec=NULL;
01456 double tot;
01457
01458 double x, y, cent, cent2, flux, cent2_cor, flux_cor;
01459 double flux_keep=0.0;
01460 double last_cent=0.0;
01461 double prec, ex_cor, med, stdev_ord;
01462 int lmax, rmax;
01463 int starty, endy;
01464
01465 xsh_free_frame(&config_frame);
01466 rec_out=fopen("rec_out","w");
01467 ex1d_out=fopen("ex1d_out","w");
01468
01469
01470
01471 cfg_file="/home/bristowp/allscripts/x-s/jun08sol/xs_uvb_def_jun08.fits";
01472
01473
01474
01475
01476
01477 p_xs_3_config=&xs_3_config;
01478 check(config_frame = cpl_frame_new());
01479 check(cpl_frame_set_filename( config_frame, cfg_file));
01480 cpl_frame_set_tag( config_frame, tag);
01481
01482 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
01483 fclose(ex1d_out);
01484 fclose(rec_out);
01485 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
01486 return -1 ;
01487 }
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500 check(im=cpl_image_load("/scratch/pen_rays/pre_allpen_mar08.fits", CPL_TYPE_DOUBLE, 0, 0));
01501
01502
01503
01504 check(detec=cpl_image_get_data_double(im));
01505
01506
01507
01508
01509
01510 check(cpl_msg_set_level(CPL_MSG_DEBUG));
01511 check(xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM)) ;
01512
01513
01514
01515 strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_UVB.fits");
01516
01517
01518
01519 cknull( order_tab_name);
01520 order_tab_frame = cpl_frame_new();
01521 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
01522 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
01523 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
01524
01525 XSH_TABLE_LOAD( table, order_tab_name);
01526 check( nbcol = cpl_table_get_nrow(table));
01527 if (nbcol == XSH_ORDERS_UVB){
01528 instrument = xsh_instrument_new() ;
01529 xsh_instrument_set_mode( instrument, XSH_MODE_IFU );
01530 xsh_instrument_set_arm( instrument, XSH_ARM_UVB);
01531 p_xs_3->ASIZE=2044;
01532 p_xs_3->BSIZE=2800;
01533 }
01534 else if (nbcol == XSH_ORDERS_VIS){
01535 instrument = xsh_instrument_new() ;
01536 xsh_instrument_set_mode( instrument, XSH_MODE_IFU );
01537 xsh_instrument_set_arm( instrument, XSH_ARM_VIS);
01538 }
01539 else if (nbcol == XSH_ORDERS_NIR){
01540 instrument = xsh_instrument_new() ;
01541 xsh_instrument_set_mode( instrument, XSH_MODE_IFU );
01542 xsh_instrument_set_arm( instrument, XSH_ARM_NIR);
01543 p_xs_3->BSIZE=p_xs_3->BSIZE-8;
01544 p_xs_3->ASIZE=p_xs_3->ASIZE-4;
01545 }
01546 else{
01547 xsh_msg("invalid nbcol %d", nbcol);
01548 }
01549 xsh_instrument_set_binx(instrument,bin_X);
01550 xsh_instrument_set_biny(instrument,bin_Y);
01551 XSH_ASSURE_NOT_NULL( instrument);
01552
01553 check (iconfig = xsh_instrument_get_config( instrument));
01554 ny = iconfig->ny;
01555 check( order_tab = xsh_order_list_load( order_tab_frame, instrument));
01556 order_tab_size = order_tab->size;
01557
01558 check ( header = cpl_propertylist_load( order_tab_name, 0));
01559 check( pro_catg = xsh_pfits_get_pcatg( header));
01560
01561 xsh_msg("Order Table of type %s",pro_catg);
01562
01563
01564 line_table = cpl_table_load(wave_list_file, 1, 0);
01565
01566 check(lines_tot = cpl_table_get_nrow(line_table)) ;
01567
01568 min_iso=5;
01569 wmin=4;
01570 h=4;
01571
01572 for( iorder=0; iorder< order_tab_size; iorder++){
01573 cpl_error_reset() ;
01574 check(extracted = cpl_vector_new(p_xs_3->BSIZE)) ;
01575
01576 starty = order_tab->list[iorder].starty;
01577 endy = order_tab->list[iorder].endy;
01578 if (starty == 0 && endy == 0){
01579 xsh_msg("Warning starty and endy equal zero, put endy to %d",ny-1);
01580 starty =0;
01581 endy = ny-1;
01582 }
01583
01584
01585
01586
01587 for( iy=300; iy<=p_xs_3->BSIZE-100; iy=iy+1){
01588 check( dx = cpl_polynomial_eval_1d( order_tab->list[iorder].cenpoly,
01589 iy, NULL));
01590
01591 iyuse=iy-200;
01592
01593
01594
01595
01596
01597 tot=0;
01598 for (ii=-h; ii<=h; ii++) {
01599 tot+=detec[p_xs_3->ASIZE*iyuse+(int)(dx+0.5)+ii];
01600
01601 }
01602 cpl_vector_set(extracted,iyuse,tot);
01603 }
01604
01605 check(bright_lines[iorder]=xsh_model_refining_detect(extracted,3,5.0,0));
01606
01607
01608
01609
01610 med=cpl_vector_get_median_const(extracted);
01611 stdev_ord=cpl_vector_get_stdev(extracted);
01612 ext_med_fil=cpl_vector_filter_median_create(extracted,50);
01613 cpl_vector_subtract(extracted,ext_med_fil);
01614
01615 for( iy=starty; iy<=endy; iy= iy+1){
01616 fprintf(ex1d_out,"%d %d %lf \n",
01617 iorder, iy, cpl_vector_get(extracted,iy));
01618 }
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629 cpl_vector_delete(ext_med_fil);
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640 for (ii=0; ii<cpl_vector_get_size(bright_lines[iorder]);ii++) {
01641 cent=cpl_vector_get(bright_lines[iorder],ii)+0.5-1.0;
01642
01643
01644 rmax=0;
01645 w=wmin;
01646 while (w<200) {
01647
01648 prec=(float)(w)/20.0;
01649 if (prec<0.5) {
01650 prec=0.5;
01651 }
01652 flux=0.0;
01653 cent2=0.0;
01654 flux_cor=0.0;
01655 cent2_cor=0.0;
01656 for (jj=(int)(cent)-wmin;jj<=(int)(cent)+w;jj++) {
01657
01658 if (jj>0 && jj<p_xs_3->BSIZE) {
01659 ex_cor=cpl_vector_get(extracted,jj);
01660 flux+=ex_cor;
01661 cent2+=ex_cor*((float)(jj)+0.5-cent);
01662
01663 }
01664 }
01665 cent2/=flux;
01666 if (w==wmin) {
01667 flux_keep=flux;
01668 }
01669
01670 cent2+=cent;
01671 if (fabs(cent2-cent)<prec) {
01672 rmax=w;
01673 }
01674 else {
01675 w=1000;
01676 }
01677 w+=1;
01678 }
01679 lmax=0;
01680 w=wmin;
01681 while (w<200) {
01682
01683 prec=(float)(w)/20.0;
01684 if (prec<0.5) {
01685 prec=0.5;
01686 }
01687 flux=0.0;
01688 cent2=0.0;
01689 flux_cor=0.0;
01690 cent2_cor=0.0;
01691 for (jj=(int)(cent)-w;jj<=(int)(cent)+wmin;jj++) {
01692 if (jj>0 && jj<p_xs_3->BSIZE) {
01693 ex_cor=cpl_vector_get(extracted,jj);
01694 flux+=ex_cor;
01695 cent2+=ex_cor*((float)(jj)+0.5-cent);
01696 }
01697 }
01698 cent2/=flux;
01699
01700 cent2+=cent;
01701 if (fabs(cent2-cent)<prec) {
01702 lmax=w;
01703 }
01704 else {
01705 w=1000;
01706 }
01707 w+=1;
01708 }
01709
01710
01711
01712
01713
01714 blaze_wav=2*(sin(-p_xs_3_config->nug))/((double)(iorder+morder_min)*p_xs_3_config->sg);
01715 lam_max=1000000.0*blaze_wav*((double)(iorder+morder_min)/((double)(iorder+morder_min)-0.5));
01716 lam_min=1000000.0*blaze_wav*((double)(iorder+morder_min)/(0.5+(double)(iorder+morder_min)));
01717
01718 if (lmax>=min_iso && rmax>=min_iso) {
01719
01720 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
01721 check(lambda_nm=cpl_table_get_float(line_table, "WAVELENGTH",
01722 line_cnt, NULL));
01723 check(inten = cpl_table_get_int(line_table, "FLUX", line_cnt, NULL));
01724
01725 blaze_wav=2*(sin(-p_xs_3_config->nug))/((double)(iorder+morder_min)*p_xs_3_config->sg);
01726 lam_max=1000000.0*blaze_wav*((double)(iorder+morder_min)/((double)(iorder+morder_min)-0.5));
01727 lam_min=1000000.0*blaze_wav*((double)(iorder+morder_min)/(0.5+(double)(iorder+morder_min)));
01728
01729
01730 if (lambda_nm<lam_max && lambda_nm>lam_min) {
01731 check(xsh_model_get_xy(p_xs_3_config,instrument,lambda_nm,iorder+morder_min,0.0,&x,&y));
01732 check(dx=cpl_polynomial_eval_1d(order_tab->list[iorder].cenpoly,(int)(y), NULL));
01733 if (iorder+morder_min==30 && fabs(y-cent)<7.5) {
01734 printf("%lf %lf %lf %lf \n",dx,y, cent, y-cent);
01735 }
01736 if (fabs(y-cent)<1.5 && rmax!=0 && lmax!=0) {
01737 printf("%d %lf %lf %lf %d %d\n",iorder+morder_min, lambda_nm, dx,cent,lmax,rmax);
01738
01739 if (cent!=last_cent) {
01740 if (fabs(y-cent)<1.5 && rmax!=0 && lmax!=0) {
01741 fprintf(rec_out,"\n %d %lf %lf %lf %lf %d %lf ",iorder+morder_min, lambda_nm, dx,cent,flux_keep,min_iso, y-cent);
01742 }
01743 }
01744 else {
01745 fprintf(rec_out,"warning");
01746 }
01747 last_cent=cent;
01748 }
01749 }
01750 }
01751 }
01752 }
01753 cpl_vector_delete(extracted) ;
01754 }
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781 cpl_image_delete(im);
01782 xsh_order_list_free( &order_tab);
01783 XSH_TABLE_FREE( table);
01784 cpl_frame_delete(config_frame) ;
01785 xsh_instrument_free(&instrument);
01786
01787 for (ii=0 ; ii<15; ii++) {
01788 if (bright_lines[ii] != NULL) cpl_vector_delete(bright_lines[ii]) ;
01789 }
01790 cpl_table_delete(line_table) ;
01791 fclose(rec_out);
01792 fclose(ex1d_out);
01793 }
01794
01795
01796
01797
01798 if (testfuncflag==0) {
01799
01800 fitsrow=0;
01801 tab_size = 150000;
01802 if (THEtabflag==1) {
01803 if (trace_out_flag==1 || BackMapFlag==1) {
01804 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");
01805 }
01806 THE_tab = cpl_table_new(tab_size);
01807 cpl_table_new_column(THE_tab, "Wavelength", CPL_TYPE_FLOAT) ;
01808 cpl_table_new_column(THE_tab, "Order", CPL_TYPE_INT) ;
01809 cpl_table_new_column(THE_tab, "Slit position", CPL_TYPE_FLOAT) ;
01810 cpl_table_new_column(THE_tab, "detector x", CPL_TYPE_DOUBLE) ;
01811 cpl_table_new_column(THE_tab, "detector y", CPL_TYPE_DOUBLE) ;
01812 }
01813
01814 ii=0;
01815 while (fscanf(qe_in,"%lf %lf \n",&dummy,&qe[ii]) !=EOF) {
01816 ii++;
01817 }
01818 fclose(qe_in);
01819 if (atmosflag==1) {
01820 ii=0;
01821 while (fscanf(tran_in,"%lf %lf \n",&dummy,&tran[ii]) !=EOF) {
01822 ii++;
01823 }
01824 }
01825 fclose(tran_in);
01826 ii=0;
01827 while (fscanf(dichro_in,"%lf %lf %lf %lf \n",
01828 &dummy,&temp[0],&temp[1],&temp[2]) !=EOF) {
01829 dichro[ii]=temp[dichro_col];
01830 ii++;
01831 }
01832 fclose(dichro_in);
01833
01834 config_frame = cpl_frame_new();
01835 cpl_frame_set_filename( config_frame, cfg_file);
01836 cpl_frame_set_tag( config_frame, tag);
01837
01838 check_msg(xsh_model_config_load_best(config_frame, p_xs_3),
01839 "Cannot load %s as a config",
01840 cfg_file);
01841
01842 xsh_model_binxy(p_xs_3, bin_X, bin_Y);
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853 ref_ind=xsh_alloc2Darray(8,7);
01854
01855
01856
01857 es_x_init=p_xs_3->es_x;
01858 es_y_init=p_xs_3->es_y;
01859
01860
01861 if (p_xs_3->arm!=2) {
01862 xsh_ref_ind_read(p_xs_3->arm,ref_ind,p_xs_3->temper);
01863 }
01864 else {
01865
01866
01867 xsh_ref_ind_read(p_xs_3->arm,ref_ind,p_xs_3->t_ir_p2);
01868 }
01869
01870
01871
01872
01873
01874
01875
01876 xsh_3_init(p_xs_3);
01877
01878
01879
01880 p_xs_3->pix_X=p_xs_3->pix*(float)(bin_X);
01881 p_xs_3->pix_Y=p_xs_3->pix*(float)(bin_Y);
01882
01883 prof_scale_s=1.50;
01884 prof_scale_w=1.50;
01885 profin=fopen("single_0.5ph_pro.dat","r");
01886 if (proftype==0) {
01887 profin=fopen("full_slit_pro.dat","r");
01888 prof_scale_s=15.0;
01889 }
01890 else if (proftype==2) {
01891 profin=fopen("single_0.5ph_pro.dat","r");
01892 prof_scale_s=1.50;
01893 prof_scale_w=1.50;
01894 }
01895 else if (proftype==3) {
01896 profin=fopen("multi_ph_pro.dat","r");
01897 prof_scale_s=15.0;
01898 prof_scale_w=1.50;
01899 }
01900 else if (proftype==4) {
01901 profin=fopen("multi7_ph_pro.dat","r");
01902 prof_scale_s=15.0;
01903 prof_scale_w=1.5;
01904 }
01905 else if (proftype==5) {
01906 profin=fopen("multi9_ph_HR_pro.dat","r");
01907 prof_scale_s=15.0;
01908 prof_scale_w=1.5;
01909 }
01910 else if (proftype==6) {
01911 profin=fopen("ifu.dat","r");
01912 prof_scale_s=15.0;
01913 prof_scale_w=1.5;
01914 }
01915 else if (proftype==7) {
01916 profin=fopen("custom_profile.dat","r");
01917 prof_scale_s=15.0;
01918 prof_scale_w=1.5;
01919 }
01920 if (proftype!=1) {
01921 ii=0;
01922 while (fscanf(profin,"%lf %lf\n",&spos,&profile[ii]) !=EOF) {
01923
01924 ii++;
01925 }
01926 fclose(profin);
01927 profin=fopen("single_0.5ph_pro.dat","r");
01928 ii=0;
01929 while (fscanf(profin,"%lf %lf\n",&spos,&profile_w[ii]) !=EOF) {
01930
01931 ii++;
01932 }
01933 fclose(profin);
01934 }
01935
01936
01937 check(lines_tab = cpl_table_load(wave_list_file, 1, 0)) ;
01938 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
01939
01940 trace_out=fopen("trace_r.dat","w");
01941 trace_out1=fopen("trace1.dat","w");
01942 trace_out2=fopen("trace2.dat","w");
01943 trace_out3=fopen("trace3.dat","w");
01944 line_no=0;
01945
01946
01947
01948
01949 if (THEtabflag==1) {
01950 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
01951 lambda_nm=cpl_table_get_float( lines_tab,
01952 XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH,
01953 line_cnt, NULL);
01954 inten = cpl_table_get_int( lines_tab, XSH_ARCLIST_TABLE_COLNAME_FLUX,
01955 line_cnt, NULL);
01956 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
01957
01958 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
01959 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
01960 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
01961
01962 lambda=lambda_nm*1e-6;
01963 if (lambda>lam_min-blaze_pad && lambda<lam_max+blaze_pad) {
01964 spos_int=0;
01965 for (spos=-sep_ph*(num_ph-1.0)/2.0; spos<=0.01+sep_ph*(num_ph-1.0)/2.0; spos+=sep_ph) {
01966 p_xs_3->es_y_tot=spos+p_xs_3->es_y;
01967 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
01968 xsh_3_detpix(p_xs_3);
01969
01970 if (p_xs_3->chippix[0]==1) {
01971
01972 cpl_table_set_float(THE_tab, "Wavelength", fitsrow, lambda_nm) ;
01973 cpl_table_set_int(THE_tab, "Order", fitsrow, morder_cnt) ;
01974 cpl_table_set_float(THE_tab, "Slit position",
01975 fitsrow, p_xs_3->es_y/p_xs_3->slit_scale);
01976 cpl_table_set_double(THE_tab, "detector x",
01977 fitsrow, p_xs_3->xpospix);
01978 cpl_table_set_double(THE_tab, "detector y",
01979 fitsrow, p_xs_3->ypospix);
01980 if (THEtxtflag==1) {
01981 printf("%lf %d %lf %lf %lf %lf \n",
01982 lambda_nm,morder_cnt,p_xs_3->es_y/p_xs_3->slit_scale,
01983 p_xs_3->xpospix,p_xs_3->ypospix,inten);
01984 }
01985
01986
01987
01988 fitsrow+=1;
01989 }
01990 spos_int+=1;
01991 }
01992 }
01993 }
01994 }
01995 }
01996 else if (det_slitflag==1) {
01997 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
01998 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
01999 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
02000 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
02001 for (lambda=lam_min-blaze_pad; lambda<=lam_max+blaze_pad;
02002 lambda+=(lam_max-lam_min)/100.0) {
02003 lambda_nm=lambda*1000000.0;
02004 p_xs_3->es_y_tot=p_xs_3->es_y-p_xs_3->es_s/2.0;
02005 p_xs_3->es_x=0.0;
02006 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02007 xsh_3_detpix(p_xs_3);
02008 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
02009 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
02010 p_xs_3->chippix[2]>=10 &&
02011 p_xs_3->chippix[2]<p_xs_3->BSIZE-10) {
02012 x1=p_xs_3->xpospix;
02013 y_1=p_xs_3->ypospix;
02014 }
02015 else {
02016 x1=0.0;
02017 y_1=0.0;
02018 }
02019 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->es_s/2.0;
02020 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02021 xsh_3_detpix(p_xs_3);
02022 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
02023 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
02024 p_xs_3->chippix[2]>=10 &&
02025 p_xs_3->chippix[2]<p_xs_3->BSIZE-10 &&
02026 x1!=0.0 && y_1!=0.0) {
02027 yslit2xdet=(p_xs_3->xpospix-x1)/p_xs_3->es_s;
02028 yslit2ydet=(p_xs_3->ypospix-y_1)/p_xs_3->es_s;
02029 }
02030 else {
02031 yslit2xdet=9999.0;
02032 yslit2ydet=9999.0;
02033 }
02034 p_xs_3->es_x=-p_xs_3->es_w/2.0;
02035 p_xs_3->es_y_tot=p_xs_3->es_y;
02036 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02037 xsh_3_detpix(p_xs_3);
02038 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
02039 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
02040 p_xs_3->chippix[2]>=10 &&
02041 p_xs_3->chippix[2]<p_xs_3->BSIZE-10) {
02042 x1=p_xs_3->xpospix;
02043 y_1=p_xs_3->ypospix;
02044 }
02045 else {
02046 x1=0.0;
02047 y_1=0.0;
02048 }
02049 p_xs_3->es_x=p_xs_3->es_w/2.0;
02050 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02051 xsh_3_detpix(p_xs_3);
02052 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
02053 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
02054 p_xs_3->chippix[2]>=10 &&
02055 p_xs_3->chippix[2]<p_xs_3->BSIZE-10 &&
02056 x1!=0.0 && y_1!=0.0) {
02057 xslit2xdet=(p_xs_3->xpospix-x1)/p_xs_3->es_w;
02058 xslit2ydet=(p_xs_3->ypospix-y_1)/p_xs_3->es_w;
02059 }
02060 else {
02061 xslit2xdet=9999.0;
02062 xslit2ydet=9999.0;
02063 }
02064 p_xs_3->es_x=0.0;
02065 p_xs_3->es_y_tot=p_xs_3->es_y;
02066 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02067 xsh_3_detpix(p_xs_3);
02068 if (xslit2xdet!=9999.0 && xslit2ydet!=9999.0 &&
02069 yslit2xdet!=9999.0 && yslit2ydet!=9999.0) {
02070 ydet2xslit=yslit2xdet/(xslit2ydet*yslit2xdet-xslit2xdet*yslit2ydet);
02071 ydet2yslit=-xslit2xdet/(xslit2ydet*yslit2xdet-xslit2xdet*yslit2ydet);
02072 xdet2xslit=yslit2ydet/(xslit2xdet*yslit2ydet-xslit2ydet*yslit2xdet);
02073 xdet2yslit=-xslit2ydet/(xslit2xdet*yslit2ydet-xslit2ydet*yslit2xdet);
02074 printf("%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",
02075 morder_cnt, lambda_nm,p_xs_3->xpospix,p_xs_3->ypospix,
02076 xslit2ydet, yslit2xdet, xslit2xdet, yslit2ydet, ydet2xslit,
02077 ydet2yslit,xdet2xslit,xdet2yslit);
02078 }
02079 }
02080 }
02081 exit(1);
02082 }
02083 else {
02084 ccd=xsh_alloc2Darray(p_xs_3->BSIZE, p_xs_3->ASIZE);
02085 for (ii=0;ii<p_xs_3->BSIZE;ii++) {
02086 for (jj=0;jj<p_xs_3->ASIZE;jj++) {
02087 ccd[ii][jj]=0.0;
02088 }
02089 }
02090 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
02091 lambda_nm=cpl_table_get_float(lines_tab,
02092 XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH,
02093 line_cnt, NULL);
02094 inten = cpl_table_get_int(lines_tab,
02095 XSH_ARCLIST_TABLE_COLNAME_FLUX, line_cnt, NULL);
02096 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
02097
02098
02099 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
02100 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
02101 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
02102
02103 lambda=lambda_nm*1e-6;
02104 if (lambda>lam_min-blaze_pad && lambda<lam_max+blaze_pad) {
02105 p_xs_3->es_y_tot=p_xs_3->es_y;
02106 p_xs_3->es_x=0.0;
02107 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02108 xsh_3_detpix(p_xs_3);
02109 p_dif=(sin(p_xs_3->grat_alpha-(-p_xs_3->nug))+
02110 sin(p_xs_3->grat_beta-(-p_xs_3->nug)));
02111 if (p_xs_3->grat_alpha>p_xs_3->grat_beta) {
02112 sinc_arg=(M_PI/(lambda*p_xs_3->sg))*
02113 (cos(p_xs_3->grat_alpha)/
02114 cos(p_xs_3->grat_alpha-(-p_xs_3->nug)))*p_dif;
02115 blaze=((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
02116 }
02117 else {
02118 sinc_arg=(M_PI/(lambda*p_xs_3->sg))*
02119 (cos(p_xs_3->grat_beta)/
02120 cos(p_xs_3->grat_alpha-(-p_xs_3->nug)))*p_dif;
02121 blaze=(cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha))*
02122 (cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha))*
02123 ((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
02124 }
02125 if (p_xs_3->chippix[0]==1) {
02126
02127 if (p_xs_3->arm==0) {
02128 if (morder_cnt-arm_ef_ord0+1>0 && ((int)(lambda_nm)-arm_lam_min)/arm_lam_fac>=0) {
02129 ef_corr_down=ef[morder_cnt-arm_ef_ord0+1][((int)(lambda_nm)-arm_lam_min)/arm_lam_fac];
02130 ef_corr_up=ef[morder_cnt-arm_ef_ord0+1][(((int)(lambda_nm)-arm_lam_min)/arm_lam_fac)+1];
02131 substep=(lambda_nm/(double)(arm_lam_fac)-(double)((int)(lambda_nm/(double)(arm_lam_fac))));
02132 ef_corr=ef_corr_down+substep*(ef_corr_up-ef_corr_down);
02133 }
02134 else {
02135 ef_corr=blaze;
02136 }
02137 }
02138 else ef_corr=blaze;
02139 if ((int)(lambda_nm)-qe_wv_min>=0) {
02140 qe_corr_down=qe[((int)(lambda_nm)-qe_wv_min)/qe_wv_step];
02141 qe_corr_up=qe[(((int)(lambda_nm)-qe_wv_min)/qe_wv_step)+1];
02142 substep=(lambda_nm/(double)(qe_wv_step))-(double)((int)(lambda_nm/(double)(qe_wv_step)));
02143 qe_corr=(qe_corr_down+substep*(qe_corr_up-qe_corr_down))/100.0;
02144 }
02145 else {
02146 qe_corr=qe[0]/100.0;
02147 }
02148 dichro_corr_down=dichro[(int)(lambda_nm)-200];
02149 dichro_corr_up=dichro[((int)(lambda_nm)-200)+1];
02150 substep=(lambda_nm)-(double)((int)(lambda_nm));
02151 dichro_corr=(dichro_corr_down+substep*(dichro_corr_up-dichro_corr_down))/100.0;
02152
02153 if (atmosflag==1) {
02154 if (lambda_nm>tran_wv_min && lambda_nm<tran_wv_max) {
02155
02156 tran_corr_down=tran[(int)((lambda_nm-tran_wv_min)/tran_wv_step)];
02157 tran_corr_up=tran[(int)((lambda_nm-tran_wv_min)/tran_wv_step)+1];
02158 substep=((lambda_nm-tran_wv_min)/tran_wv_step)-(double)((int)((lambda_nm-tran_wv_min)/tran_wv_step));
02159 at_tran=tran_corr_down+substep*(tran_corr_up-tran_corr_down);
02160
02161 }
02162 else {
02163 at_tran=1.0;
02164 }
02165 }
02166
02167
02168
02169
02170 if (flux_scale*at_tran*ef_corr*qe_corr*dichro_corr<1.0) {
02171 phot_in=(int)(inten);
02172 pfac=flux_scale*at_tran*ef_corr*qe_corr*dichro_corr;
02173 }
02174 else {
02175 phot_in=(int)(inten*flux_scale);
02176 pfac=at_tran*ef_corr*qe_corr*dichro_corr;
02177 }
02178 pfac2=at_tran*ef_corr*qe_corr*dichro_corr;
02179 if (phot_in==0) {
02180 prob=rand();
02181 if (prob/2147483648.0<at_tran*ef_corr*qe_corr*dichro_corr*inten*flux_scale) {
02182 tot_phot=1;
02183 }
02184 else {
02185 tot_phot=0;
02186 }
02187 }
02188 else if (phot_in<10000){
02189 tot_phot=0.0;
02190 for (ii=0;ii<phot_in;ii++) {
02191 prob=rand();
02192 if (prob/2147483648.0<pfac) {
02193 tot_phot+=1.0;
02194 }
02195 }
02196 }
02197 else {
02198 tot_phot=(double)(phot_in)*pfac;
02199 }
02200
02201
02202 printf("m %d lam %lf tot %g inten %lf \n", morder_cnt,lambda_nm,tot_phot, inten);
02203
02204 nphot=1.0;
02205 phot_res=1000000.0;
02206 if (inten*pfac>phot_res/flux_scale) {
02207 nphot=(inten/phot_res)*flux_scale*pfac;
02208 tot_phot=phot_res;
02209 }
02210
02211
02212 photons=1;
02213 while (photons<=(int)(tot_phot)) {
02214
02215
02216
02217
02218
02219
02220
02221
02222
02223
02224
02225
02226 prob=rand();
02227 prob=prob/2147483648.0;
02228 prob2=rand();
02229 prob2=prob2/2147483648.0;
02230 prob3=rand();
02231 prob3=prob3/2147483648.0;
02232 prob4=rand();
02233 prob4=prob4/2147483648.0;
02234 gauss1=sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
02235
02236 gauss2=sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
02237 flag=0;
02238 if (proftype!=1) {
02239 if (proftype==0 || proftype==6) {
02240 p_xs_3->es_x=(p_xs_3->es_w)*(prob3-0.5)+(p_xs_3->es_w/100.0)*gauss1;
02241
02242 }
02243 else {
02244 p_xs_3->es_x=(prob3-0.5)*p_xs_3->slit_scale*prof_scale_w;
02245 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) {
02246 flag=1;
02247 }
02248 }
02249
02250 if (proftype==5) {
02251 prof_data_size=6001;
02252 }
02253 else {
02254 prof_data_size=301;
02255 }
02256 p_xs_3->es_y_tot=(prob4-0.5)*p_xs_3->slit_scale*prof_scale_s;
02257 if (prob>profile[(int)(prob4*prof_data_size)]) {
02258 flag=1;
02259 }
02260
02261 }
02262 else if (proftype==1) {
02263
02264 p_xs_3->es_x=gauss1*0.255/2.35;
02265 p_xs_3->es_y_tot=gauss2*0.255/2.35;
02266
02267
02268 if (2.0*p_xs_3->es_y_tot>p_xs_3->es_s || 2.0*p_xs_3->es_y_tot<-p_xs_3->es_s || 2.0*p_xs_3->es_x<-p_xs_3->es_w || 2.0*p_xs_3->es_x>p_xs_3->es_w) {
02269 flag=1;
02270 }
02271
02272 }
02273
02274
02275 if (flag!=1) {
02276 photons+=1;
02277 p_xs_3->es_y_tot+=es_y_init;
02278 p_xs_3->es_x=es_x_init+p_xs_3->es_x;
02279 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02280 xsh_3_detpix(p_xs_3);
02281
02282
02283 xsh_model_io_output_cfg_txt(p_xs_3);
02284 if (p_xs_3->chippix[0]==1) {
02285 ccd[(int)(p_xs_3->ypospix-0.5)][(int)(p_xs_3->xpospix-0.5)]+=nphot;
02286
02287 }
02288 }
02289 }
02290
02291 }
02292 }
02293 }
02294
02295 if (bgflag==0 && continuum==0) line_no+=1;
02296 }
02297 }
02298 cpl_table_delete(lines_tab) ;
02299 total_lines=line_no;
02300 if ((xsh_free2Darray(ef,n_order))!=0) {
02301 fclose(trace_out);
02302 fclose(trace_out1);
02303 fclose(trace_out2);
02304 fclose(trace_out3);
02305 cpl_msg_error(__func__, "Cannot free 2D array ef");
02306 return -1;
02307 }
02308
02309
02310
02311 p_xs_3->es_x=es_x_init;
02312 p_xs_3->es_y_tot=es_y_init;
02313 lam_range=0.00015;
02314 if (BackMapFlag==1 && THEtabflag!=1) {
02315 if (p_xs_3->arm==2) {
02316 SIZE=p_xs_3->ASIZE;
02317 }
02318 else {
02319 SIZE=p_xs_3->BSIZE;
02320 }
02321 if ((trace_lam=xsh_alloc1Darray(SIZE))==NULL){
02322 cpl_msg_error(__func__, "Cannot allocate 2D array");
02323 return -1;
02324 }
02325 if ((trace_flux=xsh_alloc1Darray(SIZE))==NULL){
02326 cpl_msg_error(__func__, "Cannot allocate 2D array");
02327 return -1;
02328 }
02329 if ((trace_flux1=xsh_alloc1Darray(SIZE))==NULL){
02330 cpl_msg_error(__func__, "Cannot allocate 2D array");
02331 return -1;
02332 }
02333 if ((trace_flux2=xsh_alloc1Darray(SIZE))==NULL){
02334 cpl_msg_error(__func__, "Cannot allocate 2D array");
02335 return -1;
02336 }
02337 if ((trace_flux3=xsh_alloc1Darray(SIZE))==NULL){
02338 cpl_msg_error(__func__, "Cannot allocate 2D array");
02339 return -1;
02340 }
02341 if ((trace_xdisp=xsh_alloc1Darray(SIZE))==NULL){
02342 cpl_msg_error(__func__, "Cannot allocate 2D array");
02343 return -1;
02344 }
02345 if ((trace_mm=xsh_alloc1Darray_INT(SIZE))==NULL){
02346 cpl_msg_error(__func__, "Cannot allocate 2D array");
02347 return -1;
02348 }
02349 ccd_wav=xsh_alloc2Darray(p_xs_3->BSIZE, p_xs_3->ASIZE);
02350 for (ii=0;ii<p_xs_3->BSIZE;ii++) {
02351 for (jj=0;jj<p_xs_3->ASIZE;jj++) {
02352 ccd_wav[ii][jj]=0.0;
02353 }
02354 }
02355 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
02356 for (ii=0;ii<SIZE;ii+=1) {
02357 trace_mm[ii]=0;
02358 trace_lam[ii]=0.0;
02359 trace_xdisp[ii]=0.0;
02360 trace_flux[ii]=0.0;
02361 trace_flux1[ii]=0.0;
02362 trace_flux2[ii]=0.0;
02363 trace_flux3[ii]=0.0;
02364 }
02365 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
02366 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
02367 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
02368 printf("blaze wav=%lf lam_max=%lf lam_min=%lf \n",blaze_wav, lam_max, lam_min);
02369 for (p_xs_3->es_y_tot=p_xs_3->es_y-p_xs_3->es_s/2.0;p_xs_3->es_y_tot<=p_xs_3->es_y+p_xs_3->es_s/2.0;p_xs_3->es_y_tot+=p_xs_3->es_s/100.0) {
02370 pixylast=0.0;
02371 pixxlast=0.0;
02372 pixwavlast=1000000*(lam_min-blaze_pad);
02373 if (p_xs_3->arm==2) {
02374 chipdist_current=2047;
02375 }
02376 else {
02377 chipdist_current=1;
02378 }
02379 for (lambda=lam_min-blaze_pad; lambda<=lam_max+blaze_pad; lambda+=(lam_max-lam_min)/10000.0) {
02380 lambda_nm=lambda*1000000.0;
02381 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02382 xsh_3_detpix(p_xs_3);
02383 if (p_xs_3->chippix[0]==1) {
02384 if (p_xs_3->chippix[1]>=1 &&
02385 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
02386 p_xs_3->chippix[2]>=1 &&
02387 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
02388 if (p_xs_3->arm==2) {
02389 if (p_xs_3->xpospix<(double)(chipdist_current)-0.5) {
02390 ccd_wav[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]=
02391 (pixwavlast+(lambda_nm-pixwavlast)*
02392 ((double)(chipdist_current)-0.5-pixxlast)/
02393 (p_xs_3->xpospix-pixxlast));
02394 chipdist_current=p_xs_3->chippix[1]-1;
02395
02396 }
02397 }
02398 else {
02399 if (p_xs_3->ypospix>(double)(chipdist_current)-0.5) {
02400 ccd_wav[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]=
02401 pixwavlast+(lambda_nm-pixwavlast)*
02402 ((double)(chipdist_current)-0.5-pixylast)/
02403 (p_xs_3->ypospix-pixylast);
02404 chipdist_current=p_xs_3->chippix[2]+1;
02405 if (p_xs_3->chippix[2]-1==0) {
02406
02407 }
02408 }
02409 }
02410 }
02411 }
02412 pixxlast=p_xs_3->xpospix;
02413 pixylast=p_xs_3->ypospix;
02414 pixwavlast=lambda_nm;
02415 }
02416
02417 if (p_xs_3->arm==2) {
02418 chipdist_current=3000;
02419 }
02420 else {
02421 chipdist_current=0;
02422 }
02423 }
02424 pixxlast=0.0;
02425 pixylast=0.0;
02426 pixwavlast=1000000*(lam_min-blaze_pad);
02427 for (lambda=lam_min-blaze_pad; lambda<=lam_max+blaze_pad;
02428 lambda+=(lam_max-lam_min)/10000.0) {
02429 lambda_nm=1000000.0*lambda;
02430 p_xs_3->es_y_tot=p_xs_3->es_y;
02431 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02432 xsh_3_detpix(p_xs_3);
02433 if (p_xs_3->chippix[0]==1) {
02434 if (p_xs_3->chippix[1]>=1 &&
02435 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
02436 p_xs_3->chippix[2]>=1 &&
02437 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
02438 if (p_xs_3->arm==2){
02439 if (p_xs_3->xpospix<(double)(chipdist_current)-0.5) {
02440 trace_lam[p_xs_3->chippix[1]-1]=pixwavlast+(lambda_nm-pixwavlast)*
02441 ((double)(chipdist_current)-0.5-pixxlast)/(p_xs_3->xpospix-pixxlast);
02442 trace_xdisp[p_xs_3->chippix[1]-1]=pixylast+(p_xs_3->ypospix-pixylast)*
02443 ((double)(chipdist_current)-0.5-pixxlast)/(p_xs_3->xpospix-pixxlast);
02444 trace_flux[p_xs_3->chippix[1]-1]=0.0;
02445 trace_flux1[p_xs_3->chippix[1]-1]=0.0;
02446 trace_flux2[p_xs_3->chippix[1]-1]=0.0;
02447 trace_flux3[p_xs_3->chippix[1]-1]=0.0;
02448 for (ii=-half_prof_wid;ii<half_prof_wid+1;ii+=1) {
02449 if (p_xs_3->chippix[2]-1+ii>=0 &&
02450 p_xs_3->chippix[2]-1+ii<p_xs_3->BSIZE) {
02451 ccd_sig=ccd[p_xs_3->chippix[2]-1+ii][p_xs_3->chippix[1]-1];
02452 if (ccd_sig>65535 && ccd_sig<=131072) {
02453 ccd_sig=65535;
02454 }
02455 else if (ccd_sig>131072) {
02456 ccd_sig=0;
02457 }
02458 trace_flux[p_xs_3->chippix[1]-1]+=ccd_sig;
02459 prob=rand();
02460 prob=prob/2147483648.0;
02461 prob2=rand();
02462 prob2=prob2/2147483648.0;
02463 prob3=rand();
02464 prob3=prob/2147483648.0;
02465 prob4=rand();
02466 prob4=prob2/2147483648.0;
02467 gauss1=bg_sig*sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
02468 gauss2=bg_sig*sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
02469 gauss3=bg_sig*sqrt(-2.0*log(1-prob3))*sin(2*M_PI*prob4);
02470 trace_flux1[p_xs_3->chippix[1]-1]+=ccd_sig+gauss1;
02471 trace_flux2[p_xs_3->chippix[1]-1]+=ccd_sig+gauss2;
02472 trace_flux3[p_xs_3->chippix[1]-1]+=ccd_sig+gauss3;
02473 }
02474 }
02475 trace_mm[p_xs_3->chippix[1]-1]=morder_cnt;
02476
02477 chipdist_current=p_xs_3->chippix[1]+1;
02478 }
02479 }
02480 else {
02481 if (p_xs_3->ypospix>(double)(chipdist_current)-0.5) {
02482 trace_lam[p_xs_3->chippix[2]-1]=
02483 pixwavlast+(lambda_nm-pixwavlast)*
02484 ((double)(chipdist_current)-0.5-pixylast)/
02485 (p_xs_3->ypospix-pixylast);
02486 trace_xdisp[p_xs_3->chippix[2]-1]=
02487 pixxlast+(p_xs_3->xpospix-pixxlast)*
02488 ((double)(chipdist_current)-0.5-pixylast)/
02489 (p_xs_3->ypospix-pixylast);
02490 trace_flux[p_xs_3->chippix[2]-1]=0.0;
02491 trace_flux1[p_xs_3->chippix[1]-1]=0.0;
02492 trace_flux2[p_xs_3->chippix[1]-1]=0.0;
02493 trace_flux3[p_xs_3->chippix[1]-1]=0.0;
02494 for (ii=-half_prof_wid;ii<half_prof_wid+1;ii+=1) {
02495 if (p_xs_3->chippix[1]-1+ii>=0 &&
02496 p_xs_3->chippix[1]-1+ii<p_xs_3->ASIZE) {
02497 ccd_sig=ccd[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1+ii];
02498 trace_flux[p_xs_3->chippix[2]-1]+=ccd_sig;
02499 if (ccd_sig>65535 && ccd_sig<=131072) {
02500 ccd_sig=65535;
02501 }
02502 else if (ccd_sig>131072) {
02503 ccd_sig=0;
02504 }
02505 prob=rand();
02506 prob=prob/2147483648.0;
02507 prob2=rand();
02508 prob2=prob2/2147483648.0;
02509 prob3=rand();
02510 prob3=prob3/2147483648.0;
02511 prob4=rand();
02512 prob4=prob4/2147483648.0;
02513 gauss1=bg_sig*sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
02514 gauss2=bg_sig*sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
02515 gauss3=bg_sig*sqrt(-2.0*log(1-prob3))*sin(2*M_PI*prob4);
02516 trace_flux1[p_xs_3->chippix[2]-1]+=ccd_sig+gauss1;
02517 trace_flux2[p_xs_3->chippix[2]-1]+=ccd_sig+gauss2;
02518 trace_flux3[p_xs_3->chippix[2]-1]+=ccd_sig+gauss3;
02519 }
02520 }
02521 trace_mm[p_xs_3->chippix[2]-1]=morder_cnt;
02522 chipdist_current=p_xs_3->chippix[2]+1;
02523 }
02524 }
02525 pixxlast=p_xs_3->xpospix;
02526 pixylast=p_xs_3->ypospix;
02527 }
02528 }
02529 pixwavlast=lambda_nm;
02530 }
02531 for (ii=1;ii<SIZE;ii+=1) {
02532 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]));
02533 fprintf(trace_out1,"%d %lf o%d\n", ii, trace_flux1[ii], trace_mm[ii]);
02534 fprintf(trace_out2,"%d %lf o%d\n", ii, trace_flux2[ii], trace_mm[ii]);
02535 fprintf(trace_out3,"%d %lf o%d\n", ii, trace_flux3[ii], trace_mm[ii]);
02536
02537 }
02538 }
02539 cpl_free(trace_lam);
02540 cpl_free(trace_xdisp);
02541 cpl_free(trace_flux);
02542 cpl_free(trace_flux1);
02543 cpl_free(trace_flux2);
02544 cpl_free(trace_flux3);
02545 cpl_free(trace_mm);
02546 }
02547 if ((xsh_free2Darray(ref_ind,8))!=0) {
02548 cpl_msg_error(__func__, "Cannot free 2D array ref_ind");
02549 return -1;
02550 }
02551 fclose(trace_out);
02552 fclose(trace_out1);
02553 fclose(trace_out2);
02554 fclose(trace_out3);
02555
02556 if (THEtabflag==1) {
02557
02558 sprintf(name_o, "THE.fits") ;
02559 cpl_msg_info(__func__, "Writing %s" , name_o) ;
02560
02561
02562 THEplist = cpl_propertylist_new();
02563 cpl_propertylist_append_string(THEplist, "INSTRUME", "XSHOOTER") ;
02564
02565
02566 THEproduct_frame = cpl_frame_new() ;
02567 cpl_frame_set_filename(THEproduct_frame, name_o) ;
02568 cpl_frame_set_tag(THEproduct_frame, "XSHOOTER_THE") ;
02569 cpl_frame_set_type(THEproduct_frame, CPL_FRAME_TYPE_TABLE);
02570 cpl_frame_set_group(THEproduct_frame, CPL_FRAME_GROUP_PRODUCT);
02571 cpl_frame_set_level(THEproduct_frame, CPL_FRAME_LEVEL_FINAL);
02572 cpl_table_set_size(THE_tab,fitsrow);
02573 cpl_msg_info(__func__, "Save the table") ;
02574 if (cpl_table_save(THE_tab, NULL, THEplist, name_o,
02575 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
02576 cpl_msg_error(__func__, "Cannot write the table") ;
02577 }
02578
02579 cpl_frame_delete(THEproduct_frame) ;
02580 cpl_table_delete(THE_tab) ;
02581 cpl_propertylist_delete(THEplist) ;
02582 }
02583 else {
02584 naxes[0]=p_xs_3->ASIZE;
02585 naxes[1]=p_xs_3->BSIZE;
02586
02587
02588 if (readout_flag==1) {
02589 rn=2.0;
02590 for (ii=0;ii<p_xs_3->BSIZE;ii++) {
02591 for (jj=0;jj<p_xs_3->ASIZE;jj++) {
02592 prob=rand();
02593 prob=prob/2147483648.0;
02594 prob2=rand();
02595 prob2=prob2/2147483648.0;
02596 gauss1=rn*sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
02597 gauss2=rn*sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
02598 ccd[ii][jj]+=gauss1*rn;
02599 }
02600 }
02601 }
02602
02603
02604
02605 oneD_AB=xsh_copy2D_to_1D(ccd, p_xs_3->BSIZE,p_xs_3->ASIZE);
02606 ima = cpl_image_wrap_double(naxes[0], naxes[1], oneD_AB) ;
02607 cpl_image_save(ima, "chips_out.fits", CPL_BPP_IEEE_DOUBLE, NULL, CPL_IO_DEFAULT) ;
02608 cpl_image_unwrap(ima) ;
02609 cpl_free(oneD_AB);
02610
02611 if (BackMapFlag==1) {
02612 oneD_AB=xsh_copy2D_to_1D(ccd_wav, p_xs_3->BSIZE,p_xs_3->ASIZE);
02613 ima2 = cpl_image_wrap_double(naxes[0], naxes[1], oneD_AB) ;
02614 cpl_image_save(ima2, "chips_wav.fits", CPL_BPP_IEEE_DOUBLE, NULL, CPL_IO_DEFAULT) ;
02615 cpl_image_unwrap(ima2) ;
02616 cpl_free(oneD_AB);
02617 if ((xsh_free2Darray(ccd_wav,p_xs_3->BSIZE))!=0) {
02618 cpl_msg_error(__func__, "Cannot free 2D array ccd_wav");
02619 return -1;
02620 }
02621 }
02622 if ((xsh_free2Darray(ccd,p_xs_3->BSIZE))!=0) {
02623 cpl_msg_error(__func__, "Cannot free 2D array ccd");
02624 return -1;
02625 }
02626 }
02627 }
02628
02629 cleanup:
02630 xsh_msg("Doing Cleanup");
02631 if (cpl_error_get_code() != CPL_ERROR_NONE) {
02632 xsh_error_dump(CPL_MSG_ERROR);
02633 return -1;
02634 }
02635 else {
02636 return 0;
02637 }
02638 }
02639