!= 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
!
!= Settings of array for atmospheric composition
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: composition.f90,v 1.6 2012/09/08 15:25:53 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module composition
  !
  != 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
  !
  != Settings of array for atmospheric composition
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
  !
  ! Settings of array for atmospheric composition
  !
  !== Variables List
  !
  ! ncmax        :: 蠕ｮ驥乗�蛻��謨ｰ
  ! ------------ :: ------------
  ! ncmax        :: Number of minor comstituents
  !
  !== Procedures List
  !
  ! CompositionInit :: 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
  ! --------------  :: ------------
  ! CompositionInit :: Settings of parameters for array for composition
  !
  !
  !== NAMELIST
  !
  ! NAMELIST#composition_nml
  !

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

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

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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public :: CompositionInqIndex
  public :: CompositionInqFlagMassFix
  public :: CompositionInqFlagAdv
  public :: CompositionInqFlagVDiff
  public :: CompositionInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !
  logical          , save, public             :: composition_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  integer          , save, public             :: ncmax
                              ! 謌仙�縺ｮ謨ｰ
                              ! Number of composition
  character(STRING), save, public, allocatable:: a_QMixName(:)
                              ! 謌仙�縺ｮ螟画焚蜷�
                              ! Name of variables for composition
  character(STRING), save, public, allocatable:: a_QMixLongName(:)
                              ! 謌仙�縺ｮ髟ｷ縺�､画焚蜷�
                              ! Long name of variables for composition
  logical          , save,         allocatable:: a_FlagMassFix(:)
                              ! 雉ｪ驥丈ｿ晏ｭ倩｣懈ｭ｣縺ｮ繝輔Λ繧ｰ
                              ! Flag for mass fix
  logical          , save,         allocatable:: a_FlagAdv(:)
                              ! 謌仙�縺ｮ遘ｻ豬∬ｨ育ｮ励�繝輔Λ繧ｰ
                              ! Flag for advection calculation
  logical          , save,         allocatable:: a_FlagVDiff(:)
                              ! 驩帷峩豺ｷ蜷医�遘ｻ豬∬ｨ育ｮ励�繝輔Λ繧ｰ
                              ! Flag for vertical diffusion

  character(STRING), parameter                :: NameH2OVap = "QH2OVap"
  character(STRING), parameter                :: NameH2OLiq = "QH2OLiq"
  character(STRING), parameter                :: NameH2OSol = "QH2OSol"
  character(STRING), parameter                :: NameTKE    = "TKE"

  integer          , save, public             :: IndexH2OVap
                              ! 豌ｴ闥ｸ豌励�繧､繝ｳ繝�ャ繧ｯ繧ｹ
                              ! Index for water vapor
  integer          , save, public             :: IndexH2OLiq
  integer          , save, public             :: IndexH2OSol
  integer          , save, public             :: IndexTKE

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  character(*), parameter:: module_name = 'composition'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: composition.f90,v 1.6 2012/09/08 15:25:53 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  function CompositionInqIndex( Name ) result( Index )
    !
    ! Name 縺ｧ荳弱∴繧峨ｌ縺溷錐蜑阪�謌仙�縺ｮ繧､繝ｳ繝�け繧ｹ繧定ｿ斐＠縺励∪縺�.
    !
    ! Inquiry of index of constituent
    !

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

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

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

    character(len=*), intent(in ) :: Name

    integer:: Index

    integer:: n               ! 邨��譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in dimension of constituents

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. composition_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    Index = -1

    do n = 1, ncmax
      if ( a_QMixName(n) == trim( Name ) ) then
        Index = n
        exit
      end if
    end do

