      subroutine bulk_flux (tile)      


      use Agrif_Util
#include "Param_BeforeCall_bulk_flux.h"
      integer(4) :: tile
      

        call Sub_Loop_bulk_flux(tile, Agrif_tabvars_i(15)%iarray0, Agrif
     &_tabvars(117)%array3, Agrif_tabvars_i(8)%iarray0, Agrif_tabvars_i(
     &7)%iarray0, Agrif_tabvars_i(16)%iarray0)

      end


#include "Sub_Loop_bulk_flux.h"
      subroutine bulk_flux_tile (Istr,Iend,Jstr,Jend, aer,cer)      


      use Agrif_Util
#include "Param_BeforeCall_bulk_flux_tile.h"
      INTEGER(4) :: Istr
      INTEGER(4) :: Iend
      INTEGER(4) :: Jstr
      INTEGER(4) :: Jend
      REAL, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: aer
      REAL, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: cer
      

        call Sub_Loop_bulk_flux_tile(Istr,Iend,Jstr,Jend,aer,cer, Agrif_
     &tabvars_i(14)%iarray0, Agrif_tabvars_i(4)%iarray0, Agrif_tabvars_i
     &(13)%iarray0, Agrif_tabvars_i(2)%iarray0, Agrif_tabvars(78)%array2
     &, Agrif_tabvars(232)%array2, Agrif_tabvars(264)%array2, Agrif_tabv
     &ars(79)%array2, Agrif_tabvars(233)%array2, Agrif_tabvars(265)%arra
     &y2, Agrif_tabvars(248)%array2, Agrif_tabvars(246)%array2, Agrif_ta
     &bvars(247)%array2, Agrif_tabvars(249)%array2, Agrif_tabvars(81)%ar
     &ray2, Agrif_tabvars(237)%array2, Agrif_tabvars(215)%array2, Agrif_
     &tabvars(250)%array3, Agrif_tabvars(236)%array2, Agrif_tabvars(238)
     &%array2, Agrif_tabvars_i(154)%iarray0, Agrif_tabvars(148)%array5, 
     &Agrif_tabvars(239)%array2, Agrif_tabvars(234)%array2, Agrif_tabvar
     &s_r(12)%array0, Agrif_tabvars_l(13)%larray0, Agrif_tabvars_l(14)%l
     &array0, Agrif_tabvars_l(11)%larray0, Agrif_tabvars_l(12)%larray0)

      end


#include "Sub_Loop_bulk_flux_tile.h"
      FUNCTION spec_hum (RH,psfc,TairC)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      REAL, parameter :: CtoK = 273.16D0
      REAL, parameter :: blk_Rgas = 287.0596736665907D0
      REAL, parameter :: blk_Rvap = 461.5249933083879D0
      REAL, parameter :: blk_Cpa = 1004.708857833067D0
      REAL, parameter :: ip00 = 1.D-5
      REAL, parameter :: p00 = 1.D+5
      REAL, parameter :: rdocpd = blk_Rgas/blk_Cpa
      REAL, parameter :: cpdord = blk_Cpa/blk_Rgas
      REAL, parameter :: r_gas = 8.314510D0
      REAL, parameter :: mm_dryair = 28.9644D-3
      REAL, parameter :: mm_water = 18.0153D-3
      REAL, parameter :: cpvir = blk_Rvap/blk_Rgas-1.D0
      REAL, parameter :: MvoMa = mm_water/mm_dryair
      REAL, parameter :: eps = 1.D-8
      REAL, parameter :: r3 = 1.D0/3.D0
      REAL, parameter :: pis2 = 2.D0*ATAN(1.D0)
      REAL, parameter :: sqr3 = SQRT(3.D0)
      REAL, parameter :: pis2osqr3 = pis2/sqr3
      REAL, parameter :: LMOmin = -200.D0
      REAL, parameter :: LMOmax = 0.25D0
      REAL, parameter :: blk_beta = 1.2D0
      REAL, parameter :: blk_Zabl = 600.D0
      REAL, parameter :: dWstar0 = 1.D-6
      REAL, parameter :: dTstar0 = 1.D-6
      REAL, parameter :: dQstar0 = 1.D-9
      REAL, parameter :: emiss_lw = 0.985D0
      REAL, parameter :: SigmaSB = 5.6697D-8
      REAL, parameter :: psurf = 100000.0D0
