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

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use ut_mpi_module_svpack
  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,jm)              :: xy_Data
  real(8), dimension(0:im-1)                 :: x_Data
  real(8), dimension(jm)                     :: y_Data
  real(8)                                    :: Data

  real(8), allocatable :: xyh_Data(:,:,:)
  real(8), allocatable :: xh_Data(:,:)
  real(8), allocatable :: yh_Data(:,:)
  real(8), allocatable :: h_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)

  !============================ 積分(xyz,xyh) ==============================

  call MessageNotify('M','ut_mpi_module_svpack_integral_test', &
       'ut_mpi_module_svpack integration functions tests')

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

  allocate(xyh_Data(0:im-1,1:jm,kc))
  allocate(xh_Data(0:im-1,kc))
  allocate(yh_Data(1:jm,kc))
  allocate(h_Data(kc))

 !---------- 定数 ----------
  xyh_Data = 1.0D0

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

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

  xy_Data = 1/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='xy_IntRad_xyh with constant field',                &
       answer = xy_Data,                                           &
       check = xy_IntRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

  y_Data = 2*pi/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='y_IntLonRad_xyh with constant field',              &
       answer = y_Data,                                            &
       check = y_IntLonRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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


  !------------------ r -----------------------
  xyh_Data = xyh_Rad 

  yh_Data = 2*pi* xyh_Rad(1,:,:)
  call AssertEqual(&
       message='yh_IntLon_xyh with r-field',                       &
       answer = yh_Data,                                           &
       check = yh_IntLon_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  xy_Data = 1/4.0D0*(ro**4-ri**4) 
  call AssertEqual(&
       message='xy_IntRad_xyh with r field',                       &
       answer = xy_Data,                                           &
       check = xy_IntRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = 4*pi * xyh_Data(1,1,:)
  call AssertEqual(&
       message='h_IntLonLat_xyh with r field',                     &
       answer = h_Data,                                            &
       check = h_IntLonLat_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 1/2.0D0*(ro**4-ri**4)
  call AssertEqual(&
       message='x_IntLatRad_xyh with r field',                     &
       answer = x_Data,                                            &
       check = x_IntLatRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  y_Data = pi/2.0D0*(ro**4-ri**4)
  call AssertEqual(&
       message='y_IntLonRad_xyh with constant field',              &
       answer = y_Data,                                            &
       check = y_IntLonRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  !--------- sin(phi)**2  ---------
  xyh_Data = sin(xyh_Lat)**2

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

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

  xy_Data = 1/3.0D0*(ro**3-ri**3) * xyh_Data(:,:,1)
  call AssertEqual(&
       message='xy_IntRad_xyh with sin^2φ field',                 &
       answer = xy_Data,                                           &
       check = xy_IntRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

  y_Data = 2*pi/3.0D0*(ro**3-ri**3) * xyh_Data(1,:,1)
  call AssertEqual(&
       message='y_IntLonRad_xyh with sin^2φ field',               &
       answer = y_Data,                                            &
       check = y_IntLonRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  ! sin(lambda)**2
  xyh_Data = sin(xyh_Lon)**2

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

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

  xy_Data = 1/3.0D0*(ro**3-ri**3) * xyh_Data(:,:,1)
  call AssertEqual(&
       message='xy_IntRad_xyh with sin(lambda)**2',                &
       answer = xy_Data,                                           &
       check = xy_IntRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

  y_Data = pi/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='y_IntLonRad_xyh with sin(lambda)**2',              &
       answer = y_Data,                                            &
       check = y_IntLonRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

 !============================ 積分(xy,xy) =============================
  ! 定数
  xy_Data = 1.0D0

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

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

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

  ! sin(lambda)**2
  xy_Data = sin(xy_Lon)**2

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

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

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

  ! sin(phi)**2 
  xy_Data = sin(xy_Lat)**2

  y_Data = 2*pi* xy_Data(1,:)
  call AssertEqual(&
       message='y_IntLon_xy with sin(phi)**2',                     &
       answer = y_Data,                                            &
       check = y_IntLon_xy(xy_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

 !============================ 積分(yz,yh) =============================
  ! 定数
  yh_Data = 1.0D0

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

  y_Data = 1/3.0D0*(ro**3-ri**3)
  call AssertEqual(&
       message='y_IntRad_yh with constant field',                  &
       answer = y_Data,                                            &
       check = y_IntRad_yh(yh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  ! r^2 
  yh_Data = xyh_Rad(0,:,:)**2

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

  y_Data = 1/5.0D0*(ro**5-ri**5)
  call AssertEqual(&
       message='y_IntRad_yh with r^2',                             &
       answer = y_Data,                                            &
       check = y_IntRad_yh(yh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  ! sin(phi)**2 
  yh_Data = sin(xyh_Lat(0,:,:))**2

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

  y_Data = 1/3.0D0*(ro**3-ri**3) * yh_Data(:,1)
  call AssertEqual(&
       message='y_IntRad_yh with sin(phi)**2',                     &
       answer = y_Data,                                            &
       check = y_IntRad_yh(yh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

 !============================ 積分(xh) =============================
  ! 定数
  xh_Data = 1.0D0

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

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

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

  ! sin(lambda)**2
  xh_Data = sin(xyh_Lon(:,1,:))**2

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

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

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

  ! r^2 
  xh_Data = spread(h_Rad**2,1,im)

  h_Data = 2*pi* h_Rad**2
  call AssertEqual(&
       message='h_IntLon_xh with r^2',                             &
       answer = h_Data,                                            &
       check = h_IntLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  call AssertEqual(&
       message='IntLonRad_xh with r^2',                            &
       answer = 2*pi/5.0D0*(ro**5-ri**5),                          &
       check = IntLonRad_xh(xh_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) =============================
  ! 定数
  y_Data = 1.0D0

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

  ! sin(phi)**2 
  y_Data = sin(y_Lat)**2

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

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

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

  ! r^2 
  h_Data = h_Rad**2

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

!============================ 平均(xyz,xyh) ==============================

 !---------- 定数 ----------
  xyh_Data = 1.0D0

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

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

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

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

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

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

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

  !------------------ r -----------------------
  xyh_Data = xyh_Rad 

  yh_Data = xyh_Rad(1,:,:)
  call AssertEqual(&
       message='yh_AvrLon_xyh with r-field',                &
       answer = yh_Data,                                           &
       check = yh_AvrLon_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xh_Data = xyh_Data(:,1,:)
  call AssertEqual(&
       message='xh_AvrLat_xyh with r-field',                &
       answer = xh_Data,                                           &
       check = xh_AvrLat_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xy_Data = 1.0D0/4.0D0*(ro**4-ri**4)/(1/3.0D0*(ro**3-ri**3))
  call AssertEqual(&
       message='xy_AvrRad_xyh with r field',                &
       answer = xy_Data,                                           &
       check = xy_AvrRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  h_Data = xyh_Data(1,1,:)
  call AssertEqual(&
       message='h_AvrLonLat_xyh with r field',                     &
       answer = h_Data,                                            &
       check = h_AvrLonLat_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  y_Data = pi/2.0D0*(ro**4-ri**4)/(2*pi/3.0D0*(ro**3-ri**3))
  call AssertEqual(&
       message='y_AvrLonRad_xyh with constant field',              &
       answer = y_Data,                                            &
       check = y_AvrLonRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  !--------- sin(phi)**2  ---------
  xyh_Data = sin(xyh_Lat)**2

  yh_Data = xyh_Data(1,:,:)
  call AssertEqual(&
       message='yh_AvrLon_xyh with sin^2φ field',                 &
       answer = yh_Data,                                           &
       check = yh_AvrLon_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  xy_Data = xyh_Data(:,:,1)
  call AssertEqual(&
       message='xy_AvrRad_xyh with sin^2φ field',                 &
       answer = xy_Data,                                           &
       check = xy_AvrRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

  y_Data = xyh_Data(1,:,1)
  call AssertEqual(&
       message='y_AvrLonRad_xyh with sin^2φ field',               &
       answer = y_Data,                                            &
       check = y_AvrLonRad_xyh(xyh_Data),                          &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

  ! sin(lambda)**2
  xyh_Data = sin(xyh_Lon)**2

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

  xh_Data = xyh_Data(:,1,:)
  call AssertEqual(&
       message='xh_AvrLat_xyh with sin(lambda)**2',                &
       answer = xh_Data,                                           &
       check = xh_AvrLat_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  xy_Data = xyh_Data(:,:,1)
  call AssertEqual(&
       message='xy_AvrRad_xyh with sin(lambda)**2',                &
       answer = xy_Data,                                           &
       check = xy_AvrRad_xyh(xyh_Data),                            &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

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

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

 !============================ 平均(xy,xy) =============================
  ! 定数
  xy_Data = 1.0D0

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

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

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

  ! sin(lambda)**2
  xy_Data = sin(xy_Lon)**2

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

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

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

  ! sin(phi)**2 
  xy_Data = sin(xy_Lat)**2

  y_Data = xy_Data(1,:)
  call AssertEqual(&
       message='y_AvrLon_xy with sin(phi)**2',                     &
       answer = y_Data,                                            &
       check = y_AvrLon_xy(xy_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

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

 !============================ 平均(yz,yh) =============================
  ! 定数
  yh_Data = 1.0D0

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

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

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

  ! r^2 
  yh_Data = xyh_Rad(0,:,:)**2

  h_Data = yh_Data(1,:)
  call AssertEqual(&
       message='h_AvrLat_yh with r^2',                             &
       answer = h_Data,                                            &
       check = h_AvrLat_yh(yh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  y_Data = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3)
  call AssertEqual(&
       message='y_AvrRad_yh with r^2',                             &
       answer = y_Data,                                            &
       check = y_AvrRad_yh(yh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLatRad_yh with r^2',                            &
       answer = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3),           &
       check = AvrLatRad_yh(yh_Data),                              &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  ! sin(phi)**2 
  yh_Data = sin(xyh_Lat(0,:,:))**2

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

  y_Data = yh_Data(:,1)
  call AssertEqual(&
       message='y_AvrRad_yh with sin(phi)**2',                     &
       answer = y_Data,                                            &
       check = y_AvrRad_yh(yh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

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

 !============================ 平均(xh) =============================
  ! 定数
  xh_Data = 1.0D0

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

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

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

  ! sin(lambda)**2
  xh_Data = sin(xyh_Lon(:,1,:))**2

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

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

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

  ! r^2 
  xh_Data = spread(h_Rad**2,1,im)

  h_Data = h_Rad**2
  call AssertEqual(&
       message='h_AvrLon_xh with r^2',                             &
       answer = h_Data,                                            &
       check = h_AvrLon_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  x_Data = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3)
  call AssertEqual(&
       message='x_AvrRad_xh with r^2',                             &
       answer = x_Data,                                            &
       check = x_AvrRad_xh(xh_Data),                               &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call AssertEqual(&
       message='AvrLonRad_xh with r^2',                            &
       answer = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3),           &
       check = AvrLonRad_xh(xh_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) =============================
  ! 定数
  y_Data = 1.0D0

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

  ! sin(phi)**2 
  y_Data = sin(y_Lat)**2

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

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

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

  ! r^2 
  h_Data = h_Rad**2

  call AssertEqual(&
       message='AvrRad_r with r^2',                                &
       answer = 3.0D0/5.0D0*(ro**5-ri**5)/(ro**3-ri**3),           &
       check = AvrRad_h(h_Data),                                   &
       significant_digits = check_digits, ignore_digits = ignore   &
       )

  call MessageNotify('M','ut_mpi_module_svpack_integral_test', &
       'ut_mpi_module_svpack integration/averaging function tests succeeded!')

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

  call MPI_FINALIZE(IERR)
  
end program ut_mpi_module_svpack_integral_test
