%VVP-modified Sector Processing of LIDAR data
function prof=vvpanalysis_3_old(vad)
[na nr]=size(vad.RWS);
ind=1;
oops=na*nr;
xd=reshape(vad.xd,oops,1);
yd=reshape(vad.yd,oops,1);
zd=reshape(vad.zd,oops,1);
vr=reshape(vad.RWS,oops,1);
beta=reshape(vad.Beta,oops,1);
snr=reshape(vad.SNR,oops,1);

el=reshape(vad.elevation,oops,1);
az=reshape(vad.azimuth,oops,1);
drange=double(vad.ranges(2)-vad.ranges(1));
prof.heights=[15:1:1000];
dheight=42;
nheights=length(prof.heights);
clear vad
for j=1:length(prof.heights)
    zpp=prof.heights(j);
    indb=find(abs(zd-prof.heights(j))<=dheight/2);
    ind=find((abs(zd-prof.heights(j))<=dheight/2) & (~isnan(vr)) );
%     if (length(ind)<=6 & length(ind)>3)
%         pp=pred(xd(ind),yd(ind),zd(ind),0,0,zpp,el(ind)*pi/180,az(ind)*pi/180,2);
%         vel=pinv(pp)*vr(ind);
%         vel(3)=NaN;
%         betasp=mean(beta(ind));
    if (length(ind)>=9)
        pp=pred(xd(ind),yd(ind),zd(ind),0,0,zpp,el(ind)*pi/180,az(ind)*pi/180,9);
        vel=pinv(pp)*vr(ind);
         %     vel=pp\vr(ind);
        betasp=mean(beta(ind));
        snrsp=mean(snr(ind));
    else
        betasp=NaN;
        snrsp=NaN;
        vel(1:9)=NaN;
    end
    P(j,:)=vel;
    ll=length(vel);
    dvr=0.;
    
    for k=1:length(ind)
        vrt=cosd(el(ind(k)))*(sind(az(ind(k)))*vel(1) +cosd(az(ind(k)))*vel(2)) + ...
            sind(el(ind(k)))*vel(6);
        dvr=dvr+abs(vr(ind(k))-vrt)/length(ind);
    end
    prof.turb(j)=dvr;
    prof.nind(j)=length(ind);
    prof.beta(j)=mean(beta(indb));
    prof.avgsnr(j)=mean(snrsp);
end
prof.vels=P; 
theta=atand(abs(prof.vels(:,1))./abs(prof.vels(:,2)));
for j=1:length(prof.heights)
if (prof.vels(j,1)>0 & prof.vels(j,2)>0)
theta(j)=theta(j)+180;
elseif (prof.vels(j,1)>0 & prof.vels(j,2)<0)
theta(j)=360-theta(j);
elseif (prof.vels(j,1)<0 & prof.vels(j,2)<0)
theta(j)=theta(j);
elseif (prof.vels(j,1)<0 & prof.vels(j,2)>0)
theta(j)=360-theta(j)-180;
end
end
prof.theta=theta;
prof.u=prof.vels(:,1);
prof.v=prof.vels(:,2);
prof.w=prof.vels(:,3);
prof.Vh=sqrt(prof.u.^2+prof.v.^2);
return
