!= Module HistoryFileIO
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: historyfileio.f90,v 1.1 2011-04-26 04:08:10 sugiyama Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!ファイル出力. 長い時間ステップの値を出力.
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module HistoryFileIO
  !
  !ファイル出力. 長い時間ステップの値を出力.
  !

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use gtool_historyauto, only: HistoryAutoCreate, HistoryAutoClose, &
    & HistoryAutoAddAttr, HistoryAutoAddWeight, HistoryAutoPutAxis

  ! ファイル入出力補助
  ! File I/O support
  !
  use dc_iounit, only: FileOpen
  
  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 倍精度実数型. Double precision. 
    &                 STRING, &  ! 文字列.       Strings. 
    &                 TOKEN,  &  ! キーワード.   Keywords. 
    &                 STDOUT     ! 標準出力の装置番号. Unit number of standard output
  use axesset,  only: x_X, y_Y, z_Z
  use gridset, only:  nx, ny, nz
  use fileset,  only: DefaultFilePrefix, &!ヒストリファイル名
    &                 filetitle,        &!データの表題
    &                 filesource,       &!データを作成する手順
    &                 FileInstitution    !最終変更者・組織
  use timeset, only: StartTime, EndTime

  !暗黙の型宣言禁止
  implicit none
  
  !属性の指定
  private

  !公開手続き
  public HistoryFileAutoOpen
  public HistoryFileAutoClose

  ! 非公開変数
  ! Private variables
  !
  character(*), parameter:: module_name = 'history_file_io'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: historyfileio.f90,v 1.1 2011-04-26 04:08:10 sugiyama Exp $'
                              ! モジュールのバージョン
                              ! Module version

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

  interface HistoryFileAutoClose
    module procedure HistoryFileAutoClose
  end interface

contains 

!!!------------------------------------------------------------------------
  subroutine HistoryFileAutoOpen( cfgfile ) 
    !
    ! history_file_io モジュールの初期化を行います. 
    !--
    ! NAMELIST#history_file_io_nml の読み込みはこの手続きで行われます. 
    !++
    !
    ! "history_file_io" module is initialized. 
    !--
    ! "NAMELIST#history_file_io_nml" is loaded in this procedure. 
    !++
    !

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

    ! gtool5 netCDF データの入出力インターフェース (大規模モデル用)
    ! Interface of Input/Output of gtool5 netCDF data (For large models)
    !

    ! 宣言文 ; Declaration statements
    !
    implicit none
    
    !変数定義
    character(STRING), intent(in) :: cfgfile
    real(DP), parameter       :: TimeDisp = 1.0e5 !出力間隔のデフォルト値

    !-----------------------------------------------------------
    ! ヒストリー作成
    !-----------------------------------------------------------
    call HistoryAutoCreate(                             &
      & title = FileTitle,                              &
      & source = FileSource,                            &
      & institution = FileInstitution,                  &
      & dims=(/'x','y','z','t'/),                       &
      & dimsizes=(/nx, ny, nz, 0/),                     &
      & longnames=(/'x-coordinate',                     &
      &             'y-coordinate',                     &
      &             'z-coordinate',                     &
      &             'time        '/),                   &
      & units=(/'m  ','m  ','m  ','sec'/),              &
      & xtypes=(/'double','double','double','double'/), &
      & origin   = StartTime,                           & 
      & terminus = EndTime,                             &
      & interval = TimeDisp,                            &       
!      & flag_mpi_split = .true.,                        &
!      & file_prefix = DefaultFilePrefix,                & 
      & namelist_filename = cfgfile)  

    call HistoryAutoAddAttr( &
      & varname = 'x', attrname = 'standard_name', &   ! (in)
      & value = 'x-coordinate' )                       ! (in)
    call HistoryAutoAddAttr( &
      & varname = 'y', attrname = 'standard_name', &   ! (in)
      & value = 'y-coordinate' )                       ! (in)
    call HistoryAutoAddAttr( &
      & varname = 'z', attrname = 'standard_name', &   ! (in)
      & value = 'z-coordinate' )                       ! (in)
    
    call HistoryAutoPutAxis('x', x_X(1:nx))
    call HistoryAutoPutAxis('y', y_Y(1:ny))
    call HistoryAutoPutAxis('z', z_Z(1:nz))

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


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

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    call HistoryAutoClose

  end subroutine HistoryFileAutoClose

end module HistoryFileIO
