!= バケツモデル
!
!= Bucket model
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: bucket_model.f90,v 1.1 2009-04-15 22:24:27 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module Bucket_Model

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


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

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  logical, save:: bucket_model_inited = .false.
                              ! 初期設定フラグ.
                              ! Initialization flag


  logical, save, public:: FlagBucketModel   ! バケツモデル オン／オフ. 
                                            ! bucket model on/off. 

  ! 公開手続き
  ! Public procedure
  !
  public:: BucketEvap
  public:: BucketPrepAdjust
  public:: BucketSetHumidCoef
  public:: BucketModQvapFlux

  ! 非公開変数
  ! Private variables
  !
  real(DP), save:: SoilMoistCritAmnt
                        ! <Japanese>
                        ! Critical amount of soil moisture
  real(DP), save:: SoilMoistCritRatioforEvapEff
                        ! <Japanese>
                        ! Critical ratio of soil moisture for evaporation efficiency
  real(DP), save:: SoilMoistCritAmntforEvapEff
                        ! <Japanese>
                        ! Critical amount of soil moisture for evaporation efficiency

  real(DP), save:: SoilMoistMeaningLess = -1.0d0
                        ! <Japanese>
                        ! Meaning less value for soil moisture on the ocean

  character(*), parameter:: module_name = 'bucket_model'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: bucket_model.f90,v 1.1 2009-04-15 22:24:27 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version


  ! INTERFACE 文 ; INTERFACE statements
  !


  !**************************************************************************************

