function [h,t,p,ws,wd,u,v,i,k,FT2]=TROPOPAUSE(T,Z,P,WS,WD,U,V)
% ; Purpose:
% ;		This is a function to calculate tropopause height using the WMO
% ;		thermal tropopause definition given a temperature, geopotential
% ;		height and pressure, with referencs of Cameron R. Homeyer IDL tropopause code
% ; Inputs:
% ;		T      : Given temperature column. Expected in k
% ;		Z      : Given height column.      Expected in meters.
% ;		P      : Given pressure column.    Expected in hPa. 
% :             WS     : Given wind speed column.  Expected in m/s. 
% :             WD     : Given wind direction column. Expected in degree. 
% :             U      : Given zonal wind column.  Expected in m/s. 
% :             U      : Given zonal wind column.  Expected in m/s. 

% ; Output:
% ;		h  : Tropopause height in meters.
% ;		t  : Tropopause temperature in k .
% :             p  : Tropopause pressure 
% :             ...........................
% :             ...........................
% :             ...........................
% ;             i: row of tropopause index
% ;             k: start row for next tropopause judgement
% ;             FT2: logical value whether to compute next tropopause,0 means not compute the next tropopause,

%  Author and history:
% 		Xuelong Chen  2010-4-20.


FT2=0;  % intial the next tropopause existing logical value, 
h=NaN;t=NaN;p=NaN;i=NaN;k=NaN;
ws=NaN;wd=NaN;u=NaN;v=NaN;

for i=1:length(T)-100   
      if (T(i)-T(i+1))/20 <=0.002                    %judge start condition to compute tropopause 
           lapse=NaN;
           for s=1:100                               %compute lapse rate between start layer and each layer above it in 2km                            
               lapse(s)=(T(i)-T(i+s))/(20*s);
           end 
           rr=lapse>0.002;                           %numb of lapse rate>0.002
           if sum(rr)==0                             %nanmean(lapse) <=0.002
                                                     %output tropopause pressure, height, wind etc information
              h=Z(i);  
              t=T(i);p=P(i);
              ws=WS(i); u=U(i);v=V(i);
              wd=WD(i);
                                                     %judge the condition for next tropopause
              for k=i:length(T)-50
                  lapse=NaN;
                  for s=1:50                         %vertical resolution is 20m,so compute 1km lapse rate (1:50)                      
                     lapse(s)=(T(k)-T(s+k))/(20*s);  %lapse rate above 1km
                  end
                  rr=lapse>0.003;                    %numb of lapse rate>0.003
                  if  sum(rr)==50                    %all lapse rate >0.003               
                      FT2=1;                         %true condition to compute the next tropopause
                      break;
                  end
              end             
              break;
           end
       end
end
    