!= phy_cooling_s08 モジュールのテストプログラム
!
!= Test program for "phy_cooling_s08"
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: phy_cooling_s08_test.f90,v 1.1 2008-06-03 12:55:19 morikawa Exp $
! Tag Name::  $Name: dcpam4-20080624-1 $
! 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_cooling_s08 モジュールの動作テストを行うためのプログラムです.
! このプログラムがコンパイルできること, および実行時に
! プログラムが正常終了することを確認してください.
!
! This program checks the operation of "phy_cooling_s08" module.
! Confirm compilation and execution of this program.
!

program phy_cooling_s08_test
  use phy_cooling_s08, only: PHYCOOL, PhyCoolCreate, &
    & Cooling, PhyCoolClose, &
    & PhyCoolPutLine, PhyCoolInitialized, &
    & PhyCoolSetTime
  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: GT_HISTORY, &
    & HistoryCreate, HistoryAddVariable, HistoryPut, HistoryClose, &
    & HistoryAddAttr, HistoryGet
  implicit none

  !-------------------------------------------------------------------
  !  実験の表題, モデルの名称, 所属機関名
  !  Title of a experiment, name of model, sub-organ
  !-------------------------------------------------------------------
  character(*), parameter:: title = &
    & 'phy_cooling_s08_test $Name: dcpam4-20080624-1 $ :: ' // &
    & 'Test program of "phy_cooling_s08" 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 = 64
                              ! 経度格子点数. 
                              ! Number of grid points in longitude
  integer, parameter:: jmax = 32
                              ! 緯度格子点数. 
                              ! Number of grid points in latitude
  integer, parameter:: kmax = 24
                              ! 鉛直層数. 
                              ! 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):: xy_Ps  (0:imax-1, 0:jmax-1)
                              ! $ p_s $ .   地表面気圧. Surface pressure
  real(DP):: xyz_DTempDtCooling (0:imax-1, 0:jmax-1, 0:kmax-1)
                              ! $ \DP{T}{t} $ . 
                              ! 冷却の効果による温度変化. 
                              ! Temperature tendency by cooling effect
  real(DP):: xyz_DTempDtCoolingAns (0:imax-1, 0:jmax-1, 0:kmax-1)
                              ! $ \DP{T}{t} $ . 
                              ! 冷却の効果による温度変化. 
                              ! Temperature tendency by cooling effect

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


  !-------------------------------------------------------------------
  !  データ入出力
  !  Data I/O
  !-------------------------------------------------------------------
  type(GT_HISTORY):: gthist

  !-------------------------------------------------------------------
  !  作業変数
  !  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(PHYCOOL):: phy_cool00, phy_cool01
!!$  type(PHYCOOL):: phy_cool02, phy_cool03
  logical:: err
  character(*), parameter:: subname = 'phy_cooling_s08_test'
