!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
  use fileset, only: fileset_init
  use expname, only: expname_init
  use arareprm,   only: arareprm_init
  use gacchanprm, only: gacchanprm_init

  implicit none

  real(8), allocatable  :: ss_ThetaDistrb(:,:)
  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_ThetaBasicZ(:,:)
  real(8), allocatable  :: ss_VelSoundBasicZ(:,:)

  type(gt_history)      :: basic, distrb
  

!!!
!!!初期化
!!!
  !--- I/O ファイル名の初期化
  call fileset_init
  
  !--- 格子点情報の初期化
  call gridset_init
  
  !--- 実験名の初期化
  call expname_init

  !--- 実験設定を読み込む
  call arareprm_init
  call gacchanprm_init
  
  !--- 配列の確保
  call GacchanAlloc

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

  !--- 擾乱の設定
  call DistrbType(&
       & ss_ExnerDistrb, ss_ThetaDistrb, fs_VelXDistrb, sf_VelZDistrb)
  
  !--- ファイル出力
  call GacchanOutputDistrbFile
  call GacchanOutputBasicFile

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

contains

!!!
!!!配列の確保
!!!  
  subroutine GacchanAlloc
    use gridset, only: DimXMin, DimXMax, DimZMin, DimZMax
    allocate( &
         & ss_ThetaDistrb(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_ThetaBasicZ(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax) ) 
  end subroutine GacchanAlloc


!!!
!!!出力ファイルの定義. 計算領域のみ
!!! 
  subroutine GacchanOpenDistrbFile
    use gridset, only: s_X, s_Z
    use expname, only: exptitle, expsrc, expinst
    use fileset, 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,                                 &
         & history = distrb)
    
    ! 変数出力
    call HistoryPut('x', s_X, distrb)
    call HistoryPut('z', s_Z, distrb)
    
    ! 無次元圧力
    call HistoryAddVariable(                             &
         & varname='ExnerDistrb', dims=(/'x','z'/),      &
         & longname='nondimensional pressure', units='1',&
         & xtype='double',                               & 
         & history = distrb)    
    ! 温位
    call HistoryAddVariable(                            &
         & varname='ThetaDistrb', dims=(/'x','z'/),     &
         & longname='potential temperature',            &
         & units='K', xtype='double',                   & 
         & history = distrb)    
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelXDistrb', dims=(/'x','z'/),      &
         & longname='zonal velocity',                   &
         & units='m/s', xtype='double',                 & 
         & history = distrb)        
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelZDistrb', dims=(/'x','z'/),      &
         & longname='vertical velocity',                &
         & units='m/s', xtype='double',                 & 
         & history = distrb)    
    
  end subroutine GacchanOpenDistrbFile


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

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


!!!
!!!出力ファイルの定義. 全領域
!!!
  subroutine GacchanOpenBasicFile
    use gridset, only: s_X, s_Z
    use expname, only: exptitle, expsrc, expinst
    use fileset, 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,                                 &
         & history = basic)
    
    ! 変数出力
    call HistoryPut('x', s_X, basic)
    call HistoryPut('z', s_Z, basic)
    
    ! 無次元圧力
    call HistoryAddVariable(                             &
         & varname='ExnerBasicZ', dims=(/'x','z'/),      &
         & longname='nondimensional pressure', units='1',&
         & xtype='double',                               & 
         & history = basic)    
    ! 温位
    call HistoryAddVariable(                            &
         & varname='ThetaBasicZ', dims=(/'x','z'/),    &
         & longname='potential temperature',            &
         & units='K', xtype='double',                   & 
         & history = basic)    
    ! 定圧比熱
    call HistoryAddVariable(                            &
         & varname='CpBasicZ', dims=(/'x','z'/),        &
         & longname='Specific Heat',                    &
         & units='J/K kg', xtype='double',              & 
         & history = basic)    
    ! 密度
    call HistoryAddVariable(                            &
         & varname='DensBasicZ', dims=(/'x','z'/),      &
         & longname='density',                          &
         & units='Kg/m^3', xtype='double',              & 
         & history = basic)    
    ! 音波速度
    call HistoryAddVariable(                            &
         & varname='VelSoundBasicZ', dims=(/'x','z'/),  &
         & longname='sound velocity',                   &
         & units='m/s^2', xtype='double',               & 
         & history = basic)    

  end subroutine GacchanOpenBasicFile


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


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

end program arare
