!= 雪と海氷によるアルベド変化
!
!= modification of surface albedo on the snow covered ground and on the sea ice
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: modify_albedo_snowseaice.f90,v 1.1 2009-08-09 12:36:24 yot Exp $
! Tag Name::  $Name: dcpam5-20110221-4 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module modify_albedo_snowseaice

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

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


  ! 格子点設定
  ! Grid points settings

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: ModAlbedoDueToSnowSeaIce

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: modify_albedo_snowseaice_inited = .false.
                              ! 初期設定フラグ.
                              ! Initialization flag

  ! 非公開変数
  ! Private variables
  !

  character(*), parameter:: module_name = 'modify_albedo_snowseaice'
                              ! モジュールの名称.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name: dcpam5-20110221-4 $' // &
    & '$Id: modify_albedo_snowseaice.f90,v 1.1 2009-08-09 12:36:24 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version

  ! INTERFACE 文 ; INTERFACE statements
  !

contains

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

  subroutine ModAlbedoDueToSnowSeaIce(          &
    & xy_SurfCond,                              &
    & xy_SurfSnow, xy_SeaIceConc,               & ! (in   ) optional
    & xy_SurfAlbedo                             & ! (inout)
    )

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

    ! 雪と海氷の定数の設定
    ! Setting constants of snow and sea ice
    !
    use constants_snowseaice, only:            &
      & SnowThreshold => ThresholdSurfSnow,    &
      & SnowAlbedo,                            &
      & SeaIceThreshold,                       &
      & SeaIceAlbedo

    integer , intent(in   )           :: xy_SurfCond  ( 0:imax-1, 1:jmax )
    real(DP), intent(in   ), optional :: xy_SurfSnow  ( 0:imax-1, 1:jmax )
    real(DP), intent(in   ), optional :: xy_SeaIceConc( 0:imax-1, 1:jmax )
    real(DP), intent(inout)           :: xy_SurfAlbedo( 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. modify_albedo_snowseaice_inited ) call ModAlbedoSnowSeaIceInit

    if ( present( xy_SurfSnow ) ) then
      ! modify surface albedo on the snow covered ground
      !
      do j = 1, jmax
        do i = 0, imax-1
          if ( xy_SurfCond(i,j) > 0 .and. xy_SurfSnow(i,j) > SnowThreshold ) then
            xy_SurfAlbedo(i,j) = SnowAlbedo
          end if
        end do
      end do
    end if

    if ( present( xy_SeaIceConc ) ) then
      ! modify surface albedo on the sea ice
      !
      do j = 1, jmax
        do i = 0, imax-1
          if ( xy_SurfCond(i,j) == 0 .and. xy_SeaIceConc(i,j) > SeaIceThreshold ) then
            xy_SurfAlbedo(i,j) = SeaIceAlbedo
          end if
        end do
      end do
    end if


  end subroutine ModAlbedoDueToSnowSeaIce

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

  subroutine ModAlbedoSnowSeaIceInit

    ! 作業変数
    ! Work variables
    !


    modify_albedo_snowseaice_inited = .true.

  end subroutine ModAlbedoSnowSeaIceInit

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

end module modify_albedo_snowseaice
