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

program phy_adjust_fixed_test
  use phy_adjust_fixed, only: PHYADJFIX, PhyAdjFixCreate, &
    & AdjustFixed, PhyAdjFixClose, &
    & PhyAdjFixPutLine, PhyAdjFixInitialized, &
    & PhyAdjFixSetTime
  use dc_test, only: AssertEqual, AssertGreaterThan, AssertLessThan
  use dc_types, only: DP, STRING
  use dc_string, only: StoA, PutLine, toChar
  use dc_args, only: ARGS, DCArgsOpen, DCArgsHelpMsg, DCArgsOption, &
    & DCArgsDebug, DCArgsHelp, DCArgsStrict, DCArgsClose
  use gt4_history, only: GT_HISTORY, &
    & HistoryCreate, HistoryAddVariable, HistoryPut, HistoryClose, &
    & HistoryAddAttr, HistoryGet
  implicit none

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

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

  !-------------------------------------------------------------------
  !  軸データ
  !  Axes data
  !-------------------------------------------------------------------
  real(DP):: x_Lon (0:imax-1)
                              ! 経度. Longitude
  real(DP):: y_Lat (0:jmax-1)
                              ! 緯度. Latitude
  real(DP):: z_Sigma (0:kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
  real(DP):: r_Sigma (0:kmax)
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
  real(DP):: x_Lon_Weight (0:imax-1)
                              ! 経度積分用座標重み. 
                              ! Weight for integration in longitude
  real(DP):: y_Lat_Weight (0:jmax-1)
                              ! 緯度積分用座標重み. 
                              ! Weight for integration in latitude
  real(DP):: z_DelSigma (0:kmax-1)
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)

  !-------------------------------------------------------------------
  !  物理量
  !  Physical values
  !-------------------------------------------------------------------
  real(DP):: xyz_Temp (0:imax-1, 0:jmax-1, 0:kmax-1)
                              ! $ T $ .     温度. Temperature
  real(DP):: xyz_TempAns (0:imax-1, 0:jmax-1, 0:kmax-1)
                              ! $ T $ .     温度. Temperature
  real(DP):: xyz_QVap (0:imax-1, 0:jmax-1, 0:kmax-1)
                              ! $ q $ .     比湿. Specific humidity
  real(DP):: xyz_QVapAns (0:imax-1, 0:jmax-1, 0:kmax-1)
                              ! $ q $ .     比湿. Specific humidity
  real(DP):: xy_Ps (0:imax-1, 0:jmax-1)
                              ! $ p_s $ .   地表面気圧. Surface pressure

  real(DP):: xyz_DTempDtFixed (0:imax-1,0:jmax-1,0:0)
                              ! $ DP{T}{t} $ .
                              ! 固定値への調節に関する温度の変化率
                              ! Temperature tendency by adjustment to fixed value
  real(DP):: xyz_DTempDtFixedAns (0:imax-1,0:jmax-1,0:0)
                              ! $ DP{T}{t} $ .
                              ! 固定値への調節に関する温度の変化率
                              ! Temperature tendency by adjustment to fixed value
  real(DP):: xyz_DQVapDtFixed (0:imax-1,0:jmax-1,0:0)
                              ! $ q $ .
                              ! 固定値への調節に関する比湿の変化率
                              ! Specific humidity tendency by adjustment to fixed value
  real(DP):: xyz_DQVapDtFixedAns (0:imax-1,0:jmax-1,0:0)
                              ! $ q $ .
                              ! 固定値への調節に関する比湿の変化率
                              ! Specific humidity tendency by adjustment to fixed value

  !-------------------------------------------------------------------
  !  定数
  !  Constants
  !-------------------------------------------------------------------
  real(DP), parameter:: Cp = 11900.9264_DP
                              ! $ C_p $ [J kg-1 K-1].  大気定圧比熱.   Specific heat of air at constant pressure. 
                              ! Sugiyama et al. (2008) で用いられている値を使用. 
                              ! A value used in Sugiyama et al. (2008) is used. 
  real(DP), parameter:: RAir = 3611.44466_DP
                              ! $ R $   [J kg-1 K-1].  大気気体定数.   Gas constant of air. 
                              ! Sugiyama et al. (2008) で用いられている値を使用. 

  real(DP), parameter:: PI = 3.1415926535897930_DP
                              ! $ \pi $ . 円周率. Circular constant

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

  type(PHYADJFIX):: phy_adj_fix00, phy_adj_fix01
