!= 莠亥�ｱ螟画焚縺ｮ蛟､縺ｮ遒ｺ隱�
!
!= Check values of prognostic variables
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: check_prog_vars.f90,v 1.5 2013/02/19 01:33:12 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module check_prog_vars
  !
  != 莠亥�ｱ螟画焚縺ｮ蛟､縺ｮ遒ｺ隱�
  !
  != Check values of prognostic variables
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 莠亥�ｱ螟画焚縺ｮ蛟､縺ｮ遒ｺ隱�
  !
  ! Check values of prognostic variables
  !
  !== Procedures List
  !
!!$  ! SurfaceFlux       :: 蝨ｰ陦ｨ髱｢繝輔Λ繝�け繧ｹ縺ｮ險育ｮ�
!!$  ! SurfaceFluxOutput :: 蝨ｰ陦ｨ髱｢繝輔Λ繝�け繧ｹ縺ｮ蜃ｺ蜉�
!!$  ! ------------      :: ------------
!!$  ! SurfaceFlux       :: Calculate surface fluxes
!!$  ! SurfaceFluxOutput :: Output surface fluxes
  !
  !== NAMELIST
  !
  ! NAMELIST#check_prog_vars_nml
  !

  ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
  !

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.
    &                 STRING     ! 譁�ｭ怜�.       Strings.

  ! 螳｣險譁� ; Declaration statements
  !
  implicit none
  private

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public :: CheckProgVars
  public :: CheckProgVarsInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save :: check_prog_vars_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  real(DP), save:: TempMin
                            ! 貂ｩ蠎ｦ譛蟆丞､
                            ! Minimum value of temperature
  real(DP), save:: TempMax
                            ! 貂ｩ蠎ｦ譛螟ｧ蛟､
                            ! Maximum value of temperature
  real(DP), save:: VelMin
                            ! 鬚ｨ騾滓怙蟆丞､
                            ! Minimum value of wind velocity
  real(DP), save:: VelMax
                            ! 鬚ｨ騾滓怙螟ｧ蛟､
                            ! Maximum value of wind velocity

  character(*), parameter:: module_name = 'check_prog_vars'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: check_prog_vars.f90,v 1.5 2013/02/19 01:33:12 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

  ! INTERFACE 譁� ; INTERFACE statements
  !
!!$  interface SurfaceFlux
!!$    module procedure SurfaceFlux
!!$  end interface
!!$
!!$  interface SurfaceFluxOutput
!!$    module procedure SurfaceFluxOutput
!!$  end interface

