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

module phy_interpolate
  !
  != 温度の半整数σレベルの補間, 気圧とジオポテンシャルの算出
  !
  != 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
  !
  ! Create           :: PHYINTPOL 型変数の初期設定
  ! Close            :: PHYINTPOL 型変数の終了処理
  ! PutLine          :: PHYINTPOL 型変数に格納されている情報の印字
  ! initialized      :: PHYINTPOL 型変数が初期設定されているか否か
  ! InterpolateTemp  :: 温度の半整数 $ \sigma $ レベルの補間
  ! InterpolateGeoPot:: 気圧とジオポテンシャルの算出
  ! ---------------  :: ---------------
  ! Create           :: Constructor of "PHYINTPOL"
  ! Close            :: Deconstructor of "PHYINTPOL"
  ! PutLine          :: Print information of "PHYINTPOL"
  ! initialized      :: Check initialization of "PHYINTPOL"
  ! InterpolateTemp  :: Interpolate temperature on half $ \sigma $ level
  ! InterpolateGeoPot:: Calculate pressure and geo-potential
  !
  !== Usage
  !
  ! 始めに, PHYINTPOL 型の変数を定義し, Create で初期設定を行います.
  ! 温度の半整数 $ \sigma $ レベルの補間には InterpolateTemp を,
  ! 気圧とジオポテンシャルの算出には InterpolateGeoPot を用いてください.
  ! PHYINTPOL 型の変数の終了処理には Close を用いてください.
  !
  ! First, initialize "PHYINTPOL" by "Create".
  ! Use "InterpolateTemp" in order to 
  ! interpolate temperature on half $ \sigma $ level. 
  ! And use "InterpolateGeoPot" in order to 
  ! calculate pressure and geo-potential.
  ! In order to terminate "PHYINTPOL", use "Close".
  !

  use dc_types, only: DP, TOKEN
  implicit none
  private
  public:: PHYINTPOL, Create, Close, PutLine, initialized
  public:: InterpolateTemp, InterpolateGeoPot

  type PHYINTPOL
    !
    ! まず, Create で "PHYINTPOL" 型の変数を初期設定して下さい.
    ! 初期設定された "PHYINTPOL" 型の変数を再度利用する際には,
    ! Close によって終了処理を行ってください.
    !
    ! Initialize "PHYINTPOL" variable by "Create" before usage.
    ! If you reuse "PHYINTPOL" 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):: Cp         ! $ C_p $ .    大気定圧比熱.   Specific heat of air at constant pressure
    real(DP):: RAir       ! $ R $ .      大気気体定数.   Gas constant of air
    real(DP):: Grav       ! $ g $ .      重力加速度.     Gravitational acceleration
    real(DP), pointer:: z_Sigma (:) =>null()
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP), pointer:: r_Sigma (:) =>null()
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
    real(DP), pointer:: z_DelSigma (:) =>null()
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)
    real(DP), pointer:: r_DelSigma (:) =>null()
                              ! $ \Delta \sigma $ (半整数). 
                              ! $ \Delta \sigma $ (Half)
    real(DP), pointer:: z_TempInpolM (:) =>null()
                              ! 補間用の係数. 
                              ! Coefficient for interpolation
    real(DP), pointer:: z_TempInpol (:) =>null()
                              ! 補間用の係数. 
                              ! Coefficient for interpolation
  end type PHYINTPOL

  character(*), parameter:: version = &
    & '$Name: dcpam4-20080624-1 $' // &
    & '$Id: phy_interpolate.f90,v 1.4 2008-06-14 11:44:15 morikawa Exp $'

  interface Create
    module procedure PhyIntpolCreate
  end interface

  interface Close
    module procedure PhyIntpolClose
  end interface

  interface PutLine
    module procedure PhyIntpolPutLine
  end interface

  interface initialized
    module procedure PhyIntpolInitialized
  end interface

  interface NmlRead
    module procedure PhyIntpolNmlRead
  end interface

  interface InterpolateTemp
    module procedure PhyIntpolInterpolateTemp
  end interface

  interface InterpolateGeoPot
    module procedure PhyIntpolInterpolateGeoPot
  end interface

