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