 clear all;
% ******lidar latitude resolution setting *************

dz=0.0075;  %*********0.0075----->7.5 meter per lidar point

ray=1.998e-2;
 h=daubcqf(4); 
str='C:\d pan\Hampton Work\data\2017\July\31txt\';
str='C:\d pan\Hampton Work\data\2017\August\21txt\';
str='C:\d pan\Hampton Work\data\2019\February\13txt';
str='C:\d pan\Hampton Work\data\2019\March\05txt\';
str='C:\d pan\Hampton Work\data\2019\May\22txt\';
str='C:\d pan\Hampton Work\data\2019\April\23txt\';
str='C:\';
% ****** Balloon Data File Input *************
%str='C:\d pan\Hampton Work\data\2018\September\19txt\combine\'
%str='C:\d pan\Hampton Work\data\2019\March\06txt\';
xv=1:4000;
%cd(str);


    

[FileName,PathName] = uigetfile({'*.txt';'*.*'},'Balloon Data File Selector');
full_name = [PathName,FileName];
  
       
       ttt1=importdata(full_name); 
        %[ttt]=textread(Files(k).name,'%f');
       
     height=ttt1(:,2)/1000;
    
    mole=ttt1(:,1)./(ttt1(:,3)+273);
     
xi =  height(1):dz: height(length(height)); 
 
temw=interp1(height,ttt1(:,3)+273,xi);

m = interp1(height,mole,xi); 
%xv=1:3500;
tm=m(xv)*ray;
z=xv;
tm3=m(z)*ray;




kk=1;
PathName = uigetdir(str);

Files=dir(fullfile(PathName, '/*.txt'));


for k = 1:length(Files);
      
        %ttt=textscan(fid, '%f');
   full_name = [PathName,'\',Files(k).name];
   if Files(k).name(1)=='t'
  
        
      ttt=importdata(full_name);
          % for i=1:length(ttt1.textdata)
         % if findstr('1064',ttt1.textdata{12,i})>0
             % i5=i;
              %break;
         % end
          %end
          
          l5=ttt.data(:,6);%386
          
         bkr=mean(l5(6666:7900)); 
         l51=l5-bkr;
    [rrl5d,rrl5n,opt2] = denoise(l51(1:4096),h,1);   
    cl5r=smooth(rrl5d,100);
  
   for i=1:length(rrl5d)
     wcl5(i)=cl5r(i)*i*i*dz*dz;
     rl51(i)=l51(i)*i*i*dz*dz;
   end
   X532(kk,:)= rl51;
  
          
          l6=ttt.data(:,2);%353.35
          
         bkr=mean(l6(6666:7900)); 
         l61=l6-bkr;
         
         [rrl6d,rrl6n,opt2] = denoise(l61(1:4096),h,1); 
         cl6r=smooth(rrl6d,100);
         
       for i=1:length(rrl6d)
     wcl6(i)=cl6r(i)*i*i*dz*dz;
     rl61(i)=l61(i)*i*i*dz*dz;
       end    
  
   cl5=smooth(rl51,20)';
    cl6=smooth(rl61,20)';
    Tc=(cl6(1:4000)./tm3(1:4000).*exp(cumsum(((355/353.5)^4+1)*tm3(1:4000))*dz));
    Tc1=(cl5(1:4000)./tm3(1:4000).*exp(cumsum(((355/386)^4+1)*tm3(1:4000))*dz));
    Tc2=Tc1.^(2/(1+355/386));
    Tc3=Tc2;
    Tc3(1001:length(Tc3))=Tc3(1001);
   
 
   ntem=469./(log(abs(cl5)./abs(cl6))+2.35);
   mtem(kk,:)=ntem;
  
   gff=temw(1:600)./mtem(1,1:600);
   ntem1=ntem;
   ntem1(1:600)=ntem(1:600).*gff(1:600);
   cmtem(kk,:)=smooth(ntem1,30);
   
   time(kk)=sscanf(Files(k).name(5:6), '%f')+(sscanf(Files(k).name(7:8), '%f')+ sscanf(Files(k).name(10:11), '%f')/60)/24;
  nff=char(Files(k).name(2:13));
 name1(kk,:)=nff;
 kk=kk+1;
   end
end





 
mt1=cmtem;
for k = 3:kk-3
    mt1(k,:)=mean(cmtem(k-2:k+2,:));
end



alt_new = 0.0075:0.0075:length(mt1)*0.0075;
figure
imagesc(time,alt_new,abs(mt1'));
set(gca,'YDir','normal');
caxis([200 310]);
colormap([0 0 0;jet(100)]);
title(' The relative ozone density ','FontSize',12);
xlabel(' UTC Time (July.31 2017)','FontSize',12);
ylabel('Altitude (km)','FontSize',12);
colorbar;   
