files=uipickfiles;
nfiles=length(files);
basedir='G:\projects\ccny-2020';
if ~isdir([basedir,'\Analysis\profiles'])
    mkdir([basedir,'\Analysis\profiles'])
end
for jfiles=1:nfiles
    [filedir,filenm]=fileparts(char(files(jfiles)));
    fid=fopen(char(files(jfiles)));
    lidar=read_CSM(fid,0.0);
    ym=datestr(lidar.start_time,'yyyymm');
    ymd=datestr(lidar.start_time,'yyyymmdd');
    hms=datestr(lidar.start_time,'HHMMSS');
    savedir=[basedir,'\Analysis\profiles','\',ym,'\',ymd,'\'];
    if ~isdir(savedir)
        mkdir(savedir)
    end
    [na,nr]=size(lidar.RWS);
    for j=1:nr-lidar.gatelengthinpts
        nn(:,j)=estimatenoise(lidar.RWS(:,j:j+lidar.gatelengthinpts-1),2);
    end
    vad=lidar;
    deln=2*mean(nn(:,10:60),2);
    for ja=1:na
        ind=find(nn(ja,:)>deln(ja));
        vad.RWS(ja,ind)=NaN;
        vad.SNR(ja,ind)=NaN;
    end
    ind=find(vad.SNR<1.008);
    vad.RWS(ind)=NaN;
    vad.SNR(ind)=NaN;
    profile=vvp_regression(vad);
    lidar.profile=profile;
  
    uname=[savedir,'profile_',hms,'.mat']
    save(uname,'profile','lidar')
    clear vvp profile
end