!= 螟ｧ隕乗ｨ｡蜃晉ｵ� (髱槫ｯｾ豬∵ｧ蜃晉ｵ�) (Le Treut and Li, 1991)
!
!= Large scale condensation (non-convective condensation) (Le Treut and Li, 1991)
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: lscond_LL91.f90,v 1.4 2015/01/29 12:02:56 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module lscond_LL91
  !
  != 螟ｧ隕乗ｨ｡蜃晉ｵ� (髱槫ｯｾ豬∵ｧ蜃晉ｵ�)
  !
  != Large scale condensation (non-convective condensation)
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 螟ｧ隕乗ｨ｡蜃晉ｵ宣℃遞九↓繧医▲縺ｦ貂ｩ蠎ｦ縺ｨ豈疲ｹｿ繧定ｪｿ遽縺励∪縺�.
  !
  ! Adjust temperature and specific humidity by
  ! a large scale condensation process.
  !
  !== References
  !
  !  Le Treut, H., and Z.-X. Li,
  !    Sensitivity of an atmospheric general circulation model to prescribed SST changes:
  !    feedback effects associated with the simulation of cloud optical properties,
  !    Clim. Dyn., 5, 175-187, 1991.
  !
  !  Manabe, S., J. Smagorinsky, R. F. Strickler,
  !    Simulated climatology of a general circulation model with a hydrologic cycle,
  !    Mon. Wea. Rev., 93, 769-798, 1965.
  !
  !== Procedures List
  !
  ! LScaleCondLL91     :: 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ縺ｮ隱ｿ遽
  ! LScaleCondLL91Init :: 蛻晄悄蛹�
  ! -----------------  :: ------------
  ! LScaleCondLL91     :: Adjust temperature and specific humidity
  ! LScaleCondLL91Init :: Initialization
  !
  !== NAMELIST
  !
  ! NAMELIST#lscond_nml
  !

  ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
  !

  ! 譬ｼ蟄千せ險ｭ螳�
  ! Grid points settings
  !
  use gridset, only: imax, & ! 邨悟ｺｦ譬ｼ蟄千せ謨ｰ.
                             ! Number of grid points in longitude
    &                jmax, & ! 邱ｯ蠎ｦ譬ｼ蟄千せ謨ｰ.
                             ! Number of grid points in latitude
    &                kmax    ! 驩帷峩螻､謨ｰ.
                             ! Number of vertical level

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.
    &                 STRING     ! 譁�ｭ怜�.       Strings.

  ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 螳｣險譁� ; Declaration statements
  !
  implicit none
  private

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: LScaleCondLL91Ice
  public:: LScaleCondLL91
  public:: LScaleCondLL91Init

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save :: lscond_LL91_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  real(DP), save :: Gamma
                              !
                              ! Subgrid scale variation of specific humidity
                              ! expressed as fraction of specific humidity
!!$  real(DP), save:: CrtlRH
!!$                              ! 閾ｨ逡檎嶌蟇ｾ貉ｿ蠎ｦ.
!!$                              ! Critical relative humidity
  integer, save :: ItrtMax
                              ! 繧､繝�Ξ繝ｼ繧ｷ繝ｧ繝ｳ蝗樊焚.
                              ! Number of iteration
