
files=dir('C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\modified\rhi\*.dat');
load('wind_tr0910');
xi=[];
zi=[];
vel=[];
ele_ppi=[0.5;0.8;1.1;1.4;1.7;2.0;2.3;2.6;2.9;3.2;3.5;3.8;4.1;4.5;5.0;5.5;6.0;6.5;7.0;7.5;8.0;8.5];%[0.75 2.25 3.75 5.25 6.75 8.25];[0.5 0.8 1.5 2.2 3.0 5.2 6.5 7.9 9.5];%
for fil=154:length(files)
    filename=files(fil).name;
    rhi_original=load(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\modified\rhi\',filename]);
    az=str2num(filename(30:32));
    ele_rhi=rhi_original(length(rhi_original),:);
    u_el=find(ele_rhi<=9.5);
    ele_rhi=ele_rhi(u_el);
    L=length(ele_rhi);
    rhi=rhi_original(1:600,1:L);
    [m,n]=size(rhi);
    r=0:67:67*m-1;
    rhi_ws=[];
    rhi_wd=[];
    L1=m*n;
    
    [th,rg]=meshgrid(deg2rad(ele_rhi),r);
    [h,i]=pol2cart(th,rg);
    xi=reshape(h,L1,1);
    zi=reshape(i,L1,1);
    mod_rhi=reshape(rhi(1:m,:),L1,1);
    
    
    u=wind.u;
    v=wind.v;
    ws=wind.ws;
    [li,col]=size(u);
    L2=li*col;
    [th,rg]=meshgrid(deg2rad(ele_ppi),r);
    [h,i]=pol2cart(th,rg);
    x=reshape(h,L2,1);
    z=reshape(i,L2,1);
    u=reshape(u(1:li,:),L2,1);
    v=reshape(v(1:li,:),L2,1);
    ws=reshape(ws(1:li,:),L2,1);
    
    ui=griddata(x,z,u,xi,zi);
    vi=griddata(x,z,v,xi,zi);
    wsi=griddata(x,z,ws,xi,zi);
    
    
    wdi=[];
    for i=1:L1
        if ui(i) >0 & vi(i) >0
            alpha=atan(ui(i)/vi(i));
            alpha=pi+alpha;
            
        elseif ui(i) >0 & vi(i) <0
            alpha=-atan((vi(i)/ui(i)));
            alpha=3*pi/2+alpha;
            
        elseif ui(i) <0 & vi(i) >0
            alpha=-atan((vi(i)/ui(i)));
            alpha=pi/2+alpha;
            
        elseif ui(i) <0 & vi(i) <0
            alpha=atan(ui(i)/vi(i));
        else
            alpha=NaN;
        end
        alpha=rad2deg(alpha);
        wdi=[wdi alpha];
    end
    clear('ui','vi');
    rhi_ws=wsi;
    rhi_wd=wdi';
    
    
    beta=abs(az-rhi_wd);
u_bad_angle=find(beta <0 | beta >=360);
beta(u_bad_angle)=NaN;

u_NaN=find(isnan(beta));



u_toward=find(beta < 90  | beta >= 270);
toward=-rhi_ws(u_toward).*cos(deg2rad(beta(u_toward)));


u_away=find(beta >= 90  & beta <270);
beta(u_away)=abs(beta(u_away)-180);
away=rhi_ws(u_away).*cos(deg2rad(beta(u_away)));

%rhi_radial=ones(750,L);
rhi_radial(u_away)=away;
rhi_radial(u_toward)=toward;
rhi_radial(u_NaN)=NaN;
meanless_rhi=mod_rhi-rhi_radial';
meanless_rhi=reshape(meanless_rhi,m,n);

eli=0.5:0.1:8.5;
rhii=[];
for rg=1:600
        temp=interp1(ele_rhi,meanless_rhi(rg,:),eli,'linear');
        rhii=[rhii;temp];
    end

 clear('rhi_radial');
    save(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\modified\meanless\rhi\',filename],'rhii','eli','-ascii')

end