!!$  type(PHYADJFIX):: phy_adj_fix02, phy_adj_fix03
  logical:: err
  character(*), parameter:: subname = 'phy_adjust_fixed_test'
continue

  !-------------------------------------------------------------------
  !  コマンドライン引数の処理
  !  Command line options handling
  !-------------------------------------------------------------------
  call cmdline_optparse ! これは内部サブルーチン. This is an internal subroutine

  !-------------------------------------------------------------------
  !  軸データの設定
  !  Configure axes data
  !-------------------------------------------------------------------
  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'lon', & ! (in)
    & array = x_Lon, quiet = .true. )                 ! (out)
  x_Lon = x_Lon * PI / 180.0_DP

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'lon_weight', & ! (in)
    & array = x_Lon_Weight, quiet = .true. )                 ! (out)

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'lat', & ! (in)
    & array = y_Lat, quiet = .true. )                 ! (out)
  y_Lat = y_Lat * PI / 180.0_DP

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'lat_weight', & ! (in)
    & array = y_Lat_Weight, quiet = .true. )                 ! (out)

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'sig', & ! (in)
    & array = z_Sigma, quiet = .true. )                 ! (out)

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'sigm', & ! (in)
    & array = r_Sigma, quiet = .true. )                 ! (out)

  do k = 0, kmax - 1
    z_DelSigma(k) = r_Sigma(k) - r_Sigma(k+1)
  enddo

  !---------------------------------------------------------
  !  データ入力
  !  Input data
  !---------------------------------------------------------
  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'Temp', & ! (in)
    & array = xyz_Temp, quiet = .true. )                 ! (out)
  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'QVap', & ! (in)
    & array = xyz_QVap, quiet = .true. )                 ! (out)

  !-------------------------------------------------------------------
  !  基本の初期設定, 終了処理テスト
  !  Basic initialization and termination test
  !-------------------------------------------------------------------
  call PhyAdjFixCreate( &
    & phy_adj_fix = phy_adj_fix00, & ! (out)
    & imax = imax, jmax = jmax, kmax = 1, &              ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP )                               ! (in)
  call AssertEqual( 'basic initialization test 1', &
    & answer = .true., check = PhyAdjFixInitialized(phy_adj_fix00) )
!!$  call PhyAdjFixPutLine( phy_adj_fix = phy_adj_fix00 ) ! (in)

  call PhyAdjFixClose( phy_adj_fix = phy_adj_fix00 ) ! (inout)
  call AssertEqual( 'basic termination test 1', &
    & answer = .false., check = PhyAdjFixInitialized(phy_adj_fix00) )
  call PhyAdjFixPutLine( phy_adj_fix = phy_adj_fix00 ) ! (in)


  !-------------------------------------------------------------------
  !  重複初期設定に関するエラー処理のテスト
  !  Error handling related to duplicated initialization test
  !-------------------------------------------------------------------
  call PhyAdjFixCreate( &
    & phy_adj_fix = phy_adj_fix00, & ! (inout)
    & imax = imax, jmax = jmax, kmax = 1, &              ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP )                               ! (in)
  call PhyAdjFixCreate( &
    & phy_adj_fix = phy_adj_fix00, & ! (inout)
    & imax = imax, jmax = jmax, kmax = 1, &              ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP, &                              ! (in)
    & err = err )                             ! (out)
  call AssertEqual( 'error handling related to duplicated initialization test 1', &
    & answer = .true., check = err )
!!$  call PhyAdjFixPutLine( phy_adj_fix = phy_adj_fix00 ) ! (in)
  call PhyAdjFixClose( phy_adj_fix = phy_adj_fix00 ) ! (inout)


  !-------------------------------------------------------------------
  !  終了処理に関するエラー処理のテスト
  !  Error handling related to termination test
  !-------------------------------------------------------------------
  call PhyAdjFixClose( &
    & phy_adj_fix = phy_adj_fix00, & ! (inout)
    & err = err )                                    ! (out)
  call AssertEqual( 'error handling related to termination test 1', &
    & answer = .true., check = err )


  !-------------------------------------------------------------------
  !  AdjustFixed テスト
  !  AdjustFixed test
  !-------------------------------------------------------------------
  call PhyAdjFixCreate( &
    & phy_adj_fix = phy_adj_fix00, & ! (out)
    & imax = imax, jmax = jmax, kmax = 1, &              ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP, &                              ! (in)
    & x_Lon_Weight = x_Lon_Weight, &                     ! (in)
    & y_Lat_Weight = y_Lat_Weight, &                     ! (in)
    & z_DelSigma = z_DelSigma, &                         ! (in)
    & TempFixed = 490.0_DP * z_Sigma(0) ** ( RAir / Cp ), & ! (in)
    & QVapFixed = 0.00611641_DP )                           ! (in)
