!= dcpam 水惑星実験用主プログラム
!
!= dcpam main program for aqua planet experiments
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: dcpam_ape.f90,v 1.13 2008-06-14 11:44:17 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

program dcpam_ape
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 水惑星, すなわち全球が水に覆われているような惑星大気の計算を
  ! 行います. 
  !
  ! Calculation of 
  ! atmosphere on a planet covered with water globally is performed. 
  !

  ! モジュール引用 ; USE statements
  !

  ! 力学過程 (スペクトル法, Arakawa and Suarez (1983))
  ! Dynamical process (Spectral method, Arakawa and Suarez (1983))
  !
  use dyn_hspl_vas83, only: Dynamics

  ! 放射フラックス
  ! Radiation flux
  !
  use radiation, only: RadiationFlux, RadiationCorrect, RadiationDTempDt

  ! 鉛直拡散フラックス (Mellor and Yamada, 1974, レベル 2)
  ! Vertical diffusion flux (Mellor and Yamada, 1974, Level 2)
  !
!!!USE!!!  use vdiffusion_my1974, only: VerticalDiffusion

  ! 積雲パラメタリゼーション (対流調節)
  ! Cumulus parameterization (convection adjust)
  !
!!!USE!!!  use cumulus_adjust, only: Cumulus

  ! 温度の半整数σレベルの補間, 気圧とジオポテンシャルの算出
  ! Interpolate temperature on half sigma level, 
  ! and calculate pressure and geo-potential
  !
  use intpol_half, only: IntpolHalfLevel

  ! 陰解法のための行列処理 (一部の物理過程用)
  ! Matrices handling for implicit scheme (for a part of physical processes)
  !