continue

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

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

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

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

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

  z_Sigma = &
    & (/ 0.8987071,   0.7155389,   0.5485278,    0.3987033,   0.299569, &
    &    0.2498709,   0.2165177,   0.1831572,    0.1497845,   0.1163892, &
    &    0.08294342,  0.05471022,  0.03476715,   0.01973636,  0.009868185, &
    &    0.004934088, 0.003123387, 0.002706487,  0.002289447, 0.001872307, &
    &    0.001454881, 0.001036775, 0.0006167641, 0.0001739958 /)

  r_Sigma = &
    & (/  1.0,       0.8,       0.6333333, 0.4666667, 0.3333333, &
    &     0.2666667, 0.2333333, 0.2,       0.1666667, 0.1333333, &
    &     0.1,       0.0666667, 0.0433333, 0.0266667, 0.0133333, &
    &     0.0066667, 0.0033333, 0.0029167, 0.0025,    0.0020833, &
    &     0.0016667, 0.00125,   0.0008333, 0.0004167, 0.0 /)

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

  !---------------------------------------------------------
  !  データ入力
  !  Input data
  !---------------------------------------------------------
  call HistoryGet ( &
    & file = 'phy_cooling_s08_test00.nc', varname = 'Ps', & ! (in)
    & array = xy_Ps, quiet = .true. )                ! (out)
  xy_Ps = xy_Ps * 30.0_DP

  !-------------------------------------------------------------------
  !  基本の初期設定, 終了処理テスト
  !  Basic initialization and termination test
  !-------------------------------------------------------------------
  call PhyCoolCreate( &
    & phy_cool = phy_cool00, &                     ! (out)
    & imax = imax, jmax = jmax, kmax = kmax, &           ! (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 = PhyCoolInitialized(phy_cool00) )
!!$  call PhyCoolPutLine( phy_cool = phy_cool00 ) ! (in)

  call PhyCoolClose( phy_cool = phy_cool00 ) ! (inout)
  call AssertEqual( 'basic termination test 1', &
    & answer = .false., check = PhyCoolInitialized(phy_cool00) )
!!$  call PhyCoolPutLine( phy_cool = phy_cool00 ) ! (in)


  !-------------------------------------------------------------------
  !  エラー処理のテスト
  !  Error handling test
  !-------------------------------------------------------------------
  call PhyCoolCreate( &
    & phy_cool = phy_cool00, &                     ! (out)
    & imax = imax, jmax = jmax, kmax = kmax, &           ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP )                               ! (in)
  call PhyCoolCreate( &
    & phy_cool = phy_cool00, &                     ! (out)
    & imax = imax, jmax = jmax, kmax = kmax, &           ! (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 PhyCoolPutLine( phy_cool = phy_cool00 ) ! (in)
  call PhyCoolClose( phy_cool = phy_cool00 ) ! (inout)


  call PhyCoolCreate( &
    & phy_cool = phy_cool00, &                     ! (out)
    & imax = imax, jmax = jmax, kmax = kmax, &           ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & DelTime = 600.0_DP, &                              ! (in)
    & UpperPress = 1.0e5_DP, LowerPress = 1.0e4_DP, &    ! (in)
    & err = err )                                        ! (out)
  call AssertEqual( 'error handling related to cooling range test 1', &
    & answer = .true., check = err )
  call AssertEqual( 'error handling related to cooling range test 2', &
    & answer = .false., check = PhyCoolInitialized(phy_cool00) )

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

  !-------------------------------------------------------------------
  !  Cooling テスト
  !  Cooling test
  !-------------------------------------------------------------------
  call PhyCoolCreate( &
    & phy_cool = phy_cool00, &                     ! (out)
    & imax = imax, jmax = jmax, kmax = kmax, &           ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & x_Lon_Weight = x_Lon_Weight, &                     ! (in)
    & y_Lat_Weight = y_Lat_Weight, &                     ! (in)
    & z_DelSigma = z_DelSigma, &                         ! (in)
    & DelTime = 600.0_DP )                               ! (in)
!!$  call PhyCoolPutLine( phy_cool = phy_cool00 )   ! (in)

  call Cooling( &
    & phy_cool = phy_cool00, &            ! (inout)
    & xy_Ps = xy_Ps, &                          ! (in)
    & xyz_DTempDtCooling = xyz_DTempDtCooling ) ! (out)

  call HistoryGet ( &
    & file = 'phy_cooling_s08_test01.nc', &     ! (in)
    & varname = 'DTempDtCooling', &                   ! (in)
    & array = xyz_DTempDtCoolingAns, quiet = .true. ) ! (out)

  call AssertEqual( 'Cooling test 1', &
    & answer = xyz_DTempDtCoolingAns, check = xyz_DTempDtCooling, &
    & significant_digits = 15, ignore_digits = -15 )

  !-------------------------------------------------------------------
  !  NAMELIST ファイルの読み込み, ヒストリ出力テスト
  !  NAMELIST file loading and history data output test
  !-------------------------------------------------------------------
  call PhyCoolCreate( &
    & phy_cool = phy_cool01, &                     ! (out)
    & imax = imax, jmax = jmax, kmax = kmax, &           ! (in)
    & x_Lon = x_Lon, y_Lat = y_Lat, z_Sigma = z_Sigma, & ! (in)
    & x_Lon_Weight = x_Lon_Weight, &                     ! (in)
    & y_Lat_Weight = y_Lat_Weight, &                     ! (in)
    & z_DelSigma = z_DelSigma, &                         ! (in)
    & DelTime = 600.0_DP, &                              ! (in)
    & nmlfile = VAL_namelist )                ! (in)
  call AssertEqual( 'NAMELIST file loading test 1', &
    & answer = .true., check = PhyCoolInitialized(phy_cool01) )
  call PhyCoolPutLine( phy_cool = phy_cool01 ) ! (in)

  do i = 1, 2
    call Cooling( &
      & phy_cool = phy_cool01, &            ! (inout)
      & xy_Ps = xy_Ps, &                          ! (in)
      & xyz_DTempDtCooling = xyz_DTempDtCooling ) ! (out)
  end do

  call PhyCoolClose( phy_cool = phy_cool01 ) ! (inout)

  call HistoryGet ( &
    & file = 'phy_cooling_s08_test02.nc', &     ! (in)
    & varname = 'DTempDtCooling', &                   ! (in)
    & array = xyz_DTempDtCoolingAns, quiet = .true. ) ! (out)

  call AssertEqual( 'NAMELIST loading test 1', &
    & answer = xyz_DTempDtCoolingAns, check = xyz_DTempDtCooling, &
    & significant_digits = 7, ignore_digits = -10 )


  !-------------------------------------------------------------------
  !  ヒストリデータ出力テスト
  !  History data output test
  !-------------------------------------------------------------------
!!$  call PhyCoolCreate( &
!!$    & phy_cool = phy_cool03, & ! (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 PhyCoolPutLine( phy_cool = phy_cool03 ) ! (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 PhyCoolCalculation( &
!!$      & phy_cool = phy_cool03, & ! (inout)
!!$      & x_Data1 = x_Data1, y_Data2 = y_Data2 ) ! (inout)
!!$  end do
!!$
!!$  call PhyCoolSetTime( &
!!$    & phy_cool = phy_cool03, & ! (inout)
!!$    & current_time_value = 1.0, current_time_unit = 'minute' ) ! (in)
!!$
!!$  call PhyCoolCalculation( &
!!$    & phy_cool = phy_cool03, & ! (inout)
!!$    & x_Data1 = x_Data1, y_Data2 = y_Data2, & ! (inout)
!!$    & historyput_flag = .true. )              ! (in)
!!$
!!$  call PhyCoolClose( phy_cool = phy_cool03 ) ! (inout)

!!$  !----------------------------------------------------------------
!!$  !  データ出力
!!$  !  Output data
!!$  !----------------------------------------------------------------
!!$  call HistoryCreate( &
!!$    & history = gthist, &                                ! (out)
!!$    & file = 'phy_cooling_s08_test01.nc', &        ! (in)
!!$    & title = title, &                                   ! (in)
!!$    & source = source, institution = institution, &      ! (in)
!!$    & dims = StoA('lon', 'lat', 'sig', 'sigm'), &        ! (in)
!!$    & dimsizes = (/imax, jmax, kmax, kmax + 1/), &       ! (in)
!!$    & longnames = &
!!$    &  StoA('longitude', 'latitude', &
!!$    &       'sigma at layer midpoints', &
!!$    &       'sigma at layer end-points (half level)'), & ! (in)
!!$    & units = StoA('degree_east', 'degree_north', &
!!$    &              '1', '1') )                           ! (out)
!!$
!!$  call HistoryPut( &
!!$    & history = gthist, &               ! (out)
!!$    & varname = 'lon', array = x_Lon )  ! (in)
!!$  call HistoryPut( &
!!$    & history = gthist, &               ! (out)
!!$    & varname = 'lat', array = y_Lat  ) ! (in)
!!$  call HistoryPut( &
!!$    & history = gthist, &                ! (out)
!!$    & varname = 'sig', array = z_Sigma ) ! (in)
!!$  call HistoryPut( &
!!$    & history = gthist, &                 ! (out)
!!$    & varname = 'sigm', array = r_Sigma ) ! (in)
!!$
!!$  call HistoryAddAttr( &
!!$    & history = gthist, &                    ! (inout)
!!$    & varname = 'lon', attrname = 'standard_name', & ! (in)
!!$    & value = 'longitude' )                          ! (in)
!!$  call HistoryAddAttr( &
!!$    & history = gthist, &                    ! (inout)
!!$    & varname = 'lat', attrname = 'standard_name', & ! (in)
!!$    & value = 'latitude' )                           ! (in)
!!$
!!$  call HistoryAddAttr( &
!!$    & history = gthist, &                              ! (inout)
!!$    & varname = 'sig', attrname = 'standard_name', &   ! (in)
!!$    & value = 'atmosphere_sigma_coordinate' )          ! (in)
!!$  call HistoryAddAttr( &
!!$    & history = gthist, &                              ! (inout)
!!$    & varname = 'sigm', attrname = 'standard_name', &  ! (in)
!!$    & value = 'atmosphere_sigma_coordinate' )          ! (in)
!!$  call HistoryAddAttr( &
!!$    & history = gthist, &                         ! (inout)
!!$    & varname = 'sig', attrname = 'positive', &   ! (in)
!!$    & value = 'down' )                            ! (in)
!!$  call HistoryAddAttr( &
!!$    & history = gthist, &                         ! (inout)
!!$    & varname = 'sigm', attrname = 'positive', &  ! (in)
!!$    & value = 'down' )                            ! (in)
!!$
!!$
!!$  call HistoryAddVariable( &
!!$    & history = gthist, &                           ! (inout)
!!$    & varname = 'DTempDtCooling', &                 ! (in)
!!$    & dims = StoA('lon', 'lat', 'sig'), &           ! (in)
!!$    & longname = 'temperature tendency by effect of radiation cooling like Jovian atmoshere', &             ! (in)
!!$    & units = 'K s-1', xtype = 'double' )               ! (in)
!!$  call HistoryPut( &
!!$    & history = gthist, &                                      ! (inout)
!!$    & varname = 'DTempDtCooling', array = xyz_DTempDtCooling ) ! (in)
!!$
!!$  call HistoryClose( history = gthist ) ! (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_cooling_s08_test
