!----------------------------------------------------------------------
!     Copyright (c) 2017 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wa_wave_interpolate_module_svpack テストプログラム :: 補間関数のテスト
!
!履歴  2017/03/09  竹広真一 wa_interpolate_module_sjpack_test.f90 より改造
!
program wa_wave_interpolate_module_svpack_test

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

  integer, parameter :: im=2, jm=16, nm=10, km=2
  integer            :: m

  real(8), allocatable, dimension(:,:) ::  wa_data
  real(8), dimension(0:im-1,1:jm,km)   ::  xya_data

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

  real(8) :: Alon
  real(8) :: Alat
  real(8), dimension(km) :: a_data_sol
  real(8) :: pi

  pi = atan(1.0D0)*4.0D0
  Alon = pi/5 ; Alat = pi/3

  call MessageNotify('M','wa_wave_interpolate_module_svpack_test', &
                         'wa_wave_interpolate_module_svpack interpolate tests') 


  !---- Y_1^1 のテスト ----
  m=1
  call wa_Initial( nm, m, im, jm, km )
  allocate(wa_data(2*(nm-m+1),km))

  xya_data(0,:,1) = sqrt(3.0D0/2)*cos(y_Lat) * 0.5           ! Y_1^1 
  xya_data(1,:,1) = 0.0D0
  a_data_sol(1) = sqrt(3.0D0/2)*cos(ALat)*cos(ALon)

  xya_data(0,:,2)   = 0.0D0
  xya_data(1,:,2)   = -sqrt(3.0D0)*cos(y_Lat) * 0.5
  a_data_sol(2) = sqrt(3.0D0)*cos(ALat)*sin(ALon)

  wa_data = wa_xya(xya_data)

  call AssertEqual(&
    message='a_Interpolate_wa with Y_1^1',                        &
    answer = a_Interpolate_wa(wa_data,alon,alat),                 &
    check =  a_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^1,-1 のテスト ----

  xya_data(0,:,1) = sqrt(5.0D0/6)*3.0*sin(y_Lat)*cos(y_Lat) * 0.5  !Y_2^1
  xya_data(1,:,1) = 0.0D0
  a_data_sol(1) = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * cos(ALon)

  xya_data(0,:,2) = 0.0D0
  xya_data(1,:,2) = - sqrt(5.0D0/6)*3.0*sin(y_Lat)*cos(y_Lat) * 0.5  !Y_2^-1
  a_data_sol(2) = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * sin(ALon)

  wa_data = wa_xya(xya_data)

  call AssertEqual(&
    message='a_Interpolate_wa with Y_2^1',                        &
    answer = a_Interpolate_wa(wa_data,alon,alat),                 &
    check =  a_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call wa_Finalize
  deallocate(wa_data)

  !---- Y_2^2,-2 のテスト ----
  m=2
  call wa_Initial( nm, m, im, jm, km )
  allocate(wa_data(2*(nm-m+1),km))

  xya_data(0,:,1) = sqrt(5.0D0/24)*3.0*cos(y_Lat)**2*0.5        ! Y_2^-2
  xya_data(1,:,1) = 0.0
  a_data_sol(1) = sqrt(5.0D0/24)*3.0*cos(ALat)**2*cos(2*ALon)

  xya_data(0,:,2) = 0.0
  xya_data(1,:,2) = -sqrt(5.0D0/24)*3.0*cos(y_Lat)**2*0.5        ! Y_2^-2
  a_data_sol(2) = sqrt(5.0D0/24)*3.0*cos(ALat)**2*sin(2*ALon)

  wa_data = wa_xya(xya_data)

  call AssertEqual(&
    message='a_Interpolate_wa with Y_2^2',                        &
    answer = a_Interpolate_wa(wa_data,alon,alat),                 &
    check =  a_data_sol,                                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wa_wave_interpolate_module_svpack_test', &
                      'wa_wave_interpolate_module_svpack function tests succeeded') 

end program wa_wave_interpolate_module_svpack_test

