function get_wl_shift,star_id,n_ext,r_elo=r_elo,wl_elo=wl_elo,$
        t0=t0,g0=g0,z0=z0,exclreg=exclreg0,$
        iu_atm_par=iu_atm_par,precise=precise,max_cc=max_cc,verbose=verbose,$
        zoom=zoom,xamp=xamp,plot=plot,debug=debug,sm_w=sm_w,em_lines=em_lines,k_teff=k_teff

    if(n_elements(k_teff) ne 1) then k_teff=1.0
    if(n_elements(em_lines) ne 1) then em_lines=-1
    if(n_elements(sm_w) ne 1) then sm_w=0.0

    max_cc=!values.f_nan
    r_work=mrdfits('BINTABLE/'+star_id+'.fits',n_ext,/silent)
    atm_par_idx=where(iu_atm_par.name eq star_id,catm_par_idx)
    atm_par=(catm_par_idx eq 1)? iu_atm_par[atm_par_idx] : {TEFF:6000.0,LOG_G_:4.44,_FE_H_:0.00,SPTYPE:'F8V'}
    if(atm_par._FE_H_ lt -1.5) then atm_par._FE_H_=-1.5
    if(atm_par.LOG_G_ gt 4.5) then atm_par.LOG_G_=4.5

    if(((strpos(atm_par.sptype,'e') gt -1) and (em_lines ne 0)) or (em_lines eq 1)) then begin ;;; emission-line stars
        n_excl=n_elements(exclreg0[0,*])
        exclreg=dblarr(2,n_excl+7)
        exclreg[*,0:n_excl-1]=exclreg0
        exclreg[*,n_excl]=[6563.0,15.0]
        exclreg[*,n_excl+1]=[4861.0,12.0]
;        exclreg[*,n_excl+2]=[4340.0,12.0]
        exclreg[*,n_excl+3]=[5876.0,8.0]
        exclreg[*,n_excl+4]=[6678.0,8.0]
        exclreg[*,n_excl+5]=[6891.0,8.0]
        exclreg[*,n_excl+6]=[7176.0,8.0]
    endif else exclreg=exclreg0
    
    d_tgm=sqrt(k_teff^2*(t0-alog10(atm_par.teff))^2+(g0-atm_par.log_g_)^2+(z0-atm_par._fe_h_)^2)
    min_d_tgm=min(d_tgm,imin_d_tgm)
    if(keyword_set(verbose)) then print,'Parameters:     ',atm_par.teff,atm_par.log_g_,atm_par._fe_h_
    if(keyword_set(verbose)) then print,'The best model: ',10^t0[imin_d_tgm], g0[imin_d_tgm],z0[imin_d_tgm]
    
    min_wwl=min(r_work.wavelength,max=max_wwl)
    elo_idx=where(wl_elo ge min_wwl and wl_elo le max_wwl, celo_idx)
    if(celo_idx lt 300) then begin
;        message,'Too low wavelength overlap',/inf
        return,!values.f_nan
    endif
    
    wl_elo_w=wl_elo[elo_idx]
    fl_elo_w0=r_elo[elo_idx,imin_d_tgm]
    gr_idx=where((mask_exclreg(r_work.wavelength,exclreg) eq 0) and $
                 (finite(r_work.spectrum) eq 1) and $
                 (r_work.spectrum gt 0), cgr_idx)
    good_elo=where(mask_exclreg(wl_elo_w,exclreg) eq 0 and finite(fl_elo_w0) eq 1,cgood_elo)
        
    fl_elo_w=interpol(fl_elo_w0[good_elo],wl_elo_w[good_elo],wl_elo_w)
    fl_ui_w=interpol(r_work.spectrum[gr_idx],r_work.wavelength[gr_idx],wl_elo_w)
    
    reg_cont=range(0.06*n_elements(wl_elo_w),0.94*n_elements(wl_elo_w))
    k_cnt_elo=poly_fit((10d*double(wl_elo_w/median(wl_elo_w)-1.0))[reg_cont],(double(median(fl_elo_w,0.1*celo_idx)))[reg_cont],5)
    k_cnt_iu=poly_fit((10d*double(wl_elo_w/median(wl_elo_w)-1.0))[reg_cont],(double(median(fl_ui_w,0.1*celo_idx)))[reg_cont],5)
    cnt_elo=poly(10d*double(wl_elo_w/median(wl_elo_w)-1.0),k_cnt_elo)
    cnt_iu=poly(10d*double(wl_elo_w/median(wl_elo_w)-1.0),k_cnt_iu)
    
