!= 蜃ｺ蜉帙ヵ繧｡繧､繝ｫ縺ｮ蝓ｺ譛ｬ諠��ｱ邂｡逅�
!= Management basic information for output files
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: fileset.f90,v 1.3 2009/03/03 15:57:35 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module fileset
  !
  != 蜃ｺ蜉帙ヵ繧｡繧､繝ｫ縺ｮ蝓ｺ譛ｬ諠��ｱ邂｡逅�
  !
  != Management basic information for output files
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Variables List
  !
  ! FileTitle        :: 蜃ｺ蜉帙ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ縺ｮ陦ｨ鬘�
  ! FileSource       :: 繝��繧ｿ繝輔ぃ繧､繝ｫ菴懈�縺ｮ謇区ｮｵ
  ! FileInstitution  :: 繝��繧ｿ繝輔ぃ繧､繝ｫ繧呈怙邨ら噪縺ｫ螟画峩縺励◆邨�ｹ�/蛟倶ｺｺ
  ! ------------     :: ------------
  ! FileTitle        :: Title of output data files
  ! FileSource       :: Source of data file
  ! FileInstitution  :: Institution or person that changes data files for the last time
  !
  !
  !== Procedures List
  !
  ! FilesetInit   :: fileset 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹�
  ! ------------  :: ------------
  ! FilesetInit   :: Initialize "fileset" module
  !
  !
  !== NAMELIST
  !
  ! NAMELIST#fileset_nml
  !

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

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: STRING ! 譁�ｭ怜�.       Strings.

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

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

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

  character(STRING), save, public:: FileTitle
                              ! 蜃ｺ蜉帙ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ縺ｮ陦ｨ鬘�.
                              ! Title of output data files
  character(STRING), save, public:: FileSource
                              ! 繝��繧ｿ繝輔ぃ繧､繝ｫ菴懈�縺ｮ謇区ｮｵ.
                              ! Source of data file
  character(STRING), save, public:: FileInstitution
                              ! 繝��繧ｿ繝輔ぃ繧､繝ｫ繧呈怙邨ら噪縺ｫ螟画峩縺励◆邨�ｹ�/蛟倶ｺｺ.
                              ! Institution or person that changes data files for the last time

  ! 髱槫�髢句､画焚
  ! Private variables
  !

  character(*), parameter:: module_name = 'fileset'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: fileset.f90,v 1.3 2009/03/03 15:57:35 morikawa Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

  ! INTERFACE 譁� ; INTERFACE statements
  !
  interface FilesetInit
    module procedure FilesetInit
  end interface

contains

  subroutine FilesetInit
    !
    ! fileset 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#fileset_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "fileset" module is initialized.
    ! NAMELIST#fileset_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

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
    namelist /fileset_nml/ &
      & FileTitle, FileSource, FileInstitution
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "fileset#FilesetInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "fileset#FilesetInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( fileset_inited ) return
    call InitCheck

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    FileTitle  = 'dcpam5 test run'
    FileSource = 'dcpam5 $Name:  $ (http://www.gfd-dennou.org/library/dcpam)'
    FileInstitution = 'GFD Dennou Club (http://www.gfd-dennou.org)'

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

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

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  FileTitle       = %c', c1 = trim(FileTitle) )
    call MessageNotify( 'M', module_name, '  FileSource      = %c', c1 = trim(FileSource) )
    call MessageNotify( 'M', module_name, '  FileInstitution = %c', c1 = trim(FileInstitution) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    fileset_inited = .true.
  end subroutine FilesetInit

  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 fileset