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

      use precision
      use pmgrid
      use infnan

      real(r8), allocatable:: asdir(:,:)   ! albedo: shortwave, direct
      real(r8), allocatable:: asdif(:,:)   ! albedo: shortwave, diffuse
      real(r8), allocatable:: aldir(:,:)   ! albedo: longwave, direct
      real(r8), allocatable:: aldif(:,:)   ! albedo: longwave, diffuse
      real(r8), allocatable:: lwup(:,:)    ! longwave up radiative flux
      real(r8), allocatable:: oro(:,:)     ! land/ocean/sea ice flag
      real(r8), allocatable:: sgh(:,:)     ! land/ocean/sea ice flag
      real(r8), allocatable:: ts(:,:)      ! sfc temp (merged w/ocean if coupled)
      real(r8), allocatable:: tssub(:,:,:) ! ccm surface/subsurface temperatures
      real(r8), allocatable:: sicthk(:,:)  ! ccm sea-ice thickness (m)
      real(r8), allocatable:: snowh(:,:)   ! ccm snow depth (liquid water)
      real(r8), allocatable:: flwds(:,:)   ! downward longwave radiation at surface
      real(r8), allocatable:: sols(:,:)    ! direct beam solar radiation onto srf
      real(r8), allocatable:: soll(:,:)    ! direct beam solar radiation onto srf
      real(r8), allocatable:: solsd(:,:)   ! diffuse solar radiation onto srf (sw)
      real(r8), allocatable:: solld(:,:)   ! diffuse solar radiation onto srf (lw)
      real(r8), allocatable:: lhf(:,:)     ! latent heat flux
      real(r8), allocatable:: shf(:,:)     ! sensible heat flux
      real(r8), allocatable:: cflx(:,:,:)  ! pcnst+pnats constituent flux (evap)
      real(r8), allocatable:: wsx(:,:)     ! surface u-stress (N)
      real(r8), allocatable:: wsy(:,:)     ! surface v-stress (N)
      real(r8), allocatable:: tref(:,:)    ! ref height surface air temp
      real(r8), allocatable:: tbot(:,:)    ! bot level temperature
      real(r8), allocatable:: zbot(:,:)    ! bot level height above surface
      real(r8), allocatable:: ubot(:,:)    ! bot level u wind
      real(r8), allocatable:: vbot(:,:)    ! bot level v wind
      real(r8), allocatable:: qbot(:,:)    ! bot level specific humidity
      real(r8), allocatable:: pbot(:,:)    ! bot level pressure
      real(r8), allocatable:: precc(:,:)   ! convective precip rate
      real(r8), allocatable:: precl(:,:)   ! large-scale precip rate
      real(r8), allocatable:: precsc(:,:)  ! convective snow-fall rate
      real(r8), allocatable:: precsl(:,:)  ! large-scale snow-fall rate
      real(r8), allocatable:: thbot(:,:)   ! bot level potential temperature
      real(r8), allocatable:: srfrpdel(:,:)! 1./(pint(k+1)-pint(k))
      real(r8), allocatable:: srfrad(:,:)  ! surface net radiative flux
      real(r8), allocatable:: psm1(:,:)    ! surface pressure
      real(r8), allocatable:: absorb(:,:)  ! ccm surf absorbed solar flux (W/m2)
      real(r8), allocatable:: prcsnw(:,:)  ! ccm tot snow precip
!ku
      real(r8) :: my_aldif(128,64) ! my albedo: longwave, diffuse
      real(kind=4) :: T42_oro(128,64)  ! my albedo: longwave, diffuse
!ku

#if ( defined COUP_CSM )
      real(r8), allocatable:: rho(:,:)
      real(r8), allocatable:: netsw(:,:)
      real(r8), allocatable:: precca(:,:)   ! Average convective precipitation
      real(r8), allocatable:: precla(:,:)   ! Average large-scale precipation
      real(r8), allocatable:: precsca(:,:)  ! Average convective snow-fall
      real(r8), allocatable:: precsla(:,:)  ! Average large-scale snow-fall
      real(r8), allocatable:: rainconv(:,:) ! Convective rainfall
      real(r8), allocatable:: rainlrsc(:,:) ! Large-scale rainfall
      real(r8), allocatable:: snowconv(:,:) ! Convective snowfall
      real(r8), allocatable:: snowlrsc(:,:) ! Larse-scale snowfall
      real(r8), allocatable:: prc_err(:,:)  ! Error in precipitation sent to coupler
      real(r8), allocatable:: ocnfrac(:,:)  ! frac of sfc area covered by open ocean
      real(r8), allocatable:: icefrac(:,:)  ! fraction of sfc area covered by seaice
