!= Program ArareAnal
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: arareanal.f90,v 1.19 2006/09/21 02:41:47 odakker Exp $
! Tag Name::  $Name: arare4-20060922 $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
! 非静力学モデル deepconv/arare 2 次データ作成プログラム
!
!
program ArareAnal

  !モジュール読み込み
  use gt4_history
  use fileset
  use gridset 
  use debugset
  use timeset
  use dc_trace
  use dc_string

  use MoistAdjust
  use WarmRainPrm 
  use MoistBuoyancy
  use MoistFunc

  use Average
  use DynFunc
  use basicset
  use boundary
  use Turbulence
  use NumDiffusion

  use radiation
  
  use chemcalc
  use Boundary
  use ECCM

  use StoreSet
  use Damping

  implicit none
  
  !変数定義
  real(8), allocatable :: xz_PotTemp(:,:)
  real(8), allocatable :: xz_PotTempAll(:,:)
  real(8), allocatable :: xz_Exner(:,:)
  real(8), allocatable :: pz_VelX(:,:)
  real(8), allocatable :: xr_VelZ(:,:)
  real(8), allocatable :: xza_MixRt(:,:,:)
  real(8), allocatable :: xza_MixRtAll(:,:,:)
  real(8), allocatable :: xza_MixRtSat(:,:,:)
  real(8), allocatable :: xz_Temp(:,:)
  real(8), allocatable :: xz_VPotTemp(:,:)
  real(8), allocatable :: xz_VPotTempAll(:,:)
  real(8), allocatable :: xza_Rho(:,:,:)
  real(8), allocatable :: xza_CloudPhys1(:,:,:)
  real(8), allocatable :: xza_CloudPhys2(:,:,:)
  real(8), allocatable :: xza_CloudPhys3(:,:,:)
  real(8), allocatable :: xz_BuoyAll(:,:)
  real(8), allocatable :: xz_BuoyTemp(:,:)
  real(8), allocatable :: xz_BuoyMolWt(:,:)
  real(8), allocatable :: xz_BuoyDrag(:,:)
  real(8), allocatable :: xz_Stab(:,:)
  real(8), allocatable :: xz_StabTemp(:,:)
  real(8), allocatable :: xz_StabMolWt(:,:)
  real(8)              :: AnalTime

  real(8), allocatable :: xz_Km(:,:)
  real(8), allocatable :: xz_Km1(:,:)
  real(8), allocatable :: xz_Km2(:,:)
  real(8), allocatable :: xz_Km3(:,:)
  real(8), allocatable :: xz_Km4(:,:)
  real(8), allocatable :: xz_Km5(:,:)

  real(8), allocatable :: xz_TempAll(:,:)
  real(8), allocatable :: xz_PressAll(:,:)
  real(8), allocatable :: xz_DelNH4SH(:,:)
  real(8), allocatable :: xz_EquivConst(:,:)
  real(8), allocatable :: xza_DelMixRt(:,:,:)

  real(8), allocatable :: xz_Cloud(:,:)
  real(8), allocatable :: xz_Rain(:,:)

  integer      :: LoopNum, LoopNum2
  integer      :: GasNum(10)
  integer      :: CloudNum(10)
  integer      :: RainNum(10)
  integer      :: NH3Num  
  integer      :: H2SNum  
  integer      :: NH4SHNum

  integer             :: spc, t, s
  character(50)       :: cfgfile
  character(50)       :: AnalFile