contains

  subroutine CheckProgVars( &
    & xy_Ps, xyz_U, xyz_V, xyz_Temp, xyzf_QMix   & ! (in)
    & )
    !
    ! 莠亥�ｱ螟画焚縺ｮ蛟､縺ｮ遒ｺ隱�
    !
    ! Check values of prognostic variables
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; 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

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
    ! Axes data settings
    !
    use axesset, only: &
      & x_Lon,   & ! $ \lambda $ [rad.] . 邨悟ｺｦ. Longitude
      & y_Lat,   & ! $ \varphi $ [rad.] . 邱ｯ蠎ｦ. Latitude
      & z_Sigma    ! $ \sigma $ 繝ｬ繝吶Ν (謨ｴ謨ｰ).  Full $ \sigma $ level

    ! 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
    ! Settings of array for atmospheric composition
    !
    use composition, only: ncmax, IndexH2OVap

    ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
    ! Message output
    !
    use dc_message, only: MessageNotify

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾. Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! 繝�ヰ繝�げ逕ｨ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
    ! Utilities for debug
    !
    use dc_trace, only: DbgMessage, BeginSub, EndSub

    ! 迚ｩ逅��謨ｰ蟄ｦ螳壽焚險ｭ螳�
    ! Physical and mathematical constants settings
    !
    use constants0, only: &
      & PI                    ! $ \pi $ .
                              ! Circular constant

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(in):: xy_Ps (0:imax-1, 1:jmax)
                              ! $ p_s (t-\Delta t) $ . 蝨ｰ陦ｨ髱｢豌怜悸. Surface pressure (Pa)
    real(DP), intent(in):: xyz_U (0:imax-1, 1:jmax, 1:kmax)
                              ! $ u $ . 譚ｱ隘ｿ鬚ｨ騾�. Eastward wind
    real(DP), intent(in):: xyz_V (0:imax-1, 1:jmax, 1:kmax)
                              ! $ v $ . 蜊怜圏鬚ｨ騾�. Northward wind
    real(DP), intent(in):: xyz_Temp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ . 貂ｩ蠎ｦ (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Temperature (full level)
    real(DP), intent(in):: xyzf_QMix(0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q $ .     豈疲ｹｿ. Specific humidity

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    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 vertical direction
    integer:: n               ! 邨��譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in dimension of constituents

    real(DP) :: minval, maxval
    integer  :: mini, minj, mink
    integer  :: maxi, maxj, maxk

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. check_prog_vars_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    i = 0
    j = 1
    k = 1
    minval = xyz_U(i,j,k)
    maxval = xyz_U(i,j,k)
    mini   = i
    minj   = j
    mink   = k
    maxi   = i
    maxj   = j
    maxk   = k
    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1
          if ( xyz_U   (i,j,k) < minval ) then
            minval = xyz_U   (i,j,k)
            mini   = i
            minj   = j
            mink   = k
          end if
          if ( maxval < xyz_U   (i,j,k) ) then
            maxval = xyz_U   (i,j,k)
            maxi   = i
            maxj   = j
            maxk   = k
          end if
        end do
      end do
    end do
    if ( minval  < VelMin  ) then
      call MessageNotify( 'M', module_name, 'Min value : U(%f,%f,%f) = %f.', &
        & d = (/ x_Lon(mini)*180.0_DP/PI, y_Lat(minj)*180.0_DP/PI, z_Sigma(mink), minval /) )
    end if
    if ( VelMax  < maxval  ) then
      call MessageNotify( 'M', module_name, 'Max value : U(%f,%f,%f) = %f.', &
        & d = (/ x_Lon(maxi)*180.0_DP/PI, y_Lat(maxj)*180.0_DP/PI, z_Sigma(maxk), maxval /) )
    end if

    i = 0
    j = 1
    k = 1
    minval = xyz_V(i,j,k)
    maxval = xyz_V(i,j,k)
    mini   = i
    minj   = j
    mink   = k
    maxi   = i
    maxj   = j
    maxk   = k
    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1
          if ( xyz_V   (i,j,k) < minval ) then
            minval = xyz_V   (i,j,k)
            mini   = i
            minj   = j
            mink   = k
          end if
          if ( maxval < xyz_V   (i,j,k) ) then
            maxval = xyz_V   (i,j,k)
            maxi   = i
            maxj   = j
            maxk   = k
          end if
        end do
      end do
    end do
    if ( minval  < VelMin  ) then
      call MessageNotify( 'M', module_name, 'Min value : V(%f,%f,%f) = %f.', &
        & d = (/ x_Lon(mini)*180.0_DP/PI, y_Lat(minj)*180.0_DP/PI, z_Sigma(mink), minval /) )
    end if
    if ( VelMax  < maxval  ) then
      call MessageNotify( 'M', module_name, 'Max value : V(%f,%f,%f) = %f.', &
        & d = (/ x_Lon(maxi)*180.0_DP/PI, y_Lat(maxj)*180.0_DP/PI, z_Sigma(maxk), maxval /) )
    end if

    i = 0
    j = 1
    k = 1
    minval = xyz_Temp(i,j,k)
    maxval = xyz_Temp(i,j,k)
    mini   = i
    minj   = j
    mink   = k
    maxi   = i
    maxj   = j
    maxk   = k
    do k = 1, kmax
      do j = 1, jmax
        do i = 0, imax-1
          if ( xyz_Temp(i,j,k) < minval ) then
            minval = xyz_Temp(i,j,k)
            mini   = i
            minj   = j
            mink   = k
          end if
          if ( maxval < xyz_Temp(i,j,k) ) then
            maxval = xyz_Temp(i,j,k)
            maxi   = i
            maxj   = j
            maxk   = k
          end if
        end do
      end do
    end do
    if ( minval  < TempMin ) then
      call MessageNotify( 'M', module_name, 'Min value : T(%f,%f,%f) = %f.', &
        & d = (/ x_Lon(mini)*180.0_DP/PI, y_Lat(minj)*180.0_DP/PI, z_Sigma(mink), minval /) )
    end if
    if ( TempMax < maxval  ) then
      call MessageNotify( 'M', module_name, 'Max value : T(%f,%f,%f) = %f.', &
        & d = (/ x_Lon(maxi)*180.0_DP/PI, y_Lat(maxj)*180.0_DP/PI, z_Sigma(maxk), maxval /) )
    end if

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine CheckProgVars

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

  subroutine CheckProgVarsInit
    !
    ! check_prog_vars 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#check_prog_vars_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "check_prog_vars" module is initialized.
    ! "NAMELIST#check_prog_vars_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 /check_prog_vars_nml/ &
      & VelMin, VelMax, TempMin, TempMax
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "check_prog_vars#CheckProgVarsInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "check_prog_vars#CheckProgVarsInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( check_prog_vars_inited ) return
!!$    call InitCheck

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    VelMin  = -300.0_DP
    VelMax  =  300.0_DP
    TempMin =   50.0_DP
    TempMax =  330.0_DP

    ! 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 = check_prog_vars_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, '  VelMin  = %f', d = (/ VelMin  /) )
    call MessageNotify( 'M', module_name, '  VelMax  = %f', d = (/ VelMax  /) )
    call MessageNotify( 'M', module_name, '  TempMin = %f', d = (/ TempMin /) )
    call MessageNotify( 'M', module_name, '  TempMax = %f', d = (/ TempMax /) )

    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    check_prog_vars_inited = .true.

  end subroutine CheckProgVarsInit

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

end module check_prog_vars