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

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

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

  real(8), allocatable ::  wa_data(:,:)
  real(8), allocatable ::  xva_data(:,:,:)
  real(8), allocatable ::  xya_data(:,:,:)

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

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

  integer :: n, k

 !---------------- 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','wa_base_mpi_module_supack_test', &
                         'wa_base_mpi_module_supack functions tests') 

  call wa_mpi_Initial( nm, im, jm, km )

  allocate(wa_data(nc,km))
  allocate(xva_data(0:im-1,jc,km))
  allocate(xya_data(0:im-1,jm,km))

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

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

  call AssertEqual(&
    message='wa_xva with Y_1^*',                                  &
    answer = wa_cleanup_wa(wa_xva(xva_data)),                     &
    check = wa_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xva_wa with Y_1^*',                                  &
    answer = xva_wa(wa_xva(xva_data)),                            &
    check = xva_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

  call AssertEqual(&
    message='wa_xva with Y_2^*',                                  &
    answer = wa_cleanup_wa(wa_xva(xva_data)),                     &
    check = wa_data,                                              &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='xva_wa with Y_2^*',                                  &
    answer = xva_wa(wa_xva(xva_data)),                            &
    check = xva_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

  call AssertEqual(&
    message='wa_xva, xva_wa with a general function',             &
    answer = xva_wa(wa_xva(xva_data)),                            &
    check = xva_data,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wa_base_mpi_module_supack_test', &
                         'wa_base_mpi_module_supack functions tests succeeded!') 

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

  call MPI_FINALIZE(IERR)

contains

  function wa_cleanup_wa(wa_data)
    real(8), intent(IN) :: wa_data(:,:)
    real(8)             :: wa_cleanup_wa(nc,size(wa_data,2))

    integer l,nmary(2)

    wa_cleanup_wa = wa_data

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

  end function wa_cleanup_wa

end program wa_base_mpi_module_supack_test