#include "Param_BeforeCall_spec_hum.h"
      REAL :: RH
      REAL :: psfc
      REAL :: TairC
      REAL :: spec_hum
                   

                                                
                                                           
                                                           
                                                          
                                               
                                               
                                                       
                                                       
                                                 
                                                 
                                                 
                                                               
                                                         
                                            
                                                
                                                      
                                                 
                                                
                                              
                                             
                                            
                                             
                                            
                                            
                                            
                                               
                                                 
                               
                                  

                                           
      REAL :: cff
                                 
      cff=(1.0007D0+3.46D-6*0.01D0*psfc)*6.1121D0*
     &        exp(17.502D0*TairC/(240.97D0+TairC))
      IF (RH.lt.2.0D0) then
         cff=cff*RH
         spec_hum=MvoMa*(cff/(psfc*0.01D0-0.378D0*cff))
      ELSE
         spec_hum=0.001D0*RH
      ENDIF
      END FUNCTION spec_hum
      SUBROUTINE exner_patm_from_tairabs (iexn,pair,q,tairabs,z,psfc)
      use Agrif_Types, only : Agrif_tabvars

      implicit none

      REAL, parameter :: CtoK = 273.16D0
      REAL, parameter :: blk_Rgas = 287.0596736665907D0
      REAL, parameter :: blk_Rvap = 461.5249933083879D0
      REAL, parameter :: blk_Cpa = 1004.708857833067D0
      REAL, parameter :: ip00 = 1.D-5
      REAL, parameter :: p00 = 1.D+5
      REAL, parameter :: rdocpd = blk_Rgas/blk_Cpa
      REAL, parameter :: cpdord = blk_Cpa/blk_Rgas
      REAL, parameter :: r_gas = 8.314510D0
      REAL, parameter :: mm_dryair = 28.9644D-3
      REAL, parameter :: mm_water = 18.0153D-3
      REAL, parameter :: cpvir = blk_Rvap/blk_Rgas-1.D0
      REAL, parameter :: MvoMa = mm_water/mm_dryair
      REAL, parameter :: eps = 1.D-8
      REAL, parameter :: r3 = 1.D0/3.D0
      REAL, parameter :: pis2 = 2.D0*ATAN(1.D0)
      REAL, parameter :: sqr3 = SQRT(3.D0)
      REAL, parameter :: pis2osqr3 = pis2/sqr3
      REAL, parameter :: LMOmin = -200.D0
      REAL, parameter :: LMOmax = 0.25D0
      REAL, parameter :: blk_beta = 1.2D0
      REAL, parameter :: blk_Zabl = 600.D0
      REAL, parameter :: dWstar0 = 1.D-6
      REAL, parameter :: dTstar0 = 1.D-6
      REAL, parameter :: dQstar0 = 1.D-9
      REAL, parameter :: emiss_lw = 0.985D0
      REAL, parameter :: SigmaSB = 5.6697D-8
      REAL, parameter :: psurf = 100000.0D0
      REAL, parameter :: g = 9.80665D0
      INTEGER(4), parameter :: Niter = 3
