!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
!---------------------------------------------------------------------
! physics_ground.f90 
! SST, 及び地表面諸量の読み込み
!
! History
!   2005/09/20 Yamada Yukiko     create
!

module physics_ground_mod

  use type_mod,    only : REKIND, DBKIND, INTKIND, TOKEN, STRING

  implicit none

  private
  public :: physics_ground

contains

  subroutine physics_ground( &
       &  xy_SurfTemp   ,& ! (out) 地表温度
       &  y_Lat         ) ! (in) 緯度座標

!!!! AGCM5 で他に設定できるらしい地表面情報 !!!!!
!       & xy_SurfTemp                  , & ! 地表温度     = SST
!       & xy_SurfHeatCapa              , & ! 地表熱容量   = 0.0
!       & xy_SurfAlbedo                , & ! 地表アルベド = 0.15
!       & xy_SurfHumidCoeff            , & ! 地表湿潤度   = 1.0
!       & xy_SurfRoughLength           , & ! 地表粗度長   = 0.0001
!      REAL       GRZSD  ( IDIM*JDIM )    !" 地表高度分散 = 0.0
!      INTEGER    IDSFC  ( IDIM*JDIM )    !" 地表状態     = 0.0
!      REAL       GFLUX  ( IDIM*JDIM )    !" 地中熱フラックス = 0.0


    !==== Dependency
    use type_mod,      only: REKIND, DBKIND, INTKIND, TOKEN, STRING
    use grid_3d_mod,   only: im, jm, km
    use dc_trace,      only: SetDebug, BeginSub, EndSub, DbgMessage, DataDump

    implicit none

    !==== Input
    !
    real(DBKIND), intent(in) ::   &
         &  y_Lat(:)                ! (in) 緯度座標

    !==== Output
    !
    real(DBKIND), intent(out) ::  &
         & xy_SurfTemp(im,jm)       ! (out) 地表面温度

    !----- 作業用内部変数 -----
    character(STRING),  parameter:: subname = "physics_ground"
    real(DBKIND)        ::  & 
         & xy_SeaSurfaceTemp(im,jm)            ! 海表面温度

    ! Hosaka et al. (1998) SST 用パラメータ
    real(DBKIND), parameter :: TEQ   = 302.0d0
    real(DBKIND), parameter :: ALAT0 =   0.0d0
    real(DBKIND), parameter :: ALAT1 =  30.0d0
    real(DBKIND), parameter :: ALPHA =  60.0d0
    real(DBKIND), parameter :: BETA  =  32.0d0
    real(DBKIND), parameter :: GAMMA =   0.0d0
    real(DBKIND), parameter :: ALACON = 7.0d0

    continue

    !----------------------------------------------------------------
    !   開始処理
    !----------------------------------------------------------------
    call BeginSub(subname)

    !----------------------------------------------------------------
    !   地表面温度の設定
    !----------------------------------------------------------------
    
    !----- 海面温度の設定 -----
    ! 本当はファイルから読み出すべきだが暫定的処置. 
    ! Hosaka et al. (1998) の SST 分布を与える. 
    
    call mksst( &
         & xy_SeaSurfaceTemp                           , &
         & y_Lat                                       , &
         & TEQ , ALAT0 , ALAT1 , ALPHA , BETA  , GAMMA , &
         & ALACON                                       )


    !----- 地表面温度を海面温度で置き換え-----
    xy_SurfTemp = xy_SeaSurfaceTemp


    !----------------------------------------------------------------
    !   終了処理
    !----------------------------------------------------------------
    call EndSub(subname)

  end subroutine physics_ground


  subroutine mksst( &
    & xy_SeaSurfaceTemp                           , &
    & y_Lat                                       , &
    & TEQ , ALAT0 , ALAT1 , ALPHA , BETA  , GAMMA , &
    & ALACON                                       )

    !==== Dependency
    use type_mod,      only: REKIND, DBKIND, INTKIND, TOKEN, STRING
    use grid_3d_mod,   only: im, jm, km
    use constants_mod, only: PI 
    use dc_trace,      only: SetDebug, BeginSub, EndSub, DbgMessage, DataDump

    implicit none

    !==== Input
    !
    real(DBKIND), intent(in) ::   &
         &  y_Lat(:)             ,& ! (in) 経度座標
         &  TEQ , ALAT0 , ALAT1 , ALPHA , BETA  , GAMMA , ALACON

    !==== Output
    !
    real(DBKIND), intent(out) ::  &
         & xy_SeaSurfaceTemp(im,jm)       ! (out) 地表面温度

    !----- 作業用内部変数 -----
    character(STRING),  parameter:: subname = "mksst"

    ! do ループ用作業変数 (東西 i*、南北 j*、鉛直 k*、波数 l*用)
    integer(INTKIND)    :: i, j

    real(DBKIND) :: PHI1, AB4, PHI, GSSTP, ALATP, ALATM, GSSTMX
    integer(INTKIND)    :: JP, JMM

    continue

    !----------------------------------------------------------------
    !   開始処理
    !----------------------------------------------------------------
    call BeginSub(subname)

    !----------------------------------------------------------------
    !   海表面温度の設定
    !----------------------------------------------------------------

    PHI1 = ABS( ALAT1 * PI/180. )
    AB4  = 2. *( PHI1**3 )*BETA/ALPHA
    
    do j = 1, jm
       PHI   = ABS( y_Lat( J )*PI/180. - ALAT0 *PI/180. )
       GSSTP = TEQ                                                      &
            &         - ALPHA/2.                                        &
            &           * ( PHI - MAX(   SQRT(  PHI1**2+AB4 )      &
            &                     - SQRT( (PHI-PHI1)**2+AB4 ), 0.  )  ) &
            &         + GAMMA *( PHI**3 )
       do i = 1, im
          xy_SeaSurfaceTemp(i,j) = GSSTP
       end do
    end do
        
    ! ---- 中心 ALAT0 +/- ALACON の間を平坦に -----
    ALATP = ( ALAT0 + ALACON )*PI/180.
    ALATM = ( ALAT0 - ALACON )*PI/180.

    JP = 1
    JMM = jm
    do  j = 1, jm
       IF ( (-y_Lat( J )*PI/180.) .GT. ALATP ) THEN
          JMM = J
       ENDIF
       IF ( (-y_Lat( J )*PI/180.) .GE. ALATM ) THEN
          JP = J
       ENDIF
    end do

    GSSTMX = ( xy_SeaSurfaceTemp(1,JP) * ( ALATP - y_Lat(JP+1)*PI/180.)     &
         &   +  xy_SeaSurfaceTemp(1,JP+1) * ( y_Lat(JP)*PI/180. - ALATP ))  &
         &        /( y_Lat(JP)*PI/180. - y_Lat(JP+1)*PI/180. ) 

    do j = JMM+1, JP 
       do i = 1, im 
          xy_SeaSurfaceTemp(i,j) = GSSTMX
       end do
    end do

    !----------------------------------------------------------------
    !   終了処理
    !----------------------------------------------------------------
    call EndSub(subname)

  end subroutine mksst

end module physics_ground_mod




