!c Description: 
!c   非静力学モデル deepconv/arare
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2004, All rights reserved

program arare
  use gt4_history
  use gridset, only: gridset_init, DimXMin, DimXMax, DimZMin, DimZMax, &
       & RegZMin, RegZMax
  use fileset, only: fileset_init
  use timeset, only: timeset_init, NstepTShort, NstepTLong, &
       & DelTShort, DelTLong
  use physprm, only: physprm_init
  use expname, only: expname_init
  use basicprm, only: basicprm_init
  use linlib,  only: linlib_init
  use if_display

  implicit none

  real(8), allocatable  :: ss_Exner_AS(:,:)
  real(8), allocatable  :: fs_VelX_AS(:,:)
  real(8), allocatable  :: sf_VelZ_AS(:,:)
  real(8), allocatable  :: ss_Exner_BS(:,:)
  real(8), allocatable  :: fs_VelX_BS(:,:)
  real(8), allocatable  :: sf_VelZ_BS(:,:)

  real(8), allocatable  :: ss_VelDiv_BS(:,:)
  real(8), allocatable  :: fs_ForceX_B(:,:)
  real(8), allocatable  :: sf_ForceZ_B(:,:)
  
  real(8), allocatable  :: ss_Vtheta_C(:,:)
  real(8), allocatable  :: ss_Exner_C(:,:)
  real(8), allocatable  :: fs_VelX_C(:,:)
  real(8), allocatable  :: sf_VelZ_C(:,:)
  real(8), allocatable  :: ss_Vtheta_B(:,:)
  real(8), allocatable  :: ss_Exner_B(:,:)
  real(8), allocatable  :: fs_VelX_B(:,:)
  real(8), allocatable  :: sf_VelZ_B(:,:)
  real(8), allocatable  :: ss_Vtheta_A(:,:)
  real(8), allocatable  :: ss_Exner_A(:,:)
  real(8), allocatable  :: fs_VelX_A(:,:)
  real(8), allocatable  :: sf_VelZ_A(:,:)

  real(8)               :: Time
  integer               :: t, tau

  type(gt_history)      :: ss_reg, fs_reg, sf_reg
  type(gt_history)      :: ss_dim, fs_dim, sf_dim
  

!!!
!!!初期化
!!!
  !--- I/O ファイル名の初期化
  call fileset_init
  
  !--- 格子点情報の初期化
  call gridset_init
  
  !--- 時間積分の設定の初期化
  call timeset_init
  
  !--- 物理量の初期化 
  call physprm_init

  !--- 実験名の初期化
  call expname_init

  !--- 基本場の値を設定
  call basicprm_init

  !--- 行列計算ライブラリの初期化. 
  !--- 引数はスカラーの z 方向の計算領域の大きさ
  call linlib_init(RegZMax - RegZMin)
  
  !--- 出力ファイルの定義
  call ArareOpenRegFile
  call ArareOpenDimFile

!!!
!!! CFL 条件のチェック
!!!
  call cflchk


!!!
!!!配列の確保, 初期化
!!!  
  call ArareAlloc
  call ArareArrayInit

  !--- 初期擾乱の設定
  call ArareDisturb


