!= Held and Suarez (1994) による強制と散逸
!
!= Forcing and dissipation suggested by Held and Suarez (1994)
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: phy_hs94.f90,v 1.3 2007/07/31 04:00:57 morikawa Exp $
! Tag Name::  $Name: dcpam4-20070731 $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   
!

module phy_hs94
  != Held and Suarez (1994) による強制と散逸
  !
  != Forcing and dissipation suggested by Held and Suarez (1994)
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! Held and Suarez (1994) で提案される乾燥大気 GCM ベンチマーク用の
  ! 強制と散逸を計算します. 
  ! 与える強制と散逸として, 温度場の帯状対称場への簡単なニュートン冷却と, 
  ! 境界層摩擦を表現する下層風のレイリー摩擦を用います. 
  ! 詳細を以下に記します. 
  !
  ! Forcing and dissipation for dry air GCM benchmark
  ! suggested by Held and Suarez (1994) are caluclate.
  ! We use simple Newtonian relaxation of the temperature field to a
  ! zonally symmetric state and Rayleigh damping of low-level winds to
  ! represent boundary-layer friction.
  ! Their specifications are detailed as follows.
  !
  ! \[
  !    \left( \DP{\Dvect{v}}{t} \right)_{\mathrm{HS94}} = 
  !        - k_v (\sigma) \Dvect{v}, \] \[
  !    \left( \DP{T}{t} \right)_{\mathrm{HS94}} = 
  !        - k_T (\phi, \sigma) [T - T_{eq} (\phi,p)], \] \[
  !    T_{eq} = \mathrm{max}
  !     \left\{
  !        200 \mathrm{K}, 
  !        \left[
  !          315 \mathrm{K} - (\Delta T)_y \sin^2\phi 
  !                         - (\Delta \theta)_z 
  !                           \log \left(\frac{p}{p_0}\right) \cos^2\phi
  !        \right] \left(\frac{p}{p_0}\right)^\kappa
  !     \right\}, \] \[
  !    k_T = k_a + (k_s - k_a) 
  !          \mathrm{max} 
  !          \left(0, \frac{\sigma - \sigma_b}{1 - \sigma_b}\right) \cos^4\phi,
  !     \] \[
  !    k_v = k_f
  !          \mathrm{max} 
  !          \left(0, \frac{\sigma - \sigma_b}{1 - \sigma_b}\right),
  !     \] \[
  !    \sigma_b = 0.7, \qquad 
  !    k_f = 1 \mathrm{day}^{-1}, \qquad
  !    k_a = \Dinv{40} \mathrm{day}^{-1}, \qquad
  !    k_s = \Dinv{4} \mathrm{day}^{-1}, \] \[
  !    (\Delta T)_y = 60 \mathrm{K}, \qquad
  !    (\Delta \theta)_z = 10 \mathrm{K}, \qquad
  !    p_0 = 1000 \mathrm{hPa}, \qquad
  !    \kappa = \frac{R}{c_p}.
  ! \]
  !
  ! Forcing では, 与えられた速度や温度 ( $ t+\Delta t$ を想定) 
  ! に対して以下のように強制と散逸を適用します.
  !
  ! By Forcing, forcing and dissipation are applied to 
  ! given wind and temperature ($ t+\Delta t$ is expected) as follows.
  !
  ! \[
  !    \hat{\Dvect{v}}^{t+\Delta t} = 
  !      \Dvect{v}^{t+\Delta t} 
  !      + 2 \Delta t \left( \DP{\Dvect{v}}{t} \right)_{\mathrm{HS94}} \] \[
  !    \hat{T}^{t+\Delta t} = 
  !      T^{t+\Delta t} 
  !      + 2 \Delta t \left( \DP{T}{t} \right)_{\mathrm{HS94}}
  ! \]
  !
  !== Procedures List
  !
  ! Create        :: PHYHS94 型変数の初期設定
  ! Close         :: PHYHS94 型変数の終了処理
  ! PutLine       :: PHYHS94 型変数に格納されている情報の印字
  ! initialized   :: PHYHS94 型変数が初期設定されているか否か
  ! Forcing       :: 強制と散逸の計算
  ! ------------  :: ------------
  ! Create        :: Constructor of "PHYHS94"
  ! Close         :: Deconstructor of "PHYHS94"
  ! PutLine       :: Print information of "PHYHS94"
  ! initialized   :: Check initialization of "PHYHS94"
  ! Forcing       :: Calculate forcing and dissipation
  !
  !== Usage
  !
  ! 始めに, PHYHS94 型の変数を定義し, Create で初期設定を行います.
  ! 強制と散逸の計算に Forcing を用います.
  ! PHYHS94 型の変数の終了処理には Close を用いてください.
  !
  ! First, initialize "PHYHS94" by Create.
  ! Use "Forcing" in order to calculate forcing and dissipation
  ! In order to terminate "PHYHS94", use Close.
  !
  !== References
  !
  ! * Held, I. M., Suarez, M. J., 1994: 
  !   A proposal for the intercomparison of the dynamical cores of
  !   atmospheric general circuation models.
  !   <i>Bull. Am. Meteor. Soc.</i>, <b>75</b>, 1825--1830.
  !

  use dc_types, only: DP, TOKEN
  implicit none
  private
  public:: PHYHS94, Create, Close, PutLine, initialized, Forcing

  type PHYHS94
    !
    ! まず, Create で "PHYHS94" 型の変数を初期設定して下さい.
    ! 初期設定された "PHYHS94" 型の変数を再度利用する際には,
    ! Close によって終了処理を行ってください.
    !
    ! Initialize "PHYHS94" variable by "Create" before usage.
    ! If you reuse "PHYHS94" variable again for another application, 
    ! terminate by "Close".
    !
    logical:: initialized = .false.     ! 初期設定フラグ. 
                                        ! Initialization flag
    integer:: imax ! 経度格子点数. 
                   ! Number of grid points in longitude
    integer:: jmax ! 緯度格子点数. 
                   ! Number of grid points in latitude
    integer:: kmax ! 鉛直層数. 
                   ! Number of vertical level
    real(DP), pointer:: y_Lat (:) =>null()
                              ! 緯度. Latitude
    real(DP), pointer:: z_Sigma (:) =>null()
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP):: DelTime    ! $ \Delta t $ . タイムステップ. Time step
    real(DP):: Kappa      ! $ \kappa = R/C_p $ .
                          ! 気体定数の定圧比熱に対する比. Ratio of gas constant to specific heat

    !-----------------------------------
    !  Held and Suarez (1994) で使用される係数
    !  Coefficient used by Held and Suarez (1994) 
    real(DP):: P0             ! $ p_0 $ .
    real(DP):: DelTempY       ! $ (\Delta T)_y $ .
    real(DP):: DelPotTempZ    ! $ (\Delta \theta)_z $ .
    real(DP), pointer:: z_kv (:) =>null()
                              ! $ k_v $ .
    real(DP), pointer:: yz_kt (:,:) =>null()
                              ! $ k_T $ .
  end type PHYHS94

  character(*), parameter:: version = &
    & '$Name: dcpam4-20070731 $' // &
    & '$Id: phy_hs94.f90,v 1.3 2007/07/31 04:00:57 morikawa Exp $'

  interface Create
    module procedure PhyHS94Create
  end interface

  interface Close
    module procedure PhyHS94Close
  end interface

  interface PutLine
    module procedure PhyHS94PutLine
  end interface

  interface initialized
    module procedure PhyHS94Initialized
  end interface

  interface NmlRead
    module procedure PhyHS94NmlRead
  end interface

  interface Forcing
    module procedure PhyHS94Forcing
  end interface

