!= 蝨ｰ髱｢貂ｩ蠎ｦ, 蝨溷｣梧ｸｩ蠎ｦ縺ｮ譎る俣遨榊�
!
!= Time integration of surface temperature and soil temperature
!
! Authors::   Yoshiyuki O. Takahashi, Yasuhiro MORIKAWA
! Version::   $Id: intg_surftemp.f90,v 1.10 2011/07/08 03:55:20 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module intg_surftemp
  !
  != 蝨ｰ髱｢貂ｩ蠎ｦ, 蝨溷｣梧ｸｩ蠎ｦ縺ｮ譎る俣遨榊�
  !
  != Time integration of surface temperature and soil temperature
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 蝨ｰ髱｢貂ｩ蠎ｦ縺ｨ蝨溷｣梧ｸｩ蠎ｦ縺ｮ譎る俣遨榊�繧定｡後＞縺ｾ縺�.
  !
  ! Time integration of surface temperature and soiltemperature
  !
  !== Procedures List
  !
  ! IntegralSurfTemp   :: 蝨ｰ髱｢貂ｩ蠎ｦ, 蝨溷｣梧ｸｩ蠎ｦ縺ｮ譎る俣遨榊�
  ! ---------------    :: ---------------
  ! IntegralSurfTemp   :: Time integration of surface temperature and soil temperature
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#intg_surftemp_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
    &                kslmax    ! 蝨ｰ荳九�驩帷峩螻､謨ｰ.
                               ! Number of subsurface 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 :: IntegralSurfTemp
  public :: IntgSurfTempInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !

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

  character(*), parameter:: module_name = 'intg_surftemp'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: intg_surftemp.f90,v 1.10 2011/07/08 03:55:20 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  subroutine IntegralSurfTemp(         &
    & xy_DSurfTempDt, xyz_DSoilTempDt, &          ! (in)
    & xy_SurfTemp   , xyz_SoilTemp     &          ! (inout)
    & )
    !
    ! 蝨ｰ髱｢貂ｩ蠎ｦ, 蝨溷｣梧ｸｩ蠎ｦ縺ｮ譎る俣遨榊�繧定｡後＞縺ｾ縺�.
    !
    ! Time integration of surface temperature and soil temperature
    !
    !

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

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: DelTime, &  ! $ \Delta t $
      & TimesetClockStart, TimesetClockStop

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(in):: xy_DSurfTempDt (0:imax-1, 1:jmax)
                              ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ螟牙喧邇� (K s-1)
                              ! Surface temperature tendency (K s-1)
    real(DP), intent(in):: xyz_DSoilTempDt (0:imax-1, 1:jmax, 1:kslmax)
                              ! $ \DP{Tg}{t} $ . 蝨溷｣梧ｸｩ蠎ｦ螟牙喧 (K s-1)
                              ! Temperature tendency (K s-1)
    real(DP), intent(inout):: xy_SurfTemp (0:imax-1, 1:jmax)
                              ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ.
                              ! Surface temperature
    real(DP), intent(inout):: xyz_SoilTemp(0:imax-1, 1:jmax, 1:kslmax)
                              ! 蝨溷｣梧ｸｩ蠎ｦ (K)
                              ! Soil temperature (K)

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

    ! 螳溯｡梧枚 ; Executable statement
    !

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

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

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ譎る俣遨榊�
    ! Time integration of surface temperature
    !
    xy_SurfTemp = xy_SurfTemp + xy_DSurfTempDt * DelTime

    ! 蝨溷｣梧ｸｩ蠎ｦ譎る俣遨榊�
    ! Time integration of subsurface temperature
    !
    xyz_SoilTemp = xyz_SoilTemp + xyz_DSoilTempDt * DelTime

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

  end subroutine IntegralSurfTemp

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

  subroutine IntgSurfTempInit
    !
    ! intg_surftemp 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#intg_surftemp_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "intg_surftemp" module is initialized.
    ! "NAMELIST#intg_surftemp_nml" is loaded in this procedure.
    !

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

    ! 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

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
!!$    namelist /intg_surftemp_nml/
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "intg_surftemp#IntgSurfTempInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "intg_surftemp#IntgSurfTempInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( intg_surftemp_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! 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 = intg_surftemp_nml, &  ! (out)
!!$        & iostat = iostat_nml )   ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    intg_surftemp_inited = .true.

  end subroutine IntgSurfTempInit

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

end module intg_surftemp