!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wa_base_module_supack テストプログラム :: 基本変換関数のテスト
!
!履歴  2016/03/15  竹広真一
!      2016/03/30  竹広真一 wa_cleanup_wa 関数追加
!
program ua_base_mpi_module_svpack_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use ua_mpi_module_svpack
  use mpi
  implicit none

  integer, parameter :: im=32, jm=16, nm=10, km=4

  real(8), allocatable ::  ua_data(:,:)
  real(8), allocatable ::  xyb_data(:,:,:)

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

  real(8) :: pi
  integer :: iproc, np, ierr

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','ua_base_mpi_module_svpack_test', &
                         'ua_base_mpi_module_svpack functions tests') 

  call ua_mpi_Initial( nm, im, jm, km )  
  
  allocate(ua_data(nc,km))
  allocate(xyb_data(0:im-1,jm,kc))

  !========================= xva_wa, wa_xva のテスト===========================

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

  call AssertEqual(&
    message='wa_xya with Y_1^*',                                  &
    answer = ua_cleanup_ua(ua_xyb(xyb_data)),                     &
    check = ua_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyb_ua with Y_1^*',                                  &
    answer = xyb_ua(ua_xyb(xyb_data)),                            &
    check = xyb_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

  call AssertEqual(&
    message='ua_xyb with Y_2^*',                                  &
    answer = ua_cleanup_ua(ua_xyb(xyb_data)),                     &
    check = ua_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xyb_ua with Y_2^*',                                  &
    answer = xyb_ua(ua_xyb(xyb_data)),                            &
    check = xyb_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 一般的関数のテスト ----
  xyb_data = 0.0D0
  if ( kp_kl(1) /= 0 ) &
       xyb_data(:,:,kp_kl(1)) = cos(2*xy_Lon-pi/3) &
          *(sin(xy_Lat)-1)**2*(sin(xy_Lat)-0.5)*(sin(xy_Lat)+1)
  if ( kp_kl(2) /= 0 ) &
       xyb_data(:,:,kp_kl(2)) = sin(2*xy_Lon+pi/2) &
          *(sin(xy_Lat)-1)**2*(sin(xy_Lat)+1)**2
  if ( kp_kl(3) /= 0 ) &
       xyb_data(:,:,kp_kl(3)) = cos(2*xy_Lon+pi/3) &
          *(sin(xy_Lat)-1)*(sin(xy_Lat)-0.5)**2*(sin(xy_Lat)+1)
  if ( kp_kl(4) /= 0 ) &
       xyb_data(:,:,kp_kl(4)) = cos(2*xy_Lon+pi/2) &
          *(sin(xy_Lat)-1)**2*(sin(xy_Lat)+1)**2
       
  call AssertEqual(&
    message='ua_xyb, xyb_ua with a general function',             &
    answer = xyb_ua(ua_xyb(xyb_data)),                            &
    check = xyb_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','ua_base_mpi_module_svpack_test', &
                         'ua_base_mpi_module_svpack functions tests succeeded!') 

 !------ MPIの終了 ------

  call MPI_FINALIZE(IERR)

contains

  function ua_cleanup_ua(ua_data)
    real(8), intent(IN) :: ua_data(:,:)
    real(8)             :: ua_cleanup_ua(nc,size(ua_data,2))

    integer l,nmary(2)

    ua_cleanup_ua = ua_data

    do l=1,nc
       nmary = nm_l(l)
       if ( nmary(1) .lt. 0 ) ua_cleanup_ua(l,:) = 0.0D0
    end do

  end function ua_cleanup_ua

end program ua_base_mpi_module_svpack_test