contains

  !**************************************************************************************

  subroutine BucketEvap( &
    & xy_SurfCond, xy_SurfEvapFlux, xy_SoilMoistB, &  ! (in )
    & xy_SoilMoistA                                &  ! (out)
    )

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & DelTime            ! $ \Delta t $ [s]

    ! 格子点設定
    ! 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

    integer , intent(in ) :: xy_SurfCond    ( 0:imax-1, 1:jmax )
    real(DP), intent(in ) :: xy_SurfEvapFlux( 0:imax-1, 1:jmax )
    real(DP), intent(in ) :: xy_SoilMoistB  ( 0:imax-1, 1:jmax )
    real(DP), intent(out) :: xy_SoilMoistA  ( 0:imax-1, 1:jmax )

    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude


    ! 初期化
    ! Initialization
    !
    if ( .not. bucket_model_inited ) call BucketInit

    if ( .not. FlagBucketModel ) then
      xy_SoilMoistA = xy_SoilMoistB
      return
    end if


    ! Evaporation is subtracted from soil moisture
    !
    xy_SoilMoistA = xy_SoilMoistB - xy_SurfEvapFlux * 2.0d0 * DelTime

    ! Remove negative values
    !
    do j = 1, jmax
      do i = 0, imax-1
        if( xy_SoilMoistA( i, j ) < 0.0d0 ) xy_SoilMoistA( i, j ) = 0.0d0
      end do
    end do

    ! Fill meaningless value in ocean grid
    !
    do j = 1, jmax
      do i = 0, imax-1
        if ( xy_SurfCond( i, j ) == 0 ) then
          xy_SoilMoistA( i, j ) = SoilMoistMeaningLess
        end if
      end do
    end do


  end subroutine BucketEvap

  !**************************************************************************************

  subroutine BucketPrepAdjust( &
    & xy_SurfCond, xy_SurfPrepFlux, &  ! (in )
    & xy_SoilMoist                  &  ! (inout)
    )

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & DelTime            ! $ \Delta t $ [s]

    ! 格子点設定
    ! 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

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

    integer , intent(in   ) :: xy_SurfCond    ( 0:imax-1, 1:jmax )
    real(DP), intent(in   ) :: xy_SurfPrepFlux( 0:imax-1, 1:jmax )
    real(DP), intent(inout) :: xy_SoilMoist   ( 0:imax-1, 1:jmax )


    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude


    ! 初期化
    ! Initialization
    !
    if ( .not. bucket_model_inited ) call BucketInit

    if ( .not. FlagBucketModel ) return


    ! Precipitation is added to soil moisture
    !
    xy_SoilMoist = xy_SoilMoist + xy_SurfPrepFlux * 2.0d0 * DelTime

    ! Calculation of Run-off
    !
    do j = 1, jmax
      do i = 0, imax-1
        if ( xy_SoilMoist( i, j ) > SoilMoistCritAmnt ) &
          & xy_SoilMoist( i, j ) = SoilMoistCritAmnt
      end do
    end do

    ! Fill meaningless value in ocean grid
    !
    do j = 1, jmax
      do i = 0, imax-1
        if ( xy_SurfCond( i, j ) == 0 ) then
          xy_SoilMoist( i, j ) = SoilMoistMeaningLess
        end if
      end do
    end do


  end subroutine BucketPrepAdjust

  !**************************************************************************************

  subroutine BucketSetHumidCoef(           &
    & xy_SurfCond, xy_SoilMoist,           & ! (in   )
    & xy_SurfHumidCoef                     & ! (inout)
    )

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & DelTime            ! $ \Delta t $ [s]

    ! 格子点設定
    ! 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

    integer , intent(in   ) :: xy_SurfCond     ( 0:imax-1, 1:jmax )
    real(DP), intent(in   ) :: xy_SoilMoist    ( 0:imax-1, 1:jmax )
    real(DP), intent(inout) :: xy_SurfHumidCoef( 0:imax-1, 1:jmax )


    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude


    ! 初期化
    ! Initialization
    !
    if ( .not. bucket_model_inited ) call BucketInit

    if ( .not. FlagBucketModel ) return


    ! Surface humidity coefficient is modified.
    !
    do j = 1, jmax
      do i = 0, imax-1
        if ( xy_SurfCond( i, j ) == 0 ) then
          xy_SurfHumidCoef( i, j ) = 1.0d0
        else
          xy_SurfHumidCoef( i, j ) = xy_SoilMoist( i, j ) / SoilMoistCritAmntforEvapEff
          if ( xy_SurfHumidCoef( i, j ) > 1.0d0 ) xy_SurfHumidCoef( i, j ) = 1.0d0
        end if
      end do
    end do


  end subroutine BucketSetHumidCoef

  !**************************************************************************************

  subroutine BucketModQvapFlux(            &
    & xy_SurfCond, xy_SoilMoist,           & ! (in   )
    & xy_SurfEvapFlux                      & ! (inout)
    )

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & DelTime            ! $ \Delta t $ [s]

    ! 格子点設定
    ! 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

    integer , intent(in   ) :: xy_SurfCond     ( 0:imax-1, 1:jmax )
    real(DP), intent(in   ) :: xy_SoilMoist    ( 0:imax-1, 1:jmax )
    real(DP), intent(inout) :: xy_SurfEvapFlux ( 0:imax-1, 1:jmax )


    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude


    ! 初期化
    ! Initialization
    !
    if ( .not. bucket_model_inited ) call BucketInit

    if ( .not. FlagBucketModel ) return


    ! Surface water vapor flux is limited up to the water amount on the land. 
    !
    do j = 1, jmax
      do i = 0, imax-1
        if ( ( xy_SurfCond    ( i, j )                   >= 1                    ) .and.&
          &  ( xy_SurfEvapFlux( i, j )                   >  0.0d0                ) .and.&
          &  ( xy_SurfEvapFlux( i, j ) * 2.0d0 * DelTime >  xy_SoilMoist( i, j ) ) ) &
          & then
          xy_SurfEvapFlux( i, j ) = xy_SoilMoist( i, j ) / ( 2.0d0 * DelTime )
        end if
      end do
    end do


  end subroutine BucketModQvapFlux

  !**************************************************************************************

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

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

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

    ! ファイル入出力補助
    ! 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

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


    ! 宣言文 ; 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 /bucket_model_nml/ &
      & FlagBucketModel, &
      & SoilMoistCritAmnt, SoilMoistCritRatioforEvapEff


    ! 実行文 ; Executable statement
    !

    if ( bucket_model_inited ) return
!!$    call InitCheck

    ! デフォルト値の設定
    ! Default values settings
    !
    FlagBucketModel              = .false.

    SoilMoistCritAmnt            = 1.0d3 * 0.15d0
    SoilMoistCritRatioforEvapEff = 0.75d0

    SoilMoistCritAmntforEvapEff  = SoilMoistCritAmnt * SoilMoistCritRatioforEvapEff


    ! 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 = bucket_model_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, '  FlagBucketModel              = %y', l = (/ FlagBucketModel   /) )
    call MessageNotify( 'M', module_name, '  SoilMoistCritAmnt            = %f', d = (/ SoilMoistCritAmnt  /) )
    call MessageNotify( 'M', module_name, '  SoilMoistCritRatioforEvapEff = %f', d = (/ SoilMoistCritRatioforEvapEff /) )


    bucket_model_inited = .true.


  end subroutine BucketInit

  !**************************************************************************************

end module Bucket_Model
