!= deepconv/arare 湿潤大気対流計算用主プログラム (三次元版)
!
!= deepconv/arare main program for moist atmospheric convection (3D)
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: arare.f90,v 1.1 2011-04-26 04:08:10 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!

program deepconv_arare
  !
  ! 非静力学モデル deepconv/arare 湿潤大気対流計算用主プログラム (三次元版)
  !

  ! モジュール引用  use statement 
  !

  ! gtool5 関連 
  ! gtool5 modules
  !
  use dc_types,      only: STRING, DP
  use dc_message,    only: MessageNotify
  use gtool_historyauto, only: HistoryAutoPut

  ! 初期設定モジュール
  ! Initialize module
  !
  use mpi_wrapper,   only : MPIWrapperInit, MPIWrapperFinalize
  use argset,   only: argset_init
  use gridset,  only: gridset_init, imin, imax, jmin, jmax, kmin, kmax, nf
  use timeset,  only: timeset_init, DelTimeLong, DelTimeShort, EndTime, StartTime, &
    &                 NstepLong, NstepShort, NstepDisp
  use axesset,  only: axesset_init, x_X, y_Y, z_Z, xyz_avr_pqz, xyz_avr_xqz, xyz_avr_xyr
  use constants,only: constants_init, SpcWetSymbol
  use moistset, only: moistset_init
  use fileset,  only: fileset_init, InputFile
  use basicset, only: xyza_MixRtBasicZ, xyz_DensBasicZ, xyz_EffMolWtBasicZ, &
    &                 xyz_PotTempBasicZ, xyz_TempBasicZ, xyz_PressBasicZ, &
    &                 xyz_VelSoundBasicZ, xyz_ExnerBasicZ 
  use ChemCalc, only: ChemCalc_init

  ! 力学過程計算用関数モジュール
  ! Dynamical processes module
  !
  use dynamics_exner, only:  dynamic_exner_implicit_init, &
    &                       calc_dynamic_exner_implicit
  use dynamics_pottemp, only: dynamic_pottemp_init, &
    &                        Tendency_Dynamic_PotTemp
  use dynamics_mixrt,only: 
  use dynamics_velx, only: 
  use dynamics_vely, only: 
  use dynamics_velz, only: 

  ! 乱流拡散計算用モジュール
  ! Turbulent diffusion module
  !
  use Turbulence_kw1978,  only: Turbulence_Init, KmMax, Tendency_Turbulence_KW1978, &
    &                           Steady_Turbulence_KW1978

  ! 境界からのフラックス計算用モジュール
  ! Surface flux module
  !
!  use HeatFlux,    only: xyz_HeatFluxBulk, pyz_MomFluxBulk, xqz_MomFluxBulk

  ! 放射強制計算用モジュール
  ! Radiative forceing module
  !
!  use Radiation,   only: Radiation_init, xyz_RadHeatConst, xyz_NewtonCool

  ! 湿潤過程計算用モジュール
  ! Moist processes modules
  !
