!----------------------------------------------------------------------
!     Copyright (c) 2016 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  wtq_mpi_module_supack テストプログラム
!      積分・平均計算
!
!履歴  2016/04/03  竹広真一  wq_mpi_module_supack_integral_test.f90 より改造
!
program wtq_mpi_module_supack_integral

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

  integer,parameter  :: im=32, jm=16         ! 格子点の設定(経度, 緯度, 動径)
  integer,parameter  :: kmo=16, kmi=10       ! 格子点の設定(球殻動径, 球動径)
  integer,parameter  :: nm=10                ! 切断波数の設定(水平)
  integer,parameter  :: lmo=10, lmi=16       ! 切断波数の設定(球殻動径, 球動径)
  real(8),parameter  :: ri=0.5, ro=1.5       ! 内外半径

   real(8), dimension(0:im-1,kmi)          :: xr_Data
  real(8), dimension(0:im-1)               :: x_Data
  real(8), dimension(kmi)                  :: r_Data

  real(8), allocatable :: xvr_Data(:,:,:)
  real(8), allocatable :: xv_Data(:,:)
  real(8), allocatable :: vr_Data(:,:)
  real(8), allocatable :: v_Data(:)

  real(8), parameter  :: pi=3.1415926535897932385D0

  ! 判定誤差設定
  integer, parameter :: check_digits = 4
  integer, parameter :: ignore = -5

  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)

  call MessageNotify('M','wtq_mpi_module_supack_integral_test', &
       'wtq_mpi_module_supack integration functions tests')

  call wtq_mpi_Initial(im,jm,kmi,kmo,nm,lmi,lmo,ri,ro)
 
  allocate(xvr_Data(0:im-1,1:jc,kmi))
  allocate(xv_Data(0:im-1,1:jc))
  allocate(vr_Data(1:jc,kmi))
  allocate(v_Data(1:jc))

 !============================ 積分(xvr) =============================
 ! 定数
  xvr_Data = 1.0D0

  vr_Data = 2*pi
  call AssertEqual(&
       message='vr_IntLon_xvr with constant field',                &
       answer = vr_Data,                                           &
       check = vr_IntLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = 2.0D0
  call AssertEqual(&
       message='xr_IntLat_xvr with constant field',                &
       answer = xr_Data,                                           &
       check = xr_IntLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/3.0D0*ri**3
  call AssertEqual(&
       message='xv_IntRad_xvr with constant field',                &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = 4*pi
  call AssertEqual(&
       message='r_IntLonLat_xvr with constant field',              &
       answer = r_Data,                                            &
       check = r_IntLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2/3.0D0*ri**3
  call AssertEqual(&
       message='x_IntLatRad_xvr with constant field',              &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi/3.0D0*ri**3
  call AssertEqual(&
       message='v_IntLonRad_xvr with constant field',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLatRad_xvr with constant field',             &
       answer = 4*pi/3*ri**3,                                      &
       check = IntLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 ! r^2 
  xvr_Data = xvr_Rad**2

  vr_Data = 2*pi* xvr_Rad(1,:,:)**2
  call AssertEqual(&
       message='vr_IntLon_xvr with r^2',                           &
       answer = vr_Data,                                           &
       check = vr_IntLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = 2.0D0 * xvr_Data(:,1,:)
  call AssertEqual(&
       message='xr_IntLat_xvr with r^2',                           &
       answer = xr_Data,                                           &
       check = xr_IntLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/5.0D0*ri**5
  call AssertEqual(&
       message='xv_IntRad_xvr with r^2',                           &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = 4*pi * xvr_Data(1,1,:)
  call AssertEqual(&
       message='r_IntLonLat_xvr with r^2',                         &
       answer = r_Data,                                            &
       check = r_IntLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2/5.0D0*ri**5
  call AssertEqual(&
       message='x_IntLatRad_xvr with r^2',                         &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi/5.0D0*ri**5
  call AssertEqual(&
       message='v_IntLonRad_xvr with r^2',                         &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLatRad_xvr with r^2',                        &
       answer = 4*pi/5*ri**5,                                      &
       check = IntLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  xvr_Data = sin(xvr_Lat)**2

  vr_Data = 2*pi* xvr_Data(1,:,:)
  call AssertEqual(&
       message='vr_IntLon_xvr with sin(phi)**2',                   &
       answer = vr_Data,                                           &
       check = vr_IntLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='xr_IntLat_xvr with sin(phi)**2',                   &
       answer = xr_Data,                                           &
       check = xr_IntLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/3.0D0*ri**3 * xvr_Data(:,:,1)
  call AssertEqual(&
       message='xv_IntRad_xvr with sin(phi)**2',                   &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = 4*pi/3.0
  call AssertEqual(&
       message='r_IntLonLat_xvr with sin(phi)**2',                 &
       answer = r_Data,                                            &
       check = r_IntLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2/9.0D0*ri**3
  call AssertEqual(&
       message='x_IntLatRad_xvr with sin(phi)**2',                 &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi/3.0D0*ri**3 * xvr_Data(1,:,1)
  call AssertEqual(&
       message='v_IntLonRad_xvr with sin(phi)**2',                 &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLatRad_xvr with sin(phi)**2',                &
       answer = 4*pi/9*ri**3,                                      &
       check = IntLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xvr_Data = sin(xvr_Lon)**2

  vr_Data = pi
  call AssertEqual(&
       message='vr_IntLon_xvr with sin(lambda)**2',                &
       answer = vr_Data,                                           &
       check = vr_IntLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = 2.0D0 * xvr_Data(:,1,:)
  call AssertEqual(&
       message='xr_IntLat_xvr with sin(lambda)**2',                &
       answer = xr_Data,                                           &
       check = xr_IntLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/3.0D0*ri**3 * xvr_Data(:,:,1)
  call AssertEqual(&
       message='xv_IntRad_xvr with sin(lambda)**2',                &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = 2*pi
  call AssertEqual(&
       message='r_IntLonLat_xvr with sin(lambda)**2',              &
       answer = r_Data,                                            &
       check = r_IntLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0D0/3.0D0*ri**3 * xvr_Data(:,1,1)
  call AssertEqual(&
       message='x_IntLatRad_xvr with sin(lambda)**2',              &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi/3.0D0*ri**3
  call AssertEqual(&
       message='v_IntLonRad_xvr with sin(lambda)**2',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLatRad_xvr with sin(lambda)**2',             &
       answer = 2*pi/3*ri**3,                                      &
       check = IntLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(xv) =============================
  ! 定数
  xv_Data = 1.0D0

  v_Data = 2*pi
  call AssertEqual(&
       message='v_IntLon_xv with constant field',                  &
       answer = v_Data,                                            &
       check = v_IntLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0D0
  call AssertEqual(&
       message='x_IntLat_xv with constant field',                  &
       answer = x_Data,                                            &
       check = x_IntLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLat_xv with constant field',                 &
       answer = 4*pi,                                              &
       check = IntLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xv_Data = sin(xv_Lon)**2

  v_Data = pi
  call AssertEqual(&
       message='v_IntLon_xv with sin(lambda)**2',                  &
       answer = v_Data,                                            &
       check = v_IntLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0d0 * xv_Data(:,1)
  call AssertEqual(&
       message='x_IntLat_xv with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_IntLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLat_xv with sin(lambda)**2',                 &
       answer = 2*pi,                                              &
       check = IntLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  xv_Data = sin(xv_Lat)**2

  v_Data = 2*pi* xv_Data(1,:)
  call AssertEqual(&
       message='v_IntLon_xv with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_IntLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='x_IntLat_xv with sin(phi)**2',                     &
       answer = x_Data,                                            &
       check = x_IntLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLat_xv with sin(phi)**2',                    &
       answer = 4*pi/3.0,                                          &
       check = IntLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(yr,vr) =============================
  ! 定数
  vr_Data = 1.0D0

  r_Data = 2.0D0
  call AssertEqual(&
       message='r_IntLat_vr with constant field',                  &
       answer = r_Data,                                            &
       check = r_IntLat_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/3.0D0*ri**3
  call AssertEqual(&
       message='v_IntRad_vr with constant field',                  &
       answer = v_Data,                                            &
       check = v_IntRad_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vr with constant field',                 &
       answer = 2/3.0D0*ri**3,                                     &
       check = IntLatRad_vr(vr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2 
  vr_Data = xvr_Rad(0,:,:)**2

  r_Data = 2.0D0 * vr_Data(1,:)
  call AssertEqual(&
       message='r_IntLat_vr with r^2',                             &
       answer = r_Data,                                            &
       check = r_IntLat_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/5.0D0*ri**5
  call AssertEqual(&
       message='v_IntRad_vr with r^2',                             &
       answer = v_Data,                                            &
       check = v_IntRad_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vr with r^2',                            &
       answer = 2/5.0D0*ri**5,                                     &
       check = IntLatRad_vr(vr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  vr_Data = sin(xvr_Lat(0,:,:))**2

  r_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='r_IntLat_vr with sin(phi)**2',                     &
       answer = r_Data,                                            &
       check = r_IntLat_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/3.0D0*ri**3 * vr_Data(:,1)
  call AssertEqual(&
       message='v_IntRad_vr with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_IntRad_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vr with sin(phi)**2',                    &
       answer = 2/9.0D0*ri**3,                                     &
       check = IntLatRad_vr(vr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(xr) =============================
  ! 定数
  xr_Data = 1.0D0

  r_Data = 2*pi
  call AssertEqual(&
       message='r_IntLon_xr with constant field',                  &
       answer = r_Data,                                            &
       check = r_IntLon_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/3.0D0*ri**3
  call AssertEqual(&
       message='x_IntRad_xr with constant field',                  &
       answer = x_Data,                                            &
       check = x_IntRad_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xr with constant field',                 &
       answer = 2*pi/3.0D0*ri**3,                                  &
       check = IntLonRad_xr(xr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xr_Data = sin(xvr_Lon(:,1,:))**2

  r_Data = pi
  call AssertEqual(&
       message='r_IntLon_xr with sin(lambda)**2',                  &
       answer = r_Data,                                            &
       check = r_IntLon_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/3.0D0*ri**3 * xr_Data(:,1)
  call AssertEqual(&
       message='x_IntRad_xr with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_IntRad_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xr with sin(lambda)**2',                 &
       answer = pi/3.0D0*ri**3 ,                                   &
       check = IntLonRad_xr(xr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2 
  xr_Data = spread(r_Rad**2,1,im)

  r_Data = 2*pi* r_Rad**2
  call AssertEqual(&
       message='r_IntLon_xr with r^2',                             &
       answer = r_Data,                                            &
       check = r_IntLon_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/5.0D0*ri**5
  call AssertEqual(&
       message='x_IntRad_xr with r^2',                             &
       answer = x_Data,                                            &
       check = x_IntRad_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xr with r^2',                            &
       answer = 2*pi/5.0D0*ri**5,                                  &
       check = IntLonRad_xr(xr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(x) =============================
  ! 定数
  x_Data = 1.0D0

  call AssertEqual(&
       message='IntLon_x with constant field',                     &
       answer = 2*pi,                                              &
       check = IntLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  x_Data = sin(x_Lon)**2

  call AssertEqual(&
       message='IntLon_x with sin(lambda)**2',                     &
       answer = pi,                                                &
       check = IntLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(y,v) =============================
  ! 定数
  v_Data = 1.0D0

  call AssertEqual(&
       message='IntLat_v with constant field',                     &
       answer = 2.0D0,                                             &
       check = IntLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  v_Data = sin(v_Lat)**2

  call AssertEqual(&
       message='IntLat_v with sin(phi)**2',                        &
       answer = 2.0D0/3.0D0,                                       &
       check = IntLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(r) =============================
  ! 定数
  r_Data = 1.0D0

  call AssertEqual(&
       message='IntRad_r with constant field',                     &
       answer = 1/3.0D0*ri**3,                                     &
       check = IntRad_r(r_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2 
  r_Data = r_Rad**2

  call AssertEqual(&
       message='IntRad_r with r^2',                                &
       answer = 1/5.0D0*ri**5,                                     &
       check = IntRad_r(r_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !=========================== 平均(xvr) =============================

  call MessageNotify('M','wtq_mpi_module_supack_integral_test', &
       'wtq_mpi_module_supack averaging functions tests')

 ! 定数
  xvr_Data = 1.0

  vr_Data = 1.0D0
  call AssertEqual(&
       message='vr_AvrLon_xvr with constant field',                &
       answer = vr_Data,                                           &
       check = vr_AvrLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = 1.0D0
  call AssertEqual(&
       message='xr_AvrLat_xvr with constant field',                &
       answer = xr_Data,                                           &
       check = xr_AvrLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1.0D0
  call AssertEqual(&
       message='xv_AvrRad_xvr with constant field',                &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = r_AvrLonLat_xvr(xvr_Data)
  call AssertEqual(&
       message='r_AvrLonLat_xvr with constant field',              &
       answer = r_Data,                                            &
       check = r_AvrLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0
  call AssertEqual(&
       message='x_AvrLatRad_xvr with constant field',              &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1.0D0
  call AssertEqual(&
       message='v_AvrLonRad_xvr with constant field',              &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLatRad_xvr with constant field',             &
       answer = 1.0D0,                                             &
       check = AvrLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 ! r 
  xvr_Data = xvr_Rad 

  vr_Data = xvr_Data(1,:,:)
  call AssertEqual(&
       message='vr_AvrLon_xvr with r',                             &
       answer = vr_Data,                                           &
       check = vr_AvrLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = xvr_Data(:,1,:)
  call AssertEqual(&
       message='xr_AvrLat_xvr with r',                             &
       answer = xr_Data,                                           &
       check = xr_AvrLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1.0D0/4.0D0*ri**4/(1.0D0/3.0D0*ri**3)
  call AssertEqual(&
       message='xv_AvrRad_xvr with r',                             &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = xvr_Data(1,1,:)
  call AssertEqual(&
       message='r_AvrLonLat_xvr with r',                           &
       answer = r_Data,                                            &
       check = r_AvrLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/2.0D0*ri**4/(2/3.0D0*ri**3)
  call AssertEqual(&
       message='x_AvrLatRad_xvr with r',                           &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi/2.0D0*ri**4/(2*pi/3.0D0*ri**3)
  call AssertEqual(&
       message='v_AvrLonRad_xvr with r',                           &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLatRad_xvr with r',                          &
       answer = pi*ri**4/(4*pi/3*ri**3),                           &
       check = AvrLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2
  xvr_Data = sin(xvr_Lat)**2

  vr_Data = xvr_Data(1,:,:)
  call AssertEqual(&
       message='vr_AvrLon_xvr with sin(phi)**2',                   &
       answer = vr_Data,                                           &
       check = vr_AvrLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='xr_AvrLat_xvr with sin(phi)**2',                   &
       answer = xr_Data,                                           &
       check = xr_AvrLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = xvr_Data(:,:,1)
  call AssertEqual(&
       message='xv_AvrRad_xvr with sin(phi)**2',                   &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='r_AvrLonLat_xvr with sin(phi)**2',                 &
       answer = r_Data,                                            &
       check = r_AvrLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='x_AvrLatRad_xvr with sin(phi)**2',                 &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = xvr_Data(1,:,1)
  call AssertEqual(&
       message='v_AvrLonRad_xvr with sin(phi)**2',                 &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLatRad_xvr with sin(phi)**2',                &
       answer = 1.0D0/3D0,                                         &
       check = AvrLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xvr_Data = sin(xvr_Lon)**2

  vr_Data = 0.5D0
  call AssertEqual(&
       message='vr_AvrLon_xvr with sin(lambda)**2',                &
       answer = vr_Data,                                           &
       check = vr_AvrLon_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xr_Data = xvr_Data(:,1,:)
  call AssertEqual(&
       message='xr_AvrLat_xvr with sin(lambda)**2',                &
       answer = xr_Data,                                           &
       check = xr_AvrLat_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = xvr_Data(:,:,1)
  call AssertEqual(&
       message='xv_AvrRad_xvr with sin(lambda)**2',                &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvr(xvr_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  r_Data = 0.5D0
  call AssertEqual(&
       message='r_AvrLonLat_xvr with sin(lambda)**2',              &
       answer = r_Data,                                            &
       check = r_AvrLonLat_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = xvr_Data(:,1,1)
  call AssertEqual(&
       message='x_AvrLatRad_xvr with sin(lambda)**2',              &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 0.5D0
  call AssertEqual(&
       message='v_AvrLonRad_xvr with sin(lambda)**2',              &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvr(xvr_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLatRad_xvr with sin(lambda)**2',             &
       answer = 0.5D0,                                             &
       check = AvrLonLatRad_xvr(xvr_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(xv) =============================
  ! 定数
  xv_Data = 1.0D0

  v_Data = 1.0D0
  call AssertEqual(&
       message='v_AvrLon_xv with constant field',                  &
       answer = v_Data,                                            &
       check = v_AvrLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0
  call AssertEqual(&
       message='x_AvrLat_xv with constant field',                  &
       answer = x_Data,                                            &
       check = x_AvrLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLat_xv with constant field',                 &
       answer = 1.0D0,                                             &
       check = AvrLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xv_Data = sin(xv_Lon)**2

  v_Data = 0.5D0
  call AssertEqual(&
       message='v_AvrLon_xv with sin(lambda)**2',                  &
       answer = v_Data,                                            &
       check = v_AvrLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = xv_Data(:,1)
  call AssertEqual(&
       message='x_AvrLat_xv with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_AvrLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLat_xv with sin(lambda)**2',                 &
       answer = 0.5D0,                                             &
       check = AvrLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  xv_Data = sin(xv_Lat)**2

  v_Data = xv_Data(1,:)
  call AssertEqual(&
       message='v_AvrLon_xv with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_AvrLon_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='x_AvrLat_xv with sin(phi)**2',                     &
       answer = x_Data,                                            &
       check = x_AvrLat_xv(xv_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonLat_xv with sin(phi)**2',                    &
       answer = 1.0D0/3.0D0,                                       &
       check = AvrLonLat_xv(xv_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(yr,vr) =============================
  ! 定数
  vr_Data = 1.0D0

  r_Data = 1.0D0
  call AssertEqual(&
       message='r_AvrLat_vr with constant field',                  &
       answer = r_Data,                                            &
       check = r_AvrLat_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1.0D0
  call AssertEqual(&
       message='v_AvrRad_vr with constant field',                  &
       answer = v_Data,                                            &
       check = v_AvrRad_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vr with constant field',                 &
       answer = 1.0D0,                                             &
       check = AvrLatRad_vr(vr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2 
  vr_Data = xvr_Rad(0,:,:)**2

  r_Data = vr_Data(1,:)
  call AssertEqual(&
       message='r_AvrLat_vr with r^2',                             &
       answer = r_Data,                                            &
       check = r_AvrLat_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 3.0D0/5.0D0*ri**2
  call AssertEqual(&
       message='v_AvrRad_vr with r^2',                             &
       answer = v_Data,                                            &
       check = v_AvrRad_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vr with r^2',                            &
       answer = 3.0D0/5.0D0*ri**2,                                 &
       check = AvrLatRad_vr(vr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  vr_Data = sin(xvr_Lat(0,:,:))**2

  r_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='r_AvrLat_vr with sin(phi)**2',                     &
       answer = r_Data,                                            &
       check = r_AvrLat_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = vr_Data(:,1)
  call AssertEqual(&
       message='v_AvrRad_vr with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_AvrRad_vr(vr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vr with sin(phi)**2',                    &
       answer = 1.0D0/3.0D0,                                       &
       check = AvrLatRad_vr(vr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(xr) =============================
  ! 定数
  xr_Data = 1.0D0

  r_Data = 1.0D0
  call AssertEqual(&
       message='r_AvrLon_xr with constant field',                  &
       answer = r_Data,                                            &
       check = r_AvrLon_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0
  call AssertEqual(&
       message='x_AvrRad_xr with constant field',                  &
       answer = x_Data,                                            &
       check = x_AvrRad_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xr with constant field',                 &
       answer = 1.0D0,                                             &
       check = AvrLonRad_xr(xr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xr_Data = sin(xvr_Lon(:,1,:))**2

  r_Data = 0.5D0
  call AssertEqual(&
       message='r_AvrLon_xr with sin(lambda)**2',                  &
       answer = r_Data,                                            &
       check = r_AvrLon_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = xr_Data(:,1)
  call AssertEqual(&
       message='x_AvrRad_xr with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_AvrRad_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xr with sin(lambda)**2',                 &
       answer = 0.5D0,                                             &
       check = AvrLonRad_xr(xr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2 
  xr_Data = spread(r_Rad**2,1,im)

  r_Data = r_Rad**2
  call AssertEqual(&
       message='r_AvrLon_xr with r^2',                             &
       answer = r_Data,                                            &
       check = r_AvrLon_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 3.0D0/5.0D0*ri**2
  call AssertEqual(&
       message='x_AvrRad_xr with r^2',                             &
       answer = x_Data,                                            &
       check = x_AvrRad_xr(xr_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xr with r^2',                            &
       answer = 3.0D0/5.0D0*ri**2,                                 &
       check = AvrLonRad_xr(xr_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(x) =============================
  ! 定数
  x_Data = 1.0D0

  call AssertEqual(&
       message='AvrLon_x with constant field',                     &
       answer = 1.0D0,                                             &
       check = AvrLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  x_Data = sin(x_Lon)**2

  call AssertEqual(&
       message='AvrLon_x with sin(lambda)**2',                     &
       answer = 0.5D0,                                             &
       check = AvrLon_x(x_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(y,v) =============================
  ! 定数
  v_Data = 1.0D0

  call AssertEqual(&
       message='AvrLat_v with constant field',                     &
       answer = 1.0D0,                                             &
       check = AvrLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  v_Data = sin(v_Lat)**2

  call AssertEqual(&
       message='AvrLat_v with sin(phi)**2',                        &
       answer = 1.0D0/3.0D0,                                       &
       check = AvrLat_v(v_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 平均(r) =============================
  ! 定数
  r_Data = 1.0D0

  call AssertEqual(&
       message='AvrRad_r with constant field',                     &
       answer = 1.0D0,                                             &
       check = AvrRad_r(r_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! r^2 
  r_Data = r_Rad**2

  call AssertEqual(&
       message='AvrRad_r with r^2',                                &
       answer = 3.0D0/5.0D0*ri**2,                                 &
       check = AvrRad_r(r_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call MessageNotify('M','wtq_mpi_module_supack_integral_test', &
       'wtq_mpi_module_supack integration/averaging function tests succeeded!')

  call MPI_FINALIZE(IERR)

end program wtq_mpi_module_supack_integral
