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'],'dir')
        mkdir([datapath,'/analysis'])
    end
    writedir(j)=cellstr([datapath,'/analysis']);
    files(j)=cellstr(filename)
end
% for jfiles=1:nfiles
%    fid=fopen(char(file(jfiles)));
%    lidar=read_Proc_profiles(fid,0.0);
%    fclose(fid);
% 
%     nfloor=mean(lidar.SNR(:,500:end),2);
%     vad2=lidar;
%     for j=1:length(nfloor)
%         vad2.SNR(j,:)=vad2.SNR(j,:)-nfloor(j)+1;
%     end
%     SNR_thresh=1.003;
%     ip=1;
%     while ip==1
%         vad=vad2;
%         [inda,indb]=size(vad.SNR);
%         numble=inda*indb;
%         snr=reshape(vad.SNR,numble,1);
%         rws=reshape(vad.RWS,numble,1);
%         ind=find(snr<SNR_thresh | abs(rws)>15);
%         snr(ind)=NaN;
%         rws(ind)=NaN;
% 
%         snr=reshape(snr,inda,indb);
%         rws=reshape(rws,inda,indb);
%         hfig=figure('Position',[50,500,500,400]);
%         subplot(1,2,1)
%         hg1=pcolor([1:length(nfloor)],vad.ranges,snr');
%         set(hg1,'EdgeColor','none')
%         subplot(1,2,2)
%         hg2=pcolor([1:length(nfloor)],vad.ranges,rws');
%         set(hg2,'EdgeColor','none')
%         ip=input('Try another value? (Y=1,N=0)');
%         if ip==1
%             SNR_thresh=input('input SNR threshhold (>1): ');
%         end 
%         close(hfig);
%     end
%     snrthreshhold(jfiles)=SNR_thresh;
% 
% end
SNR_thresh=1.005;
for jfiles=1:nfiles
    fid=fopen(char(file(jfiles)));
    lidar=read_Proc_profiles(fid,0.0);
    fclose(fid);

    nfloor=mean(lidar.SNR(:,500:end),2);
    vad=lidar;
    for j=1:length(nfloor)
        vad.SNR(j,:)=vad.SNR(j,:)-nfloor(j)+1;
    end
    [inda,indb]=size(vad.SNR);
    numble=inda*indb;
    snr=reshape(vad.SNR,numble,1);
    rws=reshape(vad.RWS,numble,1);
    ind=find(snr<SNR_thresh | abs(rws)>10);
    snr(ind)=NaN;
    rws(ind)=NaN;
    snr=reshape(snr,inda,indb);
    rws=reshape(rws,inda,indb);
    vad.SNR=snr;
    vad.RWS=rws;

    push=rand(25,20);
    [q,IX]=sort(push,'ascend');
    yind=IX(1:15,:);
    for j=1:20
        ind=yind(:,j);
        prof(j)=vvpanalysis_3a(vad,ind);
        disp(['current run number: ',num2str(j)])
    end
    profile=prof(1);
    for j=1:20
        u(:,j)=prof(j).u;
        v(:,j)=prof(j).v;
        w(:,j)=prof(j).w;
        Vh(:,j)=prof(j).Vh;
        theta(:,j)=prof(j).theta;
        beta(:,j)=prof(j).beta;
        avgsnr(:,j)=prof(j).avgsnr;
    end
    profile.u=mean(u,2);
    profile.du=std(u');
    profile.v=mean(v,2);
    profile.dv=std(v');
    profile.w=mean(w,2);
    profile.dw=std(w');
    profile.Vh=mean(Vh,2);
    profile.dVh=std(Vh');
    profile.theta=mean(theta,2);
    profile.dtheta=std(theta');
    profile.beta=mean(beta,2);
    profile.dbeta=std(beta');
    profile.avgsnr=mean(avgsnr,2);
    profile.davgsnr=std(avgsnr');
    %profile.snr_threshhold=snrthreshhold(jfiles);
    profile.starttime=vad.start_time;
    
    ind=find((profile.turb'./profile.Vh)<0.01);
    profile.u(ind)=NaN;
    profile.du(ind)=NaN;
    profile.v(ind)=NaN;
    profile.dv(ind)=NaN;
    profile.w(ind)=NaN;
    profile.dw(ind)=NaN;
    profile.Vh(ind)=NaN;
    profile.dVh(ind)=NaN;
    profile.theta(ind)=NaN;
    profile.dtheta(ind)=NaN;
    
    textfile=fullfile(char(writedir(jfiles)),[char(files(jfiles)),'_profile.dat']);
    prtid=print_file(textfile,profile,vad);
    textfile=fullfile(char(writedir(jfiles)),[char(files(jfiles)),'_profile.mat']);
    save(textfile,'profile');
    
    

end