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

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use wsc_mpi_module_supack
  use mpi

  implicit none

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

  real(8), dimension(0:im-1,0:km)            :: xz_Data
  real(8), dimension(0:im-1)                 :: x_Data
  real(8), dimension(0:km)                   :: z_Data
  real(8)                                    :: Data

  real(8), allocatable :: xvz_Data(:,:,:)

  real(8), allocatable :: xv_Data(:,:)
  real(8), allocatable :: vz_Data(:,:)
  real(8), allocatable :: v_Data(:)

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

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

  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','wsc_mpi_module_supack_integral_test', &
       'wsc_mpi_module_supack integration functions tests')

  call wsc_mpi_Initial(im,jm,km,nm,lm,ri,ro)

  allocate(xvz_Data(0:im-1,1:jc,0:km))

  allocate(xv_Data(0:im-1,1:jc))
  allocate(vz_Data(1:jc,0:km))
  allocate(v_Data(1:jc))
  
  !---------- 定数 ----------
  xvz_Data = 1.0D0

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

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

  xv_Data = ro**2*(ro-ri)
  call AssertEqual(&
       message='xv_IntRad_xvz with constant field',                &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  x_Data = 2*ro**2*(ro-ri)
  call AssertEqual(&
       message='x_IntLatRad_xvz with constant field',              &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi*ro**2*(ro-ri)
  call AssertEqual(&
       message='v_IntLonRad_xvz with constant field',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 4*pi*ro**2*(ro-ri)
  call AssertEqual(&
       message='IntLonLatRad_xvz with constant field',             &
       answer = Data,                                              &
       check = IntLonLatRad_xvz(xvz_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !------------------ r -----------------------
  xvz_Data = xvz_Rad 

  vz_Data = 2*pi* xvz_Rad(1,:,:)
  call AssertEqual(&
       message='vz_IntLon_xvz with r-field',                       &
       answer = vz_Data,                                           &
       check = vz_IntLon_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xz_Data = 2.0D0 * xvz_Data(:,1,:)
  call AssertEqual(&
       message='xz_IntLat_xvz with r-field',                       &
       answer = xz_Data,                                           &
       check = xz_IntLat_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1/2.0D0*ro**2*(ro**2-ri**2)
  call AssertEqual(&
       message='xv_IntRad_xvz with r field',                       &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  z_Data = 4*pi * xvz_Data(1,1,:)
  call AssertEqual(&
       message='z_IntLonLat_xvz with r field',                     &
       answer = z_Data,                                            &
       check = z_IntLonLat_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = ro**2*(ro**2-ri**2)
  call AssertEqual(&
       message='x_IntLatRad_xvz with r field',                     &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi*ro**2*(ro**2-ri**2)
  call AssertEqual(&
       message='v_IntLonRad_xvz with r field',                     &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 2*pi*ro**2*(ro**2-ri**2)
  call AssertEqual(&
       message='IntLonLatRad_xvz with r field',                   &
       answer = Data,                                             &
       check = IntLonLatRad_xvz(xvz_Data),                        &
       significant_digits = check_digits, ignore_digits = ignore  &
       )

  !--------- sin(phi)**2  ---------
  xvz_Data = sin(xvz_Lat)**2

  vz_Data = 2*pi* xvz_Data(1,:,:)
  call AssertEqual(&
       message='vz_IntLon_xvz with sin^2φ field',                 &
       answer = vz_Data,                                           &
       check = vz_IntLon_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xz_Data = 2.0D0/3.0D0
  call AssertEqual(&
       message='xz_IntLat_xvz with sin^2φ field',                 &
       answer = xz_Data,                                           &
       check = xz_IntLat_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = ro**2*(ro-ri) * xvz_Data(:,:,1)
  call AssertEqual(&
       message='xv_IntRad_xvz with sin^2φ field',                 &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  z_Data = 4*pi/3.0
  call AssertEqual(&
       message='z_IntLonLat_xvz with sin^2φ field',               &
       answer = z_Data,                                            &
       check = z_IntLonLat_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 2/3.0D0*ro**2*(ro-ri)
  call AssertEqual(&
       message='x_IntLatRad_xvz with sin^2φ field',               &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 2*pi*ro**2*(ro-ri) * xvz_Data(1,:,1)
  call AssertEqual(&
       message='v_IntLonRad_xvz with sin^2φ field',               &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 4*pi/3*ro**2*(ro-ri)
  call AssertEqual(&
       message='IntLonLatRad_xvz with constant field',             &
       answer = Data,                                              &
       check = IntLonLatRad_xvz(xvz_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xvz_Data = sin(xvz_Lon)**2

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

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

  xv_Data = ro**2*(ro-ri) * xvz_Data(:,:,1)
  call AssertEqual(&
       message='xv_IntRad_xvz with sin(lambda)**2',                &
       answer = xv_Data,                                           &
       check = xv_IntRad_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  x_Data = 2.0D0*ro**2*(ro-ri) * xvz_Data(:,1,1)
  call AssertEqual(&
       message='x_IntLatRad_xvz with sin(lambda)**2',              &
       answer = x_Data,                                            &
       check = x_IntLatRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = pi* ro**2*(ro-ri)
  call AssertEqual(&
       message='v_IntLonRad_xvz with sin(lambda)**2',              &
       answer = v_Data,                                            &
       check = v_IntLonRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonLatRad_xvz with sin(lambda)**2',             &
       answer = 2*pi*ro**2*(ro-ri),                                &
       check = IntLonLatRad_xvz(xvz_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   &
       )

 !============================ 積分(vz) =============================
  ! 定数
  vz_Data = 1.0D0

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

  v_Data = ro**2*(ro-ri)
  call AssertEqual(&
       message='v_IntRad_vz with constant field',                  &
       answer = v_Data,                                            &
       check = v_IntRad_vz(vz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vz with constant field',                 &
       answer = 2*ro**2*(ro-ri),                                   &
       check = IntLatRad_vz(vz_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin^2(z)
  vz_Data = sin(PI*(xvz_Rad(0,:,:)-ri)/(ro-ri))**2
!  vz_Data = xvz_Rad(0,:,:)**2

  z_Data = 2.0D0 * vz_Data(1,:)
  call AssertEqual(&
       message='z_IntLat_vz with sin^2(z)',                        &
       answer = z_Data,                                            &
       check = z_IntLat_vz(vz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1/2.0D0*(ro-ri)*ro**2
  call AssertEqual(&
       message='v_IntRad_vz with sin^2(z)',                        &
       answer = v_Data,                                            &
       check = v_IntRad_vz(vz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vz with sin^2(z)',                       &
       answer = ro**2*(ro-ri),                                     &
       check = IntLatRad_vz(vz_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  vz_Data = sin(xvz_Lat(0,:,:))**2

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

  v_Data = ro**2*(ro-ri) * vz_Data(:,1)
  call AssertEqual(&
       message='v_IntRad_vz with sin(phi)**2',                     &
       answer = v_Data,                                            &
       check = v_IntRad_vz(vz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLatRad_vz with sin(phi)**2',                    &
       answer = 2/3.0D0*ro**2*(ro-ri),                             &
       check = IntLatRad_vz(vz_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

 !============================ 積分(xz) =============================
  ! 定数
  xz_Data = 1.0D0

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

  x_Data = ro**2*(ro-ri)
  call AssertEqual(&
       message='x_IntRad_xz with constant field',                  &
       answer = x_Data,                                            &
       check = x_IntRad_xz(xz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xz with constant field',                 &
       answer = 2*pi*ro**2*(ro-ri),                                &
       check = IntLonRad_xz(xz_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xz_Data = sin(xvz_Lon(:,1,:))**2

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

  x_Data = ro**2*(ro-ri) * xz_Data(:,1)
  call AssertEqual(&
       message='x_IntRad_xz with sin(lambda)**2',                  &
       answer = x_Data,                                            &
       check = x_IntRad_xz(xz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xz with sin(lambda)**2',                 &
       answer = pi*ro**2*(ro-ri),                                  &
       check = IntLonRad_xz(xz_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! cos^2(z) 
  xz_Data = spread(cos(PI*(z_Rad-ri)/(ro-ri))**2,1,im)

  z_Data = 2*pi* xz_Data(1,:)
  call AssertEqual(&
       message='z_IntLon_xz with cos^2(z)',                        &
       answer = z_Data,                                            &
       check = z_IntLon_xz(xz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/2.0D0*ro**2*(ro-ri)
  call AssertEqual(&
       message='x_IntRad_xz with cos^2(z)',                        &
       answer = x_Data,                                            &
       check = x_IntRad_xz(xz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='IntLonRad_xz with cos^2(z)',                       &
       answer = pi*ro**2*(ro-ri),                                  &
       check = IntLonRad_xz(xz_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_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   &
       )

 !============================ 積分(z) =============================
  ! 定数
  z_Data = 1.0D0

  call AssertEqual(&
       message='IntRad_r with constant field',                     &
       answer = ro**2*(ro-ri),                                     &
       check = IntRad_z(z_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin^2(z)
  z_Data = sin(PI*(z_Rad-ri)/(ro-ri))**2

  call AssertEqual(&
       message='IntRad_r with sin^2(z)',                           &
       answer = 1/2.0D0*ro**2*(ro-ri),                             &
       check = IntRad_z(z_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

!============================ 平均(xvz) ==============================

 !---------- 定数 ----------
  xvz_Data = 1.0D0

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

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

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

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

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

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

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

  !------------------ r -----------------------
  xvz_Data = xvz_Rad 

  vz_Data = xvz_Rad(1,:,:)
  call AssertEqual(&
       message='vz_AvrLon_xvz with r-field',                &
       answer = vz_Data,                                           &
       check = vz_AvrLon_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xz_Data = xvz_Data(:,1,:)
  call AssertEqual(&
       message='xz_AvrLat_xvz with r-field',                &
       answer = xz_Data,                                           &
       check = xz_AvrLat_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = 1.0D0/2.0D0*ro**2*(ro**2-ri**2)/(ro**2*(ro-ri))
  call AssertEqual(&
       message='xv_AvrRad_xvz with r field',                &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  z_Data = xvz_Data(1,1,:)
  call AssertEqual(&
       message='z_AvrLonLat_xvz with r field',                     &
       answer = z_Data,                                            &
       check = z_AvrLonLat_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/2.0D0*ro**2*(ro**2-ri**2)/(ro**2*(ro-ri))
  call AssertEqual(&
       message='x_AvrLatRad_xvz with r field',                     &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = PI*ro**2*(ro**2-ri**2)/(2*PI*ro**2*(ro-ri))
  call AssertEqual(&
       message='v_AvrLonRad_xvz with r field',                     &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 2.0*PI*ro**2*(ro**2-ri**2)/(4*PI*(ro**2*(ro-ri)))
  call AssertEqual(&
       message='AvrLonLatRad_xvz with r field',                    &
       answer = Data,                                              &
       check = AvrLonLatRad_xvz(xvz_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  !--------- sin(phi)**2  ---------
  xvz_Data = sin(xvz_Lat)**2

  vz_Data = xvz_Data(1,:,:)
  call AssertEqual(&
       message='vz_AvrLon_xvz with sin^2φ field',                 &
       answer = vz_Data,                                           &
       check = vz_AvrLon_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xz_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='xz_AvrLat_xvz with sin^2φ field',                 &
       answer = xz_Data,                                           &
       check = xz_AvrLat_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xv_Data = xvz_Data(:,:,1)
  call AssertEqual(&
       message='xv_AvrRad_xvz with sin^2φ field',                 &
       answer = xv_Data,                                           &
       check = xv_AvrRad_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  z_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='z_AvrLonLat_xvz with sin^2φ field',               &
       answer = z_Data,                                            &
       check = z_AvrLonLat_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/3.0D0
  call AssertEqual(&
       message='x_AvrLatRad_xvz with sin^2φ field',               &
       answer = x_Data,                                            &
       check = x_AvrLatRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = xvz_Data(1,:,1)
  call AssertEqual(&
       message='v_AvrLonRad_xvz with sin^2φ field',               &
       answer = v_Data,                                            &
       check = v_AvrLonRad_xvz(xvz_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  Data = 1.0D0/3D0
  call AssertEqual(&
       message='AvrLonLatRad_xvz with constant field',             &
       answer = Data,                                              &
       check = AvrLonLatRad_xvz(xvz_Data),                         &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(lambda)**2
  xvz_Data = sin(xvz_Lon)**2

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

  xz_Data = xvz_Data(:,1,:)
  call AssertEqual(&
       message='xz_AvrLat_xvz with sin(lambda)**2',                &
       answer = xz_Data,                                           &
       check = xz_AvrLat_xvz(xvz_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

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

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

  call AssertEqual(&
       message='AvrLonLatRad_xvz with sin(lambda)**2',             &
       answer = 0.5D0,                                             &
       check = AvrLonLatRad_xvz(xvz_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   &
       )

 !============================ 平均(vz) =============================
  ! 定数
  vz_Data = 1.0D0

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

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

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

  ! r^2 
  vz_Data = sin(PI*(xvz_Rad(0,:,:)-ri)/(ro-ri))**2

  z_Data = vz_Data(1,:)
  call AssertEqual(&
       message='z_AvrLat_vz with sin^2(z)',                        &
       answer = z_Data,                                            &
       check = z_AvrLat_vz(vz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  v_Data = 1.0D0/2.0D0*ro**2*(ro-ri)/(ro**2*(ro-ri))
  call AssertEqual(&
       message='v_AvrRad_vz with sin^2(z)',                        &
       answer = v_Data,                                            &
       check = v_AvrRad_vz(vz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_vz with sin^2(z)',                       &
       answer = 1.0D0/2.0D0*ro**2*(ro-ri)/(ro**2*(ro-ri)),         &
       check = AvrLatRad_vz(vz_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  vz_Data = sin(xvz_Lat(0,:,:))**2

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

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

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

 !============================ 平均(xz) =============================
  ! 定数
  xz_Data = 1.0D0

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

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

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

  ! sin(lambda)**2
  xz_Data = sin(xvz_Lon(:,1,:))**2

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

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

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

  ! cos^2(z)
  xz_Data = spread(cos(PI*(z_Rad-ri)/(ro-ri))**2,1,im)

  z_Data = xz_Data(1,:)
  call AssertEqual(&
       message='z_AvrLon_xz with cos^2(z)',                        &
       answer = z_Data,                                            &
       check = z_AvrLon_xz(xz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1.0D0/2.0D0*ro**2*(ro-ri)/(ro**2*(ro-ri))
  call AssertEqual(&
       message='x_AvrRad_xz with cos^2(z)',                        &
       answer = x_Data,                                            &
       check = x_AvrRad_xz(xz_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xz with cos^2(z)',                       &
       answer = 1.0D0/2.0D0*ro**2*(ro-ri)/(ro**2*(ro-ri)),         &
       check = AvrLonRad_xz(xz_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_Data = 1.0D0

  xz_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   &
       )

 !============================ 平均(z) =============================
  ! 定数
  z_Data = 1.0D0

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

  ! cos^2(z) 
  z_Data = cos(PI*(z_Rad-ri)/(ro-ri))**2

  call AssertEqual(&
       message='AvrRad_r with cos^2(z)',                           &
       answer = 1.0D0/2.0D0*ro**2*(ro-ri)/(ro**2*(ro-ri)),         &
       check = AvrRad_z(z_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  call MPI_FINALIZE(IERR)
  
end program wsc_mpi_module_supack_integral_test

