!= 組成に関わる配列の設定
!
!= Settings of array for atmospheric composition
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: composition.f90,v 1.2 2009-09-01 15:37:30 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:: CompositionInit

  ! 公開変数
  ! Public variables
  !
  logical          , save, public             :: composition_inited = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag
  integer          , save, public             :: ncmax
                              ! 成分の数
                              ! Number of composition
  character(STRING), save, public, allocatable:: QMixName(:)
                              ! 成分の変数名
                              ! Name of variables for composition
  character(STRING), save, public, allocatable:: QMixLongName(:)
                              ! 成分の長い変数名
                              ! Long name of variables for composition
  integer          , save, public             :: IndexH2OVap
                              ! 水蒸気のインデックス
                              ! Index for water vapor

  ! 非公開変数
  ! Private variables
  !
  character(*), parameter:: module_name = 'composition'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: composition.f90,v 1.2 2009-09-01 15:37:30 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version

  ! INTERFACE 文 ; INTERFACE statements
  !
  interface CompositionInit
    module procedure CompositionInit
  end interface

contains

  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

    ! ファイル入出力補助
    ! 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

    integer:: n               ! 組成方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in dimension of constituents

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
    namelist /composition_nml/ ncmax, IndexH2OVap
          !
          ! デフォルト値については初期化手続 "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
    IndexH2OVap = 1

    ! 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

    ! 水蒸気のインデックスのチェック
    ! 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


    ! Set variable name of constituents for ouptut
    !
    allocate( QMixName    ( ncmax ) )
    allocate( 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, IndexH2OVap-1
      write( QMixName(n), '(a,i3.3)' ) "QMix", n
      QMixLongName(n) = QMixName(n)
    end do
    n = IndexH2OVap
    QMixName    (n) = "QVap"
    QMixLongName(n) = "specific humidity"
    do n = IndexH2OVap+1, ncmax
      write( QMixName(n), '(a,i3.3)' ) "QMix", n
      QMixLongName(n) = QMixName(n)
    end do

    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '    ncmax              = %d', i = (/ ncmax /) )
    call MessageNotify( 'M', module_name, '    IndexH2OVap        = %d', i = (/ IndexH2OVap /) )
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    QMixName(%d)     = %c', i = (/ n /), c1 = trim(QMixName(n)) )
    end do
    do n = 1, ncmax
      call MessageNotify( 'M', module_name, '    QMixLongName(%d) = %c', i = (/ n /), c1 = trim(QMixLongName(n)) )
    end do
    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