!!$    if ( Index == -1 ) then
!!$      call MessageNotify( 'E', module_name, &
!!$        & 'Cannot find %c in a list of constituents.', c1 = trim( Name ) )
!!$    end if

  end function CompositionInqIndex

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

  function CompositionInqFlagMassFix( Index ) result( FlagMassFix )
    !
    ! Name 縺ｧ荳弱∴繧峨ｌ縺溷錐蜑阪�謌仙�縺ｮ遘ｻ豬√ヵ繝ｩ繧ｰ繧定ｿ斐＠縺励∪縺�.
    !
    ! Inquiry of flag for advection of constituent
    !

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

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

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

    integer, intent(in ) :: Index

    logical :: FlagMassFix

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. composition_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    if ( ( Index < 1 ) .or. ( Index > ncmax ) ) then
      call MessageNotify( 'E', module_name, &
        & 'Index is inappropriate, Index = %d', i = (/ Index /) )
    end if

    FlagMassFix = a_FlagMassFix( Index )

  end function CompositionInqFlagMassFix

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

  function CompositionInqFlagAdv( Index ) result( FlagAdv )
    !
    ! Name 縺ｧ荳弱∴繧峨ｌ縺溷錐蜑阪�謌仙�縺ｮ遘ｻ豬√ヵ繝ｩ繧ｰ繧定ｿ斐＠縺ｾ縺�.
    !
    ! Inquiry of flag for advection of constituent
    !

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

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

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

    integer, intent(in ) :: Index

    logical :: FlagAdv

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. composition_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    if ( ( Index < 1 ) .or. ( Index > ncmax ) ) then
      call MessageNotify( 'E', module_name, &
        & 'Index is inappropriate, Index = %d', i = (/ Index /) )
    end if

    FlagAdv = a_FlagAdv( Index )

  end function CompositionInqFlagAdv

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

  function CompositionInqFlagVDiff( Index ) result( FlagVDiff )
    !
    ! Name 縺ｧ荳弱∴繧峨ｌ縺溷錐蜑阪�謌仙�縺ｮ遘ｻ豬√ヵ繝ｩ繧ｰ繧定ｿ斐＠縺励∪縺�.
    !
    ! Inquiry of flag for advection of constituent
    !

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

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

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

    integer, intent(in ) :: Index

    logical :: FlagVDiff

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. composition_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    if ( ( Index < 1 ) .or. ( Index > ncmax ) ) then
      call MessageNotify( 'E', module_name, &
        & 'Index is inappropriate, Index = %d', i = (/ Index /) )
    end if

    FlagVDiff = a_FlagVDiff( Index )

  end function CompositionInqFlagVDiff

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

  subroutine CompositionInit
    !
    ! composition 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#composition_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "composition" module is initialized.
    ! NAMELIST#composition_nml is loaded in this procedure.
    !

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

    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, MaxNmlArySize

    ! 繝輔ぃ繧､繝ｫ蜈･蜃ｺ蜉幄｣懷勧
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 讓呎ｺ門�蜉帙�陬�ｽｮ逡ｪ蜿ｷ. Unit number of standard output

    ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
    ! 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

    character(len=STRING) :: Names      (1:MaxNmlArySize)
    character(len=STRING) :: LongNames  (1:MaxNmlArySize)
    logical               :: FlagMassFix(1:MaxNmlArySize)
    logical               :: FlagAdv    (1:MaxNmlArySize)
    logical               :: FlagVDiff  (1:MaxNmlArySize)

    integer:: n               ! 邨��譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in dimension of constituents

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
    namelist /composition_nml/ &
      & ncmax, Names, LongNames, FlagMassFix, FlagAdv, FlagVDiff
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "composition#CompositionInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "composition#CompositionInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( composition_inited ) return
!!$    call InitCheck

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    ncmax        = 1

    Names    (1) = NameH2OVap
    LongNames(1) = 'specific humidity'
    do n = 2, MaxNmlArySize
      Names    (n) = ''
      LongNames(n) = ''
    end do

    FlagMassFix = .true.

    FlagAdv     = .true.

    FlagVDiff   = .true.

    ! 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 = composition_nml, & ! (out)
        & iostat = iostat_nml    & ! (out)
        & )
      close( unit_nml )

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
      if ( iostat_nml == 0 ) write( STDOUT, nml = composition_nml )
    end if

    ! 驟榊�繧ｵ繧､繧ｺ縺ｮ繝√ぉ繝�け
    ! Check number of array size
    !
    if ( ncmax < 1 ) then
      call MessageNotify( 'E', module_name, &
        & 'number of composition has to be greater than 0. ' // &
        & 'ncmax=%d' , &
        & i = (/ ncmax /) )
    end if

    ! Set variable name of constituents for ouptut
    !
    allocate( a_QMixName    ( ncmax ) )
    allocate( a_QMixLongName( ncmax ) )
    if ( ncmax > 99 ) then
      call MessageNotify( 'E', module_name, &
        & 'number of composition greater than 99 is inappropriate for current version. ' // &
        & 'ncmax=%d' , &
        & i = (/ ncmax /) )
    end if
    do n = 1, ncmax
      if ( Names(n) == '' ) then
        write( a_QMixName(n), '(a,i3.3)' ) "QMix", n
      else
        a_QMixName(n) = Names(n)
      end if
      if ( LongNames(n) == '' ) then
        a_QMixLongName(n) = a_QMixName(n)
      else
        a_QMixLongName(n) = LongNames(n)
      end if
    end do

    allocate( a_FlagMassFix( ncmax ) )
    do n = 1, ncmax
      a_FlagMassFix( n ) = FlagMassFix(n)
    end do

    allocate( a_FlagAdv( ncmax ) )
    do n = 1, ncmax
      a_FlagAdv( n ) = FlagAdv(n)
    end do

    allocate( a_FlagVDiff( ncmax ) )
    do n = 1, ncmax
      a_FlagVDiff( n ) = FlagVDiff(n)
    end do

    IndexH2OVap = -1
    do n = 1, ncmax
      if ( a_QMixName(n) == NameH2OVap ) then
        IndexH2OVap = n
        exit
      end if
    end do
    !
    ! 豌ｴ闥ｸ豌励�繧､繝ｳ繝�ャ繧ｯ繧ｹ縺ｮ繝√ぉ繝�け
    ! Check index for water vapor
    !
    if ( ( IndexH2OVap < 1 ) .or. ( IndexH2OVap > ncmax ) ) then
      call MessageNotify( 'E', module_name, &
        & 'IndexH2OVap has to be greater than or equal to 1 and less than or equal to ncmax. ' // &
        & 'IndexH2OVap=%d' , &
        & i = (/ IndexH2OVap /) )
    end if

    IndexH2OLiq = -1
    do n = 1, ncmax
      if ( a_QMixName(n) == NameH2OLiq ) then
        IndexH2OLiq = n
        exit
      end if
    end do
    IndexH2OSol = -1
    do n = 1, ncmax
      if ( a_QMixName(n) == NameH2OSol ) then
        IndexH2OSol = n
        exit
      end if
    end do
    IndexTKE = -1
    do n = 1, ncmax
      if ( a_QMixName(n) == NameTKE ) then
        IndexTKE = n
        exit
      end if
    end do

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '    ncmax              = %d', i = (/ ncmax /) )
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    QMixName(%d)     = %c', i = (/ n /), c1 = trim(a_QMixName(n)) )
    end do
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    QMixLongName(%d) = %c', i = (/ n /), c1 = trim(a_QMixLongName(n)) )
    end do
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    FlagMassFix(%d)  = %b', i = (/ n /), l = (/ a_FlagMassFix(n) /) )
    end do
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    FlagAdv(%d)      = %b', i = (/ n /), l = (/ a_FlagAdv(n) /) )
    end do
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    FlagVDiff(%d)    = %b', i = (/ n /), l = (/ a_FlagVDiff(n) /) )
    end do
    call MessageNotify( 'M', module_name, '    IndexH2OVap        = %d', i = (/ IndexH2OVap /) )
    call MessageNotify( 'M', module_name, '    IndexH2OLiq        = %d', i = (/ IndexH2OLiq /) )
    call MessageNotify( 'M', module_name, '    IndexH2OSol        = %d', i = (/ IndexH2OSol /) )
    call MessageNotify( 'M', module_name, '    IndexTKE           = %d', i = (/ IndexTKE    /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    composition_inited = .true.
  end subroutine CompositionInit

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

!!$  subroutine InitCheck
!!$    !
!!$    ! 萓晏ｭ倥Δ繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶メ繧ｧ繝�け
!!$    !
!!$    ! Check initialization of dependency modules
!!$
!!$    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
!!$    !
!!$
!!$    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
!!$    ! Utilities for NAMELIST file input
!!$    !
!!$    use namelist_util, only: namelist_util_inited
!!$
!!$    ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
!!$    ! Message output
!!$    !
!!$    use dc_message, only: MessageNotify
!!$
!!$    ! 螳溯｡梧枚 ; Executable statement
!!$    !
!!$
!!$    if ( .not. namelist_util_inited ) &
!!$      & call MessageNotify( 'E', module_name, '"namelist_util" module is not initialized.' )
!!$
!!$  end subroutine InitCheck

end module composition