! use MoistAdjust, only: MoistAdjustSvapPress, MoistAdjustNH4SH
!  use WarmRainPrm, only: WarmRainPrm_Init, xyz_Rain2GasHeat, xyza_Rain2Gas,&
!    &                       xyza_Rain2GasNH4SH, xyz_Rain2GasHeatNH4SH, &
!    &                       xyza_Cloud2Rain, xyza_FallRain
!  use MoistBuoyancy,only: MoistBuoy_Init, xyz_BuoyMoistKm, xyr_BuoyMolWt, &
!    &                        xyr_BuoyDrag
  use fillnegative,only: FillNegative_init, xyza_FillNegative_xyza

  ! 数値拡散/摩擦計算用モジュール
  ! Numerical diffussion /dumping module
  !
  use damping,     only: damping_init, & 
    &                       DampSponge_xyz, DampSponge_xyr, DampSponge_pyz,DampSponge_xqz

  ! ファイル入出力モジュール
  ! File I/O module
  !
  use RestartFileIO, only : ReStartFile_Open, ReStartFile_OutPut, &
    &                          ReStartFile_Close, ReStartFile_Get
  use BasicZFileIO, only: BasicZFileOutput
  use HistoryAutoFileIO, only: HistoryFileAutoOpen, HistoryFileAutoClose

  ! 下請けモジュール
  ! Utility modules
  !
  use cflcheck,    only : CFLCheckTimeShort, &
    &                        CFLCheckTimeLongVelX, &
    &                        CFLCheckTimeLongVelY, &
    &                        CFLCheckTimeLongVelZ
  use timefilter,  only : AsselinFilter
  use xyz_bc_module,  only : BoundaryXCyc_xyz, BoundaryYCyc_xyz, &
    &                        BoundaryZSym_xyz, BoundaryZCyc_xyz, &
    &                        BoundaryXCyc_pyz, BoundaryYCyc_pyz, &
    &                        BoundaryZSym_pyz, BoundaryZCyc_pyz, &
    &                        BoundaryXCyc_xqz, BoundaryYCyc_xqz, &
    &                        BoundaryZSym_xqz, BoundaryZCyc_xqz, &
    &                        BoundaryXCyc_xyr, BoundaryYCyc_xyr, &
    &                        BoundaryZSym_xyr, BoundaryZCyc_xyr, &
    &                        BoundaryZAsym_xyr

  implicit none

  ! 内部変数
  ! Internal variables
  !
  character(80) :: cfgfile
                             ! NAMELIST ファイル名 ; NAMELIST fine name
  real(DP), allocatable :: pyz_VelXBl(:,:,:)    
                             ! $ u (t-\Delta t) $ 東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXNl(:,:,:)    
                             ! $ u (t) $          東西風 ; zonal wind
  real(DP), allocatable :: xyz_VelXNl(:,:,:)    
                             ! $ u (t) $          東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXAl(:,:,:)    
                             ! $ u (t+\Delta t) $ 東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXNs(:,:,:)    
                             ! $ u (\tau) $ 東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXAs(:,:,:)    
                             ! $ u (\tau +\Delta \tau) $ 東西風 ; zonal wind
  real(DP), allocatable :: xqz_VelYBl(:,:,:)    
                             ! $ v (t-\Delta t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYNl(:,:,:)    
                             ! $ v (t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xyz_VelYNl(:,:,:)    
                             ! $ v (t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYAl(:,:,:)    
                             ! $ v (t+\Delta t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYNs(:,:,:)   
                             ! $ v (\tau -\tau) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYAs(:,:,:)
                             ! $ v (\tau) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xyr_VelZBl(:,:,:)    
                             ! $ w (t-\Delta t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZNl(:,:,:)    
                             ! $ w (t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyz_VelZNl(:,:,:)    
                             ! $ w (t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZAl(:,:,:)    
                             ! $ w (t+\Delta t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZNs(:,:,:)    
                             ! $ w (\tau) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZAs(:,:,:) 
                             ! $ w (\tau +\Delta \tau)  鉛直風 ; vertical wind
  real(DP), allocatable :: xyz_ExnerBl(:,:,:)   
                             ! $ \pi (t-\Delta t) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerNl(:,:,:)   
                             ! $ \pi (t) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerAl(:,:,:)
                             ! $ \pi (t+\Delta t) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerNs(:,:,:)   
                             ! $ \pi (\tau -\Delta \tau) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerAs(:,:,:)   
                             ! $ \pi (\tau) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_PotTempBl(:,:,:) 
                             ! $ \theta (t-\Delta t) $ 温位 ; Potential temp.
  real(DP), allocatable :: xyz_PotTempNl(:,:,:) 
                             ! $ \theta (t) $ 温位 ; Potential temp.
  real(DP), allocatable :: xyz_PotTempAl(:,:,:) 
                             ! $ \theta (t+\Delta t) $ 温位 ; Potential temp.
  real(DP), allocatable :: xyz_PotTempWork(:,:,:) 
                             ! 温位 $ \theta $ の作業配列 ; Work array
  real(DP), allocatable :: xyz_KmBl(:,:,:)
                             ! $ Km (t-\Delta t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KmNl(:,:,:)
                             ! $ K_m (t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KmAl(:,:,:)
                             ! $ K_m (t+\Delta t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KhBl(:,:,:)      
                             ! $ K_h (t-\Delta t) $ 乱流拡散係数
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KhNl(:,:,:)
                             ! $ K_h (t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KhAl(:,:,:)
                             ! $ K_h (t+\Delta t) $ 乱流拡散係数
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyza_MixRtBl(:,:,:,:) 
                             ! $ q (t-\Delta t) $ 湿潤量の混合比
                             ! Mixing ratio of moist variables.
  real(DP), allocatable :: xyza_MixRtNl(:,:,:,:) 
                             ! $ q (t) $ 湿潤量の混合比 
                             ! Mixing ratio of moist variables
  real(DP), allocatable :: xyza_MixRtAl(:,:,:,:) ! 
                             ! $ q (t+\Delta t) $ 湿潤量の混合比 
                             !Mixing ratio of moist variables
  real(DP), allocatable :: xyza_MixRtWork(:,:,:,:)
                             ! 湿潤量の作業配列
                             ! Work array for mixing ratio.

  real(DP) :: Time           ! 時刻 ; Time 
  real(DP) :: ReStartTime(2) ! リスタートファイル出力時刻用配列
                             ! Output time array for restart file
  real(DP), allocatable :: DelTimeLFrog(:)      
                             ! リープフロッグスキーム用時間格子間隔
                             ! Time interval for Leap-frog scheme
  real(DP) :: DelTimeEuler   ! オイラースキーム用時間格子
                             ! Time interval for Eular scheme
  integer :: NStepLFrog      ! リープフロッグスキーム用時間ステップ数
                             ! The number of time step for Leap-frog scheme
  integer, allocatable :: NStepEuler(:) 
                             ! オイラースキーム用時間ステップ数
                             ! The number of time step for Eular scheme
  real(DP), allocatable :: pyz_VelXTendDynS(:,:,:)
  real(DP), allocatable :: pyz_VelXTendDynL(:,:,:)
  real(DP), allocatable :: pyz_VelXTendFlux(:,:,:)
  real(DP), allocatable :: xqz_VelYTendDynS(:,:,:)
  real(DP), allocatable :: xqz_VelYTendDynL(:,:,:)
  real(DP), allocatable :: xqz_VelYTendFlux(:,:,:)
  real(DP), allocatable :: xyr_VelZTendDynS(:,:,:)
  real(DP), allocatable :: xyr_VelZTendDynL(:,:,:)
  real(DP), allocatable :: xyz_KmTend(:,:,:)
  real(DP), allocatable :: xyz_PotTempTendDyn(:,:,:)
  real(DP), allocatable :: xyz_PotTempTendRad(:,:,:)
  real(DP), allocatable :: xyz_PotTempTendFlux(:,:,:)
  real(DP), allocatable :: xyza_MixRtTendDyn(:,:,:,:)
  real(DP), allocatable :: xyza_MixRtTendFlux(:,:,:,:)
  
  integer :: & 
    & t,     & 
    & tau,   & ! do ループ変数 ; do loop variable  
    & t1,    & ! do ループ変数 ; do loop variable  
    & t2,    & ! do ループ変数 ; do loop variable 
    & l        ! do ループ変数 ; do loop variable 

  logical           :: sw_NH4SH = .false.
  logical           :: sw_moist = .false.
  character(STRING) :: FlagTurbulence = ""
  character(STRING) :: FlagRadiation  = ""
  character(STRING) :: FlagCloudMicroPhys = ""
  character(STRING) :: FlagSurfaceHeating = ""
  integer           :: ID_FlagTurbulence = 0
  integer, parameter:: ID_FlagTurbulence_Steady = 1
  integer, parameter:: ID_FlagTurbulence_KW1978 = 2
  integer           :: ID_FlagRadiation = 0
  integer, parameter:: ID_FlagRadiation_HeatConst = 1
  integer, parameter:: ID_FlagRadiation_NewtonCooling = 2
  integer           :: ID_FlagSurfaceHeating = 0
  integer, parameter:: ID_FlagSurfaceHeating_Diff = 1
  integer, parameter:: ID_FlagSurfaceHeating_Bulk = 2
  integer           :: ID_FlagCloudMicroPhys = 0
  integer, parameter:: ID_FlagCloudMicroPhys_K1969 = 1

  !------------------------------------------
  ! 初期化手続き ; Initialize procedure 
  !
  call MainInit
  
  !------------------------------------------
  ! 時間積分 time integration 
  !
  call MessageNotify( "M", "main", "Time Integration Start" )

  t = 0
  do while (time <= EndTime) 
      
    ! 時刻の設定
    ! Time setting.
    !
    t = t + 1
    Time = Time + DelTimeLong



    !-------------------------------
    ! 物理過程: 乱流
    !
    select case (ID_FlagTurbulence)    

    case (ID_FlagTurbulence_Steady)

    case (ID_FlagTurbulence_KW1978)
      call KW1978Forcing(    &
        & pyz_VelXNl,                     & ! (in)
        & xqz_VelYNl,                     & ! (in)
        & xyr_VelZNl,                     & ! (in)
        & xyz_PotTempBl,                  & ! (in)
        & xyz_ExnerBl,                    & ! (in)
        & xyza_MixRtBl,                   & ! (in)
        & xyz_KmTend                      & ! (out)

        xyz_DKmDt
        xyz_DPTempDt
        xyz_DVelXDt
        xyz_DVelYDt
        xyz_DVelZDt

        & )

    end select

    !-------------------------------
    ! 物理過程: 凝結
    !



    !-------------------------------
    ! 物理過程: 凝結
    !


    !------------------------------------
    ! 渦拡散係数の移流拡散
    ! Advection and diffusion of turbulent diffusion coefficient.
    !
    select case (ID_FlagTurbulence)
      
    case (ID_FlagTurbulence_Steady)
      !  乱流エネルギーを定常とした場合の渦拡散係数, 渦粘性係数を求める
      !
      call Steady_Turbulence_KW1978( &
        & pyz_VelXNl,     & ! (in)
        & xqz_VelYNl,     & ! (in)
        & xyr_VelZNl,     & ! (in)
        & xyz_PotTempNl,  & ! (in)
        & xyz_KmNl,       & ! (in)
        & xyz_KmAl        & ! (out)
        & )
      
    case (ID_FlagTurbulence_KW1978)
      ! 乱流エネルギーの時間発展を解く
      !

      ! 時間発展
      xyz_KmAl = xyz_KmBl + DelTimeLFrog(t) * xyz_KmTend
      
      ! 値の上限下限の設定
      ! Upper and lower bound value are specified.
      !
      ! 上限値は namelist で与えるようにすること
      xyz_KmAl = max( 0.0d0, min( xyz_KmAl, KmMax ) )

    end select
    
    ! 境界条件 ; Boundary condition
    !
    call BoundaryXCyc_xyz( xyz_KmAl ) ! (inout)
    call BoundaryYCyc_xyz( xyz_KmAl ) ! (inout)
    call BoundaryZSym_xyz( xyz_KmAl ) ! (inout)
       
    ! スカラーに対する渦拡散係数の計算 
    ! Specify turbulent diffusion coefficient for scalar variables.
    !
    select case (ID_FlagTurbulence)
      
    case (ID_FlagTurbulence_Steady)
      xyz_KhAl = 3.0d0 * xyz_KmAl      

    case (ID_FlagTurbulence_KW1978)
      xyz_KhAl = 3.0d0 * xyz_KmAl      
    end select

    !-----------------------------------------
    ! 温位の移流拡散の計算 
    ! Advection and diffusion of potential temperature.
    !
    call Tendency_Dynamic_PotTemp( &
      & pyz_VelXNl,              & ! (in)
      & xqz_VelYNl,              & ! (in)
      & xyr_VelZNl,              & ! (in)
      & xyz_PotTempBl,           & ! (in)
      & xyz_PotTempNl,           & ! (in)
      & xyz_PotTempTendDyn       & ! (out)
      )


    call Tendency_PotTemp_KW1978( &
      & xyz_KmBl,              &
      & xyz_KhBl,              &
      & xyz_PotTempBl          &
      & xyz_PotTempTendTurb    &
      & )

    
    ! 物理過程: 放射強制
    ! Radiation of potential temperature
    !
    select case (ID_FlagRadiation)
    case (ID_FlagRadiation_HeatConst)
      ! とある高度領域を加熱・冷却
      call Tendency_PotTemp_Radiation_HeatConst( &
        & xyz_ExnerBl,                           & ! (in)
        & xyz_PotTempTendRad                     & ! (out)
        & )

    case (ID_FlagRadiation_NewtonCooling)
      !        call Tendency_PotTemp_Radiation_NewtonCool( &
      !          & xyz_ExnerBl,                            & ! (in)
      !          & xyz_PotTempTendRad                      & ! (out)
      !          & )
    end select

    ! 物理過程: 境界からの熱輸送
    !!!! Flux of potential temperature from bottom boundary.
    !
    select case (ID_FlagSurfaceHeating)
    case (ID_FlagSurfaceHeating_Diff)
      !        call Tendency_PotTemp_Flux_Diff(   &
      !          & xyz_PotTempNl,                 & !(in)
      !          & xyz_PotTempTendFlux            & !(out)
      !          & )
    case (ID_FlagSurfaceHeating_Bulk)
      !        call Tendency_PotTemp_Flux_Bulk(   &
      !          & xyz_PotTempNl,                 & !(in)
      !          & xyz_PotTempTendFlux            & !(out)
      !          & )
    end select
    
    ! 時間積分
    ! Time integration
    ! 
    xyz_PotTempAl = xyz_PotTempBl + DelTimeLFrog(t) * (xyz_PotTempTendDyn + xyz_PotTempTendTurb + xyz_PotTempTendRad + xyz_PotTempTendFlux)
    
    ! 境界条件 ; Boundary condition
    !
    call BoundaryXCyc_xyz( xyz_PotTempAl ) ! (inout)
    call BoundaryYCyc_xyz( xyz_PotTempAl ) ! (inout)
    call BoundaryZSym_xyz( xyz_PotTempAl ) ! (inout)
    
    !---------------------------------------
    ! 凝結成分混合比の時間発展
    !
    ! 凝縮成分混合比の移流拡散 
    ! Advection and diffusion of vapor, cloud and rain mixing ratios.
    !
    if (sw_moist) then 
      call Tendency_MixRt_Dynamic(              & 
        & pyz_VelXNl, xqz_VelYNl, xyr_VelZNl,   & !(in)
        & xyza_MixRtBl, xyza_MixRtNl, xyz_KhBl, & !(in)
        & xyza_MixRtTendDyn                     & !(out)
        & )


      call Tendency_MixRt_KW1978(              & 
        & pyz_VelXNl, xqz_VelYNl, xyr_VelZNl,   & !(in)
        & xyza_MixRtBl, xyza_MixRtNl, xyz_KhBl, & !(in)
        & xyza_MixRtTendDyn                     & !(out)
        & )
      
      ! 凝縮成分混合比の下部境界からのフラックス
      ! Flux of vapor, cloud and rain mixing ratios from bottom boundary.
      !
      select case (ID_FlagSurfaceHeating)
      case (ID_FlagSurfaceHeating_Diff)
        !          call Tendency_MixRt_Flux_Diff( &
        !            & xyza_MixRtNl,              & !(in)
        !            & xyza_MixRtTendFlux         & !(out)
        !            & )
      case (ID_FlagSurfaceHeating_Bulk)
        call Tendency_MixRt_Flux_Bulk( &
          & xyza_MixRtNl,              & !(in)
          & xyza_MixRtTendFlux         & !(out)
          & )
      end select
      
      ! 時間積分 Time Integration
      !
      xyza_MixRtAl = xyza_MixRtBl + DelTimeLFrog(t) * ( xyza_MixRtTendDyn + xyza_MixRtTendFlux )
      
      ! 境界条件 Boundary condition
      !
      do l = 1, nf
        call BoundaryXCyc_xyz( xyza_MixRtAl(:,:,:,l) ) ! (inout)
        call BoundaryYCyc_xyz( xyza_MixRtAl(:,:,:,l) ) ! (inout)
        call BoundaryZSym_xyz( xyza_MixRtAl(:,:,:,l) ) ! (inout)
      end do
      
      ! 移流によって負になった部分を埋める
      ! Negative values due to advection are corrected.
      !
      call Adjust_MixRt_FillNegative( &
        & DelTimeLFrog(t),            & !(in)
        & xyza_MixRtAl                & !(out)
        & )
      
      ! 凝結過程
      ! 
      select case (ID_FlagCloudMicroPhys)
      case (ID_FlagCloudMicroPhys_K1969)
        call Adjust_MixRt_Condensation_Kessler( &
          & DelTimeLFrog(t), xyz_ExnerNl,       & !(in) 
          & xyz_PotTempAl, xyza_MixRtAl         & !(inout)
          & )
      end select
      
      ! 境界条件 Boundary condition
      !
      call BoundaryXCyc_xyz( xyz_PotTempAl ) ! (inout)
      call BoundaryYCyc_xyz( xyz_PotTempAl ) ! (inout)
      call BoundaryZSym_xyz( xyz_PotTempAl ) ! (inout)
      do l = 1, nf
        call BoundaryXCyc_xyz( xyza_MixRtAl(:,:,:,l) )  ! (inout)
        call BoundaryYCyc_xyz( xyza_MixRtAl(:,:,:,l) )  ! (inout)
        call BoundaryZSym_xyz( xyza_MixRtAl(:,:,:,l) )  ! (inout)
      end do
      
    end if
    
    !-----------------------------------------
    ! 速度の移流拡散.
    ! Advection and diffusion of velocity components.
    !
    call Tendency_VelX_DynamicLong(                    &
      &            pyz_VelXBl,                         & !(in)
      &            pyz_VelXNl, xqz_VelYNl, xyr_VelZNl, & !(in)
      &            pyz_VelXTendDynL                    & !(out)
      &  )      

    call Tendency_VelX_KW1978(                         &
      &            pyz_VelXBl, xqz_VelYBl, xyr_VelZBl, & !(in)
      &            xyz_KmBl,                           & !(in)
      &            pyz_VelXTendTurb                    & !(out) 
      )
      
    
    call Tendency_VelY_DynamicLong(                    &
      &            pyz_VelYYBl,                         & !(in)
      &            pyz_VelXNl, xqz_VelYNl, xyr_VelZNl, & !(in)
      &            xqz_VelYTendDynL                    & !(out)
      &  )

    call Tendency_VelY_KW1978(                         &
      &            pyz_VelXBl, xqz_VelYBl, xyr_VelZBl, & !(in)
      &            xyz_KmBl,                           & !(in)
      &            pyz_VelYTendTurb                    & !(out) 
      )
    
    call Tendency_VelZ_DynamicLong(                    & 
      &            xyr_VelZBl,                         & !(in)
      &            pyz_VelXNl, xqz_VelYNl, xyr_VelZNl, & !(in)
      &            xyz_PotTempNl, xyza_MixRtNl,        & !(in)
      &            xyr_VelZTendDynL                    & !(out)
      &  )

    call Tendency_VelZ_KW1978(    &
      &            pyz_VelXBl, xqz_VelYBl, xyr_VelZBl, & !(in)
      &            xyz_KmBl,                           & !(in)
      &            xyr_VelZTendTurb                    & !(out) 
      )

    
    ! select case (ID_FlagSurfaceFlux)
    !      case (ID_FlagSurfaceFlux_Bulk)
    !        call Tendency_VelX_Flux_Bulk()
    !        pyz_MomFluxBulk( pyz_VelXBl )
    !        xqz_MomFluxBulk( xqz_VelYBl )
    ! end select
    
    ! 短い時間ステップの初期値作成.
    ! Initial values set up for time integration with short time step.
    !
    pyz_VelXNs  = pyz_VelXBl
    xqz_VelYNs  = xqz_VelYBl
    xyr_VelZNs  = xyr_VelZBl
    xyz_ExnerNs = xyz_ExnerBl
    
    ! 短い時間ステップの時間積分. オイラー法を利用.
    ! Time integration with short time step.
    !
    Euler: do tau = 1, NstepEuler(t)
      
      ! 速度 u の計算.
      ! Time integration horizontal velocity (u).
      !
      call Tendency_VelX_DynamicShort(                   &
        &            pyz_VelXNs, xqz_VelYNs, xyr_VelZNs, &
        &            xyz_ExnerNs,                        &
        &            pyz_VelXTendDynS)
      
      ! 時間積分
      !
      pyz_VelXAs = pyz_VelXNs + DelTimeEuler * ( pyz_VelXTendDynL + pyz_VelXTendDynS )
      
      ! 境界条件 Boundary condition
      !
      call BoundaryXCyc_pyz( pyz_VelXAs ) ! (inout)
      call BoundaryYCyc_pyz( pyz_VelXAs ) ! (inout)
      call BoundaryZSym_pyz( pyz_VelXAs ) ! (inout)
      
      ! 速度 v の計算.
      ! Time integration horizontal velocity (v).
      !
      call Tendency_VelY_DynamicShort(                   &
        &            pyz_VelXNs, xqz_VelYNs, xyr_VelZNs, &
        &            xyz_ExnerNs,                        &
        &            xqz_VelYTendDynS)
      
      ! 時間積分
      !
      xqz_VelYAs = xqz_VelYNs + DelTimeEuler * ( xqz_VelYTendDynL + xqz_VelYTendDynS )
      
      ! 境界条件 Boundary condition
      !
      call BoundaryXCyc_xqz( xqz_VelYAs ) ! (inout)
      call BoundaryYCyc_xqz( xqz_VelYAs ) ! (inout)
      call BoundaryZSym_xqz( xqz_VelYAs ) ! (inout)
      
      ! エクスナー関数の計算.
      ! Time integration exner function.
      !
      call calc_dynamic_exner_implicit(  &
        & xyr_VelZTendDynL,    & !(in)
        & pyz_VelXNs,          & !(in)
        & pyz_VelXAs,          & !(in)
        & xqz_VelYNs,          & !(in)
        & xqz_VelYAs,          & !(in)
        & xyr_VelZNs,          & !(in)
        & xyz_ExnerNs,         & !(in)
        & xyz_ExnerAs          & !(out)
        & )
      
      ! 境界条件 Boundary condition
      !
      call BoundaryXCyc_xyz( xyz_ExnerAs ) ! (inout)
      call BoundaryYCyc_xyz( xyz_ExnerAs ) ! (inout)
      call BoundaryZSym_xyz( xyz_ExnerAs ) ! (inout)
      
      ! 速度 w の計算
      ! Time integration vertical velocity.
      !
      call Tendency_VelZ_DynamicShort(                   &
        &            pyz_VelXNs, xqz_VelYNs, xyr_VelZNs, &
        &            xyz_ExnerNs, xyz_ExnerAs,           &
        &            xyr_VelZTendDynS)
      
      ! 時間積分
      !
      xyr_VelZAs = xyr_VelZNs + DelTimeEuler * (xyr_VelZTendDynL + xyr_VelZTendDynS)
      
      ! 境界条件 Boundary condition
      !
      call BoundaryXCyc_xyr( xyr_VelZAs ) ! (inout)
      call BoundaryYCyc_xyr( xyr_VelZAs ) ! (inout)
      call BoundaryZAsym_xyr( xyr_VelZAs ) ! (inout)
      
      ! 短い時間ステップのループを回すための処置
      ! Renew prognostic variables for next short time step integration.
      !
      xyz_ExnerNs  = xyz_ExnerAs
      pyz_VelXNs   = pyz_VelXAs
      xqz_VelYNs   = xqz_VelYAs
      xyr_VelZNs   = xyr_VelZAs
      
    end do Euler
    
    ! 最終的な短い時間ステップでの値を長い時間ステップでの値とみなす
    ! Renew prognostic variables for next long time step integration.
    !
    xyz_ExnerAl  = xyz_ExnerAs
    pyz_VelXAl   = pyz_VelXAs
    xqz_VelYAl   = xqz_VelYAs
    xyr_VelZAl   = xyr_VelZAs
    
    ! 時間フィルタ. 
    ! Time filter. 
    !
    call AsselinFilter( &
      &  xyz_ExnerAl,   & ! (in)
      &  xyz_ExnerNl,   & ! (inout)
      &  xyz_ExnerBl    & ! (in)
      & )         
    call AsselinFilter( &
      & pyz_VelXAl,     & ! (in)
      & pyz_VelXNl,     & ! (inout)
      & pyz_VelXBl      & ! (in)
      & )
    call AsselinFilter( &
      & xqz_VelYAl,     & ! (in)
      & xqz_VelYNl,     & ! (inout)
      & xqz_VelYBl      & ! (in)
      & )
    call AsselinFilter( &
      & xyr_VelZAl,     & ! (in)
      & xyr_VelZNl,     & ! (inout)
      & xyr_VelZBl      & ! (in)
      & )
    call AsselinFilter( &
      & xyz_PotTempAl,  & ! (in)
      & xyz_PotTempNl,  & ! (inout)
      & xyz_PotTempBl   & ! (in)
      & )
    call AsselinFilter( &
      & xyz_KmAl,       & ! (in)
      & xyz_KmNl,       & ! (inout)
      & xyz_KmBl        & ! (in)
      & )
    do l = 1, nf
      call AsselinFilter(        &
        & xyza_MixRtAl(:,:,:,l), &
        & xyza_MixRtNl(:,:,:,l), &
        & xyza_MixRtBl(:,:,:,l))
    end do
    
    ! スポンジ層
    ! Numerical dumping.
    !
    call DampSponge_pyz( pyz_VelXAl,    pyz_VelXBl,    DelTimeLFrog(t) )
    call DampSponge_xqz( xqz_VelYAl,    xqz_VelYBl,    DelTimeLFrog(t) )
    call DampSponge_xyr( xyr_VelZAl,    xyr_VelZBl,    DelTimeLFrog(t) )
    call DampSponge_xyz( xyz_ExnerAl,   xyz_ExnerBl,   DelTimeLFrog(t) )
    call DampSponge_xyz( xyz_PotTempAl, xyz_PotTempBl, DelTimeLFrog(t) )
    call DampSponge_xyz( xyz_KmAl,      xyz_KmBl,      DelTimeLFrog(t) )
    
    ! 長い時間ステップのループを回すための処置.
    ! Renew prognostic variables for next long time step integration.
    !
    xyz_PotTempBl = xyz_PotTempNl
    xyza_MixRtBl  = xyza_MixRtNl
    xyz_ExnerBl   = xyz_ExnerNl
    pyz_VelXBl    = pyz_VelXNl
    xqz_VelYBl    = xqz_VelYNl
    xyr_VelZBl    = xyr_VelZNl
    xyz_KmBl      = xyz_KmNl
    xyz_KhBl      = xyz_KhNl
    
    xyz_PotTempNl = xyz_PotTempAl
    xyza_MixRtNl  = xyza_MixRtAl
    xyz_ExnerNl   = xyz_ExnerAl
    pyz_VelXNl    = pyz_VelXAl
    xqz_VelYNl    = xqz_VelYAl
    xyr_VelZNl    = xyr_VelZAl
    xyz_KmNl      = xyz_KmAl
    xyz_KhNl      = xyz_KhAl
    
    ! ヒストリーファイルへの出力.
    ! Out put to history file.
    !
!    call MessageNotify( "M", "main", "Time = %f", d=(/Time/) )
    
    ! 移流に対する CFL 条件のチェック 
    ! CFL condtion check for advection
    !
!    call CFLCheckTimeLongVelX( &
!      & pyz_VelXNl             & ! (in)
!      & )
!    call CFLCheckTimeLongVelY( &
!      & xqz_VelYNl             & ! (in)
!      & )
!    call CFLCheckTimeLongVelZ( &
!      & xyr_VelZNl             & ! (in)
!      & )

    ! ヒストリファイル出力.
    ! Out put to history file.
    !
    xyz_VelXNl = xyz_avr_pqz(pyz_VelXNl)
    xyz_VelYNl = xyz_avr_xqz(xqz_VelYNl)
    xyz_VelZNl = xyz_avr_xyr(xyr_VelZNl)
    call HistoryAutoPut(Time, 'PotTemp', xyz_PotTempNl(1:nx, 1:ny, 1:nz))
    call HistoryAutoPut(Time, 'Exner',   xyz_ExnerNl(1:nx, 1:ny, 1:nz))
    call HistoryAutoPut(Time, 'VelX',    xyz_VelXNl(1:nx, 1:ny, 1:nz))
    call HistoryAutoPut(Time, 'VelY',    xyz_VelYNl(1:nx, 1:ny, 1:nz))
    call HistoryAutoPut(Time, 'VelZ',    xyz_VelZNl(1:nx, 1:ny, 1:nz))
    call HistoryAutoPut(Time, 'Km',      xyz_KmNl(1:nx, 1:ny, 1:nz))
    call HistoryAutoPut(Time, 'Kh',      xyz_KhNl(1:nx, 1:ny, 1:nz))
    do l = 1, nf
      call HistoryAutoPut(Time, trim(SpcWetSymbol(l)), xyza_MixRtNl(1:nx, 1:ny, 1:nz, l))
    end do
  end do
  
  ! 出力ファイルのクローズ
  ! Close out put files.
  !
  call HistoryFileAutoClose

  !----------------------------------------------------
  ! リスタートファイルの作成
  ! Make restartfile.
  !
  call ReStartFile_Open( )
  call ReStartFile_OutPut( &
    & Time - DelTimeLong,  & ! (in)
    & xyz_PotTempBl,       & ! (in)
    & xyz_ExnerBl,         & ! (in)
    & pyz_VelXBl,          & ! (in)
    & xqz_VelYBl,          & ! (in)
    & xyr_VelZBl,          & ! (in)
    & xyza_MixRtBl,        & ! (in)
    & xyz_KmBl,            & ! (in)
    & xyz_KhBl             & ! (in)
    & )
  call ReStartFile_OutPut( & ! (in)
    & Time,                & ! (in)
    & xyz_PotTempNl,       & ! (in)
    & xyz_ExnerNl,         & ! (in)
    & pyz_VelXNl,          & ! (in)
    & xqz_VelYNl,          & ! (in)
    & xyr_VelZNl,          & ! (in)
    & xyza_MixRtNl,        & ! (in)
    & xyz_KmNl,            & ! (in)
    & xyz_KhNl             & ! (in)
    & )
  call ReStartFile_Close


  !----------------------------------------------------
  ! MPI END
  !
  call MPIWrapperFinalize
  
contains
!-----------------------------------------------------------------------
  subroutine VariableAllocate
    !
    !初期化として, 配列を定義し, 値としてゼロを代入する.
    !

    !暗黙の型宣言禁止
    implicit none

    !配列割り当て
    allocate( pyz_VelXBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( pyz_VelXNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_VelXNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( pyz_VelXAl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( pyz_VelXNs(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( pyz_VelXAs(imin:imax,jmin:jmax,kmin:kmax) )

    pyz_VelXBl  = 0.0d0;    pyz_VelXNl  = 0.0d0;    pyz_VelXAl  = 0.0d0
    pyz_VelXNs  = 0.0d0;    pyz_VelXAs = 0.0d0    
    xyz_VelXNl  = 0.0d0

    allocate( xqz_VelYBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xqz_VelYNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_VelYNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xqz_VelYAl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xqz_VelYNs(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xqz_VelYAs(imin:imax,jmin:jmax,kmin:kmax) )

    xqz_VelYBl  = 0.0d0;    xqz_VelYNl  = 0.0d0;    xqz_VelYAl  = 0.0d0
    xqz_VelYNs  = 0.0d0;    xqz_VelYAs = 0.0d0    
    xyz_VelYNl  = 0.0d0

    allocate( xyr_VelZBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyr_VelZNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_VelZNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyr_VelZAl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyr_VelZNs(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyr_VelZAs(imin:imax,jmin:jmax,kmin:kmax) )

    xyr_VelZBl  = 0.0d0;    xyr_VelZNl  = 0.0d0;    xyr_VelZAl  = 0.0d0
    xyr_VelZNs  = 0.0d0;    xyr_VelZAs = 0.0d0
    xyz_VelZNl  = 0.0d0

    allocate( xyz_ExnerBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_ExnerNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_ExnerAl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_ExnerNs(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_ExnerAs(imin:imax,jmin:jmax,kmin:kmax) )

    xyz_ExnerBl = 0.0d0;    xyz_ExnerNl = 0.0d0;    xyz_ExnerAl = 0.0d0
    xyz_ExnerNs = 0.0d0;    xyz_ExnerAs = 0.0d0

    allocate( xyz_PotTempBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_PotTempNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_PotTempAl(imin:imax,jmin:jmax,kmin:kmax) )

    xyz_PotTempBl = 0.0d0;  xyz_PotTempNl = 0.0d0;  xyz_PotTempAl = 0.0d0

    allocate( xyz_KmBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_KmNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_KmAl(imin:imax,jmin:jmax,kmin:kmax) )

    xyz_KmBl    = 0.0d0;    xyz_KmNl    = 0.0d0;    xyz_KmAl    = 0.0d0

    allocate( xyz_KhBl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_KhNl(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_KhAl(imin:imax,jmin:jmax,kmin:kmax) )

    xyz_KhBl    = 0.0d0;    xyz_KhNl    = 0.0d0;    xyz_KhAl    = 0.0d0

    allocate( xyza_MixRtBl(imin:imax,jmin:jmax,kmin:kmax,nf) )
    allocate( xyza_MixRtNl(imin:imax,jmin:jmax,kmin:kmax,nf) )
    allocate( xyza_MixRtAl(imin:imax,jmin:jmax,kmin:kmax,nf) )

    xyza_MixRtBl = 0.0d0;   xyza_MixRtNl = 0.0d0;   xyza_MixRtAl = 0.0d0

    allocate( DelTimeLFrog(NstepLong) )
    allocate( NStepEuler(NStepLong) )

    DelTimeEuler = 0.0d0
    DelTimeLFrog = 0.0d0 
    NStepEuler = 0.0d0
    NstepLFrog = 0.0d0

    allocate( pyz_VelXTendDynS(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( pyz_VelXTendDynL(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( pyz_VelXTendFlux(imin:imax,jmin:jmax,kmin:kmax) )

    pyz_VelXTendDynS = 0.0d0
    pyz_VelXTendDynL = 0.0d0
    pyz_VelXTendFlux = 0.0d0

    allocate( xqz_VelYTendDynS(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xqz_VelYTendDynL(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xqz_VelYTendFlux(imin:imax,jmin:jmax,kmin:kmax) )

    xqz_VelYTendDynS = 0.0d0
    xqz_VelYTendDynL = 0.0d0
    xqz_VelYTendFlux = 0.0d0   

    allocate( xyr_VelZTendDynS(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyr_VelZTendDynL(imin:imax,jmin:jmax,kmin:kmax) )

    xyr_VelZTendDynS = 0.0d0
    xyr_VelZTendDynL = 0.0d0

    allocate( xyz_KmTend(imin:imax,jmin:jmax,kmin:kmax) )

    xyz_KmTend = 0.0d0

    allocate( xyz_PotTempTendDyn(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_PotTempTendRad(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyz_PotTempTendFlux(imin:imax,jmin:jmax,kmin:kmax) )

    xyz_PotTempTendDyn  = 0.0d0
    xyz_PotTempTendRad  = 0.0d0
    xyz_PotTempTendFlux = 0.0d0

    allocate( xyza_MixRtTendDyn(imin:imax,jmin:jmax,kmin:kmax,nf) )
    allocate( xyza_MixRtTendFlux(imin:imax,jmin:jmax,kmin:kmax,nf) )  

    xyza_MixRtTendDyn = 0.0d0
    xyza_MixRtTendFlux = 0.0d0
    
  end subroutine VariableAllocate
  
  !-----------------------------------------------------------------------
  subroutine MainInit
    implicit none

    ! MPI
    !
    call MPIWrapperInit

    ! NAMELIST ファイル名の読み込み
    ! Loading NAMELIST file.
    !
    call argset_init( &
      & cfgfile       & ! (out)
      & )
    
    ! Flag の設定
    !
    !
    call deepconv_main_nml ( &
         & cfgfile           & ! (in) 
         & )

    ! 時間積分の初期化
    ! Initialization of time integration.
    !
    call timeset_init( &
      & cfgfile        & ! (in)
      & )
    
    ! 格子点情報の初期化
    ! Initialization of grid arrangement.
    !
    call gridset_init( &
      & cfgfile        & ! (in)
      & )
    
    ! 化学計算ルーチンの初期化
    ! Initialization of chemical routines.
    !
    call chemcalc_init( )
    
    ! 定数の情報の初期化
    ! Initialization of constant variables.
    !
    call constants_init( &
      & cfgfile        & ! (in)
      & )

    ! 軸の情報の初期化
    ! Initialization of axis variables.
    !
    call axesset_init( &
      & cfgfile        & ! (in)
      & )
    
    ! I/O ファイル名の初期化
    ! Initialization of output file name. 
    !
    call fileset_init( &
      & cfgfile        & ! (in)
      & )
    
    ! 湿潤過程共有変数の初期化
    ! Initialization of common variables for moist process.
    !
    if (sw_moist) call moistset_init( )
    
    ! 内部変数の初期化
    ! Initialization of internal variables.
    !
    call VariableAllocate
    
    ! 初期値の代入 
    ! * ReStartFile が設定されている場合にはファイルを読み込む. 
    !   設定されていない場合にはデフォルトの基本場と擾乱場を作る. 
    !
    ! Initial value set up.
    ! * Read restartfile if it is specified. If not, make default basic
    !   state and disturbance.
    !
    call MessageNotify( "M", "main", "Initial value setup." )
    
    !基本場, 擾乱場の初期値を netCDF ファイルから取得する.
    call ReStartFile_Get( &
      & ReStartTime,      & ! (out)
      & xyz_PotTempBl,    & ! (out)
      & xyz_ExnerBl,      & ! (out)
      & pyz_VelXBl,       & ! (out)
      & xqz_VelYBl,       & ! (out)
      & xyr_VelZBl,       & ! (out)
      & xyza_MixRtBl,     & ! (out)
      & xyz_KmBl,         & ! (out)
      & xyz_KhBl,         & ! (out)
      & xyz_PotTempNl,    & ! (out)
      & xyz_ExnerNl,      & ! (out)
      & pyz_VelXNl,       & ! (out)
      & xqz_VelYNl,       & ! (out)
      & xyr_VelZNl,       & ! (out)
      & xyza_MixRtNl,     & ! (out)
      & xyz_KmNl,         & ! (out)
      & xyz_KhNl          & ! (out)
      & )


    ! 数値摩擦係数の初期化
    ! Initialization of numerical friction coefficient.
    !
    call Damping_Init( &
      & cfgfile        & ! (in)
      & )     

    ! 力学モジュールの初期化
    ! Initialization of dynamical modules
    !
    call dynamic_exner_implicit_init()  

    
    ! 乱流拡散項の初期化
    ! Initialization of turbulent diffusion term.
    !
    call Turbulence_KW1978_Init()
    
    ! 暖かい雨のパラメタリゼーションの初期化 
    ! Initialization of warmrain parameterization.
    !
!    call WarmRainPrm_Init( &
!      & cfgfile            & ! (in)
!      & )  
    
    ! 負の湿潤量の補填計算の初期化
    ! Initialization of negative moist value correction.
    !
    call FillNegative_Init( &
      & xyza_MixRtBasicZ,   & ! (in)
      & xyz_DensBasicZ      & ! (in)
      & )
    
    ! 放射強制の初期化
    !  Initialization of radiative forcing.
    !
!    call Radiation_Init( &
!      & cfgfile          & ! (in)    
!      & )
    
    ! 湿潤気塊の浮力計算の初期化
    ! Initialization of moist buoyancy calculation.
    !
!    if (sw_moist) call MoistBuoy_Init( )
    
   
    ! 時刻とループ回数の初期化
    ! Initialization of time integration.
    !
    NstepLFrog   = NstepLong 
    NstepEuler   = NstepShort 
    DelTimeLFrog = DelTimeLong * 2.0d0 
    DelTimeEuler = DelTimeShort
    
    ! 計算開始時刻と時間格子間隔の初期化
    ! * ReStartFile が設定されている場合, ファイルから読み込んだ値を指定.
    ! * ReStartFile が設定されてない場合
    !   * 開始時刻は 0.0
    !   * 1 ステップ目の時間格子間隔を陽に指定
    !
    ! Setup restart time and time interval. 
    ! * Read restartfile if it is specified.
    ! * If not, 
    !   * "t" is set to be 0.
    !   * Time intervals for 1st step are specified explicitly.
    !
    Time = StartTime
    if ( StartTime == 0.0d0 ) then    
      NstepEuler(1)   = NstepShort /2       
      DelTimeLFrog(1) = DelTimeLong         
    end if
    
    call MessageNotify( "M", "main", "NstepLong= %d", i=(/NstepLong/) )
    call MessageNotify( "M", "main", "NstepEuler= %d", i=(/NstepEuler/) )
    call MessageNotify( "M", "main", "DelTimeLFrog= %f", d=(/DelTimeLFrog/) )
    call MessageNotify( "M", "main", "DelTimeEuler= %f", d=(/DelTimeEuler/) )
    
    ! ヒストリーファイルへの出力
    ! Out put to history file.
    !
    call BasicZFileOutput
    
    ! 音波に対する CFL 条件のチェック
    ! CFL condtion check for sound wave.
    !
    call CFLCheckTimeShort( &
      & xyz_VelSoundBasicZ   & ! (in)
      & )

  end subroutine MainInit


  subroutine Tendency_PotTemp_Radiation_HeatConst(xyz_ExnerBl, xyz_Rad)
    
    implicit none

    real(DP), intent(in)  :: xyz_ExnerBl(imin:imax, jmin:jmax, kmin:kmax)
    real(DP), intent(out) :: xyz_Rad(imin:imax, jmin:jmax, kmin:kmax)
    
    xyz_Rad = xyz_RadHeatConst( xyz_ExnerBl ) 

    call HistoryAutoPut(Time, 'PotTempRad', xyz_Rad(fimin:fimax, fjmin:fjmax, fkmin:fkmax))

  end subroutine Tendency_PotTemp_Radiation_HeatConst
  

  subroutine TendencyPotTemp_Flux_Diff(xyz_PotTempNl, xyz_Flux)

    implicit none

    real(DP), intent(in)  :: xyz_PotTempNl(imin:imax, jmin:jmax, kmin:kmax)
    real(DP), intent(out) :: xyz_Flux(imin:imax, jmin:jmax, kmin:kmax)
    
!    xyz_Flux = xyz_HeatFluxDiff( xyz_PotTempNl )  

    call HistoryAutoPut(Time, 'PotTempFlux', xyz_Flux(fimin:fimax, fjmin:fjmax, fkmin:fkmax))

  end subroutine TendencyPotTemp_Flux_Diff


  subroutine Tendency_MixRt_Flux_Diff(xyza_MixRtNl, xyza_Flux)

    implicit none
    real(DP), intent(in)  :: xyza_MixRtNl(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP), intent(out) :: xyza_Flux(imin:imax, jmin:jmax, kmin:kmax, nf)
    integer              :: l
    
!    xyza_Flux = xyza_MixRtFluxDiff(xyza_MixRtNl) 

    ! Output
    do l = 1, nf
      call HistoryAutoPut(Time, trim(SpcWetSymbol(l))//'_Flux', xyza_Flux(fimin:fimax, fjmin:fjmax, fkmin:fkmax, l))
    end do
    
  end subroutine Tendency_MixRt_Flux_Diff
  

  subroutine Tendency_MixRt_Flux_Bulk(xyza_MixRtNl, xyza_Flux)

    implicit none

    real(DP), intent(in)  :: xyza_MixRtNl(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP), intent(out) :: xyza_Flux(imin:imax, jmin:jmax, kmin:kmax, nf)
    integer              :: l

!    xyza_Flux = xyza_MixRtFluxBulk(xyza_MixRtNl) 

    ! Output
    do l = 1, nf
      call HistoryAutoPut(Time, trim(SpcWetSymbol(l))//'_Flux', xyza_Flux(fimin:fimax, fjmin:fjmax, fkmin:fkmax, l))
    end do
    
  end subroutine Tendency_MixRt_Flux_Bulk

  
  subroutine Adjust_MixRt_FillNegative(DelTime, xyza_MixRtAl)
    
    implicit none

    real(DP), intent(in), optional :: DelTime    
    real(DP), intent(inout)        :: xyza_MixRtAl(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyza_MixRtOrig(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyza_MixRtWork(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyza_Del(imin:imax, jmin:jmax, kmin:kmax, nf)
    integer                       :: l

    ! 初期値を保管 Store Initial Value
    !
    xyza_MixRtOrig = xyza_MixRtAl
    
    ! 移流によって負になった部分を埋める
    ! Negative values due to advection are corrected.
    !
    xyza_MixRtWork = xyza_MixRtAl
    xyza_MixRtAl   = xyza_FillNegative_xyza( xyza_MixRtWork ) 
    
    ! 埋め切れなかった部分をゼロにする. 
    ! Negative values mixing ratios are corrected.
    !
    xyza_MixRtWork = xyza_MixRtAl
    xyza_MixRtAl = max( - xyza_MixRtBasicZ, xyza_MixRtWork )
    
    ! 埋めた/削った量を保管
    ! Adjustion value is stored.
    !    
    xyza_Del = xyza_MixRtAl - xyza_MixRtOrig

    ! Output
    if (present(DelTime)) then 
      do l = 1, nf
        call HistoryAutoPut(Time, trim(SpcWetSymbol(l))//'_Fill', xyza_Del(fimin:fimax, fjmin:fjmax, fkmin:fkmax, l)/ DelTime)
      end do
    end if

  end subroutine Adjust_MixRt_FillNegative


  subroutine Adjust_MixRt_Condensation_Kessler(DelTime, xyz_ExnerNl, xyz_PotTempAl, xyza_MixRtAl)

    implicit none

    real(DP), intent(in)           :: DelTime
    real(DP), intent(in)           :: xyz_ExnerNl(imin:imax, jmin:jmax, kmin:kmax)
    real(DP), intent(inout)        :: xyz_PotTempAl(imin:imax, jmin:jmax, kmin:kmax)
    real(DP), intent(inout)        :: xyza_MixRtAl(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyz_PotTempOrig(imin:imax, jmin:jmax, kmin:kmax)
    real(DP)                       :: xyz_PotTempWork(imin:imax, jmin:jmax, kmin:kmax)
    real(DP)                       :: xyz_DelPotTemp(imin:imax, jmin:jmax, kmin:kmax)
    real(DP)                       :: xyz_Del(imin:imax, jmin:jmax, kmin:kmax)
    real(DP)                       :: xyza_MixRtOrig(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyza_MixRtWork(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyza_DelMixRt(imin:imax, jmin:jmax, kmin:kmax, nf)
    real(DP)                       :: xyza_Del(imin:imax, jmin:jmax, kmin:kmax, nf)
    integer                        :: l

    real(DP) :: xyza_Fall(imin:imax, jmin:jmax, kmin:kmax, nf)

    xyza_Fall = xyza_FallRain(xyza_MixRtBl) 

    ! 初期値を保管 Store Initial Value
    !
    xyz_PotTempOrig = xyz_PotTempAl
    xyza_MixRtOrig  = xyza_MixRtAl    
    
    ! 暖かい雨のパラメタリゼーション.
    ! * 雲<-->雨 の変換を行う.
    !
    ! Warm rain parameterization.
    ! * Conversion from cloud to rain.
    
    !これまでの値を作業配列に保管
    ! Previous values are stored to work area.
    !
    xyza_MixRtWork = xyza_MixRtAl
    
    !雨への変化量を計算
    ! Conversion values are calculated.
    !    
    xyza_MixRtAl   = xyza_MixRtWork + xyza_Cloud2Rain( xyza_MixRtAl, DelTime )
    
    ! 境界条件 Boundary condition
    !
    do l = 1, nf
      call BoundaryXCyc_xyz( xyza_MixRtAl(:,:,:,l) ) ! (inout)
      call BoundaryYCyc_xyz( xyza_MixRtAl(:,:,:,l) ) ! (inout)
      call BoundaryZSym_xyz( xyza_MixRtAl(:,:,:,l) ) ! (inout)
    end do
    
    ! 湿潤飽和調節
    ! * 蒸気<-->雲の変換を行う.
    !
    ! Moist adjustment.
    ! * Conversion from vapor to cloud.
    
!    ! これまでの値を作業配列に保管
!    ! Previous values are stored to work area.
!    !
!    xyz_PotTempWork = xyz_PotTempAl
!    xyza_MixRtWork  = xyza_MixRtAl
    
    ! 湿潤調節法を適用
    ! Moist adjustment is applied.
    !
    call MoistAdjustSvapPress(   &
      & xyz_ExnerNl,             & ! (in)
      & xyz_PotTempAl,           & ! (inout)
      & xyza_MixRtAl             & ! (inout)
      & )
    if (sw_NH4SH) then 
      call MoistAdjustNH4SH(     &
        & xyz_ExnerNl,           & !(in)
        & xyz_PotTempAl,         & !(inout)
        & xyza_MixRtAl           & !(inout)
        & )
    end if 
    
    ! 境界条件 Boundary condition
    !
    call BoundaryXCyc_xyz( xyz_PotTempAl ) ! (inout)
    call BoundaryYCyc_xyz( xyz_PotTempAl ) ! (inout)
    call BoundaryZSym_xyz( xyz_PotTempAl ) ! (inout)
    do l = 1, nf
      call BoundaryXCyc_xyz( xyza_MixRtAl(:,:,:,l) )
      call BoundaryYCyc_xyz( xyza_MixRtAl(:,:,:,l) )
      call BoundaryZSym_xyz( xyza_MixRtAl(:,:,:,l) )
    end do
    
    ! 暖かい雨のパラメタリゼーション.
    ! * 蒸気<-->雨 の変換を行う
    !
    ! Warm rain parameterization.
    ! * Conversion from rain to vapor.
    
    !これまでの値を作業配列に保管
    ! Previous values are stored to work area.
    !
    xyz_PotTempWork = xyz_PotTempAl
    xyza_MixRtWork  = xyza_MixRtAl
    
    ! 雨から蒸気への混合比変化を求める
    ! * 温位の計算において, 混合比変化が必要となるため, 
    !   混合比変化を 1 つの配列として用意する.
    !
    ! Conversion values are calculated.
    !
    if (sw_NH4SH) then 
      xyza_DelMixRt =                                                    &
        & (                                                              &
        &   + xyza_Rain2Gas(                                             &
        &       xyz_ExnerNl, xyz_PotTempAl, xyza_MixRtAl, DelTime        &
        &     )                                                          &
        &   + xyza_Rain2GasNH4SH(                                        &
        &       xyz_ExnerNl, xyz_PotTempAl, xyza_MixRtAl, DelTime        &
        &     )                                                          &
        &  )    
    else
      xyza_DelMixRt =                                                    &
        & (                                                              &
        &   + xyza_Rain2Gas(                                             &
        &       xyz_ExnerNl, xyz_PotTempAl, xyza_MixRtAl, DelTime        &
        &     )                                                          &
        &  )
    end if

    ! 温位の計算. 雨から蒸気への変換に伴う潜熱・反応熱を追加.
    !
    !
    if (sw_NH4SH) then 
      xyz_DelPotTemp =  &
        & + (                                                                 &
        &     + xyz_Rain2GasHeat( xyz_PotTempAl, xyz_ExnerNl, xyza_DelMixRt ) & 
        &     + xyz_Rain2GasHeatNH4SH(xyz_ExnerNl, xyza_DelMixRt)              &
        &    )
    else
      xyz_DelPotTemp = xyz_Rain2GasHeat( xyz_PotTempAl, xyz_ExnerNl, xyza_DelMixRt )
    end if
    
    ! 温位と混合比の計算. 雨から蒸気への変換分を追加
    !
    !
    xyz_PotTempAl = xyz_PotTempWork + xyz_DelPotTemp
    xyza_MixRtAl   = xyza_MixRtWork + xyza_DelMixRt
    
    ! 差分を用意
    !
    !
    xyz_Del  = xyz_PotTempAl - xyz_PotTempOrig
    xyza_Del = xyza_MixRtAl  - xyza_MixRtOrig

    !値の保管
    !
    !
    call HistoryAutoPut(Time, 'PotTempCond', xyz_Del(fimin:fimax, fjmin:fjmax, fkmin:fkmax) / DelTime)
    do l = 1, nf
      call HistoryAutoPut(Time, trim(SpcWetSymbol(l))//'_Cond', xyza_Del(fimin:fimax, fjmin:fjmax, fkmin:fkmax, l) / DelTime)
    end do

  end subroutine Adjust_MixRt_Condensation_Kessler


  !-----------------------------------------------------------------------
  subroutine HistoryAutoAddMain
    !
    ! 予報変数の出力
    !
    use gtool_historyauto, only: HistoryAutoAddVariable
    use gridset, only: nf

    implicit none

    integer :: l

    !無次元圧力の擾乱
    call HistoryAutoAddVariable(  &
      & varname='Exner',      &  
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of nondimensional pressure', &
      & units=' ',            &        
      & xtype='double' )
    
    !温位の擾乱
    call HistoryAutoAddVariable(  &
      & varname='PotTemp',    &
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of potential temperature',  &
      & units='K',            &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelX',       &
      & dims=(/'x','y','z','t'/), &
      & longname='zonal velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelY',       &
      & dims=(/'x','y','z','t'/), &
      & longname='zonal velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !鉛直速度
    call HistoryAutoAddVariable(  &
      & varname='VelZ',       &
      & dims=(/'x','y','z','t'/), &
      & longname='vertical velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !渦粘性係数(運動量)
    call HistoryAutoAddVariable(  &
      & varname='Km',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient', &
      & units='m2.s-1',            &
      & xtype='double' )
  
    !渦粘性係数(熱)
    call HistoryAutoAddVariable(  &
      & varname='Kh',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient for heat', &
      & units='m2.s-1',            &
      & xtype='double')
  
    !混合比
    do l = 1, nf
      call HistoryAutoAddVariable(                               &
        & varname=trim(SpcWetSymbol(l)),                     &
        & dims=(/'x','y','z','t'/),                              &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio',   &
        & units='kg.kg-1',                                 &
        & xtype='double')
    end do

  end subroutine HistoryAutoAddMain


  subroutine HistoryAutoAddFlux

    use gtool_historyauto, only: HistoryAutoAddVariable

    implicit none

    !地表面フラックス
    call HistoryAutoAddVariable(  &
      & varname='MixRtFluxBulk',         &
      & dims=(/'x','y','z','t'/), &
      & longname='flux of mixing ratio of vapor from surface', &
      & units='kg.kg-1.s-1',            &
      & xtype='double')

    !地表面フラックス(熱)
    call HistoryAutoAddVariable(  &
      & varname='HeatFluxBulk',         &
      & dims=(/'x','y','z','t'/), &
      & longname='flux of heating from surface', &
      & units='K.s-1',            &
      & xtype='double')

    !地表面フラックス(運動量)
    call HistoryAutoAddVariable(  &
      & varname='MomFluxBulk',         &
      & dims=(/'x','y','z','t'/), &
      & longname='flux of momentum from surface', &
      & units='kg.m.s-1',            &
      & xtype='double')

  end subroutine HistoryAutoAddFlux


  subroutine HistoryAutoAddBuoy

    use gtool_historyauto, only: HistoryAutoAddVariable

    implicit none

    call HistoryAutoAddVariable(  &
      & varname='BuoyTemp', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy (Temp)',  &
      & units='s-2',    &
      & xtype='double')

    call HistoryAutoAddVariable(  &
      & varname='BuoyMolWt', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy (MolWt)',  &
      & units='s-2',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='BuoyDrag', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyance (Drag)',  &
      & units='s-2',    &
      & xtype='double' )

  end subroutine HistoryAutoAddBuoy

  subroutine HistoryAutoAddPotTemp
    call HistoryAutoAddVariable(  &
      & varname='PotTempCond',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Latent heat term of potential temperature', &
      & units='K.s-1',    &
      & xtype='double')

    call HistoryAutoAddVariable(  &
      & varname='PotTempFlux',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Surface Flux term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double')

    call HistoryAutoAddVariable(  &
      & varname='PotTempDamp',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Newtonian Cooling term of potential temperature',  &
      & units='K.s-1"',    &
      & xtype='double')


  end subroutine HistoryAutoAddPotTemp

  subroutine HistoryAutoAddMom

    use gtool_historyauto, only: HistoryAutoAddVariable

    implicit none

    call HistoryAutoAddVariable(  &
      & varname='Mom_Adv',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Advection term of momentum in x direction',  &
      & units='kg.m..s-2"',    &
      & xtype='double')

    call HistoryAutoAddVariable(  &
      & varname='Mom_Turb',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Turblence term of momentum in x direction',  &
      & units='kg.m..s-2"',    &
      & xtype='double')

    call HistoryAutoAddVariable(  &
      & varname='Mom_Diff',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Numerical diffusion term of momentum in x direction',  &
      & units='kg.m..s-2"',    &
      & xtype='double')

    call HistoryAutoAddVariable(  &
      & varname='Mom_Flux',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Flux term of momentum in x direction',  &
      & units='kg.m..s-2"',    &
      & xtype='double')
    
  end subroutine HistoryAutoAddMom


  subroutine HistoryAutoAddMixRt

    use gtool_historyauto, only: HistoryAutoAddVariable
    use gridset, only: nf

    implicit none

    integer :: l

    do l = 1, nf
        
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'_Flux', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Surface Flux term of '          &
        &           //trim(SpcWetSymbol(l))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double')
    
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'_Fill', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Filling Negative term of '          &
        &           //trim(SpcWetSymbol(l))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double')
    
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'_Cond', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Condensation term of '          &
        &           //trim(SpcWetSymbol(l))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double')

      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'_Fall', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Fall Rain term of '          &
        &           //trim(SpcWetSymbol(l))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double')
    end do
    
  end subroutine HistoryAutoAddMixRt


  subroutine deepconv_main_nml ( cfgfile  )

    use dc_message,    only: MessageNotify
    use dc_iounit,     only : FileOpen    

    implicit none

    character(*), intent(in)      :: cfgfile  ! nml file
    integer                       :: unit     !装置番号

    NAMELIST /deepconv_main_nml /  &
      & sw_moist,                  &
      & sw_NH4SH,                  &
      & FlagTurbulence,            &
      & FlagRadiation,             &
      & FlagCloudMicroPhys,        &
      & FlagSurfaceHeating

    call FileOpen(unit, file=cfgfile, mode='r')
    read(unit, NML=deepconv_main_nml)
    close(unit)

    if (FlagTurbulence == "Steady") then 
      ID_FlagTurbulence = ID_FlagTurbulence_Steady
    elseif (FlagTurbulence == "KW1978") then
      ID_FlagTurbulence = ID_FlagTurbulence_KW1978 
    end if

    if (FlagRadiation == "HeatConst") then 
      ID_FlagRadiation = ID_FlagRadiation_HeatConst 
    elseif (FlagRadiation == "NewtonCooling") then 
      ID_FlagRadiation = ID_FlagRadiation_NewtonCooling
    end if

    if (FlagSurfaceHeating == "Diff") then 
      ID_FlagSurfaceHeating = ID_FlagSurfaceHeating_Diff 
    elseif (FlagSurfaceHeating == "Bulk") then 
      ID_FlagSurfaceHeating = ID_FlagSurfaceHeating_Bulk
    end if

    if (sw_moist) then 
      if (FlagCloudMicroPhys == "K1969") then 
        ID_FlagCloudMicroPhys = ID_FlagCloudMicroPhys_K1969 
      end if
    end if
!    call MessageNotify( "M", "main", "sw_moist = %c", c1=sw_moist)
!    call MessageNotify( "M", "main", "sw_NH4SH = %c", c1=sw_NH4SH) 

    call MessageNotify( "M", "main", "FlagTurbulence        = %c", c1=trim(FlagTurbulence)) 
    call MessageNotify( "M", "main", "ID_FlagTurbulence     = %i", i=(/ID_FlagTurbulence/))
    call MessageNotify( "M", "main", "FlagRadiation         = %c", c1=trim(FlagRadiation)) 
    call MessageNotify( "M", "main", "ID_FlagRadiation      = %i", i=(/ID_FlagRadiation/))
    call MessageNotify( "M", "main", "FlagSurfaceHeating    = %c", c1=trim(FlagSurfaceHeating)) 
    call MessageNotify( "M", "main", "ID_FlagSurfaceHeating = %i", i=(/ID_FlagSurfaceHeating/))
    call MessageNotify( "M", "main", "FlagCloudMicroPhys    = %c", c1=trim(FlagCloudMicroPhys)) 
    call MessageNotify( "M", "main", "ID_FlagCloudMicroPhys = %i", i=(/ID_FlagCloudMicroPhys/))
    
  end subroutine deepconv_main_nml
end program deepconv_arare
