
files=dir('C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\modified\rhi\*.dat');

for fil=221:length(files)
    filename=files(fil).name;
    rhi=load(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\modified\rhi\',filename]);
    az=str2num(filename(30:32));
    ele_rhi=rhi(length(rhi),:);
    u_el=find(ele_rhi<=9);
    ele_rhi=ele_rhi(u_el);
    L=length(ele_rhi)
    rhi=rhi(1:600,1:L);
    %Modify the data for hrd compression
    mod_rhi=rhi;
    u_bad_vel=find(rhi < -100);
    mod_rhi(u_bad_vel)=NaN;
%     alt_rhi=[];
    wd_rhi=[];
    ws_rhi=[];
    [h_mean,ws_mean,u_mean,v_mean]=mean_profile('frances');
    for j=1:L
        wd_col=[];
        %range=33.35:66.7:599*66.7+33.35;
        alt=range*66.7*sin(deg2rad(ele_rhi(j))); 
%         alt_rhi=[alt_rhi alt'];
        ws_col=ws_fit(alt');
        u_col=u_fit(alt');
        v_col=v_fit(alt');
        for i=1:600
            if u_col(i) >0 & v_col(i) >0
                alpha=atan(u_col(i)/v_col(i));
                alpha=pi+alpha;
                
            elseif u_col(i) >0 & v_col(i) <0
                alpha=-atan((v_col(i)/u_col(i)));
                alpha=3*pi/2+alpha;
                
            elseif u_col(i) <0 & v_col(i) >0
                alpha=-atan((v_col(i)/u_col(i)));
                alpha=pi/2+alpha;
                
            elseif u_col(i) <0 & v_col(i) <0
                alpha=atan(u_col(i)/v_col(i));
            else
                alpha=NaN;
            end
            alpha=rad2deg(alpha);
            wd_col=[wd_col;alpha];
        end
        wd_rhi=[wd_rhi wd_col];
        ws_rhi=[ws_rhi ws_col];
    end
    beta=abs(az-wd_rhi);
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=-ws_rhi(u_toward).*cos(deg2rad(beta(u_toward)));


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

rhi_radial=ones(600,L);
rhi_radial(u_away)=away;
rhi_radial(u_toward)=toward;
rhi_radial(u_NaN)=NaN;
meanless_rhi=mod_rhi-rhi_radial;
 clear('rhi_radial');
 save(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\frances\data\modified\meanless\rhi\23\check\',filename],'meanless_rhi','ele_rhi','-ascii')

end