!!$  interface Sample
!!$    module procedure PhyHS94Sample
!!$  end interface

contains

  subroutine PhyHS94Create( phy_hs, &
    & imax, jmax, kmax, &
    & y_Lat, z_Sigma, DelTime, &
    & Cp, RAir, &
    & day_seconds, &
    & nmlfile, err )
    !
    ! PHYHS94 型の変数の初期設定を行います.
    ! 他のサブルーチンを使用する前に必ずこのサブルーチンによって
    ! PHYHS94 型の変数を初期設定してください.
    !
    ! なお, 与えられた *phy_hs* が既に初期設定されている場合,
    ! プログラムはエラーを発生させます.
    !
    ! NAMELIST を利用する場合には引数 *nmlfile* に NAMELIST ファイル名
    ! を与えてください.
    !
    ! Constructor of "PHYHS94".
    ! Initialize *phy_hs* by this subroutine, 
    ! before other procedures are used, 
    !
    ! Note that if *phy_hs* is already initialized 
    ! by this procedure, error is occurred.
    !
    ! In order to use NAMELIST, specify a NAMELIST filename to 
    ! argument *nmlfile*.
    !
    use dc_trace, only: BeginSub, EndSub, DbgMessage
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_present, only: present_and_not_empty, present_and_true
    use dc_message, only: MessageNotify
    use dc_error, only: StoreError, DC_NOERR, DC_EALREADYINIT, &
      & DC_EARGLACK, DC_ENEGATIVE, DC_ENOFILEREAD
    use dc_date_types, only: DAY_SECONDS_EARTH
    implicit none
    type(PHYHS94), intent(inout):: phy_hs
    integer, intent(in):: imax ! 経度格子点数. 
                               ! Number of grid points in longitude
    integer, intent(in):: jmax ! 緯度格子点数. 
                               ! Number of grid points in latitude
    integer, intent(in):: kmax ! 鉛直層数. 
                               ! Number of vertical level
    real(DP), intent(in):: y_Lat (0:jmax-1)
                              ! 緯度. Latitude
    real(DP), intent(in):: z_Sigma (0:kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP), intent(in):: DelTime    ! $ \Delta t $ . タイムステップ. Time step
    real(DP), intent(in):: Cp         ! $ C_p $ .    大気定圧比熱.   Specific heat of air at constant pressure
    real(DP), intent(in):: RAir       ! $ R $ .      大気気体定数.   Gas constant of air
    real(DP), intent(in), optional:: day_seconds
                              ! 1 日の秒数. 
                              ! この引数を与えない場合, 1 日の秒数は
                              ! 86400.0 となります. 
                              ! 
                              ! Seconds in day. 
                              ! If this argument is not given, seconds in day
                              ! become 86400.0 .
    character(*), intent(in), optional :: nmlfile
                              ! NAMELIST ファイルの名称. 
                              ! この引数に空文字以外を与えた場合, 
                              ! 指定されたファイルから 
                              ! NAMELIST 変数群を読み込みます. 
                              ! ファイルを読み込めない場合にはエラーを
                              ! 生じます.
                              !
                              ! NAMELIST file name. 
                              ! If nonnull character is specified to
                              ! this argument, 
                              ! NAMELIST group name is loaded from the 
                              ! file. 
                              ! If the file can not be read, 
                              ! an error occurs.
                              ! 
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    real(DP):: day_seconds_work
                              ! 1 日の秒数. 
                              ! Seconds in day. 
    real(DP):: SigmaB         ! $ \sigma_b $ .
    real(DP):: kf             ! $ k_f $ .
    real(DP):: ka             ! $ k_a $ .
    real(DP):: ks             ! $ k_s $ .

    integer:: j, k            ! DO ループ用作業変数
                              ! Work variables for DO loop
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'PhyHS94Create'
  continue
    call BeginSub( subname,  version = version, &
      & fmt = 'Cp=<%f> RAir=<%f>', &
      & d=(/Cp, RAir/) )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( phy_hs % initialized ) then
      stat = DC_EALREADYINIT
      cause_c = 'PHYHS94'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  設定値の正当性のチェック
    !  Validate setting values
    !-----------------------------------------------------------------
    if (imax < 1) then
      stat = DC_ENEGATIVE
      cause_c = 'imax'
      goto 999
    end if
    if (jmax < 1) then
      stat = DC_ENEGATIVE
      cause_c = 'jmax'
      goto 999
    end if
    if (kmax < 1) then
      stat = DC_ENEGATIVE
      cause_c = 'kmax'
      goto 999
    end if
    if (DelTime < 0.0_DP) then
      stat = DC_ENEGATIVE
      cause_c = 'DelTime'
      goto 999
    end if
    if ( present(day_seconds) ) then
      if (day_seconds < 0.0_DP) then
        stat = DC_ENEGATIVE
        cause_c = 'day_seconds'
        goto 999
      end if
    end if

    !-----------------------------------------------------------------
    !  "PHYHS94" の設定
    !  Configure the settings for "PHYHS94"
    !-----------------------------------------------------------------

    !-------------------------
    !  デフォルト値
    !  Default values
    phy_hs % imax  = imax 
    phy_hs % jmax  = jmax 
    phy_hs % kmax  = kmax 
    phy_hs % Kappa = RAir / Cp
    phy_hs % DelTime = DelTime

    allocate( phy_hs % y_Lat (0:jmax-1) )
    phy_hs % y_Lat = y_Lat

    allocate( phy_hs % z_Sigma (0:kmax-1) )
    phy_hs % z_Sigma = z_Sigma

    if ( present(day_seconds) ) then
      day_seconds_work = day_seconds
    else
      day_seconds_work = DAY_SECONDS_EARTH
    end if

    phy_hs % P0          = 1000.0e2_DP
    phy_hs % DelTempY    = 60.0_DP
    phy_hs % DelPotTempZ = 10.0_DP

    SigmaB = 0.7_DP
    kf     = 1.0_DP / day_seconds_work
    ka     = 1.0_DP / ( 40.0_DP * day_seconds_work )
    ks     = 1.0_DP / (  4.0_DP * day_seconds_work )

    call DbgMessage( &
      & 'day_seconds=<%f>, SigmaB=<%f>, kf=<%f>, ka=<%f>, ks=<%f>', &
      & d=(/day_seconds_work, SigmaB, kf, ka, ks/) )

    allocate( phy_hs % z_kv (0:kmax-1) )

    phy_hs % z_kv = &
      & kf * max( 0.0_DP, &
      &           (    z_Sigma - SigmaB ) &
      &             / ( 1.0_DP - SigmaB ) &
      &         )

    allocate( phy_hs % yz_kt (0:jmax-1,0:kmax-1) )

    do k = 0, kmax - 1
      do j = 0, jmax - 1
        phy_hs % yz_kt(j,k) = &
          & ka + ( ks - ka ) &
          &  * max( 0.0_DP, &
          &         ( z_Sigma(k) - SigmaB ) &
          &           / ( 1.0_DP - SigmaB ) &
          &       ) * cos( y_Lat(j) ) ** 4
      end do
    end do

    !-------------------------
    !  オプショナル引数からの値
    !  Values from optional arguments