!!!
!!!数値積分
!!!
  !--- 初期状態をファイル出力
  Time = 0.0d0
  call ArareOutputRegFile
  call ArareOutputDimFile
  
  !--- 長い時間ステップ
  !--- 最初の 1 ステップだけは前進差分
  Time = Time + DelTLong
  
  !長い時間ステップでの計算
  call ArareMotionTLong( fs_VelX_B, sf_VelZ_B, ss_Vtheta_B, &
       & fs_ForceX_B, sf_ForceZ_B) 
  
  !短いタイムステップの初期値
  fs_VelX_BS  = fs_VelX_B
  sf_VelZ_BS  = sf_VelZ_B
  ss_Exner_BS = ss_Exner_B 
  
  !--- 短いタイムステップでの積分
  !--- NStepShort は偶数である必要あり
  do tau = 1, NstepTShort / 2.0d0
     
     call ArareMotionTShort( &
          & fs_VelX_BS, sf_VelZ_BS, ss_Exner_BS,  &
          & fs_ForceX_B, sf_ForceZ_B,                &
          & fs_VelX_AS, sf_VelZ_AS, ss_Exner_AS )

     !--- ループを回すための処置
     call ArareLoopTShort
  end do

  !最終的な速度を長い時間ステップでのものとみなす
  call ArareVariableTLong

  !仮温位の数値積分. Eular 法を利用
  call VthetaEular(fs_VelX_B, sf_VelZ_B, ss_Vtheta_B, ss_Vtheta_A)

  !--- ファイル出力
  call ArareOutputRegFile
  call ArareOutputDimFile

  !--- ループを回すための処置
  call ArareLoopTLong
     
  

  !--- 長い時間ステップ
  !--- 2 ステップ目以降は leap-frog スキーム
  !--- Cp は平均場? 圧力方程式を要確認
  !--- 音波減衰項はここでも必要? 
  do t = 1, NStepTLong 
     Time = Time + DelTLong
     write(*,*) Time

     !長い時間ステップでの計算     
     call ArareMotionTLong( fs_VelX_B, sf_VelZ_B, ss_Vtheta_B, &
       & fs_ForceX_B, sf_ForceZ_B) 
  
     !短いタイムステップの初期値
     fs_VelX_BS  = fs_VelX_C
     sf_VelZ_BS  = sf_VelZ_C
     ss_Exner_BS = ss_Exner_C 
     
     !--- 短いタイムステップでの積分
     do tau = 1, NstepTShort
        
        call ArareMotionTShort( &
             & fs_VelX_BS, sf_VelZ_BS, ss_Exner_BS, &
             & fs_ForceX_B, sf_ForceZ_B,               &
             & fs_VelX_AS, sf_VelZ_AS, ss_Exner_AS )
        
        !--- ループを回すための処置
        call ArareLoopTShort
     end do

     !最終的な速度を長い時間ステップでのものとみなす
     call ArareVariableTLong

     !仮温位の数値積分. Leap-Frog 法を利用
     call VthetaLeapFrog(fs_VelX_B, sf_VelZ_B, &
          & ss_Vtheta_C, ss_Vtheta_B, &
          & ss_Vtheta_A)

     !--- ファイル出力
     call ArareOutputRegFile
     call ArareOutputDimFile
     
     !--- ループを回すための処置
     call ArareLoopTLong
  end do

  !--- 出力ファイルのクローズ
  call ArareCloseRegFile
  call ArareCloseDimFile


contains
  

!!!
!!!短いタイムステップで計算した値を長いタイムステップでの値に代入
!!!
  subroutine ArareVariableTLong
    ss_Exner_A  = ss_Exner_AS 
    fs_VelX_A   = fs_VelX_AS
    sf_VelZ_A   = sf_VelZ_AS     
  end subroutine ArareVariableTLong


!!!
!!!長い時間ステップの計算でのループを回すための操作
!!!
  subroutine ArareLoopTLong
    ss_Vtheta_C = ss_Vtheta_B
    ss_Exner_C  = ss_Exner_B 
    fs_VelX_C   = fs_VelX_B
    sf_VelZ_C   = sf_VelZ_B
    ss_Vtheta_B = ss_Vtheta_A
    ss_Exner_B  = ss_Exner_A 
    fs_VelX_B   = fs_VelX_A
    sf_VelZ_B   = sf_VelZ_A
  end subroutine ArareLoopTLong


!!!
!!!短い時間ステップの計算でのループを回すための操作
!!!
  subroutine ArareLoopTShort
    ss_Exner_BS  = ss_Exner_AS 
    fs_VelX_BS = fs_VelX_AS
    sf_VelZ_BS = sf_VelZ_AS
  end subroutine ArareLoopTShort
  
  
