!= σ座標値生成モジュール
!
!= Sigma coordinate data generation module
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: sigma_data.f90,v 1.4 2008-02-27 07:35:45 morikawa Exp $
! Tag Name::  $Name: dcpam4-20080427 $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!

module sigma_data
  !
  != σ座標値生成モジュール
  !
  != Sigma coordinate data generation module
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! σ座標値データを生成します.
  !
  ! Sigma coordinate data is generated.
  !
  !== Procedures List
  !
  ! Create        :: SIGDAT 型変数の初期設定
  ! Get           :: σ座標データの取得
  ! Close         :: SIGDAT 型変数の終了処理
  ! PutLine       :: SIGDAT 型変数に格納されている情報の印字
  ! initialized   :: SIGDAT 型変数が初期設定されているか否か
  ! ------------  :: ------------
  ! Create        :: Constructor of "SIGDAT"
  ! Get           :: Get sigma coordinate data
  ! Close         :: Deconstructor of "SIGDAT"
  ! PutLine       :: Print information of "SIGDAT"
  ! initialized   :: Check initialization of "SIGDAT"
  !
  !== Usage
  !
  ! 始めに, SIGDAT 型の変数を定義し, Create で初期設定を行います.
  ! 初期設定後, Get によってσ座標値初期値を読み込んでください.
  ! SIGDAT 型の変数の終了処理には Close を用いてください.
  !
  ! First, initialize "SIGDAT" by "Create".
  ! Then, get sigma coordinate data by "Get".
  ! In order to terminate "SIGDAT", use "Close".
  !

  use dc_types, only: DP, TOKEN
  implicit none
  private
  public:: SIGDAT, Create, Get, Close, PutLine, initialized

  type SIGDAT
    !
    ! まず, Create で "SIGDAT" 型の変数を初期設定して下さい.
    ! 初期設定された "SIGDAT" 型の変数を再度利用する際には,
    ! Close によって終了処理を行ってください.
    !
    ! Initialize "SIGDAT" variable by "Create" before usage.
    ! If you reuse "SIGDAT" variable again for another application, 
    ! terminate by "Close".
    !
    logical:: initialized = .false.     ! 初期設定フラグ. 
                                        ! Initialization flag
    character(TOKEN):: pattern
                              ! 初期値データのパターン. 
                              ! Initial data pattern
    integer:: kmax ! 鉛直層数. 
                   ! Number of vertical level
    real(DP):: Cp         ! $ C_p $ .    大気定圧比熱.   Specific heat of air at constant pressure
    real(DP):: RAir       ! $ R $ .      大気気体定数.   Gas constant of air
    real(DP):: Kappa      ! $ \kappa = R/C_p $ .
                          ! 気体定数の定圧比熱に対する比. Ratio of gas constant to specific heat
    real(DP), pointer:: z_Sigma (:) =>null()
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP), pointer:: r_Sigma (:) =>null()
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
    real(DP), pointer:: z_DelSigma (:) =>null()
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)
  end type SIGDAT

  character(*), parameter:: version = &
    & '$Name: dcpam4-20080427 $' // &
    & '$Id: sigma_data.f90,v 1.4 2008-02-27 07:35:45 morikawa Exp $'

  interface Create
    module procedure SigmaDataCreate
  end interface

  interface Close
    module procedure SigmaDataClose
  end interface

  interface PutLine
    module procedure SigmaDataPutLine
  end interface

  interface initialized
    module procedure SigmaDataInitialized
  end interface

  interface NmlRead
    module procedure SigmaDataNmlRead
  end interface

  interface Get
    module procedure SigmaDataGet
  end interface

!!$  interface Sample
!!$    module procedure SigmaDataSample
!!$  end interface