!!$    phy_hs % param_i = param_i
!!$    if ( present(param_r) )  phy_hs % param_r = param_r
!!$    if ( present(param_c) )  phy_hs % param_c = param_c

    !-------------------------
    !  NAMELIST からの値
    !  Values from NAMELIST

!!$    if ( present_and_not_empty(nmlfile) ) then
!!$      call MessageNotify( 'M', subname, &
!!$        & 'Loading NAMELIST file "%c" ...', &
!!$        & c1=trim(nmlfile) )
!!$      call NmlRead ( nmlfile = nmlfile, &      ! (in)
!!$        & param_i = phy_hs % param_i, &   ! (inout)
!!$        & param_r = phy_hs % param_r, &   ! (inout)
!!$        & param_c_ = phy_hs % param_c, &  ! (inout)
!!$        & err = err )                          ! (out)
!!$      if ( present_and_true(err) ) then
!!$        call MessageNotify( 'W', subname, &
!!$          & '"%c" can not be read.', &
!!$          & c1=trim(nmlfile) )
!!$        stat = DC_ENOFILEREAD
!!$        cause_c = nmlfile
!!$        goto 999
!!$      end if
!!$    end if

    !-----------------------------------------------------------------
    !  設定値の正当性のチェック
    !  Validate setting values
    !-----------------------------------------------------------------

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
    phy_hs % initialized = .true.
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine PhyHS94Create

  subroutine PhyHS94Close( phy_hs, err )
    !
    ! PHYHS94 型の変数の終了処理を行います.
    ! なお, 与えられた *phy_hs* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Deconstructor of "PHYHS94".
    ! Note that if *phy_hs* is not initialized by Create yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(PHYHS94), intent(inout):: phy_hs
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'PhyHS94Close'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( .not. phy_hs % initialized ) then
      stat = DC_ENOTINIT
      cause_c = 'PHYHS94'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  "PHYHS94" の設定の消去
    !  Clear the settings for "PHYHS94"
    !-----------------------------------------------------------------
    deallocate( phy_hs % z_Sigma )
    deallocate( phy_hs % y_Lat )
    deallocate( phy_hs % z_kv )
    deallocate( phy_hs % yz_kt )

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
    phy_hs % initialized = .false.
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine PhyHS94Close

  subroutine PhyHS94PutLine( phy_hs, unit, indent, err )
    !
    ! 引数 *phy_hs* に設定されている情報を印字します.
    ! デフォルトではメッセージは標準出力に出力されます. 
    ! *unit* に装置番号を指定することで, 出力先を変更することが可能です.
    !
    ! Print information of *phy_hs*.
    ! By default messages are output to standard output.
    ! Unit number for output can be changed by *unit* argument.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    use dc_string, only: Printf
    implicit none
    type(PHYHS94), intent(in):: phy_hs
    integer, intent(in), optional:: unit
                              ! 出力先の装置番号.
                              ! デフォルトの出力先は標準出力.
                              !
                              ! Unit number for output.
                              ! Default value is standard output.
    character(*), intent(in), optional:: indent
                              ! 表示されるメッセージの字下げ.
                              !
                              ! Indent of displayed messages.
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: stat
    character(STRING):: cause_c
    integer:: out_unit
    integer:: indent_len
    character(STRING):: indent_str
    character(*), parameter:: subname = 'PhyHS94PutLine'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( present(unit) ) then
      out_unit = unit
    else
      out_unit = STDOUT
    end if

    indent_len = 0
    indent_str = ''
    if ( present(indent) ) then
      if ( len(indent) /= 0 ) then
        indent_len = len(indent)
        indent_str(1:indent_len) = indent
      end if
    end if


    !-----------------------------------------------------------------
    !  "PHYHS94" の設定の印字
    !  Print the settings for "PHYHS94"
    !-----------------------------------------------------------------
    if ( phy_hs % initialized ) then
      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & '#<PHYHS94:: @initialized=%y', &
        & l=(/phy_hs % initialized/))

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @imax=%d @jmax=%d @kmax=%d @DelTime', &
        & i=(/phy_hs % imax, phy_hs % jmax, phy_hs % kmax/), &
        & d=(/phy_hs % DelTime/) )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @y_Lat=%*f', &
        & d=phy_hs % y_Lat, n=(/phy_hs % jmax/) )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @z_Sigma=%*f', &
        & d=phy_hs % z_Sigma, n=(/phy_hs % kmax/) )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @Kappa=%f @P0=%f', &
        & d=(/phy_hs % Kappa, phy_hs % P0/) )