!!!USE!!!  use phy_implicit, only: GetMatrices, ImplicitTendency

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 倍精度実数型. Double precision. 
    &                 STRING, &  ! 文字列.       Strings. 
    &                 TOKEN      ! キーワード.   Keywords. 

  ! 時刻管理
  ! Time control
  !
  use timeset, only: TimesetProgress, &
    &                Nstep       ! ループ回数. Number of times of loops

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

  ! リスタートデータ入出力
  ! Restart data input/output
  !
  use restart_file_io, only: RestartFileOutPut


  ! 宣言文 ; Declaration statements
  !
  implicit none

  !  予報変数 (ステップ $ t-\Delta t $ , $ t $ , $ t+\Delta t $ )
  !  Prediction variables  (Step $ t-\Delta t $ , $ t $ , $ t+\Delta t $ )
  !
  real(DP), allocatable:: xyz_UB (:,:,:)
                              ! $ u (t-\Delta t) $ .   東西風速. Eastward wind
  real(DP), allocatable:: xyz_VB (:,:,:)
                              ! $ v (t-\Delta t) $ .   南北風速. Northward wind
  real(DP), allocatable:: xyz_TempB (:,:,:)
                              ! $ T (t-\Delta t) $ .   温度. Temperature
  real(DP), allocatable:: xyz_QVapB (:,:,:)
                              ! $ q (t-\Delta t) $ .   比湿. Specific humidity
  real(DP), allocatable:: xy_PsB (:,:)
                              ! $ p_s (t-\Delta t) $ . 地表面気圧. Surface pressure
  real(DP), allocatable:: xyz_UN (:,:,:)
                              ! $ u (t) $ .     東西風速. Eastward wind
  real(DP), allocatable:: xyz_VN (:,:,:)
                              ! $ v (t) $ .     南北風速. Northward wind
  real(DP), allocatable:: xyz_TempN (:,:,:)
                              ! $ T (t) $ .     温度. Temperature
  real(DP), allocatable:: xyz_QVapN (:,:,:)
                              ! $ q (t) $ .     比湿. Specific humidity
  real(DP), allocatable:: xy_PsN (:,:)
                              ! $ p_s (t) $ .   地表面気圧. Surface pressure
  real(DP), allocatable:: xyz_UA (:,:,:)
                              ! $ u (t+\Delta t) $ .   東西風速. Eastward wind
  real(DP), allocatable:: xyz_VA (:,:,:)
                              ! $ v (t+\Delta t) $ .   南北風速. Northward wind
  real(DP), allocatable:: xyz_TempA (:,:,:)
                              ! $ T (t+\Delta t) $ .   温度. Temperature
  real(DP), allocatable:: xyz_QVapA (:,:,:)
                              ! $ q (t+\Delta t) $ .   比湿. Specific humidity
  real(DP), allocatable:: xy_PsA (:,:)
                              ! $ p_s (t+\Delta t) $ . 地表面気圧. Surface pressure


  !  診断変数
  !  Diagnostic variables
  !
  real(DP), allocatable:: xyz_DUDt (:,:,:)
                              ! $ \DP{u}{t} $ . 東西風速変化. 
                              ! Zonal wind tendency
  real(DP), allocatable:: xyz_DVDt (:,:,:)
                              ! $ \DP{v}{t} $ . 南北風速変化. 
                              ! Meridional wind tendency
  real(DP), allocatable:: xyz_DTempDt (:,:,:)
                              ! $ \DP{T}{t} $ . 温度変化. 
                              ! Temperature tendency
  real(DP), allocatable:: xyz_DQVapDt (:,:,:)
                              ! $ \DP{q}{t} $ . 比湿変化. 
                              ! Temperature tendency

  real(DP), allocatable:: xy_SurfAlbedo (:, :)
                              ! 地表アルベド. 
                              ! Surface albedo

  real(DP), allocatable:: xyza_UVMtx (:,:,:,:)
                              ! 速度陰解行列. 
                              ! Implicit matrix about velocity 
  real(DP), allocatable:: xyza_TempMtx  (:,:,:,:)
                              ! 温度陰解行列. 
                              ! Implicit matrix about temperature
  real(DP), allocatable:: xyza_QVapMtx (:,:,:,:)
                              ! 比湿陰解行列. 
                              ! Implicit matrix about specific humidity

  real(DP), allocatable:: xyr_Temp (:,:,:)
                              ! $ \hat{T} $ . 温度 (半整数レベル). 
                              ! Temperature (half level)
  real(DP), allocatable:: xyz_Press (:,:,:)
                              ! $ p $ . 気圧 (整数レベル). 
                              ! Air pressure (full level)
  real(DP), allocatable:: xyr_Press (:,:,:)
                              ! $ \hat{p} $ . 気圧 (半整数レベル). 
                              ! Air pressure (half level)
  real(DP), allocatable:: xyz_GeoPot (:,:,:)
                              ! $ \phi $ . ジオポテンシャル (整数レベル). 
                              ! Geo-potential (full level)
  real(DP), allocatable:: xyr_GeoPot (:,:,:)
                              ! $ \hat{\phi} $ . ジオポテンシャル (半整数レベル). 
                              ! Geo-potential (half level)

  real(DP), allocatable:: xyr_RadLFlux (:,:,:)
                              ! 長波フラックス. 
                              ! Longwave flux
  real(DP), allocatable:: xyr_RadSFlux (:,:,:)
                              ! 短波 (日射) フラックス. 
                              ! Shortwave (insolation) flux
  real(DP), allocatable:: xya_SurfRadLMtx (:,:,:)
                              ! $ T $ .  陰解行列: 地表. 
                              ! implicit matrix: surface
  real(DP), allocatable:: xyra_DelRadLFlux (:,:,:,:)
                              ! 長波地表温度変化. 
                              ! Surface temperature tendency with longwave

  real(DP), allocatable:: xyr_UFlux (:,:,:)
                              ! 東西風速フラックス. 
                              ! Zonal wind flux
  real(DP), allocatable:: xyr_VFlux (:,:,:)
                              ! 南北風速フラックス. 
                              ! Meridional wind flux
  real(DP), allocatable:: xyr_TempFlux (:,:,:)
                              ! 温度フラックス. 
                              ! Temperature flux
  real(DP), allocatable:: xyr_QVapFlux (:,:,:)
                              ! 比湿フラックス. 
                              ! Specific humidity flux

  real(DP), allocatable:: xy_DSurfTempDt (:,:)
                              ! 地表面温度変化率. 
                              ! Surface temperature tendency

  real(DP), allocatable:: xyz_DRadLTempDt (:,:,:)
                              ! 長波加熱率. 
                              ! Temperature tendency with longwave
  real(DP), allocatable:: xyz_DRadSTempDt (:,:,:)
                              ! 短波加熱率. 
                              ! Temperature tendency with shortwave

  !  作業変数
  !  Work variables
  !
  integer:: i                 ! 時間方向に回る DO ループ用作業変数. 
                              ! Work variables for DO loop in time
  logical:: firstloop = .true.
                              ! 初回のループであることを示すフラグ. 
                              ! Flag implying first loop

  ! 実行文 ; Executable statement
  !

  ! 主プログラムの初期化 (内部サブルーチン)
  ! Initialization for the main program (Internal subroutine)
  !
  call MainInit


  ! 時間積分
  ! Time integration
  !
  do i = 1, Nstep

    ! 地表面条件の設定
    ! Configure surface conditions
    !
    xy_SurfAlbedo = 0.3_DP

    ! 温度の半整数σレベルの補間, 気圧とジオポテンシャルの算出
    ! Interpolate temperature on half sigma level, 
    ! and calculate pressure and geo-potential
    !
    call IntpolHalfLevel( &
      & xy_PsN,     xyz_TempN, &   ! (in)
      & xyr_Temp, &                ! (out)
      & xyz_Press,  xyr_Press, &   ! (out)
      & xyz_GeoPot, xyr_GeoPot )   ! (out)

    ! 陰解法のための行列作成
    ! Create matrices for implicit scheme
    !
    call GetMatrices( &
      & xyr_Press, &                                        ! (in)
      & xyr_UFlux, xyr_VFlux, xyr_TempFlux, xyr_QVapFlux, & ! (out)
      & xyza_UVMtx, xyza_TempMtx, xyza_QVapMtx )            ! (out)

    ! 放射フラックス
    ! Radiation flux
    !
    call RadiationFlux( & 
      & xyz_TempN, xyz_QVapN, xyr_Press, xy_SurfAlbedo, &  ! (in)
      & xyr_RadLFlux, xyr_RadSFlux, &                      ! (out)
      & xya_SurfRadLMtx, xyra_DelRadLFlux )                ! (out)