#include "Param_BeforeCall_exner_patm_from_tairabs.h"
      REAL, intent(out) :: iexn
      REAL, intent(out) :: pair
      REAL, intent(in) :: q
      REAL, intent(in) :: tairabs
      REAL, intent(in) :: z
      REAL, intent(in) :: psfc

                   

                                                
                                                           
                                                           
                                                          
                                               
                                               
                                                       
                                                       
                                                 
                                                 
                                                 
                                                               
                                                         
                                            
                                                
                                                      
                                                 
                                                
                                              
                                             
                                            
                                             
                                            
                                            
                                            
                                               
                                                 
                               
                                  

                                        
                                                  
                                            
      REAL :: xm
      REAL :: q_sat
      REAL :: qsat
                                                
                                     
      INTEGER(4) :: iter
                                          
      pair = psfc
      DO Iter = 1, Niter
        q_sat = qsat(tairabs, pair, 1.D0)
        xm    =  mm_dryair + (q/q_sat) * ( mm_water - mm_dryair )
        pair  = psfc * EXP( -g * xm * z / ( r_gas * tairabs ) )
      ENDDO
      iexn =  (pair*ip00)**(-rdocpd)
      return
      END SUBROUTINE exner_patm_from_tairabs
      FUNCTION qsat (TairK, patm, coeff)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      REAL, parameter :: CtoK = 273.16D0
      REAL, parameter :: blk_Rgas = 287.0596736665907D0
      REAL, parameter :: blk_Rvap = 461.5249933083879D0
      REAL, parameter :: blk_Cpa = 1004.708857833067D0
      REAL, parameter :: ip00 = 1.D-5
      REAL, parameter :: p00 = 1.D+5
      REAL, parameter :: rdocpd = blk_Rgas/blk_Cpa
      REAL, parameter :: cpdord = blk_Cpa/blk_Rgas
      REAL, parameter :: r_gas = 8.314510D0
      REAL, parameter :: mm_dryair = 28.9644D-3
      REAL, parameter :: mm_water = 18.0153D-3
      REAL, parameter :: cpvir = blk_Rvap/blk_Rgas-1.D0
      REAL, parameter :: MvoMa = mm_water/mm_dryair
      REAL, parameter :: eps = 1.D-8
      REAL, parameter :: r3 = 1.D0/3.D0
      REAL, parameter :: pis2 = 2.D0*ATAN(1.D0)
      REAL, parameter :: sqr3 = SQRT(3.D0)
      REAL, parameter :: pis2osqr3 = pis2/sqr3
      REAL, parameter :: LMOmin = -200.D0
      REAL, parameter :: LMOmax = 0.25D0
      REAL, parameter :: blk_beta = 1.2D0
      REAL, parameter :: blk_Zabl = 600.D0
      REAL, parameter :: dWstar0 = 1.D-6
      REAL, parameter :: dTstar0 = 1.D-6
      REAL, parameter :: dQstar0 = 1.D-9
      REAL, parameter :: emiss_lw = 0.985D0
      REAL, parameter :: SigmaSB = 5.6697D-8
      REAL, parameter :: psurf = 100000.0D0
      REAL, parameter :: alpw = 60.2227554D0
      REAL, parameter :: betaw = 6822.40088D0
      REAL, parameter :: gamw = 5.13926744D0
      REAL, parameter :: alpi = 32.62117980819471D0
      REAL, parameter :: betai = 6295.421338904806D0
      REAL, parameter :: gami = 0.5631331575423155D0
#include "Param_BeforeCall_qsat.h"
      REAL :: TairK
      REAL :: patm
      REAL :: coeff
      REAL :: qsat
                   

                                                
                                                           
                                                           
                                                          
                                               
                                               
                                                       
                                                       
                                                 
                                                 
                                                 
                                                               
                                                         
                                            
                                                
                                                      
                                                 
                                                
                                              
                                             
                                            
                                             
                                            
                                            
                                            
                                               
                                                 
                               
                                  

                                    
                                                  
                                    
      REAL :: psat
                                                      
                                                      
                                                      
                                                             
                                                             
                                                              
      IF (TairK .LE. CtoK) then
        psat = EXP( alpi - betai/TairK - gami*LOG(TairK) )
      ELSE
        psat = EXP( alpw - betaw/TairK - gamw*LOG(TairK) )
      ENDIF
      psat = coeff * psat
      qsat = (MvoMa*psat)/(patm+(MvoMa-1.0D0)*psat)
      return
      END FUNCTION qsat
      FUNCTION air_visc(TairC)

      use Agrif_Types, only : Agrif_tabvars


      REAL, parameter :: c0 = 1.326D-5
      REAL, parameter :: c1 = 6.542D-3
      REAL, parameter :: c2 = 8.301D-6
      REAL, parameter :: c3 = 4.84D-9
