!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Program Basic
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: basic.f90,v 1.3 2005/01/28 12:54:37 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!非静力学モデル deepconv/arare 用の標準的な基本場作成プログラム
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

program basic
                                                                 !=begin
  use gt4_history
  use gridset, only: gridset_init
  use expname, only: expname_init
  use basicprm, only: basicprm_init
                                                                 !=end
  implicit none

  real(8), allocatable  :: ss_CpBasicZ(:,:)
  real(8), allocatable  :: ss_DensBasicZ(:,:)
  real(8), allocatable  :: ss_ExnerBasicZ(:,:)
  real(8), allocatable  :: ss_PotTempBasicZ(:,:)
  real(8), allocatable  :: ss_VelSoundBasicZ(:,:)

  character(80) :: cfgfile    = "arare-basic.conf"  !設定ファイル


!!!
!!!初期化
!!!
  !--- 実験設定を読み込む
  call basicprm_init(cfgfile)
  
  !--- 格子点情報の初期化
  call gridset_init(cfgfile)
  
  !--- 実験名の初期化
  call expname_init(cfgfile)

  !--- 配列の確保
  call BasicAlloc

  !--- 出力ファイルの定義
  call BasicOpenBasicFile
  
  !--- 基本場の計算
  call BasicType(ss_CpBasicZ, ss_DensBasicZ, &
       & ss_ExnerBasicZ, ss_PotTempBasicZ, ss_VelSoundBasicZ )

  !--- ファイル出力
  call BasicOutputBasicFile

  !--- ファイルのクローズ
  call BasicCloseBasicFile


contains

!!!
!!!配列の確保
!!!  
  subroutine BasicAlloc
    use gridset, only: DimXMin, DimXMax, DimZMin, DimZMax
    allocate( &
         & ss_CpBasicZ(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax) ) 
  end subroutine BasicAlloc


!!!
!!!出力ファイルの定義. 
!!!
  subroutine BasicOpenBasicFile
    use gridset,   only: s_X, s_Z
    use expname,   only: exptitle, expsrc, expinst
    use basicprm, only: basicfile
    
    integer::   N

    N = size(s_X, 1)

    ! ヒストリー作成
    call HistoryCreate(                                  &
         & file = basicfile,                             &
         & title = exptitle,                             &
         & source = expsrc,                              &
         & institution = expinst,                        &
         & dims=(/'x','z'/),                             &
         & dimsizes=(/N, N/),                            &
         & longnames=(/'X-coordinate',                   &
         &             'Z-coordinate'/),                 &
         & units=(/'m','m'/), origin=0.0,                &
         & interval=0.0 )
    
    ! 変数出力
    call HistoryPut( 'x', s_X )
    call HistoryPut( 'z', s_Z )
    
    ! 無次元圧力
    call HistoryAddVariable(                             &
         & varname='ExnerBasicZ', dims=(/'x','z'/),      &
         & longname='nondimensional pressure', units='1',&
         & xtype='double' )

    ! 温位
    call HistoryAddVariable(                            &
         & varname='PotTempBasicZ', dims=(/'x','z'/),    &
         & longname='potential temperature',            &
         & units='K', xtype='double' )

    ! 定圧比熱
    call HistoryAddVariable(                            &
         & varname='CpBasicZ', dims=(/'x','z'/),        &
         & longname='Specific Heat',                    &
         & units='J/K kg', xtype='double' )

    ! 密度
    call HistoryAddVariable(                            &
         & varname='DensBasicZ', dims=(/'x','z'/),      &
         & longname='density',                          &
         & units='Kg/m^3', xtype='double' )

    ! 音波速度
    call HistoryAddVariable(                            &
         & varname='VelSoundBasicZ', dims=(/'x','z'/),  &
         & longname='sound velocity',                   &
         & units='m/s^2', xtype='double' ) 

  end subroutine BasicOpenBasicFile
  

!!!
!!!ファイル出力. 長い時間ステップの値を出力. 全領域
!!!
  subroutine BasicOutputBasicFile
    call HistoryPut( 'CpBasicZ',       ss_CpBasicZ       )
    call HistoryPut( 'DensBasicZ',     ss_DensBasicZ     )
    call HistoryPut( 'ExnerBasicZ',    ss_ExnerBasicZ    )
    call HistoryPut( 'PotTempBasicZ',    ss_PotTempBasicZ    )
    call HistoryPut( 'VelSoundBasicZ', ss_VelSoundBasicZ )
  end subroutine BasicOutputBasicFile


!!!
!!!出力ファイルのクローズ. 全領域
!!!  
  subroutine BasicCloseBasicFile
    call HistoryClose
  end subroutine BasicCloseBasicFile
  

end program basic
