!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wsc_module_svpack テストプログラム :: 基本変換関数のテスト
!
!履歴  2017/05/20  竹広真一
!
program wsc_module_svpack_base_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wsc_module_svpack
  implicit none

  integer,parameter  :: im=32, jm=16, km=16  ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: nm=10, lm=16         ! 切断波数の設定(水平, 動径)
  real(8),parameter  :: ri=0.5, ro=1.5      ! 内外半径

  real(8), dimension((nm+1)**2,lm)         ::  ws_data
  real(8), dimension((nm+1)**2,0:lm)       ::  wc_data
  real(8), dimension(0:im-1,1:jm,0:km)     ::  xyz_data
  real(8), dimension(0:im-1,1:jm,0:km)     ::  xyz_xi

  ! 判定誤差設定
  integer, parameter :: check_digits = 10
  integer, parameter :: ignore = -11

  real(8) :: pi

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','wsc_module_svpack_base_test', &
                         'wsc_module_svpack basic transformation functions tests') 

  call wsc_Initial(im,jm,km,nm,lm,ri,ro)

  xyz_xi = (xyz_Rad - ri)/(ro-ri)

  !---- Y_1^* のテスト ----
  xyz_data = sqrt(3.0D0)*sin(xyz_Lat)*sin(PI*xyz_xi)            ! Y_1^0 sin(z)
  ws_data= 0.0D0 ; ws_data(l_nm(1,0),1)=1.0D0

  call AssertEqual(&
    message='ws_xyz with Y_1^0 sin(z)',                           &
    answer = ws_data,                                             &
    check = ws_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_ws with Y_1^0 sin(z)',                           &
    answer = xyz_data,                                            &
    check = xyz_ws(ws_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xyz_data = sqrt(3.0D0/2)*cos(xyz_Lat)*cos(xyz_Lon)     ! Y_1^1
  wc_data= 0.0D0 ;  wc_data(l_nm(1,1),0)=1.0D0/2.0D0*2

  call AssertEqual(&
    message='wt_xyz with Y_1^1',                                  &
    answer = wc_data,                                             &
    check = wc_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_wt with Y_1^1',                                  &
    answer = xyz_data,                                            &
    check = xyz_wc(wc_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xyz_data = -sqrt(3.0D0/2)*cos(xyz_Lat)*sin(xyz_Lon)*cos(2*PI*xyz_xi) !Y_1^{-1} cos(2z)
  wc_data= 0.0D0 ;  wc_data(l_nm(1,-1),2)=1.0D0/2.0D0

  call AssertEqual(&
    message='wc_xyz with Y_1^-1 cos(2z)',                         &
    answer = wc_data,                                             &
    check = wc_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_wc with Y_1^-1 cos(2z)',                         &
    answer = xyz_data,                                            &
    check = xyz_wc(wc_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xyz_data = -sqrt(3.0D0/2)*cos(xyz_Lat)*sin(xyz_Lon)*sin(2*PI*xyz_xi) !Y_1^{-1}sin(2z)
  ws_data= 0.0D0 ;  ws_data(l_nm(1,-1),2)=1.0D0/2.0D0

  call AssertEqual(&
    message='ws_xyz with Y_1^-1 sin(2z)',                         &
    answer = ws_data,                                             &
    check = ws_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_ws with Y_1^-1 sin(2z)',                         &
    answer = xyz_data,                                            &
    check = xyz_ws(ws_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^* のテスト ----
  ! Y_2^0 sin(3z)
  xyz_data = sqrt(5.0D0)*(3.0/2*sin(xyz_Lat)**2-1/2.0)*sin(3*PI*xyz_xi)
  ws_data= 0.0D0 ; ws_data(l_nm(2,0),3)=1.0D0

  call AssertEqual(&
    message='wt_xyz with Y_2^0 sin(3z)',                          &
    answer = ws_data,                                             &
    check = ws_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_ws with Y_2^0 sin(3z)',                          &
    answer = xyz_data,                                            &
    check = xyz_ws(ws_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !Y_2^1 cos(4z)
  xyz_data = sqrt(5.0D0/6)*3.0*sin(xyz_Lat)*cos(xyz_Lat)*cos(xyz_Lon) &
            * cos(4*PI*xyz_xi)
  wc_data= 0.0D0 ; wc_data(l_nm(2,1),4)=1.0D0/2.0D0

  call AssertEqual(&
    message='wt_xyz with Y_2^1 cos(4z)',                          &
    answer = wc_data,                                             &
    check = wc_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_wt with Y_2^1 cos(4z)',                          &
    answer = xyz_data,                                            &
    check = xyz_wc(wc_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ! Y_2^-2
  xyz_data = -sqrt(5.0D0/24)*3.0*cos(xyz_Lat)**2*sin(2*xyz_Lon) &
            *cos(5*PI*xyz_xi)
  wc_data= 0.0D0 ; wc_data(l_nm(2,-2),5)=1.0D0/2.0D0

  call AssertEqual(&
    message='wt_xyz with Y_2^-2 cos(5z)',                         &
    answer = wc_data,                                             &
    check = wc_xyz(xyz_data),                                     &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyz_wt with Y_2^-2 cos(5z)',                         &
    answer = xyz_data,                                            &
    check = xyz_wc(wc_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的関数のテスト ----
  xyz_data = cos(2*xyz_Lon-pi/3) &
       *(sin(xyz_Lat)-1)**2*(sin(xyz_Lat)-0.5)*(sin(xyz_Lat)+1) &
       * xyz_xi*(1.0-xyz_xi)

  call AssertEqual(&
    message='xyz_ws and ws_xyz with a general function',          &
    answer = xyz_data,                                            &
    check = xyz_ws(ws_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xyz_data = cos(2*xyz_Lon-pi/3) &
       *(sin(xyz_Lat)-1)**2*(sin(xyz_Lat)-0.5)*(sin(xyz_Lat)+1) &
       * xyz_xi**2*(1.0-xyz_xi)**2

  call AssertEqual(&
    message='xyz_wc and wc_xyz with a general function',          &
    answer = xyz_data,                                            &
    check = xyz_wc(wc_xyz(xyz_data)),                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wsc_module_svpack_base_test_base', &
                         'wsc_module_svpack basic functions tests succeeded!') 

end program wsc_module_svpack_base_test