!!$    ! 鉛直拡散フラックス
!!$    ! Vertical diffusion flux
!!$    !
!!$    call VerticalDiffusion( &
!!$      & xyz_UN,    xyz_VN,    xyz_QVapN, &                  ! (in)
!!$      & xyz_TempN, xyr_Temp, &                              ! (in)
!!$      & xyz_Press, xyr_Press, xyz_GeoPot,   xyr_GeoPot, &   ! (in)
!!$      & xyr_UFlux, xyr_VFlux, xyr_TempFlux, xyr_QVapFlux, & ! (inout)
!!$      & xyza_UVMtx, xyza_TempMtx, xyza_QVapMtx  )           ! (inout)

    ! 一部の物理過程の時間変化率の計算 (陰解法)
    ! Calculate tendency by a part of physical processes (implicit)
    !
    call ImplicitTendency( &
      & xyr_UFlux, xyr_VFlux, xyr_TempFlux, xyr_QVapFlux, & ! (in)
      & xyr_RadSFlux(:,:,0), xyr_RadLFlux(:,:,0), &         ! (in)
      & xyza_UVMtx, xyza_TempMtx, xyza_QVapMtx, &           ! (in)
!!$      & xy_SurfUVMtx, xyaa_SurfTempMtx, &                ! (in)
!!$      & xyaa_SurfQVapMtx, &                              ! (in)
      & xya_SurfRadLMtx, &                                  ! (in)
      & xyz_DUDt, xyz_DVDt, xyz_DTempDt, xyz_DQVapDt, &     ! (out)
      & xy_DSurfTempDt )                                    ! (out)

    ! 放射フラックス補正
    ! Radiation flux correction
    !
    call RadiationCorrect( &
      & xyz_DTempDt, xy_DSurfTempDt, xyra_DelRadLFlux, & ! (in)
      & xyr_RadLFlux )                                   ! (inout)

    ! 放射による温度変化率
    ! Temperature tendency with radiation
    !
    call RadiationDTempDt( &
      & xyr_RadLFlux, xyr_RadSFlux, xyr_Press, & ! (in)
      & xyz_DRadLTempDt, xyz_DRadSTempDt )       ! (out)

    xyz_DTempDt = xyz_DTempDt + xyz_DRadLTempDt + xyz_DRadSTempDt

    ! 力学過程
    ! Dynamical core
    !
    call Dynamics( &
      & xyz_UB,   xyz_VB,   xyz_TempB,   xyz_QVapB,   xy_PsB, &   ! (in)
      & xyz_UN,   xyz_VN,   xyz_TempN,   xyz_QVapN,   xy_PsN, &   ! (in)
      & xyz_DUDt, xyz_DVDt, xyz_DTempDt, xyz_DQVapDt, &           ! (in)
      & xyz_UA,   xyz_VA,   xyz_TempA,   xyz_QVapA,   xy_PsA  )   ! (out)

!!$    ! 積雲パラメタリゼーション
!!$    ! Cumulus parameterization
!!$    !
!!$    call Cumulus( &
!!$      & xyz_TempN, xyz_QVapN, &  ! (inout)
!!$      & xyz_Press, xyr_Press )   ! (in)

