!= 格子点数・最大波数設定
!
!= Number of grid points and maximum truncated wavenumber settings
!
! 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 gridset
  !
  != 格子点数・最大波数設定
  !
  != Number of grid points and maximum truncated wavenumber settings
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 格子点数の設定および保管を行います. 
  ! スペクトル法を用いることを前提にしており, 
  ! 最大波数の設定と保管も行います. 
  !
  ! Number of grid points is set and stored. 
  ! Maximum truncated wavenumber is set and stored too, 
  ! because spectral method is expected to be used. 
  !
  !
  !== Procedures List
  !
  ! GridsetInit   :: 格子点数と最大波数の設定
  ! ------------  :: ------------
  ! GridsetInit   :: Settings of number of grid points and maximum truncated wavenumber

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

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP     ! 倍精度実数型. Double precision. 

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: GridsetInit

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: initialized = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag
  integer, save, public:: nmax = 10
                              ! 最大全波数. 
                              ! Maximum truncated wavenumber
  integer, save, public:: imax = 32
                              ! 経度格子点数. 
                              ! Number of grid points in longitude
  integer, save, public:: jmax = 16
                              ! 緯度格子点数. 
                              ! Number of grid points in latitude
  integer, save, public:: kmax = 5
                              ! 鉛直層数. 
                              ! Number of vertical level

  ! 非公開変数
  ! Private variables
  !
  namelist /gridset_nml/ nmax, imax, jmax, kmax

  character(*), parameter:: module_name = 'gridset'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id:  $'
                              ! モジュールのバージョン
                              ! Module version

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

contains

  subroutine GridsetInit

    ! 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

    implicit none

    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号. 
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT. 
                              ! IOSTAT of NAMELIST read

    character(*), parameter:: subname = 'GridsetInit'

    ! 実行文 ; Executable statement
    !

    if ( initialized ) return
    call InitCheck

    ! NAMELIST の読み込み
    ! NAMELIST is input
    !
    call FileOpen( unit_nml, &          ! (out)
      & namelist_filename, mode = 'r' ) ! (in)

    rewind( unit_nml )
    read( unit_nml, &         ! (in)
      & nml = gridset_nml, &  ! (out)
      & iostat = iostat_nml ) ! (out)
    close( unit_nml )

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

    ! 格子点数のチェック
    ! Check number of grid points
    !
    if ( nmax < 1 .or. imax < 1 .or. jmax < 1 .or. kmax < 1 ) then
      call MessageNotify( 'E', subname, &
        & 'number of grid points and maximum truncated wavenumber must be more than 0. ' // &
        & 'nmax=%d, imax=%d, jmax=%d, kmax=%d' , &
        & i = (/ nmax, imax, jmax, kmax /) )
    end if

    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  nmax = %d', i = (/ nmax /) )
    call MessageNotify( 'M', module_name, '  imax = %d', i = (/ imax /) )
    call MessageNotify( 'M', module_name, '  jmax = %d', i = (/ jmax /) )
    call MessageNotify( 'M', module_name, '  kmax = %d', i = (/ kmax /) )

    initialized = .true.
  end subroutine GridsetInit

  subroutine InitCheck
    !
    ! 依存モジュールの初期化チェック
    !
    ! Check initialization of dependency modules

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

    ! NAMELIST ファイル入力に関するユーティリティ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_util_init => initialized

    ! メッセージ出力
    ! Message output
    !
    use dc_message, only: MessageNotify

    ! 実行文 ; Executable statement
    !

    if ( .not. namelist_util_init ) &
      & call MessageNotify( 'E', module_name, '"namelist_util" module is not initialized.' )

  end subroutine InitCheck

end module gridset
