!= 温度の半整数σレベルの補間, 気圧とジオポテンシャルの算出
!
!= Interpolate temperature on half sigma level, and calculate pressure and geo-potential
!
! Authors::   Yasuhiro MORIKAWA, Yukiko YAMADA
! Version::   $Id: intpol_half.f90,v 1.2 2008-08-11 16:35:36 morikawa Exp $ 
! Tag Name::  $Name: dcpam5-20081109-1 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module intpol_half
  !
  != 温度の半整数σレベルの補間, 気圧とジオポテンシャルの算出
  !
  != Interpolate temperature on half sigma level, and calculate pressure and geo-potential
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! このモジュールでは, 
  ! 温度の半整数 $ \sigma $ レベルの補間や
  ! 気圧とジオポテンシャルの算出のためのサブルーチンを提供します.
  !
  ! This module provides subroutines that
  ! interpolate temperature on half $ \sigma $ level, or
  ! calculate pressure and geo-potential. 
  !
  !== Procedures List
  !
  ! IntpolHalfLevel  :: 温度の半整数 $ \sigma $ レベルの補間と
  !                     気圧とジオポテンシャルの算出
  ! ---------------  :: ---------------
  ! IntpolHalfLevel  :: Interpolate temperature on half $ \sigma $ level, 
  !                     and calculate pressure and geo-potential
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#intpol_half_nml
  !++

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

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

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

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

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: IntpolHalfLevel

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: intpol_half_inited = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag


  ! 非公開変数
  ! Private variables
  !
  real(DP), allocatable:: z_TempInpolM (:)
                              ! 温度の補間用の係数. 
                              ! Coefficient for interpolation of temperature
  real(DP), allocatable:: z_TempInpol (:)
                              ! 温度の補間用の係数. 
                              ! Coefficient for interpolation of temperature

  character(*), parameter:: module_name = 'intpol_half'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name: dcpam5-20081109-1 $' // &
    & '$Id: intpol_half.f90,v 1.2 2008-08-11 16:35:36 morikawa Exp $'
                              ! モジュールのバージョン
                              ! Module version

  ! INTERFACE 文 ; INTERFACE statements
  !
  interface IntpolHalfLevel
    module procedure IntpolHalfLevel
  end interface

