!= åœ°è¡¨é¢ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ãƒ¦ãƒ¼ãƒ†ã‚£ãƒªãƒ†ã‚£
!
!= Surface flux utility routines
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: surface_flux_util.f90,v 1.1 2013/10/06 13:44:15 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module surface_flux_util
  !
  != åœ°è¡¨é¢ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ãƒ¦ãƒ¼ãƒ†ã‚£ãƒªãƒ†ã‚£
  !
  != Surface flux utility routines
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! åœ°è¡¨é¢ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã‚’è¨ˆç®—.
  !
  ! Surface fluxes are calculated.
  !
  !== References
  !
!!$  ! Louis, J-F., M. Tiedtke, and J-F. Geleyn,
!!$  ! A short history of the PBL parameterization at ECMWF,
!!$  ! Workshop on Planetary Boundary Layer Parameterization, 59-80, ECMWF, Reading, U.K.,
!!$  ! 1982.
  !
  !== Procedures List
  !
!!$  ! SurfaceFlux       :: åœ°è¡¨é¢ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®è¨ˆç®—
!!$  ! ------------      :: ------------
!!$  ! SurfaceFlux       :: Calculate surface fluxes
  !
  !== NAMELIST
  !
  ! NAMELIST#surface_flux_util_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

  ! çµ„æˆã«é–¢ã‚ã‚‹é…åˆ—ã®è¨å®š
  ! Settings of array for atmospheric composition
  !
  use composition, only: &
    & ncmax, &
    & IndexH2OVap

  ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
  ! 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 :: SurfaceFluxUtilLimitFlux
  public :: SurfaceFluxUtilInit

  ! å…¬é–‹å¤‰æ•°
  ! Public variables
  !

  ! éžå…¬é–‹å¤‰æ•°
  ! Private variables
  !
  logical, save :: surface_flux_util_inited = .false.
                              ! åˆæœŸè¨å®šãƒ•ãƒ©ã‚°.
                              ! Initialization flag

  character(*), parameter:: module_name = 'surface_flux_util'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åç§°.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: surface_flux_util.f90,v 1.1 2013/10/06 13:44:15 yot Exp $'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³
                              ! Module version

contains

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

  subroutine SurfaceFluxUtilLimitFlux( &
    & DelTime,                         & ! (in)
    & xyzf_QMix, xyr_Press,            & ! (in)
    & xy_SurfH2OVapFlux &!, xyf_QMixFlux  & ! (inout)
    & )
    !
    !
    !
    ! Restrict surface flux
    ! Now, only the H2O vapor flux is restricted.
    !

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿å‡ºåŠ›
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoPut

    ! ç‰©ç†å®šæ•°è¨å®š
    ! Physical constants settings
    !
    use constants, only: &
      & Grav                  ! $ g $ [m s-2].
                              ! é‡åŠ›åŠ é€Ÿåº¦.
                              ! Gravitational acceleration

    ! æ™‚åˆ»ç®¡ç†
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! ã‚¹ãƒ†ãƒƒãƒ— $ t $ ã®æ™‚åˆ». Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! ãƒ‡ãƒãƒƒã‚°ç”¨ãƒ¦ãƒ¼ãƒ†ã‚£ãƒªãƒ†ã‚£
    ! Utilities for debug
    !
    use dc_trace, only: DbgMessage, BeginSub, EndSub

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: DelTime
                              ! Time step
    real(DP), intent(in):: xyzf_QMix(0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q $ .     æ¯”æ¹¿. Specific humidity
    real(DP), intent(in):: xyr_Press (0:imax-1, 1:jmax, 0:kmax)
                              ! $ p_s $ . åœ°è¡¨é¢æ°—åœ§ (åŠæ•´æ•°ãƒ¬ãƒ™ãƒ«).
                              ! Surface pressure (half level)
    real(DP), intent(inout):: xy_SurfH2OVapFlux(0:imax-1, 1:jmax)
                              ! æƒ‘æ˜Ÿè¡¨é¢æ°´è’¸æ°—ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Water vapor flux at the surface
!!$    real(DP), intent(inout):: xyf_QMixFlux(0:imax-1, 1:jmax, 1:ncmax)
!!$                              ! æƒ‘æ˜Ÿè¡¨é¢æ¯”æ¹¿ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
!!$                              ! Specific humidity flux at surface

    ! ä½œæ¥å¤‰æ•°
    ! Work variables
    !
    real(DP):: xyz_DelMass(0:imax-1, 1:jmax, 1:kmax)
                              !
                              ! Mass in each layer
    real(DP):: xy_ConsMass(0:imax-1, 1:jmax)
                              !
                              ! Constituent mass in each column
    real(DP):: xy_SurfFlux(0:imax-1, 1:jmax)

    integer:: i               ! çµŒåº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in longitude
    integer:: j               ! ç·¯åº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in latitude
    integer:: k               ! é«˜åº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in altitude
    integer:: n               ! çµ„æˆæ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in dimension of constituents

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

    ! åˆæœŸåŒ–ç¢ºèª
    ! Initialization check
    !
    if ( .not. surface_flux_util_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! è¨ˆç®—æ™‚é–“è¨ˆæ¸¬é–‹å§‹
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    do k = 1, kmax
      xyz_DelMass(:,:,k) = ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k) ) / Grav
    end do