!!$      call Printf(out_unit, &
!!$        & indent_str(1:indent_len) // &
!!$        & ' @Cp=%f @RAir=%f @Kappa=%f', &
!!$        & d=(/phy_hs % Cp, phy_hs % RAir, phy_hs % Kappa/) )
!!$
!!$      call Printf(out_unit, &
!!$        & indent_str(1:indent_len) // &
!!$        & ' @SigmaB=%f @P0=%f @kf=%f @ka=%f @ks=%f', &
!!$        & d=(/phy_hs % SigmaB, phy_hs % P0, &
!!$        &     phy_hs % kf, phy_hs % ka, phy_hs % ks/) )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @DelTempY=%f @DelPotTempZ=%f', &
        & d=(/phy_hs % DelTempY, phy_hs % DelPotTempZ/) )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @z_kv=%*f', &
        & d=phy_hs % z_kv, n=(/phy_hs % kmax/) )

!!$      call Printf(out_unit, &
!!$        & indent_str(1:indent_len) // &
!!$        & ' @yz_kt=%*f', &
!!$        & d=pack( phy_hs % yz_kt ), n=(/phy_hs % jmax * phy_hs % kmax/) )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & '>' )
    else
      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & '#<PHYHS94:: @initialized=%y>', &
        & l=(/phy_hs % initialized/))
    end if

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine PhyHS94PutLine

  logical function PhyHS94Initialized( phy_hs ) result(result)
    !
    ! *phy_hs* が初期設定されている場合には .true. が,
    ! 初期設定されていない場合には .false. が返ります.
    !
    ! If *phy_hs* is initialized, .true. is returned.
    ! If *phy_hs* is not initialized, .false. is returned.
    !
    implicit none
    type(PHYHS94), intent(in):: phy_hs
  continue
    result = phy_hs % initialized
  end function PhyHS94Initialized

  subroutine PhyHS94NmlRead( nmlfile, &
!!$    & param_i, param_r, param_c_, &
    & err )
    !
    ! NAMELIST ファイル *nmlfile* から値を入力するための
    ! 内部サブルーチンです. Create 内で呼び出されることを
    ! 想定しています.
    !
    ! 値が NAMELIST ファイル内で指定されていない場合には,
    ! 入力された値がそのまま返ります.
    !
    ! なお, *nmlfile* に空文字が与えられた場合, または
    ! 与えられた *nmlfile* を読み込むことができない場合, 
    ! プログラムはエラーを発生させます.
    !
    ! This is an internal subroutine to input values from 
    ! NAMELIST file *nmlfile*. This subroutine is expected to be
    ! called by "Create".
    !
    ! A value not specified in NAMELIST file is returned
    ! without change.
    !
    ! If *nmlfile* is empty, or *nmlfile* can not be read, 
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_iounit, only: FileOpen
    use dc_message, only: MessageNotify
    use dc_present, only: present_and_true
    use dc_error, only: StoreError, DC_NOERR, DC_ENOFILEREAD
    implicit none
    character(*), intent(in):: nmlfile
                              ! NAMELIST ファイルの名称. 
                              ! NAMELIST file name
