file=uipickfiles('FilterSpec','C:/Users/david/Desktop/Idaho/Raw_Data/*.hpl');
nfiles=length(file);
for j=1:nfiles
    [datapath, filename, ext, versn] = fileparts(char(file(j)));
    if ~exist([datapath,'/analysis/full'],'dir')
        mkdir([datapath,'/analysis/full'])
    end
    writedir(j)=cellstr([datapath,'/analysis/full']);
    files(j)=cellstr(filename)
end
for jfiles=1:nfiles
    disp(['file number: ',num2str(jfiles),' of ',num2str(nfiles)])
   fid=fopen(char(file(jfiles)));
   lidar=read_Proc_profiles(fid,0.0);
   fclose(fid);

%     nfloor=mean(lidar.SNR(:,700:end),2);
%     for j=1:length(nfloor)
%         vad.SNR(j,:)=vad.SNR(j,:)-nfloor(j)+1;
%     end
    SNR_thresh=1.005;
    [na,nr]=size(vad.RWS);
    for k=1:na
        for j=1:nr-14
            p(k,j)=std(lidar.RWS(k,j:j+13));
        end
    end
    inda=find(lidar.SNR<SNR_thresh);
    indb=find(p>2.5);
    

    numble=na*nr;
    snr=reshape(lidar.SNR,numble,1);
    rws=reshape(lidar.RWS,numble,1);
    snr(inda)=NaN;
    rws(inda)=NaN;
    snr(indb)=NaN;
    rws(indb)=NaN;
    snr=reshape(snr,na,nr);
    rws=reshape(rws,na,nr);
    for i=1:36
       vad.SNR=snr(i:i+11,:);
       vad.RWS=rws(i:i+11,:);
       vad.elevation=lidar.elevation(i:i+11,:);
       vad.azimuth=lidar.azimuth(i:i+11,:);
       vad.Beta=lidar.Beta(i:i+11,:);
       vad.xd=lidar.xd(i:i+11,:);
       vad.yd=lidar.yd(i:i+11,:);
       vad.zd=lidar.zd(i:i+11,:);
       profile=vvpanalysis_3_olda(vad);
       [year month day a b c]=datevec(lidar.start_time);
       profile.time=datenum([year month day mean(lidar.lidar_decimal_time(i:i+11)) 0 0]);
       filename=['User1_69_',datestr(profile.time,'yyyymmdd_HHMMSS'),'_profile.mat'];
    
    %textfile=fullfile(char(writedir(jfiles)),[char(files(jfiles)),'_profile.dat']);
    %prtid=print_file(textfile,profile,vad);
       textfile=fullfile(char(writedir(jfiles)),filename);
       save(textfile,'profile');
    
    end

end