!!$    & QVapFixed = 0.00611641_DP, &                          ! (in)
!!$    & history_varlist = 'DTempDtFixed,DQVapDtFixed', &      ! (in)
!!$    & history_interval_value = 1.0, &                       ! (in)
!!$    & history_interval_unit = 'min', &                      ! (in)
!!$    & history_precision = 'double', &                       ! (in)
!!$    & history_fileprefix = 'tmp_' )                         ! (in)

!!$  call PhyAdjFixPutLine( phy_adj_fix = phy_adj_fix00 ) ! (in)

  call AdjustFixed( phy_adj_fix = phy_adj_fix00 )  ! (inout)

  xyz_Temp(:,:,0) = xyz_Temp(:,:,1)
  xyz_QVap(:,:,0) = xyz_QVap(:,:,1)

  call AdjustFixed( phy_adj_fix = phy_adj_fix00, & ! (inout)
    & xyz_Temp = xyz_Temp(:,:,0:0), &              ! (inout)
    & xyz_QVap = xyz_QVap(:,:,0:0), &              ! (inout)
    & xyz_DTempDtFixed = xyz_DTempDtFixed, &       ! (out)
    & xyz_DQVapDtFixed = xyz_DQVapDtFixed )        ! (out)

  call PhyAdjFixClose( phy_adj_fix = phy_adj_fix00 ) ! (inout)

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'Temp', & ! (in)
    & array = xyz_TempAns, quiet = .true. )                 ! (out)
  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test00.nc', varname = 'QVap', & ! (in)
    & array = xyz_QVapAns, quiet = .true. )                 ! (out)

  xyz_TempAns(:,:,0) = 490.0_DP * z_Sigma(0) ** ( RAir / Cp )
  xyz_QVapAns(:,:,0) = 0.00611641_DP

  call AssertEqual( 'AdjustFixed test 1-1', &
    & answer = xyz_TempAns(:,:,0:0), check = xyz_Temp(:,:,0:0), &
    & significant_digits = 15, ignore_digits = -15 )
  call AssertEqual( 'AdjustFixed test 1-2', &
    & answer = xyz_QVapAns(:,:,0:0), check = xyz_QVap(:,:,0:0), &
    & significant_digits = 15, ignore_digits = -15 )

  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test01.nc', varname = 'DTempDtFixed', & ! (in)
    & array = xyz_DTempDtFixedAns(:,:,0), quiet = .true. )             ! (out)
  call HistoryGet ( &
    & file = 'phy_adjust_fixed_test01.nc', varname = 'DQVapDtFixed', & ! (in)
    & array = xyz_DQVapDtFixedAns(:,:,0), quiet = .true. )             ! (out)

  call AssertEqual( 'AdjustFixed test 2-1', &
    & answer = xyz_DTempDtFixedAns, check = xyz_DTempDtFixed, &
    & significant_digits = 15, ignore_digits = -15 )
  call AssertEqual( 'AdjustFixed test 2-2', &
    & answer = xyz_DQVapDtFixedAns, check = xyz_DQVapDtFixed, &
    & significant_digits = 15, ignore_digits = -15 )

  !-------------------------------------------------------------------
  !  NAMELIST ファイルの読み込みテスト
  !  NAMELIST file loading test
  !-------------------------------------------------------------------
  call PhyAdjFixCreate( &
    & phy_adj_fix = phy_adj_fix01, & ! (out)
    & imax = imax, jmax = jmax, kmax = 1, &              ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP, &                              ! (in)
    & nmlfile = VAL_namelist )                           ! (in)

  xyz_Temp(:,:,0) = xyz_Temp(:,:,1)
  xyz_QVap(:,:,0) = xyz_QVap(:,:,1)

  call AdjustFixed( phy_adj_fix = phy_adj_fix01, & ! (inout)
    & xyz_Temp = xyz_Temp(:,:,0:0), &              ! (inout)
    & xyz_QVap = xyz_QVap(:,:,0:0) )               ! (inout)

  xyz_TempAns(:,:,0) = 440.0_DP
  xyz_QVapAns(:,:,0) = 0.007_DP

  call AssertEqual( 'NAMELIST file loading test 1-1', &
    & answer = xyz_TempAns(:,:,0:0), check = xyz_Temp(:,:,0:0), &
    & significant_digits = 15, ignore_digits = -15 )
  call AssertEqual( 'NAMELIST file loading test 1-2', &
    & answer = xyz_QVapAns(:,:,0:0), check = xyz_QVap(:,:,0:0), &
    & significant_digits = 15, ignore_digits = -15 )

