!= 配列の分割／拡張 テスト
!
!= Division and expansion of arrays
!
! Authors::   Shin-ichi Takehiro
! Version::
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2026. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!
program sltt_extarr_test

  use dc_test, only : AssertEqual
  
  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 倍精度実数型. Double precision.
    &                 STRING     ! 文字列.       Strings.

  ! モジュール引用 ; USE statements
  !
  ! MPI
  !
  use mpi_wrapper, only : MPIWrapperInit, MPIWrapperFinalize, myrank
  
  ! コマンドライン引数処理
  ! Command line option parser
  !
  use option_parser, only: OptParseInit

  ! NAMELIST ファイル入力に関するユーティリティ
  ! Utilities for NAMELIST file input
  !
  use namelist_util, only: NmlutilInit, NmlutilMsg

  ! 格子点設定
  ! Grid points settings
  !
#ifdef LIB_MPI
  use gridset, only:       &
    &         GridsetInit, &
    &                imax, & ! 経度格子点数.
                             ! Number of grid points in longitude
    &                jmax, & ! 緯度格子点数.
                             ! Number of grid points in latitude
    &                kmax, & ! 鉛直層数.
                             ! Number of vertical level
    &          imax_global   ! 経度格子点数 (全球).
                             ! Number of grid points in longitude on whole globe
  use ua_mpi_module_base, only : nproc_v, iproc_v, mpi_comm_v, nproc_h, iproc_h, mpi_comm_h, kc
#else
  use gridset, only:       &
    &         GridsetInit, &
    &                imax, & ! 経度格子点数.
                             ! Number of grid points in longitude
    &                jmax, & ! 緯度格子点数.
                             ! Number of grid points in latitude
    &                kmax, & ! 鉛直層数.
                             ! Number of vertical level
    &           kc => kmax,& ! 並列鉛直層数.
                             ! Number of vertical level for parallel compuation
    &          imax_global   ! 経度格子点数 (全球).
                             ! Number of grid points in longitude on whole globe
#endif
  
  use axesset    , only : AxesSetInit, x_Lon, y_Lat

  use composition, only:  CompositionInit, ncmax

  use sltt_const, only : SLTTConstInit, &
       & jexmin, jexmax, dtjw

  use sltt_extarr, only: SLTTExtArrInit, SLTTExtArrf
    


  use gtool_history
  
  implicit none
  
  real(DP), allocatable :: x_ExtLon    (:)
  real(DP), allocatable :: y_ExtLat    (:)


  real(DP), allocatable :: xyzf_Data    (:,:,:,:)
  real(DP), allocatable :: xyzf_ExtData (:,:,:,:)

  real(DP), allocatable :: xyzf_DataAll    (:,:,:,:)

  integer :: i, j, k, n
  integer :: is, ie, js, je
!  integer :: dtiw = 2
!  real(DP) :: PM = 1.0D0
  real(DP) :: PM = -1.0D0

  character(STRING)      :: namelist_filename
                            ! NAMELIST ファイルの名称. 
                            ! NAMELIST file name

  ! 実行文 ; Executable statement
  !
  ! Initialize MPI
  !
  call MPIWrapperInit
  
  ! コマンドライン引数処理
  ! Command line option parser
  !
  call OptParseInit(       &
       & namelist_filename,    & ! (out)
       & 'sltt_extarr_test'    & ! (in )
       & )

  ! NAMELIST ファイル名入力
  ! Input NAMELIST file name
  !
  call NmlutilInit( &
       & namelist_filename  & ! (in)
       & )

  call GridsetInit

  ! 組成に関わる配列の設定
  ! Settings of array for atmospheric composition
  !
  call CompositionInit

  ! 座標データ設定
  ! Axes data settings
  !
  call AxessetInit
  
  call SLTTConstInit
  
  allocate(x_ExtLon(0:imax_global-1))
  allocate(y_ExtLat(jexmin:jexmax))
  
  call SLTTExtArrInit(   &
    & x_Lon, y_Lat,            & ! (in )
    & x_ExtLon, y_ExtLat       & ! (out)
    & )

  write(6,*) 'myrank, x_ExtLon'
  write(6,*) myrank, x_ExtLon
  write(6,*) 'myrank, y_ExtLat'
  write(6,*) myrank, y_ExtLat
  
  allocate(xyzf_Data(0:imax-1, 1:jmax, 1:kmax, 1:ncmax))
  allocate(xyzf_ExtData (0:imax_global, jexmin:jexmax, 1:kc, 1:ncmax))
  
  do j=1,jmax
     do i=0,imax-1
        !        xyz_Data(i,j,:) = cos(x_Lon(i))*cos(y_Lat(j))
        xyzf_Data(i,j,:,:) = cos(x_Lon(i))*sin(y_Lat(j))
     end do
  end do

  call SLTTExtArrf(             &
    & xyzf_Data, -1.0D0,        & ! (in)
    & xyzf_ExtData              & ! (out)
    & )

  call output_gtool
  
  call MPIWrapperFinalize

contains

  !==================== ヒストリー出力 ========================
  subroutine output_gtool

    use dc_types, only : STRING
    use dc_string, only : StoA, Printf
    
    character(STRING) :: history_file
    real(DP) :: PI
    
    call Printf(unit=history_file, fmt="extarr_test_%02d.nc",i=(/myrank/))
    PI  = atan(1.0D0)*4.0D0
    
    call HistoryCreate( &                               ! ヒストリー作成
         file=trim(history_file), title='test of sltte_extarr',&
         source='sltt_extarr_test.f90 (2026/05/21)', &
         institution='GFD_Dennou Club davis/spmodel project',      &
         dims=(/'lon','lat','lev','nc '/), dimsizes=(/imax_global,jexmax-jexmin+1,kc,ncmax/), &
         longnames=StoA('longitude','latitude','level','composition'),  &
         units=StoA('1','1','1','1') )


    call HistoryPut('lon',x_Extlon*180.0/PI)          ! 変数出力
    call HistoryAddattr('lon','topology','circular')  ! 周期属性
    call HistoryAddattr('lon','modulo',360.0)         ! 周期属性

    call HistoryPut('lat',y_ExtLat*180.0/PI)          ! 変数出力

    call HistoryPut('lev',(/(k,k=1,kc)/))             ! 変数出力
    call HistoryPut('nc',(/(n,n=1,ncmax)/))           ! 変数出力

    call HistoryAddVariable( &                        ! 変数定義
         varname='var', dims=StoA('lon','lat','lev','nc '), &
         longname='variable', units='1', xtype='double')

    call HistoryPut('var',xyzf_ExtData)

    call HistoryClose
    
  end subroutine output_gtool
  
end program sltt_extarr_test
