function lidar_proc=cloud_deck(lidar)
% use a relatively simple threshold on SNR to find cloud deck (individual
% beams). If no cloud deck is determined an attempt is made to identify MLH
SNRthresh=10*log10(1.);
lidar_proc=lidar;
[na,nr]=size(lidar.SNR);
SNR=lidar.SNR;
SNRdb=lidar.SNRdb;
RWS=lidar.RWS;
Beta=lidar.Beta;
cloud.base=[];
cloud.width=[];
cloud.xc=[];
cloud.yc=[];
cloud.zc=[];
% check for cloud deck Clouds are assumed to be larger SNR. Minimum SNR for
% signal assumed to be 1.008 and potential cloud deck should have SNR > 2
for ja=1:na
    windowSize=20;  %window size for smoothing filter
    S=filter(ones(1,windowSize)/windowSize,1,lidar.SNRdb(ja,:));
    PP=findpeaks(lidar.ranges,S,0.0001,SNRthresh,20,13,3);
    ind=find(PP(:,2)>lidar.ranges(1));
    if PP(1,1)==0
        P=PP;
    else
      for j=1:length(ind)
        P(j,:)=PP(ind(j),:);
      end
    end
     [np,lp]=size(P);
     if np>1
        for j=1:np
           P(j,1)=j;
        end
     end
     np=P(end,1);
  
    if (np==0)
        ib=find(lidar.SNRdb(ja,:)<-19);  % no good data for SNR<-20
        SNR(ja,ib)=NaN;
        SNRdb(ja,ib)=NaN;
        RWS(ja,ib)=NaN;
        Beta(ja,ib)=NaN;
        cloud(ja).base=NaN;
        cloud(ja).width=NaN;
        cloud(ja).xc=NaN;
        cloud(ja).yc=NaN;
        cloud(ja).zc=NaN;
    elseif np==1
        loclow=floor(((P(1,2)-P(1,4)/2)-lidar.ranges(1))/3);
        lochigh=floor(((P(1,2)+P(1,4)/2)-lidar.ranges(1))/3);
        if loclow<1
            loclow=1;
        end
        if lochigh>nr
            lochigh=nr;
        end
        locmid=floor((P(1,2)-lidar.ranges(1))/3);
        snrlow=mean(SNR(ja,1:loclow));
        snrhigh=mean(SNR(ja,lochigh:end));
        snrpeak=mean(SNR(ja,loclow:lochigh));

        cloud(ja).base=P(1,2);
        cloud(ja).width=P(1,4);
        cloud(ja).xc=lidar.xd(ja,locmid);
        cloud(ja).yc=lidar.yd(ja,locmid);
        cloud(ja).zc=lidar.zd(ja,locmid);
        if snrhigh<-19
            SNR(ja,lochigh:nr)=NaN;
            SNRdb(ja,lochigh:nr)=NaN;
            RWS(ja,lochigh:nr)=NaN;
            Beta(ja,lochigh:nr)=NaN;
        end
    else  % (np>1)
        ipnt=1;
        for jp=1:np
            cloud(ja).base(ipnt)=P(jp,2);
            cloud(ja).width(ipnt)=P(jp,4);
            loclow(ipnt)=floor(((P(jp,2)-P(jp,4)/2)-lidar.ranges(1))/3);
            lochigh(ipnt)=floor(((P(jp,2)+P(jp,4)/2)-lidar.ranges(1))/3);
            locmid(ipnt)=floor((P(jp,2)-lidar.ranges(1))/3);
            cloud(ja).xc(ipnt)=lidar.xd(ja,locmid(ipnt));
            cloud(ja).yc(ipnt)=lidar.yd(ja,locmid(ipnt));
            cloud(ja).zc(ipnt)=lidar.zd(ja,locmid(ipnt));
            if loclow(ipnt)<1
                loclow(ipnt)=1;
            end
            if lochigh(ipnt)>nr
                lochigh(ipnt)=nr;
            end
            ipnt=ipnt+1;
        end
        locat(1)=1;
        ind=2;
        for jp=1:np
            locat(ind)=loclow(jp);
            locat(ind+1)=lochigh(jp);
            ind=ind+2;
        end
        locat(length(locat)+1)=nr;
        for jp=1:length(locat)-1;
            snreg(jp)=mean(lidar.SNRdb(ja,locat(jp):locat(jp+1)));
        end
        for jreg=1:length(snreg)
            if snreg(jreg)<-19
                SNR(ja,locat(jreg):locat(jreg+1))=NaN;
                SNRdb(ja,locat(jreg):locat(jreg+1))=NaN;
                RWS(ja,locat(jreg):locat(jreg+1))=NaN;
                Beta(ja,locat(jreg):locat(jreg+1))=NaN;
            end
        end
    end
    clear locat ib snreg
end
ind=find(SNRdb<-19);
SNR(ind)=NaN;
SNRdb(ind)=NaN;
RWS(ind)=NaN;
Beta(ind)=NaN;
lidar_proc.SNR_filt=SNR;
lidar_proc.SNRdb_filt=SNRdb;
lidar_proc.RWS_filt=RWS;
lidar_proc.Beta_filt=Beta;
lidar_proc.cloud=cloud;

return