!!$  call PhyAdjFixPutLine( phy_adj_fix = phy_adj_fix01 ) ! (in)
  call PhyAdjFixClose( phy_adj_fix = phy_adj_fix01 ) ! (inout)

  !-------------------------------------------------------------------
  !  無効な値に関するエラー処理のテスト
  !  Error handling related to invalid values test
  !-------------------------------------------------------------------
!!$  call PhyAdjFixCreate( &
!!$    & phy_adj_fix = phy_adj_fix02, & ! (out)
!!$    & imax = imax, jmax = jmax, &                  ! (in)
!!$    & x_Lon = x_Lon, y_Lat = y_Lat, &              ! (in)
!!$    & CoefAlpha = - 0.0001_DP, DelTime = 0.5_DP, & ! (in)
!!$    & err = err )                                  ! (out)
!!$  call AssertEqual( 'error handling related to invalid values test 1', &
!!$    & answer = .true., check = err )

!!$  call PhyAdjFixCreate( &
!!$    & phy_adj_fix = phy_adj_fix02, & ! (inout)
!!$    & imax = imax, jmax = jmax, &                  ! (in)
!!$    & x_Lon = x_Lon, y_Lat = y_Lat, &              ! (in)
!!$    & CoefAlpha = 0.0001_DP, DelTime = - 0.5_DP, & ! (in)
!!$    & err = err )                                  ! (out)
!!$  call AssertEqual( 'error handling related to invalid values test 2', &
!!$    & answer = .true., check = err )

  !-------------------------------------------------------------------
  !  ヒストリデータ出力テスト
  !  History data output test
  !-------------------------------------------------------------------
!!$  call PhyAdjFixCreate( &
!!$    & phy_adj_fix = phy_adj_fix03, & ! (out)
!!$    & imax = imax, jmax = jmax, &              ! (in)
!!$    & x_Lon = x_Lon, y_Lat = y_Lat, &          ! (in)
!!$    & CoefAlpha = 0.01_DP, DelTime = 0.5_DP, & ! (in)
!!$    & current_time_value = 0.0, &              ! (in)
!!$    & current_time_unit = 'sec', &             ! (in)
!!$    & history_varlist = 'Data2', &             ! (in)
!!$    & history_interval_value = 2.0, &          ! (in)
!!$    & history_interval_unit = 'sec', &         ! (in)
!!$    & history_precision = 'float', &           ! (in)
!!$    & history_fileprefix = 'AP_' )             ! (in)
!!$  call PhyAdjFixPutLine( phy_adj_fix = phy_adj_fix03 ) ! (in)
!!$
!!$  do i = 0, imax-1
!!$    x_Data1(i) = i * 1.0_DP
!!$  end do
!!$  do j = 0, jmax-1
!!$    y_Data2(j) = j * 1.1_DP
!!$  end do
!!$
!!$  do i = 1, 12
!!$    call AdjustFixed( &
!!$      & phy_adj_fix = phy_adj_fix03, & ! (inout)
!!$      & x_Data1 = x_Data1, y_Data2 = y_Data2 ) ! (inout)
!!$  end do
!!$
!!$  call PhyAdjFixSetTime( &
!!$    & phy_adj_fix = phy_adj_fix03, & ! (inout)
!!$    & current_time_value = 1.0, current_time_unit = 'minute' ) ! (in)
!!$
!!$  call AdjustFixed( &
!!$    & phy_adj_fix = phy_adj_fix03, & ! (inout)
!!$    & x_Data1 = x_Data1, y_Data2 = y_Data2, & ! (inout)
!!$    & historyput_flag = .true. )              ! (in)
!!$
!!$  call PhyAdjFixClose( phy_adj_fix = phy_adj_fix03 ) ! (inout)

contains

  subroutine cmdline_optparse
    !
    ! コマンドライン引数の処理を行います
    !
    ! Handle command line options
    !
    call DCArgsOpen( arg = arg )               ! (out)

    call DCArgsHelpMsg( arg = arg, &           ! (inout)
      & category = 'Title', msg = title )      ! (in)
    call DCArgsHelpMsg( arg = arg, &           ! (inout)
      & category = 'Usage', &                  ! (in)
      & msg = './' // trim(subname) // &
      &       ' [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)
  end subroutine cmdline_optparse

end program phy_adjust_fixed_test
