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

dz=0.0075;  %*********0.0075----->7.5 meter per lidar point
                  %*********0.015----->15 meter per lidar point
  %h=daubcqf(4);    
 % bk1=1.76;
%bk2=1.76;

% ****** balloon Data File Input *************
str='E:\d pan\Hampton Work\data\2008\';
str='E:\d pan\Hampton Work\data\2014\May\';
str='C:\d pan\Hampton Work\data\2017\August\21txt\';
str='C:\';
%str='C:\d pan\Hampton Work\data\2019\April\23txt\';
cd(str)

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

cc = interp1(height,wv,xi); 


 

% ****** Dir Input *************
PathName = uigetdir(str);

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


 vch=25;
kk=1;
for k = 1:length(Files);
      
        %ttt=textscan(fid, '%f');
   full_name = [PathName,'\',Files(k).name];
   if Files(k).name(1)=='t'
    % [rt1,rt2,rt3]=ldatachoose(full_name,'386.000','407.000');
     ttt=importdata(full_name);
      l5=ttt.data(:,5);%386
          
         bkr=mean(l5(6666:7900)); 
         l51=l5-bkr;
    
sl2(kk,:)=smooth(l51,20);%rl5d;

 l6=ttt.data(:,7);%407
          
         bkr=mean(l6(6666:7900)); 
         l61=l6-bkr;
sl1(kk,:)=smooth(l61,20);%rl5d;
    
     
 %time(kk)=sscanf(Files(k).name(7:8), '%f')+ sscanf(Files(k).name(10:11), '%f')/60;
  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;
  time1=(sscanf(Files(k).name(7:8), '%f')+ sscanf(Files(k).name(10:11), '%f')/60)/24;
  name1(kk)=sscanf(Files(k).name(2:11), '%f');
 
 if(abs(time1-0.833)<vch)
     vch=abs(time1-0.833);
       vk=kk;
 end
    kk=kk+1;
   end
end
     






calcon=71.756;

gff=cc(1:600)./(sl1(vk,1:600)./sl2(vk,1:600)*calcon);

      for k = 1:kk-1;
 for i = 1:round(15/dz);
     if(i<121)
     %rbt(k,i)=(sl1(k,i))/(sl1(vk,i))/((sl2(k,i))/(sl2(vk,i)))*cc(i);
     rbt(k,i)=gff(i)*sl1(k,i)/sl2(k,i)*calcon;
     %rbt(k,i)=sl1(k,i)/sl2(k,i)*calcon;
     end
     if(i>120)
      rbt(k,i)=sl1(k,i)/sl2(k,i)*calcon;
         end
     
 end 
      end

      
      


path=strcat(PathName,'\Wvresult');
file_type = '.txt';
mkdir(path);
for k = 1:kk-1;
save_name =strcat(path,'\Wv',num2str(name1(k)),file_type);
savefp = fopen(save_name,'w');
for i=1:round(12/dz);
fprintf(savefp,'%f  %f\r\n',i*dz,rbt(k,i));  
end
fclose(savefp);
end

for i=1:round(12/dz);
x(i)= i*dz; 
end

rbt1=rbt;
%for k = 3:kk-4
   % rbt1(k,:)=mean(rbt(k-2:k+2,:));
%end

alt_new = 0.0075:0.0075:0.0075*2000;
figure
imagesc(time,alt_new,rbt');
set(gca,'YDir','normal');
caxis([0 8]);
colormap([0 0 0;jet(100)]);
title(' tem','FontSize',12);
xlabel('Time','FontSize',12);
ylabel('Altitude (km)','FontSize',12);
colorbar;  

