!= é‰›ç›´ÏƒåŠæ•´æ•°ãƒ¬ãƒ™ãƒ«æä¾›
!
!= Prepare vertical sigma half level
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: sigma_data.f90,v 1.2 2008/08/11 21:50:23 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module sigma_data
  !
  != é‰›ç›´ÏƒåŠæ•´æ•°ãƒ¬ãƒ™ãƒ«æä¾›
  !
  != Prepare vertical sigma half level
  !
  ! é‰›ç›´ÏƒåŠæ•´æ•°ãƒ¬ãƒ™ãƒ«ã®ã‚µãƒ³ãƒ—ãƒ«ãƒ‡ãƒ¼ã‚¿ã‚’æä¾›ã—ã¾ã™.
  !
  ! Prepare sample data of vertical sigma half level.
  !
  !== Procedures List
  !
  ! SigmaDataGetHalf   :: é‰›ç›´ÏƒåŠæ•´æ•°ãƒ¬ãƒ™ãƒ«å–å¾—.
  ! ------------  :: ------------
  ! SigmaDataGetHalf   :: Get vertical sigma half level.

  ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
  !

  ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
  ! Kind type parameter
  !
  use dc_types, only: DP     ! å€ç²¾åº¦å®Ÿæ•°åž‹. Double precision.

  ! ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸å‡ºåŠ›
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! å®£è¨€æ–‡ ; Declaration statements
  !
  implicit none
  private

  ! å…¬é–‹æ‰‹ç¶šã
  ! Public procedure
  !
  public:: SigmaDataGetHalf

  ! å…¬é–‹å¤‰æ•°
  ! Public variables
  !
  logical, save, public:: sigma_data_inited = .false.
                              ! åˆæœŸè¨å®šãƒ•ãƒ©ã‚°.
                              ! Initialization flag

  ! éžå…¬é–‹å¤‰æ•°
  ! Private variables
  !

  character(*), parameter:: module_name = 'sigma_data'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åç§°.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: sigma_data.f90,v 1.2 2008/08/11 21:50:23 morikawa Exp $'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³
                              ! Module version

  ! INTERFACE æ–‡ ; INTERFACE statements
  !
  interface SigmaDataGetHalf
    module procedure SigmaDataGetHalf
  end interface

