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

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

!!$  integer, parameter :: im=1024, jm=512, nm=341
  integer, parameter :: im=128, jm=64, nm=42

  real(8), allocatable  :: w_data(:)
  real(8), allocatable  :: xv_data(:,:)
  real(8), allocatable  :: xv_Ddata(:,:)

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

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

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','w_base_mpi_module_supack_test', &
                         'w_base_mpi_module_supack functions tests') 

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

  call w_mpi_Initial( nm, im, jm )

  allocate(w_data(nc))
  allocate(xv_data(0:im-1,jc))
  allocate(xv_Ddata(0:im-1,jc))

  !========================= xv_w, w_xv のテスト===========================

  call MessageNotify('M','w_base_mpi_module_supack_test', &
                         'w_base_mpi_module_supack functions xv_w, w_xv tests') 
  !==================== Y_1^0 のテスト ====================
  xv_data = sqrt(3.0D0)*sin(xv_Lat)                      ! Y_1^0
  w_data= 0.0D0 ; if ( l_nm(1,0) .ne. 0 ) w_data(l_nm(1,0))=1.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^0 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = 0.0D0
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(3.0D0) * ( cos(xv_Lat)**2 - 2*sin(xv_Lat)**2 )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(3.0D0)*cos(xv_Lat)
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(3.0D0) * sin(xv_Lat)**2
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_1^0','Test of Y_1^0 succeeded!') 

  !==================== Y_1^1 のテスト ====================
  xv_data = sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon)          ! Y_1^1
  w_data= 0.0D0 ; if ( l_nm(1,1) .ne. 0 ) w_data(l_nm(1,1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^1 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(3.0D0/2)*cos(xv_Lat)*sin(xv_Lon)          ! Y_1^1
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = - sqrt(3.0D0/2) * 3 * sin(xv_Lat) * cos(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = - sqrt(3.0D0/2) * sin(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(3.0D0/2) * sin(xv_Lat)* cos(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_1^1','Test of Y_1^1 succeeded!') 

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

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^1 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon)          ! Y_1^{-1}
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(3.0D0/2) * 3 * sin(xv_Lat) * cos(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(3.0D0/2) * sin(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = - sqrt(3.0D0/2) * sin(xv_Lat)* cos(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !==================== Y_2^0 のテスト ====================
  xv_data = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)                 ! Y_2^0
  w_data= 0.0D0 ; if ( l_nm(2,0) .ne. 0 ) w_data(l_nm(2,0))=1.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^0 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = 0.0D0
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0)*(   3.0*sin(xv_Lat)*cos(xv_Lat)**2  &
                           - 3.0*sin(xv_Lat)**3 + sin(xv_Lat)  ) 
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0)*3.0*sin(xv_Lat)*cos(xv_Lat)
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)*sin(xv_Lat)
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^0','Test of Y_2^0 succeeded!') 

  !==================== Y_2^1 のテスト ====================
  xv_data = sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * cos(xv_Lon)! Y_2^1
  w_data= 0.0D0 ; if ( l_nm(2,1) .ne. 0 ) w_data(l_nm(2,1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^1 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat)*sin(xv_Lon)! Y_2^1
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0/6)*3.0 &
       *(cos(xv_Lat)**3-3*sin(xv_Lat)**2*cos(xv_Lat))*cos(xv_Lon)
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=1))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0/6)*3.0 * cos(2*xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0/6)*3.0*sin(xv_Lat)**2*cos(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^1','Test of Y_2^1 succeeded!') 

  !==================== Y_2^-1 のテスト ====================
  xv_data = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * sin(xv_Lon)! Y_2^-1
  w_data= 0.0D0 ; if ( l_nm(2,-1) .ne. 0 ) w_data(l_nm(2,-1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^-1 Test of w_xv(xv_data)',                       &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data))',                 &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat)*cos(xv_Lon)! Y_2^-1
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data,iflag=-1))',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data),iflag=-1)',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0 &
       *(cos(xv_Lat)**3-3*sin(xv_Lat)**2*cos(xv_Lat))*sin(xv_Lon)
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = -sqrt(5.0D0/6)*3.0 * cos(2*xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)**2*cos(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data,iflag=2))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data),iflag=2)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^-1','Test of Y_2^-1 succeeded!') 

  !==================== Y_2^2 のテスト ====================
  xv_data = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*cos(2*xv_Lon)         ! Y_2^2
  w_data= 0.0D0 ; if ( l_nm(2,2) .ne. 0 ) w_data(l_nm(2,2))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^2 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2* (-2)* sin(2*xv_Lon)  ! Y_2^2
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = - sqrt(5.0D0/24)*3.0*4*sin(xv_Lat)*cos(xv_Lat)**2*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = -sqrt(5.0D0/24)*3.0*2*cos(xv_Lat)*sin(xv_Lat)*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(xv_Lat)*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^2','Test of Y_2^2 succeeded!') 

  !==================== Y_2^-2 のテスト ====================
  xv_data = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_Lon)         ! Y_2^-2
  w_data= 0.0D0 ; if ( l_nm(2,-2) .ne. 0 ) w_data(l_nm(2,-2))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^-2 Test of w_xv(xv_data)',                       &
    answer = w_data,                                              &
    check = w_cleanup_w(w_xv(xv_data)),                           &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data))',                 &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2* 2*cos(2*xv_Lon)  ! Y_2^2
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=-1))',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=-1)',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*4*sin(xv_Lat)*cos(xv_Lat)**2*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0/24)*3.0*2*cos(xv_Lat)*sin(xv_Lat)*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(xv_Lat)*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=2))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=2)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^-2','Test of Y_2^-2 succeeded!') 

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

  !---- 通常変換 ----
  call AssertEqual(&
    message='general function of xv_w(w_xv(xv_data))',            &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_data  = cos(xv_Lat)
  xv_Ddata = -2*sin(xv_Lat)

  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data,iflag=1,ipow=1))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=1)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data  = cos(xv_Lat)**2
  xv_Ddata = -2*sin(xv_Lat)
  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data,iflag=1,ipow=2))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=2)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data  = sin(xv_Lat)
  xv_Ddata = cos(xv_Lat)**2

  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data),iflag=1,ipow=0)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=0),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data  = cos(xv_Lat)**2
  xv_Ddata = -2.0D0 * sin(xv_Lat)

  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data),iflag=1,ipow=2)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=2),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

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

  call MPI_FINALIZE(IERR)

contains

  function w_cleanup_w(w_data)
    real(8), intent(IN) :: w_data(nc)
    real(8)             :: w_cleanup_w(nc)

    integer l,nmary(2)

    w_cleanup_w = w_data

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

  end function w_cleanup_w
  
end program w_base_mpi_module_supack_test
