file=uipickfiles('FilterSpec','c:/lidar/C17FSR/0813-70/*.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);
    [naz,nr]=size(lidar.RWS);
    nn=naz-1;
    [na,nr]=size(lidar.azimuth);
    rws=lidar.RWS(1:nn,:);
    snr=lidar.SNR(1:nn,:);
    beta=lidar.Beta(1:nn,:);
    xd=lidar.xd(1:nn,:);
    yd=lidar.yd(1:nn,:);
    zd=lidar.zd(1:nn,:);
    azimuth=lidar.azimuth(1:nn,:);
    elevation=lidar.elevation(1:nn,:);
    lidar_decimal_time=lidar.lidar_decimal_time(1:nn);
    [sd sm sd sH sM sS]=datevec(lidar.start_time);
    for i=1:na-1;
        beamtime(i,1)=datenum([sd sm sd lidar.lidar_decimal_time(i) 0 0]);
    end
    ranges=lidar.ranges;
    [na,nr]=size(rws);
    vad=lidar;


    vad.RWS=rws;
    vad.Beta=beta;
    vad.SNR=snr;
    vad.beamtime=beamtime;
    vad.lidar_decimal_time=lidar_decimal_time;
    vad.elevation=elevation;
    vad.azimuth=azimuth;
    vad.xd=xd;
    vad.yd=yd;
    vad.zd=zd;

    SNR_thresh=1.01;
    [na,nr]=size(vad.RWS);
    for k=1:na
        for j=1:nr-5
            p(k,j)=std(vad.RWS(k,j:j+5));
        end
    end
    [indax,inday]=find(vad.SNR<SNR_thresh);
    [indbx,indby]=find(p>0.5);

    numble=na*nr;
    snr=reshape(vad.SNR,na,nr);
    rws=reshape(vad.RWS,na,nr);
    for j=1:length(indax)
        snr(indax(j),inday(j))=NaN;
        rws(indax(j),inday(j))=NaN;
    end
    for j=1:length(indbx)
        snr(indbx(j),indby(j))=NaN;
        rws(indbx(j),indby(j))=NaN;
    end
    %snr=reshape(snr,na,nr);
    %rws=reshape(rws,na,nr);
    vad2=vad;
    vad2.RWS=rws;
    vad2.SNR=snr;
    profile=vvp_regression(vad);
    [year month day a b c]=datevec(lidar.start_time);
    profile.time=datenum([year month day mean(vad.lidar_decimal_time) 0 0]);
    filename=['User1_70_',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');
    ind=find(profile.turb'./(profile.Vh+0.01)>0.7);
    Vhor=profile.Vh;
    %Vhor(ind)=NaN;
    hfig=figure('Position',[100,100 1000 640]);
    set(hfig,'Name',['Wind Conditions ',datestr(vad.start_time),' UTC'],'NumberTitle','off')
    subplot(1,4,1)
    plot(1.94384*profile.Vh,3.2808*profile.heights','linewidth',3)
    grid on
    haxe1=gca;
    set(haxe1,'fontsize',12,'fontweight','b')
    xlabel('Horizontal Wind (knt)','fontsize',14,'fontweight','b')
    ylabel('Height AGL (ft)','fontsize',14,'fontweight','b')
    axis(haxe1,[0 40 0 5000])
    set(haxe1,'XTick',[0 10 20 30 40])
    subplot(1,4,2)
    scatter(profile.theta,3.2808*profile.heights,'linewidth',3)
    grid on
    haxe2=gca;
    set(haxe2,'fontsize',12,'fontweight','b')
    xlabel('Direction','fontsize',14,'fontweight','b')
    axis([0 360 0 5000])
    set(haxe2,'XTick',[0 90 180 270 360])
    subplot(1,4,3)
    plot(1.94384*profile.w,3.2808*profile.heights,'linewidth',3)
    grid on
    haxe3=gca;
    set(haxe3,'fontsize',12,'fontweight','b')
    xlabel('Vertical Wind (knt)','fontsize',14,'fontweight','b')
    axis([-10 10 0 5000])
    set(haxe3,'XTick',[-10 -5 0 5 10])
    text(-20,5300,['Wind Conditions ',datestr(vad.start_time),' UTC'],'fontsize',14,'fontweight','b')
    subplot(1,4,4)
    plot(1.94384*profile.turb,3.2808*profile.heights,'linewidth',3)
    grid on
    haxe4=gca;
    set(haxe4,'fontsize',12,'fontweight','b')
    xlabel('Variance (knt)','fontsize',14,'fontweight','b')
    axis([0 10 0 5000])
    set(haxe4,'XTick',[0 5 10])
    filename=['User1_70_',datestr(profile.time,'yyyymmdd_HHMMSS'),'_profile.png'];

    textfile=fullfile(char(writedir(jfiles)),filename);
    saveas(hfig,textfile)
    pause(1)
    close(hfig)
end
