!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Program Distrb
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: distrb.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 distrb
                                                                 !=begin
  !== Dependency
  use gt4_history
  use gridset,   only: gridset_init
  use expname,   only: expname_init
  use distrbprm, only: distrbprm_init
                                                                 !=end
  implicit none

  real(8), allocatable  :: ss_PotTempDistrb(:,:)
  real(8), allocatable  :: ss_ExnerDistrb(:,:)
  real(8), allocatable  :: fs_VelXDistrb(:,:)
  real(8), allocatable  :: sf_VelZDistrb(:,:)
  
  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-distrb.conf"      !設定ファイル
  

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

  !--- 配列の確保
  call DistrbAlloc

  !--- 出力ファイルの定義
  call DistrbOpenDistrbFile
  
  !--- 擾乱の設定
  call DistrbType(&
       & ss_ExnerDistrb, ss_PotTempDistrb, fs_VelXDistrb, sf_VelZDistrb)
  
  !--- ファイル出力
  call DistrbOutputDistrbFile

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

contains

!!!
!!!配列の確保
!!!  
  subroutine DistrbAlloc
    use gridset, only: DimXMin, DimXMax, DimZMin, DimZMax
    allocate( &
         & ss_PotTempDistrb(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_ExnerDistrb(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelXDistrb(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZDistrb(DimXMin:DimXMax, DimZMin:DimZMax), &
         & 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 DistrbAlloc


!!!
!!!出力ファイルの定義. 計算領域のみ
!!! 
  subroutine DistrbOpenDistrbFile
    use gridset,   only: s_X, s_Z
    use expname,   only: exptitle, expsrc, expinst
    use distrbprm, only: distrbfile
    
    integer :: N

    N = size(s_X, 1)

    ! ヒストリー作成
    call HistoryCreate(                                  &
         & file = distrbfile,                            &         
         & 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='ExnerDistrb', dims=(/'x','z'/),      &
         & longname='nondimensional pressure', units='1',&
         & xtype='double' )

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

    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelXDistrb', dims=(/'x','z'/),      &
         & longname='zonal velocity',                   &
         & units='m/s', xtype='double' )

    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelZDistrb', dims=(/'x','z'/),      &
         & longname='vertical velocity',                &
         & units='m/s', xtype='double' )
    
  end subroutine DistrbOpenDistrbFile


!!!
!!!ファイル出力. 長い時間ステップで出力. 計算領域のみ
!!!
  subroutine DistrbOutputDistrbFile
    call HistoryPut( 'ExnerDistrb', ss_ExnerDistrb )
    call HistoryPut( 'PotTempDistrb', ss_PotTempDistrb )
    call HistoryPut( 'VelXDistrb',  fs_VelXDistrb  )
    call HistoryPut( 'VelZDistrb',  sf_VelZDistrb  )
  end subroutine DistrbOutputDistrbFile
  

!!!
!!!出力ファイルのクローズ. 計算領域のみ
!!!  
  subroutine DistrbCloseDistrbFile
    call HistoryClose
  end subroutine DistrbCloseDistrbFile
  

end program distrb
