!= NAMELIST ファイル入力に関するユーティリティ
!
!= Utilities for NAMELIST file input
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: gridset.f90,v 1.5 2007/06/09 13:12:30 sugiyama Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!

module namelist_util
  !
  != NAMELIST ファイル入力に関するユーティリティ
  !
  != Utilities for NAMELIST file input
  !

  ! モジュール引用 ; USE statements
  !

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: STRING  ! 文字列. Strings. 

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: NmlutilInit, NmlutilMsg

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: initialized = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag
  character(STRING), save, public:: namelist_filename = ''
                              ! NAMELIST ファイルの名称. 
                              ! NAMELIST file name

  ! 非公開変数
  ! Private variables
  !
  character(*), parameter:: module_name = 'namelist_util'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: $'
                              ! モジュールのバージョン
                              ! Module version

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

  interface NmlutilMsg
    module procedure NmlutilMsg
  end interface

contains

  subroutine NmlutilInit
    !
    ! namelist_util モジュールの初期設定を行います. 
    !
    ! Initialize "namelist_util" module. 
    !

    ! コマンドライン引数処理
    ! Command line option parser
    !
    use option_parser, only: opt_nml_file => namelist_filename

  continue
    if ( initialized ) return
    call InitDepmod
    namelist_filename = opt_nml_file
    initialized = .true.
  end subroutine NmlutilInit

  subroutine NmlutilMsg( iostat, module_name )
    !
    ! NAMELIST ファイル入力ステータスから
    ! 適切なメッセージを表示します. 
    !
    ! Appropriate messages are output from 
    ! status of NAMELIST file loading. 
    !

    ! メッセージ出力
    ! Message output
    !
    use dc_message, only: MessageNotify
    implicit none
    integer, intent(in):: iostat
                              ! NAMELIST 読み込み時のステータス. 
                              ! Status of NAMELIST loading
    character(*), intent(in):: module_name
                              ! モジュールの名称. 
                              ! Module name
  continue
    if ( iostat == 0 ) then
      call MessageNotify( 'M', '', &
        & 'NAMELIST group "%c" is loaded from "%c".', &
        & c1 = trim(module_name) // '_nml', &
        & c2 = trim(namelist_filename) )
    else
      call MessageNotify( 'W', '', &
        & 'NAMELIST group "%c" is not found in "%c" (iostat=%d).', &
        & c1 = trim(module_name) // '_nml', &
        & c2 = trim(namelist_filename), &
        & i = (/iostat/) )
    end if
  end subroutine NmlutilMsg

  subroutine InitDepmod
    !
    ! 依存モジュールの初期化
    !
    ! Initialize dependency modules

    ! コマンドライン引数処理
    ! Command line option parser
    !
    use option_parser, only: OptParseInit
  continue

    call OptParseInit

  end subroutine InitDepmod

end module namelist_util