contains

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

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

    ! 座標データ設定
    ! Axes data settings
    !
    use axesset, only: &
      & z_Sigma, &
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
      & r_Sigma, &
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
      & z_DelSigma, &
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)
      & r_DelSigma
                              ! $ \Delta \sigma $ (半整数). 
                              ! $ \Delta \sigma $ (Half)

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: &
      & Grav, &               ! $ g $ [m s-2]. 
                              ! 重力加速度. 
                              ! Gravitational acceleration
      & GasRDry               ! $ R $ [J kg-1 K-1]. 
                              ! 乾燥大気の気体定数. 
                              ! Gas constant of air

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xy_Ps       (0:imax-1, 1:jmax)
                              ! $ p_s (t-\Delta t) $ . 地表面気圧. Surface pressure
    real(DP), intent(in):: xyz_Temp    (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ .     温度. Temperature
    real(DP), intent(out):: xyr_Temp   (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{T} $ . 温度 (半整数レベル). 
                              ! Temperature (half level)
    real(DP), intent(out):: xyz_Press  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 気圧 (整数レベル). 
                              ! Air pressure (full level)
    real(DP), intent(out):: xyr_Press  (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . 気圧 (半整数レベル). 
                              ! Air pressure (half level)
    real(DP), intent(out):: xyz_GeoPot (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \phi $ . ジオポテンシャル (整数レベル). 
                              ! Geo-potential (full level)
    real(DP), intent(out):: xyr_GeoPot (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{\phi} $ . ジオポテンシャル (半整数レベル). 
                              ! Geo-potential (half level)


    ! 作業変数
    ! Work variables
    !
    integer:: k               ! 鉛直方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in vertical direction

    ! 実行文 ; Executable statement
    !

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

    ! 初期化
    ! Initialization
    !
    if ( .not. intpol_half_inited ) call IntpolHalfInit

    ! 温度の補間
    ! Interpolate temperature
    !
    do k = 2, kmax
      xyr_Temp(:,:,k-1) =   z_TempInpolM(k) * xyz_Temp(:,:,k-1)  &
        &                 + z_TempInpol (k) * xyz_Temp(:,:,k)
    end do

    xyr_Temp(:,:,kmax) = xyz_Temp(:,:,kmax)
    xyr_Temp(:,:,0)    = xyz_Temp(:,:,1)

    ! 気圧の計算
    ! Calculate pressure
    !
    do k = 1, kmax
      xyz_Press(:,:,k) = xy_Ps * z_Sigma(k)
    end do

    do k = 0, kmax
      xyr_Press(:,:,k) = xy_Ps * r_Sigma(k)
    end do

    ! ジオポテンシャルの計算
    ! Calculate geo-potential
    !
    xyz_GeoPot(:,:,1) = &
      & GasRDry / Grav * xyz_Temp(:,:,1) * ( 1.0_DP - z_Sigma(1) )

    do k = 2, kmax
      xyz_GeoPot(:,:,k) = &
        &   xyz_GeoPot(:,:,k-1) &
        & + GasRDry / Grav * xyr_Temp(:,:,k-1) &
        &   * r_DelSigma(k-1) / r_Sigma(k-1)
    end do

    xyr_GeoPot = 0.0_DP
    do k = 1, kmax
      xyr_GeoPot(:,:,k) = &
        &   xyr_GeoPot(:,:,k-1) &
        & + GasRDry / Grav * xyz_Temp(:,:,k)  &
        &   * z_DelSigma(k) / z_Sigma(k)
    end do


    ! 計算時間計測一時停止
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine IntpolHalfLevel

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

  subroutine IntpolHalfInit
    !
    ! intpol_half モジュールの初期化を行います. 
    ! NAMELIST#intpol_half_nml の読み込みはこの手続きで行われます. 
    !
    ! "intpol_half" module is initialized. 
    ! "NAMELIST#intpol_half_nml" is loaded in this procedure. 
    !

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

    ! 座標データ設定
    ! Axes data settings
    !
    use axesset, only: &
      & z_Sigma, &
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
      & r_Sigma
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level

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

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

    ! 種別型パラメタ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 標準出力の装置番号. Unit number of standard output

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

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

    integer:: k               ! 鉛直方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in vertical direction

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
!!$    namelist /intpol_half_nml/
          !
          ! デフォルト値については初期化手続 "intpol_half#IntpolHalfInit" 
          ! のソースコードを参照のこと. 
          !
          ! Refer to source codes in the initialization procedure
          ! "intpol_half#IntpolHalfInit" for the default values. 
          !

    ! 実行文 ; Executable statement
    !

    if ( intpol_half_inited ) return
    call InitCheck

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

!!$    ! NAMELIST の読み込み
!!$    ! NAMELIST is input
!!$    !
!!$    if ( trim(namelist_filename) /= '' ) then
!!$      call FileOpen( unit_nml, &          ! (out)
!!$        & namelist_filename, mode = 'r' ) ! (in)
!!$
!!$      rewind( unit_nml )
!!$      read( unit_nml, &           ! (in)
!!$        & nml = intpol_half_nml, &  ! (out)
!!$        & iostat = iostat_nml )   ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if

    !  補間用の係数の計算
    !  Calculate coefficient for interpolation
    !
    allocate( z_TempInpolM(1:kmax) )
    allocate( z_TempInpol(1:kmax) )

    z_TempInpolM = 0.0_DP
    z_TempInpol = 0.0_DP

    do k = 2, kmax
      z_TempInpolM(k) = &
        &    ( log( r_Sigma( k-1 ) ) - log( z_Sigma( k ) ) ) &
        &  / ( log( z_Sigma( k-1 ) ) - log( z_Sigma( k ) ) )
      z_TempInpol(k) = &
        &    ( log( z_Sigma( k-1 ) ) - log( r_Sigma( k-1 ) ) ) &
        &  / ( log( z_Sigma( k-1 ) ) - log( z_Sigma( k )   ) )
    end  do

    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  z_TempInpolM = %*r', r = real( z_TempInpolM ), n = (/ kmax /) )
    call MessageNotify( 'M', module_name, '  z_TempInpol  = %*r', r = real( z_TempInpol  ), n = (/ kmax /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    intpol_half_inited = .true.
  end subroutine IntpolHalfInit

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

  subroutine InitCheck
    !
    ! 依存モジュールの初期化チェック
    !
    ! Check initialization of dependency modules

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

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

    ! 格子点設定
    ! Grid points settings
    !
    use gridset, only: gridset_inited

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

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

    ! 実行文 ; Executable statement
    !

    if ( .not. namelist_util_inited ) &
      & call MessageNotify( 'E', module_name, '"namelist_util" module is not initialized.' )

    if ( .not. gridset_inited ) &
      & call MessageNotify( 'E', module_name, '"gridset" module is not initialized.' )

    if ( .not. constants_inited ) &
      & call MessageNotify( 'E', module_name, '"constants" module is not initialized.' )

    if ( .not. axesset_inited ) &
      & call MessageNotify( 'E', module_name, '"axesset" module is not initialized.' )

  end subroutine InitCheck

end module intpol_half
