 clear all;
 
 % calibration procedure with Balloon Sonde
 
 file_ball=uipickfiles('FilterSpec','*.txt','Prompt','Balloon Sonde File Selector');
 sonde=read_sonde(file_ball);
 nsondes=length(sonde);
 
 
 % select Raman Data from Big Lidar
 
 file_raman=uipickfiles('FilterSpec','*.txt','Prompt','HU Big Raman File Selector');
 niles=length(file_raman);
 ts=[sonde.time];
 
 
 % determine time stamps
 for j=1:niles
     s=file_raman(j);
     yy=str2num(s(2:3));
     mm=str2num(s(4:4));
     dd=str2num(s(5:6));
     hh=str2num(s(7:15));
     timestamp(j)=datenum([yy,mm,dd,hh+4,0,0]);
 end
 
 [C,is]=min(abs(sonde.time-timestamp(1)));
 kb=1.38064852e-23;
 calib_s=sonde(is);
 idx=find(calib.PRESS>0);
 calib_TEMP=calib_s.TEMP(idx)+276; % convert temperature from Celsius to Kelvin
 calib_PRESS=calib_s.PRESS(idx)*100; % convert pressure from millibars to Pa
 calib_Height=calib_s.GPH(idx);
 calib_mol_dens=calib_PRESS./kb*calib_TEMP; % units are number of molecules/m^3
 
 clear sonde calib_s
 %ray=1.998e-2;
 

for k = 1:niles
              
    lidar=read_HUBIG(file_raman(k));
    nr=lidar.ch(6).nrange;
    l5=lidar.ch(6).data(1:nr);          
    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;   