!!$    integer, intent(inout):: param_i
!!$    real(DP), intent(inout):: param_r
!!$    character(*), intent(inout):: param_c_
!!$    character(TOKEN):: param_c
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

!!$    namelist /phy_hs94_nml/ &
!!$      & param_i, param_r, param_c
                              ! phy_hs94 モジュール用
                              ! NAMELIST 変数群名.
                              !
                              ! NAMELIST group name for
                              ! 'phy_hs94' module.

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: stat
    character(STRING):: cause_c
    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号. 
                              ! Unit number for NAMELIST file open
!!$    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT. 
!!$                              ! IOSTAT of NAMELIST read
    character(*), parameter:: subname = 'PhyHS94NmlRead'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''



    !-----------------------------------------------------------------
    !  文字型引数を NAMELIST 変数群へ代入
    !  Substitute character arguments to NAMELIST group
    !-----------------------------------------------------------------
!!$    param_c = param_c_

    !----------------------------------------------------------------
    !  NAMELIST ファイルのオープン
    !  Open NAMELIST file
    !----------------------------------------------------------------
    call FileOpen( unit = unit_nml, & ! (out)
      & file = nmlfile, mode = 'r', & ! (in)
      & err = err )                   ! (out)
    if ( present_and_true(err) ) then
      stat = DC_ENOFILEREAD
      cause_c = nmlfile
      goto 999
    end if


    !-----------------------------------------------------------------
    !  NAMELIST 変数群の取得
    !  Get NAMELIST group
    !-----------------------------------------------------------------