#include "Param_BeforeCall_air_visc.h"
      REAL :: air_visc
                                           
      REAL :: cff
                                            
                                            
                                            
                                           
      cff      = TairC*TairC
      air_visc = c0*(1.D0+c1*TairC+c2*cff-c3*cff*TairC)
      return
      END FUNCTION air_visc
      FUNCTION bulk_psiu_coare (ZoL)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      REAL, parameter :: CtoK = 273.16D0
      REAL, parameter :: blk_Rgas = 287.0596736665907D0
      REAL, parameter :: blk_Rvap = 461.5249933083879D0
      REAL, parameter :: blk_Cpa = 1004.708857833067D0
      REAL, parameter :: ip00 = 1.D-5
      REAL, parameter :: p00 = 1.D+5
      REAL, parameter :: rdocpd = blk_Rgas/blk_Cpa
      REAL, parameter :: cpdord = blk_Cpa/blk_Rgas
      REAL, parameter :: r_gas = 8.314510D0
      REAL, parameter :: mm_dryair = 28.9644D-3
      REAL, parameter :: mm_water = 18.0153D-3
      REAL, parameter :: cpvir = blk_Rvap/blk_Rgas-1.D0
      REAL, parameter :: MvoMa = mm_water/mm_dryair
      REAL, parameter :: eps = 1.D-8
      REAL, parameter :: r3 = 1.D0/3.D0
      REAL, parameter :: pis2 = 2.D0*ATAN(1.D0)
      REAL, parameter :: sqr3 = SQRT(3.D0)
      REAL, parameter :: pis2osqr3 = pis2/sqr3
      REAL, parameter :: LMOmin = -200.D0
      REAL, parameter :: LMOmax = 0.25D0
      REAL, parameter :: blk_beta = 1.2D0
      REAL, parameter :: blk_Zabl = 600.D0
      REAL, parameter :: dWstar0 = 1.D-6
      REAL, parameter :: dTstar0 = 1.D-6
      REAL, parameter :: dQstar0 = 1.D-9
      REAL, parameter :: emiss_lw = 0.985D0
      REAL, parameter :: SigmaSB = 5.6697D-8
      REAL, parameter :: psurf = 100000.0D0
