!= initial_data モジュールのテストプログラム
!
!= Test program for "initial_data"
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: initial_data_test.f90,v 1.4 2008-02-15 14:49:49 morikawa Exp $
! Tag Name::  $Name: dcpam4-20080427 $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
! <b>Note that Japanese and English are described in parallel.</b>
!
! initial_data モジュールの動作テストを行うためのプログラムです.
! このプログラムがコンパイルできること, および実行時に
! プログラムが正常終了することを確認してください.
!
! This program checks the operation of "initial_data" module.
! Confirm compilation and execution of this program.
!

program initial_data_test
  use constants, only: CONST, Create, Get
  use initial_data, only: INIDAT, IniDataCreate, IniDataClose, &
    & IniDataPutLine, IniDataInitialized, IniDataGetAxes, IniDataGet
  use dc_test, only: AssertEqual, AssertGreaterThan, AssertLessThan
  use dc_types, only: DP, STRING
  use dc_string, only: StoA, PutLine
  use dc_args, only: ARGS, DCArgsOpen, DCArgsHelpMsg, DCArgsOption, &
    & DCArgsDebug, DCArgsHelp, DCArgsStrict, DCArgsClose
  use gt4_history, only: HistoryGet
  implicit none

  !---------------------------------------------------------
  !  実験の表題, モデルの名称, 所属機関名
  !  Title of a experiment, name of model, sub-organ
  !---------------------------------------------------------
  character(*), parameter:: title = &
    & 'initial_data_test $Name: dcpam4-20080427 $ :: ' // &
    & 'Test program of "initial_data" module'
  character(*), parameter:: source = &
    & 'dcpam4 ' // &
    & '(See http://www.gfd-dennou.org/library/dcpam)'
  character(*), parameter:: institution = &
    & 'GFD Dennou Club (See http://www.gfd-dennou.org)'

  !-------------------------------------------------------------------
  !  格子点数・最大全波数
  !  Grid points and maximum truncated wavenumber
  !-------------------------------------------------------------------
  integer:: nmax = 10         ! 最大全波数. 
                              ! Maximum truncated wavenumber
  integer:: imax = 32         ! 経度格子点数. 
                              ! Number of grid points in longitude
  integer:: jmax = 16         ! 緯度格子点数. 
                              ! Number of grid points in latitude
  integer:: kmax = 12         ! 鉛直層数. 
                              ! Number of vertical level

  !---------------------------------------------------------
  !  軸データ
  !  Axes data
  !---------------------------------------------------------
  real(DP), allocatable:: x_Lon (:) ! 経度. Longitude
  real(DP), allocatable:: x_Lon_Weight (:)
                                    ! 経度積分用座標重み. 
                                    ! Weight for integration in longitude
  real(DP), allocatable:: y_Lat (:) ! 緯度. Latitude
  real(DP), allocatable:: y_Lat_Weight (:)
                                    ! 緯度積分用座標重み. 
                                    ! Weight for integration in latitude
  real(DP), allocatable:: z_Sigma (:) ! 緯度. Latitude
  real(DP), allocatable:: r_Sigma (:) ! 緯度. Latitude
!!$  real(DP), allocatable:: x_LonAns (:) ! 経度. Longitude
!!$  real(DP), allocatable:: x_Lon_WeightAns (:)
!!$                                    ! 経度積分用座標重み. 
!!$                                    ! Weight for integration in longitude
!!$  real(DP), allocatable:: y_LatAns (:) ! 緯度. Latitude
!!$  real(DP), allocatable:: y_Lat_WeightAns (:)
!!$                                    ! 緯度積分用座標重み. 
!!$                                    ! Weight for integration in latitude
!!$  real(DP), allocatable:: z_SigmaAns (:) ! 緯度. Latitude
!!$  real(DP), allocatable:: r_SigmaAns (:) ! 緯度. Latitude

  !---------------------------------------------------------
  !  初期値データ
  !  surface data
  !---------------------------------------------------------
  real(DP), allocatable:: xyz_Vor (:,:,:)
                              ! $ \zeta (t-\Delta t) $ . 渦度. Vorticity
  real(DP), allocatable:: xyz_Div (:,:,:)
                              ! $ D (t-\Delta t) $ .     発散. Divergence
  real(DP), allocatable:: xyz_Temp (:,:,:)
                              ! $ T (t-\Delta t) $ .     温度. Temperature
  real(DP), allocatable:: xyz_QVap (:,:,:)
                              ! $ q (t-\Delta t) $ .     比湿. Specific humidity
  real(DP), allocatable:: xy_Ps (:,:)
                              ! $ P_s (t-\Delta t) $ .   地表面気圧. Surface pressure

