function sweep = make_sweep(lidar)
% ==================================================================
%
%	MATLAB m-file for splitting up lidar data into several sweeps
%
%				Alexander Gohm
%				University of Innsbruck, Austria
%				alexander.gohm@uibk.ac.at
%				May 2000
%
%               Modified by Jason Porter for CTI Lidar data format
%               July 2002
%
%	INPUT:	data			....	structure cell array containing lidar data
%										read with VME_Extract_Prod_SingLOS_V2.m
%
%	OUTPUT:	    Sweep ranges displayed to screen.
%
%               AND
%
%               sweep.....	structure array with lidar sweep data
%										containing "i" sweeps:
%
%				sweep{nfi,isweep,1}.. sweep ID number
%				sweep{nfi,isweep,2}.. file name
%				sweep{nfi,isweep,3}.. scan type ('RHI' or'PPI')
%				sweep{nfi,isweep,4}.. fixed angle of sweep
%				sweep{nfi,isweep,5}.. array containing record numbers in sweep
%
%
% ==================================================================


% ==== CHECK IF INPUT DATA EXIST ==================================

if ~exist('lidar','var')
    disp('LIDAR "Data"  DOES NOT EXIST!!')
    return
end

% ==== determine motor speed for SWEEPS ==========================
dt=diff(lidar.lidar_decimal_time)*3600;   %timesteps in (s)
azspeed=diff(lidar.azimuth)./dt;        %azimuth motor speed in (deg/s)
elspeed=diff(lidar.elevation)./dt;      %elevation motor speed in (deg/s)

az_threshold=0.2;
el_threshold=0.2;
sweept=[];
ind=[];
% Determine any RHI scans (azimuth motor off, elevation motor on)

ind=find(abs(azspeed)<az_threshold & abs(elspeed)>el_threshold) ;

if ~isempty(ind)
    delind=diff(ind);
    ii=find(delind~=1);
    if ~isempty(ii)
        nrhi=length(ii);
        start=ind(1);
        for j=1:nrhi
            endpt=ind(ii(j));
            sweept(j).type='RHI';
            sweept(j).fixed=mean(lidar.azimuth(start:endpt));
            sweept(j).ind=[start:endpt];
            start=ind(ii(j)+1);
        end
        endpt=ind(end)
        sweept(j+1).type='RHI';
        sweept(j+1).fixed=mean(lidar.azimuth(start:endpt));
        sweept(j+1).ind=[start:endpt];
        
    end
end


% Determine any  PPI scans (aelevation motor off, azimuth motor on)

ind=find(abs(azspeed)>az_threshold & abs(elspeed)<el_threshold) ;
if isempty(sweept)
    nb=0;
else
    nb=length(sweept);
end

if ~isempty(ind)
    delind=diff(ind);
    ii=find(delind~=1);
    if ~isempty(ii)
        nrhi=length(ii);
        start=ind(1);
        for j=1:nrhi
            endpt=ind(ii(j));
            sweept(nb+j).type='PPI';
            sweept(nb+j).fixed=mean(lidar.elevation(start:endpt));
            sweept(nb+j).ind=[start:endpt];
            start=ind(ii(j)+1);
        end
        endpt=ind(end)
        sweept(nb+j+1).type='PPI';
        sweept(nb+j+1).fixed=mean(lidar.elevation(start:endpt));
        sweept(nb+j+1).ind=[start:endpt];
        
    end
end

for j=1:length(sweept)
    ll(j)=length(sweept(j).ind);
end

clear ii
ii=find(ll>10);
for j=1:length(ii)
    sweep(j)=sweept(ii(j));
end
clear sweept
return