!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine SurfaceHeatFlux
!
!   * Developer: KITAMORI Taichi 
!   * Version: $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!地表面熱フラックスを計算する. 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!  
!
!== Future Plans
!
                                                                 !=end
subroutine surfaceheatflux(ss_CpBasicZ,      &
                      &    ss_DensBasicZ,    &
                      &    ss_PotTempBasicZ, &
                      &    ss_ExnerBasicZ,   &
                      &    fs_VelX_bl,       &
                      &    ss_PotTemp_bl,    &
                      &    ss_Exner_bl,      &
                      &    sf_SfcTemp,       &
                      &    sf_SfcHeatFlux)

  !==Dependency
  use dc_trace, only: BeginSub, EndSub  
  use gridset,  only: DimXMin, DimXMax, DimZMin, DimZMax, RegZMin
  use average,  only: ss_avr_fs

  !==暗黙の型宣言を禁止
  implicit none
  
  !==Input
  real(8), intent(in)   :: ss_CpBasicZ(DimXMin:DimXMax,DimZMin:DimZMax)   
  real(8), intent(in)   :: ss_DensBasicZ(DimXMin:DimXMax,DimZMin:DimZMax)   
  real(8), intent(in)   :: ss_PotTempBasicZ(DimXMin:DimXMax,DimZMin:DimZMax)   
  real(8), intent(in)   :: ss_ExnerBasicZ(DimXMin:DimXMax,DimZMin:DimZMax)   
  real(8), intent(in)   :: fs_VelX_bl(DimXMin:DimXMax,DimZMin:DimZMax)
  real(8), intent(in)   :: ss_PotTemp_bl(DimXMin:DimXMax,DimZMin:DimZMax)
  real(8), intent(in)   :: ss_Exner_bl(DimXMin:DimXMax,DimZMin:DimZMax)
  real(8), intent(in)   :: sf_SfcTemp(DimXMin:DimXMax)   

  !==Output
  real(8), intent(out)  :: sf_SfcHeatFlux(DimXMin:DimXMax)
  
  !==Work
  real(8)               :: ss_TempTotal(DimXMin:DimXMax)
  real(8)               :: ss_VelX_bl(DimXMin:DimXMax,DimZMin:DimZMax)
  real(8), parameter    :: Bulk = 0.01d0

  call BeginSub("SurfaceHeatFlux", &
&           fmt="%c",        &
&           c1="Calculate temparature on surface (sf_SfcHeatFlux).")

  !--- モデル最下層の温位を温度になおす

  write(*,*) 'PotTempBasic',ss_PotTempBasicZ(20, RegZMin + 1)

  ss_TempTotal(DimXMin:DimXMax) =     &
       & (                       &
       &     ss_PotTempBasicZ(DimXMin:DimXMax, RegZMin + 1)  &
       &   + ss_PotTemp_bl(DimXMin:DimXMax, RegZMin + 1)     &
       & )                                                   &
       & * (                     & 
       &       ss_Exner_bl(DimXMin:DimXMax, RegZMin + 1)     &
       &     + ss_ExnerBasicZ(DimXMin:DimXMax, RegZMin + 1)  &
       &   )
  
  !--- 平均操作
  ss_VelX_bl = ss_avr_fs( fs_VelX_bl )

  !--- 地表面熱フラックスを計算
  sf_SfcHeatFlux =  Bulk    &
       & * ss_CpBasicZ(DimXMin:DimXMax, RegZMin + 1) &
       & * ss_DensBasicZ(DimXMin:DimXMax, RegZMin + 1) &
       & * abs( ss_VelX_bl(DimXMin:DimXMax, RegZMin + 1) ) &
       & * (sf_SfcTemp - ss_TempTotal)

  write(*,*) 'Heat',sf_SfcHeatFlux(20)
  write(*,*) 'Dens',ss_DensBasicZ(20, RegZMin + 1)
  write(*,*) 'VelX',ss_VelX_bl(20, RegZMin + 1)
  write(*,*) 'DelT',sf_SfcTemp(20) - ss_TempTotal(20)

  call EndSub("SurfaceHeatFlux")

end subroutine surfaceheatflux

