!= spml wrapper module
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: spml.f90,v 1.2 2006/09/29 13:39:56 morikawa Exp $
! Tag Name::  $Name: dcpam3-20061118 $
! Copyright:: Copyright (C) GFD Dennou Club, 2004-2006. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!
module spml_mod
  !
  !== spml wrapper module
  !
  !このモジュールは
  !((<SPMODEL|URL:http://www.gfd-dennou.org/arch/spmodel>))の
  !spml ライブラリの
  !((<wa_module|URL:http://www.gfd-dennou.org/arch/spmodel/spml/doc/wa_module.htm>))
  !のラッパーです.
  !他のモジュールから SPMODEL のサブルーチンおよび関数を
  !呼ぶ場合には, 原則的にこのモジュールを介して呼び出します.
  !
  !This module is wrapper of
  !((<wa_module|URL:http://www.gfd-dennou.org/arch/spmodel/spml/doc/wa_module.htm>))
  !of spml library in ((<SPMODEL|URL:http://www.gfd-dennou.org/arch/spmodel>)).
  !
  !

  use wa_module, only: AvrLonLat_xy, l_nm, w_xy, wa_Div_xya_xya, &
    & x_Lon, x_Lon_Weight, y_Lat, y_Lat_Weight, wa_xya, xy_GradLat_w, &
    & xy_GradLon_w, xy_Lat, xy_w, xya_GradLat_wa, xya_GradLon_wa, xya_wa, &
    & w_Div_xy_xy, w_LaplaInv_w, IntLonLat_xy, wa_Lapla_wa, wa_LaplaInv_wa
!!$  use w_module, only : rn
  use w_module
  use type_mod, only : STRING
  implicit none
  private
  public :: spml_init, spml_end
  public :: AvrLonLat_xy, l_nm, rn, w_xy, wa_Div_xya_xya
  public :: x_Lon, x_Lon_Weight
  public :: y_Lat, y_Lat_Weight, wa_xya, xy_GradLat_w
  public :: xy_GradLon_w, xy_Lat, xy_w, xya_GradLat_wa, xya_GradLon_wa
  public :: xya_wa, w_Div_xy_xy, w_LaplaInv_w, IntLonLat_xy
  public :: wa_Lapla_wa, wa_LaplaInv_wa

  logical, private, save :: spml_initialized = .false.
  character(STRING),parameter:: version = &
       & '$Id: spml.f90,v 1.2 2006/09/29 13:39:56 morikawa Exp $'
  character(STRING),parameter:: tagname = '$Name: dcpam3-20061118 $'

contains
  subroutine spml_init
    !== Initialize wa_module
    !
    !((<wa_module|URL:http://www.gfd-dennou.org/arch/spmodel/spml/doc/wa_module.htm>))
    !の初期化関数
    !((<wa_Initial|URL:http://www.gfd-dennou.org/arch/spmodel/spml/doc/wa_module.htm#wa_Initial>))
    !を呼ぶ。
    !
    use type_mod           , only: INTKIND, STRING
    use grid_3d_mod        , only: im, jm, km, grid_3d_init
    use grid_wavenumber_mod, only: nm, grid_wavenumber_init
    use dc_trace,    only: DbgMessage, BeginSub, EndSub
    use dc_error,    only: StoreError, USR_ECHAR
    use wa_module,   only: wa_Initial
    implicit none

    !-------------------------------------------------------------------
    !   変数定義
    !-------------------------------------------------------------------
    character(STRING), parameter:: subname = "spml_init"

  continue
    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if (spml_initialized) then
       call EndSub( subname, '%c is already called', c1=trim(subname) )
       return
    else
       spml_initialized = .true.
    endif

    !----------------------------------------------------------------
    !   Version identifier
    !----------------------------------------------------------------
    call DbgMessage('%c :: %c', c1=trim(version), c2=trim(tagname))

    !----------------------------------------------------------------
    !   grid_3d_mod と grid_wavenumber による初期化
    !----------------------------------------------------------------
    call grid_3d_init
    call grid_wavenumber_init

    !----------------------------------------------------------------
    !   wa_initial による初期化
    !----------------------------------------------------------------
    call wa_Initial(nm,im,jm,km)

    call EndSub( subname, 'wa_initial(nm=<%d>, im=<%d>, jm=<%d>, km=<%d>)', &
         &       i=(/nm, im, jm, km/) )
  end subroutine spml_init


  subroutine spml_end
    !== Terminate module
    !
    !現状では wa_Initial を解除できないので、
    !何もしない。
    !wa_Initial による初期化を解除できるのなら、
    !((<spml_init>)) による初期化フラグをオフにすべきである。
    !
    use type_mod,  only: INTKIND, STRING
    use dc_trace,  only: DbgMessage, BeginSub, EndSub
    implicit none
    character(STRING), parameter:: subname = "spml_end"

    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if ( .not. spml_initialized) then
       call EndSub( subname, 'spml_init was not called', &
            &       c1=trim(subname) )
       return
    else
       ! 初期化を解除できないので .true. のまま
!       spml_initialized = .false.
       spml_initialized = .true.
    endif

    call EndSub( subname, 'spml_init can not release')
  end subroutine spml_end

end module spml_mod
