!= Module TOPOGRAPHY
!
! Authors::   $BGr_@M}2V(B
! Version::   $Id: topography.f90, 2021/12/08 $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2014. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]

module Topography
  !
  ! $BCO7A$K4XO"$9$kNL$N7W;;(B
  ! $B%b%8%e!<%kFI$_9~$_(B
  use dc_types,   only : DP
  use dc_message, only : MessageNotify

  !$B0EL[$N7?@k8@6X;_(B
  implicit none

  !$BB0@-$N;XDj(B
  private

  !$BJQ?t$NDj5A(B
  character(*), parameter:: module_name = 'topography'
                                           ! $B%b%8%e!<%k$NL>>N(B.
                                           ! Module name
  real(DP), save, private :: MountHeight               = 1.0d+3
                                           ! $B;33YGH7W;;MQ(B $B;3$N9b$5(B
  real(DP), save, private :: MountRange                = 1.0d+3
                                           ! $B;33YGH7W;;MQ(B $B;3$NH>CMI}(B
  real(DP), save, public, allocatable  :: xyr_M1(:,:,:)
  real(DP), save, public, allocatable  :: xyr_M3(:,:,:)
  real(DP), save, public, allocatable  :: xy_ZSfc(:,:) 
  
  ! public 
  !
  public Topography_Init

contains

!!!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  subroutine Topography_Init
    
    !
    ! M1 $B$H(B M3 $B$r7W;;$9$k$?$a$N%5%V%k!<%A%s(B
    !

    ! $B%b%8%e!<%kFI$_9~$_(B
    !
    use dc_types,      only : DP
    use dc_iounit,     only : FileOpen
    use namelist_util, only : namelist_filename
    use gridset,   only : imin, imax, jmin, jmax, kmin, kmax   !$BG[Ns%5%$%:(B
    use axesset,   only : x_X, r_Z, Xmax, Xmin, Zmax
    
    
    ! $B0EL[$N7?@k8@6X;_(B
    !
    implicit none

    ! $BJQ?t$NDj5A(B
    !
    integer  :: unit                   !$BAuCVHV9f(B

    integer  :: i, j, k 
    real(DP) :: xy_DZSfcDx(imin:imax,jmin:jmax)

    !-------------------------------------------------------------------
    ! Namelist $B$+$i>pJs$r<hF@$9$k(B
    !

    NAMELIST /LeeWave_nml/ &
      & MountHeight, MountRange
        
    call FileOpen(unit, file=namelist_filename, mode='r')
    read(unit, NML=leewave_nml)
    close(unit)


    call MessageNotify( "M", module_name, "MountHeight = %f", d=(/MountHeight/) )
    call MessageNotify( "M", module_name, "MountRange = %f", d=(/MountRange/) )

    !-------------------------------------------------------------------

    allocate( xyr_M1(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xyr_M3(imin:imax,jmin:jmax,kmin:kmax) )
    allocate( xy_ZSfc(imin:imax,jmin:jmax) )
    
    !$BCO7A$K4X78$9$k$H$3$m(B
    do j = jmin, jmax
      do i = imin, imax
        xy_ZSfc(i,j) = ( MountHeight * MountRange**2 )/( MountRange**2 + ( x_X(i) - ( (Xmax + Xmin)/2 ) )**2 )
        xy_DZSfcDx(i,j) = ( - 2 * MountHeight * MountRange**2 * ( x_X(i) - ( (Xmax + Xmin)/2 ) ) )   &
          & /( MountRange**2 + (x_X(i) - ( (Xmax + Xmin)/2 ) )**2 )**2
      end do
    end do

    !$BCO7A$O4X78$J$$$H$3$m(B
    do k = kmin, kmax
      do j = jmin, jmax
        do i = imin, imax
          xyr_M1(i,j,k) = -( ( Zmax - r_Z(k) )/Zmax )*xy_DZSfcDx(i,j)
          xyr_M3(i,j,k) =  ( Zmax - xy_ZSfc(i,j) )/Zmax
        end do
      end do
    end do
     
  end subroutine Topography_Init
!
!!!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
end module Topography