!  call setdebug
    
  !----------------------------------------------------------------------
  ! 変数参照型モジュールの初期化
  !----------------------------------------------------------------------
  !NAMELIST ファイルの取得
  call getarg( 1, cfgfile )
  write(*,*) "Input NAMELIST file: ", cfgfile
  
  !I/O ファイル名の初期化
  !  NAMELIST ファイル名を指定し, deepconv/arare の
  !  出力ファイル名を NAMELIST から得る
  call fileset_init(cfgfile)
  
  !デバグ設定の初期化
  !  NAMELIST から情報を得て, デバッグ出力スイッチの切替えを行う.  
  call debugset_init(cfgfile)
  
  !時刻に関する設定の初期化
  !  NAMELIST から必要な情報を読み取り, 時間関連の変数の設定を行う. 
  call timeset_init(cfgfile)

  !格子点情報の初期化
  !  NAMELIST から情報を得て, 格子点を計算する
  call gridset_init(cfgfile)
  
  !基本場の情報の初期化
  !  NAMELIST から情報を得て, 基本場を設定する.
  call basicset_init(cfgfile)
  
  !積算値を保管するためのモジュールの初期化
  !  NAMELIST から情報を得て, 基本場を設定する.
  call storeset_init( )
  
  !内部変数の初期化. とりあえずゼロを入れて値を確定させておく. 
  call ArareAlloc
  
    
  !----------------------------------------------------------------------
  ! パッケージ型モジュールの初期化
  !   デフォルトの値から変更する必要のあるルーチンのみ初期化
  !----------------------------------------------------------------------
  AnalFile = HistoryFile(1:len_trim(HistoryFile)-3) // '_anal.nc'
  call AnalFile_Open( )
  call AnalFile_BasicZ_Get( )

  call Damping_Init( cfgfile )      !波の減衰係数の初期化
  call NumDiffusion_Init()          !数値拡散項の初期化
  call Turbulence_Init()            !乱流計算の初期化
  call MoistAdjust_Init()           !湿潤飽和調整法の初期化
  call WarmRainPrm_Init( cfgfile )  !暖かい雨のパラメタリゼーションの初期化
!  call FillNegative_Init( xza_MixRtBasicZ, xz_DensBasicZ) 
!                                    !移流による負の量の処理
  call Radiation_Init( cfgfile )    !放射強制の初期化
  call MoistBuoy_Init()             !分子量に対する浮力計算ルーチンの初期化
!  call xz_Exner_Init()              !陰解法の初期化  

  call WarmRainPrm_prm( LoopNum, LoopNum2, RainNum, CloudNum, GasNum, NH3Num, H2SNum, NH4SHNum )