!!!
!!!短いタイムステップでの u, w, pi の計算
!!!
  subroutine ArareMotionTShort( fs_VelX_BS, sf_VelZ_BS, ss_Exner_BS, &
       & fs_ForceX_B, sf_ForceZ_B,                              &
       & fs_VelX_AS, sf_VelZ_AS, ss_Exner_AS )

    implicit none
    real(8), intent(in)  :: fs_VelX_BS(:,:)
    real(8), intent(in)  :: sf_VelZ_BS(:,:)
    real(8), intent(in)  :: ss_Exner_BS(:,:)
    real(8), intent(in)  :: fs_ForceX_B(:,:)
    real(8), intent(in)  :: sf_ForceZ_B(:,:)
    real(8), intent(out) :: fs_VelX_AS(:,:)
    real(8), intent(out) :: sf_VelZ_AS(:,:)
    real(8), intent(out) :: ss_Exner_AS(:,:)

    !--- 速度の収束
    call VelDiv(fs_VelX_BS, sf_VelZ_BS, ss_VelDiv_BS)
     
    !--- 速度 u の計算
    call VelXHeVi(ss_Exner_BS, ss_VelDiv_BS, fs_ForceX_B, fs_VelX_BS, &
         & fs_VelX_AS)
    
    !--- エクスナー関数の計算
    call ExnerHeVi(   &
         & ss_VelDiv_BS, sf_ForceZ_B, fs_VelX_AS, sf_VelZ_BS, ss_Exner_BS, &
         & ss_Exner_AS)
    
    !--- 速度 w の計算
    call VelZHeVi(    &
         & ss_Exner_BS, ss_Exner_AS, ss_VelDiv_BS, sf_ForceZ_B, sf_VelZ_BS, &
         & sf_VelZ_AS)

  end subroutine ArareMotionTShort


!!!
!!!長いタイムステップでの u, w, pi の計算
!!!
  subroutine ArareMotionTLong( fs_VelX_B, sf_VelZ_B, ss_Vtheta_B, &
       & fs_ForceX_B, sf_ForceZ_B)
    use gridset, only: DimXMin, DimXMax, DimZMin, DimZMax

    implicit none
    real(8), intent(in)  :: fs_VelX_B(:,:)
    real(8), intent(in)  :: sf_VelZ_B(:,:)
    real(8), intent(in)  :: ss_Vtheta_B(:,:)
    real(8), intent(out) :: fs_ForceX_B(:,:)
    real(8), intent(out) :: sf_ForceZ_B(:,:)

    real(8)  ::  fs_AdvX_B(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)  ::  sf_AdvZ_B(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)  ::  sf_Buoy_B(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)  ::  fs_DiffuseX_B(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)  ::  sf_DiffuseZ_B(DimXMin:DimXMax, DimZMin:DimZMax)


    !--- 移流項
    call AdvectX(fs_VelX_B, sf_VelZ_B, fs_AdvX_B)
    call AdvectZ(fs_VelX_B, sf_VelZ_B, sf_AdvZ_B)

    !--- 浮力項
    call  Buoyancy(ss_VTheta_B, sf_Buoy_B)

    !--- 音波に関連しない項 (Z)
    call DiffuseX(fs_VelX_B, sf_VelZ_B, fs_DiffuseX_B)
    call DiffuseZ(fs_VelX_B, sf_VelZ_B, sf_DiffuseZ_B)

    !--- 項をまとめる
    fs_ForceX_B = - fs_AdvX_B + fs_DiffuseX_B
    sf_ForceZ_B = - sf_AdvZ_B + sf_DiffuseZ_B + sf_Buoy_B

  end subroutine ArareMotionTLong