!!$    do n = 1, ncmax
    do n = IndexH2OVap, IndexH2OVap

      xy_ConsMass = 0.0_DP
      do k = kmax, 1, -1
        xy_ConsMass = xy_ConsMass + xyz_DelMass(:,:,k) * xyzf_QMix(:,:,k,n)
      end do

      if ( n == IndexH2OVap ) then
        xy_SurfFlux = xy_SurfH2OVapFlux
      else
!!$        xy_SurfFlux = xyf_QMixFlux(:,:,n)
      end if

      do j = 1, jmax
        do i = 0, imax-1
          if ( - xy_SurfFlux(i,j) * DelTime > xy_ConsMass(i,j) ) then
            xy_SurfFlux(i,j) = - xy_ConsMass(i,j) / DelTime !&
!!$              & * ( 1.0_DP - 1.0d-15 )
          end if
        end do
      end do

      if ( n == IndexH2OVap ) then
        xy_SurfH2OVapFlux = xy_SurfFlux
      else
!!$        xyf_QMixFlux(:,:,n) = xy_SurfFlux
      end if

    end do

    ! è¨ˆç®—æ™‚é–“è¨ˆæ¸¬ä¸€æ™‚åœæ¢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine SurfaceFluxUtilLimitFlux

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

  subroutine SurfaceFluxUtilInit
    !
    ! surface_flux_util ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸåŒ–ã‚’è¡Œã„ã¾ã™.
    ! NAMELIST#surface_flux_util_nml ã®èªã¿è¾¼ã¿ã¯ã“ã®æ‰‹ç¶šãã§è¡Œã‚ã‚Œã¾ã™.
    !
    ! "surface_flux_util" module is initialized.
    ! "NAMELIST#surface_flux_util_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

    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿å‡ºåŠ›
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! åº§æ¨™ãƒ‡ãƒ¼ã‚¿è¨å®š
    ! Axes data settings
    !
    use axesset, only: &
      & AxnameX, &
      & AxnameY, &
      & AxnameZ, &
      & AxnameR, &
      & AxnameT

    ! å®£è¨€æ–‡ ; 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 /surface_flux_util_nml/                                  &
!!$      & FlagConstBulkCoef
          !
          ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã«ã¤ã„ã¦ã¯åˆæœŸåŒ–æ‰‹ç¶š "surface_flux_util#SurfaceFluxUtilInit"
          ! ã®ã‚½ãƒ¼ã‚¹ã‚³ãƒ¼ãƒ‰ã‚’å‚ç…§ã®ã“ã¨.
          !
          ! Refer to source codes in the initialization procedure
          ! "surface_flux_bulk#SurfaceFluxInit" for the default values.
          !

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

    if ( surface_flux_util_inited ) return

    ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã®è¨å®š
    ! 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 = surface_flux_util_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, '  VelMinForRi       = %f', d = (/ VelMinForRi   /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    surface_flux_util_inited = .true.

  end subroutine SurfaceFluxUtilInit

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

end module surface_flux_util