!!!
!!!時刻を進めながら, ファイルの値を得る
!!!
  do t = 1, int( TimeInt / TimeDisp ) + 1, 10

    !----------------------------------------------------------------
    ! ヒストリファイルを開き値を得る. 
    !----------------------------------------------------------------
    call AnalFile_Get(                                           &
      &       t,  AnalTime,                                      &
      &       xz_PotTemp, xz_Exner, pz_VelX, xr_VelZ, xza_MixRt )
    write(*,*) t, AnalTime
    

    !----------------------------------------------------------------
    ! 初期化
    !----------------------------------------------------------------
    xz_Cloud = 0.0d0
    xz_Rain  = 0.0d0
    xza_MixRtAll = xza_MixRt + xza_MixRtBasicZ
    
    !----------------------------------------------------------------
    ! 密度, 温度, 仮温位の計算
    !----------------------------------------------------------------
    do spc = 1, SpcNum
      xza_Rho(:,:, spc) =                                                   &
        &  xz_DensBasicZ * ( xza_MixRtBasicZ(:,:,spc) + xza_MixRt(:,:,spc) )
    end do

    xz_PotTempAll  = xz_PotTemp + xz_PotTempBasicZ
    
    xz_VPotTemp    = xz_PotTemp / xz_EffMolWtBasicZ
    xz_VPotTempAll = xz_PotTempAll / xz_EffMolWtBasicZ

    xz_TempAll = &
      &   ( xz_Exner + xz_ExnerBasicZ ) * xz_PotTempAll
    xz_Temp = xz_TempAll - xz_ExnerBasicZ * xz_PotTempBasicZ 
    
    !----------------------------------------------------------------
    ! 圧力の計算
    !----------------------------------------------------------------
    xz_PressAll = PressBasis * ((xz_Exner + xz_ExnerBasicZ ) ** (CpDry / GasRDry))
    
    !----------------------------------------------------------------
    ! 浮力
    !----------------------------------------------------------------
    xz_BuoyTemp  =  xz_avr_xr( xr_Buoy( xz_PotTemp ) )
    xz_BuoyMolWt =  xz_avr_xr( xr_BuoyMolWt( xza_MixRt ) )
    xz_BuoyDrag  =  xz_avr_xr( xr_BuoyDrag( xza_MixRt ) )
    xz_BuoyAll   =  xz_BuoyTemp + xz_BuoyMolWt + xz_BuoyDrag
    
    !----------------------------------------------------------------
    ! 雲物理量を計算
    !----------------------------------------------------------------
    xza_CloudPhys1 = xza_Cloud2Rain( xza_MixRt, 2.0d0 * DelTimeLong )

    if ( LoopNum /= 0 ) then 
      xza_CloudPhys2 =                                         &
        &   xza_Rain2Gas(xz_Exner, xz_PotTemp, xza_MixRt, 2.0d0 * DelTimeLong )    
    end if
    if ( NH4SHNum /= 0 ) then     
      xza_CloudPhys2 =                                          &
        &   xza_CloudPhys2                                      &
        & + xza_CloudPhys2 + xza_Rain2GasNH4SH(xz_Exner, xz_PotTemp, xza_MixRt, 2.0d0 * DelTimeLong )
    end if

    do spc = 1, SpcNum      
      xza_CloudPhys3(:,:,spc) = xz_FallRain(xza_MixRt(:,:,spc), spc)
    end do
    
    !----------------------------------------------------------------
    ! 乱流拡散係数の計算
    !----------------------------------------------------------------
    xz_Km1 = xz_AdvScalar(xz_Km, pz_VelX, xr_VelZ)           
    xz_Km2 = xz_BuoyMoistKm(xz_PotTemp, xz_Exner, xza_MixRt)
    xz_Km3 = xz_ShearKm(xz_Km, pz_VelX, xr_VelZ)         
    xz_Km4 = xz_NumDiffScalar(xz_Km)                             
    xz_Km5 = xz_DispKm(xz_Km)                                    

    !----------------------------------------------------------------
    ! 雲混合比, 雨混合比をまとめる
    !----------------------------------------------------------------
    do s = 1, LoopNum2
      xz_Cloud = xz_Cloud + xza_MixRt(:,:,CloudNum(s))
      xz_Rain  = xz_Rain  + xza_MixRt(:,:,RainNum(s))
    end do
    
    !----------------------------------------------------------------
    ! 飽和蒸気圧のチェック
    !----------------------------------------------------------------
    xza_MixRtSat = 1.0d0
    if (LoopNum /= 0 ) then 
      do s = 1, LoopNum
        xza_MixRtSat(:,:,GasNum(s)) =                      &
          & xz_SvapPress(SpcWetID(CloudNum(s)), xz_TempAll) &
          &  * MolWtWet(CloudNum(s))                        &
          &  / (MolWtDry * xz_PressAll)                     
      end do

      xza_DelMixRt = 0.0d0
      xza_DelMixRt = ( - xza_MixRtSat + xza_MixRtAll ) / xza_MixRtSat
    end if
    
    !----------------------------------------------------------------
    ! 安定度の計算
    !----------------------------------------------------------------
    call ECCM_Stab( xz_PotTemp, xz_Exner, xza_MixRt, &
      &             xz_Stab, xz_StabTemp, xz_StabMolWt)

    !----------------------------------------------------------------    
    ! ファイル出力
    !----------------------------------------------------------------
    call AnalFile_OutPut( AnalTime )
    
  end do
  
  !----------------------------------------------------------------    
  ! ファイルを閉じる
  !----------------------------------------------------------------    
  call HistoryClose

    
contains

  subroutine AnalFile_Open( )
    !
    !解析ファイルの定義
    !
    
    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(4)        :: SpcID(SpcNum)
