#include <misc.h>
#include <params.h>

module buffer

!----------------------------------------------------------------------- 
! 
! Purpose: 
! Initialization of prognostic variables.
!
! Author: 
! 
!-----------------------------------------------------------------------

  use precision
  use pmgrid
  use infnan

  real(r8), allocatable :: vort(:,:,:)   ! vorticity
  real(r8), allocatable :: div(:,:,:)    ! divergence
  real(r8), allocatable :: duh(:,:,:)    ! horizontal u-momentum diffusion
  real(r8), allocatable :: dvh(:,:,:)    ! horizontal v-momentum diffusion
  real(r8), allocatable :: dth(:,:,:)    ! horizontal temperature diffusion
  real(r8), allocatable :: vorttl(:,:,:)   ! vorticity: half time-filtered
  real(r8), allocatable :: divtl(:,:,:)    ! divergence: half time-filtered
  real(r8), allocatable :: um2(:,:,:)      ! u-wind (time n-2)
  real(r8), allocatable :: vm2(:,:,:)      ! v-wind (time n-2)
  real(r8), allocatable :: tm2(:,:,:)      ! temperature (time n-2)
  real(r8), allocatable :: qrs(:,:,:)      ! shortwave radiative heating rate 
  real(r8), allocatable :: qrl(:,:,:)      ! longwave  radiative heating rate 
  real(r8), allocatable :: vortm1(:,:,:)   ! vorticity (time n-1)
  real(r8), allocatable :: divm1(:,:,:)    ! divergence (time n-1)
  real(r8), allocatable :: omga(:,:,:)     ! vertical velocity
  real(r8), allocatable :: psomc(:,:,:)    ! (psm1/pmidm1)**cappa
  real(r8), allocatable :: thm(:,:,:)      ! potential temperature
  real(r8), allocatable :: zm(:,:,:)       ! height of midpoints above surface
  real(r8), allocatable :: pmln(:,:,:)     ! log(pmid)

  real(r8), allocatable :: pilnm1(:,:,:)   ! ln(pintm1)

  real(r8), allocatable :: dpsl(:,:)       ! longitudinal pressure gradient
  real(r8), allocatable :: dpsm(:,:)       ! meridional pressure gradient
  real(r8), allocatable :: dps(:,:)        ! pressure gradient
  real(r8), allocatable :: phis(:,:)       ! surface geopotential
  real(r8), allocatable :: psm2(:,:)       ! surface pressure (time n-2)
  real(r8), allocatable :: pblht(:,:)      ! PBL height
  real(r8), allocatable :: tpert(:,:)      ! temperature perturbation (PBL)
  real(r8), allocatable :: fsns(:,:)       ! surface absorbed solar flux
  real(r8), allocatable :: bpstr(:,:)     

  real(r8), allocatable :: qminus(:,:,:,:) ! saves q being advected in slt code
  real(r8), allocatable :: qm2(:,:,:,:)    ! constituent-h2o first (time n-2)
  real(r8), allocatable :: qpert(:,:,:)    ! moisture/constituent perturb.(PBL)
  real(r8), allocatable :: cnt(:,:)        ! top lvl of convective activity
  real(r8), allocatable :: cnb(:,:)        ! btm lvl of convective activity
  real(r8), allocatable :: cmfmc(:,:,:)    ! convective mass flux--m sub c

contains

  subroutine initialize_buffer
!
! Allocate memory
!
    allocate (vort  (plond,plev,beglat:endlat))   
    allocate (div   (plond,plev,beglat:endlat))    
    allocate (duh   (plond,plev,beglat:endlat))    
    allocate (dvh   (plond,plev,beglat:endlat))    
    allocate (dth   (plond,plev,beglat:endlat))    
    allocate (vorttl(plond,plev,beglat:endlat)) 
    allocate (divtl (plond,plev,beglat:endlat))  
    allocate (um2   (plond,plev,beglat:endlat))    
    allocate (vm2   (plond,plev,beglat:endlat))    
    allocate (tm2   (plond,plev,beglat:endlat))    
    allocate (qrs   (plond,plev,beglat:endlat))     
    allocate (qrl   (plond,plev,beglat:endlat))     
    allocate (vortm1(plond,plev,beglat:endlat))  
    allocate (divm1 (plond,plev,beglat:endlat))   
    allocate (omga  (plond,plev,beglat:endlat))    
    allocate (psomc (plond,plev,beglat:endlat))   
    allocate (thm   (plond,plev,beglat:endlat))     
    allocate (zm    (plond,plev,beglat:endlat))      
    allocate (pmln  (plond,plev,beglat:endlat))    

    allocate (pilnm1(plond,plevp,beglat:endlat)) 

    allocate (dpsl (plond,beglat:endlat))        
    allocate (dpsm (plond,beglat:endlat))        
    allocate (dps  (plond,beglat:endlat))         
    allocate (phis (plond,beglat:endlat))        
    allocate (psm2 (plond,beglat:endlat))        
    allocate (pblht(plond,beglat:endlat))       
    allocate (tpert(plond,beglat:endlat))       
    allocate (fsns (plond,beglat:endlat))         
    allocate (bpstr(plond,beglat:endlat))        

    allocate (qminus(plond,plev,pcnst,beglat:endlat)) 
    allocate (qm2   (plond,plev,pcnst+pnats,beglat:endlat))
    allocate (qpert (plond,pcnst+pnats,beglat:endlat)) 
    allocate (cnt   (plond,beglat:endlat))
    allocate (cnb   (plond,beglat:endlat))
    allocate (cmfmc (plond,plev,beglat:endlat))
!
! Initialize to NaN or Inf
!
    vort  (:,:,:) = inf
    div   (:,:,:) = inf
    duh   (:,:,:) = inf
    dvh   (:,:,:) = inf
    dth   (:,:,:) = inf
    vorttl(:,:,:) = inf
    divtl (:,:,:) = inf
    um2   (:,:,:) = inf
    vm2   (:,:,:) = inf
    tm2   (:,:,:) = inf
    qrs   (:,:,:) = inf
    qrl   (:,:,:) = inf
    vortm1(:,:,:) = inf
    divm1 (:,:,:) = inf
    omga  (:,:,:) = inf
    psomc (:,:,:) = inf
    thm   (:,:,:) = inf
    zm    (:,:,:) = inf
    pmln  (:,:,:) = inf

    pilnm1(:,:,:) = inf

    dpsl (:,:) = inf
    dpsm (:,:) = inf
    dps  (:,:) = inf
    phis (:,:) = inf
    psm2 (:,:) = inf
!
! Initialize pblht,tpert to zero for nstep=0 cmfmca
!
    pblht(:,:) = 0.
    tpert(:,:) = 0.
    fsns (:,:) = inf
    bpstr(:,:) = inf

    qminus(:,:,:,:) = inf
    qm2(:,:,:,:) = inf
    qpert(:,:,:) = 0.     ! for conv_ccm nstep=0
    cnt(:,:) = 0.
    cnb(:,:) = 0.
    cmfmc(:,:,:) = inf

    return
  end subroutine initialize_buffer

end module buffer