!!$  logical, save :: FlagUse
!!$                              ! 菴ｿ逕ｨ繝輔Λ繧ｰ
!!$                              ! flag for use of this scheme

  character(*), parameter:: module_name = 'lscond_LL91'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: lscond_LL91.f90,v 1.4 2015/01/29 12:02:56 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

  !--------------------------------------------------------------------------------------

  subroutine LScaleCondLL91Ice(                            &
    & xyz_Press, xyr_Press,                                &  ! (in)
    & xyz_Temp, xyz_QVap, xyz_QLiq, xyz_QSol,              &  ! (inout)
    & xyz_DTempDt, xyz_DQVapDt, xyz_DQLiqDt, xyz_DQSolDt   &  ! (out)
    & )
    !
    ! 螟ｧ隕乗ｨ｡蜃晉ｵ舌せ繧ｭ繝ｼ繝�縺ｫ繧医ｊ, 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ繧定ｪｿ遽縺励∪縺�.
    !
    ! Adjust temperature and specific humidity by
    ! large scale condensation scheme.
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 迚ｩ逅�ｮ壽焚險ｭ螳�
    ! Physical constants settings
    !
    use constants, only: &
      & Grav, &
                              ! $ g $ [m s-2].
                              ! 驥榊鴨蜉�騾溷ｺｦ.
                              ! Gravitational acceleration
      & CpDry, &
                              ! $ C_p $ [J kg-1 K-1].
                              ! 荵ｾ辯･螟ｧ豌励�螳壼悸豈皮�.
                              ! Specific heat of air at constant pressure
      & LatentHeat, &
                              ! $ L $ [J kg-1] .
                              ! 蜃晉ｵ舌�貎懃�.
                              ! Latent heat of condensation
      & LatentHeatFusion

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: &
      & DelTime, &            ! $ \Delta t $
      & TimeN, &              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾. Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoPut

    ! 鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ邂怜�
    ! Evaluate saturation specific humidity
    !
    use saturate, only: xyz_CalcQVapSat, xyz_CalcDQVapSatDTemp, SaturateWatFraction

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_Press (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(in):: xyr_Press (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . 豌怜悸 (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (half level)
    real(DP), intent(inout):: xyz_Temp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ .     貂ｩ蠎ｦ. Temperature
    real(DP), intent(inout):: xyz_QVap (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q $ .     豈疲ｹｿ. Specific humidity
    real(DP), intent(inout):: xyz_QLiq (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q_l $ .     Specific liquid water content
    real(DP), intent(inout):: xyz_QSol (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q_i $ .     Specific ice water content
    real(DP), intent(out):: xyz_DTempDt (0:imax-1, 1:jmax, 1:kmax)
                              ! 貂ｩ蠎ｦ螟牙喧邇�.
                              ! Temperature tendency
    real(DP), intent(out):: xyz_DQVapDt (0:imax-1, 1:jmax, 1:kmax)
                              ! 豈疲ｹｿ螟牙喧邇�.
                              ! Specific humidity tendency
    real(DP), intent(out) :: xyz_DQLiqDt(0:imax-1,1:jmax,1:kmax)
    real(DP), intent(out) :: xyz_DQSolDt(0:imax-1,1:jmax,1:kmax)

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP) :: xyz_CldFrac   (0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Cloud fraction

    real(DP) :: QCld
    real(DP) :: WatFrac
    real(DP) :: IceFrac

    real(DP) :: xy_RainLsc (0:imax-1, 1:jmax)
                              ! 髯肴ｰｴ驥�.
                              ! Precipitation
    real(DP) :: xyz_DTempDtLsc (0:imax-1, 1:jmax, 1:kmax)
                              ! 貂ｩ蠎ｦ螟牙喧邇�.
                              ! Temperature tendency
    real(DP) :: xyz_DQVapDtLsc (0:imax-1, 1:jmax, 1:kmax)
                              ! 豈疲ｹｿ螟牙喧邇�.
                              ! Specific humidity tendency
    real(DP) :: xyz_DQLiqDtLsc (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_DQSolDtLsc (0:imax-1, 1:jmax, 1:kmax)

    real(DP) :: xyz_TempTentative(0:imax-1, 1:jmax, 1:kmax)

    real(DP) :: xyz_TempB (0:imax-1, 1:jmax, 1:kmax)
                              ! 隱ｿ遽蜑阪�貂ｩ蠎ｦ.
                              ! Temperature before adjust.
    real(DP) :: xyz_QVapB (0:imax-1, 1:jmax, 1:kmax)
                              ! 隱ｿ遽蜑阪�豈疲ｹｿ.
                              ! Specific humidity before adjust.
    real(DP) :: xyz_QLiqB (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_QSolB (0:imax-1, 1:jmax, 1:kmax)
                              !

    real(DP) :: xyz_QTot         (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_DelQ         (0:imax-1, 1:jmax, 1:kmax)

    real(DP) :: xyz_QVapSat      (0:imax-1, 1:jmax, 1:kmax)
                              ! 鬟ｽ蜥梧ｯ疲ｹｿ.
                              ! Saturation specific humidity.
    real(DP) :: xyz_DQVapSatDTemp(0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DD{q_{\rm{sat}}}{T} $
    real(DP) :: DelTemp
                              ! 隱ｿ遽縺ｫ繧医ｋ貂ｩ蠎ｦ螟牙喧驥�.
                              ! Temperature variation by adjustment

    integer :: i              ! 邨悟ｺｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in longitude
    integer :: j              ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in latitude
    integer :: k              ! 驩帷峩譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in vertical direction
    integer :: itr            ! 繧､繝�Ξ繝ｼ繧ｷ繝ｧ繝ｳ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in iteration direction

    real(DP) :: xyz_RainLSC(0:imax-1, 1:jmax, 1:kmax)

    logical  :: xyz_FlagSaturated(0:imax-1, 1:jmax, 1:kmax)

    ! Variables for debug
!!$    real(DP) :: TempBefAdj
!!$    real(DP) :: QVapBefAdj

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. lscond_LL91_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

!!$    if ( .not. FlagUse ) return

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! 隱ｿ遽蜑� "QVap", "Temp" 縺ｮ菫晏ｭ�
    ! Store "QVap", "Temp" before adjustment
    !
    xyz_TempB  = xyz_Temp
    xyz_QVapB  = xyz_QVap
    xyz_QLiqB  = xyz_QLiq
    xyz_QSolB  = xyz_QSol

    xyz_QTot = xyz_QVap + xyz_QLiq + xyz_QSol
    xyz_DelQ = Gamma * xyz_QTot

    ! All cloud water and ice are evaporated temporarily
    ! After this temporal evaporation, adjustment will be done below.
    !
    xyz_TempTentative = xyz_Temp                       &
      & -   LatentHeat                      * xyz_QLiq &
      & - ( LatentHeat + LatentHeatFusion ) * xyz_QSol

    ! 隱ｿ遽
    ! Adjustment
    !

    ! 鬟ｽ蜥梧ｯ疲ｹｿ險育ｮ�
    ! Calculate saturation specific humidity
    !
    xyz_QVapSat = xyz_CalcQVapSat( xyz_TempTentative, xyz_Press )

    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1

!!$          if ( ( xyz_QVap(i,j,k) / xyz_QVapSat(i,j,k) ) >= CrtlRH ) then
          if ( ( ( xyz_QTot(i,j,k) + xyz_DelQ(i,j,k) ) / xyz_QVapSat(i,j,k) ) >= 1.0_DP ) then
            xyz_FlagSaturated(i,j,k) = .true.
          else
            xyz_FlagSaturated(i,j,k) = .false.
          end if

        end do
      end do
    end do

    do itr = 1, ItrtMax

      ! 鬟ｽ蜥梧ｯ疲ｹｿ險育ｮ�
      ! Calculate saturation specific humidity
      !
      xyz_QVapSat       = xyz_CalcQVapSat      ( xyz_Temp, xyz_Press   )
      xyz_DQVapSatDTemp = xyz_CalcDQVapSatDTemp( xyz_Temp, xyz_QVapSat )

      do k = 1, kmax
        do j = 1, jmax
          do i = 0, imax-1

            ! 鬟ｽ蜥後＠縺ｦ縺�◆繧�, 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ縺ｮ螟牙喧繧定ｨ育ｮ�
            ! Calculate tendency of temperature and humidity
            ! if moist is saturation.
            !
            if ( xyz_FlagSaturated(i,j,k) ) then

              ! Liquid water and ice fractions
              call SaturateWatFraction(   &
                & xyz_Temp(i,j,k),        & ! (in )
                & WatFrac                 & ! (out)
                & )
              IceFrac = 1.0_DP - WatFrac

              ! 貂ｩ蠎ｦ縺ｮ螟牙喧蛻�ｒ繝九Η繝ｼ繝医Φ豕輔〒豎ゅａ繧�
              ! Calculate variation of temperature
              !
              DelTemp = &
                & ( &
                &     LatentHeat            &
                &     * (   xyz_QVap(i,j,k) &
                &         - ( xyz_QVapSat(i,j,k) + xyz_QTot(i,j,k) - xyz_DelQ(i,j,k) ) / 2.0_DP ) &
                &   - LatentHeatFusion                      &
                &     * (   xyz_QSol(i,j,k)                 &
                &         - IceFrac * ( xyz_QVapSat(i,j,k) + xyz_QTot(i,j,k) + xyz_DelQ(i,j,k) ) / 2.0_DP ) &
                & ) &
                &    / (   CpDry &
                &        + ( LatentHeat - LatentHeatFusion * IceFrac ) &
                &          * xyz_DQVapSatDTemp(i,j,k) / 2.0_DP )

              ! 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ縺ｮ隱ｿ遽
              ! Adjust temperature and specific humidity
              !
              xyz_QVapSat(i,j,k) = &
                & xyz_QVapSat(i,j,k) + xyz_DQVapSatDTemp(i,j,k) * DelTemp
              xyz_Temp(i,j,k) = xyz_Temp(i,j,k) + DelTemp
              QCld = ( xyz_QVapSat(i,j,k) + xyz_QTot(i,j,k) + xyz_DelQ(i,j,k) ) / 2.0_DP
              xyz_QVap(i,j,k) = ( xyz_QVapSat(i,j,k) + xyz_QTot(i,j,k) - xyz_DelQ(i,j,k) ) / 2.0_DP
              xyz_QLiq(i,j,k) = WatFrac * QCld
              xyz_QSol(i,j,k) = IceFrac * QCld

            end if

          end do
        end do
      end do

      xyz_CldFrac = ( xyz_QTot + xyz_DelQ - xyz_QVapSat )
      xyz_CldFrac = max( min( xyz_CldFrac, 1.0_DP ), 0.0_DP )

    end do

    ! 豈疲ｹｿ螟牙喧邇�, 貂ｩ蠎ｦ螟牙喧邇�, 髯肴ｰｴ驥上�邂怜�
    ! Calculate specific humidity tendency, temperature tendency,
    ! precipitation
    !
    xyz_DTempDt = ( xyz_Temp - xyz_TempB ) / ( 2.0_DP * DelTime )
    xyz_DQVapDt = ( xyz_QVap - xyz_QVapB ) / ( 2.0_DP * DelTime )
    xyz_DQLiqDt = ( xyz_QLiq - xyz_QLiqB ) / ( 2.0_DP * DelTime )
    xyz_DQSolDt = ( xyz_QSol - xyz_QSolB ) / ( 2.0_DP * DelTime )

    ! calculation for output
    xy_RainLsc     = 0.0_DP
    do k = kmax, 1, -1
      xy_RainLsc = xy_RainLsc                                     &
        & + ( xyz_DQLiqDt(:,:,k) + xyz_DQSolDt(:,:,k) )           &
        &   * ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav
    end do

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    call HistoryAutoPut( TimeN, 'RainLsc',    xy_RainLsc * LatentHeat )
    call HistoryAutoPut( TimeN, 'DTempDtLsc', xyz_DTempDtLsc )
    call HistoryAutoPut( TimeN, 'DQVapDtLsc', xyz_DQVapDtLsc )

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine LScaleCondLL91Ice

  !--------------------------------------------------------------------------------------

  subroutine LScaleCondLL91(       &
    & xyz_Temp, xyz_QVap,          &  ! (inout)
    & xyz_DTempDt, xyz_DQVapDt,    &  ! (inout)
    & xyz_Press, xyr_Press,        &  ! (in)
    & xyz_DQH2OLiqDt               &  ! (out)
    & )
    !
    ! 螟ｧ隕乗ｨ｡蜃晉ｵ舌せ繧ｭ繝ｼ繝�縺ｫ繧医ｊ, 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ繧定ｪｿ遽縺励∪縺�.
    !
    ! Adjust temperature and specific humidity by
    ! large scale condensation scheme.
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 迚ｩ逅�ｮ壽焚險ｭ螳�
    ! Physical constants settings
    !
    use constants, only: &
      & Grav, &
                              ! $ g $ [m s-2].
                              ! 驥榊鴨蜉�騾溷ｺｦ.
                              ! Gravitational acceleration
      & CpDry, &
                              ! $ C_p $ [J kg-1 K-1].
                              ! 荵ｾ辯･螟ｧ豌励�螳壼悸豈皮�.
                              ! Specific heat of air at constant pressure
      & LatentHeat
                              ! $ L $ [J kg-1] .
                              ! 蜃晉ｵ舌�貎懃�.
                              ! Latent heat of condensation

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: &
      & DelTime, &            ! $ \Delta t $
      & TimeN, &              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾. Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoPut

    ! 鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ邂怜�
    ! Evaluate saturation specific humidity
    !
    use saturate, only: xyz_CalcQVapSat, xyz_CalcDQVapSatDTemp

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(inout):: xyz_Temp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ .     貂ｩ蠎ｦ. Temperature
    real(DP), intent(inout):: xyz_QVap (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q $ .     豈疲ｹｿ. Specific humidity
    real(DP), intent(inout):: xyz_DTempDt (0:imax-1, 1:jmax, 1:kmax)
                              ! 貂ｩ蠎ｦ螟牙喧邇�.
                              ! Temperature tendency
    real(DP), intent(inout):: xyz_DQVapDt (0:imax-1, 1:jmax, 1:kmax)
                              ! 豈疲ｹｿ螟牙喧邇�.
                              ! Specific humidity tendency
    real(DP), intent(in):: xyz_Press (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(in):: xyr_Press (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . 豌怜悸 (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (half level)
    real(DP), intent(out) :: xyz_DQH2OLiqDt(0:imax-1,1:jmax,1:kmax)

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP) :: xyz_CldFrac   (0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Cloud fraction
    real(DP) :: xyz_QVapHalRan(0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Half range of specific humidity
    real(DP) :: xyz_QVapCler  (0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Specific humidity in clear region
    real(DP) :: xyz_QVapCldy  (0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Specific humidity in cloudy region
    real(DP) :: xyz_TempCler  (0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Temperature in clear region
    real(DP) :: xyz_TempCldy  (0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Temperature in cloudy region

    real(DP) :: xy_RainLsc (0:imax-1, 1:jmax)
                              ! 髯肴ｰｴ驥�.
                              ! Precipitation
    real(DP) :: xyz_DTempDtLsc (0:imax-1, 1:jmax, 1:kmax)
                              ! 貂ｩ蠎ｦ螟牙喧邇�.
                              ! Temperature tendency
    real(DP) :: xyz_DQVapDtLsc (0:imax-1, 1:jmax, 1:kmax)
                              ! 豈疲ｹｿ螟牙喧邇�.
                              ! Specific humidity tendency

    real(DP) :: xyz_QVapB (0:imax-1, 1:jmax, 1:kmax)
                              ! 隱ｿ遽蜑阪�豈疲ｹｿ.
                              ! Specific humidity before adjust.
    real(DP) :: xyz_TempB (0:imax-1, 1:jmax, 1:kmax)
                              ! 隱ｿ遽蜑阪�貂ｩ蠎ｦ.
                              ! Temperature before adjust.
                              !
    real(DP) :: xyz_QVapSat      (0:imax-1, 1:jmax, 1:kmax)
                              ! 鬟ｽ蜥梧ｯ疲ｹｿ.
                              ! Saturation specific humidity.
    real(DP) :: xyz_DQVapSatDTemp(0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DD{q_{\rm{sat}}}{T} $
    real(DP) :: DelTemp
                              ! 隱ｿ遽縺ｫ繧医ｋ貂ｩ蠎ｦ螟牙喧驥�.
                              ! Temperature variation by adjustment

    integer :: i              ! 邨悟ｺｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in longitude
    integer :: j              ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in latitude
    integer :: k              ! 驩帷峩譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in vertical direction
    integer :: itr            ! 繧､繝�Ξ繝ｼ繧ｷ繝ｧ繝ｳ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in iteration direction

    real(DP) :: xyz_RainLSC(0:imax-1, 1:jmax, 1:kmax)

    logical  :: xyz_FlagSaturated(0:imax-1, 1:jmax, 1:kmax)

    ! Variables for debug
!!$    real(DP) :: TempBefAdj
!!$    real(DP) :: QVapBefAdj

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. lscond_LL91_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

!!$    if ( .not. FlagUse ) return

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! 隱ｿ遽蜑� "QVap", "Temp" 縺ｮ菫晏ｭ�
    ! Store "QVap", "Temp" before adjustment
    !
    xyz_QVapB  = xyz_QVap
    xyz_TempB  = xyz_Temp

    ! 隱ｿ遽
    ! Adjustment
    !

    ! 鬟ｽ蜥梧ｯ疲ｹｿ險育ｮ�
    ! Calculate saturation specific humidity
    !
    xyz_QVapSat = xyz_CalcQVapSat( xyz_Temp, xyz_Press )

    ! Preparation for temperature in clear and cloudy regions
    !
    xyz_TempCler = xyz_Temp
    xyz_TempCldy = xyz_Temp

    ! Calculation of cloud fraction, specific humidities in clear and cloudy regions
    !
    xyz_QVapHalRan = Gamma * xyz_QVap
    xyz_CldFrac  = ( xyz_QVap + xyz_QVapHalRan - xyz_QVapSat ) &
      & / ( 2.0_DP * xyz_QVapHalRan + 1.0e-100_DP )
    xyz_QVapCler = ( xyz_QVapSat + xyz_QVap - xyz_QVapHalRan ) / 2.0_DP
    xyz_QVapCldy = ( xyz_QVapSat + xyz_QVap + xyz_QVapHalRan ) / 2.0_DP
    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1
          if ( xyz_CldFrac(i,j,k) >= 1.0_DP ) then
            xyz_QVapCler(i,j,k) = 0.0_DP
            xyz_QVapCldy(i,j,k) = xyz_QVap(i,j,k)
          else if ( xyz_CldFrac(i,j,k) <= 0.0_DP ) then
            xyz_QVapCler(i,j,k) = xyz_QVap(i,j,k)
            xyz_QVapCldy(i,j,k) = 0.0_DP
          end if
        end do
      end do
    end do
    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1
          if ( xyz_CldFrac(i,j,k) > 1.0_DP ) then
            xyz_CldFrac(i,j,k) = 1.0_DP
          else if ( xyz_CldFrac(i,j,k) < 0.0_DP ) then
            xyz_CldFrac(i,j,k) = 0.0_DP
          end if
        end do
      end do
    end do

    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1

!!$          if ( ( xyz_QVap(i,j,k) / xyz_QVapSat(i,j,k) ) >= CrtlRH ) then
          if ( xyz_CldFrac(i,j,k) > 0.0_DP ) then
            xyz_FlagSaturated(i,j,k) = .true.
          else
            xyz_FlagSaturated(i,j,k) = .false.
          end if

        end do
      end do
    end do

    do itr = 1, ItrtMax

      ! 鬟ｽ蜥梧ｯ疲ｹｿ險育ｮ�
      ! Calculate saturation specific humidity
      !
      xyz_QVapSat       = xyz_CalcQVapSat      ( xyz_TempCldy, xyz_Press   )
      xyz_DQVapSatDTemp = xyz_CalcDQVapSatDTemp( xyz_TempCldy, xyz_QVapSat )

      do k = 1, kmax
        do j = 1, jmax
          do i = 0, imax-1

            ! 鬟ｽ蜥後＠縺ｦ縺�◆繧�, 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ縺ｮ螟牙喧繧定ｨ育ｮ�
            ! Calculate tendency of temperature and humidity
            ! if moist is saturation.
            !
            if ( xyz_FlagSaturated(i,j,k) ) then

              ! 貂ｩ蠎ｦ縺ｮ螟牙喧蛻�ｒ繝九Η繝ｼ繝医Φ豕輔〒豎ゅａ繧�
              ! Calculate variation of temperature
              !
              DelTemp = &
                & LatentHeat * ( xyz_QVapCldy(i,j,k) - xyz_QVapSat(i,j,k) ) &
                &    / ( CpDry + LatentHeat * xyz_DQVapSatDTemp(i,j,k) )

              !=========
              ! check routine
              !---------
!!$              TempBefAdj = xyz_TempCldy(i,j,k)
!!$              QVapBefAdj = xyz_QVapCldy(i,j,k)
              !=========

              ! 貂ｩ蠎ｦ縺ｨ豈疲ｹｿ縺ｮ隱ｿ遽
              ! Adjust temperature and specific humidity
              !
              xyz_TempCldy(i,j,k) = xyz_TempCldy(i,j,k) &
                & + DelTemp
              xyz_QVapCldy(i,j,k) = xyz_QVapSat(i,j,k)  &
                & + xyz_DQVapSatDTemp(i,j,k) * DelTemp

              !=========
              ! check routine
              !---------
!!$              write( 6, * ) '====='
!!$              write( 6, * ) 'Energy difference before and after adjustment and each energy', xyz_CldFrac(i,j,k)
!!$              write( 6, * ) &
!!$                & (     CpDry*TempBefAdj          + LatentHeat*QVapBefAdj              &
!!$                &   - ( CpDry*xyz_TempCldy(i,j,k) + LatentHeat*xyz_QVapCldy(i,j,k) ) ) &
!!$                & /(    CpDry*TempBefAdj          + LatentHeat*QVapBefAdj ),           &
!!$                &     CpDry*TempBefAdj          + LatentHeat*QVapBefAdj,               &
!!$                &   ( CpDry*xyz_TempCldy(i,j,k) + LatentHeat*xyz_QVapCldy(i,j,k) )
              !=========

            end if

          end do
        end do
      end do

    end do

    ! Calculation of temperature and specific humidity averaged over a grid
    !
    xyz_Temp = xyz_CldFrac * xyz_TempCldy + ( 1.0_DP - xyz_CldFrac ) * xyz_TempCler
    xyz_QVap = xyz_CldFrac * xyz_QVapCldy + ( 1.0_DP - xyz_CldFrac ) * xyz_QVapCler

    ! 豈疲ｹｿ螟牙喧邇�, 貂ｩ蠎ｦ螟牙喧邇�, 髯肴ｰｴ驥上�邂怜�
    ! Calculate specific humidity tendency, temperature tendency,
    ! precipitation
    !
    xyz_DQVapDtLsc = ( xyz_QVap - xyz_QVapB ) / ( 2.0_DP * DelTime )
    xyz_DTempDtLsc = ( xyz_Temp - xyz_TempB ) / ( 2.0_DP * DelTime )

    xyz_DTempDt = xyz_DTempDt + xyz_DTempDtLsc
    xyz_DQVapDt = xyz_DQVapDt + xyz_DQVapDtLsc

!!$    xy_RainLsc     = 0.0d0
!!$    do k = kmax, 1, -1
!!$      xy_RainLsc = xy_RainLsc                                     &
!!$        & + ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav      &
!!$        & * ( xyz_QVapB(:,:,k  ) - xyz_QVap (:,:,k) )
!!$    end do
!!$    xy_RainLsc = xy_RainLsc / ( 2.0_DP * DelTime )

!!$    i = 0
!!$    j = jmax/2+1
!!$    write( 6, * ) xy_RainLsc(i,j)

!!$    xyz_DQH2OLiqDt = - xyz_DQVapDtLsc
!!$    xy_RainLsc     = 0.0d0
!!$    do k = kmax, 1, -1
!!$      xy_RainLsc = xy_RainLsc                                     &
!!$        & + xyz_DQH2OLiqDt(:,:,k) * ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav
!!$    end do

!!$    i = 0
!!$    j = jmax/2+1
!!$    write( 6, * ) xy_RainLsc(i,j)
!!$    write( 6, * ) '---'

!!$    xy_Rain     = xy_Rain     + xy_RainLsc

    xyz_DQH2OLiqDt = - xyz_DQVapDtLsc

    ! calculation for output
    xy_RainLsc     = 0.0_DP
    do k = kmax, 1, -1
      xy_RainLsc = xy_RainLsc                                     &
        & + xyz_DQH2OLiqDt(:,:,k) * ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav
    end do

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    call HistoryAutoPut( TimeN, 'RainLsc',    xy_RainLsc * LatentHeat )
    call HistoryAutoPut( TimeN, 'DTempDtLsc', xyz_DTempDtLsc )
    call HistoryAutoPut( TimeN, 'DQVapDtLsc', xyz_DQVapDtLsc )

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine LScaleCondLL91

  !--------------------------------------------------------------------------------------

  subroutine LScaleCondLL91Init
    !
    ! lscond 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#lscond_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "lscond" module is initialized.
    ! "NAMELIST#lscond_nml" is loaded in this procedure.
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid

    ! 繝輔ぃ繧､繝ｫ蜈･蜃ｺ蜉幄｣懷勧
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 讓呎ｺ門�蜉帙�陬�ｽｮ逡ｪ蜿ｷ. Unit number of standard output

    ! 譁�ｭ怜�謫堺ｽ�
    ! Character handling
    !
    use dc_string, only: StoA

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    integer:: unit_nml        ! NAMELIST 繝輔ぃ繧､繝ｫ繧ｪ繝ｼ繝励Φ逕ｨ陬�ｽｮ逡ｪ蜿ｷ.
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 隱ｭ縺ｿ霎ｼ縺ｿ譎ゅ� IOSTAT.
                              ! IOSTAT of NAMELIST read

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
    namelist /lscond_nml/ &
!!$      & CrtlRH, ItrtMax, FlagUse
      & Gamma, ItrtMax
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "lscond#LSCondInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "lscond#LSCondInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( lscond_LL91_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
!!$    CrtlRH  = 1.0_DP
    Gamma   = 0.2_DP
    !  This value is from Le Treut and Li (1991).
    ItrtMax = 3
!!$    FlagUse = .true.

    ! NAMELIST 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ
    ! NAMELIST is input
    !
    if ( trim(namelist_filename) /= '' ) then
      call FileOpen( unit_nml, &          ! (out)
        & namelist_filename, mode = 'r' ) ! (in)

      rewind( unit_nml )
      read( unit_nml, &         ! (in)
        & nml = lscond_nml, &   ! (out)
        & iostat = iostat_nml ) ! (out)
      close( unit_nml )

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
      if ( iostat_nml == 0 ) write( STDOUT, nml = lscond_nml )
    end if

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉帙�縺溘ａ縺ｮ縺ｸ縺ｮ螟画焚逋ｻ骭ｲ
    ! Register of variables for history data output
    !
    call HistoryAutoAddVariable( 'RainLsc', &
      & (/ 'lon ', 'lat ', 'time' /), &
      & 'precipitation by large scale condensation', 'W m-2' )
    call HistoryAutoAddVariable( 'DTempDtLsc', &
      & (/ 'lon ', 'lat ', 'sig ', 'time' /), &
      & 'large-scale condensation heating', 'K s-1' )
    call HistoryAutoAddVariable( 'DQVapDtLsc', &
      & (/ 'lon ', 'lat ', 'sig ', 'time' /), &
      & 'large-scale condensation moistening', 'kg kg-1 s-1' )

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
!!$    call MessageNotify( 'M', module_name, '  FlagUse = %b', l = (/ FlagUse /) )
!!$    call MessageNotify( 'M', module_name, '  CrtlRH  = %f', d = (/ CrtlRH /) )
    call MessageNotify( 'M', module_name, '  Gamma   = %f', d = (/ Gamma /) )
    call MessageNotify( 'M', module_name, '  ItrtMax = %d', i = (/ ItrtMax /) )
    call MessageNotify( 'M', module_name, '' )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    lscond_LL91_inited = .true.

  end subroutine LScaleCondLL91Init

  !--------------------------------------------------------------------------------------

end module lscond_LL91