!
      module ar_module
!
! Advance argon by one time step.
!
! Boundary conditions, production and loss for CH4 are defined
! by comp_ar, and referenced by minor_ar. Comp_ar is called
! from a latitude loop in dynamics. After comp_ar, dynamics calls
! minor_ar, which passes this module data to sub minor. Sub
! minor contains 3d mpi calls and its own latitude loops. 
!
      use params_module,only: nlevp1
      use addfld_module,only: addfld
      implicit none
!
! Boundary conditions and production and loss terms are allocated
! subdomains by sub alloc_ar (called from allocdata.F).
!
      real,allocatable,dimension(:,:)   :: ar_ubc ! upper boundary (i,j)
      real,allocatable,dimension(:,:,:) :: ar_lbc ! lower boundary (i,3,j)
      real,allocatable,dimension(:,:,:) ::
     |  ar_prod,     ! production of argon (k,i,j)
     |  ar_loss      ! loss of argon       (k,i,j)
!
      contains
!-----------------------------------------------------------------------
      subroutine comp_ar(ar,barm,xnmbar,
     |  lev0,lev1,lon0,lon1,lat)
!
! Input args:
      integer,intent(in) :: lev0,lev1,lon0,lon1,lat
      real,dimension(lev0:lev1,lon0-2:lon1+2),intent(in) ::
     |  ar,          ! argon from previous step
     |  barm,        ! mbar
     |  xnmbar       ! p0e(-z), etc.
!
! Local:
      integer :: i
      real :: war(2) = (/.01297,.01296/) ! from tgcm24 (see comp_dat.F)
!
      do i=lon0,lon1 
        ar_lbc(i,1,lat) = 0.    ! t1
        ar_lbc(i,2,lat) = 1.    ! t2
        ar_lbc(i,3,lat) = -sqrt(war(1)*war(2))
        ar_ubc(i,lat)   = 0.
      enddo
!     write(6,"('comp_ar: lat=',i3,' ar_lbc(3)=',/,(6e12.4))") 
!    |  lat,ar_lbc(:,3,lat)
!
! Production and loss of argon are zero:
      ar_prod = 0.
      ar_loss = 0.

      end subroutine comp_ar
!-----------------------------------------------------------------------
      subroutine minor_ar(tn,o2,ox,n2,he,w,difkk,ar,ar_nm,hdar,
     |  ar_out,arnm_out,lev0,lev1,lon0,lon1,lat0,lat1)
      use cons_module,only: rmass_ar
      use compdat_module,only: war ! currently zeroed out (data from tgcm24 (nlevp1=45) in comp_dat.F)
      implicit none
!
! Input args:
      integer,intent(in) :: lev0,lev1,lon0,lon1,lat0,lat1
      real,dimension(lev0:lev1,lon0-2:lon1+2,lat0-2:lat1+2),
     |  intent(in) ::
     |  tn,      ! neutral temperature (deg K)
     |  o2,      ! molecular oxygen (mmr)
     |  ox,      ! oxygen family (mmr)
     |  n2,      ! molecular nitrogen (mmr)
     |  he,      ! helium (mmr)
     |  w,       ! omega (vertical velocity)
     |  difkk,   ! eddy viscosity (from mgw)
     |  ar,     ! ch4 (mmr)
     |  ar_nm,  ! ch4 at time n-1
     |  hdar    ! horizontal diffusion
!
! Output args:
      real,dimension(lev0:lev1,lon0-2:lon1+2,lat0-2:lat1+2),
     |  intent(out) ::
     |  ar_out,    ! ar output
     |  arnm_out   ! ar output at time n-1
!
! Local:
      integer :: i,k,ibndbot,ibndtop,i0,i1,lat
      real,parameter :: phi_ar(3) = (/1.042,1.509,1.176/) ! from tgcm24
      real,parameter :: alfa_ar   = 0.17    ! thermal diffusion coefficient (from tgcm24)
      real,parameter :: xyar      = 1.e-10  ! from tgcm24

      ibndbot = 0 ! ilbc in minor
      ibndtop = 1 ! iubc in minor
!
!     subroutine minor(tn,o2,ox,n2,he,w,difkk,fin,fnm,fhd,flbc,fubc,
!    |  sloss,sprod,small,rmass,phix,alfax,xy,ilbc,iubc,fout,fout_nm,
!    |  lev0,lev1,lon0,lon1,lat0,lat1,idebug)
!
      call minor(tn,o2,ox,n2,he,w,difkk,ar,ar_nm,hdar,ar_lbc,ar_ubc,
     |  ar_loss,ar_prod,war,rmass_ar,phi_ar,alfa_ar,xyar,ibndbot,
     |  ibndtop,ar_out,arnm_out,lev0,lev1,lon0,lon1,lat0,lat1,0)
!
      end subroutine minor_ar
!-----------------------------------------------------------------------
      subroutine alloc_ar(lon0,lon1,lat0,lat1)
!
! Allocate subdomains (without ghost cells) to module data for boundary
! conditions and production and loss terms. This is called once per run
! from sub allocdata (allocdata.F).
!
! Args:
      integer,intent(in) :: lon0,lon1,lat0,lat1
!
! Local:
      integer :: istat
!
! Allocate subdomains to boundary conditions:
      allocate(ar_ubc(lon0:lon1,lat0:lat1),stat=istat)
      if (istat /= 0) write(6,"('>>> alloc_ar: error allocating',
     |  ' ar_ubc: stat=',i3)") istat
      allocate(ar_lbc(lon0:lon1,3,lat0:lat1),stat=istat)
      if (istat /= 0) write(6,"('>>> alloc_ar: error allocating',
     |  ' ar_lbc: stat=',i3)") istat
      ar_ubc = 0. ; ar_lbc = 0.
!
! Allocate subdomains to production and loss:
      allocate(ar_prod(nlevp1,lon0:lon1,lat0:lat1),stat=istat)
      if (istat /= 0) write(6,"('>>> alloc_ar: error allocating',
     |  ' ar_prod: stat=',i3)") istat
      allocate(ar_loss(nlevp1,lon0:lon1,lat0:lat1),stat=istat)
      if (istat /= 0) write(6,"('>>> alloc_ar: error allocating',
     |  ' ar_loss: stat=',i3)") istat
      ar_prod = 0. ; ar_loss = 0.
!
      write(6,"('alloc_ar: allocated module data')")
!
      end subroutine alloc_ar
!-----------------------------------------------------------------------
      end module ar_module