!!$  interface Sample
!!$    module procedure PhyIntpolSample
!!$  end interface

contains

  subroutine PhyIntpolCreate( phy_intpol, &
    & imax, jmax, kmax, &
    & z_Sigma, r_Sigma, &
!!$    & Cp, &
    & RAir, Grav, &
    & nmlfile, err )
    !
    ! PHYINTPOL 型の変数の初期設定を行います.
    ! 他のサブルーチンを使用する前に必ずこのサブルーチンによって
    ! PHYINTPOL 型の変数を初期設定してください.
    !
    ! なお, 与えられた *phy_intpol* が既に初期設定されている場合,
    ! プログラムはエラーを発生させます.
    !
    ! NAMELIST を利用する場合には引数 *nmlfile* に NAMELIST ファイル名
    ! を与えてください. NAMELIST 変数群の詳細に関しては 
    ! NAMELIST#phy_interpolate_nml を参照してください. 
    !
    ! Constructor of "PHYINTPOL".
    ! Initialize *phy_intpol* by this subroutine, 
    ! before other procedures are used, 
    !
    ! Note that if *phy_intpol* is already initialized 
    ! by this procedure, error is occurred.
    !
    ! In order to use NAMELIST, specify a NAMELIST filename to 
    ! argument *nmlfile*. See "NAMELIST#phy_interpolate_nml"
    ! for details about a NAMELIST group.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_string, only: PutLine, Printf
    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
    implicit none
    type(PHYINTPOL), intent(inout):: phy_intpol
    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):: z_Sigma (0:kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP), intent(in):: r_Sigma (0:kmax)
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
!!$    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):: Grav       ! $ g $ .      重力加速度.     Gravitational acceleration
    character(*), intent(in), optional:: nmlfile
                              ! NAMELIST ファイルの名称. 
                              ! この引数に空文字以外を与えた場合, 
                              ! 指定されたファイルから 
                              ! NAMELIST 変数群を読み込みます. 
                              ! ファイルを読み込めない場合にはエラーを
                              ! 生じます.
                              !
                              ! NAMELIST 変数群の詳細に関しては 
                              ! NAMELIST#phy_interpolate_nml 
                              ! を参照してください. 
                              !
                              ! 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.
                              ! 
                              ! See "NAMELIST#phy_interpolate_nml" 
                              ! for details about a NAMELIST group.
                              ! 
    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:: k               ! DO ループ用作業変数
                              ! Work variables for DO loop

    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'PhyIntpolCreate'
  continue
    call BeginSub( subname, version )
    stat = DC_NOERR
    cause_c = ''

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

    !-----------------------------------------------------------------
    !  引数の正当性のチェック
    !  Validation of arguments
    !-----------------------------------------------------------------
    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

    !-----------------------------------------------------------------
    !  波数・格子点の設定
    !  Configure wave number and grid point
    !-----------------------------------------------------------------
    phy_intpol % imax = imax
    phy_intpol % jmax = jmax
    phy_intpol % kmax = kmax

    !-----------------------------------------------------------------
    !  物理定数の設定
    !  Configure physical constants
    !-----------------------------------------------------------------
!!$    phy_intpol % Cp = Cp
    phy_intpol % RAir = RAir
    phy_intpol % Grav = Grav

    !-----------------------------------------------------------------
    !  鉛直格子点の設定
    !  Configure values of vertical grid point
    !-----------------------------------------------------------------
    allocate( phy_intpol % z_Sigma(0:kmax-1) )
    allocate( phy_intpol % r_Sigma(0:kmax) )
    allocate( phy_intpol % z_DelSigma(0:kmax-1) )
    allocate( phy_intpol % r_DelSigma(0:kmax) )

    phy_intpol % r_Sigma = r_Sigma
    phy_intpol % z_Sigma = z_Sigma

    do k = 0, kmax - 1
      phy_intpol % z_DelSigma(k) = r_Sigma(k) - r_Sigma(k+1)
    end do

    do k = 1, kmax - 1
      phy_intpol % r_DelSigma(k) = z_Sigma(k-1) - z_Sigma(k)
    end do
    phy_intpol % r_DelSigma(0)    = r_Sigma(0)      - z_Sigma(0)
    phy_intpol % r_DelSigma(kmax) = z_Sigma(kmax-1) - r_Sigma(kmax)

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

    phy_intpol % z_TempInpolM = 0.0_DP
    phy_intpol % z_TempInpol = 0.0_DP

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

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

    !-------------------------
    !  デフォルト値
    !  Default values