#include "Param_BeforeCall_bulk_psiu_coare.h"
      REAL :: ZoL
      REAL :: bulk_psiu_coare
                   

                                                
                                                           
                                                           
                                                          
                                               
                                               
                                                       
                                                       
                                                 
                                                 
                                                 
                                                               
                                                         
                                            
                                                
                                                      
                                                 
                                                
                                              
                                             
                                            
                                             
                                            
                                            
                                            
                                               
                                                 
                               
                                  

                                               
                                   
                                          
      REAL :: chik
      REAL :: psik
                                          
      REAL :: chic
      REAL :: psic
      IF (ZoL <= 0.0D0) then
        chik = (1.0D0-15.0D0*ZoL)**0.25D0
        psik = 2.0D0*LOG(0.5D0*(1.0D0+chik))+LOG(0.5D0*(1.0D0+chik**2))
     &                                  -2.0D0*ATAN(chik)+pis2
        chic = (1.0D0-10.15D0*ZoL)**r3
        psic  = 1.5D0*LOG(r3*(chic**2+chic+1.0D0))
     &          - sqr3*ATAN((2.0D0*chic+1.0D0)/sqr3)+2.D0*pis2osqr3
        bulk_psiu_coare=psic+(psik-psic)/(1.0D0+ZoL**2)
      ELSE
        chic=-MIN(50.0D0,0.35D0*ZoL)
        bulk_psiu_coare=-((1.0D0+ZoL)+0.6667D0*(ZoL-14.28D0)*EXP(chic)+
     &                                                          8.525D0)
      ENDIF
      return
      END FUNCTION bulk_psiu_coare
      FUNCTION bulk_psit_coare (ZoL)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      REAL, parameter :: CtoK = 273.16D0
      REAL, parameter :: blk_Rgas = 287.0596736665907D0
      REAL, parameter :: blk_Rvap = 461.5249933083879D0
      REAL, parameter :: blk_Cpa = 1004.708857833067D0
      REAL, parameter :: ip00 = 1.D-5
      REAL, parameter :: p00 = 1.D+5
      REAL, parameter :: rdocpd = blk_Rgas/blk_Cpa
      REAL, parameter :: cpdord = blk_Cpa/blk_Rgas
      REAL, parameter :: r_gas = 8.314510D0
      REAL, parameter :: mm_dryair = 28.9644D-3
      REAL, parameter :: mm_water = 18.0153D-3
      REAL, parameter :: cpvir = blk_Rvap/blk_Rgas-1.D0
      REAL, parameter :: MvoMa = mm_water/mm_dryair
      REAL, parameter :: eps = 1.D-8
      REAL, parameter :: r3 = 1.D0/3.D0
      REAL, parameter :: pis2 = 2.D0*ATAN(1.D0)
      REAL, parameter :: sqr3 = SQRT(3.D0)
      REAL, parameter :: pis2osqr3 = pis2/sqr3
      REAL, parameter :: LMOmin = -200.D0
      REAL, parameter :: LMOmax = 0.25D0
      REAL, parameter :: blk_beta = 1.2D0
      REAL, parameter :: blk_Zabl = 600.D0
      REAL, parameter :: dWstar0 = 1.D-6
      REAL, parameter :: dTstar0 = 1.D-6
      REAL, parameter :: dQstar0 = 1.D-9
      REAL, parameter :: emiss_lw = 0.985D0
      REAL, parameter :: SigmaSB = 5.6697D-8
      REAL, parameter :: psurf = 100000.0D0
#include "Param_BeforeCall_bulk_psit_coare.h"
      REAL :: ZoL
      REAL :: bulk_psit_coare
                   

                                                
                                                           
                                                           
                                                          
                                               
                                               
                                                       
                                                       
                                                 
                                                 
                                                 
                                                               
                                                         
                                            
                                                
                                                      
                                                 
                                                
                                              
                                             
                                            
                                             
                                            
                                            
                                            
                                               
                                                 
                               
                                  

                                               
                                   
                                          
      REAL :: chik
      REAL :: psik
                                          
      REAL :: chic
      REAL :: psic
      IF (ZoL < 0.0D0) THEN
        chik  = (1.0D0-15.0D0*ZoL)**0.25D0
        psik  = 2.0D0*LOG(0.5D0*(1.0D0+chik**2))
        chic  = (1.0D0-34.15D0*ZoL)**r3
        psic  = 1.5D0*LOG((chic**2+chic+1.0D0)*r3)
     &               -sqr3*ATAN((2.0D0*chic+1.0D0)/sqr3)
     &               +2.D0*pis2osqr3
        bulk_psit_coare = psic+(psik-psic)/(1.0D0+ZoL**2)
      ELSE
        chic=-MIN(50.0D0,0.35D0*ZoL)
        bulk_psit_coare = -((1.0D0+2.0D0*ZoL/3.0D0)**1.5D0+
     &            0.6667D0*(ZoL-14.28D0)*EXP(chic)+8.525D0)
      ENDIF
      return
      END FUNCTION bulk_psit_coare
