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