!!$    phy_intpol % param_r = 0.0_DP
!!$    phy_intpol % param_c = 'hogehoge'

    !-------------------------
    !  オプショナル引数からの値
    !  Values from optional arguments
!!$    phy_intpol % param_i = param_i
!!$    if ( present(param_r) )  phy_intpol % param_r = param_r
!!$    if ( present(param_c) )  phy_intpol % 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_intpol % param_i, &   ! (inout)
!!$        & param_r = phy_intpol % param_r, &   ! (inout)
!!$        & param_c_ = phy_intpol % 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
    !-----------------------------------------------------------------
!!$    if ( phy_intpol % param_i < 0 ) then
!!$      stat = DC_ENEGATIVE
!!$      cause_c = 'param_i'
!!$      goto 999
!!$    end if


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

  subroutine PhyIntpolClose( phy_intpol, err )
    !
    ! PHYINTPOL 型の変数の終了処理を行います.
    ! なお, 与えられた *phy_intpol* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Deconstructor of "PHYINTPOL".
    ! Note that if *phy_intpol* is not initialized by "Create" yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_string, only: PutLine, Printf
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(PHYINTPOL), intent(inout):: phy_intpol
    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 = 'PhyIntpolClose'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

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

    !-----------------------------------------------------------------
    !  "PHYINTPOL" の設定の消去
    !  Clear the settings for "PHYINTPOL"
    !-----------------------------------------------------------------
    deallocate( phy_intpol % z_Sigma )
    deallocate( phy_intpol % r_Sigma )
    deallocate( phy_intpol % z_DelSigma )
    deallocate( phy_intpol % r_DelSigma )
    deallocate( phy_intpol % z_TempInpolM )
    deallocate( phy_intpol % z_TempInpol )

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

  subroutine PhyIntpolPutLine( phy_intpol, unit, indent, err )
    !
    ! 引数 *phy_intpol* に設定されている情報を印字します.
    ! デフォルトではメッセージは標準出力に出力されます. 
    ! *unit* に装置番号を指定することで, 出力先を変更することが可能です.
    !
    ! Print information of *phy_intpol*.
    ! 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_string, only: PutLine, Printf
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(PHYINTPOL), intent(in):: phy_intpol
    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 = 'PhyIntpolPutLine'
  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


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

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

