!----------------------------------------------------------------------
!     Copyright (c) 2025 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_integrale テストプログラム :: 積分・平均関数のテスト
!
!履歴  2025/12/15  竹広真一
!
program wa_zonal_module_integral_test

  use dc_message, only : MessageNotify
  use dc_types, only: DP
  use dc_test, only : AssertEqual
  use wa_zonal_module
  implicit none

  integer, parameter :: im=1, jm=64, nm=42, km=2

  real(DP), dimension(0:im-1,1:jm,km) ::  xya_data    ! 元の関数
  real(DP), dimension(0:im-1,km)      ::  xa_idata    ! 積分・平均の正解
  real(DP), dimension(1:jm,km)        ::  ya_idata    ! 積分・平均の正解
  real(DP), dimension(km)             ::  a_idata     ! 積分・平均の正解

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

  real(DP) :: pi

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','wa_zonal_module_integral_test', &
    & 'wa_zonal_module_integral function tests')

  call wa_Initial( nm, im, jm, km )

  !---- 積分のテスト ----
  xya_data(:,:,1) = sin(xy_Lat)**2
  xya_data(:,:,2) = sin(xy_Lat)**4

  a_idata(1) = 4*pi/3       ! a_IntLonLat_xya
  a_idata(2) = 4*pi/5       ! a_IntLonLat_xya

  call AssertEqual(                                               &
    message ='a_IntLonLat_xya',                                   &
    answer  = a_IntLonLat_xya(xya_data),                          &
    check   = a_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ya_idata(:,1) = 2*pi*sin(y_Lat)**2       ! y_IntLon_xya
  ya_idata(:,2) = 2*pi*sin(y_Lat)**4       ! y_IntLon_xya
  call AssertEqual(&
    message='ya_IntLon_xya',                                      &
    answer = ya_IntLon_xya(xya_data),                             &
    check =  ya_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_IntLat_ya',                                        &
    answer = a_IntLat_ya(ya_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xa_idata(:,1) = 2.0D0/3       ! x_IntLat_xya
  xa_idata(:,2) = 2.0D0/5       ! x_IntLat_xya

  call AssertEqual(&
    message='xa_IntLat_xya',                                      &
    answer = xa_IntLat_xya(xya_data),                             &
    check =  xa_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_IntLon_xa',                                        &
    answer = a_IntLon_xa(xa_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- 平均のテスト ----
  xya_data(:,:,1) = sin(xy_Lat)**2
  xya_data(:,:,2) = sin(xy_Lat)**4

  a_idata(1) = 1.0D0/3       ! AvrLonLat_xya
  a_idata(2) = 1.0D0/5       ! AvrLonLat_xya
  call AssertEqual(&
    message='a_AvrLonLat_xya',                                    &
    answer = a_AvrLonLat_xya(xya_data),                           &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  ya_idata(:,1) = sin(y_Lat)**2       ! y_AvrLon_xya
  ya_idata(:,2) = sin(y_Lat)**4       ! y_AvrLon_xya
  call AssertEqual(&
    message='ya_AvrLon_xya',                                      &
    answer = ya_AvrLon_xya(xya_data),                             &
    check =  ya_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_AvrLat_ya',                                        &
    answer = a_AvrLat_ya(ya_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xa_idata(:,1) = 1.0D0/3       ! x_AvrLat_xya
  xa_idata(:,2) = 1.0D0/5       ! x_AvrLat_xya

  call AssertEqual(&
    message='xa_AvrLat_xya',                                      &
    answer = xa_AvrLat_xya(xya_data),                             &
    check =  xa_idata,                                            &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='a_AvrLon_xa',                                        &
    answer = a_AvrLon_xa(xa_idata),                               &
    check =  a_idata,                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','wa_zonal_module_integral_test', &
    & 'wa_zonal_module_integral function tests succeded!')

end program wa_zonal_module_integral_test
