!**** 1D-Var in clouds/precipitation for SSM/I radiances

! Provides diagnostic output for archiving in the ODB, 
! including the calculation of total column integrals 
! (in kg m^-2) of cloud and rain-related parameters.

! A. Geer    16-Feb-2007     First implementation

subroutine onedvar_diagnostics ( &  
 & kstart, kend, & ! in: start and end points in grid point array 
 & kproma, &  ! in: no of grid pts.
 & p_sfc, &   ! in: pressure @ sfc
 & pt, &      ! in: temperature
 & pq, &      ! in: spec. humidity
 & prr, &     ! in: rain
 & prs, &     ! in: snow
 & prl, &     ! in: cloud water
 & pri, &     ! in: cloud ice
 & ptcwv, &   ! out: total column water vapour
 & prwp, &    ! out: rain water path
 & pswp, &    ! out: snow water path
 & pcwp, &    ! out: cloud water path
 & piwp, &    ! out: cloud ice water path
 & prr_sfc, & ! out: surface rain rate
 & prs_sfc)   ! out: surface snow rate     
 
! Module constants
use yomdim       , only: nflevg ! no. of vertical levels
use yomcst       , only: rg, rd
use parkind1     , only: jprb, jpim
use yomgem       , only: vah, vbh
use yomhook      , only: lhook, dr_hook

implicit none

! Interface
integer (kind=jpim), intent (in) :: kstart, kend, kproma

real (kind=jprb), intent (in), dimension (kproma) :: p_sfc

real (kind=jprb), intent (in), dimension (kproma, nflevg) ::  &  
  & pt, pq, prr, prs, prl, pri

real (kind=jprb), intent (out), dimension (kproma) ::  &  
  & ptcwv, prwp, pswp, pcwp, piwp, prr_sfc, prs_sfc

! Local variables
real (kind=jprb), dimension (nflevg+1) :: zph
real (kind=jprb), dimension (nflevg) :: zdp, zdlnp, zdz, zrhoair_rain, zrhoair_snow
integer (kind=jpim) :: i_prof, i_lev
real (kind=jprb) :: zhook_handle
real (kind=jprb) :: zrho_rain, zrho_snow, zconv_rain(2), zconv_sp(2) 

if (lhook) call dr_hook ('ONEDVAR_DIAGNOSTICS',0,zhook_handle)

! Get coefficients used within RTTOV to covert rain fluxes to
! densities
call onedvar_rain_coeffs (zrho_rain, zrho_snow, zconv_rain, zconv_sp) 

do i_prof = kstart, kend

  ! Calculate half level pressures
  zph(1:nflevg+1) = vbh (0:nflevg) * p_sfc(i_prof) + vah (0:nflevg)

  ! Calculate full level pressure width, and logarithmic pressure ratio
  do i_lev = 1, nflevg
    zdp (i_lev) = zph(i_lev+1) - zph(i_lev)
  enddo 

  ! Calculate logarithmic pressure ratio; at TOA set it to 0 as this
  ! ratio would otherwise be infinite with a model top pressure of 0
  zdlnp(1) = 0.0_JPRB
  do i_lev = 2, nflevg
    zdlnp (i_lev) = log(zph(i_lev+1) / zph(i_lev))
  enddo 

  ! Column integrated specific humidity, cloud water and ice
  ptcwv(i_prof) = sum(pq(i_prof,:)  * zdp / rg) 
  pcwp(i_prof)  = sum(prl(i_prof,:) * zdp / rg) 
  piwp(i_prof)  = sum(pri(i_prof,:) * zdp / rg) 

  ! Calculate rain and snow densities from fluxes, using same 
  ! assumptions and relationship as used in the RTTOV scattering 
  ! r/t operator. This relationship is not given in SI units. The
  ! other constants here convert fluxes from kg m^-2 s^-1 to 
  ! mm hr^-1, then densities back from g m^-3 to kg m^-3
  zrhoair_rain = 0.0_JPRB
  where(prr(i_prof,:) > 0.0_JPRB) zrhoair_rain = ( &
    & (zconv_rain(1) * 3600.0_JPRB * prr(i_prof,:) / zrho_rain ) ** &
    & zconv_rain(2) ) / 1000.0_JPRB   
  zrhoair_snow = 0.0_JPRB
  where(prs(i_prof,:) > 0.0_JPRB) zrhoair_snow = ( &
    & ( zconv_sp(1) * 3600.0_JPRB * prs(i_prof,:) / zrho_snow ) ** &
    & zconv_sp(2) ) / 1000.0_JPRB   

  ! Layer thickness ignoring any dependence on q
  zdz = pt(i_prof,:) * zdlnp(:) * rd / rg 

  ! Column integrated rain and snow water paths
  prwp(i_prof) = sum(zdz * zrhoair_rain)
  pswp(i_prof) = sum(zdz * zrhoair_snow)

  ! Surface rainfall is the rainfall in the lowest full model level.
  ! (Note: although the physics works with rain fluxes on half levels,
  ! onedvar works with full level averages instead)
  prr_sfc(i_prof) = prr(i_prof,nflevg)
  prs_sfc(i_prof) = prs(i_prof,nflevg)

enddo

if (lhook) call dr_hook('ONEDVAR_DIAGNOSTICS',1,zhook_handle)
end subroutine onedvar_diagnostics



