files=uipickfiles;
nfiles=length(files);
basedir='D:\projects\ccny-2020';
if ~isdir([basedir,'\Analysis\profiles'])
    mkdir([basedir,'\Analysis\profiles'])
end
for jfiles=1:nfiles
    jfiles
    [filedir,filenm]=fileparts(char(files(jfiles)));
    fid=fopen(char(files(jfiles)));
    lidar=read_CSM(fid,0.0);
    fclose(fid)
    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);
    bb=struct2cell(lidar.cloud);
    a=bb{1,1,:};
    heightmean=mean(a);
    %[minn ind]=min(abs(lidar.ranges-heightmean));
    %lidar.RWS_filt(:,ind:end)=NaN;
    %lidar.SNR_filt(:,ind:end)=NaN;
    %lidar.Beta_filt(:,ind:end)=NaN;
    profile=vvp_regression(lidar);
    lidar.profile=profile;
  
    uname=[savedir,'profile_',hms,'.mat']
    save(uname,'profile','lidar')
    clear vvp profile fid
end