!= Module HistoryFileIO
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: basiczfileio.f90,v 1.6 2011-06-03 06:11:21 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 BasicFileIO
  !
  !ファイル出力. 長い時間ステップの値を出力.
  !

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use gtool_history, only: HistoryCreate, HistoryClose, &
    & HistoryPut, HistoryAddVariable

  ! ファイル入出力補助
  ! 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,            &!X 座標軸(スカラー格子点)
    &                 y_Y,            &
    &                 z_Z
  use gridset,  only: nx, ny, nz, ncmax
  use fileset,  only: HistoryFilePrefix, &!ヒストリファイル名
    &                 filetitle,          &!データの表題
    &                 filesource,            &!データを作成する手順
    &                 FileInstitution             !最終変更者・組織
  
  !暗黙の型宣言禁止
  implicit none
  
  !属性の指定
  private

  !公開手続き  
  public BasicFileMake

  ! 非公開変数
  ! Private variables
  !
  character(*), parameter:: module_name = 'history_file_io'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: basiczfileio.f90,v 1.6 2011-06-03 06:11:21 sugiyama Exp $'
                              ! モジュールのバージョン
                              ! Module version

contains 

  !------------------------------------------------------
  subroutine BasicFileMake

    use basicset,  only:  xyzf_QMixBZ, xyz_DensBZ, xyz_EffMolWtBZ, &
      &                   xyz_PTempBZ, xyz_TempBZ, xyz_PressBZ, &
      &                   xyz_VelSoundBZ, xyz_ExnerBZ
    use composition, only:   SpcWetSymbol

    implicit none

    character(STRING) :: bzfile
    integer           :: l

    ! 初期化
    !
    bzfile = trim(HistoryFilePrefix)//'BasicZ.nc'

    ! HistoryCreate
    !
    call HistoryCreate(                              &
      & file = bzfile,                               &
      & 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'/),           &
      & origin=0.0,                                  &
      & xtypes=(/'double', 'double', 'double'/),     &
      & quiet=.true.,                                &
      & interval=0.0)
    
    call HistoryPut('x', x_X(1:nx))
    call HistoryPut('y', y_Y(1:ny))
    call HistoryPut('z', z_Z(1:nz))

    !-----------------------------------------------------------  
    ! 基本場の出力
    !-----------------------------------------------------------  
    !無次元圧力の基本場
    call HistoryAddVariable(  &
      & varname='ExnerBZ',&
      & dims=(/'x','y','z'/),     &
      & longname='nondimensional pressure', units='1',&
      & xtype='double' )
    
    !温位の基本場
    call HistoryAddVariable(  &
      & varname='PTempBZ',&
      & dims=(/'x','y','z'/),     &
      & longname='potential temperature', &
      & units='K',            &
      & xtype='double' )
    
    !仮温位の基本場
    call HistoryAddVariable(  &
      & varname='VPTempBZ',&
      & dims=(/'x','y','z'/),     &
      & longname='vertial potential temperature', &
      & units='K',            &
      & xtype='double' )
    
    !密度の基本場
    call HistoryAddVariable(  &
      & varname='DensBZ', &
      & dims=(/'x','y','z'/),     &
      & longname='density',   &
      & units='Kg.m-3',       &
      & xtype='double'  )
    
    !音波速度の基本場
    call HistoryAddVariable(  &
      & varname='VelSoundBZ',&
      & dims=(/'x','y','z'/),     &
      & longname='sound velocity',&
      & units='m.s-2',        &
      & xtype='double'  )
    
    !温度の基本場
    call HistoryAddVariable(  &
      & varname='TempBZ', &
      & dims=(/'x','y','z'/),     &
      & longname='Temperature of basic state', &
      & units='K',            &
      & xtype='double'  )
    
    !圧力の基本場
    call HistoryAddVariable(  &
      & varname='PressBZ',&
      & dims=(/'x','y','z'/),     &
      & longname='Pressure of basic state', &
      & units='Pa',           &
      & xtype='double'  )
    
    !混合比の基本場
    do l = 1, ncmax
      call HistoryAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'BZ',&
        & dims=(/'x','y','z'/), &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio of basic state', &
        & units='kg.kg-1',        &
        & xtype='double'  )
    end do

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


    !-------------------------------------------------------------
    ! 基本場のファイル出力
    !
    call HistoryPut(   &
      & 'DensBZ',  &
      & xyz_DensBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'ExnerBZ', &
      & xyz_ExnerBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'PTempBZ',&
      & xyz_PTempBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'VPTempBZ',&
      & xyz_PTempBZ(1:nx,1:ny,1:nz)    &
      & / xyz_EffMolWtBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'VelSoundBZ', &
      & xyz_VelSoundBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'TempBZ',  &
      & xyz_TempBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'PressBZ', &
      & xyz_PressBZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'EffMolWtBZ', &
      & xyz_EffMolWtBZ(1:nx,1:ny,1:nz) )

    do l = 1, ncmax
      call HistoryPut( &
        & trim(SpcWetSymbol(l))//'BZ',    &
        & xyzf_QMixBZ(1:nx,1:ny,1:nz, l) )
    end do

    call HistoryClose
    
  end subroutine BasicFileMake

end module BasicFileIO