!!$    ! 時間フィルター
!!$    ! Time filter
!!$    !
!!$    call TimeFilter( &
!!$      & xyz_UB, xyz_VB, xyz_TempB, xyz_QVapB, xy_PsB, &   ! (in)
!!$      & xyz_UN, xyz_VN, xyz_TempN, xyz_QVapN, xy_PsN, &   ! (inout)
!!$      & xyz_UA, xyz_VA, xyz_TempA, xyz_QVapA, xy_PsA  )   ! (in)

    ! ヒストリデータ出力
    ! History data output
    !
    if ( .not. firstloop ) then
      call HistoryFileOutput( 'U',    xyz_UN )
      call HistoryFileOutput( 'V',    xyz_VN )
      call HistoryFileOutput( 'Temp', xyz_TempN )
      call HistoryFileOutput( 'QVap', xyz_QVapN )
      call HistoryFileOutput( 'Ps',   xy_PsN )
    end if

    ! 予報変数の時刻付け替え
    ! Exchange time of prediction variables
    !
    xyz_UB    = xyz_UN    ; xyz_UN    = xyz_UA    ; xyz_UA    = 0.0_DP
    xyz_VB    = xyz_VN    ; xyz_VN    = xyz_VA    ; xyz_VA    = 0.0_DP
    xyz_TempB = xyz_TempN ; xyz_TempN = xyz_TempA ; xyz_TempA = 0.0_DP
    xyz_QVapB = xyz_QVapN ; xyz_QVapN = xyz_QVapA ; xyz_QVapA = 0.0_DP
    xy_PsB    = xy_PsN    ; xy_PsN    = xy_PsA    ; xy_PsA    = 0.0_DP

    ! 時刻の進行
    ! Progress time
    !
    call TimesetProgress

    ! リスタートデータ出力
    ! Restart data output
    !
    if ( .not. firstloop ) then
      call RestartFileOutput( &
        & xyz_UB, xyz_VB, xyz_TempB, xyz_QVapB, xy_PsB, &   ! (in)
        & xyz_UN, xyz_VN, xyz_TempN, xyz_QVapN, xy_PsN  )   ! (in)
    end if

    firstloop = .false.
  end do

  ! 主プログラムの終了処理 (内部サブルーチン)
  ! Termination for the main program (Internal subroutine)
  !
  call MainTerminate