!!$  real(DP), allocatable:: xyz_VorAns (:,:,:)
!!$                              ! $ \zeta (t-\Delta t) $ . 渦度. Vorticity
!!$  real(DP), allocatable:: xyz_DivAns (:,:,:)
!!$                              ! $ D (t-\Delta t) $ .     発散. Divergence
!!$  real(DP), allocatable:: xyz_TempAns (:,:,:)
!!$                              ! $ T (t-\Delta t) $ .     温度. Temperature
!!$  real(DP), allocatable:: xyz_QVapAns (:,:,:)
!!$                              ! $ q (t-\Delta t) $ .     比湿. Specific humidity
!!$  real(DP), allocatable:: xy_PsAns (:,:)
!!$                              ! $ P_s (t-\Delta t) $ .   地表面気圧. Surface pressure


  !---------------------------------------------------------
  !  作業変数
  !  Work variables
  !---------------------------------------------------------
  type(ARGS):: arg            ! コマンドライン引数. 
                              ! Command line arguments
  logical:: OPT_namelist      ! -N, --namelist オプションの有無. 
                              ! Existence of '-N', '--namelist' option
  character(STRING):: VAL_namelist
                              ! -N, --namelist オプションの値. 
                              ! Value of '-N', '--namelist' option

  type(INIDAT):: ini_dat00, ini_dat01
  logical:: err