;    vec1=fl_elo_w/cnt_elo-1.0
;    vec2=fl_ui_w/cnt_iu-1.0
    vec1=((fl_elo_w-cnt_elo)/median(cnt_elo))[reg_cont]
    vec2=((fl_ui_w-cnt_iu)/median(cnt_iu))[reg_cont]
    if(sm_w gt 0.0) then begin
        vec1=smooth(vec1,sm_w,/nan)
        vec2=smooth(vec2,sm_w,/nan)
    endif

    if(n_elements(zoom) ne 1) then zoom=5.0
    if(n_elements(xamp) ne 1) then xamp=60.0
    xvec=findgen(2L *long(xamp*zoom)+1L)/zoom-xamp

    vec1m=rebin(vec1,long(n_elements(vec1))*zoom)
    vec2m=rebin(vec2,long(n_elements(vec2))*zoom)
    
    cc_vec=c_correlate(vec1m,vec2m,zoom*xvec)
    max_cc=max(cc_vec,imax_cc)
    if(keyword_set(precise) and imax_cc gt 20 and imax_cc lt n_elements(xvec)-20) then begin
        kp=poly_fit(xvec[imax_cc-20:imax_cc+20],cc_vec[imax_cc-20:imax_cc+20],2)
        dwlpix=-kp[1]/(2.0*kp[2])
    endif else dwlpix=xvec[imax_cc]
    
    if(keyword_set(verbose)) then print,'WL shift (A) =',dwlpix*(wl_elo_w[1]-wl_elo_w[0])

    dwla=dwlpix*(wl_elo_w[1]-wl_elo_w[0])
    if(max_cc lt 0.5 or abs(dwla) gt 5.0) then begin
        print,'WL shift (A) =',dwla,'  K_corr=',max_cc
    endif

    if(star_id eq '109387' and n_ext eq 7) then dwla=0.0 ;; HD109387 4700
    if(star_id eq '109387' and n_ext eq 9) then dwla=0.0 ;; HD109387 5450
    if(star_id eq '109387' and n_ext eq 10) then dwla=0.0 ;; HD109387 6800
    if(star_id eq '109387' and n_ext eq 11) then dwla=0.0 ;; HD109387 8100
    if(star_id eq '164284' and n_ext eq 8) then dwla=0.0 ;; HD164284 5450
    if(star_id eq '191610' and n_ext eq 10) then dwla=0.0 ;; HD191610 5450
    if(star_id eq '191639' and n_ext eq 10) then dwla=0.0 ;; HD191639 5450
    if(star_id eq '193621' and n_ext eq 9) then dwla=0.0 ;; HD193621 6800
    if(star_id eq '202904' and n_ext eq 7) then dwla=0.0 ;; HD202904 5450
    if(star_id eq '212571' and n_ext eq 11) then dwla=0.0 ;; HD212571 6800
    if(star_id eq '212571' and n_ext eq 12) then dwla=0.0 ;; HD212571 6800
    if(star_id eq '31996' and n_ext eq 10) then dwla=0.0 ;; HD31996 3450 low s/n
    if(star_id eq '5394' and n_ext eq 11) then dwla=0.0 ;; HD5394 5450 low s/n
    if(star_id eq '33111' and n_ext eq 7) then dwla=0.0 ;; HD31111 4700 low s/n
    if(star_id eq 'BD+04_4551' and n_ext eq 9) then dwla=0.0 ;; BD+04_4551 6800
    if(star_id eq '40183' and n_ext eq 9) then dwla=-0.13 ;; HD40183 4700
    if(star_id eq '40183' and n_ext eq 10) then dwla=0.00 ;; HD40183 3450
    if(star_id eq '40183' and n_ext eq 11) then dwla=0.00 ;; HD40183 5450
    if(star_id eq 'BD+45_1668' and n_ext eq 7) then dwla=-0.45 ;; BD+04_4551 6800
    if(star_id eq 'BD+45_1668' and n_ext eq 9) then dwla=-0.31 ;; BD+04_4551 6800
    if(star_id eq '138279' and n_ext eq 9) then dwla=0.0 ;; HD138279 6800
    if(star_id eq '156164' and n_ext eq 4) then dwla=0.0 ;; HD156164 6800
    if(star_id eq '164284' and n_ext eq 12) then dwla=-1.1 ;; HD156164 6800
    if(star_id eq '202904' and n_ext eq 11) then dwla=-0.35 ;; HD202904 8100
    if(star_id eq '25329' and n_ext eq 9) then dwla=0.0;; HD25329 6800
    if(star_id eq '25940' and n_ext eq 10) then dwla=0.0 ;; HD25940 8100
    if(star_id eq '35497' and n_ext eq 11) then dwla=0.0 ;; HD35497 8100
    if(star_id eq '4306' and n_ext eq 9) then dwla=1.45 ;; HD4306 6800
    if(star_id eq '5394' and n_ext eq 9) then dwla=0.0 ;; HD5394 8100
    if(star_id eq '5394' and n_ext eq 10) then dwla=0.0 ;; HD5394 6800
    if(star_id eq '58343' and n_ext eq 13) then dwla=0.0 ;; HD58343 8100
    if(star_id eq '149757' and n_ext eq 14) then dwla=0.0 ;; HD149757 8100
    if(star_id eq '154445' and n_ext eq 12) then dwla=0.345 ;; HD154445 8100
    if(star_id eq '161056' and n_ext eq 8) then dwla=0.0 ;; HD161056 8100
    if(star_id eq '183144' and n_ext eq 12) then dwla=0.0 ;; HD183144 8100
    if(star_id eq '196740' and n_ext eq 10) then dwla=0.513 ;; HD183144 8100
    if(star_id eq '54662' and n_ext eq 11) then dwla=0.0 ;; HD54662 8100
    if(star_id eq '177940' and n_ext eq 7) then dwla=0.0 ;; HD177940 3450
    if(star_id eq '166014' and n_ext eq 11) then dwla=0.0 ;; 
    if(star_id eq '187811' and n_ext eq 12) then dwla=0.0 ;; 8100
    if(star_id eq '161056' and n_ext eq 8) then dwla=0.0 ;;  8100
    if(star_id eq '149757' and n_ext eq 14) then dwla=0.0 ;; 8100
    if(star_id eq '54662' and n_ext eq 11) then dwla=0.0 ;; 8100
    if(star_id eq '17378' and n_ext eq 9) then dwla=0.0 ;; 8100
    if(star_id eq '25940' and n_ext eq 11) then dwla=0.0 ;; 5450
    if(star_id eq '168092' and n_ext eq 10) then dwla=0.0 ;; 5450
    if(star_id eq '168092' and n_ext eq 11) then dwla=0.0 ;; 8100
    if(star_id eq '167771' and n_ext eq 7) then dwla=0.0 ;; 6800
    if(star_id eq '167771' and n_ext eq 11) then dwla=0.0 ;; 8100

    if(keyword_set(plot)) then begin
        !p.multi=[0,1,2]
        plot,wl_elo_w[reg_cont],vec1,xs=1,ys=1,yr=[-1.0,1.0]
        oplot,wl_elo_w[reg_cont],vec2,col=254
        plot,xvec*(wl_elo_w[1]-wl_elo_w[0]),cc_vec,xs=1,ys=1,yr=[0,1]
        oplot,[dwla,dwla],[0,1],col=254
        !p.multi=0
    endif
    if(keyword_set(debug)) then begin
        aaa=''
        read,aaa
    endif
    
    return,dwla
