!----------------------------------------------------------------------
! Copyright (c) 2013-2019 SPMODEL Development Group. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_base_module テストプログラム :: 終了処理のテスト
!
!履歴  2013/02/23  竹広真一
!      2019/12/24  佐々木洋平 正規化の変更: 1/sqrt(2) -> 1/2
!
program wa_base_module_finalize_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use dc_types, only: DP
  use wa_module
  implicit none

  integer, parameter :: im1=32, jm1=16, nm1=10, km1=3
  integer, parameter :: im2=64, jm2=32, nm2=21, km2=3

  real(DP), allocatable ::  wa_data(:,:)
  real(DP), allocatable ::  xya_data(:,:,:)

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

  real(DP) :: pi

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','wa_base_module_test', &
                         'wa_base_module finalize subroutine tests')

  call wa_Initial( nm1, im1, jm1, km1 )

  allocate(wa_data((nm1+1)**2,km1))
  allocate(xya_data(0:im1-1,1:jm1,km1))

  !---- Y_1^* のテスト ----
  xya_data(:,:,1) = sqrt(3.0D0)*sin(xy_Lat)                       ! Y_1^0
  wa_data= 0.0D0 ; wa_data(l_nm(1,0),1)=1.0D0
  xya_data(:,:,2) = sqrt(3.0D0/2)*cos(xy_Lat)*cos(xy_Lon)     ! Y_1^1
  wa_data(l_nm(1,1),2)=1.0D0/2.0D0
  xya_data(:,:,3) = -sqrt(3.0D0/2)*cos(xy_Lat)*sin(xy_Lon)    ! Y_1^{-1}
  wa_data(l_nm(1,-1),3)=1.0D0/2.0D0

  call AssertEqual(&
    message='wa_xya with Y_1^*',                                  &
    answer = wa_xya(xya_data),                                    &
    check = wa_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xya_wa with Y_1^*',                                  &
    answer = xya_wa(wa_xya(xya_data)),                            &
    check = xya_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^* のテスト ----
  xya_data(:,:,1) = sqrt(5.0D0)*(3.0/2*sin(xy_Lat)**2-1/2.0)           ! Y_2^0
  wa_data= 0.0D0 ; wa_data(l_nm(2,0),1)=1.0D0
  xya_data(:,:,2) = sqrt(5.0D0/6)*3.0*sin(xy_Lat)*cos(xy_Lat)*cos(xy_Lon)!Y_2^1
  wa_data(l_nm(2,1),2)=1.0D0/2.0D0
  xya_data(:,:,3) = -sqrt(5.0D0/24)*3.0*cos(xy_Lat)**2*sin(2*xy_Lon)   ! Y_2^-2
  wa_data(l_nm(2,-2),3)=1.0D0/2.0D0

  call AssertEqual(&
    message='wa_xya with Y_2^*',                                  &
    answer = wa_xya(xya_data),                                    &
    check = wa_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xya_wa with Y_2^*',                                  &
    answer = xya_wa(wa_xya(xya_data)),                            &
    check = xya_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的関数のテスト ----
  xya_data(:,:,1) = cos(2*xy_Lon-pi/3) &
       *(sin(xy_Lat)-1)**2*(sin(xy_Lat)-0.5)*(sin(xy_Lat)+1)
  xya_data(:,:,2) = sin(2*xy_Lon+pi/2) &
       *(sin(xy_Lat)-1)**2*(sin(xy_Lat)+1)**2
  xya_data(:,:,3) = cos(2*xy_Lon+pi/3) &
       *(sin(xy_Lat)-1)*(sin(xy_Lat)-0.5)**2*(sin(xy_Lat)+1)

  call AssertEqual(&
    message='wa_xya, xya_wa with a general function',             &
    answer = xya_wa(wa_xya(xya_data)),                            &
    check = xya_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !========================= 解像度変更 ===============================

  call wa_Finalize
  call wa_Initial( nm2, im2, jm2, km2 )

  deallocate(wa_data)
  deallocate(xya_data)

  allocate(wa_data((nm2+1)**2,km2))
  allocate(xya_data(0:im2-1,1:jm2,km2))

  !---- Y_1^* のテスト ----
  xya_data(:,:,1) = sqrt(3.0D0)*sin(xy_Lat)                       ! Y_1^0
  wa_data= 0.0D0 ; wa_data(l_nm(1,0),1)=1.0D0
  xya_data(:,:,2) = sqrt(3.0D0/2)*cos(xy_Lat)*cos(xy_Lon)     ! Y_1^1
  wa_data(l_nm(1,1),2)=1.0D0/2.0D0
  xya_data(:,:,3) = -sqrt(3.0D0/2)*cos(xy_Lat)*sin(xy_Lon)    ! Y_1^{-1}
  wa_data(l_nm(1,-1),3)=1.0D0/2.0D0

  call AssertEqual(&
    message='wa_xya with Y_1^*',                                  &
    answer = wa_xya(xya_data),                                    &
    check = wa_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xya_wa with Y_1^*',                                  &
    answer = xya_wa(wa_xya(xya_data)),                            &
    check = xya_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^* のテスト ----
  xya_data(:,:,1) = sqrt(5.0D0)*(3.0/2*sin(xy_Lat)**2-1/2.0)           ! Y_2^0
  wa_data= 0.0D0 ; wa_data(l_nm(2,0),1)=1.0D0
  xya_data(:,:,2) = sqrt(5.0D0/6)*3.0*sin(xy_Lat)*cos(xy_Lat)*cos(xy_Lon)!Y_2^1
  wa_data(l_nm(2,1),2)=1.0D0/2.0D0
  xya_data(:,:,3) = -sqrt(5.0D0/24)*3.0*cos(xy_Lat)**2*sin(2*xy_Lon)   ! Y_2^-2
  wa_data(l_nm(2,-2),3)=1.0D0/2.0D0

  call AssertEqual(&
    message='wa_xya with Y_2^*',                                  &
    answer = wa_xya(xya_data),                                    &
    check = wa_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xya_wa with Y_2^*',                                  &
    answer = xya_wa(wa_xya(xya_data)),                            &
    check = xya_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的関数のテスト ----
  xya_data(:,:,1) = cos(2*xy_Lon-pi/3) &
       *(sin(xy_Lat)-1)**2*(sin(xy_Lat)-0.5)*(sin(xy_Lat)+1)
  xya_data(:,:,2) = sin(2*xy_Lon+pi/2) &
       *(sin(xy_Lat)-1)**2*(sin(xy_Lat)+1)**2
  xya_data(:,:,3) = cos(2*xy_Lon+pi/3) &
       *(sin(xy_Lat)-1)*(sin(xy_Lat)-0.5)**2*(sin(xy_Lat)+1)

  call AssertEqual(&
    message='wa_xya, xya_wa with a general function',             &
    answer = xya_wa(wa_xya(xya_data)),                            &
    check = xya_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )


  call MessageNotify('M','wa_base_module', &
       'wa_base_module finalize subroutine tests succeeded!')

end program wa_base_module_finalize_test
