!----------------------------------------------------------------------
!     Copyright (c) 2014 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_wave_interpolate_module_sjpack テストプログラム :: 補間関数のテスト
!
!履歴  2014/05/17  竹広真一    w_interpolate_module_sjpack_test.f90 を改造
!
program w_wave_interpolate_module_sjpack_test

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

  integer, parameter :: im=2, jm=32, nm=21
  integer            :: m

  real(8), allocatable             ::  w_data(:)
  real(8), dimension(0:im-1,1:jm)  ::  xy_data

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

  real(8) :: Alon
  real(8) :: Alat
  real(8) :: data_sol
  real(8) :: pi

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

  call MessageNotify('M','w_wave_interpolate_module_test', &
                         'w_wave_interpolate_module interpolate(00) tests') 

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

  xy_data(0,:) = sqrt(3.0D0/2)*cos(y_Lat) * 0.5            ! Y_1^1
  xy_data(1,:) = 0.0D0
  data_sol = sqrt(3.0D0/2)*cos(ALat)*cos(ALon)
  w_data = w_xy(xy_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^1',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_1^-1 のテスト ----
  xy_data(0,:) = 0.0D0
  xy_data(1,:) = -sqrt(3.0D0)*cos(y_Lat) * 0.5             ! Y_1^{-1}
  data_sol = sqrt(3.0D0)*cos(ALat)*sin(ALon)
  w_data = w_xy(xy_data)
  call AssertEqual(&
    message='Interpolate_w with Y_1^-1',                          &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^1 のテスト ----
  xy_data(0,:) = sqrt(5.0D0/6)*3.0*sin(y_Lat)*cos(y_Lat) * 0.5  ! Y_2^1
  xy_data(1,:) = 0.0D0
  data_sol = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * cos(ALon)
  w_data = w_xy(xy_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^1',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^-1 のテスト ----
  xy_data(0,:) = 0.0D0
  xy_data(1,:) = -sqrt(5.0D0/6)*3.0*sin(y_Lat)*cos(y_Lat) * 0.5 ! Y_2^{-1}
  data_sol = sqrt(5.0D0/6)*3.0*sin(ALat)*cos(ALat) * sin(ALon)
  w_data = w_xy(xy_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^-1',                          &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call w_Finalize
  deallocate(w_data)

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

  xy_data(0,:) = sqrt(5.0D0/24)*3.0*cos(y_Lat)**2 * 0.5        ! Y_2^2
  xy_data(1,:) = 0.0D0
  data_sol = sqrt(5.0D0/24)*3.0*cos(ALat)**2*cos(2*ALon)
  w_data = w_xy(xy_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^2',                           &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y_2^-2 のテスト ----
  xy_data(0,:) = 0.0D0
  xy_data(1,:) = -sqrt(5.0D0/24)*3.0*cos(y_Lat)**2 * 0.5       ! Y_2^-2
  data_sol = sqrt(5.0D0/24)*3.0*cos(ALat)**2*sin(2*ALon)
  w_data = w_xy(xy_data)
  call AssertEqual(&
    message='Interpolate_w with Y_2^-2',                          &
    answer = data_sol,                                            &
    check = Interpolate_w(w_data,alon,alat),                      &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','w_wave_interpolate_module_sjpack_test', &
                      'w_wave_interpolate_module_sjpack function tests succeeded') 

end program w_wave_interpolate_module_sjpack_test