!    character(50)  :: AnalFile
    integer        :: s
    
    do s = 1, SpcNum
      SpcID(s) = real( s, 4 )
    end do


    !-----------------------------------------------------------
    ! ヒストリー作成
    !-----------------------------------------------------------
    call HistoryCreate(                              &
      & file = AnalFile,                             &
      & title = exptitle,                            &
      & source = expsrc,                             &
      & institution = expinst,                       &
      & dims=(/'x','z','s','t'/),                    &
      & dimsizes=(/FileNX, FileNZ, SpcNum, 0/),      &
      & longnames=(/'X-coordinate',                  &
      &             'Z-coordinate',                  &
      &             'Species Num ',                  &
      &             'Time        '/),                &
      & units=(/'m','m','1','s'/), origin=0.0,       &
      & interval=0.0 )
!      & units=(/'m','m','1','s'/) )
    
    !-----------------------------------------------------------  
    ! 軸の出力
    !-----------------------------------------------------------
    call HistoryPut('x', s_X( FileXMin: FileXMax ) )
    call HistoryPut('z', s_Z( FileZMin: FileZMax ) )
    call HistoryPut('s', real(SpcID, 4))

    !-----------------------------------------------------------  
    ! 解析用の変数の出力
    !-----------------------------------------------------------  
    !仮温位の擾乱
    call HistoryAddVariable(                                     &
      & varname='VPotTemp', dims=(/'x','z','t'/),                &
      & longname='disturbunce of virtual potential temperature', &
      & units='K', xtype='double' )

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

    !温位の擾乱
    call HistoryAddVariable(                         &
      & varname='PotTempAll', dims=(/'x','z','t'/),  &
      & longname='potential temperature',            &
      & units='K', xtype='double' )

    !温度擾乱
    call HistoryAddVariable(                         &
      & varname='Temp', dims=(/'x','z','t'/),        &
      & longname='disturbunce of temperature',       &
      & units='K', xtype='double' )

    !温度
    call HistoryAddVariable(                          &
      & varname='TempAll', dims=(/'x','z','t'/),      &
      & longname='temperature',                       &
      & units='K', xtype='double' )

    !圧力
    call HistoryAddVariable(                          &
      & varname='PressAll', dims=(/'x','z','t'/),     &
      & longname='Pressure',                          &
      & units='Pa', xtype='double' )

    !密度
    call HistoryAddVariable(                          &
      & varname='Rho', dims=(/'x','z','s','t'/),      &
      & longname='Density of condensable components', &
      & units='kg m|-2"', xtype='double' )

    !飽和蒸気圧
    call HistoryAddVariable(                                         &
      & varname='MixRtSat', dims=(/'x','z','s','t'/),                &
      & longname='Saturated Mixing ratio of condensable components', &
      & units='kg kg|-1"', xtype='double' )

    call HistoryAddVariable(                                         &
      & varname='Cloud2Rain', dims=(/'x','z','s','t'/),              &
      & longname='WarmRainPrm Cloud2Rain of condensable components', &
      & units='kg kg|-1"', xtype='double' ) 
 
    call HistoryAddVariable(                                       &
      & varname='Rain2Gas', dims=(/'x','z','s','t'/),              &
      & longname='WarmRainPrm Rain2Gas of condensable components', &
      & units='kg kg|-1"', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='FallRain', dims=(/'x','z','s','t'/),               &
      & longname='WarmRainPrm FallRain of condensable components',  &
      & units='kg kg|-1"', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='BuoyAll', dims=(/'x','z','t'/),                    &
      & longname='Buoyancy',                                        &
      & units=' ', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='BuoyTemp', dims=(/'x','z','t'/),                   &
      & longname='Buoyancy',                                        &
      & units=' ', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='BuoyMolWt', dims=(/'x','z','t'/),                  &
      & longname='Buoyancy',                                        &
      & units=' ', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='BuoyDrag', dims=(/'x','z','t'/),                   &
      & longname='Buoyancy',                                        &
      & units=' ', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='Stab', dims=(/'x','z','t'/),                       &
      & longname='Static Stability',                                &
      & units='s|-2" ', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='StabTemp', dims=(/'x','z','t'/),                   &
      & longname='Static Stability (Temp)',                         &
      & units='s|-2" ', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='StabMolWt', dims=(/'x','z','t'/),                  &
      & longname='Static Stability (MolWt)',                        &
      & units='s|-2" ', xtype='double' ) 

    !----------------------------------------------------------------
    ! 乱流拡散係数関連
    !----------------------------------------------------------------
    call HistoryAddVariable(                                        &
      & varname='AdvKm', dims=(/'x','z','t'/),                      &
      & longname='Advection term of Km',                            &
      & units='1', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='BuoyKm', dims=(/'x','z','t'/),                     &
      & longname='Buoyancy term of Km',                             &
      & units='1', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='ShearKm', dims=(/'x','z','t'/),                    &
      & longname='Shear term of Km',                                &
      & units='1', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='NumDiffKm', dims=(/'x','z','t'/),                  &
      & longname='Numerical Diffusion term of Km',                  &
      & units='1', xtype='double' ) 

    call HistoryAddVariable(                                        &
      & varname='DispKm', dims=(/'x','z','t'/),                     &
      & longname='Dissipation term of Km',                          &
      & units='1', xtype='double' ) 


    !----------------------------------------------------------------
    ! 飽和調節法関連
    !----------------------------------------------------------------

    call HistoryAddVariable(                                       &
      & varname='DelMixRt', dims=(/'x','z','s','t'/),              &
      & longname='Difference between MixRtAll and MixRtSat',       &
      & units='kg kg|-1"', xtype='double' ) 

    call HistoryAddVariable(                                       &
      & varname='DelNH4SH', dims=(/'x','z','t'/),                  &
      & longname='Difference between Equiv-Const',                 &
      & units='kg kg|-1"', xtype='double' ) 

    call HistoryAddVariable(                                       &
      & varname='EquivConst', dims=(/'x','z','t'/),                &
      & longname='Difference between Equiv-Const',                 &
      & units='kg kg|-1"', xtype='double' ) 

    !----------------------------------------------------------------
    ! 雲と雨
    !----------------------------------------------------------------
    call HistoryAddVariable(                                       &
      & varname='Cloud', dims=(/'x','z','t'/),                     &
      & longname='All Clouds Mixing Ratio',                        &
      & units='kg kg|-1"', xtype='double' ) 

    call HistoryAddVariable(                                       &
      & varname='Rain', dims=(/'x','z','t'/),                      &
      & longname='All Rains Mixing Ratio',                         &
      & units='kg kg|-1"', xtype='double' ) 

    call HistoryAddVariable(                            &
      & varname='MixRtAll', dims=(/'x','z','s','t'/),   &
      & longname='Mixing Ratio',                        &
      & units='kg kg|-1"', xtype='double' ) 

  end subroutine AnalFile_Open



  subroutine AnalFile_OutPut( Time )

    real(8), intent(in) :: Time    

    !----------------------------------------------------------------
    ! 値を出力
    !----------------------------------------------------------------
    call HistoryPut( 't', Time )
    
    call HistoryPut( 'Temp',     xz_Temp(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'TempAll',  xz_TempAll(FileXMin:FileXMax, FileZMin:FileZMax))
    
    call HistoryPut( 'VPotTemp',    xz_VPotTemp(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'VPotTempAll', xz_VPotTempAll(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'PotTempAll', xz_PotTempAll(FileXMin:FileXMax, FileZMin:FileZMax))
    
    call HistoryPut( 'PressAll', xz_PressAll(FileXMin:FileXMax, FileZMin:FileZMax))
    
    call HistoryPut( 'Rho', xza_Rho(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum))
    
    call HistoryPut( 'Cloud2Rain', xza_CloudPhys1(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum))

    call HistoryPut( 'Rain2Gas', xza_CloudPhys2(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum))
    
    call HistoryPut( 'FallRain', xza_CloudPhys3(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum))

    call HistoryPut( 'BuoyAll', xz_BuoyAll(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'BuoyTemp', xz_BuoyTemp(FileXMin:FileXMax, FileZMin:FileZMax))    
    
    call HistoryPut( 'BuoyMolWt', xz_BuoyMolWt(FileXMin:FileXMax, FileZMin:FileZMax))
    
    call HistoryPut( 'BuoyDrag', xz_BuoyDrag(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'DelMixRt',   abs(xza_DelMixRt(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum)) )

    call HistoryPut( 'MixRtAll',   xza_MixRtAll(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum))

    call HistoryPut( 'MixRtSat',   xza_MixRtSat(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum))

    call HistoryPut( 'DelNH4SH',   xz_DelNH4SH(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'EquivConst',   xz_EquivConst(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'Cloud',   xz_Cloud(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'Rain',    xz_Rain(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'Stab',   xz_Stab(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'StabTemp',   xz_StabTemp(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'StabMolWt',   xz_StabMolWt(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'AdvKm',   xz_Km1(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'BuoyKm',   xz_Km2(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'ShearKm',   xz_Km3(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'NumDiffKm',   xz_Km4(FileXMin:FileXMax, FileZMin:FileZMax))

    call HistoryPut( 'DispKm',   xz_Km5(FileXMin:FileXMax, FileZMin:FileZMax))
    
  end subroutine AnalFile_OutPut


!!!------------------------------------------------------------------------------!!!
  subroutine AnalFile_Get(                                      &
    &       i, AnalTime,                                        &
    &       xz_PotTemp, xz_Exner, pz_VelX, xr_VelZ, xza_MixRt )

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    integer, intent(in)  :: i    
    real(8), intent(out) :: AnalTime
    real(8), intent(out) :: pz_VelX(DimXMin:DimXMax,DimZMin:DimZMax)
    real(8), intent(out) :: xr_VelZ(DimXMin:DimXMax,DimZMin:DimZMax)
    real(8), intent(out) :: xz_Exner(DimXMin:DimXMax,DimZMin:DimZMax)
    real(8), intent(out) :: xz_PotTemp(DimXMin:DimXMax,DimZMin:DimZMax)
    real(8), intent(out) :: xza_MixRt(DimXMin:DimXMax,DimZMin:DimZMax,1:SpcNum)
    character(30)        :: name               !変数名
    character(10)       :: step
    

    step = '^' // adjustl(toChar(i))

    !-------------------------------------------------------------
    !Get a Value from netCDF File
    !-------------------------------------------------------------
    name = "t"
    call HistoryGet( HistoryFile, name, AnalTime, step )

    !-------------------------------------------------------------    
    ! Get a Value from netCDF File 
    !-------------------------------------------------------------
    name = "VelX"
    call HistoryGet( HistoryFile, name, pz_VelX(FileXMin:FileXMax, FileZMin:FileZMax), range=step )    
    pz_VelX = pz_BoundaryXCyc_pz( pz_VelX )
    pz_VelX = pz_BoundaryZSym_pz( pz_VelX )
    
    name = "VelZ"
    call HistoryGet( HistoryFile, name, xr_VelZ(FileXMin:FileXMax, FileZMin:FileZMax), range=step )
    xr_VelZ = xr_BoundaryXCyc_xr( xr_VelZ )
    xr_VelZ = xr_BoundaryZAntiSym_xr( xr_VelZ )
    
    name = "Exner"
    call HistoryGet( HistoryFile, name, xz_Exner(FileXMin:FileXMax, FileZMin:FileZMax), range=step )
    xz_Exner = xz_BoundaryXCyc_xz( xz_Exner )
    xz_Exner = xz_BoundaryZSym_xz( xz_Exner )
    
    name = "PotTemp"
    call HistoryGet( HistoryFile, name, xz_PotTemp(FileXMin:FileXMax, FileZMin:FileZMax), range=step )
    xz_PotTemp = xz_BoundaryXCyc_xz( xz_PotTemp )
    xz_PotTemp = xz_BoundaryZSym_xz( xz_PotTemp )

    name = "MixRt"
    call HistoryGet( HistoryFile, name, xza_MixRt(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum), range=step )
    xza_MixRt = xza_BoundaryXCyc_xza( xza_MixRt )
    xza_MixRt = xza_BoundaryZSym_xza( xza_MixRt )

    name = "Km"
    call HistoryGet( HistoryFile, name, xz_Km(FileXMin:FileXMax, FileZMin:FileZMax), range=step )
    xz_Km = xz_BoundaryXCyc_xz( xz_Km )
    xz_Km = xz_BoundaryZSym_xz( xz_Km )

  end subroutine AnalFile_Get



  subroutine AnalFile_BasicZ_Get( )

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    real(8) :: xz_DensBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8) :: xz_PotTempBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8) :: xz_ExnerBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8) :: xza_MixRtBZ(DimXMin:DimXMax, DimZMin:DimZMax,1:SpcNum)
    real(8) :: xz_EffMolWtBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8) :: xz_PressBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8) :: xz_TempBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8) :: xz_VelSoundBZ(DimXMin:DimXMax, DimZMin:DimZMax)
    character(30)        :: name               !変数名
    
    !-------------------------------------------------------------
    ! 基本場の取得
    !-------------------------------------------------------------
    name = "TempBasicZ"
    call HistoryGet( HistoryFile, name, xz_TempBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_TempBZ  = xz_BoundaryXCyc_xz( xz_TempBZ )
    xz_TempBZ  = xz_BoundaryZSym_xz( xz_TempBZ )

    name = "PressBasicZ"
    call HistoryGet( HistoryFile, name, xz_PressBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_PressBZ = xz_BoundaryXCyc_xz( xz_PressBZ )
    xz_PressBZ = xz_BoundaryZSym_xz( xz_PressBZ )

    name = "ExnerBasicZ"
    call HistoryGet( HistoryFile, name, xz_ExnerBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_ExnerBZ = xz_BoundaryXCyc_xz( xz_ExnerBZ )
    xz_ExnerBZ = xz_BoundaryZSym_xz( xz_ExnerBZ )

    name = "PotTempBasicZ"
    call HistoryGet( HistoryFile, name, xz_PotTempBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_PotTempBZ = xz_BoundaryXCyc_xz( xz_PotTempBZ )
    xz_PotTempBZ = xz_BoundaryZSym_xz( xz_PotTempBZ )

    name = "DensBasicZ"
    call HistoryGet( HistoryFile, name, xz_DensBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_DensBZ = xz_BoundaryXCyc_xz( xz_DensBZ )
    xz_DensBZ = xz_BoundaryZSym_xz( xz_DensBZ )

    name = "VelSoundBasicZ"
    call HistoryGet( HistoryFile, name, xz_VelSoundBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_VelSoundBZ = xz_BoundaryXCyc_xz( xz_VelSoundBZ )
    xz_VelSoundBZ = xz_BoundaryZSym_xz( xz_VelSoundBZ )

    name = "EffMolWtBasicZ"
    call HistoryGet( HistoryFile, name, xz_EffMolWtBZ(FileXMin:FileXMax, FileZMin:FileZMax) )
    xz_EffMolWtBZ = xz_BoundaryXCyc_xz( xz_EffMolWtBZ )
    xz_EffMolWtBZ = xz_BoundaryZSym_xz( xz_EffMolWtBZ )

    name = "MixRtBasicZ"
    call HistoryGet( HistoryFile, name, xza_MixRtBZ(FileXMin:FileXMax, FileZMin:FileZMax, 1:SpcNum) )
    xza_MixRtBZ = xza_BoundaryXCyc_xza( xza_MixRtBZ )
    xza_MixRtBZ = xza_BoundaryZSym_xza( xza_MixRtBZ )


    !----------------------------------------------------------
    ! BasicSet モジュールに値を設定
    !----------------------------------------------------------
    call BasicSetArray_Init(                       &
      & xz_PressBZ,   xz_ExnerBZ,   xz_TempBZ,     &
      & xz_PotTempBZ, xz_DensBZ,    xz_VelSoundBZ, &
      & xza_MixRtBZ,  xz_EffMolWtBZ )

  end subroutine AnalFile_BasicZ_Get


  subroutine ArareAlloc

    !基本場, 擾乱場の取得.
    allocate( &
      &       xz_PotTemp(DimXMin:DimXMax, DimZMin:DimZMax ),             &
      &       xz_PotTempAll(DimXMin:DimXMax, DimZMin:DimZMax ),          &
      &       xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax ),               &
      &       pz_VelX(DimXMin:DimXMax, DimZMin:DimZMax ),                &
      &       xr_VelZ(DimXMin:DimXMax, DimZMin:DimZMax ),                &
      &       xza_MixRt(DimXMin:DimXMax, DimZMin:DimZMax, 1:SpcNum ),    &
      &       xza_MixRtAll(DimXMin:DimXMax, DimZMin:DimZMax, 1:SpcNum ), &
      &       xza_MixRtSat(DimXMin:DimXMax, DimZMin:DimZMax, 1:SpcNum ), &
      &       xz_Temp(DimXMin:DimXMax, DimZMin:DimZMax ),                &
      &       xz_VPotTemp(DimXMin:DimXMax, DimZMin:DimZMax ),            &
      &       xz_VPotTempAll(DimXMin:DimXMax, DimZMin:DimZMax ),         &
      &       xza_Rho(DimXMin:DimXMax, DimZMin:DimZMax,SpcNum ),         &
      &       xza_CloudPhys1(DimXMin:DimXMax, DimZMin:DimZMax,1:SpcNum ),&
      &       xza_CloudPhys2(DimXMin:DimXMax, DimZMin:DimZMax,1:SpcNum ),&
      &       xza_CloudPhys3(DimXMin:DimXMax, DimZMin:DimZMax,1:SpcNum ),&
      !
      &       xz_Km(DimXMin:DimXMax, DimZMin:DimZMax ),                  &
      &       xz_Km1(DimXMin:DimXMax, DimZMin:DimZMax ),                 &
      &       xz_Km2(DimXMin:DimXMax, DimZMin:DimZMax ),                 &
      &       xz_Km3(DimXMin:DimXMax, DimZMin:DimZMax ),                 &
      &       xz_Km4(DimXMin:DimXMax, DimZMin:DimZMax ),                 &
      &       xz_Km5(DimXMin:DimXMax, DimZMin:DimZMax ),                 &
      !
      &       xz_Stab(DimXMin:DimXMax, DimZMin:DimZMax ),               &
      &       xz_StabTemp(DimXMin:DimXMax, DimZMin:DimZMax ),           &
      &       xz_StabMolWt(DimXMin:DimXMax, DimZMin:DimZMax ),          &
      &       xz_TempAll(DimXMin:DimXMax, DimZMin:DimZMax ),            &
      &       xz_PressAll(DimXMin:DimXMax, DimZMin:DimZMax ),           &
      &       xz_DelNH4SH(DimXMin:DimXMax, DimZMin:DimZMax ),           &
      &       xz_EquivConst(DimXMin:DimXMax, DimZMin:DimZMax ),         &
      &       xza_DelMixRt(DimXMin:DimXMax, DimZMin:DimZMax, 1:SpcNum ),&
      !
      &       xz_Cloud(DimXMin:DimXMax, DimZMin:DimZMax ),              &
      &       xz_Rain(DimXMin:DimXMax, DimZMin:DimZMax ),               &
      !
      &       xz_BuoyAll(DimXMin:DimXMax, DimZMin:DimZMax ),            &
      &       xz_BuoyTemp(DimXMin:DimXMax, DimZMin:DimZMax ),           &
      &       xz_BuoyMolWt(DimXMin:DimXMax, DimZMin:DimZMax ),          &
      &       xz_BuoyDrag(DimXMin:DimXMax, DimZMin:DimZMax )          )

    LoopNum  = 0 
    LoopNum2 = 0
    RainNum = 0
    CloudNum = 0
    GasNum   = 0
    NH3Num   = 0
    H2SNum   = 0
    NH4SHNum = 0

  end subroutine ArareAlloc



end program ArareAnal