continue

  !---------------------------------------------------------
  !  コマンドライン引数の処理
  !  Command line arguments handling
  !---------------------------------------------------------
  call DCArgsOpen( arg = arg )               ! (out)
  call DCArgsHelpMsg( arg = arg, &           ! (inout)
    & category = 'Title', msg = title )      ! (in)
  call DCArgsHelpMsg( arg = arg, &           ! (inout)
    & category = 'Usage', &                   ! (in)
    & msg = './initial_data_test [Options]' ) ! (in)
  call DCArgsHelpMsg( arg = arg, &           ! (inout)
    & category = 'Source', msg = source )    ! (in)
  call DCArgsHelpMsg( arg = arg, &           ! (inout)
    & category = 'Institution', &            ! (in)
    & msg = institution )                    ! (in)
  call DCArgsOption( arg = arg, &           ! (inout)
    & options = StoA('-N', '--namelist'), & ! (in)
    & flag = OPT_namelist, &                ! (out)
    & value = VAL_namelist, &               ! (out)
    & help = "NAMELIST filename")           ! (in)
  call DCArgsDebug( arg = arg )  ! (inout)
  call DCArgsHelp( arg = arg )   ! (inout)
  call DCArgsStrict( arg = arg ) ! (inout)
  call DCArgsClose( arg = arg )  ! (inout)

  !---------------------------------------------------------
  !  初期設定テスト
  !  Initialization test
  !---------------------------------------------------------
  call IniDataCreate( ini_dat = ini_dat00, & ! (inout)
    & nmax = nmax, imax = imax, jmax = jmax, kmax = kmax ) ! (in)
  call AssertEqual( 'initialization test 1', &
    & answer = .true., check = IniDataInitialized(ini_dat00) )
  call IniDataPutLine( ini_dat = ini_dat00 ) ! (in)

  !---------------------------------------------------------
  !  軸データテスト
  !  Axes data test
  !---------------------------------------------------------
  allocate( x_Lon (0:imax-1) )
  allocate( x_Lon_Weight (0:imax-1) )
  allocate( y_Lat (0:jmax-1) )
  allocate( y_Lat_Weight (0:jmax-1) )
  allocate( z_Sigma (0:kmax-1) )
  allocate( r_Sigma (0:kmax) )

  call IniDataGetAxes( &
    & ini_dat = ini_dat00, &                        ! (inout)
    & x_Lon = x_Lon, x_Lon_Weight = x_Lon_Weight, & ! (out)
    & y_Lat = y_Lat, y_Lat_Weight = y_Lat_Weight, & ! (out)
    & z_Sigma = z_Sigma, r_Sigma = r_Sigma )        ! (out)

  call PutLine( x_Lon, &
    & lbounds = lbound(x_Lon), &
    & ubounds = ubound(x_Lon), &
    & indent = ' x_Lon=' )

  call PutLine( x_Lon_Weight, &
    & lbounds = lbound(x_Lon_Weight), &
    & ubounds = ubound(x_Lon_Weight), &
    & indent = ' x_Lon_Weight=' )

  call PutLine( y_Lat, &
    & lbounds = lbound(y_Lat), &
    & ubounds = ubound(y_Lat), &
    & indent = ' y_Lat=' )

  call PutLine( y_Lat_Weight, &
    & lbounds = lbound(y_Lat_Weight), &
    & ubounds = ubound(y_Lat_Weight), &
    & indent = ' y_Lat_Weight=' )

  call PutLine( z_Sigma, &
    & lbounds = lbound(z_Sigma), &
    & ubounds = ubound(z_Sigma), &
    & indent = ' z_Sigma=' )

  call PutLine( r_Sigma, &
    & lbounds = lbound(r_Sigma), &
    & ubounds = ubound(r_Sigma), &
    & indent = ' r_Sigma=' )

  !---------------------------------------------------------
  !  初期値データテスト
  !  Initial data test
  !---------------------------------------------------------
  allocate( xyz_Vor (0:imax-1, 0:jmax-1, 0:kmax-1) )
  allocate( xyz_Div (0:imax-1, 0:jmax-1, 0:kmax-1) )
  allocate( xyz_Temp (0:imax-1, 0:jmax-1, 0:kmax-1) )
  allocate( xyz_QVap (0:imax-1, 0:jmax-1, 0:kmax-1) )
  allocate( xy_Ps (0:imax-1, 0:jmax-1) )

  call IniDataGet( &
    & ini_dat = ini_dat00, & ! (inout)
    & xyz_Vor = xyz_Vor, &   ! (out)
    & xyz_Div = xyz_Div, &   ! (out)
    & xyz_Temp = xyz_Temp, & ! (out)
    & xyz_QVap = xyz_QVap, & ! (out)
    & xy_Ps = xy_Ps )        ! (out)

  call PutLine( xyz_Vor, &
    & lbounds = lbound(xyz_Vor), &
    & ubounds = ubound(xyz_Vor), &
    & indent = ' xyz_Vor=' )

  call PutLine( xyz_Div, &
    & lbounds = lbound(xyz_Div), &
    & ubounds = ubound(xyz_Div), &
    & indent = ' xyz_Div=' )

  call PutLine( xyz_Temp, &
    & lbounds = lbound(xyz_Temp), &
    & ubounds = ubound(xyz_Temp), &
    & indent = ' xyz_Temp=' )

  call PutLine( xy_Ps, &
    & lbounds = lbound(xy_Ps), &
    & ubounds = ubound(xy_Ps), &
    & indent = ' xy_Ps=' )

  call PutLine( xyz_QVap, &
    & lbounds = lbound(xyz_QVap), &
    & ubounds = ubound(xyz_QVap), &
    & indent = ' xyz_QVap=' )

  !---------------------------------------------------------
  !  終了処理テスト
  !  Termination test
  !---------------------------------------------------------
  call IniDataClose( ini_dat = ini_dat00 ) ! (inout)
  call AssertEqual( 'termination test 1', &
    & answer = .false., check = IniDataInitialized(ini_dat00) )
  call IniDataPutLine( ini_dat = ini_dat00 ) ! (in)

  !---------------------------------------------------------
  !  NAMELIST 読み込みテスト
  !  NAMELIST loading test
  !---------------------------------------------------------
  call IniDataCreate( ini_dat = ini_dat01, & ! (inout)
    & nmax = nmax, imax = imax, jmax = jmax, kmax = kmax, &
    & nmlfile = VAL_namelist, err = err ) ! (in)
  call AssertEqual( 'NAMELIST loading test 1', &
    & answer = .true., check = err )
  call IniDataPutLine( ini_dat = ini_dat01 ) ! (in)


end program initial_data_test
