00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00038
00041
00042
00043
00044
00045 #include <math.h>
00046 #include <xsh_drl.h>
00047 #include <xsh_pfits.h>
00048 #include <xsh_utils.h>
00049 #include <xsh_model_utils.h>
00050 #include <xsh_data_order.h>
00051 #include <xsh_error.h>
00052 #include <xsh_utils.h>
00053 #include <xsh_msg.h>
00054 #include <xsh_data_pre.h>
00055 #include <xsh_data_order.h>
00056 #include <xsh_data_the_map.h>
00057 #include <xsh_data_arclist.h>
00058 #include <xsh_data_wavesol.h>
00059 #include <xsh_data_resid_tab.h>
00060 #include <xsh_data_wavemap.h>
00061 #include <xsh_data_spectralformat.h>
00062 #include <xsh_model_io.h>
00063 #include <xsh_model_kernel.h>
00064 #include <xsh_fit.h>
00065 #include <gsl/gsl_multifit.h>
00066 #include <cpl.h>
00067
00068
00069
00070
00071 static void theo_tab_filter( xsh_the_map *the_tab, xsh_arclist *arclist,
00072 int* size, double **lambda, double **n, double **s, int **s_index,
00073 double **xthe, double **ythe, int nb_pinhole);
00074
00075 static void theo_tab_model( xsh_xs_3* config_model, xsh_arclist *arclist,
00076 xsh_spectralformat_list *spectralformat_list, int* size, double **lambda,
00077 double **n, double **s, double **sn,
00078 int **s_index, double **xthe, double **ythe,
00079 xsh_instrument* instr, int nb_pinhole);
00080
00081 static void data_wavesol_fit_with_sigma( xsh_wavesol *wavesol,
00082 double *A, double *lambda, double *n, double *s, int size,
00083 int max_iter, double min_frac, double sigma, int* rejected);
00084
00085 static int lines_filter_by_sn( xsh_pre* pre, double sn_ref, double x,
00086 double y, double* sn);
00087
00088
00089
00090
00091 static int lines_filter_by_sn( xsh_pre* pre, double sn_ref, double x,
00092 double y, double* sn)
00093 {
00094 int res = 0;
00095 int xpix, ypix, rej;
00096 double flux, noise;
00097
00098 XSH_ASSURE_NOT_NULL( pre);
00099
00100
00101 xpix =(int) rint(x);
00102 ypix = (int)rint(y);
00103
00104 check( flux = cpl_image_get( pre->data, xpix, ypix, &rej));
00105 check( noise = cpl_image_get( pre->errs, xpix, ypix, &rej));
00106 *sn = flux / noise;
00107 res = (*sn > sn_ref);
00108 cleanup:
00109 return res;
00110 }
00111
00112 static void theo_tab_model( xsh_xs_3* config_model, xsh_arclist *arclist,
00113 xsh_spectralformat_list *spectralformat_list, int* size, double **lambda,
00114 double **n, double **s, double** sn,
00115 int **s_index, double **xthe, double **ythe,
00116 xsh_instrument* instr, int nb_pinhole)
00117 {
00118 int i,j;
00119 cpl_vector** spectral_tab = NULL;
00120 int global_size = 0;
00121 int arc_size;
00122 int index_loc = 0;
00123 int nb_slit = 1;
00124
00125 XSH_ASSURE_NOT_NULL( config_model);
00126 XSH_ASSURE_NOT_NULL( arclist);
00127 XSH_ASSURE_NOT_NULL( spectralformat_list);
00128
00129 check( arc_size = xsh_arclist_get_size( arclist));
00130 XSH_CALLOC( spectral_tab, cpl_vector*, arc_size);
00131
00132 nb_slit = nb_pinhole;
00133
00134 XSH_REGDEBUG("nb pinhole %d ", nb_slit);
00135
00136 for( i=0; i< arc_size; i++){
00137 cpl_vector* res = NULL;
00138 float lambdaARC;
00139 int res_size;
00140
00141 check( lambdaARC = xsh_arclist_get_wavelength(arclist, i));
00142 check( res = xsh_spectralformat_list_get_orders(spectralformat_list,
00143 lambdaARC));
00144 if (res != NULL){
00145 check( res_size = cpl_vector_get_size( res));
00146 res_size *= nb_slit;
00147 }
00148 else{
00149 res_size = 0;
00150 check(xsh_arclist_reject(arclist,i));
00151 }
00152 global_size += res_size;
00153 spectral_tab[i] = res;
00154 }
00155
00156 XSH_MALLOC( *lambda, double, global_size);
00157 XSH_MALLOC( *n, double, global_size);
00158 XSH_MALLOC( *s, double, global_size);
00159 XSH_MALLOC( *sn, double, global_size);
00160 XSH_MALLOC( *s_index, int, global_size);
00161 XSH_MALLOC( *xthe, double, global_size);
00162 XSH_MALLOC( *ythe, double, global_size);
00163
00164
00165 for( i=0; i< arc_size; i++){
00166 double model_x, model_y;
00167 cpl_vector *spectral_res = NULL;
00168 int spectral_res_size;
00169 float lambdaARC;
00170
00171 check( lambdaARC = xsh_arclist_get_wavelength(arclist, i));
00172 check( spectral_res = spectral_tab[i]);
00173
00174 if (spectral_res != NULL){
00175 check( spectral_res_size = cpl_vector_get_size( spectral_res));
00176 for( j=0; j< spectral_res_size; j++){
00177 int absorder = 0;
00178 int islit;
00179 double slit;
00180
00181 if (nb_slit > 1){
00182
00183 for(islit=0; islit < nb_slit; islit++){
00184
00185 slit = config_model->slit[islit];
00186 check( absorder = (int) cpl_vector_get( spectral_res, j));
00187
00188
00189
00190 check( xsh_model_get_xy( config_model, instr, lambdaARC, absorder, slit,
00191 &model_x, &model_y));
00192 (*lambda)[index_loc] = lambdaARC;
00193 (*n)[index_loc] = absorder;
00194 (*s_index)[index_loc] = islit;
00195 (*s)[index_loc] = slit;
00196 (*sn)[index_loc] = 0;
00197 (*xthe)[index_loc] = model_x;
00198 (*ythe)[index_loc] = model_y;
00199 index_loc++;
00200 }
00201 }
00202 else{
00203 islit = 4;
00204 slit = config_model->slit[islit];
00205 check( absorder = (int) cpl_vector_get( spectral_res, j));
00206 check( xsh_model_get_xy( config_model, instr, lambdaARC, absorder, slit,
00207 &model_x, &model_y));
00208 (*lambda)[index_loc] = lambdaARC;
00209 (*n)[index_loc] = absorder;
00210 (*s_index)[index_loc] = islit;
00211 (*s)[index_loc] = slit;
00212 (*sn)[index_loc] = 0;
00213 (*xthe)[index_loc] = model_x;
00214 (*ythe)[index_loc] = model_y;
00215 index_loc++;
00216 }
00217
00218
00219 }
00220 }
00221 }
00222 *size = global_size;
00223
00224 cleanup:
00225 if ( spectral_tab != NULL){
00226 for(i=0; i< arc_size; i++){
00227 xsh_free_vector( &spectral_tab[i]);
00228 }
00229 XSH_FREE( spectral_tab);
00230 }
00231 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00232 XSH_FREE( *lambda);
00233 XSH_FREE( *n);
00234 XSH_FREE( *s);
00235 XSH_FREE( *s_index);
00236 XSH_FREE( *xthe);
00237 XSH_FREE( *ythe);
00238 }
00239 return;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 static void theo_tab_filter( xsh_the_map *the_tab, xsh_arclist *arclist,
00263 int* size, double **lambda, double **n, double **s, int **s_index,
00264 double **xthe, double **ythe, int nb_pinhole)
00265 {
00266 int the_size = 0;
00267 int arc_size = 0;
00268 int i=0, j=0;
00269 int sol_size = 0;
00270 int nb_slit = 1;
00271
00272 XSH_ASSURE_NOT_NULL( the_tab);
00273 XSH_ASSURE_NOT_NULL( arclist);
00274
00275 check( arc_size = xsh_arclist_get_size( arclist));
00276 check( the_size = xsh_the_map_get_size( the_tab));
00277
00278 XSH_MALLOC( *lambda, double, the_size);
00279 XSH_MALLOC( *n, double, the_size);
00280 XSH_MALLOC( *s, double, the_size);
00281 XSH_MALLOC( *s_index, int, the_size);
00282 XSH_MALLOC( *xthe, double, the_size);
00283 XSH_MALLOC( *ythe, double, the_size);
00284
00285 nb_slit = nb_pinhole;
00286 XSH_REGDEBUG("nb pinhole %d ", nb_slit);
00287
00288 for(i=0; i< arc_size; i++){
00289 float lambdaARC, lambdaTHE;
00290 int nb_match = 0, max_match = 0;
00291
00292 check(lambdaARC = xsh_arclist_get_wavelength(arclist, i));
00293 check(lambdaTHE = xsh_the_map_get_wavelength(the_tab, j));
00294
00295 xsh_msg_dbg_medium("LINETABLE : Line %d / %d Lambda %f",i+1, arc_size,
00296 lambdaARC);
00297
00298 while ( (j < the_size-1) &&
00299 ( (lambdaARC-lambdaTHE) > WAVELENGTH_PRECISION) ){
00300 j++;
00301 check(lambdaTHE = xsh_the_map_get_wavelength(the_tab, j));
00302 }
00303
00304 xsh_msg_dbg_medium("THETABLE : Line %d / %d Lambda %f",j+1, the_size, lambdaTHE);
00305
00306 max_match = the_size-j;
00307
00308 while ( nb_match < max_match && fabs(lambdaARC-lambdaTHE) <= WAVELENGTH_PRECISION ){
00309 double order, slit, xtheval, ytheval;
00310 int islit;
00311
00312 check( slit = (double) xsh_the_map_get_slit_position( the_tab, j));
00313 check( islit = xsh_the_map_get_slit_index( the_tab, j));
00314
00315 if ( (nb_slit > 1) || (islit == 4) ){
00316
00317 check( order = (double)xsh_the_map_get_order(the_tab, j));
00318 check( xtheval = xsh_the_map_get_detx(the_tab, j));
00319 check( ytheval = xsh_the_map_get_dety(the_tab, j));
00320 (*lambda)[sol_size] = lambdaTHE;
00321 (*n)[sol_size] = order;
00322 (*s)[sol_size] = slit;
00323 (*s_index)[sol_size] = islit;
00324 (*xthe)[sol_size] = xtheval;
00325 (*ythe)[sol_size] = ytheval;
00326
00327 XSH_REGDEBUG("sol_size %d order %f slit %f lambda %f",
00328 sol_size, (*n)[sol_size], (*s)[sol_size], (*lambda)[sol_size]);
00329
00330 sol_size++;
00331 nb_match++;
00332 }
00333 if ( j < (the_size-1) ){
00334 j++;
00335 check( lambdaTHE = (double) xsh_the_map_get_wavelength( the_tab, j));
00336 }
00337
00338 }
00339
00340 if (nb_match == 0) {
00341 check(xsh_arclist_reject(arclist, i));
00342 }
00343 }
00344 *size = sol_size;
00345
00346 cleanup:
00347 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00348 XSH_FREE( lambda);
00349 XSH_FREE( n);
00350 XSH_FREE( s);
00351 XSH_FREE( s_index);
00352 XSH_FREE( xthe);
00353 XSH_FREE( ythe);
00354 }
00355 return;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 static void data_wavesol_fit_with_sigma( xsh_wavesol *wavesol,
00383 double *A, double *lambda, double *n, double *s, int size,
00384 int max_iter, double min_frac, double sigma, int* rejected)
00385 {
00386 int nbiter = 0;
00387 float frac = 1;
00388 int nbrejected = 0;
00389 int new_rejected = 1;
00390 cpl_polynomial *fitpoly = NULL;
00391 cpl_vector *dispy = NULL;
00392 int * idx = NULL;
00393 int index_size = 0;
00394 int i;
00395
00396 XSH_ASSURE_NOT_NULL( wavesol);
00397 XSH_ASSURE_NOT_NULL( A);
00398 XSH_ASSURE_NOT_NULL( lambda);
00399 XSH_ASSURE_NOT_NULL( n);
00400 XSH_ASSURE_NOT_NULL( s);
00401 XSH_ASSURE_NOT_ILLEGAL( size > 0);
00402
00403 XSH_CALLOC( idx, int, size);
00404 check(fitpoly = xsh_wavesol_get_poly( wavesol));
00405 check(xsh_wavesol_compute( wavesol, size, A,
00406 &(wavesol->min_y), &(wavesol->max_y), lambda, n, s, fitpoly));
00407
00408 index_size = size;
00409 for(i=0; i< index_size; i++){
00410 idx[i] = i;
00411 }
00412
00413 xsh_msg( "Fit wavesol with sigma clipping");
00414
00415 while (nbiter < max_iter && frac > min_frac && new_rejected > 0){
00416 double sigma_med;
00417
00418 new_rejected = 0;
00419 xsh_msg_dbg_high( " *** NBITER = %d / %d ***", nbiter+1, max_iter);
00420 dispy = cpl_vector_new( index_size);
00421
00422 for(i = 0; i < index_size; i ++){
00423 double soly;
00424 double diffy;
00425 check( soly = xsh_wavesol_eval_poly(wavesol, lambda[i], n[i], s[i]));
00426 diffy = A[i]-soly;
00427 check( cpl_vector_set( dispy, i, diffy));
00428
00429 }
00430 check( sigma_med = cpl_vector_get_stdev( dispy));
00431 xsh_msg_dbg_high(" sigma %f SIGMA MEDIAN = %f", sigma, sigma_med);
00432
00433 for(i = 0; i < index_size; i++){
00434 if ( fabs(cpl_vector_get( dispy, i)) > (sigma * sigma_med) ){
00435 new_rejected++;
00436 rejected[idx[i]] = 1;
00437 }
00438 else{
00439 idx[i-new_rejected] = idx[i];
00440 A[i-new_rejected] = A[i];
00441 lambda[i-new_rejected] = lambda[i];
00442 n[i-new_rejected] = n[i];
00443 s[i-new_rejected] = s[i];
00444 }
00445 }
00446 xsh_free_vector(&dispy);
00447 index_size -= new_rejected;
00448 nbrejected += new_rejected;
00449
00450 frac = 1 - ( (float) nbrejected / (float) size);
00451 xsh_msg_dbg_high(" NB_REJECT = %d", nbrejected);
00452 xsh_msg_dbg_high(" GOOD PIXEL FRACTION = %f", frac);
00453
00454 check( xsh_wavesol_compute(wavesol, index_size, A,
00455 &(wavesol->min_y), &(wavesol->max_y), lambda, n, s, fitpoly));
00456 nbiter++;
00457 }
00458
00459 cleanup:
00460 XSH_FREE( idx);
00461 xsh_free_vector(&dispy);
00462 return;
00463 }
00464
00465
00466
00467
00468
00506 void
00507 xsh_detect_arclines_dan( cpl_frame *frame,
00508 cpl_frame *theo_tab_frame,
00509 cpl_frame *arc_lines_tab_frame,
00510 cpl_frame* wave_tab_guess_frame,
00511 cpl_frame *order_tab_recov_frame,
00512 cpl_frame *config_model_frame,
00513 cpl_frame *spectralformat_frame,
00514 cpl_frame **resid_tab_orders_frame,
00515 cpl_frame **arc_lines_clean_tab_frame,
00516 cpl_frame **wave_tab_frame,
00517 cpl_frame **resid_tab_frame,
00518 xsh_sol_wavelength solwave_type,
00519 xsh_detect_arclines_param *da,
00520 xsh_clipping_param *dac,
00521 xsh_instrument *instr,
00522 const char* rec_id,
00523 const int clean_tmp,
00524 const int resid_tab_name_sw)
00525 {
00526
00527
00528
00529 xsh_the_map *themap = NULL;
00530 xsh_resid_tab *resid = NULL;
00531 xsh_resid_tab *resid_orders = NULL;
00532 xsh_arclist *arclist = NULL;
00533 xsh_pre *pre = NULL;
00534 cpl_polynomial *fit2dx = NULL;
00535 cpl_propertylist* header = NULL;
00536 int * sort = NULL;
00537 double *vlambdadata = NULL, *vsdata = NULL, *vorderdata = NULL, *vsndata=NULL;
00538 int *vsindexdata = NULL;
00539 double *vxthedata = NULL, *vythedata = NULL;
00540 double *corr_x = NULL, *corr_y = NULL;
00541 double *gaussian_pos_x = NULL, *gaussian_pos_y = NULL,
00542 *gaussian_sigma_x = NULL, *gaussian_sigma_y = NULL,
00543 *gaussian_fwhm_x = NULL, *gaussian_fwhm_y = NULL, *gaussian_norm=NULL;
00544 int* flag=NULL;
00545 cpl_vector *gaussian_sigma_x_vect =NULL;
00546 cpl_vector *gaussian_sigma_y_vect =NULL;
00547 cpl_vector *gaussian_fwhm_x_vect =NULL;
00548 cpl_vector *gaussian_fwhm_y_vect =NULL;
00549
00550
00551 double *diffx = NULL, *diffy = NULL;
00552 double *diffxmean = NULL, *diffymean = NULL, *diffxsig = NULL, *diffysig = NULL;
00553 xsh_order_list *order_tab_recov = NULL;
00554
00555 int nlinematched, nlinecat_clean=0, nlinecat;
00556 int i, sol_size = 0;
00557 xsh_wavesol* wave_table = NULL;
00558 char wave_table_name[80];
00559 const char* wave_table_tag = NULL;
00560 xsh_wavesol* wave_tab_guess = NULL;
00561 xsh_xs_3 config_model;
00562 xsh_xs_3* p_xs_3;
00563 xsh_spectralformat_list *spectralformat_list = NULL;
00564 int lines_not_in_image = 0;
00565 int lines_not_good_sn = 0;
00566 int lines_not_gauss_fit = 0;
00567 int lines_not_valid_pixels = 0;
00568 int lines_too_few_ph=0;
00569
00570 int* rejected = NULL;
00571 int nb_rejected = 0;
00572 int solution_type = 0;
00573 const char* solution_type_name[2] = { "POLY", "MODEL"};
00574 int detection_mode = 0;
00575 const char* detection_mode_name[3] = { "NORMAL", "CORRECTED", "RECOVER"};
00576 int nb_pinhole;
00577 char fname[256];
00578 char rname[256];
00579 char rtag[80];
00580 const char* tag=NULL;
00581
00582 char dpr_type[80];
00583 char type[80];
00584 cpl_table* wave_trace=NULL;
00585
00586
00587 cpl_table* cfg_tab=NULL;
00588 const char* cfg_name=NULL;
00589 char new_name[256];
00590 char basename[256];
00591
00592
00593 cpl_propertylist* plist=NULL;
00594
00595
00596
00597 int starti=0;
00598 int newi=0;
00599 int j,k;
00600 int min_slit_match=7;
00601 int found_temp=true;
00602
00603
00604 XSH_ASSURE_NOT_NULL( frame);
00605 XSH_ASSURE_NOT_NULL( arc_lines_tab_frame);
00606 XSH_ASSURE_NOT_NULL( spectralformat_frame);
00607 XSH_ASSURE_NOT_NULL( instr);
00608 XSH_ASSURE_NOT_NULL( da);
00609 XSH_ASSURE_NOT_NULL( dac);
00610
00611
00612 XSH_ASSURE_NOT_NULL( arc_lines_clean_tab_frame);
00613 XSH_ASSURE_NOT_NULL( resid_tab_frame);
00614
00615
00616
00617 xsh_msg_dbg_medium("---Detect arclines parameters");
00618 xsh_msg_dbg_medium("min_sn %f fit-window-half-size %d", da->min_sn, da->fit_window_hsize);
00619 xsh_msg_dbg_medium("Clipping sigma %f niter %d frac %f", dac->sigma, dac->niter, dac->frac);
00620
00621 check( pre = xsh_pre_load (frame, instr));
00622 check_msg( nb_pinhole = xsh_pfits_get_nb_pinhole( pre->data_header),
00623 "fail to find number of pinholes info in frame %s",
00624 cpl_frame_get_filename(frame) );
00625 check(strcpy(dpr_type,xsh_pfits_get_dpr_type(pre->data_header)));
00626
00627 if(strstr(dpr_type,"FMTCHK") != NULL) {
00628 strcpy(type,"FMTCHK_");
00629 } else if(strstr(dpr_type,"WAVE") != NULL) {
00630 strcpy(type,"WAVE_");
00631 } else if(strstr(dpr_type,"WAVE") != NULL) {
00632 strcpy(type,"ARC_");
00633 } else {
00634 strcpy(type,"");
00635 }
00636
00637 check( arclist = xsh_arclist_load( arc_lines_tab_frame));
00638 check( spectralformat_list = xsh_spectralformat_list_load(
00639 spectralformat_frame, instr));
00640 check(nlinecat = xsh_arclist_get_size( arclist));
00641
00642 check(xsh_arclist_lambda_sort( arclist));
00643
00644
00645
00646 if ( theo_tab_frame != NULL) {
00647
00648 XSH_ASSURE_NOT_ILLEGAL( config_model_frame == NULL);
00649 solution_type = XSH_DETECT_ARCLINES_TYPE_POLY;
00650 check( themap = xsh_the_map_load( theo_tab_frame));
00651 check( xsh_the_map_lambda_order_slit_sort( themap));
00652 check( theo_tab_filter( themap, arclist, &sol_size, &vlambdadata,
00653 &vorderdata, &vsdata, &vsindexdata, &vxthedata, &vythedata, nb_pinhole));
00654
00655 }
00656 else if ( config_model_frame != NULL){
00657
00658 solution_type = XSH_DETECT_ARCLINES_TYPE_MODEL;
00659 p_xs_3=&config_model;
00660
00661
00662
00663 check(cfg_name=cpl_frame_get_filename(config_model_frame));
00664 check(plist=cpl_propertylist_load(cfg_name,0));
00665 check(cfg_tab=cpl_table_load(cfg_name,1,0));
00666
00667 tag=XSH_GET_TAG_FROM_ARM(XSH_MOD_CFG_TAB,instr);
00668 sprintf(basename,"%s.fits",tag);
00669 sprintf(new_name,"local_%s",basename);
00670 check( xsh_pfits_set_pcatg(plist,tag));
00671 check(cpl_table_save(cfg_tab,plist,NULL,new_name,CPL_IO_DEFAULT));
00672 xsh_add_temporary_file(new_name);
00673 check(cpl_frame_set_filename(config_model_frame,new_name));
00674 xsh_free_table(&cfg_tab);
00675 xsh_free_propertylist(&plist);
00676
00677 check( xsh_model_config_load_best( config_model_frame, &config_model));
00678 XSH_REGDEBUG("load config model ok");
00679
00680 check(xsh_model_temperature_update_frame(&config_model_frame,frame,
00681 instr,&found_temp));
00682 check(xsh_model_temperature_update_structure(&config_model,frame,instr));
00683 check( theo_tab_model( &config_model, arclist, spectralformat_list,
00684 &sol_size, &vlambdadata, &vorderdata, &vsdata,
00685 &vsndata, &vsindexdata, &vxthedata, &vythedata,
00686 instr, nb_pinhole));
00687 }
00688 else{
00689 XSH_ASSURE_NOT_ILLEGAL_MSG(1==0,
00690 "Undefined solution type (POLY or MODEL). See your input sof");
00691 }
00692
00693 xsh_spectralformat_list_free(&spectralformat_list);
00694 xsh_msg_dbg_high("Solution type %s", solution_type_name[solution_type]);
00695
00696
00697 if ( wave_tab_guess_frame == NULL){
00698 if ( order_tab_recov_frame == NULL){
00699 detection_mode = XSH_DETECT_ARCLINES_MODE_NORMAL;
00700 }
00701 else{
00702 detection_mode = XSH_DETECT_ARCLINES_MODE_RECOVER;
00703 check( order_tab_recov = xsh_order_list_load( order_tab_recov_frame,
00704 instr));
00705 }
00706 }
00707 else {
00708 if ( order_tab_recov_frame == NULL){
00709 detection_mode = XSH_DETECT_ARCLINES_MODE_CORRECTED;
00710
00711 check( wave_tab_guess = xsh_wavesol_load( wave_tab_guess_frame,
00712 instr ));
00713 xsh_msg( "BinX,Y: %d, %d", wave_tab_guess->bin_x,
00714 wave_tab_guess->bin_y ) ;
00715 }
00716 else{
00717 detection_mode = -1;
00718 }
00719 }
00720
00721 XSH_ASSURE_NOT_ILLEGAL( detection_mode >=XSH_DETECT_ARCLINES_MODE_NORMAL
00722 && detection_mode <= XSH_DETECT_ARCLINES_MODE_RECOVER);
00723
00724 xsh_msg( "Detection mode : %s", detection_mode_name[detection_mode]);
00725
00726 xsh_msg( "Solution size %d", sol_size);
00727
00728
00729 XSH_MALLOC( corr_x, double, sol_size);
00730 XSH_MALLOC( corr_y, double, sol_size);
00731
00732 XSH_MALLOC( gaussian_pos_x, double, sol_size);
00733 XSH_MALLOC( gaussian_pos_y, double, sol_size);
00734 XSH_MALLOC( gaussian_sigma_x, double, sol_size);
00735 XSH_MALLOC( gaussian_sigma_y, double, sol_size);
00736
00737 XSH_MALLOC( gaussian_fwhm_x, double, sol_size);
00738 XSH_MALLOC( gaussian_fwhm_y, double, sol_size);
00739 XSH_MALLOC( gaussian_norm, double, sol_size);
00740
00741 XSH_MALLOC( diffx, double, sol_size);
00742 XSH_MALLOC( diffy, double, sol_size);
00743
00744 XSH_MALLOC( diffxmean, double, sol_size);
00745 XSH_MALLOC( diffymean, double, sol_size);
00746
00747 XSH_MALLOC( diffxsig, double, sol_size);
00748 XSH_MALLOC( diffysig, double, sol_size);
00749 XSH_MALLOC( flag, int, sol_size);
00750
00751 nlinematched = 0;
00752
00753 if ( da->find_center_method == XSH_GAUSSIAN_METHOD){
00754 xsh_msg_dbg_medium( "USE method GAUSSIAN");
00755 }
00756 else{
00757 xsh_msg_dbg_medium( "USE method BARYCENTER");
00758 }
00759
00760 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_HIGH) {
00761
00762 FILE* regfile = NULL;
00763
00764 regfile = fopen( "FIT.reg", "w");
00765 fprintf( regfile, "# Region file format: DS9 version 4.0\n"\
00766 "global color=red font=\"helvetica 4 normal\""\
00767 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 "\
00768 "source\nimage\n");
00769 fclose( regfile);
00770 regfile = fopen( "NOFIT.reg", "w");
00771 fprintf( regfile, "# Region file format: DS9 version 4.0\n"\
00772 "global color=red font=\"helvetica 4 normal\""\
00773 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 "\
00774 "source\nimage\n");
00775 fclose( regfile);
00776 }
00777
00778
00779
00780
00781 for(i=0; i< sol_size; i++){
00782 int xpos = 0, ypos = 0;
00783 int slit_index;
00784 double corrxv = 0.0, corryv = 0.0;
00785 double lambda, order, slit, xthe, ythe,sn=0;
00786 double xcor, ycor, x, y, sig_x, sig_y, norm, fwhm_x, fwhm_y;
00787
00788
00789 lambda = vlambdadata[i];
00790 order = vorderdata[i];
00791 slit = vsdata[i];
00792
00793 if(vsndata != NULL) {
00794 sn = vsndata[i];
00795 }
00796
00797 slit_index = vsindexdata[i];
00798 xthe = vxthedata[i];
00799 ythe = vythedata[i];
00800 xsh_msg_dbg_high( "THE LAMBDA %f ORDER %f SLIT %f X %f Y %f", lambda,
00801 order, slit, xthe, ythe);
00802
00803
00804 xcor = xthe;
00805 ycor = ythe;
00806 xsh_msg_dbg_high("THE x%f y %f", xthe, ythe);
00807
00808
00809 if ( order_tab_recov != NULL){
00810 int iorder = 0;
00811 cpl_polynomial *center_poly_recov = NULL;
00812
00813 check (iorder = xsh_order_list_get_index_by_absorder(
00814 order_tab_recov, order));
00815 center_poly_recov = order_tab_recov->list[iorder].cenpoly;
00816 check( xcor = cpl_polynomial_eval_1d( center_poly_recov, ycor, NULL));
00817 corrxv = xcor-xthe;
00818 }
00819
00820 if ( wave_tab_guess != NULL){
00821 double diffxv, diffyv;
00822
00823 check( diffxv = xsh_wavesol_eval_polx( wave_tab_guess, lambda,
00824 order, 0));
00825 check( diffyv = xsh_wavesol_eval_poly( wave_tab_guess, lambda,
00826 order, 0));
00827
00828 xcor = xthe+diffxv;
00829 ycor = ythe+diffyv;
00830 corrxv = diffxv;
00831 corryv = diffyv;
00832 }
00833
00834 xsh_msg_dbg_high("x %f y %f CORR_x %f CORR_y %f", xcor, ycor,
00835 corrxv, corryv);
00836
00837 if ( xcor >=0.5 && ycor >=0.5 &&
00838 xcor < (pre->nx+0.5) && ycor < (pre->ny+0.5)){
00839 int best_med_res = 0;
00840 check ( best_med_res = xsh_pre_window_best_median_flux_pos( pre,
00841 (int)(xcor+0.5)-1, (int)(ycor+0.5)-1,
00842 da->search_window_hsize, da->running_median_hsize, &xpos, &ypos));
00843
00844 if (best_med_res != 0){
00845 lines_not_valid_pixels++;
00846 flag[i]=1;
00847 xsh_msg_dbg_medium("Not valid pixels for this line %d",i);
00848 continue;
00849 }
00850
00851 xpos = xpos+1;
00852 ypos = ypos+1;
00853 xsh_msg_dbg_high("ADJ x %d y %d", xpos, ypos);
00854
00855 if ( da->find_center_method == XSH_GAUSSIAN_METHOD){
00856 cpl_image_fit_gaussian(pre->data, xpos, ypos, 1+2*da->fit_window_hsize,
00857 &norm, &x, &y, &sig_x, &sig_y, &fwhm_x, &fwhm_y);
00858 xsh_msg_dbg_high("GAUS x %f y %f %d %d %d", x, y,da->fit_window_hsize, da->search_window_hsize, da->running_median_hsize);
00859 }
00860 else{
00861 xsh_image_find_barycenter( pre->data, xpos, ypos,
00862 1+2*da->fit_window_hsize,
00863 &norm, &x, &y, &sig_x, &sig_y, &fwhm_x, &fwhm_y);
00864 xsh_msg_dbg_high("BARY x %f y %f", x, y);
00865 }
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880 if (nb_pinhole==1 && solution_type==XSH_DETECT_ARCLINES_TYPE_MODEL) {
00881 if (p_xs_3->arm==2) {
00882 x=x+0.125;
00883 }
00884 else if (p_xs_3->arm==0) {
00885 x=x-0.51;
00886 }
00887 }
00888
00889 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_HIGH) {
00890 const char* fit = NULL;
00891 const char* color = NULL;
00892 FILE* regfile = NULL;
00893
00894 if (cpl_error_get_code() == CPL_ERROR_NONE){
00895 fit = "FIT.reg";
00896 color = "green";
00897 }
00898 else{
00899 fit = "NOFIT.reg";
00900 color = "red";
00901 }
00902 regfile = fopen( fit, "a");
00903 fprintf( regfile, "point(%d,%d) #point=cross color=blue\n", (int)(xcor), (int)(ycor));
00904 fprintf( regfile, "box(%d,%d,%d,%d) #color=blue\n", (int)(xcor), (int)(ycor),
00905 da->search_window_hsize*2,da->search_window_hsize*2);
00906 fprintf( regfile, "point(%d,%d) #point=cross color=%s font=\"helvetica 10 normal\""\
00907 " text={%.3f}\n", xpos, ypos, color, lambda);
00908 fprintf( regfile, "box(%d,%d,%d,%d) #color=%s\n", (int)xpos, (int)ypos,
00909 1+2*da->fit_window_hsize, 1+2*da->fit_window_hsize, color);
00910 fclose( regfile);
00911 }
00912
00913 if( cpl_error_get_code() == CPL_ERROR_NONE ){
00914 if ( lines_filter_by_sn( pre, da->min_sn, x, y, &sn) ){
00915 vlambdadata[i] = lambda;
00916 vorderdata[i] = order;
00917 vsdata[i] = slit;
00918 if(vsndata!=NULL) {
00919 vsndata[i] = sn;
00920 }
00921
00922 vsindexdata[i] = slit_index;
00923 vxthedata[i] = xthe;
00924 vythedata[i] = ythe;
00925 corr_x[i] = corrxv;
00926 corr_y[i] = corryv;
00927 gaussian_pos_x[i] = x;
00928 gaussian_pos_y[i] = y;
00929 gaussian_sigma_x[i] = sig_x;
00930 gaussian_sigma_y[i] = sig_y;
00931 gaussian_fwhm_x[i] = fwhm_x;
00932 gaussian_fwhm_y[i] = fwhm_y;
00933 gaussian_norm[i] = norm;
00934 diffx[i] = x-xthe;
00935 diffy[i] = y-ythe;
00936 flag[i] = 0;
00937
00938
00939
00940
00941
00942
00943 nlinematched++;
00944
00945 }
00946 else{
00947 lines_not_good_sn++;
00948 flag[i]=2;
00949 xsh_msg_dbg_medium("Not good s/n for this line");
00950 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_HIGH) {
00951
00952 int xpix, ypix, rej;
00953 float flux, noise, sn;
00954 FILE* regfile = NULL;
00955 char sn_name[256];
00956
00957 xpix =(int) rint(x);
00958 ypix = (int)rint(y);
00959
00960 check( flux = cpl_image_get( pre->data, xpix, ypix, &rej));
00961 check( noise = cpl_image_get( pre->errs, xpix, ypix, &rej));
00962 sn = flux / noise;
00963 sprintf( sn_name, "bad_sn_%.3f.reg", da->min_sn);
00964 regfile = fopen( sn_name, "a");
00965 fprintf( regfile, "point(%d,%d) #point=cross color=red font=\"helvetica 10 normal\""\
00966 " text={%.3f [%.3f}\n", xpix, ypix, lambda, sn);
00967 fclose( regfile);
00968 }
00969 }
00970 }
00971 else{
00972 flag[i]=3;
00973 lines_not_gauss_fit++;
00974 xsh_msg_dbg_medium("No Fit Gaussian for this line %d",i);
00975 xsh_error_reset();
00976 }
00977 }
00978 else{
00979 flag[i]=4;
00980 lines_not_in_image++;
00981 xsh_msg_dbg_medium("Coordinates are not in the image");
00982 }
00983 }
00984
00985
00986
00987
00988
00989 if (nb_pinhole>1) {
00990 for (i=1; i<sol_size; i++) {
00991 if(flag[i]==0) {
00992 xsh_msg_dbg_high("filter 7 pinhole : line %d : lambda %f order %f slit %f",
00993 i, vlambdadata[i], vorderdata[i], vsdata[i]);
00994
00995 if (vlambdadata[i]!=vlambdadata[i-1] ||
00996 vorderdata[i]!=vorderdata[i-1] ||
00997 i==nlinematched-1) {
00998
00999
01000 if (i==nlinematched-1) {
01001 i++;
01002 }
01003 xsh_msg_dbg_high("filter 7 pinhole : from %d to %d find %d pinholes",
01004 starti, i, i-starti);
01005
01006 if (i-starti>=min_slit_match) {
01007 for (k=starti; k<=i-1; k++) {
01008 diffxmean[k]=0.0;
01009 diffymean[k]=0.0;
01010 for (j=starti; j<=i-1; j++) {
01011 if (j!=k) {
01012 diffxmean[k]+=diffx[j];
01013 diffymean[k]+=diffy[j];
01014 }
01015 }
01016 diffxmean[k]/=(float)(i-starti-1);
01017 diffymean[k]/=(float)(i-starti-1);
01018 }
01019 for (k=starti; k<=i-1; k++) {
01020 diffxsig[k]=0.0;
01021 diffysig[k]=0.0;
01022 for (j=starti; j<=i-1; j++) {
01023 if (j!=k) {
01024 diffysig[k]+=(diffy[j]-diffymean[k])*(diffy[j]-diffymean[k]);
01025 diffxsig[k]+=(diffx[j]-diffxmean[k])*(diffx[j]-diffxmean[k]);
01026 }
01027 }
01028 diffxsig[k]=sqrt(diffxsig[k]/(float)(i-starti-1));
01029 diffysig[k]=sqrt(diffysig[k]/(float)(i-starti-1));
01030 }
01031 for (j=starti; j<=i-1; j++) {
01032
01033
01034 vlambdadata[newi] = vlambdadata[j];
01035 vorderdata[newi] = vorderdata[j];
01036 vsdata[newi] =vsdata[j];
01037 vsindexdata[newi] = vsindexdata[j];
01038 vxthedata[newi] =vxthedata[j];
01039 vythedata[newi] =vythedata[j];
01040 corr_x[newi] = corr_x[j];
01041 corr_y[newi] = corr_y[j];
01042 gaussian_pos_x[newi] = gaussian_pos_x[j];
01043 gaussian_pos_y[newi] = gaussian_pos_y[j];
01044 gaussian_sigma_x[newi] = gaussian_sigma_x[j];
01045 gaussian_sigma_y[newi] = gaussian_sigma_y[j];
01046 gaussian_fwhm_x[newi] = gaussian_fwhm_x[j];
01047 gaussian_fwhm_y[newi] = gaussian_fwhm_y[j];
01048 gaussian_norm[newi] = gaussian_norm[j];
01049 diffx[newi] = diffx[j];
01050 diffy[newi] = diffy[j];
01051 newi++;
01052
01053
01054
01055
01056
01057 }
01058 }
01059 else {
01060 lines_too_few_ph+=i-starti;
01061 flag[i]=5;
01062 xsh_msg_dbg_medium("Too few pin-holes for this line %d",i);
01063 }
01064 starti=i;
01065 }
01066 }
01067 }
01068 nlinematched=newi;
01069 }
01070
01071
01072
01073 xsh_msg("nlinematched / sol_size = %d / %d", nlinematched, sol_size);
01074 assure(nlinematched > 0, CPL_ERROR_ILLEGAL_INPUT,
01075 "No line matched, value of parameter "
01076 "detectarclines-search-window-half-size may be too large or too small "
01077 "detectarclines-fit-window-half-size=%d may be too large or too small "
01078 "detectarclines-running-median-half-size may be too large or too small "
01079 "or value of parameter detectarclines-min-sn may be too large",
01080 da->fit_window_hsize);
01081
01082 xsh_msg_dbg_medium(" %d lines not found in image", lines_not_in_image);
01083 xsh_msg_dbg_medium(" %d lines not good S/N", lines_not_good_sn);
01084 xsh_msg_dbg_medium(" %d lines no fit gaussian", lines_not_gauss_fit);
01085 xsh_msg_dbg_medium(" %d lines no valid pixels", lines_not_valid_pixels);
01086 xsh_msg_dbg_medium(" %d lines detected in less than %d/9 ph positions", lines_too_few_ph,min_slit_match);
01087 check( gaussian_sigma_x_vect = cpl_vector_wrap( nlinematched, gaussian_sigma_x));
01088 check( gaussian_sigma_y_vect = cpl_vector_wrap( nlinematched, gaussian_sigma_y));
01089 check( gaussian_fwhm_x_vect = cpl_vector_wrap( nlinematched, gaussian_fwhm_x));
01090 check( gaussian_fwhm_y_vect = cpl_vector_wrap( nlinematched, gaussian_fwhm_y));
01091 xsh_msg("sigma gaussian median in x %lg",
01092 cpl_vector_get_median_const( gaussian_sigma_x_vect));
01093 xsh_msg("sigma gaussian median in y %lg",
01094 cpl_vector_get_median_const( gaussian_sigma_y_vect));
01095
01096 xsh_msg("FWHM gaussian median in x %lg",
01097 cpl_vector_get_median_const( gaussian_fwhm_x_vect));
01098 xsh_msg("FWHM gaussian median in y %lg",
01099 cpl_vector_get_median_const( gaussian_fwhm_y_vect));
01100
01101
01102 xsh_unwrap_vector( &gaussian_sigma_x_vect);
01103 xsh_unwrap_vector( &gaussian_sigma_y_vect);
01104
01105 xsh_unwrap_vector( &gaussian_fwhm_x_vect);
01106 xsh_unwrap_vector( &gaussian_fwhm_y_vect);
01107
01108
01109
01110
01111 if ( resid_tab_orders_frame != NULL){
01112 check(resid_orders=xsh_resid_tab_create(sol_size,vlambdadata,vorderdata,
01113 vsdata, vsndata,vsindexdata,
01114 vxthedata, vythedata,
01115 corr_x, corr_y,
01116 gaussian_norm,
01117 gaussian_pos_x, gaussian_pos_y,
01118 gaussian_sigma_x, gaussian_sigma_y,
01119 gaussian_fwhm_x, gaussian_fwhm_y,flag,
01120 wave_table, solution_type));
01121
01122 if(resid_tab_name_sw) {
01123 sprintf(rtag,"%s%s%s",type,"RESID_TAB_ORDERS_",
01124 xsh_instrument_arm_tostring( instr ));
01125 } else {
01126 sprintf(rtag,"%s%s%s",type,"RESID_ALL_TAB_ORDERS_",
01127 xsh_instrument_arm_tostring( instr ));
01128 }
01129
01130 sprintf(rname,"%s%s",rtag,".fits");
01131
01132 check( *resid_tab_orders_frame = xsh_resid_tab_save( resid_orders,
01133 rname, instr,rtag));
01134 xsh_add_temporary_file(rname);
01135
01136 }
01137 xsh_msg_dbg_high("solution_type=%d poly=%d model=%d",solution_type,
01138 XSH_DETECT_ARCLINES_TYPE_POLY, XSH_DETECT_ARCLINES_TYPE_MODEL);
01139
01140
01141
01142
01143 if ( solution_type == XSH_DETECT_ARCLINES_TYPE_POLY &&
01144 (wave_tab_frame != NULL)){
01145 check( wave_table = xsh_wavesol_create( spectralformat_frame, da, instr));
01146 XSH_CALLOC( rejected, int, nlinematched);
01147 if ( solwave_type == XSH_SOLUTION_RELATIVE) {
01148
01149 check( xsh_wavesol_set_type( wave_table, XSH_WAVESOL_GUESS));
01150 check( data_wavesol_fit_with_sigma( wave_table, diffy, vlambdadata,
01151 vorderdata, vsdata, nlinematched, dac->niter, dac->frac,
01152 dac->sigma, rejected));
01153 nb_rejected = 0;
01154 for(i=0; i< nlinematched; i++){
01155 if (rejected[i] == 1){
01156 nb_rejected++;
01157 }
01158 else{
01159
01160 vxthedata[i-nb_rejected] = vxthedata[i];
01161 vythedata[i-nb_rejected] = vythedata[i];
01162 vsindexdata[i-nb_rejected] = vsindexdata[i];
01163 corr_x[i-nb_rejected] = corr_x[i];
01164 corr_y[i-nb_rejected] = corr_y[i];
01165 gaussian_pos_x[i-nb_rejected] = gaussian_pos_x[i];
01166 gaussian_pos_y[i-nb_rejected] = gaussian_pos_y[i];
01167 gaussian_sigma_x[i-nb_rejected] = gaussian_sigma_x[i];
01168 gaussian_sigma_y[i-nb_rejected] = gaussian_sigma_y[i];
01169 gaussian_fwhm_x[i-nb_rejected] = gaussian_fwhm_x[i];
01170 gaussian_fwhm_y[i-nb_rejected] = gaussian_fwhm_y[i];
01171 gaussian_norm[i-nb_rejected] = gaussian_norm[i];
01172 diffx[i-nb_rejected] = diffx[i];
01173 }
01174 }
01175 nlinematched = nlinematched-nb_rejected;
01176
01177 check( fit2dx = xsh_wavesol_get_polx( wave_table));
01178 check( xsh_wavesol_compute( wave_table,
01179 nlinematched, diffx,
01180 &(wave_table->min_x), &(wave_table->max_x),
01181 vlambdadata, vorderdata, vsdata, fit2dx));
01182
01183
01184 sprintf(wave_table_name,"%s%s.fits","WAVE_TAB_GUESS_",
01185 xsh_instrument_arm_tostring( instr )) ;
01186
01187 wave_table_tag = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_GUESS, instr);
01188
01189 }
01190 else{
01191 check( xsh_wavesol_set_type( wave_table, XSH_WAVESOL_2D));
01192
01193 check( data_wavesol_fit_with_sigma( wave_table, gaussian_pos_y,
01194 vlambdadata, vorderdata, vsdata, nlinematched, dac->niter, dac->frac,
01195 dac->sigma, rejected));
01196 nb_rejected = 0;
01197 for(i=0; i< nlinematched; i++){
01198 if (rejected[i] == 1){
01199 nb_rejected++;
01200 }
01201 else{
01202
01203 vxthedata[i-nb_rejected] = vxthedata[i];
01204 vythedata[i-nb_rejected] = vythedata[i];
01205 vsindexdata[i-nb_rejected] = vsindexdata[i];
01206 corr_x[i-nb_rejected] = corr_x[i];
01207 corr_y[i-nb_rejected] = corr_y[i];
01208 gaussian_pos_x[i-nb_rejected] = gaussian_pos_x[i];
01209 gaussian_sigma_x[i-nb_rejected] = gaussian_sigma_x[i];
01210 gaussian_sigma_y[i-nb_rejected] = gaussian_sigma_y[i];
01211 gaussian_fwhm_x[i-nb_rejected] = gaussian_fwhm_x[i];
01212 gaussian_fwhm_y[i-nb_rejected] = gaussian_fwhm_y[i];
01213 }
01214 }
01215 nlinematched = nlinematched-nb_rejected;
01216
01217 check(fit2dx = xsh_wavesol_get_polx(wave_table));
01218 check(xsh_wavesol_compute(wave_table, nlinematched, gaussian_pos_x,
01219 &wave_table->min_x, &wave_table->max_x, vlambdadata, vorderdata,
01220 vsdata, fit2dx));
01221
01222 sprintf(wave_table_name,"%s%s.fits","WAVE_TAB_2D_",
01223 xsh_instrument_arm_tostring( instr )) ;
01224
01225
01226 wave_table_tag = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_2D, instr);
01227 }
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248 check(header = xsh_wavesol_get_header(wave_table));
01249 check(xsh_pfits_set_qc_nlinecat(header,nlinecat));
01250 check(xsh_pfits_set_qc_nlinefound(header,sol_size));
01251
01252
01253
01254 check( wave_trace=xsh_wavesol_trace(wave_table,vlambdadata,vorderdata,
01255 vsdata,nlinematched));
01256
01257 check( *wave_tab_frame=xsh_wavesol_save(wave_table,wave_trace,
01258 wave_table_name,wave_table_tag));
01259
01260 check( cpl_frame_set_tag( *wave_tab_frame, wave_table_tag));
01261 }
01262
01263
01264 check( xsh_arclist_clean_from_list( arclist, vlambdadata, nlinematched));
01265 nlinecat_clean = arclist->size-arclist->nbrejected;
01266 check( header = xsh_arclist_get_header( arclist));
01267 check( xsh_pfits_set_qc_nlinecat( header,nlinecat));
01268 check( xsh_pfits_set_qc_nlinefound( header, sol_size));
01269 check( xsh_pfits_set_qc_nlinecat_clean( header,nlinecat_clean));
01270 check( xsh_pfits_set_qc_nlinefound_clean( header,nlinematched));
01271 if(strcmp(rec_id,"xsh_predict") == 0) {
01272 tag=XSH_GET_TAG_FROM_ARM( XSH_ARC_LINE_LIST_PREDICT, instr);
01273 } else {
01274 tag=XSH_GET_TAG_FROM_ARM( XSH_ARC_LINE_LIST_2DMAP, instr);
01275 }
01276 sprintf(fname,"%s%s",tag,".fits");
01277 check( *arc_lines_clean_tab_frame = xsh_arclist_save( arclist,fname,tag));
01278 if(clean_tmp) {
01279 xsh_add_temporary_file(fname);
01280 }
01281
01282 check( resid = xsh_resid_tab_create( sol_size, vlambdadata, vorderdata,
01283 vsdata,vsndata,vsindexdata,vxthedata,vythedata,
01284 corr_x,corr_y,gaussian_norm,
01285 gaussian_pos_x,gaussian_pos_y,
01286 gaussian_sigma_x, gaussian_sigma_y,
01287 gaussian_fwhm_x, gaussian_fwhm_y,flag,
01288 wave_table,solution_type));
01289
01290 if(resid_tab_name_sw) {
01291 sprintf(rtag,"%s%s%s",type,"RESID_TAB_LINES_",
01292 xsh_instrument_arm_tostring( instr ));
01293 } else {
01294 sprintf(rtag,"%s%s%s",type,"RESID_TAB_ALL_LINES_",
01295 xsh_instrument_arm_tostring( instr ));
01296 }
01297 check( tag = cpl_frame_get_tag( frame));
01298
01299 XSH_REGDEBUG("TAG %s", tag);
01300
01301 if ( strstr( tag, XSH_AFC_CAL) ){
01302 sprintf(rname,"AFC_CAL_%s%s",rtag,".fits") ;
01303 }
01304 else if ( strstr( tag, XSH_AFC_ATT) ){
01305 sprintf(rname,"AFC_ATT_%s%s",rtag,".fits") ;
01306 }
01307 else {
01308 sprintf(rname,"%s%s",rtag,".fits") ;
01309 }
01310 check( *resid_tab_frame = xsh_resid_tab_save( resid,rname,instr,rtag));
01311 if(clean_tmp) {
01312 xsh_add_temporary_file(rname);
01313 }
01314
01315 cleanup:
01316 XSH_FREE( vlambdadata);
01317 XSH_FREE( vorderdata);
01318 XSH_FREE( vsdata);
01319 XSH_FREE( vsndata);
01320 XSH_FREE( vsindexdata);
01321 XSH_FREE( vxthedata);
01322 XSH_FREE( vythedata);
01323 XSH_FREE( corr_x);
01324 XSH_FREE( corr_y);
01325 XSH_FREE( gaussian_pos_x);
01326 XSH_FREE( gaussian_pos_y);
01327 XSH_FREE( gaussian_sigma_x);
01328 XSH_FREE( gaussian_sigma_y);
01329 XSH_FREE( gaussian_fwhm_x);
01330 XSH_FREE( gaussian_fwhm_y);
01331 XSH_FREE( gaussian_norm);
01332 XSH_FREE( sort);
01333 XSH_FREE( diffx);
01334 XSH_FREE( diffy);
01335 XSH_FREE( diffxmean);
01336 XSH_FREE( diffymean);
01337 XSH_FREE( diffxsig);
01338 XSH_FREE( diffysig);
01339 XSH_FREE( rejected);
01340 XSH_FREE( flag);
01341
01342 xsh_free_table( &wave_trace);
01343 xsh_spectralformat_list_free(&spectralformat_list);
01344 xsh_pre_free( &pre);
01345 xsh_the_map_free( &themap);
01346 xsh_wavesol_free( &wave_table);
01347 xsh_wavesol_free( &wave_tab_guess);
01348 xsh_order_list_free( &order_tab_recov);
01349 xsh_resid_tab_free( &resid_orders);
01350 xsh_resid_tab_free( &resid);
01351 xsh_arclist_free( &arclist);
01352 return;
01353 }
01354
01392 void
01393 xsh_detect_arclines( cpl_frame *frame,
01394 cpl_frame *theo_tab_frame,
01395 cpl_frame *arc_lines_tab_frame,
01396 cpl_frame* wave_tab_guess_frame,
01397 cpl_frame *order_tab_recov_frame,
01398 cpl_frame *config_model_frame,
01399 cpl_frame *spectralformat_frame,
01400 cpl_frame **resid_tab_orders_frame,
01401 cpl_frame **arc_lines_clean_tab_frame,
01402 cpl_frame **wave_tab_frame,
01403 cpl_frame **resid_tab_frame,
01404 xsh_sol_wavelength solwave_type,
01405 xsh_detect_arclines_param *da,
01406 xsh_clipping_param *dac,
01407 xsh_instrument *instr,
01408 const char* rec_id,
01409 const int clean_tmp,const int resid_tab_name_sw)
01410 {
01411
01412
01413
01414 xsh_the_map *themap = NULL;
01415 xsh_resid_tab *resid = NULL;
01416 xsh_resid_tab *resid_orders = NULL;
01417 xsh_arclist *arclist = NULL;
01418 xsh_pre *pre = NULL;
01419 cpl_polynomial *fit2dx = NULL;
01420 cpl_propertylist* header = NULL;
01421 int * sort = NULL;
01422 double *vlambdadata = NULL, *vsdata = NULL, *vorderdata = NULL, *vsndata=NULL;
01423 int *vsindexdata = NULL;
01424 double *vxthedata = NULL, *vythedata = NULL;
01425 double *corr_x = NULL, *corr_y = NULL;
01426 double *gaussian_pos_x = NULL, *gaussian_pos_y = NULL,
01427 *gaussian_sigma_x = NULL, *gaussian_sigma_y = NULL,
01428 *gaussian_fwhm_x = NULL, *gaussian_fwhm_y = NULL, *gaussian_norm=NULL;
01429 cpl_vector *gaussian_sigma_x_vect =NULL;
01430 cpl_vector *gaussian_sigma_y_vect =NULL;
01431 cpl_vector *gaussian_fwhm_x_vect =NULL;
01432 cpl_vector *gaussian_fwhm_y_vect =NULL;
01433
01434
01435 double *diffx = NULL, *diffy = NULL;
01436 double *diffxmean = NULL, *diffymean = NULL, *diffxsig = NULL, *diffysig = NULL;
01437 xsh_order_list *order_tab_recov = NULL;
01438
01439 int nlinematched, nlinecat_clean=0, nlinecat;
01440 int i, sol_size = 0;
01441 xsh_wavesol* wave_table = NULL;
01442 char wave_table_name[80];
01443 const char* wave_table_tag = NULL;
01444 xsh_wavesol* wave_tab_guess = NULL;
01445 xsh_xs_3 config_model;
01446 xsh_xs_3* p_xs_3;
01447 xsh_spectralformat_list *spectralformat_list = NULL;
01448 int lines_not_in_image = 0;
01449 int lines_not_good_sn = 0;
01450 int lines_not_gauss_fit = 0;
01451 int lines_not_valid_pixels = 0;
01452 int lines_too_few_ph=0;
01453
01454 int* rejected = NULL;
01455 int nb_rejected = 0;
01456 int solution_type = 0;
01457 const char* solution_type_name[2] = { "POLY", "MODEL"};
01458 int detection_mode = 0;
01459 const char* detection_mode_name[3] = { "NORMAL", "CORRECTED", "RECOVER"};
01460 int nb_pinhole;
01461 char fname[256];
01462 char rname[256];
01463 char rtag[80];
01464 const char* tag=NULL;
01465
01466 char dpr_type[80];
01467 char type[80];
01468 cpl_table* wave_trace=NULL;
01469 int* flag=NULL;
01470
01471 cpl_table* cfg_tab=NULL;
01472 const char* cfg_name=NULL;
01473 char new_name[256];
01474 char basename[256];
01475
01476
01477 cpl_propertylist* plist=NULL;
01478
01479
01480
01481 int starti=0;
01482 int newi=0;
01483 int j,k;
01484 int min_slit_match=7;
01485 int found_temp=true;
01486
01487
01488 XSH_ASSURE_NOT_NULL( frame);
01489 XSH_ASSURE_NOT_NULL( arc_lines_tab_frame);
01490 XSH_ASSURE_NOT_NULL( spectralformat_frame);
01491 XSH_ASSURE_NOT_NULL( instr);
01492 XSH_ASSURE_NOT_NULL( da);
01493 XSH_ASSURE_NOT_NULL( dac);
01494
01495
01496 XSH_ASSURE_NOT_NULL( arc_lines_clean_tab_frame);
01497 XSH_ASSURE_NOT_NULL( resid_tab_frame);
01498
01499
01500
01501 xsh_msg_dbg_medium("---Detect arclines parameters");
01502 xsh_msg_dbg_medium("min_sn %f fit-window-half-size %d", da->min_sn, da->fit_window_hsize);
01503 xsh_msg_dbg_medium("Clipping sigma %f niter %d frac %f", dac->sigma, dac->niter, dac->frac);
01504
01505 check( pre = xsh_pre_load (frame, instr));
01506 check_msg( nb_pinhole = xsh_pfits_get_nb_pinhole( pre->data_header),
01507 "fail to find number of pinholes info in frame %s",
01508 cpl_frame_get_filename(frame) );
01509 check(strcpy(dpr_type,xsh_pfits_get_dpr_type(pre->data_header)));
01510
01511 if(strstr(dpr_type,"FMTCHK") != NULL) {
01512 strcpy(type,"FMTCHK_");
01513 } else if(strstr(dpr_type,"WAVE") != NULL) {
01514 strcpy(type,"WAVE_");
01515 } else if(strstr(dpr_type,"WAVE") != NULL) {
01516 strcpy(type,"ARC_");
01517 } else {
01518 strcpy(type,"");
01519 }
01520
01521 check( arclist = xsh_arclist_load( arc_lines_tab_frame));
01522 check( spectralformat_list = xsh_spectralformat_list_load(
01523 spectralformat_frame, instr));
01524 check(nlinecat = xsh_arclist_get_size( arclist));
01525
01526 check(xsh_arclist_lambda_sort( arclist));
01527
01528
01529
01530 if ( theo_tab_frame != NULL) {
01531 XSH_ASSURE_NOT_ILLEGAL( config_model_frame == NULL);
01532 solution_type = XSH_DETECT_ARCLINES_TYPE_POLY;
01533 check( themap = xsh_the_map_load( theo_tab_frame));
01534 check( xsh_the_map_lambda_order_slit_sort( themap));
01535 check( theo_tab_filter( themap, arclist, &sol_size, &vlambdadata,
01536 &vorderdata, &vsdata, &vsindexdata, &vxthedata, &vythedata, nb_pinhole));
01537
01538 }
01539 else if ( config_model_frame != NULL){
01540 solution_type = XSH_DETECT_ARCLINES_TYPE_MODEL;
01541 p_xs_3=&config_model;
01542
01543
01544
01545 check(cfg_name=cpl_frame_get_filename(config_model_frame));
01546 check(plist=cpl_propertylist_load(cfg_name,0));
01547 check(cfg_tab=cpl_table_load(cfg_name,1,0));
01548
01549 tag=XSH_GET_TAG_FROM_ARM(XSH_MOD_CFG_TAB,instr);
01550 sprintf(basename,"%s.fits",tag);
01551 sprintf(new_name,"local_%s",basename);
01552 check( xsh_pfits_set_pcatg(plist,tag));
01553 check(cpl_table_save(cfg_tab,plist,NULL,new_name,CPL_IO_DEFAULT));
01554 xsh_add_temporary_file(new_name);
01555 check(cpl_frame_set_filename(config_model_frame,new_name));
01556 xsh_free_table(&cfg_tab);
01557 xsh_free_propertylist(&plist);
01558
01559 check( xsh_model_config_load_best( config_model_frame, &config_model));
01560 XSH_REGDEBUG("load config model ok");
01561
01562 check(xsh_model_temperature_update_frame(&config_model_frame,frame,
01563 instr,&found_temp));
01564 check(xsh_model_temperature_update_structure(&config_model,frame,instr));
01565 check( theo_tab_model( &config_model, arclist, spectralformat_list,
01566 &sol_size, &vlambdadata, &vorderdata, &vsdata,
01567 &vsndata, &vsindexdata, &vxthedata, &vythedata,
01568 instr, nb_pinhole));
01569 }
01570 else{
01571 XSH_ASSURE_NOT_ILLEGAL_MSG(1==0,
01572 "Undefined solution type (POLY or MODEL). See your input sof");
01573 }
01574
01575 xsh_spectralformat_list_free(&spectralformat_list);
01576 xsh_msg_dbg_high("Solution type %s", solution_type_name[solution_type]);
01577
01578
01579 if ( wave_tab_guess_frame == NULL){
01580 if ( order_tab_recov_frame == NULL){
01581 detection_mode = XSH_DETECT_ARCLINES_MODE_NORMAL;
01582 }
01583 else{
01584 detection_mode = XSH_DETECT_ARCLINES_MODE_RECOVER;
01585 check( order_tab_recov = xsh_order_list_load( order_tab_recov_frame,
01586 instr));
01587 }
01588 }
01589 else {
01590 if ( order_tab_recov_frame == NULL){
01591 detection_mode = XSH_DETECT_ARCLINES_MODE_CORRECTED;
01592
01593 check( wave_tab_guess = xsh_wavesol_load( wave_tab_guess_frame,
01594 instr ));
01595 xsh_msg( "BinX,Y: %d, %d", wave_tab_guess->bin_x,
01596 wave_tab_guess->bin_y ) ;
01597 }
01598 else{
01599 detection_mode = -1;
01600 }
01601 }
01602
01603 XSH_ASSURE_NOT_ILLEGAL( detection_mode >=XSH_DETECT_ARCLINES_MODE_NORMAL
01604 && detection_mode <= XSH_DETECT_ARCLINES_MODE_RECOVER);
01605
01606 xsh_msg( "Detection mode : %s", detection_mode_name[detection_mode]);
01607
01608 xsh_msg( "Solution size %d", sol_size);
01609
01610
01611 XSH_MALLOC( corr_x, double, sol_size);
01612 XSH_MALLOC( corr_y, double, sol_size);
01613
01614 XSH_MALLOC( gaussian_pos_x, double, sol_size);
01615 XSH_MALLOC( gaussian_pos_y, double, sol_size);
01616 XSH_MALLOC( gaussian_sigma_x, double, sol_size);
01617 XSH_MALLOC( gaussian_sigma_y, double, sol_size);
01618
01619 XSH_MALLOC( gaussian_fwhm_x, double, sol_size);
01620 XSH_MALLOC( gaussian_fwhm_y, double, sol_size);
01621 XSH_MALLOC( gaussian_norm, double, sol_size);
01622
01623 XSH_MALLOC( diffx, double, sol_size);
01624 XSH_MALLOC( diffy, double, sol_size);
01625
01626 XSH_MALLOC( diffxmean, double, sol_size);
01627 XSH_MALLOC( diffymean, double, sol_size);
01628
01629 XSH_MALLOC( diffxsig, double, sol_size);
01630 XSH_MALLOC( diffysig, double, sol_size);
01631
01632
01633 nlinematched = 0;
01634
01635 if ( da->find_center_method == XSH_GAUSSIAN_METHOD){
01636 xsh_msg_dbg_medium( "USE method GAUSSIAN");
01637 }
01638 else{
01639 xsh_msg_dbg_medium( "USE method BARYCENTER");
01640 }
01641
01642 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_HIGH) {
01643 FILE* regfile = NULL;
01644
01645 regfile = fopen( "FIT.reg", "w");
01646 fprintf( regfile, "# Region file format: DS9 version 4.0\n"\
01647 "global color=red font=\"helvetica 4 normal\""\
01648 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 "\
01649 "source\nimage\n");
01650 fclose( regfile);
01651 regfile = fopen( "NOFIT.reg", "w");
01652 fprintf( regfile, "# Region file format: DS9 version 4.0\n"\
01653 "global color=red font=\"helvetica 4 normal\""\
01654 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 "\
01655 "source\nimage\n");
01656 fclose( regfile);
01657 }
01658
01659 for(i=0; i< sol_size; i++){
01660 int xpos = 0, ypos = 0;
01661 int slit_index;
01662 double corrxv = 0.0, corryv = 0.0;
01663 double lambda, order, slit, xthe, ythe,sn=0;
01664 double xcor, ycor, x, y, sig_x, sig_y, norm, fwhm_x, fwhm_y;
01665
01666
01667 lambda = vlambdadata[i];
01668 order = vorderdata[i];
01669 slit = vsdata[i];
01670
01671 if(vsndata != NULL) {
01672 sn = vsndata[i];
01673 }
01674
01675 slit_index = vsindexdata[i];
01676 xthe = vxthedata[i];
01677 ythe = vythedata[i];
01678 xsh_msg_dbg_high( "THE LAMBDA %f ORDER %f SLIT %f X %f Y %f", lambda,
01679 order, slit, xthe, ythe);
01680
01681
01682 xcor = xthe;
01683 ycor = ythe;
01684 xsh_msg_dbg_high("THE x%f y %f", xthe, ythe);
01685
01686
01687 if ( order_tab_recov != NULL){
01688 int iorder = 0;
01689 cpl_polynomial *center_poly_recov = NULL;
01690
01691 check (iorder = xsh_order_list_get_index_by_absorder(
01692 order_tab_recov, order));
01693 center_poly_recov = order_tab_recov->list[iorder].cenpoly;
01694 check( xcor = cpl_polynomial_eval_1d( center_poly_recov, ycor, NULL));
01695 corrxv = xcor-xthe;
01696 }
01697
01698 if ( wave_tab_guess != NULL){
01699 double diffxv, diffyv;
01700
01701 check( diffxv = xsh_wavesol_eval_polx( wave_tab_guess, lambda,
01702 order, 0));
01703 check( diffyv = xsh_wavesol_eval_poly( wave_tab_guess, lambda,
01704 order, 0));
01705
01706 xcor = xthe+diffxv;
01707 ycor = ythe+diffyv;
01708 corrxv = diffxv;
01709 corryv = diffyv;
01710 }
01711
01712 xsh_msg_dbg_high("x %f y %f CORR_x %f CORR_y %f", xcor, ycor,
01713 corrxv, corryv);
01714
01715 if ( xcor >=0.5 && ycor >=0.5 &&
01716 xcor < (pre->nx+0.5) && ycor < (pre->ny+0.5)){
01717 int best_med_res = 0;
01718 check ( best_med_res = xsh_pre_window_best_median_flux_pos( pre,
01719 (int)(xcor+0.5)-1, (int)(ycor+0.5)-1,
01720 da->search_window_hsize, da->running_median_hsize, &xpos, &ypos));
01721
01722 if (best_med_res != 0){
01723 lines_not_valid_pixels++;
01724 continue;
01725 }
01726
01727 xpos = xpos+1;
01728 ypos = ypos+1;
01729 xsh_msg_dbg_high("ADJ x %d y %d", xpos, ypos);
01730
01731 if ( da->find_center_method == XSH_GAUSSIAN_METHOD){
01732 cpl_image_fit_gaussian(pre->data, xpos, ypos, 1+2*da->fit_window_hsize,
01733 &norm, &x, &y, &sig_x, &sig_y, &fwhm_x, &fwhm_y);
01734 xsh_msg_dbg_high("GAUS x %f y %f %d %d %d", x, y,da->fit_window_hsize, da->search_window_hsize, da->running_median_hsize);
01735 }
01736 else{
01737 xsh_image_find_barycenter( pre->data, xpos, ypos,
01738 1+2*da->fit_window_hsize,
01739 &norm, &x, &y, &sig_x, &sig_y, &fwhm_x, &fwhm_y);
01740 xsh_msg_dbg_high("BARY x %f y %f", x, y);
01741 }
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756 if (nb_pinhole==1 && solution_type==XSH_DETECT_ARCLINES_TYPE_MODEL) {
01757 if (p_xs_3->arm==2) {
01758 x=x+0.125;
01759 }
01760 else if (p_xs_3->arm==0) {
01761 x=x-0.51;
01762 }
01763 }
01764
01765 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_HIGH) {
01766 const char* fit = NULL;
01767 const char* color = NULL;
01768 FILE* regfile = NULL;
01769
01770 if (cpl_error_get_code() == CPL_ERROR_NONE){
01771 fit = "FIT.reg";
01772 color = "green";
01773 }
01774 else{
01775 fit = "NOFIT.reg";
01776 color = "red";
01777 }
01778 regfile = fopen( fit, "a");
01779 fprintf( regfile, "point(%d,%d) #point=cross color=blue\n", (int)(xcor), (int)(ycor));
01780 fprintf( regfile, "box(%d,%d,%d,%d) #color=blue\n", (int)(xcor), (int)(ycor),
01781 da->search_window_hsize*2,da->search_window_hsize*2);
01782 fprintf( regfile, "point(%d,%d) #point=cross color=%s font=\"helvetica 10 normal\""\
01783 " text={%.3f}\n", xpos, ypos, color, lambda);
01784 fprintf( regfile, "box(%d,%d,%d,%d) #color=%s\n", (int)xpos, (int)ypos,
01785 1+2*da->fit_window_hsize, 1+2*da->fit_window_hsize, color);
01786 fclose( regfile);
01787 }
01788
01789 if( cpl_error_get_code() == CPL_ERROR_NONE ){
01790 if ( lines_filter_by_sn( pre, da->min_sn, x, y, &sn) ){
01791 vlambdadata[nlinematched] = lambda;
01792 vorderdata[nlinematched] = order;
01793 vsdata[nlinematched] = slit;
01794 if(vsndata!=NULL) {
01795 vsndata[nlinematched] = sn;
01796 }
01797
01798 vsindexdata[nlinematched] = slit_index;
01799 vxthedata[nlinematched] = xthe;
01800 vythedata[nlinematched] = ythe;
01801 corr_x[nlinematched] = corrxv;
01802 corr_y[nlinematched] = corryv;
01803 gaussian_pos_x[nlinematched] = x;
01804 gaussian_pos_y[nlinematched] = y;
01805 gaussian_sigma_x[nlinematched] = sig_x;
01806 gaussian_sigma_y[nlinematched] = sig_y;
01807 gaussian_fwhm_x[nlinematched] = fwhm_x;
01808 gaussian_fwhm_y[nlinematched] = fwhm_y;
01809 gaussian_norm[nlinematched] = norm;
01810 diffx[nlinematched] = x-xthe;
01811 diffy[nlinematched] = y-ythe;
01812
01813
01814
01815
01816
01817
01818 nlinematched++;
01819
01820 }
01821 else{
01822 lines_not_good_sn++;
01823 xsh_msg_dbg_medium("Not good s/n for this line");
01824 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_HIGH) {
01825
01826 int xpix, ypix, rej;
01827 float flux, noise, sn;
01828 FILE* regfile = NULL;
01829 char sn_name[256];
01830
01831 xpix =(int) rint(x);
01832 ypix = (int)rint(y);
01833
01834 check( flux = cpl_image_get( pre->data, xpix, ypix, &rej));
01835 check( noise = cpl_image_get( pre->errs, xpix, ypix, &rej));
01836 sn = flux / noise;
01837 sprintf( sn_name, "bad_sn_%.3f.reg", da->min_sn);
01838 regfile = fopen( sn_name, "a");
01839 fprintf( regfile, "point(%d,%d) #point=cross color=red font=\"helvetica 10 normal\""\
01840 " text={%.3f [%.3f}\n", xpix, ypix, lambda, sn);
01841 fclose( regfile);
01842 }
01843 }
01844 }
01845 else{
01846 lines_not_gauss_fit++;
01847 xsh_msg_dbg_medium("No Fit Gaussian for this line");
01848 xsh_error_reset();
01849 }
01850 }
01851 else{
01852 lines_not_in_image++;
01853 xsh_msg_dbg_medium("Coordinates are not in the image");
01854 }
01855 }
01856
01857
01858
01859
01860
01861 if (nb_pinhole>1) {
01862 for (i=1; i<nlinematched; i++) {
01863 xsh_msg_dbg_high("filter 7 pinhole : line %d : lambda %f order %f slit %f",
01864 i, vlambdadata[i], vorderdata[i], vsdata[i]);
01865
01866 if (vlambdadata[i]!=vlambdadata[i-1] || vorderdata[i]!=vorderdata[i-1] || i==nlinematched-1) {
01867
01868
01869 if (i==nlinematched-1) {
01870 i++;
01871 }
01872 xsh_msg_dbg_high("filter 7 pinhole : from %d to %d find %d pinholes",
01873 starti, i, i-starti);
01874
01875 if (i-starti>=min_slit_match) {
01876 for (k=starti; k<=i-1; k++) {
01877 diffxmean[k]=0.0;
01878 diffymean[k]=0.0;
01879 for (j=starti; j<=i-1; j++) {
01880 if (j!=k) {
01881 diffxmean[k]+=diffx[j];
01882 diffymean[k]+=diffy[j];
01883 }
01884 }
01885 diffxmean[k]/=(float)(i-starti-1);
01886 diffymean[k]/=(float)(i-starti-1);
01887 }
01888 for (k=starti; k<=i-1; k++) {
01889 diffxsig[k]=0.0;
01890 diffysig[k]=0.0;
01891 for (j=starti; j<=i-1; j++) {
01892 if (j!=k) {
01893 diffysig[k]+=(diffy[j]-diffymean[k])*(diffy[j]-diffymean[k]);
01894 diffxsig[k]+=(diffx[j]-diffxmean[k])*(diffx[j]-diffxmean[k]);
01895 }
01896 }
01897 diffxsig[k]=sqrt(diffxsig[k]/(float)(i-starti-1));
01898 diffysig[k]=sqrt(diffysig[k]/(float)(i-starti-1));
01899 }
01900 for (j=starti; j<=i-1; j++) {
01901
01902
01903 vlambdadata[newi] = vlambdadata[j];
01904 vorderdata[newi] = vorderdata[j];
01905 vsdata[newi] =vsdata[j];
01906 vsindexdata[newi] = vsindexdata[j];
01907 vxthedata[newi] =vxthedata[j];
01908 vythedata[newi] =vythedata[j];
01909 corr_x[newi] = corr_x[j];
01910 corr_y[newi] = corr_y[j];
01911 gaussian_pos_x[newi] = gaussian_pos_x[j];
01912 gaussian_pos_y[newi] = gaussian_pos_y[j];
01913 gaussian_sigma_x[newi] = gaussian_sigma_x[j];
01914 gaussian_sigma_y[newi] = gaussian_sigma_y[j];
01915 gaussian_fwhm_x[newi] = gaussian_fwhm_x[j];
01916 gaussian_fwhm_y[newi] = gaussian_fwhm_y[j];
01917 gaussian_norm[newi] = gaussian_norm[j];
01918 diffx[newi] = diffx[j];
01919 diffy[newi] = diffy[j];
01920 newi++;
01921
01922
01923
01924
01925
01926 }
01927 }
01928 else {
01929 lines_too_few_ph+=i-starti;
01930 }
01931 starti=i;
01932 }
01933 }
01934 nlinematched=newi;
01935 }
01936
01937 xsh_msg("nlinematched / sol_size = %d / %d", nlinematched, sol_size);
01938 assure(nlinematched > 0, CPL_ERROR_ILLEGAL_INPUT,
01939 "No line matched, value of parameter "
01940 "detectarclines-search-window-half-size may be too large or too small "
01941 "detectarclines-fit-window-half-size=%d may be too large or too small "
01942 "detectarclines-running-median-half-size may be too large or too small "
01943 "or value of parameter detectarclines-min-sn may be too large",
01944 da->fit_window_hsize);
01945
01946 xsh_msg_dbg_medium(" %d lines not found in image", lines_not_in_image);
01947 xsh_msg_dbg_medium(" %d lines not good S/N", lines_not_good_sn);
01948 xsh_msg_dbg_medium(" %d lines no fit gaussian", lines_not_gauss_fit);
01949 xsh_msg_dbg_medium(" %d lines no valid pixels", lines_not_valid_pixels);
01950 xsh_msg_dbg_medium(" %d lines detected in less than %d/9 ph positions", lines_too_few_ph,min_slit_match);
01951 check( gaussian_sigma_x_vect = cpl_vector_wrap( nlinematched, gaussian_sigma_x));
01952 check( gaussian_sigma_y_vect = cpl_vector_wrap( nlinematched, gaussian_sigma_y));
01953 check( gaussian_fwhm_x_vect = cpl_vector_wrap( nlinematched, gaussian_fwhm_x));
01954 check( gaussian_fwhm_y_vect = cpl_vector_wrap( nlinematched, gaussian_fwhm_y));
01955 xsh_msg("sigma gaussian median in x %lg",
01956 cpl_vector_get_median_const( gaussian_sigma_x_vect));
01957 xsh_msg("sigma gaussian median in y %lg",
01958 cpl_vector_get_median_const( gaussian_sigma_y_vect));
01959
01960 xsh_msg("FWHM gaussian median in x %lg",
01961 cpl_vector_get_median_const( gaussian_fwhm_x_vect));
01962 xsh_msg("FWHM gaussian median in y %lg",
01963 cpl_vector_get_median_const( gaussian_fwhm_y_vect));
01964
01965
01966 xsh_unwrap_vector( &gaussian_sigma_x_vect);
01967 xsh_unwrap_vector( &gaussian_sigma_y_vect);
01968
01969 xsh_unwrap_vector( &gaussian_fwhm_x_vect);
01970 xsh_unwrap_vector( &gaussian_fwhm_y_vect);
01971
01972
01973
01974
01975 if ( resid_tab_orders_frame != NULL){
01976 check(resid_orders=xsh_resid_tab_create(nlinematched,vlambdadata,vorderdata,
01977 vsdata, vsndata,vsindexdata,
01978 vxthedata, vythedata,
01979 corr_x, corr_y,
01980 gaussian_norm,
01981 gaussian_pos_x, gaussian_pos_y,
01982 gaussian_sigma_x, gaussian_sigma_y,
01983 gaussian_fwhm_x, gaussian_fwhm_y,flag,
01984 wave_table, solution_type));
01985
01986
01987 sprintf(rtag,"%s%s%s",type,"RESID_TAB_ORDERS_",
01988 xsh_instrument_arm_tostring( instr ));
01989
01990 sprintf(rname,"%s%s",rtag,".fits");
01991
01992 check( *resid_tab_orders_frame = xsh_resid_tab_save( resid_orders,
01993 rname, instr,rtag));
01994 xsh_add_temporary_file(rname);
01995
01996 }
01997 xsh_msg_dbg_high("solution_type=%d poly=%d model=%d",solution_type,
01998 XSH_DETECT_ARCLINES_TYPE_POLY, XSH_DETECT_ARCLINES_TYPE_MODEL);
01999
02000
02001
02002 if ( solution_type == XSH_DETECT_ARCLINES_TYPE_POLY &&
02003 (wave_tab_frame != NULL)){
02004 check( wave_table = xsh_wavesol_create( spectralformat_frame, da, instr));
02005 XSH_CALLOC( rejected, int, nlinematched);
02006 if ( solwave_type == XSH_SOLUTION_RELATIVE) {
02007
02008 check( xsh_wavesol_set_type( wave_table, XSH_WAVESOL_GUESS));
02009 check( data_wavesol_fit_with_sigma( wave_table, diffy, vlambdadata,
02010 vorderdata, vsdata, nlinematched, dac->niter, dac->frac,
02011 dac->sigma, rejected));
02012 nb_rejected = 0;
02013 for(i=0; i< nlinematched; i++){
02014 if (rejected[i] == 1){
02015 nb_rejected++;
02016 }
02017 else{
02018
02019 vxthedata[i-nb_rejected] = vxthedata[i];
02020 vythedata[i-nb_rejected] = vythedata[i];
02021 vsindexdata[i-nb_rejected] = vsindexdata[i];
02022 corr_x[i-nb_rejected] = corr_x[i];
02023 corr_y[i-nb_rejected] = corr_y[i];
02024 gaussian_pos_x[i-nb_rejected] = gaussian_pos_x[i];
02025 gaussian_pos_y[i-nb_rejected] = gaussian_pos_y[i];
02026 gaussian_sigma_x[i-nb_rejected] = gaussian_sigma_x[i];
02027 gaussian_sigma_y[i-nb_rejected] = gaussian_sigma_y[i];
02028 gaussian_fwhm_x[i-nb_rejected] = gaussian_fwhm_x[i];
02029 gaussian_fwhm_y[i-nb_rejected] = gaussian_fwhm_y[i];
02030 gaussian_norm[i-nb_rejected] = gaussian_norm[i];
02031 diffx[i-nb_rejected] = diffx[i];
02032 }
02033 }
02034 nlinematched = nlinematched-nb_rejected;
02035
02036 check( fit2dx = xsh_wavesol_get_polx( wave_table));
02037 check( xsh_wavesol_compute( wave_table,
02038 nlinematched, diffx,
02039 &(wave_table->min_x), &(wave_table->max_x),
02040 vlambdadata, vorderdata, vsdata, fit2dx));
02041
02042
02043 sprintf(wave_table_name,"%s%s.fits","WAVE_TAB_GUESS_",
02044 xsh_instrument_arm_tostring( instr )) ;
02045
02046 wave_table_tag = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_GUESS, instr);
02047
02048 }
02049 else{
02050 check( xsh_wavesol_set_type( wave_table, XSH_WAVESOL_2D));
02051
02052 check( data_wavesol_fit_with_sigma( wave_table, gaussian_pos_y,
02053 vlambdadata, vorderdata, vsdata, nlinematched, dac->niter, dac->frac,
02054 dac->sigma, rejected));
02055 nb_rejected = 0;
02056 for(i=0; i< nlinematched; i++){
02057 if (rejected[i] == 1){
02058 nb_rejected++;
02059 }
02060 else{
02061
02062 vxthedata[i-nb_rejected] = vxthedata[i];
02063 vythedata[i-nb_rejected] = vythedata[i];
02064 vsindexdata[i-nb_rejected] = vsindexdata[i];
02065 corr_x[i-nb_rejected] = corr_x[i];
02066 corr_y[i-nb_rejected] = corr_y[i];
02067 gaussian_pos_x[i-nb_rejected] = gaussian_pos_x[i];
02068 gaussian_sigma_x[i-nb_rejected] = gaussian_sigma_x[i];
02069 gaussian_sigma_y[i-nb_rejected] = gaussian_sigma_y[i];
02070 gaussian_fwhm_x[i-nb_rejected] = gaussian_fwhm_x[i];
02071 gaussian_fwhm_y[i-nb_rejected] = gaussian_fwhm_y[i];
02072 }
02073 }
02074 nlinematched = nlinematched-nb_rejected;
02075
02076 check(fit2dx = xsh_wavesol_get_polx(wave_table));
02077 check(xsh_wavesol_compute(wave_table, nlinematched, gaussian_pos_x,
02078 &wave_table->min_x, &wave_table->max_x, vlambdadata, vorderdata,
02079 vsdata, fit2dx));
02080
02081 sprintf(wave_table_name,"%s%s.fits","WAVE_TAB_2D_",
02082 xsh_instrument_arm_tostring( instr )) ;
02083
02084
02085 wave_table_tag = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_2D, instr);
02086 }
02087
02088
02089 check(header = xsh_wavesol_get_header(wave_table));
02090 check(xsh_pfits_set_qc_nlinecat(header,nlinecat));
02091 check(xsh_pfits_set_qc_nlinefound(header,sol_size));
02092
02093
02094
02095 check( wave_trace=xsh_wavesol_trace(wave_table,vlambdadata,vorderdata,
02096 vsdata,nlinematched));
02097
02098 check( *wave_tab_frame=xsh_wavesol_save(wave_table,wave_trace,
02099 wave_table_name,wave_table_tag));
02100
02101 check( cpl_frame_set_tag( *wave_tab_frame, wave_table_tag));
02102 }
02103
02104
02105 check( xsh_arclist_clean_from_list( arclist, vlambdadata, nlinematched));
02106 nlinecat_clean = arclist->size-arclist->nbrejected;
02107 check( header = xsh_arclist_get_header( arclist));
02108 check( xsh_pfits_set_qc_nlinecat( header,nlinecat));
02109 check( xsh_pfits_set_qc_nlinefound( header, sol_size));
02110 check( xsh_pfits_set_qc_nlinecat_clean( header,nlinecat_clean));
02111 check( xsh_pfits_set_qc_nlinefound_clean( header,nlinematched));
02112 if(strcmp(rec_id,"xsh_predict") == 0) {
02113 tag=XSH_GET_TAG_FROM_ARM( XSH_ARC_LINE_LIST_PREDICT, instr);
02114 } else {
02115 tag=XSH_GET_TAG_FROM_ARM( XSH_ARC_LINE_LIST_2DMAP, instr);
02116 }
02117 sprintf(fname,"%s%s",tag,".fits");
02118 check( *arc_lines_clean_tab_frame = xsh_arclist_save( arclist,fname,tag));
02119 if(clean_tmp) {
02120 xsh_add_temporary_file(fname);
02121 }
02122
02123 check( resid = xsh_resid_tab_create( nlinematched, vlambdadata, vorderdata,
02124 vsdata,vsndata,vsindexdata,vxthedata,vythedata,
02125 corr_x,corr_y,gaussian_norm,
02126 gaussian_pos_x,gaussian_pos_y,
02127 gaussian_sigma_x, gaussian_sigma_y,
02128 gaussian_fwhm_x, gaussian_fwhm_y,flag,
02129 wave_table,solution_type));
02130
02131 if(resid_tab_name_sw) {
02132 sprintf(rtag,"%s%s%s",type,"RESID_TAB_DRL_LINES_",
02133 xsh_instrument_arm_tostring( instr ));
02134 } else {
02135 sprintf(rtag,"%s%s%s",type,"RESID_TAB_LINES_",
02136 xsh_instrument_arm_tostring( instr ));
02137 }
02138 check( tag = cpl_frame_get_tag( frame));
02139
02140 XSH_REGDEBUG("TAG %s", tag);
02141
02142 if ( strstr( tag, XSH_AFC_CAL) ){
02143 sprintf(rname,"AFC_CAL_%s%s",rtag,".fits") ;
02144 }
02145 else if ( strstr( tag, XSH_AFC_ATT) ){
02146 sprintf(rname,"AFC_ATT_%s%s",rtag,".fits") ;
02147 }
02148 else {
02149 sprintf(rname,"%s%s",rtag,".fits") ;
02150 }
02151 check( *resid_tab_frame = xsh_resid_tab_save( resid,rname,instr,rtag));
02152 if(clean_tmp || resid_tab_name_sw) {
02153 xsh_add_temporary_file(rname);
02154 }
02155
02156 cleanup:
02157 XSH_FREE( vlambdadata);
02158 XSH_FREE( vorderdata);
02159 XSH_FREE( vsdata);
02160 XSH_FREE( vsndata);
02161 XSH_FREE( vsindexdata);
02162 XSH_FREE( vxthedata);
02163 XSH_FREE( vythedata);
02164 XSH_FREE( corr_x);
02165 XSH_FREE( corr_y);
02166 XSH_FREE( gaussian_pos_x);
02167 XSH_FREE( gaussian_pos_y);
02168 XSH_FREE( gaussian_sigma_x);
02169 XSH_FREE( gaussian_sigma_y);
02170 XSH_FREE( gaussian_fwhm_x);
02171 XSH_FREE( gaussian_fwhm_y);
02172 XSH_FREE( gaussian_norm);
02173 XSH_FREE( sort);
02174 XSH_FREE( diffx);
02175 XSH_FREE( diffy);
02176 XSH_FREE( diffxmean);
02177 XSH_FREE( diffymean);
02178 XSH_FREE( diffxsig);
02179 XSH_FREE( diffysig);
02180 XSH_FREE( rejected);
02181
02182 xsh_free_table( &wave_trace);
02183 xsh_spectralformat_list_free(&spectralformat_list);
02184 xsh_pre_free( &pre);
02185 xsh_the_map_free( &themap);
02186 xsh_wavesol_free( &wave_table);
02187 xsh_wavesol_free( &wave_tab_guess);
02188 xsh_order_list_free( &order_tab_recov);
02189 xsh_resid_tab_free( &resid_orders);
02190 xsh_resid_tab_free( &resid);
02191 xsh_arclist_free( &arclist);
02192 return;
02193 }
02194
02195