!!$    read( unit = unit_nml, & ! (in)
!!$      & nml = phy_hs94_nml, iostat = iostat_nml ) ! (out)
!!$    if ( iostat_nml == 0 ) then
!!$      call MessageNotify( 'M', subname, &
!!$        & 'NAMELIST group "%c" is loaded from "%c".', &
!!$        & c1='phy_hs94_nml', c2=trim(nmlfile) )
!!$      write(STDOUT, nml = phy_hs94_nml)
!!$    else
!!$      call MessageNotify( 'W', subname, &
!!$        & 'NAMELIST group "%c" is not found in "%c" (iostat=%d).', &
!!$        & c1='phy_hs94_nml', c2=trim(nmlfile), &
!!$        & i=(/iostat_nml/) )
!!$    end if

    close( unit_nml )

    !-----------------------------------------------------------------
    !  NAMELIST 変数群を文字型引数へ代入
    !  Substitute NAMELIST group to character arguments
    !-----------------------------------------------------------------
!!$    param_c_ = param_c

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine PhyHS94NmlRead

  subroutine PhyHS94Forcing( phy_hs, &
    & xyz_U,    xyz_V, &
    & xyz_Temp, xy_Ps, &
    & err )
    !
    ! 引数として与えられた東西風速 xyz_U, 南北風速 xyz_V, 
    ! 温度 xyz_Temp に対し, 
    ! 温度場の帯状対称場への簡単なニュートン冷却と, 
    ! 境界層摩擦を表現する下層風のレイリー摩擦を用います. 
    !
    ! なお, 与えられた *phy_hs* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Simple Newtonian relaxation of the temperature field to a
    ! zonally symmetric state and Rayleigh damping of low-level winds to
    ! represent boundary-layer friction are applied to 
    ! arguments 
    ! zonal wind 'xyz_U', meridional wind 'xyz_V', 
    ! temperature 'xyz_Temp'.
    !
    ! If *phy_hs* is not initialized by Create yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(PHYHS94), intent(inout):: phy_hs
    real(DP), intent(inout):: xyz_U (0:phy_hs%imax-1, 0:phy_hs%jmax-1, 0:phy_hs%kmax-1)
                              ! $ U $ . 東西風速. 
                              ! Zonal wind
    real(DP), intent(inout):: xyz_V (0:phy_hs%imax-1, 0:phy_hs%jmax-1, 0:phy_hs%kmax-1)
                              ! $ V $ . 南北風速. 
                              ! Meridional wind
    real(DP), intent(inout):: xyz_Temp (0:phy_hs%imax-1, 0:phy_hs%jmax-1, 0:phy_hs%kmax-1)
                              ! $ T $ . 温度. 
                              ! Temperature
    real(DP), intent(in):: xy_Ps (0:phy_hs%imax-1, 0:phy_hs%jmax-1)
                              ! $ P_s $ . 地表面気圧. 
                              ! Surface pressure
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: jmax ! 緯度格子点数. 
                   ! Number of grid points in latitude
    integer:: kmax ! 鉛直層数. 
                   ! Number of vertical level

    real(DP):: y_Lat (0:phy_hs%jmax-1)
                              ! 緯度. Latitude
    real(DP):: z_Sigma (0:phy_hs%kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP):: DelTime    ! $ \Delta t $ . タイムステップ. Time step
    real(DP):: Kappa      ! $ \kappa = R/C_p $ .
                          ! 気体定数の定圧比熱に対する比. Ratio of gas constant to specific heat

    real(DP):: P0             ! $ p_0 $ .
    real(DP):: DelTempY       ! $ (\Delta T)_y $ .
    real(DP):: DelPotTempZ    ! $ (\Delta \theta)_z $ .
    real(DP):: z_kv (0:phy_hs%kmax-1)
                              ! $ k_v $ .
    real(DP):: yz_kt (0:phy_hs%jmax-1, 0:phy_hs%kmax-1)
                              ! $ k_T $ .
    real(DP):: xyz_TempEQ (0:phy_hs%imax-1, 0:phy_hs%jmax-1, 0:phy_hs%kmax-1)
                              ! $ T_{eq} $ . 平衡温度. 
                              ! Equilibrium temperature
    real(DP):: xyz_Press (0:phy_hs%imax-1, 0:phy_hs%jmax-1, 0:phy_hs%kmax-1)
                              ! $ T $ . 圧力. 
                              ! Pressure
    integer:: j, k            ! DO ループ用作業変数
                              ! Work variables for DO loop
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'PhyHS94Forcing'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( .not. phy_hs % initialized ) then
      stat = DC_ENOTINIT
      cause_c = 'PHYHS94'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  *phy_hs* に格納されている設定値の取り出し
    !  Fetch setting values stored in *phy_hs*
    !-----------------------------------------------------------------
    jmax = phy_hs % jmax
    kmax = phy_hs % kmax

    y_Lat   = phy_hs % y_Lat
    z_Sigma = phy_hs % z_Sigma
    DelTime = phy_hs % DelTime
    Kappa   = phy_hs % Kappa

    P0          = phy_hs % P0
    DelTempY    = phy_hs % DelTempY
    DelPotTempZ = phy_hs % DelPotTempZ
    z_kv        = phy_hs % z_kv
    yz_kt       = phy_hs % yz_kt

    !-----------------------------------------------------------------
    !  東西風速 $ U $ と南北風速 $ V $ へレイリー摩擦を適用
    !  Apply Rayleigh damping to zonal wind $ U $ and meridional wind $ V $
    !-----------------------------------------------------------------
    do k = 0, kmax - 1
      xyz_U (:,:,k) = &
        & xyz_U (:,:,k) &
        & * ( 1.0_DP - 2.0_DP * DelTime * z_kv (k) )

      xyz_V (:,:,k) = &
        & xyz_V (:,:,k) &
        & * ( 1.0_DP - 2.0_DP * DelTime * z_kv (k) )
    end do

    !-----------------------------------------------------------------
    !  温度 $ T $ へニュートン冷却を適用
    !  Apply Newtonian relaxation to temperature $ T $
    !-----------------------------------------------------------------
    do k = 0, kmax - 1
       xyz_Press(:,:,k) = z_Sigma(k) * xy_Ps
    enddo

    do j = 0, jmax - 1
      xyz_TempEQ(:,j,:) = &
        & max( 200.0_DP, &
        &      ( 315.0_DP &
        &        - DelTempY * sin( y_Lat(j) ) ** 2 &
        &        - DelPotTempZ * log( xyz_Press(:,j,:) / P0 ) &
        &                                 * cos( y_Lat(j) ) ** 2 &
        &      ) &
        &      * ( xyz_Press(:,j,:) / P0 ) ** Kappa &
        &     )
    end do

    do k = 0, kmax - 1
      do j = 0, jmax - 1
        xyz_Temp (:,j,k) = xyz_Temp (:,j,k) &
          & - 2.0_DP * DelTime &
          &   * yz_kt (j,k) * ( xyz_Temp (:,j,k) - xyz_TempEQ (:,j,k) )
      end do
    end do

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine PhyHS94Forcing


