!= 雲の設定
!
!= Set cloud
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: set_cloud.f90,v 1.8 2011-12-02 04:06:36 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module set_cloud
  !
  != 雲の設定
  !
  != Set cloud
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 雲の分布を設定.
  !
  ! In this module, the amount of cloud or cloud optical depth are set.
  ! This module is under development and is still a preliminary version. 
  !
  !== Procedures List
  !
!!$  ! RadiationFluxDennouAGCM :: 放射フラックスの計算
!!$  ! ------------            :: ------------
!!$  ! RadiationFluxDennouAGCM :: Calculate radiation flux
  !
  !== NAMELIST
  !
  ! NAMELIST#set_cloud_nml
  !

  ! モジュール引用 ; USE statements

  !
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! Double precision.
    &                 STRING, &  ! Strings.
    &                 TOKEN      ! Keywords.

  ! メッセージ出力
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 格子点設定
  ! 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

  implicit none

  private


  ! 公開手続き
  ! Public procedure
  !
  public :: SetCloudCloudWaterAdjust
  public :: SetCloudCalcCloudCover
  public :: SetCloudCalcOverlappedCloudTrans
!!$  public :: SetCloudSmearCloudOptDep
  public :: SetCloudLocalizeCloud
  public :: SetCloudDivideWatAndIce
  public :: SetCloudInit



  ! 公開変数
  ! Public variables
  !


  ! 非公開変数
  ! Private variables
  !
  logical , save        :: FlagCloud
                           ! A flag for cloud set.
                           ! If FlagCloud is true, the effect of cloud is considered.

  integer , save        :: IDCloudOverlapType
  integer , parameter   :: IDCloudOverlapTypeRandom     = 1
  integer , parameter   :: IDCloudOverlapTypeMaxOverlap = 2

  integer , save        :: IDCloudCoverMethod
  integer , parameter   :: IDCloudCoverMethodConst    = 1
  integer , parameter   :: IDCloudCoverMethodRH       = 2

  real(DP), save        :: CloudLifeTime

  real(DP), save        :: CloudCover

  real(DP), save        :: RHCrtl

  real(DP), save        :: TempWatLim
  real(DP), save        :: TempIceLim




  logical, save :: set_cloud_inited = .false.
                              ! 初期設定フラグ.
                              ! Initialization flag

  character(*), parameter:: module_name = 'set_cloud'
                              ! モジュールの名称.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: set_cloud.f90,v 1.8 2011-12-02 04:06:36 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version

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