contains

  subroutine SigmaDataCreate( sig_dat, &
    & kmax, &
    & Cp, RAir, &
    & r_SigmaSet, &
    & pattern, &
    & nmlfile, err )
    !
    ! SIGDAT 型の変数の初期設定を行います.
    ! 他のサブルーチンを使用する前に必ずこのサブルーチンによって
    ! SIGDAT 型の変数を初期設定してください.
    !
    ! 引数 *Cp* および *RAir* は半整数 $ \sigma $ 
    ! レベルから整数 $ \sigma $ レベルを作成する際に使用されます. 
    !
    ! なお, 与えられた *sig_dat* が既に初期設定されている場合,
    ! プログラムはエラーを発生させます.
    !
    ! NAMELIST を利用する場合には引数 *nmlfile* に NAMELIST ファイル名
    ! を与えてください. NAMELIST 変数群の詳細に関しては 
    ! NAMELIST#sigma_data_nml を参照してください. 
    !
    ! Constructor of "SIGDAT".
    ! Initialize *sig_dat* by this subroutine, 
    ! before other procedures are used, 
    !
    ! Arguments *Cp* and *RAir* are used when 
    ! full $ \sigma $ level is created from half $ \sigma $ level. 
    !
    ! Note that if *sig_dat* is already initialized 
    ! by this procedure, error is occurred.
    !
    ! In order to use NAMELIST, specify a NAMELIST filename to 
    ! argument *nmlfile*. See "NAMELIST#sigma_data_nml"
    ! for details about a NAMELIST group.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_present, only: present_and_not_empty, present_and_true
    use dc_message, only: MessageNotify
    use dcpam_error, only: StoreError, DCPAM_EBADPATTERN, DCPAM_EBADNUMBER, &
      & DCPAM_EARGSIZEMISMATCH
    use dc_error, only: DC_NOERR, DC_EALREADYINIT, &
      & DC_EARGLACK, DC_ENEGATIVE, DC_ENOFILEREAD
    use dc_string, only: LChar
    implicit none
    type(SIGDAT), intent(inout):: sig_dat
    integer, intent(in):: kmax ! 鉛直層数. 
                               ! Number of vertical level
    real(DP), intent(in):: Cp         ! $ C_p $ .    大気定圧比熱.   Specific heat of air at constant pressure
    real(DP), intent(in):: RAir       ! $ R $ .      大気気体定数.   Gas constant of air
    real(DP), intent(in), optional:: r_SigmaSet (:)
                              ! デフォルトでは, kmax の値に応じ,
                              ! 自動的に $ \sigma $ レベル (半整数) 
                              ! は設定される (kmax がある特定の値のみ).
                              ! $ \sigma $ レベル (半整数). 
                              ! を明示的に設定する必要がある場合, 
                              ! この引数を与える.
                              !
                              ! By default, half $ \sigma $ level is 
                              ! specified automatically according to
                              ! the value of kmax (only the value is 
                              ! same as particular values).
                              ! If the half $ \sigma $ level is specified
                              ! manually, give this argument.
    character(*), intent(in), optional:: pattern
                              ! 地表面データのパターン. 
                              ! Surface data pattern
    character(*), intent(in), optional :: nmlfile
                              ! NAMELIST ファイルの名称. 
                              ! この引数に空文字以外を与えた場合, 
                              ! 指定されたファイルから 
                              ! NAMELIST 変数群を読み込みます. 
                              ! ファイルを読み込めない場合にはエラーを
                              ! 生じます.
                              !
                              ! NAMELIST 変数群の詳細に関しては 
                              ! NAMELIST#sigma_data_nml 
                              ! を参照してください. 
                              !
                              ! NAMELIST file name. 
                              ! If nonnull character is specified to
                              ! this argument, 
                              ! NAMELIST group name is loaded from the 
                              ! file. 
                              ! If the file can not be read, 
                              ! an error occurs.
                              ! 
                              ! See "NAMELIST#sigma_data_nml" 
                              ! for details about a NAMELIST group.
                              ! 
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    real(DP):: Kappa      ! $ \kappa = R/C_p $ .
                          ! 気体定数の定圧比熱に対する比. Ratio of gas constant to specific heat
    real(DP):: z_Sigma (0:kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP):: r_Sigma (0:kmax)
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
    real(DP):: z_DelSigma (0:kmax-1)
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)

    integer:: k               ! DO ループ用作業変数
                              ! Work variables for DO loop
    integer:: size_r_SigmaSet
    real(DP), parameter:: invalid_value_r_Sigma = -999.0_DP
                              ! 無効な r_Sigma の値. 
                              ! Invalid value of 'r_Sigma'
    integer:: insufficient_num_r_Sigma
                              ! NAMELIST ファイルから得られた r_Sigma の
                              ! 不足分データの数. 
                              ! Number of insufficient data of 'r_Sigma'
                              ! loaded from NAMELIST file
    logical:: nmlfile_valid   ! NAMELIST ファイルからの値の妥当性. 
                              ! Validity of values from NAMELIST file
    integer:: stat
    character(STRING):: cause_c
    integer:: cause_i
    character(*), parameter:: subname = 'SigmaDataCreate'
  continue
    call BeginSub( subname, version )
    stat = DC_NOERR
    cause_c = ''
    cause_i = 0

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( sig_dat % initialized ) then
      stat = DC_EALREADYINIT
      cause_c = 'SIGDAT'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  引数の正当性のチェック
    !  Validate arguments
    !-----------------------------------------------------------------
    if (kmax < 1) then
      stat = DC_ENEGATIVE
      cause_c = 'kmax'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  格子点の設定
    !  Configure grid point
    !-----------------------------------------------------------------
    sig_dat % kmax = kmax

    !-----------------------------------------------------------------
    !  物理定数の設定
    !  Configure physical constants
    !-----------------------------------------------------------------
    Kappa = RAir / Cp
    sig_dat % Kappa = Kappa

    !-----------------------------------------------------------------
    !  σ座標値データのパターン
    !  Sigma coordinate data pattern
    !-----------------------------------------------------------------
    if ( .not. present_and_not_empty(pattern) ) then
      sig_dat % pattern = 'default'
    else
      select case ( LChar( trim(sig_dat % pattern) ) )
      case ('default')
      case default
        stat = DCPAM_EBADPATTERN
        cause_c = trim(pattern)
        goto 999
      end select
    end if

    !-----------------------------------------------------------------
    !  半整数σレベルデータの作成
    !  Generate half sigma level data
    !-----------------------------------------------------------------

    !-------------------------
    !  デフォルト値
    !  Default values
    r_Sigma(0:kmax) = invalid_value_r_Sigma

    !-------------------------
    !  NAMELIST からの値
    !  Values from NAMELIST
    nmlfile_valid = .false.
    if ( present_and_not_empty(nmlfile) ) then
      call MessageNotify( 'M', subname, &
        & 'Loading NAMELIST file "%c" ...', &
        & c1=trim(nmlfile) )
      call NmlRead ( nmlfile = nmlfile, & ! (in)
        & r_Sigma_ = r_Sigma, &           ! (inout)
        & err = err )                     ! (out)
      if ( present_and_true(err) ) then
        call MessageNotify( 'W', subname, &
          & '"%c" can not be read.', &
          & c1=trim(nmlfile) )
        stat = DC_ENOFILEREAD
        cause_c = nmlfile
        goto 999
      end if

      if ( any( r_Sigma < 0.0_DP ) ) then
        insufficient_num_r_Sigma = count( r_Sigma < 0.0_DP )
        call MessageNotify( 'W', subname, &
          & 'Number of data "%c" from "%c" is "%d" (insufficient). ' // &
          & 'Number of grids must be "%d".', &
          & c1='r_Sigma', c2=trim(nmlfile), &
          & i=(/kmax + 1 - insufficient_num_r_Sigma, kmax + 1/) )
      else
        nmlfile_valid = .true.
        call MessageNotify( 'M', subname, &
          & '"%c" is loaded from "%c".', &
          & c1='r_Sigma', c2=trim(nmlfile) )
        call MessageNotify( 'M', subname, &
          & '%c(%d:%d) = %*f', &
          & c1='r_Sigma', i=(/0, kmax/), d=r_Sigma, n=(/kmax+1/) )
      end if
    end if

    if ( nmlfile_valid ) then
    !-------------------------
    !  オプショナル引数からの値
    !  Values from optional arguments
    elseif (present(r_SigmaSet)) then
      size_r_SigmaSet = size(r_SigmaSet)
      if (.not. size_r_SigmaSet == kmax + 1) then
        call MessageNotify('W', subname, &
          & 'Size of argument r_SigmaSet (%d) is mismatch to kmax + 1 (%d). ' // &
          & 'Modify the size of r_SigmaSet to kmax + 1 (%d).', &
          & i=(/size_r_SigmaSet, kmax + 1, kmax + 1/))
        stat = DCPAM_EARGSIZEMISMATCH
        cause_c = 'r_SigmaSet'
        goto 999
      end if
      r_Sigma(0:kmax) = r_SigmaSet

    !-------------------------
    !  自動設定
    !  Automatic setting
    else
      select case ( LChar( trim(sig_dat % pattern) ) )
      case ('default')
        select case (kmax)
        case (2)
          r_Sigma(0:kmax) = (/ &
            & 1.0_DP, 0.63_DP, 0.0_DP /)
        case (5)
          r_Sigma(0:kmax) = (/ &
            & 1.00_DP, 0.80_DP, 0.60_DP, 0.30_DP, 0.15_DP, &
            & 0.00_DP /)
        case (12)
          r_Sigma(0:kmax) = (/ &
            & 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(0:kmax) = (/ &
            & 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(0:kmax) = (/ &
            & 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(0:kmax) = (/ &
            & 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(0:kmax) = (/ &
            & 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('W', subname, &
            & '%c = %*d is only valid. ', &
            & i=(/2,5,12,16,20,23,45/), n=(/4/), c1='kmax' )
          stat = DCPAM_EBADNUMBER
          cause_c = 'kmax'
          cause_i = kmax
          goto 999
        end select
      case default
        stat = DCPAM_EBADPATTERN
        cause_c = trim(pattern)
        goto 999
      end select
    end if

    allocate( sig_dat % r_Sigma(0:kmax) )
    sig_dat % r_Sigma(0:kmax) = r_Sigma(0:kmax)

    !-----------------------------------------------------------------
    !  整数σレベルデータの作成
    !  Generate full sigma level data
    !-----------------------------------------------------------------
    do k = 0, kmax - 1
      z_DelSigma(k) = r_Sigma(k) - r_Sigma(k+1)
    enddo
    allocate( sig_dat % z_DelSigma(0:kmax-1) )
    sig_dat % z_DelSigma = z_DelSigma

    do k = 0, kmax - 1
      z_Sigma(k) = &
        & ( &
        &   ( r_Sigma(k) ** ( 1.0_DP + Kappa ) &
        &     - r_Sigma(k+1) ** ( 1.0_DP + Kappa ) ) &
        &   / ( z_DelSigma(k) *  ( 1.0_DP + Kappa ) ) &
        & ) ** ( 1.0_DP / Kappa )
    enddo

    allocate( sig_dat % z_Sigma(0:kmax-1) )
    sig_dat % z_Sigma = z_Sigma

    !-----------------------------------------------------------------
    !  設定値の正当性のチェック
    !  Validate setting values
    !-----------------------------------------------------------------
!!$    if ( sig_dat % param_i < 0 ) then
!!$      stat = DC_ENEGATIVE
!!$      cause_c = 'param_i'
!!$      goto 999
!!$    end if


    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
    sig_dat % initialized = .true.
999 continue
    call StoreError( stat, subname, err, cause_c, cause_i )
    call EndSub( subname )
  end subroutine SigmaDataCreate

  subroutine SigmaDataClose( sig_dat, err )
    !
    ! SIGDAT 型の変数の終了処理を行います.
    ! なお, 与えられた *sig_dat* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Deconstructor of "SIGDAT".
    ! Note that if *sig_dat* is not initialized by "Create" yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(SIGDAT), intent(inout):: sig_dat
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'SigmaDataClose'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( .not. sig_dat % initialized ) then
      stat = DC_ENOTINIT
      cause_c = 'SIGDAT'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  "SIGDAT" の設定の消去
    !  Clear the settings for "SIGDAT"
    !-----------------------------------------------------------------
    deallocate( sig_dat % z_Sigma )
    deallocate( sig_dat % r_Sigma )
    deallocate( sig_dat % z_DelSigma )

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
    sig_dat % initialized = .false.
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine SigmaDataClose

  subroutine SigmaDataPutLine( sig_dat, unit, indent, err )
    !
    ! 引数 *sig_dat* に設定されている情報を印字します.
    ! デフォルトではメッセージは標準出力に出力されます. 
    ! *unit* に装置番号を指定することで, 出力先を変更することが可能です.
    !
    ! Print information of *sig_dat*.
    ! By default messages are output to standard output.
    ! Unit number for output can be changed by *unit* argument.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    use dc_string, only: Printf, PutLine
    implicit none
    type(SIGDAT), intent(in):: sig_dat
    integer, intent(in), optional:: unit
                              ! 出力先の装置番号.
                              ! デフォルトの出力先は標準出力.
                              !
                              ! Unit number for output.
                              ! Default value is standard output.
    character(*), intent(in), optional:: indent
                              ! 表示されるメッセージの字下げ.
                              !
                              ! Indent of displayed messages.
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: stat
    character(STRING):: cause_c
    integer:: out_unit
    integer:: indent_len
    character(STRING):: indent_str
    character(*), parameter:: subname = 'SigmaDataPutLine'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( present(unit) ) then
      out_unit = unit
    else
      out_unit = STDOUT
    end if

    indent_len = 0
    indent_str = ''
    if ( present(indent) ) then
      if ( len(indent) /= 0 ) then
        indent_len = len(indent)
        indent_str(1:indent_len) = indent
      end if
    end if


    !-----------------------------------------------------------------
    !  "SIGDAT" の設定の印字
    !  Print the settings for "SIGDAT"
    !-----------------------------------------------------------------
    if ( sig_dat % initialized ) then
      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & '#<SIGDAT:: @initialized=%y @pattern=%c @kmax=%d', &
        & i=(/sig_dat % kmax/), c1=trim(sig_dat % pattern), &
        & l=(/sig_dat % initialized/))

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & ' @Cp=%f @RAir=%f @Kappa=%f', &
        & d=(/sig_dat % Cp, sig_dat % RAir, sig_dat % Kappa/))

      call PutLine( sig_dat % z_Sigma, unit = out_unit, &
        & lbounds = lbound(sig_dat % z_Sigma), &
        & ubounds = ubound(sig_dat % z_Sigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @z_Sigma=' )

      call PutLine( sig_dat % r_Sigma, unit = out_unit, &
        & lbounds = lbound(sig_dat % r_Sigma), &
        & ubounds = ubound(sig_dat % r_Sigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @r_Sigma=' )

      call PutLine( sig_dat % z_DelSigma, unit = out_unit, &
        & lbounds = lbound(sig_dat % z_DelSigma), &
        & ubounds = ubound(sig_dat % z_DelSigma), &
        & indent = indent_str(1:indent_len) // &
        & ' @z_DelSigma=' )

      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & '>' )
    else
      call Printf(out_unit, &
        & indent_str(1:indent_len) // &
        & '#<SIGDAT:: @initialized=%y>', &
        & l=(/sig_dat % initialized/))
    end if

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine SigmaDataPutLine

  logical function SigmaDataInitialized( sig_dat ) result(result)
    !
    ! *sig_dat* が初期設定されている場合には .true. が,
    ! 初期設定されていない場合には .false. が返ります.
    !
    ! If *sig_dat* is initialized, .true. is returned.
    ! If *sig_dat* is not initialized, .false. is returned.
    !
    implicit none
    type(SIGDAT), intent(in):: sig_dat
  continue
    result = sig_dat % initialized
  end function SigmaDataInitialized

  subroutine SigmaDataNmlRead( nmlfile, &
    & r_Sigma_, &
    & err )
    !
    ! NAMELIST ファイル *nmlfile* から値を入力するための
    ! 内部サブルーチンです. Create 内で呼び出されることを
    ! 想定しています.
    !
    ! 値が NAMELIST ファイル内で指定されていない場合には,
    ! 入力された値がそのまま返ります.
    !
    ! なお, *nmlfile* に空文字が与えられた場合, または
    ! 与えられた *nmlfile* を読み込むことができない場合, 
    ! プログラムはエラーを発生させます.
    !
    ! This is an internal subroutine to input values from 
    ! NAMELIST file *nmlfile*. This subroutine is expected to be
    ! called by "Create".
    !
    ! A value not specified in NAMELIST file is returned
    ! without change.
    !
    ! If *nmlfile* is empty, or *nmlfile* can not be read, 
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_iounit, only: FileOpen
    use dc_message, only: MessageNotify
    use dc_present, only: present_and_true
    use dc_error, only: DC_NOERR, DC_ENOFILEREAD
    use dcpam_error, only: StoreError, DCPAM_ENMLARRAYINSUFF
    implicit none
    character(*), intent(in):: nmlfile
                              ! NAMELIST ファイルの名称. 
                              ! NAMELIST file name
    real(DP), intent(inout):: r_Sigma_ (:)
    integer, parameter:: size_r_Sigma = 256
    real(DP):: r_Sigma (1:size_r_Sigma)
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

    namelist /sigma_data_nml/ &
      & r_Sigma
                              ! sigma_data モジュール用
                              ! NAMELIST 変数群名.
                              !
                              ! sigma_data#Create を使用する際に, 
                              ! オプショナル引数 *nmlfile* へ NAMELIST 
                              ! ファイル名を指定することで, そのファイルから
                              ! この NAMELIST 変数群を読み込みます.
                              !
                              ! NAMELIST group name for
                              ! "sigma_data" module.
                              ! 
                              ! If a NAMELIST filename is specified to 
                              ! an optional argument *nmlfile* 
                              ! when "sigma_data#Create" is used, 
                              ! this NAMELIST group is loaded from 
                              ! the file.

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: size_r_Sigma_
    real(DP), parameter:: invalid_value_r_Sigma = -999.0_DP
    integer:: stat
    character(STRING):: cause_c
    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号. 
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT. 
                              ! IOSTAT of NAMELIST read
    character(*), parameter:: subname = 'SigmaDataNmlRead'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  文字型引数を NAMELIST 変数群へ代入
    !  Substitute character arguments to NAMELIST group
    !-----------------------------------------------------------------

    !-----------------------------------------------------------------
    !  無効なデフォルト値を NAMELIST 変数群へ代入
    !  Substitute invalid default values to NAMELIST group
    !-----------------------------------------------------------------
    r_Sigma = invalid_value_r_Sigma

    !----------------------------------------------------------------
    !  NAMELIST ファイルのオープン
    !  Open NAMELIST file
    !----------------------------------------------------------------
    call FileOpen( unit = unit_nml, & ! (out)
      & file = nmlfile, mode = 'r', & ! (in)
      & err = err )                   ! (out)
    if ( present_and_true(err) ) then
      stat = DC_ENOFILEREAD
      cause_c = nmlfile
      goto 999
    end if


    !-----------------------------------------------------------------
    !  NAMELIST 変数群の取得
    !  Get NAMELIST group
    !-----------------------------------------------------------------
    read( unit = unit_nml, & ! (in)
      & nml = sigma_data_nml, iostat = iostat_nml ) ! (out)
    if ( iostat_nml == 0 ) then
      call MessageNotify( 'M', subname, &
        & 'NAMELIST group "%c" is loaded from "%c".', &
        & c1='sigma_data_nml', c2=trim(nmlfile) )
!      write(STDOUT, nml = sigma_data_nml)  ! Size of r_Sigma is too large.
    else
      call MessageNotify( 'W', subname, &
        & 'NAMELIST group "%c" is not found in "%c" (iostat=%d).', &
        & c1='sigma_data_nml', c2=trim(nmlfile), &
        & i=(/iostat_nml/) )
    end if

    close( unit_nml )

    if ( all( r_Sigma > - 1.0_DP ) ) then
      call MessageNotify( 'W', subname, &
        & 'Array size of "%c" from "%c" exceeds prepared size "%d". ' // &
        & 'Edit "%c" manually.', &
        & c1='r_Sigma', c2=trim(nmlfile), c3='size_r_Sigma', &
        & i=(/size_r_Sigma/) )
      stat = DCPAM_ENMLARRAYINSUFF
      cause_c = 'r_Sigma'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  NAMELIST 変数群を文字型引数へ代入
    !  Substitute NAMELIST group to character arguments
    !-----------------------------------------------------------------

    !-----------------------------------------------------------------
    !  NAMELIST 変数群を引数へ代入
    !  Substitute NAMELIST group to arguments
    !-----------------------------------------------------------------
    size_r_Sigma_ = size( r_Sigma_ )
    r_Sigma_ = r_Sigma(1:size_r_Sigma_)

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine SigmaDataNmlRead

  subroutine SigmaDataGet( sig_dat, &
    & z_Sigma, r_Sigma, z_DelSigma, &
    & err )
    !
    ! σ座標軸データを返します.
    !    
    ! なお, 与えられた *sig_dat* が Create によって初期設定
    ! されていない場合, プログラムはエラーを発生させます.
    !
    ! Return sigma coordinate data.
    !
    ! If *sig_dat* is not initialized by "Create" yet,
    ! error is occurred.
    !
    use dc_trace, only: BeginSub, EndSub
    use dc_types, only: DP, STRING, TOKEN, STDOUT
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    implicit none
    type(SIGDAT), intent(inout):: sig_dat
    real(DP), intent(out):: z_Sigma (0:sig_dat%kmax-1)
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
    real(DP), intent(out):: r_Sigma (0:sig_dat%kmax)
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
    real(DP), intent(out), optional:: z_DelSigma (0:sig_dat%kmax-1)
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)
    logical, intent(out), optional:: err
                              ! 例外処理用フラグ.
                              ! デフォルトでは, この手続き内でエラーが
                              ! 生じた場合, プログラムは強制終了します.
                              ! 引数 *err* が与えられる場合,
                              ! プログラムは強制終了せず, 代わりに
                              ! *err* に .true. が代入されます.
                              !
                              ! Exception handling flag. 
                              ! By default, when error occur in 
                              ! this procedure, the program aborts. 
                              ! If this *err* argument is given, 
                              ! .true. is substituted to *err* and 
                              ! the program does not abort. 

!!$    integer:: param_i
!!$    real(DP):: param_r
!!$    character(STRING):: param_c

    !-----------------------------------
    !  作業変数
    !  Work variables
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'SigmaDataGet'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    !-----------------------------------------------------------------
    !  初期設定のチェック
    !  Check initialization
    !-----------------------------------------------------------------
    if ( .not. sig_dat % initialized ) then
      stat = DC_ENOTINIT
      cause_c = 'SIGDAT'
      goto 999
    end if

    !-----------------------------------------------------------------
    !  $ \sigma $ (整数, 半整数) レベルの設定
    !  Configure full and half $ \sigma $ level data
    !-----------------------------------------------------------------
    z_Sigma = sig_dat % z_Sigma
    r_Sigma = sig_dat % r_Sigma

    !-----------------------------------------------------------------
    ! $ \Delta \sigma $ (整数) の設定
    ! Configure $ \Delta \sigma $ (Full)
    !-----------------------------------------------------------------
    if (present(z_DelSigma)) z_DelSigma = sig_dat % z_DelSigma

    !-----------------------------------------------------------------
    !  終了処理, 例外処理
    !  Termination and Exception handling
    !-----------------------------------------------------------------
999 continue
    call StoreError( stat, subname, err, cause_c )
    call EndSub( subname )
  end subroutine SigmaDataGet


!!$  subroutine SigmaDataSample( sig_dat, err )
!!$    !--
!!$    ! SigmaDataSample の要約を記述してください.
!!$    !++
!!$    ! なお, 与えられた *sig_dat* が Create によって初期設定
!!$    ! されていない場合, プログラムはエラーを発生させます.
!!$    !--
!!$    ! Describe brief of SigmaDataSample
!!$    !++
!!$    ! If *sig_dat* is not initialized by "Create" yet,
!!$    ! error is occurred.
!!$    !
!!$    use dc_trace, only: BeginSub, EndSub
!!$    use dc_types, only: DP, STRING, TOKEN, STDOUT
!!$    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
!!$    implicit none
!!$    type(SIGDAT), intent(inout):: sig_dat
!!$    logical, intent(out), optional:: err
!!$                              ! 例外処理用フラグ.
!!$                              ! デフォルトでは, この手続き内でエラーが
!!$                              ! 生じた場合, プログラムは強制終了します.
!!$                              ! 引数 *err* が与えられる場合,
!!$                              ! プログラムは強制終了せず, 代わりに
!!$                              ! *err* に .true. が代入されます.
!!$                              !
!!$                              ! Exception handling flag. 
!!$                              ! By default, when error occur in 
!!$                              ! this procedure, the program aborts. 
!!$                              ! If this *err* argument is given, 
!!$                              ! .true. is substituted to *err* and 
!!$                              ! the program does not abort. 
!!$
!!$!!$    integer:: param_i
!!$!!$    real(DP):: param_r
!!$!!$    character(STRING):: param_c
!!$
!!$    !-----------------------------------
!!$    !  作業変数
!!$    !  Work variables
!!$    integer:: stat
!!$    character(STRING):: cause_c
!!$    character(*), parameter:: subname = 'SigmaDataSample'
!!$  continue
!!$    call BeginSub( subname )
!!$    stat = DC_NOERR
!!$    cause_c = ''
!!$
!!$    !-----------------------------------------------------------------
!!$    !  初期設定のチェック
!!$    !  Check initialization
!!$    !-----------------------------------------------------------------
!!$    if ( .not. sig_dat % initialized ) then
!!$      stat = DC_ENOTINIT
!!$      cause_c = 'SIGDAT'
!!$      goto 999
!!$    end if
!!$
!!$    !-----------------------------------------------------------------
!!$    !  *sig_dat* に格納されている設定値の取り出し
!!$    !  Fetch setting values stored in *sig_dat*
!!$    !-----------------------------------------------------------------
!!$!!$    param_i = sig_dat % param_i
!!$!!$    param_r = sig_dat % param_r
!!$!!$    param_c = sig_dat % param_c
!!$
!!$
!!$    !-----------------------------------------------------------------
!!$    !  終了処理, 例外処理
!!$    !  Termination and Exception handling
!!$    !-----------------------------------------------------------------
!!$999 continue
!!$    call StoreError( stat, subname, err, cause_c )
!!$    call EndSub( subname )
!!$  end subroutine SigmaDataSample

end module sigma_data
