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