!= ヒストリデータ出力
!
!= History data output
!
! 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 history_file_io
  !
  != ヒストリデータ出力
  !
  != History data output
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !

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

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &  ! 倍精度実数型. Double precision. 
    &                 TOKEN  ! キーワード.   Keywords. 

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

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: HistoryFileOpen, HistoryFileClose, HistoryFileOutPut
  public:: HistoryFileRegister

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: history_file_io_inited = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag

  ! 非公開変数
  ! Private variables
  !

!!$  namelist /history_file_io_nml/ 

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

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

  interface HistoryFileClose
    module procedure HistoryFileClose
  end interface

  interface HistoryFileOutput
    module procedure HistoryFileOutput2
    module procedure HistoryFileOutput3
  end interface

  interface HistoryFileRegister
    module procedure HistoryFileRegister
  end interface

contains

  subroutine HistoryFileOpen
    !
    ! history_file_io モジュールの初期化を行います. 
    ! NAMELIST#history_file_io_nml の値が読み込まれます. 
    !
    ! "history_file_io" module is initialized. 
    ! Values of NAMELIST#history_file_io_nml are loaded. 
    !

    ! モジュール引用 ; 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

    ! 宣言文 ; Declaration statements
    !
    implicit none

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

    ! 実行文 ; Executable statement
    !

    if ( history_file_io_inited ) 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 = history_file_io_nml, &  ! (out)
!!$      & iostat = iostat_nml ) ! (out)
!!$    close( unit_nml )
!!$
!!$    call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    if ( iostat_nml == 0 ) write( STDOUT, nml = history_file_io_nml )


    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    history_file_io_inited = .true.
  end subroutine HistoryFileOpen

  subroutine HistoryFileClose
    !
    ! ヒストリデータファイル出力の終了処理を行います. 
    !
    ! Terminate history data files output. 

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

  end subroutine HistoryFileClose

  subroutine HistoryFileOutput2( name, data & ! (in)
    & )
    !
    ! ヒストリデータの出力を行います. 
    !
    ! Output history data

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

    ! 宣言文 ; Declaration statements
    !
    implicit none
    character(*), intent(in):: name
                              ! 変数名. Variable name
    real(DP), intent(in):: data(:,:)
                              ! データ. Data

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

  end subroutine HistoryFileOutput2

  subroutine HistoryFileOutput3( name, data & ! (in)
    & )
    !
    ! ヒストリデータの出力を行います. 
    !
    ! Output history data

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

    ! 宣言文 ; Declaration statements
    !
    implicit none
    character(*), intent(in):: name
                              ! 変数名. Variable name
    real(DP), intent(in):: data(:,:,:)
                              ! データ. Data

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

  end subroutine HistoryFileOutput3

  subroutine HistoryFileRegister( &
    & name, longname, units, dims & ! (in)
    & )
    !
    ! ヒストリデータの登録を行います. 
    !
    ! Regist history data

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

    ! 宣言文 ; Declaration statements
    !
    implicit none
    character(*), intent(in):: name
                              ! 変数名. Variable name
    character(*), intent(in):: longname
                              ! 変数の記述的名称. 
                              ! 
                              ! Descriptive name of a variable
    character(*), intent(in):: units
                              ! 変数の単位. 
                              ! 
                              ! Units of a variable
    character(*), intent(in):: dims(:)
                              ! 変数が依存する次元の名前. 
                              ! 時間の次元は配列の最後に指定すること. 
                              !
                              ! Names of dependency dimensions of a variable. 
                              ! Dimension of time must be specified 
                              ! to last of an array. 

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

  end subroutine HistoryFileRegister

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

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

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

    ! 実行文 ; Executable statement
    !

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

  end subroutine InitCheck

end module history_file_io