!!$  subroutine PhyHS94Sample( phy_hs, err )
!!$    !--
!!$    ! PhyHS94Sample の要約を記述してください.
!!$    !++
!!$    ! なお, 与えられた *phy_hs* が Create によって初期設定
!!$    ! されていない場合, プログラムはエラーを発生させます.
!!$    !--
!!$    ! Describe brief of PhyHS94Sample
!!$    !++
!!$    ! If *phy_hs* is not initialized by Create yet,
!!$    ! error is occurred.
!!$    !
!!$    use dc_trace, only: BeginSub, EndSub
!!$    use dc_types, only: DP, STRING, TOKEN, STDOUT
!!$    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
!!$    implicit none
!!$    type(PHYHS94), intent(inout):: phy_hs
!!$    logical, intent(out), optional:: err
!!$                              ! 例外処理用フラグ.
!!$                              ! デフォルトでは, この手続き内でエラーが
!!$                              ! 生じた場合, プログラムは強制終了します.
!!$                              ! 引数 *err* が与えられる場合,
!!$                              ! プログラムは強制終了せず, 代わりに
!!$                              ! *err* に .true. が代入されます.
!!$                              !
!!$                              ! Exception handling flag. 
!!$                              ! By default, when error occur in 
!!$                              ! this procedure, the program aborts. 
!!$                              ! If this *err* argument is given, 
!!$                              ! .true. is substituted to *err* and 
!!$                              ! the program does not abort. 
!!$
!!$!!$    integer:: param_i
!!$!!$    real(DP):: param_r
!!$!!$    character(STRING):: param_c
!!$
!!$    !-----------------------------------
!!$    !  作業変数
!!$    !  Work variables
!!$    integer:: stat
!!$    character(STRING):: cause_c
!!$    character(*), parameter:: subname = 'PhyHS94Sample'
!!$  continue
!!$    call BeginSub( subname )
!!$    stat = DC_NOERR
!!$    cause_c = ''
!!$
!!$    !-----------------------------------------------------------------
!!$    !  初期設定のチェック
!!$    !  Check initialization
!!$    !-----------------------------------------------------------------
!!$    if ( .not. phy_hs % initialized ) then
!!$      stat = DC_ENOTINIT
!!$      cause_c = 'PHYHS94'
!!$      goto 999
!!$    end if
!!$
!!$    !-----------------------------------------------------------------
!!$    !  *phy_hs* に格納されている設定値の取り出し
!!$    !  Fetch setting values stored in *phy_hs*
!!$    !-----------------------------------------------------------------
!!$!!$    param_i = phy_hs % param_i
!!$!!$    param_r = phy_hs % param_r
!!$!!$    param_c = phy_hs % param_c
!!$
!!$
!!$    !-----------------------------------------------------------------
!!$    !  終了処理, 例外処理
!!$    !  Termination and Exception handling
!!$    !-----------------------------------------------------------------
!!$999 continue
!!$    call StoreError( stat, subname, err, cause_c )
!!$    call EndSub( subname )
!!$  end subroutine PhyHS94Sample

end module phy_hs94
