function [x_km,flt_ws_final,ws_radar_final]=flight_level_ws(input_data) %[x_km,flt_tke]=flight_level_tke(data)%
rappel=input_data.file_number;

filename=dir(['C:\Documents and Settings\sylvie.lorsolo\My Documents\research\HRD research\Gamache_soft\wfiles\',input_data.hurricane,'\',input_data.jour,'\*.w']) ;
fichier=filename(input_data.file_number).name
ws_radar_final=[];
file_flight=dir([['C:\Documents and Settings\sylvie.lorsolo\My Documents\research\HRD research\Gamache_soft\wfiles\',input_data.hurricane,'\',input_data.jour(1:2)],'\flight_data\*']) 

[temp_t, temp_lat, temp_lon,temp_rad, temp_wd, temp_ws, temp_w, temp_GS]=textread(['C:\Documents and Settings\sylvie.lorsolo\My Documents\research\HRD research\Gamache_soft\wfiles\',input_data.hurricane,'\',input_data.jour(1:2),'\flight_data\',fichier(1:7)],'%f %f %f %f %f %f %f %f', 'headerlines',4,'bufsize', 8191);



file_info=dir([['C:\Documents and Settings\sylvie.lorsolo\My Documents\research\HRD research\Gamache_soft\wfiles\',input_data.hurricane,'\',input_data.jour(1:2)],'\*',fichier(10:13),'*leg*']) 
fichier2=['C:\Documents and Settings\sylvie.lorsolo\My Documents\research\HRD research\Gamache_soft\wfiles\',input_data.hurricane,'\',input_data.jour(1:2),'\',file_info.name];

leg_data=textread(fichier2,'%s','bufsize', 8191);

inb_az=str2num(cell2mat(leg_data(17)));
t0=str2num(cell2mat(leg_data(1)));
t1=str2num(cell2mat(leg_data(4)));
tc=str2num(cell2mat(leg_data(7)));


u_t=find(temp_t>=t0 & temp_t <= t1);
wsi=temp_ws(u_t);
wdi=temp_wd(u_t);
w_insti=temp_w(u_t);
lati=temp_lat(u_t);
loni=-temp_lon(u_t);
ti=temp_t(u_t);
alt_radi=temp_rad(u_t);
[u_insti,v_insti]=wind_decomposition(wdi,wsi);
L=length(u_t);
u_center=find(ti==tc);



lat0=repmat(lati(u_center),L,1);
lon0=repmat(loni(u_center),L,1);
xaxisi=distdim(distance(lat0, lon0, lati, loni),'deg','m');
i_center=u_center(1);
xaxisi(i_center:L)=-xaxisi(i_center:L);


x_inb_leg=flipud((750:1500:xaxisi(1)+700)');
x_oub_leg=(-750:-1500:xaxisi(L)-750)';
x_leg=[x_inb_leg;x_oub_leg];

[x_sort,position]=sort(xaxisi);
u_loop=find(diff(x_sort)==0);
same_axis=length(u_loop)
while same_axis >0

   xaxisi(position(u_loop))=[];
    u_insti(position(u_loop))=[];
    v_insti(position(u_loop))=[];
    
    alt_radi(position(u_loop))=[];
    ti(position(u_loop))=[];
lati(position(u_loop))=[];
loni(position(u_loop))=[];
[x_sort,position]=sort(xaxisi);
u_loop=find(diff(x_sort)==0);
same_axis=length(u_loop);
end
% figure
% axesm('mercator','MapLatLimit',[min(lati)-15 max(lati)+15],'MapLonLimit',[min(loni)-15 max(loni)+15]);
% plotm(lati,loni);
% 
% reply = input('Did they circle in the center? y/n [y]: ','s');
% if strcmp(reply,'y')==1
%     
%     disp('first start point')
%     [start_lat,start_long]=inputm(1);%find((diff(xaxis))>0);
%     disp('now end point')
%     [end_lat,end_long]=inputm(1);
%     dist_up=distdim(distance(lat0(1,1), lon0(1,1), start_lat,start_long),'deg','m');
%     dist_down=distdim(distance(lat0(1,1), lon0(1,1), end_lat,end_long),'deg','m');
%     no_looping=find(xaxisi > dist_up | xaxisi < -dist_down);
%     
%     
%     xaxis2=xaxisi(no_looping);%[xaxis(1:start_pt(1));xaxis(u_neg(length(u_neg)):length(xaxis))];
%     u_inst2=u_insti(no_looping);
%     v_inst2=v_insti(no_looping);
%     w_inst2=w_insti(no_looping);
%     alt_rad2=alt_radi(no_looping);
% else
    u_inst2=u_insti;
    v_inst2=v_insti;
    w_inst2=w_insti;
    xaxis2=xaxisi;
    alt_rad2=alt_radi;
% end

u_zeros=find((diff(xaxis2))~=0);

if length(u_zeros) > 0
    u_inst=u_inst2(u_zeros);
    v_inst=v_inst2(u_zeros);
    w_inst=w_inst2(u_zeros);
    xaxis=xaxis2(u_zeros);
    alt_rad=alt_rad2(u_zeros);
end

dd=diff(xaxis);
u_neg1=find(dd>=0);
if length(u_neg1)>0
u_inst(u_neg1(1): u_neg1(end))=[];
v_inst(u_neg1(1): u_neg1(end))=[];
w_inst(u_neg1(1): u_neg1(end))=[];
xaxis(u_neg1(1): u_neg1(end))=[];
end


temp=[xaxis;x_leg];
xi=flipud(sort(temp));
u_leg=interp1(xaxis,u_inst,xi);
v_leg=interp1(xaxis,v_inst,xi);
w_leg=interp1(xaxis,w_inst,xi);

for i=1:length(x_leg)-1
    u_1500=find(xi<=x_leg(i) & xi > x_leg(i+1));
    U(i,1)=mean(u_leg(u_1500));
    V(i,1)=mean(v_leg(u_1500));
    W(i,1)=mean(w_leg(u_1500));
    x_km(i,1)=(x_leg(i)-750)/1000;
    
    if length(u_1500)==1
         U(i,1)=NaN;
         V(i,1)=NaN;
         W(i,1)=NaN;
    end
    
end
close

flt_ws=sqrt((U.^2) + (V.^2));
u_0_abs=find(x_km == 0);
x_km(u_0_abs)=[];
flt_ws(u_0_abs)=[];

u_bound_abs=find(x_km > 0);
x_km_inb=x_km(u_bound_abs);
flt_inb_ws=flt_ws(u_bound_abs);
u_bound_abs=find(x_km < 0);
x_km_inb=x_km(u_bound_abs);
flt_outb_ws=flt_ws(u_bound_abs);
close


plot(x_km,flt_ws,'--','LineWidth',2)
hold on

temp_k=input_data.file_number;
for k=temp_k:2:temp_k+2
    input_data.file_number=k;
    r_final=[];
    indice=[];
    [ws,radwind,tangwind,w,dbz, x_fill, y_fill, z_fill,L]=read_w_files(input_data);
    fichier=filename(input_data.file_number).name
    
    low_data=min(alt_rad);
    high_data=max(alt_rad);
    u_z=find(z_fill >=low_data-150 & z_fill <= high_data +150);
    
    
    
    
    
    plot((x_fill)/1000,nanmean((ws(:,u_z(1):u_z(end)))'),'r')
    xlabel('Radius (km)')
    ylabel('WS (m^.s^{-1})')
    title(['Comparison with flight level data for ', input_data.hurricane(6:end-1), ' ', input_data.jour(1:2), ' ' ,fichier(10:13)])
    legend('Flight Level Data','Radar Data')
    
    ws_radar_all=nanmean((ws(:,u_z(1):u_z(end)))');
    if length(findstr(fichier,'inbound'))==1
        u_x=find(x_km > 0);
        xx=x_km(u_x);
        ws_radar=flipud(ws_radar_all(1:length(xx))');
        
        for bias=0:8
            temp_flt_ws=[flt_inb_ws;NaN*ones(bias,1)];
            temp_ws_radar=[NaN*ones(bias,1);ws_radar];
            u_nan=find(isnan(temp_ws_radar)==1);
            temp_ws_radar(u_nan)=[];
            temp_flt_ws(u_nan)=[];
            u_nan=find(isnan(temp_flt_ws)==1);
            temp_flt_ws(u_nan)=[];
            temp_ws_radar(u_nan)=[];
            temp_r=corrcoef(temp_ws_radar, temp_flt_ws);
            r_final=[r_final;temp_r(1,2)];
            indice=[indice;bias];
            
        end
        
        
        for bias=1:8
            temp_flt_ws=[NaN*ones(bias,1);flt_inb_ws];
            temp_ws_radar=[ws_radar;NaN*ones(bias,1)];
            u_nan=find(isnan(temp_ws_radar)==1);
            temp_ws_radar(u_nan)=[];
            temp_flt_ws(u_nan)=[];
            u_nan=find(isnan(temp_flt_ws)==1);
            temp_flt_ws(u_nan)=[];
            temp_ws_radar(u_nan)=[];
            temp_r=corrcoef(temp_ws_radar, temp_flt_ws);
            r_final=[r_final;temp_r(1,2)];
            indice=[indice;-bias];
            
            
        end
        [max_corr,index_max]=max(r_final);
        
        
        final_shift_inb=indice(index_max);
        
        if final_shift_inb >0
            flt_ws_final_inb=[flt_inb_ws;NaN*ones(final_shift_inb,1)];
            ws_radar_final_inb=[NaN*ones(final_shift_inb,1);ws_radar];
            u_nan=find(isnan(ws_radar_final_inb)==1);
            ws_radar_final_inb(u_nan)=[];
            flt_ws_final_inb(u_nan)=[];
            u_nan=find(isnan(flt_ws_final_inb)==1);
            flt_ws_final_inb(u_nan)=[];
            ws_radar_final_inb(u_nan)=[];
            corrcoef(ws_radar_final_inb, flt_ws_final_inb)
        else
            
            flt_ws_final_inb=[NaN*ones((-final_shift_inb),1);flt_inb_ws];
            ws_radar_final_inb=[ws_radar;NaN*ones((-final_shift_inb),1)];
            u_nan=find(isnan(ws_radar_final_inb)==1);
            ws_radar_final_inb(u_nan)=[];
            flt_ws_final_inb(u_nan)=[];
            u_nan=find(isnan(flt_ws_final_inb)==1);
            flt_ws_final_inb(u_nan)=[];
            ws_radar_final_inb(u_nan)=[];
            
            corrcoef(ws_radar_final_inb, flt_ws_final_inb)
        end
        
        
        
        
        
        
        
        
    elseif length(findstr(fichier,'outbound'))==1
        u_x=find(x_km < 0);
        xx=x_km(u_x);
        ws_radar=ws_radar_all(1:length(xx))';
        
        
        
        for bias=0:8
            temp_flt_ws=[flt_outb_ws;NaN*ones(bias,1)];
            temp_ws_radar=[NaN*ones(bias,1);ws_radar];
            u_nan=find(isnan(temp_ws_radar)==1);
            temp_ws_radar(u_nan)=[];
            temp_flt_ws(u_nan)=[];
            u_nan=find(isnan(temp_flt_ws)==1);
            temp_flt_ws(u_nan)=[];
            temp_ws_radar(u_nan)=[];
            temp_r=corrcoef(temp_ws_radar, temp_flt_ws);
            r_final=[r_final;temp_r(1,2)];
            indice=[indice;bias];
            
        end
        
        
        for bias=1:8
            temp_flt_ws=[NaN*ones(bias,1);flt_outb_ws];
            temp_ws_radar=[ws_radar;NaN*ones(bias,1)];
            u_nan=find(isnan(temp_ws_radar)==1);
            temp_ws_radar(u_nan)=[];
            temp_flt_ws(u_nan)=[];
            u_nan=find(isnan(temp_flt_ws)==1);
            temp_flt_ws(u_nan)=[];
            temp_ws_radar(u_nan)=[];
            temp_r=corrcoef(temp_ws_radar, temp_flt_ws);
            r_final=[r_final;temp_r(1,2)];
            indice=[indice;-bias];
            
            
        end
        [max_corr,index_max]=max(r_final);
        
        
        final_shift_outb=indice(index_max);
        
        if final_shift_outb >0
            flt_ws_final_outb=[flt_outb_ws;NaN*ones(final_shift_outb,1)];
            ws_radar_final_outb=[NaN*ones(final_shift_outb,1);ws_radar];
            u_nan=find(isnan(ws_radar_final_outb)==1);
            ws_radar_final_outb(u_nan)=[];
            flt_ws_final_outb(u_nan)=[];
            u_nan=find(isnan(flt_ws_final_outb)==1);
            flt_ws_final_outb(u_nan)=[];
            ws_radar_final_outb(u_nan)=[];
            corrcoef(ws_radar_final_outb, flt_ws_final_outb)
        else
            
            flt_ws_final_outb=[NaN*ones((-final_shift_outb),1);flt_outb_ws];
            ws_radar_final_outb=[ws_radar;NaN*ones((-final_shift_outb),1)];
            u_nan=find(isnan(ws_radar_final_outb)==1);
            ws_radar_final_outb(u_nan)=[];
            flt_ws_final_outb(u_nan)=[];
            u_nan=find(isnan(flt_ws_final_outb)==1);
            flt_ws_final_outb(u_nan)=[];
            ws_radar_final_outb(u_nan)=[];
            
            corrcoef(ws_radar_final_outb, flt_ws_final_outb)
        end
        
        
        
        
    end
    
   
end
%close all


 ws_radar_final=[ws_radar_final_inb;ws_radar_final_outb];

flt_ws_final=[flt_ws_final_inb;flt_ws_final_outb];








figure
plot(x_km,flt_ws,'--','LineWidth',2)
hold on
temp_k=rappel;
for k=temp_k:2:temp_k+2
    input_data.file_number=k;
    
    [ws,radwind,tangwind,w,dbz, x_fill, y_fill, z_fill,L]=read_w_files(input_data);
    fichier=filename(input_data.file_number).name
    
    low_data=min(alt_rad);
    high_data=max(alt_rad);
    u_z=find(z_fill >=low_data-150 & z_fill <= high_data +150);
    if k==temp_k
        final_shift=final_shift_inb;
    else
        final_shift=final_shift_outb;
    end
    
    plot((x_fill-final_shift*1500)/1000,nanmean((ws(:,u_z(1):u_z(end)))'),'r')
    xlabel('Radius (km)')
    ylabel('ws (m^2.s^{-2})')
    title(['Comparison with flight level data for ', input_data.hurricane(6:end-1), ' ', input_data.jour(1:2), ' ' ,fichier(10:13)])
    legend('Flight Level Data','Radar Data')
end