contains

  subroutine SigmaDataGetHalf( r_Sigma &  ! (out)
    & )
    !
    ! é‰›ç›´ÏƒåŠæ•´æ•°ãƒ¬ãƒ™ãƒ«ã®ã‚µãƒ³ãƒ—ãƒ«ãƒ‡ãƒ¼ã‚¿ã‚’æä¾›ã—ã¾ã™.
    ! é‰›ç›´å±¤ã®æ•°ã¯ä¸ŽãˆãŸé…åˆ— r_Sigma ã®ãƒ‡ãƒ¼ã‚¿ã‚µã‚¤ã‚ºã‹ã‚‰
    ! è‡ªå‹•çš„ã«æ¤œå‡ºã•ã‚Œã¾ã™.
    !
    ! Prepare sample data of vertical sigma half level.
    ! Number of vertical level is detected automatically from
    ! size of a given array "r_Sigma".
    !

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none
    real(DP), intent(out):: r_Sigma (:)
                              ! $ \sigma $ ãƒ¬ãƒ™ãƒ« (åŠæ•´æ•°).
                              ! Half $ \sigma $ level

    ! ä½œæ¥å¤‰æ•°
    ! Work variables
    !
    integer:: kmax           ! é‰›ç›´å±¤æ•°.
                             ! Number of vertical level

    ! å®Ÿè¡Œæ–‡ ; Executable statement

    if ( .not. sigma_data_inited ) call SigmaDataInit

    ! kmax ã®è¨å®š (r_Sigma ã®ã‚µã‚¤ã‚ºã‹ã‚‰è¨å®š)
    ! Setting "kmax" (set from size of "r_Sigma")
    !
    kmax = size(r_Sigma) - 1
    if ( kmax < 1 ) &
      call MessageNotify('E', module_name, 'size of "r_Sigma" is too small. ' )

    ! r_Sigma (åŠæ•´æ•°ãƒ¬ãƒ™ãƒ«Ïƒ) ã®è‡ªå‹•è¨å®š
    ! Automation setting of "r_Sigma" (half level sigma)
    !
    select case (kmax)
    case (2)
      r_Sigma(1:kmax+1) = (/ &
        & 1.0_DP, 0.63_DP, 0.0_DP /)
    case (5)
      r_Sigma(1:kmax+1) = (/ &
        & 1.00_DP, 0.80_DP, 0.60_DP, 0.30_DP, 0.15_DP, &
        & 0.00_DP /)
    case (12)
      r_Sigma(1:kmax+1) = (/ &
        & 1.00_DP, 0.99_DP, 0.97_DP, 0.93_DP, 0.85_DP, &
        & 0.75_DP, 0.63_DP, 0.50_DP, 0.36_DP, 0.22_DP, &
        & 0.10_DP, 0.05_DP, 0.00_DP /)
    case (16)
      r_Sigma(1:kmax+1) = (/ &
        & 1.00_DP, 0.99_DP, 0.97_DP, 0.93_DP, 0.87_DP, &
        & 0.79_DP, 0.70_DP, 0.60_DP, 0.50_DP, 0.41_DP, &
        & 0.33_DP, 0.26_DP, 0.20_DP, 0.15_DP, 0.10_DP, &
        & 0.05_DP, 0.00_DP /)
    case (20)
      r_Sigma(1:kmax+1) = (/ &
        & 1.00_DP, 0.95_DP, 0.90_DP, 0.85_DP, 0.80_DP, &
        & 0.75_DP, 0.70_DP, 0.65_DP, 0.60_DP, 0.55_DP, &
        & 0.50_DP, 0.45_DP, 0.40_DP, 0.35_DP, 0.30_DP, &
        & 0.25_DP, 0.20_DP, 0.15_DP, 0.10_DP, 0.05_DP, &
        & 0.0_DP /)
    case (23)
      r_Sigma(1:kmax+1) = (/ &
        & 1.000_DP, 0.990_DP, 0.980_DP, 0.965_DP, 0.945_DP, &
        & 0.920_DP, 0.890_DP, 0.850_DP, 0.800_DP, 0.740_DP, &
        & 0.650_DP, 0.550_DP, 0.450_DP, 0.360_DP, 0.300_DP, &
        & 0.260_DP, 0.220_DP, 0.180_DP, 0.150_DP, 0.120_DP, &
        & 0.090_DP, 0.060_DP, 0.030_DP, 0.000_DP /)
    case (45)
      r_Sigma(1:kmax+1) = (/ &
        & 1.00000_DP, 0.99000_DP, 0.97000_DP, 0.93000_DP, 0.87000_DP, &
        & 0.79000_DP, 0.70000_DP, 0.60000_DP, 0.50000_DP, 0.41000_DP, &
        & 0.33000_DP, 0.26000_DP, 0.20000_DP, 0.15000_DP, 0.10000_DP, &
        & 0.08610_DP, 0.07410_DP, 0.06380_DP, 0.05490_DP, 0.04720_DP, &
        & 0.04070_DP, 0.03500_DP, 0.03010_DP, 0.02590_DP, 0.02230_DP, &
        & 0.01920_DP, 0.01650_DP, 0.01420_DP, 0.01220_DP, 0.01050_DP, &
        & 0.00907_DP, 0.00781_DP, 0.00672_DP, 0.00578_DP, 0.00498_DP, &
        & 0.00429_DP, 0.00369_DP, 0.00317_DP, 0.00273_DP, 0.00235_DP, &
        & 0.00202_DP, 0.00174_DP, 0.00150_DP, 0.00129_DP, 0.00111_DP, &
        & 0.00000_DP /)
    case default
      call MessageNotify( 'E', module_name, &
        & 'vertical sigma half level data auto-setting is valid ' // &
        & 'only when kmax=<%*d>. ( now kmax=<%d> )', &
        & i = (/2,5,12,16,20,23,45,kmax/), n = (/ 7 /) )
    end select

  end subroutine SigmaDataGetHalf

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

  subroutine SigmaDataInit

    implicit none

!!$  namelist /sigma_data_nml/

    ! å®Ÿè¡Œæ–‡ ; Executable statement

    if ( sigma_data_inited ) return
    call InitCheck

!!$    ! NAMELIST ã®èªã¿è¾¼ã¿
!!$    ! NAMELIST is input
!!$    !
!!$    if ( trim(namelist_filename) /= '' ) then
!!$      call FileOpen( unit_nml, &          ! (out)
!!$        & namelist_filename, mode = 'r' ) ! (in)
!!$
!!$      rewind( unit_nml )
!!$      read( unit_nml, &           ! (in)
!!$        & nml = sigma_data_nml, & ! (out)
!!$        & iostat = iostat_nml )   ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if

    ! å°å— ; Print
    !

    sigma_data_inited = .true.
  end subroutine SigmaDataInit

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

  subroutine InitCheck
    !
    ! ä¾å˜ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸåŒ–ãƒã‚§ãƒƒã‚¯
    !
    ! Check initialization of dependency modules

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! å®Ÿè¡Œæ–‡ ; Executable statement

  end subroutine InitCheck

end module sigma_data