












      subroutine lmd_bkpp_tile (Istr,Iend,Jstr,Jend, Kv,Kt,Ks,
     &                     Gm1,dGm1dS, Gt1,dGt1dS, Gs1,dGs1dS,
     &                        wm,ws, my_hbbl,my_kbbl,wrk, Rib)      


      use Agrif_Util
      integer(4), parameter :: N = 32
      interface
        subroutine Sub_Loop_lmd_bkpp_tile(Istr,Iend,Jstr,Jend,Kv,Kt,Ks,G
     &m1,dGm1dS,Gt1,dGt1dS,Gs1,dGs1dS,wm,ws,my_hbbl,my_kbbl,wrk,Rib,padd
     &_E,Mm,padd_X,Lm,hbls,kbbl,hbbl,rmask,pmask2,f,bvf,v,nstp,u,rho1,rh
     &o0,z_w,z_r,bvstr,bustr,ustar,NORTH_INTER,SOUTH_INTER,EAST_INTER,WE
     &ST_INTER)
          implicit none
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: qp2 = 0.0000172D0
      real, parameter :: Gslope_max = 1.D0
      real, parameter :: Rslope_max = 0.05D0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      real, parameter :: eps = 1.D-20
      real, parameter :: lmd_cs = 98.96D0
      real, parameter :: lmd_Cv = 1.8D0
      real, parameter :: Ric = 0.3D0
      real, parameter :: lmd_betaT = -0.2D0
      real, parameter :: lmd_epsilon = 0.1D0
      real, parameter :: lmd_cekman = 0.7D0
      real, parameter :: lmd_nu0c = 0.1D0
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      integer(4) :: nstp
      real :: rho0
      logical :: NORTH_INTER
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      logical :: WEST_INTER
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:2) :: hbls
      integer(4), dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: kbbl
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: hbbl
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: rmask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: pmask2
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: f
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N) :: bvf
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N,1:3) :: v
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N,1:3) :: u
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N) :: rho1
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N) :: z_w
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N) :: z_r
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: bvstr
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: bustr
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: ustar
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Kv
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Kt
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Ks
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gm1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGm1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gt1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGt1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gs1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGs1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: wm
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: ws
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: my_hbbl
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: my_kbbl
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: wrk
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,1:2) :: Rib
        end subroutine Sub_Loop_lmd_bkpp_tile

      end interface
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Kv
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Kt
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Ks
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gm1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGm1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gt1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGt1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gs1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGs1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: wm
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: ws
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: my_hbbl
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: my_kbbl
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: wrk
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,1:2) :: Rib
      

        call Sub_Loop_lmd_bkpp_tile(Istr,Iend,Jstr,Jend,Kv,Kt,Ks,Gm1,dGm
     &1dS,Gt1,dGt1dS,Gs1,dGs1dS,wm,ws,my_hbbl,my_kbbl,wrk,Rib, Agrif_tab
     &vars_i(14)%iarray0, Agrif_tabvars_i(4)%iarray0, Agrif_tabvars_i(13
     &)%iarray0, Agrif_tabvars_i(2)%iarray0, Agrif_tabvars(119)%array3, 
     &Agrif_tabvars_i(243)%iarray2, Agrif_tabvars(120)%array2, Agrif_tab
     &vars(81)%array2, Agrif_tabvars(77)%array2, Agrif_tabvars(113)%arra
     &y2, Agrif_tabvars(122)%array3, Agrif_tabvars(149)%array4, Agrif_ta
     &bvars_i(153)%iarray0, Agrif_tabvars(150)%array4, Agrif_tabvars(140
     &)%array3, Agrif_tabvars_r(12)%array0, Agrif_tabvars(146)%array3, A
     &grif_tabvars(142)%array3, Agrif_tabvars(257)%array2, Agrif_tabvars
     &(258)%array2, Agrif_tabvars(121)%array2, Agrif_tabvars_l(13)%larra
     &y0, Agrif_tabvars_l(14)%larray0, Agrif_tabvars_l(11)%larray0, Agri
     &f_tabvars_l(12)%larray0)

      end


      subroutine Sub_Loop_lmd_bkpp_tile(Istr,Iend,Jstr,Jend,Kv,Kt,Ks,Gm1
     &,dGm1dS,Gt1,dGt1dS,Gs1,dGs1dS,wm,ws,my_hbbl,my_kbbl,wrk,Rib,padd_E
     &,Mm,padd_X,Lm,hbls,kbbl,hbbl,rmask,pmask2,f,bvf,v,nstp,u,rho1,rho0
     &,z_w,z_r,bvstr,bustr,ustar,NORTH_INTER,SOUTH_INTER,EAST_INTER,WEST
     &_INTER)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: qp2 = 0.0000172D0
      real, parameter :: Gslope_max = 1.D0
      real, parameter :: Rslope_max = 0.05D0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      real, parameter :: eps = 1.D-20
      real, parameter :: lmd_cs = 98.96D0
      real, parameter :: lmd_Cv = 1.8D0
      real, parameter :: Ric = 0.3D0
      real, parameter :: lmd_betaT = -0.2D0
      real, parameter :: lmd_epsilon = 0.1D0
      real, parameter :: lmd_cekman = 0.7D0
      real, parameter :: lmd_nu0c = 0.1D0
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      integer(4) :: nstp
      real :: rho0
      logical :: NORTH_INTER
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      logical :: WEST_INTER
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:2) :: hbls
      integer(4), dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: kbbl
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: hbbl
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: rmask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: pmask2
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: f
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N) :: bvf
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N,1:3) :: v
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N,1:3) :: u
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N) :: rho1
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N) :: z_w
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:N) :: z_r
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: bvstr
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: bustr
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: ustar
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Kv
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Kt
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,0:N) :: Ks
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gm1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGm1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gt1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGt1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Gs1
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: dGs1dS
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: wm
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: ws
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: my_hbbl
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: my_kbbl
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: wrk
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2,1:2) :: Rib

                   

                                            
                             
                                            

                                                             
                                                            
                                           
                                                               
                                    
                                      
                                                          

                       

                                    

                        
                              

                                          
                                             
                          

                        

                                          
                        

                                                        
                      
                              

                                       
                                                                     
                                          
                         

                             

                             

                            

                            

                                             

                            

                                     

                                                               

                         

                                                      
                                                      
                                                                  
                               

                               

                              

                              

                               

                              

                              

                                

                             

                                            
                                               
                                            
                                               
                                                                 
                                                 
                                 
                                               
                                               
                                               
                                               
                                               
                                               
                                             
                                             
                                             
                                             
                                             
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                              
                                                  
                                                  
                                                  
                                                  
                                                 
                                                 
                                               
                                               
                                                      
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                           
                                                           
                                                           
                                    
                                                
                                                
                                                
                                                
                                                 
                           
                           
                           
                           
                             
                                              
                          
                                                
                                                
                                                   
                                              
                                               
                                                   
                                                
                                                  
                                                 
                                                 
                                                              
                             
                                                 
                                                  
                                                 
                                                
                                             
                                                
                            
               
                                 

                                                
                                                
                                                     
                                                   
                                                   
                                                         
                                                
                                   
                                                    
                                              
                                     
                                                
                                              
                                            
                                                           
                                                     
                           
                                  
                                                
                                                
                                                     
                                                   
                                                   
                                                         
                                                           
                                                         
                                                              
                                                           
                                                                
                                                                   
                                                                    
                                                                   
                                                   
                                 
                                                    
                                      
                                                    
                                      
                                                    
                                      
                                                    
                                      
                                                      
                                   
                                        
                                        
                                                       
                                                     
                                             
                                                               
                                              
                                                           
                                        
                                                   
                                 
                                               
                                               
                                                
                                                
                                                
                                               
                                               
                                               
                              
                              
                                
                                
                                
                              
                              
                              
                                                  
                                                  
                                                   
                                                   
                                                   
                                                  
                                                  
                                                  
                                  
                                  
                                    
                                    
                                    
                               
                               
                                  
                                                              
                                
                                       
                                                           
                                                                
                                             
                                                  
                                                     
                           
                                                                       
                                                                       
                                                                 
                             
                                                                    
                                       
                                                              
                                                             
                                                                       
                                                                 
                                                      
                                        
                                                
                                 
                                                   
                                   
                                 
                                
                                           
                                         
                                             
                                                              
                                              
                                                                       
                                                  
                                                  
                                                         
                                                         
                                                             
                                                   
                                                   
                                                       
                                                   
                                               
                                 
                                                       
                                                   
                                               
                                 
                                                     
                                                     
                                       
                                       
                                                 
                                                 
                                                   
                               
                               
                               
                                 
                                                    

                        
                   
                                      
                                      
                                                  
                                                    
                                             
                                                  
                              
                                                
                                  
                                                   
                                                    
                                               
                               
                                 
                                 
                                                 
                                
                                                    
                                  
                                                                  
                                                                       
                                                                 
                                 
                                
                                                                       
                                                                 
                                                                 
                                                                 
                                                        
                                                                       
                                                                 
                                                                 
            
                                              
                                                   
                     
                              
                               
                                 
                                                  
                                                                       
                                                                 
                                                                 
                        
                      
                           
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                    
                           
                                      
                             
                                                 
                                                                       
                                                                 
                     
                                                      
                                                                       
                                                                 
              
                                          
                                                                       
                                                                 
       
                                                           
      real(8) :: Cu_Nbq_X
      real(8) :: Cu_Nbq_Y
      real(8) :: Cu_Nbq_Z
                                             
                                                                       
                                                                 
              
                                                  
                                                                       
                                                                 
              
                                  
                                           
                                                  
                                                                   
                                                               
                                                      
                                                                       
                                                                 
                      
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                        
                                
                                                                       
                                                                 
                                                        

                                                                       
                                                                 
                  
                                                                       
                                                                 
                                                                 
                                                                 
                  

                          

              
                             

                  
                               

                 
                                

                       
                                   

                                                                       
                                                                 
                    
      integer(4) :: i
      integer(4) :: j
      integer(4) :: k
      integer(4) :: ka
      integer(4) :: ku
      integer(4) :: ksave
      integer(4) :: imin
      integer(4) :: imax
      integer(4) :: jmin
      integer(4) :: jmax
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                           
                                                                       
                                                                 
                                                                 
                                                                 
                                                    
      real :: Vtc
      real :: hekman
      real :: dVsq
      real :: Vtsq
      real :: sig
      real :: Kv_bl
      real :: Kt_bl
      real :: Ks_bl
      real :: cff
      real :: lmd_a1
      real :: dKv_bl
      real :: dKt_bl
      real :: dKs_bl
      real :: cff_up
      real :: lmd_a2
      real :: Gm
      real :: Gt
      real :: Gs
      real :: cff_dn
      real :: Ritop
      real :: lmd_a3
      real :: zbl
      real :: zsbl
                            

                                                                       
                                                                 
                                           
      real :: Kv0
      real :: Kt0
      real :: Ks0
                                                                       
                                                                 
                                                                 
                                                                 
                                                         

      if (.not.WEST_INTER) then
        imin=istr
      else
        imin=istr-1
      endif
      if (.not.EAST_INTER) then
        imax=iend
      else
        imax=iend+1
      endif
      if (.not.SOUTH_INTER) then
        jmin=jstr
      else
        jmin=jstr-1
      endif
      if (.not.NORTH_INTER) then
        jmax=jend
      else
        jmax=jend+1
      endif
      Vtc=lmd_Cv*sqrt(-lmd_betaT)/( sqrt(lmd_cs*lmd_epsilon)
     &                                        *Ric*vonKar*vonKar )
      do j=jmin,jmax
        do i=imin,imax
          ustar(i,j)=sqrt(sqrt( (0.5D0*(bustr(i,j)+bustr(i+1,j)))**2
     &                         +(0.5D0*(bvstr(i,j)+bvstr(i,j+1)))**2))
        enddo
      enddo
      do j=jmin,jmax
        do i=imin,imax
          wm(i,j)=vonKar*ustar(i,j)
          ws(i,j)=wm(i,j)
        enddo
      enddo
      ka=1
      ku=2
      do j=jmin,jmax
        do i=imin,imax
          my_hbbl(i,j)=z_r(i,j,N)-z_w(i,j,0)
          my_kbbl(i,j)=N
          Rib(i,j,ku)=0.D0
        enddo
      enddo
      do k=2,N
        cff=g/rho0
        do j=jmin,jmax
          do i=imin,imax
            Ritop=-cff*(rho1(i,j,k)-rho1(i,j,1))
     &                           *(z_r(i,j,k)-z_r(i,j,1))
            dVsq=0.25D0*( (u(i  ,j,k,nstp)-u(i  ,j,1,nstp)+
     &                   u(i+1,j,k,nstp)-u(i+1,j,1,nstp))**2
     &                 +(v(i,j  ,k,nstp)-v(i,j  ,1,nstp)+
     &                   v(i,j+1,k,nstp)-v(i,j+1,1,nstp))**2)
            Vtsq=Vtc*(z_r(i,j,k)-z_r(i,j,1))*ws(i,j)
     &          *sqrt(max(0.D0,0.5D0*(bvf(i,j,k)+bvf(i,j,k-1))))
            Rib(i,j,ka)=Ritop/(dVsq+Vtsq+eps)
          enddo
        enddo
        do j=jmin,jmax
          do i=imin,imax
            if (my_kbbl(i,j).eq.N .and. Rib(i,j,ka).gt.Ric) then
              zbl=z_r(i,j,k)-(z_r(i,j,k)-z_r(i,j,k-1))*
     &           (Ric-Rib(i,j,ka))/(Rib(i,j,ku)-Rib(i,j,ka))
              my_hbbl(i,j)=zbl-z_w(i,j,0)
              my_kbbl(i,j)=k
            endif
          enddo
        enddo
        ksave=ka
        ka=ku
        ku=ksave
      enddo
      do j=jmin,jmax
        do i=imin,imax
          hekman=lmd_cekman*ustar(i,j)/max(abs(f(i,j)),1.D-6)
          my_hbbl(i,j)=min(my_hbbl(i,j),hekman)
        enddo
      enddo
      if (.not.WEST_INTER) then
        do j=jmin,jmax
          my_hbbl(Istr-1,j)=my_hbbl(Istr,j)
        enddo
      endif
      if (.not.EAST_INTER) then
        do j=jmin,jmax
          my_hbbl(Iend+1,j)=my_hbbl(Iend,j)
        enddo
      endif
      if (.not.SOUTH_INTER) then
        do i=imin,imax
          my_hbbl(i,Jstr-1)=my_hbbl(i,Jstr)
        enddo
      endif
      if (.not.NORTH_INTER) then
        do i=imin,imax
          my_hbbl(i,Jend+1)=my_hbbl(i,Jend)
        enddo
      endif
      if (.not.WEST_INTER.and..not.SOUTH_INTER) then
        my_hbbl(Istr-1,Jstr-1)=my_hbbl(Istr,Jstr)
      endif
      if (.not.WEST_INTER.and..not.NORTH_INTER) then
        my_hbbl(Istr-1,Jend+1)=my_hbbl(Istr,Jend)
      endif
      if (.not.EAST_INTER.and..not.SOUTH_INTER) then
        my_hbbl(Iend+1,Jstr-1)=my_hbbl(Iend,Jstr)
      endif
      if (.not.EAST_INTER.and..not.NORTH_INTER) then
        my_hbbl(Iend+1,Jend+1)=my_hbbl(Iend,Jend)
      endif
      do j=Jstr,Jend+1
        do i=Istr,Iend+1
          wrk(i,j)=0.25D0*(my_hbbl(i,j)  +my_hbbl(i-1,j)
     &                  +my_hbbl(i,j-1)+my_hbbl(i-1,j-1))
     &                   *pmask2(i,j)
        enddo
      enddo
      do j=Jstr,Jend
        do i=Istr,Iend
          cff=0.25D0*(pmask2(i,j)   +pmask2(i+1,j)
     &             +pmask2(i,j+1) +pmask2(i+1,j+1))
          my_hbbl(i,j)=(1.D0-cff)*my_hbbl(i,j)+
     &              0.25D0*(wrk(i,j)  +wrk(i+1,j)
     &                   +wrk(i,j+1)+wrk(i+1,j+1))
          my_hbbl(i,j)=my_hbbl(i,j)*rmask(i,j)
        enddo
      enddo
      do j=Jstr,Jend
        do i=Istr,Iend
          hbbl(i,j)=min(my_hbbl(i,j),z_w(i,j,N)-z_w(i,j,0))
     &                                          *rmask(i,j)
          kbbl(i,j)=N
        enddo
      enddo
      do k=N,1,-1
        do j=Jstr,Jend
          do i=Istr,Iend
            if (z_r(i,j,k)-z_w(i,j,0).gt.hbbl(i,j)) then
              kbbl(i,j)=k
            endif
          enddo
        enddo
      enddo
      do j=Jstr,Jend
        do i=Istr,Iend
          zbl=z_w(i,j,0)+hbbl(i,j)
          k=kbbl(i,j)
          if (zbl.lt.z_w(i,j,k-1)) k=k-1
          cff=1.D0/(z_w(i,j,k)-z_w(i,j,k-1))
          cff_up=cff*(zbl-z_w(i,j,k-1))
          cff_dn=cff*(z_w(i,j,k)-zbl)
          Kv_bl=cff_up*Kv(i,j,k)+cff_dn*Kv(i,j,k-1)
          dKv_bl=-cff*(Kv(i,j,k)-Kv(i,j,k-1))
          Gm1(i,j)=Kv_bl/(hbbl(i,j)*wm(i,j)+eps)
          dGm1dS(i,j)=min(0.D0,dKv_bl/(wm(i,j)+eps))
          Kt_bl=cff_up*Kt(i,j,k)+cff_dn*Kt(i,j,k-1)
          dKt_bl=-cff*(Kt(i,j,k)-Kt(i,j,k-1))
          Gt1(i,j)=Kt_bl/(hbbl(i,j)*ws(i,j)+eps)
          dGt1dS(i,j)=min(0.D0,dKt_bl/(ws(i,j)+eps))
          Ks_bl=cff_up*Ks(i,j,k)+cff_dn*Ks(i,j,k-1)
          dKs_bl=-cff*(Ks(i,j,k)-Ks(i,j,k-1))
          Gs1(i,j)=Ks_bl/(hbbl(i,j)*ws(i,j)+eps)
          dGs1dS(i,j)=min(0.D0,dKs_bl/(ws(i,j)+eps))
        enddo
      enddo
      do k=1,N-1
        do j=Jstr,Jend
          do i=Istr,Iend
            if (k.lt.kbbl(i,j)) then
              sig=min((z_w(i,j,k)-z_w(i,j,0))/(hbbl(i,j)+eps),1.D0)
              sig=sig*rmask(i,j)
              lmd_a1=sig-2.D0
              lmd_a2=3.D0-2.D0*sig
              lmd_a3=sig-1.D0
              Gm=lmd_a1+lmd_a2*Gm1(i,j)+lmd_a3*dGm1dS(i,j)
              Gt=lmd_a1+lmd_a2*Gt1(i,j)+lmd_a3*dGt1dS(i,j)
              Gs=lmd_a1+lmd_a2*Gs1(i,j)+lmd_a3*dGs1dS(i,j)
              Kv0=hbbl(i,j)*wm(i,j)*sig*(1.D0+sig*Gm)
              Kt0=hbbl(i,j)*ws(i,j)*sig*(1.D0+sig*Gt)
              Ks0=hbbl(i,j)*ws(i,j)*sig*(1.D0+sig*Gs)
              zsbl=z_w(i,j,N)-hbls(i,j,3-nstp)
              if (z_w(i,j,k).gt.zsbl) then
                Kv0=max(Kv0,Kv(i,j,k))
                Kt0=max(Kt0,Kt(i,j,k))
                Ks0=max(Ks0,Ks(i,j,k))
              endif
              Kv(i,j,k)=Kv0
              Kt(i,j,k)=Kt0
              Ks(i,j,k)=Ks0
            else
              if (bvf(i,j,k).lt.0.D0) then
             zsbl=z_w(i,j,N)-hbls(i,j,3-nstp)
                if (z_w(i,j,k).lt.zsbl) then
                  Kv(i,j,k)=Kv(i,j,k)+lmd_nu0c
                  Kt(i,j,k)=Kt(i,j,k)+lmd_nu0c
                  Ks(i,j,k)=Ks(i,j,k)+lmd_nu0c
                endif
              endif
            endif
          enddo
        enddo
      enddo
      if (.not.WEST_INTER) then
        do j=jstr,jend
          hbbl(istr-1,j)=hbbl(istr,j)
        enddo
      endif
      if (.not.EAST_INTER) then
        do j=jstr,jend
          hbbl(iend+1,j)=hbbl(iend,j)
        enddo
      endif
      if (.not.SOUTH_INTER) then
        do i=istr,iend
          hbbl(i,jstr-1)=hbbl(i,jstr)
        enddo
      endif
      if (.not.NORTH_INTER) then
        do i=istr,iend
          hbbl(i,jend+1)=hbbl(i,jend)
        enddo
      endif
      if (.not.WEST_INTER .and. .not.SOUTH_INTER) then
        hbbl(istr-1,jstr-1)=hbbl(istr,jstr)
      endif
      if (.not.WEST_INTER .and. .not.NORTH_INTER) then
        hbbl(istr-1,jend+1)=hbbl(istr,jend)
      endif
      if (.not.EAST_INTER .and. .not.SOUTH_INTER) then
        hbbl(iend+1,jstr-1)=hbbl(iend,jstr)
      endif
      if (.not.EAST_INTER .and. .not.NORTH_INTER) then
        hbbl(iend+1,jend+1)=hbbl(iend,jend)
      endif
      call exchange_r2d_tile (Istr,Iend,Jstr,Jend,hbbl(-1,-1))
      return
       
      

      end subroutine Sub_Loop_lmd_bkpp_tile

