X-shooter Pipeline Reference Manual 3.8.15
xsh_model_compute.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-12-18 15:26:03 $
23 * $Revision: 1.66 $
24 * $Name: not supported by cvs2svn $*/
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
40/*----------------------------------------------------------------------------
41 Includes
42 ---------------------------------------------------------------------------*/
43
44
45/* DRL steps */
46
47/* Error handling */
48#include <xsh_error.h>
49/* Utility fonctions */
50#include <xsh_utils.h>
51#include <xsh_msg.h>
52#include <xsh_data_instrument.h>
53/* DFS functions */
54#include <xsh_dfs.h>
55/* DRL functions */
56#include <xsh_drl.h>
57/* Library */
58#include <cpl.h>
59
60#include <math.h>
61#include "xsh_model_kernel.h"
63#include "xsh_model_io.h"
64#include "xsh_pfits.h"
65#include <stdlib.h>
66#include <xsh_data_order.h>
67#include <xsh_data_arclist.h>
68#include <xsh_utils_table.h>
69
70/*----------------------------------------------------------------------------
71 Defines
72 ---------------------------------------------------------------------------*/
73
74#define RECIPE_ID "xsh_model_compute"
75#define RECIPE_AUTHOR "P. Bristow"
76#define RECIPE_CONTACT "bristowp@eso.org"
77
78/*----------------------------------------------------------------------------
79 Functions prototypes
80 ---------------------------------------------------------------------------*/
81
82/*
83 * Plugin initalization, execute and cleanup handlers
84 */
85
86static int xsh_model_create(cpl_plugin *);
87static int xsh_model_exec(cpl_plugin *);
88static int xsh_model_destroy(cpl_plugin *);
89
90/* The actual executor function */
91static int xsh_model(cpl_parameterlist *, cpl_frameset *);
92static int xsh_model_reduce(const char *, const char *) ;
93/*----------------------------------------------------------------------------
94 Static variables
95 ---------------------------------------------------------------------------*/
96static char xsh_model_compute_description_short[] = "Development Only";
97
99 "This recipe is for development use only.\n";
100
101/* for conversion from degree to radians */
102/* dimension of the wavelength, 3D array */
103static const int vectordim=4;
104
105
106/*----------------------------------------------------------------------------
107 Functions code
108 ---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120int cpl_plugin_get_info(cpl_pluginlist * list)
121{
122 cpl_recipe *recipe = NULL;
123 cpl_plugin *plugin = NULL;
124
125 recipe = cpl_calloc(1, sizeof(*recipe));
126 if (recipe == NULL) {
127 return -1;
128 }
129
130 plugin = &recipe->interface;
131
132 cpl_plugin_init(plugin, CPL_PLUGIN_API, /* Plugin API */
133 XSH_BINARY_VERSION, /* Plugin version */
134 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
135 RECIPE_ID, /* Plugin name */
137 xsh_model_compute_description, /* Detailed help */
138 RECIPE_AUTHOR, /* Author name */
139 RECIPE_CONTACT, /* Contact address */
140 xsh_get_license(), /* Copyright */
144
145 cpl_pluginlist_append(list, plugin);
146
147 return (cpl_error_get_code() != CPL_ERROR_NONE);
148}
149/*--------------------------------------------------------------------------*/
159/*--------------------------------------------------------------------------*/
160static int xsh_model_create(cpl_plugin * plugin)
161{
162 cpl_recipe * recipe ;
163 cpl_parameter * p ;
164
165 /* Reset library state */
166 xsh_init();
167
168 /* Check that the plugin is part of a valid recipe */
169 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
170 recipe = (cpl_recipe *)plugin ;
171 else return -1 ;
172
173 /* Create the parameters list in the cpl_recipe object */
174 recipe->parameters = cpl_parameterlist_new() ;
175
176 /* Fill the parameters list */
177 /* --param_example */
178
179 p = cpl_parameter_new_enum("xsh.xsh_model_compute.arm",
180 CPL_TYPE_STRING,
181 "Arm setting: ",
182 "xsh.xsh_model_compute",
183 "vis",
184 3,"uvb","vis","nir");
185
186 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"arm");
187 cpl_parameterlist_append(recipe->parameters, p);
188
189 /* Return */
190 return 0;
191}
192
193
194/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202static int xsh_model_exec(cpl_plugin * plugin)
203{
204 cpl_recipe * recipe ;
205
206 /* Get the recipe out of the plugin */
207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
208 recipe = (cpl_recipe *)plugin ;
209 else return -1 ;
210
211 return xsh_model(recipe->parameters, recipe->frames) ;
212}
213
214
215/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
222static int xsh_model_destroy(cpl_plugin * plugin)
223{
224 cpl_recipe * recipe ;
225
226 /* Get the recipe out of the plugin */
227 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
228 recipe = (cpl_recipe *)plugin ;
229 else return -1 ;
230
231 cpl_parameterlist_delete(recipe->parameters) ;
232 return 0 ;
233}
234
235
236/*---------------------------------------------------------------------------*/
244/*---------------------------------------------------------------------------*/
245static int xsh_model(
246 cpl_parameterlist * parlist,
247 cpl_frameset * frameset)
248{
249
250 const char * fctid = "xsh_model" ;
251 cpl_parameter * param ;
252 cpl_frame * wave_list ;
253 cpl_frame * test_par ;
254 const char* arm=NULL;
256
257 /* RETRIEVE INPUT PARAMETERS */
258 /* --param_example */
259 param = cpl_parameterlist_find(parlist,"xsh.xsh_model_compute.arm");
260 arm = cpl_parameter_get_string(param);
261
263 if(strcmp(arm,"uvb") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_UVB);
264 if(strcmp(arm,"vis") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_VIS);
265 if(strcmp(arm,"nir") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_NIR);
266
267 if (cpl_error_get_code()) {
268 cpl_msg_error(fctid, "Failed to retrieve the input parameters") ;
269 return -1 ;
270 }
271
272 /* RETRIEVE INPUT DATA */
273 check(wave_list = xsh_find_model_wavelist(frameset,instrument)) ;
274 check(test_par = xsh_find_model_testpar(frameset,instrument)) ;
275
276 if (cpl_error_get_code()) {
277 cpl_msg_error(fctid, "Some input file(s) is(are) missing") ;
278 return -1 ;
279 }
280
281 /* Apply the computation here */
282 check_msg(xsh_model_reduce(cpl_frame_get_filename(wave_list),
283 cpl_frame_get_filename(test_par)),
284 "Failed computing the model") ;
285
286
287 cleanup:
288
290
291 /* Return */
292 if (cpl_error_get_code())
293 return -1 ;
294 else
295 return 0 ;
296}
297
298/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
307
308static int
310 const char * wave_list_file,
311 const char * test_par_file)
312{
313 FILE* rec_out = NULL;
314 FILE* ex1d_out = NULL;
315 int ii,morder, morder_cnt; /* grating spectral order */
316 int morder_it,morder_min,morder_max, n_order;
317 int photons;
318 int fitsrow, tab_size;
319 int test_p,temp_fs=0, spos_int;
320 char test_string[20];
321 DOUBLE lambda; /*wavelength in mm */
322 DOUBLE lambda_nm; /* wavelength in nm */
323 DOUBLE lam_range, blaze_wav, blaze_pad, lam_min, lam_max;
324 DOUBLE pixwavlast, pixylast, pixxlast;
325 int chipdist_current = 0;
326 DOUBLE inten, blaze, p_dif, sinc_arg; /* intensity given in line list */
327 /*probability functions for entrance slit positions*/
328 DOUBLE prob, prob2, prob3, prob4;
329 DOUBLE gauss1, gauss2, gauss3;
330 DOUBLE es_x_init, es_y_init, spos;
331 DOUBLE x1, xdet2xslit, xdet2yslit, ydet2xslit, ydet2yslit; /*For slit to det*/
332 /*transformation */
333 DOUBLE y_1, xslit2xdet, xslit2ydet;
334 DOUBLE yslit2xdet, yslit2ydet;
335
336 DOUBLE flux_scale, bg_sig=0.0, ccd_sig;
337 int trace_out_flag;
338 double* trace_lam=NULL; /*trace array */
339 double* trace_xdisp=NULL; /*trace array */
340 double* trace_flux=NULL; /*trace array */
341 double* trace_flux1=NULL; /*trace array */
342 double* trace_flux2=NULL; /*trace array */
343 double* trace_flux3=NULL; /*trace array */
344 int* trace_mm=NULL; /*trace array */
345 int half_prof_wid=20;
346 int flag, bgflag, BackMapFlag, THEtabflag, readout_flag;
347 int THEtxtflag, atmosflag, testfuncflag, det_slitflag;
348 int continuum;
349 /*These pinhole params are for alterantives to the mask method*/
350 double num_ph;
351 double fwhm_ph=1.0;
352 double sep_ph;
353 double profile[6001],profile_w[301];
354 DOUBLE prof_scale_s, prof_scale_w;
355 /*lines has wavelength, order (as float) and intensity for each line */
356 DOUBLE lines[3][10000];
357 int line_no, total_lines; /*for lines array above */
358 int line_cnt, lines_tot; /* counters for reading in line list */
359 cpl_table* lines_tab ;
360 long naxes[2];
361 double rn;
362
363 int bin_X=1;
364 int bin_Y=1;
365
366 /*xs_3 is the main structure with all the instrument configuration parameters
367 Structure is defined in xs_model_kernel.h, *most* of the contents are
368 filled by the readfile routine */
369 struct xs_3 xs_3_model;
370 struct xs_3* p_xs_3; /*pointer to xs_3_model */
371
372 /* structures and arrays for annealing - in
373 non-annealing version they are filled by the
374 readfile routine but not used beyond that */
375 ann_all_par all_par[100],* p_all_par;
376
377 int arm_ef_ord0,arm_lam_min,arm_lam_fac,jj,SIZE;
378 const char* cfg_file=NULL;
379 const char* tag=NULL;
380
381 /* Now fixed in test_par.dat
382 0=full slit;
383 1=point source;
384 2=single ph;
385 3=multi ph;
386 4=multi7 ph;
387 5=multi9 ph;
388 6=ifu;
389 7=limits
390 */
391 int proftype;
392 int prof_data_size;
393 double at_tran;
394 FILE* fin,* efin,* profin,* qe_in,* dichro_in,* tran_in;
395 double** ef=NULL; /*Efficiency array*/
396 double ef_corr,ef_corr_down,ef_corr_up,substep,pfac,pfac2;
397 double phot_res,tot_phot,nphot;
398 int phot_in;
399 double qe[102];
400 int qe_wv_min, qe_wv_step;
401 double dummy,qe_corr_down,qe_corr_up,qe_corr;
402 double dichro[2400];
403 double dichro_corr_down,dichro_corr_up,dichro_corr;
404 double temp[3];
405 int dichro_col;
406 double tran[50000];
407 double tran_corr_down,tran_corr_up;
408 double tran_wv_max;
409 double tran_wv_min;
410 double tran_wv_step;
411 DOUBLE fsc;
412
413 cpl_table* THE_tab = NULL;
414 cpl_propertylist* THEplist = NULL;
415 cpl_frame* THEproduct_frame = NULL;
416 char name_o[512] ;
417
418 double** ccd=NULL; /*detector flux array */
419 double** ccd_wav=NULL; /*detector wavelengths array */
420 double** ref_ind;
421 FILE* trace_out;
422 FILE* trace_out1;
423 FILE* trace_out2;
424 FILE* trace_out3;
425 cpl_image* ima ;
426 cpl_image* ima2 ;
427 double* oneD_AB; /* 1D array to take contents of ccd */
428 cpl_frame* config_frame = NULL;
429 struct xs_3* p_xs_3_config;
430
431 p_xs_3=&xs_3_model;
432 p_all_par=&all_par[0];
433 tran_in=fopen("tran.dat","r");
434 dichro_in=fopen("dichroics.dat","r");
435
436 //loop value below is seed
437 for (ii=0;ii<154321;ii++) {
438 prob=rand();
439 //printf("rand= %lf %d \n",(double)(prob)/2147483648.0, prob);
440 }
441 flux_scale=100.0;
442 trace_out_flag=0;
443 readout_flag=0;
444 flag=0;
445 bgflag=1;
446 BackMapFlag=0;
447 THEtabflag=0;
448 THEtxtflag=0;
449 det_slitflag=0;
450 testfuncflag=0;
451 atmosflag=0;
452 continuum=0;
453 num_ph=1.0;
454 fwhm_ph=1.0;
455
456 for (ii=0;ii<6001;ii++) profile[ii]=0.0;
457 for (ii=0;ii<301;ii++) profile_w[ii]=0.0;
458 for (ii=0;ii<10000;ii++) {
459 lines[0][ii]=0.0;
460 lines[1][ii]=0.0;
461 lines[2][ii]=0.0;
462 }
463 naxes[0]=0;
464 naxes[1]=0;
465 at_tran=1.0;
466 for (ii=0;ii<102;ii++) qe[ii]=0.0;
467 for (ii=0;ii<2400;ii++) dichro[ii]=0.0;
468 for (ii=0;ii<50000;ii++) tran[ii]=0.0;
469 tran_wv_max=2500.0;
470 tran_wv_min=903.0;
471 tran_wv_step=0.5;
472
473
474 /*open and read test parameters */
475 fin=fopen(test_par_file,"r");
476 if(fin==NULL) {
477 printf("couldn't find test parameter settings file %s\n",
478 test_par_file);
479 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
480 fclose(dichro_in);
481 fclose(tran_in);
482 return -1;
483 }
484
485 proftype=2;
486 /* Note: update the string format (the %__s) if test_string changes size.
487 * Remember: field width must equal 1 less than sizeof(test_string). */
488 while (fscanf(fin,"%19s %64d\n",test_string, &test_p) !=EOF) {
489 //printf("%s %d\n",test_string, test_p);
490 if (strncmp(test_string,"arm",3)==0) {
491 p_xs_3->arm=test_p;
492 }
493 if (strncmp(test_string,"proftype",14)==0) {
494 proftype=test_p;
495 }
496 if (strncmp(test_string,"trace_out_flag",14)==0) {
497 trace_out_flag=test_p;
498 }
499 if (strncmp(test_string,"BackMapFlag",11)==0) {
500 BackMapFlag=test_p;
501 }
502 if (strncmp(test_string,"bgflag",6)==0) {
503 bgflag=test_p;
504 }
505 if (strncmp(test_string,"flux_scale1000",14)==0) {
506 temp_fs=test_p;
507 flux_scale=(double)(test_p)/1000.0;
508 //printf("fs %lf %d %lf\n",flux_scale, test_p, (double)(test_p)/1000.0);
509 }
510 if (strncmp(test_string,"THEtab",6)==0) {
511 THEtabflag=test_p;
512 }
513 if (strncmp(test_string,"Atmos",5)==0) {
514 atmosflag=test_p;
515 }
516 if (strncmp(test_string,"bin_X",5)==0) {
517 bin_X=test_p;
518 }
519 if (strncmp(test_string,"bin_Y",5)==0) {
520 bin_Y=test_p;
521 }
522 if (strncmp(test_string,"THEtxt",6)==0) {
523 THEtxtflag=test_p;
524 }
525 if (strncmp(test_string,"det_slit",6)==0) {
526 det_slitflag=test_p;
527 }
528 if (strncmp(test_string,"testfunc",8)==0) {
529 testfuncflag=test_p;
530 }
531 if (strncmp(test_string,"bg_sig",8)==0) {
532 bg_sig=(double)(test_p);
533 }
534 }
535 fclose(fin);
536
537 flux_scale=(double)(temp_fs)/1000.0;
538 fsc=200.0;
539 /*
540 printf(" %d %lf %lf %lf %lf \n",
541 temp_fs, (double)(temp_fs), (double)(temp_fs)/1000.0,flux_scale,fsc);
542 */
543
544 if (p_xs_3->arm==0) {
545 p_xs_3->BSIZE=UVB_BSIZE;
546 p_xs_3->ASIZE=UVB_ASIZE;
547 p_xs_3->SIZE=UVB_SIZE;
548 p_xs_3->chipxpix=UVB_chipxpix;
549 p_xs_3->chipypix=UVB_chipypix;
550 //cfg_file="jun08sol/xs_uvb_def_jun08.fits";
551 //cfg_file="startup/sof/new_xs_uvb_def_jun08.fits";
552 //cfg_file="startup/sof/xsh_model_first_anneal_save.fits";
553 //cfg_file="anneal_dev/xsh_model_anneal_save.fits";
554 //cfg_file="new_new_CONF_OPT_UVB3.fits";
555 //cfg_file="startup/9ph/xs_uvf_def1.fits";
556 //cfg_file="/scratch/xcom1/uvb/xs_uvb_def_com1.2.fits";
557 //cfg_file="/scratch/xcom1/review/uvb/startup/amo_prob/data_uvb_startup.sof/XSH_MODEL_CONFIG_FIRST_ANNEAL_UVB.fits";
558 //cfg_file="/home/bristowp/xshp/model/xs_uvb_def_com4.fits";
559 //cfg_file="/scratch/xcom3/uvb/new_local_MODEL_CONFIG_TAB_UVB.fits";
560 //cfg_file="/scratch/xcom1/discrep/uvb/data_2dmap1.sof/xs_uvb_def_com1.3ll.fits";
561 //cfg_file="/scratch/xcom2/uvb/xs_uvb_def_com2.pre.fits";
562 //cfg_file="/scratch/xcom1/uvb/XSH_MODEL_CONFIG_FIRST_ANNEAL_UVB.fits";
563 //cfg_file="/home/bristowp/allscripts/x-s/CCCDD/xs_uvb_FCCDD.fits";
564 //cfg_file="/home/bristowp/allscripts/x-s/CCCDD/xs_uvb_CCCDD.fits";
565 //cfg_file="/home/bristowp/allscripts/x-s/CCCDD/xs_uvb_xCCCDD.fits";
566 //cfg_file="/home/bristowp/xshp/model/xs_uvb_def_oct09.fits";
567 //cfg_file="/scratch/bristowp/bin_check/data_xsh_scired_slit_stare.sof/r.SHOOT.2009-10-07T10:05:49.595_0007.fits";
568 //cfg_file="/scratch/bristowp/IFU_analysis/IFU_DATA/2dmap/PROD_XSH_MOD_CFG_OPT_2D_UVB_keep.fits";
569 //cfg_file="/home/bristowp/allscripts/x-s/cfg_standard.fits";
570 cfg_file="/home/bristowp/allscripts/x-s/cfg_edge.fits";
571 //cfg_file="/scratch/bristowp/IFU_analysis/afc_uvb/SHOOT_IFU_STD_UVB_325_0010_AFC_CFG.fits";
572
573 tag=XSH_MOD_CFG_UVB;
574 arm_ef_ord0=14;
575 arm_lam_min=200;
576 arm_lam_fac=1;
577 n_order=13;
578 ef=xsh_alloc2Darray(n_order,501); /*orders x discrete wavelengths*/
579 efin=fopen("effic_uvb.dat","r");
580 ii=0;
581 while (fscanf(efin,"%64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf \n",
582 &ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],
583 &ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],
584 &ef[10][ii],&ef[11][ii]) !=EOF) {
585 /*
586 printf("%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",
587 ii,ef[0][ii],ef[1][ii],ef[2][ii],ef[3][ii],ef[4][ii],
588 ef[5][ii],ef[6][ii],ef[7][ii],ef[8][ii],ef[9][ii],
589 ef[10][ii],ef[11][ii]);
590 */
591 ii++;
592 }
593 fclose(efin);
595 morder_it=5;
598 blaze_pad=0.000015;
599 dichro_col=0;
600 qe_in=fopen("uvb_qe.dat","r");
601 qe_wv_min=300;
602 qe_wv_step=10;
603 sep_ph=0.357; // 1"=0.255mm
604 }
605 else if (p_xs_3->arm==1) {
606 p_xs_3->BSIZE=VIS_BSIZE;
607 p_xs_3->ASIZE=VIS_ASIZE;
608 p_xs_3->SIZE=VIS_SIZE;
609 p_xs_3->chipypix=VIS_chipypix;
610 p_xs_3->chipxpix=VIS_chipxpix;
611 //cfg_file="jun08sol/xs_vis_def_jun08.fits";
612 //cfg_file="startup/sof/new_xs_vis_def_jun08.fits";
613 //cfg_file="anneal_dev/xsh_model_anneal_save.fits";
614 //cfg_file="startup/sof/xsh_model_first_anneal_save.fits";
615 //cfg_file="startup/sof/PROD_XSH_MOD_CFG_OPT_VIS.fits";
616 //cfg_file="startup/9ph/vis/xs_vis_def.fits";
617 //cfg_file="/scratch/xcom1/vis/xs_vis_def_com1.3.fits";
618 //cfg_file="/scratch/xcom1/discrep/vis/data_2dmap.sof/xs_vis_def_com1.3ll.fits";
619 //cfg_file="/scratch/xcom2/vis/xs_vis_def_com2.1.fits";
620 // cfg_file="/home/bristowp/xshp/model/xs_vis_def_com3.fits";
621 //cfg_file="/home/bristowp/xshp/model/xs_vis_def_com4.fits";
622 //cfg_file="/home/bristowp/allscripts/x-s/test_cfg.fits";
623 //cfg_file="/scratch/bristowp/IFU_analysis/IFU_DATA/2dmap/PROD_XSH_MOD_CFG_OPT_2D_VIS.fits";
624 //cfg_file="/scratch/bristowp/IFU_analysis/afc_vis/PROD_XSH_MOD_CFG_OPT_AFC_VIS.fits";
625 //cfg_file="SHOOT_IFU_STD_VIS_325_0004_AFC_CFG_minus.fits";
626 //cfg_file="/scratch/bristowp/sab_2dmap_QC_data/vis/vis_QC_cfg.fits";
627 //cfg_file="/scratch/bristowp/spieflex/vis/xs_temp.fits";
628 cfg_file="xs_vis_def_aug10.fits";
629 tag=XSH_MOD_CFG_VIS;
630 arm_ef_ord0=12;
631 arm_lam_min=400;
632 arm_lam_fac=1;
633 n_order=15;
634 ef=xsh_alloc2Darray(n_order,701); /*orders x discrete wavelengths*/
635 efin=fopen("effic_vis.dat","r");
636 ii=0;
637 while (fscanf(efin,"%64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf \n",
638 &ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],
639 &ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],
640 &ef[10][ii],&ef[11][ii],&ef[12][ii],&ef[13][ii],&ef[14][ii]) !=EOF) {
641 ii++;
642 }
643 fclose(efin);
645 morder_it=7;
648 blaze_pad=0.00003;
649 dichro_col=1;
650 qe_in=fopen("vis_qe.dat","r");
651 qe_wv_min=320;
652 qe_wv_step=10;
653 sep_ph=0.357;
654 }
655 else {
656 p_xs_3->ASIZE=NIR_ASIZE;
657 p_xs_3->BSIZE=NIR_BSIZE;
658 p_xs_3->SIZE=NIR_SIZE;
659 p_xs_3->chipxpix=NIR_chipxpix;
660 p_xs_3->chipypix=NIR_chipypix;
661 //cfg_file="jun08sol/xs_nir_def_jun08.fits";
662 //cfg_file="startup/sof/XSH_MODEL_CONFIG_FIRST_ANNEAL_NIR.fits";
663 //cfg_file="/home/bristowp/allscripts/x-s/startup/review/nir/startup/PROD_XSH_MODEL_CONFIG_OPT_NIR.fits";
664 //cfg_file="/home/bristowp/allscripts/x-s/startup/9ph/xs_nir_def.fits";
665 //cfg_file="/scratch/xcom1/discrep/nir/new_local_new_local_PROD_XSH_MODEL_CONFIG_OPT_NIR.fits";
666 //cfg_file="/home/bristowp/xshp/model/xs_nir_def_com3.fits";
667 //cfg_file="/home/bristowp/xshp/model/xs_nir_def_jan10.fits";
668 //cfg_file="/scratch/bristowp/zerotab/data_xsh_util_physmod_nir.sof/xs_nir_def_com3.fits";
669 cfg_file="/scratch/bristowp/IFU_analysis/IFU_DATA/2dmap/PROD_XSH_MOD_CFG_OPT_2D_NIR.fits";
670 tag=XSH_MOD_CFG_NIR;
671 arm_ef_ord0=13;
672 arm_lam_min=900;
673 arm_lam_fac=1;
674 n_order=21;
675 ef=xsh_alloc2Darray(n_order,851); /*orders x discrete wavelengths*/
676 efin=fopen("effic_nir.dat","r");
677 ii=0;
678 while (fscanf(efin,"%64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf %64lf \n",
679 &ef[0][ii],&ef[1][ii],&ef[2][ii],&ef[3][ii],&ef[4][ii],
680 &ef[5][ii],&ef[6][ii],&ef[7][ii],&ef[8][ii],&ef[9][ii],
681 &ef[10][ii],&ef[11][ii],&ef[12][ii],&ef[13][ii],&ef[14][ii],
682 &ef[15][ii],&ef[16][ii],&ef[17][ii],&ef[18][ii],&ef[19][ii],
683 &ef[20][ii]) !=EOF) {
684 ii++;
685 }
686 fclose(efin);
688 morder_it=8;
691 blaze_pad=0.00005;
692 dichro_col=2;
693 qe_in=fopen("nir_qe.dat","r");
694 qe_wv_min=800;
695 qe_wv_step=20;
696 sep_ph=0.743162;
697 }
698
699 /*----------------------------------------------
700 Test the maps_create function
701 --------------------------------------------*/
702 if (testfuncflag==11) {
703 //cpl_image* wlmap;
704 struct xs_3 xs_3_config;
705 //struct xs_3* p_xs_3_config;
706 //cpl_frame* config_frame = NULL;
707 cpl_frame* wav_frame = NULL;
708 cpl_frame* slit_frame = NULL;
709 xsh_instrument* inst=NULL;
710 cpl_error_code error_maps;
711 const char * wtag;
712 const char * stag;
713 xsh_free_frame(&config_frame);
714 p_xs_3_config=&xs_3_config;
715 config_frame = cpl_frame_new();
716 cpl_frame_set_filename( config_frame, cfg_file);
717 cpl_frame_set_tag( config_frame,tag);
718 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
719 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
720 return -1;
721 }
722
723 inst=xsh_instrument_new();
724 if (p_xs_3_config->arm==0) {
726 }
727 else if (p_xs_3_config->arm==1) {
729 }
730 else if (p_xs_3_config->arm==2) {
732 }
733 else {
734 printf("ARM NOT SET\n");
735 }
736
738
739 xsh_instrument_set_binx(inst,bin_X);
740 xsh_instrument_set_biny(inst,bin_Y);
741
742 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
743 wtag="chips_wav_test";
744 stag="chips_slit_test";
745 error_maps=xsh_model_maps_create(p_xs_3_config,inst,wtag,stag, &wav_frame, &slit_frame);
746
747 xsh_instrument_free(&inst);
748 }
749 /*----------------------------------------------
750 Test the maps_create function end
751 --------------------------------------------*/
752
753 /*----------------------------------------------
754 Test the xy function
755 -------------------------------------------*/
756 if (testfuncflag==2) {
757 double x,y;
758 struct xs_3 xs_3_config;
759 //struct xs_3* p_xs_3_config;
760 xsh_instrument* inst=NULL;
761 xsh_free_frame(&config_frame);
762
763 p_xs_3_config=&xs_3_config;
764
765 config_frame = cpl_frame_new();
766 cpl_frame_set_filename( config_frame, cfg_file);
767 cpl_frame_set_tag( config_frame, tag);
768
769 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
770 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
771 return -1 ;
772 }
773
774 inst=xsh_instrument_new();
775
776 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
777 xsh_instrument_set_binx(inst,bin_X);
778 xsh_instrument_set_biny(inst,bin_Y);
779
780 if (p_xs_3_config->arm==0) {
782
783 xsh_model_get_xy(p_xs_3_config,inst,404.656494,18,0.0, &x, &y);
784 printf("404.656494nm in order 18 on arm %d : x=%lf y=%lf \n",
786
787 xsh_model_get_xy(p_xs_3_config,inst,385.11790,20,-5.50696, &x, &y);
788 printf("385.11790nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
790 xsh_model_get_xy(p_xs_3_config,inst,385.11790,20,-5.49536, &x, &y);
791 printf("385.11790nm at s=-5.49536 in order 20 on arm %d : x=%lf y=%lf \n",
793 xsh_model_get_xy(p_xs_3_config,inst,385.11790,20,-5.51854, &x, &y);
794 printf("385.11790nm at s=-5.51854 in order 20 on arm %d : x=%lf y=%lf \n",
796 xsh_model_get_xy(p_xs_3_config,inst,385.11940,20,-5.50696, &x, &y);
797 printf("385.11940nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
799 xsh_model_get_xy(p_xs_3_config,inst,385.11590,20,-5.50696, &x, &y);
800 printf("385.11590nm at s=-5.50696 in order 20 on arm %d : x=%lf y=%lf \n",
802
803 xsh_model_get_xy(p_xs_3_config,inst,369.4774,20,-2.50531, &x, &y);
804 printf("369.4774nm at s=-2.50531 in order 20 on arm %d : x=%lf y=%lf \n",
806 }
807 else if (p_xs_3_config->arm==1) {
809 xsh_model_get_xy(p_xs_3_config,inst,649.06520,25,-3.85736,&x,&y);
810 printf("649.06520nm in order 25 on arm %d : x=%lf y=%lf \n",
812 xsh_model_get_xy(p_xs_3_config,inst,676.0587,24,-1.9593574,&x,&y);
813 printf("676.0587nm in order 24 on arm %d : x=%lf y=%lf \n",
815 xsh_model_get_xy(p_xs_3_config,inst,573.0815,29,-0.6741213,&x,&y);
816 printf("573.0815nm in order 29 on arm %d : x=%lf y=%lf \n",
818 xsh_model_get_xy(p_xs_3_config,inst,871.2175,18,2.8843977,&x,&y);
819 printf("871.2175nm in order 18 on arm %d : x=%lf y=%lf \n",
821 xsh_model_get_xy(p_xs_3_config,inst,676.0599,24,-1.9593425,&x,&y);
822 printf("676.0599nm in order 24 on arm %d : x=%lf y=%lf \n",
824 xsh_model_get_xy(p_xs_3_config,inst,573.0818,29,-0.6741213,&x,&y);
825 printf("573.0818nm in order 29 on arm %d : x=%lf y=%lf \n",
827 xsh_model_get_xy(p_xs_3_config,inst,871.2192,18,2.9050856,&x,&y);
828 printf("871.2192nm in order 18 on arm %d : x=%lf y=%lf \n",
830/* xsh_model_get_xy(p_xs_3_config,inst,742.752,22,0.0,&x,&y); */
831/* printf("742.752nm in order 22 on arm %d : x=%lf y=%lf \n", */
832/* xsh_instrument_get_arm(inst),x,y); */
833/* xsh_model_get_xy(p_xs_3_config,inst,742.738,22,0.0,&x,&y); */
834/* printf("742.738nm in order 22 on arm %d : x=%lf y=%lf \n", */
835/* xsh_instrument_get_arm(inst),x,y); */
836/* xsh_model_get_xy(p_xs_3_config,inst,742.746,22,0.0,&x,&y); */
837/* printf("742.746nm in order 22 on arm %d : x=%lf y=%lf \n", */
838/* xsh_instrument_get_arm(inst),x,y); */
839/* xsh_model_get_xy(p_xs_3_config,inst,742.758,22,0.0,&x,&y); */
840/* printf("742.758nm in order 22 on arm %d : x=%lf y=%lf \n", */
841/* xsh_instrument_get_arm(inst),x,y); */
842/* xsh_model_get_xy(p_xs_3_config,inst,742.760,22,0.0,&x,&y); */
843/* printf("742.760nm in order 22 on arm %d : x=%lf y=%lf \n", */
844/* xsh_instrument_get_arm(inst),x,y); */
845/* xsh_model_get_xy(p_xs_3_config,inst,742.766,22,0.0,&x,&y); */
846/* printf("742.766nm in order 22 on arm %d : x=%lf y=%lf \n", */
847/* xsh_instrument_get_arm(inst),x,y); */
848
849/* xsh_model_get_xy(p_xs_3_config,inst,750.0,22,0.0,&x,&y); */
850/* printf("750nm in order 22 on arm %d : x=%lf y=%lf \n", */
851/* xsh_instrument_get_arm(inst),x,y); */
852/* xsh_model_get_xy(p_xs_3_config,inst,649.07310,25,-4.72614,&x,&y); */
853/* printf("649.07310nm at s=-4.72614 in order 25 on arm %d : x=%lf y=%lf \n", */
854/* xsh_instrument_get_arm(inst),x,y); */
855/* xsh_model_get_xy(p_xs_3_config,inst,633.4427,26,0.0,&x,&y); */
856/* printf("633.4427nm at s=0.0 in order 26 on arm %d : x=%lf y=%lf \n", */
857/* xsh_instrument_get_arm(inst),x,y); */
858
859
860/* xsh_model_get_xy(p_xs_3_config,inst,713.0,22,0.0,&x,&y); */
861/* printf("713nm in order 22 on arm %d : x=%lf y=%lf \n", */
862/* xsh_instrument_get_arm(inst),x,y); */
863/* xsh_model_get_xy(p_xs_3_config,inst,742.0,22,0.0,&x,&y); */
864/* printf("742nm in order 22 on arm %d : x=%lf y=%lf \n", */
865/* xsh_instrument_get_arm(inst),x,y); */
866/* xsh_model_get_xy(p_xs_3_config,inst,763.0,22,0.0,&x,&y); */
867/* printf("763nm in order 22 on arm %d : x=%lf y=%lf \n", */
868/* xsh_instrument_get_arm(inst),x,y); */
869 }
870 else if (p_xs_3_config->arm==2) {
872 xsh_model_get_xy(p_xs_3_config,inst,1382.36,19,-0.7106,&x,&y);
873 printf("1382.36nm in order 19 on arm %d : x=%lf y=%lf \n",
875 xsh_model_get_xy(p_xs_3_config,inst,1382.37,19,-0.7106,&x,&y);
876 printf("1382.37nm in order 19 on arm %d : x=%lf y=%lf \n",
878 xsh_model_get_xy(p_xs_3_config,inst,1382.38,19,-0.7106,&x,&y);
879 printf("1382.38nm in order 19 on arm %d : x=%lf y=%lf \n",
881 xsh_model_get_xy(p_xs_3_config,inst,1382.108,19,5.55761,&x,&y);
882 printf("1382.108nm at s=5.55761 in order 19 on arm %d : x=%lf y=%lf \n",
884 }
885 else {
886 printf("ARM NOT SET\n");
887 }
888
889/* xsh_model_get_xy(p_xs_3_config,inst,1296.021,20,0.0, &x, &y); */
890/* printf("1296.021nm in order 20 on arm %d : x=%lf y=%lf \n", */
891/* xsh_instrument_get_arm(inst),x,y); */
892 xsh_instrument_free(&inst);
893 cpl_frame_delete(config_frame) ;
894 }
895 /*----------------------------------------------
896 Test the xy function end
897 --------------------------------------------*/
898
899 /*----------------------------------------------
900 Test the locus function
901 --------------------------------------------*/
902 if (testfuncflag==3) {
903 cpl_vector** trace;
904 struct xs_3 xs_3_config;
905 //struct xs_3* p_xs_3_config;
906 int tempsize=p_xs_3->BSIZE;
907 xsh_instrument* inst=NULL;
908
909 xsh_free_frame(&config_frame);
910 //if (p_xs_3->arm<2) tempsize=p_xs_3->BSIZE;
911
912 p_xs_3_config=&xs_3_config;
913
914 config_frame = cpl_frame_new();
915 cpl_frame_set_filename( config_frame, cfg_file);
916 cpl_frame_set_tag( config_frame, tag);
917
918 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
919 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
920 return -1 ;
921 }
922 inst=xsh_instrument_new();
923 xsh_instrument_set_binx(inst,bin_X);
924 xsh_instrument_set_biny(inst,bin_Y);
925 if (p_xs_3_config->arm==0) {
927 }
928 else if (p_xs_3_config->arm==1) {
930 }
931 else if (p_xs_3_config->arm==2) {
933 }
934 else {
935 printf("ARM NOT SET\n");
936 }
937 trace=xsh_model_locus(p_xs_3_config,inst,0.0);
939 for (ii=0;ii<tempsize;ii+=1) {
940 printf("order %d dispersion pixel no: %d, x-dispersion: %lf \n",
941 morder, ii, cpl_vector_get(trace[morder-morder_min],ii));
942 }
943 }
944 for (ii=0 ; ii<16 ; ii++) cpl_vector_delete(trace[ii]);
945 cpl_free(trace) ;
946 xsh_instrument_free(&inst);
947 cpl_frame_delete(config_frame) ;
948 }
949 /*----------------------------------------------
950 Test the locus function end
951 --------------------------------------------*/
952
953 /*----------------------------------------------
954 Test the spectralformat_create function
955 --------------------------------------------*/
956 if (testfuncflag==5) {
957 struct xs_3 xs_3_config;
958 //struct xs_3* p_xs_3_config;
959 //cpl_frame* config_frame = NULL;
960 cpl_frame* temp_sf_frame=NULL ;
961
962 p_xs_3_config=&xs_3_config;
963
964 config_frame = cpl_frame_new();
965 cpl_frame_set_filename( config_frame, cfg_file);
966 cpl_frame_set_tag( config_frame, tag);
967
968 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
969 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
970 return -1 ;
971 }
972
973 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
974 check(temp_sf_frame=xsh_model_spectralformat_create(p_xs_3_config,
975 "SFtestTAB.fits"));
976
977 xsh_msg("SF2 table %s %s", cpl_frame_get_filename(temp_sf_frame),
978 cpl_frame_get_tag(temp_sf_frame));
979
980 cpl_frame_delete(config_frame) ;
981 xsh_free_frame(&temp_sf_frame);
982
983 }
984 /*----------------------------------------------
985 Test the spectralformat_create function end
986 --------------------------------------------*/
987
988
989 /*----------------------------------------------
990 Test the THE function
991 --------------------------------------------*/
992 if (testfuncflag==4) {
993 struct xs_3 xs_3_config;
994 //struct xs_3* p_xs_3_config;
995 xsh_instrument* inst=NULL;
996 cpl_frame * temp_THEproduct_frame ;
997 //char name_p[512] ;
998
999 xsh_free_frame(&config_frame);
1000 p_xs_3_config=&xs_3_config;
1001
1002 config_frame = cpl_frame_new();
1003 cpl_frame_set_filename( config_frame, cfg_file);
1004 cpl_frame_set_tag( config_frame,tag);
1005
1006 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
1007 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
1008 return -1 ;
1009 }
1010 inst=xsh_instrument_new();
1011 xsh_instrument_set_binx(inst,bin_X);
1012 xsh_instrument_set_biny(inst,bin_Y);
1013
1014 if (p_xs_3_config->arm==0) {
1016 }
1017 else if (p_xs_3_config->arm==1) {
1019 }
1020 else if (p_xs_3_config->arm==2) {
1022 }
1023 else {
1024 printf("ARM NOT SET\n");
1025 }
1026 xsh_model_binxy(p_xs_3_config,1,1);
1027 fitsrow=0;
1028 if (xsh_instrument_get_arm(inst)<2) {
1029 temp_THEproduct_frame=xsh_model_THE_create(p_xs_3_config,inst,
1030 wave_list_file,1,0.0,
1031 "THEtestTAB.fits");
1032 }
1033 else {
1034 temp_THEproduct_frame=xsh_model_THE_create(p_xs_3_config,inst,
1035 wave_list_file,9,1.4,
1036 "THEtestTAB.fits");
1037 }
1038
1039 xsh_msg(" THE table %s %s",
1040 cpl_frame_get_filename(temp_THEproduct_frame),
1041 cpl_frame_get_tag(temp_THEproduct_frame));
1042
1043 cpl_frame_delete(config_frame) ;
1044 xsh_free_frame(&temp_THEproduct_frame);
1045 xsh_instrument_free(&inst);
1046 }
1047 /*----------------------------------------------
1048 Test the THE function end
1049 --------------------------------------------*/
1050
1051 /*----------------------------------------------
1052 ex1D test
1053 --------------------------------------------*/
1054 if (testfuncflag==7) {
1055 cpl_image * im = NULL;
1056 cpl_image * wavmap = NULL;
1057 double* detec=NULL;
1058 double* wavmap_pix=NULL;
1059 //int ord_min=17;
1060 int h,kk, real_ord;
1061 cpl_vector * extracted;
1062 cpl_vector** trace;
1063 struct xs_3 xs_3_config;
1064 xsh_instrument* inst=NULL;
1065 char order_tab_name[256];
1066 cpl_propertylist *header = NULL;
1067 cpl_frame* order_tab_frame = NULL;
1068 cpl_table* table = NULL;
1069 int nbcol, med_wid;
1070 int starty, endy, order_tab_flag;
1071 xsh_order_list* order_tab = NULL;
1072 int order_tab_size = 0;
1073 double wvlen, dx, tot;
1074 cpl_vector * ext_med_fil;
1075 //cpl_vector * spec_clean;
1076 //cpl_vector * filtered;
1077
1078 xsh_free_frame(&config_frame);
1079 p_xs_3_config=&xs_3_config;
1080 config_frame = cpl_frame_new();
1081 cpl_frame_set_filename( config_frame, cfg_file);
1082 cpl_frame_set_tag( config_frame,tag);
1083 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
1084 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
1085 return -1;
1086 }
1087 inst=xsh_instrument_new();
1088 xsh_instrument_set_binx(inst,bin_X);
1089 xsh_instrument_set_biny(inst,bin_Y);
1090
1091 if (p_xs_3_config->arm==0) {
1093 }
1094 else if (p_xs_3_config->arm==1) {
1096 }
1097 else if (p_xs_3_config->arm==2) {
1099 }
1100 else {
1101 printf("ARM NOT SET\n");
1102 }
1103
1104 order_tab_flag=0;
1105 if (order_tab_flag==0) {
1106 strcpy(order_tab_name,"none");
1107 nbcol=morder_max-morder_min+1;
1108 trace=xsh_model_locus(p_xs_3_config,inst,0.0);
1109 }
1110 else {
1111 //strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Mar08.fits");
1112 strcpy(order_tab_name,"PROD_ORDER_TAB_CENTR_UVB.fits");
1113 //strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Dec07.fits");
1114 /* Create frames */
1115 //XSH_ASSURE_NOT_NULL( order_tab_name);
1116 cknull( order_tab_name);
1117 order_tab_frame = cpl_frame_new();
1118 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
1119 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
1120 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
1121 XSH_TABLE_LOAD( table, order_tab_name);
1122 check( nbcol = cpl_table_get_nrow(table));
1123 if (nbcol == XSH_ORDERS_UVB){
1124 inst = xsh_instrument_new() ;
1127 }
1128 else if (nbcol == XSH_ORDERS_VIS){
1129 inst = xsh_instrument_new() ;
1132 }
1133 else if (nbcol == XSH_ORDERS_NIR){
1134 inst = xsh_instrument_new() ;
1137 }
1138 else{
1139 xsh_msg("invalid nbcol %d", nbcol);
1140 }
1141 xsh_instrument_set_binx(inst,bin_X);
1142 xsh_instrument_set_biny(inst,bin_Y);
1143 check( order_tab = xsh_order_list_load( order_tab_frame, inst));
1144 order_tab_size = order_tab->size;
1145 check ( header = cpl_propertylist_load( order_tab_name, 0));
1146 XSH_TABLE_LOAD( table, order_tab_name);
1147 check( nbcol = cpl_table_get_nrow(table));
1148 }
1149
1150 lines_tab = cpl_table_load(wave_list_file, 1, 0);
1151 cpl_error_reset() ;
1152 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
1153
1154 //check(wavmap=cpl_image_load("xs_uvb_Mar08_wav_map.fits", CPL_TYPE_DOUBLE, 0, 0));
1155 check(wavmap=cpl_image_load("xs_vis_Mar08_wav_map.fits", CPL_TYPE_DOUBLE, 0, 0));
1156 //check(wavmap=cpl_image_load("xs_nir_Apr08_wav_map.fits", CPL_TYPE_DOUBLE, 0, 0));
1157 check(wavmap_pix=cpl_image_get_data_double(wavmap));
1158
1159 check(extracted = cpl_vector_new(p_xs_3_config->SIZE)) ;
1160 check(im=cpl_image_load("9ph_test/FMTCHK_VIS_multi.fits", CPL_TYPE_DOUBLE, 0, 0));
1161 //check(im=cpl_image_load("9ph_test/FMTCHK_NIR_PRE.fits", CPL_TYPE_DOUBLE, 0, 0));
1162 //check(im=cpl_image_load("071205_VIS_ThAr_05pin_RM1_30s.fits", CPL_TYPE_DOUBLE, 0, 0));
1163 //check(im=cpl_image_load("9ph_test/FMTCHK_UVB_multi.fits", CPL_TYPE_DOUBLE, 0, 0));
1164 check(detec=cpl_image_get_data_double(im));
1165 h=3;
1166 med_wid=200;
1167 for (ii=0;ii<nbcol;ii++) {
1168 real_ord=ii+morder_min;
1169 if (order_tab_flag!=0){
1170 starty = order_tab->list[ii].starty;
1171 endy = order_tab->list[ii].endy;
1172 }
1173 else {
1174 starty = 1;
1175 endy = p_xs_3_config->SIZE-201 ;
1176 }
1177 if (starty == 0 && endy == 0){
1178 xsh_msg("Warning starty and endy equal zero, put endy to %d",
1179 p_xs_3_config->SIZE-1);
1180 starty =0;
1181 endy = p_xs_3_config->SIZE-1;
1182 }
1183
1184 for (jj=0;jj<p_xs_3_config->SIZE;jj++) {
1185 cpl_vector_set(extracted,jj,0.0);
1186 }
1187 /*now loop over dispersion coordinate:
1188 - Compute x-disp coord from orderpos poly
1189 - flux from +/-h pix
1190 */
1191 for (jj=starty;jj<endy;jj++) {
1192 tot=0;
1193 if (order_tab_flag!=0){
1194 check(dx=cpl_polynomial_eval_1d(order_tab->list[ii].cenpoly,jj, NULL));
1195 }
1196 else {
1197 dx=cpl_vector_get(trace[ii],jj);
1198 }
1199 //printf("%d %d %lf \n",ii,jj,dx);
1200 for (kk=-h; kk<=h; kk++) {
1201 if (p_xs_3_config->arm==0) {
1202 tot+=detec[2044*jj+(int)(dx+0.5)+kk];// for UVB
1203 }
1204 else if (p_xs_3_config->arm==1) {
1205 tot+=detec[2044*jj+(int)(dx+0.5)+kk];//
1206 }
1207 else {
1208 tot+=detec[1056*jj+((int)(dx+0.5)+kk)];// for NIR
1209 //printf("%d %d %lf \n",jj, (int)(dx+0.5)+kk,detec[((int)(dx+0.5)+kk+18)*2048+(2048-jj)]);
1210 }
1211 }
1212 cpl_vector_set(extracted,jj,tot);
1213 }
1214 ext_med_fil=cpl_vector_filter_median_create(extracted,med_wid);
1215/* for (jj=0;jj<p_xs_3_config->BSIZE;jj++) { */
1216/* printf("%d %lf\n",jj,cpl_vector_get(ext_med_fil,jj)); */
1217/* } */
1218 for (jj=0;jj<med_wid;jj++) {
1219 cpl_vector_set(ext_med_fil,jj,cpl_vector_get(ext_med_fil,med_wid+1));
1220 }
1221 for (jj=p_xs_3_config->SIZE-med_wid-2;jj<p_xs_3_config->SIZE;jj++) {
1222 cpl_vector_set(ext_med_fil,jj,cpl_vector_get(ext_med_fil,p_xs_3_config->BSIZE-med_wid-3));
1223 }
1224 cpl_vector_subtract(extracted,ext_med_fil);
1225 for (jj=starty;jj<endy;jj++) {
1226 if (order_tab_flag!=0){
1227 check(dx=cpl_polynomial_eval_1d(order_tab->list[ii].cenpoly,jj, NULL));
1228 }
1229 else {
1230 dx=cpl_vector_get(trace[ii],jj);
1231 }
1232 wvlen=wavmap_pix[(jj)*p_xs_3_config->ASIZE+(int)(dx)];
1233 if (wvlen>0.0) {
1234 printf("%d %d %lf %lf %lf \n",real_ord,jj,wvlen,dx,cpl_vector_get(extracted,jj));
1235 }
1236 //printf("%lf %d \n",dx,jj);
1237 }
1238 cpl_vector_delete(ext_med_fil);
1239 }
1240 if (strcmp(order_tab_name,"none") == 0){
1241 for (ii=0 ; ii<16 ; ii++) cpl_vector_delete(trace[ii]);
1242 cpl_free(trace) ;
1243 }
1244 cpl_vector_delete(extracted);
1245 xsh_instrument_free(&inst);
1246 }
1247
1248 /*----------------------------------------------
1249 END ex1D test
1250 --------------------------------------------*/
1251
1252 /*----------------------------------------------
1253 fk test
1254 --------------------------------------------*/
1255 if (testfuncflag==8) {
1256 cpl_image * im = NULL;
1257 cpl_image * wavmap = NULL;
1258 double* ex_detec=NULL;
1259 double* wavmap_pix=NULL;
1260 int n_ord=15;
1261 int ord_min=17;
1262 int kk, real_ord;
1263 cpl_vector * extracted;
1264 cpl_vector * bright_lines[n_ord] ;
1265 struct xs_3 xs_3_config;
1266 //struct xs_3* p_xs_3_config;
1267 //cpl_frame* config_frame = NULL;
1268 xsh_instrument* inst=NULL;
1269 //double* detec=NULL;
1270 char order_tab_name[256];
1271 cpl_propertylist *header = NULL;
1272 cpl_frame* order_tab_frame = NULL;
1273 cpl_table* table = NULL;
1274 int nbcol;
1275 //int starty, endy;
1276 int yint;
1277 xsh_order_list* order_tab = NULL;
1278 int order_tab_size = 0;
1279 double wvlen, dx;
1280 //double iorder, x, y;
1281 xsh_free_frame(&config_frame);
1282 p_xs_3_config=&xs_3_config;
1283 config_frame = cpl_frame_new();
1284 cpl_frame_set_filename( config_frame, cfg_file);
1285 cpl_frame_set_tag( config_frame,tag);
1286 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
1287 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
1288 return -1;
1289 }
1290 inst=xsh_instrument_new();
1291 xsh_instrument_set_binx(inst,bin_X);
1292 xsh_instrument_set_biny(inst,bin_Y);
1293 if (p_xs_3_config->arm==0) {
1295 }
1296 else if (p_xs_3_config->arm==1) {
1298 }
1299 else if (p_xs_3_config->arm==2) {
1301 }
1302 else {
1303 printf("ARM NOT SET\n");
1304 }
1305
1306 strcpy(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Mar08.fits");
1307 //strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Dec07.fits");
1308
1309 /* Create frames */
1310 //XSH_ASSURE_NOT_NULL( order_tab_name);
1311 cknull( order_tab_name);
1312 order_tab_frame = cpl_frame_new();
1313 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
1314 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
1315 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
1316 XSH_TABLE_LOAD( table, order_tab_name);
1317 check( nbcol = cpl_table_get_nrow(table));
1318 if (nbcol == XSH_ORDERS_UVB){
1319 inst = xsh_instrument_new() ;
1322 }
1323 else if (nbcol == XSH_ORDERS_VIS){
1324 inst = xsh_instrument_new() ;
1327 }
1328 else if (nbcol == XSH_ORDERS_NIR){
1329 inst = xsh_instrument_new() ;
1332 }
1333 else{
1334 xsh_msg("invalid nbcol %d", nbcol);
1335 }
1336 check( order_tab = xsh_order_list_load( order_tab_frame, inst));
1337 order_tab_size = order_tab->size;
1338
1339 check ( header = cpl_propertylist_load( order_tab_name, 0));
1340
1341 lines_tab = cpl_table_load(wave_list_file, 1, 0);
1342 cpl_error_reset() ;
1343 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
1344
1345 XSH_TABLE_LOAD( table, order_tab_name);
1346 check( nbcol = cpl_table_get_nrow(table));
1347
1348 check(wavmap=cpl_image_load("chips_wav_test.fits", CPL_TYPE_DOUBLE, 0, 0));
1349 check(wavmap_pix=cpl_image_get_data_double(wavmap));
1350
1351 // check(im2d=cpl_image_load("SHOOT_SLT_FCK_VIS_063_0019.fits", CPL_TYPE_DOUBLE, 0, 0));
1352 // check(detec=cpl_image_get_data_double(im2d));
1353
1354 check(extracted = cpl_vector_new(p_xs_3->BSIZE)) ;
1355 check(im=cpl_image_load("/home/fkerber/Xshooter/AIT/ThAr_currrent/080131_VIS_RM4_ThAr2+ND1_20mA_1s_wcal.fits", CPL_TYPE_DOUBLE, 0, 0));
1356 check(ex_detec=cpl_image_get_data_double(im));
1357 for (ii=0;ii<n_ord-1;ii++) {
1358 real_ord=30-ii;
1359 for (jj=0;jj<p_xs_3->BSIZE;jj++) {
1360 cpl_vector_set(extracted,jj,ex_detec[ii*p_xs_3->BSIZE+jj]);
1361 //printf("%d %d %lf \n",ii,jj,cpl_vector_get(extracted,jj));
1362 }
1363 bright_lines[ii]=xsh_model_refining_detect(extracted,3,5.0, 0);
1364 for (kk=0; kk<cpl_vector_get_size(bright_lines[ii]);kk++) {
1365 yint=(int)(cpl_vector_get(bright_lines[ii],kk));
1366 check(dx=cpl_polynomial_eval_1d(order_tab->list[real_ord-ord_min].cenpoly,yint, NULL));
1367 wvlen=wavmap_pix[yint*p_xs_3->ASIZE+(int)(dx)];
1368 printf("%d %d %lf %lf \n",
1369 real_ord,kk,cpl_vector_get(bright_lines[ii],kk),wvlen);
1370/* for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) { */
1371/* check(lambda_nm=cpl_table_get_double(lines_tab, "Wavelength", line_cnt, NULL)); */
1372/* check(inten = (float)(cpl_table_get_double(lines_tab, "Emission", line_cnt, NULL))); */
1373/* blaze_wav=2*(sin(-p_xs_3_config->nug))/((float)(real_ord)*p_xs_3_config->sg); */
1374/* lam_max=1000000.0*blaze_wav*((float)(real_ord)/((float)(real_ord)-0.5)); */
1375/* lam_min=1000000.0*blaze_wav*((float)(real_ord)/(0.5+(float)(real_ord))); */
1376/* /\* check lambda is in FSR for this order and get its disp co-ord from model, x-disp co-ord from order_tab*\/ */
1377/* //if (lambda_nm<lam_max && lambda_nm>lam_min) { */
1378
1379/* starty = order_tab->list[real_ord-ord_min].starty; */
1380/* endy = order_tab->list[real_ord-ord_min].endy; */
1381/* if (starty == 0 && endy == 0){ */
1382/* xsh_msg("Warning starty and endy equal zero, put endy to %d",3999); */
1383/* starty =0; */
1384/* endy = 3999; */
1385/* } */
1386/* check(xsh_model_get_xy(p_xs_3_config,inst,lambda_nm,real_ord,0.0,&x,&y)); */
1387/* if ((int)(y)>starty && (int)(y)<endy) { */
1388/* check(dx=cpl_polynomial_eval_1d(order_tab->list[real_ord-ord_min].cenpoly,(int)(y), NULL)); */
1389/* //printf("%lf %lf \n",dx,y); */
1390/* if (fabs(y-cpl_vector_get(bright_lines[ii],kk))<5.0) { */
1391/* printf("%d %lf %lf %lf %lf %lf %lf \n",real_ord, lambda_nm, y, cpl_vector_get(bright_lines[ii],kk), dx, x, y-cpl_vector_get(bright_lines[ii],kk)); */
1392/* } */
1393/* } */
1394/* } */
1395 }
1396 }
1397 xsh_instrument_free(&inst);
1398 }
1399 /*----------------------------------------------
1400 fk test
1401 --------------------------------------------*/
1402
1403
1404 /*----------------------------------------------
1405 order tab test
1406 --------------------------------------------*/
1407 if (testfuncflag==10) {
1408 struct xs_3 xs_3_config;
1409 cpl_frame* temp_oe_frame=NULL ;
1410
1411 //cfg_file="/home/bristowp/allscripts/x-s/CCCDD/xs_uvb_FCCDD.fits";
1412
1413 p_xs_3_config=&xs_3_config;
1414
1415 check(config_frame = cpl_frame_new());
1416 check(cpl_frame_set_filename(config_frame, cfg_file));
1417 cpl_frame_set_tag( config_frame, tag);
1418
1419 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
1420 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
1421 return -1 ;
1422 }
1423 xsh_model_binxy(p_xs_3_config, bin_X, bin_Y);
1424 check(temp_oe_frame=xsh_model_order_edges_tab_create(p_xs_3_config,
1425 "OEtestTAB.fits"));
1426 xsh_msg("oet table %s %s", cpl_frame_get_filename(temp_oe_frame),
1427 cpl_frame_get_tag(temp_oe_frame));
1428
1429 cpl_frame_delete(config_frame) ;
1430 xsh_free_frame(&temp_oe_frame);
1431
1432 exit(1);
1433 }
1434
1435
1436
1437 /*----------------------------------------------
1438 rec test
1439 --------------------------------------------*/
1440 if (testfuncflag==9) {
1441 float dx;
1442 struct xs_3 xs_3_config;
1443 //struct xs_3* p_xs_3_config;
1445
1446 XSH_INSTRCONFIG* iconfig = NULL;
1447
1448 char order_tab_name[256];
1449 cpl_frame* order_tab_frame = NULL;
1450 cpl_table* table = NULL;
1451 cpl_table* line_table = NULL;
1452 int nbcol;
1453 xsh_order_list* order_tab = NULL;
1454 int order_tab_size = 0;
1455 int iy,iyuse, iorder;
1456 //int selected;
1457 int ny;
1458 //int wid;
1459 int h, w, wmin, min_iso;
1460 //FILE* order_tab_file = NULL;
1461 cpl_propertylist *header = NULL;
1462 const char* pro_catg = NULL;
1463 cpl_vector * extracted;
1464 cpl_vector * ext_med_fil;
1465 //cpl_vector * spec_clean;
1466 //cpl_vector * filtered;
1467 cpl_vector * bright_lines[15] ;
1468
1469 cpl_image * im = NULL;
1470 double* detec=NULL;
1471 double tot;
1472 //int intensity;
1473 double x, y, cent, cent2, flux, cent2_cor, flux_cor;
1474 double flux_keep=0.0;
1475 double last_cent=0.0;
1476 double prec, ex_cor, med, stdev_ord;
1477 int lmax, rmax;
1478 int starty, endy;
1479
1480 xsh_free_frame(&config_frame);
1481 rec_out=fopen("rec_out","w");
1482 ex1d_out=fopen("ex1d_out","w");
1483
1484 //cfg_file="anneal_dev/xs_vis_def_Dec07.fits";
1485 //cfg_file="/home/bristowp/allscripts/x-s/jun08sol/xs_vis_def_jun08.fits";
1486 cfg_file="/home/bristowp/allscripts/x-s/jun08sol/xs_uvb_def_jun08.fits";
1487 //cfg_file="9ph_test/xs_uvb_pre_def.fits";
1488 //cfg_file="new_new_new_xs_uvb_pre_def.fits";
1489 //cfg_file="anneal_dev/xs_nir_long_open_anneal.fits";
1490 //cfg_file="9ph_test/nir/new_xs_nir_d2_xd.fits";
1491
1492 p_xs_3_config=&xs_3_config;
1493 check(config_frame = cpl_frame_new());
1494 check(cpl_frame_set_filename( config_frame, cfg_file));
1495 cpl_frame_set_tag( config_frame, tag);
1496
1497 if (xsh_model_config_load_best(config_frame, p_xs_3_config) != CPL_ERROR_NONE) {
1498 fclose(ex1d_out);
1499 fclose(rec_out);
1500 cpl_msg_error(__func__, "Cannot load %s as a config", cfg_file) ;
1501 return -1 ;
1502 }
1503
1504
1505 //check(im=cpl_image_load("/scratch/NIR_080423/080423_NIR_ThAr_Pinhole_DIT6s0001.fits", CPL_TYPE_DOUBLE, 0, 0));
1506 //check(im=cpl_image_load("9ph_test/nir/ON_PRE_0.fits", CPL_TYPE_DOUBLE, 0, 0));
1507 //check(im=cpl_image_load("/scratch/pen_rays/080218_VIS_RM4_Ar_0p1s.fits", CPL_TYPE_DOUBLE, 0, 0));
1508 //check(im=cpl_image_load("/scratch/pen_rays/080218_VIS_RM4_Kr_0p1s.fits", CPL_TYPE_DOUBLE, 0, 0));
1509 //check(im=cpl_image_load("/scratch/pen_rays/080218_VIS_RM4_Ne_0p05s.fits", CPL_TYPE_DOUBLE, 0, 0));
1510 //check(im=cpl_image_load("/scratch/pen_rays/080218_VIS_RM4_Xe_0p05s.fits", CPL_TYPE_DOUBLE, 0, 0));
1511 //check(im=cpl_image_load("/scratch/pen_rays/080222_UVB_Pin03_Ar_RM4_12s.1.fits", CPL_TYPE_DOUBLE, 0, 0));
1512 //check(im=cpl_image_load("/scratch/pen_rays/080222_UVB_Pin03_Ne_RM4_6s.fits", CPL_TYPE_DOUBLE, 0, 0));
1513 //check(im=cpl_image_load("/scratch/pen_rays/080222_UVB_Pin03_Xe_RM4_24s.fits", CPL_TYPE_DOUBLE, 0, 0));
1514 //check(im=cpl_image_load("/scratch/pen_rays/080222_UVB_Pin03_Kr_RM4_12s.fits", CPL_TYPE_DOUBLE, 0, 0));
1515 check(im=cpl_image_load("/scratch/pen_rays/pre_allpen_mar08.fits", CPL_TYPE_DOUBLE, 0, 0));
1516 //check(im=cpl_image_load("SHOOT_SLT_FCK_VIS_063_0019.fits", CPL_TYPE_DOUBLE, 0, 0));
1517 //check(im=cpl_image_load("/scratch/UVB_080227/080226_UVB_pin0p5_ThAr_RM4_5p0s_FMTCHK_PRE.fits", CPL_TYPE_DOUBLE, 0, 0));
1518 //check(im=cpl_image_load("071205_VIS_ThAr_05pin_RM1_30s.fits", CPL_TYPE_DOUBLE, 0, 0));
1519 check(detec=cpl_image_get_data_double(im));
1520
1521 //detect is 1D, if x is disp and y is x-disp:
1522 //x,y is @ ASIZE*y+x
1523
1524 /* Initialize libraries */
1525 check(cpl_msg_set_level(CPL_MSG_DEBUG));
1527
1528 //strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Mar08.fits");
1529 //strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_VIS_Dec07.fits");
1530 strcpy(order_tab_name,"PROD_ORDER_TAB_CENTR_UVB.fits");
1531 //strcat(order_tab_name,"PROD_ORDER_TAB_CENTR_NIR.fits");
1532 /* Create frames */
1533 //XSH_ASSURE_NOT_NULL( order_tab_name);
1534 cknull( order_tab_name);
1535 order_tab_frame = cpl_frame_new();
1536 cpl_frame_set_filename( order_tab_frame, order_tab_name) ;
1537 cpl_frame_set_level( order_tab_frame, CPL_FRAME_LEVEL_TEMPORARY);
1538 cpl_frame_set_group( order_tab_frame, CPL_FRAME_GROUP_RAW );
1539
1540 XSH_TABLE_LOAD( table, order_tab_name);
1541 check( nbcol = cpl_table_get_nrow(table));
1542 if (nbcol == XSH_ORDERS_UVB){
1546 p_xs_3->ASIZE=2044;
1547 p_xs_3->BSIZE=2800;
1548 }
1549 else if (nbcol == XSH_ORDERS_VIS){
1553 }
1554 else if (nbcol == XSH_ORDERS_NIR){
1558 p_xs_3->BSIZE=p_xs_3->BSIZE-8;
1559 p_xs_3->ASIZE=p_xs_3->ASIZE-4;
1560 }
1561 else{
1562 xsh_msg("invalid nbcol %d", nbcol);
1563 }
1567
1569 ny = iconfig->ny;
1570 check( order_tab = xsh_order_list_load( order_tab_frame, instrument));
1571 order_tab_size = order_tab->size;
1572
1573 check ( header = cpl_propertylist_load( order_tab_name, 0));
1574 check( pro_catg = xsh_pfits_get_pcatg( header));
1575
1576 xsh_msg("Order Table of type %s",pro_catg);
1577
1578 /*load a line list*/
1579 line_table = cpl_table_load(wave_list_file, 1, 0);
1580 //cpl_error_reset() ;
1581 check(lines_tot = cpl_table_get_nrow(line_table)) ;
1582
1583 min_iso=5;
1584 wmin=4;
1585 h=4;
1586
1587 for( iorder=0; iorder< order_tab_size; iorder++){
1588 cpl_error_reset() ;
1589 check(extracted = cpl_vector_new(p_xs_3->BSIZE)) ;
1590 //xsh_msg(" Produce solution for orders %d",order_tab->list[iorder].absorder);
1591 starty = order_tab->list[iorder].starty;
1592 endy = order_tab->list[iorder].endy;
1593 if (starty == 0 && endy == 0){
1594 xsh_msg("Warning starty and endy equal zero, put endy to %d",ny-1);
1595 starty =0;
1596 endy = ny-1;
1597 }
1598 /*now loop over dispersion coordinate:
1599 - Compute x-disp coord from orderpos poly
1600 - flux from +/-h pix
1601 */
1602 for( iy=300; iy<=p_xs_3->BSIZE-100; iy=iy+1){
1603 check( dx = cpl_polynomial_eval_1d( order_tab->list[iorder].cenpoly,
1604 iy, NULL));
1605 /*HACK BELOW IS ONLY FOR UVB FRAMES*/
1606 iyuse=iy-200;
1607 //iyuse=iy;
1608
1609 /*HACK BELOW IS ONLY FOR VIS*/
1610 //dx=dx+50.0;
1611
1612 tot=0;
1613 for (ii=-h; ii<=h; ii++) {
1614 tot+=detec[p_xs_3->ASIZE*iyuse+(int)(dx+0.5)+ii];//[iyuse+p_xs_3->BSIZE*((int)(dx+0.5)+ii)];
1615 //printf("---- %d %d %lf %d %lf\n",iorder, iyuse, tot, (int)(dx+0.5)+ii, detec[p_xs_3->ASIZE*iyuse+(int)(dx+0.5)+ii]);
1616 }
1617 cpl_vector_set(extracted,iyuse,tot);
1618 }
1619 /*Compute peaks using Yves method for this order*/
1620 check(bright_lines[iorder]=xsh_model_refining_detect(extracted,3,5.0,0));
1621 //printf(" %d %lf %d \n",iorder,cpl_vector_get(bright_lines[iorder],0),cpl_vector_get_size(bright_lines[iorder]));
1622 //}
1623
1624 /*Subtract the running median so that the C of G is not affected by local background*/
1625 med=cpl_vector_get_median_const(extracted);
1626 stdev_ord=cpl_vector_get_stdev(extracted);
1627 ext_med_fil=cpl_vector_filter_median_create(extracted,50);
1628 cpl_vector_subtract(extracted,ext_med_fil);
1629
1630 for( iy=starty; iy<=endy; iy= iy+1){
1631 fprintf(ex1d_out,"%d %d %lf \n",
1632 iorder, iy, cpl_vector_get(extracted,iy));
1633 }
1634
1635/* filtered=cpl_vector_filter_median_create(extracted, 5); */
1636/* spec_clean = cpl_vector_duplicate(extracted) ; */
1637/* cpl_vector_subtract(spec_clean, filtered) ; */
1638/* cpl_vector_delete(filtered) ; */
1639
1640 /*for ( ii=starty; ii<=endy; ii= ii+1) {
1641 printf("1 %d %d %lf \n",ii,iorder,cpl_vector_get(extracted,ii));
1642 printf("2 %d %d %lf \n",ii,iorder,cpl_vector_get(spec_clean,ii));
1643 }*/
1644 cpl_vector_delete(ext_med_fil);
1645 // cpl_vector_delete(spec_clean);
1646
1647 /*loop over these peaks:
1648 - loop over w
1649 - get CofG for region containing core on left and window on right
1650 - get CofG for region containing window on left and core on right
1651 - check that both are within prec of listed peak centre
1652 - record the value of w to which the CofG is OK
1653 */
1654 //printf(" %d %lf %d \n",iorder,cpl_vector_get(bright_lines[iorder],0),cpl_vector_get_size(bright_lines[iorder]));
1655 for (ii=0; ii<cpl_vector_get_size(bright_lines[iorder]);ii++) {
1656 cent=cpl_vector_get(bright_lines[iorder],ii)+0.5-1.0;
1657 /*The -1.0 above is because Yves' function labels the first bin in the
1658 vector as 1, I label it 0*/
1659 rmax=0;
1660 w=wmin;
1661 while (w<200) {
1662 //prec=2.0;
1663 prec=(float)(w)/20.0;
1664 if (prec<0.5) {
1665 prec=0.5;
1666 }
1667 flux=0.0;
1668 cent2=0.0;
1669 flux_cor=0.0;
1670 cent2_cor=0.0;
1671 for (jj=(int)(cent)-wmin;jj<=(int)(cent)+w;jj++) {
1672 //printf("%d %d %d \n",iorder, ii, jj);
1673 if (jj>0 && jj<p_xs_3->BSIZE) {
1674 ex_cor=cpl_vector_get(extracted,jj);
1675 flux+=ex_cor;
1676 cent2+=ex_cor*((float)(jj)+0.5-cent);
1677 //printf("%d %d %d %lf %lf %lf %lf \n",iorder, ii, jj, flux, cent2, flux_cor, cent2_cor);
1678 }
1679 }
1680 cent2/=flux;
1681 if (w==wmin) {
1682 flux_keep=flux;
1683 }
1684 //printf("1 %d %d %lf %lf %lf %lf %d %d \n",iorder, ii, cent2, flux, med, cent, w, jj);
1685 cent2+=cent;
1686 if (fabs(cent2-cent)<prec) {
1687 rmax=w;
1688 }
1689 else {
1690 w=1000;
1691 }
1692 w+=1;
1693 }
1694 lmax=0;
1695 w=wmin;
1696 while (w<200) {
1697 //prec=2.0;
1698 prec=(float)(w)/20.0;
1699 if (prec<0.5) {
1700 prec=0.5;
1701 }
1702 flux=0.0;
1703 cent2=0.0;
1704 flux_cor=0.0;
1705 cent2_cor=0.0;
1706 for (jj=(int)(cent)-w;jj<=(int)(cent)+wmin;jj++) {
1707 if (jj>0 && jj<p_xs_3->BSIZE) {
1708 ex_cor=cpl_vector_get(extracted,jj);
1709 flux+=ex_cor;
1710 cent2+=ex_cor*((float)(jj)+0.5-cent);
1711 }
1712 }
1713 cent2/=flux;
1714 //printf("2 %d %d %lf %lf %lf %lf %d %d \n",iorder, ii, cent2, flux, med, cent, w, (int)(cent)-w);
1715 cent2+=cent;
1716 if (fabs(cent2-cent)<prec) {
1717 lmax=w;
1718 }
1719 else {
1720 w=1000;
1721 }
1722 w+=1;
1723 }
1724
1725 //morder_min=14;
1726
1727 //printf("%d %lf %d %d\n",iorder+morder_min,cent,lmax,rmax);
1728
1729 blaze_wav=2*(sin(-p_xs_3_config->nug))/((double)(iorder+morder_min)*p_xs_3_config->sg);
1730 lam_max=1000000.0*blaze_wav*((double)(iorder+morder_min)/((double)(iorder+morder_min)-0.5));
1731 lam_min=1000000.0*blaze_wav*((double)(iorder+morder_min)/(0.5+(double)(iorder+morder_min)));
1732 //printf("max %lf min%lf \n",lam_max,lam_min);
1733 if (lmax>=min_iso && rmax>=min_iso) {
1734 //printf("%d %lf \n",iorder+morder_min,cent);
1735 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
1736 check(lambda_nm=cpl_table_get_float(line_table, "WAVELENGTH",
1737 line_cnt, NULL));
1738 check(inten = cpl_table_get_int(line_table, "FLUX", line_cnt, NULL));
1739 //printf("wav %lf int %d \n",lambda_nm, inten);
1740 blaze_wav=2*(sin(-p_xs_3_config->nug))/((double)(iorder+morder_min)*p_xs_3_config->sg);
1741 lam_max=1000000.0*blaze_wav*((double)(iorder+morder_min)/((double)(iorder+morder_min)-0.5));
1742 lam_min=1000000.0*blaze_wav*((double)(iorder+morder_min)/(0.5+(double)(iorder+morder_min)));
1743
1744 /* check lambda is in FSR for this order and get its disp co-ord from model, x-disp co-ord from order_tab*/
1745 if (lambda_nm<lam_max && lambda_nm>lam_min) {
1746 check(xsh_model_get_xy(p_xs_3_config,instrument,lambda_nm,iorder+morder_min,0.0,&x,&y));
1747 check(dx=cpl_polynomial_eval_1d(order_tab->list[iorder].cenpoly,(int)(y), NULL));
1748 if (iorder+morder_min==30 && fabs(y-cent)<7.5) {
1749 printf("%lf %lf %lf %lf \n",dx,y, cent, y-cent);
1750 }
1751 if (fabs(y-cent)<1.5 && rmax!=0 && lmax!=0) {
1752 printf("%d %lf %lf %lf %d %d\n",iorder+morder_min, lambda_nm, dx,cent,lmax,rmax);
1753 //printf("0 %lf %lf 1 100.0 4 %lf %d\n",cpl_vector_get(bright_lines[morder_cnt-17],ii),dx,lambda_nm,morder_cnt);
1754 if (cent!=last_cent) {
1755 if (fabs(y-cent)<1.5 && rmax!=0 && lmax!=0) {
1756 fprintf(rec_out,"\n %d %lf %lf %lf %lf %d %lf ",iorder+morder_min, lambda_nm, dx,cent,flux_keep,min_iso, y-cent);
1757 }
1758 }
1759 else {
1760 fprintf(rec_out,"warning");
1761 }
1762 last_cent=cent;
1763 }
1764 }
1765 }
1766 }
1767 }
1768 cpl_vector_delete(extracted) ;
1769 }
1770
1771
1772/* /\* loop over lines*\/ */
1773/* for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) { */
1774/* check(lambda_nm=cpl_table_get_double(line_table, XSH_COL_SOURCE_WAVELENGTH, line_cnt, NULL)); */
1775/* check(inten = cpl_table_get_double(line_table, XSH_COL_SOURCE_INTEN, line_cnt, NULL)); */
1776/* /\*loop over orders and compute SFR for each order from model*\/ */
1777/* for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) { */
1778/* blaze_wav=2*(sin(-p_xs_3_config->nug))/(morder_cnt*p_xs_3_config->sg); */
1779/* lam_max=1000000.0*blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5)); */
1780/* lam_min=1000000.0*blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt))); */
1781/* /\* check lambda is in FSR for this order and get its disp co-ord from model, x-disp co-ord from order_tab*\/ */
1782/* if (lambda_nm<lam_max && lambda_nm>lam_min) { */
1783/* check(xsh_model_get_xy(p_xs_3_config,instrument,lambda_nm,morder_cnt,0.0,&x,&y)); */
1784/* check(dx=50.0+cpl_polynomial_eval_1d(order_tab->list[morder_cnt-17].cenpoly, */
1785/* (int)(y), NULL)); */
1786/* //printf("%d %lf %lf \n",morder_cnt,x,y); */
1787/* //printf("%lf %lf \n",dx,y); */
1788/* for (ii=0; ii<cpl_vector_get_size(bright_lines[morder_cnt-17]);ii++) { */
1789/* if (fabs(y+25-cpl_vector_get(bright_lines[morder_cnt-17],ii))<8.0) { */
1790/* //printf("0 %lf %lf 1 100.0 4 %lf %d\n",cpl_vector_get(bright_lines[morder_cnt-17],ii),dx,lambda_nm,morder_cnt); */
1791/* } */
1792/* } */
1793/* } */
1794/* } */
1795/* } */
1796 cpl_image_delete(im);
1797 xsh_order_list_free( &order_tab);
1798 XSH_TABLE_FREE( table);
1799 cpl_frame_delete(config_frame) ;
1801
1802 for (ii=0 ; ii<15; ii++) {
1803 if (bright_lines[ii] != NULL) cpl_vector_delete(bright_lines[ii]) ;
1804 }
1805 cpl_table_delete(line_table) ;
1806 fclose(rec_out);
1807 rec_out = NULL;
1808 fclose(ex1d_out);
1809 ex1d_out = NULL;
1810 }
1811 /*----------------------------------------------
1812 rec test
1813 --------------------------------------------*/
1814
1815 if (testfuncflag==0) {
1816
1817 fitsrow=0;
1818 tab_size = 150000;
1819 if (THEtabflag==1) {
1820 if (trace_out_flag==1 || BackMapFlag==1) {
1821 printf("Warning: THE table generation has been chosen, backgroud map and\nspectral profile generation will be ignored. \nRun again with:\n THEtab 0\nto get these products.\n\n");
1822 }
1823 THE_tab = cpl_table_new(tab_size);
1824 cpl_table_new_column(THE_tab, "Wavelength", CPL_TYPE_FLOAT) ;
1825 cpl_table_new_column(THE_tab, "Order", CPL_TYPE_INT) ;
1826 cpl_table_new_column(THE_tab, "Slit position", CPL_TYPE_FLOAT) ;
1827 cpl_table_new_column(THE_tab, "detector x", CPL_TYPE_DOUBLE) ;
1828 cpl_table_new_column(THE_tab, "detector y", CPL_TYPE_DOUBLE) ;
1829 }
1830
1831 ii=0;
1832 while (fscanf(qe_in,"%64lf %64lf \n",&dummy,&qe[ii]) !=EOF) {
1833 ii++;
1834 }
1835 fclose(qe_in);
1836 if (atmosflag==1) {
1837 ii=0;
1838 while (fscanf(tran_in,"%64lf %64lf \n",&dummy,&tran[ii]) !=EOF) {
1839 ii++;
1840 }
1841 }
1842 fclose(tran_in);
1843 ii=0;
1844 while (fscanf(dichro_in,"%64lf %64lf %64lf %64lf \n",
1845 &dummy,&temp[0],&temp[1],&temp[2]) !=EOF) {
1846 dichro[ii]=temp[dichro_col];
1847 ii++;
1848 }
1849 fclose(dichro_in);
1850
1851 config_frame = cpl_frame_new();
1852 cpl_frame_set_filename( config_frame, cfg_file);
1853 cpl_frame_set_tag( config_frame, tag);
1854
1855 check_msg(xsh_model_config_load_best(config_frame, p_xs_3),
1856 "Cannot load %s as a config",
1857 cfg_file);
1858
1859 xsh_model_binxy(p_xs_3, bin_X, bin_Y);
1860
1861 /*znse_ref is the array holding Sellmeier coefs for ZnSe refractive index
1862 at each temp. Currently hard coded to take 6 temperatures in [2][*] to
1863 [7][*], can be extended.
1864 First two rows ([0][*] to [1][*]) take the entries for the temperature
1865 bracketing the operation.
1866 Column [*][0] takes the temperature, the other columns,
1867 [*][1] to [*][6] take the
1868 Sellmeier coeffs
1869 */
1870 ref_ind=xsh_alloc2Darray(8,7);
1871
1872 /*-------------------------------------------------------------------------*/
1873
1874 es_x_init=p_xs_3->es_x;
1875 es_y_init=p_xs_3->es_y;
1876
1877
1878 if (p_xs_3->arm!=2) {
1879 xsh_ref_ind_read(p_xs_3->arm,ref_ind,p_xs_3->temper);
1880 }
1881 else {
1882 /*Strictly speaking there should be two ref_ind structures set up for
1883 the IR ZnSe prisms to allow for different temperatures*/
1884 xsh_ref_ind_read(p_xs_3->arm,ref_ind,p_xs_3->t_ir_p2);
1885 }
1886
1887 /*initalize the model. Inside the echelle_init the cfg file is
1888 read and the variables assigned to the xsh structure
1889 initialisation. Sets up those matrices that are not dependent upon
1890 wavelength */
1891
1892 //xsh_model_io_output_cfg_txt(p_xs_3);
1893 xsh_3_init(p_xs_3);
1894
1895 //printf("%d %d %lf %lf %lf %lf %lf \n",p_xs_3->BSIZE,p_xs_3->ASIZE,p_xs_3->chipxpix,p_xs_3->chipypix,p_xs_3->pix_X,p_xs_3->pix_Y,p_xs_3->pix);
1896
1897 p_xs_3->pix_X=p_xs_3->pix*(float)(bin_X);
1898 p_xs_3->pix_Y=p_xs_3->pix*(float)(bin_Y);
1899
1900 prof_scale_s=1.50; //angular distance corresponding to profile data
1901 prof_scale_w=1.50; //angular distance corresponding to profile data
1902 profin=fopen("single_0.5ph_pro.dat","r");
1903 if (proftype==0) {
1904 profin=fopen("full_slit_pro.dat","r");
1905 prof_scale_s=15.0; //angular distance corresponding to profile data
1906 }
1907 else if (proftype==2) {
1908 profin=fopen("single_0.5ph_pro.dat","r");
1909 prof_scale_s=1.50; //angular distance corresponding to profile data
1910 prof_scale_w=1.50; //angular distance corresponding to profile data
1911 }
1912 else if (proftype==3) {
1913 profin=fopen("multi_ph_pro.dat","r");
1914 prof_scale_s=15.0; //angular distance corresponding to profile data
1915 prof_scale_w=1.50; //angular distance corresponding to profile data
1916 }
1917 else if (proftype==4) {
1918 profin=fopen("multi7_ph_pro.dat","r");
1919 prof_scale_s=15.0; //angular distance corresponding to profile data
1920 prof_scale_w=1.5; //angular distance corresponding to profile data
1921 }
1922 else if (proftype==5) {
1923 profin=fopen("multi9_ph_HR_pro.dat","r");
1924 prof_scale_s=15.0; //angular distance corresponding to profile data
1925 prof_scale_w=1.5; //angular distance corresponding to profile data
1926 }
1927 else if (proftype==6) {
1928 profin=fopen("ifu.dat","r");
1929 prof_scale_s=15.0; //angular distance corresponding to profile data
1930 prof_scale_w=1.5; //angular distance corresponding to profile data
1931 }
1932 else if (proftype==7) {
1933 profin=fopen("custom_profile.dat","r");
1934 prof_scale_s=15.0; //angular distance corresponding to profile data
1935 prof_scale_w=1.5; //angular distance corresponding to profile data
1936 }
1937 if (proftype!=1) {
1938 ii=0;
1939 while (fscanf(profin,"%64lf %64lf\n",&spos,&profile[ii]) !=EOF) {
1940 //printf("%d %lf %lf \n",ii, spos,profile[ii]);
1941 ii++;
1942 }
1943 fclose(profin);
1944 profin=fopen("single_0.5ph_pro.dat","r");
1945 ii=0;
1946 while (fscanf(profin,"%64lf %64lf\n",&spos,&profile_w[ii]) !=EOF) {
1947 //printf("%d %lf %lf \n",ii, spos,profile[ii]);
1948 ii++;
1949 }
1950 fclose(profin);
1951 }
1952
1953 /* Read the input FITS lines list */
1954 check(lines_tab = cpl_table_load(wave_list_file, 1, 0)) ;
1955 check(lines_tot = cpl_table_get_nrow(lines_tab)) ;
1956
1957 trace_out=fopen("trace_r.dat","w");
1958 trace_out1=fopen("trace1.dat","w");
1959 trace_out2=fopen("trace2.dat","w");
1960 trace_out3=fopen("trace3.dat","w");
1961 line_no=0;
1962 /*Calculate the order of the given wavelength */
1963 // morder=(int)(0.5+(sin(p_xs_3->mug+inout/2.0)+sin(p_xs_3->mug-inout/2.0))/((p_xs_3->sg)*lambda));
1964 /* while there is a line in the file read it and call the stis model routine*/
1965 /*without order in input*/
1966 if (THEtabflag==1) {
1967 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
1968 lambda_nm=cpl_table_get_float( lines_tab,
1970 line_cnt, NULL);
1971 inten = cpl_table_get_int( lines_tab, XSH_ARCLIST_TABLE_COLNAME_FLUX,
1972 line_cnt, NULL);
1973 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
1974 // for (morder_cnt=morder-morder_it; morder_cnt<morder+morder_it; morder_cnt+=1) {
1975 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
1976 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
1977 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
1978 //printf("blaze wav=%lf %lf %lf\n",blaze_wav,lam_min,lam_max);
1979 lambda=lambda_nm*1e-6; //lambda in mm
1980 if (lambda>lam_min-blaze_pad && lambda<lam_max+blaze_pad) {
1981 spos_int=0;
1982 for (spos=-sep_ph*(num_ph-1.0)/2.0; spos<=0.01+sep_ph*(num_ph-1.0)/2.0; spos+=sep_ph) {
1983 p_xs_3->es_y_tot=spos+p_xs_3->es_y;
1984 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
1985 xsh_3_detpix(p_xs_3);
1986 //printf("%lf %d %lf %lf %lf %d\n",lambda_nm, morder_cnt, p_xs_3->es_y_tot/p_xs_3->slit_scale, p_xs_3->xdet, p_xs_3->ydet,p_xs_3->chippix[0]);
1987 if (p_xs_3->chippix[0]==1) {
1988 //printf("%lf %lf %lf\n",p_xs_3->es_y_tot, p_xs_3->slit_scale, p_xs_3->es_y_tot/p_xs_3->slit_scale);
1989 cpl_table_set_float(THE_tab, "Wavelength", fitsrow, lambda_nm) ;
1990 cpl_table_set_int(THE_tab, "Order", fitsrow, morder_cnt) ;
1991 cpl_table_set_float(THE_tab, "Slit position",
1992 fitsrow, p_xs_3->es_y/p_xs_3->slit_scale);
1993 cpl_table_set_double(THE_tab, "detector x",
1994 fitsrow, p_xs_3->xpospix);
1995 cpl_table_set_double(THE_tab, "detector y",
1996 fitsrow, p_xs_3->ypospix);
1997 if (THEtxtflag==1) {
1998 printf("%lf %d %lf %lf %lf %lf \n",
1999 lambda_nm,morder_cnt,p_xs_3->es_y/p_xs_3->slit_scale,
2000 p_xs_3->xpospix,p_xs_3->ypospix,inten);
2001 }
2002 /*if (inten>1000.0) {
2003 printf("1 %lf %lf %d %lf %d %lf %d\n", p_xs_3->ypospix, p_xs_3->xpospix, p_xs_3->arm+1, inten, spos_int, lambda*1000000.0, morder_cnt);
2004 }*/
2005 fitsrow+=1;
2006 }
2007 spos_int+=1;
2008 }
2009 }
2010 }
2011 }
2012 }
2013 else if (det_slitflag==1) {
2014 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
2015 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
2016 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
2017 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
2018 for (lambda=lam_min-blaze_pad; lambda<=lam_max+blaze_pad;
2019 lambda+=(lam_max-lam_min)/100.0) {
2020 lambda_nm=lambda*1000000.0;
2021 p_xs_3->es_y_tot=p_xs_3->es_y-p_xs_3->es_s/2.0;
2022 p_xs_3->es_x=0.0;
2023 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2024 xsh_3_detpix(p_xs_3);
2025 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
2026 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
2027 p_xs_3->chippix[2]>=10 &&
2028 p_xs_3->chippix[2]<p_xs_3->BSIZE-10) {
2029 x1=p_xs_3->xpospix;
2030 y_1=p_xs_3->ypospix;
2031 }
2032 else {
2033 x1=0.0;
2034 y_1=0.0;
2035 }
2036 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->es_s/2.0;
2037 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2038 xsh_3_detpix(p_xs_3);
2039 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
2040 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
2041 p_xs_3->chippix[2]>=10 &&
2042 p_xs_3->chippix[2]<p_xs_3->BSIZE-10 &&
2043 x1!=0.0 && y_1!=0.0) {
2044 yslit2xdet=(p_xs_3->xpospix-x1)/p_xs_3->es_s;
2045 yslit2ydet=(p_xs_3->ypospix-y_1)/p_xs_3->es_s;
2046 }
2047 else {
2048 yslit2xdet=9999.0;
2049 yslit2ydet=9999.0;
2050 }
2051 p_xs_3->es_x=-p_xs_3->es_w/2.0;
2052 p_xs_3->es_y_tot=p_xs_3->es_y;
2053 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2054 xsh_3_detpix(p_xs_3);
2055 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
2056 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
2057 p_xs_3->chippix[2]>=10 &&
2058 p_xs_3->chippix[2]<p_xs_3->BSIZE-10) {
2059 x1=p_xs_3->xpospix;
2060 y_1=p_xs_3->ypospix;
2061 }
2062 else {
2063 x1=0.0;
2064 y_1=0.0;
2065 }
2066 p_xs_3->es_x=p_xs_3->es_w/2.0;
2067 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2068 xsh_3_detpix(p_xs_3);
2069 if (p_xs_3->chippix[0]==1 && p_xs_3->chippix[1]>=10 &&
2070 p_xs_3->chippix[1]<p_xs_3->ASIZE-10 &&
2071 p_xs_3->chippix[2]>=10 &&
2072 p_xs_3->chippix[2]<p_xs_3->BSIZE-10 &&
2073 x1!=0.0 && y_1!=0.0) {
2074 xslit2xdet=(p_xs_3->xpospix-x1)/p_xs_3->es_w;
2075 xslit2ydet=(p_xs_3->ypospix-y_1)/p_xs_3->es_w;
2076 }
2077 else {
2078 xslit2xdet=9999.0;
2079 xslit2ydet=9999.0;
2080 }
2081 p_xs_3->es_x=0.0;
2082 p_xs_3->es_y_tot=p_xs_3->es_y;
2083 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2084 xsh_3_detpix(p_xs_3);
2085 if (xslit2xdet!=9999.0 && xslit2ydet!=9999.0 &&
2086 yslit2xdet!=9999.0 && yslit2ydet!=9999.0) {
2087 ydet2xslit=yslit2xdet/(xslit2ydet*yslit2xdet-xslit2xdet*yslit2ydet);
2088 ydet2yslit=-xslit2xdet/(xslit2ydet*yslit2xdet-xslit2xdet*yslit2ydet);
2089 xdet2xslit=yslit2ydet/(xslit2xdet*yslit2ydet-xslit2ydet*yslit2xdet);
2090 xdet2yslit=-xslit2ydet/(xslit2xdet*yslit2ydet-xslit2ydet*yslit2xdet);
2091 printf("%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf \n",
2092 morder_cnt, lambda_nm,p_xs_3->xpospix,p_xs_3->ypospix,
2093 xslit2ydet, yslit2xdet, xslit2xdet, yslit2ydet, ydet2xslit,
2094 ydet2yslit,xdet2xslit,xdet2yslit);
2095 }
2096 }
2097 }
2098 exit(1);
2099 }
2100 else {
2101 ccd=xsh_alloc2Darray(p_xs_3->BSIZE, p_xs_3->ASIZE);
2102 for (ii=0;ii<p_xs_3->BSIZE;ii++) {
2103 for (jj=0;jj<p_xs_3->ASIZE;jj++) {
2104 ccd[ii][jj]=0.0;
2105 }
2106 }
2107 for (line_cnt=0 ; line_cnt<lines_tot ; line_cnt++) {
2108 lambda_nm=cpl_table_get_float(lines_tab,
2110 line_cnt, NULL);
2111 inten = cpl_table_get_int(lines_tab,
2112 XSH_ARCLIST_TABLE_COLNAME_FLUX, line_cnt, NULL);
2113 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
2114 //for (morder_cnt=21;morder_cnt<22;morder_cnt+=1) {
2115 // for (morder_cnt=morder-morder_it; morder_cnt<morder+morder_it; morder_cnt+=1) {
2116 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
2117 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
2118 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
2119 //printf("O%d blaze wav=%lf %lf %lf\n",morder_cnt,blaze_wav,lam_min,lam_max);
2120 lambda=lambda_nm*1e-6; //lambda in mm
2121 if (lambda>lam_min-blaze_pad && lambda<lam_max+blaze_pad) {
2122 p_xs_3->es_y_tot=p_xs_3->es_y;
2123 p_xs_3->es_x=0.0;
2124 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2125 xsh_3_detpix(p_xs_3);
2126 p_dif=(sin(p_xs_3->grat_alpha-(-p_xs_3->nug))+
2127 sin(p_xs_3->grat_beta-(-p_xs_3->nug)));
2128 if (p_xs_3->grat_alpha>p_xs_3->grat_beta) {
2129 sinc_arg=(M_PI/(lambda*p_xs_3->sg))*
2130 (cos(p_xs_3->grat_alpha)/
2131 cos(p_xs_3->grat_alpha-(-p_xs_3->nug)))*p_dif;
2132 blaze=((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
2133 }
2134 else {
2135 sinc_arg=(M_PI/(lambda*p_xs_3->sg))*
2136 (cos(p_xs_3->grat_beta)/
2137 cos(p_xs_3->grat_alpha-(-p_xs_3->nug)))*p_dif;
2138 blaze=(cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha))*
2139 (cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha))*
2140 ((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
2141 }
2142 if (p_xs_3->chippix[0]==1) {
2143 //xsh_model_io_output_cfg_txt(p_xs_3);
2144 if (p_xs_3->arm==0) {
2145 if (morder_cnt-arm_ef_ord0+1>0 && ((int)(lambda_nm)-arm_lam_min)/arm_lam_fac>=0) {
2146 ef_corr_down=ef[morder_cnt-arm_ef_ord0+1][((int)(lambda_nm)-arm_lam_min)/arm_lam_fac];
2147 ef_corr_up=ef[morder_cnt-arm_ef_ord0+1][(((int)(lambda_nm)-arm_lam_min)/arm_lam_fac)+1];
2148 substep=(lambda_nm/(double)(arm_lam_fac)-(double)((int)(lambda_nm/(double)(arm_lam_fac))));
2149 ef_corr=ef_corr_down+substep*(ef_corr_up-ef_corr_down);
2150 }
2151 else {
2152 ef_corr=blaze;
2153 }
2154 }
2155 else ef_corr=blaze;
2156 if ((int)(lambda_nm)-qe_wv_min>=0) {
2157 qe_corr_down=qe[((int)(lambda_nm)-qe_wv_min)/qe_wv_step];
2158 qe_corr_up=qe[(((int)(lambda_nm)-qe_wv_min)/qe_wv_step)+1];
2159 substep=(lambda_nm/(double)(qe_wv_step))-(double)((int)(lambda_nm/(double)(qe_wv_step)));
2160 qe_corr=(qe_corr_down+substep*(qe_corr_up-qe_corr_down))/100.0;
2161 }
2162 else {
2163 qe_corr=qe[0]/100.0;
2164 }
2165 dichro_corr_down=dichro[(int)(lambda_nm)-200];
2166 dichro_corr_up=dichro[((int)(lambda_nm)-200)+1];
2167 substep=(lambda_nm)-(double)((int)(lambda_nm));
2168 dichro_corr=(dichro_corr_down+substep*(dichro_corr_up-dichro_corr_down))/100.0;
2169 //printf("%lf %lf \n",dichro_corr,lambda_nm);
2170 if (atmosflag==1) {
2171 if (lambda_nm>tran_wv_min && lambda_nm<tran_wv_max) {
2172
2173 tran_corr_down=tran[(int)((lambda_nm-tran_wv_min)/tran_wv_step)];
2174 tran_corr_up=tran[(int)((lambda_nm-tran_wv_min)/tran_wv_step)+1];
2175 substep=((lambda_nm-tran_wv_min)/tran_wv_step)-(double)((int)((lambda_nm-tran_wv_min)/tran_wv_step));
2176 at_tran=tran_corr_down+substep*(tran_corr_up-tran_corr_down);
2177 //printf("%lf %lf %lf \n",at_tran,lambda_nm, substep);
2178 }
2179 else {
2180 at_tran=1.0;
2181 }
2182 }
2183/* ef_corr=1.0; */
2184/* qe_corr=1.0; */
2185/* at_tran=1.0; */
2186/* dichro_corr=1.0; */
2187 if (flux_scale*at_tran*ef_corr*qe_corr*dichro_corr<1.0) {
2188 phot_in=(int)(inten);
2189 pfac=flux_scale*at_tran*ef_corr*qe_corr*dichro_corr;
2190 }
2191 else {
2192 phot_in=(int)(inten*flux_scale);
2193 pfac=at_tran*ef_corr*qe_corr*dichro_corr;
2194 }
2195 pfac2=at_tran*ef_corr*qe_corr*dichro_corr;
2196 if (phot_in==0) {
2197 prob=rand();
2198 if (prob/2147483648.0<at_tran*ef_corr*qe_corr*dichro_corr*inten*flux_scale) {
2199 tot_phot=1;
2200 }
2201 else {
2202 tot_phot=0;
2203 }
2204 }
2205 else if (phot_in<10000){
2206 tot_phot=0.0;
2207 for (ii=0;ii<phot_in;ii++) {
2208 prob=rand();
2209 if (prob/2147483648.0<pfac) {
2210 tot_phot+=1.0;
2211 }
2212 }
2213 }
2214 else {
2215 tot_phot=(double)(phot_in)*pfac;
2216 }
2217
2218 /*OK if tot below is zero, this is dealt with below where phot_in > flux_scale*/
2219 printf("m %d lam %lf tot %g inten %lf \n", morder_cnt,lambda_nm,tot_phot, inten);
2220 //printf("%d %lf %lf %lf\n", morder_cnt,lambda_nm, (double)(phot_in)*pfac/flux_scale,ef_corr);
2221 nphot=1.0;
2222 phot_res=1000000.0;
2223 if (inten*pfac>phot_res/flux_scale) {
2224 nphot=(inten/phot_res)*flux_scale*pfac;
2225 tot_phot=phot_res;
2226 }
2227 //printf("m %d lam %lf tot %lf inten %lf pfac %lf phot_in %d fs %lf at %lf ef %lf qe %lf dc %lf nphot %lf \n", morder_cnt,lambda_nm,tot_phot, inten, pfac, phot_in,flux_scale,at_tran,ef_corr,qe_corr,dichro_corr,nphot);
2228 /*iterate over photons in spectral line*/
2229 photons=1;
2230 while (photons<=(int)(tot_phot)) {
2231 //lines[0][line_no]=lambda;
2232 //lines[2][line_no]=inten*10000.0;
2233 /*Calculate the order of the given wavelength */
2234 // morder=(int)(0.5+(sin(p_xs_3->mug+inout/2.0)+sin(p_xs_3->mug-inout/2.0))/((p_xs_3->sg)*lambda));
2235 //lines[1][line_no]=morder_cnt;
2236 /* Send to the model the wavelength, order, temperature,
2237 sellmeier refractive index coefs and a pointer to the structure
2238 Get back the chip no. and x,y position)*/
2239 //for (spos=-(num_ph-1.0)*p_xs_3->es_s/(num_ph/2.0); spos<=0.01+(num_ph-1.0)*p_xs_3->es_s/(num_ph/2.0); spos+=p_xs_3->es_s/num_ph) {
2240 //for (spos=-sep_ph*(num_ph-1.0)/2.0; spos<=0.01+sep_ph*(num_ph-1.0)/2.0; spos+=sep_ph) {
2241 /*call random number generator twice and generate two
2242 gaussian distributions with mean=0, sigma=1.0 => FWHM=2.35*/
2243 prob=rand();
2244 prob=prob/2147483648.0;
2245 prob2=rand();
2246 prob2=prob2/2147483648.0;
2247 prob3=rand();
2248 prob3=prob3/2147483648.0;
2249 prob4=rand();
2250 prob4=prob4/2147483648.0;
2251 gauss1=sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
2252 //printf("%lf %lf %lf %lf %lf %lf %lf \n",prob,prob2,prob3, prob4,M_PI,gauss1, sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2));
2253 gauss2=sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
2254 flag=0;
2255 if (proftype!=1) {
2256 if (proftype==0 || proftype==6) {
2257 p_xs_3->es_x=(p_xs_3->es_w)*(prob3-0.5)+(p_xs_3->es_w/100.0)*gauss1;
2258 //printf("%lf %lf %lf \n",p_xs_3->es_x,p_xs_3->es_w,gauss1);
2259 }
2260 else {
2261 p_xs_3->es_x=(prob3-0.5)*p_xs_3->slit_scale*prof_scale_w;
2262 if (prob2>profile_w[(int)(prob3*301.0)] || p_xs_3->es_x<-p_xs_3->es_w/2.0 || p_xs_3->es_x>p_xs_3->es_w/2.0) {
2263 flag=1;
2264 }
2265 }
2266 //printf("%lf %lf %lf %lf \n",p_xs_3->es_x,(p_xs_3->es_w/100.0)*gauss1,(p_xs_3->es_w/2.0)*(prob3-0.5),gauss1);
2267 if (proftype==5) {
2268 prof_data_size=6001;
2269 }
2270 else {
2271 prof_data_size=301;
2272 }
2273 p_xs_3->es_y_tot=(prob4-0.5)*p_xs_3->slit_scale*prof_scale_s;
2274 if (prob>profile[(int)(prob4*prof_data_size)]) {
2275 flag=1;
2276 }
2277 //printf("%d %lf %lf %d %lf %lf %d\n",(int)(prob4*301.0),prob,profile[(int)(prob4*301.0)],(int)(prob3*301.0),prob2,profile[(int)(prob3*301.0)],flag);
2278 }
2279 else if (proftype==1) {
2280 /*otherwise make the x dist. appropriate to signal, i.e. seeing disk: 1"=0.255mm*/
2281 p_xs_3->es_x=gauss1*0.255/2.35;
2282 p_xs_3->es_y_tot=gauss2*0.255/2.35;
2283 /*Set flag if photon is not on slit (pos was drawn from a
2284 gaussian, it could be several sigma from the centre)*/
2285 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) {
2286 flag=1;
2287 }
2288
2289 }
2290 /* Send to the model the wavelength, order, temperature,
2291 sellmeier refractive index coefs and a pointer to the structure*/
2292 if (flag!=1) {
2293 photons+=1;
2294 p_xs_3->es_y_tot+=es_y_init;
2295 p_xs_3->es_x=es_x_init+p_xs_3->es_x;
2296 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2297 xsh_3_detpix(p_xs_3);
2298 /*ccd is an array with one element for each pixel */
2299 /*add one to value at x,y co-ord*/
2301 if (p_xs_3->chippix[0]==1) {
2302 ccd[(int)(p_xs_3->ypospix-0.5)][(int)(p_xs_3->xpospix-0.5)]+=nphot;
2303 //ccd[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]=pfac2;
2304 }
2305 }
2306 }
2307 //}
2308 }
2309 }
2310 }
2311 /*if not background increment the line count (only signal is recorded)*/
2312 if (bgflag==0 && continuum==0) line_no+=1;
2313 }
2314 }
2315 cpl_table_delete(lines_tab) ;
2316 total_lines=line_no;
2317 if ((xsh_free2Darray(ef,n_order))!=0) {
2318 fclose(trace_out);
2319 fclose(trace_out1);
2320 fclose(trace_out2);
2321 fclose(trace_out3);
2322 cpl_msg_error(__func__, "Cannot free 2D array ef");
2323 return -1;
2324 }
2325
2326 /*Below pixylast, chipdistcurrent, chippix[2] and ypospix all refer to the
2327 dispersion direction*/
2328 p_xs_3->es_x=es_x_init;
2329 p_xs_3->es_y_tot=es_y_init;
2330 lam_range=0.00015;
2331 if (BackMapFlag==1 && THEtabflag!=1) {
2332 if (p_xs_3->arm==2) {
2333 SIZE=p_xs_3->ASIZE;
2334 }
2335 else {
2336 SIZE=p_xs_3->BSIZE;
2337 }
2338 if ((trace_lam=xsh_alloc1Darray(SIZE))==NULL){
2339 cpl_msg_error(__func__, "Cannot allocate 2D array");
2340 return -1;
2341 }
2342 if ((trace_flux=xsh_alloc1Darray(SIZE))==NULL){
2343 cpl_msg_error(__func__, "Cannot allocate 2D array");
2344 return -1;
2345 }
2346 if ((trace_flux1=xsh_alloc1Darray(SIZE))==NULL){
2347 cpl_msg_error(__func__, "Cannot allocate 2D array");
2348 return -1;
2349 }
2350 if ((trace_flux2=xsh_alloc1Darray(SIZE))==NULL){
2351 cpl_msg_error(__func__, "Cannot allocate 2D array");
2352 return -1;
2353 }
2354 if ((trace_flux3=xsh_alloc1Darray(SIZE))==NULL){
2355 cpl_msg_error(__func__, "Cannot allocate 2D array");
2356 return -1;
2357 }
2358 if ((trace_xdisp=xsh_alloc1Darray(SIZE))==NULL){
2359 cpl_msg_error(__func__, "Cannot allocate 2D array");
2360 return -1;
2361 }
2362 if ((trace_mm=xsh_alloc1Darray_INT(SIZE))==NULL){
2363 cpl_msg_error(__func__, "Cannot allocate 2D array");
2364 return -1;
2365 }
2366 ccd_wav=xsh_alloc2Darray(p_xs_3->BSIZE, p_xs_3->ASIZE);
2367 for (ii=0;ii<p_xs_3->BSIZE;ii++) {
2368 for (jj=0;jj<p_xs_3->ASIZE;jj++) {
2369 ccd_wav[ii][jj]=0.0;
2370 }
2371 }
2372 for (morder_cnt=morder_min; morder_cnt<=morder_max; morder_cnt+=1) {
2373 for (ii=0;ii<SIZE;ii+=1) {
2374 trace_mm[ii]=0;
2375 trace_lam[ii]=0.0;
2376 trace_xdisp[ii]=0.0;
2377 trace_flux[ii]=0.0;
2378 trace_flux1[ii]=0.0;
2379 trace_flux2[ii]=0.0;
2380 trace_flux3[ii]=0.0;
2381 }
2382 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
2383 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
2384 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
2385 printf("blaze wav=%lf lam_max=%lf lam_min=%lf \n",blaze_wav, lam_max, lam_min);
2386 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) {
2387 pixylast=0.0;
2388 pixxlast=0.0;
2389 pixwavlast=1000000*(lam_min-blaze_pad);
2390 if (p_xs_3->arm==2) {
2391 chipdist_current=2047;
2392 }
2393 else {
2394 chipdist_current=1;
2395 }
2396 for (lambda=lam_min-blaze_pad; lambda<=lam_max+blaze_pad; lambda+=(lam_max-lam_min)/10000.0) {
2397 lambda_nm=lambda*1000000.0;
2398 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2399 xsh_3_detpix(p_xs_3);
2400 if (p_xs_3->chippix[0]==1) {
2401 if (p_xs_3->chippix[1]>=1 &&
2402 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
2403 p_xs_3->chippix[2]>=1 &&
2404 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
2405 if (p_xs_3->arm==2) {
2406 if (p_xs_3->xpospix<(double)(chipdist_current)-0.5) {
2407 ccd_wav[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]=
2408 (pixwavlast+(lambda_nm-pixwavlast)*
2409 ((double)(chipdist_current)-0.5-pixxlast)/
2410 (p_xs_3->xpospix-pixxlast));
2411 chipdist_current=p_xs_3->chippix[1]-1;
2412 //printf("%lf %lf %d %d %d %lf %d %d %lf %lf %lf \n",p_xs_3->xpospix,p_xs_3->ypospix,p_xs_3->chippix[0],p_xs_3->chippix[1],p_xs_3->chippix[2],lambda_nm,morder_cnt, chipdist_current, pixxlast, pixwavlast,ccd_wav[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]);
2413 }
2414 }
2415 else {
2416 if (p_xs_3->ypospix>(double)(chipdist_current)-0.5) {
2417 ccd_wav[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]=
2418 pixwavlast+(lambda_nm-pixwavlast)*
2419 ((double)(chipdist_current)-0.5-pixylast)/
2420 (p_xs_3->ypospix-pixylast);
2421 chipdist_current=p_xs_3->chippix[2]+1;
2422 if (p_xs_3->chippix[2]-1==0) {
2423 //printf("%lf %lf %d %d %d %lf %d %d %lf %lf %lf %lf \n",p_xs_3->xpospix,p_xs_3->ypospix,p_xs_3->chippix[0],p_xs_3->chippix[1],p_xs_3->chippix[2],lambda_nm,morder_cnt, chipdist_current, pixxlast, pixylast, pixwavlast,ccd_wav[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1]);
2424 }
2425 }
2426 }
2427 }
2428 }
2429 pixxlast=p_xs_3->xpospix;
2430 pixylast=p_xs_3->ypospix;
2431 pixwavlast=lambda_nm;
2432 }
2433 //}
2434 if (p_xs_3->arm==2) {
2435 chipdist_current=3000;
2436 }
2437 else {
2438 chipdist_current=0;
2439 }
2440 }
2441 pixxlast=0.0;
2442 pixylast=0.0;
2443 pixwavlast=1000000*(lam_min-blaze_pad);
2444 for (lambda=lam_min-blaze_pad; lambda<=lam_max+blaze_pad;
2445 lambda+=(lam_max-lam_min)/10000.0) {
2446 lambda_nm=1000000.0*lambda;
2447 p_xs_3->es_y_tot=p_xs_3->es_y;
2448 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
2449 xsh_3_detpix(p_xs_3);
2450 if (p_xs_3->chippix[0]==1) {
2451 if (p_xs_3->chippix[1]>=1 &&
2452 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
2453 p_xs_3->chippix[2]>=1 &&
2454 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
2455 if (p_xs_3->arm==2){
2456 if (p_xs_3->xpospix<(double)(chipdist_current)-0.5) {
2457 trace_lam[p_xs_3->chippix[1]-1]=pixwavlast+(lambda_nm-pixwavlast)*
2458 ((double)(chipdist_current)-0.5-pixxlast)/(p_xs_3->xpospix-pixxlast);
2459 trace_xdisp[p_xs_3->chippix[1]-1]=pixylast+(p_xs_3->ypospix-pixylast)*
2460 ((double)(chipdist_current)-0.5-pixxlast)/(p_xs_3->xpospix-pixxlast);
2461 trace_flux[p_xs_3->chippix[1]-1]=0.0;
2462 trace_flux1[p_xs_3->chippix[1]-1]=0.0;
2463 trace_flux2[p_xs_3->chippix[1]-1]=0.0;
2464 trace_flux3[p_xs_3->chippix[1]-1]=0.0;
2465 for (ii=-half_prof_wid;ii<half_prof_wid+1;ii+=1) {
2466 if (p_xs_3->chippix[2]-1+ii>=0 &&
2467 p_xs_3->chippix[2]-1+ii<p_xs_3->BSIZE) {
2468 ccd_sig=ccd[p_xs_3->chippix[2]-1+ii][p_xs_3->chippix[1]-1];
2469 if (ccd_sig>65535 && ccd_sig<=131072) {
2470 ccd_sig=65535;
2471 }
2472 else if (ccd_sig>131072) {
2473 ccd_sig=0;
2474 }
2475 trace_flux[p_xs_3->chippix[1]-1]+=ccd_sig;
2476 prob=rand();
2477 prob=prob/2147483648.0;
2478 prob2=rand();
2479 prob2=prob2/2147483648.0;
2480 prob3=rand();
2481 prob3=prob/2147483648.0;
2482 prob4=rand();
2483 prob4=prob2/2147483648.0;
2484 gauss1=bg_sig*sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
2485 gauss2=bg_sig*sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
2486 gauss3=bg_sig*sqrt(-2.0*log(1-prob3))*sin(2*M_PI*prob4);
2487 trace_flux1[p_xs_3->chippix[1]-1]+=ccd_sig+gauss1;
2488 trace_flux2[p_xs_3->chippix[1]-1]+=ccd_sig+gauss2;
2489 trace_flux3[p_xs_3->chippix[1]-1]+=ccd_sig+gauss3;
2490 }
2491 }
2492 trace_mm[p_xs_3->chippix[1]-1]=morder_cnt;
2493 //printf("%d %lf %lf %lf %lf %lf %lf %lf\n", morder_cnt, lambda_nm, p_xs_3->xpospix, p_xs_3->ypospix, ccd[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1], pixxlast, pixylast, pixwavlast);
2494 chipdist_current=p_xs_3->chippix[1]+1;
2495 }
2496 }
2497 else {
2498 if (p_xs_3->ypospix>(double)(chipdist_current)-0.5) {
2499 trace_lam[p_xs_3->chippix[2]-1]=
2500 pixwavlast+(lambda_nm-pixwavlast)*
2501 ((double)(chipdist_current)-0.5-pixylast)/
2502 (p_xs_3->ypospix-pixylast);
2503 trace_xdisp[p_xs_3->chippix[2]-1]=
2504 pixxlast+(p_xs_3->xpospix-pixxlast)*
2505 ((double)(chipdist_current)-0.5-pixylast)/
2506 (p_xs_3->ypospix-pixylast);
2507 trace_flux[p_xs_3->chippix[2]-1]=0.0;
2508 trace_flux1[p_xs_3->chippix[1]-1]=0.0;
2509 trace_flux2[p_xs_3->chippix[1]-1]=0.0;
2510 trace_flux3[p_xs_3->chippix[1]-1]=0.0;
2511 for (ii=-half_prof_wid;ii<half_prof_wid+1;ii+=1) {
2512 if (p_xs_3->chippix[1]-1+ii>=0 &&
2513 p_xs_3->chippix[1]-1+ii<p_xs_3->ASIZE) {
2514 ccd_sig=ccd[p_xs_3->chippix[2]-1][p_xs_3->chippix[1]-1+ii];
2515 trace_flux[p_xs_3->chippix[2]-1]+=ccd_sig;
2516 if (ccd_sig>65535 && ccd_sig<=131072) {
2517 ccd_sig=65535;
2518 }
2519 else if (ccd_sig>131072) {
2520 ccd_sig=0;
2521 }
2522 prob=rand();
2523 prob=prob/2147483648.0;
2524 prob2=rand();
2525 prob2=prob2/2147483648.0;
2526 prob3=rand();
2527 prob3=prob3/2147483648.0;
2528 prob4=rand();
2529 prob4=prob4/2147483648.0;
2530 gauss1=bg_sig*sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
2531 gauss2=bg_sig*sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
2532 gauss3=bg_sig*sqrt(-2.0*log(1-prob3))*sin(2*M_PI*prob4);
2533 trace_flux1[p_xs_3->chippix[2]-1]+=ccd_sig+gauss1;
2534 trace_flux2[p_xs_3->chippix[2]-1]+=ccd_sig+gauss2;
2535 trace_flux3[p_xs_3->chippix[2]-1]+=ccd_sig+gauss3;
2536 }
2537 }
2538 trace_mm[p_xs_3->chippix[2]-1]=morder_cnt;
2539 chipdist_current=p_xs_3->chippix[2]+1;
2540 }
2541 }
2542 pixxlast=p_xs_3->xpospix;
2543 pixylast=p_xs_3->ypospix;
2544 }
2545 }
2546 pixwavlast=lambda_nm;
2547 }
2548 for (ii=1;ii<SIZE;ii+=1) {
2549 fprintf(trace_out,"%d 0 %lf %lf %d %lf %lf\n", trace_mm[ii], trace_lam[ii], trace_xdisp[ii], ii, trace_flux[ii], trace_flux[ii]/(trace_lam[ii]-trace_lam[ii-1]));
2550 fprintf(trace_out1,"%d %lf o%d\n", ii, trace_flux1[ii], trace_mm[ii]);
2551 fprintf(trace_out2,"%d %lf o%d\n", ii, trace_flux2[ii], trace_mm[ii]);
2552 fprintf(trace_out3,"%d %lf o%d\n", ii, trace_flux3[ii], trace_mm[ii]);
2553 //printf("%d 1 %lf %lf %d %lf\n", trace_mm[ii], trace_lam[ii], trace_xdisp[ii], ii, trace_flux[ii]);
2554 }
2555 }
2556 cpl_free(trace_lam);
2557 cpl_free(trace_xdisp);
2558 cpl_free(trace_flux);
2559 cpl_free(trace_flux1);
2560 cpl_free(trace_flux2);
2561 cpl_free(trace_flux3);
2562 cpl_free(trace_mm);
2563 }
2564 if ((xsh_free2Darray(ref_ind,8))!=0) {
2565 cpl_msg_error(__func__, "Cannot free 2D array ref_ind");
2566 return -1;
2567 }
2568 fclose(trace_out);
2569 fclose(trace_out1);
2570 fclose(trace_out2);
2571 fclose(trace_out3);
2572
2573 if (THEtabflag==1) {
2574 /* Set the file name */
2575 sprintf(name_o, "THE.fits") ;
2576 cpl_msg_info(__func__, "Writing %s" , name_o) ;
2577
2578 /* Get FITS header from reference file */
2579 THEplist = cpl_propertylist_new();
2580 cpl_propertylist_append_string(THEplist, "INSTRUME", "XSHOOTER") ;
2581
2582 /* Create product frame */
2583 THEproduct_frame = cpl_frame_new() ;
2584 cpl_frame_set_filename(THEproduct_frame, name_o) ;
2585 cpl_frame_set_tag(THEproduct_frame, "XSHOOTER_THE") ;
2586 cpl_frame_set_type(THEproduct_frame, CPL_FRAME_TYPE_TABLE);
2587 cpl_frame_set_group(THEproduct_frame, CPL_FRAME_GROUP_PRODUCT);
2588 cpl_frame_set_level(THEproduct_frame, CPL_FRAME_LEVEL_FINAL);
2589 cpl_table_set_size(THE_tab,fitsrow);
2590 cpl_msg_info(__func__, "Save the table") ;
2591 if (cpl_table_save(THE_tab, NULL, THEplist, name_o,
2592 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
2593 cpl_msg_error(__func__, "Cannot write the table") ;
2594 }
2595
2596 cpl_frame_delete(THEproduct_frame) ;
2597 cpl_table_delete(THE_tab) ;
2598 cpl_propertylist_delete(THEplist) ;
2599 }
2600 else {
2601 naxes[0]=p_xs_3->ASIZE;
2602 naxes[1]=p_xs_3->BSIZE;
2603
2604 /*readout noise*/
2605 if (readout_flag==1) {
2606 rn=2.0;
2607 for (ii=0;ii<p_xs_3->BSIZE;ii++) {
2608 for (jj=0;jj<p_xs_3->ASIZE;jj++) {
2609 prob=rand();
2610 prob=prob/2147483648.0;
2611 prob2=rand();
2612 prob2=prob2/2147483648.0;
2613 gauss1=rn*sqrt(-2.0*log(1-prob))*sin(2*M_PI*prob2);
2614 gauss2=rn*sqrt(-2.0*log(1-prob))*cos(2*M_PI*prob2);
2615 ccd[ii][jj]+=gauss1*rn;
2616 }
2617 }
2618 }
2619 /*memory allocation for 1D representation of CCD array for fitsio*/
2620 /* line below is NOT needed when copy2D_to_1D is used, since copy2D_to_1D allocates the memory itself. the cpl_free for oneD_AB is however still needed*/
2621 //oneD_AB=(double *)cpl_malloc((size_t)(p_xs_3->ASIZE*p_xs_3->BSIZE)*sizeof(double));
2622 oneD_AB=xsh_copy2D_to_1D(ccd, p_xs_3->BSIZE,p_xs_3->ASIZE);
2623 ima = cpl_image_wrap_double(naxes[0], naxes[1], oneD_AB) ;
2624 cpl_image_save(ima, "chips_out.fits", CPL_BPP_IEEE_DOUBLE, NULL, CPL_IO_DEFAULT) ;
2625 cpl_image_unwrap(ima) ;
2626 cpl_free(oneD_AB);
2627
2628 if (BackMapFlag==1) {
2629 oneD_AB=xsh_copy2D_to_1D(ccd_wav, p_xs_3->BSIZE,p_xs_3->ASIZE);
2630 ima2 = cpl_image_wrap_double(naxes[0], naxes[1], oneD_AB) ;
2631 cpl_image_save(ima2, "chips_wav.fits", CPL_BPP_IEEE_DOUBLE, NULL, CPL_IO_DEFAULT) ;
2632 cpl_image_unwrap(ima2) ;
2633 cpl_free(oneD_AB);
2634 if ((xsh_free2Darray(ccd_wav,p_xs_3->BSIZE))!=0) {
2635 cpl_msg_error(__func__, "Cannot free 2D array ccd_wav");
2636 return -1;
2637 }
2638 }
2639 if ((xsh_free2Darray(ccd,p_xs_3->BSIZE))!=0) {
2640 cpl_msg_error(__func__, "Cannot free 2D array ccd");
2641 return -1;
2642 }
2643 }
2644 }
2645
2646 cleanup:
2647 xsh_msg("Doing Cleanup");
2648 if (rec_out != NULL) fclose(rec_out);
2649 if (ex1d_out != NULL) fclose(ex1d_out);
2650 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2651 xsh_error_dump(CPL_MSG_ERROR);
2652 return -1;
2653 }
2654 else {
2655 return 0;
2656 }
2657}
2658
static int starty
static int endy
static xsh_instrument * instrument
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_2dmap.c:151
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define cknull(NULLEXP)
Definition: xsh_error.h:77
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
void xsh_instrument_set_biny(xsh_instrument *instrument, const int biny)
void xsh_instrument_set_binx(xsh_instrument *instrument, const int binx)
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
xsh_instrument * xsh_instrument_new(void)
create new instrument structure
cpl_error_code xsh_model_config_load_best(cpl_frame *config_frame, xsh_xs_3 *p_xs_3)
Load the config model table and fill the struct.
Definition: xsh_model_io.c:174
double * xsh_copy2D_to_1D(double **ccdtemp, int asize, int bsize)
copy 2D to 1D array
void xsh_ref_ind_read(int arm, DOUBLE **ref_ind_T, DOUBLE temper)
Load the refractive index coeffs for a given arm and temperature.
cpl_vector * xsh_model_refining_detect(const cpl_vector *in, int fwhm, double sigma, int display)
Detect the brightest features in a spectrum.
void xsh_model_get_xy(xsh_xs_3 *p_xs_3, xsh_instrument *instr, double lambda_nm, int morder, double ent_slit_pos, double *x, double *y)
Compute the detector location (floating point pixels) of a given wavelength/entrance slit position.
void xsh_3_detpix(struct xs_3 *p_xs_3)
Takes the physical x,y position at the detector array and converts this to a pixel position.
cpl_frame * xsh_model_order_edges_tab_create(xsh_xs_3 *p_xs_3, const char *tab_filename)
Create an order edges table using the phys mod.
void xsh_model_binxy(xsh_xs_3 *p_xs_3, int bin_X, int bin_Y)
corrects model for detector's binning
void xsh_3_eval(DOUBLE lambda, int morder, DOUBLE **ref_ind, struct xs_3 *p_xs_3)
Compute the physical x,y position at the detector array for a given wavelength, order and parameter c...
cpl_frame * xsh_model_spectralformat_create(xsh_xs_3 *p_xs_3, const char *tab_filename)
creates the model spectral format table
DOUBLE * xsh_alloc1Darray(int asize)
allocates memory for an array of doubles
int xsh_free2Darray(double **ccdtemp, int asize)
free memory for a 2D array of doubles
int * xsh_alloc1Darray_INT(int asize)
allocates memory for an array of integers
void xsh_3_init(struct xs_3 *p_xs_3)
Pre-compute a number of non-wavelength dependent secondary parameters required by the model.
cpl_error_code xsh_model_maps_create(xsh_xs_3 *p_xs_3, xsh_instrument *instr, const char *wtag, const char *stag, cpl_frame **wmap_frame, cpl_frame **smap_frame, const int save_tmp)
Compute the wavelength and slit maps.
cpl_frame * xsh_model_THE_create(xsh_xs_3 *p_xs_3, xsh_instrument *instr, const char *line_list, int num_ph, double sep_ph, const char *THE_filename)
Compute the THE table (centroid for each feature in lamp spectrum)
double ** xsh_alloc2Darray(int asize, int bsize)
allocates memory for a 2D array of doubles
cpl_vector ** xsh_model_locus(struct xs_3 *p_xs_3, xsh_instrument *instr, double ent_slit_pos)
Compute the locus of the spectrum.
static int xsh_model_create(cpl_plugin *)
Setup the recipe options.
#define RECIPE_CONTACT
static int xsh_model(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
int * y
static int xsh_model_reduce(const char *, const char *)
The actual model computation takes place here.
static char xsh_model_compute_description[]
static const int vectordim
static int xsh_model_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
static char xsh_model_compute_description_short[]
#define RECIPE_ID
#define RECIPE_AUTHOR
int * x
static int xsh_model_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
Definition: xsh_pfits.c:1627
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
void xsh_init(void)
Reset library state.
Definition: xsh_utils.c:1160
DOUBLE es_x
DOUBLE pix
DOUBLE xpospix
DOUBLE pix_Y
DOUBLE es_w
DOUBLE blaze_pad
int morder_max
DOUBLE pix_X
double chipypix
DOUBLE grat_beta
DOUBLE t_ir_p2
DOUBLE grat_alpha
DOUBLE slit_scale
DOUBLE ypospix
DOUBLE es_y
DOUBLE temper
int morder_min
DOUBLE es_y_tot
DOUBLE nug
detloc chippix
DOUBLE sg
DOUBLE es_s
int morder
double chipxpix
xsh_order * list
cpl_polynomial * cenpoly
#define XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH
#define XSH_ARCLIST_TABLE_COLNAME_FLUX
#define XSH_ORDERS_NIR
#define XSH_ORDERS_VIS
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
#define XSH_ORDERS_UVB
@ XSH_MODE_IFU
int ny
cpl_frame * xsh_find_model_wavelist(cpl_frameset *frames, xsh_instrument *instr)
Find model wavelength list frame.
Definition: xsh_dfs.c:4265
cpl_frame * xsh_find_model_testpar(cpl_frameset *frames, xsh_instrument *instr)
Find model test parameter list.
Definition: xsh_dfs.c:4292
#define XSH_MOD_CFG_VIS
Definition: xsh_dfs.h:1263
#define XSH_MOD_CFG_UVB
Definition: xsh_dfs.h:1262
#define XSH_MOD_CFG_NIR
Definition: xsh_dfs.h:1264
#define NIR_chipypix
#define NIR_morder_max
#define UVB_chipypix
#define NIR_morder_min
#define VIS_chipxpix
#define VIS_morder_max
#define NIR_SIZE
#define VIS_chipypix
#define UVB_morder
#define UVB_chipxpix
#define UVB_morder_max
#define NIR_BSIZE
#define VIS_SIZE
#define NIR_morder
#define UVB_morder_min
#define VIS_morder
#define UVB_ASIZE
#define VIS_BSIZE
#define UVB_BSIZE
#define NIR_chipxpix
#define NIR_ASIZE
#define UVB_SIZE
#define VIS_ASIZE
#define VIS_morder_min
void xsh_model_io_output_cfg_txt(struct xs_3 *p_xs_3)
double DOUBLE
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138
#define M_PI
Definition: xsh_utils.h:43
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)