!= Module HistoryFileIO
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: historyfileio.f90,v 1.9 2014/03/04 05:55:04 sugiyama Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]


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

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

  ! ファイル入出力補助
  ! 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 mpi_wrapper, only: FLAG_LIB_MPI
  use namelist_util, only: namelist_filename

  use axesset,  only: x_X, y_Y, z_Z
  use gridset,  only: nx, ny, nz, ncmax
  use fileset,  only: filetitle,        &!データの表題
    &                 filesource,       &!データを作成する手順
    &                 FileInstitution    !最終変更者・組織
  use timeset,  only: Restarttime, EndTime
  use composition, only: SpcWetSymbol

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

  !公開手続き
  public HistoryFileIO_init
  public HistoryFileIO_finalize

  ! 非公開変数
  ! Private variables
  !
  character(STRING), parameter:: module_name = 'historyfileio'
                              ! モジュールの名称. 
                              ! Module name
  character(STRING), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: historyfileio.f90,v 1.9 2014/03/04 05:55:04 sugiyama Exp $'
                              ! モジュールのバージョン
                              ! Module version

contains 

!!!------------------------------------------------------------------------
  subroutine HistoryFileIO_init
    !
    ! 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
    
    !変数定義
    real(DP), parameter       :: TimeDisp = 1.0e5 !出力間隔のデフォルト値
    integer :: l, s

    !-----------------------------------------------------------
    ! ヒストリー作成
    !-----------------------------------------------------------
    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   = Restarttime,                         & 
      & terminus = EndTime,                             &
      & interval = TimeDisp,                            &       
      & flag_mpi_split = FLAG_LIB_MPI,                  &
      & namelist_filename = namelist_filename)  

    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) )


    !無次元圧力の擾乱
    call HistoryAutoAddVariable(  &
      & varname='Exner',      &  
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of nondimensional pressure', &
      & units=' ',            &        
      & xtype='double' )

    !無次元圧力
    call HistoryAutoAddVariable(  &
      & varname='ExnerAll',      &  
      & dims=(/'x','y','z','t'/), &
      & longname='nondimensional pressure', &
      & units=' ',            &        
      & xtype='double' )
    
    !温位の擾乱
    call HistoryAutoAddVariable(  &
      & varname='PotTemp',    &
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of potential temperature',  &
      & units='K',            &
      & xtype='double' )

    !温位の擾乱
    call HistoryAutoAddVariable(  &
      & varname='PotTempAll',    &
      & dims=(/'x','y','z','t'/), &
      & longname='potential temperature',  &
      & units='K',            &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelX',       &
      & dims=(/'x','y','z','t'/), &
      & longname='zonal velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelY',       &
      & dims=(/'x','y','z','t'/), &
      & longname='meridional velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !鉛直速度
    call HistoryAutoAddVariable(  &
      & varname='VelZ',       &
      & dims=(/'x','y','z','t'/), &
      & longname='vertical velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !渦粘性係数(運動量)
    call HistoryAutoAddVariable(  &
      & varname='Km',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient', &
      & units='m2.s-1',            &
      & xtype='double' )
  
    !渦粘性係数(熱)
    call HistoryAutoAddVariable(  &
      & varname='Kh',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient for heat', &
      & units='m2.s-1',            &
      & xtype='double')

    !渦粘性係数(熱)
    call HistoryAutoAddVariable(  &
      & varname='CDens',          &
      & dims=(/'x','y','z','t'/), &
      & longname='Cloud density', &
      & units='kg.m-3',           &
      & xtype='double')
  
    !混合比
    do l = 1, ncmax
      call HistoryAutoAddVariable(                         &
        & varname=trim(SpcWetSymbol(l)),                   &
        & dims=(/'x','y','z','t'/),                        &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio', &
        & units='kg.kg-1',                                 &
        & xtype='double')

      call HistoryAutoAddVariable(                         &
        & varname=trim(SpcWetSymbol(l))//'All',            &
        & dims=(/'x','y','z','t'/),                        &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio', &
        & units='kg.kg-1',                                 &
        & xtype='double')
    end do


!!!------------------------------------------------------------------
!!! 
!!!
    !無次元圧力の擾乱
    call HistoryAutoAddVariable(  &
      & varname='ExnerBl',      &  
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of nondimensional pressure', &
      & units=' ',            &        
      & xtype='double' )

    !温位の擾乱
    call HistoryAutoAddVariable(  &
      & varname='PotTempBl',    &
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of potential temperature',  &
      & units='K',            &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelXBl',       &
      & dims=(/'x','y','z','t'/), &
      & longname='zonal velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelYBl',       &
      & dims=(/'x','y','z','t'/), &
      & longname='meridional velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !鉛直速度
    call HistoryAutoAddVariable(  &
      & varname='VelZBl',       &
      & dims=(/'x','y','z','t'/), &
      & longname='vertical velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !渦粘性係数(運動量)
    call HistoryAutoAddVariable(  &
      & varname='KmBl',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient', &
      & units='m2.s-1',            &
      & xtype='double' )
  
    !渦粘性係数(熱)
    call HistoryAutoAddVariable(  &
      & varname='KhBl',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient for heat', &
      & units='m2.s-1',            &
      & xtype='double')

    !混合比
    do l = 1, ncmax
      call HistoryAutoAddVariable(                         &
        & varname=trim(SpcWetSymbol(l))//'Bl',                   &
        & dims=(/'x','y','z','t'/),                        &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio', &
        & units='kg.kg-1',                                 &
        & xtype='double')
    end do


!!!------------------------------------------------------------------
!!! 
!!!
    !無次元圧力の擾乱
    call HistoryAutoAddVariable(  &
      & varname='ExnerNl',      &  
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of nondimensional pressure', &
      & units=' ',            &        
      & xtype='double' )

    !温位の擾乱
    call HistoryAutoAddVariable(  &
      & varname='PotTempNl',    &
      & dims=(/'x','y','z','t'/), &
      & longname='disturbunce of potential temperature',  &
      & units='K',            &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelXNl',       &
      & dims=(/'x','y','z','t'/), &
      & longname='zonal velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !水平速度
    call HistoryAutoAddVariable(  &
      & varname='VelYNl',       &
      & dims=(/'x','y','z','t'/), &
      & longname='meridional velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !鉛直速度
    call HistoryAutoAddVariable(  &
      & varname='VelZNl',       &
      & dims=(/'x','y','z','t'/), &
      & longname='vertical velocity', &
      & units='m.s-1',      &
      & xtype='double' )

    !渦粘性係数(運動量)
    call HistoryAutoAddVariable(  &
      & varname='KmNl',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient', &
      & units='m2.s-1',            &
      & xtype='double' )
  
    !渦粘性係数(熱)
    call HistoryAutoAddVariable(  &
      & varname='KhNl',         &
      & dims=(/'x','y','z','t'/), &
      & longname='turbulet diffusion coefficient for heat', &
      & units='m2.s-1',            &
      & xtype='double')

    !混合比
    do l = 1, ncmax
      call HistoryAutoAddVariable(                         &
        & varname=trim(SpcWetSymbol(l))//'Nl',             &
        & dims=(/'x','y','z','t'/),                        &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio', &
        & units='kg.kg-1',                                 &
        & xtype='double')
    end do


!!!------------------------------------------------------------------
!!! 
!!!
    call HistoryAutoAddVariable(  &
      & varname='KmAdv', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Advection term of Km',  &
      & units='s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='KmDiff', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Numerical diffusion term of Km',  &
      & units='s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='KmShear', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Shear term of Km',  &
      & units='s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='KmBuoyT', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy term of Km (temperature)',  &
      & units='s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='KmBuoy', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy term of Km (composition)',  &
      & units='s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='KmTurb', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Diffusion term of Km',  &
      & units='s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='KmDisp', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Dissipation term of Km',  &
      & units='s-1',    &
      & xtype='double' )

!----------------------------------------------------------

    call HistoryAutoAddVariable(  &
      & varname='PotTempAdv', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Advection term of potential temperature',  &
      & units='K.s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='PotTempTurb',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Turbulence term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='PotTempDisp',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Dissipation term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='PotTempRad', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Radiation term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double' )
    
    call HistoryAutoAddVariable(  &
      & varname='PotTempDiff',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Numerical diffusion term of potential temperature',&
      & units='K.s-1',    &
      & xtype='double' )
    
    call HistoryAutoAddVariable(  &
      & varname='PotTempFlux',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Surface Flux term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='PotTempSpng',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Damping term of Sponge Layer of potential temperature',  &
      & units='K.s-1"',    &
      & xtype='double' )

!    call HistoryAutoAddVariable(  &
!      & varname='PotTempR2G',&
!      & dims=(/'x','y','z','t'/),     &
!      & longname='Condensation term of potential temperature (R2G)', &
!      & units='K.s-1',    &
!      & xtype='double' )

!    call HistoryAutoAddVariable(  &
!      & varname='PotTempG2C',&
!      & dims=(/'x','y','z','t'/),     &
!      & longname='Condensation term of potential temperature (G2C)', &
!      & units='K.s-1',    &
!      & xtype='double' )

!-----------------------------------------------------------

    call HistoryAutoAddVariable(  &
      & varname='VelXAdv', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Advection term of horizontal velocity',  &
      & units='K.s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelXTurb',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Turbulence term of horizontal velocity', &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelXDiff',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Numerical diffusion term of horizontal velocity',&
      & units='K.s-1',    &
      & xtype='double' )
    
    call HistoryAutoAddVariable(  &
      & varname='VelXSpng',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Damping term of Sponge Layer of horizontal velocity',  &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelXPGrad',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Pressure gradient term of horizontal velocity', &
      & units='K.s-1"',    &
      & xtype='double' )

!-----------------------------------------------------------

    call HistoryAutoAddVariable(  &
      & varname='VelYAdv', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Advection term of horizontal velocity',  &
      & units='K.s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelYTurb',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Turbulence term of horizontal velocity', &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelYDiff',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Numerical diffusion term of horizontal velocity',&
      & units='K.s-1',    &
      & xtype='double' )
    
    call HistoryAutoAddVariable(  &
      & varname='VelYSpng',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Damping term of Sponge Layer of horizontal velocity',  &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelYPGrad',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Pressure gradient term of horizontal velocity', &
      & units='K.s-1"',    &
      & xtype='double' )

!------------------------------------------------------

    call HistoryAutoAddVariable(  &
      & varname='VelZAdv', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Advection term of vertical velocity',  &
      & units='K.s-1',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelZTurb',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Turbulence term of vertical velocity', &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelZDiff',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Numerical diffusion term of vertical velocity',&
      & units='K.s-1',    &
      & xtype='double' )
    
    call HistoryAutoAddVariable(  &
      & varname='VelZSpng',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Damping term of Sponge Layer of vertical velocity',  &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelZBuoyT',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy term (temperature) of vertical velocity',  &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelZBuoyM',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy term (mol weight) of vertical velocity',  &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelZBuoyD',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Buoyancy term (drag) of vertical velocity',  &
      & units='K.s-1"',    &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='VelZPGrad',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Pressure gradient term of vertical velocity', &
      & units='K.s-1"',    &
      & xtype='double' )

    !----------------------------------------------------------------
    ! Mixing Ratio time change
    !----------------------------------------------------------------
    do s = 1, ncmax
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'_Adv', &
        & dims=(/'x','y','z','t'/),     &
        & longname='Advection term of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
      
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'_Turb', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Turbulence term of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
      
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'_Diff', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Diffusion term of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
      
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'_Flux', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Surface Flux term of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
    
!      call HistoryAutoAddVariable(  &
!        & varname=trim(SpcWetSymbol(s))//'_Fall', & 
!        & dims=(/'x','y','z','t'/),     &
!        & longname='Fall Rain term of '          &
!        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
!        & units='kg.kg-1.s-1',    &
!        & xtype='double' )
    
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'_Fill1', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Filling Negative term 1 of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
    
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'_Fill2', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Filling Negative term 2 of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
    
!      call HistoryAutoAddVariable(  &
!        & varname=trim(SpcWetSymbol(s))//'_R2G', & 
!        & dims=(/'x','y','z','t'/),     &
!        & longname='Condensation term of '          &
!        &           //trim(SpcWetSymbol(s))//' mixing ratio (R2G)',  &
!        & units='kg.kg-1.s-1',    &
!        & xtype='double' )
!
!      call HistoryAutoAddVariable(  &
!        & varname=trim(SpcWetSymbol(s))//'_C2R', & 
!        & dims=(/'x','y','z','t'/),     &
!        & longname='Condensation term of '          &
!        &           //trim(SpcWetSymbol(s))//' mixing ratio (C2R)',  &
!        & units='kg.kg-1.s-1',    &
!        & xtype='double' )
!
!      call HistoryAutoAddVariable(  &
!        & varname=trim(SpcWetSymbol(s))//'_G2C', & 
!        & dims=(/'x','y','z','t'/),     &
!        & longname='Condensation term of '          &
!        &           //trim(SpcWetSymbol(s))//' mixing ratio (G2C)',  &
!        & units='kg.kg-1.s-1',    &
!        & xtype='double' )
    
!      call HistoryAutoAddVariable(  &
!        & varname=trim(SpcWetSymbol(s))//'_Asln', & 
!        & dims=(/'x','y','z','t'/),     &
!        & longname='Asselin time filter term of '          &
!        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
!        & units='kg.kg-1.s-1',    &
!        & xtype='double',       &
!        & history=gt_hist(8) ) 
    
    end do

    !-----------------------------------------------------------  
    ! 基本場の出力
    !-----------------------------------------------------------  
    !無次元圧力の基本場
    call HistoryAutoAddVariable(  &
      & varname='ExnerBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='nondimensional pressure', units='1',&
      & xtype='double' )
    
    !温位の基本場
    call HistoryAutoAddVariable(  &
      & varname='PotTempBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='potential temperature', &
      & units='K',            &
      & xtype='double' )
    
    !仮温位の基本場
    call HistoryAutoAddVariable(  &
      & varname='VPotTempBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='vertial potential temperature', &
      & units='K',            &
      & xtype='double' )
    
    !密度の基本場
    call HistoryAutoAddVariable(  &
      & varname='DensBasicZ', &
      & dims=(/'x','y','z'/),     &
      & longname='density',   &
      & units='Kg.m-3',       &
      & xtype='double' )
    
    !音波速度の基本場
    call HistoryAutoAddVariable(  &
      & varname='VelSoundBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='sound velocity',&
      & units='m.s-2',        &
      & xtype='double' )
    
    !温度の基本場
    call HistoryAutoAddVariable(  &
      & varname='TempBasicZ', &
      & dims=(/'x','y','z'/),     &
      & longname='Temperature of basic state', &
      & units='K',            &
      & xtype='double' )
    
    !圧力の基本場
    call HistoryAutoAddVariable(  &
      & varname='PressBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='Pressure of basic state', &
      & units='Pa',           &
      & xtype='double' )
    
    !混合比の基本場
    do s = 1, ncmax
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(s))//'BasicZ',&
        & dims=(/'x','y','z'/), &
        & longname=trim(SpcWetSymbol(s))//' Mixing Ratio of basic state', &
        & units='kg.kg-1',        &
        & xtype='double' )
    end do

    !分子量効果
    call HistoryAutoAddVariable(  &
      & varname='EffMolWtBasicZ', &
      & dims=(/'x','y','z'/),     &
      & longname='Effect of Mole Weight', &
      & units='1',            &
      & xtype='double' )

    !-------------------------------------------------------------
    ! 基本場のファイル出力
    !-------------------------------------------------------------
!    call HistoryAutoPut(   &
!      & TimeN, 'DensBasicZ',  &
!      & xz_DensBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

!    call HistoryAutoPut(   &
!      & TimeN, 'ExnerBasicZ', &
!      & xz_ExnerBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

!    call HistoryAutoPut(   &
!      & TimeN, 'PotTempBasicZ',&
!      & xz_PotTempBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

!    call HistoryAutoPut(   &
!      & TimeN, 'VPotTempBasicZ',&
!      & xz_PotTempBasicZ(FileXMin:FileXMax, FileZMin:FileZMax)    &
!      & / xz_EffMolWtBasicZ(FileXMin:FileXMax, FileZMin:FileZMax)&
!      & )

!    call HistoryAutoPut(   &
!      & TimeN, 'VelSoundBasicZ', &
!      & xz_VelSoundBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

!    call HistoryAutoPut(   &
!      & TimeN, 'TempBasicZ',  &
!      & xz_TempBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

!    call HistoryAutoPut(   &
!      & TimeN, 'PressBasicZ', &
!      & xz_PressBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

!    do s = 1, ncmax
!      call HistoryAutoPut( &
!        & TimeN, trim(SpcWetSymbol(s))//'BasicZ',    &
!        & xza_MixRtBasicZ(FileXMin:FileXMax, FileZMin:FileZMax, s) &
!        & )
!    end do

!    call HistoryAutoPut(   &
!      & TimeN, 'EffMolWtBasicZ', &
!      & xz_EffMolWtBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) &
!      & )

    
  end subroutine HistoryFileIO_init


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

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    call HistoryAutoClose

  end subroutine HistoryFileIO_finalize
  
end module HistoryFileIO_3d
