!= Module HistoryFileIO
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: basiczfileio.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 BasicZFileIO
  !
  !ファイル出力. 長い時間ステップの値を出力.
  !

  ! 種別型パラメタ
  ! 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, nf
  use fileset,  only: DefaultFilePrefix, &!ヒストリファイル名
    &                 filetitle,          &!データの表題
    &                 filesource,            &!データを作成する手順
    &                 FileInstitution             !最終変更者・組織
  
  !暗黙の型宣言禁止
  implicit none
  
  !属性の指定
  private

  !公開手続き  
  public BasicZFileOutput

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

contains 

  !------------------------------------------------------
  subroutine BasicZFileOutput

    use basicset,  only:  xyza_MixRtBasicZ, xyz_DensBasicZ, xyz_EffMolWtBasicZ, &
      &                   xyz_PotTempBasicZ, xyz_TempBasicZ, xyz_PressBasicZ, &
      &                   xyz_VelSoundBasicZ, xyz_ExnerBasicZ
    use moistset, only:   SpcWetSymbol

    implicit none

    character(STRING) :: bzfile
    integer           :: l

    ! 初期化
    !
    bzfile = trim(DefaultFilePrefix)//'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='ExnerBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='nondimensional pressure', units='1',&
      & xtype='double' )
    
    !温位の基本場
    call HistoryAddVariable(  &
      & varname='PotTempBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='potential temperature', &
      & units='K',            &
      & xtype='double' )
    
    !仮温位の基本場
    call HistoryAddVariable(  &
      & varname='VPotTempBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='vertial potential temperature', &
      & units='K',            &
      & xtype='double' )
    
    !密度の基本場
    call HistoryAddVariable(  &
      & varname='DensBasicZ', &
      & dims=(/'x','y','z'/),     &
      & longname='density',   &
      & units='Kg.m-3',       &
      & xtype='double'  )
    
    !音波速度の基本場
    call HistoryAddVariable(  &
      & varname='VelSoundBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='sound velocity',&
      & units='m.s-2',        &
      & xtype='double'  )
    
    !温度の基本場
    call HistoryAddVariable(  &
      & varname='TempBasicZ', &
      & dims=(/'x','y','z'/),     &
      & longname='Temperature of basic state', &
      & units='K',            &
      & xtype='double'  )
    
    !圧力の基本場
    call HistoryAddVariable(  &
      & varname='PressBasicZ',&
      & dims=(/'x','y','z'/),     &
      & longname='Pressure of basic state', &
      & units='Pa',           &
      & xtype='double'  )
    
    !混合比の基本場
    do l = 1, nf
      call HistoryAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'BasicZ',&
        & dims=(/'x','y','z'/), &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio of basic state', &
        & units='kg.kg-1',        &
        & xtype='double'  )
    end do

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


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

    call HistoryPut(   &
      & 'ExnerBasicZ', &
      & xyz_ExnerBasicZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'PotTempBasicZ',&
      & xyz_PotTempBasicZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'VPotTempBasicZ',&
      & xyz_PotTempBasicZ(1:nx,1:ny,1:nz)    &
      & / xyz_EffMolWtBasicZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'VelSoundBasicZ', &
      & xyz_VelSoundBasicZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'TempBasicZ',  &
      & xyz_TempBasicZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'PressBasicZ', &
      & xyz_PressBasicZ(1:nx,1:ny,1:nz) )

    call HistoryPut(   &
      & 'EffMolWtBasicZ', &
      & xyz_EffMolWtBasicZ(1:nx,1:ny,1:nz) )

    do l = 1, nf
      call HistoryPut( &
        & trim(SpcWetSymbol(l))//'BasicZ',    &
        & xyza_MixRtBasicZ(1:nx,1:ny,1:nz, l) )
    end do

    call HistoryClose
    
  end subroutine BasicZFileOutput

end module BasicZFileIO