#endif

      contains

      subroutine initialize_comsrf

      allocate (asdir   (plond,beglat:endlat))
      allocate (asdif   (plond,beglat:endlat))
      allocate (aldir   (plond,beglat:endlat))
      allocate (aldif   (plond,beglat:endlat))
      allocate (lwup    (plond,beglat:endlat))
      allocate (oro     (plond,beglat:endlat))
      allocate (sgh     (plond,beglat:endlat))
      allocate (ts      (plond,beglat:endlat))
      allocate (tssub   (plond,plevmx,beglat:endlat))
      allocate (sicthk  (plond,beglat:endlat))
      allocate (snowh   (plond,beglat:endlat))
      allocate (flwds   (plond,beglat:endlat))
      allocate (sols    (plond,beglat:endlat))
      allocate (soll    (plond,beglat:endlat))
      allocate (solsd   (plond,beglat:endlat))
      allocate (solld   (plond,beglat:endlat))
      allocate (lhf     (plond,beglat:endlat))
      allocate (shf     (plond,beglat:endlat))
      allocate (cflx    (plond,pcnst+pnats,beglat:endlat))
      allocate (wsx     (plond,beglat:endlat))
      allocate (wsy     (plond,beglat:endlat))
      allocate (tref    (plond,beglat:endlat))
      allocate (tbot    (plond,beglat:endlat))
      allocate (zbot    (plond,beglat:endlat))
      allocate (ubot    (plond,beglat:endlat))
      allocate (vbot    (plond,beglat:endlat))
      allocate (qbot    (plond,beglat:endlat))
      allocate (pbot    (plond,beglat:endlat))
      allocate (precc   (plond,beglat:endlat))
      allocate (precl   (plond,beglat:endlat))
      allocate (precsc  (plond,beglat:endlat))
      allocate (precsl  (plond,beglat:endlat))
      allocate (thbot   (plond,beglat:endlat))
      allocate (srfrpdel(plond,beglat:endlat))
      allocate (srfrad  (plond,beglat:endlat))
      allocate (psm1    (plond,beglat:endlat))
      allocate (absorb  (plond,beglat:endlat))
      allocate (prcsnw  (plond,beglat:endlat))
!ku
!      allocate (my_aldif   (plond,beglat:endlat))
!      allocate (T42_oro    (plond,beglat:endlat))
!ku

#if ( defined COUP_CSM )
      allocate (rho     (plond,beglat:endlat))
      allocate (netsw   (plond,beglat:endlat))
      allocate (precca  (plon ,beglat:endlat))
      allocate (precla  (plon ,beglat:endlat))
      allocate (precsca (plon ,beglat:endlat))
      allocate (precsla (plon ,beglat:endlat))
      allocate (rainconv(plon ,beglat:endlat))
      allocate (rainlrsc(plon ,beglat:endlat))
      allocate (snowconv(plon ,beglat:endlat))
      allocate (snowlrsc(plon ,beglat:endlat))
      allocate (prc_err (plon ,beglat:endlat))
      allocate (ocnfrac (plond,beglat:endlat))
      allocate (icefrac (plond,beglat:endlat))
#endif
!
! Initialize to NaN or Inf
!
      asdir    (:,:) = inf
      asdif    (:,:) = inf
      aldir    (:,:) = inf
      aldif    (:,:) = inf
      lwup     (:,:) = inf
      oro      (:,:) = inf
      sgh      (:,:) = inf
      ts       (:,:) = inf
      tssub    (:,:,:) = inf
      sicthk   (:,:) = inf
      snowh    (:,:) = inf
      flwds    (:,:) = inf
      sols     (:,:) = inf
      soll     (:,:) = inf
      solsd    (:,:) = inf
      solld    (:,:) = inf
      lhf      (:,:) = inf
      shf      (:,:) = inf
      cflx     (:,:,:) = inf
      wsx      (:,:) = inf
      wsy      (:,:) = inf
      tref     (:,:) = inf
      tbot     (:,:) = inf
      zbot     (:,:) = inf
      ubot     (:,:) = inf
      vbot     (:,:) = inf
      qbot     (:,:) = inf
      pbot     (:,:) = inf
      precc    (:,:) = inf
      precl    (:,:) = inf
      precsc   (:,:) = inf
      precsl   (:,:) = inf
      thbot    (:,:) = inf
      srfrpdel (:,:) = inf
      srfrad   (:,:) = inf
      psm1     (:,:) = inf
      absorb   (:,:) = inf
      prcsnw   (:,:) = inf
!ku
!     my_aldif (:,:) = inf 
!     T42_oro  (:,:) = inf
!ku

#if ( defined COUP_CSM )
      rho      (:,:) = inf
      netsw    (:,:) = inf
      precca   (:,:) = inf
      precla   (:,:) = inf
      precsca  (:,:) = inf
      precsla  (:,:) = inf
      snowconv (:,:) = inf
      snowlrsc (:,:) = inf
      rainconv (:,:) = inf
      rainlrsc (:,:) = inf
      prc_err  (:,:) = inf
      ocnfrac  (:,:) = inf
      icefrac  (:,:) = inf
#endif

      return
      end subroutine initialize_comsrf

      end module comsrf