!!!
!!!配列の割り当て
!!!
  subroutine ArareAlloc
    allocate(ss_Exner_AS(DimXMin:DimXMax, DimZMin:DimZMax),  &
         & fs_VelX_AS(DimXMin:DimXMax, DimZMin:DimZMax)  , &
         & sf_VelZ_AS(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_BS(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_BS(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_BS(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_A(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_A(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_A(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Vtheta_A(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_B(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_B(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_B(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Vtheta_B(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_C(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_C(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_C(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Vtheta_C(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_ForceX_B(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_ForceZ_B(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_VelDiv_BS(DimXMin:DimXMax, DimZMin:DimZMax) )
  end subroutine ArareAlloc


!!!
!!!配列の初期化. 全て零に. 
!!!  
  subroutine ArareArrayInit
    use basicprm, only: ss_ExnerBasicZ

    fs_VelX_A = 0.0d0
    fs_VelX_B = 0.0d0
    fs_VelX_C = 0.0d0

    sf_VelZ_A = 0.0d0
    sf_VelZ_B = 0.0d0
    sf_VelZ_C = 0.0d0

    ss_Exner_A = 0.0d0
    ss_Exner_B = 0.0d0
    ss_Exner_C = 0.0d0
    
    ss_VTheta_A = 0.0d0
    ss_VTheta_B = 0.0d0
    ss_VTheta_C = 0.0d0

    fs_VelX_AS = 0.0d0
    fs_VelX_BS = 0.0d0

    sf_VelZ_AS = 0.0d0
    sf_VelZ_BS = 0.0d0

    ss_Exner_AS = 0.0d0
    ss_Exner_BS = 0.0d0

    fs_ForceX_B = 0.0d0
    sf_ForceZ_B = 0.0d0

    ss_VelDiv_BS = 0.0d0

    Time = 0.0d0
  end subroutine ArareArrayInit


!!!
!!!初期値を与える. ここでは *_B, *_A な変数に値を入れる
!!!
  subroutine ArareDisturb
    use gridset, only: s_X, s_Z, Xmin, Xmax, Zmin, Zmax, &
         & DimXMin, DimXMax, DimZMin, DimZMax
    use physprm, only: pi

    integer :: i, k
    real(8) :: mu
    real(8) :: sigma

    !--- エクスナー関数の擾乱
!    mu = ( Xmax - Xmin ) * 4.0d0  / 5.0d0
!    sigma = ( Xmax - Xmin ) / 20.0d0
!    
!    do i = DimXMin, DimXMax
!       ss_Exner_B(i,:) = ss_Exner_B(i,:) &
!            & + 10.0d0 * dexp(-((s_X(i) - mu) / sigma)**2.0d0 / 2.0d0) &
!            & / (dsqrt(2.0d0 * pi) * sigma)
!    end do

!    mu = ( Zmax - Zmin ) * 4.0d0 / 5.0d0
!    sigma = ( Zmax - Zmin ) / 20.0d0
!    
!    do k = DimZMin, DimZMax
!       ss_Exner_B(:,k) = ss_Exner_B(:,k) &
!            & + 10.0d0 * dexp(-((s_Z(k) - mu) / sigma)**2.0d0 / 2.0d0) &
!            & / (dsqrt(2.0d0 * pi) * sigma)
!    end do

    ss_Exner_A = ss_Exner_B


    !--- 速度
!    fs_VelX_B = 350.0d0 * 1.0d-1
!    fs_VelX_A = fs_VelX_B
!    sf_VelZ_B = 350.0d0 * 1.0d-1
!    sf_VelZ_A = sf_VelZ_B


    !--- 仮温位の擾乱
!    mu = ( Xmax - Xmin ) / 2.0d0
!    sigma = ( Xmax - Xmin ) / 20.0d0
!    sigma = ( Xmax - Xmin ) / 10.0d0
   
!    do i = DimXMin, DimXMax
!       ss_Vtheta_B(i,:) = &
!            & 1.0d3 * dexp(-((s_X(i) - mu) / sigma)**2.0d0 / 2.0d0) &
!            & / (dsqrt(2.0d0 * pi) * sigma)
!    end do

!    mu = ( Zmax - Zmin ) / 2.0d0
!    sigma = ( Zmax - Zmin ) / 20.0d0
!    sigma = ( Zmax - Zmin ) / 10.0d0
    
!    do k = DimZMin, DimZMax
!       ss_Vtheta_B(:,k) = &
!            & 1.0d3 * dexp(-((s_Z(k) - mu) / sigma)**2.0d0 / 2.0d0) &
!            &   / (dsqrt(2.0d0 * pi) * sigma)
!       ss_Vtheta_B(:,k) =  &
!            & ss_Vtheta_B(:,k) &
!            &   * 1.0d3 * dexp(-((s_Z(k) - mu) / sigma)**2.0d0 / 2.0d0) &
!            &   / (dsqrt(2.0d0 * pi) * sigma)
!    end do

    ss_Vtheta_A = ss_Vtheta_B

  end subroutine ArareDisturb


!!!
!!!出力ファイルの定義. 計算領域のみ
!!! 
  subroutine ArareOpenRegFile
    use gridset, only: RegXMin, RegXMax, RegZMin, RegZMax, &
         & NX, NZ, f_X, f_Z, s_X, s_Z
    use expname, only: exptitle, expsrc, expinst
    use fileset, only: fs_RegFile, sf_RegFile, ss_RegFile
    
    ! ヒストリー作成
    call HistoryCreate(                                    &
         & file = fs_RegFile,                               &
         & title = exptitle,                               &
         & source = expsrc,                                &
         & institution = expinst,                          &
         & dims=(/'x','z','t'/),                           &
         & dimsizes=(/NX+1, NZ, 0/),                       &
         & longnames=(/'X-coordinate',                     &
         &             'Z-coordinate',                     &
         &             'Time        '/),                   &
         & units=(/'m','m','s'/), origin=0.0,              &
         & interval=0.0, &
         & history = fs_reg)
    
    ! 変数出力
    call HistoryPut('x', f_X(RegXMin: RegXMax), fs_reg)
    call HistoryPut('z', s_Z(RegXMin+1: RegXMax), fs_reg)
    
    ! ヒストリー作成
    call HistoryCreate(                                    &
         & file = sf_RegFile,                               &
         & title = exptitle,                               &
         & source = expsrc,                                &
         & institution = expinst,                          &
         & dims=(/'x','z','t'/),                           &
         & dimsizes=(/NX, NZ+1, 0/),                       &
         & longnames=(/'X-coordinate',                     & 
         &             'Z-coordinate',                     &
         &             'Time        '/),                   &
         & units=(/'m','m','s'/),                          &
         & origin=0.0,                                     &
         & interval=0.0,                                   &
         & history = sf_reg)
    
    ! 変数出力
    call HistoryPut('x', s_X(RegXMin+1: RegXMax), sf_reg)
    call HistoryPut('z', f_Z(RegXMin: RegXMax), sf_reg)

    ! ヒストリー作成
    call HistoryCreate(                                  &
         & file = ss_RegFile,                            &         
         & title = exptitle,                             &
         & source = expsrc,                              &
         & institution = expinst,                        &
         & dims=(/'x','z','t'/),                         &
         & dimsizes=(/NX, NZ, 0/),                       &
         & longnames=(/'X-coordinate',                   &
         &             'Z-coordinate',                   &
         &             'Time        '/),                 &
         & units=(/'m','m','s'/), origin=0.0,            &
         & interval=0.0,                                 &
         & history = ss_reg)
    
    ! 変数出力
    call HistoryPut('x', s_X(RegXMin+1: RegXMax), ss_reg)
    call HistoryPut('z', s_Z(RegXMin+1: RegXMax), ss_reg)
    
    ! 無次元圧力
    call HistoryAddVariable(                             &
         & varname='Exner', dims=(/'x','z','t'/),        &
         & longname='nondimensional pressure', units='1',&
         & xtype='double',                               & 
         & history = ss_reg)    
    ! 仮温位
    call HistoryAddVariable(                            &
         & varname='Vtheta', dims=(/'x','z','t'/),      &
         & longname='virtual potential temperature',    &
         & units='K', xtype='double',                 & 
         & history = ss_reg)    
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelX', dims=(/'x','z','t'/),             &
         & longname='zonal velocity',         &
         & units='m/s', xtype='double',                & 
         & history = fs_reg)        
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelZ', dims=(/'x','z','t'/),             &
         & longname='vertical velocity',         &
         & units='m/s', xtype='double',                & 
         & history = sf_reg)    
    
  end subroutine ArareOpenRegFile


!!!
!!!ファイル出力. 長い時間ステップで出力. 計算領域のみ
!!!
  subroutine ArareOutputRegFile
    use gridset, only: RegXMin, RegXMax, RegZMin, RegZMax

    call HistoryPut('t', Time, ss_reg)
    call HistoryPut('t', Time, fs_reg)
    call HistoryPut('t', Time, sf_reg)

    call HistoryPut('Exner', &
         & ss_Exner_A(RegXMin+1:RegXMax, RegZMin+1:RegZMax), ss_reg)
    call HistoryPut('Vtheta', &
         & ss_Vtheta_A(RegXMin+1:RegXMax, RegZMin+1:RegZMax), ss_reg)
    call HistoryPut('VelX', &
         & fs_VelX_A(RegXMin:RegXMax, RegZMin+1:RegZMax), fs_reg)
    call HistoryPut('VelZ', &
         & sf_VelZ_A(RegXMin+1:RegXMax, RegZMin:RegZMax), sf_reg)
  end subroutine ArareOutputRegFile


!!!
!!!出力ファイルのクローズ. 計算領域のみ
!!!  
  subroutine ArareCloseRegFile
    call HistoryClose( ss_reg )
    call HistoryClose( fs_reg )
    call HistoryClose( sf_reg )
  end subroutine ArareCloseRegFile


!!!
!!!出力ファイルの定義. 全領域
!!!
  subroutine ArareOpenDimFile
    use gridset, only: f_X, f_Z, s_X, s_Z
    use expname, only: exptitle, expsrc, expinst
    use fileset, only: fs_DimFile, sf_DimFile, ss_DimFile
    
    integer::   N
    
    N = size(f_X, 1)

    ! ヒストリー作成
    call HistoryCreate(                                    &
         & file = fs_DimFile,                              &
         & title = exptitle,                               &
         & source = expsrc,                                &
         & institution = expinst,                          &
         & dims=(/'x','z','t'/),                           &
         & dimsizes=(/N, N, 0/),                           &
         & longnames=(/'X-coordinate',                     &
         &             'Z-coordinate',                     &
         &             'Time        '/),                   &
         & units=(/'m','m','s'/), origin=0.0,              &
         & interval=0.0, &
         & history = fs_dim)
    
    ! 変数出力
    call HistoryPut('x', f_X, fs_dim)
    call HistoryPut('z', s_Z, fs_dim)
    
    ! ヒストリー作成
    call HistoryCreate(                                    &
         & file = sf_DimFile,                              &
         & title = exptitle,                               &
         & source = expsrc,                                &
         & institution = expinst,                          &
         & dims=(/'x','z','t'/),                           &
         & dimsizes=(/N, N, 0/),                           &
         & longnames=(/'X-coordinate',                     & 
         &             'Z-coordinate',                     &
         &             'Time        '/),                   &
         & units=(/'m','m','s'/),                          &
         & origin=0.0,                                     &
         & interval=0.0,                                   &
         & history = sf_dim)
    
    ! 変数出力
    call HistoryPut('x', s_X, sf_dim)
    call HistoryPut('z', f_Z, sf_dim)

    ! ヒストリー作成
    call HistoryCreate(                                  &
         & file = ss_DimFile,                            &
         & title = exptitle,                             &
         & source = expsrc,                              &
         & institution = expinst,                        &
         & dims=(/'x','z','t'/),                         &
         & dimsizes=(/N, N, 0/),                         &
         & longnames=(/'X-coordinate',                   &
         &             'Z-coordinate',                   &
         &             'Time        '/),                 &
         & units=(/'m','m','s'/), origin=0.0,            &
         & interval=0.0,                                 &
         & history = ss_dim)
    
    ! 変数出力
    call HistoryPut('x', s_X, ss_dim)
    call HistoryPut('z', s_Z, ss_dim)
    
    ! 無次元圧力
    call HistoryAddVariable(                             &
         & varname='Exner', dims=(/'x','z','t'/),        &
         & longname='nondimensional pressure', units='1',&
         & xtype='double',                               & 
         & history = ss_dim)    
    ! 仮温位
    call HistoryAddVariable(                            &
         & varname='Vtheta', dims=(/'x','z','t'/),      &
         & longname='virtual potential temperature',    &
         & units='K', xtype='double',                   & 
         & history = ss_dim)    
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelX', dims=(/'x','z','t'/),        &
         & longname='zonal velocity',                   &
         & units='m/s', xtype='double',                 & 
         & history = fs_dim)        
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelZ', dims=(/'x','z','t'/),        &
         & longname='vertical velocity',                &
         & units='m/s', xtype='double',                 & 
         & history = sf_dim)    
  end subroutine ArareOpenDimFile


!!!
!!!ファイル出力. 長い時間ステップの値を出力. 全領域
!!!
  subroutine ArareOutputDimFile
    call HistoryPut('t', Time, ss_dim)
    call HistoryPut('t', Time, fs_dim)
    call HistoryPut('t', Time, sf_dim)

    call HistoryPut('Exner', ss_Exner_A, ss_dim)
    call HistoryPut('Vtheta', ss_Vtheta_A, ss_dim)
    call HistoryPut('VelX', fs_VelX_A, fs_dim)
    call HistoryPut('VelZ', sf_VelZ_A, sf_dim)
  end subroutine ArareOutputDimFile


!!!
!!!出力ファイルのクローズ. 全領域
!!!  
  subroutine ArareCloseDimFile
    call HistoryClose( ss_dim )
    call HistoryClose( fs_dim )
    call HistoryClose( sf_dim )
  end subroutine ArareCloseDimFile


end program arare