!!$      call Printf( out_unit, &
!!$        & indent_str(1:indent_len) // &
!!$        & ' @Cp=%f', &
!!$        & d = (/phy_intpol % Cp/) )
!!$
      call Printf( out_unit, &
        & indent_str(1:indent_len) // &
        & ' @RAir=%f @Grav=%f', &
        & d = (/phy_intpol % RAir, phy_intpol % Grav/) )

      call PutLine( phy_intpol % z_Sigma, unit = out_unit, &
        & lbounds = lbound(phy_intpol % z_Sigma), &
        & ubounds = ubound(phy_intpol % z_Sigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @z_Sigma=' )

      call PutLine( phy_intpol % r_Sigma, unit = out_unit, &
        & lbounds = lbound(phy_intpol % r_Sigma), &
        & ubounds = ubound(phy_intpol % r_Sigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @r_Sigma=' )

      call PutLine( phy_intpol % z_DelSigma, unit = out_unit, &
        & lbounds = lbound(phy_intpol % z_DelSigma), &
        & ubounds = ubound(phy_intpol % z_DelSigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @z_DelSigma=' )

      call PutLine( phy_intpol % r_DelSigma, unit = out_unit, &
        & lbounds = lbound(phy_intpol % r_DelSigma), &
        & ubounds = ubound(phy_intpol % r_DelSigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @r_DelSigma=' )

      call PutLine( phy_intpol % z_TempInpolM, unit = out_unit, &
        & lbounds = lbound(phy_intpol % z_TempInpolM), &
        & ubounds = ubound(phy_intpol % z_TempInpolM), &
        & indent = indent_str(1:indent_len) // &
        & ' @z_TempInpolM=' )

      call PutLine( phy_intpol % z_TempInpol, unit = out_unit, &
        & lbounds = lbound(phy_intpol % z_TempInpol), &
        & ubounds = ubound(phy_intpol % z_TempInpol), &
        & indent = indent_str(1:indent_len) // &
        & ' @z_TempInpol=' )

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

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

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

  subroutine PhyIntpolNmlRead( 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_string, only: PutLine, Printf
    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_interpolate_nml/ &
!!$      & param_i, param_r, param_c
                              ! phy_interpolate モジュール用
                              ! NAMELIST 変数群名.
                              !
                              ! phy_interpolate#Create を使用する際に, 
                              ! オプショナル引数 *nmlfile* へ NAMELIST 
                              ! ファイル名を指定することで, そのファイルから
                              ! この NAMELIST 変数群を読み込みます.
                              !
                              ! NAMELIST group name for 
                              ! "phy_interpolate" module.
                              ! 
                              ! If a NAMELIST filename is specified to 
                              ! an optional argument *nmlfile* 
                              ! when "phy_interpolate#Create" is used, 
                              ! this NAMELIST group is loaded from 
                              ! the file.

    !-----------------------------------
    !  作業変数
    !  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 = 'PhyIntpolNmlRead'
  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_interpolate_nml, iostat = iostat_nml ) ! (out)
!!$    if ( iostat_nml == 0 ) then
!!$      call MessageNotify( 'M', subname, &
!!$        & 'NAMELIST group "%c" is loaded from "%c".', &
!!$        & c1='phy_interpolate_nml', c2=trim(nmlfile) )
!!$      write(STDOUT, nml = phy_interpolate_nml)
!!$    else
!!$      call MessageNotify( 'W', subname, &
!!$        & 'NAMELIST group "%c" is not found in "%c" (iostat=%d).', &
!!$        & c1='phy_interpolate_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 PhyIntpolNmlRead

  subroutine PhyIntpolInterpolateTemp( phy_intpol, &
    & xyz_Temp, &
    & xyr_Temp, &
    & err )
    !
    ! 温度 (整数 $ \sigma $ レベル) *xyz_Temp* から
    ! 温度 (半整数 $ \sigma $ レベル) *xyr_Temp* を算出します.
    !
    ! なお, 与えられた *phy_intpol* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Calculate *xyr_Temp* that is temperature on half $ \sigma $ level 
    ! from *xyz_Temp* that is temperature on full $ \sigma $ level 
    ! 
    ! If *phy_intpol* is not initialized by "Create" yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_string, only: PutLine, Printf
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(PHYINTPOL), intent(inout):: phy_intpol
    real(DP), intent(in):: xyz_Temp (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax-1)
                              ! $ T $ . 温度 (整数レベル). 
                              ! Temperature (full level)
    real(DP), intent(out):: xyr_Temp (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax)
                              ! $ T $ . 温度 (半整数レベル). 
                              ! Temperature (half level)
    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:: kmax ! 鉛直層数. 
                   ! Number of vertical level

    real(DP):: z_TempInpolM  (0:phy_intpol%kmax-1)
                              ! 補間用の係数. 
                              ! Coefficient for interpolation
    real(DP):: z_TempInpol (0:phy_intpol%kmax-1)
                              ! 補間用の係数. 
                              ! Coefficient for interpolation

    integer:: k               ! DO ループ用作業変数
                              ! Work variables for DO loop
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'PhyIntpolInterpolateTemp'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

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

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

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

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

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

  subroutine PhyIntpolInterpolateGeoPot( phy_intpol, &
    & xy_Ps, &
    & xyz_Temp,   xyr_Temp, &
    & xyz_Press,  xyr_Press, &
    & xyz_GeoPot, xyr_GeoPot, &
    & err )
    !
    ! 地表面気圧 *xy_Ps*, と温度 *xyz_Temp*, *xyr_Temp* から, 
    ! 気圧 *xyz_Press*, *xyr_Press* と
    ! ジオポテンシャル *xyz_GeoPot*, *xyr_GeoPot* を算出します.
    !
    ! なお, 与えられた *phy_intpol* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Calculate pressure *xyz_Press*, *xyr_Press* and
    ! geo-potential *xyz_GeoPot*, *xyr_GeoPot* from
    ! surface pressure *xy_Ps*, and temperature *xyz_Temp*, *xyr_Temp*.
    ! 
    ! If *phy_intpol* is not initialized by "Create" yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_string, only: PutLine, Printf
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(PHYINTPOL), intent(inout):: phy_intpol
    real(DP), intent(in):: xy_Ps (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1)
                              ! $ p_s $ .   地表面気圧. Surface pressure
    real(DP), intent(in):: xyz_Temp (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax-1)
                              ! $ T $ . 温度 (整数レベル). 
                              ! Temperature (full level)
    real(DP), intent(in):: xyr_Temp (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax)
                              ! $ T $ . 温度 (半整数レベル). 
                              ! Temperature (half level)
    real(DP), intent(out):: xyz_Press (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax-1)
                              ! $ p_s $ . 地表面気圧 (整数レベル). 
                              ! Surface pressure (full level)
    real(DP), intent(out):: xyr_Press (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax)
                              ! $ p_s $ . 地表面気圧 (半整数レベル). 
                              ! Surface pressure (half level)
    real(DP), intent(out):: xyz_GeoPot (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax-1)
                              ! $ \phi $ . ジオポテンシャル (整数レベル). 
                              ! Geo-potential (full level)
    real(DP), intent(out):: xyr_GeoPot (0:phy_intpol%imax-1, 0:phy_intpol%jmax-1, 0:phy_intpol%kmax)
                              ! $ \phi $ . ジオポテンシャル (半整数レベル). 
                              ! Geo-potential (half level)
    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:: kmax ! 鉛直層数. 
                   ! Number of vertical level
!!$    real(DP):: Cp         ! $ C_p $ .    大気定圧比熱.   Specific heat of air at constant pressure
    real(DP):: RAir       ! $ R $ .      大気気体定数.   Gas constant of air
    real(DP):: Grav       ! $ g $ .      重力加速度.     Gravitational acceleration

    real(DP):: z_Sigma (0:phy_intpol%kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP):: r_Sigma (0:phy_intpol%kmax)
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
    real(DP):: z_DelSigma (0:phy_intpol%kmax-1)
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)
    real(DP):: r_DelSigma (0:phy_intpol%kmax)
                              ! $ \Delta \sigma $ (半整数). 
                              ! $ \Delta \sigma $ (Half)

!!$    real(DP):: z_PhiA(0:phy_intpol%kmax-1)
!!$    real(DP):: z_PhiB(0:phy_intpol%kmax-1)
!!$    real(DP):: z_PhiC(0:phy_intpol%kmax-1)

    integer:: k               ! DO ループ用作業変数
                              ! Work variables for DO loop
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'PhyIntpolInterpolateGeoPot'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

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

    !-----------------------------------------------------------------
    !  *phy_intpol* に格納されている設定値の取り出し
    !  Fetch setting values stored in *phy_intpol*
    !-----------------------------------------------------------------
    kmax = phy_intpol % kmax
!!$    Cp = phy_intpol % Cp
    RAir = phy_intpol % RAir
    Grav = phy_intpol % Grav

    z_Sigma = phy_intpol % z_Sigma
    r_Sigma = phy_intpol % r_Sigma
    z_DelSigma = phy_intpol % z_DelSigma
    r_DelSigma = phy_intpol % r_DelSigma

    !----------------------------------------------------------------
    !  気圧の計算
    !  Calculate pressure
    !----------------------------------------------------------------
    do k = 0, kmax - 1
      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(:,:,0) = &
      &   RAir / Grav * xyz_Temp(:,:,0) &
      &   * ( 1.0_DP - z_Sigma(0) )

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

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

!!$    !-----------------------------------
!!$    !  AFES での算出方法 (参考までに)
!!$    !  Calculation of AFES (for reference)
!!$    do k = 0, kmax - 1
!!$      z_PhiA(k) = ( r_Sigma(k) / z_Sigma(k) )**( RAir / Cp ) - 1.0_DP
!!$      z_PhiB(k) = 1.0_DP - ( r_Sigma(k+1) / z_Sigma(k) )**( RAir / Cp )
!!$      z_PhiC(k) =   ( r_Sigma(k) / z_Sigma(k) )**( RAir / Cp ) &
!!$        &         - ( r_Sigma(k+1) / z_Sigma(k) )**( RAir / Cp )
!!$    end do
!!$    z_PhiB(kmax-1) = 0.0_DP
!!$
!!$    xyz_GeoPot(:,:,0) = Cp / Grav * z_PhiA(0) * xyz_Temp(:,:,0)
!!$    xyr_GeoPot(:,:,0) = 0.0_DP
!!$
!!$    do k = 1, kmax - 1
!!$      xyz_GeoPot(:,:,k) =   &
!!$        &   xyz_GeoPot(:,:,k-1) &
!!$        & + Cp / Grav &
!!$        &    * (   z_PhiA(k) * xyz_Temp(:,:,k) &
!!$        &        + z_PhiB(k-1) * xyz_Temp(:,:,k-1) )
!!$    end do
!!$
!!$    do k = 1, kmax
!!$      xyr_GeoPot(:,:,k) = &
!!$        &   xyr_GeoPot(:,:,k-1) &
!!$        & + Cp / Grav * z_PhiC(k-1) * xyz_Temp(:,:,k-1)
!!$    end do

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

!!$  subroutine PhyIntpolSample( phy_intpol, err )
!!$    !--
!!$    ! PhyIntpolSample の要約を記述してください.
!!$    !++
!!$    ! なお, 与えられた *phy_intpol* が Create によって初期設定
!!$    ! されていない場合, プログラムはエラーを発生させます.
!!$    !--
!!$    ! Describe brief of PhyIntpolSample
!!$    !++
!!$    ! If *phy_intpol* is not initialized by "Create" yet,
!!$    ! error is occurred.
!!$    !
!!$    use dc_trace, only: BeginSub, EndSub
!!$    use dc_string, only: PutLine, Printf
!!$    use dc_types, only: DP, STRING, TOKEN, STDOUT
!!$    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
!!$    implicit none
!!$    type(PHYINTPOL), intent(inout):: phy_intpol
!!$    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 = 'PhyIntpolSample'
!!$  continue
!!$    call BeginSub( subname )
!!$    stat = DC_NOERR
!!$    cause_c = ''
!!$
!!$    !-----------------------------------------------------------------
!!$    !  初期設定のチェック
!!$    !  Check initialization
!!$    !-----------------------------------------------------------------
!!$    if ( .not. phy_intpol % initialized ) then
!!$      stat = DC_ENOTINIT
!!$      cause_c = 'PHYINTPOL'
!!$      goto 999
!!$    end if
!!$
!!$    !-----------------------------------------------------------------
!!$    !  *phy_intpol* に格納されている設定値の取り出し
!!$    !  Fetch setting values stored in *phy_intpol*
!!$    !-----------------------------------------------------------------
!!$!!$    param_i = phy_intpol % param_i
!!$!!$    param_r = phy_intpol % param_r
!!$!!$    param_c = phy_intpol % param_c
!!$
!!$
!!$    !-----------------------------------------------------------------
!!$    !  終了処理, 例外処理
!!$    !  Termination and Exception handling
!!$    !-----------------------------------------------------------------
!!$999 continue
!!$    call StoreError( stat, subname, err, cause_c )
!!$    call EndSub( subname )
!!$  end subroutine PhyIntpolSample

end module phy_interpolate