contains

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

  subroutine SetCloudCloudWaterAdjust(              &
    & xyz_DQCloudWaterDtCum, xyz_DQCloudWaterDtLSC, & ! (in)
    & xyz_QCloudWater                               & ! (inout)
    & )

    ! USE statements
    !

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & DelTime            ! $ \Delta t $ [s]


    real(DP), intent(in   ) :: xyz_DQCloudWaterDtCum( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(in   ) :: xyz_DQCloudWaterDtLSC( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(inout) :: xyz_QCloudWater      ( 0:imax-1, 1:jmax, 1:kmax )


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

    ! 実行文 ; Executable statement
    !

    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_cloud_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    ! Cloud optical depth
    !
    if ( FlagCloud ) then

      ! save cloud water before adjustment
      xyz_QCloudWaterB = xyz_QCloudWater

!!$      xyz_DQCloudWaterDt = xyz_DQCloudWaterDt &
!!$        & - xyz_QCloudWater / ( CloudLifeTime + 1.0d-100 )


!!$      ( X_{t+1} - X_{t-1} ) / ( 2 \Delta t ) = Q - X_{t+1} / \tau
!!$
!!$      X_{t+1} / ( 2 \Delta t )  + X_{t+1} / \tau = X_{t-1} / ( 2 \Delta t ) + Q
!!$      ( 1 / ( 2 \Delta t )  + 1 / \tau ) X_{t+1} = X_{t-1} / ( 2 \Delta t ) + Q
!!$      X_{t+1} = ( X_{t-1} / ( 2 \Delta t ) + Q ) / ( 1 / ( 2 \Delta t )  + 1 / \tau ) 

      xyz_QCloudWater =                                                               &
        &   (                                                                         &
        &       xyz_QCloudWater / ( 2.0_DP * DelTime )                                &
        &     + xyz_DQCloudWaterDtCum + xyz_DQCloudWaterDtLSC                         &
        &   )                                                                         &
        & / ( 1.0_DP / ( 2.0_DP * DelTime ) + 1.0_DP / ( CloudLifeTime + 1.0d-100 ) )


!!$      do k = 1, kmax
!!$        xyz_Rain(:,:,k) =                                                       &
!!$          & (                                                                   &
!!$          &     xyz_QCloudWaterB(:,:,k)                                         &
!!$          &   + ( xyz_DQCloudWaterDtCum(:,:,k) + xyz_DQCloudWaterDtLSC(:,:,k) ) &
!!$          &     * ( 2.0_DP * DelTime )                                          &
!!$          &   - xyz_QCloudWater(:,:,k)                                          &
!!$          & )                                                                   &
!!$          & * ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav
!!$      end do
!!$      xy_Rain = 0.0_DP
!!$      do k = kmax, 1, -1
!!$        xy_Rain = xy_Rain + xyz_Rain(:,:,k)
!!$      end do
!!$      xy_Rain = xy_Rain / ( 2.0_DP * DelTime )


    end if


  end subroutine SetCloudCloudWaterAdjust

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

  subroutine SetCloudCalcCloudCover(     &
    & xyz_Press, xyz_Temp, xyz_QH2OVap,  & ! (in )
    & xyz_CloudCover                     & ! (out)
    & )

    ! USE statements
    !

    ! 飽和比湿の算出
    ! Evaluate saturation specific humidity
    !
    use saturate, only: xyz_CalcQVapSat

    real(DP), intent(in ) :: xyz_Press     ( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(in ) :: xyz_Temp      ( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(in ) :: xyz_QH2OVap   ( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(out) :: xyz_CloudCover( 0:imax-1, 1:jmax, 1:kmax )


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


    ! 実行文 ; Executable statement
    !

    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_cloud_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    ! Cloud optical depth
    !
    if ( .not. FlagCloud ) then

      xyz_CloudCover = 0.0_DP

    else

      select case ( IDCloudCoverMethod )
      case ( IDCloudCoverMethodConst )

        xyz_CloudCover = CloudCover

      case ( IDCloudCoverMethodRH )

        ! see Sundqvist et al. (1989), Del Genio et al. (1996)
        xyz_RH = xyz_QH2OVap / xyz_CalcQVapSat( xyz_Temp, xyz_Press )
        xyz_RH = min( xyz_RH, 1.0_DP )

        xyz_CloudCover = 1.0d0 - sqrt( ( 1.0d0 - xyz_RH ) / ( 1.0d0 - RHCrtl ) )

        xyz_CloudCover = max( xyz_CloudCover, 0.0_DP )
        xyz_CloudCover = min( xyz_CloudCover, 1.0_DP )

      end select

    end if


  end subroutine SetCloudCalcCloudCover

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

  subroutine SetCloudCalcOverlappedCloudTrans(  &
    & xyz_TransCloudOneLayer, xyz_CloudCover,   & ! (in)
    & xyrr_OverlappedCloudTrans                 & ! (out)
    & )

    ! USE statements
    !

    ! ヒストリデータ出力
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoPut

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! ステップ $ t $ の時刻.
                              ! Time of step $ t $.
      & EndTime, &            ! 計算終了時刻.
                              ! End time of calculation
      & TimesetClockStart, TimesetClockStop

    use sort, only : SortQuick

    real(DP), intent(in ) :: xyz_TransCloudOneLayer   (0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(in ) :: xyz_CloudCover           (0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(out) :: xyrr_OverlappedCloudTrans(0:imax-1, 1:jmax, 0:kmax, 0:kmax)


    real(DP) :: xyz_EffCloudCover(0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_CloudCoverSorted        (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_EffCloudCoverSorted     (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_TransCloudOneLayerSorted(0:imax-1, 1:jmax, 1:kmax)
    integer  :: k
    integer  :: kk
    integer  :: kkk


    ! 実行文 ; Executable statement
    !

    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_cloud_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    ! Cloud optical depth
    !
    if ( .not. FlagCloud ) then

      xyrr_OverlappedCloudTrans = 1.0_DP

    else

      select case ( IDCloudOverlapType )

        ! This is old version to be deleted. 

!!$      case ( IDCloudOverlapTypeFillGrid )
!!$        ! Cloud fills a grid box.
!!$
!!$        do k = 0, kmax
!!$          kk = k
!!$          xyrr_OverlappedCloudTrans(:,:,k,kk) = 1.0d0
!!$          do kk = k+1, kmax
!!$            xyrr_OverlappedCloudTrans(:,:,k,kk) = xyrr_OverlappedCloudTrans(:,:,k,kk-1) &
!!$              & * xyz_TransCloudOneLayer(:,:,kk)
!!$          end do
!!$        end do
!!$        do k = 0, kmax
!!$          do kk = 0, k-1
!!$            xyrr_OverlappedCloudTrans(:,:,k,kk) = xyrr_OverlappedCloudTrans(:,:,kk,k)
!!$          end do
!!$        end do

      case ( IDCloudOverlapTypeRandom )

        xyz_EffCloudCover = xyz_CloudCover * ( 1.0_DP - xyz_TransCloudOneLayer )

        do k = 0, kmax
          kk = k
          xyrr_OverlappedCloudTrans(:,:,k,kk) = 1.0_DP
          do kk = k+1, kmax
            xyrr_OverlappedCloudTrans(:,:,k,kk) =        &
              & xyrr_OverlappedCloudTrans(:,:,k,kk-1)    &
              & * ( 1.0_DP - xyz_EffCloudCover(:,:,kk) )
          end do
        end do

        do k = 0, kmax
          do kk = 0, k-1
            xyrr_OverlappedCloudTrans(:,:,k,kk) = xyrr_OverlappedCloudTrans(:,:,kk,k)
          end do
        end do

      case ( IDCloudOverlapTypeMaxOverlap )

!!$        call MessageNotify( 'E', module_name, 'This has not been implemented, yet.' )


        ! see Chou et al. (2001)

        xyz_EffCloudCover = xyz_CloudCover * ( 1.0_DP - xyz_TransCloudOneLayer )


        do k = 0, kmax
          kk = k
          xyrr_OverlappedCloudTrans(:,:,k,kk) = 1.0_DP
          do kk = k+1, kmax

            xyz_CloudCoverSorted         = xyz_CloudCover
            xyz_EffCloudCoverSorted      = xyz_EffCloudCover
            xyz_TransCloudOneLayerSorted = xyz_TransCloudOneLayer

            call SortQuick( imax, jmax, kk-k,             &
              & xyz_CloudCoverSorted        (:,:,k+1:kk), &
              & xyz_EffCloudCoverSorted     (:,:,k+1:kk), &
              & xyz_TransCloudOneLayerSorted(:,:,k+1:kk)  &
              & )

            xyrr_OverlappedCloudTrans(:,:,k,kk) = 0.0_DP
            do kkk = k+1, kk
              xyrr_OverlappedCloudTrans(:,:,k,kk) =         &
                & xyz_EffCloudCoverSorted(:,:,kkk)          &
                & + xyrr_OverlappedCloudTrans(:,:,k,kk)     &
                &   * xyz_TransCloudOneLayerSorted(:,:,kkk)
            end do
            xyrr_OverlappedCloudTrans(:,:,k,kk) = &
              & 1.0_DP - xyrr_OverlappedCloudTrans(:,:,k,kk)

          end do
        end do
        do k = 0, kmax
          do kk = 0, k-1
            xyrr_OverlappedCloudTrans(:,:,k,kk) = xyrr_OverlappedCloudTrans(:,:,kk,k)
          end do
        end do


      end select

    end if


    ! Output effective cloud cover
    !
!!$    call HistoryAutoPut( TimeN, 'EffCloudCover', &
!!$      & 1.0_DP - xyrr_OverlappedCloudTrans(:,:,0,kmax) )


  end subroutine SetCloudCalcOverlappedCloudTrans

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

  subroutine SetCloudSmearCloudOptDep(  &
    & xyz_CloudCover,                   & ! (in   )
    & xyz_DelCloudOptDep                & ! (inout)
    & )

    ! USE statements
    !

    real(DP), intent(in   ) :: xyz_CloudCover    (0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(inout) :: xyz_DelCloudOptDep(0:imax-1, 1:jmax, 1:kmax)


    ! 実行文 ; Executable statement
    !

    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_cloud_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    ! Cloud optical depth is scaled by the way of Kiehl et al. (1994).

    xyz_DelCloudOptDep = xyz_DelCloudOptDep * xyz_CloudCover**1.5_DP


  end subroutine SetCloudSmearCloudOptDep

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

  subroutine SetCloudLocalizeCloud(  &
    & xyz_CloudCover,                & ! (in   )
    & xyz_DelCloudOptDep             & ! (inout)
    & )

    ! USE statements
    !

    real(DP), intent(in   ) :: xyz_CloudCover    (0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(inout) :: xyz_DelCloudOptDep(0:imax-1, 1:jmax, 1:kmax)


    ! 実行文 ; Executable statement
    !

    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_cloud_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    ! Cloud optical depth is scaled by considering cloud cover less than 1. 

    xyz_DelCloudOptDep = xyz_DelCloudOptDep / max( xyz_CloudCover, 1.0d-3 )


  end subroutine SetCloudLocalizeCloud

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

  subroutine SetCloudDivideWatAndIce(  &
    & xyz_Temp,                        & ! (in )
    & xyz_QH2OWatAndIce,               & ! (in )
    & xyz_QH2OWat, xyz_QH2OIce         & ! (out)
    & )

    ! USE statements
    !

    real(DP), intent(in ) :: xyz_Temp         (0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(in ) :: xyz_QH2OWatAndIce(0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(out) :: xyz_QH2OWat      (0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(out) :: xyz_QH2OIce      (0:imax-1, 1:jmax, 1:kmax)


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

    ! 実行文 ; Executable statement
    !

    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_cloud_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    xyz_WatFrac = ( 1.0_DP - 0.0_DP ) / ( TempWatLim - TempIceLim ) &
      & * ( xyz_Temp - TempIceLim )
    xyz_WatFrac = min( xyz_WatFrac, 1.0_DP )
    xyz_WatFrac = max( xyz_WatFrac, 0.0_DP )

    xyz_QH2OWat = xyz_QH2OWatAndIce * xyz_WatFrac
    xyz_QH2OIce = xyz_QH2OWatAndIce * ( 1.0_DP - xyz_WatFrac )


  end subroutine SetCloudDivideWatAndIce

  !--------------------------------------------------------------------------------------
!!$
!!$  subroutine SetCloudCalcLWP(                    &
!!$    & xyz_Press, xyr_Press, xyz_Temp, xyz_QVap, xyz_Height, &
!!$    & xyz_DelLWP   &
!!$    & )
!!$
!!$    ! 物理定数設定
!!$    ! Physical constants settings
!!$    !
!!$    use constants, only: &
!!$      & Grav, &
!!$                              ! $ g $ [m s-2].
!!$                              ! 重力加速度.
!!$                              ! Gravitational acceleration
!!$      & GasRDry
!!$                              ! $ R $ [J kg-1 K-1].
!!$                              ! 乾燥大気の気体定数.
!!$                              ! Gas constant of air
!!$
!!$    ! ヒストリデータ出力
!!$    ! History data output
!!$    !
!!$    use gtool_historyauto, only: HistoryAutoPut
!!$
!!$    ! 時刻管理
!!$    ! Time control
!!$    !
!!$    use timeset, only: &
!!$      & TimeN, &              ! ステップ $ t $ の時刻.
!!$                              ! Time of step $ t $.
!!$      & EndTime, &            ! 計算終了時刻.
!!$                              ! End time of calculation
!!$      & TimesetClockStart, TimesetClockStop
!!$
!!$    ! 飽和比湿の算出
!!$    ! Evaluate saturation specific humidity
!!$    !
!!$    use saturate, only: xyz_CalcQVapSat
!!$
!!$
!!$    real(DP), intent(in ) :: xyz_Press ( 0:imax-1, 1:jmax, 1:kmax )
!!$    real(DP), intent(in ) :: xyr_Press ( 0:imax-1, 1:jmax, 0:kmax )
!!$    real(DP), intent(in ) :: xyz_Temp  ( 0:imax-1, 1:jmax, 1:kmax )
!!$    real(DP), intent(in ) :: xyz_QVap  ( 0:imax-1, 1:jmax, 1:kmax )
!!$    real(DP), intent(in ) :: xyz_Height( 0:imax-1, 1:jmax, 1:kmax )
!!$    real(DP), intent(out) :: xyz_DelLWP( 0:imax-1, 1:jmax, 1:kmax )
!!$
!!$
!!$    integer  :: xy_NumCloudLayer      ( 0:imax-1, 1:jmax )
!!$    real(DP) :: xy_CloudCoverTot      ( 0:imax-1, 1:jmax )
!!$    real(DP) :: xy_CloudCoverEachLayer( 0:imax-1, 1:jmax )
!!$    real(DP) :: xyz_CloudCover        ( 0:imax-1, 1:jmax, 1:kmax )
!!$
!!$    real(DP):: xyz_QVapSat            ( 0:imax-1, 1:jmax, 1:kmax )
!!$                              ! 飽和比湿.
!!$                              ! Saturation specific humidity.
!!$
!!$    real(DP):: xyz_RH                 ( 0:imax-1, 1:jmax, 1:kmax )
!!$    real(DP):: xyz_RHCor              ( 0:imax-1, 1:jmax, 1:kmax )
!!$
!!$    real(DP), parameter :: CloudLiqWatDen0 = 0.18d-3
!!$    real(DP)            :: xy_ColWatVapor           ( 0:imax-1, 1:jmax )
!!$    real(DP)            :: xy_CloudLiqWatScaleHeight( 0:imax-1, 1:jmax )
!!$    real(DP)            :: xyz_CloudLiqWatDen       ( 0:imax-1, 1:jmax, 1:kmax )
!!$    real(DP)            :: xyz_DelCloudLiqWat       ( 0:imax-1, 1:jmax, 1:kmax )
!!$
!!$    integer  :: i
!!$    integer  :: j
!!$    integer  :: k
!!$
!!$
!!$    ! 初期化
!!$    ! Initialization
!!$    !
!!$    if ( .not. set_cloud_inited ) call SetCloudInit
!!$
!!$
!!$    ! 飽和比湿計算
!!$    ! Calculate saturation specific humidity
!!$    !
!!$    xyz_QVapSat = xyz_CalcQVapSat( xyz_Temp, xyz_Press )
!!$
!!$    xyz_RH = xyz_QVap / xyz_QVapSat
!!$
!!$
!!$    xy_NumCloudLayer = 0
!!$    do k = 1, kmax
!!$      do j = 1, jmax
!!$        do i = 0, imax-1
!!$          if ( xyz_DTempDtCumSave(i,j,k) > 0.0d0 ) then
!!$            xy_NumCloudLayer = xy_NumCloudLayer + 1
!!$          end if
!!$        end do
!!$      end do
!!$    end do
!!$
!!$    xy_CloudCoverTot = 0.0d0
!!$    do j = 1, jmax
!!$      do i = 0, imax-1
!!$        if ( xy_PRCPSave(i,j) >= 0.0d0 ) then
!!$          xy_CloudCoverTot(i,j) = 0.20d0 &
!!$            & + 0.125d0 * log( 1.0d0 + xy_PRCPSave(i,j) * 86400.0d0 / 1.0d3 * 1.0d3 )
!!$        end if
!!$      end do
!!$    end do
!!$    do j = 1, jmax
!!$      do i = 0, imax-1
!!$        if ( xy_CloudCoverTot(i,j) > 0.8d0 ) then
!!$          xy_CloudCoverTot(i,j) = 0.8d0
!!$        end if
!!$      end do
!!$    end do
!!$    do j = 1, jmax
!!$      do i = 0, imax-1
!!$        if ( xy_NumCloudLayer(i,j) == 0 ) then
!!$          xy_CloudCoverEachLayer(i,j) = 0.0d0
!!$        else
!!$          xy_CloudCoverEachLayer(i,j) = &
!!$            & 1.0d0 - ( 1.0d0 - xy_CloudCoverTot(i,j) )**(1.0d0/xy_NumCloudLayer(i,j))
!!$        end if
!!$      end do
!!$    end do
!!$
!!$    do k = 1, kmax
!!$      xyz_RHCor(:,:,k) =                               &
!!$        &   ( xyz_RH(:,:,k) - xy_CloudCoverEachLayer ) &
!!$        & / ( 1.0d0 - xy_CloudCoverEachLayer )
!!$    end do
!!$
!!$
!!$    xyz_CloudCover = 0.0d0
!!$
!!$!    ! Frontal and tropical low-cloud
!!$!    !
!!$!    do k = 1, kmax
!!$!      do j = 1, jmax
!!$!        do i = 0, imax-1
!!$!          if ( xyz_Press(i,j,k) > 750.0d2 .and. xyz_DPDtSave(i,j,k) < 0.0d0 ) then
!!$!            xyz_CloudCover(i,j,k) = ( ( xyz_RHCor(i,j,k) - 0.9d0 ) / 0.1d0 )**2
!!$!          end if
!!$!        end do
!!$!      end do
!!$!    end do
!!$
!!$
!!$    do k = 1, kmax
!!$      do j = 1, jmax
!!$        do i = 0, imax-1
!!$          if ( ( xyz_RHCor(i,j,k) > 0.6d0 ) .and. &
!!$            &  ( xyz_DTempDtCumSave(i,j,k) > 0.0d0 ) ) then
!!$            xyz_CloudCover(i,j,k) = xy_CloudCoverEachLayer(i,j)
!!$          else
!!$            xyz_CloudCover(i,j,k) = 0.0d0
!!$          end if
!!$        end do
!!$      end do
!!$    end do
!!$
!!$
!!$
!!$
!!$    xy_ColWatVapor = 0.0d0
!!$    do k = 1, kmax
!!$      do j = 1, jmax
!!$        do i = 0, imax-1
!!$          xy_ColWatVapor = xy_ColWatVapor &
!!$            & + xyz_QVap(:,:,k) * ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav
!!$        end do
!!$      end do
!!$    end do
!!$
!!$    xy_CloudLiqWatScaleHeight = &
!!$      &   700.0d0 * log( 1.0d0 + 1.0d0 * xy_ColWatVapor ) &
!!$      & + 1.0d0
!!$
!!$    do k = 1, kmax
!!$      xyz_CloudLiqWatDen(:,:,k) = &
!!$        & CloudLiqWatDen0 * exp( - xyz_Height(:,:,k) / xy_CloudLiqWatScaleHeight )
!!$    end do
!!$
!!$
!!$    do k = 1, kmax
!!$      xyz_DelCloudLiqWat(:,:,k) = xyz_CloudLiqWatDen(:,:,k)              &
!!$        & * ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) )                    &
!!$        & / ( xyz_Press(:,:,k) / ( GasRDry * xyz_Temp(:,:,k) ) * Grav )
!!$    end do
!!$
!!$    xyz_DelLWP = xyz_CloudCover * xyz_DelCloudLiqWat
!!$
!!$
!!$    call HistoryAutoPut( TimeN, 'CloudCover', xyz_CloudCover )
!!$
!!$
!!$  end subroutine SetCloudCalcLWP
!!$
  !--------------------------------------------------------------------------------------

  subroutine SetCloudInit

    ! ファイル入出力補助
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! NAMELIST ファイル入力に関するユーティリティ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid

    ! ヒストリデータ出力
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable


    ! 宣言文 ; Declaration statements
    !
    character(STRING) :: CloudOverlapType
    character(STRING) :: CloudCoverMethod

    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号.
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT.
                              ! IOSTAT of NAMELIST read

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
    namelist /set_cloud_nml/ &
      & FlagCloud,           &
      & CloudLifeTime,       &
      & CloudOverlapType,    &
      & CloudCoverMethod,    &
      & RHCrtl,              &
      & CloudCover,          &
      & TempWatLim,          &
      & TempIceLim
          !
          ! デフォルト値については初期化手続 "set_cloud#setCloudInit"
          ! のソースコードを参照のこと.
          !
          ! Refer to source codes in the initialization procedure
          ! "set_cloud#SetCloudInit" for the default values.
          !

    ! 実行文 ; Executable statement
    !

    if ( set_cloud_inited ) return


    ! デフォルト値の設定
    ! Default values settings
    !

    FlagCloud           = .true.

    CloudLifeTime       = 3600.0_DP

    CloudOverlapType    = "Random"
!!$    CloudOverlapType    = "MaxOverlap"

    CloudCoverMethod    = 'Const'
!!$    CloudCoverMethod    = 'RH'

    RHCrtl              = 0.8_DP

    CloudCover          = 1.0_DP

    TempWatLim          = 273.15_DP
    TempIceLim          = 273.15_DP - 40.0_DP


    ! 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 = set_cloud_nml, & ! (out)
        & iostat = iostat_nml )             ! (out)
      close( unit_nml )

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
    end if


    select case ( CloudOverlapType )
    case ( 'Random' )
      IDCloudOverlapType = IDCloudOverlapTypeRandom
    case ( 'MaxOverlap' )
      IDCloudOverlapType = IDCloudOverlapTypeMaxOverlap
    case default
      call MessageNotify( 'E', module_name,         &
        & 'CloudOverlapType=<%c> is not supported.', &
        & c1 = trim(CloudOverlapType) )
    end select

    select case ( CloudCoverMethod )
    case ( 'Const' )
      IDCloudCoverMethod = IDCloudCoverMethodConst
    case ( 'RH' )
      IDCloudCoverMethod = IDCloudCoverMethodRH
    case default
      call MessageNotify( 'E', module_name,         &
        & 'CloudCoverMethod=<%c> is not supported.', &
        & c1 = trim(CloudCoverMethod) )
    end select


    ! ヒストリデータ出力のためのへの変数登録
    ! Register of variables for history data output
    !
!!$    call HistoryAutoAddVariable( 'EffCloudCover', &
!!$      & (/ 'lon ', 'lat ', 'time' /), &
!!$      & 'effective cloud cover', '1' )



    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, 'FlagCloud           = %b', l = (/ FlagCloud /) )
    call MessageNotify( 'M', module_name, 'CloudLifeTime       = %f', d = (/ CloudLifeTime /) )
    call MessageNotify( 'M', module_name, 'CloudOverlapType    = %c', c1 = trim(CloudOverlapType) )
    call MessageNotify( 'M', module_name, 'CloudCoverMethod    = %c', c1 = trim(CloudCoverMethod) )
    call MessageNotify( 'M', module_name, 'RHCrtl              = %f', d = (/ RHCrtl /) )
    call MessageNotify( 'M', module_name, 'CloudCover          = %f', d = (/ CloudCover /) )
    call MessageNotify( 'M', module_name, 'TempWatLim          = %f', d = (/ TempWatLim /) )
    call MessageNotify( 'M', module_name, 'TempIceLim          = %f', d = (/ TempIceLim /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )


    set_cloud_inited = .true.

  end subroutine SetCloudInit

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

end module set_cloud
