!= 地面温度の時間積分・地表面放射補正
!
!= Time integration of surface temperature, correction of flux on surface
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: intg_surftemp.f90,v 1.1 2008-11-08 15:34:03 morikawa Exp $
! Tag Name::  $Name: dcpam5-20081109-1 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module intg_surftemp
  !
  != 地面温度の時間積分・地表面放射補正
  !
  != Time integration of surface temperature, correction of flux on surface
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 地面温度の時間積分と地表面放射補正を行います.
  !
  ! Time integration of surface temperature, and
  ! correction of flux on surface
  !
  !== Procedures List
  !
  ! IntegralSurfTemp   :: 地面温度の時間積分・地表面放射補正
  ! ---------------    :: ---------------
  ! IntegralSurfTemp   :: Time integration of surface temperature, correction of flux on surface
  !
  !--
  !== 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

  ! 種別型パラメタ
  ! 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 variables
  !
  logical, save, public:: intg_surftemp_inited = .false.
                              ! 初期設定フラグ.
                              ! Initialization flag

  ! 非公開変数
  ! Private variables
  !

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

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

contains

  subroutine IntegralSurfTemp( &
    & xy_DSurfTempDt, &                              ! (in)
    & xy_SurfTemp, xyr_RadLFlux, xyra_DelRadLFlux &  ! (inout)
    & )
    !
    ! 地面温度の時間積分を地表面放射補正を行います.
    !
    ! Time integration of surface temperature, correction of flux on surface.
    !
    !

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

    ! 積分と平均の操作
    ! Operation for integral and average
    !
    use intavr_operate, only: AvrLonLat_xy

    ! 時刻管理
    ! 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)
                              ! 地表面温度変化率.
                              ! Surface temperature tendency
    real(DP), intent(inout):: xy_SurfTemp (0:imax-1, 1:jmax)
                              ! 地表面温度.
                              ! Surface temperature
    real(DP), intent(inout):: xyr_RadLFlux (0:imax-1, 1:jmax, 0:kmax)
                              ! 長波フラックス.
                              ! Longwave flux
    real(DP), intent(inout):: xyra_DelRadLFlux (0:imax-1, 1:jmax, 0:kmax, 0:1)
                              ! 長波地表温度変化.
                              ! Surface temperature tendency with longwave

    ! 作業変数
    ! 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. intg_surftemp_inited ) call IntgSurfTempInit

    ! 長波フラックスの補正 ( 地表フラックス分の補正 )
    ! Correction of long wave flux ( amount of surface flux )
    !
    do k = 0, kmax
      xyr_RadLFlux (:,:,k) = &
        &     xyr_RadLFlux (:,:,k) &
        &   + xyra_DelRadLFlux(:,:,k,0) * xy_DSurfTempDt (:,:) * DelTime
    end do

    ! 補正項係数変更 ( 地表面放射分 その1 )
    ! Modification of coefficient of correction term ( amount of surface radition part I )
    !
    do k = 0, kmax
      xyra_DelRadLFlux (:,:,k,0) = xyra_DelRadLFlux (:,:,k,0) / xy_SurfTemp (:,:)**3
    end do

    ! 地表面温度時間積分
    ! Time integration of surface temperature
    !
    xy_SurfTemp(:,:) = xy_SurfTemp(:,:) + xy_DSurfTempDt(:,:) * DelTime

    ! 補正項係数変更 地表面放射分 その2
    ! Modification of coefficient of correction term ( amount of surface radition part II )
    !
    do k = 0, kmax
      xyra_DelRadLFlux (:,:,k,0) = &
        & xyra_DelRadLFlux (:,:,k,0) * xy_SurfTemp (:,:)**3
    end do

    ! 計算時間計測一時停止
    ! 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
    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 = 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

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

  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

    ! 時刻管理
    ! Time control
    !
    use timeset, only: timeset_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.' )

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

  end subroutine InitCheck

end module intg_surftemp