end



iu_atm_par=get_atm_par()

r_elo=readfits('/usr/local/PEGASE-HR/data/stellibs/stellibELODIE_3.1.fits',h_elo,/silent)
t_elo=mrdfits('/usr/local/PEGASE-HR/data/stellibs/stellibELODIE_3.1.fits',1,/silent)
parse_spechdr,h_elo,wl=wl_elo
n_grid=n_elements(t_elo)
t0=t_elo.teff
g0=double(t_elo.logg)
z0=double(alog10(t_elo.z/0.02d))


exclreg=[[3913.0,13.5],[5890.0,1.4],[5896.0,1.4],[6891.0,10.0],[7640.0,35.0],[8620.0,30.0],[10000.0,10.0]]

f_inp='extnames_indous_all.txt'
f_out='extnames_indous_all_wlcorr_elodie.txt'

s_l=read_asc(f_inp,/str)
n_seg=n_elements(s_l[0,*])

openw,u,f_out,/get_lun

for i=0,n_seg-1 do begin
    em_lines=-1

    star_id=s_l[0,i]
    n_ext=fix(s_l[2,i])

    if((star_id eq '187961') or $
       (star_id eq '191639') or $
;       (star_id eq '192685') or $
       (star_id eq 'BD+04_4551') $
       ) then em_lines=1

    val=get_wl_shift(star_id,n_ext,r_elo=r_elo,wl_elo=wl_elo,em_lines=em_lines,$
            t0=t0,g0=g0,z0=z0,exclreg=exclreg,iu_atm_par=iu_atm_par,max_cc=max_cc,/precise)
    if(finite(val) ne 1) then val=0.0
    print,s_l[*,i],val,max_cc,format='(a10,1x,a12,2x,i4,2f10.2,2x,2f8.3)'
    printf,u,s_l[*,i],val,max_cc,format='(a10,1x,a12,2x,i4,2f10.2,2x,2f8.3)'
endfor

close,u
free_lun,u

end
