!= 陬懷勧逧�↑螟画焚繧定ｨ育ｮ励☆繧九し繝悶Ν繝ｼ繝√Φ繝ｻ髢｢謨ｰ鄒､
!
!= Subroutines and functions for calculating auxiliary variables
!
! Authors::   Yoshiyuki O. Takahashi, Yasuhiro MORIKAWA, Yukiko YAMADA
! Version::   $Id: auxiliary.f90,v 1.18 2015/02/06 11:25:12 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module auxiliary
  !
  != 陬懷勧逧�↑螟画焚繧定ｨ育ｮ励☆繧九し繝悶Ν繝ｼ繝√Φ繝ｻ髢｢謨ｰ鄒､
  !
  != Subroutines and functions for calculating auxiliary variables
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 縺薙�繝｢繧ｸ繝･繝ｼ繝ｫ縺ｧ縺ｯ, 陬懷勧逧�↑螟画焚繧定ｨ育ｮ励☆繧九し繝悶Ν繝ｼ繝√Φ繧�未謨ｰ繧呈署萓帙＠縺ｾ縺�.
  !
  ! This module provides subroutines and functions for calculating auxiliary variables.
  !
  !== Procedures List
  !
  ! AuxVars          :: 貂ｩ蠎ｦ縺ｮ蜊頑紛謨ｰ $ \sigma $ 繝ｬ繝吶Ν縺ｮ陬憺俣縺ｨ
  !                     豌怜悸縺ｨ鬮伜ｺｦ縺ｮ邂怜�
  ! AuxVarsInit      ::
  ! AuxVarsFinalize  :: 邨ゆｺ��逅� (繝｢繧ｸ繝･繝ｼ繝ｫ蜀�Κ縺ｮ螟画焚縺ｮ蜑ｲ繧贋ｻ倥￠隗｣髯､)
  ! ---------------  :: ---------------
  ! AuxVars          :: Interpolate temperature on half $ \sigma $ level,
  !                     and calculate pressure and height
  ! AuxVarsInit      ::
  ! AuxVarsFinalize  :: Termination (deallocate variables in this module)
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#auxiliary_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 :: AuxVars
  public :: AuxVarsFinalize
  public :: AuxVarsInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !
  real(DP), save, public:: RefPress
                              ! 蝓ｺ貅匁ｰ怜悸.
                              ! Reference air pressure

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save :: auxiliary_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  real(DP), save, allocatable:: z_TempInpolM (:)
                              ! 貂ｩ蠎ｦ縺ｮ陬憺俣逕ｨ縺ｮ菫よ焚.
                              ! Coefficient for interpolation of temperature
  real(DP), save, allocatable:: z_TempInpol (:)
                              ! 貂ｩ蠎ｦ縺ｮ陬憺俣逕ｨ縺ｮ菫よ焚.
                              ! Coefficient for interpolation of temperature

  character(*), parameter:: module_name = 'auxiliary'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: auxiliary.f90,v 1.18 2015/02/06 11:25:12 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  recursive subroutine AuxVars(                           &
    & xy_Ps, xyz_Temp, xyz_QVap,                          &  ! (in )
    & xyr_Temp, xyz_VirTemp, xyr_VirTemp, xy_SurfVirTemp, &  ! (out) optional
    & xyz_Press,  xyr_Press,                              &  ! (out) optional
    & xy_SurfHeight, xy_SurfTemp,                         &  ! (in ) optional
    & xyz_Height, xyr_Height,                             &  ! (out) optional
    & xyz_Exner,  xyr_Exner                               &  ! (out) optional
    & )
    !
    ! 貂ｩ蠎ｦ縺ｮ蜊頑紛謨ｰﾏ�Ξ繝吶Ν縺ｮ陬憺俣, 豌怜悸縺ｨ鬮伜ｺｦ縺ｮ邂怜�,
    ! 繧ｨ繧ｯ繧ｹ繝翫�髢｢謨ｰ縺ｮ險育ｮ励ｒ陦後＞縺ｾ縺�.
    !
    ! Interpolate temperature on half sigma level,
    ! and calculate pressure and height,
    ! and calculate exner function.

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; 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
      & CpDry, &
                              ! $ C_p $ [J kg-1 K-1].
                              ! 荵ｾ辯･螟ｧ豌励�螳壼悸豈皮�.
                              ! Specific heat of air at constant pressure
      & EpsV
                              ! $ \epsilon_v $ .
                              ! 豌ｴ闥ｸ豌怜�蟄宣㍼豈�.
                              ! Molecular weight of water vapor

    ! 譎ょ綾邂｡逅�
    ! 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(in):: xyz_QVap    (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q $ .     豈疲ｹｿ. Specific humidity
    real(DP), intent(out), optional:: xyr_Temp   (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{T} $ . 貂ｩ蠎ｦ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Temperature (half level)
    real(DP), intent(out), optional:: xyz_VirTemp(0:imax-1, 1:jmax, 1:kmax)
                              ! $ \hat{T} $ . 莉ｮ貂ｩ蠎ｦ
                              ! Virtual temperature
    real(DP), intent(out), optional:: xyr_VirTemp(0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{T} $ . 莉ｮ貂ｩ蠎ｦ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Virtual temperature (half level)
    real(DP), intent(out), optional:: xy_SurfVirTemp(0:imax-1, 1:jmax)
                              ! $ \hat{T} $ . 莉ｮ貂ｩ蠎ｦ (諠第弌陦ｨ髱｢).
                              ! Virtual temperature (surface)
    real(DP), intent(out), optional:: xyz_Press  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(out), optional:: xyr_Press  (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . 豌怜悸 (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (half level)
    real(DP), intent(in ), optional:: xy_SurfHeight(0:imax-1,1:jmax)
                              ! $ z_s $ . 蝨ｰ陦ｨ髱｢鬮伜ｺｦ.
                              ! Surface height.
    real(DP), intent(in ), optional:: xy_SurfTemp(0:imax-1,1:jmax)
                              ! $ T_s $ . 諠第弌陦ｨ髱｢貂ｩ蠎ｦ.
                              ! Surface temperature.
    real(DP), intent(out), optional:: xyz_Height (0:imax-1, 1:jmax, 1:kmax)
                              ! 鬮伜ｺｦ (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Height (full level)
    real(DP), intent(out), optional:: xyr_Height (0:imax-1, 1:jmax, 0:kmax)
                              ! 鬮伜ｺｦ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Height (half level)
    real(DP), intent(out), optional:: xyz_Exner (0:imax-1, 1:jmax, 1:kmax)
                              ! Exner 髢｢謨ｰ (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Exner function (full level)
    real(DP), intent(out), optional:: xyr_Exner (0:imax-1, 1:jmax, 0:kmax)
                              ! Exner 髢｢謨ｰ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Exner function (half level)

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP):: xyz_PressWork  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP):: xyr_PressWork  (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . 豌怜悸 (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (half level)
!!$    real(DP):: xyr_TempWork   (0:imax-1, 1:jmax, 0:kmax)
!!$                              ! $ \hat{T} $ . 貂ｩ蠎ｦ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
!!$                              ! Temperature (half level)
    real(DP):: xyz_VirTempWork(0:imax-1, 1:jmax, 1:kmax)
                              ! $ T_v $ . 莉ｮ貂ｩ蠎ｦ.
                              ! Virtual temperature
    real(DP):: xyr_VirTempWork(0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{T}_v $ . 莉ｮ貂ｩ蠎ｦ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Virtual temperature (half level)

    integer:: k               ! 驩帷峩譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in vertical direction

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. auxiliary_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! 貂ｩ蠎ｦ縺ｮ陬憺俣
    ! Interpolate temperature
    !
    if ( present( xyr_Temp ) ) then
      call AuxVarsInternalRoutine(   &
        & xy_Ps,      xyz_Temp,      &  ! (in )
        & xyr_Temp = xyr_Temp  &  ! (out) optional
        & )
    end if

    ! 莉ｮ貂ｩ蠎ｦ縺ｮ險育ｮ�
    ! Calculate virtual temperature
    !
    if ( present( xy_SurfVirTemp ) ) then
      if ( .not. present( xy_SurfTemp ) ) then
        call MessageNotify( 'E', module_name, 'xy_SurfTemp has to be given in arguments to calculate xy_SurfVirTemp.' )
      end if
      xy_SurfVirTemp = xy_SurfTemp * ( 1.0_DP + ((( 1.0_DP / EpsV ) - 1.0_DP ) * xyz_QVap(:,:,1) ) )
    end if

    ! 莉ｮ貂ｩ蠎ｦ縺ｮ險育ｮ�
    ! Calculate virtual temperature
    !
    if ( present( xyz_VirTemp ) ) then
      xyz_VirTemp = xyz_Temp * ( 1.0_DP + ((( 1.0_DP / EpsV ) - 1.0_DP ) * xyz_QVap) )
    end if

    ! 莉ｮ貂ｩ蠎ｦ縺ｮ陬憺俣
    ! Interpolate virtual temperature
    !
    if ( present( xyr_VirTemp ) ) then
      if ( present( xyz_VirTemp ) ) then
        xyz_VirTempWork = xyz_VirTemp
      else
        xyz_VirTempWork = &
          & xyz_Temp * ( 1.0_DP + ((( 1.0_DP / EpsV ) - 1.0_DP ) * xyz_QVap) )
      end if
      call AuxVarsInternalRoutine(   &
        & xy_Ps, xyz_VirTempWork,    &  ! (in )
        & xyr_Temp = xyr_VirTemp     &  ! (out) optional
        & )
    end if

    ! 豌怜悸縺ｮ險育ｮ�
    ! Calculate pressure
    !
    if ( present( xyz_Press ) ) then
      call AuxVarsInternalRoutine(   &
        & xy_Ps,      xyz_Temp,      &  ! (in )
        & xyz_Press = xyz_Press  &  ! (out) optional
        & )
    end if

    if ( present( xyr_Press ) ) then
      call AuxVarsInternalRoutine(   &
        & xy_Ps,      xyz_Temp,      &  ! (in )
        & xyr_Press = xyr_Press  &  ! (out) optional
        & )
    end if

    ! 鬮伜ｺｦ縺ｮ險育ｮ�
    ! Calculate height
    !
    if ( present( xyz_Height ) ) then
      if ( .not. present( xy_SurfHeight ) ) then
        call MessageNotify( 'E', module_name, 'xy_SurfHeight has to be given in arguments to calculate xyz_Height.' )
      end if

!!$      if ( present( xyr_Temp ) ) then
!!$        xyr_TempWork = xyr_Temp
!!$      else
!!$        call AuxVarsInternalRoutine( &
!!$          & xy_Ps, xyz_Temp,         &  ! (in )
!!$          & xyr_Temp = xyr_TempWork  &  ! (out) optional
!!$          & )
!!$      end if
      if ( present( xyz_VirTemp ) .and. present( xyr_VirTemp ) ) then
        xyz_VirTempWork = xyz_VirTemp
        xyr_VirTempWork = xyr_VirTemp
      else
        ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
        ! Pause measurement of computation time
        call TimesetClockStop( module_name )
        call AuxVars(                      &
          & xy_Ps, xyz_Temp, xyz_QVap,     &  ! (in )
          & xyz_VirTemp = xyz_VirTempWork, &  ! (out) optional
          & xyr_VirTemp = xyr_VirTempWork  &  ! (out) optional
          & )
        ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
        ! Start measurement of computation time
        call TimesetClockStart( module_name )
      end if

!!$      xyz_Height(:,:,1) = &
!!$        & xy_SurfHeight(:,:) &
!!$        & + GasRDry / Grav * xyz_Temp(:,:,1) * ( 1. - z_Sigma(1) )
!!$      do k = 2, kmax
!!$        xyz_Height(:,:,k) = &
!!$          &   xyz_Height(:,:,k-1) &
!!$          & + GasRDry / Grav * xyr_TempWork(:,:,k-1) &
!!$          &   * r_DelSigma(k-1) / r_Sigma(k-1)
!!$      end do

      xyz_Height(:,:,1) =                          &
        & xy_SurfHeight                            &
        & + GasRDry / Grav * xyz_VirTempWork(:,:,1) * ( 1.0_DP - z_Sigma(1) )
      do k = 2, kmax
        xyz_Height(:,:,k) = xyz_Height(:,:,k-1)         &
          & + GasRDry / Grav * xyr_VirTempWork(:,:,k-1) &
          &   * r_DelSigma(k-1) / r_Sigma(k-1)
      end do
    end if

    if ( present( xyr_Height ) ) then
      if ( .not. present( xy_SurfHeight ) ) then
        call MessageNotify( 'E', module_name, 'xy_SurfHeight has to be given in arguments to calculate xyr_Height.' )
      end if

      if ( present( xyz_VirTemp ) ) then
        xyz_VirTempWork = xyz_VirTemp
      else
        ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
        ! Pause measurement of computation time
        call TimesetClockStop( module_name )
        call AuxVars(                      &
          & xy_Ps, xyz_Temp, xyz_QVap,     &  ! (in )
          & xyz_VirTemp = xyz_VirTempWork  &  ! (out) optional
          & )
        ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
        ! Start measurement of computation time
        call TimesetClockStart( module_name )
      end if

      xyr_Height(:,:,0) = xy_SurfHeight
      do k = 1, kmax
!!$        xyr_Height(:,:,k) = xyr_Height(:,:,k-1)        &
!!$          & + GasRDry / Grav * xyz_Temp(:,:,k)     &
!!$          &   * z_DelSigma(k) / z_Sigma(k)
        xyr_Height(:,:,k) = xyr_Height(:,:,k-1)        &
          & + GasRDry / Grav * xyz_VirTempWork(:,:,k)  &
          &   * z_DelSigma(k) / z_Sigma(k)
      end do
    end if

    ! 繧ｨ繧ｯ繧ｹ繝翫�髢｢謨ｰ縺ｮ險育ｮ�
    ! Calculate exner function
    !
    if ( present( xyz_Exner ) ) then
      if ( present( xyz_Press ) ) then
        xyz_PressWork = xyz_Press
      else
        call AuxVarsInternalRoutine(   &
          & xy_Ps,      xyz_Temp,      &  ! (in )
          & xyz_Press = xyz_PressWork  &  ! (out) optional
          & )
      end if

      xyz_Exner = ( xyz_PressWork / RefPress ) ** ( GasRDry / CpDry )
    end if

    if ( present( xyr_Exner ) ) then
      if ( present( xyr_Press ) ) then
        xyr_PressWork = xyr_Press
      else
        call AuxVarsInternalRoutine(   &
          & xy_Ps,      xyz_Temp,      &  ! (in )
          & xyr_Press = xyr_PressWork  &  ! (out) optional
          & )
      end if

      xyr_Exner = ( xyr_PressWork / RefPress ) ** ( GasRDry / CpDry )
    end if

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine AuxVars

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

  subroutine AuxVarsInternalRoutine( &
    & xy_Ps,      xyz_Temp,   &  ! (in )
    & xyr_Temp,               &  ! (out) optional
    & xyz_Press,  xyr_Press   &  ! (out) optional
    & )
    !
    ! 貂ｩ蠎ｦ縺ｮ蜊頑紛謨ｰﾏ�Ξ繝吶Ν縺ｮ陬憺俣, 豌怜悸縺ｮ邂怜�,
    ! 繧ｨ繧ｯ繧ｹ繝翫�髢｢謨ｰ縺ｮ險育ｮ励ｒ陦後＞縺ｾ縺�.
    !
    ! Interpolate temperature on half sigma level,
    ! and calculate pressure,
    ! and calculate exner function.

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
    ! Axes data settings
    !
    use axesset, only: &
      & z_Sigma, &
                              ! $ \sigma $ 繝ｬ繝吶Ν (謨ｴ謨ｰ).
                              ! Full $ \sigma $ level
      & r_Sigma
                              ! $ \sigma $ 繝ｬ繝吶Ν (蜊頑紛謨ｰ).
                              ! Half $ \sigma $ level

    ! 螳｣險譁� ; 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), optional:: xyr_Temp   (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{T} $ . 貂ｩ蠎ｦ (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Temperature (half level)
    real(DP), intent(out), optional:: xyz_Press  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(out), optional:: xyr_Press  (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . 豌怜悸 (蜊頑紛謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (half level)

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: k               ! 驩帷峩譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in vertical direction

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. auxiliary_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! 貂ｩ蠎ｦ縺ｮ陬憺俣
    ! Interpolate temperature
    !
    if ( present( xyr_Temp ) ) then
      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)

    end if

    ! 豌怜悸縺ｮ險育ｮ�
    ! Calculate pressure
    !
    if ( present( xyz_Press ) ) then
      do k = 1, kmax
        xyz_Press(:,:,k) = xy_Ps * z_Sigma(k)
      end do
    end if

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

  end subroutine AuxVarsInternalRoutine

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

  subroutine AuxVarsInit
    !
    ! auxiliary 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#auxiliary_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "auxiliary" module is initialized.
    ! "NAMELIST#auxiliary_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 /auxiliary_nml/ &
      & RefPress
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "auxiliary#AuxVarsInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "auxiliary#AuxVarsInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( auxiliary_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    RefPress        =   1.0e5_DP

    ! 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 = auxiliary_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(1) = 0.0_DP
    z_TempInpol (1) = 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 = %f', d = (/ z_TempInpolM /), n = (/ kmax /) )
    call MessageNotify( 'M', module_name, '  z_TempInpol  = %f', d = (/ z_TempInpol  /), n = (/ kmax /) )
    call MessageNotify( 'M', module_name, '  RefPress     = %f', d = (/ RefPress /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    auxiliary_inited = .true.

  end subroutine AuxVarsInit

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

  subroutine AuxVarsFinalize
    !
    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蜀�Κ縺ｮ螟画焚縺ｮ蜑ｲ繧贋ｻ倥￠隗｣髯､繧定｡後＞縺ｾ縺�.
    !
    ! Deallocate variables in this module.
    !

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( .not. auxiliary_inited ) return

    ! 蜑ｲ繧贋ｻ倥￠隗｣髯､
    ! Deallocation
    !
    if ( allocated( z_TempInpolM ) ) deallocate( z_TempInpolM )
    if ( allocated( z_TempInpol  ) ) deallocate( z_TempInpol  )

    auxiliary_inited = .false.

  end subroutine AuxVarsFinalize

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

end module auxiliary