00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034 #include <xsh_cpl_size.h>
00035
00036
00037
00038
00039 #include <math.h>
00040
00041 #include <xsh_data_the_map.h>
00042 #include <xsh_drl.h>
00043 #include <xsh_data_pre.h>
00044 #include <xsh_dfs.h>
00045 #include <xsh_pfits.h>
00046 #include <xsh_error.h>
00047 #include <xsh_msg.h>
00048 #include <xsh_data_instrument.h>
00049 #include <xsh_data_spectralformat.h>
00050 #include <xsh_badpixelmap.h>
00051 #include <xsh_model_io.h>
00052 #include <cpl.h>
00053 #include <xsh_model_ref_ind.h>
00054 #include <xsh_data_resid_tab.h>
00055 #include <xsh_data_arclist.h>
00056 #include <xsh_model_kernel.h>
00057 #include <xsh_model_metric.h>
00058 #include <xsh_model_arm_constants.h>
00059 #include <xsh_utils_wrappers.h>
00060
00061
00062
00066
00067
00068 #define VERBOSE 0
00069 #define mm2nm 1000000.0
00070 #define mm2um 1000.0
00071
00072 #if defined VERBOSE && VERBOSE > 0
00073 #define XSH_TRACE_MSG(array) \
00074 xsh_msg("array: %g, %g, %g, %g",array[0],array[1],array[2],array[3]);
00075 #else
00076 #define XSH_TRACE_MSG(array)
00077 #endif
00078
00079
00080
00081
00082
00083 typedef struct {
00084 int order ;
00085 int pos_x ;
00086 int pos_y ;
00087 double flux ;
00088 } CENTER_ORDER ;
00089
00090 static const int vectordim=4;
00091
00092
00095 static void xsh_nullmatrix(mat A)
00096 {
00097
00098 register int i,j;
00099 for(i=0; i<vectordim; i++) {
00100 for(j=0; j<vectordim; j++) {
00101 A[i][j]=0.0;
00102 }
00103 }
00104
00105
00106
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 static void xsh_nullvector(vec a)
00123 {
00124
00125 int i;
00126 for(i=0; i<vectordim; i++) a[i]=0.0;
00127
00128
00129 }
00130
00131 static void xsh_copyvector(vec a, vec b)
00132 {
00133 int i;
00134 for(i=0; i<vectordim; i++) a[i]=b[i];
00135
00136 }
00137
00138 static const char*
00139 xsh_get_tag_opt_mod_cfg(xsh_xs_3* p_xs_3,int rec_id)
00140 {
00141
00142 const char* tag=NULL;
00143
00144 if(rec_id==0) {
00145
00146 if (p_xs_3->arm == XSH_ARM_UVB) {
00147 tag=XSH_MOD_CFG_OPT_REC_UVB;
00148 }
00149 else if (p_xs_3->arm == XSH_ARM_VIS) {
00150 tag=XSH_MOD_CFG_OPT_REC_VIS;
00151 }
00152 else if (p_xs_3->arm == XSH_ARM_NIR) {
00153 tag=XSH_MOD_CFG_OPT_REC_NIR;
00154 }
00155 } else if(rec_id==1) {
00156
00157 if (p_xs_3->arm == XSH_ARM_UVB) {
00158 tag=XSH_MOD_CFG_OPT_FMT_UVB;
00159 }
00160 else if (p_xs_3->arm == XSH_ARM_VIS) {
00161 tag=XSH_MOD_CFG_OPT_FMT_VIS;
00162 }
00163 else if (p_xs_3->arm == XSH_ARM_NIR) {
00164 tag=XSH_MOD_CFG_OPT_FMT_NIR;
00165 }
00166 } else if (rec_id ==2 ) {
00167
00168
00169 if (p_xs_3->arm == XSH_ARM_UVB) {
00170 tag=XSH_MOD_CFG_OPT_2D_UVB;
00171 }
00172 else if (p_xs_3->arm == XSH_ARM_VIS) {
00173 tag=XSH_MOD_CFG_OPT_2D_VIS;
00174 }
00175 else if (p_xs_3->arm == XSH_ARM_NIR) {
00176 tag=XSH_MOD_CFG_OPT_2D_NIR;
00177 }
00178
00179 } else if (rec_id ==3 ) {
00180
00181
00182 if (p_xs_3->arm == XSH_ARM_UVB) {
00183 tag=XSH_MOD_CFG_OPT_WAV_SLIT_UVB;
00184 }
00185 else if (p_xs_3->arm == XSH_ARM_VIS) {
00186 tag=XSH_MOD_CFG_OPT_WAV_SLIT_VIS;
00187 }
00188 else if (p_xs_3->arm == XSH_ARM_NIR) {
00189 tag=XSH_MOD_CFG_OPT_WAV_SLIT_NIR;
00190 }
00191
00192
00193 } else if (rec_id ==4 ) {
00194
00195
00196 if (p_xs_3->arm == XSH_ARM_UVB) {
00197 tag=XSH_MOD_CFG_OPT_WAV_IFU_UVB;
00198 }
00199 else if (p_xs_3->arm == XSH_ARM_VIS) {
00200 tag=XSH_MOD_CFG_OPT_WAV_IFU_VIS;
00201 }
00202 else if (p_xs_3->arm == XSH_ARM_NIR) {
00203 tag=XSH_MOD_CFG_OPT_WAV_IFU_NIR;
00204 }
00205
00206 } else if (rec_id ==5 ) {
00207
00208
00209 if (p_xs_3->arm == XSH_ARM_UVB) {
00210 tag=XSH_MOD_CFG_OPT_AFC_UVB;
00211 }
00212 else if (p_xs_3->arm == XSH_ARM_VIS) {
00213 tag=XSH_MOD_CFG_OPT_AFC_VIS;
00214 }
00215 else if (p_xs_3->arm == XSH_ARM_NIR) {
00216 tag=XSH_MOD_CFG_OPT_AFC_NIR;
00217 }
00218 }
00219 return tag;
00220 }
00221
00222
00223
00224 static cpl_error_code
00225 xsh_model_compute_residuals(xsh_xs_3* p_xs_3,coord* msp_coord,
00226 DOUBLE* p_wlarray,DOUBLE** ref_ind,
00227 const int size, const int annealed,
00228 cpl_propertylist* resid_header,
00229 cpl_frame* resid_frame,
00230 cpl_propertylist** result)
00231 {
00232
00233
00234 double resx_min=0;
00235 double resx_max=0;
00236 double resy_min=0;
00237 double resy_max=0;
00238
00239 double resx_med=0;
00240 double resy_med=0;
00241
00242 double resx_avg=0;
00243 double resy_avg=0;
00244
00245 double resx_rms=0;
00246 double resy_rms=0;
00247
00248 int ndat=0;
00249 int morder_cnt=0;
00250 const char* resid_tbl_name=NULL;
00251 cpl_table* resid_tbl=NULL;
00252
00253 int jj=0;
00254 double* pxm=NULL;
00255 double* pym=NULL;
00256
00257 check(resid_tbl_name=cpl_frame_get_filename(resid_frame));
00258
00259 check(resid_tbl=cpl_table_load(resid_tbl_name,1,0));
00260 check(cpl_table_fill_column_window(resid_tbl,
00261 XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL ,0,size,0.));
00262 check(cpl_table_fill_column_window(resid_tbl,
00263 XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL ,0,size,0.));
00264
00265 check(pxm=cpl_table_get_data_double(resid_tbl,
00266 XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL));
00267 check(pym=cpl_table_get_data_double(resid_tbl,
00268 XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL));
00269
00270
00271 for (jj=0;jj<size;jj++) {
00272 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->slit[msp_coord[jj].slit_pos]*p_xs_3->slit_scale;
00273 morder_cnt=msp_coord[jj].order;
00274 xsh_3_init(p_xs_3);
00275 xsh_3_eval(p_wlarray[jj],morder_cnt,ref_ind,p_xs_3);
00276 xsh_3_detpix(p_xs_3);
00277 pxm[jj]=p_xs_3->xpospix;
00278 pym[jj]=p_xs_3->ypospix;
00279
00280
00281
00282
00283
00284
00285
00286 }
00287 check( cpl_table_copy_data_double( resid_tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
00288 cpl_table_get_data_double ( resid_tbl, XSH_RESID_TAB_TABLE_COLNAME_XGAUSS)));
00289 check( cpl_table_copy_data_double( resid_tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
00290 cpl_table_get_data_double ( resid_tbl, XSH_RESID_TAB_TABLE_COLNAME_YGAUSS)));
00291
00292 check(cpl_table_subtract_columns(resid_tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
00293 XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL));
00294 check(cpl_table_subtract_columns(resid_tbl, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
00295 XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL));
00296
00297 check(cpl_table_save(resid_tbl, resid_header, NULL,resid_tbl_name, CPL_IO_DEFAULT));
00298
00299
00300 check(resx_min=cpl_table_get_column_min(resid_tbl,
00301 XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL));
00302 check(resx_max=cpl_table_get_column_max(resid_tbl,
00303 XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL));
00304 check(resx_avg=cpl_table_get_column_mean(resid_tbl,
00305 XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL));
00306 check(resx_med=cpl_table_get_column_median(resid_tbl,
00307 XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL));
00308 check(resx_rms=cpl_table_get_column_stdev(resid_tbl,
00309 XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL));
00310
00311
00312 check(resy_min=cpl_table_get_column_min(resid_tbl,
00313 XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL));
00314 check(resy_max=cpl_table_get_column_max(resid_tbl,
00315 XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL));
00316 check(resy_avg=cpl_table_get_column_mean(resid_tbl,
00317 XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL));
00318 check(resy_med=cpl_table_get_column_median(resid_tbl,
00319 XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL));
00320 check(resy_rms=cpl_table_get_column_stdev(resid_tbl,
00321 XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL));
00322 check(ndat=cpl_table_get_nrow(resid_tbl));
00323 xsh_free_table(&resid_tbl);
00324
00325
00326
00327
00328
00329
00330
00331
00332 cpl_propertylist_append_int(*result,"ESO QC MODEL NDAT",ndat) ;
00333
00334 if(annealed==0) {
00335 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESX_MIN,resx_min) ;
00336 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESY_MIN,resy_min) ;
00337 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESX_MAX,resx_max) ;
00338 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESY_MAX,resy_max) ;
00339 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESX_AVG,resx_avg) ;
00340 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESY_AVG,resy_avg) ;
00341 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESX_MED,resx_med) ;
00342 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESY_MED,resy_med) ;
00343 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESX_RMS,resx_rms) ;
00344 cpl_propertylist_append_double(*result,XSH_QC_MODEL_PREDICT_RESY_RMS,resy_rms) ;
00345 } else {
00346
00347 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESX_MIN,resx_min) ;
00348 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESY_MIN,resy_min) ;
00349 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESX_MAX,resx_max) ;
00350 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESY_MAX,resy_max) ;
00351 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESX_AVG,resx_avg) ;
00352 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESY_AVG,resy_avg) ;
00353 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESX_MED,resx_med) ;
00354 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESY_MED,resy_med) ;
00355 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESX_RMS,resx_rms) ;
00356 cpl_propertylist_append_double(*result,XSH_QC_MODEL_ANNEAL_RESY_RMS,resy_rms) ;
00357
00358 }
00359 cleanup:
00360 xsh_free_table(&resid_tbl);
00361 return cpl_error_get_code();
00362
00363
00364 }
00365
00366
00373
00374
00375 void xsh_3_init(struct xs_3 * p_xs_3)
00376 {
00377
00378
00379
00380
00381
00382
00383
00384 #ifdef FCCDD_FLAG
00385 xsh_rotin(p_xs_3->todetector,-0.062,0.01,0.0);
00386 #endif
00387
00388
00389
00390 xsh_rotin(p_xs_3->e_slit,p_xs_3->mues,p_xs_3->nues,p_xs_3->taues);
00391
00392
00393 xsh_rotin(p_xs_3->mup_ir_cor,p_xs_3->cmup1,0.0,0.0);
00394 xsh_rotin(p_xs_3->mup_ir_cor_out,-p_xs_3->cmup1,0.0,0.0);
00395 xsh_rotin(p_xs_3->toprism1,p_xs_3->mup1,p_xs_3->nup1,p_xs_3->taup1);
00396 xsh_rotin(p_xs_3->toprism2,p_xs_3->mup2,p_xs_3->nup2,p_xs_3->taup2);
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 xsh_rotin(p_xs_3->toprism3,p_xs_3->mup3,p_xs_3->nup3,p_xs_3->taup3);
00408 xsh_rotin(p_xs_3->toprism4,p_xs_3->mup4,p_xs_3->nup4,p_xs_3->taup4);
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 xsh_rotin(p_xs_3->toprism5,p_xs_3->mup5,p_xs_3->nup5,p_xs_3->taup5);
00419 xsh_rotin(p_xs_3->toprism6,p_xs_3->mup6,p_xs_3->nup6,p_xs_3->taup6);
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 xsh_rotin(p_xs_3->tograt,p_xs_3->mug,p_xs_3->nug,p_xs_3->taug);
00430 xsh_transpose(p_xs_3->grat_out,p_xs_3->tograt);
00431
00432
00433
00434
00435
00436 xsh_transpose(p_xs_3->ret_prism2,p_xs_3->toprism1);
00437 xsh_transpose(p_xs_3->ret_prism1,p_xs_3->toprism2);
00438 xsh_multiplymatrix(p_xs_3->ret_prism_out1,p_xs_3->toprism2,p_xs_3->toprism1);
00439 xsh_transpose(p_xs_3->ret_prism4,p_xs_3->toprism3);
00440 xsh_transpose(p_xs_3->ret_prism3,p_xs_3->toprism4);
00441 xsh_multiplymatrix(p_xs_3->ret_prism_out2,p_xs_3->toprism4,p_xs_3->toprism3);
00442 xsh_transpose(p_xs_3->ret_prism6,p_xs_3->toprism5);
00443 xsh_transpose(p_xs_3->ret_prism5,p_xs_3->toprism6);
00444 xsh_multiplymatrix(p_xs_3->ret_prism_out3,p_xs_3->toprism6,p_xs_3->toprism5);
00445
00446
00447 if (p_xs_3->arm!=2) {
00448 p_xs_3->chipxup=p_xs_3->chipx+p_xs_3->pix_Y*(0.5*p_xs_3->chipypix);
00449 p_xs_3->chipxdown=p_xs_3->chipx-p_xs_3->pix_Y*(0.5*p_xs_3->chipypix);
00450 p_xs_3->chipyup=p_xs_3->chipy+p_xs_3->pix_X*(0.5*p_xs_3->chipxpix);
00451 p_xs_3->chipydown=p_xs_3->chipy-p_xs_3->pix_X*(0.5*p_xs_3->chipxpix);
00452 }
00453 else {
00454 p_xs_3->chipxup=p_xs_3->chipx+p_xs_3->pix_X*(0.5*p_xs_3->chipxpix+NIR_FIX);
00455 p_xs_3->chipxdown=p_xs_3->chipx-p_xs_3->pix_X*(0.5*p_xs_3->chipxpix);
00456 p_xs_3->chipyup=p_xs_3->chipy+p_xs_3->pix_Y*(+0.5*p_xs_3->chipypix);
00457 p_xs_3->chipydown=p_xs_3->chipy-p_xs_3->pix_Y*(0.5*p_xs_3->chipypix);
00458 }
00459 }
00460
00461
00470
00471
00472 void xsh_3_eval(DOUBLE lambda,
00473 int morder,
00474 DOUBLE** ref_ind,
00475 struct xs_3* p_xs_3)
00476 {
00477 DOUBLE lambda_um,lamf,rind_up,rind_down,xx,yy,dndT;
00478 vec vres,slitpos2,slitpos,detproj;
00479
00480 mat mech={ {1,0,0,0},
00481 {morder*(p_xs_3->sg),1,0,0},
00482 {0,0,1,0},
00483 {0,0,0,-1}};
00484 mat chrom_corr={ {1,0,0,0},
00485 {0,1,0,0},
00486 {0,0,1,0},
00487 {0,0,0,1}};
00488
00489
00490
00491
00492
00493
00494 vec vres1={lambda,
00495 0,
00496 0,
00497 -p_xs_3->fcol};
00498
00499
00500 XSH_TRACE_MSG(vres1)
00501
00502
00503 slitpos[0]=0.0;
00504 slitpos[1]=1.0*p_xs_3->es_x;
00505
00506
00507
00508 if (p_xs_3->config_mjd>2455409.0) {
00509
00510 if (p_xs_3->arm==0) {
00511 if (lambda>0.000425) {
00512 slitpos[2]=-1.0*p_xs_3->es_y_tot*(1.0+(lambda-0.000425)*p_xs_3->offx);
00513 }
00514 else{
00515 slitpos[2]=-1.0*p_xs_3->es_y_tot*(1.0+(lambda-0.000425)*p_xs_3->offy);
00516 }
00517 }
00518 else if (p_xs_3->arm==1) {
00519 if (lambda>0.000650) {
00520 slitpos[2]=1.0*(p_xs_3->es_y_tot)*(1.0+(lambda-0.000900)*p_xs_3->offx);
00521 }
00522 else{
00523 slitpos[2]=1.0*(p_xs_3->es_y_tot)*(1.0+(lambda-0.000018)*p_xs_3->offy);
00524 }
00525 }
00526 else if (p_xs_3->arm==2) {
00527 if (lambda>0.001400) {
00528 slitpos[2]=1.0*(p_xs_3->es_y_tot-p_xs_3->es_y)*(1.0+(lambda-0.001600)*p_xs_3->offx)+p_xs_3->es_y;
00529 }
00530 else{
00531 slitpos[2]=1.0*(p_xs_3->es_y_tot)*(1.0+(lambda-0.000990)*p_xs_3->offy);
00532 }
00533 }
00534 }
00535 else {
00536 if (p_xs_3->arm==0) {
00537 slitpos[2]=-1.0*p_xs_3->es_y_tot;
00538 }
00539 else {
00540 slitpos[2]=1.0*p_xs_3->es_y_tot;
00541 }
00542 }
00543 slitpos[3]=0.0;
00544 xsh_matrixforvector(slitpos2,p_xs_3->e_slit,slitpos);
00545 xsh_subtractvectors(vres1,slitpos2);
00546 xsh_copyvector(vres,vres1);
00547 xsh_normall(vres);
00548 if (p_xs_3->arm!=2 && p_xs_3->config_mjd<2455409.0) {
00549
00550 xsh_rotin(chrom_corr,vres[2]*(lambda-p_xs_3->offy)*p_xs_3->offx,0.0,0.0);
00551 xsh_matrixforvector(vres,chrom_corr,vres);
00552 }
00553
00554 XSH_TRACE_MSG(vres)
00555
00556
00557
00558 if (p_xs_3->arm==2) xsh_matrixforvector(vres,p_xs_3->mup_ir_cor,vres);
00559
00560 xsh_matrixforvector(vres,p_xs_3->toprism1,vres);
00561
00562
00563 lambda_um=lambda* mm2um;
00564 lamf=lambda_um*lambda_um;
00565
00566 if (p_xs_3->arm==1) {
00567
00568
00569 p_xs_3->rind=sqrt(ref_ind[0][0]*lamf/(lamf-ref_ind[0][3])+ref_ind[0][1]*lamf/(lamf-ref_ind[0][4])+ref_ind[0][2]*lamf/(lamf-ref_ind[0][5])+1.0);
00570
00571
00572 double lam_ig2=0.04963984;
00573 double H=71.0867E-06;
00574 double G=-50.2451E-06;
00575 double R;
00576
00577 R=lamf/(lamf-lam_ig2);
00578 dndT=(G*R+H*R*R)/(2.0*p_xs_3->rind);
00579
00580
00581
00582
00583 p_xs_3->rind+=dndT*(p_xs_3->temper-293.15);
00584
00585 }
00586 else {
00587
00588 p_xs_3->rind=xsh_model_sellmeier_ext(p_xs_3->arm,p_xs_3->temper,lamf);
00589 }
00590
00591
00592
00593
00594
00595
00596
00597
00598 xsh_refract(vres,p_xs_3->rind,vres);
00599
00600
00601
00602 xsh_matrixforvector(vres,p_xs_3->toprism2,vres);
00603
00604
00605
00606 xsh_refract(vres,1.0/(p_xs_3->rind),vres);
00607
00608
00609
00610
00611 xsh_matrixforvector(vres,p_xs_3->toprism1,vres);
00612
00613
00614
00615
00616 XSH_TRACE_MSG(vres)
00617 if (p_xs_3->arm==2) {
00618 XSH_TRACE_MSG(vres)
00619
00620
00621 xsh_matrixforvector(vres,p_xs_3->toprism3,vres);
00622
00623
00624
00625
00626 rind_up=sqrt(ref_ind[1][0]*lamf/(lamf-ref_ind[1][3]*ref_ind[1][3])+ref_ind[1][1]*lamf/(lamf-ref_ind[1][4]*ref_ind[1][4])+ref_ind[1][2]*lamf/(lamf-ref_ind[1][5]*ref_ind[1][5])+1.0);
00627
00628
00629 rind_down=sqrt(ref_ind[0][0]*lamf/(lamf-ref_ind[0][3]*ref_ind[0][3])+ref_ind[0][1]*lamf/(lamf-ref_ind[0][4]*ref_ind[0][4])+ref_ind[0][2]*lamf/(lamf-ref_ind[0][5]*ref_ind[0][5])+1.0);
00630
00631
00632 p_xs_3->rind2=rind_down+((p_xs_3->t_ir_p2-ref_ind[0][6])/(ref_ind[1][6]-ref_ind[0][6]))*(rind_up-rind_down);
00633
00634
00635 xsh_refract(vres,p_xs_3->rind2,vres);
00636
00637
00638
00639 xsh_matrixforvector(vres,p_xs_3->toprism4,vres);
00640
00641
00642
00643 xsh_refract(vres,1.0/(p_xs_3->rind2),vres);
00644
00645
00646
00647
00648 xsh_matrixforvector(vres,p_xs_3->toprism3,vres);
00649
00650
00651 XSH_TRACE_MSG(vres)
00652
00653
00654 xsh_matrixforvector(vres,p_xs_3->toprism5,vres);
00655 XSH_TRACE_MSG(vres)
00656
00657
00658
00659 rind_up=sqrt(ref_ind[1][0]*lamf/(lamf-ref_ind[1][3]*ref_ind[1][3])+ref_ind[1][1]*lamf/(lamf-ref_ind[1][4]*ref_ind[1][4])+ref_ind[1][2]*lamf/(lamf-ref_ind[1][5]*ref_ind[1][5])+1.0);
00660
00661
00662 rind_down=sqrt(ref_ind[0][0]*lamf/(lamf-ref_ind[0][3]*ref_ind[0][3])+ref_ind[0][1]*lamf/(lamf-ref_ind[0][4]*ref_ind[0][4])+ref_ind[0][2]*lamf/(lamf-ref_ind[0][5]*ref_ind[0][5])+1.0);
00663
00664
00665 p_xs_3->rind3=rind_down+((p_xs_3->t_ir_p3-ref_ind[0][6])/(ref_ind[1][6]-ref_ind[0][6]))*(rind_up-rind_down);
00666
00667
00668 xsh_refract(vres,p_xs_3->rind3,vres);
00669 XSH_TRACE_MSG(vres)
00670
00671
00672
00673
00674 xsh_matrixforvector(vres,p_xs_3->toprism6,vres);
00675 XSH_TRACE_MSG(vres)
00676
00677
00678
00679
00680 xsh_refract(vres,1.0/(p_xs_3->rind3),vres);
00681 XSH_TRACE_MSG(vres)
00682
00683
00684
00685
00686
00687
00688 xsh_matrixforvector(vres,p_xs_3->toprism5,vres);
00689 XSH_TRACE_MSG(vres)
00690
00691
00692
00693 }
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703 xsh_matrixforvector(vres,p_xs_3->tograt,vres);
00704 XSH_TRACE_MSG(vres)
00705
00706
00707 p_xs_3->grat_alpha=atan(vres[1]/vres[3]);
00708
00709
00710
00711 xsh_matrixforvector(vres,mech,vres);
00712 XSH_TRACE_MSG(vres)
00713
00714
00715
00716
00717 xsh_normz(vres);
00718 XSH_TRACE_MSG(vres)
00719
00720
00721 p_xs_3->grat_beta=atan(vres[1]/vres[3]);
00722
00723
00724
00725 xsh_matrixforvector(vres,p_xs_3->grat_out,vres);
00726 XSH_TRACE_MSG(vres)
00727
00728
00729
00730
00731 if (p_xs_3->arm==2) {
00732
00733 xsh_matrixforvector(vres,p_xs_3->ret_prism6,vres);
00734 XSH_TRACE_MSG(vres)
00735
00736
00737
00738
00739
00740 xsh_refract(vres,p_xs_3->rind3,vres);
00741 XSH_TRACE_MSG(vres)
00742
00743
00744
00745
00746
00747 xsh_matrixforvector(vres,p_xs_3->ret_prism5,vres);
00748 XSH_TRACE_MSG(vres)
00749
00750
00751
00752
00753
00754 xsh_refract(vres,1.0/(p_xs_3->rind3),vres);
00755 XSH_TRACE_MSG(vres)
00756
00757
00758
00759
00760 xsh_matrixforvector(vres,p_xs_3->ret_prism6,vres);
00761 XSH_TRACE_MSG(vres)
00762
00763
00764
00765
00766
00767 xsh_matrixforvector(vres,p_xs_3->ret_prism4,vres);
00768 XSH_TRACE_MSG(vres)
00769
00770
00771
00772
00773 xsh_refract(vres,p_xs_3->rind2,vres);
00774 XSH_TRACE_MSG(vres)
00775
00776
00777
00778
00779 xsh_matrixforvector(vres,p_xs_3->ret_prism3,vres);
00780 XSH_TRACE_MSG(vres)
00781
00782
00783
00784
00785 xsh_refract(vres,1.0/(p_xs_3->rind2),vres);
00786 XSH_TRACE_MSG(vres)
00787
00788
00789
00790
00791 xsh_matrixforvector(vres,p_xs_3->ret_prism4,vres);
00792 XSH_TRACE_MSG(vres)
00793
00794
00795 }
00796
00797
00798 xsh_matrixforvector(vres,p_xs_3->ret_prism2,vres);
00799 XSH_TRACE_MSG(vres)
00800
00801
00802
00803
00804 xsh_refract(vres,p_xs_3->rind,vres);
00805 XSH_TRACE_MSG(vres)
00806
00807
00808
00809
00810 xsh_matrixforvector(vres,p_xs_3->ret_prism1,vres);
00811 XSH_TRACE_MSG(vres)
00812
00813
00814
00815
00816 xsh_refract(vres,1.0/(p_xs_3->rind),vres);
00817 XSH_TRACE_MSG(vres)
00818
00819
00820
00821
00822 xsh_matrixforvector(vres,p_xs_3->ret_prism2,vres);
00823 XSH_TRACE_MSG(vres)
00824
00825
00826 if (p_xs_3->arm==2) xsh_matrixforvector(vres,p_xs_3->mup_ir_cor_out,vres);
00827 XSH_TRACE_MSG(vres)
00828
00829
00830 #ifdef FCCDD_FLAG
00831 xsh_matrixforvector(vres,p_xs_3->todetector,vres);
00832 XSH_TRACE_MSG(vres)
00833
00834 #endif
00835
00836
00837
00838 xx=(p_xs_3->flipx)*(p_xs_3->fdet)/((vres[3]/vres[1])*cos(p_xs_3->nud)-sin(p_xs_3->nud));
00839 yy=(p_xs_3->flipy)*(p_xs_3->fdet)/((vres[3]/vres[2])*cos(p_xs_3->mud)-sin(p_xs_3->mud));
00840
00841
00842
00843
00844 #ifdef CCCDD_FLAG
00845
00846 xx=(p_xs_3->flipx)*(p_xs_3->fdet)*vres[1];
00847 yy=(p_xs_3->flipy)*(p_xs_3->fdet)*vres[2];
00848 #endif
00849
00850 #ifdef XCCCDD_FLAG
00851
00852 xx=(p_xs_3->flipx)*(p_xs_3->fdet)*2.0*vres[1];
00853 yy=(p_xs_3->flipy)*(p_xs_3->fdet)*2.0*vres[2];
00854 #endif
00855
00856
00857 detproj[0]=0.0;
00858 detproj[1]=xx*cos(-p_xs_3->taud)+yy*sin(-p_xs_3->taud);
00859 detproj[2]=-xx*sin(-p_xs_3->taud)+yy*cos(-p_xs_3->taud);
00860 detproj[3]=0.0;
00861 xsh_copyvector(vres,detproj);
00862 XSH_TRACE_MSG(vres)
00863
00864
00865 p_xs_3->morder=(float)(morder);
00866
00867 p_xs_3->xdet=vres[1];
00868 p_xs_3->ydet=vres[2];
00869 }
00870
00871
00872
00878
00879
00880 void xsh_3_detpix(struct xs_3 *p_xs_3)
00881 {
00882
00883 double xdet2, xdet3, ydet2, ydet3;
00884 double xdet_c, xdet_c2, xdet_c3, ydet_c, ydet_c2, ydet_c3;
00885 double d2_y1=0.0;
00886 double d2_y2=0.0;
00887 double d2_y3=0.0;
00888 double pc_x, pc_y, dstn_x, dstn_y;
00889 double xdet_c4, ydet_c4;
00890
00891 int array_size_fix=0;
00892
00893
00894
00895
00896 p_xs_3->xpospix=0.0;
00897 p_xs_3->ypospix=0.0;
00898 p_xs_3->chippix[0]=-1;
00899 p_xs_3->chippix[1]=0;
00900 p_xs_3->chippix[2]=0;
00901
00902 #ifndef FCCDD_FLAG
00903
00904
00905
00906 ydet2=p_xs_3->ydet*p_xs_3->ydet;
00907 ydet3=ydet2*p_xs_3->ydet;
00908 xdet2=p_xs_3->xdet*p_xs_3->xdet;
00909 xdet3=xdet2*p_xs_3->xdet;
00910 xdet_c=p_xs_3->xdet-p_xs_3->chipx;
00911
00912 xdet_c2=xdet_c*xdet_c;
00913 xdet_c3=xdet_c2*xdet_c;
00914 xdet_c4=xdet_c3*xdet_c;
00915 ydet_c=p_xs_3->ydet-p_xs_3->chipy;
00916
00917 ydet_c2=ydet_c*ydet_c;
00918 ydet_c3=ydet_c2*ydet_c;
00919 ydet_c4=ydet_c3*ydet_c;
00920
00921
00922 pc_x=p_xs_3->chipx+
00923 (p_xs_3->pc_x_xx*xdet_c2)+
00924 (p_xs_3->pc_x_x1*xdet_c)+
00925 (p_xs_3->pc_x_yy*ydet_c2)+
00926 (p_xs_3->pc_x_y1*ydet_c)+
00927 (p_xs_3->pc_x_xy*xdet_c*ydet_c)+
00928 (p_xs_3->pc_x_x3*xdet_c3)+
00929 (p_xs_3->pc_x_x2y*xdet_c2*ydet_c)+
00930 (p_xs_3->pc_x_y2x*xdet_c*ydet_c2)+
00931 (p_xs_3->pc_x_y3*ydet_c3)+
00932 (p_xs_3->pc4_x_xy3*xdet_c*ydet_c3)+
00933 (p_xs_3->pc4_x_x3y*xdet_c3*ydet_c)+
00934 (p_xs_3->pc4_x_x2y2*xdet_c2*ydet_c2)+
00935 (p_xs_3->pc4_x_x4*xdet_c4)+
00936 (p_xs_3->pc4_x_y4*ydet_c4);
00937 pc_y=p_xs_3->chipy+
00938 (p_xs_3->pc_y_xx*xdet_c2)+
00939 (p_xs_3->pc_y_x1*xdet_c)+
00940 (p_xs_3->pc_y_yy*ydet_c2)+
00941 (p_xs_3->pc_y_y1*ydet_c)+
00942 (p_xs_3->pc_y_xy*xdet_c*ydet_c)+
00943 (p_xs_3->pc_y_x3*xdet_c3)+
00944 (p_xs_3->pc_y_x2y*xdet_c2*ydet_c)+
00945 (p_xs_3->pc_y_y2x*xdet_c*ydet_c2)+
00946 (p_xs_3->pc_y_y3*ydet_c3)+
00947 (p_xs_3->pc4_y_xy3*xdet_c*ydet_c3)+
00948 (p_xs_3->pc4_y_x3y*xdet_c3*ydet_c)+
00949 (p_xs_3->pc4_y_x2y2*xdet_c2*ydet_c2)+
00950 (p_xs_3->pc4_y_x4*xdet_c4)+
00951 (p_xs_3->pc4_y_y4*ydet_c4);
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974 p_xs_3->xdet=pc_x;
00975 p_xs_3->ydet=pc_y;
00976
00977
00978 if (p_xs_3->arm<2) {
00979 dstn_y=(p_xs_3->d2_x3*ydet_c3)+(p_xs_3->d2_x2*ydet_c2)+(p_xs_3->d2_x1*ydet_c);
00980
00981 p_xs_3->ydet=p_xs_3->ydet+dstn_y;
00982
00983
00984
00985
00986
00987
00988
00989 }
00990 else if (p_xs_3->arm==2) {
00991
00992
00993
00994
00995 if (p_xs_3->d2_x3!=0.0) {
00996 dstn_x=(p_xs_3->d2_x3*xdet_c3)+(p_xs_3->d2_x2*xdet_c2)+(p_xs_3->d2_x1*xdet_c);
00997
00998 p_xs_3->xdet=p_xs_3->xdet+dstn_x;
00999 d2_y3=(p_xs_3->d2_y3x3*xdet_c3)+(p_xs_3->d2_y3x2*xdet_c2)+(p_xs_3->d2_y3x1*xdet_c)+p_xs_3->d2_y3x0;
01000 d2_y2=(p_xs_3->d2_y2x3*xdet_c3)+(p_xs_3->d2_y2x2*xdet_c2)+(p_xs_3->d2_y2x1*xdet_c)+p_xs_3->d2_y2x0;
01001 d2_y1=(p_xs_3->d2_y1x3*xdet_c3)+(p_xs_3->d2_y1x2*xdet_c2)+(p_xs_3->d2_y1x1*xdet_c)+p_xs_3->d2_y1x0;
01002
01003
01004
01005
01006 dstn_y=(d2_y3*ydet_c3)+(d2_y2*ydet_c2)+(d2_y1*ydet_c);
01007 p_xs_3->ydet=p_xs_3->ydet+dstn_y;
01008 }
01009
01010 else {
01011 dstn_y=(p_xs_3->ca_x1*(xdet_c-p_xs_3->ca_x0)+p_xs_3->ca_y1*(ydet_c-p_xs_3->ca_y0))*p_xs_3->es_y_tot;
01012 p_xs_3->ydet=p_xs_3->ydet+dstn_y;
01013 }
01014 }
01015 #endif
01016 #ifdef CCCDD_FLAG
01017
01018 h_det=sqrt(p_xs_3->xdet*p_xs_3->xdet+p_xs_3->xdet*p_xs_3->xdet);
01019
01020 p_xs_3->xdet=(p_xs_3->fdet*p_xs_3->xdet/h_det)*sin(h_det/p_xs_3->fdet);
01021 p_xs_3->ydet=(p_xs_3->fdet*p_xs_3->ydet/h_det)*sin(h_det/p_xs_3->fdet);
01022 #endif
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043 p_xs_3->chippix[0]=1;
01044
01045
01046 if(p_xs_3->arm!=2) {
01047 p_xs_3->xpospix=0.5*(p_xs_3->chipxpix+p_xs_3->xsize_corr)-(p_xs_3->ydet-p_xs_3->chipy)/p_xs_3->pix_X;
01048 p_xs_3->ypospix=0.5*(p_xs_3->chipypix+p_xs_3->ysize_corr)-(p_xs_3->xdet-p_xs_3->chipx)/p_xs_3->pix_Y;
01049 }
01050 else {
01051 p_xs_3->xpospix=0.5*(p_xs_3->chipxpix+p_xs_3->xsize_corr)+(p_xs_3->xdet-p_xs_3->chipx)/p_xs_3->pix_X;
01052 p_xs_3->ypospix=0.5*(p_xs_3->chipypix+p_xs_3->ysize_corr)+(p_xs_3->ydet-p_xs_3->chipy)/p_xs_3->pix_Y;
01053 array_size_fix=NIR_FIX;
01054 }
01055
01056
01057 if (p_xs_3->pix_X>0.029) {
01058 p_xs_3->xpospix=p_xs_3->xpospix+0.25;
01059 }
01060 if (p_xs_3->pix_Y>0.029) {
01061 p_xs_3->ypospix=p_xs_3->ypospix+0.25;
01062 }
01063
01064 if (p_xs_3->xpospix>p_xs_3->chipxpix-1+array_size_fix) {
01065 p_xs_3->xpospix=-1.0;
01066 p_xs_3->ypospix=-1.0;
01067 p_xs_3->chippix[0]=6;
01068 }
01069 if (p_xs_3->xpospix<0) {
01070 p_xs_3->xpospix=-1.0;
01071 p_xs_3->ypospix=-1.0;
01072 p_xs_3->chippix[0]=5;
01073 }
01074 if (p_xs_3->ypospix<0) {
01075 p_xs_3->xpospix=-1.0;
01076 p_xs_3->ypospix=-1.0;
01077 p_xs_3->chippix[0]=8;
01078 }
01079 if (p_xs_3->ypospix>p_xs_3->chipypix-1) {
01080 p_xs_3->xpospix=-1.0;
01081 p_xs_3->ypospix=-1.0;
01082 p_xs_3->chippix[0]=7;
01083 }
01084
01085 p_xs_3->chippix[1]=(int)(p_xs_3->xpospix-0.5)+1;
01086 p_xs_3->chippix[2]=(int)(p_xs_3->ypospix-0.5)+1;
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110 return;
01111 }
01112
01113
01122
01123
01124 void
01125 xsh_ref_ind_read_old(const char* ref_ind_file,
01126 DOUBLE** ref_ind,
01127 DOUBLE temper)
01128 {
01129 int ii,jj,flag;
01130 FILE *p_ref_ind_file;
01131
01132
01133 p_ref_ind_file=fopen(ref_ind_file,"r");
01134 if(p_ref_ind_file==NULL) printf("couldn't find ref_ind file\n");
01135 ii=2;
01136 flag=0;
01137 while (fscanf(p_ref_ind_file,"%lf %lf %lf %lf %lf %lf %lf\n",&ref_ind[ii][6],&ref_ind[ii][0],&ref_ind[ii][1],&ref_ind[ii][2],&ref_ind[ii][3],&ref_ind[ii][4],&ref_ind[ii][5]) !=EOF)
01138
01139 {
01140 if (temper>=ref_ind[ii][6])
01141 {
01142
01143
01144 flag=1;
01145 for (jj=0; jj<7; jj++)
01146 {
01147 ref_ind[0][jj]=ref_ind[ii][jj];
01148 }
01149 }
01150 else if (temper<ref_ind[ii][6] && flag==1)
01151 {
01152
01153 flag=2;
01154 for (jj=0; jj<7; jj++)
01155 {
01156 ref_ind[1][jj]=ref_ind[ii][jj];
01157 }
01158 }
01159 ii++;
01160 }
01161
01162 if (flag!=2) printf("******* Temperature out of range! ******* %lf \n", temper);
01163 fclose(p_ref_ind_file);
01164 return;
01165 }
01166
01167
01174
01175
01176 void xsh_ref_ind_read(int arm, DOUBLE** ref_ind_T,DOUBLE temper)
01177 {
01178 int ii,jj,flag;
01179
01180
01181
01182 ii=2;
01183 flag=0;
01184 for (ii=2; ii<8; ii+=1) {
01185 for (jj=0;jj<7;jj+=1){
01186 if (arm==0) {
01187 ref_ind_T[ii][jj]=silica_zemax[ii-2][jj];
01188 }
01189 else if (arm==1) {
01190 ref_ind_T[ii][jj]=schott_sf6_manu[ii-2][jj];
01191 }
01192 else {
01193 ref_ind_T[ii][jj]=znse[ii-2][jj];
01194 }
01195 }
01196
01197 if (temper>=ref_ind_T[ii][6])
01198 {
01199
01200
01201 flag=1;
01202 for (jj=0; jj<7; jj++) {
01203 ref_ind_T[0][jj]=ref_ind_T[ii][jj];
01204 }
01205 }
01206 else if (temper<ref_ind_T[ii][6] && flag==1)
01207 {
01208
01209 flag=2;
01210 for (jj=0; jj<7; jj++) {
01211 ref_ind_T[1][jj]=ref_ind_T[ii][jj];
01212 }
01213 }
01214 }
01215
01216 if (flag!=2) {
01217 printf("******* Temperature out of range! ******* %lf \n", temper);
01218 for (jj=0; jj<7; jj++) {
01219 ref_ind_T[1][jj]=ref_ind_T[7][jj];
01220 }
01221 }
01222 return;
01223 }
01224
01225 #define ERROR 1
01226 #define OK 0
01227
01232 int* xsh_alloc1Darray_INT(int asize)
01233 {
01234 int* array;
01235
01236 if ((array=(int *)cpl_malloc(asize*sizeof(int))) == NULL)
01237 {
01238 printf("ERROR allocation memory for array1D int\n");
01239 return NULL;
01240 }
01241 return array;
01242 }
01248 DOUBLE* xsh_alloc1Darray(int asize)
01249 {
01250 DOUBLE* array;
01251
01252 if ((array=(DOUBLE *)cpl_malloc(asize*sizeof(DOUBLE))) == NULL)
01253 {
01254 printf("ERROR allocation memory for array1D double\n");
01255 return NULL;
01256 }
01257 return array;
01258 }
01259
01266 double** xsh_alloc2Darray(int asize, int bsize)
01267 {
01268 int ii;
01269 double** ccdtemp;
01270
01271 if ((ccdtemp=(double **)cpl_malloc(asize*sizeof(double *))) == NULL)
01272 {
01273 printf("ERROR allocating memory for ccdtemp at the initial pointer\n");
01274 return NULL;
01275 }
01276
01277 for (ii = 0; ii < asize; ii++)
01278 {
01279 if ( (ccdtemp[ii]=(double *)cpl_malloc(bsize*sizeof(double))) == NULL)
01280 {
01281 printf("ERROR allocating doublle memory for ccdtemp at row=%d\n",ii);
01282 return NULL;
01283 }
01284 }
01285 return ccdtemp;
01286 }
01287
01294 float** xsh_alloc2Darray_f(int asize, int bsize)
01295 {
01296 int ii;
01297 float **ccdtemp;
01298
01299 if ((ccdtemp=(float **)cpl_malloc(asize*sizeof(float *))) == NULL)
01300 {
01301 printf("ERROR allocating memory for ccdtemp at the initial pointer\n");
01302 return NULL;
01303 }
01304
01305 for (ii = 0; ii < asize; ii++)
01306 {
01307 if ( (ccdtemp[ii]=(float *)cpl_malloc(bsize*sizeof(float))) == NULL)
01308 {
01309 printf("ERROR allocating float memory for ccdtemp at row=%d\n",ii);
01310 return NULL;
01311 }
01312 }
01313 return ccdtemp;
01314 }
01315
01323 int** xsh_alloc2Darray_i(int asize, int bsize)
01324 {
01325 int ii;
01326 int **ccdtemp;
01327
01328 if ((ccdtemp=(int **)cpl_malloc(asize*sizeof(int *))) == NULL)
01329 {
01330 printf("ERROR allocating memory for ccdtemp at the initial pointer\n");
01331 return NULL;
01332 }
01333
01334 for (ii = 0; ii < asize; ii++)
01335 {
01336 if ( (ccdtemp[ii]=(int *)cpl_malloc(bsize*sizeof(int))) == NULL)
01337 {
01338 printf("ERROR allocating int memory for ccdtemp at row=%d\n",ii);
01339 return NULL;
01340 }
01341 }
01342 return ccdtemp;
01343 }
01344
01352 int xsh_free2Darray(double ** ccdtemp, int asize)
01353 {
01354 int i;
01355 const int my_asize=asize;
01356 for (i=my_asize-1; i >=0; i--)
01357 {
01358 if (ccdtemp[i] !=NULL) cpl_free(ccdtemp[i]);
01359 else
01360 {
01361 printf("Error freeing memory at row= %d\n", i);
01362 return 1;
01363 }
01364 }
01365 if( ccdtemp !=NULL) cpl_free(ccdtemp);
01366 else
01367 {
01368 printf("Error freeing memory at the initial pointer");
01369 return 1;
01370 }
01371 return 0;
01372 }
01373
01381 int xsh_free2Darray_i(int ** ccdtemp, int asize)
01382 {
01383 int i;
01384 for (i=asize-1; i >=0; i--)
01385 {
01386 if (ccdtemp[i] !=NULL) cpl_free(ccdtemp[i]);
01387 else
01388 {
01389 printf("Error freeing memory at row= %d\n", i);
01390 return 1;
01391 }
01392 }
01393 if( ccdtemp !=NULL) cpl_free(ccdtemp);
01394 else
01395 {
01396 printf("Error freeing memory at the initial pointer");
01397 return 1;
01398 }
01399 return 0;
01400 }
01401
01409 int xsh_free2Darray_f(float ** ccdtemp, int asize)
01410 {
01411 int i;
01412 for (i=asize-1; i >=0; i--)
01413 {
01414 if (ccdtemp[i] !=NULL) cpl_free(ccdtemp[i]);
01415 else
01416 {
01417 printf("Error freeing memory at row= %d\n", i);
01418 return 1;
01419 }
01420 }
01421 if( ccdtemp !=NULL) cpl_free(ccdtemp);
01422 else
01423 {
01424 printf("Error freeing memory at the initial pointer");
01425 return 1;
01426 }
01427 return 0;
01428 }
01436 double * xsh_copy2D_to_1D(double ** ccdtemp,int asize, int bsize)
01437 {
01438 int ii;
01439 int jj;
01440 int kk;
01441 double *oneDccdtemp=NULL;
01442 if ( (oneDccdtemp=(double *)cpl_malloc(asize*bsize*sizeof(double))) == NULL)
01443 {
01444 printf("ERROR allocating memory for oneDccdtemp\n");
01445 return NULL;
01446 }
01447 kk=0;
01448 for (ii = 0; ii < asize; ii++)
01449 {
01450 for (jj = 0; jj < bsize; jj++)
01451 {
01452 oneDccdtemp[kk]=ccdtemp[ii][jj];
01453 kk +=1;
01454 }
01455 }
01456 return oneDccdtemp;
01457 }
01458
01466 double ** xsh_copy1D_to_2D(double * oneDccdtemp, int asize, int bsize)
01467 {
01468 int ii;
01469 int jj;
01470 int kk;
01471 double **ccdtemp=NULL;
01472 ccdtemp=xsh_alloc2Darray(asize,bsize);
01473 kk=0;
01474 for (ii = 0; ii < asize; ii++)
01475 {
01476 for (jj = 0; jj < bsize; jj++)
01477 {
01478 ccdtemp[ii][jj]=oneDccdtemp[kk];
01479 kk +=1;
01480 }
01481 }
01482 return ccdtemp;
01483 }
01484
01492 double *** xsh_alloc3Darray2(int asize, int bsize, int csize)
01493 {
01494 int ii;
01495 double ***ccdtemp;
01496
01497
01498 if ((ccdtemp=(double ***)cpl_malloc(asize*sizeof(double **))) == NULL)
01499 {
01500 printf("ERROR allocating memory for ccdtemp at the initial pointer\n");
01501 return NULL ;
01502 }
01503 for (ii = 0; ii < asize; ii++) ccdtemp[ii]=xsh_alloc2Darray(bsize,csize);
01504 return ccdtemp;
01505 }
01506
01507
01515 double *** xsh_alloc3Darray(int asize, int bsize, int csize)
01516 {
01517 int ii,jj;
01518 double ***ccdtemp;
01519
01520
01521 if ((ccdtemp=(double ***)cpl_malloc(asize*sizeof(double **))) == NULL)
01522 {
01523 printf("ERROR allocating memory for ccdtemp at the initial pointer\n");
01524 return NULL ;
01525 }
01526
01527 for (ii = 0; ii < asize; ii++)
01528 {
01529 if ( (ccdtemp[ii]=(double **)cpl_malloc(bsize*sizeof(double*))) == NULL)
01530 {
01531 printf("ERROR allocating memory for ccdtemp at row=%d\n",ii);
01532 return NULL;
01533 }
01534 else
01535 {
01536 for (jj = 0; jj < bsize; jj++)
01537 {
01538 if ( (ccdtemp[ii][jj]=(double *)cpl_malloc(csize*sizeof(double))) == NULL)
01539 {
01540 printf("ERROR allocating memory for ccdtemp at row=%d\n",ii);
01541 return NULL;
01542 }
01543 }
01544 }
01545 }
01546 return ccdtemp;
01547 }
01548
01557 int xsh_free3Darray(double ***ccdtemp, int asize, int bsize)
01558 {
01559 int i;
01560 for (i=asize-1; i >=0; i--)
01561 {
01562 if (ccdtemp[i] !=NULL) xsh_free2Darray(ccdtemp[i],bsize);
01563 else
01564 {
01565 printf("Error freeing memory at slide= %d\n", i);
01566 return ERROR;
01567 }
01568 }
01569 if( ccdtemp !=NULL) cpl_free(ccdtemp);
01570 else
01571 {
01572 printf("Error freeing memory at the 3D initial pointer");
01573 return ERROR;
01574 }
01575 return OK;
01576 }
01577
01584 void xsh_addvectors(vec a, vec b)
01585 {
01586 int i;
01587 for(i=0; i<vectordim; i++) a[i]+=b[i];
01588 }
01589
01596 void xsh_addvectors3D(vec a, vec b)
01597 {
01598 int i;
01599 for(i=0; i<vectordim-1; i++) a[i]+=b[i+1];
01600 }
01601
01609 void xsh_subtractvectors(vec a, vec b)
01610 {
01611 int i;
01612 for(i=0; i<vectordim; i++) a[i]-=b[i];
01613 }
01614
01621 DOUBLE xsh_scalarproduct(vec a, vec b)
01622 {
01623 int i;
01624 DOUBLE scalar;
01625 scalar=0.0;
01626 for(i=0; i<vectordim; i++) scalar += a[i]*b[i];
01627 return scalar;
01628 }
01634 void xsh_multiply(vec a, DOUBLE k)
01635 {
01636 register int i;
01637 for(i=0; i<vectordim; i++) a[i] *=k;
01638 }
01639
01644 void xsh_showvector(vec a)
01645 {
01646 int i;
01647 for(i=0; i<vectordim; i++) printf("%lf ", a[i]*1000.0);
01648 printf(" \n");
01649 }
01650
01651
01657 void xsh_normz(vec a)
01658 {
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668 if (fabs(a[2]) > 1.0) {
01669 a[1]=0.0;
01670 a[2]=1.0;
01671 a[3]=0.0;
01672 }
01673 else if (fabs(a[1]) > 1.0) {
01674 a[1]=1.0;
01675 a[2]=0.0;
01676 a[3]=0.0;
01677 }
01678 else if ((a[1]*a[1] + a[2]*a[2]) > 1.0) {
01679 double norm = 1./sqrt (a[1]*a[1] + a[2]*a[2]);
01680 a[1] *=norm;
01681 a[2] *=norm;
01682 a[3]=0.0;
01683 }
01684 else{
01685
01686 if(a[3]>=0) a[3]=sqrt(1.0-(a[1]*a[1]+a[2]*a[2]));
01687 else a[3]=-sqrt(1.0-(a[1]*a[1]+a[2]*a[2]));
01688 }
01689 }
01690
01696 void xsh_normall(vec a)
01697 {
01698 DOUBLE mag;
01699 mag=1./sqrt(a[1]*a[1]+a[2]*a[2]+a[3]*a[3]);
01700 a[1]=a[1]*mag;
01701 a[2]=a[2]*mag;
01702 a[3]=a[3]*mag;
01703 }
01704
01711 void xsh_multiplymatrix(mat A, mat B, mat C)
01712 {
01713 register int i,j,k;
01714 xsh_nullmatrix(A);
01715 for(i=0; i<vectordim; i++) {
01716 for(j=0; j<vectordim; j++) {
01717 for(k=0; k<vectordim; k++){
01718 A[i][j] +=B[i][k]*C[k][j];
01719 }
01720 }
01721 }
01722 }
01730 void xsh_multiplythreematrix(mat A, mat B, mat C,mat D)
01731 {
01732 mat temp;
01733 xsh_nullmatrix(temp);
01734 xsh_multiplymatrix(temp,C,D);
01735 xsh_multiplymatrix(A,B,temp);
01736 }
01737
01742 void xsh_showmatrix(mat A)
01743 {
01744 int i;
01745 for(i=0; i<vectordim; i++) {
01746 printf("%lf %lf %lf %lf\n", A[i][0],A[i][1],A[i][2],A[i][3]);
01747 }
01748 printf(" \n");
01749 }
01750
01755 void xsh_initializematrix(mat A)
01756 {
01757 int i,j;
01758 for(i=0; i<vectordim; i++) {
01759 for(j=0; j<vectordim; j++) {
01760 A[i][j]=i+j;
01761 }
01762 }
01763 }
01764
01773 void xsh_matrixforvector(vec a,mat B,vec c)
01774 {
01775 register int i,j;
01776 vec temp;
01777 xsh_nullvector(temp);
01778 for(i=0; i<vectordim; i++) {
01779 for(j=0; j<vectordim; j++) {
01780 temp[i] +=B[i][j]*c[j];
01781 }
01782 }
01783 xsh_copyvector(a,temp);
01784 }
01785
01791 void xsh_transpose(mat A ,mat B)
01792 {
01793 register int i,j;
01794 for(i=0; i<vectordim; i++) {
01795 for(j=0; j<vectordim; j++) {
01796 A[i][j]=B[j][i];
01797 }
01798 }
01799 }
01800
01807 void
01808 xsh_rotationmatrix(mat A, const char axis, const DOUBLE angle)
01809 {
01810 xsh_nullmatrix(A);
01811 double sin_a=sin(angle);
01812 double cos_a=cos(angle);
01813
01814
01815 switch(axis){
01816
01817 case 'x':{
01818
01819 A[0][0]=A[1][1]=1;
01820 A[2][2]=A[3][3]=cos_a;
01821 A[2][3]=sin_a;
01822 A[3][2]= -A[2][3];
01823 break;
01824 }
01825
01826
01827 case 'y':{
01828
01829 A[0][0]=A[2][2]=1;
01830 A[1][1]=A[3][3]=cos_a;
01831 A[1][3]=-sin_a;
01832 A[3][1]=-A[1][3];
01833 break;
01834 }
01835
01836
01837 case 'z':{
01838 A[0][0]=A[3][3]=1;
01839 A[1][1]=A[2][2]=cos_a;
01840 A[1][2]=sin_a;
01841 A[2][1]=-A[1][2];
01842 break;
01843 }
01844 printf("Error on creating rotation matrix\n");
01845 }
01846
01847 }
01848
01856 void
01857 xsh_rotin(mat A,const DOUBLE x_angle, const DOUBLE y_angle,const DOUBLE z_angle)
01858 {
01859 mat temp;
01860 mat X,Y,Z;
01861 xsh_rotationmatrix(X,'x',x_angle);
01862 xsh_rotationmatrix(Y,'y',y_angle);
01863 xsh_rotationmatrix(Z,'z',z_angle);
01864
01865 xsh_multiplymatrix(temp,Y,X);
01866 xsh_multiplymatrix(A,Z,temp);
01867 }
01868
01869
01881
01882 double xsh_physmod_grating(vec in,
01883 int m,
01884 double sg,
01885 mat in_rot,
01886 mat exit_rot,
01887 vec out) {
01888 vec in2, out2;
01889 double alpha,beta;
01890 mat mech={ {1,0,0,0},
01891 {m*(sg),1,0,0},
01892 {0,0,1,0},
01893 {0,0,0,-1} };
01894
01895 xsh_matrixforvector(in2,in_rot,in);
01896 alpha=atan(in2[1]/in2[3]);
01897 xsh_matrixforvector(out2,mech,in2);
01898 xsh_normz(out2);
01899 beta=atan(out2[1]/out2[3]);
01900
01901 xsh_matrixforvector(out,exit_rot,out2);
01902 return alpha;
01903 }
01904
01905
01906
01915
01916 void xsh_refract(vec b, DOUBLE rind, vec a)
01917 {
01918 DOUBLE phi, pheta, y_signflag, z_signflag ,refrangle;
01919 y_signflag=1.0;
01920 z_signflag=1.0;
01921 phi=atan(sqrt(a[1]*a[1]+a[2]*a[2])/a[3]);
01922 pheta=atan(a[1]/a[2]);
01923 if (a[2]<0.0) y_signflag=-1.0;
01924 if (a[3]<0.0) z_signflag=-1.0;
01925
01926 refrangle=asin(sin(phi)/rind);
01927 b[0]=a[0];
01928 b[1]=z_signflag*y_signflag*sin(pheta)*sin(refrangle);
01929 b[2]=z_signflag*y_signflag*cos(pheta)*sin(refrangle);
01930 b[3]=z_signflag*cos(refrangle);
01931 }
01932
01933 #define ERROR 1
01934 #define OK 0
01935
01936
01937
01938
01939
01940 static cpl_error_code
01941 xsh_model_compute_slitmap_kw(const double slit_min,
01942 const double slit_max,
01943 xsh_xs_3* p_xs_3,
01944 xsh_instrument* instr,
01945 cpl_propertylist** plist)
01946 {
01947
01948 int morder=0;
01949 double med_slit_cen=0;
01950 double med_slit_up=0;
01951 double med_slit_lo=0;
01952 double med_slit_slicup=0;
01953 double med_slit_sliclo=0;
01954
01955
01956
01957 med_slit_cen = (slit_min+slit_max)/2.0;
01958
01959 med_slit_up = med_slit_cen+p_xs_3->es_s/2.0/p_xs_3->slit_scale;
01960 med_slit_lo = med_slit_cen-p_xs_3->es_s/2.0/p_xs_3->slit_scale;
01961 med_slit_slicup = IFU_HI+med_slit_cen;
01962 med_slit_sliclo = IFU_LOW+med_slit_cen;
01963
01964 check( xsh_pfits_set_slitmap_median_cen( *plist, med_slit_cen));
01965 check( xsh_pfits_set_slitmap_median_edgup( *plist, med_slit_up));
01966 check( xsh_pfits_set_slitmap_median_edglo( *plist, med_slit_lo));
01967
01968 for( morder = p_xs_3->morder_min; morder <= p_xs_3->morder_max; morder++){
01969 check( xsh_pfits_set_slitmap_order_cen( *plist, morder, med_slit_cen));
01970 check( xsh_pfits_set_slitmap_order_edgup( *plist, morder, med_slit_up));
01971 check( xsh_pfits_set_slitmap_order_edglo( *plist, morder, med_slit_lo));
01972 }
01973 if ( xsh_instrument_get_mode( instr) == XSH_MODE_IFU){
01974 check( xsh_pfits_set_slitmap_median_slicup( *plist, med_slit_slicup));
01975 check( xsh_pfits_set_slitmap_median_sliclo( *plist, med_slit_sliclo));
01976 for( morder = p_xs_3->morder_min; morder <= p_xs_3->morder_max; morder++){
01977 check( xsh_pfits_set_slitmap_order_slicup( *plist, morder, med_slit_slicup));
01978 check( xsh_pfits_set_slitmap_order_sliclo( *plist, morder, med_slit_sliclo));
01979 }
01980 }
01981
01982 cleanup:
01983
01984 return cpl_error_get_code();
01985 }
01986
01987
01988
01989
02002
02003 cpl_error_code
02004 xsh_model_maps_create(xsh_xs_3* p_xs_3,
02005 xsh_instrument* instr,
02006 const char * wtag,
02007 const char * stag,
02008 cpl_frame** wmap_frame,
02009 cpl_frame** smap_frame,const int save_tmp)
02010
02011 {
02012 int morder_cnt=0;
02013 DOUBLE lambda;
02014 DOUBLE lambda_nm;
02015 DOUBLE blaze_wav, lam_min, lam_max, lam_inc;
02016 XSH_ARM arm = XSH_ARM_UNDEFINED;
02017 XSH_MODE mode = XSH_MODE_UNDEFINED;
02018 long naxes[2];
02019 int ii,jj,binx,biny;
02020 int slitlet,slitlet_min,slitlet_max;
02021 double es_x_config;
02022 double slit_min,slit_max,slit_inc,slit_x,slit_y;
02023 double* ccd_wav;
02024 double* ccd_slit;
02025 double* ccd_mask;
02026 int* ccd_imap;
02027 cpl_image* ccd_wave_ima=NULL;
02028 cpl_image* ccd_slit_ima=NULL;
02029 cpl_image* ccd_mask_ima=NULL;
02030 cpl_image* ccd_imap_ima=NULL;
02031
02032 DOUBLE** ref_ind;
02033 double* oneD_AB=NULL;
02034 cpl_propertylist* plist=NULL;
02035 char filename[256];
02036 int array_size_fix=0;
02037
02038 int sx=0;
02039 int sy=0;
02040 int i=0;
02041 int j=0;
02042 double s_val=0;
02043 double w_val=0;
02044 int i_val=0;
02045 cpl_table* tab_xy=NULL;
02046 int size_x=0;
02047 int size_y=0;
02048
02049 XSH_ASSURE_NOT_NULL( p_xs_3);
02050 XSH_ASSURE_NOT_NULL( instr);
02051 XSH_ASSURE_NOT_NULL( wtag);
02052 XSH_ASSURE_NOT_NULL( stag);
02053
02054
02055 check(arm=xsh_instrument_get_arm(instr));
02056 check(mode=xsh_instrument_get_mode(instr));
02057 check(binx=xsh_instrument_get_binx(instr));
02058 check(biny=xsh_instrument_get_biny(instr));
02059
02060 xsh_msg("Generate physical model based wave and slit maps");
02061
02062
02063 if (mode==XSH_MODE_IFU) {
02064 slitlet_min=-1;
02065 slitlet_max=1;
02066 slit_min=IFU_LOW+0.000001;
02067 slit_max=IFU_HI-0.000001;
02068 slit_inc=(slit_max-slit_min)/(20.0/(double)(binx));
02069 }
02070 else {
02071 slitlet_min=0;
02072 slitlet_max=0;
02073 slit_min=(-p_xs_3->es_s/2.0)/p_xs_3->slit_scale;
02074 slit_max=(p_xs_3->es_s/2.0)/p_xs_3->slit_scale;
02075 slit_inc=(slit_max-slit_min)/(50.0/(double)(binx));
02076 }
02077
02078
02079
02080
02081
02082
02083
02084 ref_ind=xsh_alloc2Darray(8,7);
02085
02086 p_xs_3->arm= arm;
02087 if (arm == XSH_ARM_UVB) {
02088 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
02089 array_size_fix=0;
02090 }
02091 else if (arm== XSH_ARM_VIS) {
02092 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
02093 array_size_fix=0;
02094 }
02095 else {
02096 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
02097 array_size_fix=NIR_FIX;
02098 }
02099
02100
02101
02102
02103
02104
02105 xsh_3_init(p_xs_3);
02106 es_x_config=p_xs_3->es_x;
02107
02108 size_x=p_xs_3->ASIZE+array_size_fix;
02109 size_y=p_xs_3->BSIZE;
02110
02111
02112
02113
02114
02115
02116
02117 ccd_wave_ima=cpl_image_new(size_x,size_y,CPL_TYPE_DOUBLE);
02118 ccd_slit_ima=cpl_image_new(size_x,size_y,CPL_TYPE_DOUBLE);
02119 ccd_mask_ima=cpl_image_new(size_x,size_y,CPL_TYPE_DOUBLE);
02120 ccd_imap_ima=cpl_image_new(size_x,size_y,CPL_TYPE_INT);
02121
02122 ccd_wav=cpl_image_get_data_double(ccd_wave_ima);
02123 ccd_slit=cpl_image_get_data_double(ccd_slit_ima);
02124 ccd_mask=cpl_image_get_data_double(ccd_mask_ima);
02125 ccd_imap=cpl_image_get_data_int(ccd_imap_ima);
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135 int fit_size=0;
02136 cpl_polynomial * fit_wave_2d=NULL;
02137 cpl_polynomial * fit_slit_2d=NULL;
02138 cpl_matrix * mat_xy_sampling_points=NULL;
02139 cpl_vector * vec_w_sampling_points=NULL;
02140 cpl_vector * vec_s_sampling_points=NULL;
02141 const cpl_size maxdeg2d = 5;
02142 const cpl_size maxdeg2ds = 5;
02143
02144 double x_ord_min=9999;
02145 double x_ord_max=-1;
02146 double y_ord_min=9999;
02147 double y_ord_max=-1;
02148
02149 cpl_vector* val = cpl_vector_new(2);
02150 double* pval = cpl_vector_get_data(val);
02151 cpl_error_code error2d;
02152 double x_val=0;
02153 double y_val=0;
02154 int x_det=0;
02155 int y_det=0;
02156 int x_min=0;
02157 int y_min=0;
02158 int fit_index=0;
02159 int x_max=0;
02160 int y_max=0;
02161 int ord=0;
02162
02163
02164 for (morder_cnt=p_xs_3->morder_min; morder_cnt<=p_xs_3->morder_max; morder_cnt+=1) {
02165
02166
02167
02168 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
02169 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
02170 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
02171 lam_inc=(lam_max-lam_min)/(0.5*size_y);
02172
02173 xsh_free_table(&tab_xy);
02174 xsh_free_matrix(&mat_xy_sampling_points);
02175 xsh_free_vector(&vec_w_sampling_points);
02176 xsh_free_vector(&vec_s_sampling_points);
02177
02178 for (slitlet=slitlet_min;slitlet<slitlet_max+1;slitlet++) {
02179
02180
02181 fit_index=0;
02182 x_ord_min=9999;
02183 x_ord_max=-1;
02184 y_ord_min=9999;
02185 y_ord_max=-1;
02186
02187 if (slitlet==-1) {
02188 slit_x=(IFU_LEFT_MIN+IFU_LEFT_MAX)/2.0;
02189 i_val=1;
02190 }
02191 else if (slitlet==1) {
02192 slit_x=(IFU_RIGHT_MIN+IFU_RIGHT_MAX)/2.0;
02193 i_val=3;
02194 }
02195 else {
02196 slit_x=(IFU_CEN_MIN+IFU_CEN_MAX)/2.0;
02197 i_val=2;
02198 }
02199
02200 for (slit_y=slit_min;slit_y<=slit_max;slit_y+=slit_inc) {
02201
02202 if (mode==XSH_MODE_IFU) {
02203 if (xsh_model_map_ifu(slit_x,slit_y,p_xs_3)!=0) {
02204 cpl_msg_error(__func__, "Outside slitlet array");
02205 return CPL_ERROR_UNSPECIFIED;
02206 }
02207 }
02208 else {
02209
02210 p_xs_3->es_y_tot=slit_y*p_xs_3->slit_scale+p_xs_3->es_y;
02211 }
02212
02213
02214 for (lambda=lam_min-p_xs_3->blaze_pad; lambda<=lam_max+p_xs_3->blaze_pad; lambda+=lam_inc) {
02215
02216 lambda_nm=lambda*mm2nm;
02217 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02218 xsh_3_detpix(p_xs_3);
02219 x_val=p_xs_3->xpospix;
02220 y_val=p_xs_3->ypospix;
02221
02222
02223 if (p_xs_3->chippix[0]==1) {
02224
02225 if (p_xs_3->chippix[1]>=1 && p_xs_3->chippix[1]<size_x+1 &&
02226 p_xs_3->chippix[2]>=1 && p_xs_3->chippix[2]<size_y+1) {
02227
02228
02229 fit_index+=1;
02230
02231 }
02232
02233
02234 }
02235
02236 }
02237
02238 }
02239
02240 fit_size=fit_index;
02241
02242 mat_xy_sampling_points=cpl_matrix_new(2,fit_size);
02243 vec_w_sampling_points=cpl_vector_new(fit_size);
02244 vec_s_sampling_points=cpl_vector_new(fit_size);
02245
02246 tab_xy=cpl_table_new(fit_size);
02247 cpl_table_new_column(tab_xy,"X",CPL_TYPE_DOUBLE);
02248 cpl_table_new_column(tab_xy,"Y",CPL_TYPE_DOUBLE);
02249 cpl_table_new_column(tab_xy,"W",CPL_TYPE_DOUBLE);
02250 cpl_table_new_column(tab_xy,"S",CPL_TYPE_DOUBLE);
02251 cpl_table_new_column(tab_xy,"ord",CPL_TYPE_INT);
02252
02253 cpl_table_fill_column_window_int(tab_xy,"ord",0,fit_size,0);
02254 cpl_table_fill_column_window_double(tab_xy,"X",0,fit_size,0);
02255 cpl_table_fill_column_window_double(tab_xy,"Y",0,fit_size,0);
02256 cpl_table_fill_column_window_double(tab_xy,"W",0,fit_size,0);
02257 cpl_table_fill_column_window_double(tab_xy,"S",0,fit_size,0);
02258
02259 ord+=1;
02260
02261 double* pxy=NULL;
02262 double* pw=NULL;
02263 double* ps=NULL;
02264 int* pto=NULL;
02265 double* ptx=NULL;
02266 double* pty=NULL;
02267 double* pts=NULL;
02268 double* ptw=NULL;
02269
02270 pxy=cpl_matrix_get_data(mat_xy_sampling_points);
02271 pw=cpl_vector_get_data(vec_w_sampling_points);
02272 ps=cpl_vector_get_data(vec_s_sampling_points);
02273
02274 ptx=cpl_table_get_data_double(tab_xy,"X");
02275 pty=cpl_table_get_data_double(tab_xy,"Y");
02276 pts=cpl_table_get_data_double(tab_xy,"S");
02277 ptw=cpl_table_get_data_double(tab_xy,"W");
02278 pto=cpl_table_get_data_int(tab_xy,"ord");
02279 int fit_index=0;
02280
02281
02282 x_ord_min=9999;
02283 x_ord_max=-1;
02284 y_ord_min=9999;
02285 y_ord_max=-1;
02286
02287
02288
02289 for (slit_y=slit_min;slit_y<=slit_max;slit_y+=slit_inc) {
02290
02291 if (mode==XSH_MODE_IFU) {
02292 if (xsh_model_map_ifu(slit_x,slit_y,p_xs_3)!=0) {
02293 cpl_msg_error(__func__, "Outside slitlet array");
02294 return CPL_ERROR_UNSPECIFIED;
02295 }
02296 }
02297 else {
02298
02299 p_xs_3->es_y_tot=slit_y*p_xs_3->slit_scale+p_xs_3->es_y;
02300 }
02301
02302
02303 for (lambda=lam_min-p_xs_3->blaze_pad; lambda<=lam_max+p_xs_3->blaze_pad; lambda+=lam_inc) {
02304
02305 lambda_nm=lambda*mm2nm;
02306 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02307 xsh_3_detpix(p_xs_3);
02308 x_val=p_xs_3->xpospix;
02309 y_val=p_xs_3->ypospix;
02310
02311
02312 if (p_xs_3->chippix[0]==1) {
02313
02314 if (p_xs_3->chippix[1]>=1 && p_xs_3->chippix[1]<size_x+1 &&
02315 p_xs_3->chippix[2]>=1 && p_xs_3->chippix[2]<size_y+1) {
02316
02317
02318 pw[fit_index]=lambda_nm;
02319 ps[fit_index]=slit_y;
02320 check(cpl_matrix_set(mat_xy_sampling_points,0,fit_index,x_val));
02321 check(cpl_matrix_set(mat_xy_sampling_points,1,fit_index,y_val));
02322 pto[fit_index]=morder_cnt;
02323 ptx[fit_index]=x_val;
02324 pty[fit_index]=y_val;
02325 pts[fit_index]=slit_y;
02326 ptw[fit_index]=lambda_nm;
02327
02328 x_ord_min=(x_ord_min<x_val)? x_ord_min:x_val;
02329 x_ord_max=(x_ord_max>x_val)? x_ord_max:x_val;
02330
02331 y_ord_min=(y_ord_min<y_val)? y_ord_min:y_val;
02332 y_ord_max=(y_ord_max>y_val)? y_ord_max:y_val;
02333
02334 fit_index+=1;
02335
02336 }
02337
02338
02339 }
02340
02341 }
02342
02343 }
02344 xsh_free_polynomial(&fit_wave_2d);
02345 xsh_free_polynomial(&fit_slit_2d);
02346 fit_wave_2d=cpl_polynomial_new(2);
02347 fit_slit_2d=cpl_polynomial_new(2);
02348 sprintf(filename,"tab_xy_%2.2d.fits",morder_cnt);
02349
02350
02351 xsh_msg_dbg_low("SIZES vec=%" CPL_SIZE_FORMAT " ncol=%" CPL_SIZE_FORMAT " nrow=%" CPL_SIZE_FORMAT " pol size=%" CPL_SIZE_FORMAT "",
02352 cpl_vector_get_size(vec_w_sampling_points),
02353 cpl_matrix_get_ncol(mat_xy_sampling_points),
02354 cpl_matrix_get_nrow(mat_xy_sampling_points),
02355 cpl_polynomial_get_dimension(fit_wave_2d));
02356
02357 check(error2d=cpl_polynomial_fit(fit_wave_2d,mat_xy_sampling_points,
02358 NULL,vec_w_sampling_points, NULL,
02359 CPL_FALSE,NULL, &maxdeg2d));
02360
02361 check(error2d=cpl_polynomial_fit(fit_slit_2d,mat_xy_sampling_points,
02362 NULL,vec_s_sampling_points, NULL,
02363 CPL_FALSE,NULL, &maxdeg2ds));
02364
02365 sx=(int)(x_ord_max-x_ord_min+0.5);
02366 sy=size_y;
02367
02368 x_min=(int)(x_ord_min+0.5);
02369 y_min=(int)(y_ord_min+0.5);
02370 x_max=(int)(x_ord_max+0.5);
02371 y_max=(int)(y_ord_max+0.5);
02372
02373
02374
02375
02376
02377 x_det=x_min;
02378 y_det=0;
02379 for(jj=y_min;jj<=y_max;jj++) {
02380 for(ii=x_min;ii<=x_max;ii++) {
02381 ccd_mask[jj*size_x+ii]=1;
02382 }
02383 }
02384
02385 for(j=0;j<sy;j++) {
02386
02387
02388
02389 pval[1] = y_det + 1;
02390 x_det=x_min;
02391 for(i=0;i<sx;i++) {
02392
02393 pval[0] = x_det + 1;
02394
02395 check(w_val=cpl_polynomial_eval(fit_wave_2d,val ));
02396 check(s_val=cpl_polynomial_eval(fit_slit_2d,val ));
02397
02398 if ((s_val > slit_min && s_val < slit_max) &&
02399 (ccd_mask[y_det*size_x+x_det]==1) ) {
02400 ccd_wav[y_det*size_x+x_det]=w_val;
02401 ccd_slit[y_det*size_x+x_det]=s_val;
02402 ccd_imap[y_det*size_x+x_det]=i_val;
02403 }
02404 x_det+=1;
02405 }
02406 y_det+=1;
02407 }
02408
02409
02410 for (jj=0;jj<size_y;jj++) {
02411 for (ii=0;ii<size_x;ii++) {
02412
02413
02414
02415 ccd_mask[jj*size_x+ii]=0;
02416 }
02417 }
02418
02419 }
02420
02421 }
02422
02423 if ((xsh_free2Darray(ref_ind,8))!=0) {
02424 cpl_msg_error(__func__, "Cannot free 2D array ref_ind");
02425 return CPL_ERROR_UNSPECIFIED;
02426 }
02427
02428 naxes[0]=size_x;
02429 naxes[1]=size_y;
02430
02431
02432
02433
02434
02435
02436
02437
02438
02439
02440
02441
02442
02443
02444
02445
02446 check(plist=cpl_propertylist_new());
02447 sprintf(filename,"%s.fits",wtag);
02448 check(xsh_pfits_set_pcatg(plist, wtag ) ) ;
02449 check(cpl_image_save(ccd_wave_ima,filename,CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
02450 xsh_free_propertylist(&plist);
02451
02452
02453 if(save_tmp) {
02454 xsh_add_temporary_file(filename);
02455 }
02456 *wmap_frame=xsh_frame_product(filename,wtag,CPL_FRAME_TYPE_IMAGE,
02457 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
02458
02459 xsh_msg(" wave map frame %s %s",
02460 cpl_frame_get_filename(*wmap_frame),cpl_frame_get_tag(*wmap_frame));
02461
02462
02463
02464
02465
02466
02467
02468 naxes[0]=size_x;
02469 naxes[1]=size_y;
02470
02471
02472
02473
02474
02475
02476
02477
02478
02479
02480
02481
02482 check(plist=cpl_propertylist_new());
02483 sprintf(filename,"%s.fits",stag);
02484 check(xsh_pfits_set_pcatg(plist, stag ) ) ;
02485 xsh_model_compute_slitmap_kw(slit_min,slit_max,p_xs_3,instr,&plist);
02486 check(cpl_image_save(ccd_slit_ima,filename,CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
02487 if (mode==XSH_MODE_IFU) {
02488 check(cpl_image_save(ccd_imap_ima,filename,CPL_BPP_32_SIGNED,NULL,CPL_IO_EXTEND));
02489 }
02490 xsh_free_propertylist(&plist);
02491
02492
02493 if(save_tmp) {
02494 xsh_add_temporary_file(filename);
02495 }
02496 *smap_frame=xsh_frame_product(filename,stag,CPL_FRAME_TYPE_IMAGE,
02497 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
02498 xsh_msg(" slit map frame %s %s",
02499 cpl_frame_get_filename(*smap_frame),cpl_frame_get_tag(*smap_frame));
02500
02501 cpl_free(oneD_AB);
02502
02503
02504
02505
02506
02507 cleanup:
02508
02509 xsh_free_matrix(&mat_xy_sampling_points);
02510 xsh_free_vector(&vec_w_sampling_points);
02511 xsh_free_vector(&vec_s_sampling_points);
02512 xsh_free_vector(&val);
02513 xsh_free_polynomial(&fit_wave_2d);
02514 xsh_free_polynomial(&fit_slit_2d);
02515 xsh_free_table(&tab_xy);
02516 xsh_free_image(&ccd_wave_ima);
02517 xsh_free_image(&ccd_slit_ima);
02518 xsh_free_image(&ccd_mask_ima);
02519 xsh_free_image(&ccd_imap_ima);
02520
02521
02522
02523
02524
02525
02526
02527 xsh_free_propertylist(&plist);
02528
02529 return cpl_error_get_code();
02530 }
02531
02532
02544
02545 int
02546 xsh_model_map_ifu(double xifu,
02547 double yifu,
02548 xsh_xs_3* p_xs_3)
02549 {
02550 double xifu_mm, yifu_mm,es_x_tot;
02551 xifu_mm=xifu*IFU_SCALE;
02552 yifu_mm=yifu*IFU_SCALE;
02553
02554 if (xifu>IFU_LEFT_MIN && xifu<IFU_LEFT_MAX && yifu>IFU_LOW && yifu<IFU_HI) {
02555 p_xs_3->es_y_tot=(IFU_MAP_LEFT_C0Y+
02556 IFU_MAP_LEFT_C1Y*yifu_mm+
02557 IFU_MAP_LEFT_C2Y*yifu_mm*yifu_mm);
02558 es_x_tot=(IFU_MAP_LEFT_C0X+
02559 IFU_MAP_LEFT_C1X*xifu_mm+
02560 IFU_MAP_LEFT_C2X*xifu_mm*xifu_mm);
02561 }
02562 else if (xifu>IFU_CEN_MIN && xifu<IFU_CEN_MAX && yifu>IFU_LOW && yifu<IFU_HI) {
02563 p_xs_3->es_y_tot=(IFU_MAP_CEN_C0Y+
02564 IFU_MAP_CEN_C1Y*yifu_mm+
02565 IFU_MAP_CEN_C2Y*yifu_mm*yifu_mm);
02566 es_x_tot=(IFU_MAP_CEN_C0X+
02567 IFU_MAP_CEN_C1X*xifu_mm+
02568 IFU_MAP_CEN_C2X*xifu_mm*xifu_mm);
02569 }
02570 else if (xifu>IFU_RIGHT_MIN && xifu<IFU_RIGHT_MAX && yifu>IFU_LOW && yifu<IFU_HI) {
02571 p_xs_3->es_y_tot=(IFU_MAP_RIGHT_C0Y+
02572 IFU_MAP_RIGHT_C1Y*yifu_mm+
02573 IFU_MAP_RIGHT_C2Y*yifu_mm*yifu_mm);
02574 es_x_tot=(IFU_MAP_RIGHT_C0X+
02575 IFU_MAP_RIGHT_C1X*xifu_mm
02576 +IFU_MAP_RIGHT_C2X*xifu_mm*xifu_mm);
02577 }
02578 else {
02579 p_xs_3->es_x=-999;
02580 p_xs_3->es_y=-999;
02581 return 1;
02582 }
02583
02584 p_xs_3->es_y_tot= p_xs_3->es_y+p_xs_3->es_y_tot*p_xs_3->slit_scale/IFU_SCALE;
02585 p_xs_3->es_x = p_xs_3->es_x+es_x_tot*p_xs_3->slit_scale/IFU_SCALE;
02586
02587 return 0;
02588 }
02589
02590
02603
02604 cpl_vector**
02605 xsh_model_locus(struct xs_3* p_xs_3,
02606 xsh_instrument* instr,
02607 double ent_slit_pos)
02608 {
02609 int n_order, morder_cnt;
02610 DOUBLE lambda;
02611 DOUBLE lambda_nm;
02612 DOUBLE blaze_wav, lam_min, lam_max;
02613 DOUBLE pixwavlast, pixylast, pixxlast;
02614 int chipdist_current;
02615 double* trace_lam=NULL;
02616 double* trace_xdisp=NULL;
02617 int* trace_mm=NULL;
02618 cpl_vector** loci=NULL ;
02619 FILE* trace_out;
02620 int ii;
02621 DOUBLE** ref_ind=NULL;
02622 XSH_ARM arm = XSH_ARM_UNDEFINED;
02623
02624 XSH_ASSURE_NOT_NULL( p_xs_3);
02625 XSH_ASSURE_NOT_NULL( instr);
02626
02627
02628
02629
02630
02631
02632
02633 ref_ind=xsh_alloc2Darray(8,7);
02634
02635 check( arm = xsh_instrument_get_arm( instr));
02636 p_xs_3->arm=arm;
02637 if (arm==XSH_ARM_UVB) {
02638 xsh_ref_ind_read(0,ref_ind,290.0);
02639 }
02640 else if (arm==XSH_ARM_VIS) {
02641 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
02642 }
02643 else {
02644 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
02645 }
02646 n_order=16;
02647
02648
02649
02650
02651
02652
02653
02654
02655 xsh_3_init(p_xs_3);
02656
02657 trace_out=fopen("trace.dat","w");
02658 if ((trace_lam=xsh_alloc1Darray(p_xs_3->SIZE))==NULL){
02659 cpl_msg_error(__func__, "Cannot allocate 2D array");
02660 return NULL;
02661 }
02662 if ((trace_xdisp=xsh_alloc1Darray(p_xs_3->SIZE))==NULL){
02663 cpl_msg_error(__func__, "Cannot allocate 2D array");
02664 return NULL;
02665 }
02666 if ((trace_mm=xsh_alloc1Darray_INT(p_xs_3->SIZE))==NULL){
02667 cpl_msg_error(__func__, "Cannot allocate 2D array");
02668 return NULL;
02669 }
02670
02671
02672 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->slit_scale*ent_slit_pos;
02673
02674
02675 if ((loci=cpl_malloc(n_order*sizeof(cpl_vector *)))==NULL) {
02676 cpl_msg_error(__func__, "Cannot allocate loci array");
02677 return NULL;
02678 }
02679 for (ii=0; ii<n_order; ii++) {
02680 loci[ii] = cpl_vector_new(p_xs_3->SIZE) ;
02681 cpl_vector_fill(loci[ii], 0.0) ;
02682 }
02683 for (morder_cnt=p_xs_3->morder_min; morder_cnt<=p_xs_3->morder_max; morder_cnt+=1) {
02684 for (ii=0;ii<p_xs_3->SIZE;ii+=1) {
02685 trace_mm[ii]=0;
02686 trace_lam[ii]=0.0;
02687 trace_xdisp[ii]=0.0;
02688 }
02689 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
02690 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
02691 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
02692 if (arm==XSH_ARM_NIR) {
02693 chipdist_current=3000;
02694 }
02695 else {
02696 chipdist_current=0;
02697 }
02698 pixxlast=0.0;
02699 pixylast=0.0;
02700 pixwavlast=mm2nm*(lam_min-p_xs_3->blaze_pad);
02701 for (lambda=lam_min-p_xs_3->blaze_pad; lambda<=lam_max+p_xs_3->blaze_pad;
02702 lambda+=(lam_max-lam_min)/10000.0) {
02703 lambda_nm=mm2nm*lambda;
02704 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
02705 xsh_3_detpix(p_xs_3);
02706 if (p_xs_3->chippix[0]==1) {
02707 if (p_xs_3->chippix[1]>=1 &&
02708 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
02709 p_xs_3->chippix[2]>=1 &&
02710 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
02711
02712
02713
02714
02715
02716
02717
02718
02719
02720
02721
02722
02723
02724 if ((arm==XSH_ARM_NIR && p_xs_3->ypospix<(double)(chipdist_current)-0.5) || (arm!=XSH_ARM_NIR && p_xs_3->ypospix>(double)(chipdist_current)-0.5)) {
02725 trace_lam[p_xs_3->chippix[2]-1]=pixwavlast+(lambda_nm-pixwavlast)*
02726 ((double)(chipdist_current)-0.5-pixylast)/(p_xs_3->ypospix-pixylast);
02727 trace_xdisp[p_xs_3->chippix[2]-1]=pixxlast+(p_xs_3->xpospix-pixxlast)*
02728 ((double)(chipdist_current)-0.5-pixylast)/(p_xs_3->ypospix-pixylast);
02729 trace_mm[p_xs_3->chippix[2]-1]=morder_cnt;
02730 chipdist_current=p_xs_3->chippix[2]+1;
02731 }
02732
02733 pixxlast=p_xs_3->xpospix;
02734 pixylast=p_xs_3->ypospix;
02735 }
02736 }
02737 pixwavlast=lambda_nm;
02738 }
02739 for (ii=0;ii<p_xs_3->SIZE;ii+=1) {
02740 fprintf(trace_out,"%d %lf %lf %d\n", trace_mm[ii], trace_lam[ii], trace_xdisp[ii], ii);
02741
02742 cpl_vector_set(loci[morder_cnt-p_xs_3->morder_min], ii, trace_xdisp[ii]) ;
02743 }
02744 }
02745
02746 cpl_free(trace_lam);
02747 cpl_free(trace_xdisp);
02748 cpl_free(trace_mm);
02749 fclose(trace_out);
02750
02751 if ((xsh_free2Darray(ref_ind,8))!=0) {
02752 cpl_msg_error(__func__, "Cannot free 2D array ref_ind");
02753 return NULL;
02754 }
02755 cleanup:
02756 return loci;
02757 }
02758
02759
02774
02775 void
02776 xsh_model_get_xy( xsh_xs_3* p_xs_3,
02777 xsh_instrument* instr,
02778 double lambda_nm,
02779 int morder,
02780 double ent_slit_pos,
02781 double* x,
02782 double* y)
02783 {
02784 DOUBLE lambda=0;
02785
02786 DOUBLE** ref_ind = NULL;
02787 XSH_ARM arm = XSH_ARM_UNDEFINED;
02788
02789
02790 XSH_ASSURE_NOT_NULL( p_xs_3);
02791 XSH_ASSURE_NOT_NULL( instr);
02792 XSH_ASSURE_NOT_NULL( x);
02793 XSH_ASSURE_NOT_NULL( y);
02794
02795 check( arm = xsh_instrument_get_arm( instr));
02796
02797 p_xs_3->arm=arm;
02798 lambda=lambda_nm/mm2nm;
02799
02800
02801
02802
02803
02804
02805 ref_ind=xsh_alloc2Darray(8,7);
02806
02807 if ( arm== XSH_ARM_UVB) {
02808 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
02809 }
02810 else if ( arm == XSH_ARM_VIS) {
02811 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
02812 }
02813 else {
02814 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
02815 }
02816
02817
02818
02819
02820
02821 xsh_3_init(p_xs_3);
02822
02823
02824 p_xs_3->es_y_tot=p_xs_3->es_y+ent_slit_pos*p_xs_3->slit_scale;
02825
02826
02827
02828 xsh_3_eval(lambda,morder,ref_ind,p_xs_3);
02829 xsh_3_detpix(p_xs_3);
02830
02831 *x = p_xs_3->xpospix;
02832 *y = p_xs_3->ypospix;
02833
02834
02835 cleanup:
02836 if (ref_ind != NULL){
02837 xsh_free2Darray(ref_ind,8);
02838 }
02839 return;
02840 }
02841
02848 void
02849 xsh_model_binxy(xsh_xs_3* p_xs_3,
02850 int bin_X,
02851 int bin_Y)
02852 {
02853 XSH_INSTRCONFIG* instr_config=NULL;
02854 xsh_instrument* instr = NULL;
02855 if (bin_X!=1 || bin_Y!=1) {
02856 instr = xsh_instrument_new();
02857
02858 if (p_xs_3->arm==0) {
02859 xsh_instrument_set_arm(instr, XSH_ARM_UVB);
02860 cpl_msg_info(__func__,"Setting %d x %d binning for UVB arm",bin_X,bin_Y);
02861 p_xs_3->xsize_corr=UVB_xsize_corr;
02862 p_xs_3->ysize_corr=UVB_ysize_corr;
02863 }
02864 else if (p_xs_3->arm==1) {
02865 xsh_instrument_set_arm(instr, XSH_ARM_VIS);
02866 cpl_msg_info(__func__,"Setting %d x %d binning for VIS arm",bin_X,bin_Y);
02867 p_xs_3->xsize_corr=VIS_xsize_corr;
02868 p_xs_3->ysize_corr=VIS_ysize_corr;
02869 }
02870 else {
02871 xsh_instrument_set_arm(instr, XSH_ARM_NIR);
02872 cpl_msg_warning(__func__,"NIR arm does not support binned data");
02873 p_xs_3->xsize_corr=NIR_xsize_corr;
02874 p_xs_3->ysize_corr=NIR_ysize_corr;
02875 bin_X=1;
02876 bin_Y=1;
02877 }
02878 instr_config=xsh_instrument_get_config(instr);
02879 p_xs_3->pix_X=p_xs_3->pix*(float)(bin_X);
02880 p_xs_3->pix_Y=p_xs_3->pix*(float)(bin_Y);
02881 p_xs_3->ASIZE=instr_config->nx;
02882 p_xs_3->ASIZE/=bin_X;
02883 p_xs_3->BSIZE=instr_config->ny;
02884 p_xs_3->BSIZE/=bin_Y;
02885 p_xs_3->SIZE=instr_config->ny;
02886 p_xs_3->SIZE/=bin_Y;
02887 p_xs_3->chipxpix=(float)(instr_config->nx);
02888 p_xs_3->chipxpix/=(float)(bin_X);
02889 p_xs_3->chipypix=(float)(instr_config->ny);
02890 p_xs_3->chipypix/=(float)(bin_Y);
02891 p_xs_3->xsize_corr/=(float)(bin_X);
02892 p_xs_3->ysize_corr/=(float)(bin_Y);
02893
02894
02895 xsh_instrument_free(&instr);
02896 }
02897 return;
02898 }
02899
02906 cpl_frame*
02907 xsh_model_spectralformat_create(xsh_xs_3* p_xs_3,
02908 const char* tab_filename)
02909 {
02910 int morder_cnt=0;
02911 int tab_size=0;
02912 int blaze_flag=0;
02913 int cen_flag=0;
02914 int edge_flag;
02915 DOUBLE blaze_min=0.2;
02916 DOUBLE buff=0.0;
02917
02918 DOUBLE lambda=0;
02919 DOUBLE lambda_nm=0;
02920 DOUBLE blaze_wav=0;
02921
02922 int fsr_flag=0;
02923 DOUBLE baseline=0.0;
02924 DOUBLE binlfsr=0.0;
02925 DOUBLE binufsr=0.0;
02926 DOUBLE binblaze=0.0;
02927 DOUBLE lam_low=0.0;
02928 DOUBLE lam_hi=0.0;
02929 DOUBLE x_low=0.0;
02930 DOUBLE x_hi=0.0;
02931
02932 DOUBLE blaze_eff=0.0;
02933
02934 DOUBLE lfsr=0;
02935 DOUBLE ufsr=0;
02936 DOUBLE fsr=0;
02937 DOUBLE blaze=0;
02938 DOUBLE p_dif=0;
02939 DOUBLE wlmin=0;
02940 DOUBLE wlmax=0;
02941 DOUBLE wlmin_full=0;
02942 DOUBLE wlmax_full=0;
02943 DOUBLE sinc_arg=0;
02944 DOUBLE wlmean=0;
02945 DOUBLE wlcen=0;
02946 DOUBLE band=0;
02947
02948 int xmin=0;
02949 int blz_frac_xmin=0;
02950 int blz_frac_ymin=0;
02951 int xmax=0;
02952 int blz_frac_xmax=0;
02953 int blz_frac_ymax=0;
02954 int yc=0;
02955 int disp_coord=0;
02956 int extra_ord=0;
02957
02958 cpl_table* sf_tab = NULL;
02959 cpl_table* sf_tab2 = NULL;
02960 cpl_frame* sf_frame=NULL;
02961 cpl_frame* sf_frame2=NULL;
02962 cpl_propertylist *sf_plist = NULL;
02963 cpl_propertylist *x_plist = NULL;
02964
02965 xsh_instrument* inst=NULL;
02966 DOUBLE** ref_ind = NULL;
02967 const char* tag=NULL;
02968 const char* tag2=NULL;
02969 char tab_filename2[256];
02970
02971 ref_ind=xsh_alloc2Darray(8,7);
02972
02973 XSH_ASSURE_NOT_NULL( p_xs_3);
02974 XSH_ASSURE_NOT_NULL( tab_filename);
02975
02976 if (strcmp(tab_filename,"spec_form.fits")==0) {
02977 extra_ord=3;
02978 }
02979 else {
02980 extra_ord=0;
02981 }
02982 sprintf(tab_filename2,"long_%s",tab_filename);
02983
02984 check(inst=xsh_instrument_new());
02985 if (p_xs_3->arm==0) {
02986 xsh_instrument_set_arm(inst,XSH_ARM_UVB);
02987 tag="SPEC_FORM_UVB";
02988 tag2="SPECTRAL_FORMAT_TAB_UVB";
02989 buff=0.007;
02990 baseline=0.00000025;
02991 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
02992 disp_coord=2;
02993 }
02994 else if (p_xs_3->arm==1) {
02995 xsh_instrument_set_arm(inst,XSH_ARM_VIS);
02996 tag="SPEC_FORM_VIS";
02997 tag2="SPECTRAL_FORMAT_TAB_VIS";
02998 buff=0.007;
02999 baseline=0.0000005;
03000 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
03001 disp_coord=2;
03002 }
03003 else if (p_xs_3->arm==2) {
03004 xsh_instrument_set_arm(inst,XSH_ARM_NIR);
03005 tag="SPEC_FORM_NIR";
03006 tag2="SPECTRAL_FORMAT_TAB_NIR";
03007 buff=0.007;
03008 baseline=0.000001;
03009 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
03010 disp_coord=2;
03011
03012 }
03013
03014 check(sf_frame=xsh_frame_product(tab_filename,
03015 tag,
03016 CPL_FRAME_TYPE_TABLE,
03017 CPL_FRAME_GROUP_PRODUCT,
03018 CPL_FRAME_LEVEL_FINAL));
03019
03020
03021
03022
03023
03024
03025
03026
03027 tab_size= p_xs_3->morder_max-p_xs_3->morder_min+1;
03028 check(sf_tab = cpl_table_new(tab_size));
03029 check(cpl_table_new_column(sf_tab, "ORDER", CPL_TYPE_INT) );
03030 check(cpl_table_new_column(sf_tab, "LAMP", CPL_TYPE_STRING) );
03031 check(cpl_table_new_column(sf_tab, "WLMIN", CPL_TYPE_FLOAT) );
03032 check(cpl_table_new_column(sf_tab, "WLMAX", CPL_TYPE_FLOAT) );
03033 check(cpl_table_new_column(sf_tab, "DISP_MIN", CPL_TYPE_FLOAT) );
03034 check(cpl_table_new_column(sf_tab, "DISP_MAX", CPL_TYPE_FLOAT) );
03035 check(cpl_table_new_column(sf_tab, "LFSR", CPL_TYPE_FLOAT) );
03036 check(cpl_table_new_column(sf_tab, "UFSR", CPL_TYPE_FLOAT) );
03037 check(cpl_table_new_column(sf_tab, "WLMINFUL", CPL_TYPE_FLOAT) );
03038 check(cpl_table_new_column(sf_tab, "WLMAXFUL", CPL_TYPE_FLOAT) );
03039
03040 tab_size= p_xs_3->morder_max-p_xs_3->morder_min+1+(2*extra_ord);
03041 check(sf_tab2 = cpl_table_new(tab_size));
03042 check(cpl_table_new_column(sf_tab2, "ORDER", CPL_TYPE_INT) );
03043 check(cpl_table_new_column(sf_tab2, "LAMP", CPL_TYPE_STRING) );
03044 check(cpl_table_new_column(sf_tab2, "WLMIN", CPL_TYPE_FLOAT) );
03045 check(cpl_table_new_column(sf_tab2, "WLEN0", CPL_TYPE_FLOAT) );
03046 check(cpl_table_new_column(sf_tab2, "WLMAX", CPL_TYPE_FLOAT) );
03047 check(cpl_table_new_column(sf_tab2, "XDISP_CEN", CPL_TYPE_INT) );
03048 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_WLMIN", CPL_TYPE_DOUBLE) );
03049 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_WLMEAN", CPL_TYPE_DOUBLE) );
03050 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_WLMAX", CPL_TYPE_DOUBLE) );
03051 check(cpl_table_new_column(sf_tab2, "WLCEN", CPL_TYPE_DOUBLE) );
03052 check(cpl_table_new_column(sf_tab2, "BAND", CPL_TYPE_FLOAT) );
03053 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_DISP_MIN", CPL_TYPE_INT) );
03054 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_DISP_MAX", CPL_TYPE_INT) );
03055 check(cpl_table_new_column(sf_tab2, "DISP_MIN", CPL_TYPE_FLOAT) );
03056 check(cpl_table_new_column(sf_tab2, "DISP_MAX", CPL_TYPE_FLOAT) );
03057 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_XDISP_MIN", CPL_TYPE_INT) );
03058 check(cpl_table_new_column(sf_tab2, "BLZ_FRAC_XDISP_MAX", CPL_TYPE_INT) );
03059 check(cpl_table_new_column(sf_tab2, "LFSR", CPL_TYPE_FLOAT) );
03060 check(cpl_table_new_column(sf_tab2, "BLAZE", CPL_TYPE_FLOAT) );
03061 check(cpl_table_new_column(sf_tab2, "UFSR", CPL_TYPE_FLOAT) );
03062 check(cpl_table_new_column(sf_tab2, "FSR", CPL_TYPE_FLOAT) );
03063 check(cpl_table_new_column(sf_tab2, "BIN_LFSR", CPL_TYPE_DOUBLE) );
03064 check(cpl_table_new_column(sf_tab2, "BIN_BLAZE", CPL_TYPE_DOUBLE) );
03065 check(cpl_table_new_column(sf_tab2, "BIN_UFSR", CPL_TYPE_DOUBLE) );
03066 check(cpl_table_new_column(sf_tab2, "WLMINFUL", CPL_TYPE_FLOAT) );
03067 check(cpl_table_new_column(sf_tab2, "WLMAXFUL", CPL_TYPE_FLOAT) );
03068
03069 for (morder_cnt=p_xs_3->morder_min-extra_ord;morder_cnt<=p_xs_3->morder_max+extra_ord;morder_cnt+=1) {
03070 blaze_wav=2000000.0*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
03071 ufsr=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
03072 lfsr=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
03073 fsr=ufsr-lfsr;
03074 edge_flag=0;
03075 blaze_flag=0;
03076 wlmax=0.0;
03077 wlmin=0.0;
03078 wlmax_full=0.0;
03079 wlmin_full=0.0;
03080 cen_flag=0;
03081 fsr_flag=0;
03082 binlfsr=0.0;
03083 binblaze=0.0;
03084 binufsr=0.0;
03085
03086 for (lambda_nm=lfsr-(1.5*fsr);lambda_nm<ufsr+(1.5*fsr);lambda_nm+=fsr/(3.0*p_xs_3->SIZE)) {
03087 xsh_3_init(p_xs_3);
03088
03089
03090 p_xs_3->es_y_tot=p_xs_3->es_y;
03091
03092 lambda=lambda_nm/mm2nm;
03093 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
03094 xsh_3_detpix(p_xs_3);
03095
03096 if (p_xs_3->chippix[0]==1) {
03097 if (p_xs_3->chippix[1]>=1 &&
03098 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
03099 p_xs_3->chippix[2]>=1 &&
03100 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
03101 if (edge_flag==0) {
03102 edge_flag=1;
03103 wlmin_full=lambda_nm;
03104 }
03105 p_dif=(sin(p_xs_3->grat_alpha-(-p_xs_3->nug))+sin(p_xs_3->grat_beta-(-p_xs_3->nug)));
03106 if (p_xs_3->grat_alpha>p_xs_3->grat_beta) {
03107 sinc_arg=(M_PI/(lambda*p_xs_3->sg))*(cos(p_xs_3->grat_alpha)/cos(p_xs_3->grat_alpha-(-p_xs_3->nug)))*p_dif;
03108 blaze=((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
03109 }
03110 else {
03111 sinc_arg=(M_PI/(lambda*p_xs_3->sg))*(cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha-(-p_xs_3->nug)))*p_dif;
03112 blaze=(cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha))*(cos(p_xs_3->grat_beta)/cos(p_xs_3->grat_alpha))*((sin(sinc_arg))/sinc_arg)*((sin(sinc_arg))/sinc_arg);
03113 }
03114
03115
03116
03117 if ((cen_flag==0 && p_xs_3->chippix[disp_coord]>=p_xs_3->SIZE/2
03118 && p_xs_3->arm<2) ||
03119 (cen_flag==0 && p_xs_3->chippix[disp_coord]<=p_xs_3->SIZE/2
03120 && p_xs_3->arm==2)) {
03121 yc=p_xs_3->chippix[3-disp_coord];
03122 wlcen=lambda_nm;
03123 cen_flag=1;
03124 }
03125 if (blaze_flag==0 && blaze>blaze_min) {
03126 wlmin=lambda_nm;
03127 blz_frac_xmin=p_xs_3->chippix[disp_coord];
03128 blz_frac_ymin=p_xs_3->chippix[3-disp_coord];
03129 blaze_flag=1;
03130 }
03131 if (blaze_flag==1 && blaze>blaze_min) {
03132 wlmax=lambda_nm;
03133 blz_frac_xmax=p_xs_3->chippix[disp_coord];
03134 blz_frac_ymax=p_xs_3->chippix[3-disp_coord];
03135 }
03136 if (blaze_flag==1 && blaze<blaze_min) {
03137 blaze_flag=2;
03138 }
03139 if (fsr_flag==0 && lambda_nm>lfsr*(1.0-buff)) {
03140 fsr_flag=1;
03141 xmin=p_xs_3->chippix[disp_coord];
03142 lam_low=lambda-baseline;
03143 xsh_3_eval(lam_low,morder_cnt,ref_ind,p_xs_3);
03144 xsh_3_detpix(p_xs_3);
03145 x_low=p_xs_3->ypospix;
03146 lam_hi=lambda+baseline;
03147 xsh_3_eval(lam_hi,morder_cnt,ref_ind,p_xs_3);
03148 xsh_3_detpix(p_xs_3);
03149 x_hi=p_xs_3->ypospix;
03150 if (x_low>0.0 && x_hi>0.0) {
03151 if (p_xs_3->arm==2) {
03152 binlfsr=((lam_hi-lam_low)/(x_low-x_hi))*mm2nm;
03153 }
03154 else {
03155 binlfsr=((lam_hi-lam_low)/(x_hi-x_low))*mm2nm;
03156 }
03157 }
03158 else {
03159 binlfsr=-1.0;
03160 }
03161 }
03162 if (fsr_flag==1 && lambda_nm>blaze_wav) {
03163 fsr_flag=2;
03164 blaze_eff=blaze;
03165 lam_low=lambda-baseline;
03166 xsh_3_eval(lam_low,morder_cnt,ref_ind,p_xs_3);
03167 xsh_3_detpix(p_xs_3);
03168 x_low=p_xs_3->ypospix;
03169 lam_hi=lambda+baseline;
03170 xsh_3_eval(lam_hi,morder_cnt,ref_ind,p_xs_3);
03171 xsh_3_detpix(p_xs_3);
03172 x_hi=p_xs_3->ypospix;
03173 if (x_low>0.0 && x_hi>0.0) {
03174 if (p_xs_3->arm==2) {
03175 binblaze=((lam_hi-lam_low)/(x_low-x_hi))*mm2nm;
03176 }
03177 else {
03178 binblaze=((lam_hi-lam_low)/(x_hi-x_low))*mm2nm;
03179 }
03180 }
03181 else {
03182 binblaze=-1.0;
03183 }
03184 }
03185 if (fsr_flag==2) {
03186 xmax=p_xs_3->chippix[disp_coord];
03187 if (lambda_nm>ufsr*(1.0+buff)) {
03188 fsr_flag=3;
03189 lam_low=lambda-baseline;
03190 xsh_3_eval(lam_low,morder_cnt,ref_ind,p_xs_3);
03191 xsh_3_detpix(p_xs_3);
03192 x_low=p_xs_3->ypospix;
03193 lam_hi=lambda+baseline;
03194 xsh_3_eval(lam_hi,morder_cnt,ref_ind,p_xs_3);
03195 xsh_3_detpix(p_xs_3);
03196 x_hi=p_xs_3->ypospix;
03197 if (x_low>0.0 && x_hi>0.0) {
03198 if (p_xs_3->arm==2) {
03199 binufsr=((lam_hi-lam_low)/(x_low-x_hi))*mm2nm;
03200 }
03201 else {
03202 binufsr=((lam_hi-lam_low)/(x_hi-x_low))*mm2nm;
03203 }
03204 }
03205 else {
03206 binufsr=-1.0;
03207 }
03208
03209 }
03210 }
03211
03212 }
03213 }
03214 else {
03215 if (edge_flag==1) {
03216 edge_flag=2;
03217 wlmax_full=lambda_nm;
03218 }
03219 }
03220 }
03221 if (blaze_flag>0) {
03222 wlmean=(wlmin+wlmax)/2.0;
03223 band=wlmax-wlmin;
03224 }
03225 else {
03226 band=-1.0;
03227 wlmin=-1.0;
03228 blz_frac_xmin=-1;
03229 blz_frac_ymin=-1;
03230 wlmean=-1.0;
03231 wlmax=-1.0;
03232 blz_frac_xmax=-1;
03233 blz_frac_ymax=-1;
03234 }
03235 if (cen_flag!=1) {
03236 wlcen=-1.0;
03237 yc=-1;
03238 }
03239 if (fsr_flag<3) {
03240 binufsr=-1.0;
03241 if (fsr_flag<2) {
03242 xmax=-1.0;
03243 binblaze=-1.0;
03244 if (fsr_flag<1) {
03245 binlfsr=-1.0;
03246 xmin=-1.0;
03247 }
03248 }
03249 }
03250
03251
03252 if (morder_cnt>=p_xs_3->morder_min && morder_cnt<=p_xs_3->morder_max) {
03253 check(cpl_table_set_int(sf_tab, "ORDER", morder_cnt-p_xs_3->morder_min,morder_cnt));
03254
03255
03256 check(cpl_table_set_float(sf_tab, "WLMIN", morder_cnt-p_xs_3->morder_min,(float)((1.0-buff)*lfsr)));
03257 check(cpl_table_set_float(sf_tab, "WLMAX", morder_cnt-p_xs_3->morder_min,(float)((1.0+buff)*ufsr)));
03258 check(cpl_table_set_float(sf_tab, "DISP_MIN", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(xmin)));
03259 check(cpl_table_set_float(sf_tab, "DISP_MAX", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(xmax)));
03260 check(cpl_table_set_float(sf_tab, "DISP_MIN", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(xmin)));
03261 check(cpl_table_set_float(sf_tab, "DISP_MAX", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(xmax)));
03262 check(cpl_table_set_float(sf_tab, "WLMINFUL", morder_cnt-p_xs_3->morder_min,(float)(wlmin_full)));
03263 check(cpl_table_set_float(sf_tab, "WLMAXFUL", morder_cnt-p_xs_3->morder_min,(float)(wlmax_full)));
03264 check(cpl_table_set_float(sf_tab, "LFSR", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(lfsr)));
03265 check(cpl_table_set_float(sf_tab, "UFSR", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(ufsr)));
03266 if (p_xs_3->arm==0 && morder_cnt>20) {
03267 check(cpl_table_set_string(sf_tab, "LAMP", morder_cnt-p_xs_3->morder_min,"D2"));
03268 }
03269 else {
03270 check(cpl_table_set_string(sf_tab, "LAMP", morder_cnt-p_xs_3->morder_min,"QTH"));
03271 }
03272 }
03273 check(cpl_table_set_int(sf_tab2, "ORDER", morder_cnt-p_xs_3->morder_min+extra_ord,morder_cnt));
03274 if (p_xs_3->arm==0 && morder_cnt>20) {
03275 check(cpl_table_set_string(sf_tab2, "LAMP", morder_cnt-p_xs_3->morder_min,"D2"));
03276 }
03277 else {
03278 check(cpl_table_set_string(sf_tab2, "LAMP", morder_cnt-p_xs_3->morder_min,"QTH"));
03279 }
03280 check(cpl_table_set_int(sf_tab2, "XDISP_CEN", morder_cnt-p_xs_3->morder_min+extra_ord,yc));
03281
03282 check(cpl_table_set_double(sf_tab2, "BLZ_FRAC_WLMIN", morder_cnt-p_xs_3->morder_min+extra_ord,wlmin));
03283 check(cpl_table_set_double(sf_tab2, "BLZ_FRAC_WLMAX", morder_cnt-p_xs_3->morder_min+extra_ord,wlmax));
03284 check(cpl_table_set_double(sf_tab2, "BLZ_FRAC_WLMEAN", morder_cnt-p_xs_3->morder_min+extra_ord,wlmean));
03285
03286 check(cpl_table_set_float(sf_tab2, "WLMIN", morder_cnt-p_xs_3->morder_min+extra_ord,(1.0-buff)*lfsr) );
03287 check(cpl_table_set_float(sf_tab2, "WLMAX", morder_cnt-p_xs_3->morder_min+extra_ord,(1.0+buff)*ufsr));
03288 check(cpl_table_set_float(sf_tab2, "BAND", morder_cnt-p_xs_3->morder_min+extra_ord,band));
03289 check(cpl_table_set_float(sf_tab2, "WLEN0", morder_cnt-p_xs_3->morder_min+extra_ord,((1.0-buff)*lfsr+(1.0+buff)*ufsr)/2.0));
03290 check(cpl_table_set_float(sf_tab2, "WLMINFUL", morder_cnt-p_xs_3->morder_min,(float)(wlmin_full)));
03291 check(cpl_table_set_float(sf_tab2, "WLMAXFUL", morder_cnt-p_xs_3->morder_min,(float)(wlmax_full)));
03292 check(cpl_table_set_double(sf_tab2, "WLCEN", morder_cnt-p_xs_3->morder_min+extra_ord,wlcen));
03293 check(cpl_table_set_int(sf_tab2, "BLZ_FRAC_DISP_MIN", morder_cnt-p_xs_3->morder_min+extra_ord, blz_frac_xmin) );
03294 check(cpl_table_set_int(sf_tab2, "BLZ_FRAC_DISP_MAX", morder_cnt-p_xs_3->morder_min+extra_ord, blz_frac_xmax) );
03295 check(cpl_table_set_float(sf_tab2, "DISP_MIN", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(xmin)) );
03296 check(cpl_table_set_float(sf_tab2, "DISP_MAX", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(xmax)) );
03297 check(cpl_table_set_int(sf_tab2, "BLZ_FRAC_XDISP_MIN", morder_cnt-p_xs_3->morder_min+extra_ord, blz_frac_ymin) );
03298 check(cpl_table_set_int(sf_tab2, "BLZ_FRAC_XDISP_MAX", morder_cnt-p_xs_3->morder_min+extra_ord, blz_frac_ymax) );
03299 check(cpl_table_set_float(sf_tab2, "LFSR", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(lfsr)));
03300 check(cpl_table_set_float(sf_tab2, "BLAZE", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(blaze_wav)));
03301 check(cpl_table_set_float(sf_tab2, "UFSR", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(ufsr)));
03302 check(cpl_table_set_float(sf_tab2, "FSR", morder_cnt-p_xs_3->morder_min+extra_ord, (float)(ufsr-lfsr)));
03303 check(cpl_table_set_double(sf_tab2, "BIN_LFSR", morder_cnt-p_xs_3->morder_min+extra_ord, binlfsr) );
03304 check(cpl_table_set_double(sf_tab2, "BIN_BLAZE", morder_cnt-p_xs_3->morder_min+extra_ord, binblaze) );
03305 check(cpl_table_set_double(sf_tab2, "BIN_UFSR", morder_cnt-p_xs_3->morder_min+extra_ord, binufsr ));
03306 }
03307
03308 sf_plist = cpl_propertylist_new();
03309
03310 check( xsh_pfits_set_pcatg(sf_plist, tag));
03311 cpl_msg_info(__func__, "Save the tables") ;
03312
03313 x_plist=cpl_propertylist_new();
03314 cpl_propertylist_append_int(x_plist, XSH_SPECTRALFORMAT_DIST_ORDER,9);
03315
03316
03317
03318
03319
03320 check(cpl_table_save(sf_tab2, sf_plist, x_plist, tab_filename, CPL_IO_DEFAULT));
03321 xsh_msg(" sf table %s %s %s", tab_filename, cpl_frame_get_filename(sf_frame),
03322 cpl_frame_get_tag(sf_frame));
03323
03324 cleanup:
03325 xsh_free_propertylist(&sf_plist);
03326 xsh_free_propertylist(&x_plist);
03327
03328 xsh_free_table(&sf_tab);
03329 xsh_free_table(&sf_tab2);
03330 if (ref_ind != NULL){
03331 xsh_free2Darray(ref_ind,8);
03332 }
03333
03334 if ( cpl_error_get_code() != CPL_ERROR_NONE){
03335 xsh_free_frame( &sf_frame);
03336 xsh_free_frame( &sf_frame2);
03337 return NULL;
03338 } else {
03339 return sf_frame;
03340 }
03341
03342 }
03343
03344
03364
03365 cpl_frame*
03366 xsh_model_THE_create(xsh_xs_3* p_xs_3,
03367 xsh_instrument* instr,
03368 const char* line_list,
03369 int num_ph,
03370 double sep_ph,
03371 const char* THE_filename)
03372 {
03373 int morder_cnt;
03374 int spos_int;
03375 DOUBLE lambda;
03376 DOUBLE lambda_nm;
03377 DOUBLE blaze_wav, lam_min, lam_max, inten;
03378 cpl_table *lines_tab = NULL;
03379 int lines_number, line_no ;
03380 DOUBLE spos;
03381 DOUBLE** ref_ind = NULL;
03382 cpl_table* THE_tab = NULL;
03383 int fitsrow;
03384 int tab_size;
03385 XSH_ARM arm = XSH_ARM_UNDEFINED;
03386 cpl_frame* THE_frame=NULL;
03387 cpl_propertylist *THE_plist = NULL;
03388 const char* tag=NULL;
03389
03390 fitsrow=0;
03391 tab_size = 150000;
03392
03393 XSH_ASSURE_NOT_NULL( p_xs_3);
03394 XSH_ASSURE_NOT_NULL( instr);
03395 XSH_ASSURE_NOT_NULL( THE_filename);
03396
03397 check( arm = xsh_instrument_get_arm( instr));
03398 p_xs_3->arm=arm;
03399
03400
03401
03402
03403
03404
03405
03406 ref_ind=xsh_alloc2Darray(8,7);
03407
03408 if (arm==XSH_ARM_UVB) {
03409 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
03410 }
03411 else if (arm==XSH_ARM_VIS) {
03412 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
03413 }
03414 else {
03415 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
03416 }
03417
03418
03419
03420
03421
03422 xsh_3_init(p_xs_3);
03423
03424
03425
03426 THE_tab = cpl_table_new(tab_size);
03427 cpl_table_new_column(THE_tab, XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH, CPL_TYPE_FLOAT) ;
03428 cpl_table_new_column(THE_tab, XSH_THE_MAP_TABLE_COLNAME_ORDER, CPL_TYPE_INT) ;
03429 cpl_table_new_column(THE_tab, XSH_THE_MAP_TABLE_COLNAME_SLITINDEX, CPL_TYPE_INT) ;
03430 cpl_table_new_column(THE_tab, XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION, CPL_TYPE_FLOAT) ;
03431 cpl_table_new_column(THE_tab, XSH_THE_MAP_TABLE_COLNAME_DETECTORX, CPL_TYPE_DOUBLE) ;
03432 cpl_table_new_column(THE_tab, XSH_THE_MAP_TABLE_COLNAME_DETECTORY, CPL_TYPE_DOUBLE) ;
03433
03434
03435 if ((lines_tab = cpl_table_load(line_list, 1, 0))==NULL) {
03436 cpl_msg_error(__func__, "Cannot find line list %s", line_list);
03437 return NULL;
03438 }
03439 lines_number = cpl_table_get_nrow(lines_tab) ;
03440
03441
03442 for (line_no=0 ; line_no<lines_number ; line_no++) {
03443 lambda_nm=cpl_table_get_float(lines_tab,
03444 XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH, line_no, NULL);
03445 inten = cpl_table_get(lines_tab,
03446 XSH_ARCLIST_TABLE_COLNAME_FLUX, line_no, NULL);
03447
03448
03449
03450
03451
03452 for (morder_cnt=p_xs_3->morder_min;morder_cnt<=p_xs_3->morder_max; morder_cnt+=1) {
03453
03454 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
03455 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
03456 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
03457 lambda=lambda_nm*1e-6;
03458 if (lambda>lam_min-p_xs_3->blaze_pad && lambda<lam_max+p_xs_3->blaze_pad) {
03459 for (spos_int=0;spos_int<num_ph; spos_int+=1) {
03460 if (sep_ph>0.0) {
03461 spos=sep_ph*(spos_int-((num_ph-1)/2));
03462 }
03463 else {
03464 if (num_ph>1) {
03465 spos=p_xs_3->slit[spos_int];
03466 }
03467 else if (num_ph==1) {
03468 spos=p_xs_3->slit[4];
03469 }
03470 }
03471 p_xs_3->es_y_tot=p_xs_3->es_y+spos*p_xs_3->slit_scale;
03472
03473 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
03474 xsh_3_detpix(p_xs_3);
03475
03476 if (p_xs_3->chippix[0]==1) {
03477
03478 cpl_table_set_float(THE_tab, XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH, fitsrow, lambda_nm) ;
03479 cpl_table_set_int(THE_tab, XSH_THE_MAP_TABLE_COLNAME_ORDER, fitsrow, morder_cnt) ;
03480 cpl_table_set_float(THE_tab, XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION, fitsrow, spos);
03481 cpl_table_set_int(THE_tab, XSH_THE_MAP_TABLE_COLNAME_SLITINDEX, fitsrow, spos_int);
03482 cpl_table_set_double(THE_tab, XSH_THE_MAP_TABLE_COLNAME_DETECTORX, fitsrow, p_xs_3->xpospix);
03483 cpl_table_set_double(THE_tab, XSH_THE_MAP_TABLE_COLNAME_DETECTORY, fitsrow, p_xs_3->ypospix);
03484 fitsrow+=1;
03485 }
03486 }
03487 }
03488 }
03489 }
03490
03491
03492
03493
03494 THE_plist = cpl_propertylist_new();
03495
03496 check( cpl_table_set_size(THE_tab,fitsrow));
03497
03498 check( cpl_table_save(THE_tab, THE_plist, NULL, THE_filename,
03499 CPL_IO_DEFAULT));
03500
03501 if (arm == XSH_ARM_UVB) {
03502 tag="THE_UVB";
03503 }
03504 else if (arm == XSH_ARM_VIS) {
03505 tag="THE_VIS";
03506 }
03507 else if (arm == XSH_ARM_NIR) {
03508 tag="THE_NIR";
03509 }
03510
03511 check(THE_frame=xsh_frame_product(THE_filename,
03512 tag,
03513 CPL_FRAME_TYPE_TABLE,
03514 CPL_FRAME_GROUP_PRODUCT,
03515 CPL_FRAME_LEVEL_FINAL));
03516
03517 xsh_msg(" THE table %s %s",THE_filename,tag);
03518
03519
03520 cleanup:
03521 xsh_free_propertylist(&THE_plist);
03522 xsh_free_table(&THE_tab);
03523 xsh_free_table(&lines_tab);
03524 if ( cpl_error_get_code() != CPL_ERROR_NONE){
03525 xsh_free_frame( &THE_frame);
03526 }
03527 if (ref_ind != NULL){
03528 xsh_free2Darray(ref_ind,8);
03529 }
03530
03531
03532 return THE_frame;
03533
03534 }
03535
03536
03583
03584
03585
03586 cpl_frame*
03587 xsh_model_pipe_anneal(cpl_frame* cfg_frame,
03588 cpl_frame* resid_frame,
03589 int maxit,
03590 double ann_fac,
03591 int scenario,
03592 int rec_id)
03593 {
03594 cpl_table* conf_tab = NULL;
03595 const int model_coeff_num=300;
03596 ann_all_par all_par[model_coeff_num];
03597 ann_all_par* p_all_par = NULL;
03598 double abest[model_coeff_num];
03599 double amin[model_coeff_num];
03600 double amax[model_coeff_num];
03601 int aname[model_coeff_num];
03602 int wav_den[40];
03603 int adim=0;
03604
03605
03606 int ii, jj, kk;
03607 int size;
03608 DOUBLE** ref_ind = NULL;
03609 DOUBLE* p_wlarray = NULL;
03610 cpl_frame* MODEL_CONF_OPT_frame=NULL;
03611 cpl_propertylist* MODEL_CONFIG_plist=NULL ;
03612 xsh_resid_tab* resid_tab = NULL;
03613 double *vlambda=NULL, *thpre_x=NULL, *thpre_y=NULL,
03614 *xgauss=NULL, *ygauss=NULL, *vorder=NULL;
03615 int *slitindex=NULL;
03616 coord* msp_coord = NULL;
03617 char out_cfg_filename[256];
03618 xsh_xs_3 xs_3;
03619 xsh_xs_3* p_xs_3=NULL;
03620 DOUBLE wav_den_bin_sz;
03621
03622 cpl_table* resid_tbl = NULL;
03623 cpl_propertylist *resid_header = NULL;
03624 const char* tag=NULL;
03625
03626 p_xs_3=&xs_3;
03627
03628 XSH_ASSURE_NOT_NULL( resid_frame);
03629 XSH_ASSURE_NOT_NULL(cfg_frame);
03630 p_all_par=&all_par[0];
03631
03632
03633
03634
03635
03636
03637
03638
03639 for (ii=0;ii<model_coeff_num;ii++) {
03640 sprintf((p_all_par+ii)->name, "%s", "\n");
03641 }
03642
03643
03644
03645
03646
03647 adim=xsh_model_readfits(abest,
03648 amin,
03649 amax,
03650 aname,
03651 cpl_frame_get_filename(cfg_frame),
03652 cpl_frame_get_tag(cfg_frame),
03653 p_xs_3,
03654 p_all_par);
03655
03656
03657
03658
03659 if (scenario!=0) {
03660 for (ii=0;ii<model_coeff_num;ii++) {
03661 aname[ii]=-1;
03662 abest[ii]=0.0;
03663 amin[ii]=0.0;
03664 amax[ii]=0.0;
03665 }
03666
03667
03668 jj=0;
03669 if (scenario<=2) {
03670
03671
03672
03673
03674
03675
03676
03677
03678 for (ii=0;ii<100;ii++) {
03679
03680 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
03681 abest[jj]=p_xs_3->fdet;
03682 amin[jj]=p_xs_3->fdet-(1.0*ann_fac);
03683 amax[jj]=p_xs_3->fdet+(1.0*ann_fac);
03684 aname[jj]=ii;
03685 jj+=1;
03686 }
03687 else {
03688
03689
03690
03691
03692
03693
03694
03695
03696
03697
03698
03699
03700
03701
03702
03703
03704
03705
03706
03707
03708
03709
03710
03711 if (strncmp((p_all_par+ii)->name,"mup1",4)==0) {
03712 aname[jj]=ii;
03713 abest[jj]=p_xs_3->mup1/DEG2RAD;
03714 amin[jj]=p_xs_3->mup1/DEG2RAD-(0.1*ann_fac);
03715 amax[jj]=p_xs_3->mup1/DEG2RAD+(0.1*ann_fac);
03716 jj+=1;
03717 }
03718 if (strncmp((p_all_par+ii)->name,"nup1",4)==0) {
03719 aname[jj]=ii;
03720 abest[jj]=p_xs_3->nup1/DEG2RAD;
03721 amin[jj]=p_xs_3->nup1/DEG2RAD-(0.1*ann_fac);
03722 amax[jj]=p_xs_3->nup1/DEG2RAD+(0.1*ann_fac);
03723 jj+=1;
03724 }
03725 if (p_xs_3->arm==2) {
03726
03727
03728 if (strncmp((p_all_par+ii)->name,"mup3",4)==0) {
03729 aname[jj]=ii;
03730 abest[jj]=p_xs_3->mup3/DEG2RAD;
03731 amin[jj]=p_xs_3->mup3/DEG2RAD-(0.1*ann_fac);
03732 amax[jj]=p_xs_3->mup3/DEG2RAD+(0.1*ann_fac);
03733 jj+=1;
03734 }
03735 if (strncmp((p_all_par+ii)->name,"nup3",4)==0) {
03736 aname[jj]=ii;
03737 abest[jj]=p_xs_3->nup3/DEG2RAD;
03738 amin[jj]=p_xs_3->nup3/DEG2RAD-(0.1*ann_fac);
03739 amax[jj]=p_xs_3->nup3/DEG2RAD+(0.1*ann_fac);
03740 jj+=1;
03741 }
03742
03743
03744 if (strncmp((p_all_par+ii)->name,"mup5",4)==0) {
03745 aname[jj]=ii;
03746 abest[jj]=p_xs_3->mup5/DEG2RAD;
03747 amin[jj]=p_xs_3->mup5/DEG2RAD-(0.1*ann_fac);
03748 amax[jj]=p_xs_3->mup5/DEG2RAD+(0.1*ann_fac);
03749 jj+=1;
03750 }
03751 if (strncmp((p_all_par+ii)->name,"nup5",4)==0) {
03752 aname[jj]=ii;
03753 abest[jj]=p_xs_3->nup5/DEG2RAD;
03754 amin[jj]=p_xs_3->nup5/DEG2RAD-(0.1*ann_fac);
03755 amax[jj]=p_xs_3->nup5/DEG2RAD+(0.1*ann_fac);
03756 jj+=1;
03757 }
03758 }
03759
03760 if (strncmp((p_all_par+ii)->name,"mug",3)==0) {
03761 aname[jj]=ii;
03762 abest[jj]=p_xs_3->mug/DEG2RAD;
03763 amin[jj]=p_xs_3->mug/DEG2RAD-(0.1*ann_fac);
03764 amax[jj]=p_xs_3->mug/DEG2RAD+(0.1*ann_fac);
03765 jj+=1;
03766 }
03767 if (strncmp((p_all_par+ii)->name,"nug",3)==0) {
03768 aname[jj]=ii;
03769 abest[jj]=p_xs_3->nug/DEG2RAD;
03770 amin[jj]=p_xs_3->nug/DEG2RAD-(1.0*ann_fac);
03771 amax[jj]=p_xs_3->nug/DEG2RAD+(1.0*ann_fac);
03772 jj+=1;
03773 }
03774 if (strncmp((p_all_par+ii)->name,"taug",4)==0) {
03775 aname[jj]=ii;
03776 abest[jj]=p_xs_3->taug/DEG2RAD;
03777 amin[jj]=p_xs_3->taug/DEG2RAD-(0.1*ann_fac);
03778 amax[jj]=p_xs_3->taug/DEG2RAD+(0.1*ann_fac);
03779 jj+=1;
03780 }
03781
03782 if (strncmp((p_all_par+ii)->name,"mud",3)==0) {
03783 aname[jj]=ii;
03784 abest[jj]=p_xs_3->mud/DEG2RAD;
03785 amin[jj]=p_xs_3->mud/DEG2RAD-(1.0*ann_fac);
03786 amax[jj]=p_xs_3->mud/DEG2RAD+(1.0*ann_fac);
03787 jj+=1;
03788 }
03789 if (strncmp((p_all_par+ii)->name,"nud",3)==0) {
03790 aname[jj]=ii;
03791 abest[jj]=p_xs_3->nud/DEG2RAD;
03792 amin[jj]=p_xs_3->nud/DEG2RAD-(1.0*ann_fac);
03793 amax[jj]=p_xs_3->nud/DEG2RAD+(1.0*ann_fac);
03794 jj+=1;
03795 }
03796 if (strncmp((p_all_par+ii)->name,"taud",4)==0) {
03797 aname[jj]=ii;
03798 abest[jj]=p_xs_3->taud/DEG2RAD;
03799 amin[jj]=p_xs_3->taud/DEG2RAD-(1.0*ann_fac);
03800 amax[jj]=p_xs_3->taud/DEG2RAD+(1.0*ann_fac);
03801 jj+=1;
03802 }
03803
03804
03805 if (strncmp((p_all_par+ii)->name,"es_y",4)==0) {
03806 abest[jj]=p_xs_3->es_y;
03807 amin[jj]=p_xs_3->es_y-(1.0*ann_fac);
03808 amax[jj]=p_xs_3->es_y+(1.0*ann_fac);
03809 aname[jj]=ii;
03810 jj+=1;
03811 }
03812
03813 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
03814 abest[jj]=p_xs_3->chipx;
03815 amin[jj]=p_xs_3->chipx-(5.0*ann_fac);
03816 amax[jj]=p_xs_3->chipx+(5.0*ann_fac);
03817 aname[jj]=ii;
03818 jj+=1;
03819 }
03820 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
03821 abest[jj]=p_xs_3->chipy;
03822 amin[jj]=p_xs_3->chipy-(5.0*ann_fac);
03823 amax[jj]=p_xs_3->chipy+(5.0*ann_fac);
03824 aname[jj]=ii;
03825 jj+=1;
03826 }
03827
03828 if (strncmp((p_all_par+ii)->name,"sg",2)==0) {
03829 abest[jj]=p_xs_3->sg;
03830 amin[jj]=p_xs_3->sg-(1.0*ann_fac);
03831 amax[jj]=p_xs_3->sg+(1.0*ann_fac);
03832 aname[jj]=ii;
03833 jj+=1;
03834 }
03835 }
03836 if (scenario==2) {
03837
03838
03839
03840
03841
03842
03843
03844
03845 if (strncmp((p_all_par+ii)->name,"taues",5)==0) {
03846 aname[jj]=ii;
03847 abest[jj]=p_xs_3->taues/DEG2RAD;
03848 amin[jj]=p_xs_3->taues/DEG2RAD-(5.0*ann_fac);
03849 amax[jj]=p_xs_3->taues/DEG2RAD+(5.0*ann_fac);
03850 jj+=1;
03851 }
03852
03853 if (strncmp((p_all_par+ii)->name,"fcol",4)==0) {
03854 abest[jj]=p_xs_3->fcol;
03855 amin[jj]=p_xs_3->fcol-(5.0*ann_fac);
03856 amax[jj]=p_xs_3->fcol+(5.0*ann_fac);
03857 aname[jj]=ii;
03858 jj+=1;
03859 }
03860
03861 if (strncmp((p_all_par+ii)->name,"slit_scale",10)==0) {
03862 abest[jj]=p_xs_3->slit_scale;
03863 amin[jj]=p_xs_3->slit_scale-(0.05*ann_fac);
03864 amax[jj]=p_xs_3->slit_scale+(0.05*ann_fac);
03865 aname[jj]=ii;
03866 jj+=1;
03867 }
03868
03869
03870 if (strncmp((p_all_par+ii)->name,"es_x",4)==0) {
03871 abest[jj]=p_xs_3->es_x;
03872 amin[jj]=p_xs_3->es_x-(0.1*ann_fac);
03873 amax[jj]=p_xs_3->es_x+(0.1*ann_fac);
03874 aname[jj]=ii;
03875 jj+=1;
03876 }
03877 }
03878 }
03879 adim=jj;
03880 }
03881 if (scenario>2) {
03882
03883
03884 for (ii=0;ii<100;ii++) {
03885 if (scenario<6) {
03886
03887
03888
03889
03890
03891
03892
03893
03894
03895 if (strncmp((p_all_par+ii)->name,"es_y",4)==0) {
03896 abest[jj]=p_xs_3->es_y;
03897 amin[jj]=p_xs_3->es_y-(0.01*ann_fac);
03898 amax[jj]=p_xs_3->es_y+(0.01*ann_fac);
03899 aname[jj]=ii;
03900 jj+=1;
03901 }
03902
03903
03904 if (strncmp((p_all_par+ii)->name,"es_x",4)==0) {
03905 abest[jj]=p_xs_3->es_x;
03906 amin[jj]=p_xs_3->es_x-(0.01*ann_fac);
03907 amax[jj]=p_xs_3->es_x+(0.01*ann_fac);
03908 aname[jj]=ii;
03909 jj+=1;
03910 }
03911
03912
03913 if (strncmp((p_all_par+ii)->name,"mup1",4)==0) {
03914 aname[jj]=ii;
03915 abest[jj]=p_xs_3->mup1/DEG2RAD;
03916 amin[jj]=p_xs_3->mup1/DEG2RAD-(0.005*ann_fac);
03917 amax[jj]=p_xs_3->mup1/DEG2RAD+(0.005*ann_fac);
03918 jj+=1;
03919 }
03920 if (strncmp((p_all_par+ii)->name,"nup1",4)==0) {
03921 aname[jj]=ii;
03922 abest[jj]=p_xs_3->nup1/DEG2RAD;
03923 amin[jj]=p_xs_3->nup1/DEG2RAD-(0.005*ann_fac);
03924 amax[jj]=p_xs_3->nup1/DEG2RAD+(0.005*ann_fac);
03925 jj+=1;
03926 }
03927
03928
03929
03930
03931
03932
03933
03934
03935
03936 if (strncmp((p_all_par+ii)->name,"mup2",4)==0) {
03937 aname[jj]=ii;
03938 abest[jj]=p_xs_3->mup2/DEG2RAD;
03939 amin[jj]=p_xs_3->mup2/DEG2RAD-(0.005*ann_fac);
03940 amax[jj]=p_xs_3->mup2/DEG2RAD+(0.005*ann_fac);
03941 jj+=1;
03942 }
03943 if (strncmp((p_all_par+ii)->name,"nup2",4)==0) {
03944 aname[jj]=ii;
03945 abest[jj]=p_xs_3->nup2/DEG2RAD;
03946 amin[jj]=p_xs_3->nup2/DEG2RAD-(0.005*ann_fac);
03947 amax[jj]=p_xs_3->nup2/DEG2RAD+(0.005*ann_fac);
03948 jj+=1;
03949 }
03950
03951
03952
03953
03954
03955
03956
03957 if (p_xs_3->arm==2) {
03958
03959
03960 if (strncmp((p_all_par+ii)->name,"mup3",4)==0) {
03961 aname[jj]=ii;
03962 abest[jj]=p_xs_3->mup3/DEG2RAD;
03963 amin[jj]=p_xs_3->mup3/DEG2RAD-(0.005*ann_fac);
03964 amax[jj]=p_xs_3->mup3/DEG2RAD+(0.005*ann_fac);
03965 jj+=1;
03966 }
03967 if (strncmp((p_all_par+ii)->name,"nup3",4)==0) {
03968 aname[jj]=ii;
03969 abest[jj]=p_xs_3->nup3/DEG2RAD;
03970 amin[jj]=p_xs_3->nup3/DEG2RAD-(0.005*ann_fac);
03971 amax[jj]=p_xs_3->nup3/DEG2RAD+(0.005*ann_fac);
03972 jj+=1;
03973 }
03974
03975
03976
03977
03978
03979
03980
03981
03982
03983 if (strncmp((p_all_par+ii)->name,"mup4",4)==0) {
03984 aname[jj]=ii;
03985 abest[jj]=p_xs_3->mup4/DEG2RAD;
03986 amin[jj]=p_xs_3->mup4/DEG2RAD-(0.005*ann_fac);
03987 amax[jj]=p_xs_3->mup4/DEG2RAD+(0.005*ann_fac);
03988 jj+=1;
03989 }
03990 if (strncmp((p_all_par+ii)->name,"nup4",4)==0) {
03991 aname[jj]=ii;
03992 abest[jj]=p_xs_3->nup4/DEG2RAD;
03993 amin[jj]=p_xs_3->nup4/DEG2RAD-(0.005*ann_fac);
03994 amax[jj]=p_xs_3->nup4/DEG2RAD+(0.005*ann_fac);
03995 jj+=1;
03996 }
03997
03998
03999
04000
04001
04002
04003
04004
04005
04006 if (strncmp((p_all_par+ii)->name,"mup5",4)==0) {
04007 aname[jj]=ii;
04008 abest[jj]=p_xs_3->mup5/DEG2RAD;
04009 amin[jj]=p_xs_3->mup5/DEG2RAD-(0.005*ann_fac);
04010 amax[jj]=p_xs_3->mup5/DEG2RAD+(0.005*ann_fac);
04011 jj+=1;
04012 }
04013 if (strncmp((p_all_par+ii)->name,"nup5",4)==0) {
04014 aname[jj]=ii;
04015 abest[jj]=p_xs_3->nup5/DEG2RAD;
04016 amin[jj]=p_xs_3->nup5/DEG2RAD-(0.005*ann_fac);
04017 amax[jj]=p_xs_3->nup5/DEG2RAD+(0.005*ann_fac);
04018 jj+=1;
04019 }
04020
04021
04022
04023
04024
04025
04026
04027
04028
04029 if (strncmp((p_all_par+ii)->name,"mup6",4)==0) {
04030 aname[jj]=ii;
04031 abest[jj]=p_xs_3->mup6/DEG2RAD;
04032 amin[jj]=p_xs_3->mup6/DEG2RAD-(0.005*ann_fac);
04033 amax[jj]=p_xs_3->mup6/DEG2RAD+(0.005*ann_fac);
04034 jj+=1;
04035 }
04036 if (strncmp((p_all_par+ii)->name,"nup6",4)==0) {
04037 aname[jj]=ii;
04038 abest[jj]=p_xs_3->nup6/DEG2RAD;
04039 amin[jj]=p_xs_3->nup6/DEG2RAD-(0.005*ann_fac);
04040 amax[jj]=p_xs_3->nup6/DEG2RAD+(0.005*ann_fac);
04041 jj+=1;
04042 }
04043
04044
04045
04046
04047
04048
04049
04050 }
04051
04052 if (strncmp((p_all_par+ii)->name,"mug",3)==0) {
04053 aname[jj]=ii;
04054 abest[jj]=p_xs_3->mug/DEG2RAD;
04055 amin[jj]=p_xs_3->mug/DEG2RAD-(0.05*ann_fac);
04056 amax[jj]=p_xs_3->mug/DEG2RAD+(0.05*ann_fac);
04057 jj+=1;
04058 }
04059 if (strncmp((p_all_par+ii)->name,"nug",3)==0) {
04060 aname[jj]=ii;
04061 abest[jj]=p_xs_3->nug/DEG2RAD;
04062 amin[jj]=p_xs_3->nug/DEG2RAD-(0.5*ann_fac);
04063 amax[jj]=p_xs_3->nug/DEG2RAD+(0.5*ann_fac);
04064 jj+=1;
04065 }
04066 if (strncmp((p_all_par+ii)->name,"taug",4)==0) {
04067 aname[jj]=ii;
04068 abest[jj]=p_xs_3->taug/DEG2RAD;
04069 amin[jj]=p_xs_3->taug/DEG2RAD-(0.05*ann_fac);
04070 amax[jj]=p_xs_3->taug/DEG2RAD+(0.05*ann_fac);
04071 jj+=1;
04072 }
04073
04074 if (strncmp((p_all_par+ii)->name,"mud",3)==0) {
04075 aname[jj]=ii;
04076 abest[jj]=p_xs_3->mud/DEG2RAD;
04077 amin[jj]=p_xs_3->mud/DEG2RAD-(0.005*ann_fac);
04078 amax[jj]=p_xs_3->mud/DEG2RAD+(0.005*ann_fac);
04079 jj+=1;
04080 }
04081 if (strncmp((p_all_par+ii)->name,"nud",3)==0) {
04082 aname[jj]=ii;
04083 abest[jj]=p_xs_3->nud/DEG2RAD;
04084 amin[jj]=p_xs_3->nud/DEG2RAD-(0.005*ann_fac);
04085 amax[jj]=p_xs_3->nud/DEG2RAD+(0.005*ann_fac);
04086 jj+=1;
04087 }
04088 if (strncmp((p_all_par+ii)->name,"taud",4)==0) {
04089 aname[jj]=ii;
04090 abest[jj]=p_xs_3->taud/DEG2RAD;
04091 amin[jj]=p_xs_3->taud/DEG2RAD-(0.05*ann_fac);
04092 amax[jj]=p_xs_3->taud/DEG2RAD+(0.05*ann_fac);
04093 jj+=1;
04094 }
04095
04096 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
04097 abest[jj]=p_xs_3->fdet;
04098 amin[jj]=p_xs_3->fdet-(0.1*ann_fac);
04099 amax[jj]=p_xs_3->fdet+(0.1*ann_fac);
04100 aname[jj]=ii;
04101 jj+=1;
04102 }
04103
04104 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
04105 abest[jj]=p_xs_3->chipx;
04106 amin[jj]=p_xs_3->chipx-(0.05*ann_fac);
04107 amax[jj]=p_xs_3->chipx+(0.05*ann_fac);
04108 aname[jj]=ii;
04109 jj+=1;
04110 }
04111 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
04112 abest[jj]=p_xs_3->chipy;
04113 amin[jj]=p_xs_3->chipy-(0.05*ann_fac);
04114 amax[jj]=p_xs_3->chipy+(0.05*ann_fac);
04115 aname[jj]=ii;
04116 jj+=1;
04117 }
04118
04119 if (strncmp((p_all_par+ii)->name,"sg",2)==0) {
04120 abest[jj]=p_xs_3->sg;
04121 amin[jj]=p_xs_3->sg-(0.5*ann_fac);
04122 amax[jj]=p_xs_3->sg+(0.5*ann_fac);
04123 aname[jj]=ii;
04124 jj+=1;
04125 }
04126 if (scenario>3) {
04127
04128
04129
04130 if (strncmp((p_all_par+ii)->name,"slit_scale",10)==0) {
04131 abest[jj]=p_xs_3->slit_scale;
04132 amin[jj]=p_xs_3->slit_scale-(0.01*ann_fac);
04133 amax[jj]=p_xs_3->slit_scale+(0.01*ann_fac);
04134 aname[jj]=ii;
04135 jj+=1;
04136 }
04137
04138 if (strncmp((p_all_par+ii)->name,"taues",5)==0) {
04139 aname[jj]=ii;
04140 abest[jj]=p_xs_3->taues/DEG2RAD;
04141 amin[jj]=p_xs_3->taues/DEG2RAD-(0.1*ann_fac);
04142 amax[jj]=p_xs_3->taues/DEG2RAD+(0.1*ann_fac);
04143 jj+=1;
04144 }
04145
04146
04147
04148
04149
04150
04151
04152
04153
04154
04155
04156
04157
04158
04159
04160
04161 if (strncmp((p_all_par+ii)->name,"fcol",4)==0) {
04162 abest[jj]=p_xs_3->fcol;
04163 amin[jj]=p_xs_3->fcol-(0.1*ann_fac);
04164 amax[jj]=p_xs_3->fcol+(0.1*ann_fac);
04165 aname[jj]=ii;
04166 jj+=1;
04167 }
04168
04169
04170
04171
04172
04173
04174
04175
04176
04177
04178
04179
04180
04181
04182
04183
04184
04185
04186
04187
04188
04189
04190
04191
04192
04193
04194
04195
04196
04197
04198
04199
04200
04201
04202
04203
04204
04205
04206
04207
04208
04209
04210
04211
04212
04213
04214
04215
04216
04217
04218
04219
04220
04221
04222
04223
04224
04225
04226
04227
04228
04229
04230
04231
04232
04233
04234
04235
04236
04237
04238
04239
04240
04241
04242 }
04243 }
04244 if (scenario>=5 && scenario<8) {
04245 if (scenario==6) {
04246
04247
04248
04249
04250
04251 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
04252 abest[jj]=p_xs_3->chipx;
04253 amin[jj]=p_xs_3->chipx-(0.1*ann_fac);
04254 amax[jj]=p_xs_3->chipx+(0.1*ann_fac);
04255 aname[jj]=ii;
04256 jj+=1;
04257 }
04258 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
04259 abest[jj]=p_xs_3->chipy;
04260 amin[jj]=p_xs_3->chipy-(0.1*ann_fac);
04261 amax[jj]=p_xs_3->chipy+(0.1*ann_fac);
04262 aname[jj]=ii;
04263 jj+=1;
04264 }
04265 }
04266
04267
04268
04269
04270
04271
04272
04273
04274
04275
04276
04277
04278
04279
04280
04281
04282
04283
04284
04285
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296 if (strncmp((p_all_par+ii)->name,"pc_x_x1",7)==0) {
04297 abest[jj]=p_xs_3->pc_x_x1;
04298 amin[jj]=p_xs_3->pc_x_x1-(0.001*ann_fac);
04299 amax[jj]=p_xs_3->pc_x_x1+(0.001*ann_fac);
04300 aname[jj]=ii;
04301 jj+=1;
04302 }
04303 if (strncmp((p_all_par+ii)->name,"pc_x_xx",7)==0) {
04304 abest[jj]=p_xs_3->pc_x_xx;
04305 amin[jj]=p_xs_3->pc_x_xx-(0.0001*ann_fac);
04306 amax[jj]=p_xs_3->pc_x_xx+(0.0001*ann_fac);
04307 aname[jj]=ii;
04308 jj+=1;
04309 }
04310 if (strncmp((p_all_par+ii)->name,"pc_x_y1",7)==0) {
04311 abest[jj]=p_xs_3->pc_x_y1;
04312 amin[jj]=p_xs_3->pc_x_y1-(0.001*ann_fac);
04313 amax[jj]=p_xs_3->pc_x_y1+(0.001*ann_fac);
04314 aname[jj]=ii;
04315 jj+=1;
04316 }
04317 if (strncmp((p_all_par+ii)->name,"pc_x_yy",7)==0) {
04318 abest[jj]=p_xs_3->pc_x_yy;
04319 amin[jj]=p_xs_3->pc_x_yy-(0.0001*ann_fac);
04320 amax[jj]=p_xs_3->pc_x_yy+(0.0001*ann_fac);
04321 aname[jj]=ii;
04322 jj+=1;
04323 }
04324 if (strncmp((p_all_par+ii)->name,"pc_x_xy",7)==0) {
04325 abest[jj]=p_xs_3->pc_x_xy;
04326 amin[jj]=p_xs_3->pc_x_xy-(0.0001*ann_fac);
04327 amax[jj]=p_xs_3->pc_x_xy+(0.0001*ann_fac);
04328 aname[jj]=ii;
04329 jj+=1;
04330 }
04331 if (strncmp((p_all_par+ii)->name,"pc_x_x2y",8)==0) {
04332 abest[jj]=p_xs_3->pc_x_x2y;
04333 amin[jj]=p_xs_3->pc_x_x2y-(0.00001*ann_fac);
04334 amax[jj]=p_xs_3->pc_x_x2y+(0.00001*ann_fac);
04335 aname[jj]=ii;
04336 jj+=1;
04337 }
04338 if (strncmp((p_all_par+ii)->name,"pc_x_y2x",8)==0) {
04339 abest[jj]=p_xs_3->pc_x_y2x;
04340 amin[jj]=p_xs_3->pc_x_y2x-(0.00001*ann_fac);
04341 amax[jj]=p_xs_3->pc_x_y2x+(0.00001*ann_fac);
04342 aname[jj]=ii;
04343 jj+=1;
04344 }
04345 if (strncmp((p_all_par+ii)->name,"pc_x_x3",7)==0) {
04346 abest[jj]=p_xs_3->pc_x_x3;
04347 amin[jj]=p_xs_3->pc_x_x3-(0.00001*ann_fac);
04348 amax[jj]=p_xs_3->pc_x_x3+(0.00001*ann_fac);
04349 aname[jj]=ii;
04350 jj+=1;
04351 }
04352 if (strncmp((p_all_par+ii)->name,"pc_x_y3",7)==0) {
04353 abest[jj]=p_xs_3->pc_x_y3;
04354 amin[jj]=p_xs_3->pc_x_y3-(0.00001*ann_fac);
04355 amax[jj]=p_xs_3->pc_x_y3+(0.00001*ann_fac);
04356 aname[jj]=ii;
04357 jj+=1;
04358 }
04359 if (strncmp((p_all_par+ii)->name,"pc_y_x1",7)==0) {
04360 abest[jj]=p_xs_3->pc_y_x1;
04361 amin[jj]=p_xs_3->pc_y_x1-(0.001*ann_fac);
04362 amax[jj]=p_xs_3->pc_y_x1+(0.001*ann_fac);
04363 aname[jj]=ii;
04364 jj+=1;
04365 }
04366 if (strncmp((p_all_par+ii)->name,"pc_y_xx",7)==0) {
04367 abest[jj]=p_xs_3->pc_y_xx;
04368 amin[jj]=p_xs_3->pc_y_xx-(0.0001*ann_fac);
04369 amax[jj]=p_xs_3->pc_y_xx+(0.0001*ann_fac);
04370 aname[jj]=ii;
04371 jj+=1;
04372 }
04373 if (strncmp((p_all_par+ii)->name,"pc_y_y1",7)==0) {
04374 abest[jj]=p_xs_3->pc_y_y1;
04375 amin[jj]=p_xs_3->pc_y_y1-(0.001*ann_fac);
04376 amax[jj]=p_xs_3->pc_y_y1+(0.001*ann_fac);
04377 aname[jj]=ii;
04378 jj+=1;
04379 }
04380 if (strncmp((p_all_par+ii)->name,"pc_y_yy",7)==0) {
04381 abest[jj]=p_xs_3->pc_y_yy;
04382 amin[jj]=p_xs_3->pc_y_yy-(0.0001*ann_fac);
04383 amax[jj]=p_xs_3->pc_y_yy+(0.0001*ann_fac);
04384 aname[jj]=ii;
04385 jj+=1;
04386 }
04387 if (strncmp((p_all_par+ii)->name,"pc_y_xy",7)==0) {
04388 abest[jj]=p_xs_3->pc_y_xy;
04389 amin[jj]=p_xs_3->pc_y_xy-(0.0001*ann_fac);
04390 amax[jj]=p_xs_3->pc_y_xy+(0.0001*ann_fac);
04391 aname[jj]=ii;
04392 jj+=1;
04393 }
04394 if (strncmp((p_all_par+ii)->name,"pc_y_x2y",8)==0) {
04395 abest[jj]=p_xs_3->pc_y_x2y;
04396 amin[jj]=p_xs_3->pc_y_x2y-(0.00001*ann_fac);
04397 amax[jj]=p_xs_3->pc_y_x2y+(0.00001*ann_fac);
04398 aname[jj]=ii;
04399 jj+=1;
04400 }
04401 if (strncmp((p_all_par+ii)->name,"pc_y_y2x",8)==0) {
04402 abest[jj]=p_xs_3->pc_y_y2x;
04403 amin[jj]=p_xs_3->pc_y_y2x-(0.00001*ann_fac);
04404 amax[jj]=p_xs_3->pc_y_y2x+(0.00001*ann_fac);
04405 aname[jj]=ii;
04406 jj+=1;
04407 }
04408 if (strncmp((p_all_par+ii)->name,"pc_y_x3",7)==0) {
04409 abest[jj]=p_xs_3->pc_y_x3;
04410 amin[jj]=p_xs_3->pc_y_x3-(0.00001*ann_fac);
04411 amax[jj]=p_xs_3->pc_y_x3+(0.00001*ann_fac);
04412 aname[jj]=ii;
04413 jj+=1;
04414 }
04415 if (strncmp((p_all_par+ii)->name,"pc_y_y3",7)==0) {
04416 abest[jj]=p_xs_3->pc_y_y3;
04417 amin[jj]=p_xs_3->pc_y_y3-(0.00001*ann_fac);
04418 amax[jj]=p_xs_3->pc_y_y3+(0.00001*ann_fac);
04419 aname[jj]=ii;
04420 jj+=1;
04421 }
04422 if (p_xs_3->arm<2) {
04423 if (strncmp((p_all_par+ii)->name,"d2_x1",5)==0) {
04424 abest[jj]=p_xs_3->d2_x1;
04425 amin[jj]=p_xs_3->d2_x1-(0.01*ann_fac);
04426 amax[jj]=p_xs_3->d2_x1+(0.01*ann_fac);
04427 aname[jj]=ii;
04428 jj+=1;
04429 }
04430 if (strncmp((p_all_par+ii)->name,"d2_x2",5)==0) {
04431 abest[jj]=p_xs_3->d2_x2;
04432 amin[jj]=p_xs_3->d2_x2-(0.001*ann_fac);
04433 amax[jj]=p_xs_3->d2_x2+(0.001*ann_fac);
04434 aname[jj]=ii;
04435 jj+=1;
04436 }
04437 if (strncmp((p_all_par+ii)->name,"d2_x3",5)==0) {
04438 abest[jj]=p_xs_3->d2_x3;
04439 amin[jj]=p_xs_3->d2_x3-(0.0001*ann_fac);
04440 amax[jj]=p_xs_3->d2_x3+(0.0001*ann_fac);
04441 aname[jj]=ii;
04442 jj+=1;
04443 }
04444 }
04445 else if (p_xs_3->arm==2) {
04446 if (strncmp((p_all_par+ii)->name,"pc4_x_xy3",9)==0) {
04447 abest[jj]=p_xs_3->pc4_x_xy3;
04448 amin[jj]=p_xs_3->pc4_x_xy3-(0.000001*ann_fac);
04449 amax[jj]=p_xs_3->pc4_x_xy3+(0.000001*ann_fac);
04450 aname[jj]=ii;
04451 jj+=1;
04452 }
04453 if (strncmp((p_all_par+ii)->name,"pc4_x_x3y",9)==0) {
04454 abest[jj]=p_xs_3->pc4_x_x3y;
04455 amin[jj]=p_xs_3->pc4_x_x3y-(0.000001*ann_fac);
04456 amax[jj]=p_xs_3->pc4_x_x3y+(0.000001*ann_fac);
04457 aname[jj]=ii;
04458 jj+=1;
04459 }
04460 if (strncmp((p_all_par+ii)->name,"pc4_x_x2y2",10)==0) {
04461 abest[jj]=p_xs_3->pc4_x_x2y2;
04462 amin[jj]=p_xs_3->pc4_x_x2y2-(0.000001*ann_fac);
04463 amax[jj]=p_xs_3->pc4_x_x2y2+(0.000001*ann_fac);
04464 aname[jj]=ii;
04465 jj+=1;
04466 }
04467 if (strncmp((p_all_par+ii)->name,"pc4_x_x4",8)==0) {
04468 abest[jj]=p_xs_3->pc4_x_x4;
04469 amin[jj]=p_xs_3->pc4_x_x4-(0.000001*ann_fac);
04470 amax[jj]=p_xs_3->pc4_x_x4+(0.000001*ann_fac);
04471 aname[jj]=ii;
04472 jj+=1;
04473 }
04474 if (strncmp((p_all_par+ii)->name,"pc4_x_y4",8)==0) {
04475 abest[jj]=p_xs_3->pc4_x_y4;
04476 amin[jj]=p_xs_3->pc4_x_y4-(0.000001*ann_fac);
04477 amax[jj]=p_xs_3->pc4_x_y4+(0.000001*ann_fac);
04478 aname[jj]=ii;
04479 jj+=1;
04480 }
04481 if (strncmp((p_all_par+ii)->name,"pc4_y_xy3",9)==0) {
04482 abest[jj]=p_xs_3->pc4_y_xy3;
04483 amin[jj]=p_xs_3->pc4_y_xy3-(0.000001*ann_fac);
04484 amax[jj]=p_xs_3->pc4_y_xy3+(0.000001*ann_fac);
04485 aname[jj]=ii;
04486 jj+=1;
04487 }
04488 if (strncmp((p_all_par+ii)->name,"pc4_y_xy3",9)==0) {
04489 abest[jj]=p_xs_3->pc4_y_xy3;
04490 amin[jj]=p_xs_3->pc4_y_xy3-(0.000001*ann_fac);
04491 amax[jj]=p_xs_3->pc4_y_xy3+(0.000001*ann_fac);
04492 aname[jj]=ii;
04493 jj+=1;
04494 }
04495 if (strncmp((p_all_par+ii)->name,"pc4_y_x2y2",10)==0) {
04496 abest[jj]=p_xs_3->pc4_y_x2y2;
04497 amin[jj]=p_xs_3->pc4_y_x2y2-(0.000001*ann_fac);
04498 amax[jj]=p_xs_3->pc4_y_x2y2+(0.000001*ann_fac);
04499 aname[jj]=ii;
04500 jj+=1;
04501 }
04502 if (strncmp((p_all_par+ii)->name,"pc4_y_x4",8)==0) {
04503 abest[jj]=p_xs_3->pc4_y_x4;
04504 amin[jj]=p_xs_3->pc4_y_x4-(0.000001*ann_fac);
04505 amax[jj]=p_xs_3->pc4_y_x4+(0.000001*ann_fac);
04506 aname[jj]=ii;
04507 jj+=1;
04508 }
04509 if (strncmp((p_all_par+ii)->name,"pc4_y_y4",8)==0) {
04510 abest[jj]=p_xs_3->pc4_y_y4;
04511 amin[jj]=p_xs_3->pc4_y_y4-(0.000001*ann_fac);
04512 amax[jj]=p_xs_3->pc4_y_y4+(0.000001*ann_fac);
04513 aname[jj]=ii;
04514 jj+=1;
04515 }
04516 if (strncmp((p_all_par+ii)->name,"ca_x0",5)==0) {
04517 abest[jj]=p_xs_3->ca_x0;
04518 amin[jj]=p_xs_3->ca_x0-(0.000001*ann_fac);
04519 amax[jj]=p_xs_3->ca_x0+(0.000001*ann_fac);
04520 aname[jj]=ii;
04521 jj+=1;
04522 }
04523 if (strncmp((p_all_par+ii)->name,"ca_x1",5)==0) {
04524 abest[jj]=p_xs_3->ca_x1;
04525 amin[jj]=p_xs_3->ca_x1-(0.000001*ann_fac);
04526 amax[jj]=p_xs_3->ca_x1+(0.000001*ann_fac);
04527 aname[jj]=ii;
04528 jj+=1;
04529 }
04530 if (strncmp((p_all_par+ii)->name,"ca_y0",5)==0) {
04531 abest[jj]=p_xs_3->ca_y0;
04532 amin[jj]=p_xs_3->ca_y0-(0.000001*ann_fac);
04533 amax[jj]=p_xs_3->ca_y0+(0.000001*ann_fac);
04534 aname[jj]=ii;
04535 jj+=1;
04536 }
04537 if (strncmp((p_all_par+ii)->name,"ca_y1",5)==0) {
04538 abest[jj]=p_xs_3->ca_y1;
04539 amin[jj]=p_xs_3->ca_y1-(0.000001*ann_fac);
04540 amax[jj]=p_xs_3->ca_y1+(0.000001*ann_fac);
04541 aname[jj]=ii;
04542 jj+=1;
04543 }
04544 }
04545 }
04546 if (scenario<0) {
04547
04548
04549 if (strncmp((p_all_par+ii)->name,"es_x",4)==0) {
04550 abest[jj]=p_xs_3->es_x;
04551 amin[jj]=p_xs_3->es_x-(1.0*ann_fac);
04552 amax[jj]=p_xs_3->es_x+(1.0*ann_fac);
04553 aname[jj]=ii;
04554 jj+=1;
04555 }
04556 }
04557 if (scenario==8) {
04558 if (p_xs_3->arm==0) {
04559
04560 if (strncmp((p_all_par+ii)->name,"mud",3)==0) {
04561 aname[jj]=ii;
04562 abest[jj]=p_xs_3->mud/DEG2RAD;
04563 amin[jj]=p_xs_3->mud/DEG2RAD-(0.2*ann_fac);
04564 amax[jj]=p_xs_3->mud/DEG2RAD+(0.2*ann_fac);
04565 jj+=1;
04566 }
04567 if (strncmp((p_all_par+ii)->name,"nud",3)==0) {
04568 aname[jj]=ii;
04569 abest[jj]=p_xs_3->nud/DEG2RAD;
04570 amin[jj]=p_xs_3->nud/DEG2RAD-(0.2*ann_fac);
04571 amax[jj]=p_xs_3->nud/DEG2RAD+(0.2*ann_fac);
04572 jj+=1;
04573 }
04574 if (strncmp((p_all_par+ii)->name,"taud",4)==0) {
04575 aname[jj]=ii;
04576 abest[jj]=p_xs_3->taud/DEG2RAD;
04577 amin[jj]=p_xs_3->taud/DEG2RAD-(0.05*ann_fac);
04578 amax[jj]=p_xs_3->taud/DEG2RAD+(0.05*ann_fac);
04579 jj+=1;
04580 }
04581
04582 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
04583 abest[jj]=p_xs_3->fdet;
04584 amin[jj]=p_xs_3->fdet-(0.05*ann_fac);
04585 amax[jj]=p_xs_3->fdet+(0.05*ann_fac);
04586 aname[jj]=ii;
04587 jj+=1;
04588 }
04589
04590 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
04591 abest[jj]=p_xs_3->chipx;
04592 amin[jj]=p_xs_3->chipx-(0.05*ann_fac);
04593 amax[jj]=p_xs_3->chipx+(0.05*ann_fac);
04594 aname[jj]=ii;
04595 jj+=1;
04596 }
04597 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
04598 abest[jj]=p_xs_3->chipy;
04599 amin[jj]=p_xs_3->chipy-(0.05*ann_fac);
04600 amax[jj]=p_xs_3->chipy+(0.05*ann_fac);
04601 aname[jj]=ii;
04602 jj+=1;
04603 }
04604
04605 if (strncmp((p_all_par+ii)->name,"sg",2)==0) {
04606 abest[jj]=p_xs_3->sg;
04607 amin[jj]=p_xs_3->sg-(0.02*ann_fac);
04608 amax[jj]=p_xs_3->sg+(0.02*ann_fac);
04609 aname[jj]=ii;
04610 jj+=1;
04611 }
04612 }
04613 else if (p_xs_3->arm==1) {
04614 if (strncmp((p_all_par+ii)->name,"mup1",4)==0) {
04615 aname[jj]=ii;
04616 abest[jj]=p_xs_3->mup1/DEG2RAD;
04617 amin[jj]=p_xs_3->mup1/DEG2RAD-(0.025*ann_fac);
04618 amax[jj]=p_xs_3->mup1/DEG2RAD+(0.025*ann_fac);
04619 jj+=1;
04620 }
04621
04622 if (strncmp((p_all_par+ii)->name,"nug",3)==0) {
04623 aname[jj]=ii;
04624 abest[jj]=p_xs_3->nug/DEG2RAD;
04625 amin[jj]=p_xs_3->nug/DEG2RAD-(0.02*ann_fac);
04626 amax[jj]=p_xs_3->nug/DEG2RAD+(0.02*ann_fac);
04627 jj+=1;
04628 }
04629 if (strncmp((p_all_par+ii)->name,"taud",4)==0) {
04630 aname[jj]=ii;
04631 abest[jj]=p_xs_3->taud/DEG2RAD;
04632 amin[jj]=p_xs_3->taud/DEG2RAD-(0.01*ann_fac);
04633 amax[jj]=p_xs_3->taud/DEG2RAD+(0.01*ann_fac);
04634 jj+=1;
04635 }
04636
04637 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
04638 abest[jj]=p_xs_3->fdet;
04639 amin[jj]=p_xs_3->fdet-(0.1*ann_fac);
04640 amax[jj]=p_xs_3->fdet+(0.1*ann_fac);
04641 aname[jj]=ii;
04642 jj+=1;
04643 }
04644
04645 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
04646 abest[jj]=p_xs_3->chipx;
04647 amin[jj]=p_xs_3->chipx-(0.05*ann_fac);
04648 amax[jj]=p_xs_3->chipx+(0.05*ann_fac);
04649 aname[jj]=ii;
04650 jj+=1;
04651 }
04652 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
04653 abest[jj]=p_xs_3->chipy;
04654 amin[jj]=p_xs_3->chipy-(0.05*ann_fac);
04655 amax[jj]=p_xs_3->chipy+(0.05*ann_fac);
04656 aname[jj]=ii;
04657 jj+=1;
04658 }
04659
04660 if (strncmp((p_all_par+ii)->name,"sg",2)==0) {
04661 abest[jj]=p_xs_3->sg;
04662 amin[jj]=p_xs_3->sg-(0.01*ann_fac);
04663 amax[jj]=p_xs_3->sg+(0.01*ann_fac);
04664 aname[jj]=ii;
04665 jj+=1;
04666 }
04667 }
04668 else if (p_xs_3->arm==2) {
04669
04670 if (strncmp((p_all_par+ii)->name,"mup1",4)==0) {
04671 aname[jj]=ii;
04672 abest[jj]=p_xs_3->mup1/DEG2RAD;
04673 amin[jj]=p_xs_3->mup1/DEG2RAD-(0.01*ann_fac);
04674 amax[jj]=p_xs_3->mup1/DEG2RAD+(0.01*ann_fac);
04675 jj+=1;
04676 }
04677
04678 if (strncmp((p_all_par+ii)->name,"nug",3)==0) {
04679 aname[jj]=ii;
04680 abest[jj]=p_xs_3->nug/DEG2RAD;
04681 amin[jj]=p_xs_3->nug/DEG2RAD-(0.01*ann_fac);
04682 amax[jj]=p_xs_3->nug/DEG2RAD+(0.01*ann_fac);
04683 jj+=1;
04684 }
04685 if (strncmp((p_all_par+ii)->name,"mud",3)==0) {
04686 aname[jj]=ii;
04687 abest[jj]=p_xs_3->mud/DEG2RAD;
04688 amin[jj]=p_xs_3->mud/DEG2RAD-(0.1*ann_fac);
04689 amax[jj]=p_xs_3->mud/DEG2RAD+(0.3*ann_fac);
04690 jj+=1;
04691 }
04692 if (strncmp((p_all_par+ii)->name,"nud",3)==0) {
04693 aname[jj]=ii;
04694 abest[jj]=p_xs_3->nud/DEG2RAD;
04695 amin[jj]=p_xs_3->nud/DEG2RAD-(0.1*ann_fac);
04696 amax[jj]=p_xs_3->nud/DEG2RAD+(0.3*ann_fac);
04697 jj+=1;
04698 }
04699
04700 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
04701 abest[jj]=p_xs_3->fdet;
04702 amin[jj]=p_xs_3->fdet-(0.2*ann_fac);
04703 amax[jj]=p_xs_3->fdet+(0.2*ann_fac);
04704 aname[jj]=ii;
04705 jj+=1;
04706 }
04707
04708 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
04709 abest[jj]=p_xs_3->chipx;
04710 amin[jj]=p_xs_3->chipx-(0.075*ann_fac);
04711 amax[jj]=p_xs_3->chipx+(0.075*ann_fac);
04712 aname[jj]=ii;
04713 jj+=1;
04714 }
04715 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
04716 abest[jj]=p_xs_3->chipy;
04717 amin[jj]=p_xs_3->chipy-(0.075*ann_fac);
04718 amax[jj]=p_xs_3->chipy+(0.075*ann_fac);
04719 aname[jj]=ii;
04720 jj+=1;
04721 }
04722
04723 if (strncmp((p_all_par+ii)->name,"taues",5)==0) {
04724 aname[jj]=ii;
04725 abest[jj]=p_xs_3->taues/DEG2RAD;
04726 amin[jj]=p_xs_3->taues/DEG2RAD-(0.05*ann_fac);
04727 amax[jj]=p_xs_3->taues/DEG2RAD+(0.15*ann_fac);
04728 jj+=1;
04729 }
04730
04731 if (strncmp((p_all_par+ii)->name,"fcol",4)==0) {
04732 abest[jj]=p_xs_3->fcol;
04733 amin[jj]=p_xs_3->fcol-(1.0*ann_fac);
04734 amax[jj]=p_xs_3->fcol+(1.5*ann_fac);
04735 aname[jj]=ii;
04736 jj+=1;
04737 }
04738 }
04739 }
04740 if (scenario==9) {
04741
04742 if (p_xs_3->arm!=2) {
04743 cpl_msg_error(__func__,"This scenario is only valid for NIR AFC exposures");
04744 return NULL;
04745 }
04746 if (strncmp((p_all_par+ii)->name,"mug",3)==0) {
04747 aname[jj]=ii;
04748 abest[jj]=p_xs_3->mug/DEG2RAD;
04749 amin[jj]=p_xs_3->mug/DEG2RAD-(0.02*ann_fac);
04750 amax[jj]=p_xs_3->mug/DEG2RAD+(0.02*ann_fac);
04751 jj+=1;
04752 }
04753
04754 if (strncmp((p_all_par+ii)->name,"nug",3)==0) {
04755 aname[jj]=ii;
04756 abest[jj]=p_xs_3->nug/DEG2RAD;
04757 amin[jj]=p_xs_3->nug/DEG2RAD-(0.02*ann_fac);
04758 amax[jj]=p_xs_3->nug/DEG2RAD+(0.02*ann_fac);
04759 jj+=1;
04760 }
04761
04762 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
04763 abest[jj]=p_xs_3->fdet;
04764 amin[jj]=p_xs_3->fdet-(0.4*ann_fac);
04765 amax[jj]=p_xs_3->fdet+(0.4*ann_fac);
04766 aname[jj]=ii;
04767 jj+=1;
04768 }
04769 if (strncmp((p_all_par+ii)->name,"taud",4)==0) {
04770 aname[jj]=ii;
04771 abest[jj]=p_xs_3->taud/DEG2RAD;
04772 amin[jj]=p_xs_3->taud/DEG2RAD-(0.025*ann_fac);
04773 amax[jj]=p_xs_3->taud/DEG2RAD+(0.025*ann_fac);
04774 jj+=1;
04775 }
04776
04777 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
04778 abest[jj]=p_xs_3->chipx;
04779 amin[jj]=p_xs_3->chipx-(0.1*ann_fac);
04780 amax[jj]=p_xs_3->chipx+(0.1*ann_fac);
04781 aname[jj]=ii;
04782 jj+=1;
04783 }
04784 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
04785 abest[jj]=p_xs_3->chipy;
04786 amin[jj]=p_xs_3->chipy-(0.1*ann_fac);
04787 amax[jj]=p_xs_3->chipy+(0.1*ann_fac);
04788 aname[jj]=ii;
04789 jj+=1;
04790 }
04791 }
04792 }
04793 adim=jj;
04794 }
04795 }
04796 else {
04797 if (adim==0) {
04798 cpl_msg_error(__func__,"The use of the phys mod config file optimisation flags and ranges was specified, however the input phys mod config has no optimisation flags set");
04799 return NULL;
04800 }
04801 }
04802 check( resid_tab = xsh_resid_tab_load(resid_frame));
04803 resid_header = resid_tab->header;
04804 check( size = xsh_resid_tab_get_size( resid_tab ) ) ;
04805 xsh_msg ( " Resid Table Size: %d", size ) ;
04806 XSH_CALLOC( msp_coord, coord, size);
04807
04808
04809
04810 check( vlambda = xsh_resid_tab_get_lambda_data( resid_tab));
04811 check( vorder = xsh_resid_tab_get_order_data( resid_tab ));
04812 check( slitindex = xsh_resid_tab_get_slit_index( resid_tab));
04813 check( thpre_x = xsh_resid_tab_get_thpre_x_data( resid_tab ));
04814 check( thpre_y = xsh_resid_tab_get_thpre_y_data( resid_tab ));
04815 check( xgauss = xsh_resid_tab_get_xgauss_data( resid_tab ));
04816 check( ygauss = xsh_resid_tab_get_ygauss_data( resid_tab ));
04817
04818
04819
04820 if (p_xs_3->arm==0) {
04821 wav_den_bin_sz=25.0;
04822 }
04823 else if (p_xs_3->arm==1) {
04824 wav_den_bin_sz=50.0;
04825 }
04826 else {
04827 wav_den_bin_sz=125.0;
04828 }
04829 for (kk=0;kk<40;kk+=1) {
04830 wav_den[kk]=0;
04831 }
04832 for (kk=0;kk<size;kk+=1){
04833 if (vlambda[kk]>0.0) {
04834 wav_den[(int)(vorder[kk])]+=1;
04835 }
04836
04837 }
04838
04839 kk=0;
04840 for (jj=0;jj<size;jj+=1){
04841
04842 msp_coord[kk].wave=*(vlambda+jj);
04843 msp_coord[kk].x= *(xgauss+jj) ;
04844
04845
04846
04847
04848
04849 msp_coord[kk].flux=10000.0/(float)(wav_den[(int)(vorder[jj])]);
04850 msp_coord[kk].order=(int)(*(vorder+jj));
04851 msp_coord[kk].y= *(ygauss+jj);
04852 msp_coord[kk].slit_pos=slitindex[jj];
04853 msp_coord[kk].counter=jj;
04854 msp_coord[kk].arm=p_xs_3->arm;
04855
04856 kk+=1;
04857 }
04858
04859
04860 ref_ind=xsh_alloc2Darray(8,7);
04861 if (p_xs_3->arm==0) {
04862 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
04863 }
04864 else if (p_xs_3->arm==1) {
04865 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
04866 }
04867 else if (p_xs_3->arm==2) {
04868 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
04869 }
04870 else {
04871 printf("Arm not set. \n");
04872 return NULL;
04873 }
04874
04875 xsh_3_init(p_xs_3);
04876
04877
04878 p_wlarray=xsh_alloc1Darray(size);
04879 for(ii=0;ii<size;ii++) {
04880 p_wlarray[ii]=msp_coord[ii].wave*1e-6;
04881 }
04882
04883 #ifdef DEBUG
04884 printf ("Before anneal: \n");
04885 for (ii=0;ii<adim;ii++) {
04886 printf("%d %s %lf \n", aname[ii], (p_all_par+aname[ii])->name, abest[ii]);
04887 }
04888
04889
04890
04891 morder_cnt=0;
04892 for (jj=0;jj<size;jj++) {
04893 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->slit[msp_coord[jj].slit_pos]*p_xs_3->slit_scale;
04894 morder_cnt=msp_coord[jj].order;
04895 xsh_3_init(p_xs_3);
04896
04897 xsh_3_eval(p_wlarray[jj],morder_cnt,ref_ind,p_xs_3);
04898 xsh_3_detpix(p_xs_3);
04899 printf("check %d %lf %lf %d %d %lf %lf %lf %lf %d %lf %lf \n",
04900 jj,p_wlarray[jj],p_xs_3->es_y_tot,p_xs_3->chippix[0],
04901 msp_coord[jj].arm,p_xs_3->xpospix,p_xs_3->ypospix,
04902 msp_coord[jj].x-p_xs_3->xpospix,msp_coord[jj].y-p_xs_3->ypospix,
04903 msp_coord[jj].order, p_xs_3->xdet, p_xs_3->ydet);
04904 }
04905 #endif
04906
04907 MODEL_CONFIG_plist=cpl_propertylist_new();
04908 check(xsh_model_compute_residuals(p_xs_3,msp_coord,p_wlarray,ref_ind,size,0,
04909 resid_header,resid_frame,
04910 &MODEL_CONFIG_plist));
04911
04912 if ((conf_tab = xsh_model_anneal_comp(p_all_par,
04913 adim,
04914 abest,
04915 amin,
04916 amax,
04917 aname,
04918 p_xs_3,
04919 size,
04920 msp_coord,
04921 p_wlarray,
04922 ref_ind,
04923 maxit)) == NULL) {
04924 xsh_free2Darray(ref_ind,8);
04925 cpl_free(p_wlarray);
04926 return NULL ;
04927 }
04928
04929 check(xsh_model_compute_residuals(p_xs_3,msp_coord,p_wlarray,ref_ind,size,1,
04930 resid_header,resid_frame,
04931 &MODEL_CONFIG_plist));
04932
04933 check(tag=xsh_get_tag_opt_mod_cfg(p_xs_3,rec_id));
04934
04935
04936 sprintf(out_cfg_filename,"%s.fits",tag);
04937
04938 xsh_msg_dbg_medium("Save the table") ;
04939 xsh_msg_dbg_medium("out file=%s",out_cfg_filename);
04940
04941 check(xsh_pfits_set_pcatg(MODEL_CONFIG_plist, tag ) ) ;
04942
04943 check(cpl_table_save(conf_tab, MODEL_CONFIG_plist, NULL, out_cfg_filename, CPL_IO_DEFAULT));
04944
04945
04946 check(MODEL_CONF_OPT_frame=xsh_frame_product(out_cfg_filename,
04947 tag,
04948 CPL_FRAME_TYPE_TABLE,
04949 CPL_FRAME_GROUP_PRODUCT,
04950 CPL_FRAME_LEVEL_FINAL));
04951 xsh_msg_dbg_medium("optimised model parameter table %s %s", cpl_frame_get_filename(MODEL_CONF_OPT_frame),cpl_frame_get_tag(MODEL_CONF_OPT_frame));
04952
04953 cleanup:
04954
04955 xsh_free_table(&resid_tbl);
04956 xsh_free_table(&conf_tab);
04957 xsh_resid_tab_free(&resid_tab);
04958 if ( cpl_error_get_code() != CPL_ERROR_NONE){
04959 xsh_free_frame( &MODEL_CONF_OPT_frame);
04960 }
04961 XSH_FREE( msp_coord);
04962 XSH_FREE( p_wlarray);
04963 xsh_free_propertylist( &MODEL_CONFIG_plist);
04964
04965 if (ref_ind!=NULL){
04966 xsh_free2Darray(ref_ind,8);
04967 }
04968 return MODEL_CONF_OPT_frame;
04969 }
04970
04971
04988
04989 int xsh_model_open_param(char * listname,
04990 char * findname,
04991 int * ref,
04992 int setref,
04993 double * best,
04994 double setbest,
04995 double * min,
04996 double * max,
04997 double gfac,
04998 double fac,
04999 int counter)
05000 {
05001 if (strncmp(listname,findname,strlen(findname))==0) {
05002 *ref=setref;
05003 *best=setbest;
05004 *min=setbest-(fac*gfac);
05005 *max=setbest=(fac*gfac);
05006 counter++;
05007 }
05008 return counter;
05009 }
05010
05020
05021
05022 double xsh_model_sellmeier_ext(int arm, double temper, double lam_sqr)
05023 {
05024 typedef DOUBLE sellmeier_mat[3][5];
05025 int ii, jj;
05026 double ref_ind,top,bottom;
05027 double T[5];
05028 sellmeier_mat S_UVB={{1.10127, -4.94251E-05, 5.27414E-07, -1.59700E-09, 1.75949E-12},
05029 {1.78752E-05, 4.76391E-05, -4.49019E-07, 1.44546E-09, -1.57223E-12},
05030 {0.793552, -1.27815E-03, 1.84595E-05, -9.20275E-08, 1.48829E-10}};
05031 sellmeier_mat L_UVB={{-0.08906, 9.08730E-06, -6.53638E-08, 7.77072E-11, 6.84605E-14},
05032 {0.297562, -859578E-04, 6.59069E-06, -1.09482E-08, 7.85145E-13},
05033 {9.34454, -7.09788E-03, 1.01968E-04, -5.07660E-07, 8.21348E-10}};
05034
05035 sellmeier_mat S_NIR={{0.104940144,5.40E-06,3.23E-08,1.83E-13,-3.60E-14},
05036 {0.996335571,-3.29E-06,1.48E-08,-3.01E-11,4.99E-14},
05037 {0.832484961,6.38E-04,-2.40E-06,6.10E-10,4.77E-12}};
05038 sellmeier_mat L_NIR={{-3.07E-03,-2.56E-05,6.21E-07,-2.54E-09,2.88E-12},
05039 {9.40E-02,-1.59E-06,1.28E-08,1.82E-12,-3.01E-14},
05040 {9.598633568,3.15E-03,-1.22E-05,5.48E-09,1.96E-11}};
05041 T[0]=1.0;
05042 T[1]=temper;
05043 T[2]=temper*temper;
05044 T[3]=T[2]*temper;
05045 T[4]=T[3]*temper;
05046 ref_ind=1.0;
05047 if (arm==0) {
05048 for (ii=0;ii<3;ii++) {
05049 top=bottom=0.0;
05050 for (jj=0;jj<5;jj++) {
05051 top+=S_UVB[ii][jj]*T[jj];
05052 bottom+=L_UVB[ii][jj]*T[jj];
05053 }
05054 ref_ind+=(top*lam_sqr)/(lam_sqr-(bottom*bottom));
05055 }
05056 }
05057 else if (arm==2) {
05058 for (ii=0;ii<3;ii++) {
05059 top=bottom=0.0;
05060 for (jj=0;jj<5;jj++) {
05061 top+=S_NIR[ii][jj]*T[jj];
05062 bottom+=L_NIR[ii][jj]*T[jj];
05063 }
05064 ref_ind+=(top*lam_sqr)/(lam_sqr-(bottom*bottom));
05065 }
05066 }
05067 return sqrt(ref_ind);
05068 }
05069
05070
05079
05080 double
05081 xsh_model_ref_ind_air(double temper, double lam_sqr)
05082 {
05083 double n288=0, n_air=0;
05084 n288=1.0+(6432.8+((2949810.0*lam_sqr)/(146.0*lam_sqr-1.0))+((25540.0*lam_sqr)/(41.0*lam_sqr-1.0)))*1.0E-08;
05085 n_air=1.0+(n288-1.0)/(1.0+0.0034785*(temper-288.0));
05086 return n_air;
05087 }
05088
05089
05098
05099 cpl_vector *
05100 xsh_model_refining_detect(
05101 const cpl_vector * in,
05102 int fwhm,
05103 double sigma,
05104 int display)
05105 {
05106 cpl_vector * filtered =NULL;
05107 cpl_vector * spec_clean =NULL;
05108 double * pspec_clean =NULL;
05109 int filt_size =0;
05110 cpl_vector * conv_kernel =NULL;
05111 cpl_vector * big_detected =NULL;
05112 double * pbig_detected =NULL;
05113 cpl_vector * detected =NULL;
05114 double * pdetected =NULL;
05115 double max=0, med=0, stdev=0, cur_val=0 ;
05116 int nb_det=0, nb_samples=0 ;
05117 int i=0;
05118 int j=0;
05119
05120
05121 if (in == NULL) return NULL ;
05122
05123
05124
05125 check(nb_samples = cpl_vector_get_size(in)) ;
05126 filt_size = 50 ;
05127
05128
05129
05130 if ((filtered=cpl_vector_filter_median_create(in, filt_size))==NULL){
05131 cpl_msg_error(__func__, "Cannot filter the spectrum") ;
05132 return NULL ;
05133 }
05134 spec_clean = cpl_vector_duplicate(in) ;
05135 cpl_vector_subtract(spec_clean, filtered) ;
05136 cpl_vector_delete(filtered) ;
05137
05138
05139 if (display) {
05140 cpl_plot_vector(
05141 "set grid;set xlabel 'Position (pixels)';set ylabel 'Intensity (ADU)';",
05142 "t 'Filtered extracted spectrum' w lines", "", spec_clean);
05143 }
05144
05145
05146
05147
05148 if ((conv_kernel = cpl_wlcalib_xc_convolve_create_kernel(fwhm,
05149 fwhm)) == NULL) {
05150 cpl_msg_error(cpl_func, "Cannot create convolution kernel") ;
05151 cpl_vector_delete(spec_clean) ;
05152 return NULL ;
05153 }
05154
05155
05156 if (cpl_wlcalib_xc_convolve(spec_clean, conv_kernel)) {
05157 cpl_msg_error(cpl_func, "Cannot smoothe the signal");
05158 cpl_vector_delete(spec_clean) ;
05159 cpl_vector_delete(conv_kernel) ;
05160 return NULL ;
05161 }
05162 cpl_vector_delete(conv_kernel) ;
05163
05164
05165 if (display) {
05166 cpl_plot_vector(
05167 "set grid;set xlabel 'Position (pixels)';set ylabel 'Intensity (ADU)';",
05168 "t 'Convolved extracted spectrum' w lines", "", spec_clean);
05169 }
05170
05171
05172 big_detected = cpl_vector_duplicate(spec_clean) ;
05173 pbig_detected = cpl_vector_get_data(big_detected) ;
05174 pspec_clean = cpl_vector_get_data(spec_clean) ;
05175
05176
05177 pspec_clean[0] = pspec_clean[nb_samples-1] = 0.0 ;
05178
05179
05180 max = cpl_vector_get_max(spec_clean) ;
05181 stdev = cpl_vector_get_stdev(spec_clean) ;
05182 med = cpl_vector_get_median_const(spec_clean) ;
05183
05184
05185 nb_det = 0 ;
05186 while (max > med + stdev * sigma) {
05187
05188 i=0 ;
05189 while (pspec_clean[i] < max) i++ ;
05190 if (i<=0 || i>=nb_samples-1) break ;
05191
05192
05193 pbig_detected[nb_det] = (pspec_clean[i]*i +
05194 pspec_clean[i-1]*(i-1) + pspec_clean[i+1]*(i+1)) /
05195 (pspec_clean[i]+pspec_clean[i-1]+pspec_clean[i+1]);
05196
05197
05198 pbig_detected[nb_det] ++ ;
05199
05200
05201 nb_det ++ ;
05202
05203
05204 j = i-1 ;
05205 cur_val = pspec_clean[i] ;
05206 while (j>=0 && pspec_clean[j] < cur_val) {
05207 cur_val = pspec_clean[j] ;
05208 pspec_clean[j] = 0.0 ;
05209 j-- ;
05210 }
05211
05212 j = i+1 ;
05213 cur_val = pspec_clean[i] ;
05214 while (j<=nb_samples-1 && pspec_clean[j] < cur_val) {
05215 cur_val = pspec_clean[j] ;
05216 pspec_clean[j] = 0.0 ;
05217 j++ ;
05218 }
05219
05220 pspec_clean[i] = 0.0 ;
05221
05222
05223 max = cpl_vector_get_max(spec_clean) ;
05224 stdev = cpl_vector_get_stdev(spec_clean) ;
05225 med = cpl_vector_get_median_const(spec_clean) ;
05226 }
05227 cpl_vector_delete(spec_clean) ;
05228
05229
05230 if (nb_det == 0) {
05231 detected = NULL ;
05232 } else {
05233 detected = cpl_vector_new(nb_det) ;
05234 pdetected = cpl_vector_get_data(detected) ;
05235 pbig_detected = cpl_vector_get_data(big_detected) ;
05236 for (i=0 ; i<nb_det ; i++) pdetected[i] = pbig_detected[i] ;
05237 }
05238 cpl_vector_delete(big_detected) ;
05239
05240 cleanup:
05241 if(cpl_error_get_code() != CPL_ERROR_NONE) {
05242 xsh_print_rec_status(0);
05243 }
05244
05245 return detected ;
05246 }
05247
05248
05255
05256 int
05257 xsh_model_first_anneal(cpl_parameterlist* parlist, cpl_frameset* frameset)
05258 {
05259 cpl_frame* xs_config =NULL;
05260 cpl_frame* meas_coord =NULL;
05261 cpl_parameter* param=NULL;
05262 xsh_instrument* instrument = NULL;
05263 const char* arm=NULL;
05264 int nraw=0;
05265 int nrows=0;
05266
05267
05268 cpl_table * conf_tab = NULL;
05269 int ii=0, jj=0;
05270
05271 int sizearraywavelengths=0, adim=0;
05272
05273 struct xs_3 xs_model, *p_xs_3;
05274 ann_all_par all_par[300], *p_all_par;
05275 double abest[300];
05276 double amin[300];
05277 double amax[300];
05278 int aname[300];
05279 int coord_switch=0;
05280
05281
05282 coord *msp_coord = NULL;
05283 DOUBLE ** ref_ind = NULL;
05284 DOUBLE *p_wlarray = NULL;
05285 cpl_table * meas_coord_temp=NULL;
05286 double ann_fac=1.0000000000001;
05287
05288 int MAXIT=10;
05289
05290 FILE* file_list=NULL;
05291 int cnt=0;
05292 double* pw=NULL;
05293 double* px=NULL;
05294 double* py=NULL;
05295 int* po=NULL;
05296 const char * name_i=NULL ;
05297 float wave=0;
05298 float x=0;
05299 float y=0;
05300 int ord=0;
05301
05302 if( (nraw=cpl_frameset_get_size(frameset)) ==0) goto cleanup;
05303
05304 param = cpl_parameterlist_find(parlist,"xsh.xsh_model_compute.arm");
05305 arm = cpl_parameter_get_string(param);
05306 xsh_msg("User selected arm '%s'. Recipe expects '%s' input data",arm,arm);
05307
05308 param = cpl_parameterlist_find(parlist,"xsh.xsh_model_compute.niter");
05309 MAXIT = cpl_parameter_get_int(param);
05310
05311 coord_switch = cpl_parameter_get_int(cpl_parameterlist_find(parlist,"xsh.xsh_model_compute.coord_frame"));
05312
05313 if (arm==NULL) {
05314 xsh_msg_error("arm parameter value not set. exit!");
05315 goto cleanup;
05316 }
05317
05318 instrument=xsh_instrument_new();
05319 if(strcmp(arm,"uvb") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_UVB);
05320 if(strcmp(arm,"vis") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_VIS);
05321 if(strcmp(arm,"nir") == 0) xsh_instrument_set_arm(instrument,XSH_ARM_NIR);
05322
05323
05324
05325
05326
05327 if((xs_config = xsh_find_frame_with_tag(frameset,XSH_MOD_CFG,
05328 instrument)) == NULL) {
05329 xsh_msg_error("Frame %s not found",
05330 xsh_stringcat_any( XSH_MOD_CFG,
05331 xsh_instrument_arm_tostring(instrument),
05332 NULL ) ) ;
05333 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
05334 goto cleanup;
05335 }
05336
05337 if((meas_coord = xsh_find_frame_with_tag(frameset,XSH_MEASCOORD,
05338 instrument))==NULL) {
05339 xsh_msg_warning("Frame %s not found",
05340 xsh_stringcat_any(XSH_MEASCOORD,
05341 xsh_instrument_arm_tostring(instrument),
05342 NULL ));
05343
05344
05345 check(param = cpl_parameterlist_find(parlist,
05346 "xsh.xsh_model_compute.name_i"));
05347 check(name_i = cpl_parameter_get_string(param));
05348 xsh_msg("name_i=%s",name_i);
05349 if ( NULL == (file_list = fopen (name_i, "r" ) ) )
05350 {
05351 xsh_msg_error("cannot open %s\n", name_i) ;
05352 goto cleanup ;
05353 }
05354
05355 cnt = 0 ;
05356 while ( fscanf( file_list, "%g %g %g %d",&wave, &x, &y, &ord ) != EOF )
05357 {
05358 cnt ++ ;
05359 }
05360 fclose(file_list);
05361
05362 nrows= cnt;
05363 check(meas_coord_temp=cpl_table_new(nrows));
05364 check(cpl_table_new_column(meas_coord_temp,"Wavelength",CPL_TYPE_DOUBLE));
05365 check(cpl_table_new_column(meas_coord_temp,"x",CPL_TYPE_DOUBLE));
05366 check(cpl_table_new_column(meas_coord_temp,"y",CPL_TYPE_DOUBLE));
05367 check(cpl_table_new_column(meas_coord_temp,"ORDER",CPL_TYPE_INT));
05368
05369 check(cpl_table_fill_column_window(meas_coord_temp,"Wavelength",0,nrows,-1));
05370 check(cpl_table_fill_column_window(meas_coord_temp,"x",0,nrows,-1));
05371 check(cpl_table_fill_column_window(meas_coord_temp,"y",0,nrows,-1));
05372 check(cpl_table_fill_column_window(meas_coord_temp,"ORDER",0,nrows,-1));
05373
05374 check(pw=cpl_table_get_data_double(meas_coord_temp,"Wavelength"));
05375 check(px=cpl_table_get_data_double(meas_coord_temp,"x"));
05376 check(py=cpl_table_get_data_double(meas_coord_temp,"y"));
05377 check(po=cpl_table_get_data_int(meas_coord_temp,"ORDER"));
05378
05379 if ( NULL == (file_list = fopen (name_i, "r" ) ) )
05380 {
05381 xsh_msg_error("cannot open %s\n", name_i) ;
05382 goto cleanup ;
05383 }
05384
05385 cnt=0;
05386 while ( fscanf( file_list, "%g %g %g %d",&wave, &x, &y, &ord ) != EOF )
05387 {
05388 pw[cnt]=wave;
05389 px[cnt]=x;
05390 py[cnt]=y;
05391 po[cnt]=ord;
05392
05393 cnt ++ ;
05394 }
05395
05396 fclose(file_list);
05397 check(cpl_table_save(meas_coord_temp, NULL, NULL,"lin_xy_hand.fits",
05398 CPL_IO_DEFAULT));
05399
05400
05401 check(meas_coord=xsh_frame_product("lin_xy_hand.fits",
05402 "LIN_XY_HAND",
05403 CPL_FRAME_TYPE_TABLE,
05404 CPL_FRAME_GROUP_CALIB,
05405 CPL_FRAME_LEVEL_TEMPORARY));
05406
05407
05408 }
05409
05410
05411
05412 p_xs_3=&xs_model;
05413 p_all_par=&all_par[0];
05414
05415 cknull_msg(meas_coord_temp=cpl_table_load(cpl_frame_get_filename(meas_coord),1,0),"Cannot load table %s",cpl_frame_get_filename(meas_coord));
05416 check(sizearraywavelengths=cpl_table_get_nrow(meas_coord_temp));
05417 XSH_CALLOC(msp_coord, coord, sizearraywavelengths+5);
05418
05419 adim=xsh_model_readfits(abest,
05420 amin,
05421 amax,
05422 aname,
05423 cpl_frame_get_filename(xs_config),
05424 cpl_frame_get_tag(xs_config),
05425 p_xs_3,
05426 p_all_par);
05427
05428 if (adim==0) {
05429
05430 for (ii=0;ii<300;ii++) {
05431 aname[ii]=-1;
05432 abest[ii]=0.0;
05433 amin[ii]=0.0;
05434 amax[ii]=0.0;
05435 }
05436
05437
05438 jj=0;
05439 for (ii=0;ii<100;ii++) {
05440
05441
05442
05443 if (strncmp((p_all_par+ii)->name,"temper",6)==0) {
05444 aname[jj]=ii;
05445 abest[jj]=p_xs_3->temper;
05446 amin[jj]=p_xs_3->temper-(5.0*ann_fac);
05447 amax[jj]=p_xs_3->temper+(5.0*ann_fac);
05448 jj+=1;
05449 }
05450 if (strncmp((p_all_par+ii)->name,"fcol",4)==0) {
05451 aname[jj]=ii;
05452 abest[jj]=p_xs_3->fcol;
05453 amin[jj]=p_xs_3->fcol-(5.0*ann_fac);
05454 amax[jj]=p_xs_3->fcol+(5.0*ann_fac);
05455 jj+=1;
05456 }
05457 if (strncmp((p_all_par+ii)->name,"mup1",4)==0) {
05458 aname[jj]=ii;
05459 abest[jj]=p_xs_3->mup1/DEG2RAD;
05460 amin[jj]=p_xs_3->mup1/DEG2RAD-(5.0*ann_fac);
05461 amax[jj]=p_xs_3->mup1/DEG2RAD+(5.0*ann_fac);
05462 jj+=1;
05463 }
05464 if (strncmp((p_all_par+ii)->name,"nup1",4)==0) {
05465 aname[jj]=ii;
05466 abest[jj]=p_xs_3->nup1/DEG2RAD;
05467 amin[jj]=p_xs_3->nup1/DEG2RAD-(5.0*ann_fac);
05468 amax[jj]=p_xs_3->nup1/DEG2RAD+(5.0*ann_fac);
05469 jj+=1;
05470 }
05471
05472 if (p_xs_3->arm==2) {
05473
05474
05475 if (strncmp((p_all_par+ii)->name,"cmup1",5)==0) {
05476 aname[jj]=ii;
05477 abest[jj]=p_xs_3->cmup1/DEG2RAD;
05478 amin[jj]=p_xs_3->cmup1/DEG2RAD-(5.0*ann_fac);
05479 amax[jj]=p_xs_3->cmup1/DEG2RAD+(5.0*ann_fac);
05480 jj+=1;
05481 }
05482 if (strncmp((p_all_par+ii)->name,"mup3",4)==0) {
05483 aname[jj]=ii;
05484 abest[jj]=p_xs_3->mup3/DEG2RAD;
05485 amin[jj]=p_xs_3->mup3/DEG2RAD-(5.0*ann_fac);
05486 amax[jj]=p_xs_3->mup3/DEG2RAD+(5.0*ann_fac);
05487 jj+=1;
05488 }
05489 if (strncmp((p_all_par+ii)->name,"nup3",4)==0) {
05490 aname[jj]=ii;
05491 abest[jj]=p_xs_3->nup3/DEG2RAD;
05492 amin[jj]=p_xs_3->nup3/DEG2RAD-(5.0*ann_fac);
05493 amax[jj]=p_xs_3->nup3/DEG2RAD+(5.0*ann_fac);
05494 jj+=1;
05495 }
05496
05497
05498 if (strncmp((p_all_par+ii)->name,"mup5",4)==0) {
05499 aname[jj]=ii;
05500 abest[jj]=p_xs_3->mup5/DEG2RAD;
05501 amin[jj]=p_xs_3->mup5/DEG2RAD-(5.0*ann_fac);
05502 amax[jj]=p_xs_3->mup5/DEG2RAD+(5.0*ann_fac);
05503 jj+=1;
05504 }
05505 if (strncmp((p_all_par+ii)->name,"nup5",4)==0) {
05506 aname[jj]=ii;
05507 abest[jj]=p_xs_3->nup5/DEG2RAD;
05508 amin[jj]=p_xs_3->nup5/DEG2RAD-(5.0*ann_fac);
05509 amax[jj]=p_xs_3->nup5/DEG2RAD+(5.0*ann_fac);
05510 jj+=1;
05511 }
05512 }
05513
05514
05515 if (strncmp((p_all_par+ii)->name,"mug",3)==0) {
05516 aname[jj]=ii;
05517 abest[jj]=p_xs_3->mug/DEG2RAD;
05518 amin[jj]=p_xs_3->mug/DEG2RAD-(5.0*ann_fac);
05519 amax[jj]=p_xs_3->mug/DEG2RAD+(5.0*ann_fac);
05520 jj+=1;
05521 }
05522 if (strncmp((p_all_par+ii)->name,"nug",3)==0) {
05523 aname[jj]=ii;
05524 abest[jj]=p_xs_3->nug/DEG2RAD;
05525 amin[jj]=p_xs_3->nug/DEG2RAD-(5.0*ann_fac);
05526 amax[jj]=p_xs_3->nug/DEG2RAD+(5.0*ann_fac);
05527 jj+=1;
05528 }
05529
05530 if (strncmp((p_all_par+ii)->name,"taud",4)==0) {
05531 aname[jj]=ii;
05532 abest[jj]=p_xs_3->taud/DEG2RAD;
05533 amin[jj]=p_xs_3->taud/DEG2RAD-(5.0*ann_fac);
05534 amax[jj]=p_xs_3->taud/DEG2RAD+(5.0*ann_fac);
05535 jj+=1;
05536 }
05537
05538 if (strncmp((p_all_par+ii)->name,"fdet",4)==0) {
05539 abest[jj]=p_xs_3->fdet;
05540 amin[jj]=p_xs_3->fdet-(10.0*ann_fac);
05541 amax[jj]=p_xs_3->fdet+(10.0*ann_fac);
05542 aname[jj]=ii;
05543 jj+=1;
05544 }
05545
05546
05547 if (strncmp((p_all_par+ii)->name,"chipx",5)==0) {
05548 abest[jj]=p_xs_3->chipx;
05549 amin[jj]=p_xs_3->chipx-(15.0*ann_fac);
05550 amax[jj]=p_xs_3->chipx+(15.0*ann_fac);
05551 aname[jj]=ii;
05552 jj+=1;
05553 }
05554 if (strncmp((p_all_par+ii)->name,"chipy",5)==0) {
05555 abest[jj]=p_xs_3->chipy;
05556 amin[jj]=p_xs_3->chipy-(15.0*ann_fac);
05557 amax[jj]=p_xs_3->chipy+(15.0*ann_fac);
05558 aname[jj]=ii;
05559 jj+=1;
05560 }
05561
05562 if (strncmp((p_all_par+ii)->name,"sg",2)==0) {
05563 abest[jj]=p_xs_3->sg;
05564 amin[jj]=p_xs_3->sg-(1.0*ann_fac);
05565 amax[jj]=p_xs_3->sg+(1.0*ann_fac);
05566 aname[jj]=ii;
05567 jj+=1;
05568 }
05569 }
05570 adim=jj;
05571 }
05572
05573 ref_ind=xsh_alloc2Darray(8,7);
05574
05575 if (p_xs_3->arm==0) {
05576 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
05577 }
05578 else if (p_xs_3->arm==1) {
05579 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
05580 }
05581 else if (p_xs_3->arm==2) {
05582 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
05583 }
05584 else {
05585 printf("Arm not set. \n");
05586 return 1;
05587 }
05588
05589
05590
05591 xsh_3_init(p_xs_3);
05592
05593
05594 p_wlarray=xsh_alloc1Darray(sizearraywavelengths);
05595 for (ii=0; ii<sizearraywavelengths; ii++) {
05596 msp_coord[ii].counter=ii;
05597 if (coord_switch==1) {
05598 msp_coord[ii].x=cpl_table_get_double(meas_coord_temp,"x",ii,NULL);
05599 msp_coord[ii].y=cpl_table_get_double(meas_coord_temp,"y",ii,NULL);
05600 }
05601 else {
05602
05603
05604 if (p_xs_3->arm==0) {
05605 msp_coord[ii].x=2097-cpl_table_get_double(meas_coord_temp,"x",ii,NULL);
05606 msp_coord[ii].y=3001-cpl_table_get_double(meas_coord_temp,"y",ii,NULL);
05607 }
05608 if (p_xs_3->arm==1) {
05609 msp_coord[ii].x=-10.0+cpl_table_get_double(meas_coord_temp,"x",ii,NULL);
05610 msp_coord[ii].y=0.0+cpl_table_get_double(meas_coord_temp,"y",ii,NULL);
05611 }
05612 if (p_xs_3->arm==2) {
05613 msp_coord[ii].x=-20.0+cpl_table_get_double(meas_coord_temp,"y",ii,NULL);
05614 msp_coord[ii].y=2045.0-cpl_table_get_double(meas_coord_temp,"x",ii,NULL);
05615 printf("%lf %lf \n",msp_coord[ii].x,msp_coord[ii].y);
05616 }
05617 }
05618 msp_coord[ii].arm=p_xs_3->arm;
05619 msp_coord[ii].flux=100.0;
05620 msp_coord[ii].slit_pos=4;
05621 msp_coord[ii].wave=cpl_table_get_double(meas_coord_temp,"Wavelength",ii,NULL);
05622 msp_coord[ii].order=cpl_table_get_int(meas_coord_temp,"ORDER",ii,NULL);
05623 p_wlarray[ii]=msp_coord[ii].wave*1e-6;
05624 }
05625 xsh_free_table(&meas_coord_temp);
05626
05627 #ifdef DEBUG
05628 printf ("Before anneal: \n");
05629 for (ii=0;ii<adim;ii++) {
05630 printf("%d %s %lf \n", aname[ii], (p_all_par+aname[ii])->name, abest[ii]);
05631 }
05632
05633 xsh_showmatrix(p_xs_3->e_slit);
05634
05635
05636 for (jj=0;jj<sizearraywavelengths;jj++) {
05637 p_xs_3->es_y_tot=p_xs_3->es_y+p_xs_3->slit[msp_coord[jj].slit_pos]*p_xs_3->slit_scale;
05638 morder=msp_coord[jj].order;
05639 xsh_3_init(p_xs_3);
05640 xsh_3_eval(p_wlarray[jj],morder,ref_ind,p_xs_3);
05641 xsh_3_detpix(p_xs_3);
05642 printf("check %d %lf %lf %d %d %lf %lf %lf %lf %d\n",
05643 jj,p_wlarray[jj],p_xs_3->es_y_tot,p_xs_3->chippix[0],
05644 msp_coord[jj].arm,p_xs_3->xpospix,p_xs_3->ypospix,
05645 msp_coord[jj].x-p_xs_3->xpospix,msp_coord[jj].y-p_xs_3->ypospix,
05646 msp_coord[jj].order);
05647 }
05648 #endif
05649
05650
05651 conf_tab = xsh_model_anneal_comp( p_all_par,
05652 adim,
05653 abest,
05654 amin,
05655 amax,
05656 aname,
05657 p_xs_3,
05658 sizearraywavelengths,
05659 msp_coord,
05660 p_wlarray,
05661 ref_ind,
05662 MAXIT);
05663
05664
05665 cpl_msg_info(__func__, "Save the products") ;
05666 cpl_msg_indent_more() ;
05667 if (xsh_model_first_anneal_save((const cpl_table*)conf_tab, instrument,
05668 parlist,frameset) == -1) {
05669 cpl_msg_error(__func__, "Cannot save products") ;
05670 cpl_msg_indent_less() ;
05671 xsh_free_table(&conf_tab) ;
05672 return -1 ;
05673 }
05674 xsh_free_table(&conf_tab) ;
05675 cpl_msg_indent_less() ;
05676 goto cleanup;
05677
05678 cleanup:
05679 xsh_instrument_free(&instrument);
05680 if(ref_ind!=NULL) xsh_free2Darray(ref_ind,8);
05681 cpl_free(p_wlarray);
05682 xsh_free_table(&conf_tab) ;
05683
05684
05685 if (cpl_error_get_code())
05686 return -1 ;
05687 else
05688 return 0 ;
05689 }
05690
05691
05700
05701 int
05702 xsh_model_first_anneal_save(
05703 const cpl_table* out_table,
05704 xsh_instrument* instr,
05705 cpl_parameterlist* parlist,
05706 cpl_frameset* set)
05707 {
05708 char name_o[512] ;
05709 cpl_propertylist* plist=NULL ;
05710 cpl_frame* product_frame=NULL ;
05711 cpl_frameset* raws=NULL;
05712 cpl_frame* ref_frame=NULL;
05713
05714 const char* pro_catg=xsh_get_tag_from_arm(XSH_MOD_CFG_FAN,instr);
05715
05716 raws=cpl_frameset_new();
05717 check(xsh_dfs_extract_raw_frames(set,raws));
05718 check(ref_frame=cpl_frameset_get_first(raws));
05719 check(plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),0));
05720 xsh_free_frameset(&raws);
05721
05722
05723 sprintf(name_o,"%s%s",
05724 xsh_get_tag_from_arm(XSH_MOD_CFG_FAN,instr),".fits") ;
05725
05726 cpl_msg_info(__func__, "Writing %s" , name_o) ;
05727
05728
05729 cpl_propertylist_append_int(plist,"Num_pinh",9);
05730
05731
05732
05733 check(product_frame=xsh_frame_product(name_o,pro_catg,CPL_FRAME_TYPE_TABLE,
05734 CPL_FRAME_GROUP_PRODUCT,
05735 CPL_FRAME_LEVEL_FINAL));
05736
05737 if (cpl_table_save(out_table, plist, NULL, name_o,
05738 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
05739 cpl_msg_error(__func__, "Cannot save the product");
05740 xsh_free_frame(&product_frame) ;
05741 xsh_free_propertylist(&plist) ;
05742 return -1 ;
05743 }
05744
05745 check(xsh_add_product_table(product_frame,set,parlist,"xsh_startup",
05746 instr,NULL));
05747
05748 cleanup:
05749 xsh_free_propertylist(&plist) ;
05750
05751
05752 if (cpl_error_get_code())
05753 return -1 ;
05754 else
05755 return 0 ;
05756
05757 }
05758
05776
05777 int
05778 xsh_model_offset(DOUBLE slit_pix_shift,
05779 DOUBLE disp_pix_shift,
05780 struct xs_3* p_xs_3)
05781 {
05782 if (p_xs_3->arm!=2) {
05783 p_xs_3->chipy+=p_xs_3->pix_Y*slit_pix_shift;
05784 p_xs_3->chipx+=p_xs_3->pix_X*disp_pix_shift;
05785 }
05786 else {
05787 p_xs_3->chipx-=p_xs_3->pix_X*slit_pix_shift;
05788 p_xs_3->chipy-=p_xs_3->pix_Y*disp_pix_shift;
05789 }
05790 return 0;
05791 }
05792
05793
05794 static int
05795 comp_center2( const void * one, const void * two )
05796 {
05797 CENTER_ORDER * first = (CENTER_ORDER *)one ;
05798 CENTER_ORDER * secnd = (CENTER_ORDER *)two ;
05799
05800 if ( first->order < secnd->order ) return -1 ;
05801 else if ( first->order > secnd->order ) return 1 ;
05802 else return 0 ;
05803 }
05804
05805 static void
05806 save_centers(CENTER_ORDER * pcent, int fpos, int npos, const char * arm )
05807 {
05808 static FILE * fgnu = NULL, * freg = NULL ;
05809 static const char * gnuname =NULL ;
05810 int first_call =0;
05811 FILE *fout ;
05812 char fname[32] ;
05813 int i=0, order=0 ;
05814 CENTER_ORDER * ppcent=NULL ;
05815
05816 if ( fgnu == NULL ) {
05817 gnuname = xsh_stringcat_any( "all_orders_", arm, ".gnu", NULL ) ;
05818 fgnu = fopen( gnuname, "w" ) ;
05819 fprintf( fgnu, "set term x11\nplot " ) ;
05820 first_call = 1 ;
05821 }
05822 else first_call = 0 ;
05823 ppcent = pcent + fpos ;
05824 order = ppcent->order ;
05825 sprintf( fname, "order_%s_%02d.dat", arm, order ) ;
05826 fout = fopen( fname, "w" ) ;
05827
05828 if ( !first_call ) fprintf( fgnu, "," ) ;
05829 fprintf( fgnu, "'%s' u 1:2 w points pt 5 t''", fname ) ;
05830
05831 for( i = fpos ; i<npos; i++, ppcent++ )
05832 fprintf( fout, "%d %d %.3lf\n",
05833 ppcent->pos_x, ppcent->pos_y, ppcent->flux ) ;
05834
05835 fclose( fout ) ;
05836 if ( order == 0 ) {
05837 fprintf( fgnu, "\n" ) ;
05838 fclose( fgnu ) ;
05839 }
05840
05841
05842 if ( freg == NULL ) {
05843 freg = fopen( "order_create.reg", "w" ) ;
05844 }
05845 fprintf( freg, "# Region file format: DS9 version 4.0\n" ) ;
05846 fprintf( freg, "global color=red font=\"helvetica 4 normal\"select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 source\n" ) ;
05847 fprintf( freg, "image\n# RED center_x center_y (pixels)\n" ) ;
05848 for( ppcent = pcent+fpos, i = fpos ; i <npos ; i++, ppcent++ ) {
05849 fprintf( freg, "point(%d.,%d.) #point=cross color=red font=\"helvetica 4 normal\"\n", ppcent->pos_x, ppcent->pos_y ) ;
05850 }
05851 if ( order == 0 ) {
05852 fclose( freg ) ;
05853 }
05854 }
05855
05866 void
05867 xsh_order_edge_list_fit(xsh_order_list *list,
05868 int size,
05869 double* order,
05870 double* posx,
05871 double* posy,
05872 int deg_poly,
05873 int edge)
05874 {
05875 int ordersize=0;
05876 int i=0;
05877 int nborder=0;
05878 int nb_keep_order=0;
05879 cpl_vector *vx = NULL;
05880 cpl_vector *vy = NULL;
05881
05882 XSH_ASSURE_NOT_NULL( list);
05883 XSH_ASSURE_NOT_NULL( order);
05884 XSH_ASSURE_NOT_NULL( posx);
05885 XSH_ASSURE_NOT_NULL( posy);
05886 XSH_ASSURE_NOT_ILLEGAL( deg_poly >= 0);
05887
05888 xsh_msg("List size=%d",size);
05889 xsh_msg("Fit a polynomial of degree %d by order",deg_poly);
05890 xsh_msg("Search from order %d to %d", list->absorder_min,
05891 list->absorder_max);
05892 for(i=1; i <= size; i++) {
05893 if ( i < size && fabs(order[i-1] - order[i] ) < 0.0001) {
05894 ordersize++;
05895 }
05896 else {
05897 int absorder = order[i-1];
05898
05899 if( (absorder >= list->absorder_min) &&
05900 (absorder <= list->absorder_max) ){
05901
05902 ordersize++;
05903 check( vx = cpl_vector_wrap( ordersize, &(posx[i-ordersize])));
05904 check( vy = cpl_vector_wrap( ordersize, &(posy[i-ordersize])));
05905 xsh_msg_dbg_low("%d) absorder %lg nbpoints %d",
05906 nborder+1, order[i-1],ordersize);
05907 XSH_ASSURE_NOT_ILLEGAL_MSG(ordersize > deg_poly,
05908 "You must have more points to fit correctly this order");
05909 if (edge==-1) {
05910 check( list->list[nb_keep_order].edguppoly =
05911 xsh_polynomial_fit_1d_create(vy, vx, deg_poly,NULL));
05912 } else if (edge==0) {
05913 check( list->list[nb_keep_order].cenpoly =
05914 xsh_polynomial_fit_1d_create(vy, vx, deg_poly,NULL));
05915 } else if (edge==1) {
05916 check( list->list[nb_keep_order].edglopoly =
05917 xsh_polynomial_fit_1d_create(vy, vx, deg_poly,NULL));
05918 }
05919 list->list[nb_keep_order].order = nborder;
05920 list->list[nb_keep_order].absorder = (int)(order[i-1]);
05921
05922 check( xsh_unwrap_vector(&vx));
05923 check( xsh_unwrap_vector(&vy));
05924 nb_keep_order++;
05925 }
05926 else{
05927 xsh_msg("WARNING skipping absorder %d because is not in range",
05928 absorder);
05929 }
05930 nborder++;
05931 ordersize = 0;
05932 }
05933 }
05934 XSH_ASSURE_NOT_ILLEGAL( list->size == nb_keep_order);
05935 cleanup:
05936 xsh_unwrap_vector(&vx);
05937 xsh_unwrap_vector(&vy);
05938 return;
05939 }
05940
05941 static void
05942 fit_order_edge_list( xsh_order_list * list,
05943 int npos,
05944 CENTER_ORDER * pcent,
05945 int degree,
05946 int edge)
05947 {
05948 double * vorder = NULL, * po =NULL;
05949 double * pos_x = NULL, * px =NULL;
05950 double * pos_y = NULL, * py =NULL;
05951 int i =0;
05952
05953 XSH_CALLOC( vorder, double, npos ) ;
05954 XSH_CALLOC( pos_x, double, npos ) ;
05955 XSH_CALLOC( pos_y, double, npos ) ;
05956
05957 po = vorder ;
05958 px = pos_x ;
05959 py = pos_y ;
05960 for( i = 0 ; i<npos ; i++, po++, px++, py++, pcent++ ) {
05961 *po = pcent->order ;
05962 *px = pcent->pos_x ;
05963 *py = pcent->pos_y ;
05964 }
05965
05966 xsh_order_edge_list_fit(list, npos, vorder, pos_x, pos_y, degree, edge) ;
05967
05968 cleanup:
05969 XSH_FREE( vorder ) ;
05970 XSH_FREE( pos_x ) ;
05971 XSH_FREE( pos_y ) ;
05972 return ;
05973 }
05974
05975
05976
05988
05989 cpl_frame*
05990 xsh_model_order_edges_tab_create(xsh_xs_3* p_xs_3,
05991 const char* tab_filename)
05992 {
05993 int morder_cnt=0;
05994 DOUBLE lambda=0;
05995 DOUBLE lambda_nm=0;
05996 DOUBLE blaze_wav=0, lam_min=0, lam_max=0, lam_inc=0;
05997
05998 int ii=0;
05999
06000 int iord=0,fpos=0,edge=0;
06001 double es_x_config=0;
06002 DOUBLE** ref_ind;
06003 cpl_propertylist* plist=NULL;
06004 CENTER_ORDER * centers = NULL ;
06005 xsh_order_list * res_list = NULL ;
06006 int low_y=0,up_y=0;
06007
06008
06009 cpl_frame* order_edges_frame=NULL;
06010
06011 xsh_instrument* instr=NULL;
06012 const char* tag=NULL;
06013
06014 char filename[256];
06015
06016 XSH_ASSURE_NOT_NULL(p_xs_3);
06017 XSH_ASSURE_NOT_NULL(tab_filename);
06018
06019
06020
06021
06022
06023
06024
06025
06026 ref_ind=xsh_alloc2Darray(8,7);
06027 instr = xsh_instrument_new() ;
06028
06029 if (p_xs_3->arm==0) {
06030 tag="XSH_ORDER_TAB_EDGES_SLIT_UVB";
06031 xsh_ref_ind_read(0,ref_ind,p_xs_3->temper);
06032 xsh_instrument_set_arm(instr,XSH_ARM_UVB);
06033 }
06034 else if (p_xs_3->arm==1) {
06035 tag="XSH_ORDER_TAB_EDGES_VIS";
06036 xsh_ref_ind_read(1,ref_ind,p_xs_3->temper);
06037 xsh_instrument_set_arm(instr,XSH_ARM_VIS);
06038 }
06039 else {
06040 tag="XSH_ORDER_TAB_EDGES_NIR";
06041 xsh_ref_ind_read(2,ref_ind,p_xs_3->t_ir_p2);
06042 xsh_instrument_set_arm(instr,XSH_ARM_NIR);
06043 }
06044
06045 check(res_list=xsh_order_list_create(instr));
06046
06047
06048
06049
06050
06051 xsh_3_init(p_xs_3);
06052 es_x_config=p_xs_3->es_x;
06053
06054 for (edge=-1;edge<2;edge++) {
06055 XSH_CALLOC(centers,CENTER_ORDER,((p_xs_3->morder_max-p_xs_3->morder_min+1)*2)*100);
06056 ii=0;
06057 iord=0;
06058 fpos=0;
06059 p_xs_3->es_y_tot=p_xs_3->es_y+fabs(edge)*p_xs_3->es_s/2.0;
06060 for (morder_cnt=p_xs_3->morder_min; morder_cnt<=p_xs_3->morder_max; morder_cnt+=1) {
06061 blaze_wav=2*(sin(-p_xs_3->nug))/(morder_cnt*p_xs_3->sg);
06062 lam_max=blaze_wav*((double)(morder_cnt)/((double)(morder_cnt)-0.5));
06063 lam_min=blaze_wav*((double)(morder_cnt)/(0.5+(double)(morder_cnt)));
06064 lam_inc=(lam_max-lam_min)/(99.99);
06065
06066 for (lambda=lam_min; lambda<=lam_max; lambda+=lam_inc) {
06067 lambda_nm=lambda*mm2nm;
06068 xsh_3_eval(lambda,morder_cnt,ref_ind,p_xs_3);
06069 xsh_3_detpix(p_xs_3);
06070 if (lambda==lam_min) low_y=p_xs_3->chippix[2];
06071 if (p_xs_3->chippix[0]==1) {
06072 if (p_xs_3->chippix[1]>=1 &&
06073 p_xs_3->chippix[1]<p_xs_3->ASIZE+1 &&
06074 p_xs_3->chippix[2]>=1 &&
06075 p_xs_3->chippix[2]<p_xs_3->BSIZE+1) {
06076 printf("%d %d %d %d %lf %lf %lf \n",edge, morder_cnt, ii, fpos, lambda_nm,p_xs_3->xpospix,p_xs_3->ypospix);
06077 centers[ii].order = morder_cnt;
06078 centers[ii].pos_x = p_xs_3->chippix[1] ;
06079 centers[ii].pos_y = p_xs_3->chippix[2] ;
06080 centers[ii].flux = 0.0 ;
06081 }
06082 }
06083 ii++;
06084 }
06085 up_y=p_xs_3->chippix[2];
06086 save_centers(centers, fpos, ii,xsh_instrument_arm_tostring(instr));
06087 printf("%d %d %d, %d %d \n", ii, fpos, morder_cnt, low_y, up_y);
06088 fpos=ii;
06089 res_list->list[iord].starty = low_y ;
06090 res_list->list[iord].endy = up_y ;
06091 res_list->list[iord].absorder = morder_cnt;
06092 iord++;
06093 }
06094 qsort(centers, ii, sizeof(CENTER_ORDER), comp_center2);
06095
06096 fit_order_edge_list(res_list, ii, centers, 5, edge);
06097 XSH_FREE(centers) ;
06098 }
06099
06100 if ((xsh_free2Darray(ref_ind,8))!=0) {
06101 cpl_msg_error(__func__, "Cannot free 2D array ref_ind");
06102 return NULL;
06103 }
06104
06105 check(plist=cpl_propertylist_new());
06106
06107 sprintf(filename,"%s.fits",tag);
06108 check(xsh_pfits_set_pcatg(plist, tag ) ) ;
06109 cpl_msg_info(__func__, "Save the tables") ;
06110
06111 check(order_edges_frame=xsh_order_list_save(res_list,instr,tab_filename,tag,p_xs_3->BSIZE)) ;
06112
06113 cleanup:
06114 xsh_free_propertylist(&plist);
06115 XSH_FREE( centers ) ;
06116 return order_edges_frame;
06117 }
06118
06119