contains



  subroutine MainInit
    !
    ! 主プログラムの初期化手続き. 
    !
    ! Initialization procedure for the main program. 
    !

    ! コマンドライン引数処理
    ! Command line option parser
    !
    use option_parser, only: OptParseInit

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

    ! 時刻管理
    ! Time control
    !
    use timeset, only: TimesetInit, TimesetClockStart

    ! 格子点設定
    ! Grid points settings
    !
    use gridset, only: GridsetInit, &
      &                imax, & ! 経度格子点数. 
                               ! Number of grid points in longitude
      &                jmax, & ! 緯度格子点数. 
                               ! Number of grid points in latitude
      &                kmax    ! 鉛直層数. 
                               ! Number of vertical level

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: ConstantsInit

    ! 座標データ設定
    ! Axes data settings
    !
    use axesset, only: AxessetInit

    ! リスタートデータ入出力
    ! Restart data input/output
    !
    use restart_file_io, only: RestartFileOpen, RestartFileGet

    ! ヒストリデータ出力
    ! History data output
    !
    use history_file_io, only: HistoryFileOpen, HistoryFileRegister, HistoryFileOutput

    ! 文字列操作
    ! Character handling
    !
    use dc_string, only: StoA

    ! 実行文 ; Executable statement
    !

    ! コマンドライン引数処理
    ! Command line option parser
    !
    call OptParseInit

    ! NAMELIST ファイル名入力
    ! Input NAMELIST file name
    !
    call NmlutilInit

    ! 時刻管理
    ! Time control
    !
    call TimesetInit

    ! 計算時間計測開始
    ! Start measurement of computation time
    !
    call TimesetClockStart( 'total' )

    ! 格子点設定
    ! Grid points settings
    !
    call GridsetInit

    ! 物理定数設定
    ! Physical constants settings
    !
    call ConstantsInit

    ! 座標データ設定
    ! Axes data settings
    !
    call AxessetInit

    ! 予報変数の割付
    ! Allocation of prediction variables
    !
    allocate( xyz_UB    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_VB    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_TempB (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_QVapB (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xy_PsB    (0:imax-1, 1:jmax) )

    allocate( xyz_UN    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_VN    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_TempN (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_QVapN (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xy_PsN    (0:imax-1, 1:jmax) )

    allocate( xyz_UA    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_VA    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_TempA (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_QVapA (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xy_PsA    (0:imax-1, 1:jmax) )

    ! リスタートデータ入力
    ! Restart data input
    !
    call RestartFileGet( &
      & xyz_UB, xyz_VB, xyz_TempB, xyz_QVapB, xy_PsB, &   ! (out)
      & xyz_UN, xyz_VN, xyz_TempN, xyz_QVapN, xy_PsN  )   ! (out)

    ! リスタートデータ出力
    ! Restart data output
    !
    call RestartFileOpen

    ! ヒストリデータファイルの初期化
    ! Initialization of history data files
    !
    call HistoryFileOpen

    ! ヒストリデータ出力のためのへの変数登録
    ! Register of variables for history data output
    !
    call HistoryFileRegister( 'U' , 'eastward wind', &
      & 'm s-1', StoA('lon', 'lat', 'sig', 'time') )

    call HistoryFileRegister( 'V' , 'northwardward wind', &
      & 'm s-1', StoA('lon', 'lat', 'sig', 'time') )

    call HistoryFileRegister( 'Temp' , 'temperature', &
      & 'K', StoA('lon', 'lat', 'sig', 'time') )

    call HistoryFileRegister( 'QVap' , 'specific humidity', &
      & 'kg kg-1', StoA('lon', 'lat', 'sig', 'time') )

    call HistoryFileRegister( 'Ps' , 'surface pressure', &
      & 'Pa', StoA('lon', 'lat', 'time') )

    ! ヒストリデータ出力 (スタート時刻)
    ! History data output (Start time)
    !
    call HistoryFileOutput( 'U', xyz_UN )
    call HistoryFileOutput( 'V', xyz_VN )
    call HistoryFileOutput( 'Temp', xyz_TempN )
    call HistoryFileOutput( 'QVap', xyz_QVapN )
    call HistoryFileOutput( 'Ps', xy_PsN )


    ! 診断変数の割付
    ! Allocation of diagnostic variables
    !
    allocate( xyz_DUDt    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_DVDt    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_DTempDt (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_DQVapDt (0:imax-1, 1:jmax, 1:kmax) )

    allocate( xy_SurfAlbedo (0:imax-1, 1:jmax) )

    allocate( xyza_UVMtx   (0:imax-1, 1:jmax, 1:kmax, -1:1) )
    allocate( xyza_TempMtx (0:imax-1, 1:jmax, 1:kmax, -1:1) )
    allocate( xyza_QVapMtx (0:imax-1, 1:jmax, 1:kmax, -1:1) )

    allocate( xyr_Temp   (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xyz_Press  (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyr_Press  (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xyz_GeoPot (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyr_GeoPot (0:imax-1, 1:jmax, 0:kmax) )

    allocate( xyr_RadLFlux     (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xyr_RadSFlux     (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xya_SurfRadLMtx  (0:imax-1, 1:jmax,         -1:1) )
    allocate( xyra_DelRadLFlux (0:imax-1, 1:jmax, 0:kmax,  0:1) )

    allocate( xyr_UFlux    (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xyr_VFlux    (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xyr_TempFlux (0:imax-1, 1:jmax, 0:kmax) )
    allocate( xyr_QVapFlux (0:imax-1, 1:jmax, 0:kmax) )

    allocate( xy_DSurfTempDt (0:imax-1, 1:jmax) )

    allocate( xyz_DRadLTempDt (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_DRadSTempDt (0:imax-1, 1:jmax, 1:kmax) )

  end subroutine MainInit



  subroutine MainTerminate
    !
    ! 主プログラムの終了処理手続き. 
    !
    ! Termination procedure for the main program. 
    !

    ! 時刻管理
    ! Time control
    !
    use timeset, only: TimesetClockStop, TimesetClose

    ! リスタートデータ入出力
    ! Restart data input/output
    !
    use restart_file_io, only: RestartFileClose

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

    ! 実行文 ; Executable statement
    !

    ! リスタートデータファイルクローズ
    ! Close restart data input
    !
    call RestartFileClose

    ! ヒストリデータファイルクローズ
    ! Close history data input
    !
    call HistoryFileClose

    ! 計算時間計測終了
    ! Stop measurement of computation time
    !
    call TimesetClockStop( 'total' )

    ! 時刻管理終了処理
    ! Termination of time control
    !
    call TimesetClose

  end subroutine MainTerminate

end program dcpam_ape
